├── .gitattributes ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── jdk10 ├── build.gradle └── src │ └── test │ └── java │ └── java10 │ ├── collections │ ├── ListTest.java │ ├── MapTest.java │ └── SetTest.java │ ├── optional │ └── OptionalTest.java │ ├── stream │ └── CollectorsTest.java │ └── var │ └── VarTest.java ├── jdk9 ├── build.gradle └── src │ └── test │ └── java │ └── java9 │ ├── collections │ ├── ListTest.java │ ├── MapTest.java │ └── SetTest.java │ ├── optional │ └── OptionalTest.java │ ├── process │ └── ProcessTest.java │ └── stream │ ├── CollectorsTest.java │ └── StreamTest.java └── settings.gradle /.gitattributes: -------------------------------------------------------------------------------- 1 | *.bat text eol=crlf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /build/ 3 | .idea 4 | /out/ 5 | jdk10/build 6 | jdk10/out 7 | jdk9/build 8 | jdk9/out 9 | 10 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 11 | !gradle-wrapper.jar 12 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | 3 | dist: trusty 4 | sudo: false 5 | 6 | jdk: 7 | - oraclejdk11 8 | - openjdk11 9 | 10 | script: 11 | # Display Gradle, Groovy, JVM and other versions 12 | - ./gradlew --version 13 | # Run all tests 14 | - ./gradlew test 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2018 Jonas Havers (http://jonas-havers.de) 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java 9 & 10 2 | 3 | [![Build Status](https://travis-ci.org/JonasHavers/javafeatures.svg?branch=master)](https://travis-ci.org/JonasHavers/javafeatures) 4 | 5 | The tests in this project demonstrate some features that were introduced in Java 9 and Java 10. 6 | 7 | As usual, the tests are in `src/test/java`. 8 | 9 | The project is based on: 10 | 11 | * Gradle 4.10.2 (wrapper) 12 | * JDK 11 13 | * JUnit 5.1 14 | 15 | Run the tests with: 16 | 17 | `./gradlew test` 18 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | group 'de.jonashavers' 2 | version '1.0-SNAPSHOT' 3 | 4 | apply plugin: 'java' 5 | 6 | repositories { 7 | mavenCentral() 8 | } 9 | 10 | test { 11 | dependsOn(':jdk9:test', ':jdk10:test') 12 | } 13 | 14 | allprojects { 15 | group 'de.jonashavers' 16 | version '1.0-SNAPSHOT' 17 | 18 | apply plugin: 'java' 19 | 20 | repositories { 21 | mavenCentral() 22 | } 23 | 24 | dependencies { 25 | testImplementation 'org.junit.jupiter:junit-jupiter-api:5.1.1' 26 | testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.1.1' 27 | } 28 | 29 | test { 30 | useJUnitPlatform() 31 | failFast = true 32 | testLogging { 33 | events 'PASSED', 'FAILED', 'SKIPPED' 34 | } 35 | afterSuite { desc, result -> 36 | if (!desc.parent) { 37 | println "\nTest result: ${result.resultType}" 38 | println "Test summary: ${result.testCount} tests completed, " + 39 | "${result.successfulTestCount} succeeded, " + 40 | "${result.failedTestCount} failed, " + 41 | "${result.skippedTestCount} skipped" 42 | } 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JonasHavers/javafeatures/6e3a487a463e426d5013e08d312ffe88f86f9abc/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.0-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /jdk10/build.gradle: -------------------------------------------------------------------------------- 1 | compileTestJava { 2 | sourceCompatibility = JavaVersion.VERSION_1_10 3 | targetCompatibility = JavaVersion.VERSION_1_10 4 | } -------------------------------------------------------------------------------- /jdk10/src/test/java/java10/collections/ListTest.java: -------------------------------------------------------------------------------- 1 | package java10.collections; 2 | 3 | import org.junit.jupiter.api.DisplayName; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | import static org.junit.jupiter.api.Assertions.assertThrows; 10 | 11 | @DisplayName("List") 12 | class ListTest { 13 | @Test 14 | void shouldCreateAnImmutableCopyOfAList() { 15 | // given 16 | List list = new ArrayList<>(); 17 | list.add("Java"); 18 | list.add("Kotlin"); 19 | list.add("Groovy"); 20 | // when 21 | List result = List.copyOf(list); 22 | // expect 23 | assertThrows(UnsupportedOperationException.class, () -> result.add("PHP")); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /jdk10/src/test/java/java10/collections/MapTest.java: -------------------------------------------------------------------------------- 1 | package java10.collections; 2 | 3 | import org.junit.jupiter.api.DisplayName; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | import static org.junit.jupiter.api.Assertions.assertThrows; 10 | 11 | @DisplayName("Map") 12 | class MapTest { 13 | @Test 14 | void shouldCreateAnImmutableCopyOfAMap() { 15 | // given 16 | Map map = new HashMap<>(); 17 | map.put("Java", "Expert"); 18 | map.put("Kotlin", "Intermediate"); 19 | map.put("Groovy", "Beginner"); 20 | // when 21 | Map result = Map.copyOf(map); 22 | // expect 23 | assertThrows(UnsupportedOperationException.class, () -> result.put("PHP", "Master")); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /jdk10/src/test/java/java10/collections/SetTest.java: -------------------------------------------------------------------------------- 1 | package java10.collections; 2 | 3 | import org.junit.jupiter.api.DisplayName; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.util.HashSet; 7 | import java.util.Set; 8 | 9 | import static org.junit.jupiter.api.Assertions.assertThrows; 10 | 11 | @DisplayName("Set") 12 | class SetTest { 13 | @Test 14 | void shouldCreateAnImmutableCopyOfASet() { 15 | // given 16 | Set set = new HashSet<>(); 17 | set.add("Java"); 18 | set.add("Kotlin"); 19 | set.add("Groovy"); 20 | // when 21 | Set result = Set.copyOf(set); 22 | // expect 23 | assertThrows(UnsupportedOperationException.class, () -> result.add("PHP")); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /jdk10/src/test/java/java10/optional/OptionalTest.java: -------------------------------------------------------------------------------- 1 | package java10.optional; 2 | 3 | import org.junit.jupiter.api.DisplayName; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.util.NoSuchElementException; 7 | import java.util.Optional; 8 | 9 | import static org.junit.jupiter.api.Assertions.assertThrows; 10 | 11 | @DisplayName("Optional") 12 | class OptionalTest { 13 | @Test 14 | void shouldThrowANoSuchElementException() { 15 | // given 16 | Optional optional = Optional.empty(); 17 | // expect 18 | assertThrows(NoSuchElementException.class, optional::orElseThrow); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /jdk10/src/test/java/java10/stream/CollectorsTest.java: -------------------------------------------------------------------------------- 1 | package java10.stream; 2 | 3 | import org.junit.jupiter.api.DisplayName; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.util.List; 7 | import java.util.Map; 8 | import java.util.Set; 9 | import java.util.stream.Collectors; 10 | import java.util.stream.Stream; 11 | 12 | import static org.junit.jupiter.api.Assertions.assertEquals; 13 | 14 | @DisplayName("Collectors") 15 | class CollectorsTest { 16 | 17 | @Test 18 | void shouldCollectToUnmodifiableList() { 19 | // given 20 | Stream stream = Stream.of(1, 2, 3, 4, 5); 21 | // when 22 | List result = stream.collect(Collectors.toUnmodifiableList()); 23 | // then 24 | assertEquals(List.of(1, 2, 3, 4, 5), result); 25 | } 26 | 27 | @Test 28 | void shouldCollectToUnmodifiableSet() { 29 | // given 30 | Stream stream = Stream.of(1, 2, 3, 4, 5); 31 | // when 32 | Set result = stream.collect(Collectors.toUnmodifiableSet()); 33 | // then 34 | assertEquals(Set.of(1, 2, 3, 4, 5), result); 35 | } 36 | 37 | @Test 38 | void shouldCollectToUnmodifiableMap() { 39 | // given 40 | Stream stream = Stream.of(1, 2, 3, 4, 5); 41 | // when 42 | Map> result = stream 43 | .collect(Collectors.toUnmodifiableMap( 44 | it -> (it % 2 == 0) ? "even" : "odd", 45 | List::of, 46 | this::mergeLists 47 | )); 48 | // then 49 | assertEquals(Map.ofEntries( 50 | Map.entry("even", List.of(2, 4)), 51 | Map.entry("odd", List.of(1, 3, 5)) 52 | ), result); 53 | } 54 | 55 | private List mergeLists(List oldList, List newList) { 56 | return List.of(oldList, newList).stream().flatMap(List::stream).collect(Collectors.toList()); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /jdk10/src/test/java/java10/var/VarTest.java: -------------------------------------------------------------------------------- 1 | package java10.var; 2 | 3 | import org.junit.jupiter.api.DisplayName; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.util.ArrayList; 7 | import java.util.Comparator; 8 | import java.util.List; 9 | import java.util.function.Function; 10 | 11 | import static org.junit.jupiter.api.Assertions.assertEquals; 12 | 13 | @DisplayName("var") 14 | class VarTest { 15 | 16 | @Test 17 | void shouldBeNoReservedKeywordForVariableNames() { 18 | var var = 42; 19 | } 20 | 21 | @Test 22 | void shouldInferLocalVariableTypeToImmutableListOfStrings() { 23 | // when 24 | var list = List.of("Value1", "Value2"); 25 | // then 26 | assertEquals("java.util.ImmutableCollections$List12", list.getClass().getName()); 27 | // and 28 | String firstItem = list.get(0); 29 | assertEquals("Value1", firstItem); 30 | } 31 | 32 | @Test 33 | void shouldInferLocalVariableTypeToArrayListOfObjects() { 34 | // when 35 | var list = new ArrayList<>(); 36 | list.add("Value1"); 37 | // then 38 | assertEquals("java.util.ArrayList", list.getClass().getName()); 39 | // and 40 | Object firstItem = list.get(0); 41 | assertEquals("Value1", firstItem); 42 | } 43 | 44 | @Test 45 | void shouldInferLocalVariableTypeToArrayListOfStrings() { 46 | // when 47 | var list = new ArrayList(); 48 | list.add("Value1"); 49 | // then 50 | assertEquals("java.util.ArrayList", list.getClass().getName()); 51 | // and 52 | String firstItem = list.get(0); 53 | assertEquals("Value1", firstItem); 54 | } 55 | 56 | @Test 57 | void shouldRequireAnExplicitTypeCastForLambdas() { 58 | // when 59 | var lambda = (Function) (String it) -> it; 60 | // then 61 | assertEquals("not recommended", lambda.apply("not recommended")); 62 | } 63 | 64 | @Test 65 | void shouldRequireAnExplicitTypeCastForMethodReferences() { 66 | // when 67 | var methodRef = (Comparator) Integer::compare; 68 | // then 69 | assertEquals(1, methodRef.compare(42, 21)); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /jdk9/build.gradle: -------------------------------------------------------------------------------- 1 | compileTestJava { 2 | sourceCompatibility = JavaVersion.VERSION_1_9 3 | targetCompatibility = JavaVersion.VERSION_1_9 4 | } 5 | -------------------------------------------------------------------------------- /jdk9/src/test/java/java9/collections/ListTest.java: -------------------------------------------------------------------------------- 1 | package java9.collections; 2 | 3 | import org.junit.jupiter.api.DisplayName; 4 | import org.junit.jupiter.api.Nested; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import java.util.List; 8 | 9 | import static org.junit.jupiter.api.Assertions.assertEquals; 10 | import static org.junit.jupiter.api.Assertions.assertThrows; 11 | 12 | @DisplayName("List") 13 | class ListTest { 14 | 15 | @Nested 16 | @DisplayName("of") 17 | class Of { 18 | 19 | @Test 20 | void shouldCreateAnImmutableList() { 21 | // given 22 | List list = List.of(); 23 | // expect 24 | assertThrows(UnsupportedOperationException.class, () -> list.add("Value")); 25 | } 26 | 27 | @Test 28 | void shouldThrowANullPointerExceptionWhenPassedNull() { 29 | // expect 30 | assertThrows(NullPointerException.class, () -> List.of((String) null)); 31 | } 32 | 33 | @Test 34 | void shouldCreateAnEmptyListOfTypeImmutableCollections$ListNWhenPassedNoParameters() { 35 | // when 36 | List list = List.of(); 37 | // then 38 | assertEquals("java.util.ImmutableCollections$ListN", list.getClass().getName()); 39 | } 40 | 41 | @Test 42 | void shouldCreateAListOfTypeImmutableCollections$List12() { 43 | // when 44 | List list = List.of("Value1"); 45 | // then 46 | assertEquals("java.util.ImmutableCollections$List12", list.getClass().getName()); 47 | } 48 | 49 | @Test 50 | void shouldCreateAListOfTypeImmutableCollections$ListN() { 51 | // when 52 | List list = List.of( 53 | "Value1", 54 | "Value2", 55 | "Value3" 56 | ); 57 | // then 58 | assertEquals("java.util.ImmutableCollections$ListN", list.getClass().getName()); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /jdk9/src/test/java/java9/collections/MapTest.java: -------------------------------------------------------------------------------- 1 | package java9.collections; 2 | 3 | import org.junit.jupiter.api.DisplayName; 4 | import org.junit.jupiter.api.Nested; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import java.util.Map; 8 | 9 | import static org.junit.jupiter.api.Assertions.assertEquals; 10 | import static org.junit.jupiter.api.Assertions.assertThrows; 11 | 12 | @DisplayName("Map") 13 | class MapTest { 14 | 15 | @Nested 16 | @DisplayName("of") 17 | class Of { 18 | 19 | @Test 20 | void shouldCreateAnImmutableMap() { 21 | // when 22 | Map map = Map.of(); 23 | // then 24 | assertThrows(UnsupportedOperationException.class, () -> map.put("Key", "Value")); 25 | } 26 | 27 | @Test 28 | void shouldCreateAMapOfTypeImmutableCollections$MapNWhenPassedNoParameters() { 29 | // when 30 | Map map = Map.of(); 31 | // then 32 | assertEquals("java.util.ImmutableCollections$MapN", map.getClass().getName()); 33 | } 34 | 35 | @Test 36 | void shouldCreateAMapOfTypeImmutableCollections$Map1() { 37 | // when 38 | Map map = Map.of("Key1", "Value1"); 39 | // then 40 | assertEquals("java.util.ImmutableCollections$Map1", map.getClass().getName()); 41 | } 42 | 43 | @Test 44 | void shouldCreateAMapOfTypeImmutableCollections$MapN() { 45 | // when 46 | Map map = Map.of( 47 | "Key1", "Value1", 48 | "Key2", "Value2", 49 | "Key3", "Value3", 50 | "Key4", "Value4", 51 | "Key5", "Value5", 52 | "Key6", "Value6", 53 | "Key7", "Value7", 54 | "Key8", "Value8", 55 | "Key9", "Value9", 56 | "Key10", "Value10" 57 | ); 58 | // then 59 | assertEquals("java.util.ImmutableCollections$MapN", map.getClass().getName()); 60 | } 61 | 62 | @Test 63 | void shouldThrowAnExceptionWhenPassedNullKey() { 64 | // expect 65 | assertThrows(NullPointerException.class, () -> Map.of(null, "Value")); 66 | } 67 | 68 | @Test 69 | void shouldThrowAnExceptionWhenPassedNullValue() { 70 | // expect 71 | assertThrows(NullPointerException.class, () -> Map.of("Key", null)); 72 | } 73 | } 74 | 75 | @Nested 76 | @DisplayName("ofEntries") 77 | class OfEntries { 78 | 79 | @Test 80 | void shouldCreateAMapOfTypeImmutableCollections$MapN() { 81 | // when 82 | Map map = Map.ofEntries( 83 | Map.entry("Key1", "Value1"), 84 | Map.entry("Key2", "Value2"), 85 | Map.entry("Key3", "Value3"), 86 | Map.entry("Key4", "Value4"), 87 | Map.entry("Key5", "Value5"), 88 | Map.entry("Key6", "Value6"), 89 | Map.entry("Key7", "Value7"), 90 | Map.entry("Key8", "Value8"), 91 | Map.entry("Key9", "Value9"), 92 | Map.entry("Key10", "Value10"), 93 | Map.entry("Key11", "Value11") 94 | ); 95 | // then 96 | assertEquals("java.util.ImmutableCollections$MapN", map.getClass().getName()); 97 | } 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /jdk9/src/test/java/java9/collections/SetTest.java: -------------------------------------------------------------------------------- 1 | package java9.collections; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.DisplayName; 5 | import org.junit.jupiter.api.Nested; 6 | import org.junit.jupiter.api.Test; 7 | 8 | import java.util.Set; 9 | 10 | import static org.junit.jupiter.api.Assertions.assertEquals; 11 | import static org.junit.jupiter.api.Assertions.assertThrows; 12 | 13 | @DisplayName("Set") 14 | class SetTest { 15 | 16 | @Nested 17 | @DisplayName("of") 18 | class Of { 19 | 20 | @Test 21 | void shouldCreateAnImmutableSet() { 22 | // given 23 | Set set = Set.of(); 24 | // expect 25 | Assertions.assertThrows(UnsupportedOperationException.class, () -> set.add("Value")); 26 | } 27 | 28 | @Test 29 | void shouldThrowANullPointerExceptionWhenPassedNull() { 30 | // expect 31 | assertThrows(NullPointerException.class, () -> Set.of((String) null)); 32 | } 33 | 34 | @Test 35 | void shouldCreateAnEmptySetOfTypeImmutableCollections$SetNWhenPassedNoParameters() { 36 | // when 37 | Set set = Set.of(); 38 | // then 39 | assertEquals("java.util.ImmutableCollections$SetN", set.getClass().getName()); 40 | } 41 | 42 | @Test 43 | void shouldCreateASetOfTypeImmutableCollections$Set12() { 44 | // when 45 | Set set = Set.of("Value1"); 46 | // then 47 | assertEquals("java.util.ImmutableCollections$Set12", set.getClass().getName()); 48 | } 49 | 50 | @Test 51 | void shouldCreateASetOfTypeImmutableCollections$SetN() { 52 | // when 53 | Set set = Set.of( 54 | "Value1", 55 | "Value2", 56 | "Value3" 57 | ); 58 | // then 59 | assertEquals("java.util.ImmutableCollections$SetN", set.getClass().getName()); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /jdk9/src/test/java/java9/optional/OptionalTest.java: -------------------------------------------------------------------------------- 1 | package java9.optional; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.DisplayName; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import java.util.List; 8 | import java.util.Optional; 9 | import java.util.stream.Collectors; 10 | import java.util.stream.Stream; 11 | 12 | @DisplayName("Optional") 13 | class OptionalTest { 14 | 15 | @Test 16 | void shouldReturnTheNotEmptyOptional() { 17 | // when 18 | Optional optional = Optional 19 | .empty() 20 | .or(() -> Optional.of("Value")); 21 | // then 22 | Assertions.assertEquals("Value", optional.get()); 23 | } 24 | 25 | @Test 26 | void shouldExecuteOneOfTheActions() { 27 | // given 28 | Optional optional = Optional.empty(); 29 | // expect 30 | optional.ifPresentOrElse( 31 | it -> Assertions.fail("present"), 32 | () -> { 33 | } 34 | ); 35 | } 36 | 37 | @Test 38 | void shouldConvertAnOptionalToAStream() { 39 | // given 40 | Stream> stream = Stream.of( 41 | Optional.of(42), 42 | Optional.empty(), 43 | Optional.of(23) 44 | ); 45 | // when 46 | Stream result = stream.flatMap(Optional::stream); 47 | // then 48 | Assertions.assertEquals(List.of(42, 23), result.collect(Collectors.toList())); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /jdk9/src/test/java/java9/process/ProcessTest.java: -------------------------------------------------------------------------------- 1 | package java9.process; 2 | 3 | import org.junit.jupiter.api.DisplayName; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertNotNull; 7 | import static org.junit.jupiter.api.Assertions.assertTrue; 8 | 9 | @DisplayName("Process") 10 | class ProcessTest { 11 | 12 | @Test 13 | void shouldProvideACurrentProcessRepresentation() { 14 | // given 15 | ProcessHandle process = ProcessHandle.current(); 16 | // expect 17 | assertNotNull(process); 18 | // and 19 | assertTrue(process.isAlive()); 20 | assertTrue(process.pid() > 0); 21 | } 22 | 23 | @Test 24 | void shouldProvideDetailsAboutTheCurrentProcess() { 25 | // given 26 | ProcessHandle process = ProcessHandle.current(); 27 | // when 28 | ProcessHandle.Info processInfo = process.info(); 29 | // then 30 | assertNotNull(processInfo); 31 | // and 32 | assertTrue(processInfo.command().isPresent()); 33 | assertTrue(processInfo.startInstant().isPresent()); 34 | assertTrue(processInfo.totalCpuDuration().isPresent()); 35 | assertTrue(processInfo.user().isPresent()); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /jdk9/src/test/java/java9/stream/CollectorsTest.java: -------------------------------------------------------------------------------- 1 | package java9.stream; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.DisplayName; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import java.util.Collection; 8 | import java.util.List; 9 | import java.util.stream.Collectors; 10 | import java.util.stream.Stream; 11 | 12 | @DisplayName("Collectors") 13 | class CollectorsTest { 14 | 15 | @Test 16 | void shouldCollectByFiltering() { 17 | // given 18 | Stream stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 19 | // when 20 | List result = stream 21 | .collect(Collectors.filtering( 22 | it -> it % 2 == 0, 23 | Collectors.toList() 24 | )); 25 | // then 26 | Assertions.assertEquals(List.of(2, 4, 6, 8, 10), result); 27 | } 28 | 29 | @Test 30 | void shouldCollectByFlatMapping() { 31 | // given 32 | Stream> stream = Stream.of(List.of(1, 2, 3, 4, 5), List.of(6, 7, 8, 9, 10)); 33 | // when 34 | List result = stream 35 | .collect(Collectors.flatMapping( 36 | Collection::stream, 37 | Collectors.toList() 38 | )); 39 | // then 40 | Assertions.assertEquals(List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), result); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /jdk9/src/test/java/java9/stream/StreamTest.java: -------------------------------------------------------------------------------- 1 | package java9.stream; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.DisplayName; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import java.util.List; 8 | import java.util.stream.Collectors; 9 | import java.util.stream.Stream; 10 | 11 | @DisplayName("Stream") 12 | class StreamTest { 13 | 14 | @Test 15 | void shouldTakeWhile() { 16 | // given 17 | Stream stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 18 | // when 19 | Stream result = stream.takeWhile(it -> it <= 5); 20 | // then 21 | Assertions.assertEquals(List.of(1, 2, 3, 4, 5), result.collect(Collectors.toList())); 22 | } 23 | 24 | @Test 25 | void shouldDropWhile() { 26 | // given 27 | Stream stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 28 | // when 29 | Stream result = stream.dropWhile(it -> it <= 5); 30 | // then 31 | Assertions.assertEquals(List.of(6, 7, 8, 9, 10), result.collect(Collectors.toList())); 32 | } 33 | 34 | @Test 35 | void shouldIterate() { 36 | // when 37 | Stream stream = Stream.iterate(0, it -> it <= 5, it -> it + 1); 38 | // then 39 | Assertions.assertEquals(List.of(0, 1, 2, 3, 4, 5), stream.collect(Collectors.toList())); 40 | } 41 | 42 | @Test 43 | void shouldCreateAStreamWithOneElement() { 44 | // when 45 | Stream stream = Stream.ofNullable(5); 46 | // then 47 | Assertions.assertEquals(List.of(5), stream.collect(Collectors.toList())); 48 | } 49 | 50 | @Test 51 | void shouldCreateAnEmptyStream() { 52 | // when 53 | Stream stream = Stream.ofNullable(null); 54 | // then 55 | Assertions.assertEquals(List.of(), stream.collect(Collectors.toList())); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'javafeatures' 2 | include 'jdk9' 3 | include 'jdk10' 4 | 5 | --------------------------------------------------------------------------------