Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
79e1085b21
|
@ -19,6 +19,7 @@
|
||||||
.idea/
|
.idea/
|
||||||
*.iml
|
*.iml
|
||||||
*.iws
|
*.iws
|
||||||
|
out/
|
||||||
|
|
||||||
# Mac
|
# Mac
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
@ -27,6 +28,9 @@
|
||||||
log/
|
log/
|
||||||
target/
|
target/
|
||||||
|
|
||||||
|
# Gradle
|
||||||
|
.gradle/
|
||||||
|
|
||||||
spring-openid/src/main/resources/application.properties
|
spring-openid/src/main/resources/application.properties
|
||||||
.recommenders/
|
.recommenders/
|
||||||
/spring-hibernate4/nbproject/
|
/spring-hibernate4/nbproject/
|
||||||
|
@ -72,4 +76,5 @@ persistence-modules/hibernate5/transaction.log
|
||||||
apache-avro/src/main/java/com/baeldung/avro/model/
|
apache-avro/src/main/java/com/baeldung/avro/model/
|
||||||
jta/transaction-logs/
|
jta/transaction-logs/
|
||||||
software-security/sql-injection-samples/derby.log
|
software-security/sql-injection-samples/derby.log
|
||||||
spring-soap/src/main/java/com/baeldung/springsoap/gen/
|
spring-soap/src/main/java/com/baeldung/springsoap/gen/
|
||||||
|
/report-*.json
|
|
@ -14,7 +14,5 @@
|
||||||
- [Calculate Factorial in Java](https://www.baeldung.com/java-calculate-factorial)
|
- [Calculate Factorial in Java](https://www.baeldung.com/java-calculate-factorial)
|
||||||
- [Find Substrings That Are Palindromes in Java](https://www.baeldung.com/java-palindrome-substrings)
|
- [Find Substrings That Are Palindromes in Java](https://www.baeldung.com/java-palindrome-substrings)
|
||||||
- [Find the Longest Substring without Repeating Characters](https://www.baeldung.com/java-longest-substring-without-repeated-characters)
|
- [Find the Longest Substring without Repeating Characters](https://www.baeldung.com/java-longest-substring-without-repeated-characters)
|
||||||
- [Java Two Pointer Technique](https://www.baeldung.com/java-two-pointer-technique)
|
|
||||||
- [Permutations of an Array in Java](https://www.baeldung.com/java-array-permutations)
|
- [Permutations of an Array in Java](https://www.baeldung.com/java-array-permutations)
|
||||||
- [Implementing Simple State Machines with Java Enums](https://www.baeldung.com/java-enum-simple-state-machine)
|
|
||||||
- [Generate Combinations in Java](https://www.baeldung.com/java-combinations-algorithm)
|
- [Generate Combinations in Java](https://www.baeldung.com/java-combinations-algorithm)
|
||||||
|
|
|
@ -8,9 +8,6 @@
|
||||||
- [Create a Sudoku Solver in Java](http://www.baeldung.com/java-sudoku)
|
- [Create a Sudoku Solver in Java](http://www.baeldung.com/java-sudoku)
|
||||||
- [Displaying Money Amounts in Words](http://www.baeldung.com/java-money-into-words)
|
- [Displaying Money Amounts in Words](http://www.baeldung.com/java-money-into-words)
|
||||||
- [A Collaborative Filtering Recommendation System in Java](http://www.baeldung.com/java-collaborative-filtering-recommendations)
|
- [A Collaborative Filtering Recommendation System in Java](http://www.baeldung.com/java-collaborative-filtering-recommendations)
|
||||||
- [Converting Between Roman and Arabic Numerals in Java](http://www.baeldung.com/java-convert-roman-arabic)
|
|
||||||
- [Practical Java Examples of the Big O Notation](http://www.baeldung.com/java-algorithm-complexity)
|
|
||||||
- [An Introduction to the Theory of Big-O Notation](http://www.baeldung.com/big-o-notation)
|
|
||||||
- [Check If Two Rectangles Overlap In Java](https://www.baeldung.com/java-check-if-two-rectangles-overlap)
|
- [Check If Two Rectangles Overlap In Java](https://www.baeldung.com/java-check-if-two-rectangles-overlap)
|
||||||
- [Calculate the Distance Between Two Points in Java](https://www.baeldung.com/java-distance-between-two-points)
|
- [Calculate the Distance Between Two Points in Java](https://www.baeldung.com/java-distance-between-two-points)
|
||||||
- [Find the Intersection of Two Lines in Java](https://www.baeldung.com/java-intersection-of-two-lines)
|
- [Find the Intersection of Two Lines in Java](https://www.baeldung.com/java-intersection-of-two-lines)
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
/target/
|
||||||
|
.settings/
|
||||||
|
.classpath
|
||||||
|
.project
|
|
@ -0,0 +1,7 @@
|
||||||
|
## Relevant articles:
|
||||||
|
|
||||||
|
- [Java Two Pointer Technique](https://www.baeldung.com/java-two-pointer-technique)
|
||||||
|
- [Implementing Simple State Machines with Java Enums](https://www.baeldung.com/java-enum-simple-state-machine)
|
||||||
|
- [Converting Between Roman and Arabic Numerals in Java](http://www.baeldung.com/java-convert-roman-arabic)
|
||||||
|
- [Practical Java Examples of the Big O Notation](http://www.baeldung.com/java-algorithm-complexity)
|
||||||
|
- [An Introduction to the Theory of Big-O Notation](http://www.baeldung.com/big-o-notation)
|
|
@ -0,0 +1,39 @@
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>algorithms-miscellaneous-3</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>algorithms-miscellaneous-3</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>${org.assertj.core.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<pluginManagement>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>exec-maven-plugin</artifactId>
|
||||||
|
<version>${exec-maven-plugin.version}</version>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</pluginManagement>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
|
@ -60,7 +60,6 @@
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
<junit.version>4.10</junit.version>
|
|
||||||
<meecrowave-junit.version>1.2.0</meecrowave-junit.version>
|
<meecrowave-junit.version>1.2.0</meecrowave-junit.version>
|
||||||
<okhttp.version>3.10.0</okhttp.version>
|
<okhttp.version>3.10.0</okhttp.version>
|
||||||
<meecrowave-jpa.version>1.2.1</meecrowave-jpa.version>
|
<meecrowave-jpa.version>1.2.1</meecrowave-jpa.version>
|
||||||
|
|
|
@ -59,7 +59,6 @@
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<jstl.version>1.2</jstl.version>
|
|
||||||
<org.apache.httpcomponents.version>4.5.2</org.apache.httpcomponents.version>
|
<org.apache.httpcomponents.version>4.5.2</org.apache.httpcomponents.version>
|
||||||
<velocity-version>1.7</velocity-version>
|
<velocity-version>1.7</velocity-version>
|
||||||
<velocity-tools-version>2.0</velocity-tools-version>
|
<velocity-tools-version>2.0</velocity-tools-version>
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
package com.baeldung.autofactory.provided;
|
package com.baeldung.autofactory.provided;
|
||||||
|
|
||||||
|
import com.baeldung.autofactory.model.Camera;
|
||||||
import com.google.auto.factory.AutoFactory;
|
import com.google.auto.factory.AutoFactory;
|
||||||
import com.google.auto.factory.Provided;
|
import com.google.auto.factory.Provided;
|
||||||
import javafx.scene.Camera;
|
|
||||||
|
|
||||||
import javax.inject.Provider;
|
import javax.inject.Provider;
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.hamcrest</groupId>
|
<groupId>org.hamcrest</groupId>
|
||||||
<artifactId>hamcrest-core</artifactId>
|
<artifactId>hamcrest-core</artifactId>
|
||||||
<version>${hamcrest-core.version}</version>
|
<version>${org.hamcrest.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -64,7 +64,6 @@
|
||||||
<cdi-api.version>2.0.SP1</cdi-api.version>
|
<cdi-api.version>2.0.SP1</cdi-api.version>
|
||||||
<weld-se-core.version>3.0.5.Final</weld-se-core.version>
|
<weld-se-core.version>3.0.5.Final</weld-se-core.version>
|
||||||
<aspectjweaver.version>1.9.2</aspectjweaver.version>
|
<aspectjweaver.version>1.9.2</aspectjweaver.version>
|
||||||
<hamcrest-core.version>1.3</hamcrest-core.version>
|
|
||||||
<assertj-core.version>3.10.0</assertj-core.version>
|
<assertj-core.version>3.10.0</assertj-core.version>
|
||||||
<spring.version>5.1.2.RELEASE</spring.version>
|
<spring.version>5.1.2.RELEASE</spring.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
### Revelant Articles
|
||||||
|
|
||||||
|
- [A Quick Guide To Using Cloud Foundry UAA](https://www.baeldung.com/cloud-foundry-uaa)
|
|
@ -34,8 +34,5 @@
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
|
||||||
<java.version>1.8</java.version>
|
|
||||||
</properties>
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -35,8 +35,4 @@
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
|
||||||
<java.version>1.8</java.version>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
# Groovy
|
||||||
|
|
||||||
|
## Relevant articles:
|
||||||
|
|
||||||
|
- [String Matching in Groovy](http://www.baeldung.com/)
|
||||||
|
- [Groovy def Keyword]
|
||||||
|
|
|
@ -0,0 +1,131 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>core-groovy-2</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<name>core-groovy-2</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.groovy</groupId>
|
||||||
|
<artifactId>groovy</artifactId>
|
||||||
|
<version>${groovy.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.groovy</groupId>
|
||||||
|
<artifactId>groovy-all</artifactId>
|
||||||
|
<version>${groovy-all.version}</version>
|
||||||
|
<type>pom</type>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.groovy</groupId>
|
||||||
|
<artifactId>groovy-dateutil</artifactId>
|
||||||
|
<version>${groovy.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.groovy</groupId>
|
||||||
|
<artifactId>groovy-sql</artifactId>
|
||||||
|
<version>${groovy-sql.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.platform</groupId>
|
||||||
|
<artifactId>junit-platform-runner</artifactId>
|
||||||
|
<version>${junit.platform.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hsqldb</groupId>
|
||||||
|
<artifactId>hsqldb</artifactId>
|
||||||
|
<version>${hsqldb.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.spockframework</groupId>
|
||||||
|
<artifactId>spock-core</artifactId>
|
||||||
|
<version>${spock-core.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.gmavenplus</groupId>
|
||||||
|
<artifactId>gmavenplus-plugin</artifactId>
|
||||||
|
<version>${gmavenplus-plugin.version}</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>addSources</goal>
|
||||||
|
<goal>addTestSources</goal>
|
||||||
|
<goal>compile</goal>
|
||||||
|
<goal>compileTests</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-failsafe-plugin</artifactId>
|
||||||
|
<version>${maven-failsafe-plugin.version}</version>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.platform</groupId>
|
||||||
|
<artifactId>junit-platform-surefire-provider</artifactId>
|
||||||
|
<version>${junit.platform.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>junit5</id>
|
||||||
|
<goals>
|
||||||
|
<goal>integration-test</goal>
|
||||||
|
<goal>verify</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<includes>
|
||||||
|
<include>**/*Test5.java</include>
|
||||||
|
</includes>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>2.20.1</version>
|
||||||
|
<configuration>
|
||||||
|
<useFile>false</useFile>
|
||||||
|
<includes>
|
||||||
|
<include>**/*Test.java</include>
|
||||||
|
<include>**/*Spec.java</include>
|
||||||
|
</includes>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>central</id>
|
||||||
|
<url>http://jcenter.bintray.com</url>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<junit.platform.version>1.0.0</junit.platform.version>
|
||||||
|
<groovy.version>2.5.6</groovy.version>
|
||||||
|
<groovy-all.version>2.5.6</groovy-all.version>
|
||||||
|
<groovy-sql.version>2.5.6</groovy-sql.version>
|
||||||
|
<hsqldb.version>2.4.0</hsqldb.version>
|
||||||
|
<spock-core.version>1.1-groovy-2.4</spock-core.version>
|
||||||
|
<gmavenplus-plugin.version>1.6</gmavenplus-plugin.version>
|
||||||
|
</properties>
|
||||||
|
</project>
|
||||||
|
|
|
@ -0,0 +1,79 @@
|
||||||
|
package com.baeldung.defkeyword
|
||||||
|
|
||||||
|
import org.codehaus.groovy.runtime.NullObject
|
||||||
|
import org.codehaus.groovy.runtime.typehandling.GroovyCastException
|
||||||
|
|
||||||
|
import groovy.transform.TypeChecked
|
||||||
|
import groovy.transform.TypeCheckingMode
|
||||||
|
|
||||||
|
@TypeChecked
|
||||||
|
class DefUnitTest extends GroovyTestCase {
|
||||||
|
|
||||||
|
def id
|
||||||
|
def firstName = "Samwell"
|
||||||
|
def listOfCountries = ['USA', 'UK', 'FRANCE', 'INDIA']
|
||||||
|
|
||||||
|
@TypeChecked(TypeCheckingMode.SKIP)
|
||||||
|
def multiply(x, y) {
|
||||||
|
return x*y
|
||||||
|
}
|
||||||
|
|
||||||
|
@TypeChecked(TypeCheckingMode.SKIP)
|
||||||
|
void testDefVariableDeclaration() {
|
||||||
|
|
||||||
|
def list
|
||||||
|
assert list.getClass() == org.codehaus.groovy.runtime.NullObject
|
||||||
|
assert list.is(null)
|
||||||
|
|
||||||
|
list = [1,2,4]
|
||||||
|
assert list instanceof ArrayList
|
||||||
|
}
|
||||||
|
|
||||||
|
@TypeChecked(TypeCheckingMode.SKIP)
|
||||||
|
void testTypeVariables() {
|
||||||
|
int rate = 200
|
||||||
|
try {
|
||||||
|
rate = [12] //GroovyCastException
|
||||||
|
rate = "nill" //GroovyCastException
|
||||||
|
} catch(GroovyCastException) {
|
||||||
|
println "Cannot assign anything other than integer"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@TypeChecked(TypeCheckingMode.SKIP)
|
||||||
|
void testDefVariableMultipleAssignment() {
|
||||||
|
def rate
|
||||||
|
assert rate == null
|
||||||
|
assert rate.getClass() == org.codehaus.groovy.runtime.NullObject
|
||||||
|
|
||||||
|
rate = 12
|
||||||
|
assert rate instanceof Integer
|
||||||
|
|
||||||
|
rate = "Not Available"
|
||||||
|
assert rate instanceof String
|
||||||
|
|
||||||
|
rate = [1, 4]
|
||||||
|
assert rate instanceof List
|
||||||
|
|
||||||
|
assert divide(12, 3) instanceof BigDecimal
|
||||||
|
assert divide(1, 0) instanceof String
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
def divide(int x, int y) {
|
||||||
|
if(y==0) {
|
||||||
|
return "Should not divide by 0"
|
||||||
|
} else {
|
||||||
|
return x/y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def greetMsg() {
|
||||||
|
println "Hello! I am Groovy"
|
||||||
|
}
|
||||||
|
|
||||||
|
void testDefVsType() {
|
||||||
|
def int count
|
||||||
|
assert count instanceof Integer
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
package com.baeldung.strings
|
||||||
|
|
||||||
|
import spock.lang.Specification
|
||||||
|
|
||||||
|
import java.util.regex.Pattern
|
||||||
|
|
||||||
|
class StringMatchingSpec extends Specification {
|
||||||
|
|
||||||
|
def "pattern operator example"() {
|
||||||
|
given: "a pattern"
|
||||||
|
def p = ~'foo'
|
||||||
|
|
||||||
|
expect:
|
||||||
|
p instanceof Pattern
|
||||||
|
|
||||||
|
and: "you can use slash strings to avoid escaping of blackslash"
|
||||||
|
def digitPattern = ~/\d*/
|
||||||
|
digitPattern.matcher('4711').matches()
|
||||||
|
}
|
||||||
|
|
||||||
|
def "match operator example"() {
|
||||||
|
expect:
|
||||||
|
'foobar' ==~ /.*oba.*/
|
||||||
|
|
||||||
|
and: "matching is strict"
|
||||||
|
!('foobar' ==~ /foo/)
|
||||||
|
}
|
||||||
|
|
||||||
|
def "find operator example"() {
|
||||||
|
when: "using the find operator"
|
||||||
|
def matcher = 'foo and bar, baz and buz' =~ /(\w+) and (\w+)/
|
||||||
|
|
||||||
|
then: "will find groups"
|
||||||
|
matcher.size() == 2
|
||||||
|
|
||||||
|
and: "can access groups using array"
|
||||||
|
matcher[0][0] == 'foo and bar'
|
||||||
|
matcher[1][2] == 'buz'
|
||||||
|
|
||||||
|
and: "you can use it as a predicate"
|
||||||
|
'foobarbaz' =~ /bar/
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
# Groovy
|
||||||
|
|
||||||
|
## Relevant articles:
|
||||||
|
|
||||||
|
- [Maps in Groovy](http://www.baeldung.com/)
|
||||||
|
|
|
@ -0,0 +1,131 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>core-groovy-collections</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<name>core-groovy-collections</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.groovy</groupId>
|
||||||
|
<artifactId>groovy</artifactId>
|
||||||
|
<version>${groovy.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.groovy</groupId>
|
||||||
|
<artifactId>groovy-all</artifactId>
|
||||||
|
<version>${groovy-all.version}</version>
|
||||||
|
<type>pom</type>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.groovy</groupId>
|
||||||
|
<artifactId>groovy-dateutil</artifactId>
|
||||||
|
<version>${groovy.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.groovy</groupId>
|
||||||
|
<artifactId>groovy-sql</artifactId>
|
||||||
|
<version>${groovy-sql.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.platform</groupId>
|
||||||
|
<artifactId>junit-platform-runner</artifactId>
|
||||||
|
<version>${junit.platform.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hsqldb</groupId>
|
||||||
|
<artifactId>hsqldb</artifactId>
|
||||||
|
<version>${hsqldb.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.spockframework</groupId>
|
||||||
|
<artifactId>spock-core</artifactId>
|
||||||
|
<version>${spock-core.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.gmavenplus</groupId>
|
||||||
|
<artifactId>gmavenplus-plugin</artifactId>
|
||||||
|
<version>${gmavenplus-plugin.version}</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>addSources</goal>
|
||||||
|
<goal>addTestSources</goal>
|
||||||
|
<goal>compile</goal>
|
||||||
|
<goal>compileTests</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-failsafe-plugin</artifactId>
|
||||||
|
<version>${maven-failsafe-plugin.version}</version>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.platform</groupId>
|
||||||
|
<artifactId>junit-platform-surefire-provider</artifactId>
|
||||||
|
<version>${junit.platform.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>junit5</id>
|
||||||
|
<goals>
|
||||||
|
<goal>integration-test</goal>
|
||||||
|
<goal>verify</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<includes>
|
||||||
|
<include>**/*Test5.java</include>
|
||||||
|
</includes>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>2.20.1</version>
|
||||||
|
<configuration>
|
||||||
|
<useFile>false</useFile>
|
||||||
|
<includes>
|
||||||
|
<include>**/*Test.java</include>
|
||||||
|
<include>**/*Spec.java</include>
|
||||||
|
</includes>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>central</id>
|
||||||
|
<url>http://jcenter.bintray.com</url>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<junit.platform.version>1.0.0</junit.platform.version>
|
||||||
|
<groovy.version>2.5.6</groovy.version>
|
||||||
|
<groovy-all.version>2.5.6</groovy-all.version>
|
||||||
|
<groovy-sql.version>2.5.6</groovy-sql.version>
|
||||||
|
<hsqldb.version>2.4.0</hsqldb.version>
|
||||||
|
<spock-core.version>1.1-groovy-2.4</spock-core.version>
|
||||||
|
<gmavenplus-plugin.version>1.6</gmavenplus-plugin.version>
|
||||||
|
</properties>
|
||||||
|
</project>
|
||||||
|
|
|
@ -0,0 +1,148 @@
|
||||||
|
package com.baeldung.map;
|
||||||
|
|
||||||
|
import static groovy.test.GroovyAssert.*
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class MapTest{
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void createMap() {
|
||||||
|
|
||||||
|
def emptyMap = [:]
|
||||||
|
assertNotNull(emptyMap)
|
||||||
|
|
||||||
|
assertTrue(emptyMap instanceof java.util.LinkedHashMap)
|
||||||
|
|
||||||
|
def map = [name:"Jerry", age: 42, city: "New York"]
|
||||||
|
assertTrue(map.size() == 3)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void addItemsToMap() {
|
||||||
|
|
||||||
|
def map = [name:"Jerry"]
|
||||||
|
|
||||||
|
map["age"] = 42
|
||||||
|
|
||||||
|
map.city = "New York"
|
||||||
|
|
||||||
|
def hobbyLiteral = "hobby"
|
||||||
|
def hobbyMap = [(hobbyLiteral): "Singing"]
|
||||||
|
map.putAll(hobbyMap)
|
||||||
|
|
||||||
|
assertTrue(map == [name:"Jerry", age: 42, city: "New York", hobby:"Singing"])
|
||||||
|
assertTrue(hobbyMap.hobby == "Singing")
|
||||||
|
assertTrue(hobbyMap[hobbyLiteral] == "Singing")
|
||||||
|
|
||||||
|
map.plus([1:20]) // returns new map
|
||||||
|
|
||||||
|
map << [2:30]
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getItemsFromMap() {
|
||||||
|
|
||||||
|
def map = [name:"Jerry", age: 42, city: "New York", hobby:"Singing"]
|
||||||
|
|
||||||
|
assertTrue(map["name"] == "Jerry")
|
||||||
|
|
||||||
|
assertTrue(map.name == "Jerry")
|
||||||
|
|
||||||
|
def propertyAge = "age"
|
||||||
|
assertTrue(map[propertyAge] == 42)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void removeItemsFromMap() {
|
||||||
|
|
||||||
|
def map = [1:20, a:30, 2:42, 4:34, ba:67, 6:39, 7:49]
|
||||||
|
|
||||||
|
def minusMap = map.minus([2:42, 4:34]);
|
||||||
|
assertTrue(minusMap == [1:20, a:30, ba:67, 6:39, 7:49])
|
||||||
|
|
||||||
|
minusMap.removeAll{it -> it.key instanceof String}
|
||||||
|
assertTrue( minusMap == [ 1:20, 6:39, 7:49])
|
||||||
|
|
||||||
|
minusMap.retainAll{it -> it.value %2 == 0}
|
||||||
|
assertTrue( minusMap == [1:20])
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void iteratingOnMaps(){
|
||||||
|
def map = [name:"Jerry", age: 42, city: "New York", hobby:"Singing"]
|
||||||
|
|
||||||
|
map.each{ entry -> println "$entry.key: $entry.value" }
|
||||||
|
|
||||||
|
map.eachWithIndex{ entry, i -> println "$i $entry.key: $entry.value" }
|
||||||
|
|
||||||
|
map.eachWithIndex{ key, value, i -> println "$i $key: $value" }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void filteringAndSearchingMaps(){
|
||||||
|
def map = [name:"Jerry", age: 42, city: "New York", hobby:"Singing"]
|
||||||
|
|
||||||
|
assertTrue(map.find{ it.value == "New York"}.key == "city")
|
||||||
|
|
||||||
|
assertTrue(map.findAll{ it.value == "New York"} == [city : "New York"])
|
||||||
|
|
||||||
|
map.grep{it.value == "New York"}.each{ it -> assertTrue(it.key == "city" && it.value == "New York")}
|
||||||
|
|
||||||
|
assertTrue(map.every{it -> it.value instanceof String} == false)
|
||||||
|
|
||||||
|
assertTrue(map.any{it -> it.value instanceof String} == true)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void collect(){
|
||||||
|
|
||||||
|
def map = [1: [name:"Jerry", age: 42, city: "New York"],
|
||||||
|
2: [name:"Long", age: 25, city: "New York"],
|
||||||
|
3: [name:"Dustin", age: 29, city: "New York"],
|
||||||
|
4: [name:"Dustin", age: 34, city: "New York"]]
|
||||||
|
|
||||||
|
def names = map.collect{entry -> entry.value.name} // returns only list
|
||||||
|
assertTrue(names == ["Jerry", "Long", "Dustin", "Dustin"])
|
||||||
|
|
||||||
|
def uniqueNames = map.collect([] as HashSet){entry -> entry.value.name}
|
||||||
|
assertTrue(uniqueNames == ["Jerry", "Long", "Dustin"] as Set)
|
||||||
|
|
||||||
|
def idNames = map.collectEntries{key, value -> [key, value.name]}
|
||||||
|
assertTrue(idNames == [1:"Jerry", 2: "Long", 3:"Dustin", 4: "Dustin"])
|
||||||
|
|
||||||
|
def below30Names = map.findAll{it.value.age < 30}.collect{key, value -> value.name}
|
||||||
|
assertTrue(below30Names == ["Long", "Dustin"])
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void group(){
|
||||||
|
def map = [1:20, 2: 40, 3: 11, 4: 93]
|
||||||
|
|
||||||
|
def subMap = map.groupBy{it.value % 2}
|
||||||
|
println subMap
|
||||||
|
assertTrue(subMap == [0:[1:20, 2:40 ], 1:[3:11, 4:93]])
|
||||||
|
|
||||||
|
def keySubMap = map.subMap([1, 2])
|
||||||
|
assertTrue(keySubMap == [1:20, 2:40])
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void sorting(){
|
||||||
|
def map = [ab:20, a: 40, cb: 11, ba: 93]
|
||||||
|
|
||||||
|
def naturallyOrderedMap = map.sort()
|
||||||
|
assertTrue([a:40, ab:20, ba:93, cb:11] == naturallyOrderedMap)
|
||||||
|
|
||||||
|
def compSortedMap = map.sort({ k1, k2 -> k1 <=> k2 } as Comparator)
|
||||||
|
assertTrue([a:40, ab:20, ba:93, cb:11] == compSortedMap)
|
||||||
|
|
||||||
|
def cloSortedMap = map.sort({ it1, it2 -> it1.value <=> it1.value })
|
||||||
|
assertTrue([cb:11, ab:20, a:40, ba:93] == cloSortedMap)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
/src/main/resources/ioSerializedObject.txt
|
Binary file not shown.
|
@ -34,7 +34,9 @@ class DataAndObjectsUnitTest {
|
||||||
@Test
|
@Test
|
||||||
void whenUsingWithObjectOutputStream_thenObjectIsSerializedToFile() {
|
void whenUsingWithObjectOutputStream_thenObjectIsSerializedToFile() {
|
||||||
Task task = new Task(description:'Take out the trash', startDate:new Date(), status:0)
|
Task task = new Task(description:'Take out the trash', startDate:new Date(), status:0)
|
||||||
new File('src/main/resources/ioSerializedObject.txt').withObjectOutputStream { out ->
|
def serializedDataFile = new File('src/main/resources/ioSerializedObject.txt')
|
||||||
|
serializedDataFile.createNewFile()
|
||||||
|
serializedDataFile.withObjectOutputStream { out ->
|
||||||
out.writeObject(task)
|
out.writeObject(task)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,148 @@
|
||||||
|
package com.baeldung.groovy.map;
|
||||||
|
|
||||||
|
import static groovy.test.GroovyAssert.*
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class MapTest{
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void createMap() {
|
||||||
|
|
||||||
|
def emptyMap = [:]
|
||||||
|
assertNotNull(emptyMap)
|
||||||
|
|
||||||
|
assertTrue(emptyMap instanceof java.util.LinkedHashMap)
|
||||||
|
|
||||||
|
def map = [name:"Jerry", age: 42, city: "New York"]
|
||||||
|
assertTrue(map.size() == 3)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void addItemsToMap() {
|
||||||
|
|
||||||
|
def map = [name:"Jerry"]
|
||||||
|
|
||||||
|
map["age"] = 42
|
||||||
|
|
||||||
|
map.city = "New York"
|
||||||
|
|
||||||
|
def hobbyLiteral = "hobby"
|
||||||
|
def hobbyMap = [(hobbyLiteral): "Singing"]
|
||||||
|
map.putAll(hobbyMap)
|
||||||
|
|
||||||
|
assertTrue(map == [name:"Jerry", age: 42, city: "New York", hobby:"Singing"])
|
||||||
|
assertTrue(hobbyMap.hobby == "Singing")
|
||||||
|
assertTrue(hobbyMap[hobbyLiteral] == "Singing")
|
||||||
|
|
||||||
|
map.plus([1:20]) // returns new map
|
||||||
|
|
||||||
|
map << [2:30]
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getItemsFromMap() {
|
||||||
|
|
||||||
|
def map = [name:"Jerry", age: 42, city: "New York", hobby:"Singing"]
|
||||||
|
|
||||||
|
assertTrue(map["name"] == "Jerry")
|
||||||
|
|
||||||
|
assertTrue(map.name == "Jerry")
|
||||||
|
|
||||||
|
def propertyAge = "age"
|
||||||
|
assertTrue(map[propertyAge] == 42)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void removeItemsFromMap() {
|
||||||
|
|
||||||
|
def map = [1:20, a:30, 2:42, 4:34, ba:67, 6:39, 7:49]
|
||||||
|
|
||||||
|
def minusMap = map.minus([2:42, 4:34]);
|
||||||
|
assertTrue(minusMap == [1:20, a:30, ba:67, 6:39, 7:49])
|
||||||
|
|
||||||
|
minusMap.removeAll{it -> it.key instanceof String}
|
||||||
|
assertTrue( minusMap == [ 1:20, 6:39, 7:49])
|
||||||
|
|
||||||
|
minusMap.retainAll{it -> it.value %2 == 0}
|
||||||
|
assertTrue( minusMap == [1:20])
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void iteratingOnMaps(){
|
||||||
|
def map = [name:"Jerry", age: 42, city: "New York", hobby:"Singing"]
|
||||||
|
|
||||||
|
map.each{ entry -> println "$entry.key: $entry.value" }
|
||||||
|
|
||||||
|
map.eachWithIndex{ entry, i -> println "$i $entry.key: $entry.value" }
|
||||||
|
|
||||||
|
map.eachWithIndex{ key, value, i -> println "$i $key: $value" }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void filteringAndSearchingMaps(){
|
||||||
|
def map = [name:"Jerry", age: 42, city: "New York", hobby:"Singing"]
|
||||||
|
|
||||||
|
assertTrue(map.find{ it.value == "New York"}.key == "city")
|
||||||
|
|
||||||
|
assertTrue(map.findAll{ it.value == "New York"} == [city : "New York"])
|
||||||
|
|
||||||
|
map.grep{it.value == "New York"}.each{ it -> assertTrue(it.key == "city" && it.value == "New York")}
|
||||||
|
|
||||||
|
assertTrue(map.every{it -> it.value instanceof String} == false)
|
||||||
|
|
||||||
|
assertTrue(map.any{it -> it.value instanceof String} == true)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void collect(){
|
||||||
|
|
||||||
|
def map = [1: [name:"Jerry", age: 42, city: "New York"],
|
||||||
|
2: [name:"Long", age: 25, city: "New York"],
|
||||||
|
3: [name:"Dustin", age: 29, city: "New York"],
|
||||||
|
4: [name:"Dustin", age: 34, city: "New York"]]
|
||||||
|
|
||||||
|
def names = map.collect{entry -> entry.value.name} // returns only list
|
||||||
|
assertTrue(names == ["Jerry", "Long", "Dustin", "Dustin"])
|
||||||
|
|
||||||
|
def uniqueNames = map.collect([] as HashSet){entry -> entry.value.name}
|
||||||
|
assertTrue(uniqueNames == ["Jerry", "Long", "Dustin"] as Set)
|
||||||
|
|
||||||
|
def idNames = map.collectEntries{key, value -> [key, value.name]}
|
||||||
|
assertTrue(idNames == [1:"Jerry", 2: "Long", 3:"Dustin", 4: "Dustin"])
|
||||||
|
|
||||||
|
def below30Names = map.findAll{it.value.age < 30}.collect{key, value -> value.name}
|
||||||
|
assertTrue(below30Names == ["Long", "Dustin"])
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void group(){
|
||||||
|
def map = [1:20, 2: 40, 3: 11, 4: 93]
|
||||||
|
|
||||||
|
def subMap = map.groupBy{it.value % 2}
|
||||||
|
println subMap
|
||||||
|
assertTrue(subMap == [0:[1:20, 2:40 ], 1:[3:11, 4:93]])
|
||||||
|
|
||||||
|
def keySubMap = map.subMap([1, 2])
|
||||||
|
assertTrue(keySubMap == [1:20, 2:40])
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void sorting(){
|
||||||
|
def map = [ab:20, a: 40, cb: 11, ba: 93]
|
||||||
|
|
||||||
|
def naturallyOrderedMap = map.sort()
|
||||||
|
assertTrue([a:40, ab:20, ba:93, cb:11] == naturallyOrderedMap)
|
||||||
|
|
||||||
|
def compSortedMap = map.sort({ k1, k2 -> k1 <=> k2 } as Comparator)
|
||||||
|
assertTrue([a:40, ab:20, ba:93, cb:11] == compSortedMap)
|
||||||
|
|
||||||
|
def cloSortedMap = map.sort({ it1, it2 -> it1.value <=> it1.value })
|
||||||
|
assertTrue([cb:11, ab:20, a:40, ba:93] == cloSortedMap)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
package com.baeldung.strings
|
||||||
|
|
||||||
|
import spock.lang.Specification
|
||||||
|
|
||||||
|
import java.util.regex.Pattern
|
||||||
|
|
||||||
|
class StringMatchingSpec extends Specification {
|
||||||
|
|
||||||
|
def "pattern operator example"() {
|
||||||
|
given: "a pattern"
|
||||||
|
def p = ~'foo'
|
||||||
|
|
||||||
|
expect:
|
||||||
|
p instanceof Pattern
|
||||||
|
|
||||||
|
and: "you can use slash strings to avoid escaping of blackslash"
|
||||||
|
def digitPattern = ~/\d*/
|
||||||
|
digitPattern.matcher('4711').matches()
|
||||||
|
}
|
||||||
|
|
||||||
|
def "match operator example"() {
|
||||||
|
expect:
|
||||||
|
'foobar' ==~ /.*oba.*/
|
||||||
|
|
||||||
|
and: "matching is strict"
|
||||||
|
!('foobar' ==~ /foo/)
|
||||||
|
}
|
||||||
|
|
||||||
|
def "find operator example"() {
|
||||||
|
when: "using the find operator"
|
||||||
|
def matcher = 'foo and bar, baz and buz' =~ /(\w+) and (\w+)/
|
||||||
|
|
||||||
|
then: "will find groups"
|
||||||
|
matcher.size() == 2
|
||||||
|
|
||||||
|
and: "can access groups using array"
|
||||||
|
matcher[0][0] == 'foo and bar'
|
||||||
|
matcher[1][2] == 'buz'
|
||||||
|
|
||||||
|
and: "you can use it as a predicate"
|
||||||
|
'foobarbaz' =~ /bar/
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,18 +0,0 @@
|
||||||
package com.baeldung;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Unit test for simple App.
|
|
||||||
*/
|
|
||||||
public class UnitTest {
|
|
||||||
/**
|
|
||||||
* Stub test
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void givenPreconditions_whenCondition_shouldResult() {
|
|
||||||
assertTrue(true);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,6 +0,0 @@
|
||||||
=========
|
|
||||||
|
|
||||||
## Core Java Collections 2
|
|
||||||
|
|
||||||
### Relevant Articles:
|
|
||||||
- Java - Copying a HashMap
|
|
|
@ -1,78 +0,0 @@
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
<artifactId>core-java-collections-map</artifactId>
|
|
||||||
<version>0.1.0-SNAPSHOT</version>
|
|
||||||
<name>core-java-collections-map</name>
|
|
||||||
<packaging>jar</packaging>
|
|
||||||
|
|
||||||
<parent>
|
|
||||||
<groupId>com.baeldung</groupId>
|
|
||||||
<artifactId>parent-java</artifactId>
|
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
|
||||||
<relativePath>../parent-java</relativePath>
|
|
||||||
</parent>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.commons</groupId>
|
|
||||||
<artifactId>commons-collections4</artifactId>
|
|
||||||
<version>${commons-collections4.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.commons</groupId>
|
|
||||||
<artifactId>commons-lang3</artifactId>
|
|
||||||
<version>${commons-lang3.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.eclipse.collections</groupId>
|
|
||||||
<artifactId>eclipse-collections</artifactId>
|
|
||||||
<version>${eclipse.collections.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.assertj</groupId>
|
|
||||||
<artifactId>assertj-core</artifactId>
|
|
||||||
<version>${assertj.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.junit.platform</groupId>
|
|
||||||
<artifactId>junit-platform-runner</artifactId>
|
|
||||||
<version>${junit.platform.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.openjdk.jmh</groupId>
|
|
||||||
<artifactId>jmh-core</artifactId>
|
|
||||||
<version>${openjdk.jmh.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.openjdk.jmh</groupId>
|
|
||||||
<artifactId>jmh-generator-annprocess</artifactId>
|
|
||||||
<version>${openjdk.jmh.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.commons</groupId>
|
|
||||||
<artifactId>commons-exec</artifactId>
|
|
||||||
<version>${commons-exec.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.projectlombok</groupId>
|
|
||||||
<artifactId>lombok</artifactId>
|
|
||||||
<version>${lombok.version}</version>
|
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
<properties>
|
|
||||||
<openjdk.jmh.version>1.19</openjdk.jmh.version>
|
|
||||||
<junit.platform.version>1.2.0</junit.platform.version>
|
|
||||||
<commons-lang3.version>3.8.1</commons-lang3.version>
|
|
||||||
<commons-collections4.version>4.1</commons-collections4.version>
|
|
||||||
<collections-generic.version>4.01</collections-generic.version>
|
|
||||||
<avaitility.version>1.7.0</avaitility.version>
|
|
||||||
<assertj.version>3.11.1</assertj.version>
|
|
||||||
<eclipse.collections.version>7.1.0</eclipse.collections.version>
|
|
||||||
<commons-exec.version>1.3</commons-exec.version>
|
|
||||||
</properties>
|
|
||||||
</project>
|
|
|
@ -1,5 +0,0 @@
|
||||||
=========
|
|
||||||
|
|
||||||
## Core Java Lang OOP 2 Cookbooks and Examples
|
|
||||||
|
|
||||||
### Relevant Articles:
|
|
Binary file not shown.
|
@ -0,0 +1,3 @@
|
||||||
|
## Relevant articles:
|
||||||
|
|
||||||
|
- [Multi-Module Maven Application with Java Modules](https://www.baeldung.com/maven-multi-module-project-java-jpms)
|
|
@ -12,6 +12,7 @@
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-modules</artifactId>
|
<artifactId>parent-modules</artifactId>
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<relativePath>../../</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<build>
|
<build>
|
|
@ -12,6 +12,7 @@
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-modules</artifactId>
|
<artifactId>parent-modules</artifactId>
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<relativePath>../../</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
0
core-java-11/src/main/java/com/baeldung/add → core-java-modules/core-java-11/src/main/java/com/baeldung/add
Executable file → Normal file
0
core-java-11/src/main/java/com/baeldung/add → core-java-modules/core-java-11/src/main/java/com/baeldung/add
Executable file → Normal file
|
@ -1,132 +1,132 @@
|
||||||
/*
|
/*
|
||||||
* To change this license header, choose License Headers in Project Properties.
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
* To change this template file, choose Tools | Templates
|
* To change this template file, choose Tools | Templates
|
||||||
* and open the template in the editor.
|
* and open the template in the editor.
|
||||||
*/
|
*/
|
||||||
package com.baeldung.java11.httpclient;
|
package com.baeldung.java11.httpclient;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
import java.net.http.HttpClient.Version;
|
import java.net.http.HttpClient.Version;
|
||||||
import java.net.http.HttpRequest;
|
import java.net.http.HttpRequest;
|
||||||
import java.net.http.HttpRequest.BodyPublishers;
|
import java.net.http.HttpRequest.BodyPublishers;
|
||||||
import java.net.http.HttpResponse;
|
import java.net.http.HttpResponse;
|
||||||
import java.net.http.HttpResponse.BodyHandlers;
|
import java.net.http.HttpResponse.BodyHandlers;
|
||||||
import java.net.http.HttpResponse.PushPromiseHandler;
|
import java.net.http.HttpResponse.PushPromiseHandler;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class HttpClientExample {
|
public class HttpClientExample {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
httpGetRequest();
|
httpGetRequest();
|
||||||
httpPostRequest();
|
httpPostRequest();
|
||||||
asynchronousGetRequest();
|
asynchronousGetRequest();
|
||||||
asynchronousMultipleRequests();
|
asynchronousMultipleRequests();
|
||||||
pushRequest();
|
pushRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void httpGetRequest() throws URISyntaxException, IOException, InterruptedException {
|
public static void httpGetRequest() throws URISyntaxException, IOException, InterruptedException {
|
||||||
HttpClient client = HttpClient.newHttpClient();
|
HttpClient client = HttpClient.newHttpClient();
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.version(HttpClient.Version.HTTP_2)
|
.version(HttpClient.Version.HTTP_2)
|
||||||
.uri(URI.create("http://jsonplaceholder.typicode.com/posts/1"))
|
.uri(URI.create("http://jsonplaceholder.typicode.com/posts/1"))
|
||||||
.headers("Accept-Enconding", "gzip, deflate")
|
.headers("Accept-Enconding", "gzip, deflate")
|
||||||
.build();
|
.build();
|
||||||
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
|
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
|
||||||
|
|
||||||
String responseBody = response.body();
|
String responseBody = response.body();
|
||||||
int responseStatusCode = response.statusCode();
|
int responseStatusCode = response.statusCode();
|
||||||
|
|
||||||
System.out.println("httpGetRequest: " + responseBody);
|
System.out.println("httpGetRequest: " + responseBody);
|
||||||
System.out.println("httpGetRequest status code: " + responseStatusCode);
|
System.out.println("httpGetRequest status code: " + responseStatusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void httpPostRequest() throws URISyntaxException, IOException, InterruptedException {
|
public static void httpPostRequest() throws URISyntaxException, IOException, InterruptedException {
|
||||||
HttpClient client = HttpClient.newBuilder()
|
HttpClient client = HttpClient.newBuilder()
|
||||||
.version(HttpClient.Version.HTTP_2)
|
.version(HttpClient.Version.HTTP_2)
|
||||||
.build();
|
.build();
|
||||||
HttpRequest request = HttpRequest.newBuilder(new URI("http://jsonplaceholder.typicode.com/posts"))
|
HttpRequest request = HttpRequest.newBuilder(new URI("http://jsonplaceholder.typicode.com/posts"))
|
||||||
.version(HttpClient.Version.HTTP_2)
|
.version(HttpClient.Version.HTTP_2)
|
||||||
.POST(BodyPublishers.ofString("Sample Post Request"))
|
.POST(BodyPublishers.ofString("Sample Post Request"))
|
||||||
.build();
|
.build();
|
||||||
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
|
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
|
||||||
String responseBody = response.body();
|
String responseBody = response.body();
|
||||||
System.out.println("httpPostRequest : " + responseBody);
|
System.out.println("httpPostRequest : " + responseBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void asynchronousGetRequest() throws URISyntaxException {
|
public static void asynchronousGetRequest() throws URISyntaxException {
|
||||||
HttpClient client = HttpClient.newHttpClient();
|
HttpClient client = HttpClient.newHttpClient();
|
||||||
URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1");
|
URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1");
|
||||||
HttpRequest request = HttpRequest.newBuilder(httpURI)
|
HttpRequest request = HttpRequest.newBuilder(httpURI)
|
||||||
.version(HttpClient.Version.HTTP_2)
|
.version(HttpClient.Version.HTTP_2)
|
||||||
.build();
|
.build();
|
||||||
CompletableFuture<Void> futureResponse = client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
|
CompletableFuture<Void> futureResponse = client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
|
||||||
.thenAccept(resp -> {
|
.thenAccept(resp -> {
|
||||||
System.out.println("Got pushed response " + resp.uri());
|
System.out.println("Got pushed response " + resp.uri());
|
||||||
System.out.println("Response statuscode: " + resp.statusCode());
|
System.out.println("Response statuscode: " + resp.statusCode());
|
||||||
System.out.println("Response body: " + resp.body());
|
System.out.println("Response body: " + resp.body());
|
||||||
});
|
});
|
||||||
System.out.println("futureResponse" + futureResponse);
|
System.out.println("futureResponse" + futureResponse);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void asynchronousMultipleRequests() throws URISyntaxException {
|
public static void asynchronousMultipleRequests() throws URISyntaxException {
|
||||||
HttpClient client = HttpClient.newHttpClient();
|
HttpClient client = HttpClient.newHttpClient();
|
||||||
List<URI> uris = Arrays.asList(new URI("http://jsonplaceholder.typicode.com/posts/1"), new URI("http://jsonplaceholder.typicode.com/posts/2"));
|
List<URI> uris = Arrays.asList(new URI("http://jsonplaceholder.typicode.com/posts/1"), new URI("http://jsonplaceholder.typicode.com/posts/2"));
|
||||||
List<HttpRequest> requests = uris.stream()
|
List<HttpRequest> requests = uris.stream()
|
||||||
.map(HttpRequest::newBuilder)
|
.map(HttpRequest::newBuilder)
|
||||||
.map(reqBuilder -> reqBuilder.build())
|
.map(reqBuilder -> reqBuilder.build())
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
System.out.println("Got pushed response1 " + requests);
|
System.out.println("Got pushed response1 " + requests);
|
||||||
CompletableFuture.allOf(requests.stream()
|
CompletableFuture.allOf(requests.stream()
|
||||||
.map(request -> client.sendAsync(request, BodyHandlers.ofString()))
|
.map(request -> client.sendAsync(request, BodyHandlers.ofString()))
|
||||||
.toArray(CompletableFuture<?>[]::new))
|
.toArray(CompletableFuture<?>[]::new))
|
||||||
.thenAccept(System.out::println)
|
.thenAccept(System.out::println)
|
||||||
.join();
|
.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void pushRequest() throws URISyntaxException, InterruptedException {
|
public static void pushRequest() throws URISyntaxException, InterruptedException {
|
||||||
System.out.println("Running HTTP/2 Server Push example...");
|
System.out.println("Running HTTP/2 Server Push example...");
|
||||||
|
|
||||||
HttpClient httpClient = HttpClient.newBuilder()
|
HttpClient httpClient = HttpClient.newBuilder()
|
||||||
.version(Version.HTTP_2)
|
.version(Version.HTTP_2)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
HttpRequest pageRequest = HttpRequest.newBuilder()
|
HttpRequest pageRequest = HttpRequest.newBuilder()
|
||||||
.uri(URI.create("https://http2.golang.org/serverpush"))
|
.uri(URI.create("https://http2.golang.org/serverpush"))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// Interface HttpResponse.PushPromiseHandler<T>
|
// Interface HttpResponse.PushPromiseHandler<T>
|
||||||
// void applyPushPromise(HttpRequest initiatingRequest, HttpRequest pushPromiseRequest, Function<HttpResponse.BodyHandler<T>,CompletableFuture<HttpResponse<T>>> acceptor)
|
// void applyPushPromise(HttpRequest initiatingRequest, HttpRequest pushPromiseRequest, Function<HttpResponse.BodyHandler<T>,CompletableFuture<HttpResponse<T>>> acceptor)
|
||||||
httpClient.sendAsync(pageRequest, BodyHandlers.ofString(), pushPromiseHandler())
|
httpClient.sendAsync(pageRequest, BodyHandlers.ofString(), pushPromiseHandler())
|
||||||
.thenAccept(pageResponse -> {
|
.thenAccept(pageResponse -> {
|
||||||
System.out.println("Page response status code: " + pageResponse.statusCode());
|
System.out.println("Page response status code: " + pageResponse.statusCode());
|
||||||
System.out.println("Page response headers: " + pageResponse.headers());
|
System.out.println("Page response headers: " + pageResponse.headers());
|
||||||
String responseBody = pageResponse.body();
|
String responseBody = pageResponse.body();
|
||||||
System.out.println(responseBody);
|
System.out.println(responseBody);
|
||||||
}).join();
|
}).join();
|
||||||
|
|
||||||
Thread.sleep(1000); // waiting for full response
|
Thread.sleep(1000); // waiting for full response
|
||||||
}
|
}
|
||||||
|
|
||||||
private static PushPromiseHandler<String> pushPromiseHandler() {
|
private static PushPromiseHandler<String> pushPromiseHandler() {
|
||||||
return (HttpRequest initiatingRequest,
|
return (HttpRequest initiatingRequest,
|
||||||
HttpRequest pushPromiseRequest,
|
HttpRequest pushPromiseRequest,
|
||||||
Function<HttpResponse.BodyHandler<String>,
|
Function<HttpResponse.BodyHandler<String>,
|
||||||
CompletableFuture<HttpResponse<String>>> acceptor) -> {
|
CompletableFuture<HttpResponse<String>>> acceptor) -> {
|
||||||
acceptor.apply(BodyHandlers.ofString())
|
acceptor.apply(BodyHandlers.ofString())
|
||||||
.thenAccept(resp -> {
|
.thenAccept(resp -> {
|
||||||
System.out.println(" Pushed response: " + resp.uri() + ", headers: " + resp.headers());
|
System.out.println(" Pushed response: " + resp.uri() + ", headers: " + resp.headers());
|
||||||
});
|
});
|
||||||
System.out.println("Promise request: " + pushPromiseRequest.uri());
|
System.out.println("Promise request: " + pushPromiseRequest.uri());
|
||||||
System.out.println("Promise request: " + pushPromiseRequest.headers());
|
System.out.println("Promise request: " + pushPromiseRequest.headers());
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,240 +1,240 @@
|
||||||
package com.baeldung.java11.httpclient.test;
|
package com.baeldung.java11.httpclient.test;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.containsString;
|
import static org.hamcrest.CoreMatchers.containsString;
|
||||||
import static org.hamcrest.CoreMatchers.equalTo;
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.Authenticator;
|
import java.net.Authenticator;
|
||||||
import java.net.CookieManager;
|
import java.net.CookieManager;
|
||||||
import java.net.CookiePolicy;
|
import java.net.CookiePolicy;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.PasswordAuthentication;
|
import java.net.PasswordAuthentication;
|
||||||
import java.net.ProxySelector;
|
import java.net.ProxySelector;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
import java.net.http.HttpRequest;
|
import java.net.http.HttpRequest;
|
||||||
import java.net.http.HttpResponse;
|
import java.net.http.HttpResponse;
|
||||||
import java.net.http.HttpResponse.BodyHandlers;
|
import java.net.http.HttpResponse.BodyHandlers;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.CompletionException;
|
import java.util.concurrent.CompletionException;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class HttpClientTest {
|
public class HttpClientTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldReturnSampleDataContentWhenConnectViaSystemProxy() throws IOException, InterruptedException, URISyntaxException {
|
public void shouldReturnSampleDataContentWhenConnectViaSystemProxy() throws IOException, InterruptedException, URISyntaxException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI("https://postman-echo.com/post"))
|
.uri(new URI("https://postman-echo.com/post"))
|
||||||
.headers("Content-Type", "text/plain;charset=UTF-8")
|
.headers("Content-Type", "text/plain;charset=UTF-8")
|
||||||
.POST(HttpRequest.BodyPublishers.ofString("Sample body"))
|
.POST(HttpRequest.BodyPublishers.ofString("Sample body"))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
|
||||||
HttpResponse<String> response = HttpClient.newBuilder()
|
HttpResponse<String> response = HttpClient.newBuilder()
|
||||||
.proxy(ProxySelector.getDefault())
|
.proxy(ProxySelector.getDefault())
|
||||||
.build()
|
.build()
|
||||||
.send(request, HttpResponse.BodyHandlers.ofString());
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
assertThat(response.body(), containsString("Sample body"));
|
assertThat(response.body(), containsString("Sample body"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldNotFollowRedirectWhenSetToDefaultNever() throws IOException, InterruptedException, URISyntaxException {
|
public void shouldNotFollowRedirectWhenSetToDefaultNever() throws IOException, InterruptedException, URISyntaxException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI("http://stackoverflow.com"))
|
.uri(new URI("http://stackoverflow.com"))
|
||||||
.version(HttpClient.Version.HTTP_1_1)
|
.version(HttpClient.Version.HTTP_1_1)
|
||||||
.GET()
|
.GET()
|
||||||
.build();
|
.build();
|
||||||
HttpResponse<String> response = HttpClient.newBuilder()
|
HttpResponse<String> response = HttpClient.newBuilder()
|
||||||
.build()
|
.build()
|
||||||
.send(request, HttpResponse.BodyHandlers.ofString());
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_MOVED_PERM));
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_MOVED_PERM));
|
||||||
assertThat(response.body(), containsString("https://stackoverflow.com/"));
|
assertThat(response.body(), containsString("https://stackoverflow.com/"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldFollowRedirectWhenSetToAlways() throws IOException, InterruptedException, URISyntaxException {
|
public void shouldFollowRedirectWhenSetToAlways() throws IOException, InterruptedException, URISyntaxException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI("http://stackoverflow.com"))
|
.uri(new URI("http://stackoverflow.com"))
|
||||||
.version(HttpClient.Version.HTTP_1_1)
|
.version(HttpClient.Version.HTTP_1_1)
|
||||||
.GET()
|
.GET()
|
||||||
.build();
|
.build();
|
||||||
HttpResponse<String> response = HttpClient.newBuilder()
|
HttpResponse<String> response = HttpClient.newBuilder()
|
||||||
.followRedirects(HttpClient.Redirect.ALWAYS)
|
.followRedirects(HttpClient.Redirect.ALWAYS)
|
||||||
.build()
|
.build()
|
||||||
.send(request, HttpResponse.BodyHandlers.ofString());
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
assertThat(response.request()
|
assertThat(response.request()
|
||||||
.uri()
|
.uri()
|
||||||
.toString(), equalTo("https://stackoverflow.com/"));
|
.toString(), equalTo("https://stackoverflow.com/"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldReturnOKStatusForAuthenticatedAccess() throws URISyntaxException, IOException, InterruptedException {
|
public void shouldReturnOKStatusForAuthenticatedAccess() throws URISyntaxException, IOException, InterruptedException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI("https://postman-echo.com/basic-auth"))
|
.uri(new URI("https://postman-echo.com/basic-auth"))
|
||||||
.GET()
|
.GET()
|
||||||
.build();
|
.build();
|
||||||
HttpResponse<String> response = HttpClient.newBuilder()
|
HttpResponse<String> response = HttpClient.newBuilder()
|
||||||
.authenticator(new Authenticator() {
|
.authenticator(new Authenticator() {
|
||||||
@Override
|
@Override
|
||||||
protected PasswordAuthentication getPasswordAuthentication() {
|
protected PasswordAuthentication getPasswordAuthentication() {
|
||||||
return new PasswordAuthentication("postman", "password".toCharArray());
|
return new PasswordAuthentication("postman", "password".toCharArray());
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.build()
|
.build()
|
||||||
.send(request, HttpResponse.BodyHandlers.ofString());
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldSendRequestAsync() throws URISyntaxException, InterruptedException, ExecutionException {
|
public void shouldSendRequestAsync() throws URISyntaxException, InterruptedException, ExecutionException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI("https://postman-echo.com/post"))
|
.uri(new URI("https://postman-echo.com/post"))
|
||||||
.headers("Content-Type", "text/plain;charset=UTF-8")
|
.headers("Content-Type", "text/plain;charset=UTF-8")
|
||||||
.POST(HttpRequest.BodyPublishers.ofString("Sample body"))
|
.POST(HttpRequest.BodyPublishers.ofString("Sample body"))
|
||||||
.build();
|
.build();
|
||||||
CompletableFuture<HttpResponse<String>> response = HttpClient.newBuilder()
|
CompletableFuture<HttpResponse<String>> response = HttpClient.newBuilder()
|
||||||
.build()
|
.build()
|
||||||
.sendAsync(request, HttpResponse.BodyHandlers.ofString());
|
.sendAsync(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
assertThat(response.get()
|
assertThat(response.get()
|
||||||
.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldUseJustTwoThreadWhenProcessingSendAsyncRequest() throws URISyntaxException, InterruptedException, ExecutionException {
|
public void shouldUseJustTwoThreadWhenProcessingSendAsyncRequest() throws URISyntaxException, InterruptedException, ExecutionException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI("https://postman-echo.com/get"))
|
.uri(new URI("https://postman-echo.com/get"))
|
||||||
.GET()
|
.GET()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
ExecutorService executorService = Executors.newFixedThreadPool(2);
|
ExecutorService executorService = Executors.newFixedThreadPool(2);
|
||||||
|
|
||||||
CompletableFuture<HttpResponse<String>> response1 = HttpClient.newBuilder()
|
CompletableFuture<HttpResponse<String>> response1 = HttpClient.newBuilder()
|
||||||
.executor(executorService)
|
.executor(executorService)
|
||||||
.build()
|
.build()
|
||||||
.sendAsync(request, HttpResponse.BodyHandlers.ofString());
|
.sendAsync(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
CompletableFuture<HttpResponse<String>> response2 = HttpClient.newBuilder()
|
CompletableFuture<HttpResponse<String>> response2 = HttpClient.newBuilder()
|
||||||
.executor(executorService)
|
.executor(executorService)
|
||||||
.build()
|
.build()
|
||||||
.sendAsync(request, HttpResponse.BodyHandlers.ofString());
|
.sendAsync(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
CompletableFuture<HttpResponse<String>> response3 = HttpClient.newBuilder()
|
CompletableFuture<HttpResponse<String>> response3 = HttpClient.newBuilder()
|
||||||
.executor(executorService)
|
.executor(executorService)
|
||||||
.build()
|
.build()
|
||||||
.sendAsync(request, HttpResponse.BodyHandlers.ofString());
|
.sendAsync(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
CompletableFuture.allOf(response1, response2, response3)
|
CompletableFuture.allOf(response1, response2, response3)
|
||||||
.join();
|
.join();
|
||||||
|
|
||||||
assertThat(response1.get()
|
assertThat(response1.get()
|
||||||
.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
assertThat(response2.get()
|
assertThat(response2.get()
|
||||||
.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
assertThat(response3.get()
|
assertThat(response3.get()
|
||||||
.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldNotStoreCookieWhenPolicyAcceptNone() throws URISyntaxException, IOException, InterruptedException {
|
public void shouldNotStoreCookieWhenPolicyAcceptNone() throws URISyntaxException, IOException, InterruptedException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI("https://postman-echo.com/get"))
|
.uri(new URI("https://postman-echo.com/get"))
|
||||||
.GET()
|
.GET()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
HttpClient httpClient = HttpClient.newBuilder()
|
HttpClient httpClient = HttpClient.newBuilder()
|
||||||
.cookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_NONE))
|
.cookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_NONE))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
assertTrue(httpClient.cookieHandler()
|
assertTrue(httpClient.cookieHandler()
|
||||||
.isPresent());
|
.isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldStoreCookieWhenPolicyAcceptAll() throws URISyntaxException, IOException, InterruptedException {
|
public void shouldStoreCookieWhenPolicyAcceptAll() throws URISyntaxException, IOException, InterruptedException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI("https://postman-echo.com/get"))
|
.uri(new URI("https://postman-echo.com/get"))
|
||||||
.GET()
|
.GET()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
HttpClient httpClient = HttpClient.newBuilder()
|
HttpClient httpClient = HttpClient.newBuilder()
|
||||||
.cookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_ALL))
|
.cookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_ALL))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
assertTrue(httpClient.cookieHandler()
|
assertTrue(httpClient.cookieHandler()
|
||||||
.isPresent());
|
.isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldProcessMultipleRequestViaStream() throws URISyntaxException, ExecutionException, InterruptedException {
|
public void shouldProcessMultipleRequestViaStream() throws URISyntaxException, ExecutionException, InterruptedException {
|
||||||
List<URI> targets = Arrays.asList(new URI("https://postman-echo.com/get?foo1=bar1"), new URI("https://postman-echo.com/get?foo2=bar2"));
|
List<URI> targets = Arrays.asList(new URI("https://postman-echo.com/get?foo1=bar1"), new URI("https://postman-echo.com/get?foo2=bar2"));
|
||||||
|
|
||||||
HttpClient client = HttpClient.newHttpClient();
|
HttpClient client = HttpClient.newHttpClient();
|
||||||
|
|
||||||
List<CompletableFuture<String>> futures = targets.stream()
|
List<CompletableFuture<String>> futures = targets.stream()
|
||||||
.map(target -> client.sendAsync(HttpRequest.newBuilder(target)
|
.map(target -> client.sendAsync(HttpRequest.newBuilder(target)
|
||||||
.GET()
|
.GET()
|
||||||
.build(), HttpResponse.BodyHandlers.ofString())
|
.build(), HttpResponse.BodyHandlers.ofString())
|
||||||
.thenApply(response -> response.body()))
|
.thenApply(response -> response.body()))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
|
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
|
||||||
.join();
|
.join();
|
||||||
|
|
||||||
if (futures.get(0)
|
if (futures.get(0)
|
||||||
.get()
|
.get()
|
||||||
.contains("foo1")) {
|
.contains("foo1")) {
|
||||||
assertThat(futures.get(0)
|
assertThat(futures.get(0)
|
||||||
.get(), containsString("bar1"));
|
.get(), containsString("bar1"));
|
||||||
assertThat(futures.get(1)
|
assertThat(futures.get(1)
|
||||||
.get(), containsString("bar2"));
|
.get(), containsString("bar2"));
|
||||||
} else {
|
} else {
|
||||||
assertThat(futures.get(1)
|
assertThat(futures.get(1)
|
||||||
.get(), containsString("bar2"));
|
.get(), containsString("bar2"));
|
||||||
assertThat(futures.get(1)
|
assertThat(futures.get(1)
|
||||||
.get(), containsString("bar1"));
|
.get(), containsString("bar1"));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void completeExceptionallyExample() {
|
public void completeExceptionallyExample() {
|
||||||
CompletableFuture<String> cf = CompletableFuture.completedFuture("message").thenApplyAsync(String::toUpperCase,
|
CompletableFuture<String> cf = CompletableFuture.completedFuture("message").thenApplyAsync(String::toUpperCase,
|
||||||
CompletableFuture.delayedExecutor(1, TimeUnit.SECONDS));
|
CompletableFuture.delayedExecutor(1, TimeUnit.SECONDS));
|
||||||
CompletableFuture<String> exceptionHandler = cf.handle((s, th) -> { return (th != null) ? "message upon cancel" : ""; });
|
CompletableFuture<String> exceptionHandler = cf.handle((s, th) -> { return (th != null) ? "message upon cancel" : ""; });
|
||||||
cf.completeExceptionally(new RuntimeException("completed exceptionally"));
|
cf.completeExceptionally(new RuntimeException("completed exceptionally"));
|
||||||
assertTrue("Was not completed exceptionally", cf.isCompletedExceptionally());
|
assertTrue("Was not completed exceptionally", cf.isCompletedExceptionally());
|
||||||
try {
|
try {
|
||||||
cf.join();
|
cf.join();
|
||||||
fail("Should have thrown an exception");
|
fail("Should have thrown an exception");
|
||||||
} catch (CompletionException ex) { // just for testing
|
} catch (CompletionException ex) { // just for testing
|
||||||
assertEquals("completed exceptionally", ex.getCause().getMessage());
|
assertEquals("completed exceptionally", ex.getCause().getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEquals("message upon cancel", exceptionHandler.join());
|
assertEquals("message upon cancel", exceptionHandler.join());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,168 +1,168 @@
|
||||||
package com.baeldung.java11.httpclient.test;
|
package com.baeldung.java11.httpclient.test;
|
||||||
|
|
||||||
import static java.time.temporal.ChronoUnit.SECONDS;
|
import static java.time.temporal.ChronoUnit.SECONDS;
|
||||||
import static org.hamcrest.CoreMatchers.containsString;
|
import static org.hamcrest.CoreMatchers.containsString;
|
||||||
import static org.hamcrest.CoreMatchers.equalTo;
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
import java.net.http.HttpRequest;
|
import java.net.http.HttpRequest;
|
||||||
import java.net.http.HttpResponse;
|
import java.net.http.HttpResponse;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class HttpRequestTest {
|
public class HttpRequestTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldReturnStatusOKWhenSendGetRequest() throws IOException, InterruptedException, URISyntaxException {
|
public void shouldReturnStatusOKWhenSendGetRequest() throws IOException, InterruptedException, URISyntaxException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI("https://postman-echo.com/get"))
|
.uri(new URI("https://postman-echo.com/get"))
|
||||||
.GET()
|
.GET()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||||
.send(request, HttpResponse.BodyHandlers.ofString());
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldUseHttp2WhenWebsiteUsesHttp2() throws IOException, InterruptedException, URISyntaxException {
|
public void shouldUseHttp2WhenWebsiteUsesHttp2() throws IOException, InterruptedException, URISyntaxException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI("https://stackoverflow.com"))
|
.uri(new URI("https://stackoverflow.com"))
|
||||||
.version(HttpClient.Version.HTTP_2)
|
.version(HttpClient.Version.HTTP_2)
|
||||||
.GET()
|
.GET()
|
||||||
.build();
|
.build();
|
||||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||||
.send(request, HttpResponse.BodyHandlers.ofString());
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
assertThat(response.version(), equalTo(HttpClient.Version.HTTP_2));
|
assertThat(response.version(), equalTo(HttpClient.Version.HTTP_2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldFallbackToHttp1_1WhenWebsiteDoesNotUseHttp2() throws IOException, InterruptedException, URISyntaxException, NoSuchAlgorithmException {
|
public void shouldFallbackToHttp1_1WhenWebsiteDoesNotUseHttp2() throws IOException, InterruptedException, URISyntaxException, NoSuchAlgorithmException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI("https://postman-echo.com/get"))
|
.uri(new URI("https://postman-echo.com/get"))
|
||||||
.version(HttpClient.Version.HTTP_2)
|
.version(HttpClient.Version.HTTP_2)
|
||||||
.GET()
|
.GET()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||||
.send(request, HttpResponse.BodyHandlers.ofString());
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
assertThat(response.version(), equalTo(HttpClient.Version.HTTP_1_1));
|
assertThat(response.version(), equalTo(HttpClient.Version.HTTP_1_1));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldReturnStatusOKWhenSendGetRequestWithDummyHeaders() throws IOException, InterruptedException, URISyntaxException {
|
public void shouldReturnStatusOKWhenSendGetRequestWithDummyHeaders() throws IOException, InterruptedException, URISyntaxException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI("https://postman-echo.com/get"))
|
.uri(new URI("https://postman-echo.com/get"))
|
||||||
.headers("key1", "value1", "key2", "value2")
|
.headers("key1", "value1", "key2", "value2")
|
||||||
.GET()
|
.GET()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||||
.send(request, HttpResponse.BodyHandlers.ofString());
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldReturnStatusOKWhenSendGetRequestTimeoutSet() throws IOException, InterruptedException, URISyntaxException {
|
public void shouldReturnStatusOKWhenSendGetRequestTimeoutSet() throws IOException, InterruptedException, URISyntaxException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI("https://postman-echo.com/get"))
|
.uri(new URI("https://postman-echo.com/get"))
|
||||||
.timeout(Duration.of(10, SECONDS))
|
.timeout(Duration.of(10, SECONDS))
|
||||||
.GET()
|
.GET()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||||
.send(request, HttpResponse.BodyHandlers.ofString());
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldReturnNoContentWhenPostWithNoBody() throws IOException, InterruptedException, URISyntaxException {
|
public void shouldReturnNoContentWhenPostWithNoBody() throws IOException, InterruptedException, URISyntaxException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI("https://postman-echo.com/post"))
|
.uri(new URI("https://postman-echo.com/post"))
|
||||||
.POST(HttpRequest.BodyPublishers.noBody())
|
.POST(HttpRequest.BodyPublishers.noBody())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||||
.send(request, HttpResponse.BodyHandlers.ofString());
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldReturnSampleDataContentWhenPostWithBodyText() throws IOException, InterruptedException, URISyntaxException {
|
public void shouldReturnSampleDataContentWhenPostWithBodyText() throws IOException, InterruptedException, URISyntaxException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI("https://postman-echo.com/post"))
|
.uri(new URI("https://postman-echo.com/post"))
|
||||||
.headers("Content-Type", "text/plain;charset=UTF-8")
|
.headers("Content-Type", "text/plain;charset=UTF-8")
|
||||||
.POST(HttpRequest.BodyPublishers.ofString("Sample request body"))
|
.POST(HttpRequest.BodyPublishers.ofString("Sample request body"))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||||
.send(request, HttpResponse.BodyHandlers.ofString());
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
assertThat(response.body(), containsString("Sample request body"));
|
assertThat(response.body(), containsString("Sample request body"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldReturnSampleDataContentWhenPostWithInputStream() throws IOException, InterruptedException, URISyntaxException {
|
public void shouldReturnSampleDataContentWhenPostWithInputStream() throws IOException, InterruptedException, URISyntaxException {
|
||||||
byte[] sampleData = "Sample request body".getBytes();
|
byte[] sampleData = "Sample request body".getBytes();
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI("https://postman-echo.com/post"))
|
.uri(new URI("https://postman-echo.com/post"))
|
||||||
.headers("Content-Type", "text/plain;charset=UTF-8")
|
.headers("Content-Type", "text/plain;charset=UTF-8")
|
||||||
.POST(HttpRequest.BodyPublishers.ofInputStream(() -> new ByteArrayInputStream(sampleData)))
|
.POST(HttpRequest.BodyPublishers.ofInputStream(() -> new ByteArrayInputStream(sampleData)))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||||
.send(request, HttpResponse.BodyHandlers.ofString());
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
assertThat(response.body(), containsString("Sample request body"));
|
assertThat(response.body(), containsString("Sample request body"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldReturnSampleDataContentWhenPostWithByteArrayProcessorStream() throws IOException, InterruptedException, URISyntaxException {
|
public void shouldReturnSampleDataContentWhenPostWithByteArrayProcessorStream() throws IOException, InterruptedException, URISyntaxException {
|
||||||
byte[] sampleData = "Sample request body".getBytes();
|
byte[] sampleData = "Sample request body".getBytes();
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI("https://postman-echo.com/post"))
|
.uri(new URI("https://postman-echo.com/post"))
|
||||||
.headers("Content-Type", "text/plain;charset=UTF-8")
|
.headers("Content-Type", "text/plain;charset=UTF-8")
|
||||||
.POST(HttpRequest.BodyPublishers.ofByteArray(sampleData))
|
.POST(HttpRequest.BodyPublishers.ofByteArray(sampleData))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||||
.send(request, HttpResponse.BodyHandlers.ofString());
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
assertThat(response.body(), containsString("Sample request body"));
|
assertThat(response.body(), containsString("Sample request body"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldReturnSampleDataContentWhenPostWithFileProcessorStream() throws IOException, InterruptedException, URISyntaxException {
|
public void shouldReturnSampleDataContentWhenPostWithFileProcessorStream() throws IOException, InterruptedException, URISyntaxException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI("https://postman-echo.com/post"))
|
.uri(new URI("https://postman-echo.com/post"))
|
||||||
.headers("Content-Type", "text/plain;charset=UTF-8")
|
.headers("Content-Type", "text/plain;charset=UTF-8")
|
||||||
.POST(HttpRequest.BodyPublishers.ofFile(Paths.get("src/test/resources/sample.txt")))
|
.POST(HttpRequest.BodyPublishers.ofFile(Paths.get("src/test/resources/sample.txt")))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||||
.send(request, HttpResponse.BodyHandlers.ofString());
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
assertThat(response.body(), containsString("Sample file content"));
|
assertThat(response.body(), containsString("Sample file content"));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,54 +1,54 @@
|
||||||
package com.baeldung.java11.httpclient.test;
|
package com.baeldung.java11.httpclient.test;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.equalTo;
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
import java.net.http.HttpRequest;
|
import java.net.http.HttpRequest;
|
||||||
import java.net.http.HttpResponse;
|
import java.net.http.HttpResponse;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class HttpResponseTest {
|
public class HttpResponseTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldReturnStatusOKWhenSendGetRequest() throws IOException, InterruptedException, URISyntaxException {
|
public void shouldReturnStatusOKWhenSendGetRequest() throws IOException, InterruptedException, URISyntaxException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI("https://postman-echo.com/get"))
|
.uri(new URI("https://postman-echo.com/get"))
|
||||||
.version(HttpClient.Version.HTTP_2)
|
.version(HttpClient.Version.HTTP_2)
|
||||||
.GET()
|
.GET()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
HttpResponse<String> response = HttpClient.newBuilder()
|
HttpResponse<String> response = HttpClient.newBuilder()
|
||||||
.followRedirects(HttpClient.Redirect.NORMAL)
|
.followRedirects(HttpClient.Redirect.NORMAL)
|
||||||
.build()
|
.build()
|
||||||
.send(request, HttpResponse.BodyHandlers.ofString());
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
assertNotNull(response.body());
|
assertNotNull(response.body());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldResponseURIDifferentThanRequestUIRWhenRedirect() throws IOException, InterruptedException, URISyntaxException {
|
public void shouldResponseURIDifferentThanRequestUIRWhenRedirect() throws IOException, InterruptedException, URISyntaxException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI("http://stackoverflow.com"))
|
.uri(new URI("http://stackoverflow.com"))
|
||||||
.version(HttpClient.Version.HTTP_2)
|
.version(HttpClient.Version.HTTP_2)
|
||||||
.GET()
|
.GET()
|
||||||
.build();
|
.build();
|
||||||
HttpResponse<String> response = HttpClient.newBuilder()
|
HttpResponse<String> response = HttpClient.newBuilder()
|
||||||
.followRedirects(HttpClient.Redirect.NORMAL)
|
.followRedirects(HttpClient.Redirect.NORMAL)
|
||||||
.build()
|
.build()
|
||||||
.send(request, HttpResponse.BodyHandlers.ofString());
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
assertThat(request.uri()
|
assertThat(request.uri()
|
||||||
.toString(), equalTo("http://stackoverflow.com"));
|
.toString(), equalTo("http://stackoverflow.com"));
|
||||||
assertThat(response.uri()
|
assertThat(response.uri()
|
||||||
.toString(), equalTo("https://stackoverflow.com/"));
|
.toString(), equalTo("https://stackoverflow.com/"));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -14,6 +14,7 @@
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-modules</artifactId>
|
<artifactId>parent-modules</artifactId>
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<relativePath>../../</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
|
@ -14,16 +14,23 @@
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-java</artifactId>
|
<artifactId>parent-java</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../parent-java</relativePath>
|
<relativePath>../../parent-java</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
<icu.version>64.2</icu.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.ibm.icu</groupId>
|
||||||
|
<artifactId>icu4j</artifactId>
|
||||||
|
<version>${icu.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.baeldung.jarArguments;
|
||||||
|
|
||||||
|
public class JarExample {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("Hello Baeldung Reader in JarExample!");
|
||||||
|
|
||||||
|
if(args == null) {
|
||||||
|
System.out.println("You have not provided any arguments!");
|
||||||
|
}else {
|
||||||
|
System.out.println("There are "+args.length+" argument(s)!");
|
||||||
|
for(int i=0; i<args.length; i++) {
|
||||||
|
System.out.println("Argument("+(i+1)+"):" + args[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.baeldung.localization;
|
||||||
|
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
public class App {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs all available formatter
|
||||||
|
* @throws ParseException
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
List<Locale> locales = Arrays.asList(new Locale[] { Locale.UK, Locale.ITALY, Locale.FRANCE, Locale.forLanguageTag("pl-PL") });
|
||||||
|
Localization.run(locales);
|
||||||
|
JavaSEFormat.run(locales);
|
||||||
|
ICUFormat.run(locales);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
package com.baeldung.localization;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
|
import com.ibm.icu.text.MessageFormat;
|
||||||
|
|
||||||
|
public class ICUFormat {
|
||||||
|
|
||||||
|
public static String getLabel(Locale locale, Object[] data) {
|
||||||
|
ResourceBundle bundle = ResourceBundle.getBundle("formats", locale);
|
||||||
|
String format = bundle.getString("label-icu");
|
||||||
|
MessageFormat formatter = new MessageFormat(format, locale);
|
||||||
|
return formatter.format(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void run(List<Locale> locales) {
|
||||||
|
System.out.println("ICU formatter");
|
||||||
|
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 0 })));
|
||||||
|
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 1 })));
|
||||||
|
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 2 })));
|
||||||
|
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 3 })));
|
||||||
|
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 0 })));
|
||||||
|
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 1 })));
|
||||||
|
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 2 })));
|
||||||
|
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 3 })));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.baeldung.localization;
|
||||||
|
|
||||||
|
import java.text.MessageFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
|
public class JavaSEFormat {
|
||||||
|
|
||||||
|
public static String getLabel(Locale locale, Object[] data) {
|
||||||
|
ResourceBundle bundle = ResourceBundle.getBundle("formats", locale);
|
||||||
|
final String pattern = bundle.getString("label");
|
||||||
|
final MessageFormat formatter = new MessageFormat(pattern, locale);
|
||||||
|
return formatter.format(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void run(List<Locale> locales) {
|
||||||
|
System.out.println("Java formatter");
|
||||||
|
final Date date = new Date(System.currentTimeMillis());
|
||||||
|
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { date, "Alice", 0 })));
|
||||||
|
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { date, "Alice", 2 })));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.baeldung.localization;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
|
public class Localization {
|
||||||
|
|
||||||
|
public static String getLabel(Locale locale) {
|
||||||
|
final ResourceBundle bundle = ResourceBundle.getBundle("messages", locale);
|
||||||
|
return bundle.getString("label");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void run(List<Locale> locales) {
|
||||||
|
locales.forEach(locale -> System.out.println(getLabel(locale)));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
Main-Class: com.baeldung.jarArguments.JarExample
|
|
@ -0,0 +1,2 @@
|
||||||
|
label=On {0, date, short} {1} has sent you {2, choice, 0#no messages|1#a message|2#two messages|2<{2,number,integer} messages}.
|
||||||
|
label-icu={0} has sent you {2, plural, =0 {no messages} =1 {a message} other {{2, number, integer} messages}}.
|
|
@ -0,0 +1,2 @@
|
||||||
|
label={0, date, short}, {1}{2, choice, 0# ne|0<} vous a envoyé {2, choice, 0#aucun message|1#un message|2#deux messages|2<{2,number,integer} messages}.
|
||||||
|
label-icu={0} {2, plural, =0 {ne } other {}}vous a envoyé {2, plural, =0 {aucun message} =1 {un message} other {{2, number, integer} messages}}.
|
|
@ -0,0 +1,2 @@
|
||||||
|
label={0, date, short} {1} ti ha inviato {2, choice, 0#nessun messagio|1#un messaggio|2#due messaggi|2<{2, number, integer} messaggi}.
|
||||||
|
label-icu={0} {2, plural, =0 {non } other {}}ti ha inviato {2, plural, =0 {nessun messaggio} =1 {un messaggio} other {{2, number, integer} messaggi}}.
|
|
@ -0,0 +1,2 @@
|
||||||
|
label=W {0, date, short} {1}{2, choice, 0# nie|0<} wys\u0142a\u0142a ci {2, choice, 0#\u017Cadnych wiadomo\u015Bci|1#wiadomo\u015B\u0107|2#dwie wiadomo\u015Bci|2<{2, number, integer} wiadomo\u015Bci}.
|
||||||
|
label-icu={0} {2, plural, =0 {nie } other {}}{1, select, male {wys\u0142a\u0142} female {wys\u0142a\u0142a} other {wys\u0142a\u0142o}} ci {2, plural, =0 {\u017Cadnej wiadomo\u015Bci} =1 {wiadomo\u015B\u0107} other {{2, number, integer} wiadomo\u015Bci}}.
|
|
@ -0,0 +1 @@
|
||||||
|
label=Alice has sent you a message.
|
|
@ -0,0 +1 @@
|
||||||
|
label=Alice vous a envoyé un message.
|
|
@ -0,0 +1 @@
|
||||||
|
label=Alice ti ha inviato un messaggio.
|
|
@ -0,0 +1 @@
|
||||||
|
label=Alice wys\u0142a\u0142a ci wiadomo\u015B\u0107.
|
|
@ -0,0 +1,74 @@
|
||||||
|
package com.baeldung.localization;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.localization.ICUFormat;
|
||||||
|
|
||||||
|
public class ICUFormatUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInUK_whenAliceSendsNothing_thenCorrectMessage() {
|
||||||
|
assertEquals("Alice has sent you no messages.", ICUFormat.getLabel(Locale.UK, new Object[] { "Alice", "female", 0 }));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInUK_whenAliceSendsOneMessage_thenCorrectMessage() {
|
||||||
|
assertEquals("Alice has sent you a message.", ICUFormat.getLabel(Locale.UK, new Object[] { "Alice", "female", 1 }));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInUK_whenBobSendsSixMessages_thenCorrectMessage() {
|
||||||
|
assertEquals("Bob has sent you 6 messages.", ICUFormat.getLabel(Locale.UK, new Object[] { "Bob", "male", 6 }));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInItaly_whenAliceSendsNothing_thenCorrectMessage() {
|
||||||
|
assertEquals("Alice non ti ha inviato nessun messaggio.", ICUFormat.getLabel(Locale.ITALY, new Object[] { "Alice", "female", 0 }));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInItaly_whenAliceSendsOneMessage_thenCorrectMessage() {
|
||||||
|
assertEquals("Alice ti ha inviato un messaggio.", ICUFormat.getLabel(Locale.ITALY, new Object[] { "Alice", "female", 1 }));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInItaly_whenBobSendsSixMessages_thenCorrectMessage() {
|
||||||
|
assertEquals("Bob ti ha inviato 6 messaggi.", ICUFormat.getLabel(Locale.ITALY, new Object[] { "Bob", "male", 6 }));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInFrance_whenAliceSendsNothing_thenCorrectMessage() {
|
||||||
|
assertEquals("Alice ne vous a envoyé aucun message.", ICUFormat.getLabel(Locale.FRANCE, new Object[] { "Alice", "female", 0 }));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInFrance_whenAliceSendsOneMessage_thenCorrectMessage() {
|
||||||
|
assertEquals("Alice vous a envoyé un message.", ICUFormat.getLabel(Locale.FRANCE, new Object[] { "Alice", "female", 1 }));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInFrance_whenBobSendsSixMessages_thenCorrectMessage() {
|
||||||
|
assertEquals("Bob vous a envoyé 6 messages.", ICUFormat.getLabel(Locale.FRANCE, new Object[] { "Bob", "male", 6 }));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInPoland_whenAliceSendsNothing_thenCorrectMessage() {
|
||||||
|
assertEquals("Alice nie wysłała ci żadnej wiadomości.", ICUFormat.getLabel(Locale.forLanguageTag("pl-PL"), new Object[] { "Alice", "female", 0 }));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInPoland_whenAliceSendsOneMessage_thenCorrectMessage() {
|
||||||
|
assertEquals("Alice wysłała ci wiadomość.", ICUFormat.getLabel(Locale.forLanguageTag("pl-PL"), new Object[] { "Alice", "female", 1 }));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInPoland_whenBobSendsSixMessages_thenCorrectMessage() {
|
||||||
|
assertEquals("Bob wysłał ci 6 wiadomości.", ICUFormat.getLabel(Locale.forLanguageTag("pl-PL"), new Object[] { "Bob", "male", 6 }));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,199 +1,199 @@
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>core-java-8</artifactId>
|
<artifactId>core-java-8</artifactId>
|
||||||
<version>0.1.0-SNAPSHOT</version>
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
<name>core-java-8</name>
|
<name>core-java-8</name>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-java</artifactId>
|
<artifactId>parent-java</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../parent-java</relativePath>
|
<relativePath>../../parent-java</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
<artifactId>commons-collections4</artifactId>
|
<artifactId>commons-collections4</artifactId>
|
||||||
<version>${commons-collections4.version}</version>
|
<version>${commons-collections4.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>commons-io</groupId>
|
<groupId>commons-io</groupId>
|
||||||
<artifactId>commons-io</artifactId>
|
<artifactId>commons-io</artifactId>
|
||||||
<version>${commons-io.version}</version>
|
<version>${commons-io.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
<artifactId>commons-lang3</artifactId>
|
<artifactId>commons-lang3</artifactId>
|
||||||
<version>${commons-lang3.version}</version>
|
<version>${commons-lang3.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
<artifactId>commons-math3</artifactId>
|
<artifactId>commons-math3</artifactId>
|
||||||
<version>${commons-math3.version}</version>
|
<version>${commons-math3.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>log4j</groupId>
|
<groupId>log4j</groupId>
|
||||||
<artifactId>log4j</artifactId>
|
<artifactId>log4j</artifactId>
|
||||||
<version>${log4j.version}</version>
|
<version>${log4j.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>commons-codec</groupId>
|
<groupId>commons-codec</groupId>
|
||||||
<artifactId>commons-codec</artifactId>
|
<artifactId>commons-codec</artifactId>
|
||||||
<version>${commons-codec.version}</version>
|
<version>${commons-codec.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
<version>${lombok.version}</version>
|
<version>${lombok.version}</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- test scoped -->
|
<!-- test scoped -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.assertj</groupId>
|
<groupId>org.assertj</groupId>
|
||||||
<artifactId>assertj-core</artifactId>
|
<artifactId>assertj-core</artifactId>
|
||||||
<version>${assertj.version}</version>
|
<version>${assertj.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.jayway.awaitility</groupId>
|
<groupId>com.jayway.awaitility</groupId>
|
||||||
<artifactId>awaitility</artifactId>
|
<artifactId>awaitility</artifactId>
|
||||||
<version>${avaitility.version}</version>
|
<version>${avaitility.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.openjdk.jmh</groupId>
|
<groupId>org.openjdk.jmh</groupId>
|
||||||
<artifactId>jmh-core</artifactId>
|
<artifactId>jmh-core</artifactId>
|
||||||
<version>${jmh-core.version}</version>
|
<version>${jmh-core.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.openjdk.jmh</groupId>
|
<groupId>org.openjdk.jmh</groupId>
|
||||||
<artifactId>jmh-generator-annprocess</artifactId>
|
<artifactId>jmh-generator-annprocess</artifactId>
|
||||||
<version>${jmh-generator.version}</version>
|
<version>${jmh-generator.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.openjdk.jmh</groupId>
|
<groupId>org.openjdk.jmh</groupId>
|
||||||
<artifactId>jmh-generator-bytecode</artifactId>
|
<artifactId>jmh-generator-bytecode</artifactId>
|
||||||
<version>${jmh-generator.version}</version>
|
<version>${jmh-generator.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.codepoetics</groupId>
|
<groupId>com.codepoetics</groupId>
|
||||||
<artifactId>protonpack</artifactId>
|
<artifactId>protonpack</artifactId>
|
||||||
<version>${protonpack.version}</version>
|
<version>${protonpack.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.vavr</groupId>
|
<groupId>io.vavr</groupId>
|
||||||
<artifactId>vavr</artifactId>
|
<artifactId>vavr</artifactId>
|
||||||
<version>${vavr.version}</version>
|
<version>${vavr.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>joda-time</groupId>
|
<groupId>joda-time</groupId>
|
||||||
<artifactId>joda-time</artifactId>
|
<artifactId>joda-time</artifactId>
|
||||||
<version>${joda.version}</version>
|
<version>${joda.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.aspectj</groupId>
|
<groupId>org.aspectj</groupId>
|
||||||
<artifactId>aspectjrt</artifactId>
|
<artifactId>aspectjrt</artifactId>
|
||||||
<version>${asspectj.version}</version>
|
<version>${asspectj.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.aspectj</groupId>
|
<groupId>org.aspectj</groupId>
|
||||||
<artifactId>aspectjweaver</artifactId>
|
<artifactId>aspectjweaver</artifactId>
|
||||||
<version>${asspectj.version}</version>
|
<version>${asspectj.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.powermock</groupId>
|
<groupId>org.powermock</groupId>
|
||||||
<artifactId>powermock-module-junit4</artifactId>
|
<artifactId>powermock-module-junit4</artifactId>
|
||||||
<version>${powermock.version}</version>
|
<version>${powermock.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.powermock</groupId>
|
<groupId>org.powermock</groupId>
|
||||||
<artifactId>powermock-api-mockito2</artifactId>
|
<artifactId>powermock-api-mockito2</artifactId>
|
||||||
<version>${powermock.version}</version>
|
<version>${powermock.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jmockit</groupId>
|
<groupId>org.jmockit</groupId>
|
||||||
<artifactId>jmockit</artifactId>
|
<artifactId>jmockit</artifactId>
|
||||||
<version>${jmockit.version}</version>
|
<version>${jmockit.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<finalName>core-java-8</finalName>
|
<finalName>core-java-8</finalName>
|
||||||
<resources>
|
<resources>
|
||||||
<resource>
|
<resource>
|
||||||
<directory>src/main/resources</directory>
|
<directory>src/main/resources</directory>
|
||||||
<filtering>true</filtering>
|
<filtering>true</filtering>
|
||||||
</resource>
|
</resource>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>${maven-compiler-plugin.version}</version>
|
<version>${maven-compiler-plugin.version}</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>1.8</source>
|
<source>1.8</source>
|
||||||
<target>1.8</target>
|
<target>1.8</target>
|
||||||
<compilerArgument>-parameters</compilerArgument>
|
<compilerArgument>-parameters</compilerArgument>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
<version>${spring-boot-maven-plugin.version}</version>
|
<version>${spring-boot-maven-plugin.version}</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<goals>
|
<goals>
|
||||||
<goal>repackage</goal>
|
<goal>repackage</goal>
|
||||||
</goals>
|
</goals>
|
||||||
<configuration>
|
<configuration>
|
||||||
<classifier>spring-boot</classifier>
|
<classifier>spring-boot</classifier>
|
||||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||||
</configuration>
|
</configuration>
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
<version>${maven-surefire-plugin.version}</version>
|
<version>${maven-surefire-plugin.version}</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<argLine>
|
<argLine>
|
||||||
-javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar
|
-javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar
|
||||||
</argLine>
|
</argLine>
|
||||||
<disableXmlReport>true</disableXmlReport>
|
<disableXmlReport>true</disableXmlReport>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<!-- util -->
|
<!-- util -->
|
||||||
<commons-lang3.version>3.5</commons-lang3.version>
|
<commons-lang3.version>3.5</commons-lang3.version>
|
||||||
<commons-math3.version>3.6.1</commons-math3.version>
|
<commons-math3.version>3.6.1</commons-math3.version>
|
||||||
<commons-collections4.version>4.1</commons-collections4.version>
|
<commons-collections4.version>4.1</commons-collections4.version>
|
||||||
<collections-generic.version>4.01</collections-generic.version>
|
<collections-generic.version>4.01</collections-generic.version>
|
||||||
<commons-codec.version>1.10</commons-codec.version>
|
<commons-codec.version>1.10</commons-codec.version>
|
||||||
<vavr.version>0.9.0</vavr.version>
|
<vavr.version>0.9.0</vavr.version>
|
||||||
<protonpack.version>1.13</protonpack.version>
|
<protonpack.version>1.13</protonpack.version>
|
||||||
<joda.version>2.10</joda.version>
|
<joda.version>2.10</joda.version>
|
||||||
<!-- testing -->
|
<!-- testing -->
|
||||||
<assertj.version>3.6.1</assertj.version>
|
<assertj.version>3.6.1</assertj.version>
|
||||||
<asspectj.version>1.8.9</asspectj.version>
|
<asspectj.version>1.8.9</asspectj.version>
|
||||||
<powermock.version>2.0.0-RC.4</powermock.version>
|
<powermock.version>2.0.0-RC.4</powermock.version>
|
||||||
<jmockit.version>1.44</jmockit.version>
|
<jmockit.version>1.44</jmockit.version>
|
||||||
<avaitility.version>1.7.0</avaitility.version>
|
<avaitility.version>1.7.0</avaitility.version>
|
||||||
<jmh-core.version>1.19</jmh-core.version>
|
<jmh-core.version>1.19</jmh-core.version>
|
||||||
<jmh-generator.version>1.19</jmh-generator.version>
|
<jmh-generator.version>1.19</jmh-generator.version>
|
||||||
<spring-boot-maven-plugin.version>2.0.4.RELEASE</spring-boot-maven-plugin.version>
|
<spring-boot-maven-plugin.version>2.0.4.RELEASE</spring-boot-maven-plugin.version>
|
||||||
<!-- plugins -->
|
<!-- plugins -->
|
||||||
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
|
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
|
||||||
<maven-surefire-plugin.version>2.22.1</maven-surefire-plugin.version>
|
<maven-surefire-plugin.version>2.22.1</maven-surefire-plugin.version>
|
||||||
</properties>
|
</properties>
|
||||||
</project>
|
</project>
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue