From accb430f5e76d3223f3ee1fb00c92c008669c30f Mon Sep 17 00:00:00 2001 From: Mohamed Sanaulla Date: Thu, 10 Aug 2017 20:45:43 +0300 Subject: [PATCH] java 9 objects api sample (#2413) --- .../baeldung/java9/process/ServiceMain.java | 2 +- .../language/Java9ObjectsAPIUnitTest.java | 50 ++++++++++--------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/core-java-9/src/main/java/com/baeldung/java9/process/ServiceMain.java b/core-java-9/src/main/java/com/baeldung/java9/process/ServiceMain.java index 299f74e877..5dc00dba25 100644 --- a/core-java-9/src/main/java/com/baeldung/java9/process/ServiceMain.java +++ b/core-java-9/src/main/java/com/baeldung/java9/process/ServiceMain.java @@ -6,7 +6,7 @@ public class ServiceMain { public static void main(String[] args) throws InterruptedException { ProcessHandle thisProcess = ProcessHandle.current(); - long pid = thisProcess.getPid(); + long pid = thisProcess.pid(); Optional opArgs = Optional.ofNullable(args); String procName = opArgs.map(str -> str.length > 0 ? str[0] : null).orElse(System.getProperty("sun.java.command")); diff --git a/core-java-9/src/test/java/com/baeldung/java9/language/Java9ObjectsAPIUnitTest.java b/core-java-9/src/test/java/com/baeldung/java9/language/Java9ObjectsAPIUnitTest.java index a174e519ab..93abe4e185 100644 --- a/core-java-9/src/test/java/com/baeldung/java9/language/Java9ObjectsAPIUnitTest.java +++ b/core-java-9/src/test/java/com/baeldung/java9/language/Java9ObjectsAPIUnitTest.java @@ -10,7 +10,7 @@ import static org.hamcrest.Matchers.*; import static org.hamcrest.MatcherAssert.assertThat; public class Java9ObjectsAPIUnitTest { - + @Test public void givenNullObject_whenRequireNonNullElse_thenElse(){ assertThat(Objects.requireNonNullElse(null, Collections.EMPTY_LIST), @@ -23,6 +23,11 @@ public class Java9ObjectsAPIUnitTest { Collections.EMPTY_LIST), is(List.of("item1", "item2"))); } + @Test(expected = NullPointerException.class) + public void givenNull_whenRequireNonNullElse_thenException(){ + Objects.requireNonNullElse(null, null); + } + @Test public void givenObject_whenRequireNonNullElseGet_thenObject(){ assertThat(Objects.requireNonNullElseGet(null, List::of), @@ -32,40 +37,39 @@ public class Java9ObjectsAPIUnitTest { @Test public void givenNumber_whenInvokeCheckIndex_thenNumber(){ int length = 5; - assertThat(Objects.checkIndex(4, length), is(4)); - - try{ - Objects.checkIndex(5, length); - }catch(Exception ex){ - assertThat(ex, instanceOf(IndexOutOfBoundsException.class)); - } } + @Test(expected = IndexOutOfBoundsException.class) + public void givenOutOfRangeNumber_whenInvokeCheckIndex_thenException(){ + int length = 5; + Objects.checkIndex(5, length); + } + + @Test public void givenSubRange_whenCheckFromToIndex_thenNumber(){ int length = 6; - assertThat(Objects.checkFromToIndex(2,length,length), is(2)); - - try{ - Objects.checkFromToIndex(2,7,length); - }catch(Exception ex){ - assertThat(ex, instanceOf(IndexOutOfBoundsException.class)); - } } - @Test - public void giveSubRange_whenCheckFromIndexSize_thenNumber(){ + @Test(expected = IndexOutOfBoundsException.class) + public void givenInvalidSubRange_whenCheckFromToIndex_thenException(){ int length = 6; + Objects.checkFromToIndex(2,7,length); + } - assertThat(Objects.checkFromToIndex(2,5,length), is(2)); - try{ - Objects.checkFromToIndex(2,6,length); - }catch(Exception ex){ - assertThat(ex, instanceOf(IndexOutOfBoundsException.class)); - } + @Test + public void givenSubRange_whenCheckFromIndexSize_thenNumber(){ + int length = 6; + assertThat(Objects.checkFromIndexSize(2,3,length), is(2)); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void givenInvalidSubRange_whenCheckFromIndexSize_thenException(){ + int length = 6; + Objects.checkFromIndexSize(2, 6, length); }