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 {
ProcessHandle thisProcess = ProcessHandle.current();
long pid = thisProcess.getPid();
long pid = thisProcess.pid();
Optional<String[]> opArgs = Optional.ofNullable(args);
String procName = opArgs.map(str -> str.length > 0 ? str[0] : null).orElse(System.getProperty("sun.java.command"));

View File

@ -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.<List>requireNonNullElse(null, null);
}
@Test
public void givenObject_whenRequireNonNullElseGet_thenObject(){
assertThat(Objects.<List>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{
@Test(expected = IndexOutOfBoundsException.class)
public void givenOutOfRangeNumber_whenInvokeCheckIndex_thenException(){
int length = 5;
Objects.checkIndex(5, length);
}catch(Exception ex){
assertThat(ex, instanceOf(IndexOutOfBoundsException.class));
}
}
@Test
public void givenSubRange_whenCheckFromToIndex_thenNumber(){
int length = 6;
assertThat(Objects.checkFromToIndex(2,length,length), is(2));
}
try{
@Test(expected = IndexOutOfBoundsException.class)
public void givenInvalidSubRange_whenCheckFromToIndex_thenException(){
int length = 6;
Objects.checkFromToIndex(2,7,length);
}catch(Exception ex){
assertThat(ex, instanceOf(IndexOutOfBoundsException.class));
}
}
@Test
public void giveSubRange_whenCheckFromIndexSize_thenNumber(){
public void givenSubRange_whenCheckFromIndexSize_thenNumber(){
int length = 6;
assertThat(Objects.checkFromToIndex(2,5,length), is(2));
try{
Objects.checkFromToIndex(2,6,length);
}catch(Exception ex){
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);
}