merge upstream
This commit is contained in:
commit
f9e6007e6b
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.algorithms.graphcycledetection.domain;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Graph {
|
||||
|
||||
private List<Vertex> vertices;
|
||||
|
||||
public Graph() {
|
||||
this.vertices = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Graph(List<Vertex> vertices) {
|
||||
this.vertices = vertices;
|
||||
}
|
||||
|
||||
public void addVertex(Vertex vertex) {
|
||||
this.vertices.add(vertex);
|
||||
}
|
||||
|
||||
public void addEdge(Vertex from, Vertex to) {
|
||||
from.addNeighbour(to);
|
||||
}
|
||||
|
||||
public boolean hasCycle() {
|
||||
for (Vertex vertex : vertices) {
|
||||
if (!vertex.isVisited() && hasCycle(vertex)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean hasCycle(Vertex sourceVertex) {
|
||||
sourceVertex.setBeingVisited(true);
|
||||
|
||||
for (Vertex neighbour : sourceVertex.getAdjacencyList()) {
|
||||
if (neighbour.isBeingVisited()) {
|
||||
// backward edge exists
|
||||
return true;
|
||||
} else if (!neighbour.isVisited() && hasCycle(neighbour)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
sourceVertex.setBeingVisited(false);
|
||||
sourceVertex.setVisited(true);
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package com.baeldung.algorithms.graphcycledetection.domain;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Vertex {
|
||||
|
||||
private String label;
|
||||
|
||||
private boolean visited;
|
||||
|
||||
private boolean beingVisited;
|
||||
|
||||
private List<Vertex> adjacencyList;
|
||||
|
||||
public Vertex(String label) {
|
||||
this.label = label;
|
||||
this.adjacencyList = new ArrayList<>();
|
||||
}
|
||||
|
||||
public String getLabel() {
|
||||
return label;
|
||||
}
|
||||
|
||||
public void setLabel(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
public boolean isVisited() {
|
||||
return visited;
|
||||
}
|
||||
|
||||
public void setVisited(boolean visited) {
|
||||
this.visited = visited;
|
||||
}
|
||||
|
||||
public boolean isBeingVisited() {
|
||||
return beingVisited;
|
||||
}
|
||||
|
||||
public void setBeingVisited(boolean beingVisited) {
|
||||
this.beingVisited = beingVisited;
|
||||
}
|
||||
|
||||
public List<Vertex> getAdjacencyList() {
|
||||
return adjacencyList;
|
||||
}
|
||||
|
||||
public void setAdjacencyList(List<Vertex> adjacencyList) {
|
||||
this.adjacencyList = adjacencyList;
|
||||
}
|
||||
|
||||
public void addNeighbour(Vertex adjacent) {
|
||||
this.adjacencyList.add(adjacent);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package com.baeldung.algorithms.graphcycledetection;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.algorithms.graphcycledetection.domain.Graph;
|
||||
import com.baeldung.algorithms.graphcycledetection.domain.Vertex;
|
||||
|
||||
public class GraphCycleDetectionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenGraph_whenCycleExists_thenReturnTrue() {
|
||||
|
||||
Vertex vertexA = new Vertex("A");
|
||||
Vertex vertexB = new Vertex("B");
|
||||
Vertex vertexC = new Vertex("C");
|
||||
Vertex vertexD = new Vertex("D");
|
||||
|
||||
Graph graph = new Graph();
|
||||
graph.addVertex(vertexA);
|
||||
graph.addVertex(vertexB);
|
||||
graph.addVertex(vertexC);
|
||||
graph.addVertex(vertexD);
|
||||
|
||||
graph.addEdge(vertexA, vertexB);
|
||||
graph.addEdge(vertexB, vertexC);
|
||||
graph.addEdge(vertexC, vertexA);
|
||||
graph.addEdge(vertexD, vertexC);
|
||||
|
||||
assertTrue(graph.hasCycle());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGraph_whenNoCycleExists_thenReturnFalse() {
|
||||
|
||||
Vertex vertexA = new Vertex("A");
|
||||
Vertex vertexB = new Vertex("B");
|
||||
Vertex vertexC = new Vertex("C");
|
||||
Vertex vertexD = new Vertex("D");
|
||||
|
||||
Graph graph = new Graph();
|
||||
graph.addVertex(vertexA);
|
||||
graph.addVertex(vertexB);
|
||||
graph.addVertex(vertexC);
|
||||
graph.addVertex(vertexD);
|
||||
|
||||
graph.addEdge(vertexA, vertexB);
|
||||
graph.addEdge(vertexB, vertexC);
|
||||
graph.addEdge(vertexA, vertexC);
|
||||
graph.addEdge(vertexD, vertexC);
|
||||
|
||||
assertFalse(graph.hasCycle());
|
||||
}
|
||||
}
|
|
@ -3,5 +3,6 @@
|
|||
## Relevant articles:
|
||||
|
||||
- [String Matching in Groovy](http://www.baeldung.com/)
|
||||
- [Template Engines in Groovy](https://www.baeldung.com/groovy-template-engines)
|
||||
- [Groovy def Keyword](https://www.baeldung.com/groovy-def-keyword)
|
||||
- [Pattern Matching in Strings in Groovy](https://www.baeldung.com/groovy-pattern-matching)
|
||||
- [Pattern Matching in Strings in Groovy](https://www.baeldung.com/groovy-pattern-matching)
|
|
@ -0,0 +1,178 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-groovy-2</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>core-groovy-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>${logback.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-all</artifactId>
|
||||
<version>${groovy.version}</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hsqldb</groupId>
|
||||
<artifactId>hsqldb</artifactId>
|
||||
<version>${hsqldb.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.spockframework</groupId>
|
||||
<artifactId>spock-core</artifactId>
|
||||
<version>${spock-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<scriptSourceDirectory>src/main/groovy</scriptSourceDirectory>
|
||||
<sourceDirectory>src/main/java</sourceDirectory>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.gmavenplus</groupId>
|
||||
<artifactId>gmavenplus-plugin</artifactId>
|
||||
<version>1.7.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>execute</goal>
|
||||
<goal>addSources</goal>
|
||||
<goal>addTestSources</goal>
|
||||
<goal>generateStubs</goal>
|
||||
<goal>compile</goal>
|
||||
<goal>generateTestStubs</goal>
|
||||
<goal>compileTests</goal>
|
||||
<goal>removeStubs</goal>
|
||||
<goal>removeTestStubs</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-all</artifactId>
|
||||
<!-- any version of Groovy \>= 1.5.0 should work here -->
|
||||
<version>${groovy.version}</version>
|
||||
<scope>runtime</scope>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-failsafe-plugin</artifactId>
|
||||
<version>${maven-failsafe-plugin.version}</version>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-surefire-provider</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>junit5</id>
|
||||
<goals>
|
||||
<goal>integration-test</goal>
|
||||
<goal>verify</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<includes>
|
||||
<include>**/*Test5.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.20.1</version>
|
||||
<configuration>
|
||||
<useFile>false</useFile>
|
||||
<includes>
|
||||
<include>**/*Test.java</include>
|
||||
<include>**/*Spec.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<!-- Maven Assembly Plugin: needed to run the jar through command line -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<configuration>
|
||||
<!-- get all project dependencies -->
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
<!-- MainClass in mainfest make a executable jar -->
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>com.baeldung.MyJointCompilationApp</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>make-assembly</id>
|
||||
<!-- bind to the packaging phase -->
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>central</id>
|
||||
<url>http://jcenter.bintray.com</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<junit.platform.version>1.0.0</junit.platform.version>
|
||||
<hsqldb.version>2.4.0</hsqldb.version>
|
||||
<spock-core.version>1.1-groovy-2.4</spock-core.version>
|
||||
<commons-lang3.version>3.9</commons-lang3.version>
|
||||
<java.version>1.8</java.version>
|
||||
<logback.version>1.2.3</logback.version>
|
||||
<groovy.version>2.5.7</groovy.version>
|
||||
<gmavenplus-plugin.version>1.6</gmavenplus-plugin.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-groovy-2</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
@ -15,25 +15,20 @@
|
|||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy</artifactId>
|
||||
<version>${groovy.version}</version>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>${logback.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-all</artifactId>
|
||||
<version>${groovy-all.version}</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-dateutil</artifactId>
|
||||
<version>${groovy.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-sql</artifactId>
|
||||
<version>${groovy-sql.version}</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
|
@ -56,21 +51,35 @@
|
|||
</dependencies>
|
||||
|
||||
<build>
|
||||
<scriptSourceDirectory>src/main/groovy</scriptSourceDirectory>
|
||||
<sourceDirectory>src/main/java</sourceDirectory>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.gmavenplus</groupId>
|
||||
<artifactId>gmavenplus-plugin</artifactId>
|
||||
<version>${gmavenplus-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>addSources</goal>
|
||||
<goal>addTestSources</goal>
|
||||
<goal>compile</goal>
|
||||
<goal>compileTests</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-eclipse-compiler</artifactId>
|
||||
<version>3.3.0-01</version>
|
||||
<extensions>true</extensions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.0</version>
|
||||
<configuration>
|
||||
<compilerId>groovy-eclipse-compiler</compilerId>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-eclipse-compiler</artifactId>
|
||||
<version>3.3.0-01</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-eclipse-batch</artifactId>
|
||||
<version>${groovy.version}-01</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-failsafe-plugin</artifactId>
|
||||
|
@ -101,13 +110,42 @@
|
|||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.20.1</version>
|
||||
<configuration>
|
||||
<useFile>false</useFile>
|
||||
<includes>
|
||||
<include>**/*Test.java</include>
|
||||
<include>**/*Spec.java</include>
|
||||
</includes>
|
||||
<useFile>false</useFile>
|
||||
<includes>
|
||||
<include>**/*Test.java</include>
|
||||
<include>**/*Spec.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<!-- Maven Assembly Plugin: needed to run the jar through command line -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<configuration>
|
||||
<!-- get all project dependencies -->
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
<!-- MainClass in mainfest make a executable jar -->
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>com.baeldung.MyJointCompilationApp</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>make-assembly</id>
|
||||
<!-- bind to the packaging phase -->
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
@ -118,14 +156,32 @@
|
|||
</repository>
|
||||
</repositories>
|
||||
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>bintray</id>
|
||||
<name>Groovy Bintray</name>
|
||||
<url>https://dl.bintray.com/groovy/maven</url>
|
||||
<releases>
|
||||
<!-- avoid automatic updates -->
|
||||
<updatePolicy>never</updatePolicy>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
|
||||
<properties>
|
||||
<junit.platform.version>1.0.0</junit.platform.version>
|
||||
<groovy.version>2.5.6</groovy.version>
|
||||
<groovy-all.version>2.5.6</groovy-all.version>
|
||||
<groovy-sql.version>2.5.6</groovy-sql.version>
|
||||
<hsqldb.version>2.4.0</hsqldb.version>
|
||||
<spock-core.version>1.1-groovy-2.4</spock-core.version>
|
||||
<gmavenplus-plugin.version>1.6</gmavenplus-plugin.version>
|
||||
<commons-lang3.version>3.9</commons-lang3.version>
|
||||
<java.version>1.8</java.version>
|
||||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||
<logback.version>1.2.3</logback.version>
|
||||
<groovy.version>2.5.7</groovy.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
</project>
|
||||
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung
|
||||
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
class CalcMath {
|
||||
def log = LoggerFactory.getLogger(this.getClass())
|
||||
|
||||
def calcSum(x, y) {
|
||||
log.info "Executing $x + $y"
|
||||
x + y
|
||||
}
|
||||
|
||||
/**
|
||||
* example of method that in java would throw error at compile time
|
||||
* @param x
|
||||
* @param y
|
||||
* @return
|
||||
*/
|
||||
def calcSum2(x, y) {
|
||||
log.info "Executing $x + $y"
|
||||
// DANGER! This won't throw a compilation issue and fail only at runtime!!!
|
||||
calcSum3()
|
||||
log.info("Logging an undefined variable: $z")
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung
|
||||
|
||||
def calcSum(x, y) {
|
||||
x + y
|
||||
}
|
||||
|
||||
def calcSum2(x, y) {
|
||||
// DANGER! The variable "log" may be undefined
|
||||
log.info "Executing $x + $y"
|
||||
// DANGER! This method doesn't exist!
|
||||
calcSum3()
|
||||
// DANGER! The logged variable "z" is undefined!
|
||||
log.info("Logging an undefined variable: $z")
|
||||
}
|
||||
|
||||
calcSum(1,5)
|
|
@ -0,0 +1,120 @@
|
|||
package com.baeldung;
|
||||
|
||||
import groovy.lang.*;
|
||||
import groovy.util.GroovyScriptEngine;
|
||||
import groovy.util.ResourceException;
|
||||
import groovy.util.ScriptException;
|
||||
import org.codehaus.groovy.jsr223.GroovyScriptEngineFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import javax.script.ScriptEngine;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
public class MyJointCompilationApp {
|
||||
private final static Logger LOG = LoggerFactory.getLogger(MyJointCompilationApp.class);
|
||||
private final GroovyClassLoader loader;
|
||||
private final GroovyShell shell;
|
||||
private final GroovyScriptEngine engine;
|
||||
private final ScriptEngine engineFromFactory;
|
||||
|
||||
public MyJointCompilationApp() {
|
||||
loader = new GroovyClassLoader(this.getClass().getClassLoader());
|
||||
shell = new GroovyShell(loader, new Binding());
|
||||
|
||||
URL url = null;
|
||||
try {
|
||||
url = new File("src/main/groovy/com/baeldung/").toURI().toURL();
|
||||
} catch (MalformedURLException e) {
|
||||
LOG.error("Exception while creating url", e);
|
||||
}
|
||||
engine = new GroovyScriptEngine(new URL[] {url}, this.getClass().getClassLoader());
|
||||
engineFromFactory = new GroovyScriptEngineFactory().getScriptEngine();
|
||||
}
|
||||
|
||||
private void addWithCompiledClasses(int x, int y) {
|
||||
LOG.info("Executing {} + {}", x, y);
|
||||
Object result1 = new CalcScript().calcSum(x, y);
|
||||
LOG.info("Result of CalcScript.calcSum() method is {}", result1);
|
||||
|
||||
Object result2 = new CalcMath().calcSum(x, y);
|
||||
LOG.info("Result of CalcMath.calcSum() method is {}", result2);
|
||||
}
|
||||
|
||||
private void addWithGroovyShell(int x, int y) throws IOException {
|
||||
Script script = shell.parse(new File("src/main/groovy/com/baeldung/", "CalcScript.groovy"));
|
||||
LOG.info("Executing {} + {}", x, y);
|
||||
Object result = script.invokeMethod("calcSum", new Object[] { x, y });
|
||||
LOG.info("Result of CalcScript.calcSum() method is {}", result);
|
||||
}
|
||||
|
||||
private void addWithGroovyShellRun() throws IOException {
|
||||
Script script = shell.parse(new File("src/main/groovy/com/baeldung/", "CalcScript.groovy"));
|
||||
LOG.info("Executing script run method");
|
||||
Object result = script.run();
|
||||
LOG.info("Result of CalcScript.run() method is {}", result);
|
||||
}
|
||||
|
||||
private void addWithGroovyClassLoader(int x, int y) throws IllegalAccessException, InstantiationException, IOException {
|
||||
Class calcClass = loader.parseClass(
|
||||
new File("src/main/groovy/com/baeldung/", "CalcMath.groovy"));
|
||||
GroovyObject calc = (GroovyObject) calcClass.newInstance();
|
||||
Object result = calc.invokeMethod("calcSum", new Object[] { x + 14, y + 14 });
|
||||
LOG.info("Result of CalcMath.calcSum() method is {}", result);
|
||||
}
|
||||
|
||||
private void addWithGroovyScriptEngine(int x, int y) throws IllegalAccessException,
|
||||
InstantiationException, ResourceException, ScriptException {
|
||||
Class<GroovyObject> calcClass = engine.loadScriptByName("CalcMath.groovy");
|
||||
GroovyObject calc = calcClass.newInstance();
|
||||
//WARNING the following will throw a ClassCastException
|
||||
//((CalcMath)calc).calcSum(1,2);
|
||||
Object result = calc.invokeMethod("calcSum", new Object[] { x, y });
|
||||
LOG.info("Result of CalcMath.calcSum() method is {}", result);
|
||||
}
|
||||
|
||||
private void addWithEngineFactory(int x, int y) throws IllegalAccessException,
|
||||
InstantiationException, javax.script.ScriptException, FileNotFoundException {
|
||||
Class calcClass = (Class) engineFromFactory.eval(
|
||||
new FileReader(new File("src/main/groovy/com/baeldung/", "CalcMath.groovy")));
|
||||
GroovyObject calc = (GroovyObject) calcClass.newInstance();
|
||||
Object result = calc.invokeMethod("calcSum", new Object[] { x, y });
|
||||
LOG.info("Result of CalcMath.calcSum() method is {}", result);
|
||||
}
|
||||
|
||||
private void addWithStaticCompiledClasses() {
|
||||
LOG.info("Running the Groovy classes compiled statically...");
|
||||
addWithCompiledClasses(5, 10);
|
||||
|
||||
}
|
||||
|
||||
private void addWithDynamicCompiledClasses() throws IOException, IllegalAccessException, InstantiationException,
|
||||
ResourceException, ScriptException, javax.script.ScriptException {
|
||||
LOG.info("Invocation of a dynamic groovy script...");
|
||||
addWithGroovyShell(5, 10);
|
||||
LOG.info("Invocation of the run method of a dynamic groovy script...");
|
||||
addWithGroovyShellRun();
|
||||
LOG.info("Invocation of a dynamic groovy class loaded with GroovyClassLoader...");
|
||||
addWithGroovyClassLoader(10, 30);
|
||||
LOG.info("Invocation of a dynamic groovy class loaded with GroovyScriptEngine...");
|
||||
addWithGroovyScriptEngine(15, 0);
|
||||
LOG.info("Invocation of a dynamic groovy class loaded with GroovyScriptEngine JSR223...");
|
||||
addWithEngineFactory(5, 6);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InstantiationException, IllegalAccessException,
|
||||
ResourceException, ScriptException, IOException, javax.script.ScriptException {
|
||||
MyJointCompilationApp myJointCompilationApp = new MyJointCompilationApp();
|
||||
LOG.info("Example of addition operation via Groovy scripts integration with Java.");
|
||||
myJointCompilationApp.addWithStaticCompiledClasses();
|
||||
myJointCompilationApp.addWithDynamicCompiledClasses();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.xml
|
||||
|
||||
import groovy.xml.MarkupBuilder
|
||||
import groovy.xml.XmlUtil
|
||||
import spock.lang.Specification
|
||||
|
||||
class MarkupBuilderUnitTest extends Specification {
|
||||
|
||||
def xmlFile = getClass().getResource("articles_short_formatted.xml")
|
||||
|
||||
def "Should create XML properly"() {
|
||||
given: "Node structures"
|
||||
|
||||
when: "Using MarkupBuilderUnitTest to create com.baeldung.xml structure"
|
||||
def writer = new StringWriter()
|
||||
new MarkupBuilder(writer).articles {
|
||||
article {
|
||||
title('First steps in Java')
|
||||
author(id: '1') {
|
||||
firstname('Siena')
|
||||
lastname('Kerr')
|
||||
}
|
||||
'release-date'('2018-12-01')
|
||||
}
|
||||
article {
|
||||
title('Dockerize your SpringBoot application')
|
||||
author(id: '2') {
|
||||
firstname('Jonas')
|
||||
lastname('Lugo')
|
||||
}
|
||||
'release-date'('2018-12-01')
|
||||
}
|
||||
}
|
||||
|
||||
then: "Xml is created properly"
|
||||
XmlUtil.serialize(writer.toString()) == XmlUtil.serialize(xmlFile.text)
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
package com.baeldung.xml
|
||||
|
||||
|
||||
import spock.lang.Shared
|
||||
import spock.lang.Specification
|
||||
|
||||
class XmlParserUnitTest extends Specification {
|
||||
|
||||
def xmlFile = getClass().getResourceAsStream("articles.xml")
|
||||
|
||||
@Shared
|
||||
def parser = new XmlParser()
|
||||
|
||||
def "Should read XML file properly"() {
|
||||
given: "XML file"
|
||||
|
||||
when: "Using XmlParser to read file"
|
||||
def articles = parser.parse(xmlFile)
|
||||
|
||||
then: "Xml is loaded properly"
|
||||
articles.'*'.size() == 4
|
||||
articles.article[0].author.firstname.text() == "Siena"
|
||||
articles.article[2].'release-date'.text() == "2018-06-12"
|
||||
articles.article[3].title.text() == "Java 12 insights"
|
||||
articles.article.find { it.author.'@id'.text() == "3" }.author.firstname.text() == "Daniele"
|
||||
}
|
||||
|
||||
|
||||
def "Should add node to existing com.baeldung.xml using NodeBuilder"() {
|
||||
given: "XML object"
|
||||
def articles = parser.parse(xmlFile)
|
||||
|
||||
when: "Adding node to com.baeldung.xml"
|
||||
def articleNode = new NodeBuilder().article(id: '5') {
|
||||
title('Traversing XML in the nutshell')
|
||||
author {
|
||||
firstname('Martin')
|
||||
lastname('Schmidt')
|
||||
}
|
||||
'release-date'('2019-05-18')
|
||||
}
|
||||
articles.append(articleNode)
|
||||
|
||||
then: "Node is added to com.baeldung.xml properly"
|
||||
articles.'*'.size() == 5
|
||||
articles.article[4].title.text() == "Traversing XML in the nutshell"
|
||||
}
|
||||
|
||||
def "Should replace node"() {
|
||||
given: "XML object"
|
||||
def articles = parser.parse(xmlFile)
|
||||
|
||||
when: "Adding node to com.baeldung.xml"
|
||||
def articleNode = new NodeBuilder().article(id: '5') {
|
||||
title('Traversing XML in the nutshell')
|
||||
author {
|
||||
firstname('Martin')
|
||||
lastname('Schmidt')
|
||||
}
|
||||
'release-date'('2019-05-18')
|
||||
}
|
||||
articles.article[0].replaceNode(articleNode)
|
||||
|
||||
then: "Node is added to com.baeldung.xml properly"
|
||||
articles.'*'.size() == 4
|
||||
articles.article[0].title.text() == "Traversing XML in the nutshell"
|
||||
}
|
||||
|
||||
def "Should modify node"() {
|
||||
given: "XML object"
|
||||
def articles = parser.parse(xmlFile)
|
||||
|
||||
when: "Changing value of one of the nodes"
|
||||
articles.article.each { it.'release-date'[0].value = "2019-05-18" }
|
||||
|
||||
then: "XML is updated"
|
||||
articles.article.findAll { it.'release-date'.text() != "2019-05-18" }.isEmpty()
|
||||
}
|
||||
|
||||
def "Should remove article from com.baeldung.xml"() {
|
||||
given: "XML object"
|
||||
def articles = parser.parse(xmlFile)
|
||||
|
||||
when: "Removing all articles but with id==3"
|
||||
articles.article
|
||||
.findAll { it.author.'@id'.text() != "3" }
|
||||
.each { articles.remove(it) }
|
||||
|
||||
then: "There is only one article left"
|
||||
articles.children().size() == 1
|
||||
articles.article[0].author.'@id'.text() == "3"
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,102 @@
|
|||
package com.baeldung.xml
|
||||
|
||||
|
||||
import groovy.xml.XmlUtil
|
||||
import spock.lang.Shared
|
||||
import spock.lang.Specification
|
||||
|
||||
class XmlSlurperUnitTest extends Specification {
|
||||
|
||||
def xmlFile = getClass().getResourceAsStream("articles.xml")
|
||||
|
||||
@Shared
|
||||
def parser = new XmlSlurper()
|
||||
|
||||
def "Should read XML file properly"() {
|
||||
given: "XML file"
|
||||
|
||||
when: "Using XmlSlurper to read file"
|
||||
def articles = parser.parse(xmlFile)
|
||||
|
||||
then: "Xml is loaded properly"
|
||||
articles.'*'.size() == 4
|
||||
articles.article[0].author.firstname == "Siena"
|
||||
articles.article[2].'release-date' == "2018-06-12"
|
||||
articles.article[3].title == "Java 12 insights"
|
||||
articles.article.find { it.author.'@id' == "3" }.author.firstname == "Daniele"
|
||||
}
|
||||
|
||||
def "Should add node to existing com.baeldung.xml"() {
|
||||
given: "XML object"
|
||||
def articles = parser.parse(xmlFile)
|
||||
|
||||
when: "Adding node to com.baeldung.xml"
|
||||
articles.appendNode {
|
||||
article(id: '5') {
|
||||
title('Traversing XML in the nutshell')
|
||||
author {
|
||||
firstname('Martin')
|
||||
lastname('Schmidt')
|
||||
}
|
||||
'release-date'('2019-05-18')
|
||||
}
|
||||
}
|
||||
|
||||
articles = parser.parseText(XmlUtil.serialize(articles))
|
||||
|
||||
then: "Node is added to com.baeldung.xml properly"
|
||||
articles.'*'.size() == 5
|
||||
articles.article[4].title == "Traversing XML in the nutshell"
|
||||
}
|
||||
|
||||
def "Should modify node"() {
|
||||
given: "XML object"
|
||||
def articles = parser.parse(xmlFile)
|
||||
|
||||
when: "Changing value of one of the nodes"
|
||||
articles.article.each { it.'release-date' = "2019-05-18" }
|
||||
|
||||
then: "XML is updated"
|
||||
articles.article.findAll { it.'release-date' != "2019-05-18" }.isEmpty()
|
||||
}
|
||||
|
||||
def "Should replace node"() {
|
||||
given: "XML object"
|
||||
def articles = parser.parse(xmlFile)
|
||||
|
||||
when: "Replacing node"
|
||||
articles.article[0].replaceNode {
|
||||
article(id: '5') {
|
||||
title('Traversing XML in the nutshell')
|
||||
author {
|
||||
firstname('Martin')
|
||||
lastname('Schmidt')
|
||||
}
|
||||
'release-date'('2019-05-18')
|
||||
}
|
||||
}
|
||||
|
||||
articles = parser.parseText(XmlUtil.serialize(articles))
|
||||
|
||||
then: "Node is replaced properly"
|
||||
articles.'*'.size() == 4
|
||||
articles.article[0].title == "Traversing XML in the nutshell"
|
||||
}
|
||||
|
||||
def "Should remove article from com.baeldung.xml"() {
|
||||
given: "XML object"
|
||||
def articles = parser.parse(xmlFile)
|
||||
|
||||
when: "Removing all articles but with id==3"
|
||||
articles.article
|
||||
.findAll { it.author.'@id' != "3" }
|
||||
.replaceNode {}
|
||||
|
||||
articles = parser.parseText(XmlUtil.serialize(articles))
|
||||
|
||||
then: "There is only one article left"
|
||||
articles.children().size() == 1
|
||||
articles.article[0].author.'@id' == "3"
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
<articles>
|
||||
<article>
|
||||
<title>First steps in Java</title>
|
||||
<author id='1'>
|
||||
<firstname>Siena</firstname>
|
||||
<lastname>Kerr</lastname>
|
||||
</author>
|
||||
<release-date>2018-12-01</release-date>
|
||||
</article>
|
||||
<article>
|
||||
<title>Dockerize your SpringBoot application</title>
|
||||
<author id='2'>
|
||||
<firstname>Jonas</firstname>
|
||||
<lastname>Lugo</lastname>
|
||||
</author>
|
||||
<release-date>2018-12-01</release-date>
|
||||
</article>
|
||||
<article>
|
||||
<title>SpringBoot tutorial</title>
|
||||
<author id='3'>
|
||||
<firstname>Daniele</firstname>
|
||||
<lastname>Ferguson</lastname>
|
||||
</author>
|
||||
<release-date>2018-06-12</release-date>
|
||||
</article>
|
||||
<article>
|
||||
<title>Java 12 insights</title>
|
||||
<author id='1'>
|
||||
<firstname>Siena</firstname>
|
||||
<lastname>Kerr</lastname>
|
||||
</author>
|
||||
<release-date>2018-07-22</release-date>
|
||||
</article>
|
||||
</articles>
|
|
@ -0,0 +1,18 @@
|
|||
<articles>
|
||||
<article>
|
||||
<title>First steps in Java</title>
|
||||
<author id='1'>
|
||||
<firstname>Siena</firstname>
|
||||
<lastname>Kerr</lastname>
|
||||
</author>
|
||||
<release-date>2018-12-01</release-date>
|
||||
</article>
|
||||
<article>
|
||||
<title>Dockerize your SpringBoot application</title>
|
||||
<author id='2'>
|
||||
<firstname>Jonas</firstname>
|
||||
<lastname>Lugo</lastname>
|
||||
</author>
|
||||
<release-date>2018-12-01</release-date>
|
||||
</article>
|
||||
</articles>
|
|
@ -1,3 +1,5 @@
|
|||
## Relevant articles:
|
||||
|
||||
- [Multi-Module Maven Application with Java Modules](https://www.baeldung.com/maven-multi-module-project-java-jpms)
|
||||
- [Guide to Java FileChannel](https://www.baeldung.com/java-filechannel)
|
||||
- [Understanding the NumberFormatException in Java](https://www.baeldung.com/java-number-format-exception)
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.set;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class CopySets {
|
||||
|
||||
// Using Java 10
|
||||
public static <T> Set<T> copyBySetCopyOf(Set<T> original) {
|
||||
Set<T> copy = Set.copyOf(original);
|
||||
return copy;
|
||||
}
|
||||
|
||||
}
|
|
@ -7,4 +7,5 @@
|
|||
- [Exploring the New HTTP Client in Java 9 and 11](https://www.baeldung.com/java-9-http-client)
|
||||
- [An Introduction to Epsilon GC: A No-Op Experimental Garbage Collector](https://www.baeldung.com/jvm-epsilon-gc-garbage-collector)
|
||||
- [Guide to jlink](https://www.baeldung.com/jlink)
|
||||
- [Negate a Predicate Method Reference with Java 11](https://www.baeldung.com/java-negate-predicate-method-reference)
|
||||
- [Transforming an Empty String into an Empty Optional](https://www.baeldung.com/java-empty-string-to-empty-optional)
|
||||
|
|
|
@ -3,5 +3,6 @@
|
|||
## Core Java 8 Cookbooks and Examples (part 2)
|
||||
|
||||
### Relevant Articles:
|
||||
- [Anonymous Classes in Java](https://www.baeldung.com/java-anonymous-classes)
|
||||
- [Anonymous Classes in Java](http://www.baeldung.com/)
|
||||
- [How to Delay Code Execution in Java](https://www.baeldung.com/java-delay-code-execution)
|
||||
- [Run JAR Application With Command Line Arguments](https://www.baeldung.com/java-run-jar-with-arguments)
|
||||
|
|
|
@ -22,9 +22,6 @@
|
|||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<icu.version>64.2</icu.version>
|
||||
<hibernate.core.version>5.4.0.Final</hibernate.core.version>
|
||||
<h2database.version>1.4.197</h2database.version>
|
||||
<jackson.databind.version>2.9.8</jackson.databind.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
@ -33,21 +30,6 @@
|
|||
<artifactId>icu4j</artifactId>
|
||||
<version>${icu.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>${hibernate.core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2database.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.databind.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -15,3 +15,4 @@
|
|||
- [Intersection Between two Integer Arrays](https://www.baeldung.com/java-array-intersection)
|
||||
- [Sorting Arrays in Java](https://www.baeldung.com/java-sorting-arrays)
|
||||
- [Convert a Float to a Byte Array in Java](https://www.baeldung.com/java-convert-float-to-byte-array)
|
||||
- [Converting Between Stream and Array in Java](https://www.baeldung.com/java-stream-to-array)
|
||||
|
|
|
@ -24,10 +24,20 @@
|
|||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.8.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-lang</groupId>
|
||||
<artifactId>commons-lang</artifactId>
|
||||
<version>2.6</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<commons-collections4.version>4.3</commons-collections4.version>
|
||||
<guava.version>27.1-jre</guava.version>
|
||||
</properties>
|
||||
</project>
|
||||
</project>
|
|
@ -0,0 +1,59 @@
|
|||
package com.baeldung.set;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.lang.SerializationUtils;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
public class CopySets {
|
||||
|
||||
// Copy Constructor
|
||||
public static <T> Set<T> copyByConstructor(Set<T> original) {
|
||||
Set<T> copy = new HashSet<>(original);
|
||||
return copy;
|
||||
}
|
||||
|
||||
// Set.addAll
|
||||
public static <T> Set<T> copyBySetAddAll(Set<T> original) {
|
||||
Set<T> copy = new HashSet<>();
|
||||
copy.addAll(original);
|
||||
return copy;
|
||||
}
|
||||
|
||||
// Set.clone
|
||||
public static <T> Set<T> copyBySetClone(HashSet<T> original) {
|
||||
Set<T> copy = (Set<T>) original.clone();
|
||||
return copy;
|
||||
}
|
||||
|
||||
// JSON
|
||||
public static <T> Set<T> copyByJson(Set<T> original) {
|
||||
Gson gson = new Gson();
|
||||
String jsonStr = gson.toJson(original);
|
||||
Set<T> copy = gson.fromJson(jsonStr, Set.class);
|
||||
|
||||
return copy;
|
||||
}
|
||||
|
||||
// Apache Commons Lang
|
||||
public static <T extends Serializable> Set<T> copyByApacheCommonsLang(Set<T> original) {
|
||||
Set<T> copy = new HashSet<>();
|
||||
for (T item : original) {
|
||||
copy.add((T) SerializationUtils.clone(item));
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
|
||||
// Collectors.toSet
|
||||
public static <T extends Serializable> Set<T> copyByCollectorsToSet(Set<T> original) {
|
||||
Set<T> copy = original.stream()
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
return copy;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.delay;
|
||||
package com.baeldung.concurrent.delay;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
@ -61,6 +61,7 @@ public class Delay {
|
|||
|
||||
executorService.schedule(Delay::someTask1, delayInSeconds, TimeUnit.SECONDS);
|
||||
|
||||
executorService.shutdown();
|
||||
}
|
||||
|
||||
private static void fixedRateServiceTask(Integer delayInSeconds) {
|
||||
|
@ -78,6 +79,7 @@ public class Delay {
|
|||
|
||||
sf.cancel(true);
|
||||
|
||||
executorService.shutdown();
|
||||
}
|
||||
|
||||
private static void someTask1() {
|
|
@ -0,0 +1,3 @@
|
|||
## Relevant articles:
|
||||
|
||||
- [Determine File Creating Date in Java](https://www.baeldung.com/file-creation-date-java)
|
|
@ -0,0 +1,35 @@
|
|||
package com.baeldung.creationdate;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.nio.file.attribute.FileTime;
|
||||
import java.time.Instant;
|
||||
import java.util.Optional;
|
||||
|
||||
public class CreationDateResolver {
|
||||
|
||||
public Instant resolveCreationTimeWithBasicAttributes(Path path) {
|
||||
try {
|
||||
final BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
|
||||
final FileTime fileTime = attr.creationTime();
|
||||
|
||||
return fileTime.toInstant();
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException("An issue occured went wrong when resolving creation time", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<Instant> resolveCreationTimeWithAttribute(Path path) {
|
||||
try {
|
||||
final FileTime creationTime = (FileTime) Files.getAttribute(path, "creationTime");
|
||||
|
||||
return Optional
|
||||
.ofNullable(creationTime)
|
||||
.map((FileTime::toInstant));
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException("An issue occured went wrong when resolving creation time", ex);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package com.baeldung.creationdate;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.time.Instant;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class CreationDateResolverUnitTest {
|
||||
|
||||
private final CreationDateResolver creationDateResolver = new CreationDateResolver();
|
||||
|
||||
@Test
|
||||
public void givenFile_whenGettingCreationDateTimeFromBasicAttributes_thenReturnDate() throws Exception {
|
||||
|
||||
final File file = File.createTempFile("createdFile", ".txt");
|
||||
final Path path = file.toPath();
|
||||
|
||||
final Instant response = creationDateResolver.resolveCreationTimeWithBasicAttributes(path);
|
||||
|
||||
assertTrue(Instant
|
||||
.now()
|
||||
.isAfter(response));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFile_whenGettingCreationDateTimeFromAttribute_thenReturnDate() throws Exception {
|
||||
|
||||
final File file = File.createTempFile("createdFile", ".txt");
|
||||
final Path path = file.toPath();
|
||||
|
||||
final Optional<Instant> response = creationDateResolver.resolveCreationTimeWithAttribute(path);
|
||||
|
||||
response.ifPresent((value) -> {
|
||||
assertTrue(Instant
|
||||
.now()
|
||||
.isAfter(value));
|
||||
});
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
=========
|
||||
|
||||
## Core Java Optional
|
||||
|
||||
### Relevant Articles:
|
|
@ -0,0 +1,53 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>core-java-optional</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<hibernate.core.version>5.4.0.Final</hibernate.core.version>
|
||||
<h2database.version>1.4.197</h2database.version>
|
||||
<jackson.databind.version>2.9.8</jackson.databind.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>${hibernate.core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2database.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.databind.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
</project>
|
|
@ -51,4 +51,5 @@
|
|||
- [Java Bitwise Operators](https://www.baeldung.com/java-bitwise-operators)
|
||||
- [Guide to Creating and Running a Jar File in Java](https://www.baeldung.com/java-create-jar)
|
||||
- [Making a JSON POST Request With HttpURLConnection](https://www.baeldung.com/httpurlconnection-post)
|
||||
- [How to Find an Exception’s Root Cause in Java](https://www.baeldung.com/java-exception-root-cause)
|
||||
- [Convert Hex to ASCII in Java](https://www.baeldung.com/java-convert-hex-to-ascii)
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
<modules>
|
||||
<module>pre-jpms</module>
|
||||
<module>core-java-exceptions</module>
|
||||
<module>core-java-optional</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
### Relevant articles
|
||||
|
||||
- [Persisting DDD Aggregates](https://www.baeldung.com/spring-persisting-ddd-aggregates)
|
||||
- [Double Dispatch in DDD](https://www.baeldung.com/ddd-double-dispatch)
|
||||
- [DDD Aggregates and @DomainEvents](https://www.baeldung.com/spring-data-ddd)
|
||||
|
|
|
@ -28,6 +28,13 @@
|
|||
<artifactId>jackson-dataformat-yaml</artifactId>
|
||||
<version>2.9.8</version>
|
||||
</dependency>
|
||||
|
||||
<!-- CSV -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-csv</artifactId>
|
||||
<version>2.9.8</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Allow use of LocalDate -->
|
||||
<dependency>
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
package com.baeldung.jackson.csv;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.entities.OrderLine;
|
||||
import com.baeldung.jackson.mixin.OrderLineForCsv;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.MappingIterator;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
|
||||
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
|
||||
import com.fasterxml.jackson.dataformat.csv.CsvSchema.Builder;
|
||||
|
||||
public class JsonCsvConverter {
|
||||
|
||||
public static void JsonToCsv(File jsonFile, File csvFile) throws IOException {
|
||||
JsonNode jsonTree = new ObjectMapper().readTree(jsonFile);
|
||||
|
||||
Builder csvSchemaBuilder = CsvSchema.builder();
|
||||
JsonNode firstObject = jsonTree.elements().next();
|
||||
firstObject.fieldNames().forEachRemaining(fieldName -> {csvSchemaBuilder.addColumn(fieldName);} );
|
||||
CsvSchema csvSchema = csvSchemaBuilder
|
||||
.build()
|
||||
.withHeader();
|
||||
|
||||
CsvMapper csvMapper = new CsvMapper();
|
||||
csvMapper.writerFor(JsonNode.class)
|
||||
.with(csvSchema)
|
||||
.writeValue(csvFile, jsonTree);
|
||||
}
|
||||
|
||||
public static void csvToJson(File csvFile, File jsonFile) throws IOException {
|
||||
CsvSchema orderLineSchema = CsvSchema.emptySchema().withHeader();
|
||||
CsvMapper csvMapper = new CsvMapper();
|
||||
MappingIterator<OrderLine> orderLines = csvMapper.readerFor(OrderLine.class)
|
||||
.with(orderLineSchema)
|
||||
.readValues(csvFile);
|
||||
|
||||
new ObjectMapper()
|
||||
.configure(SerializationFeature.INDENT_OUTPUT, true)
|
||||
.writeValue(jsonFile, orderLines.readAll());
|
||||
}
|
||||
|
||||
public static void JsonToFormattedCsv(File jsonFile, File csvFile) throws IOException {
|
||||
CsvMapper csvMapper = new CsvMapper();
|
||||
CsvSchema csvSchema = csvMapper
|
||||
.schemaFor(OrderLineForCsv.class)
|
||||
.withHeader();
|
||||
csvMapper.addMixIn(OrderLine.class, OrderLineForCsv.class);
|
||||
|
||||
OrderLine[] orderLines = new ObjectMapper()
|
||||
.readValue(jsonFile, OrderLine[].class);
|
||||
csvMapper.writerFor(OrderLine[].class)
|
||||
.with(csvSchema)
|
||||
.writeValue(csvFile, orderLines);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.jackson.mixin;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
|
||||
@JsonPropertyOrder({
|
||||
"count",
|
||||
"name"
|
||||
})
|
||||
public abstract class OrderLineForCsv {
|
||||
|
||||
@JsonProperty("name")
|
||||
private String item;
|
||||
|
||||
@JsonProperty("count")
|
||||
private int quantity;
|
||||
|
||||
@JsonIgnore
|
||||
private BigDecimal unitPrice;
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
item,quantity,unitPrice
|
||||
"No. 9 Sprockets",12,1.23
|
||||
"Widget (10mm)",4,3.45
|
|
|
@ -0,0 +1,9 @@
|
|||
[ {
|
||||
"item" : "No. 9 Sprockets",
|
||||
"quantity" : 12,
|
||||
"unitPrice" : 1.23
|
||||
}, {
|
||||
"item" : "Widget (10mm)",
|
||||
"quantity" : 4,
|
||||
"unitPrice" : 3.45
|
||||
} ]
|
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.jackson.csv;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.google.common.io.Files;
|
||||
|
||||
|
||||
public class CsvUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenJsonInput_thenWriteCsv() throws JsonParseException, JsonMappingException, IOException {
|
||||
JsonCsvConverter.JsonToCsv(new File("src/main/resources/orderLines.json"),
|
||||
new File("src/main/resources/csvFromJson.csv"));
|
||||
|
||||
assertEquals(readFile("src/main/resources/csvFromJson.csv"),
|
||||
readFile("src/test/resources/expectedCsvFromJson.csv"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCsvInput_thenWritesJson() throws JsonParseException, JsonMappingException, IOException {
|
||||
JsonCsvConverter.csvToJson(new File("src/main/resources/orderLines.csv"),
|
||||
new File("src/main/resources/jsonFromCsv.json"));
|
||||
|
||||
assertEquals(readFile("src/main/resources/jsonFromCsv.json"),
|
||||
readFile("src/test/resources/expectedJsonFromCsv.json"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJsonInput_thenWriteFormattedCsvOutput() throws JsonParseException, JsonMappingException, IOException {
|
||||
JsonCsvConverter.JsonToFormattedCsv(new File("src/main/resources/orderLines.json"),
|
||||
new File("src/main/resources/formattedCsvFromJson.csv"));
|
||||
|
||||
assertEquals(readFile("src/main/resources/formattedCsvFromJson.csv"),
|
||||
readFile("src/test/resources/expectedFormattedCsvFromJson.csv"));
|
||||
|
||||
}
|
||||
|
||||
private List<String> readFile(String filename) throws IOException {
|
||||
return Files.readLines(new File(filename), Charset.forName("utf-8"));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
;
|
|
@ -0,0 +1,3 @@
|
|||
item,quantity,unitPrice
|
||||
"No. 9 Sprockets",12,1.23
|
||||
"Widget (10mm)",4,3.45
|
|
|
@ -0,0 +1,3 @@
|
|||
count,name
|
||||
12,"No. 9 Sprockets"
|
||||
4,"Widget (10mm)"
|
|
|
@ -0,0 +1,9 @@
|
|||
[ {
|
||||
"item" : "No. 9 Sprockets",
|
||||
"quantity" : 12,
|
||||
"unitPrice" : 1.23
|
||||
}, {
|
||||
"item" : "Widget (10mm)",
|
||||
"quantity" : 4,
|
||||
"unitPrice" : 3.45
|
||||
} ]
|
|
@ -6,3 +6,4 @@
|
|||
- [A Guide to Java EE Web-Related Annotations](http://www.baeldung.com/javaee-web-annotations)
|
||||
- [Introduction to Testing with Arquillian](http://www.baeldung.com/arquillian)
|
||||
- [Java EE 7 Batch Processing](https://www.baeldung.com/java-ee-7-batch-processing)
|
||||
- [The Difference Between CDI and EJB Singleton](https://www.baeldung.com/jee-cdi-vs-ejb-singleton)
|
||||
|
|
|
@ -3,3 +3,4 @@
|
|||
- [Bean Validation in Jersey](https://www.baeldung.com/jersey-bean-validation)
|
||||
- [Set a Response Body in JAX-RS](https://www.baeldung.com/jax-rs-response)
|
||||
- [Exploring the Jersey Test Framework](https://www.baeldung.com/jersey-test)
|
||||
- [Explore Jersey Request Parameters](https://www.baeldung.com/jersey-request-parameters)
|
||||
|
|
|
@ -26,6 +26,7 @@ public class FruitResourceIntegrationTest extends JerseyTest {
|
|||
protected Application configure() {
|
||||
enable(TestProperties.LOG_TRAFFIC);
|
||||
enable(TestProperties.DUMP_ENTITY);
|
||||
forceSet(TestProperties.CONTAINER_PORT, "0");
|
||||
|
||||
ViewApplicationConfig config = new ViewApplicationConfig();
|
||||
config.register(FruitExceptionMapper.class);
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
## Relevant articles:
|
||||
|
||||
- [Jackson Support for Kotlin](https://www.baeldung.com/jackson-kotlin)
|
||||
- [Introduction to RxKotlin](https://www.baeldung.com/rxkotlin)
|
||||
|
|
|
@ -55,6 +55,39 @@
|
|||
<artifactId>spring-boot-starter</artifactId>
|
||||
<version>${spring-boot-starter.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Dependencies for response decoder with okhttp -->
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>3.14.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.9.9</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.8.5</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>mockwebserver</artifactId>
|
||||
<version>3.14.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>edu.uci.ics</groupId>
|
||||
<artifactId>crawler4j</artifactId>
|
||||
<version>${crawler4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
@ -62,6 +95,7 @@
|
|||
<classgraph.version>4.8.28</classgraph.version>
|
||||
<jbpm.version>6.0.0.Final</jbpm.version>
|
||||
<picocli.version>3.9.6</picocli.version>
|
||||
<crawler4j.version>4.4.0</crawler4j.version>
|
||||
<spring-boot-starter.version>2.1.4.RELEASE</spring-boot-starter.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.crawler4j;
|
||||
|
||||
public class CrawlerStatistics {
|
||||
private int processedPageCount = 0;
|
||||
private int totalLinksCount = 0;
|
||||
|
||||
public void incrementProcessedPageCount() {
|
||||
processedPageCount++;
|
||||
}
|
||||
|
||||
public void incrementTotalLinksCount(int linksCount) {
|
||||
totalLinksCount += linksCount;
|
||||
}
|
||||
|
||||
public int getProcessedPageCount() {
|
||||
return processedPageCount;
|
||||
}
|
||||
|
||||
public int getTotalLinksCount() {
|
||||
return totalLinksCount;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package com.baeldung.crawler4j;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import edu.uci.ics.crawler4j.crawler.Page;
|
||||
import edu.uci.ics.crawler4j.crawler.WebCrawler;
|
||||
import edu.uci.ics.crawler4j.parser.HtmlParseData;
|
||||
import edu.uci.ics.crawler4j.url.WebURL;
|
||||
|
||||
public class HtmlCrawler extends WebCrawler {
|
||||
|
||||
private final static Pattern EXCLUSIONS = Pattern.compile(".*(\\.(css|js|xml|gif|jpg|png|mp3|mp4|zip|gz|pdf))$");
|
||||
|
||||
private CrawlerStatistics stats;
|
||||
|
||||
public HtmlCrawler(CrawlerStatistics stats) {
|
||||
this.stats = stats;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldVisit(Page referringPage, WebURL url) {
|
||||
String urlString = url.getURL().toLowerCase();
|
||||
return !EXCLUSIONS.matcher(urlString).matches()
|
||||
&& urlString.startsWith("https://www.baeldung.com/");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(Page page) {
|
||||
String url = page.getWebURL().getURL();
|
||||
stats.incrementProcessedPageCount();
|
||||
|
||||
if (page.getParseData() instanceof HtmlParseData) {
|
||||
HtmlParseData htmlParseData = (HtmlParseData) page.getParseData();
|
||||
String title = htmlParseData.getTitle();
|
||||
String text = htmlParseData.getText();
|
||||
String html = htmlParseData.getHtml();
|
||||
Set<WebURL> links = htmlParseData.getOutgoingUrls();
|
||||
stats.incrementTotalLinksCount(links.size());
|
||||
|
||||
System.out.printf("Page with title '%s' %n", title);
|
||||
System.out.printf(" Text length: %d %n", text.length());
|
||||
System.out.printf(" HTML length: %d %n", html.length());
|
||||
System.out.printf(" %d outbound links %n", links.size());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.crawler4j;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import edu.uci.ics.crawler4j.crawler.CrawlConfig;
|
||||
import edu.uci.ics.crawler4j.crawler.CrawlController;
|
||||
import edu.uci.ics.crawler4j.fetcher.PageFetcher;
|
||||
import edu.uci.ics.crawler4j.robotstxt.RobotstxtConfig;
|
||||
import edu.uci.ics.crawler4j.robotstxt.RobotstxtServer;
|
||||
|
||||
public class HtmlCrawlerController {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
File crawlStorage = new File("src/test/resources/crawler4j");
|
||||
CrawlConfig config = new CrawlConfig();
|
||||
config.setCrawlStorageFolder(crawlStorage.getAbsolutePath());
|
||||
config.setMaxDepthOfCrawling(2);
|
||||
|
||||
int numCrawlers = 12;
|
||||
|
||||
PageFetcher pageFetcher = new PageFetcher(config);
|
||||
RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
|
||||
RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher);
|
||||
CrawlController controller = new CrawlController(config, pageFetcher, robotstxtServer);
|
||||
|
||||
controller.addSeed("https://www.baeldung.com/");
|
||||
|
||||
CrawlerStatistics stats = new CrawlerStatistics();
|
||||
CrawlController.WebCrawlerFactory<HtmlCrawler> factory = () -> new HtmlCrawler(stats);
|
||||
|
||||
controller.start(factory, numCrawlers);
|
||||
System.out.printf("Crawled %d pages %n", stats.getProcessedPageCount());
|
||||
System.out.printf("Total Number of outbound links = %d %n", stats.getTotalLinksCount());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package com.baeldung.crawler4j;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import edu.uci.ics.crawler4j.crawler.Page;
|
||||
import edu.uci.ics.crawler4j.crawler.WebCrawler;
|
||||
import edu.uci.ics.crawler4j.parser.BinaryParseData;
|
||||
import edu.uci.ics.crawler4j.url.WebURL;
|
||||
|
||||
public class ImageCrawler extends WebCrawler {
|
||||
private final static Pattern EXCLUSIONS = Pattern.compile(".*(\\.(css|js|xml|gif|png|mp3|mp4|zip|gz|pdf))$");
|
||||
|
||||
private static final Pattern IMG_PATTERNS = Pattern.compile(".*(\\.(jpg|jpeg))$");
|
||||
|
||||
private File saveDir;
|
||||
|
||||
public ImageCrawler(File saveDir) {
|
||||
this.saveDir = saveDir;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldVisit(Page referringPage, WebURL url) {
|
||||
String urlString = url.getURL().toLowerCase();
|
||||
if (EXCLUSIONS.matcher(urlString).matches()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (IMG_PATTERNS.matcher(urlString).matches()
|
||||
|| urlString.startsWith("https://www.baeldung.com/")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(Page page) {
|
||||
String url = page.getWebURL().getURL();
|
||||
if (IMG_PATTERNS.matcher(url).matches()
|
||||
&& page.getParseData() instanceof BinaryParseData) {
|
||||
String extension = url.substring(url.lastIndexOf("."));
|
||||
int contentLength = page.getContentData().length;
|
||||
|
||||
System.out.printf("Extension is '%s' with content length %d %n", extension, contentLength);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.crawler4j;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import edu.uci.ics.crawler4j.crawler.CrawlConfig;
|
||||
import edu.uci.ics.crawler4j.crawler.CrawlController;
|
||||
import edu.uci.ics.crawler4j.fetcher.PageFetcher;
|
||||
import edu.uci.ics.crawler4j.robotstxt.RobotstxtConfig;
|
||||
import edu.uci.ics.crawler4j.robotstxt.RobotstxtServer;
|
||||
|
||||
public class ImageCrawlerController {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
File crawlStorage = new File("src/test/resources/crawler4j");
|
||||
CrawlConfig config = new CrawlConfig();
|
||||
config.setCrawlStorageFolder(crawlStorage.getAbsolutePath());
|
||||
config.setIncludeBinaryContentInCrawling(true);
|
||||
config.setMaxPagesToFetch(500);
|
||||
|
||||
File saveDir = new File("src/test/resources/crawler4j");
|
||||
|
||||
int numCrawlers = 12;
|
||||
|
||||
PageFetcher pageFetcher = new PageFetcher(config);
|
||||
RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
|
||||
RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher);
|
||||
CrawlController controller = new CrawlController(config, pageFetcher, robotstxtServer);
|
||||
|
||||
controller.addSeed("https://www.baeldung.com/");
|
||||
|
||||
CrawlController.WebCrawlerFactory<ImageCrawler> factory = () -> new ImageCrawler(saveDir);
|
||||
|
||||
controller.start(factory, numCrawlers);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.crawler4j;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import edu.uci.ics.crawler4j.crawler.CrawlConfig;
|
||||
import edu.uci.ics.crawler4j.crawler.CrawlController;
|
||||
import edu.uci.ics.crawler4j.fetcher.PageFetcher;
|
||||
import edu.uci.ics.crawler4j.robotstxt.RobotstxtConfig;
|
||||
import edu.uci.ics.crawler4j.robotstxt.RobotstxtServer;
|
||||
|
||||
public class MultipleCrawlerController {
|
||||
public static void main(String[] args) throws Exception {
|
||||
File crawlStorageBase = new File("src/test/resources/crawler4j");
|
||||
CrawlConfig htmlConfig = new CrawlConfig();
|
||||
CrawlConfig imageConfig = new CrawlConfig();
|
||||
|
||||
htmlConfig.setCrawlStorageFolder(new File(crawlStorageBase, "html").getAbsolutePath());
|
||||
imageConfig.setCrawlStorageFolder(new File(crawlStorageBase, "image").getAbsolutePath());
|
||||
imageConfig.setIncludeBinaryContentInCrawling(true);
|
||||
|
||||
htmlConfig.setMaxPagesToFetch(500);
|
||||
imageConfig.setMaxPagesToFetch(1000);
|
||||
|
||||
PageFetcher pageFetcherHtml = new PageFetcher(htmlConfig);
|
||||
PageFetcher pageFetcherImage = new PageFetcher(imageConfig);
|
||||
|
||||
RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
|
||||
RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcherHtml);
|
||||
|
||||
CrawlController htmlController = new CrawlController(htmlConfig, pageFetcherHtml, robotstxtServer);
|
||||
CrawlController imageController = new CrawlController(imageConfig, pageFetcherImage, robotstxtServer);
|
||||
|
||||
htmlController.addSeed("https://www.baeldung.com/");
|
||||
imageController.addSeed("https://www.baeldung.com/");
|
||||
|
||||
CrawlerStatistics stats = new CrawlerStatistics();
|
||||
CrawlController.WebCrawlerFactory<HtmlCrawler> htmlFactory = () -> new HtmlCrawler(stats);
|
||||
|
||||
File saveDir = new File("src/test/resources/crawler4j");
|
||||
CrawlController.WebCrawlerFactory<ImageCrawler> imageFactory = () -> new ImageCrawler(saveDir);
|
||||
|
||||
imageController.startNonBlocking(imageFactory, 7);
|
||||
htmlController.startNonBlocking(htmlFactory, 10);
|
||||
|
||||
|
||||
htmlController.waitUntilFinish();
|
||||
System.out.printf("Crawled %d pages %n", stats.getProcessedPageCount());
|
||||
System.out.printf("Total Number of outbound links = %d %n", stats.getTotalLinksCount());
|
||||
|
||||
imageController.waitUntilFinish();
|
||||
System.out.printf("Image Crawler is finished.");
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,102 @@
|
|||
package com.baeldung.okhttp;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.gson.Gson;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.ResponseBody;
|
||||
import okhttp3.mockwebserver.MockResponse;
|
||||
import okhttp3.mockwebserver.MockWebServer;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
public class ResponseDecoderUnitTest {
|
||||
|
||||
@Rule public ExpectedException exceptionRule = ExpectedException.none();
|
||||
|
||||
@Rule public MockWebServer server = new MockWebServer();
|
||||
|
||||
SimpleEntity sampleResponse;
|
||||
|
||||
MockResponse mockResponse;
|
||||
|
||||
OkHttpClient client;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
sampleResponse = new SimpleEntity("Baeldung");
|
||||
client = new OkHttpClient.Builder().build();
|
||||
mockResponse = new MockResponse()
|
||||
.setResponseCode(200)
|
||||
.setHeader("Content-Type", "application/json")
|
||||
.setBody(new Gson().toJson(sampleResponse));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJacksonDecoder_whenGetStringOfResponse_thenExpectSimpleEntity() throws Exception {
|
||||
server.enqueue(mockResponse);
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url(""))
|
||||
.build();
|
||||
ResponseBody responseBody = client
|
||||
.newCall(request)
|
||||
.execute()
|
||||
.body();
|
||||
|
||||
Assert.assertNotNull(responseBody);
|
||||
Assert.assertNotEquals(0, responseBody.contentLength());
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
SimpleEntity entity = objectMapper.readValue(responseBody.string(), SimpleEntity.class);
|
||||
|
||||
Assert.assertNotNull(entity);
|
||||
Assert.assertEquals(sampleResponse.getName(), entity.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGsonDecoder_whenGetByteStreamOfResponse_thenExpectSimpleEntity() throws Exception {
|
||||
server.enqueue(mockResponse);
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url(""))
|
||||
.build();
|
||||
ResponseBody responseBody = client
|
||||
.newCall(request)
|
||||
.execute()
|
||||
.body();
|
||||
|
||||
Assert.assertNotNull(responseBody);
|
||||
Assert.assertNotEquals(0, responseBody.contentLength());
|
||||
|
||||
Gson gson = new Gson();
|
||||
SimpleEntity entity = gson.fromJson(new InputStreamReader(responseBody.byteStream()), SimpleEntity.class);
|
||||
|
||||
Assert.assertNotNull(entity);
|
||||
Assert.assertEquals(sampleResponse.getName(), entity.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGsonDecoder_whenGetStringOfResponse_thenExpectSimpleEntity() throws Exception {
|
||||
server.enqueue(mockResponse);
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url(""))
|
||||
.build();
|
||||
ResponseBody responseBody = client
|
||||
.newCall(request)
|
||||
.execute()
|
||||
.body();
|
||||
|
||||
Assert.assertNotNull(responseBody);
|
||||
|
||||
Gson gson = new Gson();
|
||||
SimpleEntity entity = gson.fromJson(responseBody.string(), SimpleEntity.class);
|
||||
|
||||
Assert.assertNotNull(entity);
|
||||
Assert.assertEquals(sampleResponse.getName(), entity.getName());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.okhttp;
|
||||
|
||||
public class SimpleEntity {
|
||||
protected String name;
|
||||
|
||||
public SimpleEntity(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
//no-arg constructor, getters and setters here
|
||||
|
||||
public SimpleEntity() {
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -263,6 +263,16 @@
|
|||
<groupId>org.apache.storm</groupId>
|
||||
<artifactId>storm-core</artifactId>
|
||||
<version>${storm.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>log4j-over-slf4j</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
|
|
@ -26,6 +26,7 @@ import java.util.ArrayList;
|
|||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class BackupCreatorIntegrationTest {
|
||||
|
@ -88,7 +89,7 @@ public class BackupCreatorIntegrationTest {
|
|||
SerializationSchema<Backup> serializationSchema = new BackupSerializationSchema();
|
||||
byte[] backupProcessed = serializationSchema.serialize(backup);
|
||||
|
||||
assertEquals(backupSerialized, backupProcessed);
|
||||
assertArrayEquals(backupSerialized, backupProcessed);
|
||||
}
|
||||
|
||||
private static class CollectingSink implements SinkFunction<Backup> {
|
||||
|
|
|
@ -59,5 +59,6 @@ class FtpClient {
|
|||
void downloadFile(String source, String destination) throws IOException {
|
||||
FileOutputStream out = new FileOutputStream(destination);
|
||||
ftp.retrieveFile(source, out);
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ public class AdderMethodDirtiesContextIntegrationTest {
|
|||
@Test
|
||||
public void _1_givenNumber_whenAdd_thenSumWrong() {
|
||||
adderServiceSteps.whenAdd();
|
||||
adderServiceSteps.sumWrong();
|
||||
adderServiceSteps.summedUp();
|
||||
}
|
||||
|
||||
@Rule
|
||||
|
|
|
@ -78,7 +78,7 @@
|
|||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>integration</id>
|
||||
<id>integration-lite-first</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
|
|
@ -23,7 +23,7 @@ public class MapAppenderIntegrationTest {
|
|||
|
||||
@Test
|
||||
public void whenLoggerEmitsLoggingEvent_thenAppenderReceivesEvent() throws Exception {
|
||||
logger.info("Test from {}", this.getClass()
|
||||
logger.error("Error log message from {}", this.getClass()
|
||||
.getSimpleName());
|
||||
LoggerContext context = LoggerContext.getContext(false);
|
||||
Configuration config = context.getConfiguration();
|
||||
|
|
|
@ -12,7 +12,7 @@ import java.util.List;
|
|||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ArticleWithAuthorDAOIntegrationTest {
|
||||
public class ArticleWithAuthorDAOLiveTest {
|
||||
private Connection connection;
|
||||
|
||||
private ArticleWithAuthorDAO articleWithAuthorDAO;
|
|
@ -5,9 +5,9 @@
|
|||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<artifactId>persistence-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
<relativePath>..</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>hibernate-mapping</artifactId>
|
||||
|
@ -31,22 +31,28 @@
|
|||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
</dependency>
|
||||
<!-- validation -->
|
||||
<!-- validation -->
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>${hibernate-validator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.el</groupId>
|
||||
<artifactId>javax.el-api</artifactId>
|
||||
<version>${javax.el-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish</groupId>
|
||||
<artifactId>javax.el</artifactId>
|
||||
<version>${org.glassfish.javax.el.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.money</groupId>
|
||||
<artifactId>money-api</artifactId>
|
||||
<version>1.0.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.javamoney</groupId>
|
||||
<artifactId>moneta</artifactId>
|
||||
<version>1.3</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -60,11 +66,10 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<hibernate.version>5.3.7.Final</hibernate.version>
|
||||
<hibernate.version>5.3.10.Final</hibernate.version>
|
||||
<assertj-core.version>3.8.0</assertj-core.version>
|
||||
<hibernate-validator.version>5.3.3.Final</hibernate-validator.version>
|
||||
<javax.el-api.version>2.2.5</javax.el-api.version>
|
||||
<org.glassfish.javax.el.version>3.0.1-b08</org.glassfish.javax.el.version>
|
||||
<hibernate-validator.version>6.0.16.Final</hibernate-validator.version>
|
||||
<org.glassfish.javax.el.version>3.0.1-b11</org.glassfish.javax.el.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
|
|
@ -4,8 +4,11 @@ import javax.persistence.Column;
|
|||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.validation.constraints.Size;
|
||||
import javax.money.MonetaryAmount;
|
||||
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
import org.hibernate.validator.constraints.CreditCardNumber;
|
||||
import org.hibernate.validator.constraints.Currency;
|
||||
|
||||
@Entity
|
||||
public class User {
|
||||
|
@ -62,5 +65,4 @@ public class User {
|
|||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,290 @@
|
|||
package com.baeldung.hibernate.validation;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Duration;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.money.CurrencyContextBuilder;
|
||||
import javax.money.Monetary;
|
||||
import javax.money.MonetaryAmount;
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.ValidatorFactory;
|
||||
|
||||
import org.hibernate.validator.constraints.CodePointLength;
|
||||
import org.hibernate.validator.constraints.CreditCardNumber;
|
||||
import org.hibernate.validator.constraints.Currency;
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
import org.hibernate.validator.constraints.LuhnCheck;
|
||||
import org.hibernate.validator.constraints.Range;
|
||||
import org.hibernate.validator.constraints.SafeHtml;
|
||||
import org.hibernate.validator.constraints.ScriptAssert;
|
||||
import org.hibernate.validator.constraints.URL;
|
||||
import org.hibernate.validator.constraints.time.DurationMax;
|
||||
import org.hibernate.validator.constraints.time.DurationMin;
|
||||
import org.javamoney.moneta.CurrencyUnitBuilder;
|
||||
import org.javamoney.moneta.Money;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UserAdditionalValidationUnitTest {
|
||||
|
||||
private static Validator validator;
|
||||
private Set<ConstraintViolation<AdditionalValidations>> constraintViolations;
|
||||
|
||||
@BeforeClass
|
||||
public static void before() {
|
||||
ValidatorFactory config = Validation.buildDefaultValidatorFactory();
|
||||
validator = config.getValidator();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidationWithCCNAndNullCCN_thenNoConstraintViolation() {
|
||||
AdditionalValidations validations = new AdditionalValidations();
|
||||
constraintViolations = validator.validateProperty(validations, "creditCardNumber");
|
||||
assertTrue(constraintViolations.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidationWithCCNAndValidCCN_thenNoConstraintViolation() {
|
||||
AdditionalValidations validations = new AdditionalValidations();
|
||||
validations.setCreditCardNumber("79927398713");
|
||||
constraintViolations = validator.validateProperty(validations, "creditCardNumber");
|
||||
assertTrue(constraintViolations.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidationWithCCNAndInvalidCCN_thenConstraintViolation() {
|
||||
AdditionalValidations validations = new AdditionalValidations();
|
||||
validations.setCreditCardNumber("79927398714");
|
||||
constraintViolations = validator.validateProperty(validations, "creditCardNumber");
|
||||
assertEquals(constraintViolations.size(), 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidationWithCCNAndValidCCNWithDashes_thenConstraintViolation() {
|
||||
AdditionalValidations validations = new AdditionalValidations();
|
||||
validations.setCreditCardNumber("7992-7398-713");
|
||||
constraintViolations = validator.validateProperty(validations, "creditCardNumber");
|
||||
assertEquals(1, constraintViolations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidationWithLenientCCNAndValidCCNWithDashes_thenNoConstraintViolation() {
|
||||
AdditionalValidations validations = new AdditionalValidations();
|
||||
validations.setLenientCreditCardNumber("7992-7398-713");
|
||||
constraintViolations = validator.validateProperty(validations, "lenientCreditCardNumber");
|
||||
assertTrue(constraintViolations.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMonetaryAmountWithRightCurrency_thenNoConstraintViolation() {
|
||||
AdditionalValidations bean = new AdditionalValidations();
|
||||
bean.setBalance(Money.of(new BigDecimal(100.0), Monetary.getCurrency("EUR")));
|
||||
constraintViolations = validator.validateProperty(bean, "balance");
|
||||
assertEquals(0, constraintViolations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMonetaryAmountWithWrongCurrency_thenConstraintViolation() {
|
||||
AdditionalValidations validations = new AdditionalValidations();
|
||||
validations.setBalance(Money.of(new BigDecimal(100.0), Monetary.getCurrency("USD")));
|
||||
constraintViolations = validator.validateProperty(validations, "balance");
|
||||
assertEquals(1, constraintViolations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDurationShorterThanMin_thenConstraintViolation() {
|
||||
AdditionalValidations bean = new AdditionalValidations();
|
||||
bean.setDuration(Duration.ofDays(1).plusHours(1));
|
||||
constraintViolations = validator.validateProperty(bean, "duration");
|
||||
assertEquals(1, constraintViolations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDurationLongerThanMax_thenConstraintViolation() {
|
||||
AdditionalValidations bean = new AdditionalValidations();
|
||||
bean.setDuration(Duration.ofDays(2).plusHours(3));
|
||||
constraintViolations = validator.validateProperty(bean, "duration");
|
||||
assertEquals(1, constraintViolations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDurationBetweenMinAndMax_thenNoConstraintViolation() {
|
||||
AdditionalValidations bean = new AdditionalValidations();
|
||||
bean.setDuration(Duration.ofDays(2));
|
||||
constraintViolations = validator.validateProperty(bean, "duration");
|
||||
assertEquals(0, constraintViolations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValueBelowRangeMin_thenConstraintViolation() {
|
||||
AdditionalValidations bean = new AdditionalValidations();
|
||||
bean.setPercent(new BigDecimal("-1.4"));
|
||||
constraintViolations = validator.validateProperty(bean, "percent");
|
||||
assertEquals(1, constraintViolations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValueAboveRangeMax_thenConstraintViolation() {
|
||||
AdditionalValidations bean = new AdditionalValidations();
|
||||
bean.setPercent(new BigDecimal("100.03"));
|
||||
constraintViolations = validator.validateProperty(bean, "percent");
|
||||
assertEquals(1, constraintViolations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValueInRange_thenNoConstraintViolation() {
|
||||
AdditionalValidations bean = new AdditionalValidations();
|
||||
bean.setPercent(new BigDecimal("53.23"));
|
||||
constraintViolations = validator.validateProperty(bean, "percent");
|
||||
assertEquals(0, constraintViolations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLengthInRange_thenNoConstraintViolation() {
|
||||
AdditionalValidations bean = new AdditionalValidations();
|
||||
bean.setSomeString("aaa");
|
||||
constraintViolations = validator.validateProperty(bean, "someString");
|
||||
assertEquals(0, constraintViolations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCodePointLengthNotInRange_thenConstraintViolation() {
|
||||
AdditionalValidations bean = new AdditionalValidations();
|
||||
bean.setSomeString("aa\uD835\uDD0A");
|
||||
constraintViolations = validator.validateProperty(bean, "someString");
|
||||
assertEquals(1, constraintViolations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidUrlWithWrongProtocol_thenConstraintViolation() {
|
||||
AdditionalValidations bean = new AdditionalValidations();
|
||||
|
||||
bean.setUrl("https://www.google.com/");
|
||||
constraintViolations = validator.validateProperty(bean, "url");
|
||||
assertEquals(0, constraintViolations.size());
|
||||
|
||||
bean.setUrl("http://www.google.com/");
|
||||
constraintViolations = validator.validateProperty(bean, "url");
|
||||
assertEquals(1, constraintViolations.size());
|
||||
|
||||
bean.setUrl("https://foo:bar");
|
||||
constraintViolations = validator.validateProperty(bean, "url");
|
||||
assertEquals(1, constraintViolations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenScriptAssertFails_thenConstraintViolation() {
|
||||
AdditionalValidations bean = new AdditionalValidations();
|
||||
|
||||
constraintViolations = validator.validate(bean);
|
||||
assertEquals(0, constraintViolations.size());
|
||||
|
||||
bean.setValid(false);
|
||||
|
||||
constraintViolations = validator.validate(bean);
|
||||
assertEquals(1, constraintViolations.size());
|
||||
|
||||
constraintViolations = validator.validateProperty(bean, "valid");
|
||||
assertEquals(0, constraintViolations.size());
|
||||
}
|
||||
|
||||
@ScriptAssert(lang = "nashorn", script = "_this.valid")
|
||||
public class AdditionalValidations {
|
||||
private boolean valid = true;
|
||||
|
||||
@CreditCardNumber
|
||||
@LuhnCheck(startIndex = 0, endIndex = Integer.MAX_VALUE, checkDigitIndex = -1)
|
||||
private String creditCardNumber;
|
||||
|
||||
@CreditCardNumber(ignoreNonDigitCharacters = true)
|
||||
private String lenientCreditCardNumber;
|
||||
|
||||
@Currency("EUR")
|
||||
private MonetaryAmount balance;
|
||||
|
||||
@DurationMin(days = 1, hours = 2)
|
||||
@DurationMax(days = 2, hours = 1)
|
||||
private Duration duration;
|
||||
|
||||
@Range(min = 0, max = 100)
|
||||
private BigDecimal percent;
|
||||
|
||||
@Length(min = 1, max = 3)
|
||||
@CodePointLength(min = 1, max = 3)
|
||||
private String someString;
|
||||
|
||||
@URL(protocol = "https")
|
||||
private String url;
|
||||
|
||||
public String getCreditCardNumber() {
|
||||
return creditCardNumber;
|
||||
}
|
||||
|
||||
public void setCreditCardNumber(String creditCardNumber) {
|
||||
this.creditCardNumber = creditCardNumber;
|
||||
}
|
||||
|
||||
public String getLenientCreditCardNumber() {
|
||||
return lenientCreditCardNumber;
|
||||
}
|
||||
|
||||
public void setLenientCreditCardNumber(String lenientCreditCardNumber) {
|
||||
this.lenientCreditCardNumber = lenientCreditCardNumber;
|
||||
}
|
||||
|
||||
public MonetaryAmount getBalance() {
|
||||
return balance;
|
||||
}
|
||||
|
||||
public void setBalance(MonetaryAmount balance) {
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
public Duration getDuration() {
|
||||
return duration;
|
||||
}
|
||||
|
||||
public void setDuration(Duration duration) {
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
public BigDecimal getPercent() {
|
||||
return percent;
|
||||
}
|
||||
|
||||
public void setPercent(BigDecimal percent) {
|
||||
this.percent = percent;
|
||||
}
|
||||
|
||||
public String getSomeString() {
|
||||
return someString;
|
||||
}
|
||||
|
||||
public void setSomeString(String someString) {
|
||||
this.someString = someString;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return valid;
|
||||
}
|
||||
|
||||
public void setValid(boolean valid) {
|
||||
this.valid = valid;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -39,6 +39,7 @@
|
|||
<!-- Cassandra -->
|
||||
<cassandra-driver-core.version>3.1.2</cassandra-driver-core.version>
|
||||
<cassandra-unit.version>3.1.1.0</cassandra-unit.version>
|
||||
<guava.version>18.0</guava.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -8,6 +8,8 @@
|
|||
- [Converting Between LocalDate and SQL Date](https://www.baeldung.com/java-convert-localdate-sql-date)
|
||||
- [Combining JPA And/Or Criteria Predicates](https://www.baeldung.com/jpa-and-or-criteria-predicates)
|
||||
- [Types of JPA Queries](https://www.baeldung.com/jpa-queries)
|
||||
- [JPA/Hibernate Projections](https://www.baeldung.com/jpa-hibernate-projections)
|
||||
- [Composite Primary Keys in JPA](https://www.baeldung.com/jpa-composite-primary-keys)
|
||||
- [Defining JPA Entities](https://www.baeldung.com/jpa-entities)
|
||||
- [JPA @Basic Annotation](https://www.baeldung.com/jpa-basic-annotation)
|
||||
- [Default Column Values in JPA](https://www.baeldung.com/jpa-default-column-values)
|
||||
|
|
|
@ -1,7 +1,3 @@
|
|||
=========
|
||||
|
||||
## Spring Data JPA Example Project
|
||||
|
||||
### Relevant Articles:
|
||||
- [Spring Data JPA – Derived Delete Methods](https://www.baeldung.com/spring-data-jpa-deleteby)
|
||||
- [JPA Join Types](https://www.baeldung.com/jpa-join-types)
|
||||
|
@ -15,3 +11,4 @@
|
|||
- [Spring Data JPA and Named Entity Graphs](https://www.baeldung.com/spring-data-jpa-named-entity-graphs)
|
||||
- [Batch Insert/Update with Hibernate/JPA](https://www.baeldung.com/jpa-hibernate-batch-insert-update)
|
||||
- [Difference Between save() and saveAndFlush() in Spring Data JPA](https://www.baeldung.com/spring-data-jpa-save-saveandflush)
|
||||
- [Derived Query Methods in Spring Data JPA Repositories](https://www.baeldung.com/spring-data-derived-queries)
|
||||
|
|
|
@ -79,6 +79,21 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<verbose>true</verbose>
|
||||
<fork>true</fork>
|
||||
<argLine>-Xmx1024m</argLine>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<cglib.version>3.2.4</cglib.version>
|
||||
<jedis.version>2.9.0</jedis.version>
|
||||
|
|
|
@ -2,6 +2,9 @@ package com.baeldung.spring.data.reactive.redis.template;
|
|||
|
||||
|
||||
import com.baeldung.spring.data.reactive.redis.SpringRedisReactiveApplication;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -9,22 +12,40 @@ import org.springframework.boot.test.context.SpringBootTest;
|
|||
import org.springframework.data.redis.connection.ReactiveKeyCommands;
|
||||
import org.springframework.data.redis.connection.ReactiveStringCommands;
|
||||
import org.springframework.data.redis.connection.ReactiveStringCommands.SetCommand;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.annotation.DirtiesContext.ClassMode;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
import redis.embedded.RedisServerBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringRedisReactiveApplication.class)
|
||||
@DirtiesContext(classMode = ClassMode.BEFORE_CLASS)
|
||||
public class RedisKeyCommandsIntegrationTest {
|
||||
|
||||
private static redis.embedded.RedisServer redisServer;
|
||||
|
||||
@Autowired
|
||||
private ReactiveKeyCommands keyCommands;
|
||||
|
||||
@Autowired
|
||||
private ReactiveStringCommands stringCommands;
|
||||
|
||||
@BeforeClass
|
||||
public static void startRedisServer() throws IOException {
|
||||
redisServer = new RedisServerBuilder().port(6379).setting("maxheap 256M").build();
|
||||
redisServer.start();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void stopRedisServer() throws IOException {
|
||||
redisServer.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFluxOfKeys_whenPerformOperations_thenPerformOperations() {
|
||||
|
|
|
@ -2,27 +2,48 @@ package com.baeldung.spring.data.reactive.redis.template;
|
|||
|
||||
|
||||
import com.baeldung.spring.data.reactive.redis.SpringRedisReactiveApplication;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.redis.core.ReactiveListOperations;
|
||||
import org.springframework.data.redis.core.ReactiveRedisTemplate;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.annotation.DirtiesContext.ClassMode;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
import redis.embedded.RedisServerBuilder;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringRedisReactiveApplication.class)
|
||||
@DirtiesContext(classMode = ClassMode.BEFORE_CLASS)
|
||||
public class RedisTemplateListOpsIntegrationTest {
|
||||
|
||||
private static final String LIST_NAME = "demo_list";
|
||||
private static redis.embedded.RedisServer redisServer;
|
||||
|
||||
@Autowired
|
||||
private ReactiveRedisTemplate<String, String> redisTemplate;
|
||||
|
||||
private ReactiveListOperations<String, String> reactiveListOps;
|
||||
|
||||
@BeforeClass
|
||||
public static void startRedisServer() throws IOException {
|
||||
redisServer = new RedisServerBuilder().port(6379).setting("maxheap 128M").build();
|
||||
redisServer.start();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void stopRedisServer() throws IOException {
|
||||
redisServer.stop();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
|
|
|
@ -1,29 +1,51 @@
|
|||
package com.baeldung.spring.data.reactive.redis.template;
|
||||
|
||||
|
||||
import com.baeldung.spring.data.reactive.redis.SpringRedisReactiveApplication;
|
||||
import com.baeldung.spring.data.reactive.redis.model.Employee;
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.redis.core.ReactiveRedisTemplate;
|
||||
import org.springframework.data.redis.core.ReactiveValueOperations;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.annotation.DirtiesContext.ClassMode;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.spring.data.reactive.redis.SpringRedisReactiveApplication;
|
||||
import com.baeldung.spring.data.reactive.redis.model.Employee;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.time.Duration;
|
||||
import redis.embedded.RedisServerBuilder;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringRedisReactiveApplication.class)
|
||||
@DirtiesContext(classMode = ClassMode.BEFORE_CLASS)
|
||||
public class RedisTemplateValueOpsIntegrationTest {
|
||||
|
||||
private static redis.embedded.RedisServer redisServer;
|
||||
|
||||
@Autowired
|
||||
private ReactiveRedisTemplate<String, Employee> redisTemplate;
|
||||
|
||||
private ReactiveValueOperations<String, Employee> reactiveValueOps;
|
||||
|
||||
@BeforeClass
|
||||
public static void startRedisServer() throws IOException {
|
||||
redisServer = new RedisServerBuilder().port(6379).setting("maxheap 256M").build();
|
||||
redisServer.start();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void stopRedisServer() throws IOException {
|
||||
redisServer.stop();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
|
|
|
@ -19,9 +19,11 @@ import com.baeldung.spring.data.redis.config.RedisConfig;
|
|||
import com.baeldung.spring.data.redis.queue.RedisMessagePublisher;
|
||||
import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber;
|
||||
|
||||
import redis.embedded.RedisServerBuilder;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = RedisConfig.class)
|
||||
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
|
||||
@DirtiesContext(classMode = ClassMode.BEFORE_CLASS)
|
||||
public class RedisMessageListenerIntegrationTest {
|
||||
|
||||
private static redis.embedded.RedisServer redisServer;
|
||||
|
@ -31,7 +33,7 @@ public class RedisMessageListenerIntegrationTest {
|
|||
|
||||
@BeforeClass
|
||||
public static void startRedisServer() throws IOException {
|
||||
redisServer = new redis.embedded.RedisServer(6380);
|
||||
redisServer = new RedisServerBuilder().port(6379).setting("maxheap 256M").build();
|
||||
redisServer.start();
|
||||
}
|
||||
|
||||
|
|
|
@ -20,9 +20,11 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|||
import com.baeldung.spring.data.redis.config.RedisConfig;
|
||||
import com.baeldung.spring.data.redis.model.Student;
|
||||
|
||||
import redis.embedded.RedisServerBuilder;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = RedisConfig.class)
|
||||
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
|
||||
@DirtiesContext(classMode = ClassMode.BEFORE_CLASS)
|
||||
public class StudentRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
|
@ -32,7 +34,7 @@ public class StudentRepositoryIntegrationTest {
|
|||
|
||||
@BeforeClass
|
||||
public static void startRedisServer() throws IOException {
|
||||
redisServer = new redis.embedded.RedisServer(6380);
|
||||
redisServer = new RedisServerBuilder().port(6379).setting("maxheap 128M").build();
|
||||
redisServer.start();
|
||||
}
|
||||
|
||||
|
|
3
pom.xml
3
pom.xml
|
@ -646,6 +646,7 @@
|
|||
<module>spring-boot-ops-2</module>
|
||||
<module>spring-boot-rest</module>
|
||||
<module>spring-boot-data</module>
|
||||
<module>spring-boot-parent</module>
|
||||
<module>spring-boot-property-exp</module>
|
||||
<module>spring-boot-security</module>
|
||||
<module>spring-boot-testing</module>
|
||||
|
@ -926,6 +927,7 @@
|
|||
<module>spring-vault</module>
|
||||
<module>spring-vertx</module>
|
||||
<module>spring-zuul/spring-zuul-foos-resource</module>
|
||||
<module>persistence-modules/hibernate-mapping</module>
|
||||
<module>persistence-modules/spring-data-dynamodb</module>
|
||||
<module>persistence-modules/spring-data-eclipselink</module>
|
||||
<module>persistence-modules/spring-data-solr</module>
|
||||
|
@ -1310,6 +1312,7 @@
|
|||
<module>spring-boot-ops-2</module>
|
||||
<module>spring-boot-rest</module>
|
||||
<module>spring-boot-data</module>
|
||||
<module>spring-boot-parent</module>
|
||||
<module>spring-boot-property-exp</module>
|
||||
<module>spring-boot-security</module>
|
||||
<module>spring-boot-vue</module>
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.spf4j</groupId>
|
||||
<artifactId>spf4j</artifactId>
|
||||
<name>spf4j</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>spf4j-core-app</module>
|
||||
<module>spf4j-aspects-app</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,83 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spf4j-aspects-app</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>spf4j-aspects-app</name>
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.spf4j</groupId>
|
||||
<artifactId>spf4j-aspects</artifactId>
|
||||
<version>${spf4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.spf4j</groupId>
|
||||
<artifactId>spf4j-ui</artifactId>
|
||||
<version>${spf4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<finalName>spf4j-aspects-app</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<version>3.1.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>
|
||||
${project.build.directory}/dependency-jars/
|
||||
</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>com.baeldung.spf4j.aspects.App</mainClass>
|
||||
<addClasspath>true</addClasspath>
|
||||
<classpathPrefix>dependency-jars/</classpathPrefix>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<spf4j.version>8.6.10</spf4j.version>
|
||||
<org.slf4j.version>1.7.21</org.slf4j.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.spf4j.aspects;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.spf4j.annotations.PerformanceMonitor;
|
||||
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Spf4jConfig.initialize();
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
long numberToCheck = random.nextInt(999_999_999 - 100_000_000 + 1) + 100_000_000;
|
||||
isPrimeNumber(numberToCheck);
|
||||
}
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
@PerformanceMonitor(warnThresholdMillis = 1, errorThresholdMillis = 100, recorderSource = Spf4jConfig.RecorderSourceForIsPrimeNumber.class)
|
||||
public static boolean isPrimeNumber(long number) {
|
||||
for (long i = 2; i <= number / 2; i++) {
|
||||
if (number % i == 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung.spf4j.aspects;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.spf4j.annotations.RecorderSourceInstance;
|
||||
import org.spf4j.perf.MeasurementRecorderSource;
|
||||
import org.spf4j.perf.impl.RecorderFactory;
|
||||
|
||||
public class Spf4jConfig {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
public static void initialize() {
|
||||
String tsDbFile = System.getProperty("user.dir") + File.separator + "spf4j-performance-monitoring.tsdb2";
|
||||
String tsTextFile = System.getProperty("user.dir") + File.separator + "spf4j-performance-monitoring.txt";
|
||||
|
||||
LOGGER.info("\nTime Series DB (TSDB) : {}\nTime Series text file : {}", tsDbFile, tsTextFile);
|
||||
System.setProperty("spf4j.perf.ms.config", "TSDB@" + tsDbFile + "," + "TSDB_TXT@" + tsTextFile);
|
||||
}
|
||||
|
||||
public static final class RecorderSourceForIsPrimeNumber extends RecorderSourceInstance {
|
||||
public static final MeasurementRecorderSource INSTANCE;
|
||||
|
||||
static {
|
||||
Object forWhat = App.class + " isPrimeNumber";
|
||||
String unitOfMeasurement = "ms";
|
||||
int sampleTimeMillis = 1_000;
|
||||
int factor = 10;
|
||||
int lowerMagnitude = 0;
|
||||
int higherMagnitude = 4;
|
||||
int quantasPerMagnitude = 10;
|
||||
INSTANCE = RecorderFactory.createScalableQuantizedRecorderSource(forWhat, unitOfMeasurement,
|
||||
sampleTimeMillis, factor, lowerMagnitude, higherMagnitude, quantasPerMagnitude);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
<aspectj>
|
||||
<aspects>
|
||||
<aspect
|
||||
name="org.spf4j.perf.aspects.PerformanceMonitorAspect" />
|
||||
</aspects>
|
||||
<weaver options="-verbose">
|
||||
<!-- make sure the classes you want to apply aspects to are included -->
|
||||
<include within="com..*" />
|
||||
<include
|
||||
within="org.spf4j.perf.aspects.PerformanceMonitorAspect" />
|
||||
</weaver>
|
||||
</aspectj>
|
|
@ -0,0 +1,11 @@
|
|||
<configuration>
|
||||
<appender name="STDOUT"
|
||||
class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>[%level] %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
<root level="debug">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -0,0 +1,83 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spf4j-core-app</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>spf4j-core-app</name>
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.spf4j</groupId>
|
||||
<artifactId>spf4j-core</artifactId>
|
||||
<version>${spf4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.spf4j</groupId>
|
||||
<artifactId>spf4j-ui</artifactId>
|
||||
<version>${spf4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<finalName>spf4j-core-app</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<version>3.1.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>
|
||||
${project.build.directory}/dependency-jars/
|
||||
</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>com.baeldung.spf4j.core.App</mainClass>
|
||||
<addClasspath>true</addClasspath>
|
||||
<classpathPrefix>dependency-jars/</classpathPrefix>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<spf4j.version>8.6.10</spf4j.version>
|
||||
<org.slf4j.version>1.7.21</org.slf4j.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.spf4j.core;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.spf4j.perf.MeasurementRecorder;
|
||||
|
||||
public class App {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Spf4jConfig.initialize();
|
||||
MeasurementRecorder measurementRecorder = Spf4jConfig.getMeasurementRecorder(App.class + " isPrimeNumber");
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
long numberToCheck = random.nextInt(999_999_999 - 100_000_000 + 1) + 100_000_000;
|
||||
long startTime = System.currentTimeMillis();
|
||||
boolean isPrime = isPrimeNumber(numberToCheck);
|
||||
measurementRecorder.record(System.currentTimeMillis() - startTime);
|
||||
LOGGER.info("{}. {} is prime? {}", i + 1, numberToCheck, isPrime);
|
||||
}
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
private static boolean isPrimeNumber(long number) {
|
||||
for (long i = 2; i <= number / 2; i++) {
|
||||
if (number % i == 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.spf4j.core;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.spf4j.perf.MeasurementRecorder;
|
||||
import org.spf4j.perf.impl.RecorderFactory;
|
||||
|
||||
public class Spf4jConfig {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
public static void initialize() {
|
||||
String tsDbFile = System.getProperty("user.dir") + File.separator + "spf4j-performance-monitoring.tsdb2";
|
||||
String tsTextFile = System.getProperty("user.dir") + File.separator + "spf4j-performance-monitoring.txt";
|
||||
|
||||
LOGGER.info("\nTime Series DB (TSDB) : {}\nTime Series text file : {}", tsDbFile, tsTextFile);
|
||||
System.setProperty("spf4j.perf.ms.config", "TSDB@" + tsDbFile + "," + "TSDB_TXT@" + tsTextFile);
|
||||
}
|
||||
|
||||
public static MeasurementRecorder getMeasurementRecorder(Object forWhat) {
|
||||
String unitOfMeasurement = "ms";
|
||||
int sampleTimeMillis = 1_000;
|
||||
int factor = 10;
|
||||
int lowerMagnitude = 0;
|
||||
int higherMagnitude = 4;
|
||||
int quantasPerMagnitude = 10;
|
||||
|
||||
return RecorderFactory.createScalableQuantizedRecorder(forWhat, unitOfMeasurement, sampleTimeMillis, factor, lowerMagnitude, higherMagnitude, quantasPerMagnitude);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<configuration>
|
||||
<appender name="STDOUT"
|
||||
class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>[%level] %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
<root level="debug">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -3,3 +3,4 @@
|
|||
- [Spring Boot Reactor Netty Configuration](https://www.baeldung.com/spring-boot-reactor-netty)
|
||||
- [How to Return 404 with Spring WebFlux](https://www.baeldung.com/spring-webflux-404)
|
||||
- [Spring WebClient Requests with Parameters](https://www.baeldung.com/webflux-webclient-parameters)
|
||||
- [RSocket Using Spring Boot](https://www.baeldung.com/spring-boot-rsocket)
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.autoconfiguration.service;
|
||||
|
||||
public class CustomService implements SimpleService {
|
||||
|
||||
@Override
|
||||
public String serve() {
|
||||
return "Custom Service";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.autoconfiguration.service;
|
||||
|
||||
public class DefaultService implements SimpleService {
|
||||
|
||||
@Override
|
||||
public String serve() {
|
||||
return "Default Service";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.autoconfiguration.service;
|
||||
|
||||
public interface SimpleService {
|
||||
|
||||
public String serve();
|
||||
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
package com.baeldung.autoconfiguration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
public class ConditionalOnBeanIntegrationTest {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
||||
|
||||
@Test
|
||||
public void whenDependentBeanIsPresent_thenConditionalBeanCreated() {
|
||||
this.contextRunner.withUserConfiguration(BasicConfiguration.class, ConditionalOnBeanConfiguration.class)
|
||||
.run((context) -> {
|
||||
assertThat(context).hasBean("created");
|
||||
assertThat(context).getBean("created")
|
||||
.isEqualTo("This is always created");
|
||||
assertThat(context).hasBean("createOnBean");
|
||||
assertThat(context).getBean("createOnBean")
|
||||
.isEqualTo("This is created when bean (name=created) is present");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDependentBeanIsPresent_thenConditionalMissingBeanIgnored() {
|
||||
this.contextRunner.withUserConfiguration(BasicConfiguration.class, ConditionalOnMissingBeanConfiguration.class)
|
||||
.run((context) -> {
|
||||
assertThat(context).hasBean("created");
|
||||
assertThat(context).getBean("created")
|
||||
.isEqualTo("This is always created");
|
||||
assertThat(context).doesNotHaveBean("createOnMissingBean");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDependentBeanIsNotPresent_thenConditionalMissingBeanCreated() {
|
||||
this.contextRunner.withUserConfiguration(ConditionalOnMissingBeanConfiguration.class)
|
||||
.run((context) -> {
|
||||
assertThat(context).hasBean("createOnMissingBean");
|
||||
assertThat(context).getBean("createOnMissingBean")
|
||||
.isEqualTo("This is created when bean (name=created) is missing");
|
||||
assertThat(context).doesNotHaveBean("created");
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class BasicConfiguration {
|
||||
@Bean
|
||||
public String created() {
|
||||
return "This is always created";
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnBean(name = "created")
|
||||
protected static class ConditionalOnBeanConfiguration {
|
||||
@Bean
|
||||
public String createOnBean() {
|
||||
return "This is created when bean (name=created) is present";
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnMissingBean(name = "created")
|
||||
protected static class ConditionalOnMissingBeanConfiguration {
|
||||
@Bean
|
||||
public String createOnMissingBean() {
|
||||
return "This is created when bean (name=created) is missing";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
package com.baeldung.autoconfiguration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
|
||||
import org.springframework.boot.test.context.FilteredClassLoader;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
public class ConditionalOnClassIntegrationTest {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
||||
|
||||
@Test
|
||||
public void whenDependentClassIsPresent_thenBeanCreated() {
|
||||
this.contextRunner.withUserConfiguration(ConditionalOnClassConfiguration.class)
|
||||
.run(context -> {
|
||||
assertThat(context).hasBean("created");
|
||||
assertThat(context.getBean("created")).isEqualTo("This is created when ConditionalOnClassIntegrationTest is present on the classpath");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDependentClassIsPresent_thenBeanMissing() {
|
||||
this.contextRunner.withUserConfiguration(ConditionalOnMissingClassConfiguration.class)
|
||||
.run(context -> {
|
||||
assertThat(context).doesNotHaveBean("missed");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDependentClassIsNotPresent_thenBeanMissing() {
|
||||
this.contextRunner.withUserConfiguration(ConditionalOnClassConfiguration.class)
|
||||
.withClassLoader(new FilteredClassLoader(ConditionalOnClassIntegrationTest.class))
|
||||
.run((context) -> {
|
||||
assertThat(context).doesNotHaveBean("created");
|
||||
assertThat(context).doesNotHaveBean(ConditionalOnClassIntegrationTest.class);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDependentClassIsNotPresent_thenBeanCreated() {
|
||||
this.contextRunner.withUserConfiguration(ConditionalOnMissingClassConfiguration.class)
|
||||
.withClassLoader(new FilteredClassLoader(ConditionalOnClassIntegrationTest.class))
|
||||
.run((context) -> {
|
||||
assertThat(context).hasBean("missed");
|
||||
assertThat(context).getBean("missed")
|
||||
.isEqualTo("This is missed when ConditionalOnClassIntegrationTest is present on the classpath");
|
||||
assertThat(context).doesNotHaveBean(ConditionalOnClassIntegrationTest.class);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnClass(ConditionalOnClassIntegrationTest.class)
|
||||
protected static class ConditionalOnClassConfiguration {
|
||||
@Bean
|
||||
public String created() {
|
||||
return "This is created when ConditionalOnClassIntegrationTest is present on the classpath";
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnMissingClass("com.baeldung.autoconfiguration.ConditionalOnClassIntegrationTest")
|
||||
protected static class ConditionalOnMissingClassConfiguration {
|
||||
@Bean
|
||||
public String missed() {
|
||||
return "This is missed when ConditionalOnClassIntegrationTest is present on the classpath";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
package com.baeldung.autoconfiguration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
|
||||
import com.baeldung.autoconfiguration.service.CustomService;
|
||||
import com.baeldung.autoconfiguration.service.DefaultService;
|
||||
import com.baeldung.autoconfiguration.service.SimpleService;
|
||||
|
||||
public class ConditionalOnPropertyIntegrationTest {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
||||
|
||||
@Test
|
||||
public void whenGivenCustomPropertyValue_thenCustomServiceCreated() {
|
||||
this.contextRunner.withPropertyValues("com.baeldung.service=custom")
|
||||
.withUserConfiguration(SimpleServiceConfiguration.class)
|
||||
.run(context -> {
|
||||
assertThat(context).hasBean("customService");
|
||||
SimpleService simpleService = context.getBean(CustomService.class);
|
||||
assertThat(simpleService.serve()).isEqualTo("Custom Service");
|
||||
assertThat(context).doesNotHaveBean("defaultService");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGivenDefaultPropertyValue_thenDefaultServiceCreated() {
|
||||
this.contextRunner.withPropertyValues("com.baeldung.service=default")
|
||||
.withUserConfiguration(SimpleServiceConfiguration.class)
|
||||
.run(context -> {
|
||||
assertThat(context).hasBean("defaultService");
|
||||
SimpleService simpleService = context.getBean(DefaultService.class);
|
||||
assertThat(simpleService.serve()).isEqualTo("Default Service");
|
||||
assertThat(context).doesNotHaveBean("customService");
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@TestPropertySource("classpath:ConditionalOnPropertyTest.properties")
|
||||
protected static class SimpleServiceConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(name = "com.baeldung.service", havingValue = "default")
|
||||
@ConditionalOnMissingBean
|
||||
public DefaultService defaultService() {
|
||||
return new DefaultService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(name = "com.baeldung.service", havingValue = "custom")
|
||||
@ConditionalOnMissingBean
|
||||
public CustomService customService() {
|
||||
return new CustomService();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
com.baeldung.service=custom
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue