Merge branch 'master' of https://github.com/eugenp/tutorials into public-access-modifier
This commit is contained in:
commit
6f04980436
@ -7,3 +7,4 @@
|
||||
- [Checking If a List Is Sorted in Java](https://www.baeldung.com/java-check-if-list-sorted)
|
||||
- [Checking if a Java Graph has a Cycle](https://www.baeldung.com/java-graph-has-a-cycle)
|
||||
- [A Guide to the Folding Technique in Java](https://www.baeldung.com/folding-hashing-technique)
|
||||
- [Creating a Triangle with for Loops in Java](https://www.baeldung.com/java-print-triangle)
|
||||
|
150
apache-spark/data/iris.data
Normal file
150
apache-spark/data/iris.data
Normal file
@ -0,0 +1,150 @@
|
||||
5.1,3.5,1.4,0.2,Iris-setosa
|
||||
4.9,3.0,1.4,0.2,Iris-setosa
|
||||
4.7,3.2,1.3,0.2,Iris-setosa
|
||||
4.6,3.1,1.5,0.2,Iris-setosa
|
||||
5.0,3.6,1.4,0.2,Iris-setosa
|
||||
5.4,3.9,1.7,0.4,Iris-setosa
|
||||
4.6,3.4,1.4,0.3,Iris-setosa
|
||||
5.0,3.4,1.5,0.2,Iris-setosa
|
||||
4.4,2.9,1.4,0.2,Iris-setosa
|
||||
4.9,3.1,1.5,0.1,Iris-setosa
|
||||
5.4,3.7,1.5,0.2,Iris-setosa
|
||||
4.8,3.4,1.6,0.2,Iris-setosa
|
||||
4.8,3.0,1.4,0.1,Iris-setosa
|
||||
4.3,3.0,1.1,0.1,Iris-setosa
|
||||
5.8,4.0,1.2,0.2,Iris-setosa
|
||||
5.7,4.4,1.5,0.4,Iris-setosa
|
||||
5.4,3.9,1.3,0.4,Iris-setosa
|
||||
5.1,3.5,1.4,0.3,Iris-setosa
|
||||
5.7,3.8,1.7,0.3,Iris-setosa
|
||||
5.1,3.8,1.5,0.3,Iris-setosa
|
||||
5.4,3.4,1.7,0.2,Iris-setosa
|
||||
5.1,3.7,1.5,0.4,Iris-setosa
|
||||
4.6,3.6,1.0,0.2,Iris-setosa
|
||||
5.1,3.3,1.7,0.5,Iris-setosa
|
||||
4.8,3.4,1.9,0.2,Iris-setosa
|
||||
5.0,3.0,1.6,0.2,Iris-setosa
|
||||
5.0,3.4,1.6,0.4,Iris-setosa
|
||||
5.2,3.5,1.5,0.2,Iris-setosa
|
||||
5.2,3.4,1.4,0.2,Iris-setosa
|
||||
4.7,3.2,1.6,0.2,Iris-setosa
|
||||
4.8,3.1,1.6,0.2,Iris-setosa
|
||||
5.4,3.4,1.5,0.4,Iris-setosa
|
||||
5.2,4.1,1.5,0.1,Iris-setosa
|
||||
5.5,4.2,1.4,0.2,Iris-setosa
|
||||
4.9,3.1,1.5,0.1,Iris-setosa
|
||||
5.0,3.2,1.2,0.2,Iris-setosa
|
||||
5.5,3.5,1.3,0.2,Iris-setosa
|
||||
4.9,3.1,1.5,0.1,Iris-setosa
|
||||
4.4,3.0,1.3,0.2,Iris-setosa
|
||||
5.1,3.4,1.5,0.2,Iris-setosa
|
||||
5.0,3.5,1.3,0.3,Iris-setosa
|
||||
4.5,2.3,1.3,0.3,Iris-setosa
|
||||
4.4,3.2,1.3,0.2,Iris-setosa
|
||||
5.0,3.5,1.6,0.6,Iris-setosa
|
||||
5.1,3.8,1.9,0.4,Iris-setosa
|
||||
4.8,3.0,1.4,0.3,Iris-setosa
|
||||
5.1,3.8,1.6,0.2,Iris-setosa
|
||||
4.6,3.2,1.4,0.2,Iris-setosa
|
||||
5.3,3.7,1.5,0.2,Iris-setosa
|
||||
5.0,3.3,1.4,0.2,Iris-setosa
|
||||
7.0,3.2,4.7,1.4,Iris-versicolor
|
||||
6.4,3.2,4.5,1.5,Iris-versicolor
|
||||
6.9,3.1,4.9,1.5,Iris-versicolor
|
||||
5.5,2.3,4.0,1.3,Iris-versicolor
|
||||
6.5,2.8,4.6,1.5,Iris-versicolor
|
||||
5.7,2.8,4.5,1.3,Iris-versicolor
|
||||
6.3,3.3,4.7,1.6,Iris-versicolor
|
||||
4.9,2.4,3.3,1.0,Iris-versicolor
|
||||
6.6,2.9,4.6,1.3,Iris-versicolor
|
||||
5.2,2.7,3.9,1.4,Iris-versicolor
|
||||
5.0,2.0,3.5,1.0,Iris-versicolor
|
||||
5.9,3.0,4.2,1.5,Iris-versicolor
|
||||
6.0,2.2,4.0,1.0,Iris-versicolor
|
||||
6.1,2.9,4.7,1.4,Iris-versicolor
|
||||
5.6,2.9,3.6,1.3,Iris-versicolor
|
||||
6.7,3.1,4.4,1.4,Iris-versicolor
|
||||
5.6,3.0,4.5,1.5,Iris-versicolor
|
||||
5.8,2.7,4.1,1.0,Iris-versicolor
|
||||
6.2,2.2,4.5,1.5,Iris-versicolor
|
||||
5.6,2.5,3.9,1.1,Iris-versicolor
|
||||
5.9,3.2,4.8,1.8,Iris-versicolor
|
||||
6.1,2.8,4.0,1.3,Iris-versicolor
|
||||
6.3,2.5,4.9,1.5,Iris-versicolor
|
||||
6.1,2.8,4.7,1.2,Iris-versicolor
|
||||
6.4,2.9,4.3,1.3,Iris-versicolor
|
||||
6.6,3.0,4.4,1.4,Iris-versicolor
|
||||
6.8,2.8,4.8,1.4,Iris-versicolor
|
||||
6.7,3.0,5.0,1.7,Iris-versicolor
|
||||
6.0,2.9,4.5,1.5,Iris-versicolor
|
||||
5.7,2.6,3.5,1.0,Iris-versicolor
|
||||
5.5,2.4,3.8,1.1,Iris-versicolor
|
||||
5.5,2.4,3.7,1.0,Iris-versicolor
|
||||
5.8,2.7,3.9,1.2,Iris-versicolor
|
||||
6.0,2.7,5.1,1.6,Iris-versicolor
|
||||
5.4,3.0,4.5,1.5,Iris-versicolor
|
||||
6.0,3.4,4.5,1.6,Iris-versicolor
|
||||
6.7,3.1,4.7,1.5,Iris-versicolor
|
||||
6.3,2.3,4.4,1.3,Iris-versicolor
|
||||
5.6,3.0,4.1,1.3,Iris-versicolor
|
||||
5.5,2.5,4.0,1.3,Iris-versicolor
|
||||
5.5,2.6,4.4,1.2,Iris-versicolor
|
||||
6.1,3.0,4.6,1.4,Iris-versicolor
|
||||
5.8,2.6,4.0,1.2,Iris-versicolor
|
||||
5.0,2.3,3.3,1.0,Iris-versicolor
|
||||
5.6,2.7,4.2,1.3,Iris-versicolor
|
||||
5.7,3.0,4.2,1.2,Iris-versicolor
|
||||
5.7,2.9,4.2,1.3,Iris-versicolor
|
||||
6.2,2.9,4.3,1.3,Iris-versicolor
|
||||
5.1,2.5,3.0,1.1,Iris-versicolor
|
||||
5.7,2.8,4.1,1.3,Iris-versicolor
|
||||
6.3,3.3,6.0,2.5,Iris-virginica
|
||||
5.8,2.7,5.1,1.9,Iris-virginica
|
||||
7.1,3.0,5.9,2.1,Iris-virginica
|
||||
6.3,2.9,5.6,1.8,Iris-virginica
|
||||
6.5,3.0,5.8,2.2,Iris-virginica
|
||||
7.6,3.0,6.6,2.1,Iris-virginica
|
||||
4.9,2.5,4.5,1.7,Iris-virginica
|
||||
7.3,2.9,6.3,1.8,Iris-virginica
|
||||
6.7,2.5,5.8,1.8,Iris-virginica
|
||||
7.2,3.6,6.1,2.5,Iris-virginica
|
||||
6.5,3.2,5.1,2.0,Iris-virginica
|
||||
6.4,2.7,5.3,1.9,Iris-virginica
|
||||
6.8,3.0,5.5,2.1,Iris-virginica
|
||||
5.7,2.5,5.0,2.0,Iris-virginica
|
||||
5.8,2.8,5.1,2.4,Iris-virginica
|
||||
6.4,3.2,5.3,2.3,Iris-virginica
|
||||
6.5,3.0,5.5,1.8,Iris-virginica
|
||||
7.7,3.8,6.7,2.2,Iris-virginica
|
||||
7.7,2.6,6.9,2.3,Iris-virginica
|
||||
6.0,2.2,5.0,1.5,Iris-virginica
|
||||
6.9,3.2,5.7,2.3,Iris-virginica
|
||||
5.6,2.8,4.9,2.0,Iris-virginica
|
||||
7.7,2.8,6.7,2.0,Iris-virginica
|
||||
6.3,2.7,4.9,1.8,Iris-virginica
|
||||
6.7,3.3,5.7,2.1,Iris-virginica
|
||||
7.2,3.2,6.0,1.8,Iris-virginica
|
||||
6.2,2.8,4.8,1.8,Iris-virginica
|
||||
6.1,3.0,4.9,1.8,Iris-virginica
|
||||
6.4,2.8,5.6,2.1,Iris-virginica
|
||||
7.2,3.0,5.8,1.6,Iris-virginica
|
||||
7.4,2.8,6.1,1.9,Iris-virginica
|
||||
7.9,3.8,6.4,2.0,Iris-virginica
|
||||
6.4,2.8,5.6,2.2,Iris-virginica
|
||||
6.3,2.8,5.1,1.5,Iris-virginica
|
||||
6.1,2.6,5.6,1.4,Iris-virginica
|
||||
7.7,3.0,6.1,2.3,Iris-virginica
|
||||
6.3,3.4,5.6,2.4,Iris-virginica
|
||||
6.4,3.1,5.5,1.8,Iris-virginica
|
||||
6.0,3.0,4.8,1.8,Iris-virginica
|
||||
6.9,3.1,5.4,2.1,Iris-virginica
|
||||
6.7,3.1,5.6,2.4,Iris-virginica
|
||||
6.9,3.1,5.1,2.3,Iris-virginica
|
||||
5.8,2.7,5.1,1.9,Iris-virginica
|
||||
6.8,3.2,5.9,2.3,Iris-virginica
|
||||
6.7,3.3,5.7,2.5,Iris-virginica
|
||||
6.7,3.0,5.2,2.3,Iris-virginica
|
||||
6.3,2.5,5.0,1.9,Iris-virginica
|
||||
6.5,3.0,5.2,2.0,Iris-virginica
|
||||
6.2,3.4,5.4,2.3,Iris-virginica
|
||||
5.9,3.0,5.1,1.8,Iris-virginica
|
BIN
apache-spark/model/logistic-regression/data/._SUCCESS.crc
Normal file
BIN
apache-spark/model/logistic-regression/data/._SUCCESS.crc
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
apache-spark/model/logistic-regression/metadata/._SUCCESS.crc
Normal file
BIN
apache-spark/model/logistic-regression/metadata/._SUCCESS.crc
Normal file
Binary file not shown.
BIN
apache-spark/model/logistic-regression/metadata/.part-00000.crc
Normal file
BIN
apache-spark/model/logistic-regression/metadata/.part-00000.crc
Normal file
Binary file not shown.
@ -0,0 +1 @@
|
||||
{"class":"org.apache.spark.mllib.classification.LogisticRegressionModel","version":"1.0","numFeatures":4,"numClasses":3}
|
@ -1,31 +1,32 @@
|
||||
<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</groupId>
|
||||
<artifactId>apache-spark</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>apache-spark</name>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://maven.apache.org</url>
|
||||
<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</groupId>
|
||||
<artifactId>apache-spark</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>apache-spark</name>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.spark</groupId>
|
||||
<artifactId>spark-core_2.11</artifactId>
|
||||
<version>${org.apache.spark.spark-core.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.spark</groupId>
|
||||
<artifactId>spark-sql_2.11</artifactId>
|
||||
<version>${org.apache.spark.spark-sql.version}</version>
|
||||
<scope>provided</scope>
|
||||
<groupId>org.apache.spark</groupId>
|
||||
<artifactId>spark-core_2.11</artifactId>
|
||||
<version>${org.apache.spark.spark-core.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.spark</groupId>
|
||||
<artifactId>spark-sql_2.11</artifactId>
|
||||
<version>${org.apache.spark.spark-sql.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.spark</groupId>
|
||||
@ -33,6 +34,12 @@
|
||||
<version>${org.apache.spark.spark-streaming.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.spark</groupId>
|
||||
<artifactId>spark-mllib_2.11</artifactId>
|
||||
<version>${org.apache.spark.spark-mllib.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.spark</groupId>
|
||||
<artifactId>spark-streaming-kafka-0-10_2.11</artifactId>
|
||||
@ -48,46 +55,47 @@
|
||||
<artifactId>spark-cassandra-connector-java_2.11</artifactId>
|
||||
<version>${com.datastax.spark.spark-cassandra-connector-java.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<org.apache.spark.spark-core.version>2.3.0</org.apache.spark.spark-core.version>
|
||||
<org.apache.spark.spark-sql.version>2.3.0</org.apache.spark.spark-sql.version>
|
||||
<org.apache.spark.spark-streaming.version>2.3.0</org.apache.spark.spark-streaming.version>
|
||||
<org.apache.spark.spark-streaming-kafka.version>2.3.0</org.apache.spark.spark-streaming-kafka.version>
|
||||
<com.datastax.spark.spark-cassandra-connector.version>2.3.0</com.datastax.spark.spark-cassandra-connector.version>
|
||||
<com.datastax.spark.spark-cassandra-connector-java.version>1.5.2</com.datastax.spark.spark-cassandra-connector-java.version>
|
||||
<properties>
|
||||
<org.apache.spark.spark-core.version>2.3.0</org.apache.spark.spark-core.version>
|
||||
<org.apache.spark.spark-sql.version>2.3.0</org.apache.spark.spark-sql.version>
|
||||
<org.apache.spark.spark-streaming.version>2.3.0</org.apache.spark.spark-streaming.version>
|
||||
<org.apache.spark.spark-mllib.version>2.3.0</org.apache.spark.spark-mllib.version>
|
||||
<org.apache.spark.spark-streaming-kafka.version>2.3.0</org.apache.spark.spark-streaming-kafka.version>
|
||||
<com.datastax.spark.spark-cassandra-connector.version>2.3.0</com.datastax.spark.spark-cassandra-connector.version>
|
||||
<com.datastax.spark.spark-cassandra-connector-java.version>1.5.2</com.datastax.spark.spark-cassandra-connector-java.version>
|
||||
<maven-compiler-plugin.version>3.2</maven-compiler-plugin.version>
|
||||
</properties>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
@ -0,0 +1,111 @@
|
||||
package com.baeldung.ml;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Level;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.apache.spark.SparkConf;
|
||||
import org.apache.spark.api.java.JavaPairRDD;
|
||||
import org.apache.spark.api.java.JavaRDD;
|
||||
import org.apache.spark.api.java.JavaSparkContext;
|
||||
import org.apache.spark.mllib.classification.LogisticRegressionModel;
|
||||
import org.apache.spark.mllib.classification.LogisticRegressionWithLBFGS;
|
||||
import org.apache.spark.mllib.evaluation.MulticlassMetrics;
|
||||
import org.apache.spark.mllib.linalg.Matrix;
|
||||
import org.apache.spark.mllib.linalg.Vector;
|
||||
import org.apache.spark.mllib.linalg.Vectors;
|
||||
import org.apache.spark.mllib.regression.LabeledPoint;
|
||||
import org.apache.spark.mllib.stat.MultivariateStatisticalSummary;
|
||||
import org.apache.spark.mllib.stat.Statistics;
|
||||
|
||||
import scala.Tuple2;
|
||||
|
||||
public class MachineLearningApp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
// 1. Setting the Spark Context
|
||||
SparkConf conf = new SparkConf().setAppName("Main")
|
||||
.setMaster("local[2]")
|
||||
.set("spark.executor.memory", "3g")
|
||||
.set("spark.driver.memory", "3g");
|
||||
JavaSparkContext sc = new JavaSparkContext(conf);
|
||||
Logger.getLogger("org")
|
||||
.setLevel(Level.OFF);
|
||||
Logger.getLogger("akka")
|
||||
.setLevel(Level.OFF);
|
||||
|
||||
// 2. Loading the Data-set
|
||||
String dataFile = "data\\iris.data";
|
||||
JavaRDD<String> data = sc.textFile(dataFile);
|
||||
|
||||
// 3. Exploratory Data Analysis
|
||||
// 3.1. Creating Vector of Input Data
|
||||
JavaRDD<Vector> inputData = data.map(line -> {
|
||||
String[] parts = line.split(",");
|
||||
double[] v = new double[parts.length - 1];
|
||||
for (int i = 0; i < parts.length - 1; i++) {
|
||||
v[i] = Double.parseDouble(parts[i]);
|
||||
}
|
||||
return Vectors.dense(v);
|
||||
});
|
||||
// 3.2. Performing Statistical Analysis
|
||||
MultivariateStatisticalSummary summary = Statistics.colStats(inputData.rdd());
|
||||
System.out.println("Summary Mean:");
|
||||
System.out.println(summary.mean());
|
||||
System.out.println("Summary Variance:");
|
||||
System.out.println(summary.variance());
|
||||
System.out.println("Summary Non-zero:");
|
||||
System.out.println(summary.numNonzeros());
|
||||
// 3.3. Performing Correlation Analysis
|
||||
Matrix correlMatrix = Statistics.corr(inputData.rdd(), "pearson");
|
||||
System.out.println("Correlation Matrix:");
|
||||
System.out.println(correlMatrix.toString());
|
||||
|
||||
// 4. Data Preparation
|
||||
// 4.1. Creating Map for Textual Output Labels
|
||||
Map<String, Integer> map = new HashMap<String, Integer>();
|
||||
map.put("Iris-setosa", 0);
|
||||
map.put("Iris-versicolor", 1);
|
||||
map.put("Iris-virginica", 2);
|
||||
// 4.2. Creating LabeledPoint of Input and Output Data
|
||||
JavaRDD<LabeledPoint> parsedData = data.map(line -> {
|
||||
String[] parts = line.split(",");
|
||||
double[] v = new double[parts.length - 1];
|
||||
for (int i = 0; i < parts.length - 1; i++) {
|
||||
v[i] = Double.parseDouble(parts[i]);
|
||||
}
|
||||
return new LabeledPoint(map.get(parts[parts.length - 1]), Vectors.dense(v));
|
||||
});
|
||||
|
||||
// 5. Data Splitting into 80% Training and 20% Test Sets
|
||||
JavaRDD<LabeledPoint>[] splits = parsedData.randomSplit(new double[] { 0.8, 0.2 }, 11L);
|
||||
JavaRDD<LabeledPoint> trainingData = splits[0].cache();
|
||||
JavaRDD<LabeledPoint> testData = splits[1];
|
||||
|
||||
// 6. Modeling
|
||||
// 6.1. Model Training
|
||||
LogisticRegressionModel model = new LogisticRegressionWithLBFGS().setNumClasses(3)
|
||||
.run(trainingData.rdd());
|
||||
// 6.2. Model Evaluation
|
||||
JavaPairRDD<Object, Object> predictionAndLabels = testData.mapToPair(p -> new Tuple2<>(model.predict(p.features()), p.label()));
|
||||
MulticlassMetrics metrics = new MulticlassMetrics(predictionAndLabels.rdd());
|
||||
double accuracy = metrics.accuracy();
|
||||
System.out.println("Model Accuracy on Test Data: " + accuracy);
|
||||
|
||||
// 7. Model Saving and Loading
|
||||
// 7.1. Model Saving
|
||||
model.save(sc.sc(), "model\\logistic-regression");
|
||||
// 7.2. Model Loading
|
||||
LogisticRegressionModel sameModel = LogisticRegressionModel.load(sc.sc(), "model\\logistic-regression");
|
||||
// 7.3. Prediction on New Data
|
||||
Vector newData = Vectors.dense(new double[] { 1, 1, 1, 1 });
|
||||
double prediction = sameModel.predict(newData);
|
||||
System.out.println("Model Prediction on New Data = " + prediction);
|
||||
|
||||
// 8. Clean-up
|
||||
sc.close();
|
||||
}
|
||||
|
||||
}
|
31
bazel/WORKSPACE
Normal file
31
bazel/WORKSPACE
Normal file
@ -0,0 +1,31 @@
|
||||
|
||||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_jar")
|
||||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
|
||||
|
||||
|
||||
RULES_JVM_EXTERNAL_TAG = "2.0.1"
|
||||
RULES_JVM_EXTERNAL_SHA = "55e8d3951647ae3dffde22b4f7f8dee11b3f70f3f89424713debd7076197eaca"
|
||||
|
||||
http_archive(
|
||||
name = "rules_jvm_external",
|
||||
strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
|
||||
sha256 = RULES_JVM_EXTERNAL_SHA,
|
||||
url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
|
||||
)
|
||||
|
||||
load("@rules_jvm_external//:defs.bzl", "maven_install")
|
||||
|
||||
maven_install(
|
||||
artifacts = [
|
||||
"org.apache.commons:commons-lang3:3.9"
|
||||
],
|
||||
repositories = [
|
||||
"https://repo1.maven.org/maven2",
|
||||
]
|
||||
)
|
||||
|
||||
http_jar (
|
||||
name = "apache-commons-lang",
|
||||
url = "https://repo1.maven.org/maven2/org/apache/commons/commons-lang3/3.9/commons-lang3-3.9.jar"
|
||||
)
|
||||
|
7
bazel/bazelapp/BUILD
Normal file
7
bazel/bazelapp/BUILD
Normal file
@ -0,0 +1,7 @@
|
||||
|
||||
java_binary (
|
||||
name = "BazelApp",
|
||||
srcs = glob(["src/main/java/com/baeldung/*.java"]),
|
||||
main_class = "com.baeldung.BazelApp",
|
||||
deps = ["//bazelgreeting:greeter", "@maven//:org_apache_commons_commons_lang3"]
|
||||
)
|
28
bazel/bazelapp/pom.xml
Normal file
28
bazel/bazelapp/pom.xml
Normal file
@ -0,0 +1,28 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<artifactId>bazel</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>bazelapp</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>bazelgreeting</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.9</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
15
bazel/bazelapp/src/main/java/com/baeldung/BazelApp.java
Normal file
15
bazel/bazelapp/src/main/java/com/baeldung/BazelApp.java
Normal file
@ -0,0 +1,15 @@
|
||||
package com.baeldung;
|
||||
|
||||
import com.baeldung.Greetings;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class BazelApp {
|
||||
|
||||
public static void main(String ... args) {
|
||||
Greetings greetings = new Greetings();
|
||||
|
||||
System.out.println(greetings.greet("Bazel"));
|
||||
|
||||
System.out.println(StringUtils.lowerCase("Bazel"));
|
||||
}
|
||||
}
|
6
bazel/bazelgreeting/BUILD
Normal file
6
bazel/bazelgreeting/BUILD
Normal file
@ -0,0 +1,6 @@
|
||||
|
||||
java_library (
|
||||
name = "greeter",
|
||||
srcs = glob(["src/main/java/com/baeldung/*.java"]),
|
||||
visibility = ["//bazelapp:__pkg__"]
|
||||
)
|
15
bazel/bazelgreeting/pom.xml
Normal file
15
bazel/bazelgreeting/pom.xml
Normal file
@ -0,0 +1,15 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<artifactId>bazel</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>bazelgreeting</artifactId>
|
||||
|
||||
|
||||
</project>
|
@ -0,0 +1,8 @@
|
||||
package com.baeldung;
|
||||
|
||||
public class Greetings {
|
||||
|
||||
public String greet(String name) {
|
||||
return "Hello ".concat(name);
|
||||
}
|
||||
}
|
20
bazel/pom.xml
Normal file
20
bazel/pom.xml
Normal file
@ -0,0 +1,20 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>bazel</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>bazelgreeting</module>
|
||||
<module>bazelapp</module>
|
||||
</modules>
|
||||
|
||||
|
||||
</project>
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung.java.io;
|
||||
package org.baeldung.writetofile;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
@ -11,3 +11,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
- [Working with Tree Model Nodes in Jackson](https://www.baeldung.com/jackson-json-node-tree-model)
|
||||
- [Converting JSON to CSV in Java](https://www.baeldung.com/java-converting-json-to-csv)
|
||||
- [Compare Two JSON Objects with Jackson](https://www.baeldung.com/jackson-compare-two-json-objects)
|
||||
- [Calling Default Serializer from Custom Serializer in Jackson](https://www.baeldung.com/jackson-call-default-serializer-from-custom-serializer)
|
||||
|
@ -1,3 +0,0 @@
|
||||
This file exists to ensure this empty directory is committed in Git.
|
||||
|
||||
Please remove this file when this directory is populated.
|
@ -0,0 +1,49 @@
|
||||
package com.baeldung.string.wordcount;
|
||||
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
public class WordCounter {
|
||||
static final int WORD = 0;
|
||||
static final int SEPARATOR = 1;
|
||||
|
||||
public static int countWordsUsingRegex(String arg) {
|
||||
if (arg == null) {
|
||||
return 0;
|
||||
}
|
||||
final String[] words = arg.split("[\\pP\\s&&[^']]+");
|
||||
return words.length;
|
||||
}
|
||||
|
||||
public static int countWordsUsingTokenizer(String arg) {
|
||||
if (arg == null) {
|
||||
return 0;
|
||||
}
|
||||
final StringTokenizer stringTokenizer = new StringTokenizer(arg);
|
||||
return stringTokenizer.countTokens();
|
||||
}
|
||||
|
||||
public static int countWordsManually(String arg) {
|
||||
if (arg == null) {
|
||||
return 0;
|
||||
}
|
||||
int flag = SEPARATOR;
|
||||
int count = 0;
|
||||
int stringLength = arg.length();
|
||||
int characterCounter = 0;
|
||||
|
||||
while (characterCounter < stringLength) {
|
||||
if (isAllowedInWord(arg.charAt(characterCounter)) && flag == SEPARATOR) {
|
||||
flag = WORD;
|
||||
count++;
|
||||
} else if (!isAllowedInWord(arg.charAt(characterCounter))) {
|
||||
flag = SEPARATOR;
|
||||
}
|
||||
characterCounter++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
private static boolean isAllowedInWord(char charAt) {
|
||||
return charAt == '\'' || Character.isLetter(charAt);
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.baeldung.string.wordcount;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
public class WordCountUnitTest {
|
||||
private String string1 = "This is a test sentence with eight words";
|
||||
private String string2 = "This#is%a test sentence with eight words";
|
||||
|
||||
@Test
|
||||
public void givenStringWith8Words_whenUsingRegexCount_ThenResultEqual8() {
|
||||
assertEquals(8, WordCounter.countWordsUsingRegex(string2));
|
||||
assertEquals(9, WordCounter.countWordsUsingRegex("no&one#should%ever-write-like,this;but:well"));
|
||||
assertEquals(7, WordCounter.countWordsUsingRegex("the farmer's wife--she was from Albuquerque"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringWith8Words_whenUsingManualMethod_ThenWordCountEqual8() {
|
||||
assertEquals(8, WordCounter.countWordsManually(string1));
|
||||
assertEquals(9, WordCounter.countWordsManually("no&one#should%ever-write-like,this but well"));
|
||||
assertEquals(7, WordCounter.countWordsManually("the farmer's wife--she was from Albuquerque"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAStringWith8Words_whenUsingTokenizer_ThenWordCountEqual8() {
|
||||
assertEquals(8, WordCounter.countWordsUsingTokenizer(string1));
|
||||
assertEquals(3, new StringTokenizer("three blind mice").countTokens());
|
||||
assertEquals(4, new StringTokenizer("see\thow\tthey\trun").countTokens());
|
||||
assertEquals(7, new StringTokenizer("the farmer's wife--she was from Albuquerque", " -").countTokens());
|
||||
assertEquals(10, new StringTokenizer("did,you,ever,see,such,a,sight,in,your,life", ",").countTokens());
|
||||
}
|
||||
}
|
@ -0,0 +1,126 @@
|
||||
package com.baeldung.jhipster5.security;
|
||||
|
||||
import com.baeldung.jhipster5.domain.User;
|
||||
import com.baeldung.jhipster5.security.dto.LoginRequest;
|
||||
import com.baeldung.jhipster5.security.dto.LoginResponse;
|
||||
import com.baeldung.jhipster5.service.UserService;
|
||||
import com.baeldung.jhipster5.service.dto.UserDTO;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.AuthenticationServiceException;
|
||||
import org.springframework.security.authentication.BadCredentialsException;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
public class CustomAuthenticationManager implements AuthenticationManager {
|
||||
|
||||
private final static Logger LOG = LoggerFactory.getLogger(CustomAuthenticationManager.class);
|
||||
|
||||
private final String REMOTE_LOGIN_URL = "https://example.com/login";
|
||||
|
||||
private final RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Override
|
||||
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
|
||||
|
||||
LoginRequest loginRequest = new LoginRequest();
|
||||
loginRequest.setUsername(authentication.getPrincipal().toString());
|
||||
loginRequest.setPassword(authentication.getCredentials().toString());
|
||||
|
||||
try
|
||||
{
|
||||
ResponseEntity<LoginResponse> response =
|
||||
restTemplate.postForEntity(
|
||||
REMOTE_LOGIN_URL,
|
||||
loginRequest,
|
||||
LoginResponse.class);
|
||||
|
||||
if(response.getStatusCode().is2xxSuccessful())
|
||||
{
|
||||
//
|
||||
// Need to create a new local user if this is the first time logging in; this
|
||||
// is required so they can be issued JWTs. We can use this flow to also keep
|
||||
// our local use entry up to date with data from the remote service if needed
|
||||
// (for example, if the first and last name might change, this is where we would
|
||||
// update the local user entry)
|
||||
//
|
||||
|
||||
User user = userService.getUserWithAuthoritiesByLogin(authentication.getPrincipal().toString())
|
||||
.orElseGet(() -> userService.createUser(createUserDTO(response.getBody(), authentication)));
|
||||
return createAuthentication(authentication, user);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new BadCredentialsException("Invalid username or password");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LOG.warn("Failed to authenticate", e);
|
||||
throw new AuthenticationServiceException("Failed to login", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new authentication with basic roles
|
||||
* @param auth Contains auth details that will be copied into the new one.
|
||||
* @param user User object representing who is logging in
|
||||
* @return Authentication
|
||||
*/
|
||||
private Authentication createAuthentication(Authentication auth, User user) {
|
||||
|
||||
//
|
||||
// Honor any roles the user already has set; default is just USER role
|
||||
// but could be modified after account creation
|
||||
//
|
||||
|
||||
Collection<? extends GrantedAuthority> authorities = user
|
||||
.getAuthorities()
|
||||
.stream()
|
||||
.map(a -> new SimpleGrantedAuthority(a.getName()))
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
UsernamePasswordAuthenticationToken token
|
||||
= new UsernamePasswordAuthenticationToken(
|
||||
user.getId(),
|
||||
auth.getCredentials().toString(),
|
||||
authorities);
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new UserDTO with basic info.
|
||||
* @param loginResponse Response from peloton login API
|
||||
* @param authentication Contains user login info (namely username and password)
|
||||
* @return UserDTO
|
||||
*/
|
||||
private UserDTO createUserDTO(LoginResponse loginResponse, Authentication authentication) {
|
||||
|
||||
UserDTO dto = new UserDTO();
|
||||
|
||||
dto.setActivated(true);
|
||||
dto.setEmail(loginResponse.getEmail());
|
||||
dto.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));
|
||||
dto.setFirstName(loginResponse.getFirstName());
|
||||
dto.setLastName(loginResponse.getLastName());
|
||||
|
||||
return dto;
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.baeldung.jhipster5.security.dto;
|
||||
|
||||
/**
|
||||
* Simple DTO representing a login request to a remote service.
|
||||
*/
|
||||
public class LoginRequest {
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
public LoginRequest() {
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package com.baeldung.jhipster5.security.dto;
|
||||
|
||||
/**
|
||||
* Simple DTO representing the response of logging in using a remote service.
|
||||
*/
|
||||
public class LoginResponse {
|
||||
|
||||
private String username;
|
||||
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
private String email;
|
||||
|
||||
public LoginResponse() {
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
import { Routes } from '@angular/router';
|
||||
|
||||
import { activateRoute, passwordRoute, passwordResetFinishRoute, passwordResetInitRoute, registerRoute, settingsRoute } from './';
|
||||
import { settingsRoute } from './';
|
||||
|
||||
const ACCOUNT_ROUTES = [activateRoute, passwordRoute, passwordResetFinishRoute, passwordResetInitRoute, registerRoute, settingsRoute];
|
||||
const ACCOUNT_ROUTES = [settingsRoute];
|
||||
|
||||
export const accountState: Routes = [
|
||||
{
|
||||
|
@ -114,12 +114,6 @@
|
||||
<span>Settings</span>
|
||||
</a>
|
||||
</li>
|
||||
<li *ngSwitchCase="true">
|
||||
<a class="dropdown-item" routerLink="password" routerLinkActive="active" (click)="collapseNavbar()">
|
||||
<fa-icon icon="clock" fixedWidth="true"></fa-icon>
|
||||
<span>Password</span>
|
||||
</a>
|
||||
</li>
|
||||
<li *ngSwitchCase="true">
|
||||
<a class="dropdown-item" (click)="logout()" id="logout">
|
||||
<fa-icon icon="sign-out-alt" fixedWidth="true"></fa-icon>
|
||||
@ -132,12 +126,6 @@
|
||||
<span>Sign in</span>
|
||||
</a>
|
||||
</li>
|
||||
<li *ngSwitchCase="false">
|
||||
<a class="dropdown-item" routerLink="register" routerLinkActive="active" (click)="collapseNavbar()">
|
||||
<fa-icon icon="user-plus" fixedWidth="true"></fa-icon>
|
||||
<span>Register</span>
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
@ -30,14 +30,6 @@
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Sign in</button>
|
||||
</form>
|
||||
<p></p>
|
||||
<div class="alert alert-warning">
|
||||
<a class="alert-link" (click)="requestResetPassword()">Did you forget your password?</a>
|
||||
</div>
|
||||
<div class="alert alert-warning">
|
||||
<span>You don't have an account yet?</span>
|
||||
<a class="alert-link" (click)="register()">Register a new account</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -0,0 +1,54 @@
|
||||
package com.baeldung.jhipster5.security;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.BadCredentialsException;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* AuthenticationManager used solely by unit tests.
|
||||
*/
|
||||
@Component
|
||||
@Primary
|
||||
public class MockAuthenticationManager implements AuthenticationManager
|
||||
{
|
||||
private final static Collection<? extends GrantedAuthority> ROLES =
|
||||
Collections.singleton(new SimpleGrantedAuthority("ROLE_USER"));
|
||||
|
||||
@Autowired
|
||||
private UserDetailsService userDetailsService;
|
||||
|
||||
@Autowired
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
@Override
|
||||
public Authentication authenticate(Authentication authentication) throws AuthenticationException
|
||||
{
|
||||
|
||||
UserDetails userDetails = userDetailsService.loadUserByUsername(authentication.getName());
|
||||
|
||||
if(userDetails == null || !passwordEncoder.matches(authentication.getCredentials().toString(), userDetails.getPassword()))
|
||||
{
|
||||
throw new BadCredentialsException("Invalid username/password");
|
||||
}
|
||||
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
|
||||
authentication.getPrincipal().toString(),
|
||||
authentication.getCredentials().toString(),
|
||||
ROLES);
|
||||
|
||||
return token;
|
||||
}
|
||||
}
|
@ -26,7 +26,7 @@ import com.google.common.base.Preconditions;
|
||||
@PropertySource({ "classpath:persistence-${envTarget:h2}.properties" })
|
||||
@ComponentScan({ "com.baeldung.spring.data.persistence" })
|
||||
// @ImportResource("classpath*:springDataPersistenceConfig.xml")
|
||||
@EnableJpaRepositories(basePackages = "com.baeldung.spring.data.persistence.dao")
|
||||
@EnableJpaRepositories(basePackages = { "com.baeldung.spring.data.persistence.dao", "com.baeldung.spring.data.persistence.jpaquery" })
|
||||
public class PersistenceConfig {
|
||||
|
||||
@Autowired
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.spring.data.persistence.dao.user;
|
||||
package com.baeldung.spring.data.persistence.jpaquery;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Collection;
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.spring.data.persistence.dao.user;
|
||||
package com.baeldung.spring.data.persistence.jpaquery;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.spring.data.persistence.dao.user;
|
||||
package com.baeldung.spring.data.persistence.jpaquery;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.spring.data.persistence.dao.user;
|
||||
package com.baeldung.spring.data.persistence.jpaquery;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@ -19,6 +19,7 @@ import javax.persistence.EntityManager;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import com.baeldung.spring.data.persistence.config.PersistenceConfig;
|
||||
import com.baeldung.spring.data.persistence.jpaquery.UserRepository;
|
||||
import com.baeldung.spring.data.persistence.model.User;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.spring.data.persistence.dao.user;
|
||||
package com.baeldung.spring.data.persistence.jpaquery;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
5
pom.xml
5
pom.xml
@ -564,6 +564,8 @@
|
||||
<module>spring-boot-flowable</module>
|
||||
<module>spring-security-kerberos</module>
|
||||
<module>oauth2-framework-impl</module>
|
||||
|
||||
<module>spring-boot-nashorn</module>
|
||||
</modules>
|
||||
|
||||
</profile>
|
||||
@ -798,6 +800,8 @@
|
||||
<module>tensorflow-java</module>
|
||||
<module>spring-boot-flowable</module>
|
||||
<module>spring-security-kerberos</module>
|
||||
|
||||
<module>spring-boot-nashorn</module>
|
||||
|
||||
</modules>
|
||||
|
||||
@ -947,6 +951,7 @@
|
||||
|
||||
<module>spring-boot-flowable</module>
|
||||
<module>spring-security-kerberos</module>
|
||||
<module>spring-boot-nashorn</module>
|
||||
</modules>
|
||||
|
||||
</profile>
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.swaggerboot;
|
||||
package com.baeldung.swagger2boot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.swaggerboot.configuration;
|
||||
package com.baeldung.swagger2boot.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.swaggerboot.controller;
|
||||
package com.baeldung.swagger2boot.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
3
spring-boot-nashorn/README.md
Normal file
3
spring-boot-nashorn/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
### Relevant Articles:
|
||||
- []()
|
||||
|
51
spring-boot-nashorn/pom.xml
Normal file
51
spring-boot-nashorn/pom.xml
Normal file
@ -0,0 +1,51 @@
|
||||
<?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.nashorn</groupId>
|
||||
<artifactId>spring-boot-nashorn</artifactId>
|
||||
<name>spring-boot-nashorn</name>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat.embed</groupId>
|
||||
<artifactId>tomcat-embed-jasper</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -0,0 +1,20 @@
|
||||
package com.baeldung.nashorn;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application extends SpringBootServletInitializer {
|
||||
|
||||
@Override
|
||||
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
|
||||
return application.sources(Application.class);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.baeldung.nashorn.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class MyRestController {
|
||||
|
||||
@RequestMapping("/next/{last}/{secondLast}")
|
||||
public int index(@PathVariable("last") int last, @PathVariable("secondLast") int secondLast) throws Exception {
|
||||
return last + secondLast;
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.baeldung.nashorn.controller;
|
||||
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@Controller
|
||||
public class MyWebController {
|
||||
|
||||
@RequestMapping("/")
|
||||
public String index(Map<String, Object> model) throws Exception {
|
||||
|
||||
ScriptEngine nashorn = new ScriptEngineManager().getEngineByName("nashorn");
|
||||
|
||||
getClass().getResource("classpath:storedProcedures.sql");
|
||||
|
||||
nashorn.eval(new InputStreamReader(new ClassPathResource("static/js/react.js").getInputStream()));
|
||||
nashorn.eval(new InputStreamReader(new ClassPathResource("static/js/react-dom-server.js").getInputStream()));
|
||||
|
||||
nashorn.eval(new InputStreamReader(new ClassPathResource("static/app.js").getInputStream()));
|
||||
Object html = nashorn.eval("ReactDOMServer.renderToString(" + "React.createElement(App, {data: [0,1,1]})" + ");");
|
||||
|
||||
model.put("content", String.valueOf(html));
|
||||
return "index";
|
||||
}
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
spring.mvc.view.prefix: /WEB-INF/jsp/
|
||||
spring.mvc.view.suffix: .jsp
|
37
spring-boot-nashorn/src/main/resources/static/app.js
Normal file
37
spring-boot-nashorn/src/main/resources/static/app.js
Normal file
@ -0,0 +1,37 @@
|
||||
var App = React.createClass({displayName: "App",
|
||||
|
||||
handleSubmit: function () {
|
||||
var last = this.state.data[this.state.data.length-1];
|
||||
var secondLast = this.state.data[this.state.data.length-2];
|
||||
$.ajax({
|
||||
url: '/next/'+last+'/'+secondLast,
|
||||
dataType: 'text',
|
||||
success: function (msg) {
|
||||
var series = this.state.data;
|
||||
series.push(msg);
|
||||
this.setState({data: series});
|
||||
}.bind(this),
|
||||
error: function (xhr, status, err) {
|
||||
console.error("/next", status, err.toString());
|
||||
}.bind(this)
|
||||
});
|
||||
},
|
||||
|
||||
componentDidMount: function() {
|
||||
this.setState({data: this.props.data});
|
||||
},
|
||||
|
||||
getInitialState: function () {
|
||||
return {data: []};
|
||||
},
|
||||
|
||||
render: function () {
|
||||
return (
|
||||
React.createElement("div", {className: "app"},
|
||||
React.createElement("h2", null, "Fibonacci Generator"),
|
||||
React.createElement("h2", null, this.state.data.toString()),
|
||||
React.createElement("input", {type: "submit", value: "Next", onClick: this.handleSubmit})
|
||||
)
|
||||
);
|
||||
}
|
||||
});
|
42
spring-boot-nashorn/src/main/resources/static/js/react-dom-server.js
vendored
Normal file
42
spring-boot-nashorn/src/main/resources/static/js/react-dom-server.js
vendored
Normal file
@ -0,0 +1,42 @@
|
||||
/**
|
||||
* ReactDOMServer v0.14.3
|
||||
*
|
||||
* Copyright 2013-2015, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*
|
||||
*/
|
||||
// Based off https://github.com/ForbesLindesay/umd/blob/master/template.js
|
||||
;(function(f) {
|
||||
// CommonJS
|
||||
if (typeof exports === "object" && typeof module !== "undefined") {
|
||||
module.exports = f(require('react'));
|
||||
|
||||
// RequireJS
|
||||
} else if (typeof define === "function" && define.amd) {
|
||||
define(['react'], f);
|
||||
|
||||
// <script>
|
||||
} else {
|
||||
var g
|
||||
if (typeof window !== "undefined") {
|
||||
g = window;
|
||||
} else if (typeof global !== "undefined") {
|
||||
g = global;
|
||||
} else if (typeof self !== "undefined") {
|
||||
g = self;
|
||||
} else {
|
||||
// works providing we're not in "use strict";
|
||||
// needed for Java 8 Nashorn
|
||||
// see https://github.com/facebook/react/issues/3037
|
||||
g = this;
|
||||
}
|
||||
g.ReactDOMServer = f(g.React);
|
||||
}
|
||||
|
||||
})(function(React) {
|
||||
return React.__SECRET_DOM_SERVER_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
|
||||
});
|
42
spring-boot-nashorn/src/main/resources/static/js/react-dom.js
vendored
Normal file
42
spring-boot-nashorn/src/main/resources/static/js/react-dom.js
vendored
Normal file
@ -0,0 +1,42 @@
|
||||
/**
|
||||
* ReactDOM v0.14.3
|
||||
*
|
||||
* Copyright 2013-2015, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*
|
||||
*/
|
||||
// Based off https://github.com/ForbesLindesay/umd/blob/master/template.js
|
||||
;(function(f) {
|
||||
// CommonJS
|
||||
if (typeof exports === "object" && typeof module !== "undefined") {
|
||||
module.exports = f(require('react'));
|
||||
|
||||
// RequireJS
|
||||
} else if (typeof define === "function" && define.amd) {
|
||||
define(['react'], f);
|
||||
|
||||
// <script>
|
||||
} else {
|
||||
var g
|
||||
if (typeof window !== "undefined") {
|
||||
g = window;
|
||||
} else if (typeof global !== "undefined") {
|
||||
g = global;
|
||||
} else if (typeof self !== "undefined") {
|
||||
g = self;
|
||||
} else {
|
||||
// works providing we're not in "use strict";
|
||||
// needed for Java 8 Nashorn
|
||||
// see https://github.com/facebook/react/issues/3037
|
||||
g = this;
|
||||
}
|
||||
g.ReactDOM = f(g.React);
|
||||
}
|
||||
|
||||
})(function(React) {
|
||||
return React.__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
|
||||
});
|
18794
spring-boot-nashorn/src/main/resources/static/js/react.js
vendored
Normal file
18794
spring-boot-nashorn/src/main/resources/static/js/react.js
vendored
Normal file
File diff suppressed because it is too large
Load Diff
19
spring-boot-nashorn/src/main/webapp/WEB-INF/jsp/index.jsp
Normal file
19
spring-boot-nashorn/src/main/webapp/WEB-INF/jsp/index.jsp
Normal file
@ -0,0 +1,19 @@
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>Hello React!</title>
|
||||
<script type="text/javascript" src="js/react.js"></script>
|
||||
<script type="text/javascript" src="js/react-dom.js"></script>
|
||||
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.0.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root">${content}</div>
|
||||
<script type="text/javascript" src="app.js"></script>
|
||||
<script type="text/javascript">
|
||||
ReactDOM.render(
|
||||
React.createElement(App, {data: [0,1,1]}),
|
||||
document.getElementById("root")
|
||||
);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
@ -26,7 +26,7 @@ import java.util.List;
|
||||
*/
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan({ "com.baeldung.web" })
|
||||
@ComponentScan({ "com.baeldung.web", "com.baeldung.requestmapping" })
|
||||
public class MvcConfig implements WebMvcConfigurer {
|
||||
|
||||
public MvcConfig() {
|
||||
@ -66,7 +66,6 @@ public class MvcConfig implements WebMvcConfigurer {
|
||||
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
|
||||
configurer.defaultContentType(MediaType.APPLICATION_JSON);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.web.controller;
|
||||
package com.baeldung.requestmapping;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.web.controller;
|
||||
package com.baeldung.requestmapping;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.web.controller;
|
||||
package com.baeldung.requestmapping;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
@ -1,5 +1,5 @@
|
||||
|
||||
package com.baeldung.web.test;
|
||||
package com.baeldung.requestmapping;
|
||||
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.is;
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung.client;
|
||||
package org.baeldung.resttemplate;
|
||||
|
||||
import static org.apache.commons.codec.binary.Base64.encodeBase64;
|
||||
import static org.baeldung.client.Consts.APPLICATION_PORT;
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung.spring;
|
||||
package org.baeldung.swagger2;
|
||||
|
||||
import static com.google.common.collect.Lists.newArrayList;
|
||||
|
18
testing-modules/junit-4/pom.xml
Normal file
18
testing-modules/junit-4/pom.xml
Normal file
@ -0,0 +1,18 @@
|
||||
<?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>junit-4</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>junit-4</name>
|
||||
<description>JUnit 4 Topics</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
</project>
|
13
testing-modules/junit-4/src/main/resources/logback.xml
Normal file
13
testing-modules/junit-4/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -0,0 +1,34 @@
|
||||
package com.baeldung.rules;
|
||||
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class MessageLogger implements TestRule {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(MessageLogger.class);
|
||||
|
||||
private String message;
|
||||
|
||||
public MessageLogger(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Statement apply(final Statement base, Description description) {
|
||||
return new Statement() {
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
try {
|
||||
LOG.info("Starting: {}", message);
|
||||
base.evaluate();
|
||||
} finally {
|
||||
LOG.info("Finished: {}", message);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.baeldung.rules;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.RuleChain;
|
||||
|
||||
public class RuleChainUnitTest {
|
||||
|
||||
@Rule
|
||||
public RuleChain chain = RuleChain.outerRule(new MessageLogger("First rule"))
|
||||
.around(new MessageLogger("Second rule"))
|
||||
.around(new MessageLogger("Third rule"));
|
||||
|
||||
@Test
|
||||
public void givenRuleChain_whenTestRuns_thenChainOrderApplied() {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
package com.baeldung.rules;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.isA;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.DisableOnDebug;
|
||||
import org.junit.rules.ErrorCollector;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.rules.TestName;
|
||||
import org.junit.rules.Timeout;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class RulesUnitTest {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(RulesUnitTest.class);
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder tmpFolder = new TemporaryFolder();
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Rule
|
||||
public TestName name = new TestName();
|
||||
|
||||
@Rule
|
||||
public Timeout globalTimeout = Timeout.seconds(10);
|
||||
|
||||
@Rule
|
||||
public final ErrorCollector errorCollector = new ErrorCollector();
|
||||
|
||||
@Rule
|
||||
public DisableOnDebug disableTimeout = new DisableOnDebug(Timeout.seconds(30));
|
||||
|
||||
@Rule
|
||||
public TestMethodNameLogger testLogger = new TestMethodNameLogger();
|
||||
|
||||
@Test
|
||||
public void givenTempFolderRule_whenNewFile_thenFileIsCreated() throws IOException {
|
||||
File testFile = tmpFolder.newFile("test-file.txt");
|
||||
|
||||
assertTrue("The file should have been created: ", testFile.isFile());
|
||||
assertEquals("Temp folder and test file should match: ", tmpFolder.getRoot(), testFile.getParentFile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIllegalArgument_whenExceptionThrown_thenMessageAndCauseMatches() {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectCause(isA(NullPointerException.class));
|
||||
thrown.expectMessage("This is illegal");
|
||||
|
||||
throw new IllegalArgumentException("This is illegal", new NullPointerException());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAddition_whenPrintingTestName_thenTestNameIsDisplayed() {
|
||||
LOG.info("Executing: {}", name.getMethodName());
|
||||
assertEquals("givenAddition_whenPrintingTestName_thenTestNameIsDisplayed", name.getMethodName());
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void givenLongRunningTest_whenTimout_thenTestFails() throws InterruptedException {
|
||||
TimeUnit.SECONDS.sleep(20);
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void givenMultipleErrors_whenTestRuns_thenCollectorReportsErrors() {
|
||||
errorCollector.addError(new Throwable("First thing went wrong!"));
|
||||
errorCollector.addError(new Throwable("Another thing went wrong!"));
|
||||
|
||||
errorCollector.checkThat("Hello World", not(containsString("ERROR!")));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.baeldung.rules;
|
||||
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class TestMethodNameLogger implements TestRule {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(TestMethodNameLogger.class);
|
||||
|
||||
@Override
|
||||
public Statement apply(Statement base, Description description) {
|
||||
logInfo("Before test", description);
|
||||
try {
|
||||
return new Statement() {
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
base.evaluate();
|
||||
}
|
||||
};
|
||||
} finally {
|
||||
logInfo("After test", description);
|
||||
}
|
||||
}
|
||||
|
||||
private void logInfo(String msg, Description description) {
|
||||
LOG.info(msg + description.getMethodName());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.baeldung.rules;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.Verifier;
|
||||
|
||||
public class VerifierRuleUnitTest {
|
||||
|
||||
private List<String> messageLog = new ArrayList<String>();
|
||||
|
||||
@Rule
|
||||
public Verifier verifier = new Verifier() {
|
||||
@Override
|
||||
public void verify() {
|
||||
assertFalse("Message Log is not Empty!", messageLog.isEmpty());
|
||||
}
|
||||
};
|
||||
|
||||
@Test
|
||||
public void givenNewMessage_whenVerified_thenMessageLogNotEmpty() {
|
||||
// ...
|
||||
messageLog.add("There is a new message!");
|
||||
}
|
||||
|
||||
}
|
@ -37,5 +37,6 @@
|
||||
<module>easymock</module>
|
||||
<module>junit-5-advanced</module>
|
||||
<module>xmlunit-2</module>
|
||||
<module>junit-4</module>
|
||||
</modules>
|
||||
</project>
|
||||
|
Loading…
x
Reference in New Issue
Block a user