return first non null and non empty optional unit tests

This commit is contained in:
danielmcnally285 2023-10-23 19:15:17 +00:00
parent c16eeea003
commit efdb3f65b3
3 changed files with 214 additions and 0 deletions

View File

@ -0,0 +1,37 @@
package returnfirstnonempty;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class ReturnFirstNonEmptyOptionalUnitTest {
private List<Optional<Object>> optionals;
@Before
public void init() {
optionals = Arrays.asList(
Optional.empty(),
Optional.of(new Object()),
Optional.empty()
);
}
@Test
public void givenListOfOptionals_thenReturnFirstNonEmpty() {
Object object = optionals
.stream()
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst();
assertTrue(object != null);
assertEquals(optionals.get(1), object);
}
}

View File

@ -0,0 +1,69 @@
package returnfirstnonull;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.apache.commons.lang3.ObjectUtils;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class ReturnFirstNonNullLazyEvaluateUnitTest {
private static Object METHOD_B_OBJECT = new Object();
private Object methodA() { return null; }
private Object methodB() { return METHOD_B_OBJECT; }
private Object methodC() { return null; }
@Test
public void givenChainOfMethods_thenLazilyEvaluateMethodsUntilFirstNonNull() {
Object object = methodA();
if(object == null)
object = methodB();
if(object == null)
object = methodC();
assertTrue(object != null);
assertEquals(METHOD_B_OBJECT, object);
}
@Test
public void givenChainOfMethods_whenUsingSupplierInterface_thenLazilyEvaluateMethodsUntilFirstNonNull() {
Optional<Object> object = Stream
.<Supplier<Object>>of(
this::methodA,
this::methodB,
this::methodC)
.map(Supplier::get)
.filter(Objects::nonNull)
.findFirst();
assertTrue(object.isPresent());
assertEquals(METHOD_B_OBJECT, object.get());
}
@Test
public void givenTwoObjects_whenUsingApacheCommonsLang3_thenReturnFirstNonNull() {
Object object1 = methodA();
Object object = ObjectUtils.getIfNull(object1, this::methodB);
assertTrue(object != null);
assertEquals(METHOD_B_OBJECT, object);
}
@Test
public void givenChainOfMethods_whenUsingApacheCommonsLang3_thenReturnFirstNonNull() {
Object object = ObjectUtils.getFirstNonNull(this::methodA, this::methodB, this::methodC);
assertTrue(object != null);
assertEquals(METHOD_B_OBJECT, object);
}
}

View File

@ -0,0 +1,108 @@
package returnfirstnonull;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.apache.commons.lang3.ObjectUtils;
import org.junit.Before;
import org.junit.Test;
import com.google.common.base.MoreObjects;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class ReturnFirstNonNullUnitTest {
private List<Object> objects;
@Before
public void init() {
objects = Arrays.asList(
null,
new Object(),
new Object()
);
}
@Test
public void givenListOfObjects_whenIteratingWithForLoop_thenReturnFirstNonNull() {
Object object = null;
for(int i = 0; i < objects.size(); i++) {
if(objects.get(i) != null) {
object = objects.get(i);
break;
}
}
assertTrue(object != null);
assertEquals(objects.get(1), object);
}
@Test
public void givenListOfObjects_whenIteratingWithEnhancedForLoop_thenReturnFirstNonNull() {
Object object = null;
for(Object o: objects) {
if(o != null) {
object = o;
break;
}
}
assertTrue(object != null);
assertEquals(objects.get(1), object);
}
@Test
public void givenListOfObjects_whenFilterIsLambdaNullCheck_thenReturnFirstNonNull() {
Optional<Object> object = objects
.stream()
.filter(o -> o != null)
.findFirst();
assertTrue(object.isPresent());
assertEquals(objects.get(1), object.get());
}
@Test
public void givenListOfObjects_whenFilterIsMethodRefNullCheck_thenReturnFirstNonNull() {
Optional<Object> object = objects
.stream()
.filter(Objects::nonNull)
.findFirst();
assertTrue(object.isPresent());
assertEquals(objects.get(1), object.get());
}
@Test
public void givenListOfObjects_whenUsingApacheCommonsLang3_thenReturnFirstNonNull() {
Object object = ObjectUtils.firstNonNull(objects.toArray());
assertTrue(object != null);
assertEquals(objects.get(1), object);
}
@Test
public void givenTwoObjects_whenUsingGoogleGuava_thenReturnFirstNonNull() {
Object object1 = null;
Object object2 = new Object();
Object object = MoreObjects.firstNonNull(object1, object2);
assertTrue(object != null);
assertEquals(object2, object);
}
@Test
public void givenListOfObjects_whenUsingGoogleGuava_thenReturnFirstNonNull() {
Object object = Iterables.find(objects, Predicates.notNull());
assertTrue(object != null);
assertEquals(objects.get(1), object);
}
}