From efdb3f65b3b98538daac7413171108759ceab231 Mon Sep 17 00:00:00 2001 From: danielmcnally285 <144589379+danielmcnally285@users.noreply.github.com> Date: Mon, 23 Oct 2023 19:15:17 +0000 Subject: [PATCH] return first non null and non empty optional unit tests --- .../ReturnFirstNonEmptyOptionalUnitTest.java | 37 ++++++ ...eturnFirstNonNullLazyEvaluateUnitTest.java | 69 +++++++++++ .../ReturnFirstNonNullUnitTest.java | 108 ++++++++++++++++++ 3 files changed, 214 insertions(+) create mode 100644 core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/returnfirstnonempty/ReturnFirstNonEmptyOptionalUnitTest.java create mode 100644 core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/returnfirstnonnull/ReturnFirstNonNullLazyEvaluateUnitTest.java create mode 100644 core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/returnfirstnonnull/ReturnFirstNonNullUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/returnfirstnonempty/ReturnFirstNonEmptyOptionalUnitTest.java b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/returnfirstnonempty/ReturnFirstNonEmptyOptionalUnitTest.java new file mode 100644 index 0000000000..439cae6235 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/returnfirstnonempty/ReturnFirstNonEmptyOptionalUnitTest.java @@ -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> 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); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/returnfirstnonnull/ReturnFirstNonNullLazyEvaluateUnitTest.java b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/returnfirstnonnull/ReturnFirstNonNullLazyEvaluateUnitTest.java new file mode 100644 index 0000000000..ae8ca09ea1 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/returnfirstnonnull/ReturnFirstNonNullLazyEvaluateUnitTest.java @@ -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 = Stream + .>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); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/returnfirstnonnull/ReturnFirstNonNullUnitTest.java b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/returnfirstnonnull/ReturnFirstNonNullUnitTest.java new file mode 100644 index 0000000000..ddf3a131da --- /dev/null +++ b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/returnfirstnonnull/ReturnFirstNonNullUnitTest.java @@ -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 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 = objects + .stream() + .filter(o -> o != null) + .findFirst(); + + assertTrue(object.isPresent()); + assertEquals(objects.get(1), object.get()); + } + + @Test + public void givenListOfObjects_whenFilterIsMethodRefNullCheck_thenReturnFirstNonNull() { + Optional 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); + } +} \ No newline at end of file