java 9 objects api sample (#2413)
This commit is contained in:
parent
c3de3dbfa9
commit
accb430f5e
|
@ -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"));
|
||||||
|
|
|
@ -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{
|
@Test(expected = IndexOutOfBoundsException.class)
|
||||||
|
public void givenOutOfRangeNumber_whenInvokeCheckIndex_thenException(){
|
||||||
|
int length = 5;
|
||||||
Objects.checkIndex(5, length);
|
Objects.checkIndex(5, length);
|
||||||
}catch(Exception ex){
|
|
||||||
assertThat(ex, instanceOf(IndexOutOfBoundsException.class));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@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{
|
@Test(expected = IndexOutOfBoundsException.class)
|
||||||
|
public void givenInvalidSubRange_whenCheckFromToIndex_thenException(){
|
||||||
|
int length = 6;
|
||||||
Objects.checkFromToIndex(2,7,length);
|
Objects.checkFromToIndex(2,7,length);
|
||||||
}catch(Exception ex){
|
|
||||||
assertThat(ex, instanceOf(IndexOutOfBoundsException.class));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void giveSubRange_whenCheckFromIndexSize_thenNumber(){
|
public void givenSubRange_whenCheckFromIndexSize_thenNumber(){
|
||||||
int length = 6;
|
int length = 6;
|
||||||
|
assertThat(Objects.checkFromIndexSize(2,3,length), is(2));
|
||||||
assertThat(Objects.checkFromToIndex(2,5,length), is(2));
|
|
||||||
|
|
||||||
try{
|
|
||||||
Objects.checkFromToIndex(2,6,length);
|
|
||||||
}catch(Exception ex){
|
|
||||||
assertThat(ex, instanceOf(IndexOutOfBoundsException.class));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expected = IndexOutOfBoundsException.class)
|
||||||
|
public void givenInvalidSubRange_whenCheckFromIndexSize_thenException(){
|
||||||
|
int length = 6;
|
||||||
|
Objects.checkFromIndexSize(2, 6, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue