From 0b8f6175feec7fa973d05311669f97f8824cf416 Mon Sep 17 00:00:00 2001 From: Ganesh Date: Wed, 2 May 2018 19:39:01 +0530 Subject: [PATCH] java 10 features (#4147) --- .../java10/Java10FeaturesUnitTest.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 core-java-10/src/test/java/com/baeldung/java10/Java10FeaturesUnitTest.java diff --git a/core-java-10/src/test/java/com/baeldung/java10/Java10FeaturesUnitTest.java b/core-java-10/src/test/java/com/baeldung/java10/Java10FeaturesUnitTest.java new file mode 100644 index 0000000000..60e55ef422 --- /dev/null +++ b/core-java-10/src/test/java/com/baeldung/java10/Java10FeaturesUnitTest.java @@ -0,0 +1,61 @@ +package com.baeldung.java10; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +import org.junit.Before; +import org.junit.Test; + +public class Java10FeaturesUnitTest { + + private List someIntList; + + @Before + public void setup() { + someIntList = new ArrayList<>(); + + someIntList.add(1); + someIntList.add(2); + someIntList.add(3); + } + + @Test + public void whenVarInitWithString_thenGetStringTypeVar() { + var message = "Hello, Java 10"; + assertTrue(message instanceof String); + } + + @Test + public void whenVarInitWithAnonymous_thenGetAnonymousType() { + var obj = new Object() {}; + assertFalse(obj.getClass().equals(Object.class)); + } + + @Test(expected = UnsupportedOperationException.class) + public void whenModifyCopyOfList_thenThrowsException() { + List copyList = List.copyOf(someIntList); + copyList.add(4); + } + + @Test(expected = UnsupportedOperationException.class) + public void whenModifyToUnmodifiableList_thenThrowsException() { + List evenList = someIntList.stream() + .filter(i -> i % 2 == 0) + .collect(Collectors.toUnmodifiableList()); + evenList.add(4); + } + + @Test + public void whenListContainsInteger_OrElseThrowReturnsInteger() { + Integer firstEven = someIntList.stream() + .filter(i -> i % 2 == 0) + .findFirst() + .orElseThrow(); + is(firstEven).equals(Integer.valueOf(2)); + } +}