commit
e4557c2ce7
|
@ -66,3 +66,10 @@ jmeter/src/main/resources/*-JMeter.csv
|
||||||
**/nb-configuration.xml
|
**/nb-configuration.xml
|
||||||
core-scala/.cache-main
|
core-scala/.cache-main
|
||||||
core-scala/.cache-tests
|
core-scala/.cache-tests
|
||||||
|
|
||||||
|
|
||||||
|
persistence-modules/hibernate5/transaction.log
|
||||||
|
apache-avro/src/main/java/com/baeldung/avro/model/
|
||||||
|
jta/transaction-logs/
|
||||||
|
software-security/sql-injection-samples/derby.log
|
||||||
|
spring-soap/src/main/java/com/baeldung/springsoap/gen/
|
|
@ -5,9 +5,9 @@
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>JGit</artifactId>
|
<artifactId>JGit</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<name>JGit</name>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<url>http://maven.apache.org</url>
|
<url>http://maven.apache.org</url>
|
||||||
<name>JGit</name>
|
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
|
|
|
@ -2,8 +2,8 @@
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>Twitter4J</artifactId>
|
<artifactId>Twitter4J</artifactId>
|
||||||
<packaging>jar</packaging>
|
|
||||||
<name>Twitter4J</name>
|
<name>Twitter4J</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.typesafe.akka</groupId>
|
<groupId>com.typesafe.akka</groupId>
|
||||||
<artifactId>akka-stream_2.12</artifactId>
|
<artifactId>akka-stream_2.12</artifactId>
|
||||||
<version>2.5.11</version>
|
<version>${akka.stream.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.typesafe.akka</groupId>
|
<groupId>com.typesafe.akka</groupId>
|
||||||
|
@ -41,7 +41,6 @@
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
<java.version>1.8</java.version>
|
|
||||||
<akka.http.version>10.0.11</akka.http.version>
|
<akka.http.version>10.0.11</akka.http.version>
|
||||||
<akka.stream.version>2.5.11</akka.stream.version>
|
<akka.stream.version>2.5.11</akka.stream.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
|
@ -54,7 +54,6 @@
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<lombok.version>1.16.12</lombok.version>
|
|
||||||
<commons-math3.version>3.6.1</commons-math3.version>
|
<commons-math3.version>3.6.1</commons-math3.version>
|
||||||
<io.jenetics.version>3.7.0</io.jenetics.version>
|
<io.jenetics.version>3.7.0</io.jenetics.version>
|
||||||
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
||||||
|
|
|
@ -15,3 +15,5 @@
|
||||||
- [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)
|
- [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)
|
||||||
|
- [Implementing Simple State Machines with Java Enums](https://www.baeldung.com/java-enum-simple-state-machine)
|
||||||
|
|
|
@ -39,6 +39,11 @@
|
||||||
<version>${org.assertj.core.version}</version>
|
<version>${org.assertj.core.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.dpaukov</groupId>
|
||||||
|
<artifactId>combinatoricslib3</artifactId>
|
||||||
|
<version>3.3.0</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -74,11 +79,10 @@
|
||||||
</reporting>
|
</reporting>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<lombok.version>1.16.12</lombok.version>
|
|
||||||
<commons-math3.version>3.6.1</commons-math3.version>
|
<commons-math3.version>3.6.1</commons-math3.version>
|
||||||
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
||||||
<commons-codec.version>1.11</commons-codec.version>
|
<commons-codec.version>1.11</commons-codec.version>
|
||||||
<guava.version>25.1-jre</guava.version>
|
<guava.version>27.0.1-jre</guava.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -0,0 +1,29 @@
|
||||||
|
package com.baeldung.algorithms.combination;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
import org.apache.commons.math3.util.CombinatoricsUtils;
|
||||||
|
|
||||||
|
public class ApacheCommonsCombinationGenerator {
|
||||||
|
|
||||||
|
private static final int N = 6;
|
||||||
|
private static final int R = 3;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print all combinations of r elements from a set
|
||||||
|
* @param n - number of elements in set
|
||||||
|
* @param r - number of elements in selection
|
||||||
|
*/
|
||||||
|
public static void generate(int n, int r) {
|
||||||
|
Iterator<int[]> iterator = CombinatoricsUtils.combinationsIterator(n, r);
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
final int[] combination = iterator.next();
|
||||||
|
System.out.println(Arrays.toString(combination));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
generate(N, R);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.baeldung.algorithms.combination;
|
||||||
|
|
||||||
|
import org.paukov.combinatorics3.Generator;
|
||||||
|
|
||||||
|
public class CombinatoricsLibCombinationGenerator {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Generator.combination(0, 1, 2, 3, 4, 5)
|
||||||
|
.simple(3)
|
||||||
|
.stream()
|
||||||
|
.forEach(System.out::println);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package com.baeldung.algorithms.combination;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableSet;
|
||||||
|
import com.google.common.collect.Sets;
|
||||||
|
|
||||||
|
public class GuavaCombinationsGenerator {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
Set<Set<Integer>> combinations = Sets.combinations(ImmutableSet.of(0, 1, 2, 3, 4, 5), 3);
|
||||||
|
System.out.println(combinations.size());
|
||||||
|
System.out.println(Arrays.toString(combinations.toArray()));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,52 @@
|
||||||
|
package com.baeldung.algorithms.combination;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class IterativeCombinationGenerator {
|
||||||
|
|
||||||
|
private static final int N = 5;
|
||||||
|
private static final int R = 2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate all combinations of r elements from a set
|
||||||
|
* @param n the number of elements in input set
|
||||||
|
* @param r the number of elements in a combination
|
||||||
|
* @return the list containing all combinations
|
||||||
|
*/
|
||||||
|
public List<int[]> generate(int n, int r) {
|
||||||
|
List<int[]> combinations = new ArrayList<>();
|
||||||
|
int[] combination = new int[r];
|
||||||
|
|
||||||
|
// initialize with lowest lexicographic combination
|
||||||
|
for (int i = 0; i < r; i++) {
|
||||||
|
combination[i] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (combination[r - 1] < n) {
|
||||||
|
combinations.add(combination.clone());
|
||||||
|
|
||||||
|
// generate next combination in lexicographic order
|
||||||
|
int t = r - 1;
|
||||||
|
while (t != 0 && combination[t] == n - r + t) {
|
||||||
|
t--;
|
||||||
|
}
|
||||||
|
combination[t]++;
|
||||||
|
for (int i = t + 1; i < r; i++) {
|
||||||
|
combination[i] = combination[i - 1] + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return combinations;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
IterativeCombinationGenerator generator = new IterativeCombinationGenerator();
|
||||||
|
List<int[]> combinations = generator.generate(N, R);
|
||||||
|
System.out.println(combinations.size());
|
||||||
|
for (int[] combination : combinations) {
|
||||||
|
System.out.println(Arrays.toString(combination));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,53 @@
|
||||||
|
package com.baeldung.algorithms.combination;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class SelectionRecursiveCombinationGenerator {
|
||||||
|
|
||||||
|
private static final int N = 6;
|
||||||
|
private static final int R = 3;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate all combinations of r elements from a set
|
||||||
|
* @param n - number of elements in input set
|
||||||
|
* @param r - number of elements to be chosen
|
||||||
|
* @return the list containing all combinations
|
||||||
|
*/
|
||||||
|
public List<int[]> generate(int n, int r) {
|
||||||
|
List<int[]> combinations = new ArrayList<>();
|
||||||
|
helper(combinations, new int[r], 0, n - 1, 0);
|
||||||
|
return combinations;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Choose elements from set by recursing over elements selected
|
||||||
|
* @param combinations - List to store generated combinations
|
||||||
|
* @param data - current combination
|
||||||
|
* @param start - starting element of remaining set
|
||||||
|
* @param end - last element of remaining set
|
||||||
|
* @param index - number of elements chosen so far.
|
||||||
|
*/
|
||||||
|
private void helper(List<int[]> combinations, int data[], int start, int end, int index) {
|
||||||
|
if (index == data.length) {
|
||||||
|
int[] combination = data.clone();
|
||||||
|
combinations.add(combination);
|
||||||
|
} else {
|
||||||
|
int max = Math.min(end, end + 1 - data.length + index);
|
||||||
|
for (int i = start; i <= max; i++) {
|
||||||
|
data[index] = i;
|
||||||
|
helper(combinations, data, i + 1, end, index + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SelectionRecursiveCombinationGenerator generator = new SelectionRecursiveCombinationGenerator();
|
||||||
|
List<int[]> combinations = generator.generate(N, R);
|
||||||
|
for (int[] combination : combinations) {
|
||||||
|
System.out.println(Arrays.toString(combination));
|
||||||
|
}
|
||||||
|
System.out.printf("generated %d combinations of %d items from %d ", combinations.size(), R, N);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,50 @@
|
||||||
|
package com.baeldung.algorithms.combination;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class SetRecursiveCombinationGenerator {
|
||||||
|
|
||||||
|
private static final int N = 5;
|
||||||
|
private static final int R = 2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate all combinations of r elements from a set
|
||||||
|
* @param n - number of elements in set
|
||||||
|
* @param r - number of elements in selection
|
||||||
|
* @return the list containing all combinations
|
||||||
|
*/
|
||||||
|
public List<int[]> generate(int n, int r) {
|
||||||
|
List<int[]> combinations = new ArrayList<>();
|
||||||
|
helper(combinations, new int[r], 0, n-1, 0);
|
||||||
|
return combinations;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param combinations - List to contain the generated combinations
|
||||||
|
* @param data - List of elements in the selection
|
||||||
|
* @param start - index of the starting element in the remaining set
|
||||||
|
* @param end - index of the last element in the set
|
||||||
|
* @param index - number of elements selected so far
|
||||||
|
*/
|
||||||
|
private void helper(List<int[]> combinations, int data[], int start, int end, int index) {
|
||||||
|
if (index == data.length) {
|
||||||
|
int[] combination = data.clone();
|
||||||
|
combinations.add(combination);
|
||||||
|
} else if (start <= end) {
|
||||||
|
data[index] = start;
|
||||||
|
helper(combinations, data, start + 1, end, index + 1);
|
||||||
|
helper(combinations, data, start + 1, end, index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SetRecursiveCombinationGenerator generator = new SetRecursiveCombinationGenerator();
|
||||||
|
List<int[]> combinations = generator.generate(N, R);
|
||||||
|
for (int[] combination : combinations) {
|
||||||
|
System.out.println(Arrays.toString(combination));
|
||||||
|
}
|
||||||
|
System.out.printf("generated %d combinations of %d items from %d ", combinations.size(), R, N);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
package com.baeldung.algorithms.combination;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class CombinationUnitTest {
|
||||||
|
|
||||||
|
private static final int N = 5;
|
||||||
|
private static final int R = 3;
|
||||||
|
private static final int nCr = 10;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSetAndSelectionSize_whenCalculatedUsingSetRecursiveAlgorithm_thenExpectedCount() {
|
||||||
|
SetRecursiveCombinationGenerator generator = new SetRecursiveCombinationGenerator();
|
||||||
|
List<int[]> selection = generator.generate(N, R);
|
||||||
|
assertEquals(nCr, selection.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSetAndSelectionSize_whenCalculatedUsingSelectionRecursiveAlgorithm_thenExpectedCount() {
|
||||||
|
SelectionRecursiveCombinationGenerator generator = new SelectionRecursiveCombinationGenerator();
|
||||||
|
List<int[]> selection = generator.generate(N, R);
|
||||||
|
assertEquals(nCr, selection.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSetAndSelectionSize_whenCalculatedUsingIterativeAlgorithm_thenExpectedCount() {
|
||||||
|
IterativeCombinationGenerator generator = new IterativeCombinationGenerator();
|
||||||
|
List<int[]> selection = generator.generate(N, R);
|
||||||
|
assertEquals(nCr, selection.size());
|
||||||
|
}
|
||||||
|
}
|
|
@ -68,7 +68,7 @@
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.codehaus.mojo</groupId>
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
<artifactId>cobertura-maven-plugin</artifactId>
|
<artifactId>cobertura-maven-plugin</artifactId>
|
||||||
<version>2.7</version>
|
<version>${cobertura-maven-plugin.version}</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<instrumentation>
|
<instrumentation>
|
||||||
<ignores>
|
<ignores>
|
||||||
|
@ -84,13 +84,13 @@
|
||||||
</reporting>
|
</reporting>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<lombok.version>1.16.12</lombok.version>
|
|
||||||
<commons-math3.version>3.6.1</commons-math3.version>
|
<commons-math3.version>3.6.1</commons-math3.version>
|
||||||
<tradukisto.version>1.0.1</tradukisto.version>
|
<tradukisto.version>1.0.1</tradukisto.version>
|
||||||
<org.jgrapht.core.version>1.0.1</org.jgrapht.core.version>
|
<org.jgrapht.core.version>1.0.1</org.jgrapht.core.version>
|
||||||
<org.jgrapht.ext.version>1.0.1</org.jgrapht.ext.version>
|
<org.jgrapht.ext.version>1.0.1</org.jgrapht.ext.version>
|
||||||
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
||||||
<commons-codec.version>1.11</commons-codec.version>
|
<commons-codec.version>1.11</commons-codec.version>
|
||||||
|
<cobertura-maven-plugin.version>2.7</cobertura-maven-plugin.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
Binary file not shown.
Before Width: | Height: | Size: 9.1 KiB After Width: | Height: | Size: 9.4 KiB |
|
@ -49,7 +49,6 @@
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<lombok.version>1.16.12</lombok.version>
|
|
||||||
<commons-math3.version>3.6.1</commons-math3.version>
|
<commons-math3.version>3.6.1</commons-math3.version>
|
||||||
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
||||||
<commons-codec.version>1.11</commons-codec.version>
|
<commons-codec.version>1.11</commons-codec.version>
|
||||||
|
|
|
@ -3,9 +3,9 @@
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>animal-sniffer-mvn-plugin</artifactId>
|
<artifactId>animal-sniffer-mvn-plugin</artifactId>
|
||||||
<packaging>jar</packaging>
|
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
<name>animal-sniffer-mvn-plugin</name>
|
<name>animal-sniffer-mvn-plugin</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
<url>http://maven.apache.org</url>
|
<url>http://maven.apache.org</url>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
|
|
|
@ -3,8 +3,8 @@
|
||||||
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>
|
||||||
<artifactId>annotations</artifactId>
|
<artifactId>annotations</artifactId>
|
||||||
<packaging>pom</packaging>
|
|
||||||
<name>annotations</name>
|
<name>annotations</name>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>parent-modules</artifactId>
|
<artifactId>parent-modules</artifactId>
|
||||||
|
|
|
@ -7,14 +7,6 @@
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<name>apache-avro</name>
|
<name>apache-avro</name>
|
||||||
|
|
||||||
<properties>
|
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
|
||||||
<compiler-plugin.version>3.5</compiler-plugin.version>
|
|
||||||
<avro.version>1.8.2</avro.version>
|
|
||||||
<java.version>1.8</java.version>
|
|
||||||
<slf4j.version>1.7.25</slf4j.version>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-modules</artifactId>
|
<artifactId>parent-modules</artifactId>
|
||||||
|
@ -85,4 +77,12 @@
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<compiler-plugin.version>3.5</compiler-plugin.version>
|
||||||
|
<avro.version>1.8.2</avro.version>
|
||||||
|
<slf4j.version>1.7.25</slf4j.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -3,8 +3,8 @@
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>apache-curator</artifactId>
|
<artifactId>apache-curator</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<packaging>jar</packaging>
|
|
||||||
<name>apache-curator</name>
|
<name>apache-curator</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
|
@ -39,7 +39,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
<artifactId>jackson-databind</artifactId>
|
<artifactId>jackson-databind</artifactId>
|
||||||
<version>${jackson-databind.version}</version>
|
<version>${jackson.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- test scoped -->
|
<!-- test scoped -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -59,7 +59,6 @@
|
||||||
<properties>
|
<properties>
|
||||||
<curator.version>4.0.1</curator.version>
|
<curator.version>4.0.1</curator.version>
|
||||||
<zookeeper.version>3.4.11</zookeeper.version>
|
<zookeeper.version>3.4.11</zookeeper.version>
|
||||||
<jackson-databind.version>2.9.7</jackson-databind.version>
|
|
||||||
<!-- testing -->
|
<!-- testing -->
|
||||||
<assertj.version>3.6.1</assertj.version>
|
<assertj.version>3.6.1</assertj.version>
|
||||||
<avaitility.version>1.7.0</avaitility.version>
|
<avaitility.version>1.7.0</avaitility.version>
|
||||||
|
|
|
@ -13,22 +13,6 @@
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<properties>
|
|
||||||
<geode.core>1.6.0</geode.core>
|
|
||||||
</properties>
|
|
||||||
<build>
|
|
||||||
<plugins>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
|
||||||
<configuration>
|
|
||||||
<source>1.8</source>
|
|
||||||
<target>1.8</target>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.geode</groupId>
|
<groupId>org.apache.geode</groupId>
|
||||||
|
@ -38,8 +22,24 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<version>RELEASE</version>
|
<version>${junit.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>${java.version}</source>
|
||||||
|
<target>${java.version}</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<geode.core>1.6.0</geode.core>
|
||||||
|
</properties>
|
||||||
</project>
|
</project>
|
|
@ -7,51 +7,58 @@
|
||||||
<name>apache-meecrowave</name>
|
<name>apache-meecrowave</name>
|
||||||
<description>A sample REST API application with Meecrowave</description>
|
<description>A sample REST API application with Meecrowave</description>
|
||||||
|
|
||||||
<properties>
|
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
|
||||||
</properties>
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<!-- https://mvnrepository.com/artifact/org.apache.meecrowave/meecrowave-core -->
|
<!-- https://mvnrepository.com/artifact/org.apache.meecrowave/meecrowave-core -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.meecrowave</groupId>
|
<groupId>org.apache.meecrowave</groupId>
|
||||||
<artifactId>meecrowave-core</artifactId>
|
<artifactId>meecrowave-core</artifactId>
|
||||||
<version>1.2.1</version>
|
<version>${meecrowave-core.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- https://mvnrepository.com/artifact/org.apache.meecrowave/meecrowave-jpa -->
|
<!-- https://mvnrepository.com/artifact/org.apache.meecrowave/meecrowave-jpa -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.meecrowave</groupId>
|
<groupId>org.apache.meecrowave</groupId>
|
||||||
<artifactId>meecrowave-jpa</artifactId>
|
<artifactId>meecrowave-jpa</artifactId>
|
||||||
<version>1.2.1</version>
|
<version>${meecrowave-jpa.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.squareup.okhttp3</groupId>
|
<groupId>com.squareup.okhttp3</groupId>
|
||||||
<artifactId>okhttp</artifactId>
|
<artifactId>okhttp</artifactId>
|
||||||
<version>3.10.0</version>
|
<version>${okhttp.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.meecrowave</groupId>
|
<groupId>org.apache.meecrowave</groupId>
|
||||||
<artifactId>meecrowave-junit</artifactId>
|
<artifactId>meecrowave-junit</artifactId>
|
||||||
<version>1.2.0</version>
|
<version>${meecrowave-junit.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- https://mvnrepository.com/artifact/junit/junit -->
|
<!-- https://mvnrepository.com/artifact/junit/junit -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<version>4.10</version>
|
<version>${junit.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.meecrowave</groupId>
|
<groupId>org.apache.meecrowave</groupId>
|
||||||
<artifactId>meecrowave-maven-plugin</artifactId>
|
<artifactId>meecrowave-maven-plugin</artifactId>
|
||||||
<version>1.2.1</version>
|
<version>${meecrowave-maven-plugin.version}</version>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
<junit.version>4.10</junit.version>
|
||||||
|
<meecrowave-junit.version>1.2.0</meecrowave-junit.version>
|
||||||
|
<okhttp.version>3.10.0</okhttp.version>
|
||||||
|
<meecrowave-jpa.version>1.2.1</meecrowave-jpa.version>
|
||||||
|
<meecrowave-core.version>1.2.1</meecrowave-core.version>
|
||||||
|
<meecrowave-maven-plugin.version>1.2.1</meecrowave-maven-plugin.version>
|
||||||
|
</properties>
|
||||||
</project>
|
</project>
|
|
@ -11,12 +11,14 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.pulsar</groupId>
|
<groupId>org.apache.pulsar</groupId>
|
||||||
<artifactId>pulsar-client</artifactId>
|
<artifactId>pulsar-client</artifactId>
|
||||||
<version>2.1.1-incubating</version>
|
<version>${pulsar-client.version}</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
<pulsar-client.version>2.1.1-incubating</pulsar-client.version>
|
||||||
</properties>
|
</properties>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -4,8 +4,8 @@
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>apache-solrj</artifactId>
|
<artifactId>apache-solrj</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<packaging>jar</packaging>
|
|
||||||
<name>apache-solrj</name>
|
<name>apache-solrj</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
### Relevant articles
|
### Relevant articles
|
||||||
|
|
||||||
- [Introduction to Apache Spark](http://www.baeldung.com/apache-spark)
|
- [Introduction to Apache Spark](http://www.baeldung.com/apache-spark)
|
||||||
|
- [Building a Data Pipeline with Kafka, Spark Streaming and Cassandra](https://www.baeldung.com/kafka-spark-data-pipeline)
|
||||||
|
|
|
@ -4,8 +4,8 @@
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>apache-spark</artifactId>
|
<artifactId>apache-spark</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
<packaging>jar</packaging>
|
|
||||||
<name>apache-spark</name>
|
<name>apache-spark</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
<url>http://maven.apache.org</url>
|
<url>http://maven.apache.org</url>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
|
@ -54,10 +54,10 @@
|
||||||
<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>3.2</version>
|
<version>${maven-compiler-plugin.version}</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>1.8</source>
|
<source>${java.version}</source>
|
||||||
<target>1.8</target>
|
<target>${java.version}</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
@ -85,6 +85,7 @@
|
||||||
<org.apache.spark.spark-streaming-kafka.version>2.3.0</org.apache.spark.spark-streaming-kafka.version>
|
<org.apache.spark.spark-streaming-kafka.version>2.3.0</org.apache.spark.spark-streaming-kafka.version>
|
||||||
<com.datastax.spark.spark-cassandra-connector.version>2.3.0</com.datastax.spark.spark-cassandra-connector.version>
|
<com.datastax.spark.spark-cassandra-connector.version>2.3.0</com.datastax.spark.spark-cassandra-connector.version>
|
||||||
<com.datastax.spark.spark-cassandra-connector-java.version>1.5.2</com.datastax.spark.spark-cassandra-connector-java.version>
|
<com.datastax.spark.spark-cassandra-connector-java.version>1.5.2</com.datastax.spark.spark-cassandra-connector-java.version>
|
||||||
|
<maven-compiler-plugin.version>3.2</maven-compiler-plugin.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -4,8 +4,8 @@
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<version>0.1-SNAPSHOT</version>
|
<version>0.1-SNAPSHOT</version>
|
||||||
<artifactId>apache-velocity</artifactId>
|
<artifactId>apache-velocity</artifactId>
|
||||||
<packaging>war</packaging>
|
|
||||||
<name>apache-velocity</name>
|
<name>apache-velocity</name>
|
||||||
|
<packaging>war</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
|
|
|
@ -5,8 +5,8 @@
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>aws-lambda</artifactId>
|
<artifactId>aws-lambda</artifactId>
|
||||||
<version>0.1.0-SNAPSHOT</version>
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
<packaging>jar</packaging>
|
|
||||||
<name>aws-lambda</name>
|
<name>aws-lambda</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>parent-modules</artifactId>
|
<artifactId>parent-modules</artifactId>
|
||||||
|
|
|
@ -4,7 +4,6 @@
|
||||||
- [AWS S3 with Java](http://www.baeldung.com/aws-s3-java)
|
- [AWS S3 with Java](http://www.baeldung.com/aws-s3-java)
|
||||||
- [AWS Lambda With Java](http://www.baeldung.com/java-aws-lambda)
|
- [AWS Lambda With Java](http://www.baeldung.com/java-aws-lambda)
|
||||||
- [Managing EC2 Instances in Java](http://www.baeldung.com/ec2-java)
|
- [Managing EC2 Instances in Java](http://www.baeldung.com/ec2-java)
|
||||||
- [http://www.baeldung.com/aws-s3-multipart-upload](https://github.com/eugenp/tutorials/tree/master/aws)
|
|
||||||
- [Multipart Uploads in Amazon S3 with Java](http://www.baeldung.com/aws-s3-multipart-upload)
|
- [Multipart Uploads in Amazon S3 with Java](http://www.baeldung.com/aws-s3-multipart-upload)
|
||||||
- [Integration Testing with a Local DynamoDB Instance](http://www.baeldung.com/dynamodb-local-integration-tests)
|
- [Integration Testing with a Local DynamoDB Instance](http://www.baeldung.com/dynamodb-local-integration-tests)
|
||||||
- [Using the JetS3t Java Client With Amazon S3](http://www.baeldung.com/jets3t-amazon-s3)
|
- [Using the JetS3t Java Client With Amazon S3](http://www.baeldung.com/jets3t-amazon-s3)
|
||||||
|
|
|
@ -4,8 +4,8 @@
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>aws</artifactId>
|
<artifactId>aws</artifactId>
|
||||||
<version>0.1.0-SNAPSHOT</version>
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
<packaging>jar</packaging>
|
|
||||||
<name>aws</name>
|
<name>aws</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
|
|
|
@ -36,7 +36,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||||
<version>2.1.1.RELEASE</version>
|
<version>${spring-boot.version}</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
|
@ -5,9 +5,9 @@
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>azure</artifactId>
|
<artifactId>azure</artifactId>
|
||||||
<version>0.1</version>
|
<version>0.1</version>
|
||||||
<packaging>war</packaging>
|
|
||||||
<name>azure</name>
|
<name>azure</name>
|
||||||
<description>Demo project for Spring Boot on Azure</description>
|
<description>Demo project for Spring Boot on Azure</description>
|
||||||
|
<packaging>war</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>parent-boot-2</artifactId>
|
<artifactId>parent-boot-2</artifactId>
|
||||||
|
@ -127,7 +127,6 @@
|
||||||
<docker.image.prefix>${azure.containerRegistry}.azurecr.io</docker.image.prefix>
|
<docker.image.prefix>${azure.containerRegistry}.azurecr.io</docker.image.prefix>
|
||||||
<docker-maven-plugin.version>1.1.0</docker-maven-plugin.version>
|
<docker-maven-plugin.version>1.1.0</docker-maven-plugin.version>
|
||||||
<azure-webapp-maven-plugin.version>1.1.0</azure-webapp-maven-plugin.version>
|
<azure-webapp-maven-plugin.version>1.1.0</azure-webapp-maven-plugin.version>
|
||||||
<java.version>1.8</java.version>
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -3,12 +3,12 @@
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>blade</artifactId>
|
|
||||||
<name>blade</name>
|
|
||||||
|
|
||||||
<!-- WITH THIS mvn integration-test DOES WORK -->
|
<!-- WITH THIS mvn integration-test DOES WORK -->
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>blade</artifactId>
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<name>blade</name>
|
||||||
|
|
||||||
|
|
||||||
<!-- WITH THIS mvn integration-test DOESN'T WORK -->
|
<!-- WITH THIS mvn integration-test DOESN'T WORK -->
|
||||||
<!-- <parent> -->
|
<!-- <parent> -->
|
||||||
|
@ -17,35 +17,30 @@
|
||||||
<!-- <version>1.0.0-SNAPSHOT</version> -->
|
<!-- <version>1.0.0-SNAPSHOT</version> -->
|
||||||
<!-- </parent> -->
|
<!-- </parent> -->
|
||||||
|
|
||||||
<properties>
|
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.bladejava</groupId>
|
<groupId>com.bladejava</groupId>
|
||||||
<artifactId>blade-mvc</artifactId>
|
<artifactId>blade-mvc</artifactId>
|
||||||
<version>2.0.14.RELEASE</version>
|
<version>${blade-mvc.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.webjars</groupId>
|
<groupId>org.webjars</groupId>
|
||||||
<artifactId>bootstrap</artifactId>
|
<artifactId>bootstrap</artifactId>
|
||||||
<version>4.2.1</version>
|
<version>${bootstrap.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>3.8.1</version>
|
<version>${commons-lang3.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- PROVIDED -->
|
<!-- PROVIDED -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
<version>1.18.4</version>
|
<version>${lombok.version}</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
@ -53,31 +48,31 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<version>4.12</version>
|
<version>${junit.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.assertj</groupId>
|
<groupId>org.assertj</groupId>
|
||||||
<artifactId>assertj-core</artifactId>
|
<artifactId>assertj-core</artifactId>
|
||||||
<version>3.11.1</version>
|
<version>${assertj-core.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.httpcomponents</groupId>
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
<artifactId>httpclient</artifactId>
|
<artifactId>httpclient</artifactId>
|
||||||
<version>4.5.6</version>
|
<version>${httpclient.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.httpcomponents</groupId>
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
<artifactId>httpmime</artifactId>
|
<artifactId>httpmime</artifactId>
|
||||||
<version>4.5.6</version>
|
<version>${httpmime.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.httpcomponents</groupId>
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
<artifactId>httpcore</artifactId>
|
<artifactId>httpcore</artifactId>
|
||||||
<version>4.4.10</version>
|
<version>${httpcore.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
@ -88,6 +83,7 @@
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>${maven-surefire-plugin.version}</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<forkCount>3</forkCount>
|
<forkCount>3</forkCount>
|
||||||
<reuseForks>true</reuseForks>
|
<reuseForks>true</reuseForks>
|
||||||
|
@ -100,7 +96,7 @@
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-failsafe-plugin</artifactId>
|
<artifactId>maven-failsafe-plugin</artifactId>
|
||||||
<version>3.0.0-M3</version>
|
<version>${maven-failsafe-plugin.version}</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<includes>
|
<includes>
|
||||||
<include>**/*LiveTest.java</include>
|
<include>**/*LiveTest.java</include>
|
||||||
|
@ -119,7 +115,7 @@
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>com.bazaarvoice.maven.plugins</groupId>
|
<groupId>com.bazaarvoice.maven.plugins</groupId>
|
||||||
<artifactId>process-exec-maven-plugin</artifactId>
|
<artifactId>process-exec-maven-plugin</artifactId>
|
||||||
<version>0.7</version>
|
<version>${process-exec-maven-plugin.version}</version>
|
||||||
<executions>
|
<executions>
|
||||||
<!--Start Blade -->
|
<!--Start Blade -->
|
||||||
<execution>
|
<execution>
|
||||||
|
@ -177,13 +173,33 @@
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>${maven-compiler-plugin.version}</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>1.8</source>
|
<source>${maven.compiler.source}</source>
|
||||||
<target>1.8</target>
|
<target>${maven.compiler.target}</target>
|
||||||
<encoding>UTF-8</encoding>
|
<encoding>UTF-8</encoding>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
<blade-mvc.version>2.0.14.RELEASE</blade-mvc.version>
|
||||||
|
<bootstrap.version>4.2.1</bootstrap.version>
|
||||||
|
<commons-lang3.version>3.8.1</commons-lang3.version>
|
||||||
|
<lombok.version>1.18.4</lombok.version>
|
||||||
|
<junit.version>4.12</junit.version>
|
||||||
|
<httpclient.version>4.5.6</httpclient.version>
|
||||||
|
<httpmime.version>4.5.6</httpmime.version>
|
||||||
|
<httpcore.version>4.4.10</httpcore.version>
|
||||||
|
<assertj-core.version>3.11.1</assertj-core.version>
|
||||||
|
<maven-failsafe-plugin.version>3.0.0-M3</maven-failsafe-plugin.version>
|
||||||
|
<process-exec-maven-plugin.version>0.7</process-exec-maven-plugin.version>
|
||||||
|
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
|
||||||
|
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
|
||||||
|
</properties>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -3,9 +3,9 @@
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung.bootique</groupId>
|
<groupId>com.baeldung.bootique</groupId>
|
||||||
<artifactId>bootique</artifactId>
|
<artifactId>bootique</artifactId>
|
||||||
<packaging>jar</packaging>
|
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
<name>bootique</name>
|
<name>bootique</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
<url>http://maven.apache.org</url>
|
<url>http://maven.apache.org</url>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
|
|
|
@ -3,9 +3,9 @@
|
||||||
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>
|
||||||
<artifactId>cas-secured-app</artifactId>
|
<artifactId>cas-secured-app</artifactId>
|
||||||
<packaging>jar</packaging>
|
|
||||||
<name>cas-secured-app</name>
|
<name>cas-secured-app</name>
|
||||||
<description>Demo project for CAS</description>
|
<description>Demo project for CAS</description>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>parent-boot-1</artifactId>
|
<artifactId>parent-boot-1</artifactId>
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
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>cas</artifactId>
|
||||||
|
<name>cas</name>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<relativePath>..</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<modules>
|
||||||
|
<module>cas-secured-app</module>
|
||||||
|
<module>cas-server</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
</project>
|
|
@ -59,13 +59,13 @@
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<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>
|
<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>
|
||||||
<junit.version>4.12</junit.version>
|
|
||||||
<spring.version>5.1.2.RELEASE</spring.version>
|
<spring.version>5.1.2.RELEASE</spring.version>
|
||||||
</properties>
|
</properties>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -3,9 +3,9 @@
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>checker-plugin</artifactId>
|
<artifactId>checker-plugin</artifactId>
|
||||||
<packaging>jar</packaging>
|
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
<name>checker-plugin</name>
|
<name>checker-plugin</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
<url>http://maven.apache.org</url>
|
<url>http://maven.apache.org</url>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
|
|
|
@ -4,3 +4,7 @@
|
||||||
|
|
||||||
- [JDBC with Groovy](http://www.baeldung.com/jdbc-groovy)
|
- [JDBC with Groovy](http://www.baeldung.com/jdbc-groovy)
|
||||||
- [Working with JSON in Groovy](http://www.baeldung.com/groovy-json)
|
- [Working with JSON in Groovy](http://www.baeldung.com/groovy-json)
|
||||||
|
- [Reading a File in Groovy](https://www.baeldung.com/groovy-file-read)
|
||||||
|
- [Types of Strings in Groovy](https://www.baeldung.com/groovy-strings)
|
||||||
|
- [A Quick Guide to Iterating a Map in Groovy](https://www.baeldung.com/groovy-map-iterating)
|
||||||
|
- [An Introduction to Traits in Groovy](https://www.baeldung.com/groovy-traits)
|
||||||
|
|
|
@ -23,6 +23,12 @@
|
||||||
<groupId>org.codehaus.groovy</groupId>
|
<groupId>org.codehaus.groovy</groupId>
|
||||||
<artifactId>groovy-all</artifactId>
|
<artifactId>groovy-all</artifactId>
|
||||||
<version>${groovy-all.version}</version>
|
<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>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.codehaus.groovy</groupId>
|
<groupId>org.codehaus.groovy</groupId>
|
||||||
|
@ -103,9 +109,12 @@
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<junit.platform.version>1.0.0</junit.platform.version>
|
<junit.platform.version>1.0.0</junit.platform.version>
|
||||||
<groovy.version>2.4.13</groovy.version>
|
<!-- <groovy.version>2.4.13</groovy.version> -->
|
||||||
<groovy-all.version>2.4.13</groovy-all.version>
|
<!-- <groovy-all.version>2.4.13</groovy-all.version> -->
|
||||||
<groovy-sql.version>2.4.13</groovy-sql.version>
|
<!-- <groovy-sql.version>2.4.13</groovy-sql.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>
|
<hsqldb.version>2.4.0</hsqldb.version>
|
||||||
<spock-core.version>1.1-groovy-2.4</spock-core.version>
|
<spock-core.version>1.1-groovy-2.4</spock-core.version>
|
||||||
<gmavenplus-plugin.version>1.6</gmavenplus-plugin.version>
|
<gmavenplus-plugin.version>1.6</gmavenplus-plugin.version>
|
||||||
|
|
|
@ -0,0 +1,107 @@
|
||||||
|
package com.baeldung.file
|
||||||
|
|
||||||
|
class ReadFile {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* reads file content line by line using withReader and reader.readLine
|
||||||
|
* @param filePath
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
int readFileLineByLine(String filePath) {
|
||||||
|
File file = new File(filePath)
|
||||||
|
def line, noOfLines = 0;
|
||||||
|
file.withReader { reader ->
|
||||||
|
while ((line = reader.readLine())!=null)
|
||||||
|
{
|
||||||
|
println "${line}"
|
||||||
|
noOfLines++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return noOfLines
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* reads file content in list of lines
|
||||||
|
* @param filePath
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
List<String> readFileInList(String filePath) {
|
||||||
|
File file = new File(filePath)
|
||||||
|
def lines = file.readLines()
|
||||||
|
return lines
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* reads file content in string using File.text
|
||||||
|
* @param filePath
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
String readFileString(String filePath) {
|
||||||
|
File file = new File(filePath)
|
||||||
|
String fileContent = file.text
|
||||||
|
return fileContent
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* reads file content in string with encoding using File.getText
|
||||||
|
* @param filePath
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
String readFileStringWithCharset(String filePath) {
|
||||||
|
File file = new File(filePath)
|
||||||
|
String utf8Content = file.getText("UTF-8")
|
||||||
|
return utf8Content
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* reads content of binary file and returns byte array
|
||||||
|
* @param filePath
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
byte[] readBinaryFile(String filePath) {
|
||||||
|
File file = new File(filePath)
|
||||||
|
byte[] binaryContent = file.bytes
|
||||||
|
return binaryContent
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* More Examples of reading a file
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
def moreExamples() {
|
||||||
|
|
||||||
|
//with reader with utf-8
|
||||||
|
new File("src/main/resources/utf8Content.html").withReader('UTF-8') { reader ->
|
||||||
|
def line
|
||||||
|
while ((line = reader.readLine())!=null) {
|
||||||
|
println "${line}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//collect api
|
||||||
|
def list = new File("src/main/resources/fileContent.txt").collect {it}
|
||||||
|
|
||||||
|
//as operator
|
||||||
|
def array = new File("src/main/resources/fileContent.txt") as String[]
|
||||||
|
|
||||||
|
//eachline
|
||||||
|
new File("src/main/resources/fileContent.txt").eachLine { line ->
|
||||||
|
println line
|
||||||
|
}
|
||||||
|
|
||||||
|
//newInputStream with eachLine
|
||||||
|
def is = new File("src/main/resources/fileContent.txt").newInputStream()
|
||||||
|
is.eachLine {
|
||||||
|
println it
|
||||||
|
}
|
||||||
|
is.close()
|
||||||
|
|
||||||
|
//withInputStream
|
||||||
|
new File("src/main/resources/fileContent.txt").withInputStream { stream ->
|
||||||
|
stream.eachLine { line ->
|
||||||
|
println line
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
package com.baeldung.io
|
||||||
|
|
||||||
|
class Task implements Serializable {
|
||||||
|
String description
|
||||||
|
Date startDate
|
||||||
|
Date dueDate
|
||||||
|
int status
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
package com.baeldung.strings;
|
||||||
|
|
||||||
|
class Concatenate {
|
||||||
|
String first = 'Hello'
|
||||||
|
String last = 'Groovy'
|
||||||
|
|
||||||
|
String doSimpleConcat() {
|
||||||
|
return 'My name is ' + first + ' ' + last
|
||||||
|
}
|
||||||
|
|
||||||
|
String doConcatUsingGString() {
|
||||||
|
return "My name is $first $last"
|
||||||
|
}
|
||||||
|
|
||||||
|
String doConcatUsingGStringClosures() {
|
||||||
|
return "My name is ${-> first} ${-> last}"
|
||||||
|
}
|
||||||
|
|
||||||
|
String doConcatUsingStringConcatMethod() {
|
||||||
|
return 'My name is '.concat(first).concat(' ').concat(last)
|
||||||
|
}
|
||||||
|
|
||||||
|
String doConcatUsingLeftShiftOperator() {
|
||||||
|
return 'My name is ' << first << ' ' << last
|
||||||
|
}
|
||||||
|
|
||||||
|
String doConcatUsingArrayJoinMethod() {
|
||||||
|
return ['My name is', first, last].join(' ')
|
||||||
|
}
|
||||||
|
|
||||||
|
String doConcatUsingArrayInjectMethod() {
|
||||||
|
return [first,' ', last]
|
||||||
|
.inject(new StringBuffer('My name is '), { initial, name -> initial.append(name); return initial }).toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
String doConcatUsingStringBuilder() {
|
||||||
|
return new StringBuilder().append('My name is ').append(first).append(' ').append(last)
|
||||||
|
}
|
||||||
|
|
||||||
|
String doConcatUsingStringBuffer() {
|
||||||
|
return new StringBuffer().append('My name is ').append(first).append(' ').append(last)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
package com.baeldung.traits
|
||||||
|
|
||||||
|
trait AnimalTrait {
|
||||||
|
|
||||||
|
String basicBehavior() {
|
||||||
|
return "Animalistic!!"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
package com.baeldung
|
||||||
|
|
||||||
|
class Car implements VehicleTrait {}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.baeldung.traits
|
||||||
|
|
||||||
|
class Dog implements WalkingTrait, SpeakingTrait {
|
||||||
|
|
||||||
|
String speakAndWalk() {
|
||||||
|
WalkingTrait.super.speakAndWalk()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
package com.baeldung.traits
|
||||||
|
|
||||||
|
class Employee implements UserTrait {
|
||||||
|
|
||||||
|
String name() {
|
||||||
|
return 'Bob'
|
||||||
|
}
|
||||||
|
|
||||||
|
String lastName() {
|
||||||
|
return "Marley"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
package com.baeldung.traits
|
||||||
|
|
||||||
|
interface Human {
|
||||||
|
|
||||||
|
String lastName()
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.baeldung.traits
|
||||||
|
|
||||||
|
trait SpeakingTrait {
|
||||||
|
|
||||||
|
String basicAbility() {
|
||||||
|
return "Speaking!!"
|
||||||
|
}
|
||||||
|
|
||||||
|
String speakAndWalk() {
|
||||||
|
return "Speak and walk!!"
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
package com.baeldung.traits
|
||||||
|
|
||||||
|
trait UserTrait implements Human {
|
||||||
|
|
||||||
|
String sayHello() {
|
||||||
|
return "Hello!"
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract String name()
|
||||||
|
|
||||||
|
String showName() {
|
||||||
|
return "Hello, ${name()}!"
|
||||||
|
}
|
||||||
|
|
||||||
|
private String greetingMessage() {
|
||||||
|
return 'Hello, from a private method!'
|
||||||
|
}
|
||||||
|
|
||||||
|
String greet() {
|
||||||
|
def msg = greetingMessage()
|
||||||
|
println msg
|
||||||
|
msg
|
||||||
|
}
|
||||||
|
|
||||||
|
def self() {
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
String showLastName() {
|
||||||
|
return "Hello, ${lastName()}!"
|
||||||
|
}
|
||||||
|
|
||||||
|
String email
|
||||||
|
String address
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.baeldung
|
||||||
|
|
||||||
|
trait VehicleTrait extends WheelTrait {
|
||||||
|
|
||||||
|
String showWheels() {
|
||||||
|
return "Num of Wheels $noOfWheels"
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.baeldung.traits
|
||||||
|
|
||||||
|
trait WalkingTrait {
|
||||||
|
|
||||||
|
String basicAbility() {
|
||||||
|
return "Walking!!"
|
||||||
|
}
|
||||||
|
|
||||||
|
String speakAndWalk() {
|
||||||
|
return "Walk and speak!!"
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package com.baeldung
|
||||||
|
|
||||||
|
trait WheelTrait {
|
||||||
|
|
||||||
|
int noOfWheels
|
||||||
|
|
||||||
|
}
|
Binary file not shown.
After Width: | Height: | Size: 1.1 KiB |
|
@ -0,0 +1,3 @@
|
||||||
|
Line 1 : Hello World!!!
|
||||||
|
Line 2 : This is a file content.
|
||||||
|
Line 3 : String content
|
Binary file not shown.
|
@ -0,0 +1,4 @@
|
||||||
|
First line of text
|
||||||
|
Second line of text
|
||||||
|
Third line of text
|
||||||
|
Fourth line of text
|
|
@ -0,0 +1,3 @@
|
||||||
|
Line one of output example
|
||||||
|
Line two of output example
|
||||||
|
Line three of output example
|
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 329 B |
|
@ -0,0 +1,12 @@
|
||||||
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
||||||
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||||
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<body>
|
||||||
|
<pre>
|
||||||
|
ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ
|
||||||
|
ᛋᚳᛖᚪᛚ᛫ᚦᛖᚪᚻ᛫ᛗᚪᚾᚾᚪ᛫ᚷᛖᚻᚹᛦᛚᚳ᛫ᛗᛁᚳᛚᚢᚾ᛫ᚻᛦᛏ᛫ᛞᚫᛚᚪᚾ
|
||||||
|
ᚷᛁᚠ᛫ᚻᛖ᛫ᚹᛁᛚᛖ᛫ᚠᚩᚱ᛫ᛞᚱᛁᚻᛏᚾᛖ᛫ᛞᚩᛗᛖᛋ᛫ᚻᛚᛇᛏᚪᚾ
|
||||||
|
</pre>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -0,0 +1,57 @@
|
||||||
|
package com.baeldung.groovy.sql
|
||||||
|
|
||||||
|
import static org.junit.Assert.*
|
||||||
|
import java.util.Calendar.*
|
||||||
|
import java.time.LocalDate
|
||||||
|
import java.text.SimpleDateFormat
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
|
||||||
|
class DateTest {
|
||||||
|
|
||||||
|
def dateStr = "2019-02-28"
|
||||||
|
def pattern = "yyyy-MM-dd"
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenGetStringRepresentation_thenCorrectlyConvertIntoDate() {
|
||||||
|
def dateFormat = new SimpleDateFormat(pattern)
|
||||||
|
def date = dateFormat.parse(dateStr)
|
||||||
|
|
||||||
|
println(" String to Date with DateFormatter : " + date)
|
||||||
|
|
||||||
|
def cal = new GregorianCalendar();
|
||||||
|
cal.setTime(date);
|
||||||
|
|
||||||
|
assertEquals(cal.get(Calendar.YEAR),2019)
|
||||||
|
assertEquals(cal.get(Calendar.DAY_OF_MONTH),28)
|
||||||
|
assertEquals(cal.get(Calendar.MONTH),java.util.Calendar.FEBRUARY)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenGetStringRepresentation_thenCorrectlyConvertWithDateUtilsExtension() {
|
||||||
|
|
||||||
|
def date = Date.parse(pattern, dateStr)
|
||||||
|
|
||||||
|
println(" String to Date with Date.parse : " + date)
|
||||||
|
|
||||||
|
def cal = new GregorianCalendar();
|
||||||
|
cal.setTime(date);
|
||||||
|
|
||||||
|
assertEquals(cal.get(Calendar.YEAR),2019)
|
||||||
|
assertEquals(cal.get(Calendar.DAY_OF_MONTH),28)
|
||||||
|
assertEquals(cal.get(Calendar.MONTH),java.util.Calendar.FEBRUARY)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenGetStringRepresentation_thenCorrectlyConvertIntoDateWithLocalDate() {
|
||||||
|
def date = LocalDate.parse(dateStr, pattern)
|
||||||
|
|
||||||
|
println(" String to Date with LocalDate : " + date)
|
||||||
|
|
||||||
|
assertEquals(date.getYear(),2019)
|
||||||
|
assertEquals(date.getMonth(),java.time.Month.FEBRUARY)
|
||||||
|
assertEquals(date.getDayOfMonth(),28)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,70 @@
|
||||||
|
package com.baeldung.file
|
||||||
|
|
||||||
|
import spock.lang.Specification
|
||||||
|
|
||||||
|
class ReadFileUnitTest extends Specification {
|
||||||
|
|
||||||
|
ReadFile readFile
|
||||||
|
|
||||||
|
void setup () {
|
||||||
|
readFile = new ReadFile()
|
||||||
|
}
|
||||||
|
|
||||||
|
def 'Should return number of lines in File using ReadFile.readFileLineByLine given filePath' () {
|
||||||
|
given:
|
||||||
|
def filePath = "src/main/resources/fileContent.txt"
|
||||||
|
when:
|
||||||
|
def noOfLines = readFile.readFileLineByLine(filePath)
|
||||||
|
then:
|
||||||
|
noOfLines
|
||||||
|
noOfLines instanceof Integer
|
||||||
|
assert noOfLines, 3
|
||||||
|
}
|
||||||
|
|
||||||
|
def 'Should return File Content in list of lines using ReadFile.readFileInList given filePath' () {
|
||||||
|
given:
|
||||||
|
def filePath = "src/main/resources/fileContent.txt"
|
||||||
|
when:
|
||||||
|
def lines = readFile.readFileInList(filePath)
|
||||||
|
then:
|
||||||
|
lines
|
||||||
|
lines instanceof List<String>
|
||||||
|
assert lines.size(), 3
|
||||||
|
}
|
||||||
|
|
||||||
|
def 'Should return file content in string using ReadFile.readFileString given filePath' () {
|
||||||
|
given:
|
||||||
|
def filePath = "src/main/resources/fileContent.txt"
|
||||||
|
when:
|
||||||
|
def fileContent = readFile.readFileString(filePath)
|
||||||
|
then:
|
||||||
|
fileContent
|
||||||
|
fileContent instanceof String
|
||||||
|
fileContent.contains("""Line 1 : Hello World!!!
|
||||||
|
Line 2 : This is a file content.
|
||||||
|
Line 3 : String content""")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
def 'Should return UTF-8 encoded file content in string using ReadFile.readFileStringWithCharset given filePath' () {
|
||||||
|
given:
|
||||||
|
def filePath = "src/main/resources/utf8Content.html"
|
||||||
|
when:
|
||||||
|
def encodedContent = readFile.readFileStringWithCharset(filePath)
|
||||||
|
then:
|
||||||
|
encodedContent
|
||||||
|
encodedContent instanceof String
|
||||||
|
}
|
||||||
|
|
||||||
|
def 'Should return binary file content in byte array using ReadFile.readBinaryFile given filePath' () {
|
||||||
|
given:
|
||||||
|
def filePath = "src/main/resources/sample.png"
|
||||||
|
when:
|
||||||
|
def binaryContent = readFile.readBinaryFile(filePath)
|
||||||
|
then:
|
||||||
|
binaryContent
|
||||||
|
binaryContent instanceof byte[]
|
||||||
|
binaryContent.length == 329
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
package com.baeldung.io
|
||||||
|
|
||||||
|
import static org.junit.Assert.*
|
||||||
|
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class DataAndObjectsUnitTest {
|
||||||
|
@Test
|
||||||
|
void whenUsingWithDataOutputStream_thenDataIsSerializedToAFile() {
|
||||||
|
String message = 'This is a serialized string'
|
||||||
|
int length = message.length()
|
||||||
|
boolean valid = true
|
||||||
|
new File('src/main/resources/ioData.txt').withDataOutputStream { out ->
|
||||||
|
out.writeUTF(message)
|
||||||
|
out.writeInt(length)
|
||||||
|
out.writeBoolean(valid)
|
||||||
|
}
|
||||||
|
|
||||||
|
String loadedMessage = ""
|
||||||
|
int loadedLength
|
||||||
|
boolean loadedValid
|
||||||
|
|
||||||
|
new File('src/main/resources/ioData.txt').withDataInputStream { is ->
|
||||||
|
loadedMessage = is.readUTF()
|
||||||
|
loadedLength = is.readInt()
|
||||||
|
loadedValid = is.readBoolean()
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(message, loadedMessage)
|
||||||
|
assertEquals(length, loadedLength)
|
||||||
|
assertEquals(valid, loadedValid)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingWithObjectOutputStream_thenObjectIsSerializedToFile() {
|
||||||
|
Task task = new Task(description:'Take out the trash', startDate:new Date(), status:0)
|
||||||
|
new File('src/main/resources/ioSerializedObject.txt').withObjectOutputStream { out ->
|
||||||
|
out.writeObject(task)
|
||||||
|
}
|
||||||
|
|
||||||
|
Task taskRead
|
||||||
|
|
||||||
|
new File('src/main/resources/ioSerializedObject.txt').withObjectInputStream { is ->
|
||||||
|
taskRead = is.readObject()
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(task.description, taskRead.description)
|
||||||
|
assertEquals(task.startDate, taskRead.startDate)
|
||||||
|
assertEquals(task.status, taskRead.status)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,134 @@
|
||||||
|
package com.baeldung.io
|
||||||
|
|
||||||
|
import static org.junit.Assert.*
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class ReadExampleUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingEachLine_thenCorrectLinesReturned() {
|
||||||
|
def expectedList = [
|
||||||
|
'First line of text',
|
||||||
|
'Second line of text',
|
||||||
|
'Third line of text',
|
||||||
|
'Fourth line of text']
|
||||||
|
|
||||||
|
def lines = []
|
||||||
|
|
||||||
|
new File('src/main/resources/ioInput.txt').eachLine { line ->
|
||||||
|
lines.add(line)
|
||||||
|
}
|
||||||
|
assertEquals(expectedList, lines)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingReadEachLineWithLineNumber_thenCorrectLinesReturned() {
|
||||||
|
def expectedList = [
|
||||||
|
'Second line of text',
|
||||||
|
'Third line of text',
|
||||||
|
'Fourth line of text']
|
||||||
|
|
||||||
|
def lineNoRange = 2..4
|
||||||
|
def lines = []
|
||||||
|
|
||||||
|
new File('src/main/resources/ioInput.txt').eachLine { line, lineNo ->
|
||||||
|
if (lineNoRange.contains(lineNo)) {
|
||||||
|
lines.add(line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertEquals(expectedList, lines)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingReadEachLineWithLineNumberStartAtZero_thenCorrectLinesReturned() {
|
||||||
|
def expectedList = [
|
||||||
|
'Second line of text',
|
||||||
|
'Third line of text',
|
||||||
|
'Fourth line of text']
|
||||||
|
|
||||||
|
def lineNoRange = 1..3
|
||||||
|
def lines = []
|
||||||
|
|
||||||
|
new File('src/main/resources/ioInput.txt').eachLine(0, { line, lineNo ->
|
||||||
|
if (lineNoRange.contains(lineNo)) {
|
||||||
|
lines.add(line)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
assertEquals(expectedList, lines)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingWithReader_thenLineCountReturned() {
|
||||||
|
def expectedCount = 4
|
||||||
|
def actualCount = 0
|
||||||
|
new File('src/main/resources/ioInput.txt').withReader { reader ->
|
||||||
|
while(reader.readLine()) {
|
||||||
|
actualCount++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertEquals(expectedCount, actualCount)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingNewReader_thenOutputFileCreated() {
|
||||||
|
def outputPath = 'src/main/resources/ioOut.txt'
|
||||||
|
def reader = new File('src/main/resources/ioInput.txt').newReader()
|
||||||
|
new File(outputPath).append(reader)
|
||||||
|
reader.close()
|
||||||
|
def ioOut = new File(outputPath)
|
||||||
|
assertTrue(ioOut.exists())
|
||||||
|
ioOut.delete()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingWithInputStream_thenCorrectBytesAreReturned() {
|
||||||
|
def expectedLength = 1139
|
||||||
|
byte[] data = []
|
||||||
|
new File("src/main/resources/binaryExample.jpg").withInputStream { stream ->
|
||||||
|
data = stream.getBytes()
|
||||||
|
}
|
||||||
|
assertEquals(expectedLength, data.length)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingNewInputStream_thenOutputFileCreated() {
|
||||||
|
def outputPath = 'src/main/resources/binaryOut.jpg'
|
||||||
|
def is = new File('src/main/resources/binaryExample.jpg').newInputStream()
|
||||||
|
new File(outputPath).append(is)
|
||||||
|
is.close()
|
||||||
|
def ioOut = new File(outputPath)
|
||||||
|
assertTrue(ioOut.exists())
|
||||||
|
ioOut.delete()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingCollect_thenCorrectListIsReturned() {
|
||||||
|
def expectedList = ['First line of text', 'Second line of text', 'Third line of text', 'Fourth line of text']
|
||||||
|
|
||||||
|
def actualList = new File('src/main/resources/ioInput.txt').collect {it}
|
||||||
|
assertEquals(expectedList, actualList)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingAsStringArray_thenCorrectArrayIsReturned() {
|
||||||
|
String[] expectedArray = ['First line of text', 'Second line of text', 'Third line of text', 'Fourth line of text']
|
||||||
|
|
||||||
|
def actualArray = new File('src/main/resources/ioInput.txt') as String[]
|
||||||
|
assertArrayEquals(expectedArray, actualArray)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingText_thenCorrectStringIsReturned() {
|
||||||
|
def ln = System.getProperty('line.separator')
|
||||||
|
def expectedString = "First line of text${ln}Second line of text${ln}Third line of text${ln}Fourth line of text"
|
||||||
|
def actualString = new File('src/main/resources/ioInput.txt').text
|
||||||
|
assertEquals(expectedString.toString(), actualString)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingBytes_thenByteArrayIsReturned() {
|
||||||
|
def expectedLength = 1139
|
||||||
|
def contents = new File('src/main/resources/binaryExample.jpg').bytes
|
||||||
|
assertEquals(expectedLength, contents.length)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,61 @@
|
||||||
|
package com.baeldung.io
|
||||||
|
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
import groovy.io.FileType
|
||||||
|
import groovy.io.FileVisitResult
|
||||||
|
|
||||||
|
class TraverseFileTreeUnitTest {
|
||||||
|
@Test
|
||||||
|
void whenUsingEachFile_filesAreListed() {
|
||||||
|
new File('src/main/resources').eachFile { file ->
|
||||||
|
println file.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException)
|
||||||
|
void whenUsingEachFileOnAFile_anErrorOccurs() {
|
||||||
|
new File('src/main/resources/ioInput.txt').eachFile { file ->
|
||||||
|
println file.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingEachFileMatch_filesAreListed() {
|
||||||
|
new File('src/main/resources').eachFileMatch(~/io.*\.txt/) { file ->
|
||||||
|
println file.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingEachFileRecurse_thenFilesInSubfoldersAreListed() {
|
||||||
|
new File('src/main').eachFileRecurse(FileType.FILES) { file ->
|
||||||
|
println "$file.parent $file.name"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingEachFileRecurse_thenDirsInSubfoldersAreListed() {
|
||||||
|
new File('src/main').eachFileRecurse(FileType.DIRECTORIES) { file ->
|
||||||
|
println "$file.parent $file.name"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingEachDirRecurse_thenDirsAndSubDirsAreListed() {
|
||||||
|
new File('src/main').eachDirRecurse { dir ->
|
||||||
|
println "$dir.parent $dir.name"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingTraverse_thenDirectoryIsTraversed() {
|
||||||
|
new File('src/main').traverse { file ->
|
||||||
|
if (file.directory && file.name == 'groovy') {
|
||||||
|
FileVisitResult.SKIP_SUBTREE
|
||||||
|
} else {
|
||||||
|
println "$file.parent - $file.name"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,96 @@
|
||||||
|
package com.baeldung.io
|
||||||
|
|
||||||
|
import static org.junit.Assert.*
|
||||||
|
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class WriteExampleUnitTest {
|
||||||
|
@Before
|
||||||
|
void clearOutputFile() {
|
||||||
|
new File('src/main/resources/ioOutput.txt').text = ''
|
||||||
|
new File('src/main/resources/ioBinaryOutput.bin').delete()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingWithWriter_thenFileCreated() {
|
||||||
|
def outputLines = [
|
||||||
|
'Line one of output example',
|
||||||
|
'Line two of output example',
|
||||||
|
'Line three of output example'
|
||||||
|
]
|
||||||
|
|
||||||
|
def outputFileName = 'src/main/resources/ioOutput.txt'
|
||||||
|
new File(outputFileName).withWriter { writer ->
|
||||||
|
outputLines.each { line ->
|
||||||
|
writer.writeLine line
|
||||||
|
}
|
||||||
|
}
|
||||||
|
def writtenLines = new File(outputFileName).collect {it}
|
||||||
|
assertEquals(outputLines, writtenLines)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingNewWriter_thenFileCreated() {
|
||||||
|
def outputLines = [
|
||||||
|
'Line one of output example',
|
||||||
|
'Line two of output example',
|
||||||
|
'Line three of output example'
|
||||||
|
]
|
||||||
|
|
||||||
|
def outputFileName = 'src/main/resources/ioOutput.txt'
|
||||||
|
def writer = new File(outputFileName).newWriter()
|
||||||
|
outputLines.forEach {line ->
|
||||||
|
writer.writeLine line
|
||||||
|
}
|
||||||
|
writer.flush()
|
||||||
|
writer.close()
|
||||||
|
|
||||||
|
def writtenLines = new File(outputFileName).collect {it}
|
||||||
|
assertEquals(outputLines, writtenLines)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingDoubleLessThanOperator_thenFileCreated() {
|
||||||
|
def outputLines = [
|
||||||
|
'Line one of output example',
|
||||||
|
'Line two of output example',
|
||||||
|
'Line three of output example'
|
||||||
|
]
|
||||||
|
|
||||||
|
def ln = System.getProperty('line.separator')
|
||||||
|
def outputFileName = 'src/main/resources/ioOutput.txt'
|
||||||
|
new File(outputFileName) << "Line one of output example${ln}Line two of output example${ln}Line three of output example"
|
||||||
|
def writtenLines = new File(outputFileName).collect {it}
|
||||||
|
assertEquals(outputLines.size(), writtenLines.size())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingBytes_thenBinaryFileCreated() {
|
||||||
|
def outputFileName = 'src/main/resources/ioBinaryOutput.bin'
|
||||||
|
def outputFile = new File(outputFileName)
|
||||||
|
byte[] outBytes = [44, 88, 22]
|
||||||
|
outputFile.bytes = outBytes
|
||||||
|
assertEquals(3, new File(outputFileName).size())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingWithOutputStream_thenBinaryFileCreated() {
|
||||||
|
def outputFileName = 'src/main/resources/ioBinaryOutput.bin'
|
||||||
|
byte[] outBytes = [44, 88, 22]
|
||||||
|
new File(outputFileName).withOutputStream { stream ->
|
||||||
|
stream.write(outBytes)
|
||||||
|
}
|
||||||
|
assertEquals(3, new File(outputFileName).size())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingNewOutputStream_thenBinaryFileCreated() {
|
||||||
|
def outputFileName = 'src/main/resources/ioBinaryOutput.bin'
|
||||||
|
byte[] outBytes = [44, 88, 22]
|
||||||
|
def os = new File(outputFileName).newOutputStream()
|
||||||
|
os.write(outBytes)
|
||||||
|
os.close()
|
||||||
|
assertEquals(3, new File(outputFileName).size())
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,173 @@
|
||||||
|
package com.baeldung.groovy.lists
|
||||||
|
|
||||||
|
import static groovy.test.GroovyAssert.*
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class ListTest{
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCreateList() {
|
||||||
|
|
||||||
|
def list = [1, 2, 3]
|
||||||
|
assertNotNull(list)
|
||||||
|
|
||||||
|
def listMix = ['A', "b", 1, true]
|
||||||
|
assertTrue(listMix == ['A', "b", 1, true])
|
||||||
|
|
||||||
|
def linkedList = [1, 2, 3] as LinkedList
|
||||||
|
assertTrue(linkedList instanceof LinkedList)
|
||||||
|
|
||||||
|
ArrayList arrList = [1, 2, 3]
|
||||||
|
assertTrue(arrList.class == ArrayList)
|
||||||
|
|
||||||
|
def copyList = new ArrayList(arrList)
|
||||||
|
assertTrue(copyList == arrList)
|
||||||
|
|
||||||
|
def cloneList = arrList.clone()
|
||||||
|
assertTrue(cloneList == arrList)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCreateEmptyList() {
|
||||||
|
|
||||||
|
def emptyList = []
|
||||||
|
assertTrue(emptyList.size() == 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCompareTwoLists() {
|
||||||
|
|
||||||
|
def list1 = [5, 6.0, 'p']
|
||||||
|
def list2 = [5, 6.0, 'p']
|
||||||
|
assertTrue(list1 == list2)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetItemsFromList(){
|
||||||
|
|
||||||
|
def list = ["Hello", "World"]
|
||||||
|
|
||||||
|
assertTrue(list.get(1) == "World")
|
||||||
|
assertTrue(list[1] == "World")
|
||||||
|
assertTrue(list[-1] == "World")
|
||||||
|
assertTrue(list.getAt(1) == "World")
|
||||||
|
assertTrue(list.getAt(-2) == "Hello")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testAddItemsToList() {
|
||||||
|
|
||||||
|
def list = []
|
||||||
|
|
||||||
|
list << 1
|
||||||
|
list.add("Apple")
|
||||||
|
assertTrue(list == [1, "Apple"])
|
||||||
|
|
||||||
|
list[2] = "Box"
|
||||||
|
list[4] = true
|
||||||
|
assertTrue(list == [1, "Apple", "Box", null, true])
|
||||||
|
|
||||||
|
list.add(1, 6.0)
|
||||||
|
assertTrue(list == [1, 6.0, "Apple", "Box", null, true])
|
||||||
|
|
||||||
|
def list2 = [1, 2]
|
||||||
|
list += list2
|
||||||
|
list += 12
|
||||||
|
assertTrue(list == [1, 6.0, "Apple", "Box", null, true, 1, 2, 12])
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUpdateItemsInList() {
|
||||||
|
|
||||||
|
def list =[1, "Apple", 80, "App"]
|
||||||
|
list[1] = "Box"
|
||||||
|
list.set(2,90)
|
||||||
|
assertTrue(list == [1, "Box", 90, "App"])
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRemoveItemsFromList(){
|
||||||
|
|
||||||
|
def list = [1, 2, 3, 4, 5, 5, 6, 6, 7]
|
||||||
|
|
||||||
|
list.remove(3)
|
||||||
|
assertTrue(list == [1, 2, 3, 5, 5, 6, 6, 7])
|
||||||
|
|
||||||
|
list.removeElement(5)
|
||||||
|
assertTrue(list == [1, 2, 3, 5, 6, 6, 7])
|
||||||
|
|
||||||
|
assertTrue(list - 6 == [1, 2, 3, 5, 7])
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIteratingOnAList(){
|
||||||
|
|
||||||
|
def list = [1, "App", 3, 4]
|
||||||
|
list.each{ println it * 2}
|
||||||
|
|
||||||
|
list.eachWithIndex{ it, i -> println "$i : $it" }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCollectingToAnotherList(){
|
||||||
|
|
||||||
|
def list = ["Kay", "Henry", "Justin", "Tom"]
|
||||||
|
assertTrue(list.collect{"Hi " + it} == ["Hi Kay", "Hi Henry", "Hi Justin", "Hi Tom"])
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testJoinItemsInAList(){
|
||||||
|
assertTrue(["One", "Two", "Three"].join(",") == "One,Two,Three")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testFilteringOnLists(){
|
||||||
|
def filterList = [2, 1, 3, 4, 5, 6, 76]
|
||||||
|
|
||||||
|
assertTrue(filterList.find{it > 3} == 4)
|
||||||
|
|
||||||
|
assertTrue(filterList.findAll{it > 3} == [4, 5, 6, 76])
|
||||||
|
|
||||||
|
assertTrue(filterList.findAll{ it instanceof Number} == [2, 1, 3, 4, 5, 6, 76])
|
||||||
|
|
||||||
|
assertTrue(filterList.grep( Number )== [2, 1, 3, 4, 5, 6, 76])
|
||||||
|
|
||||||
|
assertTrue(filterList.grep{ it> 6 }== [76])
|
||||||
|
|
||||||
|
def conditionList = [2, 1, 3, 4, 5, 6, 76]
|
||||||
|
|
||||||
|
assertFalse(conditionList.every{ it < 6})
|
||||||
|
|
||||||
|
assertTrue(conditionList.any{ it%2 == 0})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetUniqueItemsInAList(){
|
||||||
|
assertTrue([1, 3, 3, 4].toUnique() == [1, 3, 4])
|
||||||
|
|
||||||
|
def uniqueList = [1, 3, 3, 4]
|
||||||
|
uniqueList.unique()
|
||||||
|
assertTrue(uniqueList == [1, 3, 4])
|
||||||
|
|
||||||
|
assertTrue(["A", "B", "Ba", "Bat", "Cat"].toUnique{ it.size()} == ["A", "Ba", "Bat"])
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSorting(){
|
||||||
|
|
||||||
|
assertTrue([1, 2, 1, 0].sort() == [0, 1, 1, 2])
|
||||||
|
Comparator mc = {a,b -> a == b? 0: a < b? 1 : -1}
|
||||||
|
|
||||||
|
def list = [1, 2, 1, 0]
|
||||||
|
list.sort(mc)
|
||||||
|
assertTrue(list == [2, 1, 1, 0])
|
||||||
|
|
||||||
|
def strList = ["na", "ppp", "as"]
|
||||||
|
assertTrue(strList.max() == "ppp")
|
||||||
|
|
||||||
|
Comparator minc = {a,b -> a == b? 0: a < b? -1 : 1}
|
||||||
|
def numberList = [3, 2, 0, 7]
|
||||||
|
assertTrue(numberList.min(minc) == 0)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,85 @@
|
||||||
|
package com.baeldung.map
|
||||||
|
|
||||||
|
import static org.junit.Assert.*
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class MapUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingEach_thenMapIsIterated() {
|
||||||
|
def map = [
|
||||||
|
'FF0000' : 'Red',
|
||||||
|
'00FF00' : 'Lime',
|
||||||
|
'0000FF' : 'Blue',
|
||||||
|
'FFFF00' : 'Yellow'
|
||||||
|
]
|
||||||
|
|
||||||
|
map.each { println "Hex Code: $it.key = Color Name: $it.value" }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingEachWithEntry_thenMapIsIterated() {
|
||||||
|
def map = [
|
||||||
|
'E6E6FA' : 'Lavender',
|
||||||
|
'D8BFD8' : 'Thistle',
|
||||||
|
'DDA0DD' : 'Plum',
|
||||||
|
]
|
||||||
|
|
||||||
|
map.each { entry -> println "Hex Code: $entry.key = Color Name: $entry.value" }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingEachWithKeyAndValue_thenMapIsIterated() {
|
||||||
|
def map = [
|
||||||
|
'000000' : 'Black',
|
||||||
|
'FFFFFF' : 'White',
|
||||||
|
'808080' : 'Gray'
|
||||||
|
]
|
||||||
|
|
||||||
|
map.each { key, val ->
|
||||||
|
println "Hex Code: $key = Color Name $val"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingEachWithIndexAndEntry_thenMapIsIterated() {
|
||||||
|
def map = [
|
||||||
|
'800080' : 'Purple',
|
||||||
|
'4B0082' : 'Indigo',
|
||||||
|
'6A5ACD' : 'Slate Blue'
|
||||||
|
]
|
||||||
|
|
||||||
|
map.eachWithIndex { entry, index ->
|
||||||
|
def indent = ((index == 0 || index % 2 == 0) ? " " : "")
|
||||||
|
println "$indent Hex Code: $entry.key = Color Name: $entry.value"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingEachWithIndexAndKeyAndValue_thenMapIsIterated() {
|
||||||
|
def map = [
|
||||||
|
'FFA07A' : 'Light Salmon',
|
||||||
|
'FF7F50' : 'Coral',
|
||||||
|
'FF6347' : 'Tomato',
|
||||||
|
'FF4500' : 'Orange Red'
|
||||||
|
]
|
||||||
|
|
||||||
|
map.eachWithIndex { key, val, index ->
|
||||||
|
def indent = ((index == 0 || index % 2 == 0) ? " " : "")
|
||||||
|
println "$indent Hex Code: $key = Color Name: $val"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingForLoop_thenMapIsIterated() {
|
||||||
|
def map = [
|
||||||
|
'2E8B57' : 'Seagreen',
|
||||||
|
'228B22' : 'Forest Green',
|
||||||
|
'008000' : 'Green'
|
||||||
|
]
|
||||||
|
|
||||||
|
for (entry in map) {
|
||||||
|
println "Hex Code: $entry.key = Color Name: $entry.value"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,101 @@
|
||||||
|
import com.baeldung.strings.Concatenate;
|
||||||
|
|
||||||
|
class ConcatenateTest extends GroovyTestCase {
|
||||||
|
|
||||||
|
void testSimpleConcat() {
|
||||||
|
def name = new Concatenate()
|
||||||
|
name.first = 'Joe';
|
||||||
|
name.last = 'Smith';
|
||||||
|
def expected = 'My name is Joe Smith'
|
||||||
|
assertToString(name.doSimpleConcat(), expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
void testConcatUsingGString() {
|
||||||
|
def name = new Concatenate()
|
||||||
|
name.first = "Joe";
|
||||||
|
name.last = "Smith";
|
||||||
|
def expected = "My name is Joe Smith"
|
||||||
|
assertToString(name.doConcatUsingGString(), expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
void testConcatUsingGStringClosures() {
|
||||||
|
def name = new Concatenate()
|
||||||
|
name.first = "Joe";
|
||||||
|
name.last = "Smith";
|
||||||
|
def expected = "My name is Joe Smith"
|
||||||
|
assertToString(name.doConcatUsingGStringClosures(), expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
void testConcatUsingStringConcatMethod() {
|
||||||
|
def name = new Concatenate()
|
||||||
|
name.first = "Joe";
|
||||||
|
name.last = "Smith";
|
||||||
|
def expected = "My name is Joe Smith"
|
||||||
|
assertToString(name.doConcatUsingStringConcatMethod(), expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
void testConcatUsingLeftShiftOperator() {
|
||||||
|
def name = new Concatenate()
|
||||||
|
name.first = "Joe";
|
||||||
|
name.last = "Smith";
|
||||||
|
def expected = "My name is Joe Smith"
|
||||||
|
assertToString(name.doConcatUsingLeftShiftOperator(), expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
void testConcatUsingArrayJoinMethod() {
|
||||||
|
def name = new Concatenate()
|
||||||
|
name.first = "Joe";
|
||||||
|
name.last = "Smith";
|
||||||
|
def expected = "My name is Joe Smith"
|
||||||
|
assertToString(name.doConcatUsingArrayJoinMethod(), expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
void testConcatUsingArrayInjectMethod() {
|
||||||
|
def name = new Concatenate()
|
||||||
|
name.first = "Joe";
|
||||||
|
name.last = "Smith";
|
||||||
|
def expected = "My name is Joe Smith"
|
||||||
|
assertToString(name.doConcatUsingArrayInjectMethod(), expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
void testConcatUsingStringBuilder() {
|
||||||
|
def name = new Concatenate()
|
||||||
|
name.first = "Joe";
|
||||||
|
name.last = "Smith";
|
||||||
|
def expected = "My name is Joe Smith"
|
||||||
|
assertToString(name.doConcatUsingStringBuilder(), expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
void testConcatUsingStringBuffer() {
|
||||||
|
def name = new Concatenate()
|
||||||
|
name.first = "Joe";
|
||||||
|
name.last = "Smith";
|
||||||
|
def expected = "My name is Joe Smith"
|
||||||
|
assertToString(name.doConcatUsingStringBuffer(), expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
void testConcatMultilineUsingStringConcatMethod() {
|
||||||
|
def name = new Concatenate()
|
||||||
|
name.first = '''Joe
|
||||||
|
Smith
|
||||||
|
''';
|
||||||
|
name.last = 'Junior';
|
||||||
|
def expected = '''My name is Joe
|
||||||
|
Smith
|
||||||
|
Junior''';
|
||||||
|
assertToString(name.doConcatUsingStringConcatMethod(), expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
void testGStringvsClosure(){
|
||||||
|
def first = "Joe";
|
||||||
|
def last = "Smith";
|
||||||
|
def eagerGString = "My name is $first $last"
|
||||||
|
def lazyGString = "My name is ${-> first} ${-> last}"
|
||||||
|
|
||||||
|
assert eagerGString == "My name is Joe Smith"
|
||||||
|
assert lazyGString == "My name is Joe Smith"
|
||||||
|
first = "David";
|
||||||
|
assert eagerGString == "My name is Joe Smith"
|
||||||
|
assert lazyGString == "My name is David Smith"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
package groovy.com.baeldung.stringtypes
|
||||||
|
|
||||||
|
import org.junit.Assert
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class CharacterInGroovy {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'character'() {
|
||||||
|
char a = 'A' as char
|
||||||
|
char b = 'B' as char
|
||||||
|
char c = (char) 'C'
|
||||||
|
|
||||||
|
Assert.assertTrue(a instanceof Character)
|
||||||
|
Assert.assertTrue(b instanceof Character)
|
||||||
|
Assert.assertTrue(c instanceof Character)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package groovy.com.baeldung.stringtypes
|
||||||
|
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class DollarSlashyString {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'dollar slashy string'() {
|
||||||
|
def name = "John"
|
||||||
|
|
||||||
|
def dollarSlashy = $/
|
||||||
|
Hello $name!,
|
||||||
|
|
||||||
|
I can show you $ sign or escaped dollar sign: $$
|
||||||
|
Both slashes works: \ or /, but we can still escape it: $/
|
||||||
|
|
||||||
|
We have to escape opening and closing delimiter:
|
||||||
|
- $$$/
|
||||||
|
- $/$$
|
||||||
|
/$
|
||||||
|
|
||||||
|
print(dollarSlashy)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,67 @@
|
||||||
|
package groovy.com.baeldung.stringtypes
|
||||||
|
|
||||||
|
import org.junit.Assert
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class DoubleQuotedString {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'escape double quoted string'() {
|
||||||
|
def example = "Hello \"world\"!"
|
||||||
|
|
||||||
|
println(example)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'String ang GString'() {
|
||||||
|
def string = "example"
|
||||||
|
def stringWithExpression = "example${2}"
|
||||||
|
|
||||||
|
Assert.assertTrue(string instanceof String)
|
||||||
|
Assert.assertTrue(stringWithExpression instanceof GString)
|
||||||
|
Assert.assertTrue(stringWithExpression.toString() instanceof String)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'placeholder with variable'() {
|
||||||
|
def name = "John"
|
||||||
|
def helloName = "Hello $name!".toString()
|
||||||
|
|
||||||
|
Assert.assertEquals("Hello John!", helloName)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'placeholder with expression'() {
|
||||||
|
def result = "result is ${2 * 2}".toString()
|
||||||
|
|
||||||
|
Assert.assertEquals("result is 4", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'placeholder with dotted access'() {
|
||||||
|
def person = [name: 'John']
|
||||||
|
|
||||||
|
def myNameIs = "I'm $person.name, and you?".toString()
|
||||||
|
|
||||||
|
Assert.assertEquals("I'm John, and you?", myNameIs)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'placeholder with method call'() {
|
||||||
|
def name = 'John'
|
||||||
|
|
||||||
|
def result = "Uppercase name: ${name.toUpperCase()}".toString()
|
||||||
|
|
||||||
|
Assert.assertEquals("Uppercase name: JOHN", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'GString and String hashcode'() {
|
||||||
|
def string = "2+2 is 4"
|
||||||
|
def gstring = "2+2 is ${4}"
|
||||||
|
|
||||||
|
Assert.assertTrue(string.hashCode() != gstring.hashCode())
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
package groovy.com.baeldung.stringtypes
|
||||||
|
|
||||||
|
import org.junit.Assert
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class SingleQuotedString {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'single quoted string'() {
|
||||||
|
def example = 'Hello world'
|
||||||
|
|
||||||
|
Assert.assertEquals('Hello world!', 'Hello' + ' world!')
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
package groovy.com.baeldung.stringtypes
|
||||||
|
|
||||||
|
import org.junit.Assert
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class SlashyString {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'slashy string'() {
|
||||||
|
def pattern = /.*foobar.*\/hello.*/
|
||||||
|
|
||||||
|
Assert.assertTrue("I'm matching foobar /hello regexp pattern".matches(pattern))
|
||||||
|
}
|
||||||
|
|
||||||
|
void 'wont compile'() {
|
||||||
|
// if ('' == //) {
|
||||||
|
// println("I can't compile")
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'interpolate and multiline'() {
|
||||||
|
def name = 'John'
|
||||||
|
|
||||||
|
def example = /
|
||||||
|
Hello $name
|
||||||
|
second line
|
||||||
|
/
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package groovy.com.baeldung.stringtypes
|
||||||
|
|
||||||
|
import org.junit.Assert
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class Strings {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'string interpolation '() {
|
||||||
|
def name = "Kacper"
|
||||||
|
|
||||||
|
def result = "Hello ${name}!"
|
||||||
|
|
||||||
|
Assert.assertEquals("Hello Kacper!", result.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'string concatenation'() {
|
||||||
|
def first = "first"
|
||||||
|
def second = "second"
|
||||||
|
|
||||||
|
def concatenation = first + second
|
||||||
|
|
||||||
|
Assert.assertEquals("firstsecond", concatenation)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
package groovy.com.baeldung.stringtypes
|
||||||
|
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class TripleDoubleQuotedString {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'triple-quoted strings with interpolation'() {
|
||||||
|
def name = "John"
|
||||||
|
|
||||||
|
def multiLine = """
|
||||||
|
I'm $name.
|
||||||
|
"This is quotation"
|
||||||
|
"""
|
||||||
|
|
||||||
|
println(multiLine)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,67 @@
|
||||||
|
package groovy.com.baeldung.stringtypes
|
||||||
|
|
||||||
|
import org.junit.Assert
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class TripleSingleQuotedString {
|
||||||
|
|
||||||
|
def 'formatted json'() {
|
||||||
|
def jsonContent = '''
|
||||||
|
{
|
||||||
|
"name": "John",
|
||||||
|
"age": 20,
|
||||||
|
"birthDate": null
|
||||||
|
}
|
||||||
|
'''
|
||||||
|
}
|
||||||
|
|
||||||
|
def 'triple single quoted'() {
|
||||||
|
def triple = '''im triple single quoted string'''
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'triple single quoted with multiline string'() {
|
||||||
|
def triple = '''
|
||||||
|
firstline
|
||||||
|
secondline
|
||||||
|
'''
|
||||||
|
|
||||||
|
Assert.assertTrue(triple.startsWith("\n"))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'triple single quoted with multiline string with stripIndent() and removing newline characters'() {
|
||||||
|
def triple = '''\
|
||||||
|
firstline
|
||||||
|
secondline'''.stripIndent()
|
||||||
|
|
||||||
|
Assert.assertEquals("firstline\nsecondline", triple)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'triple single quoted with multiline string with last line with only whitespaces'() {
|
||||||
|
def triple = '''\
|
||||||
|
firstline
|
||||||
|
secondline\
|
||||||
|
'''.stripIndent()
|
||||||
|
|
||||||
|
println(triple)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'triple single quoted with multiline string with stripMargin(Character) and removing newline characters'() {
|
||||||
|
def triple = '''\
|
||||||
|
|firstline
|
||||||
|
|secondline'''.stripMargin()
|
||||||
|
|
||||||
|
println(triple)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'striple single quoted with special characters'() {
|
||||||
|
def specialCharacters = '''hello \'John\'. This is backslash - \\. \nSecond line starts here'''
|
||||||
|
|
||||||
|
println(specialCharacters)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,114 @@
|
||||||
|
package com.baeldung.traits
|
||||||
|
|
||||||
|
import spock.lang.Specification
|
||||||
|
|
||||||
|
class TraitsUnitTest extends Specification {
|
||||||
|
|
||||||
|
Employee employee
|
||||||
|
Dog dog
|
||||||
|
|
||||||
|
void setup () {
|
||||||
|
employee = new Employee()
|
||||||
|
dog = new Dog()
|
||||||
|
}
|
||||||
|
|
||||||
|
def 'Should return msg string when using Employee.sayHello method provided by UserTrait' () {
|
||||||
|
when:
|
||||||
|
def msg = employee.sayHello()
|
||||||
|
then:
|
||||||
|
msg
|
||||||
|
msg instanceof String
|
||||||
|
assert msg == "Hello!"
|
||||||
|
}
|
||||||
|
|
||||||
|
def 'Should return displayMsg string when using Employee.showName method' () {
|
||||||
|
when:
|
||||||
|
def displayMsg = employee.showName()
|
||||||
|
then:
|
||||||
|
displayMsg
|
||||||
|
displayMsg instanceof String
|
||||||
|
assert displayMsg == "Hello, Bob!"
|
||||||
|
}
|
||||||
|
|
||||||
|
def 'Should return greetMsg string when using Employee.greet method' () {
|
||||||
|
when:
|
||||||
|
def greetMsg = employee.greet()
|
||||||
|
then:
|
||||||
|
greetMsg
|
||||||
|
greetMsg instanceof String
|
||||||
|
assert greetMsg == "Hello, from a private method!"
|
||||||
|
}
|
||||||
|
|
||||||
|
def 'Should return MissingMethodException when using Employee.greetingMessage method' () {
|
||||||
|
when:
|
||||||
|
def exception
|
||||||
|
try {
|
||||||
|
employee.greetingMessage()
|
||||||
|
}catch(Exception e) {
|
||||||
|
exception = e
|
||||||
|
}
|
||||||
|
|
||||||
|
then:
|
||||||
|
exception
|
||||||
|
exception instanceof groovy.lang.MissingMethodException
|
||||||
|
assert exception.message == "No signature of method: com.baeldung.traits.Employee.greetingMessage()"+
|
||||||
|
" is applicable for argument types: () values: []"
|
||||||
|
}
|
||||||
|
|
||||||
|
def 'Should return employee instance when using Employee.whoAmI method' () {
|
||||||
|
when:
|
||||||
|
def emp = employee.self()
|
||||||
|
then:
|
||||||
|
emp
|
||||||
|
emp instanceof Employee
|
||||||
|
assert emp.is(employee)
|
||||||
|
}
|
||||||
|
|
||||||
|
def 'Should display lastName when using Employee.showLastName method' () {
|
||||||
|
when:
|
||||||
|
def lastNameMsg = employee.showLastName()
|
||||||
|
then:
|
||||||
|
lastNameMsg
|
||||||
|
lastNameMsg instanceof String
|
||||||
|
assert lastNameMsg == "Hello, Marley!"
|
||||||
|
}
|
||||||
|
|
||||||
|
def 'Should be able to define properties of UserTrait in Employee instance' () {
|
||||||
|
when:
|
||||||
|
employee = new Employee(email: "a@e.com", address: "baeldung.com")
|
||||||
|
then:
|
||||||
|
employee
|
||||||
|
employee instanceof Employee
|
||||||
|
assert employee.email == "a@e.com"
|
||||||
|
assert employee.address == "baeldung.com"
|
||||||
|
}
|
||||||
|
|
||||||
|
def 'Should execute basicAbility method from SpeakingTrait and return msg string' () {
|
||||||
|
when:
|
||||||
|
def speakMsg = dog.basicAbility()
|
||||||
|
then:
|
||||||
|
speakMsg
|
||||||
|
speakMsg instanceof String
|
||||||
|
assert speakMsg == "Speaking!!"
|
||||||
|
}
|
||||||
|
|
||||||
|
def 'Should verify multiple inheritance with traits and execute overridden traits method' () {
|
||||||
|
when:
|
||||||
|
def walkSpeakMsg = dog.speakAndWalk()
|
||||||
|
println walkSpeakMsg
|
||||||
|
then:
|
||||||
|
walkSpeakMsg
|
||||||
|
walkSpeakMsg instanceof String
|
||||||
|
assert walkSpeakMsg == "Walk and speak!!"
|
||||||
|
}
|
||||||
|
|
||||||
|
def 'Should implement AnimalTrait at runtime and access basicBehavior method' () {
|
||||||
|
when:
|
||||||
|
def dogInstance = new Dog() as AnimalTrait
|
||||||
|
def basicBehaviorMsg = dogInstance.basicBehavior()
|
||||||
|
then:
|
||||||
|
basicBehaviorMsg
|
||||||
|
basicBehaviorMsg instanceof String
|
||||||
|
assert basicBehaviorMsg == "Animalistic!!"
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,9 +3,9 @@
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>core-java-10</artifactId>
|
<artifactId>core-java-10</artifactId>
|
||||||
<packaging>jar</packaging>
|
|
||||||
<version>0.1.0-SNAPSHOT</version>
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
<name>core-java-10</name>
|
<name>core-java-10</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
<url>http://maven.apache.org</url>
|
<url>http://maven.apache.org</url>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
|
|
|
@ -3,3 +3,4 @@
|
||||||
- [Java 11 Single File Source Code](https://www.baeldung.com/java-single-file-source-code)
|
- [Java 11 Single File Source Code](https://www.baeldung.com/java-single-file-source-code)
|
||||||
- [Java 11 Local Variable Syntax for Lambda Parameters](https://www.baeldung.com/java-var-lambda-params)
|
- [Java 11 Local Variable Syntax for Lambda Parameters](https://www.baeldung.com/java-var-lambda-params)
|
||||||
- [Java 11 String API Additions](https://www.baeldung.com/java-11-string-api)
|
- [Java 11 String API Additions](https://www.baeldung.com/java-11-string-api)
|
||||||
|
- [Java 11 Nest Based Access Control](https://www.baeldung.com/java-nest-based-access-control)
|
||||||
|
|
|
@ -3,9 +3,9 @@
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>core-java-11</artifactId>
|
<artifactId>core-java-11</artifactId>
|
||||||
<packaging>jar</packaging>
|
|
||||||
<version>0.1.0-SNAPSHOT</version>
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
<name>core-java-11</name>
|
<name>core-java-11</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
<url>http://maven.apache.org</url>
|
<url>http://maven.apache.org</url>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.baeldung.epsilongc;
|
||||||
|
|
||||||
|
public class MemoryPolluter {
|
||||||
|
|
||||||
|
private static final int MEGABYTE_IN_BYTES = 1024 * 1024;
|
||||||
|
private static final int ITERATION_COUNT = 1024 * 10;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("Starting pollution");
|
||||||
|
|
||||||
|
for (int i = 0; i < ITERATION_COUNT; i++) {
|
||||||
|
byte[] array = new byte[MEGABYTE_IN_BYTES];
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Terminating");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,132 @@
|
||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package com.baeldung.java11.httpclient;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpClient.Version;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpRequest.BodyPublishers;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
import java.net.http.HttpResponse.BodyHandlers;
|
||||||
|
import java.net.http.HttpResponse.PushPromiseHandler;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class HttpClientExample {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
httpGetRequest();
|
||||||
|
httpPostRequest();
|
||||||
|
asynchronousGetRequest();
|
||||||
|
asynchronousMultipleRequests();
|
||||||
|
pushRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void httpGetRequest() throws URISyntaxException, IOException, InterruptedException {
|
||||||
|
HttpClient client = HttpClient.newHttpClient();
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.version(HttpClient.Version.HTTP_2)
|
||||||
|
.uri(URI.create("http://jsonplaceholder.typicode.com/posts/1"))
|
||||||
|
.headers("Accept-Enconding", "gzip, deflate")
|
||||||
|
.build();
|
||||||
|
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
|
||||||
|
|
||||||
|
String responseBody = response.body();
|
||||||
|
int responseStatusCode = response.statusCode();
|
||||||
|
|
||||||
|
System.out.println("httpGetRequest: " + responseBody);
|
||||||
|
System.out.println("httpGetRequest status code: " + responseStatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void httpPostRequest() throws URISyntaxException, IOException, InterruptedException {
|
||||||
|
HttpClient client = HttpClient.newBuilder()
|
||||||
|
.version(HttpClient.Version.HTTP_2)
|
||||||
|
.build();
|
||||||
|
HttpRequest request = HttpRequest.newBuilder(new URI("http://jsonplaceholder.typicode.com/posts"))
|
||||||
|
.version(HttpClient.Version.HTTP_2)
|
||||||
|
.POST(BodyPublishers.ofString("Sample Post Request"))
|
||||||
|
.build();
|
||||||
|
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
|
||||||
|
String responseBody = response.body();
|
||||||
|
System.out.println("httpPostRequest : " + responseBody);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void asynchronousGetRequest() throws URISyntaxException {
|
||||||
|
HttpClient client = HttpClient.newHttpClient();
|
||||||
|
URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1");
|
||||||
|
HttpRequest request = HttpRequest.newBuilder(httpURI)
|
||||||
|
.version(HttpClient.Version.HTTP_2)
|
||||||
|
.build();
|
||||||
|
CompletableFuture<Void> futureResponse = client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
|
||||||
|
.thenAccept(resp -> {
|
||||||
|
System.out.println("Got pushed response " + resp.uri());
|
||||||
|
System.out.println("Response statuscode: " + resp.statusCode());
|
||||||
|
System.out.println("Response body: " + resp.body());
|
||||||
|
});
|
||||||
|
System.out.println("futureResponse" + futureResponse);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void asynchronousMultipleRequests() throws URISyntaxException {
|
||||||
|
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<HttpRequest> requests = uris.stream()
|
||||||
|
.map(HttpRequest::newBuilder)
|
||||||
|
.map(reqBuilder -> reqBuilder.build())
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
System.out.println("Got pushed response1 " + requests);
|
||||||
|
CompletableFuture.allOf(requests.stream()
|
||||||
|
.map(request -> client.sendAsync(request, BodyHandlers.ofString()))
|
||||||
|
.toArray(CompletableFuture<?>[]::new))
|
||||||
|
.thenAccept(System.out::println)
|
||||||
|
.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void pushRequest() throws URISyntaxException, InterruptedException {
|
||||||
|
System.out.println("Running HTTP/2 Server Push example...");
|
||||||
|
|
||||||
|
HttpClient httpClient = HttpClient.newBuilder()
|
||||||
|
.version(Version.HTTP_2)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpRequest pageRequest = HttpRequest.newBuilder()
|
||||||
|
.uri(URI.create("https://http2.golang.org/serverpush"))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// Interface HttpResponse.PushPromiseHandler<T>
|
||||||
|
// void applyPushPromise(HttpRequest initiatingRequest, HttpRequest pushPromiseRequest, Function<HttpResponse.BodyHandler<T>,CompletableFuture<HttpResponse<T>>> acceptor)
|
||||||
|
httpClient.sendAsync(pageRequest, BodyHandlers.ofString(), pushPromiseHandler())
|
||||||
|
.thenAccept(pageResponse -> {
|
||||||
|
System.out.println("Page response status code: " + pageResponse.statusCode());
|
||||||
|
System.out.println("Page response headers: " + pageResponse.headers());
|
||||||
|
String responseBody = pageResponse.body();
|
||||||
|
System.out.println(responseBody);
|
||||||
|
}).join();
|
||||||
|
|
||||||
|
Thread.sleep(1000); // waiting for full response
|
||||||
|
}
|
||||||
|
|
||||||
|
private static PushPromiseHandler<String> pushPromiseHandler() {
|
||||||
|
return (HttpRequest initiatingRequest,
|
||||||
|
HttpRequest pushPromiseRequest,
|
||||||
|
Function<HttpResponse.BodyHandler<String>,
|
||||||
|
CompletableFuture<HttpResponse<String>>> acceptor) -> {
|
||||||
|
acceptor.apply(BodyHandlers.ofString())
|
||||||
|
.thenAccept(resp -> {
|
||||||
|
System.out.println(" Pushed response: " + resp.uri() + ", headers: " + resp.headers());
|
||||||
|
});
|
||||||
|
System.out.println("Promise request: " + pushPromiseRequest.uri());
|
||||||
|
System.out.println("Promise request: " + pushPromiseRequest.headers());
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,240 @@
|
||||||
|
package com.baeldung.java11.httpclient.test;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.containsString;
|
||||||
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.Authenticator;
|
||||||
|
import java.net.CookieManager;
|
||||||
|
import java.net.CookiePolicy;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.PasswordAuthentication;
|
||||||
|
import java.net.ProxySelector;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
import java.net.http.HttpResponse.BodyHandlers;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.CompletionException;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class HttpClientTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnSampleDataContentWhenConnectViaSystemProxy() throws IOException, InterruptedException, URISyntaxException {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI("https://postman-echo.com/post"))
|
||||||
|
.headers("Content-Type", "text/plain;charset=UTF-8")
|
||||||
|
.POST(HttpRequest.BodyPublishers.ofString("Sample body"))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
|
||||||
|
HttpResponse<String> response = HttpClient.newBuilder()
|
||||||
|
.proxy(ProxySelector.getDefault())
|
||||||
|
.build()
|
||||||
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
|
assertThat(response.body(), containsString("Sample body"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldNotFollowRedirectWhenSetToDefaultNever() throws IOException, InterruptedException, URISyntaxException {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI("http://stackoverflow.com"))
|
||||||
|
.version(HttpClient.Version.HTTP_1_1)
|
||||||
|
.GET()
|
||||||
|
.build();
|
||||||
|
HttpResponse<String> response = HttpClient.newBuilder()
|
||||||
|
.build()
|
||||||
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_MOVED_PERM));
|
||||||
|
assertThat(response.body(), containsString("https://stackoverflow.com/"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldFollowRedirectWhenSetToAlways() throws IOException, InterruptedException, URISyntaxException {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI("http://stackoverflow.com"))
|
||||||
|
.version(HttpClient.Version.HTTP_1_1)
|
||||||
|
.GET()
|
||||||
|
.build();
|
||||||
|
HttpResponse<String> response = HttpClient.newBuilder()
|
||||||
|
.followRedirects(HttpClient.Redirect.ALWAYS)
|
||||||
|
.build()
|
||||||
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
|
assertThat(response.request()
|
||||||
|
.uri()
|
||||||
|
.toString(), equalTo("https://stackoverflow.com/"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnOKStatusForAuthenticatedAccess() throws URISyntaxException, IOException, InterruptedException {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI("https://postman-echo.com/basic-auth"))
|
||||||
|
.GET()
|
||||||
|
.build();
|
||||||
|
HttpResponse<String> response = HttpClient.newBuilder()
|
||||||
|
.authenticator(new Authenticator() {
|
||||||
|
@Override
|
||||||
|
protected PasswordAuthentication getPasswordAuthentication() {
|
||||||
|
return new PasswordAuthentication("postman", "password".toCharArray());
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.build()
|
||||||
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldSendRequestAsync() throws URISyntaxException, InterruptedException, ExecutionException {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI("https://postman-echo.com/post"))
|
||||||
|
.headers("Content-Type", "text/plain;charset=UTF-8")
|
||||||
|
.POST(HttpRequest.BodyPublishers.ofString("Sample body"))
|
||||||
|
.build();
|
||||||
|
CompletableFuture<HttpResponse<String>> response = HttpClient.newBuilder()
|
||||||
|
.build()
|
||||||
|
.sendAsync(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
assertThat(response.get()
|
||||||
|
.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldUseJustTwoThreadWhenProcessingSendAsyncRequest() throws URISyntaxException, InterruptedException, ExecutionException {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI("https://postman-echo.com/get"))
|
||||||
|
.GET()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
ExecutorService executorService = Executors.newFixedThreadPool(2);
|
||||||
|
|
||||||
|
CompletableFuture<HttpResponse<String>> response1 = HttpClient.newBuilder()
|
||||||
|
.executor(executorService)
|
||||||
|
.build()
|
||||||
|
.sendAsync(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
CompletableFuture<HttpResponse<String>> response2 = HttpClient.newBuilder()
|
||||||
|
.executor(executorService)
|
||||||
|
.build()
|
||||||
|
.sendAsync(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
CompletableFuture<HttpResponse<String>> response3 = HttpClient.newBuilder()
|
||||||
|
.executor(executorService)
|
||||||
|
.build()
|
||||||
|
.sendAsync(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
CompletableFuture.allOf(response1, response2, response3)
|
||||||
|
.join();
|
||||||
|
|
||||||
|
assertThat(response1.get()
|
||||||
|
.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
|
assertThat(response2.get()
|
||||||
|
.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
|
assertThat(response3.get()
|
||||||
|
.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldNotStoreCookieWhenPolicyAcceptNone() throws URISyntaxException, IOException, InterruptedException {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI("https://postman-echo.com/get"))
|
||||||
|
.GET()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpClient httpClient = HttpClient.newBuilder()
|
||||||
|
.cookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_NONE))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
assertTrue(httpClient.cookieHandler()
|
||||||
|
.isPresent());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldStoreCookieWhenPolicyAcceptAll() throws URISyntaxException, IOException, InterruptedException {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI("https://postman-echo.com/get"))
|
||||||
|
.GET()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpClient httpClient = HttpClient.newBuilder()
|
||||||
|
.cookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_ALL))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
assertTrue(httpClient.cookieHandler()
|
||||||
|
.isPresent());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
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"));
|
||||||
|
|
||||||
|
HttpClient client = HttpClient.newHttpClient();
|
||||||
|
|
||||||
|
List<CompletableFuture<String>> futures = targets.stream()
|
||||||
|
.map(target -> client.sendAsync(HttpRequest.newBuilder(target)
|
||||||
|
.GET()
|
||||||
|
.build(), HttpResponse.BodyHandlers.ofString())
|
||||||
|
.thenApply(response -> response.body()))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
|
||||||
|
.join();
|
||||||
|
|
||||||
|
if (futures.get(0)
|
||||||
|
.get()
|
||||||
|
.contains("foo1")) {
|
||||||
|
assertThat(futures.get(0)
|
||||||
|
.get(), containsString("bar1"));
|
||||||
|
assertThat(futures.get(1)
|
||||||
|
.get(), containsString("bar2"));
|
||||||
|
} else {
|
||||||
|
assertThat(futures.get(1)
|
||||||
|
.get(), containsString("bar2"));
|
||||||
|
assertThat(futures.get(1)
|
||||||
|
.get(), containsString("bar1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void completeExceptionallyExample() {
|
||||||
|
CompletableFuture<String> cf = CompletableFuture.completedFuture("message").thenApplyAsync(String::toUpperCase,
|
||||||
|
CompletableFuture.delayedExecutor(1, TimeUnit.SECONDS));
|
||||||
|
CompletableFuture<String> exceptionHandler = cf.handle((s, th) -> { return (th != null) ? "message upon cancel" : ""; });
|
||||||
|
cf.completeExceptionally(new RuntimeException("completed exceptionally"));
|
||||||
|
assertTrue("Was not completed exceptionally", cf.isCompletedExceptionally());
|
||||||
|
try {
|
||||||
|
cf.join();
|
||||||
|
fail("Should have thrown an exception");
|
||||||
|
} catch (CompletionException ex) { // just for testing
|
||||||
|
assertEquals("completed exceptionally", ex.getCause().getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals("message upon cancel", exceptionHandler.join());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,168 @@
|
||||||
|
package com.baeldung.java11.httpclient.test;
|
||||||
|
|
||||||
|
import static java.time.temporal.ChronoUnit.SECONDS;
|
||||||
|
import static org.hamcrest.CoreMatchers.containsString;
|
||||||
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.time.Duration;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class HttpRequestTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnStatusOKWhenSendGetRequest() throws IOException, InterruptedException, URISyntaxException {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI("https://postman-echo.com/get"))
|
||||||
|
.GET()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||||
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldUseHttp2WhenWebsiteUsesHttp2() throws IOException, InterruptedException, URISyntaxException {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI("https://stackoverflow.com"))
|
||||||
|
.version(HttpClient.Version.HTTP_2)
|
||||||
|
.GET()
|
||||||
|
.build();
|
||||||
|
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||||
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
|
assertThat(response.version(), equalTo(HttpClient.Version.HTTP_2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldFallbackToHttp1_1WhenWebsiteDoesNotUseHttp2() throws IOException, InterruptedException, URISyntaxException, NoSuchAlgorithmException {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI("https://postman-echo.com/get"))
|
||||||
|
.version(HttpClient.Version.HTTP_2)
|
||||||
|
.GET()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||||
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
assertThat(response.version(), equalTo(HttpClient.Version.HTTP_1_1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnStatusOKWhenSendGetRequestWithDummyHeaders() throws IOException, InterruptedException, URISyntaxException {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI("https://postman-echo.com/get"))
|
||||||
|
.headers("key1", "value1", "key2", "value2")
|
||||||
|
.GET()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||||
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnStatusOKWhenSendGetRequestTimeoutSet() throws IOException, InterruptedException, URISyntaxException {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI("https://postman-echo.com/get"))
|
||||||
|
.timeout(Duration.of(10, SECONDS))
|
||||||
|
.GET()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||||
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnNoContentWhenPostWithNoBody() throws IOException, InterruptedException, URISyntaxException {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI("https://postman-echo.com/post"))
|
||||||
|
.POST(HttpRequest.BodyPublishers.noBody())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||||
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnSampleDataContentWhenPostWithBodyText() throws IOException, InterruptedException, URISyntaxException {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI("https://postman-echo.com/post"))
|
||||||
|
.headers("Content-Type", "text/plain;charset=UTF-8")
|
||||||
|
.POST(HttpRequest.BodyPublishers.ofString("Sample request body"))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||||
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
|
assertThat(response.body(), containsString("Sample request body"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnSampleDataContentWhenPostWithInputStream() throws IOException, InterruptedException, URISyntaxException {
|
||||||
|
byte[] sampleData = "Sample request body".getBytes();
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI("https://postman-echo.com/post"))
|
||||||
|
.headers("Content-Type", "text/plain;charset=UTF-8")
|
||||||
|
.POST(HttpRequest.BodyPublishers.ofInputStream(() -> new ByteArrayInputStream(sampleData)))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||||
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
|
assertThat(response.body(), containsString("Sample request body"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnSampleDataContentWhenPostWithByteArrayProcessorStream() throws IOException, InterruptedException, URISyntaxException {
|
||||||
|
byte[] sampleData = "Sample request body".getBytes();
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI("https://postman-echo.com/post"))
|
||||||
|
.headers("Content-Type", "text/plain;charset=UTF-8")
|
||||||
|
.POST(HttpRequest.BodyPublishers.ofByteArray(sampleData))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||||
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
|
assertThat(response.body(), containsString("Sample request body"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnSampleDataContentWhenPostWithFileProcessorStream() throws IOException, InterruptedException, URISyntaxException {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI("https://postman-echo.com/post"))
|
||||||
|
.headers("Content-Type", "text/plain;charset=UTF-8")
|
||||||
|
.POST(HttpRequest.BodyPublishers.ofFile(Paths.get("src/test/resources/sample.txt")))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||||
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
|
assertThat(response.body(), containsString("Sample file content"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,54 @@
|
||||||
|
package com.baeldung.java11.httpclient.test;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class HttpResponseTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnStatusOKWhenSendGetRequest() throws IOException, InterruptedException, URISyntaxException {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI("https://postman-echo.com/get"))
|
||||||
|
.version(HttpClient.Version.HTTP_2)
|
||||||
|
.GET()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = HttpClient.newBuilder()
|
||||||
|
.followRedirects(HttpClient.Redirect.NORMAL)
|
||||||
|
.build()
|
||||||
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||||
|
assertNotNull(response.body());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldResponseURIDifferentThanRequestUIRWhenRedirect() throws IOException, InterruptedException, URISyntaxException {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI("http://stackoverflow.com"))
|
||||||
|
.version(HttpClient.Version.HTTP_2)
|
||||||
|
.GET()
|
||||||
|
.build();
|
||||||
|
HttpResponse<String> response = HttpClient.newBuilder()
|
||||||
|
.followRedirects(HttpClient.Redirect.NORMAL)
|
||||||
|
.build()
|
||||||
|
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
assertThat(request.uri()
|
||||||
|
.toString(), equalTo("http://stackoverflow.com"));
|
||||||
|
assertThat(response.uri()
|
||||||
|
.toString(), equalTo("https://stackoverflow.com/"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -4,9 +4,9 @@
|
||||||
|
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
- [Java 8 Collectors](http://www.baeldung.com/java-8-collectors)
|
- [Java 8 Collectors](http://www.baeldung.com/java-8-collectors)
|
||||||
- [Guide to Java 8’s Functional Interfaces](http://www.baeldung.com/java-8-functional-interfaces)
|
- [Functional Interfaces in Java 8](http://www.baeldung.com/java-8-functional-interfaces)
|
||||||
- [Java 8 – Powerful Comparison with Lambdas](http://www.baeldung.com/java-8-sort-lambda)
|
- [Java 8 – Powerful Comparison with Lambdas](http://www.baeldung.com/java-8-sort-lambda)
|
||||||
- [Java 8 New Features](http://www.baeldung.com/java-8-new-features)
|
- [New Features in Java 8](http://www.baeldung.com/java-8-new-features)
|
||||||
- [Lambda Expressions and Functional Interfaces: Tips and Best Practices](http://www.baeldung.com/java-8-lambda-expressions-tips)
|
- [Lambda Expressions and Functional Interfaces: Tips and Best Practices](http://www.baeldung.com/java-8-lambda-expressions-tips)
|
||||||
- [The Double Colon Operator in Java 8](http://www.baeldung.com/java-8-double-colon-operator)
|
- [The Double Colon Operator in Java 8](http://www.baeldung.com/java-8-double-colon-operator)
|
||||||
- [Guide to Java 8 groupingBy Collector](http://www.baeldung.com/java-groupingby-collector)
|
- [Guide to Java 8 groupingBy Collector](http://www.baeldung.com/java-groupingby-collector)
|
||||||
|
@ -38,3 +38,5 @@
|
||||||
- [Java @SafeVarargs Annotation](https://www.baeldung.com/java-safevarargs)
|
- [Java @SafeVarargs Annotation](https://www.baeldung.com/java-safevarargs)
|
||||||
- [Java @Deprecated Annotation](https://www.baeldung.com/java-deprecated)
|
- [Java @Deprecated Annotation](https://www.baeldung.com/java-deprecated)
|
||||||
- [Java 8 Predicate Chain](https://www.baeldung.com/java-predicate-chain)
|
- [Java 8 Predicate Chain](https://www.baeldung.com/java-predicate-chain)
|
||||||
|
- [Method References in Java](https://www.baeldung.com/java-method-references)
|
||||||
|
- [Creating a Custom Annotation in Java](https://www.baeldung.com/java-custom-annotation)
|
||||||
|
|
|
@ -4,8 +4,8 @@
|
||||||
<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>
|
||||||
<packaging>jar</packaging>
|
|
||||||
<name>core-java-8</name>
|
<name>core-java-8</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
|
@ -180,7 +180,6 @@
|
||||||
<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>
|
||||||
<lombok.version>1.16.12</lombok.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>
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.baeldung.customannotations;
|
||||||
|
|
||||||
|
import static java.lang.annotation.ElementType.METHOD;
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
@Target(METHOD)
|
||||||
|
public @interface Init {
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.baeldung.customannotations;
|
||||||
|
|
||||||
|
import static java.lang.annotation.ElementType.FIELD;
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
@Target({ FIELD })
|
||||||
|
public @interface JsonElement {
|
||||||
|
public String key() default "";
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.baeldung.customannotations;
|
||||||
|
|
||||||
|
import static java.lang.annotation.ElementType.TYPE;
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
@Target(TYPE)
|
||||||
|
public @interface JsonSerializable {
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.baeldung.customannotations;
|
||||||
|
|
||||||
|
public class JsonSerializationException extends RuntimeException {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
public JsonSerializationException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,67 @@
|
||||||
|
package com.baeldung.customannotations;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class ObjectToJsonConverter {
|
||||||
|
public String convertToJson(Object object) throws JsonSerializationException {
|
||||||
|
try {
|
||||||
|
|
||||||
|
checkIfSerializable(object);
|
||||||
|
initializeObject(object);
|
||||||
|
return getJsonString(object);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new JsonSerializationException(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkIfSerializable(Object object) {
|
||||||
|
if (Objects.isNull(object)) {
|
||||||
|
throw new JsonSerializationException("Can't serialize a null object");
|
||||||
|
}
|
||||||
|
|
||||||
|
Class<?> clazz = object.getClass();
|
||||||
|
if (!clazz.isAnnotationPresent(JsonSerializable.class)) {
|
||||||
|
throw new JsonSerializationException("The class " + clazz.getSimpleName() + " is not annotated with JsonSerializable");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initializeObject(Object object) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
||||||
|
Class<?> clazz = object.getClass();
|
||||||
|
for (Method method : clazz.getDeclaredMethods()) {
|
||||||
|
if (method.isAnnotationPresent(Init.class)) {
|
||||||
|
method.setAccessible(true);
|
||||||
|
method.invoke(object);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getJsonString(Object object) throws IllegalArgumentException, IllegalAccessException {
|
||||||
|
Class<?> clazz = object.getClass();
|
||||||
|
Map<String, String> jsonElementsMap = new HashMap<>();
|
||||||
|
for (Field field : clazz.getDeclaredFields()) {
|
||||||
|
field.setAccessible(true);
|
||||||
|
if (field.isAnnotationPresent(JsonElement.class)) {
|
||||||
|
jsonElementsMap.put(getKey(field), (String) field.get(object));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String jsonString = jsonElementsMap.entrySet()
|
||||||
|
.stream()
|
||||||
|
.map(entry -> "\"" + entry.getKey() + "\":\"" + entry.getValue() + "\"")
|
||||||
|
.collect(Collectors.joining(","));
|
||||||
|
return "{" + jsonString + "}";
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getKey(Field field) {
|
||||||
|
String value = field.getAnnotation(JsonElement.class)
|
||||||
|
.key();
|
||||||
|
return value.isEmpty() ? field.getName() : value;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,66 @@
|
||||||
|
package com.baeldung.customannotations;
|
||||||
|
|
||||||
|
@JsonSerializable
|
||||||
|
public class Person {
|
||||||
|
@JsonElement
|
||||||
|
private String firstName;
|
||||||
|
@JsonElement
|
||||||
|
private String lastName;
|
||||||
|
@JsonElement(key = "personAge")
|
||||||
|
private String age;
|
||||||
|
|
||||||
|
private String address;
|
||||||
|
|
||||||
|
public Person(String firstName, String lastName) {
|
||||||
|
super();
|
||||||
|
this.firstName = firstName;
|
||||||
|
this.lastName = lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Person(String firstName, String lastName, String age) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
this.lastName = lastName;
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Init
|
||||||
|
private void initNames() {
|
||||||
|
this.firstName = this.firstName.substring(0, 1)
|
||||||
|
.toUpperCase() + this.firstName.substring(1);
|
||||||
|
this.lastName = this.lastName.substring(0, 1)
|
||||||
|
.toUpperCase() + this.lastName.substring(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFirstName() {
|
||||||
|
return firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFirstName(String firstName) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLastName() {
|
||||||
|
return lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastName(String lastName) {
|
||||||
|
this.lastName = lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(String age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAddress() {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAddress(String address) {
|
||||||
|
this.address = address;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,62 @@
|
||||||
|
package com.baeldung.streamreduce.application;
|
||||||
|
|
||||||
|
import com.baeldung.streamreduce.entities.User;
|
||||||
|
import com.baeldung.streamreduce.utilities.NumberUtils;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Application {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
|
||||||
|
int result1 = numbers.stream().reduce(0, (a, b) -> a + b);
|
||||||
|
System.out.println(result1);
|
||||||
|
|
||||||
|
int result2 = numbers.stream().reduce(0, Integer::sum);
|
||||||
|
System.out.println(result2);
|
||||||
|
|
||||||
|
List<String> letters = Arrays.asList("a", "b", "c", "d", "e");
|
||||||
|
String result3 = letters.stream().reduce("", (a, b) -> a + b);
|
||||||
|
System.out.println(result3);
|
||||||
|
|
||||||
|
String result4 = letters.stream().reduce("", String::concat);
|
||||||
|
System.out.println(result4);
|
||||||
|
|
||||||
|
String result5 = letters.stream().reduce("", (a, b) -> a.toUpperCase() + b.toUpperCase());
|
||||||
|
System.out.println(result5);
|
||||||
|
|
||||||
|
List<User> users = Arrays.asList(new User("John", 30), new User("Julie", 35));
|
||||||
|
int result6 = users.stream().reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum);
|
||||||
|
System.out.println(result6);
|
||||||
|
|
||||||
|
String result7 = letters.parallelStream().reduce("", String::concat);
|
||||||
|
System.out.println(result7);
|
||||||
|
|
||||||
|
int result8 = users.parallelStream().reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum);
|
||||||
|
System.out.println(result8);
|
||||||
|
|
||||||
|
List<User> userList = new ArrayList<>();
|
||||||
|
for (int i = 0; i <= 1000000; i++) {
|
||||||
|
userList.add(new User("John" + i, i));
|
||||||
|
}
|
||||||
|
|
||||||
|
long t1 = System.currentTimeMillis();
|
||||||
|
int result9 = userList.stream().reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum);
|
||||||
|
long t2 = System.currentTimeMillis();
|
||||||
|
System.out.println(result9);
|
||||||
|
System.out.println("Sequential stream time: " + (t2 - t1) + "ms");
|
||||||
|
|
||||||
|
long t3 = System.currentTimeMillis();
|
||||||
|
int result10 = userList.parallelStream().reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum);
|
||||||
|
long t4 = System.currentTimeMillis();
|
||||||
|
System.out.println(result10);
|
||||||
|
System.out.println("Parallel stream time: " + (t4 - t3) + "ms");
|
||||||
|
|
||||||
|
int result11 = NumberUtils.divideListElements(numbers, 1);
|
||||||
|
System.out.println(result11);
|
||||||
|
|
||||||
|
int result12 = NumberUtils.divideListElementsWithExtractedTryCatchBlock(numbers, 0);
|
||||||
|
System.out.println(result12);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
package com.baeldung.streamreduce.entities;
|
||||||
|
|
||||||
|
public class User {
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
private final int age;
|
||||||
|
|
||||||
|
public User(String name, int age) {
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "User{" + "name=" + name + ", age=" + age + '}';
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,52 @@
|
||||||
|
package com.baeldung.streamreduce.utilities;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public abstract class NumberUtils {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = Logger.getLogger(NumberUtils.class.getName());
|
||||||
|
|
||||||
|
public static int divideListElements(List<Integer> values, Integer divider) {
|
||||||
|
return values.stream()
|
||||||
|
.reduce(0, (a, b) -> {
|
||||||
|
try {
|
||||||
|
return a / divider + b / divider;
|
||||||
|
} catch (ArithmeticException e) {
|
||||||
|
LOGGER.log(Level.INFO, "Arithmetic Exception: Division by Zero");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int divideListElementsWithExtractedTryCatchBlock(List<Integer> values, int divider) {
|
||||||
|
return values.stream().reduce(0, (a, b) -> divide(a, divider) + divide(b, divider));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int divideListElementsWithApplyFunctionMethod(List<Integer> values, int divider) {
|
||||||
|
BiFunction<Integer, Integer, Integer> division = (a, b) -> a / b;
|
||||||
|
return values.stream().reduce(0, (a, b) -> applyFunction(division, a, divider) + applyFunction(division, b, divider));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int divide(int value, int factor) {
|
||||||
|
int result = 0;
|
||||||
|
try {
|
||||||
|
result = value / factor;
|
||||||
|
} catch (ArithmeticException e) {
|
||||||
|
LOGGER.log(Level.INFO, "Arithmetic Exception: Division by Zero");
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int applyFunction(BiFunction<Integer, Integer, Integer> function, int a, int b) {
|
||||||
|
try {
|
||||||
|
return function.apply(a, b);
|
||||||
|
}
|
||||||
|
catch(Exception e) {
|
||||||
|
LOGGER.log(Level.INFO, "Exception occurred!");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
|
@ -39,6 +39,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
public class Java8CollectorsUnitTest {
|
public class Java8CollectorsUnitTest {
|
||||||
|
|
||||||
private final List<String> givenList = Arrays.asList("a", "bb", "ccc", "dd");
|
private final List<String> givenList = Arrays.asList("a", "bb", "ccc", "dd");
|
||||||
|
private final List<String> listWithDuplicates = Arrays.asList("a", "bb", "c", "d", "bb");
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCollectingToList_shouldCollectToList() throws Exception {
|
public void whenCollectingToList_shouldCollectToList() throws Exception {
|
||||||
|
@ -48,12 +49,19 @@ public class Java8CollectorsUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCollectingToList_shouldCollectToSet() throws Exception {
|
public void whenCollectingToSet_shouldCollectToSet() throws Exception {
|
||||||
final Set<String> result = givenList.stream().collect(toSet());
|
final Set<String> result = givenList.stream().collect(toSet());
|
||||||
|
|
||||||
assertThat(result).containsAll(givenList);
|
assertThat(result).containsAll(givenList);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenContainsDuplicateElements_whenCollectingToSet_shouldAddDuplicateElementsOnlyOnce() throws Exception {
|
||||||
|
final Set<String> result = listWithDuplicates.stream().collect(toSet());
|
||||||
|
|
||||||
|
assertThat(result).hasSize(4);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCollectingToCollection_shouldCollectToCollection() throws Exception {
|
public void whenCollectingToCollection_shouldCollectToCollection() throws Exception {
|
||||||
final List<String> result = givenList.stream().collect(toCollection(LinkedList::new));
|
final List<String> result = givenList.stream().collect(toCollection(LinkedList::new));
|
||||||
|
@ -77,10 +85,23 @@ public class Java8CollectorsUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCollectingToMap_shouldCollectToMapMerging() throws Exception {
|
public void whenCollectingToMapwWithDuplicates_shouldCollectToMapMergingTheIdenticalItems() throws Exception {
|
||||||
final Map<String, Integer> result = givenList.stream().collect(toMap(Function.identity(), String::length, (i1, i2) -> i1));
|
final Map<String, Integer> result = listWithDuplicates.stream().collect(
|
||||||
|
toMap(
|
||||||
|
Function.identity(),
|
||||||
|
String::length,
|
||||||
|
(item, identicalItem) -> item
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
assertThat(result).containsEntry("a", 1).containsEntry("bb", 2).containsEntry("ccc", 3).containsEntry("dd", 2);
|
assertThat(result).containsEntry("a", 1).containsEntry("bb", 2).containsEntry("c", 1).containsEntry("d", 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenContainsDuplicateElements_whenCollectingToMap_shouldThrowException() throws Exception {
|
||||||
|
assertThatThrownBy(() -> {
|
||||||
|
listWithDuplicates.stream().collect(toMap(Function.identity(), String::length));
|
||||||
|
}).isInstanceOf(IllegalStateException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue