java 9 objects api sample (#2413)

This commit is contained in:
Mohamed Sanaulla 2017-08-10 20:45:43 +03:00 committed by maibin
parent c3de3dbfa9
commit accb430f5e
2 changed files with 28 additions and 24 deletions

View File

@ -6,7 +6,7 @@ public class ServiceMain {
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws InterruptedException {
ProcessHandle thisProcess = ProcessHandle.current(); ProcessHandle thisProcess = ProcessHandle.current();
long pid = thisProcess.getPid(); long pid = thisProcess.pid();
Optional<String[]> opArgs = Optional.ofNullable(args); Optional<String[]> opArgs = Optional.ofNullable(args);
String procName = opArgs.map(str -> str.length > 0 ? str[0] : null).orElse(System.getProperty("sun.java.command")); String procName = opArgs.map(str -> str.length > 0 ? str[0] : null).orElse(System.getProperty("sun.java.command"));

View File

@ -10,7 +10,7 @@ import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
public class Java9ObjectsAPIUnitTest { public class Java9ObjectsAPIUnitTest {
@Test @Test
public void givenNullObject_whenRequireNonNullElse_thenElse(){ public void givenNullObject_whenRequireNonNullElse_thenElse(){
assertThat(Objects.<List>requireNonNullElse(null, Collections.EMPTY_LIST), assertThat(Objects.<List>requireNonNullElse(null, Collections.EMPTY_LIST),
@ -23,6 +23,11 @@ public class Java9ObjectsAPIUnitTest {
Collections.EMPTY_LIST), is(List.of("item1", "item2"))); Collections.EMPTY_LIST), is(List.of("item1", "item2")));
} }
@Test(expected = NullPointerException.class)
public void givenNull_whenRequireNonNullElse_thenException(){
Objects.<List>requireNonNullElse(null, null);
}
@Test @Test
public void givenObject_whenRequireNonNullElseGet_thenObject(){ public void givenObject_whenRequireNonNullElseGet_thenObject(){
assertThat(Objects.<List>requireNonNullElseGet(null, List::of), assertThat(Objects.<List>requireNonNullElseGet(null, List::of),
@ -32,40 +37,39 @@ public class Java9ObjectsAPIUnitTest {
@Test @Test
public void givenNumber_whenInvokeCheckIndex_thenNumber(){ public void givenNumber_whenInvokeCheckIndex_thenNumber(){
int length = 5; int length = 5;
assertThat(Objects.checkIndex(4, length), is(4)); 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 @Test
public void givenSubRange_whenCheckFromToIndex_thenNumber(){ public void givenSubRange_whenCheckFromToIndex_thenNumber(){
int length = 6; int length = 6;
assertThat(Objects.checkFromToIndex(2,length,length), is(2)); assertThat(Objects.checkFromToIndex(2,length,length), is(2));
try{
Objects.checkFromToIndex(2,7,length);
}catch(Exception ex){
assertThat(ex, instanceOf(IndexOutOfBoundsException.class));
}
} }
@Test @Test(expected = IndexOutOfBoundsException.class)
public void giveSubRange_whenCheckFromIndexSize_thenNumber(){ public void givenInvalidSubRange_whenCheckFromToIndex_thenException(){
int length = 6; int length = 6;
Objects.checkFromToIndex(2,7,length);
}
assertThat(Objects.checkFromToIndex(2,5,length), is(2));
try{ @Test
Objects.checkFromToIndex(2,6,length); public void givenSubRange_whenCheckFromIndexSize_thenNumber(){
}catch(Exception ex){ int length = 6;
assertThat(ex, instanceOf(IndexOutOfBoundsException.class)); 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);
} }