diff --git a/.gitignore b/.gitignore index 08f570ad06..693a35c176 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,4 @@ spring-call-getters-using-reflection/.mvn/wrapper/maven-wrapper.properties spring-check-if-a-property-is-null/.mvn/wrapper/maven-wrapper.properties *.springBeans +20171220-JMeter.csv diff --git a/activejdbc/pom.xml b/activejdbc/pom.xml new file mode 100644 index 0000000000..7a49d37411 --- /dev/null +++ b/activejdbc/pom.xml @@ -0,0 +1,129 @@ + + 4.0.0 + com.baeldung + activejdbc + 1.0-SNAPSHOT + jar + activejdbc + http://maven.apache.org + + UTF-8 + 1.4.13 + development.test,development + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.0 + + 1.8 + 1.8 + UTF-8 + + + + org.javalite + activejdbc-instrumentation + ${activejdbc.version} + + + process-classes + + instrument + + + + + + org.javalite + db-migrator-maven-plugin + ${activejdbc.version} + + ${project.basedir}/src/main/resources/database.properties + ${environments} + + + + mysql + mysql-connector-java + 5.1.34 + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.18.1 + + brief + true + false + + **/*Spec*.java + **/*Test*.java + + + **/helpers/* + **/*$* + + + + + + + + junit + junit + 4.12 + test + + + org.javalite + activejdbc + ${activejdbc.version} + + + opensymphony + oscache + + + + + mysql + mysql-connector-java + 5.1.34 + + + org.slf4j + slf4j-simple + 1.7.9 + + + + + snapshots1 + JavaLite Snapshots1 + http://repo.javalite.io/ + + true + always + warn + + + + + + snapshots2 + JavaLite Snapshots2 + http://repo.javalite.io/ + + true + always + warn + + + + diff --git a/activejdbc/src/main/java/com/baeldung/ActiveJDBCApp.java b/activejdbc/src/main/java/com/baeldung/ActiveJDBCApp.java new file mode 100644 index 0000000000..8906d3e759 --- /dev/null +++ b/activejdbc/src/main/java/com/baeldung/ActiveJDBCApp.java @@ -0,0 +1,61 @@ +package com.baeldung; + + +import com.baeldung.model.Employee; +import com.baeldung.model.Role; +import org.javalite.activejdbc.Base; +import org.javalite.activejdbc.LazyList; +import org.javalite.activejdbc.Model; + +public class ActiveJDBCApp +{ + public static void main( String[] args ) + { + try { + Base.open(); + ActiveJDBCApp app = new ActiveJDBCApp(); + app.create(); + app.update(); + app.delete(); + app.deleteCascade(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + Base.close(); + } + } + + protected void create() { + Employee employee = new Employee("Hugo","C","M","BN"); + employee.saveIt(); + employee.add(new Role("Java Developer","BN")); + LazyList all = Employee.findAll(); + System.out.println(all.size()); + } + + protected void update() { + Employee employee = Employee.findFirst("first_name = ?","Hugo"); + employee.set("last_namea","Choi").saveIt(); + employee = Employee.findFirst("last_name = ?","Choi"); + System.out.println(employee.getString("first_name") + " " + employee.getString("last_name")); + } + + protected void delete() { + Employee employee = Employee.findFirst("first_name = ?","Hugo"); + employee.delete(); + employee = Employee.findFirst("last_name = ?","Choi"); + if(null == employee){ + System.out.println("No such Employee found!"); + } + } + + protected void deleteCascade() { + create(); + Employee employee = Employee.findFirst("first_name = ?","Hugo"); + employee.deleteCascade(); + employee = Employee.findFirst("last_name = ?","C"); + if(null == employee){ + System.out.println("No such Employee found!"); + } + } +} diff --git a/activejdbc/src/main/java/com/baeldung/model/Employee.java b/activejdbc/src/main/java/com/baeldung/model/Employee.java new file mode 100644 index 0000000000..b7fa8aaf1f --- /dev/null +++ b/activejdbc/src/main/java/com/baeldung/model/Employee.java @@ -0,0 +1,19 @@ +package com.baeldung.model; + + +import org.javalite.activejdbc.Model; + +public class Employee extends Model { + + public Employee(){ + + } + + public Employee(String firstName, String lastName, String gender, String createdBy) { + set("first_name1",firstName); + set("last_name",lastName); + set("gender",gender); + set("created_by",createdBy); + } + +} diff --git a/activejdbc/src/main/java/com/baeldung/model/Role.java b/activejdbc/src/main/java/com/baeldung/model/Role.java new file mode 100644 index 0000000000..3f425dbe6b --- /dev/null +++ b/activejdbc/src/main/java/com/baeldung/model/Role.java @@ -0,0 +1,18 @@ +package com.baeldung.model; + + +import org.javalite.activejdbc.Model; +import org.javalite.activejdbc.annotations.Table; + +@Table("EMP_ROLES") +public class Role extends Model { + + public Role(){ + + } + + public Role(String role,String createdBy){ + set("role_name",role); + set("created_by",createdBy); + } +} diff --git a/activejdbc/src/main/migration/_create_tables.sql b/activejdbc/src/main/migration/_create_tables.sql new file mode 100644 index 0000000000..19fc1e72d7 --- /dev/null +++ b/activejdbc/src/main/migration/_create_tables.sql @@ -0,0 +1,25 @@ +# noinspection SqlNoDataSourceInspectionForFile + +create table organisation.employees +( + id int not null auto_increment + primary key, + first_name varchar(100) not null, + last_name varchar(100) not null, + gender varchar(1) not null, + created_at datetime not null, + updated_at datetime null, + created_by varchar(100) not null, + updated_by varchar(100) null +)ENGINE = InnoDB DEFAULT CHARSET = utf8; + +create table organisation.emp_roles +( + id int not null auto_increment primary key, + employee_id int not null, + role_name varchar(100) not null, + created_at datetime not null, + updated_at datetime null, + created_by varchar(100) not null, + updated_by varchar(100) null +)ENGINE = InnoDB DEFAULT CHARSET = utf8; diff --git a/activejdbc/src/main/resources/database.properties b/activejdbc/src/main/resources/database.properties new file mode 100644 index 0000000000..7e665fe8a1 --- /dev/null +++ b/activejdbc/src/main/resources/database.properties @@ -0,0 +1,10 @@ +development.driver=com.mysql.jdbc.Driver +development.username=root +development.password=123456 +development.url=jdbc:mysql://localhost/organisation + + +development.test.driver=com.mysql.jdbc.Driver +development.test.username=root +development.test.password=123456 +development.test.url=jdbc:mysql://localhost/organisation_test \ No newline at end of file diff --git a/activejdbc/src/test/java/com/baeldung/ActiveJDBCAppTest.java b/activejdbc/src/test/java/com/baeldung/ActiveJDBCAppTest.java new file mode 100644 index 0000000000..316dc34712 --- /dev/null +++ b/activejdbc/src/test/java/com/baeldung/ActiveJDBCAppTest.java @@ -0,0 +1,51 @@ +package com.baeldung; + +import com.baeldung.model.Employee; +import com.baeldung.model.Role; +import org.javalite.activejdbc.test.DBSpec; +import org.junit.Test; + +import java.util.List; + +public class ActiveJDBCAppTest extends DBSpec +{ + @Test + public void ifEmployeeCreated_thenIsValid() { + Employee employee = new Employee("B", "N", "M", "BN"); + the(employee).shouldBe("valid"); + } + + @Test + public void ifEmployeeCreatedWithRoles_thenShouldPersist() { + Employee employee = new Employee("B", "N", "M", "BN"); + employee.saveIt(); + employee.add(new Role("Java Developer","BN")); + employee.add(new Role("Lead Java Developer","BN")); + a(Role.count()).shouldBeEqual(2); + List roles = employee.getAll(Role.class).orderBy("created_at"); + the(roles.get(0).getRoleName()).shouldBeEqual("Java Developer"); + the(roles.get(1).getRoleName()).shouldBeEqual("Lead Java Developer"); + } + + @Test + public void ifEmployeeCreatedWithRoles_whenNameUpdated_thenShouldShowNewName() { + Employee employee = new Employee("Binesh", "N", "M", "BN"); + employee.saveIt(); + employee.add(new Role("Java Developer","BN")); + employee.add(new Role("Lead Java Developer","BN")); + employee = Employee.findFirst("first_name = ?", "Binesh"); + employee.set("last_name","Narayanan").saveIt(); + Employee updated = Employee.findFirst("first_name = ?", "Binesh"); + the(updated.getLastName()).shouldBeEqual("Narayanan"); + } + + @Test + public void ifEmployeeCreatedWithRoles_whenDeleted_thenShouldNotBeFound() { + Employee employee = new Employee("Binesh", "N", "M", "BN"); + employee.saveIt(); + employee.add(new Role("Java Developer","BN")); + employee.delete(); + employee = Employee.findFirst("first_name = ?", "Binesh"); + the(employee).shouldBeNull(); + } +} diff --git a/algorithms/README.md b/algorithms/README.md index 3401b6d935..31cb8076d9 100644 --- a/algorithms/README.md +++ b/algorithms/README.md @@ -16,3 +16,4 @@ - [Introduction to Minimax Algorithm](http://www.baeldung.com/java-minimax-algorithm) - [How to Calculate Levenshtein Distance in Java?](http://www.baeldung.com/java-levenshtein-distance) - [How to Find the Kth Largest Element in Java](http://www.baeldung.com/java-kth-largest-element) +- [Multi-Swarm Optimization Algorithm in Java](http://www.baeldung.com/java-multi-swarm-algorithm) diff --git a/algorithms/pom.xml b/algorithms/pom.xml index 2eb8cd42b6..8751cf45c0 100644 --- a/algorithms/pom.xml +++ b/algorithms/pom.xml @@ -9,6 +9,7 @@ 1.5.0 1.16.12 3.6.1 + 1.0.1 @@ -39,6 +40,11 @@ jgrapht-core 1.0.1 + + pl.allegro.finance + tradukisto + ${tradukisto.version} + org.assertj assertj-core @@ -46,7 +52,6 @@ test - @@ -77,4 +82,4 @@ - + \ No newline at end of file diff --git a/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/BFSMazeSolver.java b/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/BFSMazeSolver.java new file mode 100644 index 0000000000..08972251b8 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/BFSMazeSolver.java @@ -0,0 +1,52 @@ +package com.baeldung.algorithms.maze.solver; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public class BFSMazeSolver { + private static final int[][] DIRECTIONS = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; + + public List solve(Maze maze) { + LinkedList nextToVisit = new LinkedList<>(); + Coordinate start = maze.getEntry(); + nextToVisit.add(start); + + while (!nextToVisit.isEmpty()) { + Coordinate cur = nextToVisit.remove(); + + if (!maze.isValidLocation(cur.getX(), cur.getY()) || maze.isExplored(cur.getX(), cur.getY())) { + continue; + } + + if (maze.isWall(cur.getX(), cur.getY())) { + maze.setVisited(cur.getX(), cur.getY(), true); + continue; + } + + if (maze.isExit(cur.getX(), cur.getY())) { + return backtrackPath(cur); + } + + for (int[] direction : DIRECTIONS) { + Coordinate coordinate = new Coordinate(cur.getX() + direction[0], cur.getY() + direction[1], cur); + nextToVisit.add(coordinate); + maze.setVisited(cur.getX(), cur.getY(), true); + } + } + return Collections.emptyList(); + } + + private List backtrackPath(Coordinate cur) { + List path = new ArrayList<>(); + Coordinate iter = cur; + + while (iter != null) { + path.add(iter); + iter = iter.parent; + } + + return path; + } +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/Coordinate.java b/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/Coordinate.java new file mode 100644 index 0000000000..09b2ced5e6 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/Coordinate.java @@ -0,0 +1,31 @@ +package com.baeldung.algorithms.maze.solver; + +public class Coordinate { + int x; + int y; + Coordinate parent; + + public Coordinate(int x, int y) { + this.x = x; + this.y = y; + this.parent = null; + } + + public Coordinate(int x, int y, Coordinate parent) { + this.x = x; + this.y = y; + this.parent = parent; + } + + int getX() { + return x; + } + + int getY() { + return y; + } + + Coordinate getParent() { + return parent; + } +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/DFSMazeSolver.java b/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/DFSMazeSolver.java new file mode 100644 index 0000000000..f9640066b9 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/DFSMazeSolver.java @@ -0,0 +1,48 @@ +package com.baeldung.algorithms.maze.solver; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class DFSMazeSolver { + private static final int[][] DIRECTIONS = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; + + public List solve(Maze maze) { + List path = new ArrayList<>(); + if (explore(maze, maze.getEntry() + .getX(), + maze.getEntry() + .getY(), + path)) { + return path; + } + return Collections.emptyList(); + } + + private boolean explore(Maze maze, int row, int col, List path) { + if (!maze.isValidLocation(row, col) || maze.isWall(row, col) || maze.isExplored(row, col)) { + return false; + } + + path.add(new Coordinate(row, col)); + maze.setVisited(row, col, true); + + if (maze.isExit(row, col)) { + return true; + } + + for (int[] direction : DIRECTIONS) { + Coordinate coordinate = getNextCoordinate(row, col, direction[0], direction[1]); + if (explore(maze, coordinate.getX(), coordinate.getY(), path)) { + return true; + } + } + + path.remove(path.size() - 1); + return false; + } + + private Coordinate getNextCoordinate(int row, int col, int i, int j) { + return new Coordinate(row + i, col + j); + } +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/Maze.java b/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/Maze.java new file mode 100644 index 0000000000..8aaa44d9b1 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/Maze.java @@ -0,0 +1,141 @@ +package com.baeldung.algorithms.maze.solver; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; + +public class Maze { + private static final int ROAD = 0; + private static final int WALL = 1; + private static final int START = 2; + private static final int EXIT = 3; + private static final int PATH = 4; + + private int[][] maze; + private boolean[][] visited; + private Coordinate start; + private Coordinate end; + + public Maze(File maze) throws FileNotFoundException { + String fileText = ""; + try (Scanner input = new Scanner(maze)) { + while (input.hasNextLine()) { + fileText += input.nextLine() + "\n"; + } + } + initializeMaze(fileText); + } + + private void initializeMaze(String text) { + if (text == null || (text = text.trim()).length() == 0) { + throw new IllegalArgumentException("empty lines data"); + } + + String[] lines = text.split("[\r]?\n"); + maze = new int[lines.length][lines[0].length()]; + visited = new boolean[lines.length][lines[0].length()]; + + for (int row = 0; row < getHeight(); row++) { + if (lines[row].length() != getWidth()) { + throw new IllegalArgumentException("line " + (row + 1) + " wrong length (was " + lines[row].length() + " but should be " + getWidth() + ")"); + } + + for (int col = 0; col < getWidth(); col++) { + if (lines[row].charAt(col) == '#') + maze[row][col] = WALL; + else if (lines[row].charAt(col) == 'S') { + maze[row][col] = START; + start = new Coordinate(row, col); + } else if (lines[row].charAt(col) == 'E') { + maze[row][col] = EXIT; + end = new Coordinate(row, col); + } else + maze[row][col] = ROAD; + } + } + } + + public int getHeight() { + return maze.length; + } + + public int getWidth() { + return maze[0].length; + } + + public Coordinate getEntry() { + return start; + } + + public Coordinate getExit() { + return end; + } + + public boolean isExit(int x, int y) { + return x == end.getX() && y == end.getY(); + } + + public boolean isStart(int x, int y) { + return x == start.getX() && y == start.getY(); + } + + public boolean isExplored(int row, int col) { + return visited[row][col]; + } + + public boolean isWall(int row, int col) { + return maze[row][col] == WALL; + } + + public void setVisited(int row, int col, boolean value) { + visited[row][col] = value; + } + + public boolean isValidLocation(int row, int col) { + if (row < 0 || row >= getHeight() || col < 0 || col >= getWidth()) { + return false; + } + return true; + } + + public void printPath(List path) { + int[][] tempMaze = Arrays.stream(maze) + .map(int[]::clone) + .toArray(int[][]::new); + for (Coordinate coordinate : path) { + if (isStart(coordinate.getX(), coordinate.getY()) || isExit(coordinate.getX(), coordinate.getY())) { + continue; + } + tempMaze[coordinate.getX()][coordinate.getY()] = PATH; + } + System.out.println(toString(tempMaze)); + } + + public String toString(int[][] maze) { + StringBuilder result = new StringBuilder(getWidth() * (getHeight() + 1)); + for (int row = 0; row < getHeight(); row++) { + for (int col = 0; col < getWidth(); col++) { + if (maze[row][col] == ROAD) { + result.append(' '); + } else if (maze[row][col] == WALL) { + result.append('#'); + } else if (maze[row][col] == START) { + result.append('S'); + } else if (maze[row][col] == EXIT) { + result.append('E'); + } else { + result.append('.'); + } + } + result.append('\n'); + } + return result.toString(); + } + + public void reset() { + for (int i = 0; i < visited.length; i++) + Arrays.fill(visited[i], false); + } +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/MazeDriver.java b/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/MazeDriver.java new file mode 100644 index 0000000000..60263deba3 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/MazeDriver.java @@ -0,0 +1,34 @@ +package com.baeldung.algorithms.maze.solver; + +import java.io.File; +import java.util.List; + +public class MazeDriver { + public static void main(String[] args) throws Exception { + File maze1 = new File("src/main/resources/maze/maze1.txt"); + File maze2 = new File("src/main/resources/maze/maze2.txt"); + + execute(maze1); + execute(maze2); + } + + private static void execute(File file) throws Exception { + Maze maze = new Maze(file); + dfs(maze); + bfs(maze); + } + + private static void bfs(Maze maze) { + BFSMazeSolver bfs = new BFSMazeSolver(); + List path = bfs.solve(maze); + maze.printPath(path); + maze.reset(); + } + + private static void dfs(Maze maze) { + DFSMazeSolver dfs = new DFSMazeSolver(); + List path = dfs.solve(maze); + maze.printPath(path); + maze.reset(); + } +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Constants.java b/algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Constants.java new file mode 100644 index 0000000000..b646c686b2 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Constants.java @@ -0,0 +1,47 @@ +package com.baeldung.algorithms.multiswarm; + +/** + * Constants used by the Multi-swarm optimization algorithms. + * + * @author Donato Rimenti + * + */ +public class Constants { + + /** + * The inertia factor encourages a particle to continue moving in its + * current direction. + */ + public static final double INERTIA_FACTOR = 0.729; + + /** + * The cognitive weight encourages a particle to move toward its historical + * best-known position. + */ + public static final double COGNITIVE_WEIGHT = 1.49445; + + /** + * The social weight encourages a particle to move toward the best-known + * position found by any of the particle’s swarm-mates. + */ + public static final double SOCIAL_WEIGHT = 1.49445; + + /** + * The global weight encourages a particle to move toward the best-known + * position found by any particle in any swarm. + */ + public static final double GLOBAL_WEIGHT = 0.3645; + + /** + * Upper bound for the random generation. We use it to reduce the + * computation time since we can rawly estimate it. + */ + public static final int PARTICLE_UPPER_BOUND = 10000000; + + /** + * Private constructor for utility class. + */ + private Constants() { + } + +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/multiswarm/FitnessFunction.java b/algorithms/src/main/java/com/baeldung/algorithms/multiswarm/FitnessFunction.java new file mode 100644 index 0000000000..2d86ec8d94 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/multiswarm/FitnessFunction.java @@ -0,0 +1,21 @@ +package com.baeldung.algorithms.multiswarm; + +/** + * Interface for a fitness function, used to decouple the main algorithm logic + * from the specific problem solution. + * + * @author Donato Rimenti + * + */ +public interface FitnessFunction { + + /** + * Returns the fitness of a particle given its position. + * + * @param particlePosition + * the position of the particle + * @return the fitness of the particle + */ + public double getFitness(long[] particlePosition); + +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Multiswarm.java b/algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Multiswarm.java new file mode 100644 index 0000000000..ef60726278 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Multiswarm.java @@ -0,0 +1,227 @@ +package com.baeldung.algorithms.multiswarm; + +import java.util.Arrays; +import java.util.Random; + +/** + * Represents a collection of {@link Swarm}. + * + * @author Donato Rimenti + * + */ +public class Multiswarm { + + /** + * The swarms managed by this multiswarm. + */ + private Swarm[] swarms; + + /** + * The best position found within all the {@link #swarms}. + */ + private long[] bestPosition; + + /** + * The best fitness score found within all the {@link #swarms}. + */ + private double bestFitness = Double.NEGATIVE_INFINITY; + + /** + * A random generator. + */ + private Random random = new Random(); + + /** + * The fitness function used to determine how good is a particle. + */ + private FitnessFunction fitnessFunction; + + /** + * Instantiates a new Multiswarm. + * + * @param numSwarms + * the number of {@link #swarms} + * @param particlesPerSwarm + * the number of particle for each {@link #swarms} + * @param fitnessFunction + * the {@link #fitnessFunction} + */ + public Multiswarm(int numSwarms, int particlesPerSwarm, FitnessFunction fitnessFunction) { + this.fitnessFunction = fitnessFunction; + this.swarms = new Swarm[numSwarms]; + for (int i = 0; i < numSwarms; i++) { + swarms[i] = new Swarm(particlesPerSwarm); + } + } + + /** + * Main loop of the algorithm. Iterates all particles of all + * {@link #swarms}. For each particle, computes the new fitness and checks + * if a new best position has been found among itself, the swarm and all the + * swarms and finally updates the particle position and speed. + */ + public void mainLoop() { + for (Swarm swarm : swarms) { + for (Particle particle : swarm.getParticles()) { + + long[] particleOldPosition = particle.getPosition().clone(); + + // Calculate the particle fitness. + particle.setFitness(fitnessFunction.getFitness(particleOldPosition)); + + // Check if a new best position has been found for the particle + // itself, within the swarm and the multiswarm. + if (particle.getFitness() > particle.getBestFitness()) { + particle.setBestFitness(particle.getFitness()); + particle.setBestPosition(particleOldPosition); + + if (particle.getFitness() > swarm.getBestFitness()) { + swarm.setBestFitness(particle.getFitness()); + swarm.setBestPosition(particleOldPosition); + + if (swarm.getBestFitness() > bestFitness) { + bestFitness = swarm.getBestFitness(); + bestPosition = swarm.getBestPosition().clone(); + } + + } + } + + // Updates the particle position by adding the speed to the + // actual position. + long[] position = particle.getPosition(); + long[] speed = particle.getSpeed(); + + position[0] += speed[0]; + position[1] += speed[1]; + + // Updates the particle speed. + speed[0] = getNewParticleSpeedForIndex(particle, swarm, 0); + speed[1] = getNewParticleSpeedForIndex(particle, swarm, 1); + } + } + } + + /** + * Computes a new speed for a given particle of a given swarm on a given + * axis. The new speed is computed using the formula: + * + *
+	 * ({@link Constants#INERTIA_FACTOR} * {@link Particle#getSpeed()}) + 
+	 * (({@link Constants#COGNITIVE_WEIGHT} * random(0,1)) * ({@link Particle#getBestPosition()} - {@link Particle#getPosition()})) +
+	 * (({@link Constants#SOCIAL_WEIGHT} * random(0,1)) * ({@link Swarm#getBestPosition()} - {@link Particle#getPosition()})) + 
+	 * (({@link Constants#GLOBAL_WEIGHT} * random(0,1)) * ({@link #bestPosition} - {@link Particle#getPosition()}))
+	 * 
+ * + * @param particle + * the particle whose new speed needs to be computed + * @param swarm + * the swarm which contains the particle + * @param index + * the index of the particle axis whose speeds needs to be + * computed + * @return the new speed of the particle passed on the given axis + */ + private int getNewParticleSpeedForIndex(Particle particle, Swarm swarm, int index) { + return (int) ((Constants.INERTIA_FACTOR * particle.getSpeed()[index]) + + (randomizePercentage(Constants.COGNITIVE_WEIGHT) + * (particle.getBestPosition()[index] - particle.getPosition()[index])) + + (randomizePercentage(Constants.SOCIAL_WEIGHT) + * (swarm.getBestPosition()[index] - particle.getPosition()[index])) + + (randomizePercentage(Constants.GLOBAL_WEIGHT) + * (bestPosition[index] - particle.getPosition()[index]))); + } + + /** + * Returns a random number between 0 and the value passed as argument. + * + * @param value + * the value to randomize + * @return a random value between 0 and the one passed as argument + */ + private double randomizePercentage(double value) { + return random.nextDouble() * value; + } + + /** + * Gets the {@link #bestPosition}. + * + * @return the {@link #bestPosition} + */ + public long[] getBestPosition() { + return bestPosition; + } + + /** + * Gets the {@link #bestFitness}. + * + * @return the {@link #bestFitness} + */ + public double getBestFitness() { + return bestFitness; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + long temp; + temp = Double.doubleToLongBits(bestFitness); + result = prime * result + (int) (temp ^ (temp >>> 32)); + result = prime * result + Arrays.hashCode(bestPosition); + result = prime * result + ((fitnessFunction == null) ? 0 : fitnessFunction.hashCode()); + result = prime * result + ((random == null) ? 0 : random.hashCode()); + result = prime * result + Arrays.hashCode(swarms); + return result; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Multiswarm other = (Multiswarm) obj; + if (Double.doubleToLongBits(bestFitness) != Double.doubleToLongBits(other.bestFitness)) + return false; + if (!Arrays.equals(bestPosition, other.bestPosition)) + return false; + if (fitnessFunction == null) { + if (other.fitnessFunction != null) + return false; + } else if (!fitnessFunction.equals(other.fitnessFunction)) + return false; + if (random == null) { + if (other.random != null) + return false; + } else if (!random.equals(other.random)) + return false; + if (!Arrays.equals(swarms, other.swarms)) + return false; + return true; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "Multiswarm [swarms=" + Arrays.toString(swarms) + ", bestPosition=" + Arrays.toString(bestPosition) + + ", bestFitness=" + bestFitness + ", random=" + random + ", fitnessFunction=" + fitnessFunction + "]"; + } + +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Particle.java b/algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Particle.java new file mode 100644 index 0000000000..5930a94267 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Particle.java @@ -0,0 +1,204 @@ +package com.baeldung.algorithms.multiswarm; + +import java.util.Arrays; + +/** + * Represents a particle, the basic component of a {@link Swarm}. + * + * @author Donato Rimenti + * + */ +public class Particle { + + /** + * The current position of this particle. + */ + private long[] position; + + /** + * The speed of this particle. + */ + private long[] speed; + + /** + * The fitness of this particle for the current position. + */ + private double fitness; + + /** + * The best position found by this particle. + */ + private long[] bestPosition; + + /** + * The best fitness found by this particle. + */ + private double bestFitness = Double.NEGATIVE_INFINITY; + + /** + * Instantiates a new Particle. + * + * @param initialPosition + * the initial {@link #position} + * @param initialSpeed + * the initial {@link #speed} + */ + public Particle(long[] initialPosition, long[] initialSpeed) { + this.position = initialPosition; + this.speed = initialSpeed; + } + + /** + * Gets the {@link #position}. + * + * @return the {@link #position} + */ + public long[] getPosition() { + return position; + } + + /** + * Gets the {@link #speed}. + * + * @return the {@link #speed} + */ + public long[] getSpeed() { + return speed; + } + + /** + * Gets the {@link #fitness}. + * + * @return the {@link #fitness} + */ + public double getFitness() { + return fitness; + } + + /** + * Gets the {@link #bestPosition}. + * + * @return the {@link #bestPosition} + */ + public long[] getBestPosition() { + return bestPosition; + } + + /** + * Gets the {@link #bestFitness}. + * + * @return the {@link #bestFitness} + */ + public double getBestFitness() { + return bestFitness; + } + + /** + * Sets the {@link #position}. + * + * @param position + * the new {@link #position} + */ + public void setPosition(long[] position) { + this.position = position; + } + + /** + * Sets the {@link #speed}. + * + * @param speed + * the new {@link #speed} + */ + public void setSpeed(long[] speed) { + this.speed = speed; + } + + /** + * Sets the {@link #fitness}. + * + * @param fitness + * the new {@link #fitness} + */ + public void setFitness(double fitness) { + this.fitness = fitness; + } + + /** + * Sets the {@link #bestPosition}. + * + * @param bestPosition + * the new {@link #bestPosition} + */ + public void setBestPosition(long[] bestPosition) { + this.bestPosition = bestPosition; + } + + /** + * Sets the {@link #bestFitness}. + * + * @param bestFitness + * the new {@link #bestFitness} + */ + public void setBestFitness(double bestFitness) { + this.bestFitness = bestFitness; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + long temp; + temp = Double.doubleToLongBits(bestFitness); + result = prime * result + (int) (temp ^ (temp >>> 32)); + result = prime * result + Arrays.hashCode(bestPosition); + temp = Double.doubleToLongBits(fitness); + result = prime * result + (int) (temp ^ (temp >>> 32)); + result = prime * result + Arrays.hashCode(position); + result = prime * result + Arrays.hashCode(speed); + return result; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Particle other = (Particle) obj; + if (Double.doubleToLongBits(bestFitness) != Double.doubleToLongBits(other.bestFitness)) + return false; + if (!Arrays.equals(bestPosition, other.bestPosition)) + return false; + if (Double.doubleToLongBits(fitness) != Double.doubleToLongBits(other.fitness)) + return false; + if (!Arrays.equals(position, other.position)) + return false; + if (!Arrays.equals(speed, other.speed)) + return false; + return true; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "Particle [position=" + Arrays.toString(position) + ", speed=" + Arrays.toString(speed) + ", fitness=" + + fitness + ", bestPosition=" + Arrays.toString(bestPosition) + ", bestFitness=" + bestFitness + "]"; + } + +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Swarm.java b/algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Swarm.java new file mode 100644 index 0000000000..e6d37bb7e6 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Swarm.java @@ -0,0 +1,155 @@ +package com.baeldung.algorithms.multiswarm; + +import java.util.Arrays; +import java.util.Random; + +/** + * Represents a collection of {@link Particle}. + * + * @author Donato Rimenti + * + */ +public class Swarm { + + /** + * The particles of this swarm. + */ + private Particle[] particles; + + /** + * The best position found within the particles of this swarm. + */ + private long[] bestPosition; + + /** + * The best fitness score found within the particles of this swarm. + */ + private double bestFitness = Double.NEGATIVE_INFINITY; + + /** + * A random generator. + */ + private Random random = new Random(); + + /** + * Instantiates a new Swarm. + * + * @param numParticles + * the number of particles of the swarm + */ + public Swarm(int numParticles) { + particles = new Particle[numParticles]; + for (int i = 0; i < numParticles; i++) { + long[] initialParticlePosition = { random.nextInt(Constants.PARTICLE_UPPER_BOUND), + random.nextInt(Constants.PARTICLE_UPPER_BOUND) }; + long[] initialParticleSpeed = { random.nextInt(Constants.PARTICLE_UPPER_BOUND), + random.nextInt(Constants.PARTICLE_UPPER_BOUND) }; + particles[i] = new Particle(initialParticlePosition, initialParticleSpeed); + } + } + + /** + * Gets the {@link #particles}. + * + * @return the {@link #particles} + */ + public Particle[] getParticles() { + return particles; + } + + /** + * Gets the {@link #bestPosition}. + * + * @return the {@link #bestPosition} + */ + public long[] getBestPosition() { + return bestPosition; + } + + /** + * Gets the {@link #bestFitness}. + * + * @return the {@link #bestFitness} + */ + public double getBestFitness() { + return bestFitness; + } + + /** + * Sets the {@link #bestPosition}. + * + * @param bestPosition + * the new {@link #bestPosition} + */ + public void setBestPosition(long[] bestPosition) { + this.bestPosition = bestPosition; + } + + /** + * Sets the {@link #bestFitness}. + * + * @param bestFitness + * the new {@link #bestFitness} + */ + public void setBestFitness(double bestFitness) { + this.bestFitness = bestFitness; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + long temp; + temp = Double.doubleToLongBits(bestFitness); + result = prime * result + (int) (temp ^ (temp >>> 32)); + result = prime * result + Arrays.hashCode(bestPosition); + result = prime * result + Arrays.hashCode(particles); + result = prime * result + ((random == null) ? 0 : random.hashCode()); + return result; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Swarm other = (Swarm) obj; + if (Double.doubleToLongBits(bestFitness) != Double.doubleToLongBits(other.bestFitness)) + return false; + if (!Arrays.equals(bestPosition, other.bestPosition)) + return false; + if (!Arrays.equals(particles, other.particles)) + return false; + if (random == null) { + if (other.random != null) + return false; + } else if (!random.equals(other.random)) + return false; + return true; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "Swarm [particles=" + Arrays.toString(particles) + ", bestPosition=" + Arrays.toString(bestPosition) + + ", bestFitness=" + bestFitness + ", random=" + random + "]"; + } + +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/numberwordconverter/NumberWordConverter.java b/algorithms/src/main/java/com/baeldung/algorithms/numberwordconverter/NumberWordConverter.java new file mode 100644 index 0000000000..0fe2960f96 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/numberwordconverter/NumberWordConverter.java @@ -0,0 +1,75 @@ +package com.baeldung.algorithms.numberwordconverter; + +import java.math.BigDecimal; + +import pl.allegro.finance.tradukisto.MoneyConverters; + +public class NumberWordConverter { + + public static final String INVALID_INPUT_GIVEN = "Invalid input given"; + + public static final String[] ones = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }; + + public static final String[] tens = { + "", // 0 + "", // 1 + "twenty", // 2 + "thirty", // 3 + "forty", // 4 + "fifty", // 5 + "sixty", // 6 + "seventy", // 7 + "eighty", // 8 + "ninety" // 9 + }; + + public static String getMoneyIntoWords(String input) { + MoneyConverters converter = MoneyConverters.ENGLISH_BANKING_MONEY_VALUE; + return converter.asWords(new BigDecimal(input)); + } + + public static String getMoneyIntoWords(final double money) { + long dollar = (long) money; + long cents = Math.round((money - dollar) * 100); + if (money == 0D) { + return ""; + } + if (money < 0) { + return INVALID_INPUT_GIVEN; + } + String dollarPart = ""; + if (dollar > 0) { + dollarPart = convert(dollar) + " dollar" + (dollar == 1 ? "" : "s"); + } + String centsPart = ""; + if (cents > 0) { + if (dollarPart.length() > 0) { + centsPart = " and "; + } + centsPart += convert(cents) + " cent" + (cents == 1 ? "" : "s"); + } + return dollarPart + centsPart; + } + + private static String convert(final long n) { + if (n < 0) { + return INVALID_INPUT_GIVEN; + } + if (n < 20) { + return ones[(int) n]; + } + if (n < 100) { + return tens[(int) n / 10] + ((n % 10 != 0) ? " " : "") + ones[(int) n % 10]; + } + if (n < 1000) { + return ones[(int) n / 100] + " hundred" + ((n % 100 != 0) ? " " : "") + convert(n % 100); + } + if (n < 1_000_000) { + return convert(n / 1000) + " thousand" + ((n % 1000 != 0) ? " " : "") + convert(n % 1000); + } + if (n < 1_000_000_000) { + return convert(n / 1_000_000) + " million" + ((n % 1_000_000 != 0) ? " " : "") + convert(n % 1_000_000); + } + return convert(n / 1_000_000_000) + " billion" + ((n % 1_000_000_000 != 0) ? " " : "") + convert(n % 1_000_000_000); + } +} \ No newline at end of file diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java new file mode 100644 index 0000000000..4b37558aab --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java @@ -0,0 +1,104 @@ +package com.baeldung.algorithms.sudoku; + +import java.util.stream.IntStream; + +public class BacktrackingAlgorithm { + + private static final int BOARD_SIZE = 9; + private static final int SUBSECTION_SIZE = 3; + private static final int BOARD_START_INDEX = 0; + + private static final int NO_VALUE = 0; + private static final int MIN_VALUE = 1; + private static final int MAX_VALUE = 9; + + private static int[][] board = { + {8, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 3, 6, 0, 0, 0, 0, 0}, + {0, 7, 0, 0, 9, 0, 2, 0, 0}, + {0, 5, 0, 0, 0, 7, 0, 0, 0}, + {0, 0, 0, 0, 4, 5, 7, 0, 0}, + {0, 0, 0, 1, 0, 0, 0, 3, 0}, + {0, 0, 1, 0, 0, 0, 0, 6, 8}, + {0, 0, 8, 5, 0, 0, 0, 1, 0}, + {0, 9, 0, 0, 0, 0, 4, 0, 0} + }; + + public static void main(String[] args) { + BacktrackingAlgorithm solver = new BacktrackingAlgorithm(); + solver.solve(board); + solver.printBoard(); + } + + private void printBoard() { + for (int row = BOARD_START_INDEX; row < BOARD_SIZE; row++) { + for (int column = BOARD_START_INDEX; column < BOARD_SIZE; column++) { + System.out.print(board[row][column] + " "); + } + System.out.println(); + } + } + + private boolean solve(int[][] board) { + for (int row = BOARD_START_INDEX; row < BOARD_SIZE; row++) { + for (int column = BOARD_START_INDEX; column < BOARD_SIZE; column++) { + if (board[row][column] == NO_VALUE) { + for (int k = MIN_VALUE; k <= MAX_VALUE; k++) { + board[row][column] = k; + if (isValid(board, row, column) && solve(board)) { + return true; + } + board[row][column] = NO_VALUE; + } + return false; + } + } + } + return true; + } + + private boolean isValid(int[][] board, int row, int column) { + return rowConstraint(board, row) && + columnConstraint(board, column) && + subsectionConstraint(board, row, column); + } + + private boolean subsectionConstraint(int[][] board, int row, int column) { + boolean[] constraint = new boolean[BOARD_SIZE]; + int subsectionRowStart = (row / SUBSECTION_SIZE) * SUBSECTION_SIZE; + int subsectionRowEnd = subsectionRowStart + SUBSECTION_SIZE; + + int subsectionColumnStart = (column / SUBSECTION_SIZE) * SUBSECTION_SIZE; + int subsectionColumnEnd = subsectionColumnStart + SUBSECTION_SIZE; + + for (int r = subsectionRowStart; r < subsectionRowEnd; r++) { + for (int c = subsectionColumnStart; c < subsectionColumnEnd; c++) { + if (!checkConstraint(board, r, constraint, c)) return false; + } + } + return true; + } + + private boolean columnConstraint(int[][] board, int column) { + boolean[] constraint = new boolean[BOARD_SIZE]; + return IntStream.range(BOARD_START_INDEX, BOARD_SIZE) + .allMatch(row -> checkConstraint(board, row, constraint, column)); + } + + private boolean rowConstraint(int[][] board, int row) { + boolean[] constraint = new boolean[BOARD_SIZE]; + return IntStream.range(BOARD_START_INDEX, BOARD_SIZE) + .allMatch(column -> checkConstraint(board, row, constraint, column)); + } + + private boolean checkConstraint(int[][] board, int row, boolean[] constraint, int column) { + if (board[row][column] != NO_VALUE) { + if (!constraint[board[row][column] - 1]) { + constraint[board[row][column] - 1] = true; + } else { + return false; + } + } + return true; + } +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/ColumnNode.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/ColumnNode.java new file mode 100644 index 0000000000..46995ca42f --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/ColumnNode.java @@ -0,0 +1,33 @@ +package com.baeldung.algorithms.sudoku; + +class ColumnNode extends DancingNode { + int size; + String name; + + ColumnNode(String n) { + super(); + size = 0; + name = n; + C = this; + } + + void cover() { + unlinkLR(); + for (DancingNode i = this.D; i != this; i = i.D) { + for (DancingNode j = i.R; j != i; j = j.R) { + j.unlinkUD(); + j.C.size--; + } + } + } + + void uncover() { + for (DancingNode i = this.U; i != this; i = i.U) { + for (DancingNode j = i.L; j != i; j = j.L) { + j.C.size++; + j.relinkUD(); + } + } + relinkLR(); + } +} \ No newline at end of file diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinks.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinks.java new file mode 100644 index 0000000000..d3cbb2bd02 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinks.java @@ -0,0 +1,133 @@ +package com.baeldung.algorithms.sudoku; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +public class DancingLinks { + + private ColumnNode header; + private List answer; + + private void search(int k) { + if (header.R == header) { + handleSolution(answer); + } else { + ColumnNode c = selectColumnNodeHeuristic(); + c.cover(); + + for (DancingNode r = c.D; r != c; r = r.D) { + answer.add(r); + + for (DancingNode j = r.R; j != r; j = j.R) { + j.C.cover(); + } + + search(k + 1); + + r = answer.remove(answer.size() - 1); + c = r.C; + + for (DancingNode j = r.L; j != r; j = j.L) { + j.C.uncover(); + } + } + c.uncover(); + } + } + + private ColumnNode selectColumnNodeHeuristic() { + int min = Integer.MAX_VALUE; + ColumnNode ret = null; + for (ColumnNode c = (ColumnNode) header.R; c != header; c = (ColumnNode) c.R) { + if (c.size < min) { + min = c.size; + ret = c; + } + } + return ret; + } + + private ColumnNode makeDLXBoard(boolean[][] grid) { + final int COLS = grid[0].length; + + ColumnNode headerNode = new ColumnNode("header"); + List columnNodes = new ArrayList<>(); + + for (int i = 0; i < COLS; i++) { + ColumnNode n = new ColumnNode(Integer.toString(i)); + columnNodes.add(n); + headerNode = (ColumnNode) headerNode.hookRight(n); + } + headerNode = headerNode.R.C; + + for (boolean[] aGrid : grid) { + DancingNode prev = null; + for (int j = 0; j < COLS; j++) { + if (aGrid[j]) { + ColumnNode col = columnNodes.get(j); + DancingNode newNode = new DancingNode(col); + if (prev == null) + prev = newNode; + col.U.hookDown(newNode); + prev = prev.hookRight(newNode); + col.size++; + } + } + } + + headerNode.size = COLS; + + return headerNode; + } + + DancingLinks(boolean[][] cover) { + header = makeDLXBoard(cover); + } + + public void runSolver() { + answer = new LinkedList<>(); + search(0); + } + + private void handleSolution(List answer) { + int[][] result = parseBoard(answer); + printSolution(result); + } + + private int size = 9; + + private int[][] parseBoard(List answer) { + int[][] result = new int[size][size]; + for (DancingNode n : answer) { + DancingNode rcNode = n; + int min = Integer.parseInt(rcNode.C.name); + for (DancingNode tmp = n.R; tmp != n; tmp = tmp.R) { + int val = Integer.parseInt(tmp.C.name); + if (val < min) { + min = val; + rcNode = tmp; + } + } + int ans1 = Integer.parseInt(rcNode.C.name); + int ans2 = Integer.parseInt(rcNode.R.C.name); + int r = ans1 / size; + int c = ans1 % size; + int num = (ans2 % size) + 1; + result[r][c] = num; + } + return result; + } + + private static void printSolution(int[][] result) { + int size = result.length; + for (int[] aResult : result) { + StringBuilder ret = new StringBuilder(); + for (int j = 0; j < size; j++) { + ret.append(aResult[j]).append(" "); + } + System.out.println(ret); + } + System.out.println(); + } +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java new file mode 100644 index 0000000000..df02ff3d11 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java @@ -0,0 +1,121 @@ +package com.baeldung.algorithms.sudoku; + +import java.util.Arrays; + +public class DancingLinksAlgorithm { + private static final int BOARD_SIZE = 9; + private static final int SUBSECTION_SIZE = 3; + private static final int NO_VALUE = 0; + private static final int CONSTRAINTS = 4; + private static final int MIN_VALUE = 1; + private static final int MAX_VALUE = 9; + private static final int COVER_START_INDEX = 1; + + private static int[][] board = { + {8, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 3, 6, 0, 0, 0, 0, 0}, + {0, 7, 0, 0, 9, 0, 2, 0, 0}, + {0, 5, 0, 0, 0, 7, 0, 0, 0}, + {0, 0, 0, 0, 4, 5, 7, 0, 0}, + {0, 0, 0, 1, 0, 0, 0, 3, 0}, + {0, 0, 1, 0, 0, 0, 0, 6, 8}, + {0, 0, 8, 5, 0, 0, 0, 1, 0}, + {0, 9, 0, 0, 0, 0, 4, 0, 0} + }; + + public static void main(String[] args) { + DancingLinksAlgorithm solver = new DancingLinksAlgorithm(); + solver.solve(board); + } + + private void solve(int[][] board) { + boolean[][] cover = initializeExactCoverBoard(board); + DancingLinks dlx = new DancingLinks(cover); + dlx.runSolver(); + } + + private int getIndex(int row, int column, int num) { + return (row - 1) * BOARD_SIZE * BOARD_SIZE + (column - 1) * BOARD_SIZE + (num - 1); + } + + private boolean[][] createExactCoverBoard() { + boolean[][] coverBoard = new boolean[BOARD_SIZE * BOARD_SIZE * MAX_VALUE][BOARD_SIZE * BOARD_SIZE * CONSTRAINTS]; + + int hBase = 0; + hBase = checkCellConstraint(coverBoard, hBase); + hBase = checkRowConstraint(coverBoard, hBase); + hBase = checkColumnConstraint(coverBoard, hBase); + checkSubsectionConstraint(coverBoard, hBase); + + return coverBoard; + } + + private int checkSubsectionConstraint(boolean[][] coverBoard, int hBase) { + for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row += SUBSECTION_SIZE) { + for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column += SUBSECTION_SIZE) { + for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) { + for (int rowDelta = 0; rowDelta < SUBSECTION_SIZE; rowDelta++) { + for (int columnDelta = 0; columnDelta < SUBSECTION_SIZE; columnDelta++) { + int index = getIndex(row + rowDelta, column + columnDelta, n); + coverBoard[index][hBase] = true; + } + } + } + } + } + return hBase; + } + + private int checkColumnConstraint(boolean[][] coverBoard, int hBase) { + for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++) { + for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) { + for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) { + int index = getIndex(row, column, n); + coverBoard[index][hBase] = true; + } + } + } + return hBase; + } + + private int checkRowConstraint(boolean[][] coverBoard, int hBase) { + for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) { + for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) { + for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++) { + int index = getIndex(row, column, n); + coverBoard[index][hBase] = true; + } + } + } + return hBase; + } + + private int checkCellConstraint(boolean[][] coverBoard, int hBase) { + for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) { + for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++, hBase++) { + for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++) { + int index = getIndex(row, column, n); + coverBoard[index][hBase] = true; + } + } + } + return hBase; + } + + private boolean[][] initializeExactCoverBoard(int[][] board) { + boolean[][] coverBoard = createExactCoverBoard(); + for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) { + for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++) { + int n = board[row - 1][column - 1]; + if (n != NO_VALUE) { + for (int num = MIN_VALUE; num <= MAX_VALUE; num++) { + if (num != n) { + Arrays.fill(coverBoard[getIndex(row, column, num)], false); + } + } + } + } + } + return coverBoard; + } +} \ No newline at end of file diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java new file mode 100644 index 0000000000..2422ff0dff --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java @@ -0,0 +1,50 @@ +package com.baeldung.algorithms.sudoku; + +class DancingNode { + DancingNode L, R, U, D; + ColumnNode C; + + DancingNode hookDown(DancingNode node) { + assert (this.C == node.C); + node.D = this.D; + node.D.U = node; + node.U = this; + this.D = node; + return node; + } + + DancingNode hookRight(DancingNode node) { + node.R = this.R; + node.R.L = node; + node.L = this; + this.R = node; + return node; + } + + void unlinkLR() { + this.L.R = this.R; + this.R.L = this.L; + } + + void relinkLR() { + this.L.R = this.R.L = this; + } + + void unlinkUD() { + this.U.D = this.D; + this.D.U = this.U; + } + + void relinkUD() { + this.U.D = this.D.U = this; + } + + DancingNode() { + L = R = U = D = this; + } + + DancingNode(ColumnNode c) { + this(); + C = c; + } +} \ No newline at end of file diff --git a/algorithms/src/main/resources/maze/maze1.txt b/algorithms/src/main/resources/maze/maze1.txt new file mode 100644 index 0000000000..0a6309d25b --- /dev/null +++ b/algorithms/src/main/resources/maze/maze1.txt @@ -0,0 +1,12 @@ +S ######## +# # +# ### ## # +# # # # +# # # # # +# ## ##### +# # # +# # # # # +##### #### +# # E +# # # # +########## \ No newline at end of file diff --git a/algorithms/src/main/resources/maze/maze2.txt b/algorithms/src/main/resources/maze/maze2.txt new file mode 100644 index 0000000000..22e6d0382a --- /dev/null +++ b/algorithms/src/main/resources/maze/maze2.txt @@ -0,0 +1,22 @@ +S ########################## +# # # # +# # #### ############### # +# # # # # # +# # #### # # ############### +# # # # # # # +# # # #### ### ########### # +# # # # # # +# ################## # +######### # # # # # +# # #### # ####### # # +# # ### ### # # # # # +# # ## # ##### # # +##### ####### # # # # # +# # ## ## #### # # +# ##### ####### # # +# # ############ +####### ######### # # +# # ######## # +# ####### ###### ## # E +# # # ## # +############################ \ No newline at end of file diff --git a/algorithms/src/test/java/com/baeldung/algorithms/moneywords/NumberWordConverterTest.java b/algorithms/src/test/java/com/baeldung/algorithms/moneywords/NumberWordConverterTest.java new file mode 100644 index 0000000000..a4a169f158 --- /dev/null +++ b/algorithms/src/test/java/com/baeldung/algorithms/moneywords/NumberWordConverterTest.java @@ -0,0 +1,84 @@ +package com.baeldung.algorithms.moneywords; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import com.baeldung.algorithms.numberwordconverter.NumberWordConverter; + +public class NumberWordConverterTest { + + @Test + public void whenMoneyNegative_thenReturnInvalidInput() { + assertEquals(NumberWordConverter.INVALID_INPUT_GIVEN, NumberWordConverter.getMoneyIntoWords(-13)); + } + + @Test + public void whenZeroDollarsGiven_thenReturnEmptyString() { + assertEquals("", NumberWordConverter.getMoneyIntoWords(0)); + } + + @Test + public void whenOnlyDollarsGiven_thenReturnWords() { + assertEquals("one dollar", NumberWordConverter.getMoneyIntoWords(1)); + } + + @Test + public void whenOnlyCentsGiven_thenReturnWords() { + assertEquals("sixty cents", NumberWordConverter.getMoneyIntoWords(0.6)); + } + + @Test + public void whenAlmostAMillioDollarsGiven_thenReturnWords() { + String expectedResult = "nine hundred ninety nine thousand nine hundred ninety nine dollars"; + assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(999_999)); + } + + @Test + public void whenThirtyMillionDollarsGiven_thenReturnWords() { + String expectedResult = "thirty three million three hundred forty eight thousand nine hundred seventy eight dollars"; + assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(33_348_978)); + } + + @Test + public void whenTwoBillionDollarsGiven_thenReturnWords() { + String expectedResult = "two billion one hundred thirty three million two hundred forty seven thousand eight hundred ten dollars"; + assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(2_133_247_810)); + } + + @Test + public void whenGivenDollarsAndCents_thenReturnWords() { + String expectedResult = "nine hundred twenty four dollars and sixty cents"; + assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(924.6)); + } + + @Test + public void whenOneDollarAndNoCents_thenReturnDollarSingular() { + assertEquals("one dollar", NumberWordConverter.getMoneyIntoWords(1)); + } + + @Test + public void whenNoDollarsAndOneCent_thenReturnCentSingular() { + assertEquals("one cent", NumberWordConverter.getMoneyIntoWords(0.01)); + } + + @Test + public void whenNoDollarsAndTwoCents_thenReturnCentsPlural() { + assertEquals("two cents", NumberWordConverter.getMoneyIntoWords(0.02)); + } + + @Test + public void whenNoDollarsAndNinetyNineCents_thenReturnWords() { + assertEquals("ninety nine cents", NumberWordConverter.getMoneyIntoWords(0.99)); + } + + @Test + public void whenNoDollarsAndNineFiveNineCents_thenCorrectRounding() { + assertEquals("ninety six cents", NumberWordConverter.getMoneyIntoWords(0.959)); + } + + @Test + public void whenGivenDollarsAndCents_thenReturnWordsVersionTwo() { + assertEquals("three hundred ten £ 00/100", NumberWordConverter.getMoneyIntoWords("310")); + } +} diff --git a/algorithms/src/test/java/com/baeldung/algorithms/multiswarm/LolFitnessFunction.java b/algorithms/src/test/java/com/baeldung/algorithms/multiswarm/LolFitnessFunction.java new file mode 100644 index 0000000000..726d4c135d --- /dev/null +++ b/algorithms/src/test/java/com/baeldung/algorithms/multiswarm/LolFitnessFunction.java @@ -0,0 +1,52 @@ +package com.baeldung.algorithms.multiswarm; + +/** + * Specific fitness function implementation to solve the League of Legends + * problem. This is the problem statement:
+ *
+ * In League of Legends, a player's Effective Health when defending against + * physical damage is given by E=H(100+A)/100, where H is health and A is armor. + * Health costs 2.5 gold per unit, and Armor costs 18 gold per unit. You have + * 3600 gold, and you need to optimize the effectiveness E of your health and + * armor to survive as long as possible against the enemy team's attacks. How + * much of each should you buy?
+ *
+ * + * @author Donato Rimenti + * + */ +public class LolFitnessFunction implements FitnessFunction { + + /* + * (non-Javadoc) + * + * @see + * com.baeldung.algorithms.multiswarm.FitnessFunction#getFitness(long[]) + */ + @Override + public double getFitness(long[] particlePosition) { + + long health = particlePosition[0]; + long armor = particlePosition[1]; + + // No negatives values accepted. + if (health < 0 && armor < 0) { + return -(health * armor); + } else if (health < 0) { + return health; + } else if (armor < 0) { + return armor; + } + + // Checks if the solution is actually feasible provided our gold. + double cost = (health * 2.5) + (armor * 18); + if (cost > 3600) { + return 3600 - cost; + } else { + // Check how good is the solution. + long fitness = (health * (100 + armor)) / 100; + return fitness; + } + } + +} diff --git a/algorithms/src/test/java/com/baeldung/algorithms/multiswarm/MultiswarmUnitTest.java b/algorithms/src/test/java/com/baeldung/algorithms/multiswarm/MultiswarmUnitTest.java new file mode 100644 index 0000000000..3455cd3932 --- /dev/null +++ b/algorithms/src/test/java/com/baeldung/algorithms/multiswarm/MultiswarmUnitTest.java @@ -0,0 +1,54 @@ +package com.baeldung.algorithms.multiswarm; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; + +import com.baeldung.algorithms.support.MayFailRule; + +/** + * Test for {@link Multiswarm}. + * + * @author Donato Rimenti + * + */ +public class MultiswarmUnitTest { + + /** + * Rule for handling expected failures. We use this since this test may + * actually fail due to bad luck in the random generation. + */ + @Rule + public MayFailRule mayFailRule = new MayFailRule(); + + /** + * Tests the multiswarm algorithm with a generic problem. The problem is the + * following:
+ *
+ * In League of Legends, a player's Effective Health when defending against + * physical damage is given by E=H(100+A)/100, where H is health and A is + * armor. Health costs 2.5 gold per unit, and Armor costs 18 gold per unit. + * You have 3600 gold, and you need to optimize the effectiveness E of your + * health and armor to survive as long as possible against the enemy team's + * attacks. How much of each should you buy?
+ *
+ * The solution is H = 1080, A = 50 for a total fitness of 1620. Tested with + * 50 swarms each with 1000 particles. + */ + @Test + public void givenMultiswarm_whenThousandIteration_thenSolutionFound() { + Multiswarm multiswarm = new Multiswarm(50, 1000, new LolFitnessFunction()); + + // Iterates 1000 times through the main loop and prints the result. + for (int i = 0; i < 1000; i++) { + multiswarm.mainLoop(); + } + + System.out.println("Best fitness found: " + multiswarm.getBestFitness() + "[" + multiswarm.getBestPosition()[0] + + "," + multiswarm.getBestPosition()[1] + "]"); + Assert.assertEquals(1080, multiswarm.getBestPosition()[0]); + Assert.assertEquals(50, multiswarm.getBestPosition()[1]); + Assert.assertEquals(1620, (int) multiswarm.getBestFitness()); + } + +} diff --git a/algorithms/src/test/java/com/baeldung/algorithms/support/MayFailRule.java b/algorithms/src/test/java/com/baeldung/algorithms/support/MayFailRule.java new file mode 100644 index 0000000000..91df78ce4a --- /dev/null +++ b/algorithms/src/test/java/com/baeldung/algorithms/support/MayFailRule.java @@ -0,0 +1,38 @@ +package com.baeldung.algorithms.support; + +import org.junit.Rule; +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +/** + * JUnit custom rule for managing tests that may fail due to heuristics or + * randomness. In order to use this, just instantiate this object as a public + * field inside the test class and annotate it with {@link Rule}. + * + * @author Donato Rimenti + * + */ +public class MayFailRule implements TestRule { + + /* + * (non-Javadoc) + * + * @see org.junit.rules.TestRule#apply(org.junit.runners.model.Statement, + * org.junit.runner.Description) + */ + @Override + public Statement apply(Statement base, Description description) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + try { + base.evaluate(); + } catch (Throwable e) { + // Ignore the exception since we expect this. + } + } + }; + } + +} diff --git a/apache-curator/pom.xml b/apache-curator/pom.xml new file mode 100644 index 0000000000..35549861c8 --- /dev/null +++ b/apache-curator/pom.xml @@ -0,0 +1,76 @@ + + 4.0.0 + apache-curator + 0.0.1-SNAPSHOT + jar + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + 4.0.1 + 3.4.11 + 2.9.4 + + + 3.6.1 + 1.7.0 + + + + + + + + + org.apache.curator + curator-x-async + ${curator.version} + + + org.apache.zookeeper + zookeeper + + + + + + org.apache.curator + curator-recipes + ${curator.version} + + + + org.apache.zookeeper + zookeeper + ${zookeeper.version} + + + + + com.fasterxml.jackson.core + jackson-databind + ${jackson-databind.version} + + + + + + org.assertj + assertj-core + ${assertj.version} + test + + + + com.jayway.awaitility + awaitility + ${avaitility.version} + test + + + diff --git a/apache-curator/src/main/java/com/baeldung/apache/curator/modeled/HostConfig.java b/apache-curator/src/main/java/com/baeldung/apache/curator/modeled/HostConfig.java new file mode 100644 index 0000000000..bab7133742 --- /dev/null +++ b/apache-curator/src/main/java/com/baeldung/apache/curator/modeled/HostConfig.java @@ -0,0 +1,31 @@ +package com.baeldung.apache.curator.modeled; + +public class HostConfig { + private String hostname; + private int port; + + public HostConfig() { + + } + + public HostConfig(String hostname, int port) { + this.hostname = hostname; + this.port = port; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public String getHostname() { + return hostname; + } + + public void setHostname(String hostname) { + this.hostname = hostname; + } +} diff --git a/apache-curator/src/test/java/com/baeldung/apache/curator/BaseTest.java b/apache-curator/src/test/java/com/baeldung/apache/curator/BaseTest.java new file mode 100644 index 0000000000..cfac3ee3f2 --- /dev/null +++ b/apache-curator/src/test/java/com/baeldung/apache/curator/BaseTest.java @@ -0,0 +1,22 @@ +package com.baeldung.apache.curator; + +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.RetryNTimes; +import org.junit.Before; + +public abstract class BaseTest { + + @Before + public void setup() { + org.apache.log4j.BasicConfigurator.configure(); + } + + protected CuratorFramework newClient() { + int sleepMsBetweenRetries = 100; + int maxRetries = 3; + RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries); + return CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy); + } +} diff --git a/apache-curator/src/test/java/com/baeldung/apache/curator/configuration/ConfigurationManagementManualTest.java b/apache-curator/src/test/java/com/baeldung/apache/curator/configuration/ConfigurationManagementManualTest.java new file mode 100644 index 0000000000..d02ef8131d --- /dev/null +++ b/apache-curator/src/test/java/com/baeldung/apache/curator/configuration/ConfigurationManagementManualTest.java @@ -0,0 +1,89 @@ +package com.baeldung.apache.curator.configuration; + +import static com.jayway.awaitility.Awaitility.await; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.x.async.AsyncCuratorFramework; +import org.junit.Test; + +import com.baeldung.apache.curator.BaseTest; + +public class ConfigurationManagementManualTest extends BaseTest { + + private static final String KEY_FORMAT = "/%s"; + + @Test + public void givenPath_whenCreateKey_thenValueIsStored() throws Exception { + try (CuratorFramework client = newClient()) { + client.start(); + AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client); + String key = getKey(); + String expected = "my_value"; + + // Create key nodes structure + client.create() + .forPath(key); + + // Set data value for our key + async.setData() + .forPath(key, expected.getBytes()); + + // Get data value + AtomicBoolean isEquals = new AtomicBoolean(); + async.getData() + .forPath(key) + .thenAccept( + data -> isEquals.set(new String(data).equals(expected))); + + await().until(() -> assertThat(isEquals.get()).isTrue()); + } + } + + @Test + public void givenPath_whenWatchAKeyAndStoreAValue_thenWatcherIsTriggered() + throws Exception { + try (CuratorFramework client = newClient()) { + client.start(); + AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client); + String key = getKey(); + String expected = "my_value"; + + // Create key structure + async.create() + .forPath(key); + + List changes = new ArrayList<>(); + + // Watch data value + async.watched() + .getData() + .forPath(key) + .event() + .thenAccept(watchedEvent -> { + try { + changes.add(new String(client.getData() + .forPath(watchedEvent.getPath()))); + } catch (Exception e) { + // fail ... + } + }); + + // Set data value for our key + async.setData() + .forPath(key, expected.getBytes()); + + await().until(() -> assertThat(changes.size() > 0).isTrue()); + } + } + + private String getKey() { + return String.format(KEY_FORMAT, UUID.randomUUID() + .toString()); + } +} diff --git a/apache-curator/src/test/java/com/baeldung/apache/curator/connection/ConnectionManagementManualTest.java b/apache-curator/src/test/java/com/baeldung/apache/curator/connection/ConnectionManagementManualTest.java new file mode 100644 index 0000000000..61fa1c7c2c --- /dev/null +++ b/apache-curator/src/test/java/com/baeldung/apache/curator/connection/ConnectionManagementManualTest.java @@ -0,0 +1,79 @@ +package com.baeldung.apache.curator.connection; + +import static com.jayway.awaitility.Awaitility.await; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.concurrent.atomic.AtomicBoolean; + +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.RetryNTimes; +import org.apache.curator.x.async.AsyncCuratorFramework; +import org.junit.Test; + +public class ConnectionManagementManualTest { + + @Test + public void givenRunningZookeeper_whenOpenConnection_thenClientIsOpened() + throws Exception { + int sleepMsBetweenRetries = 100; + int maxRetries = 3; + RetryPolicy retryPolicy = new RetryNTimes(maxRetries, + sleepMsBetweenRetries); + + try (CuratorFramework client = CuratorFrameworkFactory + .newClient("127.0.0.1:2181", retryPolicy)) { + client.start(); + + assertThat(client.checkExists() + .forPath("/")).isNotNull(); + } + } + + @Test + public void givenRunningZookeeper_whenOpenConnectionUsingAsyncNotBlocking_thenClientIsOpened() + throws InterruptedException { + int sleepMsBetweenRetries = 100; + int maxRetries = 3; + RetryPolicy retryPolicy = new RetryNTimes(maxRetries, + sleepMsBetweenRetries); + + try (CuratorFramework client = CuratorFrameworkFactory + .newClient("127.0.0.1:2181", retryPolicy)) { + client.start(); + AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client); + + AtomicBoolean exists = new AtomicBoolean(false); + + async.checkExists() + .forPath("/") + .thenAcceptAsync(s -> exists.set(s != null)); + + await().until(() -> assertThat(exists.get()).isTrue()); + } + } + + @Test + public void givenRunningZookeeper_whenOpenConnectionUsingAsyncBlocking_thenClientIsOpened() + throws InterruptedException { + int sleepMsBetweenRetries = 100; + int maxRetries = 3; + RetryPolicy retryPolicy = new RetryNTimes(maxRetries, + sleepMsBetweenRetries); + + try (CuratorFramework client = CuratorFrameworkFactory + .newClient("127.0.0.1:2181", retryPolicy)) { + client.start(); + AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client); + + AtomicBoolean exists = new AtomicBoolean(false); + + async.checkExists() + .forPath("/") + .thenAccept(s -> exists.set(s != null)); + + await().until(() -> assertThat(exists.get()).isTrue()); + } + } +} diff --git a/apache-curator/src/test/java/com/baeldung/apache/curator/modeled/ModelTypedExamplesManualTest.java b/apache-curator/src/test/java/com/baeldung/apache/curator/modeled/ModelTypedExamplesManualTest.java new file mode 100644 index 0000000000..4400c1d1aa --- /dev/null +++ b/apache-curator/src/test/java/com/baeldung/apache/curator/modeled/ModelTypedExamplesManualTest.java @@ -0,0 +1,49 @@ +package com.baeldung.apache.curator.modeled; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.x.async.AsyncCuratorFramework; +import org.apache.curator.x.async.modeled.JacksonModelSerializer; +import org.apache.curator.x.async.modeled.ModelSpec; +import org.apache.curator.x.async.modeled.ModeledFramework; +import org.apache.curator.x.async.modeled.ZPath; +import org.junit.Test; + +import com.baeldung.apache.curator.BaseTest; + +public class ModelTypedExamplesManualTest extends BaseTest { + + @Test + public void givenPath_whenStoreAModel_thenNodesAreCreated() + throws InterruptedException { + + ModelSpec mySpec = ModelSpec + .builder(ZPath.parseWithIds("/config/dev"), + JacksonModelSerializer.build(HostConfig.class)) + .build(); + + try (CuratorFramework client = newClient()) { + client.start(); + AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client); + ModeledFramework modeledClient = ModeledFramework + .wrap(async, mySpec); + + modeledClient.set(new HostConfig("host-name", 8080)); + + modeledClient.read() + .whenComplete((value, e) -> { + if (e != null) { + fail("Cannot read host config", e); + } else { + assertThat(value).isNotNull(); + assertThat(value.getHostname()).isEqualTo("host-name"); + assertThat(value.getPort()).isEqualTo(8080); + } + + }); + } + + } +} diff --git a/apache-curator/src/test/java/com/baeldung/apache/curator/recipes/RecipesManualTest.java b/apache-curator/src/test/java/com/baeldung/apache/curator/recipes/RecipesManualTest.java new file mode 100644 index 0000000000..04f4104e6b --- /dev/null +++ b/apache-curator/src/test/java/com/baeldung/apache/curator/recipes/RecipesManualTest.java @@ -0,0 +1,74 @@ +package com.baeldung.apache.curator.recipes; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.leader.LeaderSelector; +import org.apache.curator.framework.recipes.leader.LeaderSelectorListener; +import org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex; +import org.apache.curator.framework.recipes.shared.SharedCount; +import org.apache.curator.framework.state.ConnectionState; +import org.junit.Test; + +import com.baeldung.apache.curator.BaseTest; + +public class RecipesManualTest extends BaseTest { + + @Test + public void givenRunningZookeeper_whenUsingLeaderElection_thenNoErrors() { + try (CuratorFramework client = newClient()) { + client.start(); + LeaderSelector leaderSelector = new LeaderSelector(client, "/mutex/select/leader/for/job/A", new LeaderSelectorListener() { + + @Override + public void stateChanged(CuratorFramework client, ConnectionState newState) { + + } + + @Override + public void takeLeadership(CuratorFramework client) throws Exception { + // I'm the leader of the job A ! + } + + }); + + leaderSelector.start(); + + // Wait until the job A is done among all the members + + leaderSelector.close(); + } + } + + @Test + public void givenRunningZookeeper_whenUsingSharedLock_thenNoErrors() throws Exception { + try (CuratorFramework client = newClient()) { + client.start(); + InterProcessSemaphoreMutex sharedLock = new InterProcessSemaphoreMutex(client, "/mutex/process/A"); + + sharedLock.acquire(); + + // Do process A + + sharedLock.release(); + } + } + + @Test + public void givenRunningZookeeper_whenUsingSharedCounter_thenCounterIsIncrement() throws Exception { + try (CuratorFramework client = newClient()) { + client.start(); + + try (SharedCount counter = new SharedCount(client, "/counters/A", 0)) { + counter.start(); + + counter.setCount(0); + counter.setCount(counter.getCount() + 1); + + assertThat(counter.getCount()).isEqualTo(1); + } + + } + } + +} diff --git a/apache-poi/README.md b/apache-poi/README.md index 10fe8ba2e7..c052bc9bf6 100644 --- a/apache-poi/README.md +++ b/apache-poi/README.md @@ -1,3 +1,4 @@ ### Relevant Articles: - [Microsoft Word Processing in Java with Apache POI](http://www.baeldung.com/java-microsoft-word-with-apache-poi) - [Working with Microsoft Excel in Java](http://www.baeldung.com/java-microsoft-excel) +- [Creating a MS PowerPoint Presentation in Java](https://github.com/eugenp/tutorials/tree/master/apache-poi) diff --git a/apache-zookeeper/pom.xml b/apache-zookeeper/pom.xml new file mode 100644 index 0000000000..3a6fc1787b --- /dev/null +++ b/apache-zookeeper/pom.xml @@ -0,0 +1,36 @@ + + 4.0.0 + com.baeldung + apache-zookeeper + 0.0.1-SNAPSHOT + jar + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + org.apache.zookeeper + zookeeper + 3.3.2 + + + com.sun.jmx + jmxri + + + com.sun.jdmk + jmxtools + + + javax.jms + jms + + + + + diff --git a/apache-zookeeper/src/main/java/com/baeldung/zookeeper/connection/ZKConnection.java b/apache-zookeeper/src/main/java/com/baeldung/zookeeper/connection/ZKConnection.java new file mode 100644 index 0000000000..0678250d57 --- /dev/null +++ b/apache-zookeeper/src/main/java/com/baeldung/zookeeper/connection/ZKConnection.java @@ -0,0 +1,33 @@ +package com.baeldung.zookeeper.connection; + +import java.io.IOException; +import java.util.concurrent.CountDownLatch; + +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooKeeper; + +public class ZKConnection { + private ZooKeeper zoo; + final CountDownLatch connectionLatch = new CountDownLatch(1); + + public ZKConnection() { + } + + public ZooKeeper connect(String host) throws IOException, InterruptedException { + zoo = new ZooKeeper(host, 2000, new Watcher() { + public void process(WatchedEvent we) { + if (we.getState() == KeeperState.SyncConnected) { + connectionLatch.countDown(); + } + } + }); + connectionLatch.await(); + return zoo; + } + + public void close() throws InterruptedException { + zoo.close(); + } +} diff --git a/apache-zookeeper/src/main/java/com/baeldung/zookeeper/manager/ZKManager.java b/apache-zookeeper/src/main/java/com/baeldung/zookeeper/manager/ZKManager.java new file mode 100644 index 0000000000..0c0ad52123 --- /dev/null +++ b/apache-zookeeper/src/main/java/com/baeldung/zookeeper/manager/ZKManager.java @@ -0,0 +1,35 @@ +package com.baeldung.zookeeper.manager; + +import org.apache.zookeeper.KeeperException; + +public interface ZKManager { + /** + * Create a Znode and save some data + * + * @param path + * @param data + * @throws KeeperException + * @throws InterruptedException + */ + public void create(String path, byte[] data) throws KeeperException, InterruptedException; + + /** + * Get ZNode Data + * + * @param path + * @param boolean watchFlag + * @throws KeeperException + * @throws InterruptedException + */ + public Object getZNodeData(String path, boolean watchFlag); + + /** + * Update the ZNode Data + * + * @param path + * @param data + * @throws KeeperException + * @throws InterruptedException + */ + public void update(String path, byte[] data) throws KeeperException, InterruptedException, KeeperException; +} diff --git a/apache-zookeeper/src/main/java/com/baeldung/zookeeper/manager/ZKManagerImpl.java b/apache-zookeeper/src/main/java/com/baeldung/zookeeper/manager/ZKManagerImpl.java new file mode 100644 index 0000000000..adf76bc0f2 --- /dev/null +++ b/apache-zookeeper/src/main/java/com/baeldung/zookeeper/manager/ZKManagerImpl.java @@ -0,0 +1,58 @@ +package com.baeldung.zookeeper.manager; + +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; + +import com.baeldung.zookeeper.connection.ZKConnection; + +public class ZKManagerImpl implements ZKManager { + private static ZooKeeper zkeeper; + private static ZKConnection zkConnection; + + public ZKManagerImpl() { + initialize(); + } + + /** * Initialize connection */ + private void initialize() { + try { + zkConnection = new ZKConnection(); + zkeeper = zkConnection.connect("localhost"); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } + + public void closeConnection() { + try { + zkConnection.close(); + } catch (InterruptedException e) { + System.out.println(e.getMessage()); + } + } + + public void create(String path, byte[] data) throws KeeperException, InterruptedException { + zkeeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + + public Object getZNodeData(String path, boolean watchFlag) { + try { + byte[] b = null; + b = zkeeper.getData(path, null, null); + String data = new String(b, "UTF-8"); + System.out.println(data); + return data; + } catch (Exception e) { + System.out.println(e.getMessage()); + } + return null; + } + + public void update(String path, byte[] data) throws KeeperException, InterruptedException { + int version = zkeeper.exists(path, true) + .getVersion(); + zkeeper.setData(path, data, version); + } +} diff --git a/aws/src/main/java/com/baeldung/ec2/EC2Application.java b/aws/src/main/java/com/baeldung/ec2/EC2Application.java new file mode 100644 index 0000000000..6755188fcd --- /dev/null +++ b/aws/src/main/java/com/baeldung/ec2/EC2Application.java @@ -0,0 +1,137 @@ +package com.baeldung.ec2; + +import java.util.Arrays; + +import com.amazonaws.auth.AWSCredentials; +import com.amazonaws.auth.AWSStaticCredentialsProvider; +import com.amazonaws.auth.BasicAWSCredentials; +import com.amazonaws.regions.Regions; +import com.amazonaws.services.ec2.AmazonEC2; +import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; +import com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressRequest; +import com.amazonaws.services.ec2.model.CreateKeyPairRequest; +import com.amazonaws.services.ec2.model.CreateKeyPairResult; +import com.amazonaws.services.ec2.model.CreateSecurityGroupRequest; +import com.amazonaws.services.ec2.model.DescribeInstancesRequest; +import com.amazonaws.services.ec2.model.DescribeInstancesResult; +import com.amazonaws.services.ec2.model.DescribeKeyPairsRequest; +import com.amazonaws.services.ec2.model.DescribeKeyPairsResult; +import com.amazonaws.services.ec2.model.IpPermission; +import com.amazonaws.services.ec2.model.IpRange; +import com.amazonaws.services.ec2.model.MonitorInstancesRequest; +import com.amazonaws.services.ec2.model.RebootInstancesRequest; +import com.amazonaws.services.ec2.model.RunInstancesRequest; +import com.amazonaws.services.ec2.model.StartInstancesRequest; +import com.amazonaws.services.ec2.model.StopInstancesRequest; +import com.amazonaws.services.ec2.model.UnmonitorInstancesRequest; + +public class EC2Application { + + private static final AWSCredentials credentials; + + static { + // put your accesskey and secretkey here + credentials = new BasicAWSCredentials( + "", + "" + ); + } + + public static void main(String[] args) { + + // Set up the client + AmazonEC2 ec2Client = AmazonEC2ClientBuilder.standard() + .withCredentials(new AWSStaticCredentialsProvider(credentials)) + .withRegion(Regions.US_EAST_1) + .build(); + + // Create a security group + CreateSecurityGroupRequest createSecurityGroupRequest = new CreateSecurityGroupRequest().withGroupName("BaeldungSecurityGroup") + .withDescription("Baeldung Security Group"); + ec2Client.createSecurityGroup(createSecurityGroupRequest); + + // Allow HTTP and SSH traffic + IpRange ipRange1 = new IpRange().withCidrIp("0.0.0.0/0"); + + IpPermission ipPermission1 = new IpPermission().withIpv4Ranges(Arrays.asList(new IpRange[] { ipRange1 })) + .withIpProtocol("tcp") + .withFromPort(80) + .withToPort(80); + + IpPermission ipPermission2 = new IpPermission().withIpv4Ranges(Arrays.asList(new IpRange[] { ipRange1 })) + .withIpProtocol("tcp") + .withFromPort(22) + .withToPort(22); + + AuthorizeSecurityGroupIngressRequest authorizeSecurityGroupIngressRequest = new AuthorizeSecurityGroupIngressRequest() + .withGroupName("BaeldungSecurityGroup") + .withIpPermissions(ipPermission1, ipPermission2); + + ec2Client.authorizeSecurityGroupIngress(authorizeSecurityGroupIngressRequest); + + // Create KeyPair + CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest() + .withKeyName("baeldung-key-pair"); + CreateKeyPairResult createKeyPairResult = ec2Client.createKeyPair(createKeyPairRequest); + String privateKey = createKeyPairResult + .getKeyPair() + .getKeyMaterial(); // make sure you keep it, the private key, Amazon doesn't store the private key + + // See what key-pairs you've got + DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest(); + DescribeKeyPairsResult describeKeyPairsResult = ec2Client.describeKeyPairs(describeKeyPairsRequest); + + // Launch an Amazon Instance + RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withImageId("ami-97785bed") // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html | https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/usingsharedamis-finding.html + .withInstanceType("t2.micro") // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html + .withMinCount(1) + .withMaxCount(1) + .withKeyName("baeldung-key-pair") // optional - if not present, can't connect to instance + .withSecurityGroups("BaeldungSecurityGroup"); + + String yourInstanceId = ec2Client.runInstances(runInstancesRequest).getReservation().getInstances().get(0).getInstanceId(); + + // Start an Instance + StartInstancesRequest startInstancesRequest = new StartInstancesRequest() + .withInstanceIds(yourInstanceId); + + ec2Client.startInstances(startInstancesRequest); + + // Monitor Instances + MonitorInstancesRequest monitorInstancesRequest = new MonitorInstancesRequest() + .withInstanceIds(yourInstanceId); + + ec2Client.monitorInstances(monitorInstancesRequest); + + UnmonitorInstancesRequest unmonitorInstancesRequest = new UnmonitorInstancesRequest() + .withInstanceIds(yourInstanceId); + + ec2Client.unmonitorInstances(unmonitorInstancesRequest); + + // Reboot an Instance + + RebootInstancesRequest rebootInstancesRequest = new RebootInstancesRequest() + .withInstanceIds(yourInstanceId); + + ec2Client.rebootInstances(rebootInstancesRequest); + + // Stop an Instance + StopInstancesRequest stopInstancesRequest = new StopInstancesRequest() + .withInstanceIds(yourInstanceId); + + ec2Client.stopInstances(stopInstancesRequest) + .getStoppingInstances() + .get(0) + .getPreviousState() + .getName(); + + // Describe an Instance + DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest(); + DescribeInstancesResult response = ec2Client.describeInstances(describeInstancesRequest); + System.out.println(response.getReservations() + .get(0) + .getInstances() + .get(0) + .getKernelId()); + } +} diff --git a/checker-plugin/pom.xml b/checker-plugin/pom.xml new file mode 100644 index 0000000000..bc7a669d4f --- /dev/null +++ b/checker-plugin/pom.xml @@ -0,0 +1,114 @@ + + 4.0.0 + com.baeldung + checker-plugin + jar + 1.0-SNAPSHOT + checker-plugin + http://maven.apache.org + + + + + + ${org.checkerframework:jdk8:jar} + + + + + + + + + + org.checkerframework + checker-qual + 2.3.1 + + + org.checkerframework + checker + 2.3.1 + + + org.checkerframework + jdk8 + 2.3.1 + + + + org.checkerframework + compiler + 2.3.1 + + + + + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + + + properties + + + + + + + maven-compiler-plugin + 3.6.1 + + 1.8 + 1.8 + + + + 10000 + 10000 + + + + org.checkerframework.checker.nullness.NullnessChecker + org.checkerframework.checker.interning.InterningChecker + org.checkerframework.checker.fenum.FenumChecker + org.checkerframework.checker.formatter.FormatterChecker + org.checkerframework.checker.regex.RegexChecker + + + -AprintErrorStack + + + -Xbootclasspath/p:${annotatedJdk} + + + + + + -Awarns + + + + + + + diff --git a/checker-plugin/src/main/java/com/baeldung/typechecker/FakeNumExample.java b/checker-plugin/src/main/java/com/baeldung/typechecker/FakeNumExample.java new file mode 100644 index 0000000000..f1769cfdea --- /dev/null +++ b/checker-plugin/src/main/java/com/baeldung/typechecker/FakeNumExample.java @@ -0,0 +1,42 @@ +package com.baeldung.typechecker; + +import org.checkerframework.checker.fenum.qual.Fenum; + +//@SuppressWarnings("fenum:assignment.type.incompatible") +public class FakeNumExample { + + // Here we use some String constants to represents countries. + static final @Fenum("country") String ITALY = "IT"; + static final @Fenum("country") String US = "US"; + static final @Fenum("country") String UNITED_KINGDOM = "UK"; + + // Here we use other String constants to represent planets instead. + static final @Fenum("planet") String MARS = "Mars"; + static final @Fenum("planet") String EARTH = "Earth"; + static final @Fenum("planet") String VENUS = "Venus"; + + // Now we write this method and we want to be sure that + // the String parameter has a value of a country, not that is just a String. + void greetCountries(@Fenum("country") String country) { + System.out.println("Hello " + country); + } + + // Similarly we're enforcing here that the provided + // parameter is a String that represent a planet. + void greetPlanets(@Fenum("planet") String planet) { + System.out.println("Hello " + planet); + } + + public static void main(String[] args) { + + FakeNumExample obj = new FakeNumExample(); + + // This will fail because we pass a planet-String to a method that + // accept a country-String. + obj.greetCountries(MARS); + + // Here the opposite happens. + obj.greetPlanets(US); + } + +} diff --git a/checker-plugin/src/main/java/com/baeldung/typechecker/FormatExample.java b/checker-plugin/src/main/java/com/baeldung/typechecker/FormatExample.java new file mode 100644 index 0000000000..2fa53ff2c2 --- /dev/null +++ b/checker-plugin/src/main/java/com/baeldung/typechecker/FormatExample.java @@ -0,0 +1,23 @@ +package com.baeldung.typechecker; + +public class FormatExample { + + public static void main(String[] args) { + + // Just enabling org.checkerframework.checker.formatter.FormatterChecker + // we can be sure at compile time that the format strings we pass to format() + // are correct. Normally we would have those errors raised just at runtime. + // All those formats are in fact wrong. + + String.format("%y", 7); // error: invalid format string + + String.format("%d", "a string"); // error: invalid argument type for %d + + String.format("%d %s", 7); // error: missing argument for %s + + String.format("%d", 7, 3); // warning: unused argument 3 + + String.format("{0}", 7); // warning: unused argument 7, because {0} is wrong syntax + } + +} diff --git a/checker-plugin/src/main/java/com/baeldung/typechecker/KeyForExample.java b/checker-plugin/src/main/java/com/baeldung/typechecker/KeyForExample.java new file mode 100644 index 0000000000..b3072b72ee --- /dev/null +++ b/checker-plugin/src/main/java/com/baeldung/typechecker/KeyForExample.java @@ -0,0 +1,31 @@ +package com.baeldung.typechecker; + +import org.checkerframework.checker.nullness.qual.KeyFor; + +import java.util.HashMap; +import java.util.Map; + +public class KeyForExample { + + private final Map config = new HashMap<>(); + + KeyForExample() { + + // Here we initialize a map to store + // some config data. + config.put("url", "http://1.2.3.4"); + config.put("name", "foobaz"); + } + + public void dumpPort() { + + // Here, we want to dump the port value stored in the + // config, so we declare that this key has to be + // present in the config map. + // Obviously that will fail because such key is not present + // in the map. + @KeyFor("config") String key = "port"; + System.out.println( config.get(key) ); + } + +} diff --git a/checker-plugin/src/main/java/com/baeldung/typechecker/MonotonicNotNullExample.java b/checker-plugin/src/main/java/com/baeldung/typechecker/MonotonicNotNullExample.java new file mode 100644 index 0000000000..c4b9c6afe1 --- /dev/null +++ b/checker-plugin/src/main/java/com/baeldung/typechecker/MonotonicNotNullExample.java @@ -0,0 +1,28 @@ +package com.baeldung.typechecker; + +import org.checkerframework.checker.nullness.qual.MonotonicNonNull; + +import java.util.Date; + +public class MonotonicNotNullExample { + + // The idea is we need this field to be to lazily initialized, + // so it starts as null but once it becomes not null + // it cannot return null. + // In these cases, we can use @MonotonicNonNull + @MonotonicNonNull private Date firstCall; + + public Date getFirstCall() { + if (firstCall == null) { + firstCall = new Date(); + } + return firstCall; + } + + public void reset() { + // This is reported as error because + // we wrongly set the field back to null. + firstCall = null; + } + +} diff --git a/checker-plugin/src/main/java/com/baeldung/typechecker/NonNullExample.java b/checker-plugin/src/main/java/com/baeldung/typechecker/NonNullExample.java new file mode 100644 index 0000000000..c929eea23f --- /dev/null +++ b/checker-plugin/src/main/java/com/baeldung/typechecker/NonNullExample.java @@ -0,0 +1,27 @@ +package com.baeldung.typechecker; + +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +public class NonNullExample { + + // This method's parameter is annotated in order + // to tell the pluggable type system that it can never be null. + private static int countArgs(@NonNull String[] args) { + return args.length; + } + + // This method's parameter is annotated in order + // to tell the pluggable type system that it may be null. + public static void main(@Nullable String[] args) { + + // Here lies a potential error, + // because we pass a potential null reference to a method + // that does not accept nulls. + // The Checker Framework will spot this problem at compile time + // instead of runtime. + System.out.println(countArgs(args)); + + } + +} diff --git a/checker-plugin/src/main/java/com/baeldung/typechecker/RegexExample.java b/checker-plugin/src/main/java/com/baeldung/typechecker/RegexExample.java new file mode 100644 index 0000000000..9503865214 --- /dev/null +++ b/checker-plugin/src/main/java/com/baeldung/typechecker/RegexExample.java @@ -0,0 +1,18 @@ +package com.baeldung.typechecker; + +import org.checkerframework.checker.regex.qual.Regex; + +public class RegexExample { + + // For some reason we want to be sure that this regex + // contains at least one capturing group. + // However, we do an error and we forgot to define such + // capturing group in it. + @Regex(1) private static String findNumbers = "\\d*"; + + public static void main(String[] args) { + String message = "My phone number is +3911223344."; + message.matches(findNumbers); + } + +} diff --git a/core-groovy/README.md b/core-groovy/README.md new file mode 100644 index 0000000000..5954a8ab7e --- /dev/null +++ b/core-groovy/README.md @@ -0,0 +1,4 @@ +# Groovy + +## Relevant articles: + diff --git a/core-groovy/build.gradle b/core-groovy/build.gradle new file mode 100644 index 0000000000..300827ee7c --- /dev/null +++ b/core-groovy/build.gradle @@ -0,0 +1,13 @@ +group 'com.baeldung' +version '1.0-SNAPSHOT' + +apply plugin: 'groovy' + +repositories { + mavenCentral() +} + +dependencies { + compile 'org.codehaus.groovy:groovy-all:2.4.13' + testCompile 'org.spockframework:spock-core:1.1-groovy-2.4' +} diff --git a/core-groovy/pom.xml b/core-groovy/pom.xml new file mode 100644 index 0000000000..91cbe66e35 --- /dev/null +++ b/core-groovy/pom.xml @@ -0,0 +1,128 @@ + + + 4.0.0 + + core-groovy + 1.0-SNAPSHOT + jar + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + central + http://jcenter.bintray.com + + + + + + org.codehaus.groovy + groovy + 2.4.13 + + + org.codehaus.groovy + groovy-all + 2.4.13 + + + org.codehaus.groovy + groovy-sql + 2.4.13 + + + org.junit.jupiter + junit-jupiter-engine + ${junit.jupiter.version} + test + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + + + org.hsqldb + hsqldb + 2.4.0 + test + + + org.spockframework + spock-core + 1.1-groovy-2.4 + test + + + + + + + org.codehaus.gmavenplus + gmavenplus-plugin + 1.6 + + + + addSources + addTestSources + compile + compileTests + + + + + + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + maven-failsafe-plugin + 2.19.1 + + + org.junit.platform + junit-platform-surefire-provider + ${junit.platform.version} + + + + + junit5 + + integration-test + verify + + + + **/*Test5.java + + + + + + + + + + UTF-8 + 1.1.2 + 1.1.2 + 1.1.2 + 1.1.2 + 0.15 + 1.5.0 + + 5.0.0 + 1.0.0 + 4.12.0 + 4.12 + + + diff --git a/core-groovy/src/main/groovy/com/baeldung/json/Account.groovy b/core-groovy/src/main/groovy/com/baeldung/json/Account.groovy new file mode 100644 index 0000000000..84b294f0bd --- /dev/null +++ b/core-groovy/src/main/groovy/com/baeldung/json/Account.groovy @@ -0,0 +1,7 @@ +package com.baeldung.json + +class Account { + String id + BigDecimal value + Date createdAt +} \ No newline at end of file diff --git a/core-groovy/src/main/groovy/com/baeldung/json/JsonParser.groovy b/core-groovy/src/main/groovy/com/baeldung/json/JsonParser.groovy new file mode 100644 index 0000000000..6cda7c6fbd --- /dev/null +++ b/core-groovy/src/main/groovy/com/baeldung/json/JsonParser.groovy @@ -0,0 +1,27 @@ +package com.baeldung.json + +import groovy.json.JsonOutput +import groovy.json.JsonParserType +import groovy.json.JsonSlurper + +class JsonParser { + + Account toObject(String json) { + JsonSlurper jsonSlurper = new JsonSlurper() + jsonSlurper.parseText(json) as Account + } + + Account toObjectWithIndexOverlay(String json) { + JsonSlurper jsonSlurper = new JsonSlurper(type: JsonParserType.INDEX_OVERLAY) + jsonSlurper.parseText(json) as Account + } + + String toJson(Account account) { + JsonOutput.toJson(account) + } + + String prettyfy(String json) { + JsonOutput.prettyPrint(json) + } + +} diff --git a/core-groovy/src/test/groovy/com/baeldung/groovy/sql/SqlTest.groovy b/core-groovy/src/test/groovy/com/baeldung/groovy/sql/SqlTest.groovy new file mode 100644 index 0000000000..ac96a55773 --- /dev/null +++ b/core-groovy/src/test/groovy/com/baeldung/groovy/sql/SqlTest.groovy @@ -0,0 +1,229 @@ +package com.baeldung.groovy.sql + +import groovy.sql.GroovyResultSet +import groovy.sql.GroovyRowResult +import groovy.sql.Sql +import groovy.transform.CompileStatic +import static org.junit.Assert.* +import org.junit.Test + +class SqlTest { + + final Map dbConnParams = [url: 'jdbc:hsqldb:mem:testDB', user: 'sa', password: '', driver: 'org.hsqldb.jdbc.JDBCDriver'] + + @Test + void whenNewSqlInstance_thenDbIsAccessed() { + def sql = Sql.newInstance(dbConnParams) + sql.close() + sql.close() + } + + @Test + void whenTableDoesNotExist_thenSelectFails() { + try { + Sql.withInstance(dbConnParams) { Sql sql -> + sql.eachRow('select * from PROJECT') {} + } + + fail("An exception should have been thrown") + } catch (ignored) { + //Ok + } + } + + @Test + void whenTableCreated_thenSelectIsPossible() { + Sql.withInstance(dbConnParams) { Sql sql -> + def result = sql.execute 'create table PROJECT_1 (id integer not null, name varchar(50), url varchar(100))' + + assertEquals(0, sql.updateCount) + assertFalse(result) + + result = sql.execute('select * from PROJECT_1') + + assertTrue(result) + } + } + + @Test + void whenIdentityColumn_thenInsertReturnsNewId() { + Sql.withInstance(dbConnParams) { Sql sql -> + sql.execute 'create table PROJECT_2 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))' + def ids = sql.executeInsert("INSERT INTO PROJECT_2 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')") + + assertEquals(0, ids[0][0]) + + ids = sql.executeInsert("INSERT INTO PROJECT_2 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')") + + assertEquals(1, ids[0][0]) + } + } + + @Test + void whenUpdate_thenNumberOfAffectedRows() { + Sql.withInstance(dbConnParams) { Sql sql -> + sql.execute 'create table PROJECT_3 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))' + sql.executeInsert("INSERT INTO PROJECT_3 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')") + sql.executeInsert("INSERT INTO PROJECT_3 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')") + def count = sql.executeUpdate("UPDATE PROJECT_3 SET URL = 'https://' + URL") + + assertEquals(2, count) + } + } + + @Test + void whenEachRow_thenResultSetHasProperties() { + Sql.withInstance(dbConnParams) { Sql sql -> + sql.execute 'create table PROJECT_4 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))' + sql.executeInsert("INSERT INTO PROJECT_4 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')") + sql.executeInsert("INSERT INTO PROJECT_4 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')") + + sql.eachRow("SELECT * FROM PROJECT_4") { GroovyResultSet rs -> + assertNotNull(rs.name) + assertNotNull(rs.url) + assertNotNull(rs[0]) + assertNotNull(rs[1]) + assertNotNull(rs[2]) + assertEquals(rs.name, rs['name']) + assertEquals(rs.url, rs['url']) + } + } + } + + @Test + void whenPagination_thenSubsetIsReturned() { + Sql.withInstance(dbConnParams) { Sql sql -> + sql.execute 'create table PROJECT_5 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))' + sql.executeInsert("INSERT INTO PROJECT_5 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')") + sql.executeInsert("INSERT INTO PROJECT_5 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')") + def rows = sql.rows('SELECT * FROM PROJECT_5 ORDER BY NAME', 1, 1) + + assertEquals(1, rows.size()) + assertEquals('REST with Spring', rows[0].name) + } + } + + @Test + void whenParameters_thenReplacement() { + Sql.withInstance(dbConnParams) { Sql sql -> + sql.execute 'create table PROJECT_6 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))' + sql.execute('INSERT INTO PROJECT_6 (NAME, URL) VALUES (?, ?)', 'tutorials', 'github.com/eugenp/tutorials') + sql.execute("INSERT INTO PROJECT_6 (NAME, URL) VALUES (:name, :url)", [name: 'REST with Spring', url: 'github.com/eugenp/REST-With-Spring']) + + def rows = sql.rows("SELECT * FROM PROJECT_6 WHERE NAME = 'tutorials'") + + assertEquals(1, rows.size()) + + rows = sql.rows("SELECT * FROM PROJECT_6 WHERE NAME = 'REST with Spring'") + + assertEquals(1, rows.size()) + } + } + + @Test + void whenParametersInGString_thenReplacement() { + Sql.withInstance(dbConnParams) { Sql sql -> + sql.execute 'create table PROJECT_7 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))' + sql.execute "INSERT INTO PROJECT_7 (NAME, URL) VALUES (${'tutorials'}, ${'github.com/eugenp/tutorials'})" + def name = 'REST with Spring' + def url = 'github.com/eugenp/REST-With-Spring' + sql.execute "INSERT INTO PROJECT_7 (NAME, URL) VALUES (${name}, ${url})" + + def rows = sql.rows("SELECT * FROM PROJECT_7 WHERE NAME = 'tutorials'") + + assertEquals(1, rows.size()) + + rows = sql.rows("SELECT * FROM PROJECT_7 WHERE NAME = 'REST with Spring'") + + assertEquals(1, rows.size()) + } + } + + @Test + void whenTransactionRollback_thenNoDataInserted() { + Sql.withInstance(dbConnParams) { Sql sql -> + sql.withTransaction { + sql.execute 'create table PROJECT_8 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))' + sql.executeInsert("INSERT INTO PROJECT_8 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')") + sql.executeInsert("INSERT INTO PROJECT_8 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')") + sql.rollback() + + def rows = sql.rows("SELECT * FROM PROJECT_8") + + assertEquals(0, rows.size()) + } + } + } + + @Test + void whenTransactionRollbackThenCommit_thenOnlyLastInserted() { + Sql.withInstance(dbConnParams) { Sql sql -> + sql.withTransaction { + sql.execute 'create table PROJECT_9 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))' + sql.executeInsert("INSERT INTO PROJECT_9 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')") + sql.rollback() + sql.executeInsert("INSERT INTO PROJECT_9 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')") + sql.rollback() + sql.executeInsert("INSERT INTO PROJECT_9 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')") + sql.executeInsert("INSERT INTO PROJECT_9 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')") + } + + def rows = sql.rows("SELECT * FROM PROJECT_9") + + assertEquals(2, rows.size()) + } + } + + @Test + void whenException_thenTransactionIsRolledBack() { + Sql.withInstance(dbConnParams) { Sql sql -> + try { + sql.withTransaction { + sql.execute 'create table PROJECT_10 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))' + sql.executeInsert("INSERT INTO PROJECT_10 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')") + throw new Exception('rollback') + } + } catch (ignored) {} + + def rows = sql.rows("SELECT * FROM PROJECT_10") + + assertEquals(0, rows.size()) + } + } + + @Test + void givenCachedConnection_whenException_thenDataIsPersisted() { + Sql.withInstance(dbConnParams) { Sql sql -> + try { + sql.cacheConnection { + sql.execute 'create table PROJECT_11 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))' + sql.executeInsert("INSERT INTO PROJECT_11 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')") + throw new Exception('This does not rollback') + } + } catch (ignored) {} + + def rows = sql.rows("SELECT * FROM PROJECT_11") + + assertEquals(1, rows.size()) + } + } + + /*@Test + void whenModifyResultSet_thenDataIsChanged() { + Sql.withInstance(dbConnParams) { Sql sql -> + sql.execute 'create table PROJECT_5 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))' + sql.executeInsert("INSERT INTO PROJECT_5 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')") + sql.executeInsert("INSERT INTO PROJECT_5 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')") + + sql.eachRow("SELECT * FROM PROJECT_5 FOR UPDATE ") { GroovyResultSet rs -> + rs['name'] = "The great ${rs.name}!" as String + rs.updateRow() + } + + sql.eachRow("SELECT * FROM PROJECT_5") { GroovyResultSet rs -> + assertTrue(rs.name.startsWith('The great ')) + } + } + }*/ + +} diff --git a/core-groovy/src/test/groovy/com/baeldung/json/JsonParserTest.groovy b/core-groovy/src/test/groovy/com/baeldung/json/JsonParserTest.groovy new file mode 100644 index 0000000000..c3842340a5 --- /dev/null +++ b/core-groovy/src/test/groovy/com/baeldung/json/JsonParserTest.groovy @@ -0,0 +1,66 @@ +package com.baeldung.json + +import spock.lang.Specification + +import java.text.SimpleDateFormat + +class JsonParserTest extends Specification { + + JsonParser jsonParser + + void setup () { + jsonParser = new JsonParser() + } + + def 'Should parse to Account given Json String' () { + given: + def json = '{"id":"1234","value":15.6}' + when: + def account = jsonParser.toObject(json) + then: + account + account instanceof Account + account.id == '1234' + account.value == 15.6 + } + + def 'Should parse to Account given Json String with date property' () { + given: + def json = '{"id":"1234","value":15.6,"createdAt":"2018-01-01T00:00:00+0000"}' + when: + def account = jsonParser.toObjectWithIndexOverlay(json) + then: + account + account instanceof Account + account.id == '1234' + account.value == 15.6 + println account.createdAt + account.createdAt == Date.parse('yyyy-MM-dd', '2018-01-01') + } + + def 'Should parse to Json given an Account object' () { + given: + Account account = new Account( + id: '123', + value: 15.6, + createdAt: new SimpleDateFormat('MM/dd/yyyy').parse('01/01/2018') + ) + when: + def json = jsonParser.toJson(account) + then: + json + json == '{"value":15.6,"createdAt":"2018-01-01T00:00:00+0000","id":"123"}' + } + + def 'Should prettify given a json string' () { + given: + String json = '{"value":15.6,"createdAt":"01/01/2018","id":"123456"}' + when: + def jsonPretty = jsonParser.prettyfy(json) + then: + jsonPretty + jsonPretty == '{\n "value": 15.6,\n "createdAt": "01/01/2018",\n "id": "123456"\n}' + } + + +} diff --git a/core-java-8/README.md b/core-java-8/README.md index 53e8e1a44a..949577df19 100644 --- a/core-java-8/README.md +++ b/core-java-8/README.md @@ -37,3 +37,9 @@ - [Iterable to Stream in Java](http://www.baeldung.com/java-iterable-to-stream) - [Converting String to Stream of chars](http://www.baeldung.com/java-string-to-stream) - [How to Iterate Over a Stream With Indices](http://www.baeldung.com/java-stream-indices) +- [Efficient Word Frequency Calculator in Java](http://www.baeldung.com/java-word-frequency) +- [Primitive Type Streams in Java 8](http://www.baeldung.com/java-8-primitive-streams) +- [Fail-Safe Iterator vs Fail-Fast Iterator](http://www.baeldung.com/java-fail-safe-vs-fail-fast-iterator) +- [Shuffling Collections In Java](http://www.baeldung.com/java-shuffle-collection) +- [Java 8 StringJoiner](http://www.baeldung.com/java-string-joiner) +- [Introduction to Spliterator in Java](http://www.baeldung.com/java-spliterator) diff --git a/core-java-8/src/main/java/com/baeldung/spliteratorAPI/Executor.java b/core-java-8/src/main/java/com/baeldung/spliteratorAPI/Executor.java index b906acfad9..024d5dabdb 100644 --- a/core-java-8/src/main/java/com/baeldung/spliteratorAPI/Executor.java +++ b/core-java-8/src/main/java/com/baeldung/spliteratorAPI/Executor.java @@ -1,45 +1,19 @@ package com.baeldung.spliteratorAPI; -import java.util.Arrays; import java.util.List; -import java.util.Spliterator; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.stream.Collectors; -import java.util.stream.IntStream; import java.util.stream.Stream; -import java.util.stream.StreamSupport; public class Executor { - public void executeCustomSpliterator() { - Article article = new Article(Arrays.asList(new Author("Ahmad", 0), new Author("Eugen", 0), new Author("Alice", 1), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), - new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), - new Author("Alice", 1), new Author("Mike", 0), new Author("Michał", 0), new Author("Loredana", 1)), 0); - Stream stream = IntStream.range(0, article.getListOfAuthors() - .size()) - .mapToObj(article.getListOfAuthors()::get); - System.out.println("count= " + countAutors(stream.parallel())); - Spliterator spliterator = new RelatedAuthorSpliterator(article.getListOfAuthors()); - Stream stream2 = StreamSupport.stream(spliterator, true); - System.out.println("count= " + countAutors(stream2.parallel())); - } - public void executeSpliterator() { - Spliterator
split1 = generateElements().spliterator(); - Spliterator
split2 = split1.trySplit(); - ExecutorService service = Executors.newCachedThreadPool(); - service.execute(new Task(split1)); - service.execute(new Task(split2)); - } + public static int countAutors(Stream stream) { + RelatedAuthorCounter wordCounter = stream.reduce(new RelatedAuthorCounter(0, true), + RelatedAuthorCounter::accumulate, RelatedAuthorCounter::combine); + return wordCounter.getCounter(); + } - private static int countAutors(Stream stream) { - RelatedAuthorCounter wordCounter = stream.reduce(new RelatedAuthorCounter(0, true), RelatedAuthorCounter::accumulate, RelatedAuthorCounter::combine); - return wordCounter.getCounter(); - } + public static List
generateElements() { + return Stream.generate(() -> new Article("Java")).limit(35000).collect(Collectors.toList()); + } - private List
generateElements() { - return Stream.generate(() -> new Article("Java")) - .limit(35000) - .collect(Collectors.toList()); - } -} +} \ No newline at end of file diff --git a/core-java-8/src/main/java/com/baeldung/spliteratorAPI/RelatedAuthorSpliterator.java b/core-java-8/src/main/java/com/baeldung/spliteratorAPI/RelatedAuthorSpliterator.java index ae91c6fe6c..0a7190964e 100644 --- a/core-java-8/src/main/java/com/baeldung/spliteratorAPI/RelatedAuthorSpliterator.java +++ b/core-java-8/src/main/java/com/baeldung/spliteratorAPI/RelatedAuthorSpliterator.java @@ -2,46 +2,48 @@ package com.baeldung.spliteratorAPI; import java.util.List; import java.util.Spliterator; +import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; public class RelatedAuthorSpliterator implements Spliterator { - private final List list; - private int current = 0; + private final List list; + AtomicInteger current = new AtomicInteger(); - public RelatedAuthorSpliterator(List list) { - this.list = list; - } + public RelatedAuthorSpliterator(List list) { + this.list = list; + } - @Override - public boolean tryAdvance(Consumer action) { - action.accept(list.get(current++)); - return current < list.size(); - } + @Override + public boolean tryAdvance(Consumer action) { - @Override - public Spliterator trySplit() { - int currentSize = list.size() - current; - if (currentSize < 10) { - return null; - } - for (int splitPos = currentSize / 2 + current; splitPos < list.size(); splitPos++) { - if (list.get(splitPos) - .getRelatedArticleId() == 0) { - Spliterator spliterator = new RelatedAuthorSpliterator(list.subList(current, splitPos)); - current = splitPos; - return spliterator; - } - } - return null; - } + action.accept(list.get(current.getAndIncrement())); + return current.get() < list.size(); + } - @Override - public long estimateSize() { - return list.size() - current; - } + @Override + public Spliterator trySplit() { + int currentSize = list.size() - current.get(); + if (currentSize < 10) { + return null; + } + for (int splitPos = currentSize / 2 + current.intValue(); splitPos < list.size(); splitPos++) { + if (list.get(splitPos).getRelatedArticleId() == 0) { + Spliterator spliterator = new RelatedAuthorSpliterator(list.subList(current.get(), splitPos)); + current.set(splitPos); + return spliterator; + } + } + return null; + } + + @Override + public long estimateSize() { + return list.size() - current.get(); + } + + @Override + public int characteristics() { + return CONCURRENT; + } - @Override - public int characteristics() { - return SIZED + CONCURRENT; - } } diff --git a/core-java-8/src/main/java/com/baeldung/spliteratorAPI/Task.java b/core-java-8/src/main/java/com/baeldung/spliteratorAPI/Task.java index 108fdc52aa..70435d1c75 100644 --- a/core-java-8/src/main/java/com/baeldung/spliteratorAPI/Task.java +++ b/core-java-8/src/main/java/com/baeldung/spliteratorAPI/Task.java @@ -1,8 +1,9 @@ package com.baeldung.spliteratorAPI; import java.util.Spliterator; +import java.util.concurrent.Callable; -public class Task implements Runnable { +public class Task implements Callable { private Spliterator
spliterator; private final static String SUFFIX = "- published by Baeldung"; @@ -11,7 +12,7 @@ public class Task implements Runnable { } @Override - public void run() { + public String call() { int current = 0; while (spliterator.tryAdvance(article -> { article.setName(article.getName() @@ -20,7 +21,7 @@ public class Task implements Runnable { current++; } ; - System.out.println(Thread.currentThread() - .getName() + ":" + current); + return Thread.currentThread() + .getName() + ":" + current; } } diff --git a/core-java-8/src/test/java/com/baeldung/shufflingcollections/ShufflingCollectionsUnitTest.java b/core-java-8/src/test/java/com/baeldung/shufflingcollections/ShufflingCollectionsUnitTest.java new file mode 100644 index 0000000000..d013907c9a --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/shufflingcollections/ShufflingCollectionsUnitTest.java @@ -0,0 +1,70 @@ +package com.baeldung.shufflingcollections; + +import org.junit.Test; + +import java.util.*; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ShufflingCollectionsUnitTest { + + @Test + public void whenShufflingList_thenListIsShuffled() { + List students = Arrays.asList("Foo", "Bar", "Baz", "Qux"); + + System.out.println("List before shuffling:"); + System.out.println(students); + + Collections.shuffle(students); + System.out.println("List after shuffling:"); + System.out.println(students); + } + + @Test + public void whenShufflingMapEntries_thenValuesAreShuffled() { + Map studentsById = new HashMap<>(); + studentsById.put(1, "Foo"); + studentsById.put(2, "Bar"); + studentsById.put(3, "Baz"); + studentsById.put(4, "Qux"); + + System.out.println("Students before shuffling:"); + System.out.println(studentsById.values()); + + List> shuffledStudentEntries = new ArrayList<>(studentsById.entrySet()); + Collections.shuffle(shuffledStudentEntries); + + List shuffledStudents = shuffledStudentEntries.stream() + .map(Map.Entry::getValue) + .collect(Collectors.toList()); + + System.out.println("Students after shuffling"); + System.out.println(shuffledStudents); + } + + @Test + public void whenShufflingSet_thenElementsAreShuffled() { + Set students = new HashSet<>(Arrays.asList("Foo", "Bar", "Baz", "Qux")); + + System.out.println("Set before shuffling:"); + System.out.println(students); + + List studentList = new ArrayList<>(students); + + Collections.shuffle(studentList); + System.out.println("Shuffled set elements:"); + System.out.println(studentList); + } + + @Test + public void whenShufflingWithSameRandomness_thenElementsAreShuffledDeterministically() { + List students_1 = Arrays.asList("Foo", "Bar", "Baz", "Qux"); + List students_2 = Arrays.asList("Foo", "Bar", "Baz", "Qux"); + + Collections.shuffle(students_1, new Random(5)); + Collections.shuffle(students_2, new Random(5)); + + assertThat(students_1).isEqualTo(students_2); + } +} diff --git a/core-java-8/src/test/java/com/baeldung/spliteratorAPI/ExecutorTest.java b/core-java-8/src/test/java/com/baeldung/spliteratorAPI/ExecutorTest.java new file mode 100644 index 0000000000..64dd65cf5e --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/spliteratorAPI/ExecutorTest.java @@ -0,0 +1,44 @@ +package com.baeldung.spliteratorAPI; + +import java.util.Arrays; +import java.util.Spliterator; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + +import static org.assertj.core.api.Assertions.*; +import org.junit.Before; +import org.junit.Test; + +public class ExecutorTest { + Article article; + Stream stream; + Spliterator spliterator; + Spliterator
split1; + Spliterator
split2; + + @Before + public void init() { + article = new Article(Arrays.asList(new Author("Ahmad", 0), new Author("Eugen", 0), new Author("Alice", 1), + new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), + new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), + new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), + new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), + new Author("Mike", 0), new Author("Michał", 0), new Author("Loredana", 1)), 0); + stream = article.getListOfAuthors().stream(); + split1 = Executor.generateElements().spliterator(); + split2 = split1.trySplit(); + spliterator = new RelatedAuthorSpliterator(article.getListOfAuthors()); + } + + @Test + public void givenAstreamOfAuthors_whenProcessedInParallelWithCustomSpliterator_coubtProducessRightOutput() { + Stream stream2 = StreamSupport.stream(spliterator, true); + assertThat(Executor.countAutors(stream2.parallel())).isEqualTo(9); + } + + @Test + public void givenSpliterator_whenAppliedToAListOfArticle_thenSplittedInHalf() { + assertThat(new Task(split1).call()).containsSequence(Executor.generateElements().size() / 2 + ""); + assertThat(new Task(split2).call()).containsSequence(Executor.generateElements().size() / 2 + ""); + } +} diff --git a/core-java-9/src/main/java/com/baeldung/java9/methodhandles/Book.java b/core-java-9/src/main/java/com/baeldung/java9/methodhandles/Book.java new file mode 100644 index 0000000000..479f62cb4e --- /dev/null +++ b/core-java-9/src/main/java/com/baeldung/java9/methodhandles/Book.java @@ -0,0 +1,17 @@ +package com.baeldung.java9.methodhandles; + +public class Book { + + String id; + String title; + + public Book(String id, String title) { + this.id = id; + this.title = title; + } + + @SuppressWarnings("unused") + private String formatBook() { + return id + " > " + title; + } +} diff --git a/core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpClientTest.java b/core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpClientTest.java new file mode 100644 index 0000000000..5cf3b9098f --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpClientTest.java @@ -0,0 +1,218 @@ +package com.baeldung.java9.httpclient; + +import jdk.incubator.http.HttpClient; +import jdk.incubator.http.HttpRequest; +import jdk.incubator.http.HttpResponse; +import org.junit.Test; + +import java.io.IOException; +import java.net.*; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.stream.Collectors; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.collection.IsEmptyCollection.empty; +import static org.hamcrest.core.IsNot.not; +import static org.junit.Assert.assertThat; + +/** + * Created by adam. + */ +public class HttpClientTest { + + @Test + public void shouldReturnSampleDataContentWhenConnectViaSystemProxy() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/post")) + .headers("Content-Type", "text/plain;charset=UTF-8") + .POST(HttpRequest.BodyProcessor.fromString("Sample body")) + .build(); + + HttpResponse response = HttpClient.newBuilder() + .proxy(ProxySelector.getDefault()) + .build() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + assertThat(response.body(), containsString("Sample body")); + } + + @Test + public void shouldNotFollowRedirectWhenSetToDefaultNever() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("http://stackoverflow.com")) + .version(HttpClient.Version.HTTP_1_1) + .GET() + .build(); + HttpResponse response = HttpClient.newBuilder() + .build() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_MOVED_PERM)); + assertThat(response.body(), containsString("https://stackoverflow.com/")); + } + + @Test + public void shouldFollowRedirectWhenSetToAlways() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("http://stackoverflow.com")) + .version(HttpClient.Version.HTTP_1_1) + .GET() + .build(); + HttpResponse response = HttpClient.newBuilder() + .followRedirects(HttpClient.Redirect.ALWAYS) + .build() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + assertThat(response.finalRequest() + .uri() + .toString(), equalTo("https://stackoverflow.com/")); + } + + @Test + public void shouldReturnOKStatusForAuthenticatedAccess() throws URISyntaxException, IOException, InterruptedException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/basic-auth")) + .GET() + .build(); + HttpResponse response = HttpClient.newBuilder() + .authenticator(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication("postman", "password".toCharArray()); + } + }) + .build() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + } + + @Test + public void shouldSendRequestAsync() throws URISyntaxException, InterruptedException, ExecutionException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/post")) + .headers("Content-Type", "text/plain;charset=UTF-8") + .POST(HttpRequest.BodyProcessor.fromString("Sample body")) + .build(); + CompletableFuture> response = HttpClient.newBuilder() + .build() + .sendAsync(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.get() + .statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + } + + @Test + public void shouldUseJustTwoThreadWhenProcessingSendAsyncRequest() throws URISyntaxException, InterruptedException, ExecutionException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/get")) + .GET() + .build(); + + ExecutorService executorService = Executors.newFixedThreadPool(2); + + CompletableFuture> response1 = HttpClient.newBuilder() + .executor(executorService) + .build() + .sendAsync(request, HttpResponse.BodyHandler.asString()); + + CompletableFuture> response2 = HttpClient.newBuilder() + .executor(executorService) + .build() + .sendAsync(request, HttpResponse.BodyHandler.asString()); + + CompletableFuture> response3 = HttpClient.newBuilder() + .executor(executorService) + .build() + .sendAsync(request, HttpResponse.BodyHandler.asString()); + + CompletableFuture.allOf(response1, response2, response3) + .join(); + + assertThat(response1.get() + .statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + assertThat(response2.get() + .statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + assertThat(response3.get() + .statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + } + + @Test + public void shouldNotStoreCookieWhenPolicyAcceptNone() throws URISyntaxException, IOException, InterruptedException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/get")) + .GET() + .build(); + + HttpClient httpClient = HttpClient.newBuilder() + .cookieManager(new CookieManager(null, CookiePolicy.ACCEPT_NONE)) + .build(); + + httpClient.send(request, HttpResponse.BodyHandler.asString()); + + assertThat(httpClient.cookieManager() + .get() + .getCookieStore() + .getCookies(), empty()); + } + + @Test + public void shouldStoreCookieWhenPolicyAcceptAll() throws URISyntaxException, IOException, InterruptedException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/get")) + .GET() + .build(); + + HttpClient httpClient = HttpClient.newBuilder() + .cookieManager(new CookieManager(null, CookiePolicy.ACCEPT_ALL)) + .build(); + + httpClient.send(request, HttpResponse.BodyHandler.asString()); + + assertThat(httpClient.cookieManager() + .get() + .getCookieStore() + .getCookies(), not(empty())); + } + + @Test + public void shouldProcessMultipleRequestViaStream() throws URISyntaxException, ExecutionException, InterruptedException { + List targets = Arrays.asList(new URI("https://postman-echo.com/get?foo1=bar1"), new URI("https://postman-echo.com/get?foo2=bar2")); + + HttpClient client = HttpClient.newHttpClient(); + + List> futures = targets.stream() + .map(target -> client.sendAsync(HttpRequest.newBuilder(target) + .GET() + .build(), HttpResponse.BodyHandler.asString()) + .thenApply(response -> response.body())) + .collect(Collectors.toList()); + + CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])) + .join(); + + if (futures.get(0) + .get() + .contains("foo1")) { + assertThat(futures.get(0) + .get(), containsString("bar1")); + assertThat(futures.get(1) + .get(), containsString("bar2")); + } else { + assertThat(futures.get(1) + .get(), containsString("bar2")); + assertThat(futures.get(1) + .get(), containsString("bar1")); + } + + } + +} diff --git a/core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpRequestTest.java b/core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpRequestTest.java new file mode 100644 index 0000000000..7c0e9a90e0 --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpRequestTest.java @@ -0,0 +1,171 @@ +package com.baeldung.java9.httpclient; + +import jdk.incubator.http.HttpClient; +import jdk.incubator.http.HttpRequest; +import jdk.incubator.http.HttpResponse; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.file.Paths; +import java.security.NoSuchAlgorithmException; +import java.time.Duration; + +import static java.time.temporal.ChronoUnit.SECONDS; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; + +/** + * Created by adam. + */ +public class HttpRequestTest { + + @Test + public void shouldReturnStatusOKWhenSendGetRequest() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/get")) + .GET() + .build(); + + HttpResponse response = HttpClient.newHttpClient() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + } + + @Test + public void shouldUseHttp2WhenWebsiteUsesHttp2() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://stackoverflow.com")) + .version(HttpClient.Version.HTTP_2) + .GET() + .build(); + HttpResponse response = HttpClient.newHttpClient() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + assertThat(response.version(), equalTo(HttpClient.Version.HTTP_2)); + } + + @Test + public void shouldFallbackToHttp1_1WhenWebsiteDoesNotUseHttp2() throws IOException, InterruptedException, URISyntaxException, NoSuchAlgorithmException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/get")) + .version(HttpClient.Version.HTTP_2) + .GET() + .build(); + + HttpResponse response = HttpClient.newHttpClient() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.version(), equalTo(HttpClient.Version.HTTP_1_1)); + } + + @Test + public void shouldReturnStatusOKWhenSendGetRequestWithDummyHeaders() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/get")) + .headers("key1", "value1", "key2", "value2") + .GET() + .build(); + + HttpResponse response = HttpClient.newHttpClient() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + } + + @Test + public void shouldReturnStatusOKWhenSendGetRequestTimeoutSet() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/get")) + .timeout(Duration.of(10, SECONDS)) + .GET() + .build(); + + HttpResponse response = HttpClient.newHttpClient() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + } + + @Test + public void shouldReturnNoContentWhenPostWithNoBody() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/post")) + .POST(HttpRequest.noBody()) + .build(); + + HttpResponse response = HttpClient.newHttpClient() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + } + + @Test + public void shouldReturnSampleDataContentWhenPostWithBodyText() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/post")) + .headers("Content-Type", "text/plain;charset=UTF-8") + .POST(HttpRequest.BodyProcessor.fromString("Sample request body")) + .build(); + + HttpResponse response = HttpClient.newHttpClient() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + assertThat(response.body(), containsString("Sample request body")); + } + + @Test + public void shouldReturnSampleDataContentWhenPostWithInputStream() throws IOException, InterruptedException, URISyntaxException { + byte[] sampleData = "Sample request body".getBytes(); + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/post")) + .headers("Content-Type", "text/plain;charset=UTF-8") + .POST(HttpRequest.BodyProcessor.fromInputStream(() -> new ByteArrayInputStream(sampleData))) + .build(); + + HttpResponse response = HttpClient.newHttpClient() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + assertThat(response.body(), containsString("Sample request body")); + } + + @Test + public void shouldReturnSampleDataContentWhenPostWithByteArrayProcessorStream() throws IOException, InterruptedException, URISyntaxException { + byte[] sampleData = "Sample request body".getBytes(); + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/post")) + .headers("Content-Type", "text/plain;charset=UTF-8") + .POST(HttpRequest.BodyProcessor.fromByteArray(sampleData)) + .build(); + + HttpResponse response = HttpClient.newHttpClient() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + assertThat(response.body(), containsString("Sample request body")); + } + + @Test + public void shouldReturnSampleDataContentWhenPostWithFileProcessorStream() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/post")) + .headers("Content-Type", "text/plain;charset=UTF-8") + .POST(HttpRequest.BodyProcessor.fromFile(Paths.get("src/test/resources/sample.txt"))) + .build(); + + HttpResponse response = HttpClient.newHttpClient() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + assertThat(response.body(), containsString("Sample file content")); + } + +} diff --git a/core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpResponseTest.java b/core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpResponseTest.java new file mode 100644 index 0000000000..80295ff34c --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpResponseTest.java @@ -0,0 +1,55 @@ +package com.baeldung.java9.httpclient; + +import jdk.incubator.http.HttpClient; +import jdk.incubator.http.HttpRequest; +import jdk.incubator.http.HttpResponse; +import org.junit.Test; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URI; +import java.net.URISyntaxException; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.Matchers.isEmptyString; +import static org.hamcrest.core.IsNot.not; +import static org.junit.Assert.assertThat; + +/** + * Created by adam. + */ +public class HttpResponseTest { + + @Test + public void shouldReturnStatusOKWhenSendGetRequest() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/get")) + .GET() + .build(); + + HttpResponse response = HttpClient.newHttpClient() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + assertThat(response.body(), not(isEmptyString())); + } + + @Test + public void shouldResponseURIDifferentThanRequestUIRWhenRedirect() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("http://stackoverflow.com")) + .version(HttpClient.Version.HTTP_1_1) + .GET() + .build(); + HttpResponse response = HttpClient.newBuilder() + .followRedirects(HttpClient.Redirect.ALWAYS) + .build() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(request.uri() + .toString(), equalTo("http://stackoverflow.com")); + assertThat(response.uri() + .toString(), equalTo("https://stackoverflow.com/")); + } + +} diff --git a/core-java-9/src/test/java/com/baeldung/java9/httpclient/SimpleHttpRequestsUnitTest.java b/core-java-9/src/test/java/com/baeldung/java9/httpclient/SimpleHttpRequestsUnitTest.java deleted file mode 100644 index 4a704139a1..0000000000 --- a/core-java-9/src/test/java/com/baeldung/java9/httpclient/SimpleHttpRequestsUnitTest.java +++ /dev/null @@ -1,126 +0,0 @@ -package com.baeldung.java9.httpclient; - -import static java.net.HttpURLConnection.HTTP_OK; -import static org.junit.Assert.assertTrue; - -import java.io.IOException; -import java.net.CookieManager; -import java.net.CookiePolicy; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.http.HttpClient; -import java.net.http.HttpHeaders; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; -import java.security.NoSuchAlgorithmException; -import java.util.List; -import java.util.Map; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; - -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLParameters; - -import org.junit.Before; -import org.junit.Test; - -public class SimpleHttpRequestsUnitTest { - - private URI httpURI; - - @Before - public void init() throws URISyntaxException { - httpURI = new URI("http://www.baeldung.com/"); - } - - @Test - public void quickGet() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.create(httpURI).GET(); - HttpResponse response = request.response(); - int responseStatusCode = response.statusCode(); - String responseBody = response.body(HttpResponse.asString()); - assertTrue("Get response status code is bigger then 400", responseStatusCode < 400); - } - - @Test - public void asynchronousGet() throws URISyntaxException, IOException, InterruptedException, ExecutionException { - HttpRequest request = HttpRequest.create(httpURI).GET(); - long before = System.currentTimeMillis(); - CompletableFuture futureResponse = request.responseAsync(); - futureResponse.thenAccept(response -> { - String responseBody = response.body(HttpResponse.asString()); - }); - HttpResponse resp = futureResponse.get(); - HttpHeaders hs = resp.headers(); - assertTrue("There should be more then 1 header.", hs.map().size() > 1); - } - - @Test - public void postMehtod() throws URISyntaxException, IOException, InterruptedException { - HttpRequest.Builder requestBuilder = HttpRequest.create(httpURI); - requestBuilder.body(HttpRequest.fromString("param1=foo,param2=bar")).followRedirects(HttpClient.Redirect.SECURE); - HttpRequest request = requestBuilder.POST(); - HttpResponse response = request.response(); - int statusCode = response.statusCode(); - assertTrue("HTTP return code", statusCode == HTTP_OK); - } - - @Test - public void configureHttpClient() throws NoSuchAlgorithmException, URISyntaxException, IOException, InterruptedException { - CookieManager cManager = new CookieManager(); - cManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER); - - SSLParameters sslParam = new SSLParameters(new String[] { "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" }, new String[] { "TLSv1.2" }); - - HttpClient.Builder hcBuilder = HttpClient.create(); - hcBuilder.cookieManager(cManager).sslContext(SSLContext.getDefault()).sslParameters(sslParam); - HttpClient httpClient = hcBuilder.build(); - HttpRequest.Builder reqBuilder = httpClient.request(new URI("https://www.facebook.com")); - - HttpRequest request = reqBuilder.followRedirects(HttpClient.Redirect.ALWAYS).GET(); - HttpResponse response = request.response(); - int statusCode = response.statusCode(); - assertTrue("HTTP return code", statusCode == HTTP_OK); - } - - SSLParameters getDefaultSSLParameters() throws NoSuchAlgorithmException { - SSLParameters sslP1 = SSLContext.getDefault().getSupportedSSLParameters(); - String[] proto = sslP1.getApplicationProtocols(); - String[] cifers = sslP1.getCipherSuites(); - System.out.println(printStringArr(proto)); - System.out.println(printStringArr(cifers)); - return sslP1; - } - - String printStringArr(String... args) { - if (args == null) { - return null; - } - StringBuilder sb = new StringBuilder(); - for (String s : args) { - sb.append(s); - sb.append("\n"); - } - return sb.toString(); - } - - String printHeaders(HttpHeaders h) { - if (h == null) { - return null; - } - - StringBuilder sb = new StringBuilder(); - Map> hMap = h.map(); - for (String k : hMap.keySet()) { - sb.append(k).append(":"); - List l = hMap.get(k); - if (l != null) { - l.forEach(s -> { - sb.append(s).append(","); - }); - } - sb.append("\n"); - } - return sb.toString(); - } -} diff --git a/core-java-9/src/test/java/com/baeldung/java9/methodhandles/MethodHandlesTest.java b/core-java-9/src/test/java/com/baeldung/java9/methodhandles/MethodHandlesTest.java new file mode 100644 index 0000000000..7646755358 --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/methodhandles/MethodHandlesTest.java @@ -0,0 +1,152 @@ +package com.baeldung.java9.methodhandles; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.*; + +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; +import java.lang.invoke.WrongMethodTypeException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.List; + +import org.junit.Test; + +/** + * Test case for the {@link MethodHandles} API + */ +public class MethodHandlesTest { + + @Test + public void givenConcatMethodHandle_whenInvoked_thenCorrectlyConcatenated() throws Throwable { + + MethodHandles.Lookup publicLookup = MethodHandles.publicLookup(); + MethodType mt = MethodType.methodType(String.class, String.class); + MethodHandle concatMH = publicLookup.findVirtual(String.class, "concat", mt); + + String output = (String) concatMH.invoke("Effective ", "Java"); + + assertEquals("Effective Java", output); + } + + @Test + public void givenAsListMethodHandle_whenInvokingWithArguments_thenCorrectlyInvoked() throws Throwable { + MethodHandles.Lookup publicLookup = MethodHandles.publicLookup(); + MethodType mt = MethodType.methodType(List.class, Object[].class); + MethodHandle asListMH = publicLookup.findStatic(Arrays.class, "asList", mt); + + List list = (List) asListMH.invokeWithArguments(1, 2); + + assertThat(Arrays.asList(1, 2), is(list)); + } + + @Test + public void givenConstructorMethodHandle_whenInvoked_thenObjectCreatedCorrectly() throws Throwable { + MethodHandles.Lookup publicLookup = MethodHandles.publicLookup(); + MethodType mt = MethodType.methodType(void.class, String.class); + MethodHandle newIntegerMH = publicLookup.findConstructor(Integer.class, mt); + + Integer integer = (Integer) newIntegerMH.invoke("1"); + + assertEquals(1, integer.intValue()); + } + + @Test + public void givenAFieldWithoutGetter_whenCreatingAGetter_thenCorrectlyInvoked() throws Throwable { + MethodHandles.Lookup lookup = MethodHandles.lookup(); + MethodHandle getTitleMH = lookup.findGetter(Book.class, "title", String.class); + + Book book = new Book("ISBN-1234", "Effective Java"); + + assertEquals("Effective Java", getTitleMH.invoke(book)); + } + + @Test + public void givenPrivateMethod_whenCreatingItsMethodHandle_thenCorrectlyInvoked() throws Throwable { + MethodHandles.Lookup lookup = MethodHandles.lookup(); + Method formatBookMethod = Book.class.getDeclaredMethod("formatBook"); + formatBookMethod.setAccessible(true); + + MethodHandle formatBookMH = lookup.unreflect(formatBookMethod); + + Book book = new Book("ISBN-123", "Java in Action"); + + assertEquals("ISBN-123 > Java in Action", formatBookMH.invoke(book)); + } + + @Test + public void givenReplaceMethod_whenUsingReflectionAndInvoked_thenCorrectlyReplaced() throws Throwable { + Method replaceMethod = String.class.getMethod("replace", char.class, char.class); + + String string = (String) replaceMethod.invoke("jovo", 'o', 'a'); + + assertEquals("java", string); + } + + @Test + public void givenReplaceMethodHandle_whenInvoked_thenCorrectlyReplaced() throws Throwable { + MethodHandles.Lookup publicLookup = MethodHandles.publicLookup(); + MethodType mt = MethodType.methodType(String.class, char.class, char.class); + MethodHandle replaceMH = publicLookup.findVirtual(String.class, "replace", mt); + + String replacedString = (String) replaceMH.invoke("jovo", Character.valueOf('o'), 'a'); + + assertEquals("java", replacedString); + } + + @Test + public void givenReplaceMethodHandle_whenInvokingExact_thenCorrectlyReplaced() throws Throwable { + MethodHandles.Lookup lookup = MethodHandles.lookup(); + MethodType mt = MethodType.methodType(String.class, char.class, char.class); + MethodHandle replaceMH = lookup.findVirtual(String.class, "replace", mt); + + String s = (String) replaceMH.invokeExact("jovo", 'o', 'a'); + + assertEquals("java", s); + } + + @Test + public void givenSumMethodHandle_whenInvokingExact_thenSumIsCorrect() throws Throwable { + MethodHandles.Lookup lookup = MethodHandles.lookup(); + MethodType mt = MethodType.methodType(int.class, int.class, int.class); + MethodHandle sumMH = lookup.findStatic(Integer.class, "sum", mt); + + int sum = (int) sumMH.invokeExact(1, 11); + + assertEquals(12, sum); + } + + @Test(expected = WrongMethodTypeException.class) + public void givenSumMethodHandleAndIncompatibleArguments_whenInvokingExact_thenException() throws Throwable { + MethodHandles.Lookup lookup = MethodHandles.lookup(); + MethodType mt = MethodType.methodType(int.class, int.class, int.class); + MethodHandle sumMH = lookup.findStatic(Integer.class, "sum", mt); + + sumMH.invokeExact(Integer.valueOf(1), 11); + } + + @Test + public void givenSpreadedEqualsMethodHandle_whenInvokedOnArray_thenCorrectlyEvaluated() throws Throwable { + MethodHandles.Lookup publicLookup = MethodHandles.publicLookup(); + MethodType mt = MethodType.methodType(boolean.class, Object.class); + MethodHandle equalsMH = publicLookup.findVirtual(String.class, "equals", mt); + + MethodHandle methodHandle = equalsMH.asSpreader(Object[].class, 2); + + assertTrue((boolean) methodHandle.invoke(new Object[] { "java", "java" })); + assertFalse((boolean) methodHandle.invoke(new Object[] { "java", "jova" })); + } + + @Test + public void givenConcatMethodHandle_whenBindToAString_thenCorrectlyConcatenated() throws Throwable { + MethodHandles.Lookup publicLookup = MethodHandles.publicLookup(); + MethodType mt = MethodType.methodType(String.class, String.class); + MethodHandle concatMH = publicLookup.findVirtual(String.class, "concat", mt); + + MethodHandle bindedConcatMH = concatMH.bindTo("Hello "); + + assertEquals("Hello World!", bindedConcatMH.invoke("World!")); + } +} diff --git a/core-java-concurrency/README.md b/core-java-concurrency/README.md index 23509013d5..8122c71bcb 100644 --- a/core-java-concurrency/README.md +++ b/core-java-concurrency/README.md @@ -33,3 +33,4 @@ - [Daemon Threads in Java](http://www.baeldung.com/java-daemon-thread) - [Implementing a Runnable vs Extending a Thread](http://www.baeldung.com/java-runnable-vs-extending-thread) - [How to Kill a Java Thread](http://www.baeldung.com/java-thread-stop) +- [ExecutorService - Waiting for Threads to Finish](http://www.baeldung.com/java-executor-wait-for-threads) diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java new file mode 100644 index 0000000000..9900d1c63d --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java @@ -0,0 +1,25 @@ +package com.baeldung.concurrent.prioritytaskexecution; + +public class Job implements Runnable { + private String jobName; + private JobPriority jobPriority; + + public Job(String jobName, JobPriority jobPriority) { + this.jobName = jobName; + this.jobPriority = jobPriority != null ? jobPriority : JobPriority.MEDIUM; + } + + public JobPriority getJobPriority() { + return jobPriority; + } + + @Override + public void run() { + try { + System.out.println("Job:" + jobName + + " Priority:" + jobPriority); + Thread.sleep(1000); + } catch (InterruptedException ignored) { + } + } +} \ No newline at end of file diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/JobPriority.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/JobPriority.java new file mode 100644 index 0000000000..d8092a9ce7 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/JobPriority.java @@ -0,0 +1,7 @@ +package com.baeldung.concurrent.prioritytaskexecution; + +public enum JobPriority { + HIGH, + MEDIUM, + LOW +} diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobScheduler.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobScheduler.java new file mode 100644 index 0000000000..ba55696d5a --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobScheduler.java @@ -0,0 +1,56 @@ +package com.baeldung.concurrent.prioritytaskexecution; + +import java.util.Comparator; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.PriorityBlockingQueue; +import java.util.concurrent.TimeUnit; + +public class PriorityJobScheduler { + + private ExecutorService priorityJobPoolExecutor; + private ExecutorService priorityJobScheduler = + Executors.newSingleThreadExecutor(); + private PriorityBlockingQueue priorityQueue; + + public PriorityJobScheduler(Integer poolSize, Integer queueSize) { + priorityJobPoolExecutor = Executors.newFixedThreadPool(poolSize); + priorityQueue = new PriorityBlockingQueue(queueSize, + Comparator.comparing(Job::getJobPriority)); + + priorityJobScheduler.execute(()->{ + while (true) { + try { + priorityJobPoolExecutor.execute(priorityQueue.take()); + } catch (InterruptedException e) { + // exception needs special handling + break; + } + } + }); + } + + public void scheduleJob(Job job) { + priorityQueue.add(job); + } + + public int getQueuedTaskCount() { + return priorityQueue.size(); + } + + protected void close(ExecutorService scheduler) { + scheduler.shutdown(); + try { + if (!scheduler.awaitTermination(5, TimeUnit.SECONDS)) { + scheduler.shutdownNow(); + } + } catch (InterruptedException e) { + scheduler.shutdownNow(); + } + } + + public void closeScheduler() { + close(priorityJobPoolExecutor); + close(priorityJobScheduler); + } +} diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/BlockedState.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/BlockedState.java new file mode 100644 index 0000000000..19c6d08c2d --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/BlockedState.java @@ -0,0 +1,31 @@ +package com.baeldung.concurrent.threadlifecycle; + +public class BlockedState { + public static void main(String[] args) throws InterruptedException { + Thread t1 = new Thread(new DemoThreadB()); + Thread t2 = new Thread(new DemoThreadB()); + + t1.start(); + t2.start(); + + Thread.sleep(1000); + + System.out.println(t2.getState()); + System.exit(0); + } +} + +class DemoThreadB implements Runnable { + @Override + public void run() { + commonResource(); + } + + public static synchronized void commonResource() { + while(true) { + // Infinite loop to mimic heavy processing + // Thread 't1' won't leave this method + // when Thread 't2' enters this + } + } +} \ No newline at end of file diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/NewState.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/NewState.java new file mode 100644 index 0000000000..b524cc5ec7 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/NewState.java @@ -0,0 +1,14 @@ +package com.baeldung.concurrent.threadlifecycle; + +public class NewState implements Runnable { + public static void main(String[] args) { + Runnable runnable = new NewState(); + Thread t = new Thread(runnable); + System.out.println(t.getState()); + } + + @Override + public void run() { + + } +} \ No newline at end of file diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/RunnableState.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/RunnableState.java new file mode 100644 index 0000000000..4aa8b36a76 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/RunnableState.java @@ -0,0 +1,15 @@ +package com.baeldung.concurrent.threadlifecycle; + +public class RunnableState implements Runnable { + public static void main(String[] args) { + Runnable runnable = new NewState(); + Thread t = new Thread(runnable); + t.start(); + System.out.println(t.getState()); + } + + @Override + public void run() { + + } +} \ No newline at end of file diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/TerminatedState.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/TerminatedState.java new file mode 100644 index 0000000000..7c8884dc22 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/TerminatedState.java @@ -0,0 +1,15 @@ +package com.baeldung.concurrent.threadlifecycle; + +public class TerminatedState implements Runnable { + public static void main(String[] args) throws InterruptedException { + Thread t1 = new Thread(new TerminatedState()); + t1.start(); + Thread.sleep(1000); + System.out.println(t1.getState()); + } + + @Override + public void run() { + // No processing in this block + } +} \ No newline at end of file diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/TimedWaitingState.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/TimedWaitingState.java new file mode 100644 index 0000000000..8d005352eb --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/TimedWaitingState.java @@ -0,0 +1,25 @@ +package com.baeldung.concurrent.threadlifecycle; + +public class TimedWaitingState { + public static void main(String[] args) throws InterruptedException { + DemoThread obj1 = new DemoThread(); + Thread t1 = new Thread(obj1); + t1.start(); + // The following sleep will give enough time for ThreadScheduler + // to start processing of thread t1 + Thread.sleep(1000); + System.out.println(t1.getState()); + } +} + +class DemoThread implements Runnable { + @Override + public void run() { + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/WaitingState.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/WaitingState.java new file mode 100644 index 0000000000..98a6844309 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/WaitingState.java @@ -0,0 +1,35 @@ +package com.baeldung.concurrent.threadlifecycle; + +public class WaitingState implements Runnable { + public static Thread t1; + + public static void main(String[] args) { + t1 = new Thread(new WaitingState()); + t1.start(); + } + + public void run() { + Thread t2 = new Thread(new DemoThreadWS()); + t2.start(); + + try { + t2.join(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + e.printStackTrace(); + } + } +} + +class DemoThreadWS implements Runnable { + public void run() { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + e.printStackTrace(); + } + + System.out.println(WaitingState.t1.getState()); + } +} \ No newline at end of file diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Data.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Data.java index 9b850c4153..9d76c1fcd1 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Data.java +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Data.java @@ -11,7 +11,10 @@ public class Data { while (transfer) { try { wait(); - } catch (InterruptedException e) {} + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + System.out.println("Thread Interrupted"); + } } transfer = true; @@ -23,7 +26,10 @@ public class Data { while (!transfer) { try { wait(); - } catch (InterruptedException e) {} + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + System.out.println("Thread Interrupted"); + } } transfer = false; diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Receiver.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Receiver.java index 63f48b8031..21ba822bfd 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Receiver.java +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Receiver.java @@ -19,7 +19,10 @@ public class Receiver implements Runnable { //Thread.sleep() to mimic heavy server-side processing try { Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000)); - } catch (InterruptedException e) {} + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + System.out.println("Thread Interrupted"); + } } } } \ No newline at end of file diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Sender.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Sender.java index b7d782c3f5..c365294cdd 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Sender.java +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Sender.java @@ -11,11 +11,11 @@ public class Sender implements Runnable { public void run() { String packets[] = { - "First packet", - "Second packet", - "Third packet", - "Fourth packet", - "End" + "First packet", + "Second packet", + "Third packet", + "Fourth packet", + "End" }; for (String packet : packets) { @@ -24,7 +24,10 @@ public class Sender implements Runnable { //Thread.sleep() to mimic heavy server-side processing try { Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000)); - } catch (InterruptedException e) {} + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + System.out.println("Thread Interrupted"); + } } } } \ No newline at end of file diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java new file mode 100644 index 0000000000..1e67fe45c1 --- /dev/null +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java @@ -0,0 +1,40 @@ +package com.baeldung.concurrent.prioritytaskexecution; + +import org.junit.Test; + +public class PriorityJobSchedulerUnitTest { + private static int POOL_SIZE = 1; + private static int QUEUE_SIZE = 10; + + @Test + public void whenMultiplePriorityJobsQueued_thenHighestPriorityJobIsPicked() { + Job job1 = new Job("Job1", JobPriority.LOW); + Job job2 = new Job("Job2", JobPriority.MEDIUM); + Job job3 = new Job("Job3", JobPriority.HIGH); + Job job4 = new Job("Job4", JobPriority.MEDIUM); + Job job5 = new Job("Job5", JobPriority.LOW); + Job job6 = new Job("Job6", JobPriority.HIGH); + + PriorityJobScheduler pjs = new PriorityJobScheduler(POOL_SIZE, QUEUE_SIZE); + + pjs.scheduleJob(job1); + pjs.scheduleJob(job2); + pjs.scheduleJob(job3); + pjs.scheduleJob(job4); + pjs.scheduleJob(job5); + pjs.scheduleJob(job6); + + // ensure no tasks is pending before closing the scheduler + while (pjs.getQueuedTaskCount() != 0); + + // delay to avoid job sleep (added for demo) being interrupted + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new RuntimeException(e); + } + + pjs.closeScheduler(); + } +} diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/stopping/StopThreadTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/stopping/StopThreadManualTest.java similarity index 97% rename from core-java-concurrency/src/test/java/com/baeldung/concurrent/stopping/StopThreadTest.java rename to core-java-concurrency/src/test/java/com/baeldung/concurrent/stopping/StopThreadManualTest.java index af54d6932e..9ea40824ca 100644 --- a/core-java-concurrency/src/test/java/com/baeldung/concurrent/stopping/StopThreadTest.java +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/stopping/StopThreadManualTest.java @@ -10,7 +10,7 @@ import org.junit.Test; import com.jayway.awaitility.Awaitility; -public class StopThreadTest { +public class StopThreadManualTest { @Test public void whenStoppedThreadIsStopped() throws InterruptedException { diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/waitandnotify/NetworkIntegrationTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/waitandnotify/NetworkIntegrationTest.java index 49f4313e9d..e2bc328df3 100644 --- a/core-java-concurrency/src/test/java/com/baeldung/concurrent/waitandnotify/NetworkIntegrationTest.java +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/waitandnotify/NetworkIntegrationTest.java @@ -13,38 +13,38 @@ import org.junit.Test; public class NetworkIntegrationTest { - private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); - private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); - private String expected; - - @Before - public void setUpStreams() { - System.setOut(new PrintStream(outContent)); - System.setErr(new PrintStream(errContent)); - } - - @Before - public void setUpExpectedOutput() { - StringWriter expectedStringWriter = new StringWriter(); - - PrintWriter printWriter = new PrintWriter(expectedStringWriter); - printWriter.println("First packet"); - printWriter.println("Second packet"); - printWriter.println("Third packet"); - printWriter.println("Fourth packet"); - printWriter.close(); - - expected = expectedStringWriter.toString(); - } + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); + private String expected; + + @Before + public void setUpStreams() { + System.setOut(new PrintStream(outContent)); + System.setErr(new PrintStream(errContent)); + } + + @Before + public void setUpExpectedOutput() { + StringWriter expectedStringWriter = new StringWriter(); + + PrintWriter printWriter = new PrintWriter(expectedStringWriter); + printWriter.println("First packet"); + printWriter.println("Second packet"); + printWriter.println("Third packet"); + printWriter.println("Fourth packet"); + printWriter.close(); + + expected = expectedStringWriter.toString(); + } - @After - public void cleanUpStreams() { - System.setOut(null); - System.setErr(null); - } - - @Test - public void givenSenderAndReceiver_whenSendingPackets_thenNetworkSynchronized() { + @After + public void cleanUpStreams() { + System.setOut(null); + System.setErr(null); + } + + @Test + public void givenSenderAndReceiver_whenSendingPackets_thenNetworkSynchronized() { Data data = new Data(); Thread sender = new Thread(new Sender(data)); Thread receiver = new Thread(new Receiver(data)); @@ -54,12 +54,13 @@ public class NetworkIntegrationTest { //wait for sender and receiver to finish before we test against expected try { - sender.join(); - receiver.join(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - assertEquals(expected, outContent.toString()); - } + sender.join(); + receiver.join(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + System.out.println("Thread Interrupted"); + } + + assertEquals(expected, outContent.toString()); + } } diff --git a/core-java-io/.gitignore b/core-java-io/.gitignore index 3de4cc647e..c61d35324d 100644 --- a/core-java-io/.gitignore +++ b/core-java-io/.gitignore @@ -1,26 +1,5 @@ -*.class - 0.* -#folders# -/target -/neoDb* -/data -/src/main/webapp/WEB-INF/classes -*/META-INF/* -.resourceCache - -# Packaged files # -*.jar -*.war -*.ear - # Files generated by integration tests -*.txt -backup-pom.xml -/bin/ -/temp - -#IntelliJ specific -.idea/ -*.iml \ No newline at end of file +# *.txt +/temp \ No newline at end of file diff --git a/core-java-io/pom.xml b/core-java-io/pom.xml index 9aa8743aa6..b9fdca3502 100644 --- a/core-java-io/pom.xml +++ b/core-java-io/pom.xml @@ -1,5 +1,4 @@ - + 4.0.0 com.baeldung core-java-io @@ -211,16 +210,6 @@ jmh-generator-annprocess 1.19 - - org.springframework - spring-web - 4.3.4.RELEASE - - - org.springframework.boot - spring-boot-starter - 1.5.8.RELEASE - org.hsqldb hsqldb @@ -264,99 +253,6 @@ - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - - - - org.apache.maven.plugins - maven-jar-plugin - - - - true - libs/ - org.baeldung.executable.ExecutableMavenJar - - - - - - - org.apache.maven.plugins - maven-assembly-plugin - - - package - - single - - - ${project.basedir} - - - org.baeldung.executable.ExecutableMavenJar - - - - jar-with-dependencies - - - - - - - - org.apache.maven.plugins - maven-shade-plugin - - - - shade - - - true - - - org.baeldung.executable.ExecutableMavenJar - - - - - - - - - com.jolira - onejar-maven-plugin - - - - org.baeldung.executable.ExecutableMavenJar - true - ${project.build.finalName}-onejar.${project.packaging} - - - one-jar - - - - - org.springframework.boot spring-boot-maven-plugin @@ -384,19 +280,19 @@ -Xmx300m -XX:+UseParallelGC -classpath - + com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed - + org.apache.maven.plugins maven-javadoc-plugin 3.0.0-M1 1.8 - 1.8 + 1.8 @@ -442,7 +338,7 @@ run-benchmarks - + none exec @@ -452,7 +348,7 @@ java -classpath - + org.openjdk.jmh.Main .* @@ -490,7 +386,7 @@ 1.13 0.6.5 0.9.0 - + 1.3 4.12 diff --git a/core-java-io/src/main/java/com/baeldung/java/nio2/watcher/DirectoryWatcherExample.java b/core-java-io/src/main/java/com/baeldung/java/nio2/watcher/DirectoryWatcherExample.java index 35955032dc..4c35ffdb22 100644 --- a/core-java-io/src/main/java/com/baeldung/java/nio2/watcher/DirectoryWatcherExample.java +++ b/core-java-io/src/main/java/com/baeldung/java/nio2/watcher/DirectoryWatcherExample.java @@ -10,7 +10,7 @@ import java.nio.file.WatchKey; import java.nio.file.WatchService; public class DirectoryWatcherExample { - + public static void main(String[] args) throws IOException, InterruptedException { WatchService watchService = FileSystems.getDefault().newWatchService(); Path path = Paths.get(System.getProperty("user.home")); @@ -25,5 +25,5 @@ public class DirectoryWatcherExample { watchService.close(); } - + } diff --git a/core-java-io/src/main/resources/targetFile.tmp b/core-java-io/src/main/resources/targetFile.tmp deleted file mode 100644 index 20f137b416..0000000000 --- a/core-java-io/src/main/resources/targetFile.tmp +++ /dev/null @@ -1,2 +0,0 @@ -line 1 -a second line \ No newline at end of file diff --git a/core-java-io/src/test/java/com/baeldung/copyfiles/FileCopierTest.java b/core-java-io/src/test/java/com/baeldung/copyfiles/FileCopierTest.java index 973436a26a..6d96d2fc0b 100644 --- a/core-java-io/src/test/java/com/baeldung/copyfiles/FileCopierTest.java +++ b/core-java-io/src/test/java/com/baeldung/copyfiles/FileCopierTest.java @@ -19,52 +19,51 @@ import org.junit.Test; import static org.assertj.core.api.Assertions.*; public class FileCopierTest { - File original = new File("src/test/resources/original.txt"); + File original = new File("src/test/resources/original.txt"); - @Before - public void init() throws IOException { - if (!original.exists()) - Files.createFile(original.toPath()); - } + @Before + public void init() throws IOException { + if (!original.exists()) + Files.createFile(original.toPath()); + } - @Test - public void givenIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException { - File copied = new File("src/test/resources/copiedWithIo.txt"); - try (InputStream in = new BufferedInputStream(new FileInputStream(original)); - OutputStream out = new BufferedOutputStream(new FileOutputStream(copied))) { - byte[] buffer = new byte[1024]; - int lengthRead; - while ((lengthRead = in.read(buffer)) > 0) { - out.write(buffer, 0, lengthRead); - out.flush(); - } - } - assertThat(copied).exists(); - assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath()))); - } + @Test + public void givenIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException { + File copied = new File("src/test/resources/copiedWithIo.txt"); + try (InputStream in = new BufferedInputStream(new FileInputStream(original)); OutputStream out = new BufferedOutputStream(new FileOutputStream(copied))) { + byte[] buffer = new byte[1024]; + int lengthRead; + while ((lengthRead = in.read(buffer)) > 0) { + out.write(buffer, 0, lengthRead); + out.flush(); + } + } + assertThat(copied).exists(); + assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath()))); + } - @Test - public void givenCommonsIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException { - File copied = new File("src/test/resources/copiedWithApacheCommons.txt"); - FileUtils.copyFile(original, copied); - assertThat(copied).exists(); - assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath()))); - } + @Test + public void givenCommonsIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException { + File copied = new File("src/test/resources/copiedWithApacheCommons.txt"); + FileUtils.copyFile(original, copied); + assertThat(copied).exists(); + assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath()))); + } - @Test - public void givenNIO2_whenCopied_thenCopyExistsWithSameContents() throws IOException { - Path copied = Paths.get("src/test/resources/copiedWithNio.txt"); - Path originalPath = original.toPath(); - Files.copy(originalPath, copied, StandardCopyOption.REPLACE_EXISTING); - assertThat(copied).exists(); - assertThat(Files.readAllLines(originalPath).equals(Files.readAllLines(copied))); - } + @Test + public void givenNIO2_whenCopied_thenCopyExistsWithSameContents() throws IOException { + Path copied = Paths.get("src/test/resources/copiedWithNio.txt"); + Path originalPath = original.toPath(); + Files.copy(originalPath, copied, StandardCopyOption.REPLACE_EXISTING); + assertThat(copied).exists(); + assertThat(Files.readAllLines(originalPath).equals(Files.readAllLines(copied))); + } - @Test - public void givenGuava_whenCopied_thenCopyExistsWithSameContents() throws IOException { - File copied = new File("src/test/resources/copiedWithApacheCommons.txt"); - com.google.common.io.Files.copy(original, copied); - assertThat(copied).exists(); - assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath()))); - } + @Test + public void givenGuava_whenCopied_thenCopyExistsWithSameContents() throws IOException { + File copied = new File("src/test/resources/copiedWithApacheCommons.txt"); + com.google.common.io.Files.copy(original, copied); + assertThat(copied).exists(); + assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath()))); + } } diff --git a/core-java-io/src/test/java/com/baeldung/file/FileOperationsManualTest.java b/core-java-io/src/test/java/com/baeldung/file/FileOperationsManualTest.java index ea71d1b5c1..7968967679 100644 --- a/core-java-io/src/test/java/com/baeldung/file/FileOperationsManualTest.java +++ b/core-java-io/src/test/java/com/baeldung/file/FileOperationsManualTest.java @@ -3,7 +3,6 @@ package com.baeldung.file; import org.apache.commons.io.FileUtils; import org.hamcrest.CoreMatchers; import org.hamcrest.Matchers; -import org.junit.Assert; import org.junit.Test; import java.io.BufferedReader; diff --git a/core-java-io/src/test/java/com/baeldung/file/FilesTest.java b/core-java-io/src/test/java/com/baeldung/file/FilesTest.java index c5a5b8a3a1..a35cda8b23 100644 --- a/core-java-io/src/test/java/com/baeldung/file/FilesTest.java +++ b/core-java-io/src/test/java/com/baeldung/file/FilesTest.java @@ -42,19 +42,14 @@ public class FilesTest { CharSink chs = com.google.common.io.Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND); chs.write("Spain\r\n"); - assertThat(StreamUtils.getStringFromInputStream( - new FileInputStream(fileName))) - .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); + assertThat(StreamUtils.getStringFromInputStream(new FileInputStream(fileName))).isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); } - @Test public void whenAppendToFileUsingFiles_thenCorrect() throws IOException { Files.write(Paths.get(fileName), "Spain\r\n".getBytes(), StandardOpenOption.APPEND); - assertThat(StreamUtils.getStringFromInputStream( - new FileInputStream(fileName))) - .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); + assertThat(StreamUtils.getStringFromInputStream(new FileInputStream(fileName))).isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); } @Test @@ -62,9 +57,7 @@ public class FilesTest { File file = new File(fileName); FileUtils.writeStringToFile(file, "Spain\r\n", StandardCharsets.UTF_8, true); - assertThat(StreamUtils.getStringFromInputStream( - new FileInputStream(fileName))) - .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); + assertThat(StreamUtils.getStringFromInputStream(new FileInputStream(fileName))).isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); } @Test @@ -73,9 +66,7 @@ public class FilesTest { fos.write("Spain\r\n".getBytes()); fos.close(); - assertThat(StreamUtils.getStringFromInputStream( - new FileInputStream(fileName))) - .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); + assertThat(StreamUtils.getStringFromInputStream(new FileInputStream(fileName))).isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); } @Test @@ -86,9 +77,6 @@ public class FilesTest { bw.newLine(); bw.close(); - assertThat( - StreamUtils.getStringFromInputStream( - new FileInputStream(fileName))) - .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\n"); + assertThat(StreamUtils.getStringFromInputStream(new FileInputStream(fileName))).isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\n"); } } \ No newline at end of file diff --git a/core-java-io/src/test/java/com/baeldung/filesystem/jndi/test/LookupFSJNDIIntegrationTest.java b/core-java-io/src/test/java/com/baeldung/filesystem/jndi/test/LookupFSJNDIIntegrationTest.java index 330ec3aee3..023a47cb97 100644 --- a/core-java-io/src/test/java/com/baeldung/filesystem/jndi/test/LookupFSJNDIIntegrationTest.java +++ b/core-java-io/src/test/java/com/baeldung/filesystem/jndi/test/LookupFSJNDIIntegrationTest.java @@ -35,7 +35,7 @@ public class LookupFSJNDIIntegrationTest { @Test public void givenInitialContext_whenLokupFileExists_thenSuccess() { - File file = fsjndi.getFile(FILENAME); - assertNotNull("File exists", file); + File file = fsjndi.getFile(FILENAME); + assertNotNull("File exists", file); } } diff --git a/core-java-io/src/test/java/com/baeldung/java/nio2/PathManualTest.java b/core-java-io/src/test/java/com/baeldung/java/nio2/PathManualTest.java index acfb2c08e9..969dff1da2 100644 --- a/core-java-io/src/test/java/com/baeldung/java/nio2/PathManualTest.java +++ b/core-java-io/src/test/java/com/baeldung/java/nio2/PathManualTest.java @@ -9,7 +9,6 @@ import java.net.URI; import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Date; import org.junit.Test; diff --git a/core-java-io/src/test/java/com/baeldung/java/nio2/async/AsyncFileIntegrationTest.java b/core-java-io/src/test/java/com/baeldung/java/nio2/async/AsyncFileIntegrationTest.java index e2f7a0303a..cf37b92565 100644 --- a/core-java-io/src/test/java/com/baeldung/java/nio2/async/AsyncFileIntegrationTest.java +++ b/core-java-io/src/test/java/com/baeldung/java/nio2/async/AsyncFileIntegrationTest.java @@ -17,18 +17,19 @@ import java.util.concurrent.Future; import static org.junit.Assert.assertEquals; public class AsyncFileIntegrationTest { + @Test public void givenPath_whenReadsContentWithFuture_thenCorrect() throws IOException, ExecutionException, InterruptedException { - Path path = Paths.get(URI.create(this.getClass().getClassLoader().getResource("file.txt").toString())); - AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ); + final Path path = Paths.get(URI.create(this.getClass().getClassLoader().getResource("file.txt").toString())); + final AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ); - ByteBuffer buffer = ByteBuffer.allocate(1024); + final ByteBuffer buffer = ByteBuffer.allocate(1024); - Future operation = fileChannel.read(buffer, 0); + final Future operation = fileChannel.read(buffer, 0); operation.get(); - String fileContent = new String(buffer.array()).trim(); + final String fileContent = new String(buffer.array()).trim(); buffer.clear(); assertEquals(fileContent, "baeldung.com"); @@ -36,18 +37,16 @@ public class AsyncFileIntegrationTest { @Test public void givenPath_whenReadsContentWithCompletionHandler_thenCorrect() throws IOException { - Path path = Paths.get(URI.create(AsyncFileIntegrationTest.class.getResource("/file.txt").toString())); - AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ); + final Path path = Paths.get(URI.create(this.getClass().getClassLoader().getResource("file.txt").toString())); + final AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ); - ByteBuffer buffer = ByteBuffer.allocate(1024); + final ByteBuffer buffer = ByteBuffer.allocate(1024); fileChannel.read(buffer, 0, buffer, new CompletionHandler() { - @Override public void completed(Integer result, ByteBuffer attachment) { // result is number of bytes read // attachment is the buffer - } @Override @@ -59,42 +58,40 @@ public class AsyncFileIntegrationTest { @Test public void givenPathAndContent_whenWritesToFileWithFuture_thenCorrect() throws IOException, ExecutionException, InterruptedException { - String fileName = "temp"; - Path path = Paths.get(fileName); - AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE); + final String fileName = "temp"; + final Path path = Paths.get(fileName); + final AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE); - ByteBuffer buffer = ByteBuffer.allocate(1024); - long position = 0; + final ByteBuffer buffer = ByteBuffer.allocate(1024); + final long position = 0; buffer.put("hello world".getBytes()); buffer.flip(); - Future operation = fileChannel.write(buffer, position); + final Future operation = fileChannel.write(buffer, position); buffer.clear(); operation.get(); - String content = readContent(path); + final String content = readContent(path); assertEquals("hello world", content); } @Test public void givenPathAndContent_whenWritesToFileWithHandler_thenCorrect() throws IOException { - String fileName = UUID.randomUUID().toString(); - Path path = Paths.get(fileName); - AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.DELETE_ON_CLOSE); + final String fileName = UUID.randomUUID().toString(); + final Path path = Paths.get(fileName); + final AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.DELETE_ON_CLOSE); - ByteBuffer buffer = ByteBuffer.allocate(1024); + final ByteBuffer buffer = ByteBuffer.allocate(1024); buffer.put("hello world".getBytes()); buffer.flip(); fileChannel.write(buffer, 0, buffer, new CompletionHandler() { - @Override public void completed(Integer result, ByteBuffer attachment) { // result is number of bytes written // attachment is the buffer - } @Override @@ -104,23 +101,25 @@ public class AsyncFileIntegrationTest { }); } - public static String readContent(Path file) throws ExecutionException, InterruptedException { + // + + private String readContent(Path file) throws ExecutionException, InterruptedException { AsynchronousFileChannel fileChannel = null; try { fileChannel = AsynchronousFileChannel.open(file, StandardOpenOption.READ); } catch (IOException e) { - // TODO Auto-generated catch block e.printStackTrace(); } - ByteBuffer buffer = ByteBuffer.allocate(1024); + final ByteBuffer buffer = ByteBuffer.allocate(1024); - Future operation = fileChannel.read(buffer, 0); + final Future operation = fileChannel.read(buffer, 0); operation.get(); - String fileContent = new String(buffer.array()).trim(); + final String fileContent = new String(buffer.array()).trim(); buffer.clear(); return fileContent; } + } \ No newline at end of file diff --git a/core-java-io/src/test/java/com/baeldung/java/nio2/attributes/BasicAttribsIntegrationTest.java b/core-java-io/src/test/java/com/baeldung/java/nio2/attributes/BasicAttribsIntegrationTest.java index 9f84aa60d6..4b6302e93c 100644 --- a/core-java-io/src/test/java/com/baeldung/java/nio2/attributes/BasicAttribsIntegrationTest.java +++ b/core-java-io/src/test/java/com/baeldung/java/nio2/attributes/BasicAttribsIntegrationTest.java @@ -20,7 +20,6 @@ public class BasicAttribsIntegrationTest { private static final Logger LOG = LoggerFactory.getLogger(BasicAttribsIntegrationTest.class); - private static final String HOME = System.getProperty("user.home"); private static BasicFileAttributes basicAttribs; diff --git a/core-java-io/src/test/java/com/baeldung/java8/JavaFolderSizeUnitTest.java b/core-java-io/src/test/java/com/baeldung/java8/JavaFolderSizeUnitTest.java index 7f83e379cd..1f3b380772 100644 --- a/core-java-io/src/test/java/com/baeldung/java8/JavaFolderSizeUnitTest.java +++ b/core-java-io/src/test/java/com/baeldung/java8/JavaFolderSizeUnitTest.java @@ -55,12 +55,7 @@ public class JavaFolderSizeUnitTest { @Test public void whenGetFolderSizeUsingJava8_thenCorrect() throws IOException { final Path folder = Paths.get(path); - final long size = Files.walk(folder) - .filter(p -> p.toFile() - .isFile()) - .mapToLong(p -> p.toFile() - .length()) - .sum(); + final long size = Files.walk(folder).filter(p -> p.toFile().isFile()).mapToLong(p -> p.toFile().length()).sum(); assertEquals(EXPECTED_SIZE, size); } @@ -77,12 +72,8 @@ public class JavaFolderSizeUnitTest { public void whenGetFolderSizeUsingGuava_thenCorrect() { final File folder = new File(path); - final Iterable files = com.google.common.io.Files.fileTreeTraverser() - .breadthFirstTraversal(folder); - final long size = StreamSupport.stream(files.spliterator(), false) - .filter(File::isFile) - .mapToLong(File::length) - .sum(); + final Iterable files = com.google.common.io.Files.fileTreeTraverser().breadthFirstTraversal(folder); + final long size = StreamSupport.stream(files.spliterator(), false).filter(File::isFile).mapToLong(File::length).sum(); assertEquals(EXPECTED_SIZE, size); } diff --git a/core-java-io/src/test/java/com/baeldung/mappedbytebuffer/MappedByteBufferUnitTest.java b/core-java-io/src/test/java/com/baeldung/mappedbytebuffer/MappedByteBufferUnitTest.java index 0a0993a0d7..bb87529783 100644 --- a/core-java-io/src/test/java/com/baeldung/mappedbytebuffer/MappedByteBufferUnitTest.java +++ b/core-java-io/src/test/java/com/baeldung/mappedbytebuffer/MappedByteBufferUnitTest.java @@ -18,14 +18,13 @@ import static org.junit.Assert.assertNotNull; public class MappedByteBufferUnitTest { - @Test public void givenFileChannel_whenReadToTheMappedByteBuffer_thenShouldSuccess() throws Exception { - //given + // given CharBuffer charBuffer = null; Path pathToRead = getFileURIFromResources("fileToRead.txt"); - //when + // when try (FileChannel fileChannel = (FileChannel) Files.newByteChannel(pathToRead, EnumSet.of(StandardOpenOption.READ))) { MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size()); @@ -34,20 +33,19 @@ public class MappedByteBufferUnitTest { } } - //then + // then assertNotNull(charBuffer); assertEquals(charBuffer.toString(), "This is a content of the file"); } @Test public void givenPath_whenWriteToItUsingMappedByteBuffer_thenShouldSuccessfullyWrite() throws Exception { - //given - CharBuffer charBuffer = CharBuffer.wrap("This will be written to the file"); - Path pathToWrite = getFileURIFromResources("fileToWriteTo.txt"); + // given + final CharBuffer charBuffer = CharBuffer.wrap("This will be written to the file"); + final Path pathToWrite = getFileURIFromResources("fileToWriteTo.txt"); - //when - try (FileChannel fileChannel = (FileChannel) Files.newByteChannel(pathToWrite, - EnumSet.of(StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING))) { + // when + try (FileChannel fileChannel = (FileChannel) Files.newByteChannel(pathToWrite, EnumSet.of(StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING))) { MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, charBuffer.length()); if (mappedByteBuffer != null) { @@ -55,14 +53,16 @@ public class MappedByteBufferUnitTest { } } - //then - List fileContent = Files.readAllLines(pathToWrite); + // then + final List fileContent = Files.readAllLines(pathToWrite); assertEquals(fileContent.get(0), "This will be written to the file"); } - private Path getFileURIFromResources(String fileName) throws Exception { - ClassLoader classLoader = getClass().getClassLoader(); + // + + private final Path getFileURIFromResources(String fileName) throws Exception { + final ClassLoader classLoader = getClass().getClassLoader(); return Paths.get(classLoader.getResource(fileName).toURI()); } } diff --git a/core-java-io/src/test/java/com/baeldung/stream/FileCopyTest.java b/core-java-io/src/test/java/com/baeldung/stream/FileCopyTest.java deleted file mode 100644 index 3b915a3829..0000000000 --- a/core-java-io/src/test/java/com/baeldung/stream/FileCopyTest.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.baeldung.stream; - -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.io.IOException; - -import org.junit.Test; - -public class FileCopyTest { - - @Test - public void whenUsingStream_thenCopyFile() throws IOException { - File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_stream.txt"); - File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_stream.txt"); - FileCopy.copyFileUsingStream(src, dest); - assertTrue(dest.exists()); - dest.delete(); - } - - @Test - public void whenUsingFiles_thenCopyFile() throws IOException { - File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_files.txt"); - File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_files.txt"); - FileCopy.copyFileUsingJavaFiles(src, dest); - assertTrue(dest.exists()); - dest.delete(); - } - - @Test - public void whenUsingChannel_thenCopyFile() throws IOException { - File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_channel.txt"); - File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_channel.txt"); - FileCopy.copyFileUsingChannel(src, dest); - assertTrue(dest.exists()); - dest.delete(); - } - - @Test - public void whenUsingApache_thenCopyFile() throws IOException { - File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_apache.txt"); - File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_apache.txt"); - FileCopy.copyFileUsingApacheCommonsIO(src, dest); - assertTrue(dest.exists()); - dest.delete(); - } -} diff --git a/core-java-io/src/test/java/com/baeldung/stream/FileCopyUnitTest.java b/core-java-io/src/test/java/com/baeldung/stream/FileCopyUnitTest.java new file mode 100644 index 0000000000..b4641083b9 --- /dev/null +++ b/core-java-io/src/test/java/com/baeldung/stream/FileCopyUnitTest.java @@ -0,0 +1,52 @@ +package com.baeldung.stream; + +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; + +import org.junit.Test; + +public class FileCopyUnitTest { + + @Test + public void whenUsingStream_thenCopyFile() throws IOException { + final File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_stream.txt"); + final File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_stream.txt"); + FileCopy.copyFileUsingStream(src, dest); + + assertTrue(dest.exists()); + dest.delete(); + } + + @Test + public void whenUsingFiles_thenCopyFile() throws IOException { + final File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_files.txt"); + final File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_files.txt"); + FileCopy.copyFileUsingJavaFiles(src, dest); + + assertTrue(dest.exists()); + dest.delete(); + } + + @Test + public void whenUsingChannel_thenCopyFile() throws IOException { + final File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_channel.txt"); + final File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_channel.txt"); + FileCopy.copyFileUsingChannel(src, dest); + + assertTrue(dest.exists()); + dest.delete(); + } + + @Test + public void whenUsingApache_thenCopyFile() throws IOException { + final File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_apache.txt"); + final File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_apache.txt"); + FileCopy.copyFileUsingApacheCommonsIO(src, dest); + + assertTrue(dest.exists()); + dest.delete(); + } + +} diff --git a/core-java-io/src/test/java/org/baeldung/java/io/JavaInputStreamToXUnitTest.java b/core-java-io/src/test/java/org/baeldung/java/io/JavaInputStreamToXUnitTest.java index 0e20e75869..f1bea1eefb 100644 --- a/core-java-io/src/test/java/org/baeldung/java/io/JavaInputStreamToXUnitTest.java +++ b/core-java-io/src/test/java/org/baeldung/java/io/JavaInputStreamToXUnitTest.java @@ -152,11 +152,11 @@ public class JavaInputStreamToXUnitTest { @Test public final void whenConvertingToFile_thenCorrect() throws IOException { - final InputStream initialStream = new FileInputStream(new File("src/main/resources/sample.txt")); + final InputStream initialStream = new FileInputStream(new File("src/test/resources/sample.txt")); final byte[] buffer = new byte[initialStream.available()]; initialStream.read(buffer); - final File targetFile = new File("src/main/resources/targetFile.tmp"); + final File targetFile = new File("src/test/resources/targetFile.tmp"); final OutputStream outStream = new FileOutputStream(targetFile); outStream.write(buffer); @@ -166,8 +166,8 @@ public class JavaInputStreamToXUnitTest { @Test public final void whenConvertingInProgressToFile_thenCorrect() throws IOException { - final InputStream initialStream = new FileInputStream(new File("src/main/resources/sample.txt")); - final File targetFile = new File("src/main/resources/targetFile.tmp"); + final InputStream initialStream = new FileInputStream(new File("src/test/resources/sample.txt")); + final File targetFile = new File("src/test/resources/targetFile.tmp"); final OutputStream outStream = new FileOutputStream(targetFile); final byte[] buffer = new byte[8 * 1024]; @@ -182,8 +182,8 @@ public class JavaInputStreamToXUnitTest { @Test public final void whenConvertingAnInProgressInputStreamToFile_thenCorrect2() throws IOException { - final InputStream initialStream = new FileInputStream(new File("src/main/resources/sample.txt")); - final File targetFile = new File("src/main/resources/targetFile.tmp"); + final InputStream initialStream = new FileInputStream(new File("src/test/resources/sample.txt")); + final File targetFile = new File("src/test/resources/targetFile.tmp"); java.nio.file.Files.copy(initialStream, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING); @@ -192,11 +192,11 @@ public class JavaInputStreamToXUnitTest { @Test public final void whenConvertingInputStreamToFile_thenCorrect3() throws IOException { - final InputStream initialStream = new FileInputStream(new File("src/main/resources/sample.txt")); + final InputStream initialStream = new FileInputStream(new File("src/test/resources/sample.txt")); final byte[] buffer = new byte[initialStream.available()]; initialStream.read(buffer); - final File targetFile = new File("src/main/resources/targetFile.tmp"); + final File targetFile = new File("src/test/resources/targetFile.tmp"); Files.write(buffer, targetFile); IOUtils.closeQuietly(initialStream); @@ -204,13 +204,13 @@ public class JavaInputStreamToXUnitTest { @Test public final void whenConvertingInputStreamToFile_thenCorrect4() throws IOException { - final InputStream initialStream = FileUtils.openInputStream(new File("src/main/resources/sample.txt")); + final InputStream initialStream = FileUtils.openInputStream(new File("src/test/resources/sample.txt")); - final File targetFile = new File("src/main/resources/targetFile.tmp"); + final File targetFile = new File("src/test/resources/targetFile.tmp"); FileUtils.copyInputStreamToFile(initialStream, targetFile); } - + @Test public final void givenUsingPlainJava_whenConvertingAnInputStreamToString_thenCorrect() throws IOException { String originalString = randomAlphabetic(8); @@ -225,7 +225,7 @@ public class JavaInputStreamToXUnitTest { buffer.flush(); byte[] byteArray = buffer.toByteArray(); - + String text = new String(byteArray, StandardCharsets.UTF_8); assertThat(text, equalTo(originalString)); } diff --git a/core-java-io/src/test/java/org/baeldung/java/io/JavaReadFromFileUnitTest.java b/core-java-io/src/test/java/org/baeldung/java/io/JavaReadFromFileUnitTest.java index 41d0a8a02a..b56841117e 100644 --- a/core-java-io/src/test/java/org/baeldung/java/io/JavaReadFromFileUnitTest.java +++ b/core-java-io/src/test/java/org/baeldung/java/io/JavaReadFromFileUnitTest.java @@ -18,7 +18,6 @@ import static org.junit.Assert.assertTrue; public class JavaReadFromFileUnitTest { - private static final Logger LOG = LoggerFactory.getLogger(JavaReadFromFileUnitTest.class); @Test @@ -107,11 +106,12 @@ public class JavaReadFromFileUnitTest { @Test public void whenReadUTFEncodedFile_thenCorrect() throws IOException { - final String expected_value = "青空"; + final String expected_value = "é�’空"; final BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("src/test/resources/test_read7.in"), "UTF-8")); final String currentLine = reader.readLine(); reader.close(); LOG.debug(currentLine); + assertEquals(expected_value, currentLine); } diff --git a/core-java-io/src/test/java/org/baeldung/java/io/JavaXToInputStreamUnitTest.java b/core-java-io/src/test/java/org/baeldung/java/io/JavaXToInputStreamUnitTest.java index f58d66818b..08a4c673cd 100644 --- a/core-java-io/src/test/java/org/baeldung/java/io/JavaXToInputStreamUnitTest.java +++ b/core-java-io/src/test/java/org/baeldung/java/io/JavaXToInputStreamUnitTest.java @@ -68,7 +68,7 @@ public class JavaXToInputStreamUnitTest { @Test public final void givenUsingPlainJava_whenConvertingFileToInputStream_thenCorrect() throws IOException { - final File initialFile = new File("src/main/resources/sample.txt"); + final File initialFile = new File("src/test/resources/sample.txt"); final InputStream targetStream = new FileInputStream(initialFile); IOUtils.closeQuietly(targetStream); @@ -76,7 +76,7 @@ public class JavaXToInputStreamUnitTest { @Test public final void givenUsingGuava_whenConvertingFileToInputStream_thenCorrect() throws IOException { - final File initialFile = new File("src/main/resources/sample.txt"); + final File initialFile = new File("src/test/resources/sample.txt"); final InputStream targetStream = Files.asByteSource(initialFile).openStream(); IOUtils.closeQuietly(targetStream); @@ -84,7 +84,7 @@ public class JavaXToInputStreamUnitTest { @Test public final void givenUsingCommonsIO_whenConvertingFileToInputStream_thenCorrect() throws IOException { - final File initialFile = new File("src/main/resources/sample.txt"); + final File initialFile = new File("src/test/resources/sample.txt"); final InputStream targetStream = FileUtils.openInputStream(initialFile); IOUtils.closeQuietly(targetStream); diff --git a/core-java/src/test/resources/fileToWriteTo.txt b/core-java-io/src/test/resources/copiedWithApacheCommons.txt similarity index 100% rename from core-java/src/test/resources/fileToWriteTo.txt rename to core-java-io/src/test/resources/copiedWithApacheCommons.txt diff --git a/core-java-io/src/test/resources/copiedWithIo.txt b/core-java-io/src/test/resources/copiedWithIo.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java-io/src/test/resources/copiedWithNio.txt b/core-java-io/src/test/resources/copiedWithNio.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/src/test/resources/copyTest/dest/readme.txt b/core-java-io/src/test/resources/copyTest/dest/readme.txt similarity index 100% rename from core-java/src/test/resources/copyTest/dest/readme.txt rename to core-java-io/src/test/resources/copyTest/dest/readme.txt diff --git a/core-java/src/test/resources/copyTest/src/test_apache.txt b/core-java-io/src/test/resources/copyTest/src/test_apache.txt similarity index 100% rename from core-java/src/test/resources/copyTest/src/test_apache.txt rename to core-java-io/src/test/resources/copyTest/src/test_apache.txt diff --git a/core-java/src/test/resources/copyTest/src/test_channel.txt b/core-java-io/src/test/resources/copyTest/src/test_channel.txt similarity index 100% rename from core-java/src/test/resources/copyTest/src/test_channel.txt rename to core-java-io/src/test/resources/copyTest/src/test_channel.txt diff --git a/core-java/src/test/resources/copyTest/src/test_files.txt b/core-java-io/src/test/resources/copyTest/src/test_files.txt similarity index 100% rename from core-java/src/test/resources/copyTest/src/test_files.txt rename to core-java-io/src/test/resources/copyTest/src/test_files.txt diff --git a/core-java/src/test/resources/copyTest/src/test_stream.txt b/core-java-io/src/test/resources/copyTest/src/test_stream.txt similarity index 100% rename from core-java/src/test/resources/copyTest/src/test_stream.txt rename to core-java-io/src/test/resources/copyTest/src/test_stream.txt diff --git a/core-java/src/test/resources/file.txt b/core-java-io/src/test/resources/file.txt similarity index 100% rename from core-java/src/test/resources/file.txt rename to core-java-io/src/test/resources/file.txt diff --git a/core-java/src/test/resources/fileToRead.txt b/core-java-io/src/test/resources/fileToRead.txt similarity index 100% rename from core-java/src/test/resources/fileToRead.txt rename to core-java-io/src/test/resources/fileToRead.txt diff --git a/core-java-io/src/test/resources/fileToWriteTo.txt b/core-java-io/src/test/resources/fileToWriteTo.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java-io/src/test/resources/initialFile.txt b/core-java-io/src/test/resources/initialFile.txt new file mode 100644 index 0000000000..7d572d5b9d --- /dev/null +++ b/core-java-io/src/test/resources/initialFile.txt @@ -0,0 +1 @@ +With Commons IO \ No newline at end of file diff --git a/core-java-io/src/test/resources/original.txt b/core-java-io/src/test/resources/original.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java-io/src/test/resources/sample.txt b/core-java-io/src/test/resources/sample.txt new file mode 100644 index 0000000000..5e1c309dae --- /dev/null +++ b/core-java-io/src/test/resources/sample.txt @@ -0,0 +1 @@ +Hello World \ No newline at end of file diff --git a/core-java-io/src/test/resources/targetFile.tmp b/core-java-io/src/test/resources/targetFile.tmp new file mode 100644 index 0000000000..5e1c309dae --- /dev/null +++ b/core-java-io/src/test/resources/targetFile.tmp @@ -0,0 +1 @@ +Hello World \ No newline at end of file diff --git a/core-java-io/src/test/resources/targetFile.txt b/core-java-io/src/test/resources/targetFile.txt new file mode 100644 index 0000000000..424a8d0d1e --- /dev/null +++ b/core-java-io/src/test/resources/targetFile.txt @@ -0,0 +1 @@ +Some textSome text \ No newline at end of file diff --git a/core-java-io/src/test/resources/test_write.txt b/core-java-io/src/test/resources/test_write.txt new file mode 100644 index 0000000000..a15aad69b5 --- /dev/null +++ b/core-java-io/src/test/resources/test_write.txt @@ -0,0 +1 @@ +Some StringProduct name is iPhone and its price is 1000 $ \ No newline at end of file diff --git a/core-java-io/src/test/resources/test_write_1.txt b/core-java-io/src/test/resources/test_write_1.txt new file mode 100644 index 0000000000..5727d54bfc Binary files /dev/null and b/core-java-io/src/test/resources/test_write_1.txt differ diff --git a/core-java-io/src/test/resources/test_write_2.txt b/core-java-io/src/test/resources/test_write_2.txt new file mode 100644 index 0000000000..6a87dfc075 Binary files /dev/null and b/core-java-io/src/test/resources/test_write_2.txt differ diff --git a/core-java-io/src/test/resources/test_write_3.txt b/core-java-io/src/test/resources/test_write_3.txt new file mode 100644 index 0000000000..5e1c309dae --- /dev/null +++ b/core-java-io/src/test/resources/test_write_3.txt @@ -0,0 +1 @@ +Hello World \ No newline at end of file diff --git a/core-java-io/src/test/resources/test_write_4.txt b/core-java-io/src/test/resources/test_write_4.txt new file mode 100644 index 0000000000..f14fca61f6 Binary files /dev/null and b/core-java-io/src/test/resources/test_write_4.txt differ diff --git a/core-java-io/src/test/resources/test_write_5.txt b/core-java-io/src/test/resources/test_write_5.txt new file mode 100644 index 0000000000..5ab2f8a432 --- /dev/null +++ b/core-java-io/src/test/resources/test_write_5.txt @@ -0,0 +1 @@ +Hello \ No newline at end of file diff --git a/core-java/README.md b/core-java/README.md index 27f210a0c3..b7482b09cc 100644 --- a/core-java/README.md +++ b/core-java/README.md @@ -118,3 +118,22 @@ - [Implementing a Binary Tree in Java](http://www.baeldung.com/java-binary-tree) - [A Guide to ThreadLocalRandom in Java](http://www.baeldung.com/java-thread-local-random) - [RegEx for matching Date Pattern in Java](http://www.baeldung.com/java-date-regular-expressions) +- [Introduction to the JDBC RowSet Interface in Java](http://www.baeldung.com/java-jdbc-rowset) +- [Nested Classes in Java](http://www.baeldung.com/java-nested-classes) +- [A Guide to Java Loops](http://www.baeldung.com/java-loops) +- [Varargs in Java](http://www.baeldung.com/java-varargs) +- [A Guide to HashSet in Java](http://www.baeldung.com/java-hashset) +- [A Guide to Inner Interfaces in Java](http://www.baeldung.com/java-inner-interfaces) +- [Polymorphism in Java](http://www.baeldung.com/java-polymorphism) +- [Recursion In Java](http://www.baeldung.com/java-recursion) +- [A Guide to the finalize Method in Java](http://www.baeldung.com/java-finalize) +- [Compiling Java *.class Files with javac](http://www.baeldung.com/javac) +- [Method Overloading and Overriding in Java](http://www.baeldung.com/java-method-overload-override) +- [A Guide to TreeSet in Java](http://www.baeldung.com/java-tree-set) +- [Guide to ThreadLocalRandom in Java](http://www.baeldung.com/java-thread-local-random) +- [Java TreeMap vs HashMap](http://www.baeldung.com/java-treemap-vs-hashmap) +- [A Guide to Iterator in Java](http://www.baeldung.com/java-iterator) +- [The Trie Data Structure in Java](http://www.baeldung.com/trie-java) +- [Introduction to Javadoc](http://www.baeldung.com/javadoc) +- [How to TDD a List Implementation](http://jira.baeldung.com/browse/BAEL-1537) + diff --git a/core-java/pom.xml b/core-java/pom.xml index 5c1e9fcad0..e2983de9dd 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -78,6 +78,11 @@ jackson-databind ${jackson.version} + + com.google.code.gson + gson + ${gson.version} + @@ -468,6 +473,7 @@ 2.8.5 + 2.8.2 1.7.21 diff --git a/core-java/src/main/java/com/baeldung/array/Find2ndLargestInArray.java b/core-java/src/main/java/com/baeldung/array/Find2ndLargestInArray.java new file mode 100644 index 0000000000..d424bd429f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/array/Find2ndLargestInArray.java @@ -0,0 +1,20 @@ +package com.baeldung.array; + +public class Find2ndLargestInArray { + + public static int find2ndLargestElement(int[] array) { + int maxElement = array[0]; + int secondLargestElement = -1; + + for (int index = 0; index < array.length; index++) { + if (maxElement <= array[index]) { + secondLargestElement = maxElement; + maxElement = array[index]; + } else if (secondLargestElement < array[index]) { + secondLargestElement = array[index]; + } + } + return secondLargestElement; + } + +} diff --git a/core-java/src/main/java/com/baeldung/array/FindElementInArray.java b/core-java/src/main/java/com/baeldung/array/FindElementInArray.java new file mode 100644 index 0000000000..6da889fe91 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/array/FindElementInArray.java @@ -0,0 +1,22 @@ +package com.baeldung.array; + +import java.util.Arrays; + +public class FindElementInArray { + + public static boolean findGivenElementInArrayWithoutUsingStream(int[] array, int element) { + boolean actualResult = false; + + for (int index = 0; index < array.length; index++) { + if (element == array[index]) { + actualResult = true; + break; + } + } + return actualResult; + } + + public static boolean findGivenElementInArrayUsingStream(int[] array, int element) { + return Arrays.stream(array).filter(x -> element == x).findFirst().isPresent(); + } +} diff --git a/core-java/src/main/java/com/baeldung/array/SumAndAverageInArray.java b/core-java/src/main/java/com/baeldung/array/SumAndAverageInArray.java new file mode 100644 index 0000000000..e7d7172fdb --- /dev/null +++ b/core-java/src/main/java/com/baeldung/array/SumAndAverageInArray.java @@ -0,0 +1,27 @@ +package com.baeldung.array; + +import java.util.Arrays; + +public class SumAndAverageInArray { + + public static int findSumWithoutUsingStream(int[] array) { + int sum = 0; + for (int index = 0; index < array.length; index++) { + sum += array[index]; + } + return sum; + } + + public static int findSumUsingStream(int[] array) { + return Arrays.stream(array).sum(); + } + + public static double findAverageWithoutUsingStream(int[] array) { + int sum = findSumWithoutUsingStream(array); + return (double) sum / array.length; + } + + public static double findAverageUsingStream(int[] array) { + return Arrays.stream(array).average().getAsDouble(); + } +} diff --git a/core-java/src/main/java/com/baeldung/asciiart/AsciiArt.java b/core-java/src/main/java/com/baeldung/asciiart/AsciiArt.java new file mode 100644 index 0000000000..081717b4fa --- /dev/null +++ b/core-java/src/main/java/com/baeldung/asciiart/AsciiArt.java @@ -0,0 +1,62 @@ +package com.baeldung.asciiart; + +import java.awt.Font; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.RenderingHints; +import java.awt.image.BufferedImage; + +public class AsciiArt { + + public AsciiArt() { + } + + public void drawString(String text, String artChar, Settings settings) { + BufferedImage image = getImageIntegerMode(settings.width, settings.height); + + Graphics2D graphics2D = getGraphics2D(image.getGraphics(), settings); + graphics2D.drawString(text, 6, ((int) (settings.height * 0.67))); + + for (int y = 0; y < settings.height; y++) { + StringBuilder stringBuilder = new StringBuilder(); + + for (int x = 0; x < settings.width; x++) { + stringBuilder.append(image.getRGB(x, y) == -16777216 ? " " : artChar); + } + + if (stringBuilder.toString() + .trim() + .isEmpty()) { + continue; + } + + System.out.println(stringBuilder); + } + + } + + private BufferedImage getImageIntegerMode(int width, int height) { + return new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + } + + private Graphics2D getGraphics2D(Graphics graphics, Settings settings) { + graphics.setFont(settings.font); + + Graphics2D graphics2D = (Graphics2D) graphics; + graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + + return graphics2D; + } + + public class Settings { + public Font font; + public int width; + public int height; + + public Settings(Font font, int width, int height) { + this.font = font; + this.width = width; + this.height = height; + } + } +} diff --git a/core-java/src/main/java/com/baeldung/casting/Animal.java b/core-java/src/main/java/com/baeldung/casting/Animal.java new file mode 100644 index 0000000000..9f31c1dda3 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/casting/Animal.java @@ -0,0 +1,13 @@ +package com.baeldung.casting; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Animal { + private static final Logger LOGGER = LoggerFactory.getLogger(Animal.class); + + public void eat() { + LOGGER.info("animal is eating"); + } + +} diff --git a/core-java/src/main/java/com/baeldung/casting/AnimalFeeder.java b/core-java/src/main/java/com/baeldung/casting/AnimalFeeder.java new file mode 100644 index 0000000000..89b972e5c2 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/casting/AnimalFeeder.java @@ -0,0 +1,23 @@ +package com.baeldung.casting; + +import java.util.List; + +public class AnimalFeeder { + + public void feed(List animals) { + animals.forEach(animal -> { + animal.eat(); + if (animal instanceof Cat) { + ((Cat) animal).meow(); + } + }); + } + + public void uncheckedFeed(List animals) { + animals.forEach(animal -> { + animal.eat(); + ((Cat) animal).meow(); + }); + } + +} diff --git a/core-java/src/main/java/com/baeldung/casting/AnimalFeederGeneric.java b/core-java/src/main/java/com/baeldung/casting/AnimalFeederGeneric.java new file mode 100644 index 0000000000..5fbd2415c7 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/casting/AnimalFeederGeneric.java @@ -0,0 +1,24 @@ +package com.baeldung.casting; + +import java.util.ArrayList; +import java.util.List; + +public class AnimalFeederGeneric { + private Class type; + + public AnimalFeederGeneric(Class type) { + this.type = type; + } + + public List feed(List animals) { + List list = new ArrayList(); + animals.forEach(animal -> { + if (type.isInstance(animal)) { + T objAsType = type.cast(animal); + list.add(objAsType); + } + }); + return list; + } + +} diff --git a/core-java/src/main/java/com/baeldung/casting/Cat.java b/core-java/src/main/java/com/baeldung/casting/Cat.java new file mode 100644 index 0000000000..aa9a9a881a --- /dev/null +++ b/core-java/src/main/java/com/baeldung/casting/Cat.java @@ -0,0 +1,16 @@ +package com.baeldung.casting; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Cat extends Animal implements Mew { + private static final Logger LOGGER = LoggerFactory.getLogger(Cat.class); + + public void eat() { + LOGGER.info("cat is eating"); + } + + public void meow() { + LOGGER.info("meow"); + } +} diff --git a/core-java/src/main/java/com/baeldung/casting/Dog.java b/core-java/src/main/java/com/baeldung/casting/Dog.java new file mode 100644 index 0000000000..763c6b4785 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/casting/Dog.java @@ -0,0 +1,12 @@ +package com.baeldung.casting; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Dog extends Animal { + private static final Logger LOGGER = LoggerFactory.getLogger(Dog.class); + + public void eat() { + LOGGER.info("dog is eating"); + } +} diff --git a/core-java/src/main/java/com/baeldung/casting/Mew.java b/core-java/src/main/java/com/baeldung/casting/Mew.java new file mode 100644 index 0000000000..f3c7324551 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/casting/Mew.java @@ -0,0 +1,5 @@ +package com.baeldung.casting; + +public interface Mew { + public void meow(); +} diff --git a/core-java/src/main/java/com/baeldung/classloader/CustomClassLoader.java b/core-java/src/main/java/com/baeldung/classloader/CustomClassLoader.java new file mode 100644 index 0000000000..c44e863776 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/classloader/CustomClassLoader.java @@ -0,0 +1,29 @@ +package com.baeldung.classloader; + +import java.io.*; + +public class CustomClassLoader extends ClassLoader { + + + public Class getClass(String name) throws ClassNotFoundException { + byte[] b = loadClassFromFile(name); + return defineClass(name, b, 0, b.length); + } + + private byte[] loadClassFromFile(String fileName) { + InputStream inputStream = getClass().getClassLoader().getResourceAsStream( + fileName.replace('.', File.separatorChar) + ".class"); + byte[] buffer; + ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); + int nextValue = 0; + try { + while ( (nextValue = inputStream.read()) != -1 ) { + byteStream.write(nextValue); + } + } catch (IOException e) { + e.printStackTrace(); + } + buffer = byteStream.toByteArray(); + return buffer; + } +} diff --git a/core-java/src/main/java/com/baeldung/classloader/PrintClassLoader.java b/core-java/src/main/java/com/baeldung/classloader/PrintClassLoader.java new file mode 100644 index 0000000000..09ee68bf6e --- /dev/null +++ b/core-java/src/main/java/com/baeldung/classloader/PrintClassLoader.java @@ -0,0 +1,22 @@ +package com.baeldung.classloader; + +import com.sun.javafx.util.Logging; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; + +public class PrintClassLoader { + + public void printClassLoaders() throws ClassNotFoundException { + + System.out.println("Classloader of this class:" + PrintClassLoader.class.getClassLoader()); + System.out.println("Classloader of Logging:" + Logging.class.getClassLoader()); + System.out.println("Classloader of ArrayList:" + ArrayList.class.getClassLoader()); + + } +} diff --git a/core-java/src/main/java/com/baeldung/deepcopy/Address.java b/core-java/src/main/java/com/baeldung/deepcopy/Address.java new file mode 100644 index 0000000000..4e010ea92b --- /dev/null +++ b/core-java/src/main/java/com/baeldung/deepcopy/Address.java @@ -0,0 +1,59 @@ +package com.baeldung.deepcopy; + +import java.io.Serializable; + +public class Address implements Serializable, Cloneable { + + @Override + public Object clone() { + try { + return (Address) super.clone(); + } catch (CloneNotSupportedException e) { + return new Address(this.street, this.getCity(), this.getCountry()); + } + } + + private static final long serialVersionUID = 1740913841244949416L; + private String street; + private String city; + + private String country; + + public Address(Address that) { + this(that.getStreet(), that.getCity(), that.getCountry()); + } + + public Address(String street, String city, String country) { + this.street = street; + this.city = city; + this.country = country; + } + + public Address() { + } + + public String getStreet() { + return street; + } + + public String getCity() { + return city; + } + + public String getCountry() { + return country; + } + + public void setStreet(String street) { + this.street = street; + } + + public void setCity(String city) { + this.city = city; + } + + public void setCountry(String country) { + this.country = country; + } +} + diff --git a/core-java/src/main/java/com/baeldung/deepcopy/User.java b/core-java/src/main/java/com/baeldung/deepcopy/User.java new file mode 100644 index 0000000000..329cfc6b27 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/deepcopy/User.java @@ -0,0 +1,48 @@ +package com.baeldung.deepcopy; + +import java.io.Serializable; + +public class User implements Serializable, Cloneable { + + private static final long serialVersionUID = -3427002229954777557L; + private String firstName; + private String lastName; + private Address address; + + public User(String firstName, String lastName, Address address) { + this.firstName = firstName; + this.lastName = lastName; + this.address = address; + } + + public User(User that) { + this(that.getFirstName(), that.getLastName(), new Address(that.getAddress())); + } + + public User() { + } + + @Override + public Object clone() { + User user; + try { + user = (User) super.clone(); + } catch (CloneNotSupportedException e) { + user = new User(this.getFirstName(), this.getLastName(), this.getAddress()); + } + user.address = (Address) this.address.clone(); + return user; + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } + + public Address getAddress() { + return address; + } +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/flyweight/Car.java b/core-java/src/main/java/com/baeldung/designpatterns/flyweight/Car.java new file mode 100644 index 0000000000..50f62cafaa --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/flyweight/Car.java @@ -0,0 +1,85 @@ +package com.baeldung.designpatterns.flyweight; + +import java.awt.Color; + +import javax.annotation.concurrent.Immutable; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Represents a car. This class is immutable. + * + * @author Donato Rimenti + */ +@Immutable +public class Car implements Vehicle { + + /** + * Logger. + */ + private final static Logger LOG = LoggerFactory.getLogger(Car.class); + + /** + * The car's engine. + */ + private Engine engine; + + /** + * The car's color. + */ + private Color color; + + /** + * Instantiates a new Car. + * + * @param engine + * the {@link #engine} + * @param color + * the {@link #color} + */ + public Car(Engine engine, Color color) { + this.engine = engine; + this.color = color; + + // Building a new car is a very expensive operation! + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + LOG.error("Error while creating a new car", e); + } + } + + /* + * (non-Javadoc) + * + * @see com.baeldung.designpatterns.flyweight.Vehicle#start() + */ + @Override + public void start() { + LOG.info("Car is starting!"); + engine.start(); + } + + /* + * (non-Javadoc) + * + * @see com.baeldung.designpatterns.flyweight.Vehicle#stop() + */ + @Override + public void stop() { + LOG.info("Car is stopping!"); + engine.stop(); + } + + /* + * (non-Javadoc) + * + * @see com.baeldung.designpatterns.flyweight.Vehicle#getColor() + */ + @Override + public Color getColor() { + return this.color; + } + +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/flyweight/Engine.java b/core-java/src/main/java/com/baeldung/designpatterns/flyweight/Engine.java new file mode 100644 index 0000000000..05d9ca98b8 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/flyweight/Engine.java @@ -0,0 +1,31 @@ +package com.baeldung.designpatterns.flyweight; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Engine for a vehicle. + * + * @author Donato Rimenti + */ +public class Engine { + + /** + * Logger. + */ + private final static Logger LOG = LoggerFactory.getLogger(Engine.class); + + /** + * Starts the engine. + */ + public void start() { + LOG.info("Engine is starting!"); + } + + /** + * Stops the engine. + */ + public void stop() { + LOG.info("Engine is stopping!"); + } +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/flyweight/Vehicle.java b/core-java/src/main/java/com/baeldung/designpatterns/flyweight/Vehicle.java new file mode 100644 index 0000000000..c285f9fcff --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/flyweight/Vehicle.java @@ -0,0 +1,29 @@ +package com.baeldung.designpatterns.flyweight; + +import java.awt.Color; + +/** + * Interface for a vehicle. + * + * @author Donato Rimenti + */ +public interface Vehicle { + + /** + * Starts the vehicle. + */ + public void start(); + + /** + * Stops the vehicle. + */ + public void stop(); + + /** + * Gets the color of the vehicle. + * + * @return the color of the vehicle + */ + public Color getColor(); + +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/designpatterns/flyweight/VehicleFactory.java b/core-java/src/main/java/com/baeldung/designpatterns/flyweight/VehicleFactory.java new file mode 100644 index 0000000000..2854b7dab1 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/flyweight/VehicleFactory.java @@ -0,0 +1,45 @@ +package com.baeldung.designpatterns.flyweight; + +import java.awt.Color; +import java.util.HashMap; +import java.util.Map; + +/** + * Factory which implements the Flyweight pattern to return an existing vehicle + * if present or a new one otherwise. + * + * @author Donato Rimenti + */ +public class VehicleFactory { + + /** + * Stores the already created vehicles. + */ + private static Map vehiclesCache = new HashMap(); + + /** + * Private constructor to prevent this class instantiation. + */ + private VehicleFactory() { + } + + /** + * Returns a vehicle of the same color passed as argument. If that vehicle + * was already created by this factory, that vehicle is returned, otherwise + * a new one is created and returned. + * + * @param color + * the color of the vehicle to return + * @return a vehicle of the specified color + */ + public static Vehicle createVehicle(Color color) { + // Looks for the requested vehicle into the cache. + // If the vehicle doesn't exist, a new one is created. + Vehicle newVehicle = vehiclesCache.computeIfAbsent(color, newColor -> { + // Creates the new car. + Engine newEngine = new Engine(); + return new Car(newEngine, newColor); + }); + return newVehicle; + } +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/observer/Channel.java b/core-java/src/main/java/com/baeldung/designpatterns/observer/Channel.java new file mode 100644 index 0000000000..9ca2edac38 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/observer/Channel.java @@ -0,0 +1,5 @@ +package com.baeldung.designpatterns.observer; + +public interface Channel { + public void update(Object o); +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/observer/NewsAgency.java b/core-java/src/main/java/com/baeldung/designpatterns/observer/NewsAgency.java new file mode 100644 index 0000000000..0330fbdcdc --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/observer/NewsAgency.java @@ -0,0 +1,24 @@ +package com.baeldung.designpatterns.observer; + +import java.util.ArrayList; +import java.util.List; + +public class NewsAgency { + private String news; + private List channels = new ArrayList<>(); + + public void addObserver(Channel channel) { + this.channels.add(channel); + } + + public void removeObserver(Channel channel) { + this.channels.remove(channel); + } + + public void setNews(String news) { + this.news = news; + for (Channel channel : this.channels) { + channel.update(this.news); + } + } +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/observer/NewsChannel.java b/core-java/src/main/java/com/baeldung/designpatterns/observer/NewsChannel.java new file mode 100644 index 0000000000..09c22e0ad8 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/observer/NewsChannel.java @@ -0,0 +1,20 @@ +package com.baeldung.designpatterns.observer; + +public class NewsChannel implements Channel { + + private String news; + + @Override + public void update(Object news) { + this.setNews((String) news); + } + + public String getNews() { + return news; + } + + public void setNews(String news) { + this.news = news; + } + +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/observer/ONewsAgency.java b/core-java/src/main/java/com/baeldung/designpatterns/observer/ONewsAgency.java new file mode 100644 index 0000000000..2849820663 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/observer/ONewsAgency.java @@ -0,0 +1,13 @@ +package com.baeldung.designpatterns.observer; + +import java.util.Observable; + +public class ONewsAgency extends Observable { + private String news; + + public void setNews(String news) { + this.news = news; + setChanged(); + notifyObservers(news); + } +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/observer/ONewsChannel.java b/core-java/src/main/java/com/baeldung/designpatterns/observer/ONewsChannel.java new file mode 100644 index 0000000000..3989fe0286 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/observer/ONewsChannel.java @@ -0,0 +1,22 @@ +package com.baeldung.designpatterns.observer; + +import java.util.Observable; +import java.util.Observer; + +public class ONewsChannel implements Observer { + + private String news; + + @Override + public void update(Observable o, Object news) { + this.setNews((String) news); + } + + public String getNews() { + return news; + } + + public void setNews(String news) { + this.news = news; + } +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/observer/PCLNewsAgency.java b/core-java/src/main/java/com/baeldung/designpatterns/observer/PCLNewsAgency.java new file mode 100644 index 0000000000..b05b97ab0b --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/observer/PCLNewsAgency.java @@ -0,0 +1,28 @@ +package com.baeldung.designpatterns.observer; + +import java.beans.PropertyChangeListener; +import java.beans.PropertyChangeSupport; + +public class PCLNewsAgency { + private String news; + + private PropertyChangeSupport support; + + public PCLNewsAgency() { + support = new PropertyChangeSupport(this); + } + + public void addPropertyChangeListener(PropertyChangeListener pcl) { + support.addPropertyChangeListener(pcl); + } + + public void removePropertyChangeListener(PropertyChangeListener pcl) { + support.removePropertyChangeListener(pcl); + } + + public void setNews(String value) { + support.firePropertyChange("news", this.news, value); + this.news = value; + + } +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/observer/PCLNewsChannel.java b/core-java/src/main/java/com/baeldung/designpatterns/observer/PCLNewsChannel.java new file mode 100644 index 0000000000..ff8d35463c --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/observer/PCLNewsChannel.java @@ -0,0 +1,21 @@ +package com.baeldung.designpatterns.observer; + +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; + +public class PCLNewsChannel implements PropertyChangeListener { + + private String news; + + public void propertyChange(PropertyChangeEvent evt) { + this.setNews((String) evt.getNewValue()); + } + + public String getNews() { + return news; + } + + public void setNews(String news) { + this.news = news; + } +} diff --git a/core-java/src/main/java/com/baeldung/finalize/CloseableResource.java b/core-java/src/main/java/com/baeldung/finalize/CloseableResource.java new file mode 100644 index 0000000000..d286570fbc --- /dev/null +++ b/core-java/src/main/java/com/baeldung/finalize/CloseableResource.java @@ -0,0 +1,30 @@ +package com.baeldung.finalize; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +public class CloseableResource implements AutoCloseable { + private BufferedReader reader; + + public CloseableResource() { + InputStream input = this.getClass().getClassLoader().getResourceAsStream("file.txt"); + reader = new BufferedReader(new InputStreamReader(input)); + } + + public String readFirstLine() throws IOException { + String firstLine = reader.readLine(); + return firstLine; + } + + @Override + public void close() { + try { + reader.close(); + System.out.println("Closed BufferedReader in the close method"); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/core-java/src/main/java/com/baeldung/finalize/Finalizable.java b/core-java/src/main/java/com/baeldung/finalize/Finalizable.java new file mode 100644 index 0000000000..cfc6616f5c --- /dev/null +++ b/core-java/src/main/java/com/baeldung/finalize/Finalizable.java @@ -0,0 +1,30 @@ +package com.baeldung.finalize; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +public class Finalizable { + private BufferedReader reader; + + public Finalizable() { + InputStream input = this.getClass().getClassLoader().getResourceAsStream("file.txt"); + reader = new BufferedReader(new InputStreamReader(input)); + } + + public String readFirstLine() throws IOException { + String firstLine = reader.readLine(); + return firstLine; + } + + @Override + public void finalize() { + try { + reader.close(); + System.out.println("Closed BufferedReader in the finalizer"); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/core-java/src/main/java/com/baeldung/inheritance/ArmoredCar.java b/core-java/src/main/java/com/baeldung/inheritance/ArmoredCar.java new file mode 100644 index 0000000000..b6bb5181b8 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/inheritance/ArmoredCar.java @@ -0,0 +1,43 @@ +package com.baeldung.inheritance; + +public class ArmoredCar extends Car implements Floatable, Flyable{ + private int bulletProofWindows; + private String model; + + public void remoteStartCar() { + // this vehicle can be started by using a remote control + } + + public String registerModel() { + return model; + } + + public String getAValue() { + return super.model; // returns value of model defined in base class Car + // return this.model; // will return value of model defined in ArmoredCar + // return model; // will return value of model defined in ArmoredCar + } + + public static String msg() { + // return super.msg(); // this won't compile. + return "ArmoredCar"; + } + + @Override + public void floatOnWater() { + System.out.println("I can float!"); + } + + @Override + public void fly() { + System.out.println("I can fly!"); + } + + public void aMethod() { + // System.out.println(duration); // Won't compile + System.out.println(Floatable.duration); // outputs 10 + System.out.println(Flyable.duration); // outputs 20 + } + + +} diff --git a/core-java/src/main/java/com/baeldung/inheritance/BMW.java b/core-java/src/main/java/com/baeldung/inheritance/BMW.java new file mode 100644 index 0000000000..8ad3bb683f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/inheritance/BMW.java @@ -0,0 +1,12 @@ +package com.baeldung.inheritance; + +public class BMW extends Car { + public BMW() { + super(5, "BMW"); + } + + @Override + public String toString() { + return model; + } +} diff --git a/core-java/src/main/java/com/baeldung/inheritance/Car.java b/core-java/src/main/java/com/baeldung/inheritance/Car.java new file mode 100644 index 0000000000..21ea9ea569 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/inheritance/Car.java @@ -0,0 +1,32 @@ +package com.baeldung.inheritance; + +public class Car { + private final int DEFAULT_WHEEL_COUNT = 5; + private final String DEFAULT_MODEL = "Basic"; + + protected int wheels; + protected String model; + + public Car() { + this.wheels = DEFAULT_WHEEL_COUNT; + this.model = DEFAULT_MODEL; + } + + public Car(int wheels, String model) { + this.wheels = wheels; + this.model = model; + } + + public void start() { + // Check essential parts + // If okay, start. + } + public static int count = 10; + public static String msg() { + return "Car"; + } + + public String toString() { + return model; + } +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/inheritance/Employee.java b/core-java/src/main/java/com/baeldung/inheritance/Employee.java new file mode 100644 index 0000000000..599a1d7331 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/inheritance/Employee.java @@ -0,0 +1,15 @@ +package com.baeldung.inheritance; + +public class Employee { + private String name; + private Car car; + + public Employee(String name, Car car) { + this.name = name; + this.car = car; + } + + public Car getCar() { + return car; + } +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/inheritance/Floatable.java b/core-java/src/main/java/com/baeldung/inheritance/Floatable.java new file mode 100644 index 0000000000..c0b456dffb --- /dev/null +++ b/core-java/src/main/java/com/baeldung/inheritance/Floatable.java @@ -0,0 +1,10 @@ +package com.baeldung.inheritance; + +public interface Floatable { + int duration = 10; + void floatOnWater(); + + default void repair() { + System.out.println("Repairing Floatable object"); + } +} diff --git a/core-java/src/main/java/com/baeldung/inheritance/Flyable.java b/core-java/src/main/java/com/baeldung/inheritance/Flyable.java new file mode 100644 index 0000000000..cb8244cf5b --- /dev/null +++ b/core-java/src/main/java/com/baeldung/inheritance/Flyable.java @@ -0,0 +1,13 @@ +package com.baeldung.inheritance; + +public interface Flyable { + int duration = 10; + void fly(); + + /* + * Commented + */ + //default void repair() { + // System.out.println("Repairing Flyable object"); + //} +} diff --git a/core-java/src/main/java/com/baeldung/inheritance/SpaceCar.java b/core-java/src/main/java/com/baeldung/inheritance/SpaceCar.java new file mode 100644 index 0000000000..8c23b26da9 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/inheritance/SpaceCar.java @@ -0,0 +1,18 @@ +package com.baeldung.inheritance; + +public class SpaceCar extends Car implements SpaceTraveller { + @Override + public void floatOnWater() { + System.out.println("SpaceCar floating!"); + } + + @Override + public void fly() { + System.out.println("SpaceCar flying!"); + } + + @Override + public void remoteControl() { + System.out.println("SpaceCar being controlled remotely!"); + } +} diff --git a/core-java/src/main/java/com/baeldung/inheritance/SpaceTraveller.java b/core-java/src/main/java/com/baeldung/inheritance/SpaceTraveller.java new file mode 100644 index 0000000000..9b66441791 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/inheritance/SpaceTraveller.java @@ -0,0 +1,6 @@ +package com.baeldung.inheritance; + +public interface SpaceTraveller extends Floatable, Flyable { + int duration = 10; + void remoteControl(); +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/java/list/CustomList.java b/core-java/src/main/java/com/baeldung/java/list/CustomList.java new file mode 100644 index 0000000000..8b91fca32f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/java/list/CustomList.java @@ -0,0 +1,210 @@ +package com.baeldung.java.list; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + +public class CustomList implements List { + private Object[] internal = {}; + + @Override + public boolean isEmpty() { + // the first cycle + // return true; + + // the second cycle + // if (internal.length != 0) { + // return false; + // } else { + // return true; + // } + + // refactoring + return internal.length == 0; + } + + @Override + public int size() { + // the first cycle + // if (isEmpty()) { + // return 0; + // } else { + // return internal.length; + // } + + // refactoring + return internal.length; + } + + @SuppressWarnings("unchecked") + @Override + public E get(int index) { + // the first cycle + // return (E) internal[0]; + + // improvement + return (E) internal[index]; + } + + @Override + public boolean add(E element) { + // the first cycle + // internal = new Object[] { element }; + // return true; + + // the second cycle + Object[] temp = Arrays.copyOf(internal, internal.length + 1); + temp[internal.length] = element; + internal = temp; + return true; + } + + @Override + public void add(int index, E element) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean addAll(Collection collection) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean addAll(int index, Collection collection) { + throw new UnsupportedOperationException(); + } + + @Override + public E remove(int index) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean remove(Object object) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean removeAll(Collection collection) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean retainAll(Collection collection) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean contains(Object object) { + for (Object element : internal) { + if (object.equals(element)) { + return true; + } + } + return false; + } + + @Override + public boolean containsAll(Collection collection) { + for (Object element : collection) + if (!contains(element)) { + return false; + } + return true; + } + + @SuppressWarnings("unchecked") + @Override + public E set(int index, E element) { + E oldElement = (E) internal[index]; + internal[index] = element; + return oldElement; + } + + @Override + public void clear() { + internal = new Object[0]; + } + + @Override + public int indexOf(Object object) { + for (int i = 0; i < internal.length; i++) { + if (object.equals(internal[i])) { + return i; + } + } + return -1; + } + + @Override + public int lastIndexOf(Object object) { + for (int i = internal.length - 1; i >= 0; i--) { + if (object.equals(internal[i])) { + return i; + } + } + return -1; + } + + @SuppressWarnings("unchecked") + @Override + public List subList(int fromIndex, int toIndex) { + Object[] temp = new Object[toIndex - fromIndex]; + System.arraycopy(internal, fromIndex, temp, 0, temp.length); + return (List) Arrays.asList(temp); + } + + @Override + public Object[] toArray() { + return Arrays.copyOf(internal, internal.length); + } + + @SuppressWarnings("unchecked") + @Override + public T[] toArray(T[] array) { + if (array.length < internal.length) { + return (T[]) Arrays.copyOf(internal, internal.length, array.getClass()); + } + + System.arraycopy(internal, 0, array, 0, internal.length); + if (array.length > internal.length) { + array[internal.length] = null; + } + return array; + } + + @Override + public Iterator iterator() { + return new CustomIterator(); + } + + @Override + public ListIterator listIterator() { + return null; + } + + @Override + public ListIterator listIterator(int index) { + // ignored for brevity + return null; + } + + private class CustomIterator implements Iterator { + int index; + + @Override + public boolean hasNext() { + return index != internal.length; + } + + @SuppressWarnings("unchecked") + @Override + public E next() { + E element = (E) CustomList.this.internal[index]; + index++; + return element; + } + } +} diff --git a/core-java/src/main/java/com/baeldung/javac/Data.java b/core-java/src/main/java/com/baeldung/javac/Data.java new file mode 100644 index 0000000000..feef0c4f1e --- /dev/null +++ b/core-java/src/main/java/com/baeldung/javac/Data.java @@ -0,0 +1,16 @@ +package com.baeldung.javac; + +import java.util.ArrayList; +import java.util.List; + +public class Data { + List textList = new ArrayList(); + + public void addText(String text) { + textList.add(text); + } + + public List getTextList() { + return this.textList; + } +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/application/Application.java b/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/application/Application.java new file mode 100644 index 0000000000..6e023e9fa8 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/application/Application.java @@ -0,0 +1,26 @@ +package com.baeldung.methodoverloadingoverriding.application; + +import com.baeldung.methodoverloadingoverriding.model.Car; +import com.baeldung.methodoverloadingoverriding.model.Vehicle; +import com.baeldung.methodoverloadingoverriding.util.Multiplier; + +public class Application { + + public static void main(String[] args) { + Multiplier multiplier = new Multiplier(); + System.out.println(multiplier.multiply(10, 10)); + System.out.println(multiplier.multiply(10, 10, 10)); + System.out.println(multiplier.multiply(10, 10.5)); + System.out.println(multiplier.multiply(10.5, 10.5)); + + Vehicle vehicle = new Vehicle(); + System.out.println(vehicle.accelerate(100)); + System.out.println(vehicle.run()); + System.out.println(vehicle.stop()); + + Vehicle car = new Car(); + System.out.println(car.accelerate(80)); + System.out.println(car.run()); + System.out.println(car.stop()); + } +} diff --git a/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/model/Car.java b/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/model/Car.java new file mode 100644 index 0000000000..aa5bdcaec9 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/model/Car.java @@ -0,0 +1,9 @@ +package com.baeldung.methodoverloadingoverriding.model; + +public class Car extends Vehicle { + + @Override + public String accelerate(long mph) { + return "The car accelerates at : " + mph + " MPH."; + } +} diff --git a/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/model/Vehicle.java b/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/model/Vehicle.java new file mode 100644 index 0000000000..eb59e8ca23 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/model/Vehicle.java @@ -0,0 +1,16 @@ +package com.baeldung.methodoverloadingoverriding.model; + +public class Vehicle { + + public String accelerate(long mph) { + return "The vehicle accelerates at : " + mph + " MPH."; + } + + public String stop() { + return "The vehicle has stopped."; + } + + public String run() { + return "The vehicle is running."; + } +} diff --git a/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/util/Multiplier.java b/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/util/Multiplier.java new file mode 100644 index 0000000000..c43e61c78f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/util/Multiplier.java @@ -0,0 +1,16 @@ +package com.baeldung.methodoverloadingoverriding.util; + +public class Multiplier { + + public int multiply(int a, int b) { + return a * b; + } + + public int multiply(int a, int b, int c) { + return a * b * c; + } + + public double multiply(double a, double b) { + return a * b; + } +} diff --git a/core-java/src/main/java/com/baeldung/recursion/BinaryNode.java b/core-java/src/main/java/com/baeldung/recursion/BinaryNode.java new file mode 100644 index 0000000000..0c3f0ecc40 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/recursion/BinaryNode.java @@ -0,0 +1,31 @@ +package com.baeldung.recursion; + +public class BinaryNode { + int value; + BinaryNode left; + BinaryNode right; + + public BinaryNode(int value){ + this.value = value; + } + + public int getValue() { + return value; + } + public void setValue(int value) { + this.value = value; + } + public BinaryNode getLeft() { + return left; + } + public void setLeft(BinaryNode left) { + this.left = left; + } + public BinaryNode getRight() { + return right; + } + public void setRight(BinaryNode right) { + this.right = right; + } + +} diff --git a/core-java/src/main/java/com/baeldung/recursion/RecursionExample.java b/core-java/src/main/java/com/baeldung/recursion/RecursionExample.java new file mode 100644 index 0000000000..649c0e0587 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/recursion/RecursionExample.java @@ -0,0 +1,64 @@ +package com.baeldung.recursion; + +public class RecursionExample { + + public int sum(int n){ + if (n >= 1){ + return sum(n - 1) + n; + } + return n; + } + + public int tailSum(int currentSum, int n){ + if (n <= 1) { + return currentSum + n; + } + return tailSum(currentSum + n, n - 1); + } + + public int iterativeSum(int n){ + int sum = 0; + if(n < 0){ + return -1; + } + for(int i=0; i<=n; i++){ + sum += i; + } + return sum; + } + + public int powerOf10(int n){ + if (n == 0){ + return 1; + } + return powerOf10(n-1)*10; + } + + public int fibonacci(int n){ + if (n <=1 ){ + return n; + } + return fibonacci(n-1) + fibonacci(n-2); + } + + public String toBinary(int n){ + if (n <= 1 ){ + return String.valueOf(n); + } + return toBinary(n / 2) + String.valueOf(n % 2); + } + + public int calculateTreeHeight(BinaryNode root){ + if (root!= null){ + if (root.getLeft() != null || root.getRight() != null){ + return 1 + max(calculateTreeHeight(root.left) , calculateTreeHeight(root.right)); + } + } + return 0; + } + + public int max(int a,int b){ + return a>b ? a:b; + } + +} diff --git a/core-java/src/main/java/com/baeldung/resourcebundle/ExampleControl.java b/core-java/src/main/java/com/baeldung/resourcebundle/ExampleControl.java new file mode 100644 index 0000000000..88ce36c27f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/resourcebundle/ExampleControl.java @@ -0,0 +1,15 @@ +package com.baeldung.resourcebundle; + +import java.util.Arrays; +import java.util.List; +import java.util.Locale; +import java.util.ResourceBundle; + +public class ExampleControl extends ResourceBundle.Control { + + @Override + public List getCandidateLocales(String s, Locale locale) { + return Arrays.asList(new Locale("pl", "PL")); + } + +} diff --git a/core-java/src/main/java/com/baeldung/resourcebundle/ExampleResource.java b/core-java/src/main/java/com/baeldung/resourcebundle/ExampleResource.java new file mode 100644 index 0000000000..52ccc80f52 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/resourcebundle/ExampleResource.java @@ -0,0 +1,14 @@ +package com.baeldung.resourcebundle; + +import java.util.ListResourceBundle; + +public class ExampleResource extends ListResourceBundle { + + @Override + protected Object[][] getContents() { + return new Object[][] { + { "greeting", "hello" } + }; + } + +} diff --git a/core-java/src/main/java/com/baeldung/resourcebundle/ExampleResource_pl.java b/core-java/src/main/java/com/baeldung/resourcebundle/ExampleResource_pl.java new file mode 100644 index 0000000000..7d4f11a5d3 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/resourcebundle/ExampleResource_pl.java @@ -0,0 +1,15 @@ +package com.baeldung.resourcebundle; + +import java.util.ListResourceBundle; + +public class ExampleResource_pl extends ListResourceBundle { + + @Override + protected Object[][] getContents() { + return new Object[][] { + { "greeting", "cześć" }, + { "language", "polish" }, + }; + } + +} diff --git a/core-java/src/main/java/com/baeldung/resourcebundle/ExampleResource_pl_PL.java b/core-java/src/main/java/com/baeldung/resourcebundle/ExampleResource_pl_PL.java new file mode 100644 index 0000000000..252bfcb9cf --- /dev/null +++ b/core-java/src/main/java/com/baeldung/resourcebundle/ExampleResource_pl_PL.java @@ -0,0 +1,17 @@ +package com.baeldung.resourcebundle; + +import java.math.BigDecimal; +import java.util.ListResourceBundle; + +public class ExampleResource_pl_PL extends ListResourceBundle { + + @Override + protected Object[][] getContents() { + return new Object[][] { + { "currency", "polish zloty" }, + { "toUsdRate", new BigDecimal("3.401") }, + { "cities", new String[] { "Warsaw", "Cracow" } } + }; + } + +} diff --git a/core-java/src/main/java/com/baeldung/string/Palindrome.java b/core-java/src/main/java/com/baeldung/string/Palindrome.java new file mode 100644 index 0000000000..97d4d36d07 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/string/Palindrome.java @@ -0,0 +1,66 @@ +package com.baeldung.string; + +import java.util.stream.IntStream; + +public class Palindrome { + + public boolean isPalindrome(String text) { + String clean = text.replaceAll("\\s+", "").toLowerCase(); + int length = clean.length(); + int forward = 0; + int backward = length - 1; + while (backward > forward) { + char forwardChar = clean.charAt(forward++); + char backwardChar = clean.charAt(backward--); + if (forwardChar != backwardChar) + return false; + } + return true; + } + + public boolean isPalindromeReverseTheString(String text) { + StringBuilder reverse = new StringBuilder(); + String clean = text.replaceAll("\\s+", "").toLowerCase(); + char[] plain = clean.toCharArray(); + for (int i = plain.length - 1; i >= 0; i--) + reverse.append(plain[i]); + return (reverse.toString()).equals(clean); + } + + public boolean isPalindromeUsingStringBuilder(String text) { + String clean = text.replaceAll("\\s+", "").toLowerCase(); + StringBuilder plain = new StringBuilder(clean); + StringBuilder reverse = plain.reverse(); + return (reverse.toString()).equals(clean); + } + + public boolean isPalindromeUsingStringBuffer(String text) { + String clean = text.replaceAll("\\s+", "").toLowerCase(); + StringBuffer plain = new StringBuffer(clean); + StringBuffer reverse = plain.reverse(); + return (reverse.toString()).equals(clean); + } + + public boolean isPalindromeRecursive(String text) { + String clean = text.replaceAll("\\s+", "").toLowerCase(); + return recursivePalindrome(clean, 0, clean.length() - 1); + } + + private boolean recursivePalindrome(String text, int forward, int backward) { + if (forward == backward) + return true; + if ((text.charAt(forward)) != (text.charAt(backward))) + return false; + if (forward < backward + 1) { + return recursivePalindrome(text, forward + 1, backward - 1); + } + + return true; + } + + public boolean isPalindromeUsingIntStream(String text) { + String temp = text.replaceAll("\\s+", "").toLowerCase(); + return IntStream.range(0, temp.length() / 2) + .noneMatch(i -> temp.charAt(i) != temp.charAt(temp.length() - i - 1)); + } +} diff --git a/core-java/src/main/java/com/baeldung/system/DetectOS.java b/core-java/src/main/java/com/baeldung/system/DetectOS.java new file mode 100644 index 0000000000..2d605fe49f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/system/DetectOS.java @@ -0,0 +1,18 @@ +package com.baeldung.system; + +import org.apache.commons.lang3.SystemUtils; + +public class DetectOS { + + public String getOperatingSystem() { + String os = System.getProperty("os.name"); + System.out.println("Using System Property: " + os); + return os; + } + + public String getOperatingSystemSystemUtils() { + String os = SystemUtils.OS_NAME; + System.out.println("Using SystemUtils: " + os); + return os; + } +} diff --git a/core-java/src/main/java/com/baeldung/trie/Trie.java b/core-java/src/main/java/com/baeldung/trie/Trie.java new file mode 100644 index 0000000000..dd51d97b2d --- /dev/null +++ b/core-java/src/main/java/com/baeldung/trie/Trie.java @@ -0,0 +1,62 @@ +package com.baeldung.trie; + +public class Trie { + private TrieNode root; + + Trie() { + root = new TrieNode(); + } + + public void insert(String word) { + TrieNode current = root; + + for (int i = 0; i < word.length(); i++) { + current = current.getChildren().computeIfAbsent(word.charAt(i), c -> new TrieNode()); + } + current.setEndOfWord(true); + } + + public boolean delete(String word) { + return delete(root, word, 0); + } + + public boolean containsNode(String word) { + TrieNode current = root; + + for (int i = 0; i < word.length(); i++) { + char ch = word.charAt(i); + TrieNode node = current.getChildren().get(ch); + if (node == null) { + return false; + } + current = node; + } + return current.isEndOfWord(); + } + + public boolean isEmpty() { + return root == null; + } + + private boolean delete(TrieNode current, String word, int index) { + if (index == word.length()) { + if (!current.isEndOfWord()) { + return false; + } + current.setEndOfWord(false); + return current.getChildren().isEmpty(); + } + char ch = word.charAt(index); + TrieNode node = current.getChildren().get(ch); + if (node == null) { + return false; + } + boolean shouldDeleteCurrentNode = delete(node, word, index + 1); + + if (shouldDeleteCurrentNode) { + current.getChildren().remove(ch); + return current.getChildren().isEmpty(); + } + return false; + } +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/trie/TrieNode.java b/core-java/src/main/java/com/baeldung/trie/TrieNode.java new file mode 100644 index 0000000000..25dc753950 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/trie/TrieNode.java @@ -0,0 +1,31 @@ +package com.baeldung.trie; + +import java.util.HashMap; +import java.util.Map; + +class TrieNode { + private Map children; + private boolean endOfWord; + + public TrieNode() { + children = new HashMap<>(); + endOfWord = false; + } + + public Map getChildren() { + return children; + } + + public void setChildren(Map children) { + this.children = children; + } + + public boolean isEndOfWord() { + return endOfWord; + } + + public void setEndOfWord(boolean endOfWord) { + this.endOfWord = endOfWord; + } + +} \ No newline at end of file diff --git a/core-java/src/main/java/javac-args/arguments b/core-java/src/main/java/javac-args/arguments new file mode 100644 index 0000000000..51639800a7 --- /dev/null +++ b/core-java/src/main/java/javac-args/arguments @@ -0,0 +1,2 @@ +-d javac-target -verbose +com/baeldung/javac/Data.java \ No newline at end of file diff --git a/core-java/src/main/java/javac-args/options b/core-java/src/main/java/javac-args/options new file mode 100644 index 0000000000..f02f2344ff --- /dev/null +++ b/core-java/src/main/java/javac-args/options @@ -0,0 +1,2 @@ +-d javac-target +-verbose \ No newline at end of file diff --git a/core-java/src/main/java/javac-args/types b/core-java/src/main/java/javac-args/types new file mode 100644 index 0000000000..ef2d861f84 --- /dev/null +++ b/core-java/src/main/java/javac-args/types @@ -0,0 +1 @@ +com/baeldung/javac/Data.java \ No newline at end of file diff --git a/core-java/src/main/java/javac-args/xlint-ops b/core-java/src/main/java/javac-args/xlint-ops new file mode 100644 index 0000000000..cdccbc0cce --- /dev/null +++ b/core-java/src/main/java/javac-args/xlint-ops @@ -0,0 +1,3 @@ +-d javac-target +-Xlint:rawtypes,unchecked,static,cast,serial,fallthrough +com/baeldung/javac/Data.java \ No newline at end of file diff --git a/core-java/src/main/resources/resourcebundle/resource.properties b/core-java/src/main/resources/resourcebundle/resource.properties new file mode 100644 index 0000000000..edeae3ef4b --- /dev/null +++ b/core-java/src/main/resources/resourcebundle/resource.properties @@ -0,0 +1,6 @@ +# Buttons +cancelButton=cancel +continueButton continue + +! Labels +helloLabel:hello diff --git a/core-java/src/main/resources/resourcebundle/resource_en.properties b/core-java/src/main/resources/resourcebundle/resource_en.properties new file mode 100644 index 0000000000..5d72b65e2f --- /dev/null +++ b/core-java/src/main/resources/resourcebundle/resource_en.properties @@ -0,0 +1 @@ +deleteButton=delete \ No newline at end of file diff --git a/core-java/src/main/resources/resourcebundle/resource_pl_PL.properties b/core-java/src/main/resources/resourcebundle/resource_pl_PL.properties new file mode 100644 index 0000000000..37dea822ad --- /dev/null +++ b/core-java/src/main/resources/resourcebundle/resource_pl_PL.properties @@ -0,0 +1,3 @@ +backButton=cofnij +helloLabel=cze\u015b\u0107 +helloLabelNoEncoding=cześć \ No newline at end of file diff --git a/core-java/src/main/resources/targetFile.tmp b/core-java/src/main/resources/targetFile.tmp deleted file mode 100644 index 20f137b416..0000000000 --- a/core-java/src/main/resources/targetFile.tmp +++ /dev/null @@ -1,2 +0,0 @@ -line 1 -a second line \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/array/Find2ndLargestInArrayTest.java b/core-java/src/test/java/com/baeldung/array/Find2ndLargestInArrayTest.java new file mode 100644 index 0000000000..ec916af092 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/array/Find2ndLargestInArrayTest.java @@ -0,0 +1,16 @@ +package com.baeldung.array; + +import org.junit.Assert; +import org.junit.Test; + +public class Find2ndLargestInArrayTest { + @Test + public void givenAnIntArray_thenFind2ndLargestElement() { + int[] array = { 1, 3, 24, 16, 87, 20 }; + int expected2ndLargest = 24; + + int actualSecondLargestElement = Find2ndLargestInArray.find2ndLargestElement(array); + + Assert.assertEquals(expected2ndLargest, actualSecondLargestElement); + } +} diff --git a/core-java/src/test/java/com/baeldung/array/FindElementInArrayTest.java b/core-java/src/test/java/com/baeldung/array/FindElementInArrayTest.java new file mode 100644 index 0000000000..ba9188fa7b --- /dev/null +++ b/core-java/src/test/java/com/baeldung/array/FindElementInArrayTest.java @@ -0,0 +1,35 @@ +package com.baeldung.array; + +import org.junit.Assert; +import org.junit.Test; + +public class FindElementInArrayTest { + @Test + public void givenAnIntArray_whenNotUsingStream_thenFindAnElement() { + int[] array = { 1, 3, 4, 8, 19, 20 }; + int element = 19; + boolean expectedResult = true; + boolean actualResult = FindElementInArray.findGivenElementInArrayWithoutUsingStream(array, element); + Assert.assertEquals(expectedResult, actualResult); + + element = 78; + expectedResult = false; + actualResult = FindElementInArray.findGivenElementInArrayWithoutUsingStream(array, element); + Assert.assertEquals(expectedResult, actualResult); + } + + @Test + public void givenAnIntArray_whenUsingStream_thenFindAnElement() { + int[] array = { 15, 16, 12, 18 }; + int element = 16; + boolean expectedResult = true; + boolean actualResult = FindElementInArray.findGivenElementInArrayUsingStream(array, element); + Assert.assertEquals(expectedResult, actualResult); + + element = 20; + expectedResult = false; + actualResult = FindElementInArray.findGivenElementInArrayUsingStream(array, element); + Assert.assertEquals(expectedResult, actualResult); + } + +} diff --git a/core-java/src/test/java/com/baeldung/array/SumAndAverageInArrayTest.java b/core-java/src/test/java/com/baeldung/array/SumAndAverageInArrayTest.java new file mode 100644 index 0000000000..3385ed690f --- /dev/null +++ b/core-java/src/test/java/com/baeldung/array/SumAndAverageInArrayTest.java @@ -0,0 +1,41 @@ +package com.baeldung.array; + +import org.junit.Assert; +import org.junit.Test; + +public class SumAndAverageInArrayTest { + @Test + public void givenAnIntArray_whenNotUsingStream_thenFindSum() { + int[] array = { 1, 3, 4, 8, 19, 20 }; + int expectedSumOfArray = 55; + int actualSumOfArray = SumAndAverageInArray.findSumWithoutUsingStream(array); + Assert.assertEquals(expectedSumOfArray, actualSumOfArray); + } + + @Test + public void givenAnIntArray_whenUsingStream_thenFindSum() { + int[] array = { 1, 3, 4, 8, 19, 20 }; + int expectedSumOfArray = 55; + int actualSumOfArray = SumAndAverageInArray.findSumUsingStream(array); + + Assert.assertEquals(expectedSumOfArray, actualSumOfArray); + } + + @Test + public void givenAnIntArray_whenNotUsingStream_thenFindAverage() { + int[] array = { 1, 3, 4, 8, 19, 20 }; + double expectedAvgOfArray = 9.17; + double actualAvgOfArray = SumAndAverageInArray.findAverageWithoutUsingStream(array); + + Assert.assertEquals(expectedAvgOfArray, actualAvgOfArray, 0.0034); + } + + @Test + public void givenAnIntArray_whenUsingStream_thenFindAverage() { + int[] array = { 1, 3, 4, 8, 19, 20 }; + double expectedAvgOfArray = 9.17; + double actualAvgOfArray = SumAndAverageInArray.findAverageUsingStream(array); + + Assert.assertEquals(expectedAvgOfArray, actualAvgOfArray, 0.0034); + } +} diff --git a/core-java/src/test/java/com/baeldung/asciiart/AsciiArtTest.java b/core-java/src/test/java/com/baeldung/asciiart/AsciiArtTest.java new file mode 100644 index 0000000000..103681894e --- /dev/null +++ b/core-java/src/test/java/com/baeldung/asciiart/AsciiArtTest.java @@ -0,0 +1,20 @@ +package com.baeldung.asciiart; + +import java.awt.Font; + +import org.junit.Test; + +import com.baeldung.asciiart.AsciiArt.Settings; + +public class AsciiArtTest { + + @Test + public void givenTextWithAsciiCharacterAndSettings_shouldPrintAsciiArt() { + AsciiArt asciiArt = new AsciiArt(); + String text = "BAELDUNG"; + Settings settings = asciiArt.new Settings(new Font("SansSerif", Font.BOLD, 24), text.length() * 30, 30); // 30 pixel width per character + + asciiArt.drawString(text, "*", settings); + } + +} diff --git a/core-java/src/test/java/com/baeldung/casting/CastingTest.java b/core-java/src/test/java/com/baeldung/casting/CastingTest.java new file mode 100644 index 0000000000..dd95e0dd80 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/casting/CastingTest.java @@ -0,0 +1,80 @@ +package com.baeldung.casting; + +import org.junit.Test; +import static org.junit.Assert.*; +import java.util.ArrayList; +import java.util.List; + +public class CastingTest { + + @Test + public void whenPrimitiveConverted_thenValueChanged() { + double myDouble = 1.1; + int myInt = (int) myDouble; + + assertNotEquals(myDouble, myInt); + } + + @Test + public void whenUpcast_thenInstanceUnchanged() { + Cat cat = new Cat(); + Animal animal = cat; + animal = (Animal) cat; + assertTrue(animal instanceof Cat); + } + + @Test + public void whenUpcastToObject_thenInstanceUnchanged() { + Object object = new Animal(); + assertTrue(object instanceof Animal); + } + + @Test + public void whenUpcastToInterface_thenInstanceUnchanged() { + Mew mew = new Cat(); + assertTrue(mew instanceof Cat); + } + + @Test + public void whenUpcastToAnimal_thenOverridenMethodsCalled() { + List animals = new ArrayList<>(); + animals.add(new Cat()); + animals.add(new Dog()); + new AnimalFeeder().feed(animals); + } + + @Test + public void whenDowncastToCat_thenMeowIsCalled() { + Animal animal = new Cat(); + ((Cat) animal).meow(); + } + + @Test(expected = ClassCastException.class) + public void whenDownCastWithoutCheck_thenExceptionThrown() { + List animals = new ArrayList<>(); + animals.add(new Cat()); + animals.add(new Dog()); + new AnimalFeeder().uncheckedFeed(animals); + } + + @Test + public void whenDowncastToCatWithCastMethod_thenMeowIsCalled() { + Animal animal = new Cat(); + if (Cat.class.isInstance(animal)) { + Cat cat = Cat.class.cast(animal); + cat.meow(); + } + } + + @Test + public void whenParameterCat_thenOnlyCatsFed() { + List animals = new ArrayList<>(); + animals.add(new Cat()); + animals.add(new Dog()); + AnimalFeederGeneric catFeeder = new AnimalFeederGeneric(Cat.class); + List fedAnimals = catFeeder.feed(animals); + + assertTrue(fedAnimals.size() == 1); + assertTrue(fedAnimals.get(0) instanceof Cat); + } +} diff --git a/core-java/src/test/java/com/baeldung/classloader/CustomClassLoaderTest.java b/core-java/src/test/java/com/baeldung/classloader/CustomClassLoaderTest.java new file mode 100644 index 0000000000..9f3c751805 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/classloader/CustomClassLoaderTest.java @@ -0,0 +1,23 @@ +package com.baeldung.classloader; + +import org.junit.Test; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class CustomClassLoaderTest { + + @Test + public void customLoader() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { + + CustomClassLoader customClassLoader = new CustomClassLoader(); + Class c = customClassLoader.getClass(PrintClassLoader.class.getName()); + + Object ob = c.newInstance(); + + Method md = c.getMethod("printClassLoaders"); + md.invoke(ob); + + } + +} diff --git a/core-java/src/test/java/com/baeldung/classloader/PrintClassLoaderTest.java b/core-java/src/test/java/com/baeldung/classloader/PrintClassLoaderTest.java new file mode 100644 index 0000000000..f44a5cef09 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/classloader/PrintClassLoaderTest.java @@ -0,0 +1,14 @@ +package com.baeldung.classloader; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class PrintClassLoaderTest { + @Test(expected = ClassNotFoundException.class) + public void givenAppClassLoader_whenParentClassLoader_thenClassNotFoundException() throws Exception { + PrintClassLoader sampleClassLoader = (PrintClassLoader) Class.forName(PrintClassLoader.class.getName()).newInstance(); + sampleClassLoader.printClassLoaders(); + Class.forName(PrintClassLoader.class.getName(), true, PrintClassLoader.class.getClassLoader().getParent()); + } +} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/decimalformat/DecimalFormatExamplesTest.java b/core-java/src/test/java/com/baeldung/decimalformat/DecimalFormatExamplesTest.java new file mode 100644 index 0000000000..8acd4e023e --- /dev/null +++ b/core-java/src/test/java/com/baeldung/decimalformat/DecimalFormatExamplesTest.java @@ -0,0 +1,116 @@ +package com.baeldung.decimalformat; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.math.BigDecimal; +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; +import java.text.NumberFormat; +import java.text.ParseException; +import java.util.Locale; + +import org.junit.Test; + +public class DecimalFormatExamplesTest { + + double d = 1234567.89; + + @Test + public void givenSimpleDecimal_WhenFormatting_ThenCorrectOutput() { + + assertThat(new DecimalFormat("#.##", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("1234567.89"); + + assertThat(new DecimalFormat("0.00", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("1234567.89"); + + assertThat(new DecimalFormat("#########.###", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("1234567.89"); + + assertThat(new DecimalFormat("000000000.000", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("001234567.890"); + + } + + @Test + public void givenSmallerDecimalPattern_WhenFormatting_ThenRounding() { + + assertThat(new DecimalFormat("#.#", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)).isEqualTo("1234567.9"); + + assertThat(new DecimalFormat("#", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)).isEqualTo("1234568"); + + } + + @Test + public void givenGroupingSeparator_WhenFormatting_ThenGroupedOutput() { + + assertThat(new DecimalFormat("#,###.#", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("1,234,567.9"); + + assertThat(new DecimalFormat("#,###", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("1,234,568"); + + } + + @Test + public void givenMixedPattern_WhenFormatting_ThenCorrectOutput() { + + assertThat(new DecimalFormat("The # number", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("The 1234568 number"); + + assertThat(new DecimalFormat("The '#' # number", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("The # 1234568 number"); + + } + + @Test + public void givenLocales_WhenFormatting_ThenCorrectOutput() { + + assertThat(new DecimalFormat("#,###.##", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("1,234,567.89"); + + assertThat(new DecimalFormat("#,###.##", new DecimalFormatSymbols(Locale.ITALIAN)).format(d)) + .isEqualTo("1.234.567,89"); + + assertThat(new DecimalFormat("#,###.##", DecimalFormatSymbols.getInstance(new Locale("it", "IT"))).format(d)) + .isEqualTo("1.234.567,89"); + + } + + @Test + public void givenE_WhenFormatting_ThenScientificNotation() { + + assertThat(new DecimalFormat("00.#######E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("12.3456789E5"); + + assertThat(new DecimalFormat("000.000000E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("123.456789E4"); + + assertThat(new DecimalFormat("##0.######E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("1.23456789E6"); + + assertThat(new DecimalFormat("###.000000E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("1.23456789E6"); + + } + + @Test + public void givenString_WhenParsing_ThenCorrectOutput() throws ParseException { + + assertThat(new DecimalFormat("", new DecimalFormatSymbols(Locale.ENGLISH)).parse("1234567.89")) + .isEqualTo(1234567.89); + + assertThat(new DecimalFormat("", new DecimalFormatSymbols(Locale.ITALIAN)).parse("1.234.567,89")) + .isEqualTo(1234567.89); + + } + + @Test + public void givenStringAndBigDecimalFlag_WhenParsing_ThenCorrectOutput() throws ParseException { + + NumberFormat nf = new DecimalFormat("", new DecimalFormatSymbols(Locale.ENGLISH)); + ((DecimalFormat) nf).setParseBigDecimal(true); + assertThat(nf.parse("1234567.89")).isEqualTo(BigDecimal.valueOf(1234567.89)); + } + +} diff --git a/core-java/src/test/java/com/baeldung/deepcopy/DeepCopyUnitTest.java b/core-java/src/test/java/com/baeldung/deepcopy/DeepCopyUnitTest.java new file mode 100644 index 0000000000..196b69fbf7 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/deepcopy/DeepCopyUnitTest.java @@ -0,0 +1,128 @@ +package com.baeldung.deepcopy; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; + +import org.apache.commons.lang.SerializationUtils; +import org.junit.Ignore; +import org.junit.Test; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; + +public class DeepCopyUnitTest { + + @Test + public void whenCreatingDeepCopyWithCopyConstructor_thenObjectsShouldNotBeSame() { + + Address address = new Address("Downing St 10", "London", "England"); + User pm = new User("Prime", "Minister", address); + + User deepCopy = new User(pm); + + assertThat(deepCopy).isNotSameAs(pm); + } + + @Test + public void whenModifyingOriginalObject_thenConstructorCopyShouldNotChange() { + Address address = new Address("Downing St 10", "London", "England"); + User pm = new User("Prime", "Minister", address); + User deepCopy = new User(pm); + + address.setCountry("Great Britain"); + + assertThat(deepCopy.getAddress().getCountry()).isNotEqualTo(pm.getAddress().getCountry()); + } + + @Test + public void whenModifyingOriginalObject_thenCloneCopyShouldNotChange() { + Address address = new Address("Downing St 10", "London", "England"); + User pm = new User("Prime", "Minister", address); + User deepCopy = (User) pm.clone(); + + address.setCountry("Great Britain"); + + assertThat(deepCopy.getAddress().getCountry()).isNotEqualTo(pm.getAddress().getCountry()); + } + + @Test + public void whenModifyingOriginalObject_thenCommonsCloneShouldNotChange() { + Address address = new Address("Downing St 10", "London", "England"); + User pm = new User("Prime", "Minister", address); + User deepCopy = (User) SerializationUtils.clone(pm); + + address.setCountry("Great Britain"); + + assertThat(deepCopy.getAddress().getCountry()).isNotEqualTo(pm.getAddress().getCountry()); + } + + @Test + public void whenModifyingOriginalObject_thenGsonCloneShouldNotChange() { + Address address = new Address("Downing St 10", "London", "England"); + User pm = new User("Prime", "Minister", address); + Gson gson = new Gson(); + User deepCopy = gson.fromJson(gson.toJson(pm), User.class); + + address.setCountry("Great Britain"); + + assertThat(deepCopy.getAddress().getCountry()).isNotEqualTo(pm.getAddress().getCountry()); + } + + @Test + public void whenModifyingOriginalObject_thenJacksonCopyShouldNotChange() throws IOException { + Address address = new Address("Downing St 10", "London", "England"); + User pm = new User("Prime", "Minister", address); + ObjectMapper objectMapper = new ObjectMapper(); + User deepCopy = objectMapper.readValue(objectMapper.writeValueAsString(pm), User.class); + + address.setCountry("Great Britain"); + + assertThat(deepCopy.getAddress().getCountry()).isNotEqualTo(pm.getAddress().getCountry()); + } + + @Test + @Ignore + public void whenMakingCopies_thenShowHowLongEachMethodTakes() throws CloneNotSupportedException, IOException { + int times = 1000000; + Address address = new Address("Downing St 10", "London", "England"); + User pm = new User("Prime", "Minister", address); + + long start = System.currentTimeMillis(); + for (int i = 0; i < times; i++) { + User primeMinisterClone = (User) SerializationUtils.clone(pm); + } + long end = System.currentTimeMillis(); + System.out.println("Cloning with Apache Commons Lang took " + (end - start) + " milliseconds."); + + start = System.currentTimeMillis(); + Gson gson = new Gson(); + for (int i = 0; i < times; i++) { + User primeMinisterClone = gson.fromJson(gson.toJson(pm), User.class); + } + end = System.currentTimeMillis(); + System.out.println("Cloning with Gson took " + (end - start) + " milliseconds."); + + start = System.currentTimeMillis(); + for (int i = 0; i < times; i++) { + User primeMinisterClone = new User(pm); + } + end = System.currentTimeMillis(); + System.out.println("Cloning with the copy constructor took " + (end - start) + " milliseconds."); + + start = System.currentTimeMillis(); + for (int i = 0; i < times; i++) { + User primeMinisterClone = (User) pm.clone(); + } + end = System.currentTimeMillis(); + System.out.println("Cloning with Cloneable interface took " + (end - start) + " milliseconds."); + + start = System.currentTimeMillis(); + ObjectMapper objectMapper = new ObjectMapper(); + for (int i = 0; i < times; i++) { + User primeMinisterClone = objectMapper.readValue(objectMapper.writeValueAsString(pm), User.class); + } + end = System.currentTimeMillis(); + System.out.println("Cloning with Jackson took " + (end - start) + " milliseconds."); + } +} diff --git a/core-java/src/test/java/com/baeldung/deepcopy/ShallowCopyUnitTest.java b/core-java/src/test/java/com/baeldung/deepcopy/ShallowCopyUnitTest.java new file mode 100644 index 0000000000..57660fadf1 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/deepcopy/ShallowCopyUnitTest.java @@ -0,0 +1,33 @@ +package com.baeldung.deepcopy; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; + +public class ShallowCopyUnitTest { + + + @Test + public void whenShallowCopying_thenObjectsShouldNotBeSame() { + + Address address = new Address("Downing St 10", "London", "England"); + User pm = new User("Prime", "Minister", address); + + User shallowCopy = new User(pm.getFirstName(), pm.getLastName(), pm.getAddress()); + + assertThat(shallowCopy) + .isNotSameAs(pm); + } + + @Test + public void whenModifyingOriginalObject_thenCopyShouldChange() { + Address address = new Address("Downing St 10", "London", "England"); + User pm = new User("Prime", "Minister", address); + User shallowCopy = new User(pm.getFirstName(), pm.getLastName(), pm.getAddress()); + + address.setCountry("Great Britain"); + + assertThat(shallowCopy.getAddress().getCountry()) + .isEqualTo(pm.getAddress().getCountry()); + } +} diff --git a/core-java/src/test/java/com/baeldung/designpatterns/flyweight/FlyweightUnitTest.java b/core-java/src/test/java/com/baeldung/designpatterns/flyweight/FlyweightUnitTest.java new file mode 100644 index 0000000000..645e2fd459 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/designpatterns/flyweight/FlyweightUnitTest.java @@ -0,0 +1,42 @@ +package com.baeldung.designpatterns.flyweight; + +import java.awt.Color; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Unit test for {@link VehicleFactory}. + * + * @author Donato Rimenti + */ +public class FlyweightUnitTest { + + /** + * Checks that when the {@link VehicleFactory} is asked to provide two + * vehicles of different colors, the objects returned are different. + */ + @Test + public void givenDifferentFlyweightObjects_whenEquals_thenFalse() { + Vehicle blackVehicle = VehicleFactory.createVehicle(Color.BLACK); + Vehicle blueVehicle = VehicleFactory.createVehicle(Color.BLUE); + + Assert.assertNotNull("Object returned by the factory is null!", blackVehicle); + Assert.assertNotNull("Object returned by the factory is null!", blueVehicle); + Assert.assertNotEquals("Objects returned by the factory are equals!", blackVehicle, blueVehicle); + } + + /** + * Checks that when the {@link VehicleFactory} is asked to provide two + * vehicles of the same colors, the same object is returned twice. + */ + @Test + public void givenSameFlyweightObjects_whenEquals_thenTrue() { + Vehicle blackVehicle = VehicleFactory.createVehicle(Color.BLACK); + Vehicle anotherBlackVehicle = VehicleFactory.createVehicle(Color.BLACK); + + Assert.assertNotNull("Object returned by the factory is null!", blackVehicle); + Assert.assertNotNull("Object returned by the factory is null!", anotherBlackVehicle); + Assert.assertEquals("Objects returned by the factory are not equals!", blackVehicle, anotherBlackVehicle); + } +} diff --git a/core-java/src/test/java/com/baeldung/designpatterns/observer/ObserverIntegrationTest.java b/core-java/src/test/java/com/baeldung/designpatterns/observer/ObserverIntegrationTest.java new file mode 100644 index 0000000000..a8a0e29990 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/designpatterns/observer/ObserverIntegrationTest.java @@ -0,0 +1,47 @@ +package com.baeldung.designpatterns.observer; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import com.baeldung.designpatterns.observer.NewsAgency; +import com.baeldung.designpatterns.observer.NewsChannel; + +public class ObserverIntegrationTest { + + @Test + public void whenChangingNewsAgencyState_thenNewsChannelNotified() { + + NewsAgency observable = new NewsAgency(); + NewsChannel observer = new NewsChannel(); + + observable.addObserver(observer); + + observable.setNews("news"); + assertEquals(observer.getNews(), "news"); + } + + @Test + public void whenChangingONewsAgencyState_thenONewsChannelNotified() { + + ONewsAgency observable = new ONewsAgency(); + ONewsChannel observer = new ONewsChannel(); + + observable.addObserver(observer); + + observable.setNews("news"); + assertEquals(observer.getNews(), "news"); + } + + @Test + public void whenChangingPCLNewsAgencyState_thenONewsChannelNotified() { + + PCLNewsAgency observable = new PCLNewsAgency(); + PCLNewsChannel observer = new PCLNewsChannel(); + + observable.addPropertyChangeListener(observer); + + observable.setNews("news"); + assertEquals(observer.getNews(), "news"); + } +} diff --git a/core-java/src/test/java/com/baeldung/finalize/FinalizeUnitTest.java b/core-java/src/test/java/com/baeldung/finalize/FinalizeUnitTest.java new file mode 100644 index 0000000000..57d409074b --- /dev/null +++ b/core-java/src/test/java/com/baeldung/finalize/FinalizeUnitTest.java @@ -0,0 +1,23 @@ +package com.baeldung.finalize; + +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Test; + +public class FinalizeUnitTest { + @Test + public void whenGC_thenFinalizerExecuted() throws IOException { + String firstLine = new Finalizable().readFirstLine(); + Assert.assertEquals("baeldung.com", firstLine); + System.gc(); + } + + @Test + public void whenTryWResourcesExits_thenResourceClosed() throws IOException { + try (CloseableResource resource = new CloseableResource()) { + String firstLine = resource.readFirstLine(); + Assert.assertEquals("baeldung.com", firstLine); + } + } +} diff --git a/core-java/src/test/java/com/baeldung/inheritance/AppTest.java b/core-java/src/test/java/com/baeldung/inheritance/AppTest.java new file mode 100644 index 0000000000..1235761aba --- /dev/null +++ b/core-java/src/test/java/com/baeldung/inheritance/AppTest.java @@ -0,0 +1,46 @@ +package com.baeldung.inheritance; + +import com.baeldung.inheritance.*; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +public class AppTest extends TestCase { + + public AppTest(String testName) { + super( testName ); + } + + public static Test suite() { + return new TestSuite(AppTest.class); + } + + @SuppressWarnings("static-access") + public void testStaticMethodUsingBaseClassVariable() { + Car first = new ArmoredCar(); + assertEquals("Car", first.msg()); + } + + @SuppressWarnings("static-access") + public void testStaticMethodUsingDerivedClassVariable() { + ArmoredCar second = new ArmoredCar(); + assertEquals("ArmoredCar", second.msg()); + } + + public void testAssignArmoredCarToCar() { + Employee e1 = new Employee("Shreya", new ArmoredCar()); + assertNotNull(e1.getCar()); + } + + public void testAssignSpaceCarToCar() { + Employee e2 = new Employee("Paul", new SpaceCar()); + assertNotNull(e2.getCar()); + } + + public void testBMWToCar() { + Employee e3 = new Employee("Pavni", new BMW()); + assertNotNull(e3.getCar()); + } + +} diff --git a/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java b/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java new file mode 100644 index 0000000000..9ea42e88e8 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java @@ -0,0 +1,296 @@ +package com.baeldung.java.list; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +import org.junit.Test; + +public class CustomListUnitTest { + @Test + public void givenEmptyList_whenIsEmpty_thenTrueIsReturned() { + List list = new CustomList<>(); + + assertTrue(list.isEmpty()); + } + + @Test + public void givenNonEmptyList_whenIsEmpty_thenFalseIsReturned() { + List list = new CustomList<>(); + list.add(null); + + assertFalse(list.isEmpty()); + } + + @Test + public void givenListWithAnElement_whenSize_thenOneIsReturned() { + List list = new CustomList<>(); + list.add(null); + + assertEquals(1, list.size()); + } + + @Test + public void givenListWithAnElement_whenGet_thenThatElementIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + Object element = list.get(0); + + assertEquals("baeldung", element); + } + + @Test + public void givenEmptyList_whenElementIsAdded_thenGetReturnsThatElement() { + List list = new CustomList<>(); + boolean succeeded = list.add(null); + + assertTrue(succeeded); + } + + @Test + public void givenListWithAnElement_whenAnotherIsAdded_thenGetReturnsBoth() { + List list = new CustomList<>(); + list.add("baeldung"); + list.add(".com"); + Object element1 = list.get(0); + Object element2 = list.get(1); + + assertEquals("baeldung", element1); + assertEquals(".com", element2); + } + + @Test(expected = UnsupportedOperationException.class) + public void whenAddToSpecifiedIndex_thenExceptionIsThrown() { + new CustomList<>().add(0, null); + } + + @Test(expected = UnsupportedOperationException.class) + public void whenAddAllToTheEnd_thenExceptionIsThrown() { + Collection collection = new ArrayList<>(); + List list = new CustomList<>(); + list.addAll(collection); + } + + @Test(expected = UnsupportedOperationException.class) + public void whenAddAllToSpecifiedIndex_thenExceptionIsThrown() { + Collection collection = new ArrayList<>(); + List list = new CustomList<>(); + list.addAll(0, collection); + } + + @Test(expected = UnsupportedOperationException.class) + public void whenRemoveAtSpecifiedIndex_thenExceptionIsThrown() { + List list = new CustomList<>(); + list.add("baeldung"); + list.remove(0); + } + + @Test(expected = UnsupportedOperationException.class) + public void whenRemoveSpecifiedElement_thenExceptionIsThrown() { + List list = new CustomList<>(); + list.add("baeldung"); + list.remove("baeldung"); + } + + @Test(expected = UnsupportedOperationException.class) + public void whenRemoveAll_thenExceptionIsThrown() { + Collection collection = new ArrayList<>(); + collection.add("baeldung"); + List list = new CustomList<>(); + list.removeAll(collection); + } + + @Test(expected = UnsupportedOperationException.class) + public void whenRetainAll_thenExceptionIsThrown() { + Collection collection = new ArrayList<>(); + collection.add("baeldung"); + List list = new CustomList<>(); + list.add("baeldung"); + list.retainAll(collection); + } + + @Test + public void givenEmptyList_whenContains_thenFalseIsReturned() { + List list = new CustomList<>(); + + assertFalse(list.contains(null)); + } + + @Test + public void givenListWithAnElement_whenContains_thenTrueIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + + assertTrue(list.contains("baeldung")); + } + + @Test + public void givenListWithAnElement_whenContainsAll_thenTrueIsReturned() { + Collection collection = new ArrayList<>(); + collection.add("baeldung"); + List list = new CustomList<>(); + list.add("baeldung"); + + assertTrue(list.containsAll(collection)); + } + + @Test + public void givenList_whenContainsAll_thenEitherTrueOrFalseIsReturned() { + Collection collection1 = new ArrayList<>(); + collection1.add("baeldung"); + collection1.add(".com"); + Collection collection2 = new ArrayList<>(); + collection2.add("baeldung"); + + List list = new CustomList<>(); + list.add("baeldung"); + + assertFalse(list.containsAll(collection1)); + assertTrue(list.containsAll(collection2)); + } + + @Test + public void givenList_whenSet_thenOldElementIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + Object element = list.set(0, null); + + assertEquals("baeldung", element); + assertNull(list.get(0)); + } + + @Test + public void givenList_whenClear_thenAllElementsAreRemoved() { + List list = new CustomList<>(); + list.add("baeldung"); + list.clear(); + + assertTrue(list.isEmpty()); + } + + @Test + public void givenList_whenIndexOf_thenIndexZeroIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + + assertEquals(0, list.indexOf("baeldung")); + } + + @Test + public void givenList_whenIndexOf_thenPositiveIndexOrMinusOneIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + list.add(".com"); + list.add(".com"); + + assertEquals(1, list.indexOf(".com")); + assertEquals(-1, list.indexOf("com")); + } + + @Test + public void whenLastIndexOf_thenIndexZeroIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + + assertEquals(0, list.lastIndexOf("baeldung")); + } + + @Test + public void whenLastIndexOf_thenPositiveIndexOrMinusOneIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + list.add("baeldung"); + list.add(".com"); + + assertEquals(1, list.lastIndexOf("baeldung")); + assertEquals(-1, list.indexOf("com")); + } + + @Test + public void whenSubListZeroToOne_thenListContainingFirstElementIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + List subList = list.subList(0, 1); + + assertEquals("baeldung", subList.get(0)); + } + + @Test + public void whenSubListOneToTwo_thenListContainingSecondElementIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + list.add("."); + list.add("com"); + List subList = list.subList(1, 2); + + assertEquals(1, subList.size()); + assertEquals(".", subList.get(0)); + } + + @Test + public void givenListWithElements_whenToArray_thenArrayContainsThose() { + List list = new CustomList<>(); + list.add("baeldung"); + list.add(".com"); + Object[] array = list.toArray(); + + assertArrayEquals(new Object[] { "baeldung", ".com" }, array); + } + + @Test + public void givenListWithAnElement_whenToArray_thenInputArrayIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + String[] input = new String[1]; + String[] output = list.toArray(input); + + assertArrayEquals(new String[] { "baeldung" }, input); + } + + @Test + public void whenToArrayIsCalledWithEmptyInputArray_thenNewArrayIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + String[] input = {}; + String[] output = list.toArray(input); + + assertArrayEquals(new String[] { "baeldung" }, output); + } + + @Test + public void whenToArrayIsCalledWithLargerInput_thenOutputHasTrailingNull() { + List list = new CustomList<>(); + list.add("baeldung"); + String[] input = new String[2]; + String[] output = list.toArray(input); + + assertArrayEquals(new String[] { "baeldung", null }, output); + } + + @Test + public void givenListWithOneElement_whenIterator_thenThisElementIsNext() { + List list = new CustomList<>(); + list.add("baeldung"); + Iterator iterator = list.iterator(); + + assertTrue(iterator.hasNext()); + assertEquals("baeldung", iterator.next()); + } + + @Test + public void whenIteratorHasNextIsCalledTwice_thenTheSecondReturnsFalse() { + List list = new CustomList<>(); + list.add("baeldung"); + Iterator iterator = list.iterator(); + + iterator.next(); + assertFalse(iterator.hasNext()); + } +} diff --git a/core-java/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverloadingUnitTest.java b/core-java/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverloadingUnitTest.java new file mode 100644 index 0000000000..081a30c34a --- /dev/null +++ b/core-java/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverloadingUnitTest.java @@ -0,0 +1,41 @@ +package com.baeldung.methodoverloadingoverriding.test; + +import com.baeldung.methodoverloadingoverriding.util.Multiplier; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.assertj.core.api.Assertions.*; + +public class MethodOverloadingUnitTest { + + private static Multiplier multiplier; + + @BeforeClass + public static void setUpMultiplierInstance() { + multiplier = new Multiplier(); + } + + @Test + public void givenMultiplierInstance_whenCalledMultiplyWithTwoIntegers_thenOneAssertion() { + assertThat(multiplier.multiply(10, 10)).isEqualTo(100); + } + + @Test + public void givenMultiplierInstance_whenCalledMultiplyWithTreeIntegers_thenOneAssertion() { + assertThat(multiplier.multiply(10, 10, 10)).isEqualTo(1000); + } + + @Test + public void givenMultiplierInstance_whenCalledMultiplyWithIntDouble_thenOneAssertion() { + assertThat(multiplier.multiply(10, 10.5)).isEqualTo(105.0); + } + + @Test + public void givenMultiplierInstance_whenCalledMultiplyWithDoubleDouble_thenOneAssertion() { + assertThat(multiplier.multiply(10.5, 10.5)).isEqualTo(110.25); + } + + @Test + public void givenMultiplierInstance_whenCalledMultiplyWithIntIntAndMatching_thenNoTypePromotion() { + assertThat(multiplier.multiply(10, 10)).isEqualTo(100); + } +} diff --git a/core-java/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverridingUnitTest.java b/core-java/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverridingUnitTest.java new file mode 100644 index 0000000000..554ac121bc --- /dev/null +++ b/core-java/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverridingUnitTest.java @@ -0,0 +1,68 @@ +package com.baeldung.methodoverloadingoverriding.test; + +import com.baeldung.methodoverloadingoverriding.model.Car; +import com.baeldung.methodoverloadingoverriding.model.Vehicle; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.assertj.core.api.Assertions.*; + +public class MethodOverridingUnitTest { + + private static Vehicle vehicle; + private static Car car; + + @BeforeClass + public static void setUpVehicleInstance() { + vehicle = new Vehicle(); + } + + @BeforeClass + public static void setUpCarInstance() { + car = new Car(); + } + + @Test + public void givenVehicleInstance_whenCalledAccelerate_thenOneAssertion() { + assertThat(vehicle.accelerate(100)).isEqualTo("The vehicle accelerates at : 100 MPH."); + } + + @Test + public void givenVehicleInstance_whenCalledRun_thenOneAssertion() { + assertThat(vehicle.run()).isEqualTo("The vehicle is running."); + } + + @Test + public void givenVehicleInstance_whenCalledStop_thenOneAssertion() { + assertThat(vehicle.stop()).isEqualTo("The vehicle has stopped."); + } + + @Test + public void givenCarInstance_whenCalledAccelerate_thenOneAssertion() { + assertThat(car.accelerate(80)).isEqualTo("The car accelerates at : 80 MPH."); + } + + @Test + public void givenCarInstance_whenCalledRun_thenOneAssertion() { + assertThat(car.run()).isEqualTo("The vehicle is running."); + } + + @Test + public void givenCarInstance_whenCalledStop_thenOneAssertion() { + assertThat(car.stop()).isEqualTo("The vehicle has stopped."); + } + + @Test + public void givenVehicleCarInstances_whenCalledAccelerateWithSameArgument_thenNotEqual() { + assertThat(vehicle.accelerate(100)).isNotEqualTo(car.accelerate(100)); + } + + @Test + public void givenVehicleCarInstances_whenCalledRun_thenEqual() { + assertThat(vehicle.run()).isEqualTo(car.run()); + } + + @Test + public void givenVehicleCarInstances_whenCalledStop_thenEqual() { + assertThat(vehicle.stop()).isEqualTo(car.stop()); + } +} diff --git a/core-java/src/test/java/com/baeldung/recursion/RecursionExampleTest.java b/core-java/src/test/java/com/baeldung/recursion/RecursionExampleTest.java new file mode 100644 index 0000000000..c65be24240 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/recursion/RecursionExampleTest.java @@ -0,0 +1,61 @@ +package com.baeldung.recursion; + +import org.junit.Assert; +import org.junit.Test; + +public class RecursionExampleTest { + + RecursionExample recursion = new RecursionExample(); + + @Test + public void testPowerOf10() { + int p0 = recursion.powerOf10(0); + int p1 = recursion.powerOf10(1); + int p4 = recursion.powerOf10(4); + + Assert.assertEquals(1, p0); + Assert.assertEquals(10, p1); + Assert.assertEquals(10000, p4); + } + + @Test + public void testFibonacci() { + int n0 = recursion.fibonacci(0); + int n1 = recursion.fibonacci(1); + int n7 = recursion.fibonacci(7); + + Assert.assertEquals(0, n0); + Assert.assertEquals(1, n1); + Assert.assertEquals(13, n7); + } + + @Test + public void testToBinary() { + String b0 = recursion.toBinary(0); + String b1 = recursion.toBinary(1); + String b10 = recursion.toBinary(10); + + Assert.assertEquals("0", b0); + Assert.assertEquals("1", b1); + Assert.assertEquals("1010", b10); + } + + @Test + public void testCalculateTreeHeight() { + BinaryNode root = new BinaryNode(1); + root.setLeft(new BinaryNode(1)); + root.setRight(new BinaryNode(1)); + + root.getLeft().setLeft(new BinaryNode(1)); + root.getLeft().getLeft().setRight(new BinaryNode(1)); + root.getLeft().getLeft().getRight().setLeft(new BinaryNode(1)); + + root.getRight().setLeft(new BinaryNode(1)); + root.getRight().getLeft().setRight(new BinaryNode(1)); + + int height = recursion.calculateTreeHeight(root); + + Assert.assertEquals(4, height); + } + +} diff --git a/core-java/src/test/java/com/baeldung/resourcebundle/ExampleResourceUnitTest.java b/core-java/src/test/java/com/baeldung/resourcebundle/ExampleResourceUnitTest.java new file mode 100644 index 0000000000..1ce4f1bb8f --- /dev/null +++ b/core-java/src/test/java/com/baeldung/resourcebundle/ExampleResourceUnitTest.java @@ -0,0 +1,39 @@ +package com.baeldung.resourcebundle; + +import org.junit.Test; + +import java.math.BigDecimal; +import java.util.Arrays; +import java.util.Locale; +import java.util.ResourceBundle; + +import static org.junit.Assert.*; + +public class ExampleResourceUnitTest { + + @Test + public void whenGetBundleExampleResourceForLocalePlPl_thenItShouldInheritPropertiesGreetingAndLanguage() { + Locale plLocale = new Locale("pl", "PL"); + + ResourceBundle exampleBundle = ResourceBundle.getBundle("com.baeldung.resourcebundle.ExampleResource", plLocale); + + assertTrue(exampleBundle.keySet() + .containsAll(Arrays.asList("toUsdRate", "cities", "greeting", "currency", "language"))); + assertEquals(exampleBundle.getString("greeting"), "cześć"); + assertEquals(exampleBundle.getObject("toUsdRate"), new BigDecimal("3.401")); + assertArrayEquals(exampleBundle.getStringArray("cities"), new String[] { "Warsaw", "Cracow" }); + } + + @Test + public void whenGetBundleExampleResourceForLocaleUs_thenItShouldContainOnlyGreeting() { + Locale usLocale = Locale.US; + + ResourceBundle exampleBundle = ResourceBundle.getBundle("com.baeldung.resourcebundle.ExampleResource", usLocale); + + assertFalse(exampleBundle.keySet() + .containsAll(Arrays.asList("toUsdRate", "cities", "currency", "language"))); + assertTrue(exampleBundle.keySet() + .containsAll(Arrays.asList("greeting"))); + } + +} diff --git a/core-java/src/test/java/com/baeldung/resourcebundle/PropertyResourceUnitTest.java b/core-java/src/test/java/com/baeldung/resourcebundle/PropertyResourceUnitTest.java new file mode 100644 index 0000000000..4da71567e1 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/resourcebundle/PropertyResourceUnitTest.java @@ -0,0 +1,62 @@ +package com.baeldung.resourcebundle; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.Locale; +import java.util.ResourceBundle; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class PropertyResourceUnitTest { + + @Test + public void givenLocaleUsAsDefualt_whenGetBundleForLocalePlPl_thenItShouldContain3ButtonsAnd1Label() { + Locale.setDefault(Locale.US); + + ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("pl", "PL")); + + assertTrue(bundle.keySet() + .containsAll(Arrays.asList("backButton", "helloLabel", "cancelButton", "continueButton", "helloLabelNoEncoding"))); + } + + @Test + public void givenLocaleUsAsDefualt_whenGetBundleForLocaleFrFr_thenItShouldContainKeys1To3AndKey4() { + Locale.setDefault(Locale.US); + + ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("fr", "FR")); + + assertTrue(bundle.keySet() + .containsAll(Arrays.asList("deleteButton", "helloLabel", "cancelButton", "continueButton"))); + } + + @Test + public void givenLocaleChinaAsDefualt_whenGetBundleForLocaleFrFr_thenItShouldOnlyContainKeys1To3() { + Locale.setDefault(Locale.CHINA); + + ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("fr", "FR")); + + assertTrue(bundle.keySet() + .containsAll(Arrays.asList("continueButton", "helloLabel", "cancelButton"))); + } + + @Test + public void givenLocaleChinaAsDefualt_whenGetBundleForLocaleFrFrAndExampleControl_thenItShouldOnlyContainKey5() { + Locale.setDefault(Locale.CHINA); + + ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("fr", "FR"), new ExampleControl()); + + assertTrue(bundle.keySet() + .containsAll(Arrays.asList("backButton", "helloLabel"))); + } + + @Test + public void givenValuesDifferentlyEncoded_whenGetBundleForLocalePlPl_thenItShouldContain3ButtonsAnd1Label() { + ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("pl", "PL")); + + assertEquals(bundle.getString("helloLabel"), "cześć"); + assertEquals(bundle.getString("helloLabelNoEncoding"), "czeÅ\u009BÄ\u0087"); + } + +} diff --git a/core-java/src/test/java/com/baeldung/string/PalindromeTest.java b/core-java/src/test/java/com/baeldung/string/PalindromeTest.java new file mode 100644 index 0000000000..bc6fee2cd9 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/string/PalindromeTest.java @@ -0,0 +1,97 @@ +package com.baeldung.string; + +import static org.junit.Assert.*; +import org.junit.Test; + +public class PalindromeTest { + + private String[] words = { + "Anna", + "Civic", + "Kayak", + "Level", + "Madam", + }; + + private String[] sentences = { + "Sore was I ere I saw Eros", + "Euston saw I was not Sue", + "Too hot to hoot", + "No mists or frost Simon", + "Stella won no wallets" + }; + + private Palindrome palindrome = new Palindrome(); + + @Test + public void whenWord_shouldBePalindrome() { + for (String word : words) + assertTrue(palindrome.isPalindrome(word)); + } + + @Test + public void whenSentence_shouldBePalindrome() { + for (String sentence : sentences) + assertTrue(palindrome.isPalindrome(sentence)); + } + + @Test + public void whenReverseWord_shouldBePalindrome() { + for (String word : words) + assertTrue(palindrome.isPalindromeReverseTheString(word)); + } + + @Test + public void whenReverseSentence_shouldBePalindrome() { + for (String sentence : sentences) + assertTrue(palindrome.isPalindromeReverseTheString(sentence)); + } + + @Test + public void whenStringBuilderWord_shouldBePalindrome() { + for (String word : words) + assertTrue(palindrome.isPalindromeUsingStringBuilder(word)); + } + + @Test + public void whenStringBuilderSentence_shouldBePalindrome() { + for (String sentence : sentences) + assertTrue(palindrome.isPalindromeUsingStringBuilder(sentence)); + } + + @Test + public void whenStringBufferWord_shouldBePalindrome() { + for (String word : words) + assertTrue(palindrome.isPalindromeUsingStringBuffer(word)); + } + + @Test + public void whenStringBufferSentence_shouldBePalindrome() { + for (String sentence : sentences) + assertTrue(palindrome.isPalindromeUsingStringBuffer(sentence)); + } + + @Test + public void whenPalindromeRecursive_wordShouldBePalindrome() { + for (String word : words) + assertTrue(palindrome.isPalindromeRecursive(word)); + } + + @Test + public void whenPalindromeRecursive_sentenceShouldBePalindrome() { + for (String sentence : sentences) + assertTrue(palindrome.isPalindromeRecursive(sentence)); + } + + @Test + public void whenPalindromeStreams_wordShouldBePalindrome() { + for (String word : words) + assertTrue(palindrome.isPalindromeUsingIntStream(word)); + } + + @Test + public void whenPalindromeStreams_sentenceShouldBePalindrome() { + for (String sentence : sentences) + assertTrue(palindrome.isPalindromeUsingIntStream(sentence)); + } +} diff --git a/core-java/src/test/java/com/baeldung/string/StringComparisonTest.java b/core-java/src/test/java/com/baeldung/string/StringComparisonTest.java new file mode 100644 index 0000000000..5869676004 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/string/StringComparisonTest.java @@ -0,0 +1,154 @@ +package com.baeldung.string; + +import org.apache.commons.lang3.StringUtils; +import org.junit.Test; + +import java.util.Objects; + +import static org.assertj.core.api.Assertions.assertThat; + +public class StringComparisonTest { + + @Test + public void whenUsingComparisonOperator_ThenComparingStrings(){ + + String string1 = "using comparison operator"; + String string2 = "using comparison operator"; + String string3 = new String("using comparison operator"); + + assertThat(string1 == string2).isTrue(); + assertThat(string1 == string3).isFalse(); + } + + @Test + public void whenUsingEqualsMethod_ThenComparingStrings(){ + + String string1 = "using equals method"; + String string2 = "using equals method"; + + String string3 = "using EQUALS method"; + String string4 = new String("using equals method"); + + assertThat(string1.equals(string2)).isTrue(); + assertThat(string1.equals(string4)).isTrue(); + + assertThat(string1.equals(null)).isFalse(); + assertThat(string1.equals(string3)).isFalse(); + } + + @Test + public void whenUsingEqualsIgnoreCase_ThenComparingStrings(){ + + String string1 = "using equals ignore case"; + String string2 = "USING EQUALS IGNORE CASE"; + + assertThat(string1.equalsIgnoreCase(string2)).isTrue(); + } + + @Test + public void whenUsingCompareTo_ThenComparingStrings(){ + + String author = "author"; + String book = "book"; + String duplicateBook = "book"; + + assertThat(author.compareTo(book)).isEqualTo(-1); + assertThat(book.compareTo(author)).isEqualTo(1); + assertThat(duplicateBook.compareTo(book)).isEqualTo(0); + } + + @Test + public void whenUsingCompareToIgnoreCase_ThenComparingStrings(){ + + String author = "Author"; + String book = "book"; + String duplicateBook = "BOOK"; + + assertThat(author.compareToIgnoreCase(book)).isEqualTo(-1); + assertThat(book.compareToIgnoreCase(author)).isEqualTo(1); + assertThat(duplicateBook.compareToIgnoreCase(book)).isEqualTo(0); + } + + @Test + public void whenUsingObjectsEqualsMethod_ThenComparingStrings(){ + + String string1 = "using objects equals"; + String string2 = "using objects equals"; + String string3 = new String("using objects equals"); + + assertThat(Objects.equals(string1, string2)).isTrue(); + assertThat(Objects.equals(string1, string3)).isTrue(); + + assertThat(Objects.equals(null, null)).isTrue(); + assertThat(Objects.equals(null, string1)).isFalse(); + } + + @Test + public void whenUsingEqualsOfApacheCommons_ThenComparingStrings(){ + + assertThat(StringUtils.equals(null, null)).isTrue(); + assertThat(StringUtils.equals(null, "equals method")).isFalse(); + assertThat(StringUtils.equals("equals method", "equals method")).isTrue(); + assertThat(StringUtils.equals("equals method", "EQUALS METHOD")).isFalse(); + } + + @Test + public void whenUsingEqualsIgnoreCaseOfApacheCommons_ThenComparingStrings(){ + + assertThat(StringUtils.equalsIgnoreCase(null, null)).isTrue(); + assertThat(StringUtils.equalsIgnoreCase(null, "equals method")).isFalse(); + assertThat(StringUtils.equalsIgnoreCase("equals method", "equals method")).isTrue(); + assertThat(StringUtils.equalsIgnoreCase("equals method", "EQUALS METHOD")).isTrue(); + } + + @Test + public void whenUsingEqualsAnyOf_ThenComparingStrings(){ + + assertThat(StringUtils.equalsAny(null, null, null)).isTrue(); + assertThat(StringUtils.equalsAny("equals any", "equals any", "any")).isTrue(); + assertThat(StringUtils.equalsAny("equals any", null, "equals any")).isTrue(); + assertThat(StringUtils.equalsAny(null, "equals", "any")).isFalse(); + assertThat(StringUtils.equalsAny("equals any", "EQUALS ANY", "ANY")).isFalse(); + } + + @Test + public void whenUsingEqualsAnyIgnoreCase_ThenComparingStrings(){ + + assertThat(StringUtils.equalsAnyIgnoreCase(null, null, null)).isTrue(); + assertThat(StringUtils.equalsAnyIgnoreCase("equals any", "equals any", "any")).isTrue(); + assertThat(StringUtils.equalsAnyIgnoreCase("equals any", null, "equals any")).isTrue(); + assertThat(StringUtils.equalsAnyIgnoreCase(null, "equals", "any")).isFalse(); + assertThat(StringUtils.equalsAnyIgnoreCase( + "equals any ignore case", "EQUALS ANY IGNORE CASE", "any")).isTrue(); + } + + @Test + public void whenUsingCompare_thenComparingStringsWithNulls(){ + + assertThat(StringUtils.compare(null, null)).isEqualTo(0); + assertThat(StringUtils.compare(null, "abc")).isEqualTo(-1); + + assertThat(StringUtils.compare("abc", "bbc")).isEqualTo(-1); + assertThat(StringUtils.compare("bbc", "abc")).isEqualTo(1); + assertThat(StringUtils.compare("abc", "abc")).isEqualTo(0); + } + + @Test + public void whenUsingCompareIgnoreCase_ThenComparingStringsWithNulls(){ + + assertThat(StringUtils.compareIgnoreCase(null, null)).isEqualTo(0); + assertThat(StringUtils.compareIgnoreCase(null, "abc")).isEqualTo(-1); + + assertThat(StringUtils.compareIgnoreCase("Abc", "bbc")).isEqualTo(-1); + assertThat(StringUtils.compareIgnoreCase("bbc", "ABC")).isEqualTo(1); + assertThat(StringUtils.compareIgnoreCase("abc", "ABC")).isEqualTo(0); + } + + @Test + public void whenUsingCompareWithNullIsLessOption_ThenComparingStrings(){ + + assertThat(StringUtils.compare(null, "abc", true)).isEqualTo(-1); + assertThat(StringUtils.compare(null, "abc", false)).isEqualTo(1); + } + +} diff --git a/core-java/src/test/java/com/baeldung/system/WhenDetectingOSTest.java b/core-java/src/test/java/com/baeldung/system/WhenDetectingOSTest.java new file mode 100644 index 0000000000..77901f6524 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/system/WhenDetectingOSTest.java @@ -0,0 +1,25 @@ +package com.baeldung.system; + +import org.junit.Assert; +import org.junit.Ignore; +import org.junit.Test; + +@Ignore +public class WhenDetectingOSTest { + + private DetectOS os = new DetectOS(); + + @Test + public void whenUsingSystemProperty_shouldReturnOS() { + String expected = "Windows 10"; + String actual = os.getOperatingSystem(); + Assert.assertEquals(expected, actual); + } + + @Test + public void whenUsingSystemUtils_shouldReturnOS() { + String expected = "Windows 10"; + String actual = os.getOperatingSystemSystemUtils(); + Assert.assertEquals(expected, actual); + } +} diff --git a/core-java/src/test/java/com/baeldung/trie/TrieTest.java b/core-java/src/test/java/com/baeldung/trie/TrieTest.java new file mode 100644 index 0000000000..be7e5575d8 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/trie/TrieTest.java @@ -0,0 +1,68 @@ +package com.baeldung.trie; + +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class TrieTest { + + @Test + public void whenEmptyTrie_thenNoElements() { + Trie trie = new Trie(); + + assertFalse(trie.isEmpty()); + } + + @Test + public void givenATrie_whenAddingElements_thenTrieNotEmpty() { + Trie trie = createExampleTrie(); + + assertFalse(trie.isEmpty()); + } + + @Test + public void givenATrie_whenAddingElements_thenTrieHasThoseElements() { + Trie trie = createExampleTrie(); + + assertFalse(trie.containsNode("3")); + assertFalse(trie.containsNode("vida")); + + assertTrue(trie.containsNode("Programming")); + assertTrue(trie.containsNode("is")); + assertTrue(trie.containsNode("a")); + assertTrue(trie.containsNode("way")); + assertTrue(trie.containsNode("of")); + assertTrue(trie.containsNode("life")); + } + + @Test + public void givenATrie_whenLookingForNonExistingElement_thenReturnsFalse() { + Trie trie = createExampleTrie(); + + assertFalse(trie.containsNode("99")); + } + + @Test + public void givenATrie_whenDeletingElements_thenTreeDoesNotContainThoseElements() { + + Trie trie = createExampleTrie(); + + assertTrue(trie.containsNode("Programming")); + trie.delete("Programming"); + assertFalse(trie.containsNode("Programming")); + } + + private Trie createExampleTrie() { + Trie trie = new Trie(); + + trie.insert("Programming"); + trie.insert("is"); + trie.insert("a"); + trie.insert("way"); + trie.insert("of"); + trie.insert("life"); + + return trie; + } +} diff --git a/core-java/src/test/resources/testFolder/sample_file_2.in b/core-java/src/test/resources/testFolder/sample_file_2.in deleted file mode 100644 index 93b493a513..0000000000 --- a/core-java/src/test/resources/testFolder/sample_file_2.in +++ /dev/null @@ -1 +0,0 @@ -Hello world ! \ No newline at end of file diff --git a/core-kotlin/README.md b/core-kotlin/README.md index 4b5f921f7b..b8cea19c58 100644 --- a/core-kotlin/README.md +++ b/core-kotlin/README.md @@ -16,5 +16,8 @@ - [Delegated Properties in Kotlin](http://www.baeldung.com/kotlin-delegated-properties) - [Sealed Classes in Kotlin](http://www.baeldung.com/kotlin-sealed-classes) - [JUnit 5 for Kotlin Developers](http://www.baeldung.com/junit-5-kotlin) - - +- [Extension Methods in Kotlin](http://www.baeldung.com/kotlin-extension-methods) +- [Infix Functions in Kotlin](http://www.baeldung.com/kotlin-infix-functions) +- [Try-with-resources in Kotlin](http://www.baeldung.com/kotlin-try-with-resources) +- [HTTP Requests with Kotlin and khttp](http://www.baeldung.com/kotlin-khttp) +- [Kotlin Dependency Injection with Kodein](http://www.baeldung.com/kotlin-kodein-dependency-injection) diff --git a/core-kotlin/pom.xml b/core-kotlin/pom.xml index b511f0dd7b..36298ca084 100644 --- a/core-kotlin/pom.xml +++ b/core-kotlin/pom.xml @@ -44,6 +44,16 @@ kotlin-stdlib ${kotlin-stdlib.version} + + org.jetbrains.kotlin + kotlin-stdlib-jre8 + ${kotlin-stdlib.version} + + + khttp + khttp + 0.1.0 + org.jetbrains.kotlin kotlin-test-junit @@ -66,7 +76,18 @@ mockito-kotlin ${mockito-kotlin.version} test - + + + com.github.salomonbrys.kodein + kodein + ${kodein.version} + + + org.assertj + assertj-core + ${assertj.version} + test + @@ -179,11 +200,13 @@ 1.1.2 0.15 1.5.0 + 4.1.0 5.0.0 1.0.0 4.12.0 4.12 + 3.9.1 diff --git a/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/Controller.kt b/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/Controller.kt new file mode 100644 index 0000000000..721bdb04bc --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/Controller.kt @@ -0,0 +1,3 @@ +package com.baeldung.kotlin.kodein + +class Controller(private val service : Service) \ No newline at end of file diff --git a/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/Dao.kt b/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/Dao.kt new file mode 100644 index 0000000000..a0be7ef0e0 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/Dao.kt @@ -0,0 +1,3 @@ +package com.baeldung.kotlin.kodein + +interface Dao \ No newline at end of file diff --git a/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/JdbcDao.kt b/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/JdbcDao.kt new file mode 100644 index 0000000000..0a09b95dbf --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/JdbcDao.kt @@ -0,0 +1,3 @@ +package com.baeldung.kotlin.kodein + +class JdbcDao : Dao \ No newline at end of file diff --git a/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/MongoDao.kt b/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/MongoDao.kt new file mode 100644 index 0000000000..06436fcd21 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/MongoDao.kt @@ -0,0 +1,3 @@ +package com.baeldung.kotlin.kodein + +class MongoDao : Dao \ No newline at end of file diff --git a/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/Service.kt b/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/Service.kt new file mode 100644 index 0000000000..bb24a5cc21 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/Service.kt @@ -0,0 +1,3 @@ +package com.baeldung.kotlin.kodein + +class Service(private val dao: Dao, private val tag: String) \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/InfixFunctionsTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/InfixFunctionsTest.kt new file mode 100644 index 0000000000..fc4286460a --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/InfixFunctionsTest.kt @@ -0,0 +1,51 @@ +package com.baeldung.kotlin + +import org.junit.Assert +import org.junit.Test + +class InfixFunctionsTest { + @Test + fun testColours() { + val color = 0x123456 + val red = (color and 0xff0000) shr 16 + val green = (color and 0x00ff00) shr 8 + val blue = (color and 0x0000ff) shr 0 + + Assert.assertEquals(0x12, red) + Assert.assertEquals(0x34, green) + Assert.assertEquals(0x56, blue) + } + + @Test + fun testNewAssertions() { + class Assertion(private val target: T) { + infix fun isEqualTo(other: T) { + Assert.assertEquals(other, target) + } + + infix fun isDifferentFrom(other: T) { + Assert.assertNotEquals(other, target) + } + } + + val result = Assertion(5) + + result isEqualTo 5 + + // The following two lines are expected to fail + // result isEqualTo 6 + // result isDifferentFrom 5 + } + + @Test + fun testNewStringMethod() { + infix fun String.substringMatches(r: Regex) : List { + return r.findAll(this) + .map { it.value } + .toList() + } + + val matches = "a bc def" substringMatches ".*? ".toRegex() + Assert.assertEquals(listOf("a ", "bc "), matches) + } +} diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/UseTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/UseTest.kt new file mode 100644 index 0000000000..15bdfcafd8 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/UseTest.kt @@ -0,0 +1,67 @@ +package com.baeldung.kotlin + +import org.junit.Test +import java.beans.ExceptionListener +import java.beans.XMLEncoder +import java.io.* +import java.lang.Exception +import kotlin.test.assertEquals +import kotlin.test.assertTrue +import kotlin.test.fail + +class UseTest { + + @Test + fun givenCloseable_whenUseIsCalled_thenItIsClosed() { + val stringWriter = StringWriter() + val writer = BufferedWriter(stringWriter) //Using a BufferedWriter because after close() it throws. + writer.use { + assertEquals(writer, it) + + it.write("something") + } + try { + writer.write("something else") + + fail("write() should have thrown an exception because the writer is closed.") + } catch (e: IOException) { + //Ok + } + + assertEquals("something", stringWriter.toString()) + } + + @Test + fun givenAutoCloseable_whenUseIsCalled_thenItIsClosed() { + val baos = ByteArrayOutputStream() + val encoder = XMLEncoder(PrintStream(baos)) //XMLEncoder is AutoCloseable but not Closeable. + //Here, we use a PrintStream because after close() it throws. + encoder.exceptionListener = ThrowingExceptionListener() + encoder.use { + assertEquals(encoder, it) + + it.writeObject("something") + } + try { + encoder.writeObject("something else") + encoder.flush() + + fail("write() should have thrown an exception because the encoder is closed.") + } catch (e: IOException) { + //Ok + } + } + + @Test + fun whenSimpleFormIsUsed_thenItWorks() { + StringWriter().use { it.write("something") } + } +} + +class ThrowingExceptionListener : ExceptionListener { + override fun exceptionThrown(e: Exception?) { + if(e != null) { + throw e + } + } +} \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/khttp/KhttpTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/khttp/KhttpTest.kt new file mode 100644 index 0000000000..e9147c9489 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/khttp/KhttpTest.kt @@ -0,0 +1,153 @@ +package com.baeldung.kotlin.khttp + +import khttp.structures.files.FileLike +import org.json.JSONObject +import org.junit.Test +import java.beans.ExceptionListener +import java.beans.XMLEncoder +import java.io.* +import java.lang.Exception +import java.net.ConnectException +import kotlin.test.assertEquals +import kotlin.test.assertTrue +import kotlin.test.fail + +class KhttpTest { + + @Test + fun whenHttpGetRequestIsMade_thenArgsAreReturned() { + val response = khttp.get( + url = "http://httpbin.org/get", + params = mapOf("p1" to "1", "p2" to "2")) + val args = response.jsonObject.getJSONObject("args") + + assertEquals("1", args["p1"]) + assertEquals("2", args["p2"]) + } + + @Test + fun whenAlternateHttpGetRequestIsMade_thenArgsAreReturned() { + val response = khttp.request( + method = "GET", + url = "http://httpbin.org/get", + params = mapOf("p1" to "1", "p2" to "2")) + val args = response.jsonObject.getJSONObject("args") + + assertEquals("1", args["p1"]) + assertEquals("2", args["p2"]) + } + + @Test + fun whenHeadersAreSet_thenHeadersAreSent() { + val response = khttp.get( + url = "http://httpbin.org/get", + headers = mapOf("header1" to "1", "header2" to "2")) + val headers = response.jsonObject.getJSONObject("headers") + + assertEquals("1", headers["Header1"]) + assertEquals("2", headers["Header2"]) + } + + @Test + fun whenHttpPostRequestIsMadeWithJson_thenBodyIsReturned() { + val response = khttp.post( + url = "http://httpbin.org/post", + params = mapOf("p1" to "1", "p2" to "2"), + json = mapOf("pr1" to "1", "pr2" to "2")) + + val args = response.jsonObject.getJSONObject("args") + + assertEquals("1", args["p1"]) + assertEquals("2", args["p2"]) + + val json = response.jsonObject.getJSONObject("json") + + assertEquals("1", json["pr1"]) + assertEquals("2", json["pr2"]) + } + + @Test + fun whenHttpPostRequestIsMadeWithMapData_thenBodyIsReturned() { + val response = khttp.post( + url = "http://httpbin.org/post", + params = mapOf("p1" to "1", "p2" to "2"), + data = mapOf("pr1" to "1", "pr2" to "2")) + + val args = response.jsonObject.getJSONObject("args") + + assertEquals("1", args["p1"]) + assertEquals("2", args["p2"]) + + val form = response.jsonObject.getJSONObject("form") + + assertEquals("1", form["pr1"]) + assertEquals("2", form["pr2"]) + } + + @Test + fun whenHttpPostRequestIsMadeWithFiles_thenBodyIsReturned() { + val response = khttp.post( + url = "http://httpbin.org/post", + params = mapOf("p1" to "1", "p2" to "2"), + files = listOf( + FileLike("file1", "content1"), + FileLike("file2", javaClass.getResource("KhttpTest.class").openStream().readBytes()))) + + val args = response.jsonObject.getJSONObject("args") + + assertEquals("1", args["p1"]) + assertEquals("2", args["p2"]) + + val files = response.jsonObject.getJSONObject("files") + + assertEquals("content1", files["file1"]) + } + + @Test + fun whenHttpPostRequestIsMadeWithInputStream_thenBodyIsReturned() { + val response = khttp.post( + url = "http://httpbin.org/post", + params = mapOf("p1" to "1", "p2" to "2"), + data = ByteArrayInputStream("content!".toByteArray())) + + val args = response.jsonObject.getJSONObject("args") + + assertEquals("1", args["p1"]) + assertEquals("2", args["p2"]) + + assertEquals("content!", response.jsonObject["data"]) + } + + @Test + fun whenHttpPostStreamingRequestIsMade_thenBodyIsReturnedInChunks() { + val response = khttp.post( + url = "http://httpbin.org/post", + stream = true, + json = mapOf("pr1" to "1", "pr2" to "2")) + + val baos = ByteArrayOutputStream() + response.contentIterator(chunkSize = 10).forEach { arr : ByteArray -> baos.write(arr) } + val json = JSONObject(String(baos.toByteArray())).getJSONObject("json") + + assertEquals("1", json["pr1"]) + assertEquals("2", json["pr2"]) + } + + @Test + fun whenHttpRequestFails_thenExceptionIsThrown() { + try { + khttp.get(url = "http://localhost/nothing/to/see/here") + + fail("Should have thrown an exception") + } catch (e : ConnectException) { + //Ok + } + } + + @Test + fun whenHttpNotFound_thenExceptionIsThrown() { + val response = khttp.get(url = "http://httpbin.org/nothing/to/see/here") + + assertEquals(404, response.statusCode) + } +} \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/kodein/KodeinUnitTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/kodein/KodeinUnitTest.kt new file mode 100644 index 0000000000..7776eebd52 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/kodein/KodeinUnitTest.kt @@ -0,0 +1,191 @@ +package com.baeldung.kotlin.kodein + +import com.github.salomonbrys.kodein.* +import org.assertj.core.api.Assertions.assertThat +import org.junit.Test + +class KodeinUnitTest { + + class InMemoryDao : Dao + + @Test + fun whenSingletonBinding_thenSingleInstanceIsCreated() { + var created = false + val kodein = Kodein { + bind() with singleton { + created = true + MongoDao() + } + } + + assertThat(created).isFalse() + + val dao1: Dao = kodein.instance() + + assertThat(created).isTrue() + + val dao2: Dao = kodein.instance() + + assertThat(dao1).isSameAs(dao2) + } + + @Test + fun whenFactoryBinding_thenNewInstanceIsCreated() { + val kodein = Kodein { + bind() with singleton { MongoDao() } + bind() with factory { tag: String -> Service(instance(), tag) } + } + val service1: Service = kodein.with("myTag").instance() + val service2: Service = kodein.with("myTag").instance() + + assertThat(service1).isNotSameAs(service2) + } + + @Test + fun whenProviderBinding_thenNewInstanceIsCreated() { + val kodein = Kodein { + bind() with provider { MongoDao() } + } + val dao1: Dao = kodein.instance() + val dao2: Dao = kodein.instance() + + assertThat(dao1).isNotSameAs(dao2) + } + + @Test + fun whenTaggedBinding_thenMultipleInstancesOfSameTypeCanBeRegistered() { + val kodein = Kodein { + bind("dao1") with singleton { MongoDao() } + bind("dao2") with singleton { MongoDao() } + } + val dao1: Dao = kodein.instance("dao1") + val dao2: Dao = kodein.instance("dao2") + + assertThat(dao1).isNotSameAs(dao2) + } + + @Test + fun whenEagerSingletonBinding_thenCreationIsEager() { + var created = false + val kodein = Kodein { + bind() with eagerSingleton { + created = true + MongoDao() + } + } + + assertThat(created).isTrue() + val dao1: Dao = kodein.instance() + val dao2: Dao = kodein.instance() + + assertThat(dao1).isSameAs(dao2) + } + + @Test + fun whenMultitonBinding_thenInstancesAreReused() { + val kodein = Kodein { + bind() with singleton { MongoDao() } + bind() with multiton { tag: String -> Service(instance(), tag) } + } + val service1: Service = kodein.with("myTag").instance() + val service2: Service = kodein.with("myTag").instance() + + assertThat(service1).isSameAs(service2) + } + + @Test + fun whenInstanceBinding_thenItIsReused() { + val dao = MongoDao() + val kodein = Kodein { + bind() with instance(dao) + } + val fromContainer: Dao = kodein.instance() + + assertThat(dao).isSameAs(fromContainer) + } + + @Test + fun whenConstantBinding_thenItIsAvailable() { + val kodein = Kodein { + constant("magic") with 42 + } + val fromContainer: Int = kodein.instance("magic") + + assertThat(fromContainer).isEqualTo(42) + } + + @Test + fun whenUsingModules_thenTransitiveDependenciesAreSuccessfullyResolved() { + val jdbcModule = Kodein.Module { + bind() with singleton { JdbcDao() } + } + val kodein = Kodein { + import(jdbcModule) + bind() with singleton { Controller(instance()) } + bind() with singleton { Service(instance(), "myService") } + } + + val dao: Dao = kodein.instance() + assertThat(dao).isInstanceOf(JdbcDao::class.java) + } + + @Test + fun whenComposition_thenBeansAreReUsed() { + val persistenceContainer = Kodein { + bind() with singleton { MongoDao() } + } + val serviceContainer = Kodein { + extend(persistenceContainer) + bind() with singleton { Service(instance(), "myService") } + } + val fromPersistence: Dao = persistenceContainer.instance() + val fromService: Dao = serviceContainer.instance() + + assertThat(fromPersistence).isSameAs(fromService) + } + + @Test + fun whenOverriding_thenRightBeanIsUsed() { + val commonModule = Kodein.Module { + bind() with singleton { MongoDao() } + bind() with singleton { Service(instance(), "myService") } + } + val testContainer = Kodein { + import(commonModule) + bind(overrides = true) with singleton { InMemoryDao() } + } + val dao: Dao = testContainer.instance() + + assertThat(dao).isInstanceOf(InMemoryDao::class.java) + } + + @Test + fun whenMultiBinding_thenWorks() { + val kodein = Kodein { + bind() from setBinding() + bind().inSet() with singleton { MongoDao() } + bind().inSet() with singleton { JdbcDao() } + } + val daos: Set = kodein.instance() + + assertThat(daos.map { it.javaClass as Class<*> }).containsOnly(MongoDao::class.java, JdbcDao::class.java) + } + + @Test + fun whenInjector_thenWorks() { + class Controller2 { + private val injector = KodeinInjector() + val service: Service by injector.instance() + fun injectDependencies(kodein: Kodein) = injector.inject(kodein) + } + + val kodein = Kodein { + bind() with singleton { MongoDao() } + bind() with singleton { Service(instance(), "myService") } + } + val controller = Controller2() + controller.injectDependencies(kodein) + + assertThat(controller.service).isNotNull + } +} \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/stdlib/RegexTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/stdlib/RegexTest.kt new file mode 100644 index 0000000000..eeb587ee22 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/stdlib/RegexTest.kt @@ -0,0 +1,128 @@ +package com.baeldung.kotlin.stdlib + +import org.junit.Test +import java.beans.ExceptionListener +import java.beans.XMLEncoder +import java.io.* +import java.lang.Exception +import kotlin.test.* +import kotlin.text.RegexOption.* + +class RegexTest { + + @Test + fun whenRegexIsInstantiated_thenIsEqualToToRegexMethod() { + val pattern = """a([bc]+)d?\\""" + + assertEquals(Regex.fromLiteral(pattern).pattern, pattern) + assertEquals(pattern, Regex(pattern).pattern) + assertEquals(pattern, pattern.toRegex().pattern) + } + + @Test + fun whenRegexMatches_thenResultIsTrue() { + val regex = """a([bc]+)d?""".toRegex() + + assertTrue(regex.containsMatchIn("xabcdy")) + assertTrue(regex.matches("abcd")) + assertFalse(regex matches "xabcdy") + } + + @Test + fun givenCompletelyMatchingRegex_whenMatchResult_thenDestructuring() { + val regex = """a([bc]+)d?""".toRegex() + + assertNull(regex.matchEntire("xabcdy")) + + val matchResult = regex.matchEntire("abbccbbd") + + assertNotNull(matchResult) + assertEquals(matchResult!!.value, matchResult.groupValues[0]) + assertEquals(matchResult.destructured.toList(), matchResult.groupValues.drop(1)) + assertEquals("bbccbb", matchResult.destructured.component1()) + assertNull(matchResult.next()) + } + + @Test + fun givenPartiallyMatchingRegex_whenMatchResult_thenGroups() { + val regex = """a([bc]+)d?""".toRegex() + var matchResult = regex.find("abcb abbd") + + assertNotNull(matchResult) + assertEquals(matchResult!!.value, matchResult.groupValues[0]) + assertEquals("abcb", matchResult.value) + assertEquals(IntRange(0, 3), matchResult.range) + assertEquals(listOf("abcb", "bcb"), matchResult.groupValues) + assertEquals(matchResult.destructured.toList(), matchResult.groupValues.drop(1)) + + matchResult = matchResult.next() + + assertNotNull(matchResult) + assertEquals("abbd", matchResult!!.value) + assertEquals("bb", matchResult.groupValues[1]) + + matchResult = matchResult.next() + + assertNull(matchResult) + } + + @Test + fun givenPartiallyMatchingRegex_whenMatchResult_thenDestructuring() { + val regex = """([\w\s]+) is (\d+) years old""".toRegex() + val matchResult = regex.find("Mickey Mouse is 95 years old") + val (name, age) = matchResult!!.destructured + + assertEquals("Mickey Mouse", name) + assertEquals("95", age) + } + + @Test + fun givenNonMatchingRegex_whenFindCalled_thenNull() { + val regex = """a([bc]+)d?""".toRegex() + val matchResult = regex.find("foo") + + assertNull(matchResult) + } + + @Test + fun givenNonMatchingRegex_whenFindAllCalled_thenEmptySet() { + val regex = """a([bc]+)d?""".toRegex() + val matchResults = regex.findAll("foo") + + assertNotNull(matchResults) + assertTrue(matchResults.none()) + } + + @Test + fun whenReplace_thenReplacement() { + val regex = """(red|green|blue)""".toRegex() + val beautiful = "Roses are red, Violets are blue" + val grim = regex.replace(beautiful, "dark") + val shiny = regex.replaceFirst(beautiful, "rainbow") + + assertEquals("Roses are dark, Violets are dark", grim) + assertEquals("Roses are rainbow, Violets are blue", shiny) + } + + @Test + fun whenComplexReplace_thenReplacement() { + val regex = """(red|green|blue)""".toRegex() + val beautiful = "Roses are red, Violets are blue" + val reallyBeautiful = regex.replace(beautiful) { + matchResult -> matchResult.value.toUpperCase() + "!" + } + + assertEquals("Roses are RED!, Violets are BLUE!", reallyBeautiful) + } + + @Test + fun whenSplit_thenList() { + val regex = """\W+""".toRegex() + val beautiful = "Roses are red, Violets are blue" + + assertEquals(listOf("Roses", "are", "red", "Violets", "are", "blue"), regex.split(beautiful)) + assertEquals(listOf("Roses", "are", "red", "Violets are blue"), regex.split(beautiful, 4)) + assertEquals(regex.toPattern().split(beautiful).asList(), regex.split(beautiful)) + } + +} \ No newline at end of file diff --git a/dubbo/README.md b/dubbo/README.md index dec02f5cfc..0a4cd9a204 100644 --- a/dubbo/README.md +++ b/dubbo/README.md @@ -1,4 +1,4 @@ ## Relevant articles: -- [Intro to Dubbo](http://www.baeldung.com/dubbo-intro) +- [Introduction to Dubbo](http://www.baeldung.com/dubbo) diff --git a/ethereumj/pom.xml b/ethereumj/pom.xml index c9f5924d7a..9676106e38 100644 --- a/ethereumj/pom.xml +++ b/ethereumj/pom.xml @@ -1,6 +1,5 @@ - + 4.0.0 com.baeldung.ethereumj ethereumj @@ -8,16 +7,11 @@ 1.0.0 ethereumj - - UTF-8 - 1.8 - 8.5.4 - - - org.springframework.boot - spring-boot-starter-parent - 1.5.6.RELEASE + parent-boot-5 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-5 @@ -38,14 +32,12 @@ org.springframework.boot spring-boot-starter-tomcat - org.springframework.boot spring-boot-starter-test - 1.5.6.RELEASE test @@ -107,4 +99,11 @@ + + + UTF-8 + 1.8 + 8.5.4 + + \ No newline at end of file diff --git a/flyway/README.MD b/flyway/README.MD index 1b3f3c05ee..daeb9012b5 100644 --- a/flyway/README.MD +++ b/flyway/README.MD @@ -1,2 +1,3 @@ ### Relevant Articles: - [Database Migrations with Flyway](http://www.baeldung.com/database-migrations-with-flyway) +- [A Guide to Flyway Callbacks](http://www.baeldung.com/flyway-callbacks) diff --git a/geotools/src/test/java/com/baeldung/geotools/GeoToolsUnitTest.java b/geotools/src/test/java/com/baeldung/geotools/GeoToolsUnitTest.java index 053932b2a7..4a63c44c8b 100644 --- a/geotools/src/test/java/com/baeldung/geotools/GeoToolsUnitTest.java +++ b/geotools/src/test/java/com/baeldung/geotools/GeoToolsUnitTest.java @@ -3,7 +3,6 @@ package com.baeldung.geotools; import static org.junit.Assert.assertNotNull; import org.geotools.feature.DefaultFeatureCollection; -import org.geotools.feature.simple.SimpleFeatureBuilder; import org.junit.Test; import org.opengis.feature.simple.SimpleFeatureType; @@ -19,4 +18,5 @@ public class GeoToolsUnitTest { assertNotNull(collection); } + } diff --git a/google-cloud/README.md b/google-cloud/README.md new file mode 100644 index 0000000000..6022796a0e --- /dev/null +++ b/google-cloud/README.md @@ -0,0 +1,16 @@ +## Google Cloud Tutorial Project + +### Relevant Article: +- [Intro to Google Cloud Storage With Java](http://www.baeldung.com/intro-to-google-cloud-storage-with-java/) + +### Overview +This Maven project contains the Java code for the article linked above. + +### Package Organization +Java classes for the intro tutorial are in the org.baeldung.google.cloud package. Please note that Google Cloud requires +a user account and credentials, as explained in the tutorial. + + +### Running the tests + +``` diff --git a/google-cloud/pom.xml b/google-cloud/pom.xml new file mode 100644 index 0000000000..0f1eff36f8 --- /dev/null +++ b/google-cloud/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + google-cloud + 0.1-SNAPSHOT + jar + google-cloud + Google Cloud Tutorials + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + com.google.cloud + google-cloud-storage + 1.16.0 + + + org.projectlombok + lombok + + ${lombok.version} + provided + + + + + 1.16.18 + 1.8 + UTF-8 + + + diff --git a/google-cloud/src/main/java/com/baeldung/google/cloud/storage/GoogleCloudStorage.java b/google-cloud/src/main/java/com/baeldung/google/cloud/storage/GoogleCloudStorage.java new file mode 100644 index 0000000000..a69171f1db --- /dev/null +++ b/google-cloud/src/main/java/com/baeldung/google/cloud/storage/GoogleCloudStorage.java @@ -0,0 +1,105 @@ +package com.baeldung.google.cloud.storage; + +import com.google.api.gax.paging.Page; +import com.google.auth.Credentials; +import com.google.auth.oauth2.GoogleCredentials; +import com.google.cloud.storage.*; +import lombok.extern.slf4j.Slf4j; + +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.WritableByteChannel; + +import static java.nio.charset.StandardCharsets.UTF_8; + +/** + * Simple class for creating, reading and modifying text blobs on Google Cloud + */ +@Slf4j +public class GoogleCloudStorage { + + private Storage storage; + private Bucket bucket; + + public static void main(String[] args) throws Exception { + + // Use this variation to read the Google authorization JSON from the resources directory with a path + // and a project name. + GoogleCloudStorage googleCloudStorage = + new GoogleCloudStorage("google-cloud/src/main/resources/google_auth.json", "baeldung-cloud-tutorial"); + + // Bucket require globally unique names, so you'll probably need to change this + Bucket bucket = googleCloudStorage.getBucket("baeldung-1-bucket"); + + // Save a simple string + BlobId blobId = googleCloudStorage.saveString("my-first-blob", "Hi there!", bucket); + + // Get it by blob id this time + String value = googleCloudStorage.getString(blobId); + + log.info("Read data: {}", value); + + googleCloudStorage.updateString(blobId, "Bye now!"); + + // Get the string by blob name + value = googleCloudStorage.getString("my-first-blob"); + + log.info("Read modified data: {}", value); + + + } + + + // Use path and project name + private GoogleCloudStorage(String pathToConfig, String projectId) throws IOException { + Credentials credentials = GoogleCredentials.fromStream(new FileInputStream(pathToConfig)); + storage = StorageOptions.newBuilder().setCredentials(credentials).setProjectId(projectId).build().getService(); + } + + // Check for bucket existence and create if needed. + private Bucket getBucket(String bucketName) { + bucket = storage.get(bucketName); + if (bucket == null) { + System.out.println("Creating new bucket."); + bucket = storage.create(BucketInfo.of(bucketName)); + } + return bucket; + } + + // Save a string to a blob + private BlobId saveString(String blobName, String value, Bucket bucket) { + byte[] bytes = value.getBytes(UTF_8); + Blob blob = bucket.create(blobName, bytes); + return blob.getBlobId(); + } + + + // get a blob by id + private String getString(BlobId blobId) { + Blob blob = storage.get(blobId); + return new String(blob.getContent()); + } + + + // get a blob by name + private String getString(String name) { + Page blobs = bucket.list(); + for (Blob blob: blobs.getValues()) { + if (name.equals(blob.getName())) { + return new String(blob.getContent()); + } + } + return "Blob not found"; + } + + // Update a blob + private void updateString(BlobId blobId, String newString) throws IOException { + Blob blob = storage.get(blobId); + if (blob != null) { + WritableByteChannel channel = blob.writer(); + channel.write(ByteBuffer.wrap(newString.getBytes(UTF_8))); + channel.close(); + } + } +} \ No newline at end of file diff --git a/gradle/README.md b/gradle/README.md index dd5ea03a18..5fc7b40e3f 100644 --- a/gradle/README.md +++ b/gradle/README.md @@ -1,2 +1,4 @@ ## Relevant articles: - [Introduction to Gradle](http://www.baeldung.com/gradle) +- [Writing Custom Gradle Plugins](http://www.baeldung.com/gradle-create-plugin) +- [Creating a Fat Jar in Gradle](http://www.baeldung.com/gradle-fat-jar) diff --git a/gradle/build.gradle b/gradle/build.gradle index dcc592a2b4..2e5d984fba 100644 --- a/gradle/build.gradle +++ b/gradle/build.gradle @@ -33,3 +33,60 @@ task execSecondTest { } println 'This will be executed during the configuration phase as well.' } + +task welcome { + doLast { + println 'Welcome on the Baeldung!' + } +} + +task welcomeWithGroup { + group 'Sample category' + doLast { + println 'Welcome on the Baeldung!' + } +} + +task welcomeWithGroupAndDescription { + group 'Sample category' + description 'Tasks which shows welcome message' + doLast { + println 'Welcome on the Baeldung!' + } +} + +class PrintToolVersionTask extends DefaultTask { + String tool + + @TaskAction + void printToolVersion() { + switch (tool) { + case 'java': + println System.getProperty("java.version") + break + case 'groovy': + println GroovySystem.version + break + default: + throw new IllegalArgumentException("Unknown tool") + } + } +} + +task printJavaVersion(type : PrintToolVersionTask) { + tool 'java' +} + +task printGroovyVersion(type : PrintToolVersionTask) { + tool 'groovy' +} + +import com.baeldung.PrintToolVersionBuildSrcTask + +task printJavaVersionBuildSrc(type : PrintToolVersionBuildSrcTask) { + tool 'java' +} + +task printGroovyVersionBuildSrc(type : PrintToolVersionBuildSrcTask) { + tool 'groovy' +} \ No newline at end of file diff --git a/gradle/buildSrc/src/main/groovy/com/baeldung/PrintToolVersionBuildSrcTask.groovy b/gradle/buildSrc/src/main/groovy/com/baeldung/PrintToolVersionBuildSrcTask.groovy new file mode 100644 index 0000000000..0fbd71db56 --- /dev/null +++ b/gradle/buildSrc/src/main/groovy/com/baeldung/PrintToolVersionBuildSrcTask.groovy @@ -0,0 +1,22 @@ +package com.baeldung + +import org.gradle.api.DefaultTask +import org.gradle.api.tasks.TaskAction + +class PrintToolVersionBuildSrcTask extends DefaultTask { + String tool + + @TaskAction + void printToolVersion() { + switch (tool) { + case 'java': + println System.getProperty("java.version") + break + case 'groovy': + println GroovySystem.version + break + default: + throw new IllegalArgumentException("Unknown tool") + } + } +} \ No newline at end of file diff --git a/guava/README.md b/guava/README.md index 7ab70cb01f..924187867e 100644 --- a/guava/README.md +++ b/guava/README.md @@ -19,6 +19,7 @@ - [Guide to Guava’s Ordering](http://www.baeldung.com/guava-ordering) - [Guide to Guava’s PreConditions](http://www.baeldung.com/guava-preconditions) - [Introduction to Guava CacheLoader](http://www.baeldung.com/guava-cacheloader) +- [Introduction to Guava Memoizer](http://www.baeldung.com/guava-memoizer) - [Guide to Guava’s EventBus](http://www.baeldung.com/guava-eventbus) - [Guide to Guava Multimap](http://www.baeldung.com/guava-multimap) - [Guide to Guava RangeSet](http://www.baeldung.com/guava-rangeset) diff --git a/guava/pom.xml b/guava/pom.xml index 4afb4da301..ca9e1e528e 100644 --- a/guava/pom.xml +++ b/guava/pom.xml @@ -43,6 +43,8 @@ ${assertj.version} test + + diff --git a/guava/src/main/java/org/baeldung/guava/memoizer/CostlySupplier.java b/guava/src/main/java/org/baeldung/guava/memoizer/CostlySupplier.java new file mode 100644 index 0000000000..63b3fbd438 --- /dev/null +++ b/guava/src/main/java/org/baeldung/guava/memoizer/CostlySupplier.java @@ -0,0 +1,17 @@ +package org.baeldung.guava.memoizer; + +import java.math.BigInteger; +import java.util.Random; +import java.util.concurrent.TimeUnit; + +public class CostlySupplier { + + public static BigInteger generateBigNumber() { + try { + TimeUnit.SECONDS.sleep(2); + } catch (InterruptedException e) { + } + return new BigInteger("12345"); + } + +} diff --git a/guava/src/main/java/org/baeldung/guava/memoizer/Factorial.java b/guava/src/main/java/org/baeldung/guava/memoizer/Factorial.java new file mode 100644 index 0000000000..74fcbdcc14 --- /dev/null +++ b/guava/src/main/java/org/baeldung/guava/memoizer/Factorial.java @@ -0,0 +1,22 @@ +package org.baeldung.guava.memoizer; + +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; + +import java.math.BigInteger; + +public class Factorial { + + private static LoadingCache memo = CacheBuilder.newBuilder() + .build(CacheLoader.from(Factorial::getFactorial)); + + public static BigInteger getFactorial(int n) { + if (n == 0) { + return BigInteger.ONE; + } else { + return BigInteger.valueOf(n).multiply(memo.getUnchecked(n - 1)); + } + } + +} diff --git a/guava/src/main/java/org/baeldung/guava/memoizer/FibonacciSequence.java b/guava/src/main/java/org/baeldung/guava/memoizer/FibonacciSequence.java new file mode 100644 index 0000000000..0c70f08c23 --- /dev/null +++ b/guava/src/main/java/org/baeldung/guava/memoizer/FibonacciSequence.java @@ -0,0 +1,26 @@ +package org.baeldung.guava.memoizer; + +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; + +import java.math.BigInteger; +import java.util.concurrent.TimeUnit; + +public class FibonacciSequence { + + private static LoadingCache memo = CacheBuilder.newBuilder() + .maximumSize(100) + .build(CacheLoader.from(FibonacciSequence::getFibonacciNumber)); + + public static BigInteger getFibonacciNumber(int n) { + if (n == 0) { + return BigInteger.ZERO; + } else if (n == 1) { + return BigInteger.ONE; + } else { + return memo.getUnchecked(n - 1).add(memo.getUnchecked(n - 2)); + } + } + +} diff --git a/guava/src/test/java/org/baeldung/guava/GuavaMemoizerUnitTest.java b/guava/src/test/java/org/baeldung/guava/GuavaMemoizerUnitTest.java new file mode 100644 index 0000000000..1f934347b4 --- /dev/null +++ b/guava/src/test/java/org/baeldung/guava/GuavaMemoizerUnitTest.java @@ -0,0 +1,92 @@ +package org.baeldung.guava; + +import com.google.common.base.Suppliers; +import org.baeldung.guava.memoizer.CostlySupplier; +import org.baeldung.guava.memoizer.Factorial; +import org.baeldung.guava.memoizer.FibonacciSequence; +import org.junit.Test; + +import java.math.BigInteger; +import java.time.Duration; +import java.time.Instant; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.hamcrest.number.IsCloseTo.closeTo; +import static org.junit.Assert.assertThat; + +public class GuavaMemoizerUnitTest { + + @Test + public void givenInteger_whenGetFibonacciNumber_thenShouldCalculateFibonacciNumber() { + // given + int n = 95; + + // when + BigInteger fibonacciNumber = FibonacciSequence.getFibonacciNumber(n); + + // then + BigInteger expectedFibonacciNumber = new BigInteger("31940434634990099905"); + assertThat(fibonacciNumber, is(equalTo(expectedFibonacciNumber))); + } + + @Test + public void givenInteger_whenGetFactorial_thenShouldCalculateFactorial() { + // given + int n = 95; + + // when + BigInteger factorial = Factorial.getFactorial(n); + + // then + BigInteger expectedFactorial = new BigInteger("10329978488239059262599702099394727095397746340117372869212250571234293987594703124871765375385424468563282236864226607350415360000000000000000000000"); + assertThat(factorial, is(equalTo(expectedFactorial))); + } + + @Test + public void givenMemoizedSupplier_whenGet_thenSubsequentGetsAreFast() { + // given + Supplier memoizedSupplier; + memoizedSupplier = Suppliers.memoize(CostlySupplier::generateBigNumber); + + // when + BigInteger expectedValue = new BigInteger("12345"); + assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 2000D); + + // then + assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 0D); + assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 0D); + } + + @Test + public void givenMemoizedSupplierWithExpiration_whenGet_thenSubsequentGetsBeforeExpiredAreFast() throws InterruptedException { + // given + Supplier memoizedSupplier; + memoizedSupplier = Suppliers.memoizeWithExpiration(CostlySupplier::generateBigNumber, 3, TimeUnit.SECONDS); + + // when + BigInteger expectedValue = new BigInteger("12345"); + assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 2000D); + + // then + assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 0D); + // add one more second until memoized Supplier is evicted from memory + TimeUnit.SECONDS.sleep(1); + assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 2000D); + } + + private void assertSupplierGetExecutionResultAndDuration(Supplier supplier, + T expectedValue, + double expectedDurationInMs) { + Instant start = Instant.now(); + T value = supplier.get(); + Long durationInMs = Duration.between(start, Instant.now()).toMillis(); + double marginOfErrorInMs = 100D; + + assertThat(value, is(equalTo(expectedValue))); + assertThat(durationInMs.doubleValue(), is(closeTo(expectedDurationInMs, marginOfErrorInMs))); + } + +} diff --git a/guest/core-java/pom.xml b/guest/core-java/pom.xml index 222716386b..eda3846c1f 100644 --- a/guest/core-java/pom.xml +++ b/guest/core-java/pom.xml @@ -16,6 +16,24 @@ log4j-core ${log4j2.version} + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-all + ${org.hamcrest.version} + test + @@ -32,5 +50,6 @@ 2.8.2 + 1.3 \ No newline at end of file diff --git a/guest/core-java/src/main/java/com/stackify/stream/Employee.java b/guest/core-java/src/main/java/com/stackify/stream/Employee.java new file mode 100644 index 0000000000..005990c863 --- /dev/null +++ b/guest/core-java/src/main/java/com/stackify/stream/Employee.java @@ -0,0 +1,46 @@ +package com.stackify.stream; + +public class Employee { + private Integer id; + private String name; + private Double salary; + + public Employee(Integer id, String name, Double salary) { + this.id = id; + this.name = name; + this.salary = salary; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Double getSalary() { + return salary; + } + + public void setSalary(Double salary) { + this.salary = salary; + } + + public void salaryIncrement(Double percentage) { + Double newSalary = salary + percentage * salary / 100; + setSalary(newSalary); + } + + public String toString() { + return "Id: " + id + " Name:" + name + " Price:" + salary; + } +} diff --git a/guest/core-java/src/main/java/com/stackify/stream/EmployeeRepository.java b/guest/core-java/src/main/java/com/stackify/stream/EmployeeRepository.java new file mode 100644 index 0000000000..223fee5e26 --- /dev/null +++ b/guest/core-java/src/main/java/com/stackify/stream/EmployeeRepository.java @@ -0,0 +1,21 @@ +package com.stackify.stream; + +import java.util.List; + +public class EmployeeRepository { + private List empList; + + public EmployeeRepository(List empList) { + this.empList = empList; + + } + public Employee findById(Integer id) { + for (Employee emp : empList) { + if (emp.getId() == id) { + return emp; + } + } + + return null; + } +} diff --git a/guest/core-java/src/test/java/com/stackify/stream/EmployeeTest.java b/guest/core-java/src/test/java/com/stackify/stream/EmployeeTest.java new file mode 100644 index 0000000000..cf988d035a --- /dev/null +++ b/guest/core-java/src/test/java/com/stackify/stream/EmployeeTest.java @@ -0,0 +1,455 @@ +package com.stackify.stream; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.beans.HasPropertyWithValue.hasProperty; +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Collection; +import java.util.Comparator; +import java.util.DoubleSummaryStatistics; +import java.util.List; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.Optional; +import java.util.Set; +import java.util.Vector; +import java.util.function.BinaryOperator; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.junit.After; +import org.junit.Test; + +public class EmployeeTest { + private String fileName = "src/test/resources/test.txt"; + + private static Employee[] arrayOfEmps = { + new Employee(1, "Jeff Bezos", 100000.0), + new Employee(2, "Bill Gates", 200000.0), + new Employee(3, "Mark Zuckerberg", 300000.0) + }; + + private static List empList = Arrays.asList(arrayOfEmps); + private static EmployeeRepository employeeRepository = new EmployeeRepository(empList); + + @After + public void cleanup() throws IOException { + Files.deleteIfExists(Paths.get(fileName)); + } + + @Test + public void whenGetStreamFromList_ObtainStream() { + assert(empList.stream() instanceof Stream); + } + + @Test + public void whenGetStreamFromArray_ObtainStream() { + assert(Stream.of(arrayOfEmps) instanceof Stream); + } + + @Test + public void whenGetStreamFromElements_ObtainStream() { + assert(Stream.of(arrayOfEmps[0], arrayOfEmps[1], arrayOfEmps[2]) instanceof Stream); + } + + @Test + public void whenBuildStreamFromElements_ObtainStream() { + Stream.Builder empStreamBuilder = Stream.builder(); + + empStreamBuilder.accept(arrayOfEmps[0]); + empStreamBuilder.accept(arrayOfEmps[1]); + empStreamBuilder.accept(arrayOfEmps[2]); + + Stream empStream = empStreamBuilder.build(); + + assert(empStream instanceof Stream); + } + + @Test + public void whenIncrementSalaryForEachEmployee_thenApplyNewSalary() { + Employee[] arrayOfEmps = { + new Employee(1, "Jeff Bezos", 100000.0), + new Employee(2, "Bill Gates", 200000.0), + new Employee(3, "Mark Zuckerberg", 300000.0) + }; + + List empList = Arrays.asList(arrayOfEmps); + + empList.stream().forEach(e -> e.salaryIncrement(10.0)); + + assertThat(empList, contains( + hasProperty("salary", equalTo(110000.0)), + hasProperty("salary", equalTo(220000.0)), + hasProperty("salary", equalTo(330000.0)) + )); + } + + @Test + public void whenIncrementSalaryUsingPeek_thenApplyNewSalary() { + Employee[] arrayOfEmps = { + new Employee(1, "Jeff Bezos", 100000.0), + new Employee(2, "Bill Gates", 200000.0), + new Employee(3, "Mark Zuckerberg", 300000.0) + }; + + List empList = Arrays.asList(arrayOfEmps); + + empList.stream() + .peek(e -> e.salaryIncrement(10.0)) + .peek(System.out::println) + .collect(Collectors.toList()); + + assertThat(empList, contains( + hasProperty("salary", equalTo(110000.0)), + hasProperty("salary", equalTo(220000.0)), + hasProperty("salary", equalTo(330000.0)) + )); + } + + @Test + public void whenMapIdToEmployees_thenGetEmployeeStream() { + Integer[] empIds = { 1, 2, 3 }; + + List employees = Stream.of(empIds) + .map(employeeRepository::findById) + .collect(Collectors.toList()); + + assertEquals(employees.size(), empIds.length); + } + + @Test + public void whenFlatMapEmployeeNames_thenGetNameStream() { + List> namesNested = Arrays.asList( + Arrays.asList("Jeff", "Bezos"), + Arrays.asList("Bill", "Gates"), + Arrays.asList("Mark", "Zuckerberg")); + + List namesFlatStream = namesNested.stream() + .flatMap(Collection::stream) + .collect(Collectors.toList()); + + assertEquals(namesFlatStream.size(), namesNested.size() * 2); + } + + @Test + public void whenFilterEmployees_thenGetFilteredStream() { + Integer[] empIds = { 1, 2, 3, 4 }; + + List employees = Stream.of(empIds) + .map(employeeRepository::findById) + .filter(e -> e != null) + .filter(e -> e.getSalary() > 200000) + .collect(Collectors.toList()); + + assertEquals(Arrays.asList(arrayOfEmps[2]), employees); + } + + @Test + public void whenFindFirst_thenGetFirstEmployeeInStream() { + Integer[] empIds = { 1, 2, 3, 4 }; + + Employee employee = Stream.of(empIds) + .map(employeeRepository::findById) + .filter(e -> e != null) + .filter(e -> e.getSalary() > 100000) + .findFirst() + .orElse(null); + + assertEquals(employee.getSalary(), new Double(200000)); + } + + @Test + public void whenCollectStreamToList_thenGetList() { + List employees = empList.stream().collect(Collectors.toList()); + + assertEquals(empList, employees); + } + + @Test + public void whenStreamToArray_thenGetArray() { + Employee[] employees = empList.stream().toArray(Employee[]::new); + + assertThat(empList.toArray(), equalTo(employees)); + } + + @Test + public void whenStreamCount_thenGetElementCount() { + Long empCount = empList.stream() + .filter(e -> e.getSalary() > 200000) + .count(); + + assertEquals(empCount, new Long(1)); + } + + @Test + public void whenLimitInfiniteStream_thenGetFiniteElements() { + Stream infiniteStream = Stream.iterate(2, i -> i * 2); + + List collect = infiniteStream + .skip(3) + .limit(5) + .collect(Collectors.toList()); + + assertEquals(collect, Arrays.asList(16, 32, 64, 128, 256)); + } + + @Test + public void whenSortStream_thenGetSortedStream() { + List employees = empList.stream() + .sorted((e1, e2) -> e1.getName().compareTo(e2.getName())) + .collect(Collectors.toList()); + + assertEquals(employees.get(0).getName(), "Bill Gates"); + assertEquals(employees.get(1).getName(), "Jeff Bezos"); + assertEquals(employees.get(2).getName(), "Mark Zuckerberg"); + } + + + @Test + public void whenFindMin_thenGetMinElementFromStream() { + Employee firstEmp = empList.stream() + .min((e1, e2) -> e1.getId() - e2.getId()) + .orElseThrow(NoSuchElementException::new); + + assertEquals(firstEmp.getId(), new Integer(1)); + } + + @Test + public void whenFindMax_thenGetMaxElementFromStream() { + Employee maxSalEmp = empList.stream() + .max(Comparator.comparing(Employee::getSalary)) + .orElseThrow(NoSuchElementException::new); + + assertEquals(maxSalEmp.getSalary(), new Double(300000.0)); + } + + @Test + public void whenApplyDistinct_thenRemoveDuplicatesFromStream() { + List intList = Arrays.asList(2, 5, 3, 2, 4, 3); + List distinctIntList = intList.stream().distinct().collect(Collectors.toList()); + + assertEquals(distinctIntList, Arrays.asList(2, 5, 3, 4)); + } + + @Test + public void whenApplyMatch_thenReturnBoolean() { + List intList = Arrays.asList(2, 4, 5, 6, 8); + + boolean allEven = intList.stream().allMatch(i -> i % 2 == 0); + boolean oneEven = intList.stream().anyMatch(i -> i % 2 == 0); + boolean noneMultipleOfThree = intList.stream().noneMatch(i -> i % 3 == 0); + + assertEquals(allEven, false); + assertEquals(oneEven, true); + assertEquals(noneMultipleOfThree, false); + } + + @Test + public void whenFindMaxOnIntStream_thenGetMaxInteger() { + Integer latestEmpId = empList.stream() + .mapToInt(Employee::getId) + .max() + .orElseThrow(NoSuchElementException::new); + + assertEquals(latestEmpId, new Integer(3)); + } + + @Test + public void whenApplySumOnIntStream_thenGetSum() { + Double avgSal = empList.stream() + .mapToDouble(Employee::getSalary) + .average() + .orElseThrow(NoSuchElementException::new); + + assertEquals(avgSal, new Double(200000)); + } + + @Test + public void whenApplyReduceOnStream_thenGetValue() { + Double sumSal = empList.stream() + .map(Employee::getSalary) + .reduce(0.0, Double::sum); + + assertEquals(sumSal, new Double(600000)); + } + + @Test + public void whenCollectByJoining_thenGetJoinedString() { + String empNames = empList.stream() + .map(Employee::getName) + .collect(Collectors.joining(", ")) + .toString(); + + assertEquals(empNames, "Jeff Bezos, Bill Gates, Mark Zuckerberg"); + } + + @Test + public void whenCollectBySet_thenGetSet() { + Set empNames = empList.stream() + .map(Employee::getName) + .collect(Collectors.toSet()); + + assertEquals(empNames.size(), 3); + } + + @Test + public void whenToVectorCollection_thenGetVector() { + Vector empNames = empList.stream() + .map(Employee::getName) + .collect(Collectors.toCollection(Vector::new)); + + assertEquals(empNames.size(), 3); + } + + @Test + public void whenApplySummarizing_thenGetBasicStats() { + DoubleSummaryStatistics stats = empList.stream() + .collect(Collectors.summarizingDouble(Employee::getSalary)); + + assertEquals(stats.getCount(), 3); + assertEquals(stats.getSum(), 600000.0, 0); + assertEquals(stats.getMin(), 100000.0, 0); + assertEquals(stats.getMax(), 300000.0, 0); + assertEquals(stats.getAverage(), 200000.0, 0); + } + + @Test + public void whenApplySummaryStatistics_thenGetBasicStats() { + DoubleSummaryStatistics stats = empList.stream() + .mapToDouble(Employee::getSalary) + .summaryStatistics(); + + assertEquals(stats.getCount(), 3); + assertEquals(stats.getSum(), 600000.0, 0); + assertEquals(stats.getMin(), 100000.0, 0); + assertEquals(stats.getMax(), 300000.0, 0); + assertEquals(stats.getAverage(), 200000.0, 0); + } + + @Test + public void whenStreamPartition_thenGetMap() { + List intList = Arrays.asList(2, 4, 5, 6, 8); + Map> isEven = intList.stream().collect( + Collectors.partitioningBy(i -> i % 2 == 0)); + + assertEquals(isEven.get(true).size(), 4); + assertEquals(isEven.get(false).size(), 1); + } + + @Test + public void whenStreamGroupingBy_thenGetMap() { + Map> groupByAlphabet = empList.stream().collect( + Collectors.groupingBy(e -> new Character(e.getName().charAt(0)))); + + assertEquals(groupByAlphabet.get('B').get(0).getName(), "Bill Gates"); + assertEquals(groupByAlphabet.get('J').get(0).getName(), "Jeff Bezos"); + assertEquals(groupByAlphabet.get('M').get(0).getName(), "Mark Zuckerberg"); + } + + @Test + public void whenStreamMapping_thenGetMap() { + Map> idGroupedByAlphabet = empList.stream().collect( + Collectors.groupingBy(e -> new Character(e.getName().charAt(0)), + Collectors.mapping(Employee::getId, Collectors.toList()))); + + assertEquals(idGroupedByAlphabet.get('B').get(0), new Integer(2)); + assertEquals(idGroupedByAlphabet.get('J').get(0), new Integer(1)); + assertEquals(idGroupedByAlphabet.get('M').get(0), new Integer(3)); + } + + @Test + public void whenStreamReducing_thenGetValue() { + Double percentage = 10.0; + Double salIncrOverhead = empList.stream().collect(Collectors.reducing( + 0.0, e -> e.getSalary() * percentage / 100, (s1, s2) -> s1 + s2)); + + assertEquals(salIncrOverhead, 60000.0, 0); + } + + @Test + public void whenStreamGroupingAndReducing_thenGetMap() { + Comparator byNameLength = Comparator.comparing(Employee::getName); + + Map> longestNameByAlphabet = empList.stream().collect( + Collectors.groupingBy(e -> new Character(e.getName().charAt(0)), + Collectors.reducing(BinaryOperator.maxBy(byNameLength)))); + + assertEquals(longestNameByAlphabet.get('B').get().getName(), "Bill Gates"); + assertEquals(longestNameByAlphabet.get('J').get().getName(), "Jeff Bezos"); + assertEquals(longestNameByAlphabet.get('M').get().getName(), "Mark Zuckerberg"); + } + + @Test + public void whenParallelStream_thenPerformOperationsInParallel() { + Employee[] arrayOfEmps = { + new Employee(1, "Jeff Bezos", 100000.0), + new Employee(2, "Bill Gates", 200000.0), + new Employee(3, "Mark Zuckerberg", 300000.0) + }; + + List empList = Arrays.asList(arrayOfEmps); + + empList.stream().parallel().forEach(e -> e.salaryIncrement(10.0)); + + assertThat(empList, contains( + hasProperty("salary", equalTo(110000.0)), + hasProperty("salary", equalTo(220000.0)), + hasProperty("salary", equalTo(330000.0)) + )); + } + + @Test + public void whenGenerateStream_thenGetInfiniteStream() { + Stream.generate(Math::random) + .limit(5) + .forEach(System.out::println); + } + + @Test + public void whenIterateStream_thenGetInfiniteStream() { + Stream evenNumStream = Stream.iterate(2, i -> i * 2); + + List collect = evenNumStream + .limit(5) + .collect(Collectors.toList()); + + assertEquals(collect, Arrays.asList(2, 4, 8, 16, 32)); + } + + @Test + public void whenStreamToFile_thenGetFile() throws IOException { + String[] words = { + "hello", + "refer", + "world", + "level" + }; + + try (PrintWriter pw = new PrintWriter( + Files.newBufferedWriter(Paths.get(fileName)))) { + Stream.of(words).forEach(pw::println); + } + } + + private List getPalindrome(Stream stream, int length) { + return stream.filter(s -> s.length() == length) + .filter(s -> s.compareToIgnoreCase( + new StringBuilder(s).reverse().toString()) == 0) + .collect(Collectors.toList()); + } + + @Test + public void whenFileToStream_thenGetStream() throws IOException { + whenStreamToFile_thenGetFile(); + + List str = getPalindrome(Files.lines(Paths.get(fileName)), 5); + assertThat(str, contains("refer", "level")); + } +} diff --git a/guest/core-java/src/test/resources/.keep b/guest/core-java/src/test/resources/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/guest/deep-jsf/README.md b/guest/deep-jsf/README.md new file mode 100644 index 0000000000..b5f532535b --- /dev/null +++ b/guest/deep-jsf/README.md @@ -0,0 +1,15 @@ +## Building + +To build the module, use Maven's `package` goal: + +``` +mvn clean package +``` + +The `war` file will be available at `target/deep-jsf.war` + +## Running + +The `war` application is deployed to a Java EE 7 compliant application server, for example, to GlassFish 4 or later. + +The example then will be accessible at http://localhost:8080/deep-jsf/index.xhtml \ No newline at end of file diff --git a/guest/deep-jsf/pom.xml b/guest/deep-jsf/pom.xml new file mode 100644 index 0000000000..68801ba010 --- /dev/null +++ b/guest/deep-jsf/pom.xml @@ -0,0 +1,40 @@ + + 4.0.0 + + com.stackify + deep-jsf + 0.0.1-SNAPSHOT + war + + + false + + + + + + javax + javaee-api + 7.0 + provided + + + + + + deep-jsf + + + org.apache.maven.plugins + maven-compiler-plugin + 3.7.0 + + 1.8 + 1.8 + + + + + + \ No newline at end of file diff --git a/guest/deep-jsf/src/main/java/com/stackify/deepjsf/GreetControllerBean.java b/guest/deep-jsf/src/main/java/com/stackify/deepjsf/GreetControllerBean.java new file mode 100644 index 0000000000..7f5cf99781 --- /dev/null +++ b/guest/deep-jsf/src/main/java/com/stackify/deepjsf/GreetControllerBean.java @@ -0,0 +1,14 @@ +package com.stackify.deepjsf; + +import javax.faces.bean.ManagedBean; +import javax.faces.bean.RequestScoped; + +@ManagedBean +@RequestScoped +public class GreetControllerBean { + + public String greet() { + return "greet"; + } + +} diff --git a/guest/deep-jsf/src/main/java/com/stackify/deepjsf/PhaseListenerBean.java b/guest/deep-jsf/src/main/java/com/stackify/deepjsf/PhaseListenerBean.java new file mode 100644 index 0000000000..d4f6a6e815 --- /dev/null +++ b/guest/deep-jsf/src/main/java/com/stackify/deepjsf/PhaseListenerBean.java @@ -0,0 +1,47 @@ +package com.stackify.deepjsf; + +import javax.faces.bean.ManagedBean; +import javax.faces.bean.RequestScoped; +import javax.faces.component.UIComponent; +import javax.faces.component.UIViewRoot; +import javax.faces.component.visit.VisitContext; +import javax.faces.component.visit.VisitResult; +import javax.faces.event.PhaseEvent; +import javax.faces.event.PhaseId; +import javax.servlet.http.HttpServletRequest; + +@ManagedBean +@RequestScoped +public class PhaseListenerBean { + + public void beforeListener(PhaseEvent event) { + if (!event.getPhaseId().equals(PhaseId.RENDER_RESPONSE)) { + return; + } + UIViewRoot root = event.getFacesContext().getViewRoot(); + + boolean showNewFeature = showNewFeatureForIp(event); + + processComponentTree(root, event, showNewFeature); + } + + private boolean showNewFeatureForIp(PhaseEvent event) { + HttpServletRequest request = (HttpServletRequest) event.getFacesContext() + .getExternalContext().getRequest(); + String ip = request.getRemoteAddr(); + return !ip.startsWith("127.0"); + } + + private void processComponentTree(UIComponent component, PhaseEvent event, boolean show) { + component.visitTree(VisitContext.createVisitContext(event.getFacesContext()), + (context, target) -> { + if (target.getId() != null + && target.getId().startsWith("new-feature-") + && !show) { + target.setRendered(false); + } + return VisitResult.ACCEPT; + }); + } + +} diff --git a/guest/deep-jsf/src/main/java/com/stackify/deepjsf/UserBean.java b/guest/deep-jsf/src/main/java/com/stackify/deepjsf/UserBean.java new file mode 100644 index 0000000000..f6c94e87b8 --- /dev/null +++ b/guest/deep-jsf/src/main/java/com/stackify/deepjsf/UserBean.java @@ -0,0 +1,48 @@ +package com.stackify.deepjsf; + +import javax.faces.bean.ManagedBean; +import javax.faces.bean.SessionScoped; +import javax.faces.event.ValueChangeEvent; + +@ManagedBean +@SessionScoped +public class UserBean { + + private String name = ""; + + private String lastName = ""; + + private String proposedLogin = ""; + + public void nameChanged(ValueChangeEvent event) { + this.proposedLogin = event.getNewValue() + "-" + lastName; + } + + public void lastNameChanged(ValueChangeEvent event) { + this.proposedLogin = name + "-" + event.getNewValue(); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getProposedLogin() { + return proposedLogin; + } + + public void setProposedLogin(String proposedLogin) { + this.proposedLogin = proposedLogin; + } +} diff --git a/guest/deep-jsf/src/main/java/com/stackify/deepjsf/UserControllerBean.java b/guest/deep-jsf/src/main/java/com/stackify/deepjsf/UserControllerBean.java new file mode 100644 index 0000000000..c2a46a019a --- /dev/null +++ b/guest/deep-jsf/src/main/java/com/stackify/deepjsf/UserControllerBean.java @@ -0,0 +1,14 @@ +package com.stackify.deepjsf; + +import javax.faces.bean.ManagedBean; +import javax.faces.bean.RequestScoped; + +@ManagedBean +@RequestScoped +public class UserControllerBean { + + public String register() { + return "register-success"; + } + +} diff --git a/guest/deep-jsf/src/main/webapp/WEB-INF/faces-config.xml b/guest/deep-jsf/src/main/webapp/WEB-INF/faces-config.xml new file mode 100644 index 0000000000..264e60065c --- /dev/null +++ b/guest/deep-jsf/src/main/webapp/WEB-INF/faces-config.xml @@ -0,0 +1,15 @@ + + + + + /register.xhtml + + register-success + /hello.xhtml + + + + \ No newline at end of file diff --git a/guest/deep-jsf/src/main/webapp/greet.xhtml b/guest/deep-jsf/src/main/webapp/greet.xhtml new file mode 100644 index 0000000000..50c79c64e1 --- /dev/null +++ b/guest/deep-jsf/src/main/webapp/greet.xhtml @@ -0,0 +1,11 @@ + + + + + + + + diff --git a/guest/deep-jsf/src/main/webapp/hello.xhtml b/guest/deep-jsf/src/main/webapp/hello.xhtml new file mode 100644 index 0000000000..f9c3745dca --- /dev/null +++ b/guest/deep-jsf/src/main/webapp/hello.xhtml @@ -0,0 +1,10 @@ + + + + + + + diff --git a/guest/deep-jsf/src/main/webapp/index.xhtml b/guest/deep-jsf/src/main/webapp/index.xhtml new file mode 100644 index 0000000000..de99b89815 --- /dev/null +++ b/guest/deep-jsf/src/main/webapp/index.xhtml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + diff --git a/guest/deep-jsf/src/main/webapp/register.xhtml b/guest/deep-jsf/src/main/webapp/register.xhtml new file mode 100644 index 0000000000..ba1b8e0233 --- /dev/null +++ b/guest/deep-jsf/src/main/webapp/register.xhtml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/hibernate5/README.md b/hibernate5/README.md index 1eb090f05d..5143b097ce 100644 --- a/hibernate5/README.md +++ b/hibernate5/README.md @@ -5,3 +5,5 @@ - [Hibernate – Mapping Date and Time](http://www.baeldung.com/hibernate-date-time) - [Hibernate Inheritance Mapping](http://www.baeldung.com/hibernate-inheritance) - [A Guide to Multitenancy in Hibernate 5](http://www.baeldung.com/hibernate-5-multitenancy) +- [Introduction to Hibernate Spatial](http://www.baeldung.com/hibernate-spatial) +- [Hibernate Interceptors](http://www.baeldung.com/hibernate-interceptor) diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java b/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java index 25fc0d7b02..130edec8cc 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java @@ -81,6 +81,7 @@ public class HibernateUtil { metadataSources.addAnnotatedClass(Bag.class); metadataSources.addAnnotatedClass(PointEntity.class); metadataSources.addAnnotatedClass(PolygonEntity.class); + metadataSources.addAnnotatedClass(com.baeldung.hibernate.pojo.Person.class); Metadata metadata = metadataSources.buildMetadata(); return metadata.getSessionFactoryBuilder() diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/converters/PersonNameConverter.java b/hibernate5/src/main/java/com/baeldung/hibernate/converters/PersonNameConverter.java new file mode 100644 index 0000000000..506e674984 --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/converters/PersonNameConverter.java @@ -0,0 +1,61 @@ +package com.baeldung.hibernate.converters; + +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + +import com.baeldung.hibernate.pojo.PersonName; + +@Converter +public class PersonNameConverter implements AttributeConverter { + + private static final String SEPARATOR = ", "; + + @Override + public String convertToDatabaseColumn(PersonName personName) { + if (personName == null) { + return null; + } + + StringBuilder sb = new StringBuilder(); + if (personName.getSurname() != null && !personName.getSurname() + .isEmpty()) { + sb.append(personName.getSurname()); + sb.append(SEPARATOR); + } + + if (personName.getName() != null && !personName.getName() + .isEmpty()) { + sb.append(personName.getName()); + } + + return sb.toString(); + } + + @Override + public PersonName convertToEntityAttribute(String dbPersonName) { + if (dbPersonName == null || dbPersonName.isEmpty()) { + return null; + } + + String[] pieces = dbPersonName.split(SEPARATOR); + + if (pieces == null || pieces.length == 0) { + return null; + } + + PersonName personName = new PersonName(); + String firstPiece = !pieces[0].isEmpty() ? pieces[0] : null; + if (dbPersonName.contains(SEPARATOR)) { + personName.setSurname(firstPiece); + + if (pieces.length >= 2 && pieces[1] != null && !pieces[1].isEmpty()) { + personName.setName(pieces[1]); + } + } else { + personName.setName(firstPiece); + } + + return personName; + } + +} diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/interceptors/CustomInterceptorImpl.java b/hibernate5/src/main/java/com/baeldung/hibernate/interceptors/CustomInterceptorImpl.java index a84a981f7f..6736b39b64 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/interceptors/CustomInterceptorImpl.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/interceptors/CustomInterceptorImpl.java @@ -9,7 +9,7 @@ import org.hibernate.Interceptor; import org.hibernate.Transaction; import org.hibernate.type.Type; -public class CustomInterceptorImpl implements Interceptor { +public class CustomInterceptorImpl implements Interceptor, Serializable { @Override public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException { diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/pojo/Person.java b/hibernate5/src/main/java/com/baeldung/hibernate/pojo/Person.java new file mode 100644 index 0000000000..390a5954ed --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/pojo/Person.java @@ -0,0 +1,32 @@ +package com.baeldung.hibernate.pojo; + +import javax.persistence.Convert; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +import com.baeldung.hibernate.converters.PersonNameConverter; + +@Entity(name = "PersonTable") +public class Person { + + @Id + @GeneratedValue + private Long id; + + @Convert(converter = PersonNameConverter.class) + private PersonName personName; + + public PersonName getPersonName() { + return personName; + } + + public void setPersonName(PersonName personName) { + this.personName = personName; + } + + public Long getId() { + return id; + } + +} diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/pojo/PersonName.java b/hibernate5/src/main/java/com/baeldung/hibernate/pojo/PersonName.java new file mode 100644 index 0000000000..335fe73f75 --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/pojo/PersonName.java @@ -0,0 +1,29 @@ +package com.baeldung.hibernate.pojo; + +import java.io.Serializable; + +public class PersonName implements Serializable { + + private static final long serialVersionUID = 7883094644631050150L; + + private String name; + + private String surname; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSurname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } + +} diff --git a/hibernate5/src/test/java/com/baeldung/hibernate/converter/PersonNameConverterTest.java b/hibernate5/src/test/java/com/baeldung/hibernate/converter/PersonNameConverterTest.java new file mode 100644 index 0000000000..204d8775ff --- /dev/null +++ b/hibernate5/src/test/java/com/baeldung/hibernate/converter/PersonNameConverterTest.java @@ -0,0 +1,200 @@ +package com.baeldung.hibernate.converter; + +import java.io.IOException; + +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.hibernate.HibernateUtil; +import com.baeldung.hibernate.pojo.Person; +import com.baeldung.hibernate.pojo.PersonName; + +import static org.junit.Assert.assertEquals; + +public class PersonNameConverterTest { + + private Session session; + private Transaction transaction; + + @Before + public void setUp() throws IOException { + session = HibernateUtil.getSessionFactory() + .openSession(); + transaction = session.beginTransaction(); + + session.createNativeQuery("delete from personTable") + .executeUpdate(); + + transaction.commit(); + transaction = session.beginTransaction(); + } + + @After + public void tearDown() { + transaction.rollback(); + session.close(); + } + + @Test + public void givenPersonName_WhenSaving_ThenNameAndSurnameConcat() { + final String name = "name"; + final String surname = "surname"; + + PersonName personName = new PersonName(); + personName.setName(name); + personName.setSurname(surname); + + Person person = new Person(); + person.setPersonName(personName); + + Long id = (Long) session.save(person); + + session.flush(); + session.clear(); + + String dbPersonName = (String) session.createNativeQuery("select p.personName from PersonTable p where p.id = :id") + .setParameter("id", id) + .getSingleResult(); + + assertEquals(surname + ", " + name, dbPersonName); + + Person dbPerson = session.createNativeQuery("select * from PersonTable p where p.id = :id", Person.class) + .setParameter("id", id) + .getSingleResult(); + + assertEquals(dbPerson.getPersonName() + .getName(), name); + assertEquals(dbPerson.getPersonName() + .getSurname(), surname); + } + + @Test + public void givenPersonNameNull_WhenSaving_ThenNullStored() { + final String name = null; + final String surname = null; + + PersonName personName = new PersonName(); + personName.setName(name); + personName.setSurname(surname); + + Person person = new Person(); + person.setPersonName(personName); + + Long id = (Long) session.save(person); + + session.flush(); + session.clear(); + + String dbPersonName = (String) session.createNativeQuery("select p.personName from PersonTable p where p.id = :id") + .setParameter("id", id) + .getSingleResult(); + + assertEquals("", dbPersonName); + + Person dbPerson = session.createNativeQuery("select * from PersonTable p where p.id = :id", Person.class) + .setParameter("id", id) + .getSingleResult(); + + assertEquals(dbPerson.getPersonName(), null); + } + + @Test + public void givenPersonNameWithoutName_WhenSaving_ThenNotNameStored() { + final String name = null; + final String surname = "surname"; + + PersonName personName = new PersonName(); + personName.setName(name); + personName.setSurname(surname); + + Person person = new Person(); + person.setPersonName(personName); + + Long id = (Long) session.save(person); + + session.flush(); + session.clear(); + + String dbPersonName = (String) session.createNativeQuery("select p.personName from PersonTable p where p.id = :id") + .setParameter("id", id) + .getSingleResult(); + + assertEquals("surname, ", dbPersonName); + + Person dbPerson = session.createNativeQuery("select * from PersonTable p where p.id = :id", Person.class) + .setParameter("id", id) + .getSingleResult(); + + assertEquals(dbPerson.getPersonName() + .getName(), name); + assertEquals(dbPerson.getPersonName() + .getSurname(), surname); + } + + @Test + public void givenPersonNameWithoutSurName_WhenSaving_ThenNotSurNameStored() { + final String name = "name"; + final String surname = null; + + PersonName personName = new PersonName(); + personName.setName(name); + personName.setSurname(surname); + + Person person = new Person(); + person.setPersonName(personName); + + Long id = (Long) session.save(person); + + session.flush(); + session.clear(); + + String dbPersonName = (String) session.createNativeQuery("select p.personName from PersonTable p where p.id = :id") + .setParameter("id", id) + .getSingleResult(); + + assertEquals("name", dbPersonName); + + Person dbPerson = session.createNativeQuery("select * from PersonTable p where p.id = :id", Person.class) + .setParameter("id", id) + .getSingleResult(); + + assertEquals(dbPerson.getPersonName() + .getName(), name); + assertEquals(dbPerson.getPersonName() + .getSurname(), surname); + } + + @Test + public void givenPersonNameEmptyFields_WhenSaving_ThenFielsNotStored() { + final String name = ""; + final String surname = ""; + + PersonName personName = new PersonName(); + personName.setName(name); + personName.setSurname(surname); + + Person person = new Person(); + person.setPersonName(personName); + + Long id = (Long) session.save(person); + + session.flush(); + session.clear(); + + String dbPersonName = (String) session.createNativeQuery("select p.personName from PersonTable p where p.id = :id") + .setParameter("id", id) + .getSingleResult(); + + assertEquals("", dbPersonName); + + Person dbPerson = session.createNativeQuery("select * from PersonTable p where p.id = :id", Person.class) + .setParameter("id", id) + .getSingleResult(); + + assertEquals(dbPerson.getPersonName(), null); + } + +} diff --git a/influxdb/src/test/java/com/baeldung/influxdb/InfluxDBConnectionLiveTest.java b/influxdb/src/test/java/com/baeldung/influxdb/InfluxDBConnectionLiveTest.java index 50d35b9b1c..858903a676 100644 --- a/influxdb/src/test/java/com/baeldung/influxdb/InfluxDBConnectionLiveTest.java +++ b/influxdb/src/test/java/com/baeldung/influxdb/InfluxDBConnectionLiveTest.java @@ -8,7 +8,6 @@ import org.influxdb.dto.*; import org.influxdb.impl.InfluxDBResultMapper; import org.junit.Test; -import java.io.IOException; import java.util.List; import java.util.concurrent.TimeUnit; @@ -55,7 +54,7 @@ public class InfluxDBConnectionLiveTest { InfluxDB connection = connectDatabase(); - // Create "baeldung and check for it + // Create "baeldung" and check for it connection.createDatabase("baeldung"); assertTrue(connection.databaseExists("baeldung")); @@ -103,7 +102,7 @@ public class InfluxDBConnectionLiveTest { // another brief pause. Thread.sleep(10); - List memoryPointList = getPoints(connection, "Select * from memory", "baeldung"); + List memoryPointList = getPoints(connection, "Select * from memory", "baeldung"); assertEquals(10, memoryPointList.size()); diff --git a/jackson/pom.xml b/jackson/pom.xml index 001fde5021..2587e61ac1 100644 --- a/jackson/pom.xml +++ b/jackson/pom.xml @@ -129,7 +129,7 @@ - 2.9.2 + 2.9.4 19.0 diff --git a/java-lite/README.md b/java-lite/README.md index bcb84e186e..13dcd5f8c3 100644 --- a/java-lite/README.md +++ b/java-lite/README.md @@ -1,2 +1,3 @@ ### Relevant Articles: -- [RESTFul CRUD application with JavaLite] () \ No newline at end of file + +- [A Guide to JavaLite – Building a RESTful CRUD application](http://www.baeldung.com/javalite-rest) diff --git a/java-lite/pom.xml b/java-lite/pom.xml index eb18bc40a5..d7950487ca 100644 --- a/java-lite/pom.xml +++ b/java-lite/pom.xml @@ -1,7 +1,6 @@ - + 4.0.0 org.baeldung @@ -22,10 +21,15 @@ 5.1.45 1.7.0 1.8.2 - 4.11 + 4.12 + + + src/main/webapp/WEB-INF + + org.eclipse.jetty @@ -85,7 +89,14 @@ system ${java.home}/../lib/tools.jar - + + + org.javalite + activeweb-testing + 1.15 + test + + junit junit diff --git a/java-lite/src/main/java/app/config/AppBootstrap.java b/java-lite/src/main/java/app/config/AppBootstrap.java index 7a87c2d0a7..02c8360986 100644 --- a/java-lite/src/main/java/app/config/AppBootstrap.java +++ b/java-lite/src/main/java/app/config/AppBootstrap.java @@ -3,7 +3,15 @@ package app.config; import org.javalite.activeweb.AppContext; import org.javalite.activeweb.Bootstrap; +import com.google.inject.Guice; +import com.google.inject.Injector; + +import app.services.ArticleServiceModule; + public class AppBootstrap extends Bootstrap { public void init(AppContext context) { } + public Injector getInjector() { + return Guice.createInjector(new ArticleServiceModule()); + } } diff --git a/java-lite/src/main/java/app/controllers/ArticleController.java b/java-lite/src/main/java/app/controllers/ArticleController.java new file mode 100755 index 0000000000..2b8dc452bd --- /dev/null +++ b/java-lite/src/main/java/app/controllers/ArticleController.java @@ -0,0 +1,28 @@ +package app.controllers; + +import javax.inject.Inject; + +import org.javalite.activeweb.AppController; + +import app.services.ArticleService; + +public class ArticleController extends AppController { + + @Inject + private ArticleService articleService; + + public void index() { + view("articles", articleService.getArticles()); + } + + public void search() { + + String keyword = param("key"); + if (null != keyword) { + assign("article", articleService.search(keyword)); + } else { + render("/common/error"); + } + + } +} diff --git a/java-lite/src/main/java/app/controllers/HomeController.java b/java-lite/src/main/java/app/controllers/HomeController.java new file mode 100755 index 0000000000..0e284af0ad --- /dev/null +++ b/java-lite/src/main/java/app/controllers/HomeController.java @@ -0,0 +1,11 @@ +package app.controllers; + +import org.javalite.activeweb.AppController; + +public class HomeController extends AppController { + + public void index() { + render("index"); + } + +} diff --git a/java-lite/src/main/java/app/models/Article.java b/java-lite/src/main/java/app/models/Article.java new file mode 100755 index 0000000000..db318f4468 --- /dev/null +++ b/java-lite/src/main/java/app/models/Article.java @@ -0,0 +1,50 @@ +package app.models; + +public class Article { + + private String title; + private String author; + private String words; + private String date; + + public Article(String title, String author, String words, String date) { + super(); + this.title = title; + this.author = author; + this.words = words; + this.date = date; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public String getWords() { + return words; + } + + public void setWords(String words) { + this.words = words; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + +} \ No newline at end of file diff --git a/java-lite/src/main/java/app/services/ArticleService.java b/java-lite/src/main/java/app/services/ArticleService.java new file mode 100755 index 0000000000..ddd0b3ed10 --- /dev/null +++ b/java-lite/src/main/java/app/services/ArticleService.java @@ -0,0 +1,13 @@ +package app.services; + +import java.util.List; + +import app.models.Article; + +public interface ArticleService { + + List
getArticles(); + + Article search(String keyword); + +} diff --git a/java-lite/src/main/java/app/services/ArticleServiceImpl.java b/java-lite/src/main/java/app/services/ArticleServiceImpl.java new file mode 100755 index 0000000000..fc81576f91 --- /dev/null +++ b/java-lite/src/main/java/app/services/ArticleServiceImpl.java @@ -0,0 +1,34 @@ +package app.services; + +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; + +import app.models.Article; + +public class ArticleServiceImpl implements ArticleService { + + public List
getArticles() { + return fetchArticles(); + } + + public Article search(String keyword) { + Article ar = new Article("Article with " + keyword, "baeldung", "1250", Instant.now() + .toString()); + return ar; + } + + private List
fetchArticles() { + Article ar1 = new Article("Introduction to ActiveWeb", "baeldung", "1650", Instant.now() + .toString()); + + Article ar = new Article("Introduction to Mule", "baeldung", "1650", Instant.now() + .toString()); + List
articles = new ArrayList
(); + articles.add(ar); + articles.add(ar1); + return articles; + + } + +} diff --git a/java-lite/src/main/java/app/services/ArticleServiceModule.java b/java-lite/src/main/java/app/services/ArticleServiceModule.java new file mode 100755 index 0000000000..feae2a2ff7 --- /dev/null +++ b/java-lite/src/main/java/app/services/ArticleServiceModule.java @@ -0,0 +1,12 @@ +package app.services; + +import com.google.inject.AbstractModule; + +public class ArticleServiceModule extends AbstractModule { + + @Override + protected void configure() { + bind(ArticleService.class).to(ArticleServiceImpl.class) + .asEagerSingleton(); + } +} diff --git a/java-lite/src/main/webapp/WEB-INF/views/article/index.ftl b/java-lite/src/main/webapp/WEB-INF/views/article/index.ftl new file mode 100755 index 0000000000..2d5f211427 --- /dev/null +++ b/java-lite/src/main/webapp/WEB-INF/views/article/index.ftl @@ -0,0 +1,18 @@ +<@content for="title">Articles + + + + + + + + +<#list articles as article> + + + + + + + +
TitleAuthorWords #Date Published
${article.title}${article.author}${article.words}${article.date}
\ No newline at end of file diff --git a/java-lite/src/main/webapp/WEB-INF/views/article/search.ftl b/java-lite/src/main/webapp/WEB-INF/views/article/search.ftl new file mode 100755 index 0000000000..45a73808ed --- /dev/null +++ b/java-lite/src/main/webapp/WEB-INF/views/article/search.ftl @@ -0,0 +1,17 @@ +<@content for="title">Search + + + + + + + + + + + + + + + +
TitleAuthorWords #Date Published
${article.title}${article.author}${article.words}${article.date}
\ No newline at end of file diff --git a/java-lite/src/main/webapp/WEB-INF/views/common/error.ftl b/java-lite/src/main/webapp/WEB-INF/views/common/error.ftl new file mode 100755 index 0000000000..5fbf3c5243 --- /dev/null +++ b/java-lite/src/main/webapp/WEB-INF/views/common/error.ftl @@ -0,0 +1,3 @@ +<@content for="title">Simple Web App + +

Application error

diff --git a/java-lite/src/main/webapp/WEB-INF/views/home/index.ftl b/java-lite/src/main/webapp/WEB-INF/views/home/index.ftl new file mode 100755 index 0000000000..ae6a7c56a5 --- /dev/null +++ b/java-lite/src/main/webapp/WEB-INF/views/home/index.ftl @@ -0,0 +1,3 @@ +<@content for="title">Simple Web App + +

Baeldung ActiveWeb Demo Application

diff --git a/java-lite/src/main/webapp/WEB-INF/views/layouts/default_layout.ftl b/java-lite/src/main/webapp/WEB-INF/views/layouts/default_layout.ftl new file mode 100755 index 0000000000..2985e56278 --- /dev/null +++ b/java-lite/src/main/webapp/WEB-INF/views/layouts/default_layout.ftl @@ -0,0 +1,16 @@ +<#setting url_escaping_charset='ISO-8859-1'> + + + + + + +
+<#include "header.ftl" > +
+ ${page_content} +
+<#include "footer.ftl" > +
+ + diff --git a/java-lite/src/main/webapp/WEB-INF/views/layouts/footer.ftl b/java-lite/src/main/webapp/WEB-INF/views/layouts/footer.ftl new file mode 100755 index 0000000000..1408547424 --- /dev/null +++ b/java-lite/src/main/webapp/WEB-INF/views/layouts/footer.ftl @@ -0,0 +1,3 @@ + diff --git a/java-lite/src/main/webapp/WEB-INF/views/layouts/header.ftl b/java-lite/src/main/webapp/WEB-INF/views/layouts/header.ftl new file mode 100755 index 0000000000..8187435035 --- /dev/null +++ b/java-lite/src/main/webapp/WEB-INF/views/layouts/header.ftl @@ -0,0 +1,4 @@ + + diff --git a/java-lite/src/main/webapp/WEB-INF/web.xml b/java-lite/src/main/webapp/WEB-INF/web.xml index c285876c86..23bb8b865e 100644 --- a/java-lite/src/main/webapp/WEB-INF/web.xml +++ b/java-lite/src/main/webapp/WEB-INF/web.xml @@ -6,6 +6,10 @@ dispatcher org.javalite.activeweb.RequestDispatcher + + root_controller + home + exclusions css,images,js,ico diff --git a/java-lite/src/test/java/app/controllers/ArticleControllerSpec.java b/java-lite/src/test/java/app/controllers/ArticleControllerSpec.java new file mode 100755 index 0000000000..882441dad3 --- /dev/null +++ b/java-lite/src/test/java/app/controllers/ArticleControllerSpec.java @@ -0,0 +1,31 @@ +package app.controllers; + +import org.javalite.activeweb.ControllerSpec; +import org.junit.Before; +import org.junit.Test; + +import com.google.inject.Guice; + +import app.services.ArticleServiceModule; + +public class ArticleControllerSpec extends ControllerSpec { + + @Before + public void before() { + setInjector(Guice.createInjector(new ArticleServiceModule())); + } + + @Test + public void whenReturnedArticlesThenCorrect() { + request().get("index"); + a(responseContent()).shouldContain("Introduction to Mule"); + } + + @Test + public void givenKeywordWhenFoundArticleThenCorrect() { + request().param("key", "Java") + .get("search"); + a(responseContent()).shouldContain("Article with Java"); + } + +} diff --git a/java-rmi/README.md b/java-rmi/README.md new file mode 100644 index 0000000000..4d12060395 --- /dev/null +++ b/java-rmi/README.md @@ -0,0 +1,3 @@ +### Relevant articles + +- [Getting Started with Java RMI](http://www.baeldung.com/java-rmi) diff --git a/java-rmi/pom.xml b/java-rmi/pom.xml new file mode 100644 index 0000000000..7c08968cbf --- /dev/null +++ b/java-rmi/pom.xml @@ -0,0 +1,20 @@ + + 4.0.0 + + com.baeldung.rmi + java-rmi + 1.0-SNAPSHOT + jar + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + UTF-8 + + + diff --git a/java-rmi/src/main/java/com/baeldung/rmi/Message.java b/java-rmi/src/main/java/com/baeldung/rmi/Message.java new file mode 100644 index 0000000000..6d21a45fe3 --- /dev/null +++ b/java-rmi/src/main/java/com/baeldung/rmi/Message.java @@ -0,0 +1,36 @@ +package com.baeldung.rmi; + +import java.io.Serializable; + +public class Message implements Serializable { + + private String messageText; + + private String contentType; + + public Message() { + } + + public Message(String messageText, String contentType) { + + this.messageText = messageText; + this.contentType = contentType; + } + + public String getMessageText() { + return messageText; + } + + public void setMessageText(String messageText) { + this.messageText = messageText; + } + + public String getContentType() { + return contentType; + } + + public void setContentType(String contentType) { + this.contentType = contentType; + } + +} diff --git a/java-rmi/src/main/java/com/baeldung/rmi/MessengerService.java b/java-rmi/src/main/java/com/baeldung/rmi/MessengerService.java new file mode 100644 index 0000000000..f962e4ad07 --- /dev/null +++ b/java-rmi/src/main/java/com/baeldung/rmi/MessengerService.java @@ -0,0 +1,11 @@ +package com.baeldung.rmi; + +import java.rmi.Remote; +import java.rmi.RemoteException; + +public interface MessengerService extends Remote { + + public String sendMessage(String clientMessage) throws RemoteException; + + public Message sendMessage(Message clientMessage) throws RemoteException; +} \ No newline at end of file diff --git a/java-rmi/src/main/java/com/baeldung/rmi/MessengerServiceImpl.java b/java-rmi/src/main/java/com/baeldung/rmi/MessengerServiceImpl.java new file mode 100644 index 0000000000..ebf03d4b67 --- /dev/null +++ b/java-rmi/src/main/java/com/baeldung/rmi/MessengerServiceImpl.java @@ -0,0 +1,37 @@ +package com.baeldung.rmi; + +import java.rmi.RemoteException; +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; +import java.rmi.server.UnicastRemoteObject; + +public class MessengerServiceImpl implements MessengerService { + + public String sendMessage(String clientMessage) { + + String serverMessage = null; + if (clientMessage.equals("Client Message")) { + serverMessage = "Server Message"; + } + + return serverMessage; + } + + public void createStubAndBind() throws RemoteException { + + MessengerService stub = (MessengerService) UnicastRemoteObject.exportObject((MessengerService) this, 0); + Registry registry = LocateRegistry.createRegistry(1099); + registry.rebind("MessengerService", stub); + } + + public Message sendMessage(Message clientMessage) throws RemoteException { + + Message serverMessage = null; + if (clientMessage.getMessageText().equals("Client Message")) { + serverMessage = new Message("Server Message", "text/plain"); + } + + return serverMessage; + } + +} \ No newline at end of file diff --git a/java-rmi/src/test/java/com/baeldung/rmi/JavaRMIIntegrationTest.java b/java-rmi/src/test/java/com/baeldung/rmi/JavaRMIIntegrationTest.java new file mode 100644 index 0000000000..66bfbe49eb --- /dev/null +++ b/java-rmi/src/test/java/com/baeldung/rmi/JavaRMIIntegrationTest.java @@ -0,0 +1,44 @@ +package com.baeldung.rmi; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.rmi.NotBoundException; +import java.rmi.RemoteException; +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; + +import org.junit.BeforeClass; +import org.junit.Test; + +public class JavaRMIIntegrationTest { + + @BeforeClass + public static void whenRunServer_thenServerStarts() { + + try { + MessengerServiceImpl server = new MessengerServiceImpl(); + server.createStubAndBind(); + } catch (RemoteException e) { + fail("Exception Occurred"); + } + } + + @Test + public void whenClientSendsMessageToServer_thenServerSendsResponseMessage() { + + try { + Registry registry = LocateRegistry.getRegistry(); + MessengerService server = (MessengerService) registry.lookup("MessengerService"); + String responseMessage = server.sendMessage("Client Message"); + + String expectedMessage = "Server Message"; + assertEquals(responseMessage, expectedMessage); + } catch (RemoteException e) { + fail("Exception Occurred"); + } catch (NotBoundException nb) { + fail("Exception Occurred"); + } + } + +} \ No newline at end of file diff --git a/java-spi/exchange-rate-api/pom.xml b/java-spi/exchange-rate-api/pom.xml new file mode 100644 index 0000000000..27651533a9 --- /dev/null +++ b/java-spi/exchange-rate-api/pom.xml @@ -0,0 +1,14 @@ + + 4.0.0 + + exchange-rate-api + jar + + + com.baeldung + java-spi + 1.0.0-SNAPSHOT + + + diff --git a/java-spi/exchange-rate-api/src/main/java/com/baeldung/rate/ExchangeRate.java b/java-spi/exchange-rate-api/src/main/java/com/baeldung/rate/ExchangeRate.java new file mode 100644 index 0000000000..afc7ef92ce --- /dev/null +++ b/java-spi/exchange-rate-api/src/main/java/com/baeldung/rate/ExchangeRate.java @@ -0,0 +1,42 @@ +package com.baeldung.rate; + +import com.baeldung.rate.exception.ProviderNotFoundException; +import com.baeldung.rate.spi.ExchangeRateProvider; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.ServiceLoader; + +public final class ExchangeRate { + + private static final String DEFAULT_PROVIDER = "com.baeldung.rate.spi.YahooFinanceExchangeRateProvider"; + + //All providers + public static List providers() { + List services = new ArrayList<>(); + ServiceLoader loader = ServiceLoader.load(ExchangeRateProvider.class); + loader.forEach(exchangeRateProvider -> { + services.add(exchangeRateProvider); + }); + return services; + } + + //Default provider + public static ExchangeRateProvider provider() { + return provider(DEFAULT_PROVIDER); + } + + //provider by name + public static ExchangeRateProvider provider(String providerName) { + ServiceLoader loader = ServiceLoader.load(ExchangeRateProvider.class); + Iterator it = loader.iterator(); + while (it.hasNext()) { + ExchangeRateProvider provider = it.next(); + if (providerName.equals(provider.getClass().getName())) { + return provider; + } + } + throw new ProviderNotFoundException("Exchange Rate provider " + providerName + " not found"); + } +} diff --git a/java-spi/exchange-rate-api/src/main/java/com/baeldung/rate/api/Quote.java b/java-spi/exchange-rate-api/src/main/java/com/baeldung/rate/api/Quote.java new file mode 100644 index 0000000000..577af3b618 --- /dev/null +++ b/java-spi/exchange-rate-api/src/main/java/com/baeldung/rate/api/Quote.java @@ -0,0 +1,44 @@ +package com.baeldung.rate.api; + +import java.math.BigDecimal; +import java.time.LocalDate; + +public class Quote { + private String currency; + private BigDecimal ask; + private BigDecimal bid; + private LocalDate date; + //... + + public String getCurrency() { + return currency; + } + + public void setCurrency(String currency) { + this.currency = currency; + } + + public BigDecimal getAsk() { + return ask; + } + + public void setAsk(BigDecimal ask) { + this.ask = ask; + } + + public BigDecimal getBid() { + return bid; + } + + public void setBid(BigDecimal bid) { + this.bid = bid; + } + + public LocalDate getDate() { + return date; + } + + public void setDate(LocalDate date) { + this.date = date; + } +} \ No newline at end of file diff --git a/java-spi/exchange-rate-api/src/main/java/com/baeldung/rate/api/QuoteManager.java b/java-spi/exchange-rate-api/src/main/java/com/baeldung/rate/api/QuoteManager.java new file mode 100644 index 0000000000..16416eaf65 --- /dev/null +++ b/java-spi/exchange-rate-api/src/main/java/com/baeldung/rate/api/QuoteManager.java @@ -0,0 +1,8 @@ +package com.baeldung.rate.api; + +import java.time.LocalDate; +import java.util.List; + +public interface QuoteManager { + List getQuotes(String baseCurrency, LocalDate date); +} diff --git a/java-spi/exchange-rate-api/src/main/java/com/baeldung/rate/exception/ProviderNotFoundException.java b/java-spi/exchange-rate-api/src/main/java/com/baeldung/rate/exception/ProviderNotFoundException.java new file mode 100644 index 0000000000..3a2d92c4fd --- /dev/null +++ b/java-spi/exchange-rate-api/src/main/java/com/baeldung/rate/exception/ProviderNotFoundException.java @@ -0,0 +1,13 @@ +package com.baeldung.rate.exception; + +public class ProviderNotFoundException extends RuntimeException { + + public ProviderNotFoundException() { + super(); + } + + public ProviderNotFoundException(String message) { + super(message); + } + +} diff --git a/java-spi/exchange-rate-api/src/main/java/com/baeldung/rate/spi/ExchangeRateProvider.java b/java-spi/exchange-rate-api/src/main/java/com/baeldung/rate/spi/ExchangeRateProvider.java new file mode 100644 index 0000000000..c3157b2b03 --- /dev/null +++ b/java-spi/exchange-rate-api/src/main/java/com/baeldung/rate/spi/ExchangeRateProvider.java @@ -0,0 +1,7 @@ +package com.baeldung.rate.spi; + +import com.baeldung.rate.api.QuoteManager; + +public interface ExchangeRateProvider { + QuoteManager create(); +} \ No newline at end of file diff --git a/java-spi/exchange-rate-app/pom.xml b/java-spi/exchange-rate-app/pom.xml new file mode 100644 index 0000000000..7e64cf7438 --- /dev/null +++ b/java-spi/exchange-rate-app/pom.xml @@ -0,0 +1,27 @@ + + 4.0.0 + + exchange-rate-app + jar + + + com.baeldung + java-spi + 1.0.0-SNAPSHOT + + + + + com.baeldung + exchange-rate-api + 1.0.0-SNAPSHOT + + + com.baeldung + exchange-rate-impl + 1.0.0-SNAPSHOT + + + + diff --git a/java-spi/exchange-rate-app/src/main/java/com.baeldung.rate.app/MainApp.java b/java-spi/exchange-rate-app/src/main/java/com.baeldung.rate.app/MainApp.java new file mode 100644 index 0000000000..fd43ed3a85 --- /dev/null +++ b/java-spi/exchange-rate-app/src/main/java/com.baeldung.rate.app/MainApp.java @@ -0,0 +1,21 @@ +package com.baeldung.rate.app; + +import com.baeldung.rate.ExchangeRate; +import com.baeldung.rate.api.Quote; + +import java.time.LocalDate; +import java.util.List; + +public class MainApp { + public static void main(String... args) { + ExchangeRate.providers().forEach(provider -> { + System.out.println("Retreiving USD quotes from provider :" + provider); + List quotes = provider.create().getQuotes("USD", LocalDate.now()); + System.out.println(String.format("%14s%12s|%12s", "","Ask", "Bid")); + System.out.println("----------------------------------------"); + quotes.forEach(quote -> { + System.out.println("USD --> " + quote.getCurrency() + " : " + String.format("%12f|%12f", quote.getAsk(), quote.getBid())); + }); + }); + } +} diff --git a/java-spi/exchange-rate-impl/pom.xml b/java-spi/exchange-rate-impl/pom.xml new file mode 100644 index 0000000000..ec22791351 --- /dev/null +++ b/java-spi/exchange-rate-impl/pom.xml @@ -0,0 +1,42 @@ + + 4.0.0 + + exchange-rate-impl + jar + + + com.baeldung + java-spi + 1.0.0-SNAPSHOT + + + + + com.baeldung + exchange-rate-api + 1.0.0-SNAPSHOT + + + com.squareup.okhttp3 + okhttp + 3.10.0 + + + javax.json.bind + javax.json.bind-api + 1.0 + + + org.eclipse + yasson + 1.0.1 + + + org.glassfish + javax.json + 1.1.2 + + + + diff --git a/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/QuoteResponse.java b/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/QuoteResponse.java new file mode 100644 index 0000000000..9ba4fb26b0 --- /dev/null +++ b/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/QuoteResponse.java @@ -0,0 +1,26 @@ +package com.baeldung.rate.impl; + +import com.baeldung.rate.api.Quote; + +import java.util.List; + +public class QuoteResponse { + private List result; + private String error; + + public List getResult() { + return result; + } + + public void setResult(List result) { + this.result = result; + } + + public String getError() { + return error; + } + + public void setError(String error) { + this.error = error; + } +} diff --git a/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/QuoteResponseWrapper.java b/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/QuoteResponseWrapper.java new file mode 100644 index 0000000000..6d7be086f0 --- /dev/null +++ b/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/QuoteResponseWrapper.java @@ -0,0 +1,13 @@ +package com.baeldung.rate.impl; + +public class QuoteResponseWrapper { + private QuoteResponse quoteResponse; + + public QuoteResponse getQuoteResponse() { + return quoteResponse; + } + + public void setQuoteResponse(QuoteResponse quoteResponse) { + this.quoteResponse = quoteResponse; + } +} diff --git a/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/YahooFinanceExchangeRateProvider.java b/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/YahooFinanceExchangeRateProvider.java new file mode 100644 index 0000000000..9a069cfde4 --- /dev/null +++ b/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/YahooFinanceExchangeRateProvider.java @@ -0,0 +1,13 @@ +package com.baeldung.rate.impl; + +import com.baeldung.rate.api.QuoteManager; +import com.baeldung.rate.spi.ExchangeRateProvider; + +public class YahooFinanceExchangeRateProvider implements ExchangeRateProvider { + + @Override + public QuoteManager create() { + return new YahooQuoteManagerImpl(); + } + +} \ No newline at end of file diff --git a/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/YahooQuoteManagerImpl.java b/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/YahooQuoteManagerImpl.java new file mode 100644 index 0000000000..8cc68259be --- /dev/null +++ b/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/YahooQuoteManagerImpl.java @@ -0,0 +1,65 @@ +package com.baeldung.rate.impl; + +import com.baeldung.rate.api.Quote; +import com.baeldung.rate.api.QuoteManager; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + +import javax.json.bind.JsonbBuilder; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.time.LocalDate; +import java.util.Currency; +import java.util.List; + +public class YahooQuoteManagerImpl implements QuoteManager { + + static final String URL_PROVIDER = "https://query1.finance.yahoo.com/v7/finance/quote"; + OkHttpClient client = new OkHttpClient(); + + @Override + public List getQuotes(String baseCurrency, LocalDate date) { + + StringBuilder sb = new StringBuilder(); + Currency.getAvailableCurrencies().forEach(currency -> { + if (!currency.equals(currency.getCurrencyCode())) { + sb.append(baseCurrency).append(currency.getCurrencyCode()).append("=X").append(","); + } + }); + + String value = ""; + try { + value = URLEncoder.encode(sb.toString().substring(0, sb.toString().length() - 1), "UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + String queryString = String.format("%s=%s", "symbols", value); + String response = doGetRequest(queryString); + System.out.println(response); + return map(response); + } + + private List map(String response) { + QuoteResponseWrapper qrw = JsonbBuilder.create().fromJson(response, QuoteResponseWrapper.class); + return qrw.getQuoteResponse().getResult(); + } + + String doGetRequest(String queryString) { + String fullUrl = URL_PROVIDER + "?" + queryString; + + System.out.println(fullUrl); + Request request = new Request.Builder() + .url(fullUrl) + .build(); + Response response = null; + try { + response = client.newCall(request).execute(); + return response.body().string(); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } +} diff --git a/java-spi/exchange-rate-impl/src/main/resources/META-INF/services/com.baeldung.rate.spi.ExchangeRateProvider b/java-spi/exchange-rate-impl/src/main/resources/META-INF/services/com.baeldung.rate.spi.ExchangeRateProvider new file mode 100644 index 0000000000..67ba8a8227 --- /dev/null +++ b/java-spi/exchange-rate-impl/src/main/resources/META-INF/services/com.baeldung.rate.spi.ExchangeRateProvider @@ -0,0 +1 @@ +com.baeldung.rate.impl.YahooFinanceExchangeRateProvider \ No newline at end of file diff --git a/java-spi/pom.xml b/java-spi/pom.xml new file mode 100644 index 0000000000..8eaa184d8d --- /dev/null +++ b/java-spi/pom.xml @@ -0,0 +1,20 @@ + + 4.0.0 + + java-spi + pom + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + exchange-rate-api + exchange-rate-impl + exchange-rate-app + + + diff --git a/javax-servlets/src/main/java/com/baeldung/controller/StudentServlet.java b/javax-servlets/src/main/java/com/baeldung/controller/StudentServlet.java new file mode 100644 index 0000000000..b1924198a4 --- /dev/null +++ b/javax-servlets/src/main/java/com/baeldung/controller/StudentServlet.java @@ -0,0 +1,39 @@ +package com.baeldung.controller; + +import com.baeldung.service.StudentService; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +@WebServlet(name = "StudentServlet", urlPatterns = "/student-record") +public class StudentServlet extends HttpServlet { + + private final StudentService studentService = new StudentService(); + + private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String studentID = request.getParameter("id"); + if (studentID != null) { + int id = Integer.parseInt(studentID); + studentService.getStudent(id) + .ifPresent(s -> request.setAttribute("studentRecord", s)); + } + + RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/student-record.jsp"); + dispatcher.forward(request, response); + } + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + processRequest(request, response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + processRequest(request, response); + } +} diff --git a/javax-servlets/src/main/java/com/baeldung/model/Student.java b/javax-servlets/src/main/java/com/baeldung/model/Student.java new file mode 100644 index 0000000000..ce8a27375a --- /dev/null +++ b/javax-servlets/src/main/java/com/baeldung/model/Student.java @@ -0,0 +1,39 @@ +package com.baeldung.model; + +public class Student { + + private int id; + private String firstName; + private String lastName; + + public Student(int id, String firstName, String lastName) { + super(); + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + 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; + } +} diff --git a/javax-servlets/src/main/java/com/baeldung/service/StudentService.java b/javax-servlets/src/main/java/com/baeldung/service/StudentService.java new file mode 100644 index 0000000000..525d47683f --- /dev/null +++ b/javax-servlets/src/main/java/com/baeldung/service/StudentService.java @@ -0,0 +1,21 @@ +package com.baeldung.service; + +import com.baeldung.model.Student; + +import java.util.Optional; + +public class StudentService { + + public Optional getStudent(int id) { + switch (id) { + case 1: + return Optional.of(new Student(1, "John", "Doe")); + case 2: + return Optional.of(new Student(2, "Jane", "Goodall")); + case 3: + return Optional.of(new Student(3, "Max", "Born")); + default: + return Optional.empty(); + } + } +} diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java index fcd9143dff..c78129a9cf 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java @@ -1,7 +1,6 @@ package com.baeldung.servlets; import javax.servlet.RequestDispatcher; -import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; @@ -13,7 +12,7 @@ public class FormServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + throws IOException { String height = request.getParameter("height"); String weight = request.getParameter("weight"); @@ -25,23 +24,20 @@ public class FormServlet extends HttpServlet { response.setHeader("Test", "Success"); response.setHeader("BMI", String.valueOf(bmi)); - RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp"); + RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/index.jsp"); dispatcher.forward(request, response); } catch (Exception e) { - response.sendRedirect("index.jsp"); } } @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + protected void doGet(HttpServletRequest request, HttpServletResponse response) { // do something else here } private Double calculateBMI(Double weight, Double height) { - return weight / (height * height); } } \ No newline at end of file diff --git a/javax-servlets/web/index.jsp b/javax-servlets/src/main/webapp/WEB-INF/jsp/index.jsp similarity index 100% rename from javax-servlets/web/index.jsp rename to javax-servlets/src/main/webapp/WEB-INF/jsp/index.jsp diff --git a/javax-servlets/src/main/webapp/WEB-INF/jsp/student-record.jsp b/javax-servlets/src/main/webapp/WEB-INF/jsp/student-record.jsp new file mode 100644 index 0000000000..4b9d9b378f --- /dev/null +++ b/javax-servlets/src/main/webapp/WEB-INF/jsp/student-record.jsp @@ -0,0 +1,36 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + +<%@ page import="com.baeldung.model.Student"%> + + + + +Student Record + + + <% + if (request.getAttribute("studentRecord") != null) { + Student student = (Student) request.getAttribute("studentRecord"); + %> + +

Student Record

+
+ ID: + <%=student.getId()%>
+
+ First Name: + <%=student.getFirstName()%>
+
+ Last Name: + <%=student.getLastName()%>
+ + <% + } else { + %> +

No student record found.

+ <% + } + %> + + \ No newline at end of file diff --git a/javax-servlets/web/WEB-INF/web.xml b/javax-servlets/src/main/webapp/WEB-INF/web.xml similarity index 100% rename from javax-servlets/web/WEB-INF/web.xml rename to javax-servlets/src/main/webapp/WEB-INF/web.xml diff --git a/javaxval/pom.xml b/javaxval/pom.xml index 6a83a25f01..a05ee43aaf 100644 --- a/javaxval/pom.xml +++ b/javaxval/pom.xml @@ -6,10 +6,11 @@ 0.1-SNAPSHOT - 2.0.0.Final - 6.0.2.Final + 2.0.1.Final + 6.0.7.Final 3.0.0 2.2.6 + 5.0.2.RELEASE @@ -21,12 +22,6 @@ - - javax.validation - validation-api - ${validation-api.version} - - org.hibernate hibernate-validator @@ -50,6 +45,16 @@ javax.el ${javax.el.version} + + org.springframework + spring-context + ${org.springframework.version} + + + org.springframework + spring-test + ${org.springframework.version} + diff --git a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/MethodValidationConfig.java b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/MethodValidationConfig.java new file mode 100644 index 0000000000..206a145337 --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/MethodValidationConfig.java @@ -0,0 +1,38 @@ +package org.baeldung.javaxval.methodvalidation; + +import org.baeldung.javaxval.methodvalidation.model.Customer; +import org.baeldung.javaxval.methodvalidation.model.Reservation; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Scope; +import org.springframework.validation.beanvalidation.MethodValidationPostProcessor; + +import java.time.LocalDate; + +@Configuration +@ComponentScan({ "org.baeldung.javaxval.methodvalidation.model" }) +public class MethodValidationConfig { + + @Bean + public MethodValidationPostProcessor methodValidationPostProcessor() { + return new MethodValidationPostProcessor(); + } + + @Bean("customer") + @Scope(BeanDefinition.SCOPE_PROTOTYPE) + public Customer customer(String firstName, String lastName) { + + Customer customer = new Customer(firstName, lastName); + return customer; + } + + @Bean("reservation") + @Scope(BeanDefinition.SCOPE_PROTOTYPE) + public Reservation reservation(LocalDate begin, LocalDate end, Customer customer, int room) { + + Reservation reservation = new Reservation(begin, end, customer, room); + return reservation; + } +} diff --git a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ConsistentDateParameterValidator.java b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ConsistentDateParameterValidator.java new file mode 100644 index 0000000000..f1c97760d7 --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ConsistentDateParameterValidator.java @@ -0,0 +1,25 @@ +package org.baeldung.javaxval.methodvalidation.constraints; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import javax.validation.constraintvalidation.SupportedValidationTarget; +import javax.validation.constraintvalidation.ValidationTarget; +import java.time.LocalDate; + +@SupportedValidationTarget(ValidationTarget.PARAMETERS) +public class ConsistentDateParameterValidator implements ConstraintValidator { + + @Override + public boolean isValid(Object[] value, ConstraintValidatorContext context) { + + if (value[0] == null || value[1] == null) { + return true; + } + + if (!(value[0] instanceof LocalDate) || !(value[1] instanceof LocalDate)) { + throw new IllegalArgumentException("Illegal method signature, expected two parameters of type LocalDate."); + } + + return ((LocalDate) value[0]).isAfter(LocalDate.now()) && ((LocalDate) value[0]).isBefore((LocalDate) value[1]); + } +} diff --git a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ConsistentDateParameters.java b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ConsistentDateParameters.java new file mode 100644 index 0000000000..6b321f545c --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ConsistentDateParameters.java @@ -0,0 +1,23 @@ +package org.baeldung.javaxval.methodvalidation.constraints; + +import javax.validation.Constraint; +import javax.validation.Payload; +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Constraint(validatedBy = ConsistentDateParameterValidator.class) +@Target({ METHOD, CONSTRUCTOR }) +@Retention(RUNTIME) +@Documented +public @interface ConsistentDateParameters { + + String message() default "End date must be after begin date and both must be in the future"; + + Class[] groups() default {}; + + Class[] payload() default {}; +} diff --git a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ValidReservation.java b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ValidReservation.java new file mode 100644 index 0000000000..f9cdea1483 --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ValidReservation.java @@ -0,0 +1,24 @@ +package org.baeldung.javaxval.methodvalidation.constraints; + +import javax.validation.Constraint; +import javax.validation.Payload; +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.CONSTRUCTOR; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Constraint(validatedBy = ValidReservationValidator.class) +@Target({ METHOD, CONSTRUCTOR }) +@Retention(RUNTIME) +@Documented +public @interface ValidReservation { + + String message() default "End date must be after begin date and both must be in the future, room number must be bigger than 0"; + + Class[] groups() default {}; + + Class[] payload() default {}; +} diff --git a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ValidReservationValidator.java b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ValidReservationValidator.java new file mode 100644 index 0000000000..7b730480ed --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ValidReservationValidator.java @@ -0,0 +1,32 @@ +package org.baeldung.javaxval.methodvalidation.constraints; + +import org.baeldung.javaxval.methodvalidation.model.Reservation; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import java.time.LocalDate; + +public class ValidReservationValidator implements ConstraintValidator { + + @Override + public boolean isValid(Reservation reservation, ConstraintValidatorContext context) { + + if (reservation == null) { + return true; + } + + if (!(reservation instanceof Reservation)) { + throw new IllegalArgumentException("Illegal method signature, expected parameter of type Reservation."); + } + + if (reservation.getBegin() == null || reservation.getEnd() == null || reservation.getCustomer() == null) { + return false; + } + + return (reservation.getBegin() + .isAfter(LocalDate.now()) + && reservation.getBegin() + .isBefore(reservation.getEnd()) + && reservation.getRoom() > 0); + } +} diff --git a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/Customer.java b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/Customer.java new file mode 100644 index 0000000000..fe9ad7080e --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/Customer.java @@ -0,0 +1,41 @@ +package org.baeldung.javaxval.methodvalidation.model; + +import org.springframework.validation.annotation.Validated; + +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; + +@Validated +public class Customer { + + @Size(min = 5, max = 200) + private String firstName; + + @Size(min = 5, max = 200) + private String lastName; + + public Customer(@Size(min = 5, max = 200) @NotNull String firstName, @Size(min = 5, max = 200) @NotNull String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + public Customer() { + + } + + 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; + } +} diff --git a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/Reservation.java b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/Reservation.java new file mode 100644 index 0000000000..a8c01d2be1 --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/Reservation.java @@ -0,0 +1,64 @@ +package org.baeldung.javaxval.methodvalidation.model; + +import org.baeldung.javaxval.methodvalidation.constraints.ConsistentDateParameters; +import org.baeldung.javaxval.methodvalidation.constraints.ValidReservation; +import org.springframework.validation.annotation.Validated; + +import javax.validation.Valid; +import javax.validation.constraints.Positive; +import java.time.LocalDate; + +@Validated +public class Reservation { + + private LocalDate begin; + + private LocalDate end; + + @Valid + private Customer customer; + + @Positive + private int room; + + @ConsistentDateParameters + @ValidReservation + public Reservation(LocalDate begin, LocalDate end, Customer customer, int room) { + this.begin = begin; + this.end = end; + this.customer = customer; + this.room = room; + } + + public LocalDate getBegin() { + return begin; + } + + public void setBegin(LocalDate begin) { + this.begin = begin; + } + + public LocalDate getEnd() { + return end; + } + + public void setEnd(LocalDate end) { + this.end = end; + } + + public Customer getCustomer() { + return customer; + } + + public void setCustomer(Customer customer) { + this.customer = customer; + } + + public int getRoom() { + return room; + } + + public void setRoom(int room) { + this.room = room; + } +} diff --git a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/ReservationManagement.java b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/ReservationManagement.java new file mode 100644 index 0000000000..f6fec1a15d --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/ReservationManagement.java @@ -0,0 +1,50 @@ +package org.baeldung.javaxval.methodvalidation.model; + +import org.baeldung.javaxval.methodvalidation.constraints.ConsistentDateParameters; +import org.baeldung.javaxval.methodvalidation.constraints.ValidReservation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Controller; +import org.springframework.validation.annotation.Validated; + +import javax.validation.Valid; +import javax.validation.constraints.*; +import java.time.LocalDate; +import java.util.List; + +@Controller +@Validated +public class ReservationManagement { + + @Autowired + private ApplicationContext applicationContext; + + @ConsistentDateParameters + public void createReservation(LocalDate begin, LocalDate end, @NotNull Customer customer) { + + // ... + } + + public void createReservation(@NotNull @Future LocalDate begin, @Min(1) int duration, @NotNull Customer customer) { + + // ... + } + + public void createReservation(@Valid Reservation reservation) { + + // ... + } + + @NotNull + @Size(min = 1) + public List<@NotNull Customer> getAllCustomers() { + + return null; + } + + @Valid + public Reservation getReservationById(int id) { + + return null; + } +} diff --git a/javaxval/src/test/java/org/baeldung/javaxval/methodvalidation/ContainerValidationIntegrationTest.java b/javaxval/src/test/java/org/baeldung/javaxval/methodvalidation/ContainerValidationIntegrationTest.java new file mode 100644 index 0000000000..2363bf8f5d --- /dev/null +++ b/javaxval/src/test/java/org/baeldung/javaxval/methodvalidation/ContainerValidationIntegrationTest.java @@ -0,0 +1,97 @@ +package org.baeldung.javaxval.methodvalidation; + +import org.baeldung.javaxval.methodvalidation.model.Customer; +import org.baeldung.javaxval.methodvalidation.model.Reservation; +import org.baeldung.javaxval.methodvalidation.model.ReservationManagement; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import javax.validation.ConstraintViolationException; +import java.time.LocalDate; +import java.util.List; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { MethodValidationConfig.class }, loader = AnnotationConfigContextLoader.class) +public class ContainerValidationIntegrationTest { + + @Autowired + ReservationManagement reservationManagement; + + @Rule + public final ExpectedException exception = ExpectedException.none(); + + @Test + public void whenValidationWithInvalidMethodParameters_thenConstraintViolationException() { + + exception.expect(ConstraintViolationException.class); + reservationManagement.createReservation(LocalDate.now(), 0, null); + } + + @Test + public void whenValidationWithValidMethodParameters_thenNoException() { + + reservationManagement.createReservation(LocalDate.now() + .plusDays(1), 1, new Customer("William", "Smith")); + } + + @Test + public void whenCrossParameterValidationWithInvalidParameters_thenConstraintViolationException() { + + exception.expect(ConstraintViolationException.class); + reservationManagement.createReservation(LocalDate.now(), LocalDate.now(), null); + } + + @Test + public void whenCrossParameterValidationWithValidParameters_thenNoException() { + + reservationManagement.createReservation(LocalDate.now() + .plusDays(1), + LocalDate.now() + .plusDays(2), + new Customer("William", "Smith")); + } + + @Test + public void whenValidationWithInvalidReturnValue_thenConstraintViolationException() { + + exception.expect(ConstraintViolationException.class); + List list = reservationManagement.getAllCustomers(); + } + + @Test + public void whenValidationWithInvalidCascadedValue_thenConstraintViolationException() { + + Customer customer = new Customer(); + customer.setFirstName("John"); + customer.setLastName("Doe"); + Reservation reservation = new Reservation(LocalDate.now() + .plusDays(1), + LocalDate.now() + .plusDays(2), + customer, 1); + + exception.expect(ConstraintViolationException.class); + reservationManagement.createReservation(reservation); + } + + @Test + public void whenValidationWithValidCascadedValue_thenCNoException() { + + Customer customer = new Customer(); + customer.setFirstName("William"); + customer.setLastName("Smith"); + Reservation reservation = new Reservation(LocalDate.now() + .plusDays(1), + LocalDate.now() + .plusDays(2), + customer, 1); + + reservationManagement.createReservation(reservation); + } +} diff --git a/javaxval/src/test/java/org/baeldung/javaxval/methodvalidation/ValidationIntegrationTest.java b/javaxval/src/test/java/org/baeldung/javaxval/methodvalidation/ValidationIntegrationTest.java new file mode 100644 index 0000000000..6b53d3a107 --- /dev/null +++ b/javaxval/src/test/java/org/baeldung/javaxval/methodvalidation/ValidationIntegrationTest.java @@ -0,0 +1,209 @@ +package org.baeldung.javaxval.methodvalidation; + +import org.baeldung.javaxval.methodvalidation.model.Customer; +import org.baeldung.javaxval.methodvalidation.model.Reservation; +import org.baeldung.javaxval.methodvalidation.model.ReservationManagement; +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; + +import javax.validation.ConstraintViolation; +import javax.validation.Validation; +import javax.validation.ValidatorFactory; +import javax.validation.executable.ExecutableValidator; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.time.LocalDate; +import java.util.Collections; +import java.util.Set; + +public class ValidationIntegrationTest { + + private ExecutableValidator executableValidator; + + @Before + public void getExecutableValidator() { + + ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); + this.executableValidator = factory.getValidator() + .forExecutables(); + } + + @Test + public void whenValidationWithInvalidMethodParameters_thenCorrectNumberOfVoilations() throws NoSuchMethodException { + + ReservationManagement object = new ReservationManagement(); + Method method = ReservationManagement.class.getMethod("createReservation", LocalDate.class, int.class, Customer.class); + Object[] parameterValues = { LocalDate.now(), 0, null }; + Set> violations = executableValidator.validateParameters(object, method, parameterValues); + + assertEquals(3, violations.size()); + } + + @Test + public void whenValidationWithValidMethodParameters_thenZeroVoilations() throws NoSuchMethodException { + + ReservationManagement object = new ReservationManagement(); + Method method = ReservationManagement.class.getMethod("createReservation", LocalDate.class, int.class, Customer.class); + Object[] parameterValues = { LocalDate.now() + .plusDays(1), 1, new Customer("John", "Doe") }; + Set> violations = executableValidator.validateParameters(object, method, parameterValues); + + assertEquals(0, violations.size()); + } + + @Test + public void whenCrossParameterValidationWithInvalidParameters_thenCorrectNumberOfVoilations() throws NoSuchMethodException { + + ReservationManagement object = new ReservationManagement(); + Method method = ReservationManagement.class.getMethod("createReservation", LocalDate.class, LocalDate.class, Customer.class); + Object[] parameterValues = { LocalDate.now(), LocalDate.now(), new Customer("John", "Doe") }; + Set> violations = executableValidator.validateParameters(object, method, parameterValues); + + assertEquals(1, violations.size()); + } + + @Test + public void whenCrossParameterValidationWithValidParameters_thenZeroVoilations() throws NoSuchMethodException { + + ReservationManagement object = new ReservationManagement(); + Method method = ReservationManagement.class.getMethod("createReservation", LocalDate.class, LocalDate.class, Customer.class); + Object[] parameterValues = { LocalDate.now() + .plusDays(1), + LocalDate.now() + .plusDays(2), + new Customer("John", "Doe") }; + Set> violations = executableValidator.validateParameters(object, method, parameterValues); + + assertEquals(0, violations.size()); + } + + @Test + public void whenValidationWithInvalidConstructorParameters_thenCorrectNumberOfVoilations() throws NoSuchMethodException { + + Constructor constructor = Customer.class.getConstructor(String.class, String.class); + Object[] parameterValues = { "John", "Doe" }; + Set> violations = executableValidator.validateConstructorParameters(constructor, parameterValues); + + assertEquals(2, violations.size()); + } + + @Test + public void whenValidationWithValidConstructorParameters_thenZeroVoilations() throws NoSuchMethodException { + + Constructor constructor = Customer.class.getConstructor(String.class, String.class); + Object[] parameterValues = { "William", "Smith" }; + Set> violations = executableValidator.validateConstructorParameters(constructor, parameterValues); + + assertEquals(0, violations.size()); + } + + @Test + public void whenCrossParameterValidationWithInvalidConstructorParameters_thenCorrectNumberOfVoilations() throws NoSuchMethodException { + + Constructor constructor = Reservation.class.getConstructor(LocalDate.class, LocalDate.class, Customer.class, int.class); + Object[] parameterValues = { LocalDate.now(), LocalDate.now(), new Customer("William", "Smith"), 1 }; + Set> violations = executableValidator.validateConstructorParameters(constructor, parameterValues); + + assertEquals(1, violations.size()); + } + + @Test + public void whenCrossParameterValidationWithValidConstructorParameters_thenZeroVoilations() throws NoSuchMethodException { + + Constructor constructor = Reservation.class.getConstructor(LocalDate.class, LocalDate.class, Customer.class, int.class); + Object[] parameterValues = { LocalDate.now() + .plusDays(1), + LocalDate.now() + .plusDays(2), + new Customer("William", "Smith"), 1 }; + Set> violations = executableValidator.validateConstructorParameters(constructor, parameterValues); + + assertEquals(0, violations.size()); + } + + @Test + public void whenValidationWithInvalidReturnValue_thenCorrectNumberOfVoilations() throws NoSuchMethodException { + + ReservationManagement object = new ReservationManagement(); + Method method = ReservationManagement.class.getMethod("getAllCustomers"); + Object returnValue = Collections. emptyList(); + Set> violations = executableValidator.validateReturnValue(object, method, returnValue); + + assertEquals(1, violations.size()); + } + + @Test + public void whenValidationWithValidReturnValue_thenZeroVoilations() throws NoSuchMethodException { + + ReservationManagement object = new ReservationManagement(); + Method method = ReservationManagement.class.getMethod("getAllCustomers"); + Object returnValue = Collections.singletonList(new Customer("William", "Smith")); + Set> violations = executableValidator.validateReturnValue(object, method, returnValue); + + assertEquals(0, violations.size()); + } + + @Test + public void whenValidationWithInvalidConstructorReturnValue_thenCorrectNumberOfVoilations() throws NoSuchMethodException { + + Constructor constructor = Reservation.class.getConstructor(LocalDate.class, LocalDate.class, Customer.class, int.class); + Reservation createdObject = new Reservation(LocalDate.now(), LocalDate.now(), new Customer("William", "Smith"), 0); + Set> violations = executableValidator.validateConstructorReturnValue(constructor, createdObject); + + assertEquals(1, violations.size()); + } + + @Test + public void whenValidationWithValidConstructorReturnValue_thenZeroVoilations() throws NoSuchMethodException { + + Constructor constructor = Reservation.class.getConstructor(LocalDate.class, LocalDate.class, Customer.class, int.class); + Reservation createdObject = new Reservation(LocalDate.now() + .plusDays(1), + LocalDate.now() + .plusDays(2), + new Customer("William", "Smith"), 1); + Set> violations = executableValidator.validateConstructorReturnValue(constructor, createdObject); + + assertEquals(0, violations.size()); + } + + @Test + public void whenValidationWithInvalidCascadedValue_thenCorrectNumberOfVoilations() throws NoSuchMethodException { + + ReservationManagement object = new ReservationManagement(); + Method method = ReservationManagement.class.getMethod("createReservation", Reservation.class); + Customer customer = new Customer(); + customer.setFirstName("John"); + customer.setLastName("Doe"); + Reservation reservation = new Reservation(LocalDate.now() + .plusDays(1), + LocalDate.now() + .plusDays(2), + customer, 1); + Object[] parameterValues = { reservation }; + Set> violations = executableValidator.validateParameters(object, method, parameterValues); + + assertEquals(2, violations.size()); + } + + @Test + public void whenValidationWithValidCascadedValue_thenCorrectNumberOfVoilations() throws NoSuchMethodException { + + ReservationManagement object = new ReservationManagement(); + Method method = ReservationManagement.class.getMethod("createReservation", Reservation.class); + Customer customer = new Customer(); + customer.setFirstName("William"); + customer.setLastName("Smith"); + Reservation reservation = new Reservation(LocalDate.now() + .plusDays(1), + LocalDate.now() + .plusDays(2), + customer, 1); + Object[] parameterValues = { reservation }; + Set> violations = executableValidator.validateParameters(object, method, parameterValues); + + assertEquals(0, violations.size()); + } + +} diff --git a/jenkins/README.md b/jenkins/README.md new file mode 100644 index 0000000000..da60e556df --- /dev/null +++ b/jenkins/README.md @@ -0,0 +1,3 @@ +## Relevant articles: + +- [Writing a Jenkins Plugin](http://www.baeldung.com/jenkins-custom-plugin) diff --git a/jersey/pom.xml b/jersey/pom.xml new file mode 100644 index 0000000000..0c8b6b9281 --- /dev/null +++ b/jersey/pom.xml @@ -0,0 +1,63 @@ + + + 4.0.0 + + com.baeldung + jersey + 0.0.1-SNAPSHOT + war + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + 2.26 + 1.7.25 + 3.2.0 + + + + jersey + + + maven-war-plugin + ${maven-war-plugin.version} + + false + + + + + + + + org.glassfish.jersey.core + jersey-server + ${jersey.version} + + + org.glassfish.jersey.core + jersey-client + ${jersey.version} + + + org.glassfish.jersey.bundles + jaxrs-ri + ${jersey.version} + + + org.slf4j + slf4j-api + ${slf4j.version} + + + + + + + diff --git a/jersey/src/main/java/com/baeldung/jersey/client/JerseyClient.java b/jersey/src/main/java/com/baeldung/jersey/client/JerseyClient.java new file mode 100644 index 0000000000..88ad891411 --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/client/JerseyClient.java @@ -0,0 +1,45 @@ +package com.baeldung.jersey.client; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.Response; + +import org.glassfish.jersey.client.ClientConfig; + +import com.baeldung.jersey.client.filter.RequestClientFilter; +import com.baeldung.jersey.client.filter.ResponseClientFilter; +import com.baeldung.jersey.client.interceptor.RequestClientWriterInterceptor; + +public class JerseyClient { + + private static final String URI_GREETINGS = "http://localhost:8080/jersey/greetings"; + + public static String getHelloGreeting() { + return createClient().target(URI_GREETINGS) + .request() + .get(String.class); + } + + public static String getHiGreeting() { + return createClient().target(URI_GREETINGS + "/hi") + .request() + .get(String.class); + } + + public static Response getCustomGreeting() { + return createClient().target(URI_GREETINGS + "/custom") + .request() + .post(Entity.text("custom")); + } + + private static Client createClient() { + ClientConfig config = new ClientConfig(); + config.register(RequestClientFilter.class); + config.register(ResponseClientFilter.class); + config.register(RequestClientWriterInterceptor.class); + + return ClientBuilder.newClient(config); + } + +} diff --git a/jersey/src/main/java/com/baeldung/jersey/client/filter/RequestClientFilter.java b/jersey/src/main/java/com/baeldung/jersey/client/filter/RequestClientFilter.java new file mode 100644 index 0000000000..8c6ac2c5fb --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/client/filter/RequestClientFilter.java @@ -0,0 +1,24 @@ +package com.baeldung.jersey.client.filter; + +import java.io.IOException; + +import javax.ws.rs.client.ClientRequestContext; +import javax.ws.rs.client.ClientRequestFilter; +import javax.ws.rs.ext.Provider; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Provider +public class RequestClientFilter implements ClientRequestFilter { + + private static final Logger LOG = LoggerFactory.getLogger(RequestClientFilter.class); + + @Override + public void filter(ClientRequestContext requestContext) throws IOException { + LOG.info("Request client filter"); + + requestContext.setProperty("test", "test client request filter"); + } + +} diff --git a/jersey/src/main/java/com/baeldung/jersey/client/filter/ResponseClientFilter.java b/jersey/src/main/java/com/baeldung/jersey/client/filter/ResponseClientFilter.java new file mode 100644 index 0000000000..1676fa2094 --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/client/filter/ResponseClientFilter.java @@ -0,0 +1,26 @@ +package com.baeldung.jersey.client.filter; + +import java.io.IOException; + +import javax.ws.rs.client.ClientRequestContext; +import javax.ws.rs.client.ClientResponseContext; +import javax.ws.rs.client.ClientResponseFilter; +import javax.ws.rs.ext.Provider; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Provider +public class ResponseClientFilter implements ClientResponseFilter { + + private static final Logger LOG = LoggerFactory.getLogger(ResponseClientFilter.class); + + @Override + public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException { + LOG.info("Response client filter"); + + responseContext.getHeaders() + .add("X-Test-Client", "Test response client filter"); + } + +} diff --git a/jersey/src/main/java/com/baeldung/jersey/client/interceptor/RequestClientWriterInterceptor.java b/jersey/src/main/java/com/baeldung/jersey/client/interceptor/RequestClientWriterInterceptor.java new file mode 100644 index 0000000000..7216cf18cc --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/client/interceptor/RequestClientWriterInterceptor.java @@ -0,0 +1,28 @@ +package com.baeldung.jersey.client.interceptor; + +import java.io.IOException; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.ext.Provider; +import javax.ws.rs.ext.WriterInterceptor; +import javax.ws.rs.ext.WriterInterceptorContext; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Provider +public class RequestClientWriterInterceptor implements WriterInterceptor { + + private static final Logger LOG = LoggerFactory.getLogger(RequestClientWriterInterceptor.class); + + @Override + public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException { + LOG.info("request writer interceptor in the client side"); + + context.getOutputStream() + .write(("Message added in the writer interceptor in the client side").getBytes()); + + context.proceed(); + } + +} diff --git a/jersey/src/main/java/com/baeldung/jersey/server/Greetings.java b/jersey/src/main/java/com/baeldung/jersey/server/Greetings.java new file mode 100644 index 0000000000..d0ea873db1 --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/server/Greetings.java @@ -0,0 +1,32 @@ +package com.baeldung.jersey.server; + +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; + +import com.baeldung.jersey.server.config.HelloBinding; + +@Path("/greetings") +public class Greetings { + + @GET + @HelloBinding + public String getHelloGreeting() { + return "hello"; + } + + @GET + @Path("/hi") + public String getHiGreeting() { + return "hi"; + } + + @POST + @Path("/custom") + public Response getCustomGreeting(String name) { + return Response.status(Status.OK.getStatusCode()) + .build(); + } +} diff --git a/jersey/src/main/java/com/baeldung/jersey/server/config/HelloBinding.java b/jersey/src/main/java/com/baeldung/jersey/server/config/HelloBinding.java new file mode 100644 index 0000000000..49b11adeab --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/server/config/HelloBinding.java @@ -0,0 +1,11 @@ +package com.baeldung.jersey.server.config; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import javax.ws.rs.NameBinding; + +@NameBinding +@Retention(RetentionPolicy.RUNTIME) +public @interface HelloBinding { +} diff --git a/jersey/src/main/java/com/baeldung/jersey/server/config/HelloDynamicBinding.java b/jersey/src/main/java/com/baeldung/jersey/server/config/HelloDynamicBinding.java new file mode 100644 index 0000000000..e56cdec140 --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/server/config/HelloDynamicBinding.java @@ -0,0 +1,31 @@ +package com.baeldung.jersey.server.config; + +import javax.ws.rs.container.DynamicFeature; +import javax.ws.rs.container.ResourceInfo; +import javax.ws.rs.core.FeatureContext; +import javax.ws.rs.ext.Provider; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.baeldung.jersey.server.Greetings; +import com.baeldung.jersey.server.filter.ResponseServerFilter; + +@Provider +public class HelloDynamicBinding implements DynamicFeature { + + private static final Logger LOG = LoggerFactory.getLogger(HelloDynamicBinding.class); + + @Override + public void configure(ResourceInfo resourceInfo, FeatureContext context) { + LOG.info("Hello dynamic binding"); + + if (Greetings.class.equals(resourceInfo.getResourceClass()) && resourceInfo.getResourceMethod() + .getName() + .contains("HiGreeting")) { + context.register(ResponseServerFilter.class); + } + + } + +} diff --git a/jersey/src/main/java/com/baeldung/jersey/server/config/ServerConfig.java b/jersey/src/main/java/com/baeldung/jersey/server/config/ServerConfig.java new file mode 100644 index 0000000000..4670cc291c --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/server/config/ServerConfig.java @@ -0,0 +1,14 @@ +package com.baeldung.jersey.server.config; + +import javax.ws.rs.ApplicationPath; + +import org.glassfish.jersey.server.ResourceConfig; + +@ApplicationPath("/*") +public class ServerConfig extends ResourceConfig { + + public ServerConfig() { + packages("com.baeldung.jersey.server"); + } + +} diff --git a/jersey/src/main/java/com/baeldung/jersey/server/filter/PrematchingRequestFilter.java b/jersey/src/main/java/com/baeldung/jersey/server/filter/PrematchingRequestFilter.java new file mode 100644 index 0000000000..181fa7f043 --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/server/filter/PrematchingRequestFilter.java @@ -0,0 +1,27 @@ +package com.baeldung.jersey.server.filter; + +import java.io.IOException; + +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.container.ContainerRequestFilter; +import javax.ws.rs.container.PreMatching; +import javax.ws.rs.ext.Provider; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Provider +@PreMatching +public class PrematchingRequestFilter implements ContainerRequestFilter { + + private static final Logger LOG = LoggerFactory.getLogger(PrematchingRequestFilter.class); + + @Override + public void filter(ContainerRequestContext ctx) throws IOException { + LOG.info("prematching filter"); + if (ctx.getMethod() + .equals("DELETE")) { + LOG.info("\"Deleting request"); + } + } +} diff --git a/jersey/src/main/java/com/baeldung/jersey/server/filter/ResponseServerFilter.java b/jersey/src/main/java/com/baeldung/jersey/server/filter/ResponseServerFilter.java new file mode 100644 index 0000000000..de0dcbdce7 --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/server/filter/ResponseServerFilter.java @@ -0,0 +1,23 @@ +package com.baeldung.jersey.server.filter; + +import java.io.IOException; + +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.container.ContainerResponseContext; +import javax.ws.rs.container.ContainerResponseFilter; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ResponseServerFilter implements ContainerResponseFilter { + + private static final Logger LOG = LoggerFactory.getLogger(ResponseServerFilter.class); + + @Override + public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { + LOG.info("Response server filter"); + + responseContext.getHeaders() + .add("X-Test", "Filter test"); + } +} diff --git a/jersey/src/main/java/com/baeldung/jersey/server/filter/RestrictedOperationsRequestFilter.java b/jersey/src/main/java/com/baeldung/jersey/server/filter/RestrictedOperationsRequestFilter.java new file mode 100644 index 0000000000..cb0cd185f7 --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/server/filter/RestrictedOperationsRequestFilter.java @@ -0,0 +1,36 @@ +package com.baeldung.jersey.server.filter; + +import java.io.IOException; + +import javax.annotation.Priority; +import javax.ws.rs.Priorities; +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.container.ContainerRequestFilter; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.Provider; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.baeldung.jersey.server.config.HelloBinding; + +@Provider +@Priority(Priorities.AUTHORIZATION) +@HelloBinding +public class RestrictedOperationsRequestFilter implements ContainerRequestFilter { + + private static final Logger LOG = LoggerFactory.getLogger(RestrictedOperationsRequestFilter.class); + + @Override + public void filter(ContainerRequestContext ctx) throws IOException { + LOG.info("Restricted operations filter"); + if (ctx.getLanguage() != null && "EN".equals(ctx.getLanguage() + .getLanguage())) { + LOG.info("Aborting request"); + ctx.abortWith(Response.status(Response.Status.FORBIDDEN) + .entity("Cannot access") + .build()); + } + + } +} diff --git a/jersey/src/main/java/com/baeldung/jersey/server/interceptor/RequestServerReaderInterceptor.java b/jersey/src/main/java/com/baeldung/jersey/server/interceptor/RequestServerReaderInterceptor.java new file mode 100644 index 0000000000..e9cc57fc2a --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/server/interceptor/RequestServerReaderInterceptor.java @@ -0,0 +1,36 @@ +package com.baeldung.jersey.server.interceptor; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.stream.Collectors; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.ext.Provider; +import javax.ws.rs.ext.ReaderInterceptor; +import javax.ws.rs.ext.ReaderInterceptorContext; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Provider +public class RequestServerReaderInterceptor implements ReaderInterceptor { + + private static final Logger LOG = LoggerFactory.getLogger(RequestServerReaderInterceptor.class); + + @Override + public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException { + LOG.info("Request reader interceptor in the server side"); + + InputStream is = context.getInputStream(); + String body = new BufferedReader(new InputStreamReader(is)).lines() + .collect(Collectors.joining("\n")); + + context.setInputStream(new ByteArrayInputStream((body + " message added in server reader interceptor").getBytes())); + + return context.proceed(); + } + +} diff --git a/jersey/src/main/resources/logback.xml b/jersey/src/main/resources/logback.xml new file mode 100644 index 0000000000..d87a87bf53 --- /dev/null +++ b/jersey/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + \ No newline at end of file diff --git a/jersey/src/test/java/com/baeldung/jersey/client/JerseyClientTest.java b/jersey/src/test/java/com/baeldung/jersey/client/JerseyClientTest.java new file mode 100644 index 0000000000..72ba4cc5b9 --- /dev/null +++ b/jersey/src/test/java/com/baeldung/jersey/client/JerseyClientTest.java @@ -0,0 +1,35 @@ +package com.baeldung.jersey.client; + +import javax.ws.rs.core.Response; + +import org.junit.Assert; +import org.junit.Ignore; +import org.junit.Test; + +@Ignore +public class JerseyClientTest { + + private static int HTTP_OK = 200; + + @Test + public void givenGreetingResource_whenCallingHelloGreeting_thenHelloReturned() { + String response = JerseyClient.getHelloGreeting(); + + Assert.assertEquals("hello", response); + } + + @Test + public void givenGreetingResource_whenCallingHiGreeting_thenHiReturned() { + String response = JerseyClient.getHiGreeting(); + + Assert.assertEquals("hi", response); + } + + @Test + public void givenGreetingResource_whenCallingCustomGreeting_thenCustomGreetingReturned() { + Response response = JerseyClient.getCustomGreeting(); + + Assert.assertEquals(HTTP_OK, response.getStatus()); + } + +} diff --git a/jgroups/README.md b/jgroups/README.md new file mode 100644 index 0000000000..bb2813c3d6 --- /dev/null +++ b/jgroups/README.md @@ -0,0 +1,15 @@ +## Reliable Messaging with JGroups Tutorial Project + +### Relevant Article: +- [Reliable Messaging with JGroups](http://www.baeldung.com/reliable-messaging-with-jgroups/) + +### Overview +This Maven project contains the Java code for the article linked above. + +### Package Organization +Java classes for the intro tutorial are in the org.baeldung.jgroups package. + + +### Running the tests + +``` diff --git a/jgroups/pom.xml b/jgroups/pom.xml new file mode 100644 index 0000000000..0e5971875e --- /dev/null +++ b/jgroups/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + jgroups + 0.1-SNAPSHOT + jar + jgroups + Reliable Messaging with JGroups Tutorial + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + org.jgroups + jgroups + 4.0.10.Final + + + + commons-cli + commons-cli + 1.4 + + + + + 1.8 + UTF-8 + + + diff --git a/jgroups/src/main/java/com/baeldung/jgroups/JGroupsMessenger.java b/jgroups/src/main/java/com/baeldung/jgroups/JGroupsMessenger.java new file mode 100644 index 0000000000..70eacabf1e --- /dev/null +++ b/jgroups/src/main/java/com/baeldung/jgroups/JGroupsMessenger.java @@ -0,0 +1,212 @@ +package com.baeldung.jgroups; + +import org.apache.commons.cli.*; +import org.jgroups.*; +import org.jgroups.util.Util; + +import java.io.*; +import java.util.List; +import java.util.Optional; + +public class JGroupsMessenger extends ReceiverAdapter { + + private JChannel channel; + private String userName; + private String clusterName; + private View lastView; + private boolean running = true; + + // Our shared state + private Integer messageCount = 0; + + /** + * Connect to a JGroups cluster using command line options + * @param args command line arguments + * @throws Exception + */ + private void start(String[] args) throws Exception { + processCommandline(args); + + // Create the channel + // This file could be moved, or made a command line option. + channel = new JChannel("src/main/resources/udp.xml"); + + // Set a name + channel.name(userName); + + // Register for callbacks + channel.setReceiver(this); + + // Ignore our messages + channel.setDiscardOwnMessages(true); + + // Connect + channel.connect(clusterName); + + // Start state transfer + channel.getState(null, 0); + + // Do the things + processInput(); + + // Clean up + channel.close(); + + } + + /** + * Quick and dirty implementaton of commons cli for command line args + * @param args the command line args + * @throws ParseException + */ + private void processCommandline(String[] args) throws ParseException { + + // Options, parser, friendly help + Options options = new Options(); + CommandLineParser parser = new DefaultParser(); + HelpFormatter formatter = new HelpFormatter(); + + options.addOption("u", "user", true, "User name") + .addOption("c", "cluster", true, "Cluster name"); + + CommandLine line = parser.parse(options, args); + + if (line.hasOption("user")) { + userName = line.getOptionValue("user"); + } else { + formatter.printHelp("JGroupsMessenger: need a user name.\n", options); + System.exit(-1); + } + + if (line.hasOption("cluster")) { + clusterName = line.getOptionValue("cluster"); + } else { + formatter.printHelp("JGroupsMessenger: need a cluster name.\n", options); + System.exit(-1); + } + } + + // Start it up + public static void main(String[] args) throws Exception { + new JGroupsMessenger().start(args); + } + + + @Override + public void viewAccepted(View newView) { + + // Save view if this is the first + if (lastView == null) { + System.out.println("Received initial view:"); + newView.forEach(System.out::println); + } else { + // Compare to last view + System.out.println("Received new view."); + + List
newMembers = View.newMembers(lastView, newView); + System.out.println("New members: "); + newMembers.forEach(System.out::println); + + List
exMembers = View.leftMembers(lastView, newView); + System.out.println("Exited members:"); + exMembers.forEach(System.out::println); + } + lastView = newView; + } + + /** + * Loop on console input until we see 'x' to exit + */ + private void processInput() throws Exception { + + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + while (running) { + try { + + // Get a destination, means broadcast + Address destination = null; + System.out.print("Enter a destination: "); + System.out.flush(); + String destinationName = in.readLine().toLowerCase(); + + if (destinationName.equals("x")) { + running = false; + continue; + } else if (!destinationName.isEmpty()) { + destination = getAddress(destinationName) + . orElseThrow(() -> new Exception("Destination not found")); + } + + // Accept a string to send + System.out.print("Enter a message: "); + System.out.flush(); + String line = in.readLine().toLowerCase(); + sendMessage(destination, line); + } catch (IOException ioe) { + running = false; + } + } + System.out.println("Exiting."); + } + + /** + * Send message from here + * @param destination the destination + * @param messageString the message + */ + private void sendMessage(Address destination, String messageString) { + try { + System.out.println("Sending " + messageString + " to " + destination); + Message message = new Message(destination, messageString); + channel.send(message); + } catch (Exception exception) { + System.err.println("Exception sending message: " + exception.getMessage()); + running = false; + } + } + + @Override + public void receive(Message message) { + // Print source and dest with message + String line = "Message received from: " + message.getSrc() + " to: " + message.getDest() + " -> " + message.getObject(); + + // Only track the count of broadcast messages + // Tracking direct message would make for a pointless state + if (message.getDest() == null) { + messageCount++; + System.out.println("Message count: " + messageCount); + } + + System.out.println(line); + } + + @Override + public void getState(OutputStream output) throws Exception { + // Serialize into the stream + Util.objectToStream(messageCount, new DataOutputStream(output)); + } + + @Override + public void setState(InputStream input) { + + // NOTE: since we know that incrementing the count and transferring the state + // is done inside the JChannel's thread, we don't have to worry about synchronizing + // messageCount. For production code it should be synchronized! + try { + // Deserialize + messageCount = Util.objectFromStream(new DataInputStream(input)); + } catch (Exception e) { + System.out.println("Error deserialing state!"); + } + System.out.println(messageCount + " is the current messagecount."); + } + + + private Optional
getAddress(String name) { + View view = channel.view(); + return view.getMembers().stream().filter(address -> name.equals(address.toString())).findFirst(); + } + +} + + diff --git a/jgroups/src/main/resources/udp.xml b/jgroups/src/main/resources/udp.xml new file mode 100644 index 0000000000..5a9bfd0851 --- /dev/null +++ b/jgroups/src/main/resources/udp.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + + diff --git a/jmeter/README.md b/jmeter/README.md index dec8364647..e3f9d1a4db 100644 --- a/jmeter/README.md +++ b/jmeter/README.md @@ -38,4 +38,9 @@ $ curl -X POST -H "Content-Type:application/json" -d '{ "firstName" : "Dassi", " Now with default configurations it will be available at: [http://localhost:8080](http://localhost:8080) -Enjoy it :) \ No newline at end of file +Enjoy it :) + +### Relevant Articles: + +- [Intro to Performance Testing using JMeter](http://www.baeldung.com/jmeter) +- [Configure Jenkins to Run and Show JMeter Tests](http://www.baeldung.com/jenkins-and-jmeter) diff --git a/json/pom.xml b/json/pom.xml index 958dd32f53..7c74d425af 100644 --- a/json/pom.xml +++ b/json/pom.xml @@ -31,6 +31,11 @@ ${fastjson.version} + + org.json + json + 20171018 + diff --git a/json/src/main/java/com/baeldung/jsonjava/CDLDemo.java b/json/src/main/java/com/baeldung/jsonjava/CDLDemo.java new file mode 100644 index 0000000000..f5fee0c4a9 --- /dev/null +++ b/json/src/main/java/com/baeldung/jsonjava/CDLDemo.java @@ -0,0 +1,59 @@ +package com.baeldung.jsonjava; + +import org.json.CDL; +import org.json.JSONArray; +import org.json.JSONTokener; + +public class CDLDemo { + public static void main(String[] args) { + System.out.println("7.1. Producing JSONArray Directly from Comma Delimited Text: "); + jsonArrayFromCDT(); + + System.out.println("\n7.2. Producing Comma Delimited Text from JSONArray: "); + cDTfromJSONArray(); + + System.out.println("\n7.3.1. Producing JSONArray of JSONObjects Using Comma Delimited Text: "); + jaOfJOFromCDT2(); + + System.out.println("\n7.3.2. Producing JSONArray of JSONObjects Using Comma Delimited Text: "); + jaOfJOFromCDT2(); + } + + public static void jsonArrayFromCDT() { + JSONArray ja = CDL.rowToJSONArray(new JSONTokener("England, USA, Canada")); + System.out.println(ja); + } + + public static void cDTfromJSONArray() { + JSONArray ja = new JSONArray("[\"England\",\"USA\",\"Canada\"]"); + String cdt = CDL.rowToString(ja); + System.out.println(cdt); + } + + public static void jaOfJOFromCDT() { + String string = + "name, city, age \n" + + "john, chicago, 22 \n" + + "gary, florida, 35 \n" + + "sal, vegas, 18"; + + JSONArray result = CDL.toJSONArray(string); + System.out.println(result.toString()); + } + + public static void jaOfJOFromCDT2() { + JSONArray ja = new JSONArray(); + ja.put("name"); + ja.put("city"); + ja.put("age"); + + String string = + "john, chicago, 22 \n" + + "gary, florida, 35 \n" + + "sal, vegas, 18"; + + JSONArray result = CDL.toJSONArray(ja, string); + System.out.println(result.toString()); + } + +} diff --git a/json/src/main/java/com/baeldung/jsonjava/CookieDemo.java b/json/src/main/java/com/baeldung/jsonjava/CookieDemo.java new file mode 100644 index 0000000000..bc39d13642 --- /dev/null +++ b/json/src/main/java/com/baeldung/jsonjava/CookieDemo.java @@ -0,0 +1,30 @@ +package com.baeldung.jsonjava; + +import org.json.Cookie; +import org.json.JSONObject; + +public class CookieDemo { + public static void main(String[] args) { + System.out.println("8.1. Converting a Cookie String into a JSONObject"); + cookieStringToJSONObject(); + + System.out.println("\n8.2. Converting a JSONObject into Cookie String"); + jSONObjectToCookieString(); + } + + public static void cookieStringToJSONObject() { + String cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/"; + JSONObject cookieJO = Cookie.toJSONObject(cookie); + System.out.println(cookieJO); + } + + public static void jSONObjectToCookieString() { + JSONObject cookieJO = new JSONObject(); + cookieJO.put("name", "username"); + cookieJO.put("value", "John Doe"); + cookieJO.put("expires", "Thu, 18 Dec 2013 12:00:00 UTC"); + cookieJO.put("path", "/"); + String cookie = Cookie.toString(cookieJO); + System.out.println(cookie); + } +} diff --git a/json/src/main/java/com/baeldung/jsonjava/DemoBean.java b/json/src/main/java/com/baeldung/jsonjava/DemoBean.java new file mode 100644 index 0000000000..6d27b329c2 --- /dev/null +++ b/json/src/main/java/com/baeldung/jsonjava/DemoBean.java @@ -0,0 +1,26 @@ +package com.baeldung.jsonjava; + +public class DemoBean { + private int id; + private String name; + private boolean active; + + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public boolean isActive() { + return active; + } + public void setActive(boolean active) { + this.active = active; + } +} diff --git a/json/src/main/java/com/baeldung/jsonjava/HTTPDemo.java b/json/src/main/java/com/baeldung/jsonjava/HTTPDemo.java new file mode 100644 index 0000000000..800018005c --- /dev/null +++ b/json/src/main/java/com/baeldung/jsonjava/HTTPDemo.java @@ -0,0 +1,27 @@ +package com.baeldung.jsonjava; + +import org.json.HTTP; +import org.json.JSONObject; + +public class HTTPDemo { + public static void main(String[] args) { + System.out.println("9.1. Converting JSONObject to HTTP Header: "); + jSONObjectToHTTPHeader(); + + System.out.println("\n9.2. Converting HTTP Header String Back to JSONObject: "); + hTTPHeaderToJSONObject(); + } + + public static void jSONObjectToHTTPHeader() { + JSONObject jo = new JSONObject(); + jo.put("Method", "POST"); + jo.put("Request-URI", "http://www.example.com/"); + jo.put("HTTP-Version", "HTTP/1.1"); + System.out.println(HTTP.toString(jo)); + } + + public static void hTTPHeaderToJSONObject() { + JSONObject obj = HTTP.toJSONObject("POST \"http://www.example.com/\" HTTP/1.1"); + System.out.println(obj); + } +} diff --git a/json/src/main/java/com/baeldung/jsonjava/JSONArrayDemo.java b/json/src/main/java/com/baeldung/jsonjava/JSONArrayDemo.java new file mode 100644 index 0000000000..2a4fab2eab --- /dev/null +++ b/json/src/main/java/com/baeldung/jsonjava/JSONArrayDemo.java @@ -0,0 +1,52 @@ +package com.baeldung.jsonjava; + +import java.util.ArrayList; +import java.util.List; + +import org.json.JSONArray; +import org.json.JSONObject; + +public class JSONArrayDemo { + public static void main(String[] args) { + System.out.println("5.1. Creating JSON Array: "); + creatingJSONArray(); + + System.out.println("\n5.2. Creating JSON Array from JSON string: "); + jsonArrayFromJSONString(); + + System.out.println("\n5.3. Creating JSON Array from Collection Object: "); + jsonArrayFromCollectionObj(); + } + + public static void creatingJSONArray() { + JSONArray ja = new JSONArray(); + ja.put(Boolean.TRUE); + ja.put("lorem ipsum"); + + // We can also put a JSONObject in JSONArray + JSONObject jo = new JSONObject(); + jo.put("name", "jon doe"); + jo.put("age", "22"); + jo.put("city", "chicago"); + + ja.put(jo); + + System.out.println(ja.toString()); + } + + public static void jsonArrayFromJSONString() { + JSONArray ja = new JSONArray("[true, \"lorem ipsum\", 215]"); + System.out.println(ja); + } + + public static void jsonArrayFromCollectionObj() { + List list = new ArrayList<>(); + list.add("California"); + list.add("Texas"); + list.add("Hawaii"); + list.add("Alaska"); + + JSONArray ja = new JSONArray(list); + System.out.println(ja); + } +} \ No newline at end of file diff --git a/json/src/main/java/com/baeldung/jsonjava/JSONObjectDemo.java b/json/src/main/java/com/baeldung/jsonjava/JSONObjectDemo.java new file mode 100644 index 0000000000..cfe8467c30 --- /dev/null +++ b/json/src/main/java/com/baeldung/jsonjava/JSONObjectDemo.java @@ -0,0 +1,59 @@ +package com.baeldung.jsonjava; + +import java.util.HashMap; +import java.util.Map; + +import org.json.JSONObject; + +public class JSONObjectDemo { + public static void main(String[] args) { + System.out.println("4.1. Creating JSONObject: "); + jsonFromJSONObject(); + + System.out.println("\n4.2. Creating JSONObject from Map: "); + jsonFromMap(); + + System.out.println("\n4.3. Creating JSONObject from JSON string: "); + jsonFromJSONString(); + + System.out.println("\n4.4. Creating JSONObject from Java Bean: "); + jsonFromDemoBean(); + } + + public static void jsonFromJSONObject() { + JSONObject jo = new JSONObject(); + jo.put("name", "jon doe"); + jo.put("age", "22"); + jo.put("city", "chicago"); + + System.out.println(jo.toString()); + } + + public static void jsonFromMap() { + Map map = new HashMap<>(); + map.put("name", "jon doe"); + map.put("age", "22"); + map.put("city", "chicago"); + JSONObject jo = new JSONObject(map); + + System.out.println(jo.toString()); + } + + public static void jsonFromJSONString() { + JSONObject jo = new JSONObject( + "{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}" + ); + + System.out.println(jo.toString()); + } + + public static void jsonFromDemoBean() { + DemoBean demo = new DemoBean(); + demo.setId(1); + demo.setName("lorem ipsum"); + demo.setActive(true); + + JSONObject jo = new JSONObject(demo); + System.out.println(jo); + } +} diff --git a/json/src/main/java/com/baeldung/jsonjava/JSONTokenerDemo.java b/json/src/main/java/com/baeldung/jsonjava/JSONTokenerDemo.java new file mode 100644 index 0000000000..fb9fea959c --- /dev/null +++ b/json/src/main/java/com/baeldung/jsonjava/JSONTokenerDemo.java @@ -0,0 +1,13 @@ +package com.baeldung.jsonjava; + +import org.json.JSONTokener; + +public class JSONTokenerDemo { + public static void main(String[] args) { + JSONTokener jt = new JSONTokener("Sample String"); + + while(jt.more()) { + System.out.println(jt.next()); + } + } +} diff --git a/json/src/test/java/com/baeldung/jsonjava/CDLIntegrationTest.java b/json/src/test/java/com/baeldung/jsonjava/CDLIntegrationTest.java new file mode 100644 index 0000000000..441c71e78e --- /dev/null +++ b/json/src/test/java/com/baeldung/jsonjava/CDLIntegrationTest.java @@ -0,0 +1,52 @@ +package com.baeldung.jsonjava; + +import static org.junit.Assert.assertEquals; + +import org.json.CDL; +import org.json.JSONArray; +import org.json.JSONTokener; +import org.junit.Test; + +public class CDLIntegrationTest { + @Test + public void givenCommaDelimitedText_thenConvertToJSONArray() { + JSONArray ja = CDL.rowToJSONArray(new JSONTokener("England, USA, Canada")); + assertEquals("[\"England\",\"USA\",\"Canada\"]", ja.toString()); + } + + @Test + public void givenJSONArray_thenConvertToCommaDelimitedText() { + JSONArray ja = new JSONArray("[\"England\",\"USA\",\"Canada\"]"); + String cdt = CDL.rowToString(ja); + assertEquals("England,USA,Canada", cdt.toString().trim()); + } + + @Test + public void givenCommaDelimitedText_thenGetJSONArrayOfJSONObjects() { + String string = + "name, city, age \n" + + "john, chicago, 22 \n" + + "gary, florida, 35 \n" + + "sal, vegas, 18"; + + JSONArray result = CDL.toJSONArray(string); + assertEquals("[{\"name\":\"john\",\"city\":\"chicago\",\"age\":\"22\"},{\"name\":\"gary\",\"city\":\"florida\",\"age\":\"35\"},{\"name\":\"sal\",\"city\":\"vegas\",\"age\":\"18\"}]", result.toString()); + } + + @Test + public void givenCommaDelimitedText_thenGetJSONArrayOfJSONObjects2() { + JSONArray ja = new JSONArray(); + ja.put("name"); + ja.put("city"); + ja.put("age"); + + String string = + "john, chicago, 22 \n" + + "gary, florida, 35 \n" + + "sal, vegas, 18"; + + JSONArray result = CDL.toJSONArray(ja, string); + assertEquals("[{\"name\":\"john\",\"city\":\"chicago\",\"age\":\"22\"},{\"name\":\"gary\",\"city\":\"florida\",\"age\":\"35\"},{\"name\":\"sal\",\"city\":\"vegas\",\"age\":\"18\"}]", result.toString()); + } + +} diff --git a/json/src/test/java/com/baeldung/jsonjava/CookieIntegrationTest.java b/json/src/test/java/com/baeldung/jsonjava/CookieIntegrationTest.java new file mode 100644 index 0000000000..c1a3505bbc --- /dev/null +++ b/json/src/test/java/com/baeldung/jsonjava/CookieIntegrationTest.java @@ -0,0 +1,29 @@ +package com.baeldung.jsonjava; + +import static org.junit.Assert.assertEquals; + +import org.json.Cookie; +import org.json.JSONObject; +import org.junit.Test; + +public class CookieIntegrationTest { + @Test + public void givenCookieString_thenConvertToJSONObject() { + String cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/"; + JSONObject cookieJO = Cookie.toJSONObject(cookie); + + assertEquals("{\"path\":\"/\",\"expires\":\"Thu, 18 Dec 2013 12:00:00 UTC\",\"name\":\"username\",\"value\":\"John Doe\"}", cookieJO.toString()); + } + + @Test + public void givenJSONObject_thenConvertToCookieString() { + JSONObject cookieJO = new JSONObject(); + cookieJO.put("name", "username"); + cookieJO.put("value", "John Doe"); + cookieJO.put("expires", "Thu, 18 Dec 2013 12:00:00 UTC"); + cookieJO.put("path", "/"); + String cookie = Cookie.toString(cookieJO); + + assertEquals("username=John Doe;expires=Thu, 18 Dec 2013 12:00:00 UTC;path=/", cookie.toString()); + } +} diff --git a/json/src/test/java/com/baeldung/jsonjava/HTTPIntegrationTest.java b/json/src/test/java/com/baeldung/jsonjava/HTTPIntegrationTest.java new file mode 100644 index 0000000000..1aa0427c7e --- /dev/null +++ b/json/src/test/java/com/baeldung/jsonjava/HTTPIntegrationTest.java @@ -0,0 +1,25 @@ +package com.baeldung.jsonjava; + +import static org.junit.Assert.assertEquals; +import org.json.HTTP; +import org.json.JSONObject; +import org.junit.Test; + +public class HTTPIntegrationTest { + @Test + public void givenJSONObject_thenConvertToHTTPHeader() { + JSONObject jo = new JSONObject(); + jo.put("Method", "POST"); + jo.put("Request-URI", "http://www.example.com/"); + jo.put("HTTP-Version", "HTTP/1.1"); + + assertEquals("POST \"http://www.example.com/\" HTTP/1.1"+HTTP.CRLF+HTTP.CRLF, HTTP.toString(jo)); + } + + @Test + public void givenHTTPHeader_thenConvertToJSONObject() { + JSONObject obj = HTTP.toJSONObject("POST \"http://www.example.com/\" HTTP/1.1"); + + assertEquals("{\"Request-URI\":\"http://www.example.com/\",\"Method\":\"POST\",\"HTTP-Version\":\"HTTP/1.1\"}", obj.toString()); + } +} diff --git a/json/src/test/java/com/baeldung/jsonjava/JSONArrayIntegrationTest.java b/json/src/test/java/com/baeldung/jsonjava/JSONArrayIntegrationTest.java new file mode 100644 index 0000000000..c956232abe --- /dev/null +++ b/json/src/test/java/com/baeldung/jsonjava/JSONArrayIntegrationTest.java @@ -0,0 +1,47 @@ +package com.baeldung.jsonjava; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; + +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.Test; + +public class JSONArrayIntegrationTest { + @Test + public void givenJSONJava_thenCreateNewJSONArrayFromScratch() { + JSONArray ja = new JSONArray(); + ja.put(Boolean.TRUE); + ja.put("lorem ipsum"); + + // We can also put a JSONObject in JSONArray + JSONObject jo = new JSONObject(); + jo.put("name", "jon doe"); + jo.put("age", "22"); + jo.put("city", "chicago"); + + ja.put(jo); + + assertEquals("[true,\"lorem ipsum\",{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}]", ja.toString()); + } + + @Test + public void givenJsonString_thenCreateNewJSONArray() { + JSONArray ja = new JSONArray("[true, \"lorem ipsum\", 215]"); + assertEquals("[true,\"lorem ipsum\",215]", ja.toString()); + } + + @Test + public void givenListObject_thenConvertItToJSONArray() { + List list = new ArrayList<>(); + list.add("California"); + list.add("Texas"); + list.add("Hawaii"); + list.add("Alaska"); + + JSONArray ja = new JSONArray(list); + assertEquals("[\"California\",\"Texas\",\"Hawaii\",\"Alaska\"]", ja.toString()); + } +} \ No newline at end of file diff --git a/json/src/test/java/com/baeldung/jsonjava/JSONObjectIntegrationTest.java b/json/src/test/java/com/baeldung/jsonjava/JSONObjectIntegrationTest.java new file mode 100644 index 0000000000..70f7921797 --- /dev/null +++ b/json/src/test/java/com/baeldung/jsonjava/JSONObjectIntegrationTest.java @@ -0,0 +1,53 @@ +package com.baeldung.jsonjava; + +import static org.junit.Assert.assertEquals; + +import java.util.HashMap; +import java.util.Map; + +import org.json.JSONObject; +import org.junit.Test; + +public class JSONObjectIntegrationTest { + @Test + public void givenJSONJava_thenCreateNewJSONObject() { + JSONObject jo = new JSONObject(); + jo.put("name", "jon doe"); + jo.put("age", "22"); + jo.put("city", "chicago"); + + assertEquals("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}", jo.toString()); + + } + + @Test + public void givenMapObject_thenCreateJSONObject() { + Map map = new HashMap<>(); + map.put("name", "jon doe"); + map.put("age", "22"); + map.put("city", "chicago"); + JSONObject jo = new JSONObject(map); + + assertEquals("{\"name\":\"jon doe\",\"city\":\"chicago\",\"age\":\"22\"}", jo.toString()); + } + + @Test + public void givenJsonString_thenCreateJSONObject() { + JSONObject jo = new JSONObject( + "{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}" + ); + + assertEquals("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}", jo.toString()); + } + + @Test + public void givenDemoBean_thenCreateJSONObject() { + DemoBean demo = new DemoBean(); + demo.setId(1); + demo.setName("lorem ipsum"); + demo.setActive(true); + + JSONObject jo = new JSONObject(demo); + assertEquals("{\"name\":\"lorem ipsum\",\"active\":true,\"id\":1}", jo.toString()); + } +} diff --git a/json/src/test/java/com/baeldung/jsonjava/JSONTokenerIntegrationTest.java b/json/src/test/java/com/baeldung/jsonjava/JSONTokenerIntegrationTest.java new file mode 100644 index 0000000000..4fe8f27231 --- /dev/null +++ b/json/src/test/java/com/baeldung/jsonjava/JSONTokenerIntegrationTest.java @@ -0,0 +1,21 @@ +package com.baeldung.jsonjava; + +import static org.junit.Assert.assertEquals; + +import org.json.JSONTokener; +import org.junit.Test; + +public class JSONTokenerIntegrationTest { + @Test + public void givenString_convertItToJSONTokens() { + String str = "Sample String"; + JSONTokener jt = new JSONTokener(str); + + char[] expectedTokens = str.toCharArray(); + int index = 0; + + while(jt.more()) { + assertEquals(expectedTokens[index++], jt.next()); + } + } +} diff --git a/libraries-data/README.md b/libraries-data/README.md index ceb0a1d5f7..9e8d32fa44 100644 --- a/libraries-data/README.md +++ b/libraries-data/README.md @@ -2,3 +2,4 @@ - [Introduction to Reladomo](http://www.baeldung.com/reladomo) - [Introduction to ORMLite](http://www.baeldung.com/ormlite) - [Introduction To Kryo](http://www.baeldung.com/kryo) +- [Introduction to KafkaStreams in Java](http://www.baeldung.com/java-kafka-streams) diff --git a/libraries/README.md b/libraries/README.md index 990a50e711..5e4ef6a898 100644 --- a/libraries/README.md +++ b/libraries/README.md @@ -58,6 +58,12 @@ - [Introduction to BouncyCastle with Java](http://www.baeldung.com/java-bouncy-castle) - [Intro to JDO Queries 2/2](http://www.baeldung.com/jdo-queries) - [Guide to google-http-client](http://www.baeldung.com/google-http-client) +- [Interact with Google Sheets from Java](http://www.baeldung.com/google-sheets-java-client) +- [Programatically Create, Configure, and Run a Tomcat Server] (http://www.baeldung.com/tomcat-programmatic-setup) +- [A Docker Guide for Java](http://www.baeldung.com/docker-java-api) +- [Exceptions in Netty](http://www.baeldung.com/netty-exception-handling) +- [Creating and Configuring Jetty 9 Server in Java](http://www.baeldung.com/jetty-java-programmatic) + The libraries module contains examples related to small libraries that are relatively easy to use and does not require any separate module of its own. diff --git a/libraries/helloWorld.docx b/libraries/helloWorld.docx index 09e71a4d4e..12eba0a6c8 100644 Binary files a/libraries/helloWorld.docx and b/libraries/helloWorld.docx differ diff --git a/libraries/log4j.properties b/libraries/log4j.properties index e69de29bb2..2173c5d96f 100644 --- a/libraries/log4j.properties +++ b/libraries/log4j.properties @@ -0,0 +1 @@ +log4j.rootLogger=INFO, stdout diff --git a/libraries/pom.xml b/libraries/pom.xml index 1fa1c30667..beeba88ff1 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -1,121 +1,22 @@ - + + 4.0.0 + libraries + libraries + parent-modules com.baeldung 1.0.0-SNAPSHOT - 4.0.0 - libraries - libraries - - - - org.apache.maven.plugins - maven-dependency-plugin - - - org.apache.felix - maven-bundle-plugin - 3.3.0 - maven-plugin - - - true - - - maven-failsafe-plugin - 2.20 - - - chromedriver - - - - - net.serenity-bdd.maven.plugins - serenity-maven-plugin - ${serenity.plugin.version} - - - serenity-reports - post-integration-test - - aggregate - - - - - - - org.datanucleus - datanucleus-maven-plugin - 5.0.2 - - JDO - ${basedir}/datanucleus.properties - ${basedir}/log4j.properties - true - false - - - - - process-classes - - enhance - - - - - - - org.apache.maven.plugins - maven-jar-plugin - 3.0.2 - - - **/log4j.properties - - - - com.baeldung.neuroph.NeurophXOR - - - - - - org.apache.maven.plugins - maven-surefire-plugin - 2.18.1 - - - test - test - - test - - - - test/java/com/baeldung/neuroph/XORTest.java - - - - - - - - maven-compiler-plugin - 3.7.0 - - 1.8 - 1.8 - - - - + + + + org.asynchttpclient + async-http-client + ${async.http.client.version} + org.beykery @@ -194,6 +95,11 @@ jetty-servlet ${jetty.version} + + org.eclipse.jetty + jetty-webapp + ${jetty.version} + rome rome @@ -629,17 +535,22 @@ ${googleclient.version} - com.google.http-client - google-http-client-jackson2 - ${googleclient.version} + com.google.http-client + google-http-client-jackson2 + ${googleclient.version} - com.google.http-client - google-http-client-gson - ${googleclient.version} + com.google.http-client + google-http-client-gson + ${googleclient.version} + + + org.infinispan + infinispan-core + ${infinispan.version} - + com.github.docker-java docker-java @@ -664,23 +575,23 @@ jersey-client 1.19.4 - + - com.google.api-client - google-api-client - ${google-api.version} + com.google.api-client + google-api-client + ${google-api.version} - com.google.oauth-client - google-oauth-client-jetty - ${google-api.version} + com.google.oauth-client + google-oauth-client-jetty + ${google-api.version} - com.google.apis - google-api-services-sheets - ${google-sheets.version} + com.google.apis + google-api-services-sheets + ${google-sheets.version} org.apache.kafka @@ -705,6 +616,13 @@ test test + + + + org.apache.tomcat + tomcat-catalina + ${tomcat.version} + org.milyn milyn-smooks-all @@ -730,6 +648,108 @@ https://repository.apache.org/content/groups/staging + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + org.apache.felix + maven-bundle-plugin + 3.3.0 + maven-plugin + + + true + + + maven-failsafe-plugin + 2.20 + + + chromedriver + + + + + net.serenity-bdd.maven.plugins + serenity-maven-plugin + ${serenity.plugin.version} + + + serenity-reports + post-integration-test + + aggregate + + + + + + + org.datanucleus + datanucleus-maven-plugin + 5.0.2 + + JDO + ${basedir}/datanucleus.properties + ${basedir}/log4j.properties + true + false + + + + + process-classes + + enhance + + + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.0.2 + + + **/log4j.properties + + + + com.baeldung.neuroph.NeurophXOR + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.18.1 + + + **/*IntegrationTest.java + **/*LiveTest.java + + + + + + + maven-compiler-plugin + 3.7.0 + + 1.8 + 1.8 + + + + + 1.23.0 0.1.0 @@ -746,12 +766,11 @@ 3.6.2 1.5.0 3.1.0 - 9.4.3.v20170317 4.5.3 2.5 1.6 1.4.196 - 9.4.2.v20170220 + 9.4.8.v20171121 4.5.3 2.5 1.2.0 @@ -762,7 +781,7 @@ 1.1.3-rc.5 1.4.0 1.1.0 - 4.1.15.Final + 4.1.20.Final 4.1 4.12 0.10 @@ -792,5 +811,9 @@ 1.0.0 1.7.0 3.0.14 + 8.5.24 + 2.2.0 + 9.1.5.Final - \ No newline at end of file + + diff --git a/libraries/src/main/java/com/baeldung/bouncycastle/BouncyCastleCrypto.java b/libraries/src/main/java/com/baeldung/bouncycastle/BouncyCastleCrypto.java index 5d8a7a6643..d7040407db 100644 --- a/libraries/src/main/java/com/baeldung/bouncycastle/BouncyCastleCrypto.java +++ b/libraries/src/main/java/com/baeldung/bouncycastle/BouncyCastleCrypto.java @@ -42,8 +42,7 @@ import org.bouncycastle.util.Store; public class BouncyCastleCrypto { - public static byte[] signData(byte[] data, final X509Certificate signingCertificate, final PrivateKey signingKey) - throws CertificateEncodingException, OperatorCreationException, CMSException, IOException { + public static byte[] signData(byte[] data, final X509Certificate signingCertificate, final PrivateKey signingKey) throws CertificateEncodingException, OperatorCreationException, CMSException, IOException { byte[] signedMessage = null; List certList = new ArrayList(); CMSTypedData cmsData = new CMSProcessableByteArray(data); @@ -51,17 +50,14 @@ public class BouncyCastleCrypto { Store certs = new JcaCertStore(certList); CMSSignedDataGenerator cmsGenerator = new CMSSignedDataGenerator(); ContentSigner contentSigner = new JcaContentSignerBuilder("SHA256withRSA").build(signingKey); - cmsGenerator.addSignerInfoGenerator( - new JcaSignerInfoGeneratorBuilder(new JcaDigestCalculatorProviderBuilder().setProvider("BC").build()) - .build(contentSigner, signingCertificate)); + cmsGenerator.addSignerInfoGenerator(new JcaSignerInfoGeneratorBuilder(new JcaDigestCalculatorProviderBuilder().setProvider("BC").build()).build(contentSigner, signingCertificate)); cmsGenerator.addCertificates(certs); CMSSignedData cms = cmsGenerator.generate(cmsData, true); signedMessage = cms.getEncoded(); return signedMessage; } - public static boolean verifSignData(final byte[] signedData) - throws CMSException, IOException, OperatorCreationException, CertificateException { + public static boolean verifSignData(final byte[] signedData) throws CMSException, IOException, OperatorCreationException, CertificateException { ByteArrayInputStream bIn = new ByteArrayInputStream(signedData); ASN1InputStream aIn = new ASN1InputStream(bIn); CMSSignedData s = new CMSSignedData(ContentInfo.getInstance(aIn.readObject())); @@ -81,16 +77,14 @@ public class BouncyCastleCrypto { return true; } - public static byte[] encryptData(final byte[] data, X509Certificate encryptionCertificate) - throws CertificateEncodingException, CMSException, IOException { + public static byte[] encryptData(final byte[] data, X509Certificate encryptionCertificate) throws CertificateEncodingException, CMSException, IOException { byte[] encryptedData = null; if (null != data && null != encryptionCertificate) { CMSEnvelopedDataGenerator cmsEnvelopedDataGenerator = new CMSEnvelopedDataGenerator(); JceKeyTransRecipientInfoGenerator jceKey = new JceKeyTransRecipientInfoGenerator(encryptionCertificate); cmsEnvelopedDataGenerator.addRecipientInfoGenerator(jceKey); CMSTypedData msg = new CMSProcessableByteArray(data); - OutputEncryptor encryptor = new JceCMSContentEncryptorBuilder(CMSAlgorithm.AES128_CBC).setProvider("BC") - .build(); + OutputEncryptor encryptor = new JceCMSContentEncryptorBuilder(CMSAlgorithm.AES128_CBC).setProvider("BC").build(); CMSEnvelopedData cmsEnvelopedData = cmsEnvelopedDataGenerator.generate(msg, encryptor); encryptedData = cmsEnvelopedData.getEncoded(); } diff --git a/libraries/src/main/java/com/baeldung/bytebuddy/Bar.java b/libraries/src/main/java/com/baeldung/bytebuddy/Bar.java index d0362a6c92..849e363c4b 100644 --- a/libraries/src/main/java/com/baeldung/bytebuddy/Bar.java +++ b/libraries/src/main/java/com/baeldung/bytebuddy/Bar.java @@ -5,12 +5,17 @@ import net.bytebuddy.implementation.bind.annotation.BindingPriority; public class Bar { @BindingPriority(3) - public static String sayHelloBar() { return "Holla in Bar!"; } + public static String sayHelloBar() { + return "Holla in Bar!"; + } @BindingPriority(2) - public static String sayBar() { return "bar"; } - - public String bar() { return Bar.class.getSimpleName() + " - Bar"; } + public static String sayBar() { + return "bar"; + } + public String bar() { + return Bar.class.getSimpleName() + " - Bar"; + } } diff --git a/libraries/src/main/java/com/baeldung/bytebuddy/Foo.java b/libraries/src/main/java/com/baeldung/bytebuddy/Foo.java index 4be06785b1..9410fc6a13 100644 --- a/libraries/src/main/java/com/baeldung/bytebuddy/Foo.java +++ b/libraries/src/main/java/com/baeldung/bytebuddy/Foo.java @@ -2,6 +2,8 @@ package com.baeldung.bytebuddy; public class Foo { - public String sayHelloFoo() { return "Hello in Foo!"; } + public String sayHelloFoo() { + return "Hello in Foo!"; + } } diff --git a/libraries/src/main/java/com/baeldung/caffeine/DataObject.java b/libraries/src/main/java/com/baeldung/caffeine/DataObject.java index a90b3e9f21..65c4c6919f 100644 --- a/libraries/src/main/java/com/baeldung/caffeine/DataObject.java +++ b/libraries/src/main/java/com/baeldung/caffeine/DataObject.java @@ -19,9 +19,7 @@ final class DataObject { @Override public String toString() { - return "DataObject{" + - "data='" + data + '\'' + - '}'; + return "DataObject{" + "data='" + data + '\'' + '}'; } public static DataObject get(String data) { diff --git a/libraries/src/main/java/com/baeldung/chronicle/queue/ChronicleQueue.java b/libraries/src/main/java/com/baeldung/chronicle/queue/ChronicleQueue.java index f6bd25c0fe..354291ebd7 100644 --- a/libraries/src/main/java/com/baeldung/chronicle/queue/ChronicleQueue.java +++ b/libraries/src/main/java/com/baeldung/chronicle/queue/ChronicleQueue.java @@ -7,9 +7,7 @@ import net.openhft.chronicle.ExcerptAppender; public class ChronicleQueue { - static void writeToQueue( - Chronicle chronicle, String stringValue, int intValue, long longValue, double doubleValue) - throws IOException { + static void writeToQueue(Chronicle chronicle, String stringValue, int intValue, long longValue, double doubleValue) throws IOException { ExcerptAppender appender = chronicle.createAppender(); appender.startExcerpt(); appender.writeUTF(stringValue); diff --git a/libraries/src/main/java/com/baeldung/commons/beanutils/CourseEntity.java b/libraries/src/main/java/com/baeldung/commons/beanutils/CourseEntity.java index 4a0b59404d..b88ee6624d 100644 --- a/libraries/src/main/java/com/baeldung/commons/beanutils/CourseEntity.java +++ b/libraries/src/main/java/com/baeldung/commons/beanutils/CourseEntity.java @@ -24,7 +24,7 @@ public class CourseEntity { public void setCodes(List codes) { this.codes = codes; } - + public void setStudent(String id, Student student) { students.put(id, student); } diff --git a/libraries/src/main/java/com/baeldung/commons/beanutils/CourseService.java b/libraries/src/main/java/com/baeldung/commons/beanutils/CourseService.java index 1f566a782a..538fa3accb 100644 --- a/libraries/src/main/java/com/baeldung/commons/beanutils/CourseService.java +++ b/libraries/src/main/java/com/baeldung/commons/beanutils/CourseService.java @@ -8,33 +8,27 @@ import org.apache.commons.beanutils.PropertyUtils; public class CourseService { - public static void setValues(Course course, String name, List codes) - throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { + public static void setValues(Course course, String name, List codes) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { // Setting the simple properties PropertyUtils.setSimpleProperty(course, "name", name); PropertyUtils.setSimpleProperty(course, "codes", codes); } - - public static void setIndexedValue(Course course, int codeIndex, String code) - throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { + + public static void setIndexedValue(Course course, int codeIndex, String code) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { // Setting the indexed properties PropertyUtils.setIndexedProperty(course, "codes[" + codeIndex + "]", code); } - public static void setMappedValue(Course course, String enrollId, Student student) - throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { + public static void setMappedValue(Course course, String enrollId, Student student) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { // Setting the mapped properties PropertyUtils.setMappedProperty(course, "enrolledStudent(" + enrollId + ")", student); } - - public static String getNestedValue(Course course, String enrollId, String nestedPropertyName) - throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { - return (String) PropertyUtils.getNestedProperty( - course, "enrolledStudent(" + enrollId + ")." + nestedPropertyName); + + public static String getNestedValue(Course course, String enrollId, String nestedPropertyName) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { + return (String) PropertyUtils.getNestedProperty(course, "enrolledStudent(" + enrollId + ")." + nestedPropertyName); } - - public static void copyProperties(Course course, CourseEntity courseEntity) - throws IllegalAccessException, InvocationTargetException { + + public static void copyProperties(Course course, CourseEntity courseEntity) throws IllegalAccessException, InvocationTargetException { BeanUtils.copyProperties(course, courseEntity); } } diff --git a/libraries/src/main/java/com/baeldung/commons/chain/AuditFilter.java b/libraries/src/main/java/com/baeldung/commons/chain/AuditFilter.java index 973e2d498e..0acb222aa1 100644 --- a/libraries/src/main/java/com/baeldung/commons/chain/AuditFilter.java +++ b/libraries/src/main/java/com/baeldung/commons/chain/AuditFilter.java @@ -7,7 +7,7 @@ public class AuditFilter implements Filter { @Override public boolean postprocess(Context context, Exception exception) { - // Send notification to customer & bank. + // Send notification to customer & bank. return false; } diff --git a/libraries/src/main/java/com/baeldung/commons/collectionutil/Customer.java b/libraries/src/main/java/com/baeldung/commons/collectionutil/Customer.java index e22f13861e..1c6a8dc4f1 100644 --- a/libraries/src/main/java/com/baeldung/commons/collectionutil/Customer.java +++ b/libraries/src/main/java/com/baeldung/commons/collectionutil/Customer.java @@ -73,7 +73,7 @@ public class Customer implements Comparable { this.name = name; this.phone = phone; } - + public Customer(String name) { super(); this.name = name; diff --git a/libraries/src/main/java/com/baeldung/commons/dbutils/Email.java b/libraries/src/main/java/com/baeldung/commons/dbutils/Email.java index c82798d52d..7f24230c43 100644 --- a/libraries/src/main/java/com/baeldung/commons/dbutils/Email.java +++ b/libraries/src/main/java/com/baeldung/commons/dbutils/Email.java @@ -20,7 +20,6 @@ public class Email { public void setEmployeeId(Integer employeeId) { this.employeeId = employeeId; } - public String getAddress() { return address; diff --git a/libraries/src/main/java/com/baeldung/commons/lang3/BuilderMethods.java b/libraries/src/main/java/com/baeldung/commons/lang3/BuilderMethods.java index 35cae7426d..74e775383b 100644 --- a/libraries/src/main/java/com/baeldung/commons/lang3/BuilderMethods.java +++ b/libraries/src/main/java/com/baeldung/commons/lang3/BuilderMethods.java @@ -26,9 +26,7 @@ public class BuilderMethods { @Override public int hashCode() { - return new HashCodeBuilder().append(this.intValue) - .append(this.strSample) - .toHashCode(); + return new HashCodeBuilder().append(this.intValue).append(this.strSample).toHashCode(); } @Override @@ -41,16 +39,12 @@ public class BuilderMethods { } final BuilderMethods otherObject = (BuilderMethods) obj; - return new EqualsBuilder().append(this.intValue, otherObject.intValue) - .append(this.strSample, otherObject.strSample) - .isEquals(); + return new EqualsBuilder().append(this.intValue, otherObject.intValue).append(this.strSample, otherObject.strSample).isEquals(); } @Override public String toString() { - return new ToStringBuilder(this).append("INTVALUE", this.intValue) - .append("STRINGVALUE", this.strSample) - .toString(); + return new ToStringBuilder(this).append("INTVALUE", this.intValue).append("STRINGVALUE", this.strSample).toString(); } public static void main(final String[] arguments) { @@ -58,21 +52,21 @@ public class BuilderMethods { System.out.println(simple1.getName()); System.out.println(simple1.hashCode()); System.out.println(simple1.toString()); - + SampleLazyInitializer sampleLazyInitializer = new SampleLazyInitializer(); - + try { sampleLazyInitializer.get(); } catch (ConcurrentException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } - + SampleBackgroundInitializer sampleBackgroundInitializer = new SampleBackgroundInitializer(); sampleBackgroundInitializer.start(); - + // Proceed with other tasks instead of waiting for the SampleBackgroundInitializer task to finish. - + try { Object result = sampleBackgroundInitializer.get(); } catch (ConcurrentException e) { @@ -81,13 +75,13 @@ public class BuilderMethods { } } -class SampleBackgroundInitializer extends BackgroundInitializer{ +class SampleBackgroundInitializer extends BackgroundInitializer { @Override protected String initialize() throws Exception { return null; } - + // Any complex task that takes some time - + } diff --git a/libraries/src/main/java/com/baeldung/commons/lang3/SampleLazyInitializer.java b/libraries/src/main/java/com/baeldung/commons/lang3/SampleLazyInitializer.java index 56a49d2659..52c6e9c9aa 100644 --- a/libraries/src/main/java/com/baeldung/commons/lang3/SampleLazyInitializer.java +++ b/libraries/src/main/java/com/baeldung/commons/lang3/SampleLazyInitializer.java @@ -3,7 +3,7 @@ package com.baeldung.commons.lang3; import org.apache.commons.lang3.concurrent.LazyInitializer; public class SampleLazyInitializer extends LazyInitializer { - + @Override protected SampleObject initialize() { return new SampleObject(); diff --git a/libraries/src/main/java/com/baeldung/commons/lang3/SampleObject.java b/libraries/src/main/java/com/baeldung/commons/lang3/SampleObject.java index 0e61176732..4595f4c6d0 100644 --- a/libraries/src/main/java/com/baeldung/commons/lang3/SampleObject.java +++ b/libraries/src/main/java/com/baeldung/commons/lang3/SampleObject.java @@ -1,7 +1,7 @@ package com.baeldung.commons.lang3; public class SampleObject { - - //Ignored + + // Ignored } diff --git a/libraries/src/main/java/com/baeldung/docx/Docx4jExample.java b/libraries/src/main/java/com/baeldung/docx/Docx4jExample.java index d9c87b3889..97fbf4adc7 100644 --- a/libraries/src/main/java/com/baeldung/docx/Docx4jExample.java +++ b/libraries/src/main/java/com/baeldung/docx/Docx4jExample.java @@ -53,16 +53,12 @@ class Docx4jExample { File image = new File(imagePath); byte[] fileContent = Files.readAllBytes(image.toPath()); - BinaryPartAbstractImage imagePart = BinaryPartAbstractImage - .createImagePart(wordPackage, fileContent); - Inline inline = imagePart.createImageInline( - "Baeldung Image", "Alt Text", 1, 2, false); + BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordPackage, fileContent); + Inline inline = imagePart.createImageInline("Baeldung Image", "Alt Text", 1, 2, false); P Imageparagraph = addImageToParagraph(inline); mainDocumentPart.getContent().add(Imageparagraph); - int writableWidthTwips = wordPackage.getDocumentModel() - .getSections().get(0).getPageDimensions() - .getWritableWidthTwips(); + int writableWidthTwips = wordPackage.getDocumentModel().getSections().get(0).getPageDimensions().getWritableWidthTwips(); int columnNumber = 3; Tbl tbl = TblFactory.createTable(3, 3, writableWidthTwips / columnNumber); List rows = tbl.getContent(); diff --git a/libraries/src/main/java/com/baeldung/eclipsecollections/ConvertContainerToAnother.java b/libraries/src/main/java/com/baeldung/eclipsecollections/ConvertContainerToAnother.java index 069baeab9f..9d1b011e0e 100644 --- a/libraries/src/main/java/com/baeldung/eclipsecollections/ConvertContainerToAnother.java +++ b/libraries/src/main/java/com/baeldung/eclipsecollections/ConvertContainerToAnother.java @@ -2,7 +2,6 @@ package com.baeldung.eclipsecollections; import java.util.List; -import org.eclipse.collections.api.list.MutableList; import org.eclipse.collections.impl.set.mutable.UnifiedSet; public class ConvertContainerToAnother { diff --git a/libraries/src/main/java/com/baeldung/fj/FunctionalJavaIOMain.java b/libraries/src/main/java/com/baeldung/fj/FunctionalJavaIOMain.java index ebf0fa4d2d..eaa201d1ba 100644 --- a/libraries/src/main/java/com/baeldung/fj/FunctionalJavaIOMain.java +++ b/libraries/src/main/java/com/baeldung/fj/FunctionalJavaIOMain.java @@ -7,7 +7,7 @@ import fj.data.IO; import fj.data.IOFunctions; public class FunctionalJavaIOMain { - + public static IO printLetters(final String s) { return () -> { for (int i = 0; i < s.length(); i++) { @@ -21,8 +21,7 @@ public class FunctionalJavaIOMain { F> printLetters = i -> printLetters(i); - IO lowerCase = IOFunctions - .stdoutPrintln("What's your first Name ?"); + IO lowerCase = IOFunctions.stdoutPrintln("What's your first Name ?"); IO input = IOFunctions.stdoutPrint("First Name: "); @@ -32,14 +31,11 @@ public class FunctionalJavaIOMain { F toUpperCase = i -> i.toUpperCase(); - F> transformInput = F1Functions - ., String> o(printLetters).f(toUpperCase); + F> transformInput = F1Functions., String> o(printLetters).f(toUpperCase); - IO readAndPrintResult = IOFunctions.bind(readInput, - transformInput); + IO readAndPrintResult = IOFunctions.bind(readInput, transformInput); - IO program = IOFunctions.bind(userInput, - nothing -> readAndPrintResult); + IO program = IOFunctions.bind(userInput, nothing -> readAndPrintResult); IOFunctions.toSafe(program).run(); diff --git a/libraries/src/main/java/com/baeldung/fj/FunctionalJavaMain.java b/libraries/src/main/java/com/baeldung/fj/FunctionalJavaMain.java index e4d731454d..c6412f2923 100644 --- a/libraries/src/main/java/com/baeldung/fj/FunctionalJavaMain.java +++ b/libraries/src/main/java/com/baeldung/fj/FunctionalJavaMain.java @@ -11,16 +11,16 @@ import fj.function.Integers; public class FunctionalJavaMain { public static final F isEven = i -> i % 2 == 0; - + public static void main(String[] args) { - + List fList = List.list(3, 4, 5, 6); List evenList = fList.map(isEven); Show.listShow(Show.booleanShow).println(evenList); - + fList = fList.map(i -> i + 1); Show.listShow(Show.intShow).println(fList); - + Array a = Array.array(17, 44, 67, 2, 22, 80, 1, 27); Array b = a.filter(Integers.even); Show.arrayShow(Show.intShow).println(b); @@ -28,11 +28,11 @@ public class FunctionalJavaMain { Array array = Array.array("Welcome", "To", "baeldung"); Boolean isExist = array.exists(s -> List.fromString(s).forall(Characters.isLowerCase)); System.out.println(isExist); - + Array intArray = Array.array(17, 44, 67, 2, 22, 80, 1, 27); int sum = intArray.foldLeft(Integers.add, 0); System.out.println(sum); - + Option n1 = Option.some(1); Option n2 = Option.some(2); diff --git a/libraries/src/main/java/com/baeldung/flink/LineSplitter.java b/libraries/src/main/java/com/baeldung/flink/LineSplitter.java index 8deeeb01c4..f4e322f1e8 100644 --- a/libraries/src/main/java/com/baeldung/flink/LineSplitter.java +++ b/libraries/src/main/java/com/baeldung/flink/LineSplitter.java @@ -13,8 +13,6 @@ public class LineSplitter implements FlatMapFunction> out) { String[] tokens = value.toLowerCase().split("\\W+"); - Stream.of(tokens) - .filter(t -> t.length() > 0) - .forEach(token -> out.collect(new Tuple2<>(token, 1))); + Stream.of(tokens).filter(t -> t.length() > 0).forEach(token -> out.collect(new Tuple2<>(token, 1))); } } \ No newline at end of file diff --git a/libraries/src/main/java/com/baeldung/flink/WordCount.java b/libraries/src/main/java/com/baeldung/flink/WordCount.java index ab109bdbce..fc5064bafa 100644 --- a/libraries/src/main/java/com/baeldung/flink/WordCount.java +++ b/libraries/src/main/java/com/baeldung/flink/WordCount.java @@ -12,9 +12,7 @@ public class WordCount { public static DataSet> startWordCount(ExecutionEnvironment env, List lines) throws Exception { DataSet text = env.fromCollection(lines); - return text.flatMap(new LineSplitter()) - .groupBy(0) - .aggregate(Aggregations.SUM, 1); + return text.flatMap(new LineSplitter()).groupBy(0).aggregate(Aggregations.SUM, 1); } } \ No newline at end of file diff --git a/libraries/src/main/java/com/baeldung/google/sheets/GoogleAuthorizeUtil.java b/libraries/src/main/java/com/baeldung/google/sheets/GoogleAuthorizeUtil.java index 650a1d084c..641fae42dd 100644 --- a/libraries/src/main/java/com/baeldung/google/sheets/GoogleAuthorizeUtil.java +++ b/libraries/src/main/java/com/baeldung/google/sheets/GoogleAuthorizeUtil.java @@ -20,21 +20,13 @@ import com.google.api.services.sheets.v4.SheetsScopes; public class GoogleAuthorizeUtil { public static Credential authorize() throws IOException, GeneralSecurityException { InputStream in = GoogleAuthorizeUtil.class.getResourceAsStream("/google-sheets-client-secret.json"); - GoogleClientSecrets clientSecrets = GoogleClientSecrets - .load(JacksonFactory.getDefaultInstance(), new InputStreamReader(in)); + GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JacksonFactory.getDefaultInstance(), new InputStreamReader(in)); List scopes = Arrays.asList(SheetsScopes.SPREADSHEETS); - GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow - .Builder(GoogleNetHttpTransport.newTrustedTransport(), - JacksonFactory.getDefaultInstance(), - clientSecrets, - scopes) - .setDataStoreFactory(new MemoryDataStoreFactory()) - .setAccessType("offline") - .build(); - Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()) - .authorize("user"); + GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), clientSecrets, scopes).setDataStoreFactory(new MemoryDataStoreFactory()) + .setAccessType("offline").build(); + Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); return credential; } diff --git a/libraries/src/main/java/com/baeldung/google/sheets/SheetsServiceUtil.java b/libraries/src/main/java/com/baeldung/google/sheets/SheetsServiceUtil.java index bbce96f389..8a78d50551 100644 --- a/libraries/src/main/java/com/baeldung/google/sheets/SheetsServiceUtil.java +++ b/libraries/src/main/java/com/baeldung/google/sheets/SheetsServiceUtil.java @@ -14,10 +14,7 @@ public class SheetsServiceUtil { public static Sheets getSheetsService() throws IOException, GeneralSecurityException { Credential credential = GoogleAuthorizeUtil.authorize(); - return new Sheets.Builder(GoogleNetHttpTransport.newTrustedTransport(), - JacksonFactory.getDefaultInstance(), credential) - .setApplicationName(APPLICATION_NAME) - .build(); + return new Sheets.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), credential).setApplicationName(APPLICATION_NAME).build(); } } diff --git a/libraries/src/main/java/com/baeldung/googlehttpclientguide/GitHubExample.java b/libraries/src/main/java/com/baeldung/googlehttpclientguide/GitHubExample.java index 0618a7294d..fce47c6ada 100644 --- a/libraries/src/main/java/com/baeldung/googlehttpclientguide/GitHubExample.java +++ b/libraries/src/main/java/com/baeldung/googlehttpclientguide/GitHubExample.java @@ -4,9 +4,7 @@ import com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler; import com.google.api.client.http.HttpRequest; import com.google.api.client.http.HttpRequestFactory; import com.google.api.client.http.HttpResponse; -import com.google.api.client.http.HttpResponseException; import com.google.api.client.http.HttpTransport; -import com.google.api.client.http.apache.ApacheHttpTransport; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.JsonObjectParser; @@ -21,30 +19,23 @@ import java.util.concurrent.Future; public class GitHubExample { static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); - //static final HttpTransport HTTP_TRANSPORT = new ApacheHttpTransport(); + // static final HttpTransport HTTP_TRANSPORT = new ApacheHttpTransport(); static final JsonFactory JSON_FACTORY = new JacksonFactory(); - //static final JsonFactory JSON_FACTORY = new GsonFactory(); + // static final JsonFactory JSON_FACTORY = new GsonFactory(); private static void run() throws Exception { - HttpRequestFactory requestFactory - = HTTP_TRANSPORT.createRequestFactory( - (HttpRequest request) -> { - request.setParser(new JsonObjectParser(JSON_FACTORY)); - }); + HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory((HttpRequest request) -> { + request.setParser(new JsonObjectParser(JSON_FACTORY)); + }); GitHubUrl url = new GitHubUrl("https://api.github.com/users"); url.per_page = 10; url.page = 1; HttpRequest request = requestFactory.buildGetRequest(url); - ExponentialBackOff backoff = new ExponentialBackOff.Builder() - .setInitialIntervalMillis(500) - .setMaxElapsedTimeMillis(900000) - .setMaxIntervalMillis(6000) - .setMultiplier(1.5) - .setRandomizationFactor(0.5) - .build(); + ExponentialBackOff backoff = new ExponentialBackOff.Builder().setInitialIntervalMillis(500).setMaxElapsedTimeMillis(900000).setMaxIntervalMillis(6000).setMultiplier(1.5).setRandomizationFactor(0.5).build(); request.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(backoff)); - Type type = new TypeToken>() {}.getType(); - List users = (List)request.execute().parseAs(type); + Type type = new TypeToken>() { + }.getType(); + List users = (List) request.execute().parseAs(type); System.out.println(users); url.appendRawPath("/eugenp"); request = requestFactory.buildGetRequest(url); diff --git a/libraries/src/main/java/com/baeldung/googlehttpclientguide/GitHubUrl.java b/libraries/src/main/java/com/baeldung/googlehttpclientguide/GitHubUrl.java index c44de1e145..ea1b83e9fb 100644 --- a/libraries/src/main/java/com/baeldung/googlehttpclientguide/GitHubUrl.java +++ b/libraries/src/main/java/com/baeldung/googlehttpclientguide/GitHubUrl.java @@ -3,16 +3,16 @@ package com.baeldung.googlehttpclientguide; import com.google.api.client.http.GenericUrl; import com.google.api.client.util.Key; -public class GitHubUrl extends GenericUrl{ +public class GitHubUrl extends GenericUrl { public GitHubUrl(String encodedUrl) { super(encodedUrl); - } - + } + @Key public int per_page; - + @Key public int page; - + } diff --git a/libraries/src/main/java/com/baeldung/googlehttpclientguide/User.java b/libraries/src/main/java/com/baeldung/googlehttpclientguide/User.java index bf4ee96b25..88361e158e 100644 --- a/libraries/src/main/java/com/baeldung/googlehttpclientguide/User.java +++ b/libraries/src/main/java/com/baeldung/googlehttpclientguide/User.java @@ -16,7 +16,7 @@ public class User extends GenericJson { private String blog; @Key private String email; - + @Key("subscriptions_url") private String subscriptionsUrl; @@ -71,7 +71,6 @@ public class User extends GenericJson { @Override public String toString() { return "User{" + "login=" + login + ", id=" + id + ", url=" + url + ", company=" + company + ", blog=" + blog + ", email=" + email + '}'; - } - - + } + } diff --git a/libraries/src/main/java/com/baeldung/hikaricp/DataSource.java b/libraries/src/main/java/com/baeldung/hikaricp/DataSource.java index ff4bc939aa..e8d3b4ff96 100644 --- a/libraries/src/main/java/com/baeldung/hikaricp/DataSource.java +++ b/libraries/src/main/java/com/baeldung/hikaricp/DataSource.java @@ -1,9 +1,7 @@ package com.baeldung.hikaricp; -import java.io.PrintWriter; import java.sql.Connection; import java.sql.SQLException; -import java.util.Properties; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; @@ -14,15 +12,15 @@ public class DataSource { private static HikariDataSource ds; static { -// config = new HikariConfig("datasource.properties"); - -// Properties props = new Properties(); -// props.setProperty("dataSourceClassName", "org.h2.Driver"); -// props.setProperty("dataSource.user", ""); -// props.setProperty("dataSource.password", ""); -// props.put("dataSource.logWriter", new PrintWriter(System.out)); -// config = new HikariConfig(props); - + // config = new HikariConfig("datasource.properties"); + + // Properties props = new Properties(); + // props.setProperty("dataSourceClassName", "org.h2.Driver"); + // props.setProperty("dataSource.user", ""); + // props.setProperty("dataSource.password", ""); + // props.put("dataSource.logWriter", new PrintWriter(System.out)); + // config = new HikariConfig(props); + config.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;INIT=runscript from 'classpath:/db.sql'"); config.setUsername(""); config.setPassword(""); @@ -30,13 +28,14 @@ public class DataSource { config.addDataSourceProperty("prepStmtCacheSize", "250"); config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); ds = new HikariDataSource(config); - -// ds.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;INIT=runscript from 'classpath:/db.sql'"); -// ds.setUsername(""); -// ds.setPassword(""); + + // ds.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;INIT=runscript from 'classpath:/db.sql'"); + // ds.setUsername(""); + // ds.setPassword(""); } - private DataSource() {} + private DataSource() { + } public static Connection getConnection() throws SQLException { return ds.getConnection(); diff --git a/libraries/src/main/java/com/baeldung/hikaricp/HikariCPDemo.java b/libraries/src/main/java/com/baeldung/hikaricp/HikariCPDemo.java index af36ab7508..57d124fd5d 100644 --- a/libraries/src/main/java/com/baeldung/hikaricp/HikariCPDemo.java +++ b/libraries/src/main/java/com/baeldung/hikaricp/HikariCPDemo.java @@ -12,9 +12,7 @@ public class HikariCPDemo { public static List fetchData() { final String SQL_QUERY = "select * from emp"; List employees = null; - try (Connection con = DataSource.getConnection(); - PreparedStatement pst = con.prepareStatement(SQL_QUERY); - ResultSet rs = pst.executeQuery();) { + try (Connection con = DataSource.getConnection(); PreparedStatement pst = con.prepareStatement(SQL_QUERY); ResultSet rs = pst.executeQuery();) { employees = new ArrayList(); Employee employee; while (rs.next()) { @@ -38,5 +36,5 @@ public class HikariCPDemo { public static void main(String[] args) { fetchData(); } - + } diff --git a/libraries/src/main/java/com/baeldung/infinispan/CacheConfiguration.java b/libraries/src/main/java/com/baeldung/infinispan/CacheConfiguration.java new file mode 100644 index 0000000000..eda511d7a7 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/infinispan/CacheConfiguration.java @@ -0,0 +1,70 @@ +package com.baeldung.infinispan; + +import com.baeldung.infinispan.listener.CacheListener; +import org.infinispan.Cache; +import org.infinispan.configuration.cache.Configuration; +import org.infinispan.configuration.cache.ConfigurationBuilder; +import org.infinispan.eviction.EvictionType; +import org.infinispan.manager.DefaultCacheManager; +import org.infinispan.transaction.LockingMode; +import org.infinispan.transaction.TransactionMode; + +import java.util.concurrent.TimeUnit; + +public class CacheConfiguration { + + public static final String SIMPLE_HELLO_WORLD_CACHE = "simple-hello-world-cache"; + public static final String EXPIRING_HELLO_WORLD_CACHE = "expiring-hello-world-cache"; + public static final String EVICTING_HELLO_WORLD_CACHE = "evicting-hello-world-cache"; + public static final String PASSIVATING_HELLO_WORLD_CACHE = "passivating-hello-world-cache"; + public static final String TRANSACTIONAL_CACHE = "transactional-cache"; + + public DefaultCacheManager cacheManager() { + return new DefaultCacheManager(); + } + + public Cache transactionalCache(DefaultCacheManager cacheManager, CacheListener listener) { + return this.buildCache(TRANSACTIONAL_CACHE, cacheManager, listener, transactionalConfiguration()); + } + + public Cache simpleHelloWorldCache(DefaultCacheManager cacheManager, CacheListener listener) { + return this.buildCache(SIMPLE_HELLO_WORLD_CACHE, cacheManager, listener, new ConfigurationBuilder().build()); + } + + public Cache expiringHelloWorldCache(DefaultCacheManager cacheManager, CacheListener listener) { + return this.buildCache(EXPIRING_HELLO_WORLD_CACHE, cacheManager, listener, expiringConfiguration()); + } + + public Cache evictingHelloWorldCache(DefaultCacheManager cacheManager, CacheListener listener) { + return this.buildCache(EVICTING_HELLO_WORLD_CACHE, cacheManager, listener, evictingConfiguration()); + } + + public Cache passivatingHelloWorldCache(DefaultCacheManager cacheManager, CacheListener listener) { + return this.buildCache(PASSIVATING_HELLO_WORLD_CACHE, cacheManager, listener, passivatingConfiguration()); + } + + private Cache buildCache(String cacheName, DefaultCacheManager cacheManager, CacheListener listener, Configuration configuration) { + + cacheManager.defineConfiguration(cacheName, configuration); + Cache cache = cacheManager.getCache(cacheName); + cache.addListener(listener); + return cache; + } + + private Configuration expiringConfiguration() { + return new ConfigurationBuilder().expiration().lifespan(1, TimeUnit.SECONDS).build(); + } + + private Configuration evictingConfiguration() { + return new ConfigurationBuilder().memory().evictionType(EvictionType.COUNT).size(1).build(); + } + + private Configuration passivatingConfiguration() { + return new ConfigurationBuilder().memory().evictionType(EvictionType.COUNT).size(1).persistence().passivation(true).addSingleFileStore().purgeOnStartup(true).location(System.getProperty("java.io.tmpdir")).build(); + } + + private Configuration transactionalConfiguration() { + return new ConfigurationBuilder().transaction().transactionMode(TransactionMode.TRANSACTIONAL).lockingMode(LockingMode.PESSIMISTIC).build(); + } + +} diff --git a/libraries/src/main/java/com/baeldung/infinispan/listener/CacheListener.java b/libraries/src/main/java/com/baeldung/infinispan/listener/CacheListener.java new file mode 100644 index 0000000000..942a2fb62d --- /dev/null +++ b/libraries/src/main/java/com/baeldung/infinispan/listener/CacheListener.java @@ -0,0 +1,53 @@ +package com.baeldung.infinispan.listener; + +import org.infinispan.notifications.Listener; +import org.infinispan.notifications.cachelistener.annotation.*; +import org.infinispan.notifications.cachelistener.event.*; + +@Listener +public class CacheListener { + + @CacheEntryCreated + public void entryCreated(CacheEntryCreatedEvent event) { + this.printLog("Adding key '" + event.getKey() + "' to cache", event); + } + + @CacheEntryExpired + public void entryExpired(CacheEntryExpiredEvent event) { + this.printLog("Expiring key '" + event.getKey() + "' from cache", event); + } + + @CacheEntryVisited + public void entryVisited(CacheEntryVisitedEvent event) { + this.printLog("Key '" + event.getKey() + "' was visited", event); + } + + @CacheEntryActivated + public void entryActivated(CacheEntryActivatedEvent event) { + this.printLog("Activating key '" + event.getKey() + "' on cache", event); + } + + @CacheEntryPassivated + public void entryPassivated(CacheEntryPassivatedEvent event) { + this.printLog("Passivating key '" + event.getKey() + "' from cache", event); + } + + @CacheEntryLoaded + public void entryLoaded(CacheEntryLoadedEvent event) { + this.printLog("Loading key '" + event.getKey() + "' to cache", event); + } + + @CacheEntriesEvicted + public void entriesEvicted(CacheEntriesEvictedEvent event) { + final StringBuilder builder = new StringBuilder(); + event.getEntries().forEach((key, value) -> builder.append(key).append(", ")); + System.out.println("Evicting following entries from cache: " + builder.toString()); + } + + private void printLog(String log, CacheEntryEvent event) { + if (!event.isPre()) { + System.out.println(log); + } + } + +} diff --git a/libraries/src/main/java/com/baeldung/infinispan/repository/HelloWorldRepository.java b/libraries/src/main/java/com/baeldung/infinispan/repository/HelloWorldRepository.java new file mode 100644 index 0000000000..85c0d539a3 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/infinispan/repository/HelloWorldRepository.java @@ -0,0 +1,15 @@ +package com.baeldung.infinispan.repository; + +public class HelloWorldRepository { + + public String getHelloWorld() { + try { + System.out.println("Executing some heavy query"); + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + return "Hello World!"; + } + +} diff --git a/libraries/src/main/java/com/baeldung/infinispan/service/HelloWorldService.java b/libraries/src/main/java/com/baeldung/infinispan/service/HelloWorldService.java new file mode 100644 index 0000000000..de30cd5c8e --- /dev/null +++ b/libraries/src/main/java/com/baeldung/infinispan/service/HelloWorldService.java @@ -0,0 +1,77 @@ +package com.baeldung.infinispan.service; + +import com.baeldung.infinispan.listener.CacheListener; +import com.baeldung.infinispan.repository.HelloWorldRepository; +import org.infinispan.Cache; + +import java.util.concurrent.TimeUnit; + +public class HelloWorldService { + + private final HelloWorldRepository repository; + + private final Cache simpleHelloWorldCache; + private final Cache expiringHelloWorldCache; + private final Cache evictingHelloWorldCache; + private final Cache passivatingHelloWorldCache; + + public HelloWorldService(HelloWorldRepository repository, CacheListener listener, Cache simpleHelloWorldCache, Cache expiringHelloWorldCache, Cache evictingHelloWorldCache, + Cache passivatingHelloWorldCache) { + + this.repository = repository; + + this.simpleHelloWorldCache = simpleHelloWorldCache; + this.expiringHelloWorldCache = expiringHelloWorldCache; + this.evictingHelloWorldCache = evictingHelloWorldCache; + this.passivatingHelloWorldCache = passivatingHelloWorldCache; + } + + public String findSimpleHelloWorld() { + String cacheKey = "simple-hello"; + return simpleHelloWorldCache.computeIfAbsent(cacheKey, k -> repository.getHelloWorld()); + } + + public String findExpiringHelloWorld() { + String cacheKey = "expiring-hello"; + String helloWorld = simpleHelloWorldCache.get(cacheKey); + if (helloWorld == null) { + helloWorld = repository.getHelloWorld(); + simpleHelloWorldCache.put(cacheKey, helloWorld, 1, TimeUnit.SECONDS); + } + return helloWorld; + } + + public String findIdleHelloWorld() { + String cacheKey = "idle-hello"; + String helloWorld = simpleHelloWorldCache.get(cacheKey); + if (helloWorld == null) { + helloWorld = repository.getHelloWorld(); + simpleHelloWorldCache.put(cacheKey, helloWorld, -1, TimeUnit.SECONDS, 10, TimeUnit.SECONDS); + } + return helloWorld; + } + + public String findSimpleHelloWorldInExpiringCache() { + String cacheKey = "simple-hello"; + String helloWorld = expiringHelloWorldCache.get(cacheKey); + if (helloWorld == null) { + helloWorld = repository.getHelloWorld(); + expiringHelloWorldCache.put(cacheKey, helloWorld); + } + return helloWorld; + } + + public String findEvictingHelloWorld(String key) { + String value = evictingHelloWorldCache.get(key); + if (value == null) { + value = repository.getHelloWorld(); + evictingHelloWorldCache.put(key, value); + } + return value; + } + + public String findPassivatingHelloWorld(String key) { + return passivatingHelloWorldCache.computeIfAbsent(key, k -> repository.getHelloWorld()); + } + +} diff --git a/libraries/src/main/java/com/baeldung/infinispan/service/TransactionalService.java b/libraries/src/main/java/com/baeldung/infinispan/service/TransactionalService.java new file mode 100644 index 0000000000..26862b8d65 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/infinispan/service/TransactionalService.java @@ -0,0 +1,55 @@ +package com.baeldung.infinispan.service; + +import org.infinispan.Cache; +import org.springframework.util.StopWatch; + +import javax.transaction.TransactionManager; + +public class TransactionalService { + + private final Cache transactionalCache; + + private static final String KEY = "key"; + + public TransactionalService(Cache transactionalCache) { + this.transactionalCache = transactionalCache; + + transactionalCache.put(KEY, 0); + } + + public Integer getQuickHowManyVisits() { + try { + TransactionManager tm = transactionalCache.getAdvancedCache().getTransactionManager(); + tm.begin(); + Integer howManyVisits = transactionalCache.get(KEY); + howManyVisits++; + System.out.println("Ill try to set HowManyVisits to " + howManyVisits); + StopWatch watch = new StopWatch(); + watch.start(); + transactionalCache.put(KEY, howManyVisits); + watch.stop(); + System.out.println("I was able to set HowManyVisits to " + howManyVisits + " after waiting " + watch.getTotalTimeSeconds() + " seconds"); + + tm.commit(); + return howManyVisits; + } catch (Exception e) { + e.printStackTrace(); + return 0; + } + } + + public void startBackgroundBatch() { + try { + TransactionManager tm = transactionalCache.getAdvancedCache().getTransactionManager(); + tm.begin(); + transactionalCache.put(KEY, 1000); + System.out.println("HowManyVisits should now be 1000, " + "but we are holding the transaction"); + Thread.sleep(1000L); + tm.rollback(); + System.out.println("The slow batch suffered a rollback"); + } catch (Exception e) { + e.printStackTrace(); + } + } + +} diff --git a/libraries/src/main/java/com/baeldung/javasisst/Point.java b/libraries/src/main/java/com/baeldung/javasisst/Point.java index 7e5c1cedd5..7f10e8c371 100644 --- a/libraries/src/main/java/com/baeldung/javasisst/Point.java +++ b/libraries/src/main/java/com/baeldung/javasisst/Point.java @@ -1,6 +1,5 @@ package com.baeldung.javasisst; - public class Point { public int x = 0; public int y = 0; diff --git a/libraries/src/main/java/com/baeldung/javasisst/ThreeDimensionalPoint.java b/libraries/src/main/java/com/baeldung/javasisst/ThreeDimensionalPoint.java index fb24d4b85d..780604738e 100644 --- a/libraries/src/main/java/com/baeldung/javasisst/ThreeDimensionalPoint.java +++ b/libraries/src/main/java/com/baeldung/javasisst/ThreeDimensionalPoint.java @@ -1,6 +1,5 @@ package com.baeldung.javasisst; - public class ThreeDimensionalPoint { public int x = 0; public int y = 0; diff --git a/libraries/src/main/java/com/baeldung/javers/Address.java b/libraries/src/main/java/com/baeldung/javers/Address.java index 14f5907ef6..9b0c119046 100644 --- a/libraries/src/main/java/com/baeldung/javers/Address.java +++ b/libraries/src/main/java/com/baeldung/javers/Address.java @@ -1,6 +1,5 @@ package com.baeldung.javers; - public class Address { private String country; diff --git a/libraries/src/main/java/com/baeldung/javers/PersonWithAddress.java b/libraries/src/main/java/com/baeldung/javers/PersonWithAddress.java index 0b4e33fcb5..16be083d83 100644 --- a/libraries/src/main/java/com/baeldung/javers/PersonWithAddress.java +++ b/libraries/src/main/java/com/baeldung/javers/PersonWithAddress.java @@ -1,6 +1,5 @@ package com.baeldung.javers; - import java.util.List; public class PersonWithAddress { diff --git a/libraries/src/main/java/com/baeldung/jdeffered/FilterDemo.java b/libraries/src/main/java/com/baeldung/jdeffered/FilterDemo.java index ec2c52d3b5..8da601b0cf 100644 --- a/libraries/src/main/java/com/baeldung/jdeffered/FilterDemo.java +++ b/libraries/src/main/java/com/baeldung/jdeffered/FilterDemo.java @@ -7,9 +7,9 @@ import org.jdeferred.impl.DeferredObject; class FilterDemo { private static String modifiedMsg; - + static String filter(String msg) { - + Deferred d = new DeferredObject<>(); Promise p = d.promise(); Promise filtered = p.then((result) -> { diff --git a/libraries/src/main/java/com/baeldung/jdeffered/PipeDemo.java b/libraries/src/main/java/com/baeldung/jdeffered/PipeDemo.java index 95250cff76..94fe0b70a6 100644 --- a/libraries/src/main/java/com/baeldung/jdeffered/PipeDemo.java +++ b/libraries/src/main/java/com/baeldung/jdeffered/PipeDemo.java @@ -19,14 +19,11 @@ class PipeDemo { p.then((DonePipe) result -> { if (result < 90) { - return new DeferredObject() - .resolve(result); + return new DeferredObject().resolve(result); } else { - return new DeferredObject() - .reject(new Exception("Unacceptable value")); + return new DeferredObject().reject(new Exception("Unacceptable value")); } - }).done(r -> status = Result.SUCCESS) - .fail(r -> status = Result.FAILURE); + }).done(r -> status = Result.SUCCESS).fail(r -> status = Result.FAILURE); d.resolve(num); diff --git a/libraries/src/main/java/com/baeldung/jdeffered/PromiseDemo.java b/libraries/src/main/java/com/baeldung/jdeffered/PromiseDemo.java index 2a9f83dc35..4efb1ad997 100644 --- a/libraries/src/main/java/com/baeldung/jdeffered/PromiseDemo.java +++ b/libraries/src/main/java/com/baeldung/jdeffered/PromiseDemo.java @@ -11,10 +11,7 @@ class PromiseDemo { Deferred deferred = new DeferredObject<>(); Promise promise = deferred.promise(); - promise.done(result -> System.out.println("Job done")) - .fail(rejection -> System.out.println("Job fail")) - .progress(progress -> System.out.println("Job is in progress")) - .always((state, result, rejection) -> System.out.println("Job execution started")); + promise.done(result -> System.out.println("Job done")).fail(rejection -> System.out.println("Job fail")).progress(progress -> System.out.println("Job is in progress")).always((state, result, rejection) -> System.out.println("Job execution started")); deferred.resolve(jobName); // deferred.notify(""); diff --git a/libraries/src/main/java/com/baeldung/jdeffered/ThreadSafeDemo.java b/libraries/src/main/java/com/baeldung/jdeffered/ThreadSafeDemo.java index 22fd51ed92..c48b916b4b 100644 --- a/libraries/src/main/java/com/baeldung/jdeffered/ThreadSafeDemo.java +++ b/libraries/src/main/java/com/baeldung/jdeffered/ThreadSafeDemo.java @@ -12,9 +12,7 @@ public class ThreadSafeDemo { DeferredManager dm = new DefaultDeferredManager(); Deferred deferred = new DeferredObject<>(); Promise p1 = deferred.promise(); - Promise p = dm.when(p1) - .done(r -> System.out.println("done")) - .fail(r -> System.out.println("fail")); + Promise p = dm.when(p1).done(r -> System.out.println("done")).fail(r -> System.out.println("fail")); synchronized (p) { while (p.isPending()) { diff --git a/libraries/src/main/java/com/baeldung/jdeffered/manager/DeferredManagerWithExecutorDemo.java b/libraries/src/main/java/com/baeldung/jdeffered/manager/DeferredManagerWithExecutorDemo.java index 2abe9bc10f..68a113d6a2 100644 --- a/libraries/src/main/java/com/baeldung/jdeffered/manager/DeferredManagerWithExecutorDemo.java +++ b/libraries/src/main/java/com/baeldung/jdeffered/manager/DeferredManagerWithExecutorDemo.java @@ -16,9 +16,7 @@ class DeferredManagerWithExecutorDemo { Deferred deferred = new DeferredObject<>(); DeferredManager dm = new DefaultDeferredManager(executor); Promise p1 = deferred.promise(), p2 = deferred.promise(), p3 = deferred.promise(); - dm.when(p1, p2, p3) - .done(r -> System.out.println("done")) - .fail(r -> System.out.println("fail")); + dm.when(p1, p2, p3).done(r -> System.out.println("done")).fail(r -> System.out.println("fail")); deferred.resolve("done"); } } diff --git a/libraries/src/main/java/com/baeldung/jdeffered/manager/SimpleDeferredManagerDemo.java b/libraries/src/main/java/com/baeldung/jdeffered/manager/SimpleDeferredManagerDemo.java index dc2e82495f..e1ffa3b6bc 100644 --- a/libraries/src/main/java/com/baeldung/jdeffered/manager/SimpleDeferredManagerDemo.java +++ b/libraries/src/main/java/com/baeldung/jdeffered/manager/SimpleDeferredManagerDemo.java @@ -7,8 +7,6 @@ class SimpleDeferredManagerDemo { public static void initiate() { DeferredManager dm = new DefaultDeferredManager(); - dm.when(() -> 1) - .done(r -> System.out.println("done")) - .fail(Throwable::printStackTrace); + dm.when(() -> 1).done(r -> System.out.println("done")).fail(Throwable::printStackTrace); } } diff --git a/libraries/src/main/java/com/baeldung/jdo/GuideToJDO.java b/libraries/src/main/java/com/baeldung/jdo/GuideToJDO.java index 387c8c4e00..bd459f963c 100644 --- a/libraries/src/main/java/com/baeldung/jdo/GuideToJDO.java +++ b/libraries/src/main/java/com/baeldung/jdo/GuideToJDO.java @@ -42,8 +42,8 @@ public class GuideToJDO { listXMLProducts(); } - public void CreateH2Properties(){ - + public void CreateH2Properties() { + pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null); pumd.addClassName("com.baeldung.jdo.Product"); pumd.setExcludeUnlistedClasses(); @@ -51,18 +51,18 @@ public class GuideToJDO { pumd.addProperty("javax.jdo.option.ConnectionURL", "jdbc:h2:mem:mypersistence"); pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa"); pumd.addProperty("javax.jdo.option.ConnectionPassword", ""); - pumd.addProperty("datanucleus.autoCreateSchema", "true"); - + pumd.addProperty("datanucleus.autoCreateSchema", "true"); + } - - public void CreateXMLProperties(){ + + public void CreateXMLProperties() { pumdXML = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null); pumdXML.addClassName("com.baeldung.jdo.ProductXML"); pumdXML.setExcludeUnlistedClasses(); pumdXML.addProperty("javax.jdo.option.ConnectionURL", "xml:file:myPersistence.xml"); - pumdXML.addProperty("datanucleus.autoCreateSchema", "true"); + pumdXML.addProperty("datanucleus.autoCreateSchema", "true"); } - + public void CreateProducts() { PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null); PersistenceManager pm = pmf.getPersistenceManager(); @@ -91,7 +91,7 @@ public class GuideToJDO { } @SuppressWarnings("rawtypes") - public void UpdateProducts(){ + public void UpdateProducts() { PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null); PersistenceManager pm = pmf.getPersistenceManager(); Transaction tx = pm.currentTransaction(); @@ -105,13 +105,13 @@ public class GuideToJDO { } finally { if (tx.isActive()) { tx.rollback(); - } + } pm.close(); } } - + @SuppressWarnings("rawtypes") - public void DeleteProducts(){ + public void DeleteProducts() { PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null); PersistenceManager pm = pmf.getPersistenceManager(); Transaction tx = pm.currentTransaction(); @@ -125,11 +125,11 @@ public class GuideToJDO { } finally { if (tx.isActive()) { tx.rollback(); - } + } pm.close(); } } - + @SuppressWarnings({ "rawtypes", "unchecked" }) public void ListProducts() { PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null); @@ -155,9 +155,9 @@ public class GuideToJDO { pm.close(); } } - + @SuppressWarnings({ "rawtypes", "unchecked" }) - public void QueryJDOQL (){ + public void QueryJDOQL() { PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null); PersistenceManager pm = pmf.getPersistenceManager(); Transaction tx = pm.currentTransaction(); @@ -177,7 +177,7 @@ public class GuideToJDO { LOGGER.log(Level.WARNING, "Product name: {0} - Price: {1}", new Object[] { p.name, p.price }); } LOGGER.log(Level.INFO, "--------------------------------------------------------------"); - + tx.commit(); } finally { if (tx.isActive()) { @@ -187,28 +187,28 @@ public class GuideToJDO { pm.close(); } } - + @SuppressWarnings({ "rawtypes", "unchecked" }) - public void QuerySQL (){ + public void QuerySQL() { PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null); PersistenceManager pm = pmf.getPersistenceManager(); Transaction tx = pm.currentTransaction(); try { tx.begin(); - //SQL : + // SQL : LOGGER.log(Level.INFO, "SQL --------------------------------------------------------------"); Query query = pm.newQuery("javax.jdo.query.SQL", "SELECT * FROM PRODUCT"); query.setClass(Product.class); List results = query.executeList(); - + Iterator iter = results.iterator(); while (iter.hasNext()) { Product p = iter.next(); LOGGER.log(Level.WARNING, "Product name: {0} - Price: {1}", new Object[] { p.name, p.price }); } LOGGER.log(Level.INFO, "--------------------------------------------------------------"); - + tx.commit(); } finally { if (tx.isActive()) { @@ -218,27 +218,27 @@ public class GuideToJDO { pm.close(); } } - + @SuppressWarnings({ "rawtypes", "unchecked" }) - public void QueryJPQL (){ + public void QueryJPQL() { PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null); PersistenceManager pm = pmf.getPersistenceManager(); Transaction tx = pm.currentTransaction(); try { tx.begin(); - //JPQL : + // JPQL : LOGGER.log(Level.INFO, "JPQL --------------------------------------------------------------"); - Query q = pm.newQuery("JPQL", "SELECT p FROM "+Product.class.getName()+" p WHERE p.name = 'Laptop'"); - List results = (List)q.execute(); - + Query q = pm.newQuery("JPQL", "SELECT p FROM " + Product.class.getName() + " p WHERE p.name = 'Laptop'"); + List results = (List) q.execute(); + Iterator iter = results.iterator(); while (iter.hasNext()) { Product p = iter.next(); LOGGER.log(Level.WARNING, "Product name: {0} - Price: {1}", new Object[] { p.name, p.price }); } LOGGER.log(Level.INFO, "--------------------------------------------------------------"); - + tx.commit(); } finally { if (tx.isActive()) { @@ -248,18 +248,18 @@ public class GuideToJDO { pm.close(); } } - - public void persistXML(){ + + public void persistXML() { PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumdXML, null); PersistenceManager pm = pmf.getPersistenceManager(); Transaction tx = pm.currentTransaction(); try { tx.begin(); - ProductXML productXML = new ProductXML(0,"Tablet", 80.0); + ProductXML productXML = new ProductXML(0, "Tablet", 80.0); pm.makePersistent(productXML); - ProductXML productXML2 = new ProductXML(1,"Phone", 20.0); + ProductXML productXML2 = new ProductXML(1, "Phone", 20.0); pm.makePersistent(productXML2); - ProductXML productXML3 = new ProductXML(2,"Laptop", 200.0); + ProductXML productXML3 = new ProductXML(2, "Laptop", 200.0); pm.makePersistent(productXML3); tx.commit(); } finally { @@ -269,9 +269,9 @@ public class GuideToJDO { pm.close(); } } - + @SuppressWarnings({ "rawtypes", "unchecked" }) - public void listXMLProducts(){ + public void listXMLProducts() { PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumdXML, null); PersistenceManager pm = pmf.getPersistenceManager(); Transaction tx = pm.currentTransaction(); diff --git a/libraries/src/main/java/com/baeldung/jdo/query/MyApp.java b/libraries/src/main/java/com/baeldung/jdo/query/MyApp.java index 384dde48d1..235142d16e 100644 --- a/libraries/src/main/java/com/baeldung/jdo/query/MyApp.java +++ b/libraries/src/main/java/com/baeldung/jdo/query/MyApp.java @@ -26,19 +26,19 @@ public class MyApp { } - public static void createTestData(){ - ProductItem item1 = new ProductItem("supportedItem", "price less than 10", "SoldOut",5); - ProductItem item2 = new ProductItem("pro2", "price less than 10","InStock", 8); - ProductItem item3 = new ProductItem("pro3", "price more than 10","SoldOut", 15); + public static void createTestData() { + ProductItem item1 = new ProductItem("supportedItem", "price less than 10", "SoldOut", 5); + ProductItem item2 = new ProductItem("pro2", "price less than 10", "InStock", 8); + ProductItem item3 = new ProductItem("pro3", "price more than 10", "SoldOut", 15); - if( pm != null ){ + if (pm != null) { pm.makePersistent(item1); pm.makePersistent(item2); - pm.makePersistent(item3); + pm.makePersistent(item3); } } - public static void defineDynamicPersistentUnit(){ + public static void defineDynamicPersistentUnit() { PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null); pumd.addProperty("javax.jdo.option.ConnectionURL", "jdbc:mysql://localhost:3306/jdo_db"); @@ -51,53 +51,45 @@ public class MyApp { pm = pmf.getPersistenceManager(); } - public static void queryUsingJDOQL(){ + public static void queryUsingJDOQL() { - Query query = pm.newQuery("SELECT FROM com.baeldung.jdo.query.ProductItem " - + "WHERE price < threshold PARAMETERS double threshold"); - List explicitParamResults = (List)query.execute(10); + Query query = pm.newQuery("SELECT FROM com.baeldung.jdo.query.ProductItem " + "WHERE price < threshold PARAMETERS double threshold"); + List explicitParamResults = (List) query.execute(10); - query = pm.newQuery("SELECT FROM " - + "com.baeldung.jdo.query.ProductItem WHERE price < :threshold"); + query = pm.newQuery("SELECT FROM " + "com.baeldung.jdo.query.ProductItem WHERE price < :threshold"); query.setParameters("double threshold"); - List explicitParamResults2 = (List)query.execute(10); + List explicitParamResults2 = (List) query.execute(10); - query = pm.newQuery("SELECT FROM " - + "com.baeldung.jdo.query.ProductItem WHERE price < :threshold"); - List implicitParamResults = (List)query.execute(10); + query = pm.newQuery("SELECT FROM " + "com.baeldung.jdo.query.ProductItem WHERE price < :threshold"); + List implicitParamResults = (List) query.execute(10); } - public static void queryUsingTypedJDOQL(){ - + public static void queryUsingTypedJDOQL() { JDOQLTypedQuery tq = pm.newJDOQLTypedQuery(ProductItem.class); QProductItem cand = QProductItem.candidate(); - tq=tq.filter(cand.price.lt(10).and(cand.name.startsWith("pro"))); + tq = tq.filter(cand.price.lt(10).and(cand.name.startsWith("pro"))); List results = tq.executeList(); } - public static void queryUsingSQL(){ + public static void queryUsingSQL() { - Query query = pm.newQuery("javax.jdo.query.SQL","select * from " - + "product_item where price < ? and status = ?"); + Query query = pm.newQuery("javax.jdo.query.SQL", "select * from " + "product_item where price < ? and status = ?"); query.setClass(ProductItem.class); - query.setParameters(10,"InStock"); + query.setParameters(10, "InStock"); List results = query.executeList(); } - public static void queryUsingJPQL(){ - Query query = pm.newQuery("JPQL","select i from " - + "com.baeldung.jdo.query.ProductItem i where i.price < 10" - + " and i.status = 'InStock'"); + public static void queryUsingJPQL() { + Query query = pm.newQuery("JPQL", "select i from " + "com.baeldung.jdo.query.ProductItem i where i.price < 10" + " and i.status = 'InStock'"); List results = (List) query.execute(); } - public static void namedQuery(){ - Query query = pm.newNamedQuery( - ProductItem.class, "PriceBelow10"); + public static void namedQuery() { + Query query = pm.newNamedQuery(ProductItem.class, "PriceBelow10"); List results = query.executeList(); } diff --git a/libraries/src/main/java/com/baeldung/jdo/query/ProductItem.java b/libraries/src/main/java/com/baeldung/jdo/query/ProductItem.java index 52221a7d97..fbe999ba2a 100644 --- a/libraries/src/main/java/com/baeldung/jdo/query/ProductItem.java +++ b/libraries/src/main/java/com/baeldung/jdo/query/ProductItem.java @@ -1,7 +1,6 @@ package com.baeldung.jdo.query; import javax.jdo.annotations.IdGeneratorStrategy; -import javax.jdo.annotations.PersistenceAware; import javax.jdo.annotations.PersistenceCapable; import javax.jdo.annotations.Persistent; import javax.jdo.annotations.PrimaryKey; @@ -10,25 +9,24 @@ import javax.jdo.annotations.PrimaryKey; public class ProductItem { @PrimaryKey - @Persistent(valueStrategy=IdGeneratorStrategy.INCREMENT) + @Persistent(valueStrategy = IdGeneratorStrategy.INCREMENT) int id; String name; String description; String status; double price; - public ProductItem(){ + public ProductItem() { } - public ProductItem(String name,String description,String status,double price){ - this.name=name; + public ProductItem(String name, String description, String status, double price) { + this.name = name; this.description = description; this.status = status; this.price = price; } - public int getId() { return id; } @@ -40,18 +38,23 @@ public class ProductItem { public String getName() { return name; } + public void setName(String name) { this.name = name; } + public String getDescription() { return description; } + public void setDescription(String description) { this.description = description; } + public double getPrice() { return price; } + public void setPrice(double price) { this.price = price; } @@ -64,5 +67,4 @@ public class ProductItem { this.status = status; } - } diff --git a/libraries/src/main/java/com/baeldung/jdo/xml/AnnotadedPerson.java b/libraries/src/main/java/com/baeldung/jdo/xml/AnnotadedPerson.java index 53e86524a5..acfc26627a 100644 --- a/libraries/src/main/java/com/baeldung/jdo/xml/AnnotadedPerson.java +++ b/libraries/src/main/java/com/baeldung/jdo/xml/AnnotadedPerson.java @@ -1,9 +1,7 @@ package com.baeldung.jdo.xml; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import javax.jdo.annotations.Element; import javax.jdo.annotations.PersistenceCapable; @@ -12,10 +10,7 @@ import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; -@PersistenceCapable( - schema="/myproduct/people", - table="person" - ) +@PersistenceCapable(schema = "/myproduct/people", table = "person") public class AnnotadedPerson { @XmlAttribute private long personNum; @@ -24,12 +19,11 @@ public class AnnotadedPerson { private String firstName; private String lastName; - @XmlElementWrapper(name="phone-numbers") - @XmlElement(name="phone-number") - @Element(types=String.class) + @XmlElementWrapper(name = "phone-numbers") + @XmlElement(name = "phone-number") + @Element(types = String.class) private List phoneNumbers = new ArrayList(); - public AnnotadedPerson(long personNum, String firstName, String lastName) { super(); this.personNum = personNum; diff --git a/libraries/src/main/java/com/baeldung/jdo/xml/MyApp.java b/libraries/src/main/java/com/baeldung/jdo/xml/MyApp.java index 97ec49eec1..c75d3695f7 100644 --- a/libraries/src/main/java/com/baeldung/jdo/xml/MyApp.java +++ b/libraries/src/main/java/com/baeldung/jdo/xml/MyApp.java @@ -17,35 +17,35 @@ public class MyApp { private static PersistenceManagerFactory pmf; private static PersistenceManager pm; - public static void main( String[] args ) { - - //persist product object using dynamic persistence unit + public static void main(String[] args) { + + // persist product object using dynamic persistence unit defineDynamicPersistentUnit(); - Product product = new Product("id1","Sony Discman", "A standard discman from Sony", 49.99); + Product product = new Product("id1", "Sony Discman", "A standard discman from Sony", 49.99); persistObject(product); closePersistenceManager(); - - //persist AnnotatedPerson object using named pmf + + // persist AnnotatedPerson object using named pmf defineNamedPersistenceManagerFactory("XmlDatastore"); - AnnotadedPerson annotatedPerson = new AnnotadedPerson(654320,"annotated","person"); + AnnotadedPerson annotatedPerson = new AnnotadedPerson(654320, "annotated", "person"); annotatedPerson.getPhoneNumbers().add("999999999"); annotatedPerson.getPhoneNumbers().add("000000000"); persistObject(annotatedPerson); queryAnnotatedPersonsInXML(); closePersistenceManager(); - - //persist Person object using PMF created by properties file + + // persist Person object using PMF created by properties file definePersistenceManagerFactoryUsingPropertiesFile("META-INF\\datanucleus.properties"); - Person person = new Person(654321,"bealdung","author"); + Person person = new Person(654321, "bealdung", "author"); person.getPhoneNumbers().add("123456789"); person.getPhoneNumbers().add("987654321"); persistObject(person); queryPersonsInXML(); closePersistenceManager(); - } + } + + public static void defineDynamicPersistentUnit() { - public static void defineDynamicPersistentUnit(){ - PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null); pumd.addProperty("javax.jdo.option.ConnectionURL", "xml:file:myfile_dynamicPMF.xml"); pumd.addProperty("datanucleus.schema.autoCreateAll", "true"); @@ -55,27 +55,27 @@ public class MyApp { pm = pmf.getPersistenceManager(); } - public static void defineNamedPersistenceManagerFactory(String pmfName){ - + public static void defineNamedPersistenceManagerFactory(String pmfName) { + pmf = JDOHelper.getPersistenceManagerFactory("XmlDatastore"); pm = pmf.getPersistenceManager(); } - public static void definePersistenceManagerFactoryUsingPropertiesFile(String filePath){ - + public static void definePersistenceManagerFactoryUsingPropertiesFile(String filePath) { + pmf = JDOHelper.getPersistenceManagerFactory(filePath); pm = pmf.getPersistenceManager(); } - public static void closePersistenceManager(){ - - if(pm!=null && !pm.isClosed()){ + public static void closePersistenceManager() { + + if (pm != null && !pm.isClosed()) { pm.close(); } } - public static void persistObject(Object obj){ - + public static void persistObject(Object obj) { + Transaction tx = pm.currentTransaction(); try { @@ -88,18 +88,18 @@ public class MyApp { } } } - - public static void queryPersonsInXML(){ - + + public static void queryPersonsInXML() { + Query query = pm.newQuery(Person.class); List result = query.executeList(); - System.out.println("name: "+result.get(0).getFirstName()); + System.out.println("name: " + result.get(0).getFirstName()); } - - public static void queryAnnotatedPersonsInXML(){ - + + public static void queryAnnotatedPersonsInXML() { + Query query = pm.newQuery(AnnotadedPerson.class); List result = query.executeList(); - System.out.println("name: "+result.get(0).getFirstName()); + System.out.println("name: " + result.get(0).getFirstName()); } } diff --git a/libraries/src/main/java/com/baeldung/jdo/xml/Person.java b/libraries/src/main/java/com/baeldung/jdo/xml/Person.java index e3ec5c6bab..0678201afd 100644 --- a/libraries/src/main/java/com/baeldung/jdo/xml/Person.java +++ b/libraries/src/main/java/com/baeldung/jdo/xml/Person.java @@ -1,17 +1,10 @@ package com.baeldung.jdo.xml; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import javax.jdo.annotations.Element; import javax.jdo.annotations.PersistenceCapable; import javax.jdo.annotations.PrimaryKey; -import javax.xml.bind.annotation.XmlAttribute; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlElementWrapper; - @PersistenceCapable public class Person { diff --git a/libraries/src/main/java/com/baeldung/jdo/xml/Product.java b/libraries/src/main/java/com/baeldung/jdo/xml/Product.java index d8d3bb17b2..1e46f212cb 100644 --- a/libraries/src/main/java/com/baeldung/jdo/xml/Product.java +++ b/libraries/src/main/java/com/baeldung/jdo/xml/Product.java @@ -1,9 +1,6 @@ package com.baeldung.jdo.xml; -import javax.jdo.annotations.IdGeneratorStrategy; -import javax.jdo.annotations.PersistenceAware; import javax.jdo.annotations.PersistenceCapable; -import javax.jdo.annotations.Persistent; import javax.jdo.annotations.PrimaryKey; @PersistenceCapable @@ -14,19 +11,18 @@ public class Product { String name; String description; double price; - - public Product(){ - + + public Product() { + } - - public Product(String id,String name,String description,double price){ + + public Product(String id, String name, String description, double price) { this.id = id; - this.name=name; + this.name = name; this.description = description; this.price = price; } - - + public String getId() { return id; } @@ -38,21 +34,25 @@ public class Product { public String getName() { return name; } + public void setName(String name) { this.name = name; } + public String getDescription() { return description; } + public void setDescription(String description) { this.description = description; } + public double getPrice() { return price; } + public void setPrice(double price) { this.price = price; } - - + } diff --git a/libraries/src/main/java/com/baeldung/jetty/BlockingServlet.java b/libraries/src/main/java/com/baeldung/jetty/BlockingServlet.java index f1de71beeb..6bc73b055a 100644 --- a/libraries/src/main/java/com/baeldung/jetty/BlockingServlet.java +++ b/libraries/src/main/java/com/baeldung/jetty/BlockingServlet.java @@ -14,4 +14,3 @@ public class BlockingServlet extends HttpServlet { response.getWriter().println("{ \"status\": \"ok\"}"); } } - diff --git a/libraries/src/main/java/com/baeldung/jetty/JettyServer.java b/libraries/src/main/java/com/baeldung/jetty/JettyServer.java index 364b05473a..82428642c1 100644 --- a/libraries/src/main/java/com/baeldung/jetty/JettyServer.java +++ b/libraries/src/main/java/com/baeldung/jetty/JettyServer.java @@ -21,7 +21,7 @@ class JettyServer { server = new Server(threadPool); ServerConnector connector = new ServerConnector(server); connector.setPort(8090); - server.setConnectors(new Connector[]{connector}); + server.setConnectors(new Connector[] { connector }); ServletHandler servletHandler = new ServletHandler(); server.setHandler(servletHandler); diff --git a/libraries/src/main/java/com/baeldung/jetty/JettyServerFactory.java b/libraries/src/main/java/com/baeldung/jetty/JettyServerFactory.java new file mode 100644 index 0000000000..46a2e8102a --- /dev/null +++ b/libraries/src/main/java/com/baeldung/jetty/JettyServerFactory.java @@ -0,0 +1,92 @@ +package com.baeldung.jetty; + +import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.server.handler.HandlerCollection; +import org.eclipse.jetty.webapp.WebAppContext; + +/** + * Simple factory for creating Jetty basic instances. + * + * @author Donato Rimenti + * + */ +public class JettyServerFactory { + + /** + * Exposed context of the app. + */ + public final static String APP_PATH = "/myApp"; + + /** + * The server port. + */ + public final static int SERVER_PORT = 13133; + + /** + * Private constructor to avoid instantiation. + */ + private JettyServerFactory() { + } + + /** + * Returns a simple server listening on port 80 with a timeout of 30 seconds + * for connections and no handlers. + * + * @return a server + */ + public static Server createBaseServer() { + Server server = new Server(); + + // Adds a connector for port 80 with a timeout of 30 seconds. + ServerConnector connector = new ServerConnector(server); + connector.setPort(SERVER_PORT); + connector.setHost("127.0.0.1"); + connector.setIdleTimeout(30000); + server.addConnector(connector); + + return server; + } + + /** + * Creates a server which delegates the request handling to a web + * application. + * + * @return a server + */ + public static Server createWebAppServer() { + // Adds an handler to a server and returns it. + Server server = createBaseServer(); + String webAppFolderPath = JettyServerFactory.class.getClassLoader().getResource("jetty-embedded-demo-app.war").getPath(); + Handler webAppHandler = new WebAppContext(webAppFolderPath, APP_PATH); + server.setHandler(webAppHandler); + + return server; + } + + /** + * Creates a server which delegates the request handling to both a logging + * handler and to a web application, in this order. + * + * @return a server + */ + public static Server createMultiHandlerServer() { + Server server = createBaseServer(); + + // Creates the handlers and adds them to the server. + HandlerCollection handlers = new HandlerCollection(); + + String webAppFolderPath = JettyServerFactory.class.getClassLoader().getResource("jetty-embedded-demo-app.war").getPath(); + Handler customRequestHandler = new WebAppContext(webAppFolderPath, APP_PATH); + handlers.addHandler(customRequestHandler); + + Handler loggingRequestHandler = new LoggingRequestHandler(); + handlers.addHandler(loggingRequestHandler); + + server.setHandler(handlers); + + return server; + } + +} \ No newline at end of file diff --git a/libraries/src/main/java/com/baeldung/jetty/LoggingRequestHandler.java b/libraries/src/main/java/com/baeldung/jetty/LoggingRequestHandler.java new file mode 100644 index 0000000000..a5c6d09c16 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/jetty/LoggingRequestHandler.java @@ -0,0 +1,167 @@ +package com.baeldung.jetty; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.Server; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Handler implementation which simply logs that a request has been received. + * + * @author Donato Rimenti + */ +public class LoggingRequestHandler implements Handler { + + /** + * Logger. + */ + private final static Logger LOG = LoggerFactory.getLogger(LoggingRequestHandler.class); + + /* + * (non-Javadoc) + * + * @see org.eclipse.jetty.util.component.LifeCycle#addLifeCycleListener(org. + * eclipse.jetty.util.component.LifeCycle.Listener) + */ + @Override + public void addLifeCycleListener(Listener arg0) { + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jetty.util.component.LifeCycle#isFailed() + */ + @Override + public boolean isFailed() { + return false; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jetty.util.component.LifeCycle#isRunning() + */ + @Override + public boolean isRunning() { + return true; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jetty.util.component.LifeCycle#isStarted() + */ + @Override + public boolean isStarted() { + return true; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jetty.util.component.LifeCycle#isStarting() + */ + @Override + public boolean isStarting() { + return false; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jetty.util.component.LifeCycle#isStopped() + */ + @Override + public boolean isStopped() { + return false; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jetty.util.component.LifeCycle#isStopping() + */ + @Override + public boolean isStopping() { + return false; + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.jetty.util.component.LifeCycle#removeLifeCycleListener(org. + * eclipse.jetty.util.component.LifeCycle.Listener) + */ + @Override + public void removeLifeCycleListener(Listener arg0) { + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jetty.util.component.LifeCycle#start() + */ + @Override + public void start() throws Exception { + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jetty.util.component.LifeCycle#stop() + */ + @Override + public void stop() throws Exception { + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jetty.server.Handler#destroy() + */ + @Override + public void destroy() { + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jetty.server.Handler#getServer() + */ + @Override + public Server getServer() { + return null; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jetty.server.Handler#handle(java.lang.String, + * org.eclipse.jetty.server.Request, javax.servlet.http.HttpServletRequest, + * javax.servlet.http.HttpServletResponse) + */ + @Override + public void handle(String arg0, Request arg1, HttpServletRequest arg2, HttpServletResponse arg3) throws IOException, ServletException { + LOG.info("Received a new request"); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jetty.server.Handler#setServer(org.eclipse.jetty.server. + * Server) + */ + @Override + public void setServer(Server server) { + } + +} diff --git a/libraries/src/main/java/com/baeldung/netty/ChannelHandlerA.java b/libraries/src/main/java/com/baeldung/netty/ChannelHandlerA.java new file mode 100644 index 0000000000..c919bdb09c --- /dev/null +++ b/libraries/src/main/java/com/baeldung/netty/ChannelHandlerA.java @@ -0,0 +1,22 @@ +package com.baeldung.netty; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; + +import java.util.logging.Logger; + +public class ChannelHandlerA extends ChannelInboundHandlerAdapter { + + private Logger logger = Logger.getLogger(getClass().getName()); + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + throw new Exception("Ooops"); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + logger.info("Exception Occurred in ChannelHandler A"); + ctx.fireExceptionCaught(cause); + } +} diff --git a/libraries/src/main/java/com/baeldung/netty/ChannelHandlerB.java b/libraries/src/main/java/com/baeldung/netty/ChannelHandlerB.java new file mode 100644 index 0000000000..abb6bf7dd9 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/netty/ChannelHandlerB.java @@ -0,0 +1,19 @@ +package com.baeldung.netty; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; + +import java.util.logging.Logger; + +public class ChannelHandlerB extends ChannelInboundHandlerAdapter { + + private Logger logger = Logger.getLogger(getClass().getName()); + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + logger.info("Exception Handled in ChannelHandler B"); + logger.info(cause.getLocalizedMessage()); + // do more exception handling + ctx.close(); + } +} diff --git a/libraries/src/main/java/com/baeldung/netty/NettyServerB.java b/libraries/src/main/java/com/baeldung/netty/NettyServerB.java new file mode 100644 index 0000000000..49a6aa6bfd --- /dev/null +++ b/libraries/src/main/java/com/baeldung/netty/NettyServerB.java @@ -0,0 +1,43 @@ +package com.baeldung.netty; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioServerSocketChannel; + +public class NettyServerB { + + private int port; + + private NettyServerB(int port) { + this.port = port; + } + + private void run() throws Exception { + + EventLoopGroup bossGroup = new NioEventLoopGroup(); + EventLoopGroup workerGroup = new NioEventLoopGroup(); + + try { + ServerBootstrap b = new ServerBootstrap(); + b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer() { + public void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast(new ChannelHandlerA(), new ChannelHandlerB()); + } + }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); + ChannelFuture f = b.bind(port).sync(); // (7) + f.channel().closeFuture().sync(); + } finally { + workerGroup.shutdownGracefully(); + bossGroup.shutdownGracefully(); + } + } + + public static void main(String[] args) throws Exception { + new NettyServerB(8080).run(); + } +} diff --git a/libraries/src/main/java/com/baeldung/netty/RequestData.java b/libraries/src/main/java/com/baeldung/netty/RequestData.java index 402aa1ef91..475c0a4dc1 100644 --- a/libraries/src/main/java/com/baeldung/netty/RequestData.java +++ b/libraries/src/main/java/com/baeldung/netty/RequestData.java @@ -22,9 +22,6 @@ public class RequestData { @Override public String toString() { - return "RequestData{" + - "intValue=" + intValue + - ", stringValue='" + stringValue + '\'' + - '}'; + return "RequestData{" + "intValue=" + intValue + ", stringValue='" + stringValue + '\'' + '}'; } } diff --git a/libraries/src/main/java/com/baeldung/netty/ResponseData.java b/libraries/src/main/java/com/baeldung/netty/ResponseData.java index 51d1adaafb..8849e8a4cb 100644 --- a/libraries/src/main/java/com/baeldung/netty/ResponseData.java +++ b/libraries/src/main/java/com/baeldung/netty/ResponseData.java @@ -13,8 +13,6 @@ public class ResponseData { @Override public String toString() { - return "ResponseData{" + - "intValue=" + intValue + - '}'; + return "ResponseData{" + "intValue=" + intValue + '}'; } } diff --git a/libraries/src/main/java/com/baeldung/neuroph/NeurophXOR.java b/libraries/src/main/java/com/baeldung/neuroph/NeurophXOR.java index fb6a01d4c1..4cb11c3c05 100644 --- a/libraries/src/main/java/com/baeldung/neuroph/NeurophXOR.java +++ b/libraries/src/main/java/com/baeldung/neuroph/NeurophXOR.java @@ -41,7 +41,7 @@ public class NeurophXOR { ConnectionFactory.fullConnect(ann.getLayerAt(1), ann.getLayerAt(2)); ann.addLayer(3, outputLayer); ConnectionFactory.fullConnect(ann.getLayerAt(2), ann.getLayerAt(3)); - ConnectionFactory.fullConnect(ann.getLayerAt(0), ann.getLayerAt(ann.getLayersCount()-1), false); + ConnectionFactory.fullConnect(ann.getLayerAt(0), ann.getLayerAt(ann.getLayersCount() - 1), false); ann.setInputNeurons(inputLayer.getNeurons()); ann.setOutputNeurons(outputLayer.getNeurons()); @@ -55,13 +55,13 @@ public class NeurophXOR { int outputSize = 1; DataSet ds = new DataSet(inputSize, outputSize); - DataSetRow rOne = new DataSetRow(new double[] {0, 1}, new double[] {1}); + DataSetRow rOne = new DataSetRow(new double[] { 0, 1 }, new double[] { 1 }); ds.addRow(rOne); - DataSetRow rTwo = new DataSetRow(new double[] {1, 1}, new double[] {0}); + DataSetRow rTwo = new DataSetRow(new double[] { 1, 1 }, new double[] { 0 }); ds.addRow(rTwo); - DataSetRow rThree = new DataSetRow(new double[] {0, 0}, new double[] {0}); + DataSetRow rThree = new DataSetRow(new double[] { 0, 0 }, new double[] { 0 }); ds.addRow(rThree); - DataSetRow rFour = new DataSetRow(new double[] {1, 0}, new double[] {1}); + DataSetRow rFour = new DataSetRow(new double[] { 1, 0 }, new double[] { 1 }); ds.addRow(rFour); BackPropagation backPropagation = new BackPropagation(); diff --git a/libraries/src/main/java/com/baeldung/noexception/CustomExceptionHandler.java b/libraries/src/main/java/com/baeldung/noexception/CustomExceptionHandler.java index 59e13efaa0..48abe35287 100644 --- a/libraries/src/main/java/com/baeldung/noexception/CustomExceptionHandler.java +++ b/libraries/src/main/java/com/baeldung/noexception/CustomExceptionHandler.java @@ -11,10 +11,7 @@ public class CustomExceptionHandler extends ExceptionHandler { @Override public boolean handle(Throwable throwable) { - if (throwable.getClass() - .isAssignableFrom(RuntimeException.class) - || throwable.getClass() - .isAssignableFrom(Error.class)) { + if (throwable.getClass().isAssignableFrom(RuntimeException.class) || throwable.getClass().isAssignableFrom(Error.class)) { return false; } else { logger.error("Caught Exception ", throwable); diff --git a/libraries/src/main/java/com/baeldung/protonpack/StreamUtilsExample.java b/libraries/src/main/java/com/baeldung/protonpack/StreamUtilsExample.java index eead34af71..b872696510 100644 --- a/libraries/src/main/java/com/baeldung/protonpack/StreamUtilsExample.java +++ b/libraries/src/main/java/com/baeldung/protonpack/StreamUtilsExample.java @@ -20,18 +20,14 @@ public class StreamUtilsExample { public void zipAStreamWithIndex() { Stream source = Stream.of("Foo", "Bar", "Baz"); - List> zipped = StreamUtils - .zipWithIndex(source) - .collect(Collectors.toList()); + List> zipped = StreamUtils.zipWithIndex(source).collect(Collectors.toList()); } public void zipAPairOfStreams() { Stream streamA = Stream.of("A", "B", "C"); Stream streamB = Stream.of("Apple", "Banana", "Carrot"); - List zipped = StreamUtils - .zip(streamA, streamB, (a, b) -> a + " is for " + b) - .collect(Collectors.toList()); + List zipped = StreamUtils.zip(streamA, streamB, (a, b) -> a + " is for " + b).collect(Collectors.toList()); } public void zipThreeStreams() { @@ -39,9 +35,7 @@ public class StreamUtilsExample { Stream streamB = Stream.of("aggravating", "banausic", "complaisant"); Stream streamC = Stream.of("Apple", "Banana", "Carrot"); - List zipped = StreamUtils - .zip(streamA, streamB, streamC, (a, b, c) -> a + " is for " + b + " " + c) - .collect(Collectors.toList()); + List zipped = StreamUtils.zip(streamA, streamB, streamC, (a, b, c) -> a + " is for " + b + " " + c).collect(Collectors.toList()); } public void mergeThreeStreams() { @@ -79,24 +73,16 @@ public class StreamUtilsExample { public void windowedStream() { Stream integerStream = Stream.of(1, 2, 3, 4, 5); - List> windows = StreamUtils - .windowed(integerStream, 2) - .collect(toList()); - List> windowsWithSkipIndex = StreamUtils - .windowed(integerStream, 3, 2) - .collect(toList()); - List> windowsWithSkipIndexAndAllowLowerSize = StreamUtils - .windowed(integerStream, 2, 2, true) - .collect(toList()); + List> windows = StreamUtils.windowed(integerStream, 2).collect(toList()); + List> windowsWithSkipIndex = StreamUtils.windowed(integerStream, 3, 2).collect(toList()); + List> windowsWithSkipIndexAndAllowLowerSize = StreamUtils.windowed(integerStream, 2, 2, true).collect(toList()); } public void groupRunsStreams() { Stream integerStream = Stream.of(1, 1, 2, 2, 3, 4, 5); - List> runs = StreamUtils - .groupRuns(integerStream) - .collect(toList()); + List> runs = StreamUtils.groupRuns(integerStream).collect(toList()); } public void aggreagateOnBiElementPredicate() { diff --git a/libraries/src/main/java/com/baeldung/quartz/QuartzExample.java b/libraries/src/main/java/com/baeldung/quartz/QuartzExample.java index 4757d912f8..b55517b6d1 100644 --- a/libraries/src/main/java/com/baeldung/quartz/QuartzExample.java +++ b/libraries/src/main/java/com/baeldung/quartz/QuartzExample.java @@ -19,45 +19,17 @@ public class QuartzExample { Scheduler sched = schedFact.getScheduler(); - JobDetail job = JobBuilder.newJob(SimpleJob.class) - .withIdentity("myJob", "group1") - .usingJobData("jobSays", "Hello World!") - .usingJobData("myFloatValue", 3.141f) - .build(); + JobDetail job = JobBuilder.newJob(SimpleJob.class).withIdentity("myJob", "group1").usingJobData("jobSays", "Hello World!").usingJobData("myFloatValue", 3.141f).build(); - Trigger trigger = TriggerBuilder.newTrigger() - .withIdentity("myTrigger", "group1") - .startNow() - .withSchedule(SimpleScheduleBuilder.simpleSchedule() - .withIntervalInSeconds(40) - .repeatForever()) - .build(); + Trigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger", "group1").startNow().withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(40).repeatForever()).build(); - JobDetail jobA = JobBuilder.newJob(JobA.class) - .withIdentity("jobA", "group2") - .build(); + JobDetail jobA = JobBuilder.newJob(JobA.class).withIdentity("jobA", "group2").build(); - JobDetail jobB = JobBuilder.newJob(JobB.class) - .withIdentity("jobB", "group2") - .build(); + JobDetail jobB = JobBuilder.newJob(JobB.class).withIdentity("jobB", "group2").build(); - Trigger triggerA = TriggerBuilder.newTrigger() - .withIdentity("triggerA", "group2") - .startNow() - .withPriority(15) - .withSchedule(SimpleScheduleBuilder.simpleSchedule() - .withIntervalInSeconds(40) - .repeatForever()) - .build(); + Trigger triggerA = TriggerBuilder.newTrigger().withIdentity("triggerA", "group2").startNow().withPriority(15).withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(40).repeatForever()).build(); - Trigger triggerB = TriggerBuilder.newTrigger() - .withIdentity("triggerB", "group2") - .startNow() - .withPriority(10) - .withSchedule(SimpleScheduleBuilder.simpleSchedule() - .withIntervalInSeconds(20) - .repeatForever()) - .build(); + Trigger triggerB = TriggerBuilder.newTrigger().withIdentity("triggerB", "group2").startNow().withPriority(10).withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(20).repeatForever()).build(); sched.scheduleJob(job, trigger); sched.scheduleJob(jobA, triggerA); diff --git a/libraries/src/main/java/com/baeldung/quartz/SimpleJob.java b/libraries/src/main/java/com/baeldung/quartz/SimpleJob.java index 554d3b9358..03730ee6e5 100644 --- a/libraries/src/main/java/com/baeldung/quartz/SimpleJob.java +++ b/libraries/src/main/java/com/baeldung/quartz/SimpleJob.java @@ -8,8 +8,7 @@ import org.quartz.JobExecutionException; public class SimpleJob implements Job { public void execute(JobExecutionContext context) throws JobExecutionException { - JobDataMap dataMap = context.getJobDetail() - .getJobDataMap(); + JobDataMap dataMap = context.getJobDetail().getJobDataMap(); String jobSays = dataMap.getString("jobSays"); float myFloatValue = dataMap.getFloat("myFloatValue"); diff --git a/libraries/src/main/java/com/baeldung/retrofit/basic/GitHubBasicApi.java b/libraries/src/main/java/com/baeldung/retrofit/basic/GitHubBasicApi.java index 4e071d3384..2b5c1f6f62 100644 --- a/libraries/src/main/java/com/baeldung/retrofit/basic/GitHubBasicApi.java +++ b/libraries/src/main/java/com/baeldung/retrofit/basic/GitHubBasicApi.java @@ -18,7 +18,7 @@ public interface GitHubBasicApi { */ @GET("users/{user}/repos") Call> listRepos(@Path("user") String user); - + /** * List Contributors of a GitHub Repository * @param user GitHub Account @@ -26,8 +26,6 @@ public interface GitHubBasicApi { * @return GitHub Repository Contributors */ @GET("repos/{user}/{repo}/contributors") - Call> listRepoContributors( - @Path("user") String user, - @Path("repo") String repo); - + Call> listRepoContributors(@Path("user") String user, @Path("repo") String repo); + } diff --git a/libraries/src/main/java/com/baeldung/retrofit/basic/GitHubBasicApp.java b/libraries/src/main/java/com/baeldung/retrofit/basic/GitHubBasicApp.java index 6b2cd14252..df0d90af7f 100644 --- a/libraries/src/main/java/com/baeldung/retrofit/basic/GitHubBasicApp.java +++ b/libraries/src/main/java/com/baeldung/retrofit/basic/GitHubBasicApp.java @@ -7,8 +7,7 @@ public class GitHubBasicApp { public static void main(String[] args) throws IOException { String userName = "eugenp"; - List topContributors = new GitHubBasicService() - .getTopContributors(userName); + List topContributors = new GitHubBasicService().getTopContributors(userName); topContributors.forEach(System.out::println); } } diff --git a/libraries/src/main/java/com/baeldung/retrofit/basic/GitHubBasicService.java b/libraries/src/main/java/com/baeldung/retrofit/basic/GitHubBasicService.java index 20256fb540..ff6ef82183 100644 --- a/libraries/src/main/java/com/baeldung/retrofit/basic/GitHubBasicService.java +++ b/libraries/src/main/java/com/baeldung/retrofit/basic/GitHubBasicService.java @@ -16,45 +16,29 @@ class GitHubBasicService { private GitHubBasicApi gitHubApi; GitHubBasicService() { - Retrofit retrofit = new Retrofit.Builder() - .baseUrl("https://api.github.com/") - .addConverterFactory(GsonConverterFactory.create()) - .build(); + Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.github.com/").addConverterFactory(GsonConverterFactory.create()).build(); gitHubApi = retrofit.create(GitHubBasicApi.class); } List getTopContributors(String userName) throws IOException { - List repos = gitHubApi - .listRepos(userName) - .execute() - .body(); + List repos = gitHubApi.listRepos(userName).execute().body(); repos = repos != null ? repos : Collections.emptyList(); - return repos.stream() - .flatMap(repo -> getContributors(userName, repo)) - .sorted((a, b) -> b.getContributions() - a.getContributions()) - .map(Contributor::getName) - .distinct() - .sorted() - .collect(Collectors.toList()); + return repos.stream().flatMap(repo -> getContributors(userName, repo)).sorted((a, b) -> b.getContributions() - a.getContributions()).map(Contributor::getName).distinct().sorted().collect(Collectors.toList()); } private Stream getContributors(String userName, Repository repo) { List contributors = null; try { - contributors = gitHubApi - .listRepoContributors(userName, repo.getName()) - .execute() - .body(); + contributors = gitHubApi.listRepoContributors(userName, repo.getName()).execute().body(); } catch (IOException e) { e.printStackTrace(); } contributors = contributors != null ? contributors : Collections.emptyList(); - return contributors.stream() - .filter(c -> c.getContributions() > 100); + return contributors.stream().filter(c -> c.getContributions() > 100); } } diff --git a/libraries/src/main/java/com/baeldung/retrofit/models/Contributor.java b/libraries/src/main/java/com/baeldung/retrofit/models/Contributor.java index 2f8697f603..f98b19de96 100644 --- a/libraries/src/main/java/com/baeldung/retrofit/models/Contributor.java +++ b/libraries/src/main/java/com/baeldung/retrofit/models/Contributor.java @@ -3,28 +3,31 @@ package com.baeldung.retrofit.models; import com.google.gson.annotations.SerializedName; public class Contributor { - + @SerializedName("login") private String name; - + private Integer contributions; - + public String getName() { return name; } + public void setName(String name) { this.name = name; } + public Integer getContributions() { return contributions; } + public void setContributions(Integer contributions) { this.contributions = contributions; } - + @Override public String toString() { return "Contributer [name=" + name + ", contributions=" + contributions + "]"; } - + } diff --git a/libraries/src/main/java/com/baeldung/retrofit/models/Repository.java b/libraries/src/main/java/com/baeldung/retrofit/models/Repository.java index f12fcdf8f2..6bc91eb772 100644 --- a/libraries/src/main/java/com/baeldung/retrofit/models/Repository.java +++ b/libraries/src/main/java/com/baeldung/retrofit/models/Repository.java @@ -1,20 +1,23 @@ package com.baeldung.retrofit.models; public class Repository { - + private String name; - + private String description; - + public String getName() { return name; } + public void setName(String name) { this.name = name; } + public String getDescription() { return description; } + public void setDescription(String description) { this.description = description; } diff --git a/libraries/src/main/java/com/baeldung/retrofit/rx/GitHubRxApi.java b/libraries/src/main/java/com/baeldung/retrofit/rx/GitHubRxApi.java index 4e40aff448..aa0f550115 100644 --- a/libraries/src/main/java/com/baeldung/retrofit/rx/GitHubRxApi.java +++ b/libraries/src/main/java/com/baeldung/retrofit/rx/GitHubRxApi.java @@ -18,7 +18,7 @@ public interface GitHubRxApi { */ @GET("users/{user}/repos") Observable> listRepos(@Path("user") String user); - + /** * List Contributors of a GitHub Repository * @param user GitHub Account @@ -26,8 +26,6 @@ public interface GitHubRxApi { * @return GitHub Repository Contributors */ @GET("repos/{user}/{repo}/contributors") - Observable> listRepoContributors( - @Path("user") String user, - @Path("repo") String repo); - + Observable> listRepoContributors(@Path("user") String user, @Path("repo") String repo); + } diff --git a/libraries/src/main/java/com/baeldung/retrofit/rx/GitHubRxApp.java b/libraries/src/main/java/com/baeldung/retrofit/rx/GitHubRxApp.java index b136a1e40b..4941a65717 100644 --- a/libraries/src/main/java/com/baeldung/retrofit/rx/GitHubRxApp.java +++ b/libraries/src/main/java/com/baeldung/retrofit/rx/GitHubRxApp.java @@ -6,7 +6,6 @@ public class GitHubRxApp { public static void main(String[] args) throws IOException { String userName = "eugenp"; - new GitHubRxService().getTopContributors(userName) - .subscribe(System.out::println); + new GitHubRxService().getTopContributors(userName).subscribe(System.out::println); } } diff --git a/libraries/src/main/java/com/baeldung/retrofit/rx/GitHubRxService.java b/libraries/src/main/java/com/baeldung/retrofit/rx/GitHubRxService.java index 2ad50a9f39..f2c5114149 100644 --- a/libraries/src/main/java/com/baeldung/retrofit/rx/GitHubRxService.java +++ b/libraries/src/main/java/com/baeldung/retrofit/rx/GitHubRxService.java @@ -11,23 +11,13 @@ class GitHubRxService { private GitHubRxApi gitHubApi; GitHubRxService() { - Retrofit retrofit = new Retrofit.Builder() - .baseUrl("https://api.github.com/") - .addConverterFactory(GsonConverterFactory.create()) - .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) - .build(); + Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.github.com/").addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build(); gitHubApi = retrofit.create(GitHubRxApi.class); } Observable getTopContributors(String userName) { - return gitHubApi.listRepos(userName) - .flatMapIterable(x -> x) - .flatMap(repo -> gitHubApi.listRepoContributors(userName, repo.getName())) - .flatMapIterable(x -> x) - .filter(c -> c.getContributions() > 100) - .sorted((a, b) -> b.getContributions() - a.getContributions()) - .map(Contributor::getName) - .distinct(); + return gitHubApi.listRepos(userName).flatMapIterable(x -> x).flatMap(repo -> gitHubApi.listRepoContributors(userName, repo.getName())).flatMapIterable(x -> x).filter(c -> c.getContributions() > 100) + .sorted((a, b) -> b.getContributions() - a.getContributions()).map(Contributor::getName).distinct(); } } diff --git a/libraries/src/main/java/com/baeldung/retrofitguide/GitHubServiceGenerator.java b/libraries/src/main/java/com/baeldung/retrofitguide/GitHubServiceGenerator.java index d32891be9e..dc6bfbddb1 100644 --- a/libraries/src/main/java/com/baeldung/retrofitguide/GitHubServiceGenerator.java +++ b/libraries/src/main/java/com/baeldung/retrofitguide/GitHubServiceGenerator.java @@ -13,19 +13,13 @@ public class GitHubServiceGenerator { private static final String BASE_URL = "https://api.github.com/"; - private static Retrofit.Builder builder - = new Retrofit.Builder() - .baseUrl(BASE_URL) - .addConverterFactory(GsonConverterFactory.create()); + private static Retrofit.Builder builder = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()); private static Retrofit retrofit = builder.build(); - private static OkHttpClient.Builder httpClient - = new OkHttpClient.Builder(); + private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); - private static HttpLoggingInterceptor logging - = new HttpLoggingInterceptor() - .setLevel(HttpLoggingInterceptor.Level.BASIC); + private static HttpLoggingInterceptor logging = new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC); public static S createService(Class serviceClass) { if (!httpClient.interceptors().contains(logging)) { @@ -43,8 +37,7 @@ public class GitHubServiceGenerator { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request original = chain.request(); - Request.Builder builder = original.newBuilder() - .header("Authorization", token); + Request.Builder builder = original.newBuilder().header("Authorization", token); Request request = builder.build(); return chain.proceed(request); } diff --git a/libraries/src/main/java/com/baeldung/retrofitguide/Main.java b/libraries/src/main/java/com/baeldung/retrofitguide/Main.java index 8a674f634b..be7e15e3c9 100644 --- a/libraries/src/main/java/com/baeldung/retrofitguide/Main.java +++ b/libraries/src/main/java/com/baeldung/retrofitguide/Main.java @@ -11,15 +11,11 @@ import retrofit2.converter.gson.GsonConverterFactory; public class Main { public static void main(String[] args) { - //Manual creation + // Manual creation OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); - Retrofit retrofit = new Retrofit.Builder() - .baseUrl("https://api.github.com/") - .addConverterFactory(GsonConverterFactory.create()) - .client(httpClient.build()) - .build(); + Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.github.com/").addConverterFactory(GsonConverterFactory.create()).client(httpClient.build()).build(); UserService service = retrofit.create(UserService.class); - //Using GitHubServiceGenerator + // Using GitHubServiceGenerator service = GitHubServiceGenerator.createService(UserService.class); Call callSync = service.getUser("eugenp"); Call callAsync = service.getUser("eugenp"); diff --git a/libraries/src/main/java/com/baeldung/serenity/membership/Commodity.java b/libraries/src/main/java/com/baeldung/serenity/membership/Commodity.java index 208b73d4af..c889871b3d 100644 --- a/libraries/src/main/java/com/baeldung/serenity/membership/Commodity.java +++ b/libraries/src/main/java/com/baeldung/serenity/membership/Commodity.java @@ -9,7 +9,7 @@ public enum Commodity { public final int price; - Commodity(int price){ + Commodity(int price) { this.price = price; } diff --git a/libraries/src/main/java/com/baeldung/serenity/membership/Member.java b/libraries/src/main/java/com/baeldung/serenity/membership/Member.java index 6e7c4db08e..f2f443020d 100644 --- a/libraries/src/main/java/com/baeldung/serenity/membership/Member.java +++ b/libraries/src/main/java/com/baeldung/serenity/membership/Member.java @@ -12,7 +12,8 @@ public class Member { private int points; private Member(int points) { - if (points < 0) throw new IllegalArgumentException("points must not be negative!"); + if (points < 0) + throw new IllegalArgumentException("points must not be negative!"); this.points = points; } @@ -22,9 +23,12 @@ public class Member { } public MemberGrade getGrade() { - if (points < 1000) return Bronze; - else if (points >= 1000 && points < 5000) return Silver; - else return Gold; + if (points < 1000) + return Bronze; + else if (points >= 1000 && points < 5000) + return Silver; + else + return Gold; } public void spend(int moneySpent) { diff --git a/libraries/src/main/java/com/baeldung/serenity/membership/MemberGrade.java b/libraries/src/main/java/com/baeldung/serenity/membership/MemberGrade.java index 7bb6f76495..389749c2ca 100644 --- a/libraries/src/main/java/com/baeldung/serenity/membership/MemberGrade.java +++ b/libraries/src/main/java/com/baeldung/serenity/membership/MemberGrade.java @@ -3,7 +3,7 @@ package com.baeldung.serenity.membership; /** * @author aiet */ -public enum MemberGrade { +public enum MemberGrade { Bronze, Silver, Gold; diff --git a/libraries/src/main/java/com/baeldung/smooks/converter/OrderConverter.java b/libraries/src/main/java/com/baeldung/smooks/converter/OrderConverter.java index d11f5a29b2..fa317f93b7 100644 --- a/libraries/src/main/java/com/baeldung/smooks/converter/OrderConverter.java +++ b/libraries/src/main/java/com/baeldung/smooks/converter/OrderConverter.java @@ -21,7 +21,6 @@ public class OrderConverter { smooks.close(); } } - public String convertOrderXMLtoEDIFACT(String path) throws IOException, SAXException { return convertDocumentWithTempalte(path, "/smooks/smooks-transform-edi.xml"); diff --git a/libraries/src/main/java/com/baeldung/smooks/model/Item.java b/libraries/src/main/java/com/baeldung/smooks/model/Item.java index a7f7783b3f..3e1f4a7ef4 100644 --- a/libraries/src/main/java/com/baeldung/smooks/model/Item.java +++ b/libraries/src/main/java/com/baeldung/smooks/model/Item.java @@ -15,7 +15,6 @@ public class Item { private Double price; private Integer quantity; - public String getCode() { return code; } @@ -42,13 +41,17 @@ public class Item { @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; Item item = (Item) o; - if (code != null ? !code.equals(item.code) : item.code != null) return false; - if (price != null ? !price.equals(item.price) : item.price != null) return false; + if (code != null ? !code.equals(item.code) : item.code != null) + return false; + if (price != null ? !price.equals(item.price) : item.price != null) + return false; return quantity != null ? quantity.equals(item.quantity) : item.quantity == null; } @@ -62,10 +65,6 @@ public class Item { @Override public String toString() { - return "Item{" + - "code='" + code + '\'' + - ", price=" + price + - ", quantity=" + quantity + - '}'; + return "Item{" + "code='" + code + '\'' + ", price=" + price + ", quantity=" + quantity + '}'; } } diff --git a/libraries/src/main/java/com/baeldung/smooks/model/Supplier.java b/libraries/src/main/java/com/baeldung/smooks/model/Supplier.java index 31a9e1f43f..827a0fc907 100644 --- a/libraries/src/main/java/com/baeldung/smooks/model/Supplier.java +++ b/libraries/src/main/java/com/baeldung/smooks/model/Supplier.java @@ -31,12 +31,15 @@ public class Supplier { @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; Supplier supplier = (Supplier) o; - if (name != null ? !name.equals(supplier.name) : supplier.name != null) return false; + if (name != null ? !name.equals(supplier.name) : supplier.name != null) + return false; return phoneNumber != null ? phoneNumber.equals(supplier.phoneNumber) : supplier.phoneNumber == null; } diff --git a/libraries/src/main/java/com/baeldung/stm/Account.java b/libraries/src/main/java/com/baeldung/stm/Account.java index 8b17f87120..4dc2c492df 100644 --- a/libraries/src/main/java/com/baeldung/stm/Account.java +++ b/libraries/src/main/java/com/baeldung/stm/Account.java @@ -44,7 +44,6 @@ public class Account { @Override public String toString() { - return StmUtils.atomic((TxnCallable) - txn -> "Balance: " + balance.get(txn) + " lastUpdateDate: " + lastUpdate.get(txn)); + return StmUtils.atomic((TxnCallable) txn -> "Balance: " + balance.get(txn) + " lastUpdateDate: " + lastUpdate.get(txn)); } } \ No newline at end of file diff --git a/libraries/src/main/java/com/baeldung/streamex/StreamEX.java b/libraries/src/main/java/com/baeldung/streamex/StreamEX.java index 7cbfec4421..56a3860f05 100644 --- a/libraries/src/main/java/com/baeldung/streamex/StreamEX.java +++ b/libraries/src/main/java/com/baeldung/streamex/StreamEX.java @@ -15,69 +15,43 @@ import one.util.streamex.StreamEx; public class StreamEX { public static void main(String[] args) { - //Collector shortcut methods (toList, toSet, groupingBy, joining, etc.) - List users = Arrays.asList( - new User("name"), new User(), new User()); - users.stream() - .map(User::getName) - .collect(Collectors.toList()); - List userNames = StreamEx.of(users) - .map(User::getName) - .toList(); - Map> role2users = StreamEx.of(users) - .groupingBy(User::getRole); - StreamEx.of(1, 2, 3).joining("; "); // "1; 2; 3" - //Selecting stream elements of specific type + // Collector shortcut methods (toList, toSet, groupingBy, joining, etc.) + List users = Arrays.asList(new User("name"), new User(), new User()); + users.stream().map(User::getName).collect(Collectors.toList()); + List userNames = StreamEx.of(users).map(User::getName).toList(); + Map> role2users = StreamEx.of(users).groupingBy(User::getRole); + StreamEx.of(1, 2, 3).joining("; "); // "1; 2; 3" + // Selecting stream elements of specific type List usersAndRoles = Arrays.asList(new User(), new Role()); - List roles = IntStreamEx.range(usersAndRoles.size()) - .mapToObj(usersAndRoles::get) - .select(Role.class) - .toList(); + List roles = IntStreamEx.range(usersAndRoles.size()).mapToObj(usersAndRoles::get).select(Role.class).toList(); System.out.println(roles); - //adding elements to Stream - List appendedUsers = StreamEx.of(users) - .map(User::getName) - .prepend("(none)") - .append("LAST") - .toList(); + // adding elements to Stream + List appendedUsers = StreamEx.of(users).map(User::getName).prepend("(none)").append("LAST").toList(); System.out.println(appendedUsers); - //Removing unwanted elements and using the stream as Iterable: - for (String line : StreamEx.of(users).map(User::getName) - .nonNull()) { + // Removing unwanted elements and using the stream as Iterable: + for (String line : StreamEx.of(users).map(User::getName).nonNull()) { System.out.println(line); } - //Selecting map keys by value predicate: + // Selecting map keys by value predicate: Map nameToRole = new HashMap<>(); nameToRole.put("first", new Role()); nameToRole.put("second", null); - Set nonNullRoles = StreamEx. - ofKeys(nameToRole, Objects::nonNull) - .toSet(); + Set nonNullRoles = StreamEx.ofKeys(nameToRole, Objects::nonNull).toSet(); System.out.println(nonNullRoles); - //Operating on key-value pairs: + // Operating on key-value pairs: Map> users2roles = transformMap(role2users); - Map mapToString = EntryStream.of(users2roles) - .mapKeys(String::valueOf) - .mapValues(String::valueOf) - .toMap(); - //Support of byte/char/short/float types: - short[] src = {1, 2, 3}; - char[] output = IntStreamEx.of(src) - .map(x -> x * 5) - .toCharArray(); + Map mapToString = EntryStream.of(users2roles).mapKeys(String::valueOf).mapValues(String::valueOf).toMap(); + // Support of byte/char/short/float types: + short[] src = { 1, 2, 3 }; + char[] output = IntStreamEx.of(src).map(x -> x * 5).toCharArray(); } public double[] getDiffBetweenPairs(double... numbers) { - return DoubleStreamEx.of(numbers) - .pairMap((a, b) -> b - a).toArray(); + return DoubleStreamEx.of(numbers).pairMap((a, b) -> b - a).toArray(); } - public static Map> transformMap( - Map> role2users) { - Map> users2roles = EntryStream.of(role2users) - .flatMapValues(List::stream) - .invert() - .grouping(); + public static Map> transformMap(Map> role2users) { + Map> users2roles = EntryStream.of(role2users).flatMapValues(List::stream).invert().grouping(); return users2roles; } diff --git a/libraries/src/main/java/com/baeldung/streamutils/CopyStream.java b/libraries/src/main/java/com/baeldung/streamutils/CopyStream.java index 430759f3a0..d9097188b3 100644 --- a/libraries/src/main/java/com/baeldung/streamutils/CopyStream.java +++ b/libraries/src/main/java/com/baeldung/streamutils/CopyStream.java @@ -9,14 +9,14 @@ import org.apache.commons.io.IOUtils; import org.springframework.util.StreamUtils; public class CopyStream { - public static String getStringFromInputStream(InputStream input) throws IOException { - StringWriter writer = new StringWriter(); - IOUtils.copy(input, writer, "UTF-8"); - return writer.toString(); - } + public static String getStringFromInputStream(InputStream input) throws IOException { + StringWriter writer = new StringWriter(); + IOUtils.copy(input, writer, "UTF-8"); + return writer.toString(); + } - public InputStream getNonClosingInputStream() throws IOException { - InputStream in = new FileInputStream("src/test/resources/input.txt"); - return StreamUtils.nonClosing(in); - } + public InputStream getNonClosingInputStream() throws IOException { + InputStream in = new FileInputStream("src/test/resources/input.txt"); + return StreamUtils.nonClosing(in); + } } diff --git a/libraries/src/main/java/com/baeldung/streamutils/DrainStream.java b/libraries/src/main/java/com/baeldung/streamutils/DrainStream.java index 6ee4a1ef3a..1ce67a075a 100644 --- a/libraries/src/main/java/com/baeldung/streamutils/DrainStream.java +++ b/libraries/src/main/java/com/baeldung/streamutils/DrainStream.java @@ -5,7 +5,7 @@ import java.io.InputStream; import org.springframework.util.StreamUtils; public class DrainStream { - public InputStream getInputStream() { - return StreamUtils.emptyInput(); - } + public InputStream getInputStream() { + return StreamUtils.emptyInput(); + } } diff --git a/libraries/src/main/java/com/baeldung/tomcat/MyFilter.java b/libraries/src/main/java/com/baeldung/tomcat/MyFilter.java new file mode 100644 index 0000000000..9cf4a0ed95 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/tomcat/MyFilter.java @@ -0,0 +1,31 @@ +package com.baeldung.tomcat; + +import javax.servlet.*; +import javax.servlet.annotation.WebFilter; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * Created by adi on 1/14/18. + */ +@WebFilter(urlPatterns = "/my-servlet/*") +public class MyFilter implements Filter { + + @Override + public void init(FilterConfig filterConfig) { + + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + System.out.println("Filtering stuff..."); + HttpServletResponse httpResponse = (HttpServletResponse) response; + httpResponse.addHeader("myHeader", "myHeaderValue"); + chain.doFilter(request, httpResponse); + } + + @Override + public void destroy() { + + } +} diff --git a/libraries/src/main/java/com/baeldung/tomcat/MyServlet.java b/libraries/src/main/java/com/baeldung/tomcat/MyServlet.java new file mode 100644 index 0000000000..1b48e2d90b --- /dev/null +++ b/libraries/src/main/java/com/baeldung/tomcat/MyServlet.java @@ -0,0 +1,22 @@ +package com.baeldung.tomcat; + +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * Created by adi on 1/10/18. + */ +@WebServlet(name = "com.baeldung.tomcat.programmatic.MyServlet", urlPatterns = { "/my-servlet" }) +public class MyServlet extends HttpServlet { + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { + resp.setStatus(HttpServletResponse.SC_OK); + resp.getWriter().write("test"); + resp.getWriter().flush(); + resp.getWriter().close(); + } +} diff --git a/libraries/src/main/java/com/baeldung/tomcat/ProgrammaticTomcat.java b/libraries/src/main/java/com/baeldung/tomcat/ProgrammaticTomcat.java new file mode 100644 index 0000000000..6c4fed6d07 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/tomcat/ProgrammaticTomcat.java @@ -0,0 +1,61 @@ +package com.baeldung.tomcat; + +import org.apache.catalina.Context; +import org.apache.catalina.LifecycleException; +import org.apache.catalina.startup.Tomcat; +import org.apache.tomcat.util.descriptor.web.FilterDef; +import org.apache.tomcat.util.descriptor.web.FilterMap; + +import java.io.File; + +/** + * Created by adi on 1/10/18. + */ +public class ProgrammaticTomcat { + + private Tomcat tomcat = null; + + // uncomment for live test + // public static void main(String[] args) throws LifecycleException, ServletException, URISyntaxException, IOException { + // startTomcat(); + // } + + public void startTomcat() throws LifecycleException { + tomcat = new Tomcat(); + tomcat.setPort(8080); + tomcat.setHostname("localhost"); + String appBase = "."; + tomcat.getHost().setAppBase(appBase); + + File docBase = new File(System.getProperty("java.io.tmpdir")); + Context context = tomcat.addContext("", docBase.getAbsolutePath()); + + // add a servlet + Class servletClass = MyServlet.class; + Tomcat.addServlet(context, servletClass.getSimpleName(), servletClass.getName()); + context.addServletMappingDecoded("/my-servlet/*", servletClass.getSimpleName()); + + // add a filter and filterMapping + Class filterClass = MyFilter.class; + FilterDef myFilterDef = new FilterDef(); + myFilterDef.setFilterClass(filterClass.getName()); + myFilterDef.setFilterName(filterClass.getSimpleName()); + context.addFilterDef(myFilterDef); + + FilterMap myFilterMap = new FilterMap(); + myFilterMap.setFilterName(filterClass.getSimpleName()); + myFilterMap.addURLPattern("/my-servlet/*"); + context.addFilterMap(myFilterMap); + + tomcat.start(); + // uncomment for live test + // tomcat + // .getServer() + // .await(); + } + + public void stopTomcat() throws LifecycleException { + tomcat.stop(); + tomcat.destroy(); + } +} diff --git a/libraries/src/main/java/com/baeldung/yarg/DocumentController.java b/libraries/src/main/java/com/baeldung/yarg/DocumentController.java index 0e1bbca561..ff0d452108 100644 --- a/libraries/src/main/java/com/baeldung/yarg/DocumentController.java +++ b/libraries/src/main/java/com/baeldung/yarg/DocumentController.java @@ -30,25 +30,17 @@ public class DocumentController { @RequestMapping(path = "/generate/doc", method = RequestMethod.GET) public void generateDocument(HttpServletResponse response) throws IOException { ReportBuilder reportBuilder = new ReportBuilder(); - ReportTemplateBuilder reportTemplateBuilder = new ReportTemplateBuilder() - .documentPath("./src/main/resources/Letter.docx") - .documentName("Letter.docx") - .outputType(ReportOutputType.docx) - .readFileFromPath(); + ReportTemplateBuilder reportTemplateBuilder = new ReportTemplateBuilder().documentPath("./src/main/resources/Letter.docx").documentName("Letter.docx").outputType(ReportOutputType.docx).readFileFromPath(); reportBuilder.template(reportTemplateBuilder.build()); BandBuilder bandBuilder = new BandBuilder(); String json = FileUtils.readFileToString(new File("./src/main/resources/Data.json")); - ReportBand main = bandBuilder.name("Main") - .query("Main", "parameter=param1 $.main", "json") - .build(); + ReportBand main = bandBuilder.name("Main").query("Main", "parameter=param1 $.main", "json").build(); reportBuilder.band(main); Report report = reportBuilder.build(); Reporting reporting = new Reporting(); reporting.setFormatterFactory(new DefaultFormatterFactory()); - reporting.setLoaderFactory( - new DefaultLoaderFactory() - .setJsonDataLoader(new JsonDataLoader())); + reporting.setLoaderFactory(new DefaultLoaderFactory().setJsonDataLoader(new JsonDataLoader())); response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document"); reporting.runReport(new RunParams(report).param("param1", json), response.getOutputStream()); } diff --git a/libraries/src/main/resources/Greeting.sol b/libraries/src/main/resources/Greeting.sol new file mode 100644 index 0000000000..dde7b36cc3 --- /dev/null +++ b/libraries/src/main/resources/Greeting.sol @@ -0,0 +1,19 @@ +pragma solidity ^0.4.0; + +contract Greeting { + address creator; + string message; + + function Greeting(string _message) { + message = _message; + creator = msg.sender; + } + + function greet() constant returns (string) { + return message; + } + + function setGreeting(string _message) { + message = _message; + } +} \ No newline at end of file diff --git a/libraries/src/test/java/com/baeldung/asynchttpclient/AsyncHttpClientTestCase.java b/libraries/src/test/java/com/baeldung/asynchttpclient/AsyncHttpClientTestCase.java new file mode 100644 index 0000000000..7ff81c20c3 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/asynchttpclient/AsyncHttpClientTestCase.java @@ -0,0 +1,204 @@ +package com.baeldung.asynchttpclient; + +import io.netty.handler.codec.http.HttpHeaders; +import org.asynchttpclient.AsyncCompletionHandler; +import org.asynchttpclient.AsyncHandler; +import org.asynchttpclient.AsyncHttpClient; +import org.asynchttpclient.AsyncHttpClientConfig; +import org.asynchttpclient.BoundRequestBuilder; +import org.asynchttpclient.Dsl; +import org.asynchttpclient.HttpResponseBodyPart; +import org.asynchttpclient.HttpResponseStatus; +import org.asynchttpclient.ListenableFuture; +import org.asynchttpclient.Request; +import org.asynchttpclient.Response; +import org.asynchttpclient.ws.WebSocket; +import org.asynchttpclient.ws.WebSocketListener; +import org.asynchttpclient.ws.WebSocketUpgradeHandler; +import org.junit.Before; +import org.junit.Test; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class AsyncHttpClientTestCase { + + private static AsyncHttpClient HTTP_CLIENT; + + @Before + public void setup() { + AsyncHttpClientConfig clientConfig = Dsl.config().setConnectTimeout(15000).setRequestTimeout(15000).build(); + HTTP_CLIENT = Dsl.asyncHttpClient(clientConfig); + } + + @Test + public void givenHttpClient_executeSyncGetRequest() { + + BoundRequestBuilder boundGetRequest = HTTP_CLIENT.prepareGet("http://www.baeldung.com"); + + Future responseFuture = boundGetRequest.execute(); + try { + Response response = responseFuture.get(5000, TimeUnit.MILLISECONDS); + assertNotNull(response); + assertEquals(200, response.getStatusCode()); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + e.printStackTrace(); + } + + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + @Test + public void givenHttpClient_executeAsyncGetRequest() { + + // execute an unbound GET request + Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build(); + + HTTP_CLIENT.executeRequest(unboundGetRequest, new AsyncCompletionHandler() { + @Override + public Integer onCompleted(Response response) { + + int resposeStatusCode = response.getStatusCode(); + assertEquals(200, resposeStatusCode); + return resposeStatusCode; + } + }); + + // execute a bound GET request + BoundRequestBuilder boundGetRequest = HTTP_CLIENT.prepareGet("http://www.baeldung.com"); + + boundGetRequest.execute(new AsyncCompletionHandler() { + @Override + public Integer onCompleted(Response response) { + int resposeStatusCode = response.getStatusCode(); + assertEquals(200, resposeStatusCode); + return resposeStatusCode; + } + }); + + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + @Test + public void givenHttpClient_executeAsyncGetRequestWithAsyncHandler() { + + // execute an unbound GET request + Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build(); + + HTTP_CLIENT.executeRequest(unboundGetRequest, new AsyncHandler() { + + int responseStatusCode = -1; + + @Override + public State onStatusReceived(HttpResponseStatus responseStatus) { + responseStatusCode = responseStatus.getStatusCode(); + return State.CONTINUE; + } + + @Override + public State onHeadersReceived(HttpHeaders headers) { + return State.CONTINUE; + } + + @Override + public State onBodyPartReceived(HttpResponseBodyPart bodyPart) { + return State.CONTINUE; + } + + @Override + public void onThrowable(Throwable t) { + + } + + @Override + public Integer onCompleted() { + assertEquals(200, responseStatusCode); + return responseStatusCode; + } + }); + + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + @Test + public void givenHttpClient_executeAsyncGetRequestWithListanableFuture() { + // execute an unbound GET request + Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build(); + + ListenableFuture listenableFuture = HTTP_CLIENT.executeRequest(unboundGetRequest); + listenableFuture.addListener(() -> { + Response response; + try { + response = listenableFuture.get(5000, TimeUnit.MILLISECONDS); + assertEquals(200, response.getStatusCode()); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + e.printStackTrace(); + } + }, Executors.newCachedThreadPool()); + + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + @Test + public void givenWebSocketClient_tryToConnect() { + + WebSocketUpgradeHandler.Builder upgradeHandlerBuilder = new WebSocketUpgradeHandler.Builder(); + WebSocketUpgradeHandler wsHandler = upgradeHandlerBuilder.addWebSocketListener(new WebSocketListener() { + @Override + public void onOpen(WebSocket websocket) { + // WebSocket connection opened + } + + @Override + public void onClose(WebSocket websocket, int code, String reason) { + // WebSocket connection closed + } + + @Override + public void onError(Throwable t) { + // WebSocket connection error + assertTrue(t.getMessage().contains("Request timeout")); + } + }).build(); + + WebSocket WEBSOCKET_CLIENT = null; + try { + WEBSOCKET_CLIENT = Dsl.asyncHttpClient().prepareGet("ws://localhost:5590/websocket").addHeader("header_name", "header_value").addQueryParam("key", "value").setRequestTimeout(5000).execute(wsHandler).get(); + + if (WEBSOCKET_CLIENT.isOpen()) { + WEBSOCKET_CLIENT.sendPingFrame(); + WEBSOCKET_CLIENT.sendTextFrame("test message"); + WEBSOCKET_CLIENT.sendBinaryFrame(new byte[] { 't', 'e', 's', 't' }); + } + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } finally { + if (WEBSOCKET_CLIENT != null && WEBSOCKET_CLIENT.isOpen()) { + WEBSOCKET_CLIENT.sendCloseFrame(200, "OK"); + } + } + } +} diff --git a/libraries/src/test/java/com/baeldung/awaitility/AsyncServiceLongRunningUnitTest.java b/libraries/src/test/java/com/baeldung/awaitility/AsyncServiceLongRunningUnitTest.java index 7ca656efbf..87c0b13bcc 100644 --- a/libraries/src/test/java/com/baeldung/awaitility/AsyncServiceLongRunningUnitTest.java +++ b/libraries/src/test/java/com/baeldung/awaitility/AsyncServiceLongRunningUnitTest.java @@ -35,10 +35,7 @@ public class AsyncServiceLongRunningUnitTest { public void givenAsyncService_whenInitialize_thenInitOccurs2() { asyncService.initialize(); Callable isInitialized = asyncService::isInitialized; - await().atLeast(Duration.ONE_HUNDRED_MILLISECONDS) - .atMost(Duration.FIVE_SECONDS) - .with().pollInterval(Duration.ONE_HUNDRED_MILLISECONDS) - .until(isInitialized); + await().atLeast(Duration.ONE_HUNDRED_MILLISECONDS).atMost(Duration.FIVE_SECONDS).with().pollInterval(Duration.ONE_HUNDRED_MILLISECONDS).until(isInitialized); } @Test @@ -60,9 +57,7 @@ public class AsyncServiceLongRunningUnitTest { @Test public void givenAsyncService_whenInitialize_thenInitOccurs3() { asyncService.initialize(); - await().until(fieldIn(asyncService) - .ofType(boolean.class) - .andWithName("initialized"), equalTo(true)); + await().until(fieldIn(asyncService).ofType(boolean.class).andWithName("initialized"), equalTo(true)); } @Test @@ -77,10 +72,6 @@ public class AsyncServiceLongRunningUnitTest { @Test public void givenAsyncService_whenGetValue_thenExceptionIgnored() { asyncService.initialize(); - given().ignoreException(IllegalStateException.class) - .await() - .atMost(Duration.FIVE_SECONDS) - .atLeast(Duration.FIVE_HUNDRED_MILLISECONDS) - .until(asyncService::getValue, equalTo(0L)); + given().ignoreException(IllegalStateException.class).await().atMost(Duration.FIVE_SECONDS).atLeast(Duration.FIVE_HUNDRED_MILLISECONDS).until(asyncService::getValue, equalTo(0L)); } } diff --git a/libraries/src/test/java/com/baeldung/bouncycastle/BouncyCastleLiveTest.java b/libraries/src/test/java/com/baeldung/bouncycastle/BouncyCastleLiveTest.java index 3965eeecd4..009119d97a 100644 --- a/libraries/src/test/java/com/baeldung/bouncycastle/BouncyCastleLiveTest.java +++ b/libraries/src/test/java/com/baeldung/bouncycastle/BouncyCastleLiveTest.java @@ -28,14 +28,11 @@ public class BouncyCastleLiveTest { char[] keyPassword = "password".toCharArray(); @Test - public void givenCryptographicResource_whenOperationSuccess_returnTrue() - throws CertificateException, NoSuchProviderException, NoSuchAlgorithmException, IOException, - KeyStoreException, UnrecoverableKeyException, CMSException, OperatorCreationException { + public void givenCryptographicResource_whenOperationSuccess_returnTrue() throws CertificateException, NoSuchProviderException, NoSuchAlgorithmException, IOException, KeyStoreException, UnrecoverableKeyException, CMSException, OperatorCreationException { Security.addProvider(new BouncyCastleProvider()); CertificateFactory certFactory = CertificateFactory.getInstance("X.509", "BC"); - X509Certificate certificate = (X509Certificate) certFactory - .generateCertificate(new FileInputStream(certificatePath)); + X509Certificate certificate = (X509Certificate) certFactory.generateCertificate(new FileInputStream(certificatePath)); KeyStore keystore = KeyStore.getInstance("PKCS12"); keystore.load(new FileInputStream(privateKeyPath), p12Password); PrivateKey privateKey = (PrivateKey) keystore.getKey("baeldung", keyPassword); diff --git a/libraries/src/test/java/com/baeldung/bytebuddy/ByteBuddyUnitTest.java b/libraries/src/test/java/com/baeldung/bytebuddy/ByteBuddyUnitTest.java index 6b7364a0a5..5f721025c3 100644 --- a/libraries/src/test/java/com/baeldung/bytebuddy/ByteBuddyUnitTest.java +++ b/libraries/src/test/java/com/baeldung/bytebuddy/ByteBuddyUnitTest.java @@ -21,14 +21,9 @@ public class ByteBuddyUnitTest { @Test public void givenObject_whenToString_thenReturnHelloWorldString() throws InstantiationException, IllegalAccessException { - DynamicType.Unloaded unloadedType = new ByteBuddy() - .subclass(Object.class) - .method(ElementMatchers.isToString()) - .intercept(FixedValue.value("Hello World ByteBuddy!")) - .make(); + DynamicType.Unloaded unloadedType = new ByteBuddy().subclass(Object.class).method(ElementMatchers.isToString()).intercept(FixedValue.value("Hello World ByteBuddy!")).make(); - Class dynamicType = unloadedType.load(getClass().getClassLoader()) - .getLoaded(); + Class dynamicType = unloadedType.load(getClass().getClassLoader()).getLoaded(); assertEquals(dynamicType.newInstance().toString(), "Hello World ByteBuddy!"); } @@ -36,12 +31,7 @@ public class ByteBuddyUnitTest { @Test public void givenFoo_whenRedefined_thenReturnFooRedefined() throws Exception { ByteBuddyAgent.install(); - new ByteBuddy() - .redefine(Foo.class) - .method(named("sayHelloFoo")) - .intercept(FixedValue.value("Hello Foo Redefined")) - .make() - .load(Foo.class.getClassLoader(), ClassReloadingStrategy.fromInstalledAgent()); + new ByteBuddy().redefine(Foo.class).method(named("sayHelloFoo")).intercept(FixedValue.value("Hello Foo Redefined")).make().load(Foo.class.getClassLoader(), ClassReloadingStrategy.fromInstalledAgent()); Foo f = new Foo(); assertEquals(f.sayHelloFoo(), "Hello Foo Redefined"); } @@ -49,34 +39,16 @@ public class ByteBuddyUnitTest { @Test public void givenSayHelloFoo_whenMethodDelegation_thenSayHelloBar() throws IllegalAccessException, InstantiationException { - String r = new ByteBuddy() - .subclass(Foo.class) - .method( - named("sayHelloFoo") - .and(isDeclaredBy(Foo.class) - .and(returns(String.class))) - ) - .intercept(MethodDelegation.to(Bar.class)) - .make() - .load(getClass().getClassLoader()) - .getLoaded() - .newInstance() - .sayHelloFoo(); + String r = new ByteBuddy().subclass(Foo.class).method(named("sayHelloFoo").and(isDeclaredBy(Foo.class).and(returns(String.class)))).intercept(MethodDelegation.to(Bar.class)).make().load(getClass().getClassLoader()).getLoaded().newInstance() + .sayHelloFoo(); assertEquals(r, Bar.sayHelloBar()); } @Test public void givenMethodName_whenDefineMethod_thenCreateMethod() throws Exception { - Class type = new ByteBuddy() - .subclass(Object.class) - .name("MyClassName") - .defineMethod("custom", String.class, Modifier.PUBLIC) - .intercept(MethodDelegation.to(Bar.class)) - .defineField("x", String.class, Modifier.PUBLIC) - .make() - .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) - .getLoaded(); + Class type = new ByteBuddy().subclass(Object.class).name("MyClassName").defineMethod("custom", String.class, Modifier.PUBLIC).intercept(MethodDelegation.to(Bar.class)).defineField("x", String.class, Modifier.PUBLIC).make() + .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER).getLoaded(); Method m = type.getDeclaredMethod("custom", null); @@ -85,5 +57,4 @@ public class ByteBuddyUnitTest { } - } diff --git a/libraries/src/test/java/com/baeldung/caffeine/CaffeineUnitTest.java b/libraries/src/test/java/com/baeldung/caffeine/CaffeineUnitTest.java index 56dbda5974..d523d0ff8b 100644 --- a/libraries/src/test/java/com/baeldung/caffeine/CaffeineUnitTest.java +++ b/libraries/src/test/java/com/baeldung/caffeine/CaffeineUnitTest.java @@ -16,10 +16,7 @@ public class CaffeineUnitTest { @Test public void givenCache_whenPopulate_thenValueStored() { - Cache cache = Caffeine.newBuilder() - .expireAfterWrite(1, TimeUnit.MINUTES) - .maximumSize(100) - .build(); + Cache cache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).maximumSize(100).build(); String key = "A"; DataObject dataObject = cache.getIfPresent(key); @@ -44,10 +41,7 @@ public class CaffeineUnitTest { @Test public void givenLoadingCache_whenGet_thenValuePopulated() { - LoadingCache cache = Caffeine.newBuilder() - .maximumSize(100) - .expireAfterWrite(1, TimeUnit.MINUTES) - .build(k -> DataObject.get("Data for " + k)); + LoadingCache cache = Caffeine.newBuilder().maximumSize(100).expireAfterWrite(1, TimeUnit.MINUTES).build(k -> DataObject.get("Data for " + k)); String key = "A"; DataObject dataObject = cache.get(key); @@ -63,10 +57,7 @@ public class CaffeineUnitTest { @Test public void givenAsyncLoadingCache_whenGet_thenValuePopulated() { - AsyncLoadingCache cache = Caffeine.newBuilder() - .maximumSize(100) - .expireAfterWrite(1, TimeUnit.MINUTES) - .buildAsync(k -> DataObject.get("Data for " + k)); + AsyncLoadingCache cache = Caffeine.newBuilder().maximumSize(100).expireAfterWrite(1, TimeUnit.MINUTES).buildAsync(k -> DataObject.get("Data for " + k)); String key = "A"; cache.get(key).thenAccept(dataObject -> { @@ -74,16 +65,12 @@ public class CaffeineUnitTest { assertEquals("Data for " + key, dataObject.getData()); }); - cache.getAll(Arrays.asList("A", "B", "C")) - .thenAccept(dataObjectMap -> assertEquals(3, dataObjectMap.size())); + cache.getAll(Arrays.asList("A", "B", "C")).thenAccept(dataObjectMap -> assertEquals(3, dataObjectMap.size())); } @Test public void givenLoadingCacheWithSmallSize_whenPut_thenSizeIsConstant() { - LoadingCache cache = Caffeine.newBuilder() - .maximumSize(1) - .refreshAfterWrite(10, TimeUnit.MINUTES) - .build(k -> DataObject.get("Data for " + k)); + LoadingCache cache = Caffeine.newBuilder().maximumSize(1).refreshAfterWrite(10, TimeUnit.MINUTES).build(k -> DataObject.get("Data for " + k)); assertEquals(0, cache.estimatedSize()); @@ -99,10 +86,7 @@ public class CaffeineUnitTest { @Test public void givenLoadingCacheWithWeigher_whenPut_thenSizeIsConstant() { - LoadingCache cache = Caffeine.newBuilder() - .maximumWeight(10) - .weigher((k,v) -> 5) - .build(k -> DataObject.get("Data for " + k)); + LoadingCache cache = Caffeine.newBuilder().maximumWeight(10).weigher((k, v) -> 5).build(k -> DataObject.get("Data for " + k)); assertEquals(0, cache.estimatedSize()); @@ -122,20 +106,11 @@ public class CaffeineUnitTest { @Test public void givenTimeEvictionCache_whenTimeLeft_thenValueEvicted() { - LoadingCache cache = Caffeine.newBuilder() - .expireAfterAccess(5, TimeUnit.MINUTES) - .build(k -> DataObject.get("Data for " + k)); + LoadingCache cache = Caffeine.newBuilder().expireAfterAccess(5, TimeUnit.MINUTES).build(k -> DataObject.get("Data for " + k)); - cache = Caffeine.newBuilder() - .expireAfterWrite(10, TimeUnit.SECONDS) - .weakKeys() - .weakValues() - .build(k -> DataObject.get("Data for " + k)); + cache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).weakKeys().weakValues().build(k -> DataObject.get("Data for " + k)); - cache = Caffeine.newBuilder() - .expireAfterWrite(10, TimeUnit.SECONDS) - .softValues() - .build(k -> DataObject.get("Data for " + k)); + cache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).softValues().build(k -> DataObject.get("Data for " + k)); cache = Caffeine.newBuilder().expireAfter(new Expiry() { @Override @@ -154,17 +129,12 @@ public class CaffeineUnitTest { } }).build(k -> DataObject.get("Data for " + k)); - cache = Caffeine.newBuilder() - .refreshAfterWrite(1, TimeUnit.MINUTES) - .build(k -> DataObject.get("Data for " + k)); + cache = Caffeine.newBuilder().refreshAfterWrite(1, TimeUnit.MINUTES).build(k -> DataObject.get("Data for " + k)); } @Test public void givenCache_whenStatsEnabled_thenStatsRecorded() { - LoadingCache cache = Caffeine.newBuilder() - .maximumSize(100) - .recordStats() - .build(k -> DataObject.get("Data for " + k)); + LoadingCache cache = Caffeine.newBuilder().maximumSize(100).recordStats().build(k -> DataObject.get("Data for " + k)); cache.get("A"); cache.get("A"); diff --git a/libraries/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorIntegrationTest.java b/libraries/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorIntegrationTest.java index 1224d73724..080444d7b1 100644 --- a/libraries/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorIntegrationTest.java +++ b/libraries/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorIntegrationTest.java @@ -1,6 +1,5 @@ package com.baeldung.cglib.proxy; - import net.sf.cglib.beans.BeanGenerator; import org.junit.Test; @@ -12,21 +11,17 @@ public class BeanGeneratorIntegrationTest { @Test public void givenBeanCreator_whenAddProperty_thenClassShouldHaveFieldValue() throws Exception { - //given + // given BeanGenerator beanGenerator = new BeanGenerator(); - //when + // when beanGenerator.addProperty("name", String.class); Object myBean = beanGenerator.create(); - Method setter = myBean - .getClass() - .getMethod("setName", String.class); + Method setter = myBean.getClass().getMethod("setName", String.class); setter.invoke(myBean, "some string value set by a cglib"); - //then - Method getter = myBean - .getClass() - .getMethod("getName"); + // then + Method getter = myBean.getClass().getMethod("getName"); assertEquals("some string value set by a cglib", getter.invoke(myBean)); } } diff --git a/libraries/src/test/java/com/baeldung/cglib/proxy/MixinUnitTest.java b/libraries/src/test/java/com/baeldung/cglib/proxy/MixinUnitTest.java index 93b34bf92b..fc2f6cc1e1 100644 --- a/libraries/src/test/java/com/baeldung/cglib/proxy/MixinUnitTest.java +++ b/libraries/src/test/java/com/baeldung/cglib/proxy/MixinUnitTest.java @@ -14,14 +14,11 @@ public class MixinUnitTest { @Test public void givenTwoClasses_whenMixedIntoOne_thenMixinShouldHaveMethodsFromBothClasses() throws Exception { - //when - Mixin mixin = Mixin.create( - new Class[]{Interface1.class, Interface2.class, MixinInterface.class}, - new Object[]{new Class1(), new Class2()} - ); + // when + Mixin mixin = Mixin.create(new Class[] { Interface1.class, Interface2.class, MixinInterface.class }, new Object[] { new Class1(), new Class2() }); MixinInterface mixinDelegate = (MixinInterface) mixin; - //then + // then assertEquals("first behaviour", mixinDelegate.first()); assertEquals("second behaviour", mixinDelegate.second()); } diff --git a/libraries/src/test/java/com/baeldung/cglib/proxy/ProxyIntegrationTest.java b/libraries/src/test/java/com/baeldung/cglib/proxy/ProxyIntegrationTest.java index c22a148d4f..d5c8a1b589 100644 --- a/libraries/src/test/java/com/baeldung/cglib/proxy/ProxyIntegrationTest.java +++ b/libraries/src/test/java/com/baeldung/cglib/proxy/ProxyIntegrationTest.java @@ -10,34 +10,34 @@ import static org.junit.Assert.assertEquals; public class ProxyIntegrationTest { @Test public void givenPersonService_whenSayHello_thenReturnResult() { - //given + // given PersonService personService = new PersonService(); - //when + // when String res = personService.sayHello("Tom"); - //then + // then assertEquals(res, "Hello Tom"); } @Test public void givenEnhancerProxy_whenExtendPersonService_thenInterceptMethod() throws Exception { - //given + // given Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(PersonService.class); enhancer.setCallback((FixedValue) () -> "Hello Tom!"); PersonService proxy = (PersonService) enhancer.create(); - //when + // when String res = proxy.sayHello(null); - //then + // then assertEquals("Hello Tom!", res); } @Test public void givenEnhancer_whenExecuteMethodOnProxy_thenInterceptOnlyStringReturnTypeMethod() throws Exception { - //given + // given Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(PersonService.class); enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> { @@ -48,10 +48,10 @@ public class ProxyIntegrationTest { } }); - //when + // when PersonService proxy = (PersonService) enhancer.create(); - //then + // then assertEquals("Hello Tom!", proxy.sayHello(null)); int lengthOfName = proxy.lengthOfName("Mary"); assertEquals(4, lengthOfName); diff --git a/libraries/src/test/java/com/baeldung/chronicle/queue/ChronicleQueueIntegrationTest.java b/libraries/src/test/java/com/baeldung/chronicle/queue/ChronicleQueueIntegrationTest.java index 9c0a0ac910..00e9500318 100644 --- a/libraries/src/test/java/com/baeldung/chronicle/queue/ChronicleQueueIntegrationTest.java +++ b/libraries/src/test/java/com/baeldung/chronicle/queue/ChronicleQueueIntegrationTest.java @@ -14,12 +14,12 @@ import net.openhft.chronicle.ExcerptTailer; import net.openhft.chronicle.tools.ChronicleTools; public class ChronicleQueueIntegrationTest { - + @Test public void givenSetOfValues_whenWriteToQueue_thenWriteSuccesfully() throws IOException { File queueDir = Files.createTempDirectory("chronicle-queue").toFile(); ChronicleTools.deleteOnExit(queueDir.getPath()); - + Chronicle chronicle = ChronicleQueueBuilder.indexed(queueDir).build(); String stringVal = "Hello World"; int intVal = 101; @@ -37,7 +37,7 @@ public class ChronicleQueueIntegrationTest { } tailer.finish(); tailer.close(); - chronicle.close(); + chronicle.close(); } } diff --git a/libraries/src/test/java/com/baeldung/commons/beanutils/CourseServiceTest.java b/libraries/src/test/java/com/baeldung/commons/beanutils/CourseServiceUnitTest.java similarity index 86% rename from libraries/src/test/java/com/baeldung/commons/beanutils/CourseServiceTest.java rename to libraries/src/test/java/com/baeldung/commons/beanutils/CourseServiceUnitTest.java index 5407477a00..833d91b2c4 100644 --- a/libraries/src/test/java/com/baeldung/commons/beanutils/CourseServiceTest.java +++ b/libraries/src/test/java/com/baeldung/commons/beanutils/CourseServiceUnitTest.java @@ -7,11 +7,10 @@ import java.util.List; import org.junit.Assert; import org.junit.Test; -public class CourseServiceTest { +public class CourseServiceUnitTest { @Test - public void givenCourse_whenSetValuesUsingPropertyUtil_thenReturnSetValues() - throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { + public void givenCourse_whenSetValuesUsingPropertyUtil_thenReturnSetValues() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Course course = new Course(); String name = "Computer Science"; List codes = Arrays.asList("CS", "CS01"); @@ -36,8 +35,7 @@ public class CourseServiceTest { } @Test - public void givenCopyProperties_whenCopyCourseToCourseEntity_thenCopyPropertyWithSameName() - throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { + public void givenCopyProperties_whenCopyCourseToCourseEntity_thenCopyPropertyWithSameName() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Course course = new Course(); course.setName("Computer Science"); course.setCodes(Arrays.asList("CS")); diff --git a/libraries/src/test/java/com/baeldung/commons/collections/CollectionUtilsGuideTest.java b/libraries/src/test/java/com/baeldung/commons/collections/CollectionUtilsGuideTest.java index aa8b799c9d..f34f431d8e 100644 --- a/libraries/src/test/java/com/baeldung/commons/collections/CollectionUtilsGuideTest.java +++ b/libraries/src/test/java/com/baeldung/commons/collections/CollectionUtilsGuideTest.java @@ -1,6 +1,5 @@ package com.baeldung.commons.collections; - import com.baeldung.commons.collectionutil.Address; import com.baeldung.commons.collectionutil.Customer; import org.apache.commons.collections4.CollectionUtils; @@ -21,7 +20,6 @@ import static org.junit.Assert.assertTrue; public class CollectionUtilsGuideTest { - Customer customer1 = new Customer(1, "Daniel", 123456l, "locality1", "city1", "1234"); Customer customer4 = new Customer(4, "Bob", 456789l, "locality4", "city4", "4567"); List list1, list2, list3, linkedList1; @@ -77,8 +75,8 @@ public class CollectionUtilsGuideTest { } }); - //filterInverse does the opposite. It removes the element from the list if the Predicate returns true - //select and selectRejected work the same way except that they do not remove elements from the given collection and return a new collection + // filterInverse does the opposite. It removes the element from the list if the Predicate returns true + // select and selectRejected work the same way except that they do not remove elements from the given collection and return a new collection assertTrue(isModified && linkedList1.size() == 2); } @@ -88,8 +86,8 @@ public class CollectionUtilsGuideTest { List emptyList = new ArrayList<>(); List nullList = null; - //Very handy at times where we want to check if a collection is not null and not empty too. - //isNotEmpty does the opposite. Handy because using ! operator on isEmpty makes it missable while reading + // Very handy at times where we want to check if a collection is not null and not empty too. + // isNotEmpty does the opposite. Handy because using ! operator on isEmpty makes it missable while reading assertTrue(CollectionUtils.isNotEmpty(list1)); assertTrue(CollectionUtils.isEmpty(nullList)); assertTrue(CollectionUtils.isEmpty(emptyList)); diff --git a/libraries/src/test/java/com/baeldung/commons/collections/MapUtilsTest.java b/libraries/src/test/java/com/baeldung/commons/collections/MapUtilsTest.java index 4685d84781..988335b7d1 100644 --- a/libraries/src/test/java/com/baeldung/commons/collections/MapUtilsTest.java +++ b/libraries/src/test/java/com/baeldung/commons/collections/MapUtilsTest.java @@ -1,6 +1,5 @@ package com.baeldung.commons.collections; -import org.apache.commons.collections4.MapIterator; import org.apache.commons.collections4.MapUtils; import org.apache.commons.collections4.PredicateUtils; import org.apache.commons.collections4.TransformerUtils; @@ -8,35 +7,20 @@ import org.assertj.core.api.Assertions; import org.junit.Before; import org.junit.Test; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.Collection; import java.util.HashMap; import java.util.Map; -import java.util.Set; -import static org.hamcrest.CoreMatchers.not; -import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.collection.IsMapContaining.hasEntry; import static org.hamcrest.collection.IsMapWithSize.aMapWithSize; import static org.hamcrest.collection.IsMapWithSize.anEmptyMap; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; public class MapUtilsTest { - private String[][] color2DArray = new String[][]{ - {"RED", "#FF0000"}, - {"GREEN", "#00FF00"}, - {"BLUE", "#0000FF"} - }; - private String[] color1DArray = new String[]{ - "RED", "#FF0000", - "GREEN", "#00FF00", - "BLUE", "#0000FF" - }; + private String[][] color2DArray = new String[][] { { "RED", "#FF0000" }, { "GREEN", "#00FF00" }, { "BLUE", "#0000FF" } }; + private String[] color1DArray = new String[] { "RED", "#FF0000", "GREEN", "#00FF00", "BLUE", "#0000FF" }; private Map colorMap; @Before @@ -92,34 +76,26 @@ public class MapUtilsTest { Map invColorMap = MapUtils.invertMap(this.colorMap); int size = invColorMap.size(); - Assertions.assertThat(invColorMap) - .hasSameSizeAs(colorMap) - .containsKeys(this.colorMap.values().toArray(new String[size])) - .containsValues(this.colorMap.keySet().toArray(new String[size])); + Assertions.assertThat(invColorMap).hasSameSizeAs(colorMap).containsKeys(this.colorMap.values().toArray(new String[size])).containsValues(this.colorMap.keySet().toArray(new String[size])); } @Test(expected = IllegalArgumentException.class) public void whenCreateFixedSizedMapAndAdd_thenMustThrowException() { - Map rgbMap = MapUtils.fixedSizeMap(MapUtils.putAll( - new HashMap(), - this.color1DArray)); + Map rgbMap = MapUtils.fixedSizeMap(MapUtils.putAll(new HashMap(), this.color1DArray)); rgbMap.put("ORANGE", "#FFA500"); } @Test(expected = IllegalArgumentException.class) public void whenAddDuplicateToUniqueValuesPredicateMap_thenMustThrowException() { - Map uniqValuesMap - = MapUtils.predicatedMap(this.colorMap, null, PredicateUtils.uniquePredicate()); + Map uniqValuesMap = MapUtils.predicatedMap(this.colorMap, null, PredicateUtils.uniquePredicate()); uniqValuesMap.put("NEW_RED", "#FF0000"); } @Test public void whenCreateLazyMap_theMapIsCreated() { - Map intStrMap = MapUtils.lazyMap( - new HashMap(), - TransformerUtils.stringValueTransformer()); + Map intStrMap = MapUtils.lazyMap(new HashMap(), TransformerUtils.stringValueTransformer()); assertThat(intStrMap, is(anEmptyMap())); diff --git a/libraries/src/test/java/com/baeldung/commons/collections/SetUtilsUnitTest.java b/libraries/src/test/java/com/baeldung/commons/collections/SetUtilsUnitTest.java index 7d214bc5c5..aa73ed6109 100644 --- a/libraries/src/test/java/com/baeldung/commons/collections/SetUtilsUnitTest.java +++ b/libraries/src/test/java/com/baeldung/commons/collections/SetUtilsUnitTest.java @@ -17,8 +17,7 @@ public class SetUtilsUnitTest { public void givenSetAndPredicate_whenPredicatedSet_thenValidateSet_and_throw_IllegalArgumentException() { Set sourceSet = new HashSet<>(); sourceSet.addAll(Arrays.asList("London", "Lagos", "Err Source1")); - Set validatingSet - = SetUtils.predicatedSet(sourceSet, (s) -> s.startsWith("L")); + Set validatingSet = SetUtils.predicatedSet(sourceSet, (s) -> s.startsWith("L")); validatingSet.add("Err Source2"); } diff --git a/libraries/src/test/java/com/baeldung/commons/collections/orderedmap/OrderedMapUnitTest.java b/libraries/src/test/java/com/baeldung/commons/collections/orderedmap/OrderedMapUnitTest.java index 7a05228e51..c64143cba7 100644 --- a/libraries/src/test/java/com/baeldung/commons/collections/orderedmap/OrderedMapUnitTest.java +++ b/libraries/src/test/java/com/baeldung/commons/collections/orderedmap/OrderedMapUnitTest.java @@ -14,8 +14,8 @@ import static org.junit.Assert.assertEquals; public class OrderedMapUnitTest { - private String[] names = {"Emily", "Mathew", "Rose", "John", "Anna"}; - private Integer[] ages = {37, 28, 40, 36, 21}; + private String[] names = { "Emily", "Mathew", "Rose", "John", "Anna" }; + private Integer[] ages = { 37, 28, 40, 36, 21 }; private int RUNNERS_COUNT = names.length; diff --git a/libraries/src/test/java/com/baeldung/commons/collections4/BagTests.java b/libraries/src/test/java/com/baeldung/commons/collections4/BagTests.java index 55fadcbf85..4408dcc195 100644 --- a/libraries/src/test/java/com/baeldung/commons/collections4/BagTests.java +++ b/libraries/src/test/java/com/baeldung/commons/collections4/BagTests.java @@ -15,94 +15,88 @@ import static org.hamcrest.core.Is.is; import static org.hamcrest.core.IsEqual.equalTo; public class BagTests { - + @Test public void givenMultipleCopies_whenAdded_theCountIsKept() { - Bag bag = new HashBag<>( - Arrays.asList(new Integer[] { 1, 2, 3, 3, 3, 1, 4 })); - + Bag bag = new HashBag<>(Arrays.asList(new Integer[] { 1, 2, 3, 3, 3, 1, 4 })); + assertThat(bag.getCount(1), equalTo(2)); } - + @Test public void givenBag_whenBagAddAPILikeCollectionAPI_thenFalse() { Collection collection = new ArrayList<>(); - + // Collection contract defines that add() should return true assertThat(collection.add(9), is(true)); - + // Even when element is already in the collection collection.add(1); assertThat(collection.add(1), is(true)); - + Bag bag = new HashBag<>(); - + // Bag returns true on adding a new element assertThat(bag.add(9), is(true)); - + bag.add(1); // But breaks the contract with false when it has to increment the count assertThat(bag.add(1), is(not(true))); } - + @Test public void givenDecoratedBag_whenBagAddAPILikeCollectionAPI_thenTrue() { Bag bag = CollectionBag.collectionBag(new HashBag<>()); - + bag.add(1); // This time the behavior is compliant to the Java Collection assertThat(bag.add(1), is((true))); } - + @Test public void givenAdd_whenCountOfElementsDefined_thenCountAreAdded() { Bag bag = new HashBag<>(); - + // Adding 1 for 5 times bag.add(1, 5); assertThat(bag.getCount(1), equalTo(5)); } - + @Test public void givenMultipleCopies_whenRemove_allAreRemoved() { - Bag bag = new HashBag<>( - Arrays.asList(new Integer[] { 1, 2, 3, 3, 3, 1, 4 })); - + Bag bag = new HashBag<>(Arrays.asList(new Integer[] { 1, 2, 3, 3, 3, 1, 4 })); + // From 3 we delete 1, 2 remain bag.remove(3, 1); assertThat(bag.getCount(3), equalTo(2)); - + // From 2 we delete all bag.remove(1); assertThat(bag.getCount(1), equalTo(0)); } - + @Test public void givenTree_whenDuplicateElementsAdded_thenSort() { - TreeBag bag = new TreeBag<>( - Arrays.asList(new Integer[] { 7, 5, 1, 7, 2, 3, 3, 3, 1, 4, 7 })); - + TreeBag bag = new TreeBag<>(Arrays.asList(new Integer[] { 7, 5, 1, 7, 2, 3, 3, 3, 1, 4, 7 })); + assertThat(bag.first(), equalTo(1)); assertThat(bag.getCount(bag.first()), equalTo(2)); assertThat(bag.last(), equalTo(7)); assertThat(bag.getCount(bag.last()), equalTo(3)); } - + @Test public void givenDecoratedTree_whenTreeAddAPILikeCollectionAPI_thenTrue() { - SortedBag bag = CollectionSortedBag - .collectionSortedBag(new TreeBag<>()); - + SortedBag bag = CollectionSortedBag.collectionSortedBag(new TreeBag<>()); + bag.add(1); assertThat(bag.add(1), is((true))); } - + @Test public void givenSortedBag_whenDuplicateElementsAdded_thenSort() { - SynchronizedSortedBag bag = SynchronizedSortedBag - .synchronizedSortedBag(new TreeBag<>( - Arrays.asList(new Integer[] { 7, 5, 1, 7, 2, 3, 3, 3, 1, 4, 7 }))); - + SynchronizedSortedBag bag = SynchronizedSortedBag.synchronizedSortedBag(new TreeBag<>(Arrays.asList(new Integer[] { 7, 5, 1, 7, 2, 3, 3, 3, 1, 4, 7 }))); + assertThat(bag.first(), equalTo(1)); assertThat(bag.getCount(bag.first()), equalTo(2)); assertThat(bag.last(), equalTo(7)); diff --git a/libraries/src/test/java/com/baeldung/commons/csv/CSVReaderWriterTest.java b/libraries/src/test/java/com/baeldung/commons/csv/CSVReaderWriterTest.java index 6f47b89396..6210bb51a9 100644 --- a/libraries/src/test/java/com/baeldung/commons/csv/CSVReaderWriterTest.java +++ b/libraries/src/test/java/com/baeldung/commons/csv/CSVReaderWriterTest.java @@ -29,10 +29,7 @@ public class CSVReaderWriterTest { @Test public void givenCSVFile_whenRead_thenContentsAsExpected() throws IOException { Reader in = new FileReader("src/test/resources/book.csv"); - Iterable records = CSVFormat.DEFAULT - .withHeader(HEADERS) - .withFirstRecordAsHeader() - .parse(in); + Iterable records = CSVFormat.DEFAULT.withHeader(HEADERS).withFirstRecordAsHeader().parse(in); for (CSVRecord record : records) { String author = record.get("author"); String title = record.get("title"); @@ -52,9 +49,7 @@ public class CSVReaderWriterTest { } }); } - assertEquals(EXPECTED_FILESTREAM, sw - .toString() - .trim()); + assertEquals(EXPECTED_FILESTREAM, sw.toString().trim()); } } diff --git a/libraries/src/test/java/com/baeldung/commons/dbutils/DbUtilsUnitTest.java b/libraries/src/test/java/com/baeldung/commons/dbutils/DbUtilsUnitTest.java index bc7623589c..02cec7d53a 100644 --- a/libraries/src/test/java/com/baeldung/commons/dbutils/DbUtilsUnitTest.java +++ b/libraries/src/test/java/com/baeldung/commons/dbutils/DbUtilsUnitTest.java @@ -47,10 +47,8 @@ public class DbUtilsUnitTest { List> list = runner.query(connection, "SELECT * FROM employee", beanListHandler); assertEquals(list.size(), 5); - assertEquals(list.get(0) - .get("firstname"), "John"); - assertEquals(list.get(4) - .get("firstname"), "Christian"); + assertEquals(list.get(0).get("firstname"), "John"); + assertEquals(list.get(4).get("firstname"), "Christian"); } @Test @@ -61,10 +59,8 @@ public class DbUtilsUnitTest { List employeeList = runner.query(connection, "SELECT * FROM employee", beanListHandler); assertEquals(employeeList.size(), 5); - assertEquals(employeeList.get(0) - .getFirstName(), "John"); - assertEquals(employeeList.get(4) - .getFirstName(), "Christian"); + assertEquals(employeeList.get(0).getFirstName(), "John"); + assertEquals(employeeList.get(4).getFirstName(), "Christian"); } @Test @@ -85,12 +81,8 @@ public class DbUtilsUnitTest { QueryRunner runner = new QueryRunner(); List employees = runner.query(connection, "SELECT * FROM employee", employeeHandler); - assertEquals(employees.get(0) - .getEmails() - .size(), 2); - assertEquals(employees.get(2) - .getEmails() - .size(), 3); + assertEquals(employees.get(0).getEmails().size(), 2); + assertEquals(employees.get(2).getEmails().size(), 3); assertNotNull(employees.get(0).getEmails().get(0).getEmployeeId()); } diff --git a/libraries/src/test/java/com/baeldung/commons/io/CommonsIOUnitTest.java b/libraries/src/test/java/com/baeldung/commons/io/CommonsIOUnitTest.java index 3c82c30d9b..7481e5a1a3 100644 --- a/libraries/src/test/java/com/baeldung/commons/io/CommonsIOUnitTest.java +++ b/libraries/src/test/java/com/baeldung/commons/io/CommonsIOUnitTest.java @@ -25,30 +25,23 @@ import java.nio.charset.Charset; public class CommonsIOUnitTest { @Test - public void whenCopyANDReadFileTesttxt_thenMatchExpectedData() - throws IOException { + public void whenCopyANDReadFileTesttxt_thenMatchExpectedData() throws IOException { String expectedData = "Hello World from fileTest.txt!!!"; - File file = FileUtils.getFile(getClass().getClassLoader() - .getResource("fileTest.txt") - .getPath()); + File file = FileUtils.getFile(getClass().getClassLoader().getResource("fileTest.txt").getPath()); File tempDir = FileUtils.getTempDirectory(); FileUtils.copyFileToDirectory(file, tempDir); File newTempFile = FileUtils.getFile(tempDir, file.getName()); - String data = FileUtils.readFileToString(newTempFile, - Charset.defaultCharset()); + String data = FileUtils.readFileToString(newTempFile, Charset.defaultCharset()); Assert.assertEquals(expectedData, data.trim()); } @Test - public void whenUsingFileNameUtils_thenshowdifferentFileOperations() - throws IOException { + public void whenUsingFileNameUtils_thenshowdifferentFileOperations() throws IOException { - String path = getClass().getClassLoader() - .getResource("fileTest.txt") - .getPath(); + String path = getClass().getClassLoader().getResource("fileTest.txt").getPath(); String fullPath = FilenameUtils.getFullPath(path); String extension = FilenameUtils.getExtension(path); @@ -60,70 +53,54 @@ public class CommonsIOUnitTest { } @Test - public void whenUsingFileSystemUtils_thenDriveFreeSpace() - throws IOException { + public void whenUsingFileSystemUtils_thenDriveFreeSpace() throws IOException { long freeSpace = FileSystemUtils.freeSpaceKb("/"); } @SuppressWarnings("resource") @Test - public void whenUsingTeeInputOutputStream_thenWriteto2OutputStreams() - throws IOException { + public void whenUsingTeeInputOutputStream_thenWriteto2OutputStreams() throws IOException { final String str = "Hello World."; ByteArrayInputStream inputStream = new ByteArrayInputStream(str.getBytes()); ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream(); - - FilterOutputStream teeOutputStream = new TeeOutputStream(outputStream1,outputStream2); + + FilterOutputStream teeOutputStream = new TeeOutputStream(outputStream1, outputStream2); new TeeInputStream(inputStream, teeOutputStream, true).read(new byte[str.length()]); - + Assert.assertEquals(str, String.valueOf(outputStream1)); Assert.assertEquals(str, String.valueOf(outputStream2)); - } + } @Test - public void whenGetFilewithNameFileFilter_thenFindfileTesttxt() - throws IOException { + public void whenGetFilewithNameFileFilter_thenFindfileTesttxt() throws IOException { final String testFile = "fileTest.txt"; - String path = getClass().getClassLoader() - .getResource(testFile) - .getPath(); + String path = getClass().getClassLoader().getResource(testFile).getPath(); File dir = FileUtils.getFile(FilenameUtils.getFullPath(path)); String[] possibleNames = { "NotThisOne", testFile }; - Assert.assertEquals(testFile, - dir.list(new NameFileFilter(possibleNames, IOCase.INSENSITIVE))[0]); + Assert.assertEquals(testFile, dir.list(new NameFileFilter(possibleNames, IOCase.INSENSITIVE))[0]); } @Test - public void whenGetFilewith_ANDFileFilter_thenFindsampletxt() - throws IOException { + public void whenGetFilewith_ANDFileFilter_thenFindsampletxt() throws IOException { - String path = getClass().getClassLoader() - .getResource("fileTest.txt") - .getPath(); + String path = getClass().getClassLoader().getResource("fileTest.txt").getPath(); File dir = FileUtils.getFile(FilenameUtils.getFullPath(path)); - Assert.assertEquals("sample.txt", - dir.list(new AndFileFilter( - new WildcardFileFilter("*ple*", IOCase.INSENSITIVE), - new SuffixFileFilter("txt")))[0]); + Assert.assertEquals("sample.txt", dir.list(new AndFileFilter(new WildcardFileFilter("*ple*", IOCase.INSENSITIVE), new SuffixFileFilter("txt")))[0]); } @Test - public void whenSortDirWithPathFileComparator_thenFirstFileaaatxt() - throws IOException { + public void whenSortDirWithPathFileComparator_thenFirstFileaaatxt() throws IOException { - PathFileComparator pathFileComparator = new PathFileComparator( - IOCase.INSENSITIVE); - String path = FilenameUtils.getFullPath(getClass().getClassLoader() - .getResource("fileTest.txt") - .getPath()); + PathFileComparator pathFileComparator = new PathFileComparator(IOCase.INSENSITIVE); + String path = FilenameUtils.getFullPath(getClass().getClassLoader().getResource("fileTest.txt").getPath()); File dir = new File(path); File[] files = dir.listFiles(); @@ -133,16 +110,11 @@ public class CommonsIOUnitTest { } @Test - public void whenSizeFileComparator_thenLargerFile() - throws IOException { + public void whenSizeFileComparator_thenLargerFile() throws IOException { SizeFileComparator sizeFileComparator = new SizeFileComparator(); - File largerFile = FileUtils.getFile(getClass().getClassLoader() - .getResource("fileTest.txt") - .getPath()); - File smallerFile = FileUtils.getFile(getClass().getClassLoader() - .getResource("sample.txt") - .getPath()); + File largerFile = FileUtils.getFile(getClass().getClassLoader().getResource("fileTest.txt").getPath()); + File smallerFile = FileUtils.getFile(getClass().getClassLoader().getResource("sample.txt").getPath()); int i = sizeFileComparator.compare(largerFile, smallerFile); diff --git a/libraries/src/test/java/com/baeldung/commons/lang3/ArrayUtilsUnitTest.java b/libraries/src/test/java/com/baeldung/commons/lang3/ArrayUtilsUnitTest.java index 97db4ddbe4..f34008fb1f 100644 --- a/libraries/src/test/java/com/baeldung/commons/lang3/ArrayUtilsUnitTest.java +++ b/libraries/src/test/java/com/baeldung/commons/lang3/ArrayUtilsUnitTest.java @@ -9,71 +9,71 @@ import static org.junit.Assert.assertEquals; public class ArrayUtilsUnitTest { @Test public void givenArray_whenAddingElementAtSpecifiedPosition_thenCorrect() { - int[] oldArray = {2, 3, 4, 5}; + int[] oldArray = { 2, 3, 4, 5 }; int[] newArray = ArrayUtils.add(oldArray, 0, 1); - int[] expectedArray = {1, 2, 3, 4, 5}; + int[] expectedArray = { 1, 2, 3, 4, 5 }; assertArrayEquals(expectedArray, newArray); } @Test public void givenArray_whenAddingElementAtTheEnd_thenCorrect() { - int[] oldArray = {2, 3, 4, 5}; + int[] oldArray = { 2, 3, 4, 5 }; int[] newArray = ArrayUtils.add(oldArray, 1); - int[] expectedArray = {2, 3, 4, 5, 1}; + int[] expectedArray = { 2, 3, 4, 5, 1 }; assertArrayEquals(expectedArray, newArray); } @Test public void givenArray_whenAddingAllElementsAtTheEnd_thenCorrect() { - int[] oldArray = {0, 1, 2}; + int[] oldArray = { 0, 1, 2 }; int[] newArray = ArrayUtils.addAll(oldArray, 3, 4, 5); - int[] expectedArray = {0, 1, 2, 3, 4, 5}; + int[] expectedArray = { 0, 1, 2, 3, 4, 5 }; assertArrayEquals(expectedArray, newArray); } @Test public void givenArray_whenRemovingElementAtSpecifiedPosition_thenCorrect() { - int[] oldArray = {1, 2, 3, 4, 5}; + int[] oldArray = { 1, 2, 3, 4, 5 }; int[] newArray = ArrayUtils.remove(oldArray, 1); - int[] expectedArray = {1, 3, 4, 5}; + int[] expectedArray = { 1, 3, 4, 5 }; assertArrayEquals(expectedArray, newArray); } @Test public void givenArray_whenRemovingAllElementsAtSpecifiedPositions_thenCorrect() { - int[] oldArray = {1, 2, 3, 4, 5}; + int[] oldArray = { 1, 2, 3, 4, 5 }; int[] newArray = ArrayUtils.removeAll(oldArray, 1, 3); - int[] expectedArray = {1, 3, 5}; + int[] expectedArray = { 1, 3, 5 }; assertArrayEquals(expectedArray, newArray); } @Test public void givenArray_whenRemovingAnElement_thenCorrect() { - int[] oldArray = {1, 2, 3, 3, 4}; + int[] oldArray = { 1, 2, 3, 3, 4 }; int[] newArray = ArrayUtils.removeElement(oldArray, 3); - int[] expectedArray = {1, 2, 3, 4}; + int[] expectedArray = { 1, 2, 3, 4 }; assertArrayEquals(expectedArray, newArray); } @Test public void givenArray_whenRemovingElements_thenCorrect() { - int[] oldArray = {1, 2, 3, 3, 4}; + int[] oldArray = { 1, 2, 3, 3, 4 }; int[] newArray = ArrayUtils.removeElements(oldArray, 2, 3, 5); - int[] expectedArray = {1, 3, 4}; + int[] expectedArray = { 1, 3, 4 }; assertArrayEquals(expectedArray, newArray); } @Test public void givenArray_whenRemovingAllElementOccurences_thenCorrect() { - int[] oldArray = {1, 2, 2, 2, 3}; + int[] oldArray = { 1, 2, 2, 2, 3 }; int[] newArray = ArrayUtils.removeAllOccurences(oldArray, 2); - int[] expectedArray = {1, 3}; + int[] expectedArray = { 1, 3 }; assertArrayEquals(expectedArray, newArray); } @Test public void givenArray_whenCheckingExistingElement_thenCorrect() { - int[] array = {1, 3, 5, 7, 9}; + int[] array = { 1, 3, 5, 7, 9 }; boolean evenContained = ArrayUtils.contains(array, 2); boolean oddContained = ArrayUtils.contains(array, 7); assertEquals(false, evenContained); @@ -82,57 +82,57 @@ public class ArrayUtilsUnitTest { @Test public void givenArray_whenReversingElementsWithinARange_thenCorrect() { - int[] originalArray = {1, 2, 3, 4, 5}; + int[] originalArray = { 1, 2, 3, 4, 5 }; ArrayUtils.reverse(originalArray, 1, 4); - int[] expectedArray = {1, 4, 3, 2, 5}; + int[] expectedArray = { 1, 4, 3, 2, 5 }; assertArrayEquals(expectedArray, originalArray); } @Test public void givenArray_whenReversingAllElements_thenCorrect() { - int[] originalArray = {1, 2, 3, 4, 5}; + int[] originalArray = { 1, 2, 3, 4, 5 }; ArrayUtils.reverse(originalArray); - int[] expectedArray = {5, 4, 3, 2, 1}; + int[] expectedArray = { 5, 4, 3, 2, 1 }; assertArrayEquals(expectedArray, originalArray); } @Test public void givenArray_whenShiftingElementsWithinARange_thenCorrect() { - int[] originalArray = {1, 2, 3, 4, 5}; + int[] originalArray = { 1, 2, 3, 4, 5 }; ArrayUtils.shift(originalArray, 1, 4, 1); - int[] expectedArray = {1, 4, 2, 3, 5}; + int[] expectedArray = { 1, 4, 2, 3, 5 }; assertArrayEquals(expectedArray, originalArray); } @Test public void givenArray_whenShiftingAllElements_thenCorrect() { - int[] originalArray = {1, 2, 3, 4, 5}; + int[] originalArray = { 1, 2, 3, 4, 5 }; ArrayUtils.shift(originalArray, 1); - int[] expectedArray = {5, 1, 2, 3, 4}; + int[] expectedArray = { 5, 1, 2, 3, 4 }; assertArrayEquals(expectedArray, originalArray); } @Test public void givenArray_whenExtractingElements_thenCorrect() { - int[] oldArray = {1, 2, 3, 4, 5}; + int[] oldArray = { 1, 2, 3, 4, 5 }; int[] newArray = ArrayUtils.subarray(oldArray, 2, 7); - int[] expectedArray = {3, 4, 5}; + int[] expectedArray = { 3, 4, 5 }; assertArrayEquals(expectedArray, newArray); } @Test public void givenArray_whenSwapingElementsWithinARange_thenCorrect() { - int[] originalArray = {1, 2, 3, 4, 5}; + int[] originalArray = { 1, 2, 3, 4, 5 }; ArrayUtils.swap(originalArray, 0, 3, 2); - int[] expectedArray = {4, 5, 3, 1, 2}; + int[] expectedArray = { 4, 5, 3, 1, 2 }; assertArrayEquals(expectedArray, originalArray); } @Test public void givenArray_whenSwapingElementsAtSpecifiedPositions_thenCorrect() { - int[] originalArray = {1, 2, 3, 4, 5}; + int[] originalArray = { 1, 2, 3, 4, 5 }; ArrayUtils.swap(originalArray, 0, 3); - int[] expectedArray = {4, 2, 3, 1, 5}; + int[] expectedArray = { 4, 2, 3, 1, 5 }; assertArrayEquals(expectedArray, originalArray); } } diff --git a/libraries/src/test/java/com/baeldung/commons/math/IntegrationTest.java b/libraries/src/test/java/com/baeldung/commons/math/IntegrationTest.java index 65c1a0db8e..7e047577e5 100644 --- a/libraries/src/test/java/com/baeldung/commons/math/IntegrationTest.java +++ b/libraries/src/test/java/com/baeldung/commons/math/IntegrationTest.java @@ -15,7 +15,7 @@ public class IntegrationTest { final double i = integrator.integrate(100, function, 0, 10); - Assert.assertEquals(16 + 2d/3d, i, 1e-7); + Assert.assertEquals(16 + 2d / 3d, i, 1e-7); } } diff --git a/libraries/src/test/java/com/baeldung/commons/math/LinearAlgebraUnitTest.java b/libraries/src/test/java/com/baeldung/commons/math/LinearAlgebraUnitTest.java index 278f54a67f..49bf33baa5 100644 --- a/libraries/src/test/java/com/baeldung/commons/math/LinearAlgebraUnitTest.java +++ b/libraries/src/test/java/com/baeldung/commons/math/LinearAlgebraUnitTest.java @@ -8,9 +8,7 @@ public class LinearAlgebraUnitTest { @Test public void whenDecompositionSolverSolve_thenCorrect() { - RealMatrix a = - new Array2DRowRealMatrix(new double[][] { { 2, 3, -2 }, { -1, 7, 6 }, { 4, -3, -5 } }, - false); + RealMatrix a = new Array2DRowRealMatrix(new double[][] { { 2, 3, -2 }, { -1, 7, 6 }, { 4, -3, -5 } }, false); RealVector b = new ArrayRealVector(new double[] { 1, -2, 1 }, false); DecompositionSolver solver = new LUDecomposition(a).getSolver(); diff --git a/libraries/src/test/java/com/baeldung/commons/math/StatisticsUnitTest.java b/libraries/src/test/java/com/baeldung/commons/math/StatisticsUnitTest.java index 025880fc12..3069446c3c 100644 --- a/libraries/src/test/java/com/baeldung/commons/math/StatisticsUnitTest.java +++ b/libraries/src/test/java/com/baeldung/commons/math/StatisticsUnitTest.java @@ -12,10 +12,10 @@ public class StatisticsUnitTest { @Before public void setUp() { - values = new double[] {65, 51 , 16, 11 , 6519, 191 ,0 , 98, 19854, 1, 32}; + values = new double[] { 65, 51, 16, 11, 6519, 191, 0, 98, 19854, 1, 32 }; descriptiveStatistics = new DescriptiveStatistics(); - for(double v : values) { + for (double v : values) { descriptiveStatistics.addValue(v); } } diff --git a/libraries/src/test/java/com/baeldung/crdt/CRDTTest.java b/libraries/src/test/java/com/baeldung/crdt/CRDTTest.java index 8309e755ce..3d3c952863 100644 --- a/libraries/src/test/java/com/baeldung/crdt/CRDTTest.java +++ b/libraries/src/test/java/com/baeldung/crdt/CRDTTest.java @@ -13,42 +13,41 @@ public class CRDTTest { @Test public void givenGrowOnlySet_whenTwoReplicasDiverge_thenShouldMergeItWithoutAConflict() { - //given + // given final LocalCrdtStore crdtStore1 = new LocalCrdtStore(); final LocalCrdtStore crdtStore2 = new LocalCrdtStore(); crdtStore1.connect(crdtStore2); final GSet replica1 = crdtStore1.createGSet("ID_1"); - final GSet replica2 = crdtStore2.findGSet("ID_1").get(); + final GSet replica2 = crdtStore2. findGSet("ID_1").get(); - //when + // when replica1.add("apple"); replica2.add("banana"); - //then + // then assertThat(replica1).contains("apple", "banana"); assertThat(replica2).contains("apple", "banana"); - //when + // when crdtStore1.disconnect(crdtStore2); replica1.add("strawberry"); replica2.add("pear"); - assertThat(replica1).contains("apple", "banana", "strawberry"); assertThat(replica2).contains("apple", "banana", "pear"); crdtStore1.connect(crdtStore2); - //then + // then assertThat(replica1).contains("apple", "banana", "strawberry", "pear"); assertThat(replica2).contains("apple", "banana", "strawberry", "pear"); } @Test public void givenIncrementOnlyCounter_whenTwoReplicasDiverge_thenShouldMergeIt() { - //given + // given final LocalCrdtStore crdtStore1 = new LocalCrdtStore(); final LocalCrdtStore crdtStore2 = new LocalCrdtStore(); crdtStore1.connect(crdtStore2); @@ -56,21 +55,20 @@ public class CRDTTest { final GCounter replica1 = crdtStore1.createGCounter("ID_1"); final GCounter replica2 = crdtStore2.findGCounter("ID_1").get(); - //when + // when replica1.increment(); replica2.increment(2L); - //then + // then assertThat(replica1.get()).isEqualTo(3L); assertThat(replica2.get()).isEqualTo(3L); - //when + // when crdtStore1.disconnect(crdtStore2); replica1.increment(3L); replica2.increment(5L); - assertThat(replica1.get()).isEqualTo(6L); assertThat(replica2.get()).isEqualTo(8L); @@ -91,15 +89,15 @@ public class CRDTTest { final PNCounter replica1 = crdtStore1.createPNCounter("ID_1"); final PNCounter replica2 = crdtStore2.findPNCounter("ID_1").get(); - //when + // when replica1.increment(); replica2.decrement(2L); - //then + // then assertThat(replica1.get()).isEqualTo(-1L); assertThat(replica2.get()).isEqualTo(-1L); - //when + // when crdtStore1.disconnect(crdtStore2); replica1.decrement(3L); @@ -110,22 +108,22 @@ public class CRDTTest { crdtStore1.connect(crdtStore2); - //then + // then assertThat(replica1.get()).isEqualTo(1L); assertThat(replica2.get()).isEqualTo(1L); } @Test public void givenLastWriteWinsStrategy_whenReplicasDiverge_thenAfterMergeShouldKeepOnlyLastValue() { - //given + // given final LocalCrdtStore crdtStore1 = new LocalCrdtStore("N_1"); final LocalCrdtStore crdtStore2 = new LocalCrdtStore("N_2"); crdtStore1.connect(crdtStore2); final LWWRegister replica1 = crdtStore1.createLWWRegister("ID_1"); - final LWWRegister replica2 = crdtStore2.findLWWRegister("ID_1").get(); + final LWWRegister replica2 = crdtStore2. findLWWRegister("ID_1").get(); - //when + // when replica1.set("apple"); replica2.set("banana"); @@ -133,20 +131,18 @@ public class CRDTTest { assertThat(replica1.get()).isEqualTo("banana"); assertThat(replica2.get()).isEqualTo("banana"); - // when crdtStore1.disconnect(crdtStore2); replica1.set("strawberry"); replica2.set("pear"); - assertThat(replica1.get()).isEqualTo("strawberry"); assertThat(replica2.get()).isEqualTo("pear"); crdtStore1.connect(crdtStore2); - //then + // then assertThat(replica1.get()).isEqualTo("pear"); assertThat(replica2.get()).isEqualTo("pear"); } diff --git a/libraries/src/test/java/com/baeldung/date/DateDiffUnitTest.java b/libraries/src/test/java/com/baeldung/date/DateDiffUnitTest.java index 14d3f925f5..545009a2a9 100644 --- a/libraries/src/test/java/com/baeldung/date/DateDiffUnitTest.java +++ b/libraries/src/test/java/com/baeldung/date/DateDiffUnitTest.java @@ -5,7 +5,6 @@ import org.junit.Test; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.Duration; -import java.time.LocalDate; import java.time.LocalDateTime; import java.util.Date; import java.util.Locale; diff --git a/libraries/src/test/java/com/baeldung/distinct/DistinctWithJavaFunctionUnitTest.java b/libraries/src/test/java/com/baeldung/distinct/DistinctWithJavaFunctionUnitTest.java index 68775fac66..936fd3e839 100644 --- a/libraries/src/test/java/com/baeldung/distinct/DistinctWithJavaFunctionUnitTest.java +++ b/libraries/src/test/java/com/baeldung/distinct/DistinctWithJavaFunctionUnitTest.java @@ -19,25 +19,19 @@ public class DistinctWithJavaFunctionUnitTest { @Test public void whenFilterListByName_thenSizeShouldBe4() { - List personListFiltered = personList.stream() - .filter(distinctByKey(p -> p.getName())) - .collect(Collectors.toList()); + List personListFiltered = personList.stream().filter(distinctByKey(p -> p.getName())).collect(Collectors.toList()); assertTrue(personListFiltered.size() == 4); } @Test public void whenFilterListByAge_thenSizeShouldBe2() { - List personListFiltered = personList.stream() - .filter(distinctByKey(p -> p.getAge())) - .collect(Collectors.toList()); + List personListFiltered = personList.stream().filter(distinctByKey(p -> p.getAge())).collect(Collectors.toList()); assertTrue(personListFiltered.size() == 2); } @Test public void whenFilterListWithDefaultDistinct_thenSizeShouldBe5() { - List personListFiltered = personList.stream() - .distinct() - .collect(Collectors.toList()); + List personListFiltered = personList.stream().distinct().collect(Collectors.toList()); assertTrue(personListFiltered.size() == 5); } diff --git a/libraries/src/test/java/com/baeldung/distinct/DistinctWithStreamexUnitTest.java b/libraries/src/test/java/com/baeldung/distinct/DistinctWithStreamexUnitTest.java index f50c76a486..c06a5d7e8b 100644 --- a/libraries/src/test/java/com/baeldung/distinct/DistinctWithStreamexUnitTest.java +++ b/libraries/src/test/java/com/baeldung/distinct/DistinctWithStreamexUnitTest.java @@ -19,17 +19,13 @@ public class DistinctWithStreamexUnitTest { @Test public void whenFilterListByName_thenSizeShouldBe4() { - List personListFiltered = StreamEx.of(personList) - .distinct(Person::getName) - .toList(); + List personListFiltered = StreamEx.of(personList).distinct(Person::getName).toList(); assertTrue(personListFiltered.size() == 4); } @Test public void whenFilterListByAge_thenSizeShouldBe2() { - List personListFiltered = StreamEx.of(personList) - .distinct(Person::getAge) - .toList(); + List personListFiltered = StreamEx.of(personList).distinct(Person::getAge).toList(); assertTrue(personListFiltered.size() == 2); } diff --git a/libraries/src/test/java/com/baeldung/distinct/DistinctWithVavrUnitTest.java b/libraries/src/test/java/com/baeldung/distinct/DistinctWithVavrUnitTest.java index b4025cd313..24593273a1 100644 --- a/libraries/src/test/java/com/baeldung/distinct/DistinctWithVavrUnitTest.java +++ b/libraries/src/test/java/com/baeldung/distinct/DistinctWithVavrUnitTest.java @@ -17,17 +17,13 @@ public class DistinctWithVavrUnitTest { @Test public void whenFilterListByName_thenSizeShouldBe4() { - List personListFiltered = io.vavr.collection.List.ofAll(personList) - .distinctBy(Person::getName) - .toJavaList(); + List personListFiltered = io.vavr.collection.List.ofAll(personList).distinctBy(Person::getName).toJavaList(); assertTrue(personListFiltered.size() == 4); } @Test public void whenFilterListByAge_thenSizeShouldBe2() { - List personListFiltered = io.vavr.collection.List.ofAll(personList) - .distinctBy(Person::getAge) - .toJavaList(); + List personListFiltered = io.vavr.collection.List.ofAll(personList).distinctBy(Person::getAge).toJavaList(); assertTrue(personListFiltered.size() == 2); } diff --git a/libraries/src/test/java/com/baeldung/dockerapi/ContainerLiveTest.java b/libraries/src/test/java/com/baeldung/dockerapi/ContainerLiveTest.java index 531e7e7c5b..e6f0fd1c31 100644 --- a/libraries/src/test/java/com/baeldung/dockerapi/ContainerLiveTest.java +++ b/libraries/src/test/java/com/baeldung/dockerapi/ContainerLiveTest.java @@ -27,138 +27,96 @@ public class ContainerLiveTest { @Test public void whenListingRunningContainers_thenReturnNonEmptyList() { - //when + // when List containers = dockerClient.listContainersCmd().exec(); - //then + // then assertThat(containers.size(), is(not(0))); } @Test public void whenListingExitedContainers_thenReturnNonEmptyList() { - //when - List containers = dockerClient.listContainersCmd() - .withShowSize(true) - .withShowAll(true) - .withStatusFilter("exited") - .exec(); + // when + List containers = dockerClient.listContainersCmd().withShowSize(true).withShowAll(true).withStatusFilter("exited").exec(); - //then + // then assertThat(containers.size(), is(not(0))); } @Test public void whenCreatingContainer_thenMustReturnContainerId() { - //when - CreateContainerResponse container - = dockerClient.createContainerCmd("mongo:3.6") - .withCmd("--bind_ip_all") - .withName("mongo") - .withHostName("baeldung") - .withEnv("MONGO_LATEST_VERSION=3.6") - .withPortBindings(PortBinding.parse("9999:27017")) - .exec(); + // when + CreateContainerResponse container = dockerClient.createContainerCmd("mongo:3.6").withCmd("--bind_ip_all").withName("mongo").withHostName("baeldung").withEnv("MONGO_LATEST_VERSION=3.6").withPortBindings(PortBinding.parse("9999:27017")).exec(); - //then + // then assertThat(container.getId(), is(not(null))); } - @Test public void whenHavingContainer_thenRunContainer() throws InterruptedException { - //when - CreateContainerResponse container - = dockerClient.createContainerCmd("alpine:3.6") - .withCmd("sleep", "10000") - .exec(); + // when + CreateContainerResponse container = dockerClient.createContainerCmd("alpine:3.6").withCmd("sleep", "10000").exec(); Thread.sleep(3000); - //then - dockerClient.startContainerCmd(container.getId()) - .exec(); + // then + dockerClient.startContainerCmd(container.getId()).exec(); - dockerClient.stopContainerCmd(container.getId()) - .exec(); + dockerClient.stopContainerCmd(container.getId()).exec(); } @Test public void whenRunningContainer_thenStopContainer() throws InterruptedException { - //when - CreateContainerResponse container - = dockerClient.createContainerCmd("alpine:3.6") - .withCmd("sleep", "10000") - .exec(); + // when + CreateContainerResponse container = dockerClient.createContainerCmd("alpine:3.6").withCmd("sleep", "10000").exec(); Thread.sleep(3000); - dockerClient.startContainerCmd(container.getId()) - .exec(); + dockerClient.startContainerCmd(container.getId()).exec(); - //then - dockerClient.stopContainerCmd(container.getId()) - .exec(); + // then + dockerClient.stopContainerCmd(container.getId()).exec(); } @Test public void whenRunningContainer_thenKillContainer() throws InterruptedException { - //when - CreateContainerResponse container - = dockerClient.createContainerCmd("alpine:3.6") - .withCmd("sleep", "10000") - .exec(); + // when + CreateContainerResponse container = dockerClient.createContainerCmd("alpine:3.6").withCmd("sleep", "10000").exec(); - dockerClient.startContainerCmd(container.getId()) - .exec(); + dockerClient.startContainerCmd(container.getId()).exec(); Thread.sleep(3000); - dockerClient.stopContainerCmd(container.getId()) - .exec(); + dockerClient.stopContainerCmd(container.getId()).exec(); - //then - dockerClient.killContainerCmd(container.getId()) - .exec(); + // then + dockerClient.killContainerCmd(container.getId()).exec(); } @Test public void whenHavingContainer_thenInspectContainer() { - //when - CreateContainerResponse container - = dockerClient.createContainerCmd("alpine:3.6") - .withCmd("sleep", "10000") - .exec(); + // when + CreateContainerResponse container = dockerClient.createContainerCmd("alpine:3.6").withCmd("sleep", "10000").exec(); - //then - InspectContainerResponse containerResponse - = dockerClient.inspectContainerCmd(container.getId()) - .exec(); + // then + InspectContainerResponse containerResponse = dockerClient.inspectContainerCmd(container.getId()).exec(); assertThat(containerResponse.getId(), is(container.getId())); } - @Test public void givenContainer_whenCommittingContainer_thenMustReturnImageId() { - //given - CreateContainerResponse container - = dockerClient.createContainerCmd("alpine:3.6") - .withCmd("sleep", "10000") - .exec(); + // given + CreateContainerResponse container = dockerClient.createContainerCmd("alpine:3.6").withCmd("sleep", "10000").exec(); - //when - String imageId = dockerClient.commitCmd(container.getId()) - .withEnv("SNAPSHOT_YEAR=2018") - .withMessage("add git support") - .withCmd("sleep", "10000") - .withRepository("alpine") - .withTag("3.6.v2").exec(); + // when + String imageId = dockerClient.commitCmd(container.getId()).withEnv("SNAPSHOT_YEAR=2018").withMessage("add git support").withCmd("sleep", "10000").withRepository("alpine").withTag("3.6.v2").exec(); - //then + // then assertThat(imageId, is(not(null))); } diff --git a/libraries/src/test/java/com/baeldung/dockerapi/DockerClientLiveTest.java b/libraries/src/test/java/com/baeldung/dockerapi/DockerClientLiveTest.java index 1023298e25..bedcc66c53 100644 --- a/libraries/src/test/java/com/baeldung/dockerapi/DockerClientLiveTest.java +++ b/libraries/src/test/java/com/baeldung/dockerapi/DockerClientLiveTest.java @@ -14,52 +14,40 @@ public class DockerClientLiveTest { @Test public void whenCreatingDockerClient_thenReturnDefaultInstance() { - //when - DefaultDockerClientConfig.Builder config - = DefaultDockerClientConfig.createDefaultConfigBuilder(); + // when + DefaultDockerClientConfig.Builder config = DefaultDockerClientConfig.createDefaultConfigBuilder(); DockerClient dockerClient = DockerClientBuilder.getInstance(config).build(); - //then + // then assertNotNull(dockerClient); } @Test public void whenCreatingDockerClientWithDockerHost_thenReturnInstance() { - //when - DockerClient dockerClient - = DockerClientBuilder.getInstance("tcp://docker.bealdung.com:2375") - .build(); + // when + DockerClient dockerClient = DockerClientBuilder.getInstance("tcp://docker.bealdung.com:2375").build(); - //then + // then assertNotNull(dockerClient); } @Test public void whenCreatingAdvanceDockerClient_thenReturnInstance() { - //when - DefaultDockerClientConfig config - = DefaultDockerClientConfig.createDefaultConfigBuilder() - .withRegistryEmail("info@bealdung.com") - .withRegistryUrl("register.bealdung.io/v2/") - .withRegistryPassword("strongpassword") - .withRegistryUsername("bealdung") - .withDockerCertPath("/home/bealdung/public/.docker/certs") - .withDockerConfig("/home/bealdung/public/.docker/") - .withDockerTlsVerify("1") - .withDockerHost("tcp://docker.beauldung.com:2376") - .build(); + // when + DefaultDockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder().withRegistryEmail("info@bealdung.com").withRegistryUrl("register.bealdung.io/v2/").withRegistryPassword("strongpassword").withRegistryUsername("bealdung") + .withDockerCertPath("/home/bealdung/public/.docker/certs").withDockerConfig("/home/bealdung/public/.docker/").withDockerTlsVerify("1").withDockerHost("tcp://docker.beauldung.com:2376").build(); DockerClient dockerClient = DockerClientBuilder.getInstance(config).build(); - //then + // then assertNotNull(dockerClient); } @Test public void whenCreatingDockerClientWithProperties_thenReturnInstance() { - //when + // when Properties properties = new Properties(); properties.setProperty("registry.email", "info@bealdung.com"); properties.setProperty("registry.url", "register.bealdung.io/v2/"); @@ -70,14 +58,11 @@ public class DockerClientLiveTest { properties.setProperty("DOCKER_TLS_VERIFY", "1"); properties.setProperty("DOCKER_HOST", "tcp://docker.bealdung.com:2376"); - DefaultDockerClientConfig config - = DefaultDockerClientConfig.createDefaultConfigBuilder() - .withProperties(properties) - .build(); + DefaultDockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder().withProperties(properties).build(); DockerClient dockerClient = DockerClientBuilder.getInstance(config).build(); - //then + // then assertNotNull(dockerClient); } } diff --git a/libraries/src/test/java/com/baeldung/dockerapi/ImageLiveTest.java b/libraries/src/test/java/com/baeldung/dockerapi/ImageLiveTest.java index ef894b2773..7e8cd6a354 100644 --- a/libraries/src/test/java/com/baeldung/dockerapi/ImageLiveTest.java +++ b/libraries/src/test/java/com/baeldung/dockerapi/ImageLiveTest.java @@ -33,99 +33,84 @@ public class ImageLiveTest { @Test public void whenListingImages_thenReturnNonEmptyList() { - //when + // when List images = dockerClient.listImagesCmd().exec(); - //then + // then assertThat(images.size(), is(not(0))); } @Test public void whenListingImagesWithIntermediateImages_thenReturnNonEmptyList() { - //when - List images = dockerClient.listImagesCmd() - .withShowAll(true).exec(); + // when + List images = dockerClient.listImagesCmd().withShowAll(true).exec(); - //then + // then assertThat(images.size(), is(not(0))); } @Test public void whenListingDanglingImages_thenReturnNonNullList() { - //when - List images = dockerClient.listImagesCmd() - .withDanglingFilter(true).exec(); + // when + List images = dockerClient.listImagesCmd().withDanglingFilter(true).exec(); - //then + // then assertThat(images, is(not(null))); } @Test public void whenBuildingImage_thenMustReturnImageId() { - //when - String imageId = dockerClient.buildImageCmd() - .withDockerfile(new File("src/test/resources/dockerapi/Dockerfile")) - .withPull(true) - .withNoCache(true) - .withTag("alpine:git") - .exec(new BuildImageResultCallback()) - .awaitImageId(); + // when + String imageId = dockerClient.buildImageCmd().withDockerfile(new File("src/test/resources/dockerapi/Dockerfile")).withPull(true).withNoCache(true).withTag("alpine:git").exec(new BuildImageResultCallback()).awaitImageId(); - //then + // then assertThat(imageId, is(not(null))); } @Test public void givenListOfImages_whenInspectImage_thenMustReturnObject() { - //given + // given List images = dockerClient.listImagesCmd().exec(); Image image = images.get(0); - //when - InspectImageResponse imageResponse - = dockerClient.inspectImageCmd(image.getId()).exec(); + // when + InspectImageResponse imageResponse = dockerClient.inspectImageCmd(image.getId()).exec(); - //then + // then assertThat(imageResponse.getId(), is(image.getId())); } @Test public void givenListOfImages_whenTagImage_thenListMustIncrement() { - //given + // given List images = dockerClient.listImagesCmd().exec(); Image image = images.get(0); - //when + // when dockerClient.tagImageCmd(image.getId(), "baeldung/alpine", "3.6.v2").exec(); - //then + // then List imagesNow = dockerClient.listImagesCmd().exec(); assertThat(imagesNow.size(), is(greaterThan(images.size()))); } public void pushingAnImage() throws InterruptedException { - dockerClient.pushImageCmd("baeldung/alpine") - .withTag("3.6.v2") - .exec(new PushImageResultCallback()) - .awaitCompletion(90, TimeUnit.SECONDS); + dockerClient.pushImageCmd("baeldung/alpine").withTag("3.6.v2").exec(new PushImageResultCallback()).awaitCompletion(90, TimeUnit.SECONDS); } @Test public void whenPullingImage_thenImageListNotEmpty() throws InterruptedException { - //when - dockerClient.pullImageCmd("alpine") - .withTag("latest") - .exec(new PullImageResultCallback()) - .awaitCompletion(30, TimeUnit.SECONDS); + // when + dockerClient.pullImageCmd("alpine").withTag("latest").exec(new PullImageResultCallback()).awaitCompletion(30, TimeUnit.SECONDS); - //then + // then List images = dockerClient.listImagesCmd().exec(); assertThat(images.size(), is(not(0))); } @@ -133,12 +118,12 @@ public class ImageLiveTest { @Test public void whenRemovingImage_thenImageListDecrease() { - //when + // when List images = dockerClient.listImagesCmd().exec(); Image image = images.get(0); dockerClient.removeImageCmd(image.getId()).exec(); - //then + // then List imagesNow = dockerClient.listImagesCmd().exec(); assertThat(imagesNow.size(), is(lessThan(images.size()))); } @@ -146,10 +131,10 @@ public class ImageLiveTest { @Test public void whenSearchingImage_thenMustReturn25Items() { - //when + // when List items = dockerClient.searchImagesCmd("Java").exec(); - //then + // then assertThat(items.size(), is(25)); } } diff --git a/libraries/src/test/java/com/baeldung/dockerapi/NetworkLiveTest.java b/libraries/src/test/java/com/baeldung/dockerapi/NetworkLiveTest.java index 2031a3ebb4..d3abbe2e7e 100644 --- a/libraries/src/test/java/com/baeldung/dockerapi/NetworkLiveTest.java +++ b/libraries/src/test/java/com/baeldung/dockerapi/NetworkLiveTest.java @@ -6,6 +6,7 @@ import com.github.dockerjava.api.model.Network; import com.github.dockerjava.api.model.Network.Ipam; import com.github.dockerjava.core.DockerClientBuilder; import org.junit.BeforeClass; +import org.junit.Ignore; import org.junit.Test; import java.util.List; @@ -25,66 +26,54 @@ public class NetworkLiveTest { } @Test + @Ignore("temporarily") public void whenListingNetworks_thenSizeMustBeGreaterThanZero() { - //when + // when List networks = dockerClient.listNetworksCmd().exec(); - //then + // then assertThat(networks.size(), is(greaterThan(0))); } @Test public void whenCreatingNetwork_thenRetrieveResponse() { - //when - CreateNetworkResponse networkResponse - = dockerClient.createNetworkCmd() - .withName("baeldungDefault") - .withDriver("bridge").exec(); + // when + CreateNetworkResponse networkResponse = dockerClient.createNetworkCmd().withName("baeldungDefault").withDriver("bridge").exec(); - //then + // then assertThat(networkResponse, is(not(null))); } @Test public void whenCreatingAdvanceNetwork_thenRetrieveResponse() { - //when - CreateNetworkResponse networkResponse = dockerClient.createNetworkCmd() - .withName("baeldungAdvanced") - .withIpam(new Ipam() - .withConfig(new Ipam.Config() - .withSubnet("172.36.0.0/16") - .withIpRange("172.36.5.0/24"))) - .withDriver("bridge").exec(); + // when + CreateNetworkResponse networkResponse = dockerClient.createNetworkCmd().withName("baeldungAdvanced").withIpam(new Ipam().withConfig(new Ipam.Config().withSubnet("172.36.0.0/16").withIpRange("172.36.5.0/24"))).withDriver("bridge").exec(); - //then + // then assertThat(networkResponse, is(not(null))); } @Test public void whenInspectingNetwork_thenSizeMustBeGreaterThanZero() { - //when + // when String networkName = "bridge"; - Network network - = dockerClient.inspectNetworkCmd().withNetworkId(networkName).exec(); + Network network = dockerClient.inspectNetworkCmd().withNetworkId(networkName).exec(); - //then + // then assertThat(network.getName(), is(networkName)); } @Test public void whenCreatingNetwork_thenRemove() throws InterruptedException { - //when - CreateNetworkResponse networkResponse - = dockerClient.createNetworkCmd() - .withName("baeldungDefault") - .withDriver("bridge").exec(); + // when + CreateNetworkResponse networkResponse = dockerClient.createNetworkCmd().withName("baeldungDefault").withDriver("bridge").exec(); - //then + // then Thread.sleep(4000); dockerClient.removeNetworkCmd(networkResponse.getId()).exec(); } diff --git a/libraries/src/test/java/com/baeldung/dockerapi/VolumeLiveTest.java b/libraries/src/test/java/com/baeldung/dockerapi/VolumeLiveTest.java index 060af0728c..9e60a76b33 100644 --- a/libraries/src/test/java/com/baeldung/dockerapi/VolumeLiveTest.java +++ b/libraries/src/test/java/com/baeldung/dockerapi/VolumeLiveTest.java @@ -27,10 +27,10 @@ public class VolumeLiveTest { @Test public void whenListingVolumes_thenSizeMustBeGreaterThanZero() { - //when + // when ListVolumesResponse volumesResponse = dockerClient.listVolumesCmd().exec(); - //then + // then List volumes = volumesResponse.getVolumes(); assertThat(volumes.size(), is(greaterThan(0))); } @@ -38,48 +38,45 @@ public class VolumeLiveTest { @Test public void givenVolumes_whenInspectingVolume_thenReturnNonNullResponse() { - //given + // given ListVolumesResponse volumesResponse = dockerClient.listVolumesCmd().exec(); List volumes = volumesResponse.getVolumes(); InspectVolumeResponse volume = volumes.get(0); - //when - InspectVolumeResponse volumeResponse - = dockerClient.inspectVolumeCmd(volume.getName()).exec(); + // when + InspectVolumeResponse volumeResponse = dockerClient.inspectVolumeCmd(volume.getName()).exec(); - //then + // then assertThat(volumeResponse, is(not(null))); } @Test public void whenCreatingUnnamedVolume_thenGetVolumeId() { - //when + // when CreateVolumeResponse unnamedVolume = dockerClient.createVolumeCmd().exec(); - //then + // then assertThat(unnamedVolume.getName(), is(not(null))); } @Test public void whenCreatingNamedVolume_thenGetVolumeId() { - //when - CreateVolumeResponse namedVolume - = dockerClient.createVolumeCmd().withName("myNamedVolume").exec(); + // when + CreateVolumeResponse namedVolume = dockerClient.createVolumeCmd().withName("myNamedVolume").exec(); - //then + // then assertThat(namedVolume.getName(), is(not(null))); } @Test public void whenGettingNamedVolume_thenRemove() throws InterruptedException { - //when - CreateVolumeResponse namedVolume - = dockerClient.createVolumeCmd().withName("anotherNamedVolume").exec(); + // when + CreateVolumeResponse namedVolume = dockerClient.createVolumeCmd().withName("anotherNamedVolume").exec(); - //then + // then Thread.sleep(4000); dockerClient.removeVolumeCmd(namedVolume.getName()).exec(); } diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/CollectPatternTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/CollectPatternTest.java index ee384c2f9d..b1aaceb09b 100644 --- a/libraries/src/test/java/com/baeldung/eclipsecollections/CollectPatternTest.java +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/CollectPatternTest.java @@ -17,7 +17,6 @@ public class CollectPatternTest { MutableList lastNames = students.collect(Student::getLastName); - Assertions.assertThat(lastNames) - .containsExactly("Hopkins", "Adams"); + Assertions.assertThat(lastNames).containsExactly("Hopkins", "Adams"); } } diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/ConvertContainerToAnotherTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/ConvertContainerToAnotherTest.java index 4655431872..e279314034 100644 --- a/libraries/src/test/java/com/baeldung/eclipsecollections/ConvertContainerToAnotherTest.java +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/ConvertContainerToAnotherTest.java @@ -12,7 +12,6 @@ public class ConvertContainerToAnotherTest { public void whenConvertContainerToAnother_thenCorrect() { MutableList cars = (MutableList) ConvertContainerToAnother.convertToList(); - Assertions.assertThat(cars) - .containsExactlyElementsOf(FastList.newListWith("Volkswagen", "Toyota", "Mercedes")); + Assertions.assertThat(cars).containsExactlyElementsOf(FastList.newListWith("Volkswagen", "Toyota", "Mercedes")); } } diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/DetectPatternTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/DetectPatternTest.java index c5b5e1c412..4ef7348a0d 100644 --- a/libraries/src/test/java/com/baeldung/eclipsecollections/DetectPatternTest.java +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/DetectPatternTest.java @@ -20,7 +20,6 @@ public class DetectPatternTest { public void whenDetect_thenCorrect() { Integer result = list.detect(Predicates.greaterThan(30)); - Assertions.assertThat(result) - .isEqualTo(41); + Assertions.assertThat(result).isEqualTo(41); } } diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/FlatCollectTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/FlatCollectTest.java index 021c72e91e..3091f90908 100644 --- a/libraries/src/test/java/com/baeldung/eclipsecollections/FlatCollectTest.java +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/FlatCollectTest.java @@ -44,7 +44,6 @@ public class FlatCollectTest { public void whenFlatCollect_thenCorrect() { MutableList addresses = students.flatCollect(Student::getAddresses); - Assertions.assertThat(addresses) - .containsExactlyElementsOf(this.expectedAddresses); + Assertions.assertThat(addresses).containsExactlyElementsOf(this.expectedAddresses); } } diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/InjectIntoPatternTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/InjectIntoPatternTest.java index bcd34021b1..01a8fcaef4 100644 --- a/libraries/src/test/java/com/baeldung/eclipsecollections/InjectIntoPatternTest.java +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/InjectIntoPatternTest.java @@ -17,7 +17,7 @@ public class InjectIntoPatternTest { Integer v = list.get(i); result = result + v.intValue(); } - + assertEquals(15, result); } } diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/LazyIterationTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/LazyIterationTest.java index 9c216ecc87..bcb816c34a 100644 --- a/libraries/src/test/java/com/baeldung/eclipsecollections/LazyIterationTest.java +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/LazyIterationTest.java @@ -18,7 +18,6 @@ public class LazyIterationTest { LazyIterable lazyStudents = students.asLazy(); LazyIterable lastNames = lazyStudents.collect(Student::getLastName); - Assertions.assertThat(lastNames) - .containsAll(Lists.mutable.with("Hopkins", "Adams", "Rodriguez")); + Assertions.assertThat(lastNames).containsAll(Lists.mutable.with("Hopkins", "Adams", "Rodriguez")); } } diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/PartitionPatternTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/PartitionPatternTest.java index c055413cd9..8ef18004aa 100644 --- a/libraries/src/test/java/com/baeldung/eclipsecollections/PartitionPatternTest.java +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/PartitionPatternTest.java @@ -31,14 +31,10 @@ public class PartitionPatternTest { return each > 30; } }); - MutableList greaterThanThirty = partitionedFolks.getSelected() - .sortThis(); - MutableList smallerThanThirty = partitionedFolks.getRejected() - .sortThis(); + MutableList greaterThanThirty = partitionedFolks.getSelected().sortThis(); + MutableList smallerThanThirty = partitionedFolks.getRejected().sortThis(); - Assertions.assertThat(smallerThanThirty) - .containsExactly(1, 5, 8, 17, 23); - Assertions.assertThat(greaterThanThirty) - .containsExactly(31, 38, 41); + Assertions.assertThat(smallerThanThirty).containsExactly(1, 5, 8, 17, 23); + Assertions.assertThat(greaterThanThirty).containsExactly(31, 38, 41); } } diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/RejectPatternTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/RejectPatternTest.java index 1666c86333..bd743d56e8 100644 --- a/libraries/src/test/java/com/baeldung/eclipsecollections/RejectPatternTest.java +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/RejectPatternTest.java @@ -20,10 +20,8 @@ public class RejectPatternTest { @Test public void whenReject_thenCorrect() { - MutableList notGreaterThanThirty = list.reject(Predicates.greaterThan(30)) - .sortThis(); + MutableList notGreaterThanThirty = list.reject(Predicates.greaterThan(30)).sortThis(); - Assertions.assertThat(notGreaterThanThirty) - .containsExactlyElementsOf(this.expectedList); + Assertions.assertThat(notGreaterThanThirty).containsExactlyElementsOf(this.expectedList); } } diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/SelectPatternTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/SelectPatternTest.java index d79c864fc5..154be08f08 100644 --- a/libraries/src/test/java/com/baeldung/eclipsecollections/SelectPatternTest.java +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/SelectPatternTest.java @@ -18,17 +18,14 @@ public class SelectPatternTest { @Test public void givenListwhenSelect_thenCorrect() { - MutableList greaterThanThirty = list.select(Predicates.greaterThan(30)) - .sortThis(); + MutableList greaterThanThirty = list.select(Predicates.greaterThan(30)).sortThis(); - Assertions.assertThat(greaterThanThirty) - .containsExactly(31, 38, 41); + Assertions.assertThat(greaterThanThirty).containsExactly(31, 38, 41); } @SuppressWarnings("rawtypes") public MutableList selectUsingLambda() { - return list.select(each -> each > 30) - .sortThis(); + return list.select(each -> each > 30).sortThis(); } @SuppressWarnings("unchecked") @@ -36,7 +33,6 @@ public class SelectPatternTest { public void givenListwhenSelectUsingLambda_thenCorrect() { MutableList greaterThanThirty = selectUsingLambda(); - Assertions.assertThat(greaterThanThirty) - .containsExactly(31, 38, 41); + Assertions.assertThat(greaterThanThirty).containsExactly(31, 38, 41); } } diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/ZipTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/ZipTest.java index 29f0c23954..2b5aa06289 100644 --- a/libraries/src/test/java/com/baeldung/eclipsecollections/ZipTest.java +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/ZipTest.java @@ -28,7 +28,6 @@ public class ZipTest { MutableList cars = Lists.mutable.with("Porsche", "Volvo", "Toyota"); MutableList> pairs = numbers.zip(cars); - Assertions.assertThat(pairs) - .containsExactlyElementsOf(this.expectedPairs); + Assertions.assertThat(pairs).containsExactlyElementsOf(this.expectedPairs); } } diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/ZipWithIndexTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/ZipWithIndexTest.java index a2d8be44ec..724f693011 100644 --- a/libraries/src/test/java/com/baeldung/eclipsecollections/ZipWithIndexTest.java +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/ZipWithIndexTest.java @@ -27,7 +27,6 @@ public class ZipWithIndexTest { MutableList cars = FastList.newListWith("Porsche", "Volvo", "Toyota"); MutableList> pairs = cars.zipWithIndex(); - Assertions.assertThat(pairs) - .containsExactlyElementsOf(this.expectedPairs); + Assertions.assertThat(pairs).containsExactlyElementsOf(this.expectedPairs); } } diff --git a/libraries/src/test/java/com/baeldung/fj/FunctionalJavaTest.java b/libraries/src/test/java/com/baeldung/fj/FunctionalJavaTest.java index 04ab6e43be..33952c8669 100644 --- a/libraries/src/test/java/com/baeldung/fj/FunctionalJavaTest.java +++ b/libraries/src/test/java/com/baeldung/fj/FunctionalJavaTest.java @@ -12,49 +12,49 @@ import fj.function.Characters; import fj.function.Integers; public class FunctionalJavaTest { - - public static final F isEven = i -> i % 2 == 0; - + + public static final F isEven = i -> i % 2 == 0; + @Test public void calculateEvenNumbers_givenIntList_returnTrue() { - List fList = List.list(3, 4, 5, 6); + List fList = List.list(3, 4, 5, 6); List evenList = fList.map(isEven); List evenListTrueResult = List.list(false, true, false, true); List evenListFalseResult = List.list(true, false, false, true); assertEquals(evenList.equals(evenListTrueResult), true); assertEquals(evenList.equals(evenListFalseResult), false); } - + @Test public void mapList_givenIntList_returnResult() { - List fList = List.list(3, 4, 5, 6); - fList = fList.map(i -> i + 100); - List resultList = List.list(103, 104, 105, 106); - List falseResultList = List.list(15, 504, 105, 106); - assertEquals(fList.equals(resultList), true); - assertEquals(fList.equals(falseResultList), false); + List fList = List.list(3, 4, 5, 6); + fList = fList.map(i -> i + 100); + List resultList = List.list(103, 104, 105, 106); + List falseResultList = List.list(15, 504, 105, 106); + assertEquals(fList.equals(resultList), true); + assertEquals(fList.equals(falseResultList), false); } - + @Test public void filterList_givenIntList_returnResult() { - Array array = Array.array(3, 4, 5, 6); - Array filteredArray = array.filter(Integers.even); - Array result = Array.array(4, 6); - Array wrongResult = Array.array(3, 5); - assertEquals(filteredArray.equals(result), true); - assertEquals(filteredArray.equals(wrongResult), false); + Array array = Array.array(3, 4, 5, 6); + Array filteredArray = array.filter(Integers.even); + Array result = Array.array(4, 6); + Array wrongResult = Array.array(3, 5); + assertEquals(filteredArray.equals(result), true); + assertEquals(filteredArray.equals(wrongResult), false); } - + @Test public void checkForLowerCase_givenStringArray_returnResult() { - Array array = Array.array("Welcome", "To", "baeldung"); - Array array2 = Array.array("Welcome", "To", "Baeldung"); + Array array = Array.array("Welcome", "To", "baeldung"); + Array array2 = Array.array("Welcome", "To", "Baeldung"); Boolean isExist = array.exists(s -> List.fromString(s).forall(Characters.isLowerCase)); Boolean isExist2 = array2.exists(s -> List.fromString(s).forall(Characters.isLowerCase)); assertEquals(isExist, true); assertEquals(isExist2, false); } - + @Test public void checkOptions_givenOptions_returnResult() { Option n1 = Option.some(1); @@ -64,16 +64,16 @@ public class FunctionalJavaTest { Option result1 = n1.bind(f1); Option result2 = n2.bind(f1); - + assertEquals(result1, Option.none()); assertEquals(result2, Option.some(102)); } - + @Test public void foldLeft_givenArray_returnResult() { Array intArray = Array.array(17, 44, 67, 2, 22, 80, 1, 27); int sum = intArray.foldLeft(Integers.add, 0); assertEquals(sum, 260); } - + } diff --git a/libraries/src/test/java/com/baeldung/flink/WordCountIntegrationTest.java b/libraries/src/test/java/com/baeldung/flink/WordCountIntegrationTest.java index f864c5f017..5c788e86d6 100644 --- a/libraries/src/test/java/com/baeldung/flink/WordCountIntegrationTest.java +++ b/libraries/src/test/java/com/baeldung/flink/WordCountIntegrationTest.java @@ -20,51 +20,45 @@ import java.util.List; import static org.assertj.core.api.Assertions.assertThat; - public class WordCountIntegrationTest { private final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); @Test public void givenDataSet_whenExecuteWordCount_thenReturnWordCount() throws Exception { - //given + // given List lines = Arrays.asList("This is a first sentence", "This is a second sentence with a one word"); - //when + // when DataSet> result = WordCount.startWordCount(env, lines); - //then + // then List> collect = result.collect(); - assertThat(collect).containsExactlyInAnyOrder( - new Tuple2<>("a", 3), new Tuple2<>("sentence", 2), new Tuple2<>("word", 1), - new Tuple2<>("is", 2), new Tuple2<>("this", 2), new Tuple2<>("second", 1), - new Tuple2<>("first", 1), new Tuple2<>("with", 1), new Tuple2<>("one", 1)); + assertThat(collect).containsExactlyInAnyOrder(new Tuple2<>("a", 3), new Tuple2<>("sentence", 2), new Tuple2<>("word", 1), new Tuple2<>("is", 2), new Tuple2<>("this", 2), new Tuple2<>("second", 1), new Tuple2<>("first", 1), new Tuple2<>("with", 1), + new Tuple2<>("one", 1)); } @Test public void givenListOfAmounts_whenUseMapReduce_thenSumAmountsThatAreOnlyAboveThreshold() throws Exception { - //given + // given DataSet amounts = env.fromElements(1, 29, 40, 50); int threshold = 30; - //when - List collect = amounts - .filter(a -> a > threshold) - .reduce((integer, t1) -> integer + t1) - .collect(); + // when + List collect = amounts.filter(a -> a > threshold).reduce((integer, t1) -> integer + t1).collect(); - //then + // then assertThat(collect.get(0)).isEqualTo(90); } @Test public void givenDataSetOfComplexObjects_whenMapToGetOneField_thenReturnedListHaveProperElements() throws Exception { - //given + // given DataSet personDataSource = env.fromCollection(Arrays.asList(new Person(23, "Tom"), new Person(75, "Michael"))); - //when + // when List ages = personDataSource.map(p -> p.age).collect(); - //then + // then assertThat(ages).hasSize(2); assertThat(ages).contains(23, 75); @@ -72,44 +66,33 @@ public class WordCountIntegrationTest { @Test public void givenDataSet_whenSortItByOneField_thenShouldReturnSortedDataSet() throws Exception { - //given + // given Tuple2 secondPerson = new Tuple2<>(4, "Tom"); Tuple2 thirdPerson = new Tuple2<>(5, "Scott"); Tuple2 fourthPerson = new Tuple2<>(200, "Michael"); Tuple2 firstPerson = new Tuple2<>(1, "Jack"); - DataSet> transactions = env.fromElements(fourthPerson, secondPerson, - thirdPerson, firstPerson); + DataSet> transactions = env.fromElements(fourthPerson, secondPerson, thirdPerson, firstPerson); + // when + List> sorted = transactions.sortPartition(new IdKeySelectorTransaction(), Order.ASCENDING).collect(); - //when - List> sorted = transactions - .sortPartition(new IdKeySelectorTransaction(), Order.ASCENDING) - .collect(); - - //then + // then assertThat(sorted).containsExactly(firstPerson, secondPerson, thirdPerson, fourthPerson); } - @Test public void giveTwoDataSets_whenJoinUsingId_thenProduceJoinedData() throws Exception { - //given + // given Tuple3 address = new Tuple3<>(1, "5th Avenue", "London"); DataSet> addresses = env.fromElements(address); Tuple2 firstTransaction = new Tuple2<>(1, "Transaction_1"); - DataSet> transactions = - env.fromElements(firstTransaction, new Tuple2<>(12, "Transaction_2")); + DataSet> transactions = env.fromElements(firstTransaction, new Tuple2<>(12, "Transaction_2")); + // when + List, Tuple3>> joined = transactions.join(addresses).where(new IdKeySelectorTransaction()).equalTo(new IdKeySelectorAddress()).collect(); - //when - List, Tuple3>> joined = - transactions.join(addresses) - .where(new IdKeySelectorTransaction()) - .equalTo(new IdKeySelectorAddress()) - .collect(); - - //then + // then assertThat(joined).hasSize(1); assertThat(joined).contains(new Tuple2<>(firstTransaction, address)); @@ -117,48 +100,40 @@ public class WordCountIntegrationTest { @Test public void givenStreamOfEvents_whenProcessEvents_thenShouldPrintResultsOnSinkOperation() throws Exception { - //given + // given final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); - DataStream text - = env.fromElements("This is a first sentence", "This is a second sentence with a one word"); - + DataStream text = env.fromElements("This is a first sentence", "This is a second sentence with a one word"); SingleOutputStreamOperator upperCase = text.map(String::toUpperCase); upperCase.print(); - //when + // when env.execute(); } - @Test public void givenStreamOfEvents_whenProcessEvents_thenShouldApplyWindowingOnTransformation() throws Exception { - //given + // given final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); - SingleOutputStreamOperator> windowed = env.fromElements( - new Tuple2<>(16, ZonedDateTime.now().plusMinutes(25).toInstant().getEpochSecond()), - new Tuple2<>(15, ZonedDateTime.now().plusMinutes(2).toInstant().getEpochSecond()) - ).assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor>(Time.seconds(20)) { - @Override - public long extractTimestamp(Tuple2 element) { - return element.f1 * 1000; - } - }); + SingleOutputStreamOperator> windowed = env.fromElements(new Tuple2<>(16, ZonedDateTime.now().plusMinutes(25).toInstant().getEpochSecond()), new Tuple2<>(15, ZonedDateTime.now().plusMinutes(2).toInstant().getEpochSecond())) + .assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor>(Time.seconds(20)) { + @Override + public long extractTimestamp(Tuple2 element) { + return element.f1 * 1000; + } + }); - SingleOutputStreamOperator> reduced = windowed - .windowAll(TumblingEventTimeWindows.of(Time.seconds(5))) - .maxBy(0, true); + SingleOutputStreamOperator> reduced = windowed.windowAll(TumblingEventTimeWindows.of(Time.seconds(5))).maxBy(0, true); reduced.print(); - //when + // when env.execute(); } - private static class IdKeySelectorTransaction implements KeySelector, Integer> { @Override public Integer getKey(Tuple2 value) { diff --git a/libraries/src/test/java/com/baeldung/google/sheets/GoogleSheetsIntegrationTest.java b/libraries/src/test/java/com/baeldung/google/sheets/GoogleSheetsIntegrationTest.java index 5280073be2..ba1861937b 100644 --- a/libraries/src/test/java/com/baeldung/google/sheets/GoogleSheetsIntegrationTest.java +++ b/libraries/src/test/java/com/baeldung/google/sheets/GoogleSheetsIntegrationTest.java @@ -30,7 +30,7 @@ public class GoogleSheetsIntegrationTest { private static Sheets sheetsService; - // this id can be replaced with your spreadsheet id + // this id can be replaced with your spreadsheet id // otherwise be advised that multiple people may run this test and update the public spreadsheet private static final String SPREADSHEET_ID = "1sILuxZUnyl_7-MlNThjt765oWshN3Xs-PPLfqYe4DhI"; @@ -41,100 +41,56 @@ public class GoogleSheetsIntegrationTest { @Test public void whenWriteSheet_thenReadSheetOk() throws IOException { - ValueRange body = new ValueRange() - .setValues(Arrays.asList( - Arrays.asList("Expenses January"), - Arrays.asList("books", "30"), - Arrays.asList("pens", "10"), - Arrays.asList("Expenses February"), - Arrays.asList("clothes", "20"), - Arrays.asList("shoes", "5"))); - UpdateValuesResponse result = sheetsService.spreadsheets().values() - .update(SPREADSHEET_ID, "A1", body) - .setValueInputOption("RAW") - .execute(); - - List data = new ArrayList<>(); - data.add(new ValueRange() - .setRange("D1") - .setValues(Arrays.asList( - Arrays.asList("January Total", "=B2+B3")))); - data.add(new ValueRange() - .setRange("D4") - .setValues(Arrays.asList( - Arrays.asList("February Total", "=B5+B6")))); + ValueRange body = new ValueRange().setValues(Arrays.asList(Arrays.asList("Expenses January"), Arrays.asList("books", "30"), Arrays.asList("pens", "10"), Arrays.asList("Expenses February"), Arrays.asList("clothes", "20"), Arrays.asList("shoes", "5"))); + UpdateValuesResponse result = sheetsService.spreadsheets().values().update(SPREADSHEET_ID, "A1", body).setValueInputOption("RAW").execute(); + + List data = new ArrayList<>(); + data.add(new ValueRange().setRange("D1").setValues(Arrays.asList(Arrays.asList("January Total", "=B2+B3")))); + data.add(new ValueRange().setRange("D4").setValues(Arrays.asList(Arrays.asList("February Total", "=B5+B6")))); + + BatchUpdateValuesRequest batchBody = new BatchUpdateValuesRequest().setValueInputOption("USER_ENTERED").setData(data); + BatchUpdateValuesResponse batchResult = sheetsService.spreadsheets().values().batchUpdate(SPREADSHEET_ID, batchBody).execute(); + + List ranges = Arrays.asList("E1", "E4"); + BatchGetValuesResponse readResult = sheetsService.spreadsheets().values().batchGet(SPREADSHEET_ID).setRanges(ranges).execute(); - BatchUpdateValuesRequest batchBody = new BatchUpdateValuesRequest() - .setValueInputOption("USER_ENTERED") - .setData(data); - BatchUpdateValuesResponse batchResult = - sheetsService.spreadsheets().values() - .batchUpdate(SPREADSHEET_ID, batchBody) - .execute(); - - List ranges = Arrays.asList("E1","E4"); - BatchGetValuesResponse readResult = - sheetsService.spreadsheets().values() - .batchGet(SPREADSHEET_ID) - .setRanges(ranges) - .execute(); - ValueRange januaryTotal = readResult.getValueRanges().get(0); assertThat(januaryTotal.getValues().get(0).get(0)).isEqualTo("40"); ValueRange febTotal = readResult.getValueRanges().get(1); assertThat(febTotal.getValues().get(0).get(0)).isEqualTo("25"); - ValueRange appendBody = new ValueRange() - .setValues(Arrays.asList( - Arrays.asList("Total", "=E1+E4"))); - AppendValuesResponse appendResult = - sheetsService.spreadsheets().values() - .append(SPREADSHEET_ID, "A1", appendBody) - .setValueInputOption("USER_ENTERED") - .setInsertDataOption("INSERT_ROWS") - .setIncludeValuesInResponse(true) - .execute(); + ValueRange appendBody = new ValueRange().setValues(Arrays.asList(Arrays.asList("Total", "=E1+E4"))); + AppendValuesResponse appendResult = sheetsService.spreadsheets().values().append(SPREADSHEET_ID, "A1", appendBody).setValueInputOption("USER_ENTERED").setInsertDataOption("INSERT_ROWS").setIncludeValuesInResponse(true).execute(); ValueRange total = appendResult.getUpdates().getUpdatedData(); assertThat(total.getValues().get(0).get(1)).isEqualTo("65"); } - @Test public void whenUpdateSpreadSheetTitle_thenOk() throws IOException { - - UpdateSpreadsheetPropertiesRequest updateRequest = new UpdateSpreadsheetPropertiesRequest() - .setFields("*") - .setProperties(new SpreadsheetProperties().setTitle("Expenses")); - - CopyPasteRequest copyRequest = new CopyPasteRequest() - .setSource(new GridRange().setSheetId(0) - .setStartColumnIndex(0).setEndColumnIndex(2) - .setStartRowIndex(0).setEndRowIndex(1)) - .setDestination(new GridRange().setSheetId(1) - .setStartColumnIndex(0).setEndColumnIndex(2) - .setStartRowIndex(0).setEndRowIndex(1)) - .setPasteType("PASTE_VALUES"); - + + UpdateSpreadsheetPropertiesRequest updateRequest = new UpdateSpreadsheetPropertiesRequest().setFields("*").setProperties(new SpreadsheetProperties().setTitle("Expenses")); + + CopyPasteRequest copyRequest = new CopyPasteRequest().setSource(new GridRange().setSheetId(0).setStartColumnIndex(0).setEndColumnIndex(2).setStartRowIndex(0).setEndRowIndex(1)) + .setDestination(new GridRange().setSheetId(1).setStartColumnIndex(0).setEndColumnIndex(2).setStartRowIndex(0).setEndRowIndex(1)).setPasteType("PASTE_VALUES"); + List requests = new ArrayList<>(); - + requests.add(new Request().setCopyPaste(copyRequest)); requests.add(new Request().setUpdateSpreadsheetProperties(updateRequest)); - BatchUpdateSpreadsheetRequest body = - new BatchUpdateSpreadsheetRequest().setRequests(requests); + BatchUpdateSpreadsheetRequest body = new BatchUpdateSpreadsheetRequest().setRequests(requests); sheetsService.spreadsheets().batchUpdate(SPREADSHEET_ID, body).execute(); } - + @Test public void whenCreateSpreadSheet_thenIdOk() throws IOException { - Spreadsheet spreadSheet = new Spreadsheet() - .setProperties(new SpreadsheetProperties().setTitle("My Spreadsheet")); + Spreadsheet spreadSheet = new Spreadsheet().setProperties(new SpreadsheetProperties().setTitle("My Spreadsheet")); Spreadsheet result = sheetsService.spreadsheets().create(spreadSheet).execute(); - assertThat(result.getSpreadsheetId()).isNotNull(); + assertThat(result.getSpreadsheetId()).isNotNull(); } } diff --git a/libraries/src/test/java/com/baeldung/hll/HLLLongRunningUnitTest.java b/libraries/src/test/java/com/baeldung/hll/HLLLongRunningUnitTest.java index 5ecd4442d8..f762096811 100644 --- a/libraries/src/test/java/com/baeldung/hll/HLLLongRunningUnitTest.java +++ b/libraries/src/test/java/com/baeldung/hll/HLLLongRunningUnitTest.java @@ -1,6 +1,5 @@ package com.baeldung.hll; - import com.google.common.hash.HashFunction; import com.google.common.hash.Hashing; import net.agkn.hll.HLL; @@ -15,47 +14,44 @@ public class HLLLongRunningUnitTest { @Test public void givenHLL_whenAddHugeAmountOfNumbers_thenShouldReturnEstimatedCardinality() { - //given + // given long numberOfElements = 100_000_000; long toleratedDifference = 1_000_000; HashFunction hashFunction = Hashing.murmur3_128(); HLL hll = new HLL(14, 5); - //when + // when LongStream.range(0, numberOfElements).forEach(element -> { - long hashedValue = hashFunction.newHasher().putLong(element).hash().asLong(); - hll.addRaw(hashedValue); - } - ); + long hashedValue = hashFunction.newHasher().putLong(element).hash().asLong(); + hll.addRaw(hashedValue); + }); - //then + // then long cardinality = hll.cardinality(); assertThat(cardinality).isCloseTo(numberOfElements, Offset.offset(toleratedDifference)); } @Test public void givenTwoHLLs_whenAddHugeAmountOfNumbers_thenShouldReturnEstimatedCardinalityForUnionOfHLLs() { - //given + // given long numberOfElements = 100_000_000; long toleratedDifference = 1_000_000; HashFunction hashFunction = Hashing.murmur3_128(); HLL firstHll = new HLL(15, 5); HLL secondHLL = new HLL(15, 5); - //when + // when LongStream.range(0, numberOfElements).forEach(element -> { - long hashedValue = hashFunction.newHasher().putLong(element).hash().asLong(); - firstHll.addRaw(hashedValue); - } - ); + long hashedValue = hashFunction.newHasher().putLong(element).hash().asLong(); + firstHll.addRaw(hashedValue); + }); LongStream.range(numberOfElements, numberOfElements * 2).forEach(element -> { - long hashedValue = hashFunction.newHasher().putLong(element).hash().asLong(); - secondHLL.addRaw(hashedValue); - } - ); + long hashedValue = hashFunction.newHasher().putLong(element).hash().asLong(); + secondHLL.addRaw(hashedValue); + }); - //then + // then firstHll.union(secondHLL); long cardinality = firstHll.cardinality(); assertThat(cardinality).isCloseTo(numberOfElements * 2, Offset.offset(toleratedDifference * 2)); diff --git a/libraries/src/test/java/com/baeldung/hoverfly/HoverflyApiIntegrationTest.java b/libraries/src/test/java/com/baeldung/hoverfly/HoverflyApiIntegrationTest.java index 167aef5ec6..09d31eac21 100644 --- a/libraries/src/test/java/com/baeldung/hoverfly/HoverflyApiIntegrationTest.java +++ b/libraries/src/test/java/com/baeldung/hoverfly/HoverflyApiIntegrationTest.java @@ -33,38 +33,21 @@ import io.specto.hoverfly.junit.rule.HoverflyRule; public class HoverflyApiIntegrationTest { - private static final SimulationSource source = dsl( - service("http://www.baeldung.com") - .get("/api/courses/1") - .willReturn(success().body( - jsonWithSingleQuotes("{'id':'1','name':'HCI'}"))) - - .post("/api/courses") - .willReturn(success()) - - .andDelay(3, TimeUnit.SECONDS) - .forMethod("POST"), - - service(matches("www.*dung.com")) - .get(startsWith("/api/student")) - .queryParam("page", any()) - .willReturn(success()) - - .post(equalsTo("/api/student")) - .body(equalsToJson(jsonWithSingleQuotes("{'id':'1','name':'Joe'}"))) - .willReturn(success()) - - .put("/api/student/1") - .body(matchesJsonPath("$.name")) - .willReturn(success()) - - .post("/api/student") - .body(equalsToXml("2John")) - .willReturn(success()) - - .put("/api/student/2") - .body(matchesXPath("/student/name")) - .willReturn(success())); + private static final SimulationSource source = dsl(service("http://www.baeldung.com").get("/api/courses/1").willReturn(success().body(jsonWithSingleQuotes("{'id':'1','name':'HCI'}"))) + + .post("/api/courses").willReturn(success()) + + .andDelay(3, TimeUnit.SECONDS).forMethod("POST"), + + service(matches("www.*dung.com")).get(startsWith("/api/student")).queryParam("page", any()).willReturn(success()) + + .post(equalsTo("/api/student")).body(equalsToJson(jsonWithSingleQuotes("{'id':'1','name':'Joe'}"))).willReturn(success()) + + .put("/api/student/1").body(matchesJsonPath("$.name")).willReturn(success()) + + .post("/api/student").body(equalsToXml("2John")).willReturn(success()) + + .put("/api/student/2").body(matchesXPath("/student/name")).willReturn(success())); @ClassRule public static final HoverflyRule rule = HoverflyRule.inSimulationMode(source); @@ -72,19 +55,17 @@ public class HoverflyApiIntegrationTest { @Test public void givenGetCourseById_whenRequestSimulated_thenAPICalledSuccessfully() throws URISyntaxException { - final ResponseEntity courseResponse = restTemplate.getForEntity( - "http://www.baeldung.com/api/courses/1", String.class); - + final ResponseEntity courseResponse = restTemplate.getForEntity("http://www.baeldung.com/api/courses/1", String.class); + assertEquals(HttpStatus.OK, courseResponse.getStatusCode()); assertEquals("{\"id\":\"1\",\"name\":\"HCI\"}", courseResponse.getBody()); } - + @Test public void givenPostCourse_whenDelayInRequest_thenResponseIsDelayed() throws URISyntaxException { StopWatch stopWatch = new StopWatch(); stopWatch.start(); - final ResponseEntity postResponse = restTemplate.postForEntity( - "http://www.baeldung.com/api/courses", null, Void.class); + final ResponseEntity postResponse = restTemplate.postForEntity("http://www.baeldung.com/api/courses", null, Void.class); stopWatch.stop(); long postTime = stopWatch.getTime(); @@ -94,45 +75,36 @@ public class HoverflyApiIntegrationTest { @Test public void givenGetStudent_whenRequestMatcher_thenAPICalledSuccessfully() throws URISyntaxException { - final ResponseEntity courseResponse = restTemplate.getForEntity( - "http://www.baeldung.com/api/student?page=3", Void.class); - + final ResponseEntity courseResponse = restTemplate.getForEntity("http://www.baeldung.com/api/student?page=3", Void.class); + assertEquals(HttpStatus.OK, courseResponse.getStatusCode()); } - + @Test public void givenPostStudent_whenBodyRequestMatcherJson_thenResponseContainsEqualJson() throws URISyntaxException { - final ResponseEntity postResponse = restTemplate.postForEntity( - "http://www.baeldung.com/api/student", "{\"id\":\"1\",\"name\":\"Joe\"}", Void.class); + final ResponseEntity postResponse = restTemplate.postForEntity("http://www.baeldung.com/api/student", "{\"id\":\"1\",\"name\":\"Joe\"}", Void.class); assertEquals(HttpStatus.OK, postResponse.getStatusCode()); } - + @Test public void givenPutStudent_whenJsonPathMatcher_thenRequestJsonContainsElementInPath() throws URISyntaxException { - RequestEntity putRequest = RequestEntity - .put(new URI("http://www.baeldung.com/api/student/1")) - .body("{\"id\":\"1\",\"name\":\"Trevor\"}"); + RequestEntity putRequest = RequestEntity.put(new URI("http://www.baeldung.com/api/student/1")).body("{\"id\":\"1\",\"name\":\"Trevor\"}"); ResponseEntity putResponse = restTemplate.exchange(putRequest, String.class); assertEquals(HttpStatus.OK, putResponse.getStatusCode()); } - + @Test public void givenPostStudent_whenBodyRequestMatcherXml_thenResponseContainsEqualXml() throws URISyntaxException { - final ResponseEntity postResponse = restTemplate.postForEntity( - "http://www.baeldung.com/api/student", "2John", Void.class); + final ResponseEntity postResponse = restTemplate.postForEntity("http://www.baeldung.com/api/student", "2John", Void.class); assertEquals(HttpStatus.OK, postResponse.getStatusCode()); } - - + @Test public void givenPutStudent_whenXPathMatcher_thenRequestXmlContainsElementInXPath() throws URISyntaxException { - RequestEntity putRequest = RequestEntity - .put(new URI("http://www.baeldung.com/api/student/2")) - .body("" - + "2Monica"); + RequestEntity putRequest = RequestEntity.put(new URI("http://www.baeldung.com/api/student/2")).body("" + "2Monica"); ResponseEntity putResponse = restTemplate.exchange(putRequest, String.class); assertEquals(HttpStatus.OK, putResponse.getStatusCode()); diff --git a/libraries/src/test/java/com/baeldung/infinispan/ConfigurationTest.java b/libraries/src/test/java/com/baeldung/infinispan/ConfigurationTest.java new file mode 100644 index 0000000000..9210b18f0c --- /dev/null +++ b/libraries/src/test/java/com/baeldung/infinispan/ConfigurationTest.java @@ -0,0 +1,55 @@ +package com.baeldung.infinispan; + +import com.baeldung.infinispan.listener.CacheListener; +import com.baeldung.infinispan.repository.HelloWorldRepository; +import com.baeldung.infinispan.service.HelloWorldService; +import com.baeldung.infinispan.service.TransactionalService; +import org.infinispan.Cache; +import org.infinispan.manager.DefaultCacheManager; +import org.junit.After; +import org.junit.Before; + +import java.util.function.Supplier; + +public class ConfigurationTest { + + private DefaultCacheManager cacheManager; + + private HelloWorldRepository repository = new HelloWorldRepository(); + + protected HelloWorldService helloWorldService; + protected TransactionalService transactionalService; + + @Before + public void setup() { + CacheConfiguration configuration = new CacheConfiguration(); + CacheListener listener = new CacheListener(); + + cacheManager = configuration.cacheManager(); + + Cache transactionalCache = configuration.transactionalCache(cacheManager, listener); + + Cache simpleHelloWorldCache = configuration.simpleHelloWorldCache(cacheManager, listener); + + Cache expiringHelloWorldCache = configuration.expiringHelloWorldCache(cacheManager, listener); + + Cache evictingHelloWorldCache = configuration.evictingHelloWorldCache(cacheManager, listener); + + Cache passivatingHelloWorldCache = configuration.passivatingHelloWorldCache(cacheManager, listener); + + this.helloWorldService = new HelloWorldService(repository, listener, simpleHelloWorldCache, expiringHelloWorldCache, evictingHelloWorldCache, passivatingHelloWorldCache); + + this.transactionalService = new TransactionalService(transactionalCache); + } + + @After + public void tearDown() { + cacheManager.stop(); + } + + protected long timeThis(Supplier supplier) { + long millis = System.currentTimeMillis(); + supplier.get(); + return System.currentTimeMillis() - millis; + } +} diff --git a/libraries/src/test/java/com/baeldung/infinispan/service/HelloWorldServiceIntegrationTest.java b/libraries/src/test/java/com/baeldung/infinispan/service/HelloWorldServiceIntegrationTest.java new file mode 100644 index 0000000000..0a2ace9ca0 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/infinispan/service/HelloWorldServiceIntegrationTest.java @@ -0,0 +1,51 @@ +package com.baeldung.infinispan.service; + +import com.baeldung.infinispan.ConfigurationTest; +import org.junit.Test; + +import static org.assertj.core.api.Java6Assertions.assertThat; + +public class HelloWorldServiceIntegrationTest extends ConfigurationTest { + + @Test + public void whenGetIsCalledTwoTimes_thenTheSecondShouldHitTheCache() { + assertThat(timeThis(() -> helloWorldService.findSimpleHelloWorld())).isGreaterThanOrEqualTo(1000); + + assertThat(timeThis(() -> helloWorldService.findSimpleHelloWorld())).isLessThan(100); + } + + @Test + public void whenGetIsCalledTwoTimesQuickly_thenTheSecondShouldHitTheCache() { + assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())).isGreaterThanOrEqualTo(1000); + + assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())).isLessThan(100); + } + + @Test + public void whenGetIsCalledTwoTimesSparsely_thenNeitherShouldHitTheCache() throws InterruptedException { + assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())).isGreaterThanOrEqualTo(1000); + + Thread.sleep(1100); + + assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())).isGreaterThanOrEqualTo(1000); + } + + @Test + public void givenOneEntryIsConfigured_whenTwoAreAdded_thenFirstShouldntBeAvailable() { + assertThat(timeThis(() -> helloWorldService.findEvictingHelloWorld("key 1"))).isGreaterThanOrEqualTo(1000); + + assertThat(timeThis(() -> helloWorldService.findEvictingHelloWorld("key 2"))).isGreaterThanOrEqualTo(1000); + + assertThat(timeThis(() -> helloWorldService.findEvictingHelloWorld("key 1"))).isGreaterThanOrEqualTo(1000); + } + + @Test + public void givenOneEntryIsConfigured_whenTwoAreAdded_thenTheFirstShouldBeAvailable() { + assertThat(timeThis(() -> helloWorldService.findPassivatingHelloWorld("key 1"))).isGreaterThanOrEqualTo(1000); + + assertThat(timeThis(() -> helloWorldService.findPassivatingHelloWorld("key 2"))).isGreaterThanOrEqualTo(1000); + + assertThat(timeThis(() -> helloWorldService.findPassivatingHelloWorld("key 1"))).isLessThan(100); + } + +} diff --git a/libraries/src/test/java/com/baeldung/infinispan/service/TransactionalServiceIntegrationTest.java b/libraries/src/test/java/com/baeldung/infinispan/service/TransactionalServiceIntegrationTest.java new file mode 100644 index 0000000000..ace99eef36 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/infinispan/service/TransactionalServiceIntegrationTest.java @@ -0,0 +1,21 @@ +package com.baeldung.infinispan.service; + +import com.baeldung.infinispan.ConfigurationTest; +import org.junit.Test; + +import static org.assertj.core.api.Java6Assertions.assertThat; + +public class TransactionalServiceIntegrationTest extends ConfigurationTest { + + @Test + public void whenLockingAnEntry_thenItShouldBeInaccessible() throws InterruptedException { + Runnable backGroundJob = () -> transactionalService.startBackgroundBatch(); + Thread backgroundThread = new Thread(backGroundJob); + transactionalService.getQuickHowManyVisits(); + backgroundThread.start(); + Thread.sleep(100); // lets wait our thread warm up + + assertThat(timeThis(() -> transactionalService.getQuickHowManyVisits())).isGreaterThan(500).isLessThan(1000); + } + +} diff --git a/libraries/src/test/java/com/baeldung/jasypt/JasyptUnitTest.java b/libraries/src/test/java/com/baeldung/jasypt/JasyptUnitTest.java index 5e65c585aa..d67c2a5cb2 100644 --- a/libraries/src/test/java/com/baeldung/jasypt/JasyptUnitTest.java +++ b/libraries/src/test/java/com/baeldung/jasypt/JasyptUnitTest.java @@ -1,6 +1,5 @@ package com.baeldung.jasypt; - import org.jasypt.encryption.pbe.PooledPBEStringEncryptor; import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; import org.jasypt.util.password.BasicPasswordEncryptor; @@ -17,16 +16,16 @@ public class JasyptUnitTest { @Test public void givenTextPrivateData_whenDecrypt_thenCompareToEncrypted() { - //given + // given BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); String privateData = "secret-data"; textEncryptor.setPasswordCharArray("some-random-data".toCharArray()); - //when + // when String myEncryptedText = textEncryptor.encrypt(privateData); - assertNotSame(privateData, myEncryptedText); //myEncryptedText can be save in db + assertNotSame(privateData, myEncryptedText); // myEncryptedText can be save in db - //then + // then String plainText = textEncryptor.decrypt(myEncryptedText); assertEquals(plainText, privateData); } @@ -37,10 +36,10 @@ public class JasyptUnitTest { BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor(); String encryptedPassword = passwordEncryptor.encryptPassword(password); - //when + // when boolean result = passwordEncryptor.checkPassword("secret-pass", encryptedPassword); - //then + // then assertTrue(result); } @@ -50,28 +49,27 @@ public class JasyptUnitTest { BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor(); String encryptedPassword = passwordEncryptor.encryptPassword(password); - //when + // when boolean result = passwordEncryptor.checkPassword("secret-pass-not-same", encryptedPassword); - //then + // then assertFalse(result); } - @Test @Ignore("should have installed local_policy.jar") public void givenTextPrivateData_whenDecrypt_thenCompareToEncryptedWithCustomAlgorithm() { - //given + // given StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); String privateData = "secret-data"; encryptor.setPassword("some-random-data"); encryptor.setAlgorithm("PBEWithMD5AndTripleDES"); - //when + // when String encryptedText = encryptor.encrypt("secret-pass"); assertNotSame(privateData, encryptedText); - //then + // then String plainText = encryptor.decrypt(encryptedText); assertEquals(plainText, privateData); } @@ -79,18 +77,18 @@ public class JasyptUnitTest { @Test @Ignore("should have installed local_policy.jar") public void givenTextPrivateData_whenDecryptOnHighPerformance_thenDecrypt() { - //given + // given String privateData = "secret-data"; PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor(); encryptor.setPoolSize(4); encryptor.setPassword("some-random-data"); encryptor.setAlgorithm("PBEWithMD5AndTripleDES"); - //when + // when String encryptedText = encryptor.encrypt(privateData); assertNotSame(privateData, encryptedText); - //then + // then String plainText = encryptor.decrypt(encryptedText); assertEquals(plainText, privateData); } diff --git a/libraries/src/test/java/com/baeldung/java/io/JavaDirectoryDeleteUnitTest.java b/libraries/src/test/java/com/baeldung/java/io/JavaDirectoryDeleteUnitTest.java index 1cd390b873..53d9d11bbb 100644 --- a/libraries/src/test/java/com/baeldung/java/io/JavaDirectoryDeleteUnitTest.java +++ b/libraries/src/test/java/com/baeldung/java/io/JavaDirectoryDeleteUnitTest.java @@ -110,10 +110,7 @@ public class JavaDirectoryDeleteUnitTest { public void givenDirectory_whenDeletedWithFilesWalk_thenIsGone() throws IOException { Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME); - Files.walk(pathToBeDeleted) - .sorted(Comparator.reverseOrder()) - .map(Path::toFile) - .forEach(File::delete); + Files.walk(pathToBeDeleted).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); assertFalse("Directory still exists", Files.exists(pathToBeDeleted)); } diff --git a/libraries/src/test/java/com/baeldung/javassist/JavasisstUnitTest.java b/libraries/src/test/java/com/baeldung/javassist/JavasisstUnitTest.java index 30c034aa5e..2dae2adc51 100644 --- a/libraries/src/test/java/com/baeldung/javassist/JavasisstUnitTest.java +++ b/libraries/src/test/java/com/baeldung/javassist/JavasisstUnitTest.java @@ -1,6 +1,5 @@ package com.baeldung.javassist; - import javassist.CannotCompileException; import javassist.ClassPool; import javassist.NotFoundException; @@ -33,20 +32,20 @@ import static org.junit.Assert.assertTrue; public class JavasisstUnitTest { @Test public void givenJavasisstAPI_whenConstructClass_thenGenerateAClassFile() throws CannotCompileException, IOException, ClassNotFoundException, IllegalAccessException, InstantiationException { - //given + // given String classNameWithPackage = "com.baeldung.JavassistGeneratedClass"; ClassFile cf = new ClassFile(false, classNameWithPackage, null); - cf.setInterfaces(new String[]{"java.lang.Cloneable"}); + cf.setInterfaces(new String[] { "java.lang.Cloneable" }); FieldInfo f = new FieldInfo(cf.getConstPool(), "id", "I"); f.setAccessFlags(AccessFlag.PUBLIC); cf.addField(f); - //when + // when String className = "JavassistGeneratedClass.class"; cf.write(new DataOutputStream(new FileOutputStream(className))); - //then + // then ClassPool classPool = ClassPool.getDefault(); Field[] fields = classPool.makeClass(cf).toClass().getFields(); assertEquals(fields[0].getName(), "id"); @@ -57,14 +56,14 @@ public class JavasisstUnitTest { @Test public void givenJavaClass_whenLoadAtByJavassist_thenTraversWholeClass() throws NotFoundException, CannotCompileException, BadBytecode { - //given + // given ClassPool cp = ClassPool.getDefault(); ClassFile cf = cp.get("com.baeldung.javasisst.Point").getClassFile(); MethodInfo minfo = cf.getMethod("move"); CodeAttribute ca = minfo.getCodeAttribute(); CodeIterator ci = ca.iterator(); - //when + // when List operations = new LinkedList<>(); while (ci.hasNext()) { int index = ci.next(); @@ -72,23 +71,21 @@ public class JavasisstUnitTest { operations.add(Mnemonic.OPCODE[op]); } - //then - assertEquals(operations, - Arrays.asList("aload_0", "iload_1", "putfield", "aload_0", "iload_2", "putfield", "return")); + // then + assertEquals(operations, Arrays.asList("aload_0", "iload_1", "putfield", "aload_0", "iload_2", "putfield", "return")); } @Test public void givenTableOfInstructions_whenAddNewInstruction_thenShouldConstructProperSequence() throws NotFoundException, BadBytecode, CannotCompileException, IllegalAccessException, InstantiationException { - //given + // given ClassFile cf = ClassPool.getDefault().get("com.baeldung.javasisst.ThreeDimensionalPoint").getClassFile(); - //when + // when FieldInfo f = new FieldInfo(cf.getConstPool(), "id", "I"); f.setAccessFlags(AccessFlag.PUBLIC); cf.addField(f); - ClassPool classPool = ClassPool.getDefault(); Field[] fields = classPool.makeClass(cf).toClass().getFields(); List fieldsList = Stream.of(fields).map(Field::getName).collect(Collectors.toList()); @@ -98,19 +95,19 @@ public class JavasisstUnitTest { @Test public void givenLoadedClass_whenAddConstructorToClass_shouldCreateClassWithConstructor() throws NotFoundException, CannotCompileException, BadBytecode { - //given + // given ClassFile cf = ClassPool.getDefault().get("com.baeldung.javasisst.Point").getClassFile(); Bytecode code = new Bytecode(cf.getConstPool()); code.addAload(0); code.addInvokespecial("java/lang/Object", MethodInfo.nameInit, "()V"); code.addReturn(null); - //when + // when MethodInfo minfo = new MethodInfo(cf.getConstPool(), MethodInfo.nameInit, "()V"); minfo.setCodeAttribute(code.toCodeAttribute()); cf.addMethod(minfo); - //then + // then CodeIterator ci = code.toCodeAttribute().iterator(); List operations = new LinkedList<>(); while (ci.hasNext()) { @@ -119,9 +116,7 @@ public class JavasisstUnitTest { operations.add(Mnemonic.OPCODE[op]); } - assertEquals(operations, - Arrays.asList("aload_0", "invokespecial", "return")); - + assertEquals(operations, Arrays.asList("aload_0", "invokespecial", "return")); } } diff --git a/libraries/src/test/java/com/baeldung/javatuples/JavaTuplesUnitTest.java b/libraries/src/test/java/com/baeldung/javatuples/JavaTuplesUnitTest.java index a341d5957a..73dfbae3b2 100644 --- a/libraries/src/test/java/com/baeldung/javatuples/JavaTuplesUnitTest.java +++ b/libraries/src/test/java/com/baeldung/javatuples/JavaTuplesUnitTest.java @@ -26,7 +26,7 @@ public class JavaTuplesUnitTest { Pair pairFromList = Pair.fromIterable(collectionOfNames, 2); - String[] names = new String[]{"john", "doe", "anne"}; + String[] names = new String[] { "john", "doe", "anne" }; Triplet triplet2 = Triplet.fromArray(names); } diff --git a/libraries/src/test/java/com/baeldung/javers/JaversUnitTest.java b/libraries/src/test/java/com/baeldung/javers/JaversUnitTest.java index 3cdb833953..a8a7df659b 100644 --- a/libraries/src/test/java/com/baeldung/javers/JaversUnitTest.java +++ b/libraries/src/test/java/com/baeldung/javers/JaversUnitTest.java @@ -1,6 +1,5 @@ package com.baeldung.javers; - import org.javers.common.collections.Lists; import org.javers.core.Javers; import org.javers.core.JaversBuilder; @@ -21,16 +20,16 @@ public class JaversUnitTest { @Test public void givenPersonObject_whenApplyModificationOnIt_thenShouldDetectChange() { - //given + // given Javers javers = JaversBuilder.javers().build(); Person person = new Person(1, "Michael Program"); Person personAfterModification = new Person(1, "Michael Java"); - //when + // when Diff diff = javers.compare(person, personAfterModification); - //then + // then ValueChange change = diff.getChangesByType(ValueChange.class).get(0); assertThat(diff.getChanges()).hasSize(1); @@ -39,22 +38,20 @@ public class JaversUnitTest { assertThat(change.getRight()).isEqualTo("Michael Java"); } - @Test public void givenListOfPersons_whenCompare_ThenShouldDetectChanges() { - //given + // given Javers javers = JaversBuilder.javers().build(); Person personThatWillBeRemoved = new Person(2, "Thomas Link"); List oldList = Lists.asList(new Person(1, "Michael Program"), personThatWillBeRemoved); List newList = Lists.asList(new Person(1, "Michael Not Program")); - //when + // when Diff diff = javers.compareCollections(oldList, newList, Person.class); - //then + // then assertThat(diff.getChanges()).hasSize(3); - ValueChange valueChange = diff.getChangesByType(ValueChange.class).get(0); assertThat(valueChange.getPropertyName()).isEqualTo("name"); assertThat(valueChange.getLeft()).isEqualTo("Michael Program"); @@ -70,43 +67,36 @@ public class JaversUnitTest { @Test public void givenListOfPerson_whenPersonHasNewAddress_thenDetectThatChange() { - //given + // given Javers javers = JaversBuilder.javers().build(); - PersonWithAddress person = - new PersonWithAddress(1, "Tom", Arrays.asList(new Address("England"))); + PersonWithAddress person = new PersonWithAddress(1, "Tom", Arrays.asList(new Address("England"))); - PersonWithAddress personWithNewAddress = - new PersonWithAddress(1, "Tom", - Arrays.asList(new Address("England"), new Address("USA"))); + PersonWithAddress personWithNewAddress = new PersonWithAddress(1, "Tom", Arrays.asList(new Address("England"), new Address("USA"))); - - //when + // when Diff diff = javers.compare(person, personWithNewAddress); List objectsByChangeType = diff.getObjectsByChangeType(NewObject.class); - //then + // then assertThat(objectsByChangeType).hasSize(1); assertThat(objectsByChangeType.get(0).equals(new Address("USA"))); } @Test public void givenListOfPerson_whenPersonRemovedAddress_thenDetectThatChange() { - //given + // given Javers javers = JaversBuilder.javers().build(); - PersonWithAddress person = - new PersonWithAddress(1, "Tom", Arrays.asList(new Address("England"))); + PersonWithAddress person = new PersonWithAddress(1, "Tom", Arrays.asList(new Address("England"))); - PersonWithAddress personWithNewAddress = - new PersonWithAddress(1, "Tom", Collections.emptyList()); + PersonWithAddress personWithNewAddress = new PersonWithAddress(1, "Tom", Collections.emptyList()); - - //when + // when Diff diff = javers.compare(person, personWithNewAddress); List objectsByChangeType = diff.getObjectsByChangeType(ObjectRemoved.class); - //then + // then assertThat(objectsByChangeType).hasSize(1); assertThat(objectsByChangeType.get(0).equals(new Address("England"))); } diff --git a/libraries/src/test/java/com/baeldung/jcache/CacheLoaderTest.java b/libraries/src/test/java/com/baeldung/jcache/CacheLoaderTest.java index da4f51674f..a4747785cd 100644 --- a/libraries/src/test/java/com/baeldung/jcache/CacheLoaderTest.java +++ b/libraries/src/test/java/com/baeldung/jcache/CacheLoaderTest.java @@ -23,15 +23,13 @@ public class CacheLoaderTest { public void setup() { CachingProvider cachingProvider = Caching.getCachingProvider(); CacheManager cacheManager = cachingProvider.getCacheManager(); - MutableConfiguration config = new MutableConfiguration().setReadThrough(true) - .setCacheLoaderFactory(new FactoryBuilder.SingletonFactory<>(new SimpleCacheLoader())); + MutableConfiguration config = new MutableConfiguration().setReadThrough(true).setCacheLoaderFactory(new FactoryBuilder.SingletonFactory<>(new SimpleCacheLoader())); this.cache = cacheManager.createCache("SimpleCache", config); } @After public void tearDown() { - Caching.getCachingProvider() - .getCacheManager().destroyCache(CACHE_NAME); + Caching.getCachingProvider().getCacheManager().destroyCache(CACHE_NAME); } @Test diff --git a/libraries/src/test/java/com/baeldung/jcache/EntryProcessorTest.java b/libraries/src/test/java/com/baeldung/jcache/EntryProcessorIntegrationTest.java similarity index 89% rename from libraries/src/test/java/com/baeldung/jcache/EntryProcessorTest.java rename to libraries/src/test/java/com/baeldung/jcache/EntryProcessorIntegrationTest.java index eb40e63ef0..61c98d0126 100644 --- a/libraries/src/test/java/com/baeldung/jcache/EntryProcessorTest.java +++ b/libraries/src/test/java/com/baeldung/jcache/EntryProcessorIntegrationTest.java @@ -12,7 +12,7 @@ import javax.cache.spi.CachingProvider; import static org.junit.Assert.assertEquals; -public class EntryProcessorTest { +public class EntryProcessorIntegrationTest { private static final String CACHE_NAME = "MyCache"; @@ -29,8 +29,7 @@ public class EntryProcessorTest { @After public void tearDown() { - Caching.getCachingProvider() - .getCacheManager().destroyCache(CACHE_NAME); + Caching.getCachingProvider().getCacheManager().destroyCache(CACHE_NAME); } @Test diff --git a/libraries/src/test/java/com/baeldung/jcache/EventListenerTest.java b/libraries/src/test/java/com/baeldung/jcache/EventListenerTest.java index be83e572d8..e32e4ad3cc 100644 --- a/libraries/src/test/java/com/baeldung/jcache/EventListenerTest.java +++ b/libraries/src/test/java/com/baeldung/jcache/EventListenerTest.java @@ -33,14 +33,12 @@ public class EventListenerTest { @After public void tearDown() { - Caching.getCachingProvider() - .getCacheManager().destroyCache(CACHE_NAME); + Caching.getCachingProvider().getCacheManager().destroyCache(CACHE_NAME); } @Test public void whenRunEvent_thenCorrect() throws InterruptedException { - this.listenerConfiguration = new MutableCacheEntryListenerConfiguration<>(FactoryBuilder - .factoryOf(this.listener), null, false, true); + this.listenerConfiguration = new MutableCacheEntryListenerConfiguration<>(FactoryBuilder.factoryOf(this.listener), null, false, true); this.cache.registerCacheEntryListener(this.listenerConfiguration); assertEquals(false, this.listener.getCreated()); diff --git a/libraries/src/test/java/com/baeldung/jcache/JCacheTest.java b/libraries/src/test/java/com/baeldung/jcache/JCacheIntegrationTest.java similarity index 95% rename from libraries/src/test/java/com/baeldung/jcache/JCacheTest.java rename to libraries/src/test/java/com/baeldung/jcache/JCacheIntegrationTest.java index c98539a9ec..fac3d32bcb 100644 --- a/libraries/src/test/java/com/baeldung/jcache/JCacheTest.java +++ b/libraries/src/test/java/com/baeldung/jcache/JCacheIntegrationTest.java @@ -10,7 +10,7 @@ import javax.cache.spi.CachingProvider; import static org.junit.Assert.assertEquals; -public class JCacheTest { +public class JCacheIntegrationTest { @Test public void instantiateCache() { diff --git a/libraries/src/test/java/com/baeldung/jdo/GuideToJDOIntegrationTest.java b/libraries/src/test/java/com/baeldung/jdo/GuideToJDOIntegrationTest.java index e916a229f7..03e63c2580 100644 --- a/libraries/src/test/java/com/baeldung/jdo/GuideToJDOIntegrationTest.java +++ b/libraries/src/test/java/com/baeldung/jdo/GuideToJDOIntegrationTest.java @@ -106,5 +106,4 @@ public class GuideToJDOIntegrationTest { } } - } \ No newline at end of file diff --git a/libraries/src/test/java/com/baeldung/jetty/JettyIntegrationTest.java b/libraries/src/test/java/com/baeldung/jetty/JettyIntegrationTest.java index 28d4f57e77..b47d3a2e19 100644 --- a/libraries/src/test/java/com/baeldung/jetty/JettyIntegrationTest.java +++ b/libraries/src/test/java/com/baeldung/jetty/JettyIntegrationTest.java @@ -1,14 +1,11 @@ package com.baeldung.jetty; - import org.apache.commons.io.IOUtils; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; -import org.junit.After; import org.junit.AfterClass; -import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; @@ -32,26 +29,26 @@ public class JettyIntegrationTest { @Test public void givenServer_whenSendRequestToBlockingServlet_thenReturnStatusOK() throws Exception { - //given + // given String url = "http://localhost:8090/status"; HttpClient client = HttpClientBuilder.create().build(); HttpGet request = new HttpGet(url); HttpResponse response = client.execute(request); - //then + // then assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200); } @Test public void givenServer_whenSendRequestToNonBlockingServlet_thenReturnStatusOK() throws Exception { - //when + // when String url = "http://localhost:8090/heavy/async"; HttpClient client = HttpClientBuilder.create().build(); HttpGet request = new HttpGet(url); HttpResponse response = client.execute(request); - //then + // then assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200); String responseContent = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8); assertThat(responseContent).isEqualTo("This is some heavy resource that will be served in an async way"); diff --git a/libraries/src/test/java/com/baeldung/jetty/JettyServerFactoryUnitTest.java b/libraries/src/test/java/com/baeldung/jetty/JettyServerFactoryUnitTest.java new file mode 100644 index 0000000000..75b86e46b2 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/jetty/JettyServerFactoryUnitTest.java @@ -0,0 +1,86 @@ +package com.baeldung.jetty; + +import org.apache.http.HttpHost; +import org.apache.http.HttpRequest; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.HttpClientBuilder; +import org.eclipse.jetty.server.Server; +import org.junit.Assert; +import org.junit.Test; + +/** + * Test for {@link JettyServerFactory}. + * + * @author Donato Rimenti + * + */ +public class JettyServerFactoryUnitTest { + + /** + * Tests that when a base server is provided a request returns a status 404. + * + * @throws Exception + */ + @Test + public void givenBaseServer_whenHttpRequest_thenStatus404() throws Exception { + Server server = JettyServerFactory.createBaseServer(); + server.start(); + + int statusCode = sendGetRequest(); + + Assert.assertEquals(404, statusCode); + server.stop(); + } + + /** + * Tests that when a web app server is provided a request returns a status + * 200. + * + * @throws Exception + */ + @Test + public void givenWebAppServer_whenHttpRequest_thenStatus200() throws Exception { + Server server = JettyServerFactory.createWebAppServer(); + server.start(); + + int statusCode = sendGetRequest(); + + Assert.assertEquals(200, statusCode); + server.stop(); + } + + /** + * Tests that when a multi handler server is provided a request returns a + * status 200. + * + * @throws Exception + */ + @Test + public void givenMultiHandlerServerServer_whenHttpRequest_thenStatus200() throws Exception { + Server server = JettyServerFactory.createMultiHandlerServer(); + server.start(); + + int statusCode = sendGetRequest(); + + Assert.assertEquals(200, statusCode); + server.stop(); + } + + /** + * Sends a default HTTP GET request to the server and returns the response + * status code. + * + * @return the status code of the response + * @throws Exception + */ + private int sendGetRequest() throws Exception { + HttpHost target = new HttpHost("localhost", JettyServerFactory.SERVER_PORT); + HttpRequest request = new HttpGet(JettyServerFactory.APP_PATH); + HttpClient client = HttpClientBuilder.create().build(); + HttpResponse response = client.execute(target, request); + return response.getStatusLine().getStatusCode(); + } + +} diff --git a/libraries/src/test/java/com/baeldung/jool/JOOLTest.java b/libraries/src/test/java/com/baeldung/jool/JOOLTest.java index ba20e153fd..2cb393abd3 100644 --- a/libraries/src/test/java/com/baeldung/jool/JOOLTest.java +++ b/libraries/src/test/java/com/baeldung/jool/JOOLTest.java @@ -28,86 +28,53 @@ public class JOOLTest { assertEquals(concat, Arrays.asList(1, 2, 3, 4, 5, 6)); - assertTrue(Seq.of(1, 2, 3, 4).contains(2)); - assertTrue(Seq.of(1, 2, 3, 4).containsAll(2, 3)); - assertTrue(Seq.of(1, 2, 3, 4).containsAny(2, 5)); } @Test public void givenStreams_whenJoin_shouldHaveElementsFromTwoStreams() { - //given + // given Stream left = Stream.of(1, 2, 4); Stream right = Stream.of(1, 2, 3); - //when + // when List rightCollected = right.collect(Collectors.toList()); List collect = left.filter(rightCollected::contains).collect(Collectors.toList()); - //then + // then assertEquals(collect, Arrays.asList(1, 2)); } @Test public void givenSeq_whenJoin_shouldHaveElementsFromBothSeq() { - assertEquals( - Seq.of(1, 2, 4).innerJoin(Seq.of(1, 2, 3), Objects::equals).toList(), - Arrays.asList(tuple(1, 1), tuple(2, 2)) - ); + assertEquals(Seq.of(1, 2, 4).innerJoin(Seq.of(1, 2, 3), Objects::equals).toList(), Arrays.asList(tuple(1, 1), tuple(2, 2))); + assertEquals(Seq.of(1, 2, 4).leftOuterJoin(Seq.of(1, 2, 3), Objects::equals).toList(), Arrays.asList(tuple(1, 1), tuple(2, 2), tuple(4, null))); - assertEquals( - Seq.of(1, 2, 4).leftOuterJoin(Seq.of(1, 2, 3), Objects::equals).toList(), - Arrays.asList(tuple(1, 1), tuple(2, 2), tuple(4, null)) - ); + assertEquals(Seq.of(1, 2, 4).rightOuterJoin(Seq.of(1, 2, 3), Objects::equals).toList(), Arrays.asList(tuple(1, 1), tuple(2, 2), tuple(null, 3))); - assertEquals( - Seq.of(1, 2, 4).rightOuterJoin(Seq.of(1, 2, 3), Objects::equals).toList(), - Arrays.asList(tuple(1, 1), tuple(2, 2), tuple(null, 3)) - ); - - assertEquals( - Seq.of(1, 2).crossJoin(Seq.of("A", "B")).toList(), - Arrays.asList(tuple(1, "A"), tuple(1, "B"), tuple(2, "A"), tuple(2, "B")) - ); + assertEquals(Seq.of(1, 2).crossJoin(Seq.of("A", "B")).toList(), Arrays.asList(tuple(1, "A"), tuple(1, "B"), tuple(2, "A"), tuple(2, "B"))); } @Test public void givenSeq_whenManipulateSeq_seqShouldHaveNewElementsInIt() { - assertEquals( - Seq.of(1, 2, 3).cycle().limit(9).toList(), - Arrays.asList(1, 2, 3, 1, 2, 3, 1, 2, 3) - ); + assertEquals(Seq.of(1, 2, 3).cycle().limit(9).toList(), Arrays.asList(1, 2, 3, 1, 2, 3, 1, 2, 3)); - assertEquals( - Seq.of(1, 2, 3).duplicate().map((first, second) -> tuple(first.toList(), second.toList())), - tuple(Arrays.asList(1, 2, 3), Arrays.asList(1, 2, 3)) - ); + assertEquals(Seq.of(1, 2, 3).duplicate().map((first, second) -> tuple(first.toList(), second.toList())), tuple(Arrays.asList(1, 2, 3), Arrays.asList(1, 2, 3))); - assertEquals( - Seq.of(1, 2, 3, 4).intersperse(0).toList(), - Arrays.asList(1, 0, 2, 0, 3, 0, 4) - ); + assertEquals(Seq.of(1, 2, 3, 4).intersperse(0).toList(), Arrays.asList(1, 0, 2, 0, 3, 0, 4)); - assertEquals( - Seq.of(1, 2, 3, 4, 5).shuffle().toList().size(), - 5 - ); + assertEquals(Seq.of(1, 2, 3, 4, 5).shuffle().toList().size(), 5); - assertEquals( - Seq.of(1, 2, 3, 4).partition(i -> i > 2).map((first, second) -> tuple(first.toList(), second.toList())), - tuple(Arrays.asList(3, 4), Arrays.asList(1, 2)) + assertEquals(Seq.of(1, 2, 3, 4).partition(i -> i > 2).map((first, second) -> tuple(first.toList(), second.toList())), tuple(Arrays.asList(3, 4), Arrays.asList(1, 2)) ); - assertEquals( - Seq.of(1, 2, 3, 4).reverse().toList(), - Arrays.asList(4, 3, 2, 1) - ); + assertEquals(Seq.of(1, 2, 3, 4).reverse().toList(), Arrays.asList(4, 3, 2, 1)); } @Test @@ -117,66 +84,38 @@ public class JOOLTest { expectedAfterGroupBy.put(1, Arrays.asList(1, 3)); expectedAfterGroupBy.put(0, Arrays.asList(2, 4)); - assertEquals( - Seq.of(1, 2, 3, 4).groupBy(i -> i % 2), - expectedAfterGroupBy - ); + assertEquals(Seq.of(1, 2, 3, 4).groupBy(i -> i % 2), expectedAfterGroupBy); + assertEquals(Seq.of("a", "b", "c").foldLeft("!", (u, t) -> u + t), "!abc"); - assertEquals( - Seq.of("a", "b", "c").foldLeft("!", (u, t) -> u + t), - "!abc" - ); - - - assertEquals( - Seq.of("a", "b", "c").foldRight("!", (t, u) -> t + u), - "abc!" - ); + assertEquals(Seq.of("a", "b", "c").foldRight("!", (t, u) -> t + u), "abc!"); } @Test public void givenSeq_whenUsingSeqWhile_shouldBehaveAsWhileLoop() { - assertEquals( - Seq.of(1, 2, 3, 4, 5).skipWhile(i -> i < 3).toList(), - Arrays.asList(3, 4, 5) - ); + assertEquals(Seq.of(1, 2, 3, 4, 5).skipWhile(i -> i < 3).toList(), Arrays.asList(3, 4, 5)); - assertEquals( - Seq.of(1, 2, 3, 4, 5).skipUntil(i -> i == 3).toList(), - Arrays.asList(3, 4, 5) - ); + assertEquals(Seq.of(1, 2, 3, 4, 5).skipUntil(i -> i == 3).toList(), Arrays.asList(3, 4, 5)); } @Test public void givenSeq_whenZip_shouldHaveZippedSeq() { - assertEquals( - Seq.of(1, 2, 3).zip(Seq.of("a", "b", "c")).toList(), - Arrays.asList(tuple(1, "a"), tuple(2, "b"), tuple(3, "c")) - ); + assertEquals(Seq.of(1, 2, 3).zip(Seq.of("a", "b", "c")).toList(), Arrays.asList(tuple(1, "a"), tuple(2, "b"), tuple(3, "c"))); - assertEquals( - Seq.of(1, 2, 3).zip(Seq.of("a", "b", "c"), (x, y) -> x + ":" + y).toList(), - Arrays.asList("1:a", "2:b", "3:c") - ); + assertEquals(Seq.of(1, 2, 3).zip(Seq.of("a", "b", "c"), (x, y) -> x + ":" + y).toList(), Arrays.asList("1:a", "2:b", "3:c")); - - assertEquals( - Seq.of("a", "b", "c").zipWithIndex().toList(), - Arrays.asList(tuple("a", 0L), tuple("b", 1L), tuple("c", 2L)) - ); + assertEquals(Seq.of("a", "b", "c").zipWithIndex().toList(), Arrays.asList(tuple("a", 0L), tuple("b", 1L), tuple("c", 2L))); } - public Integer methodThatThrowsChecked(String arg) throws Exception { return arg.length(); } @Test public void givenOperationThatThrowsCheckedException_whenExecuteAndNeedToWrapCheckedIntoUnchecked_shouldPass() { - //when + // when List collect = Stream.of("a", "b", "c").map(elem -> { try { return methodThatThrowsChecked(elem); @@ -186,55 +125,43 @@ public class JOOLTest { } }).collect(Collectors.toList()); - //then - assertEquals( - collect, - Arrays.asList(1, 1, 1) - ); + // then + assertEquals(collect, Arrays.asList(1, 1, 1)); } - @Test public void givenOperationThatThrowsCheckedException_whenExecuteUsingUncheckedFuction_shouldPass() { - //when - List collect = Stream.of("a", "b", "c") - .map(Unchecked.function(this::methodThatThrowsChecked)) - .collect(Collectors.toList()); + // when + List collect = Stream.of("a", "b", "c").map(Unchecked.function(this::methodThatThrowsChecked)).collect(Collectors.toList()); - //then - assertEquals( - collect, - Arrays.asList(1, 1, 1) - ); + // then + assertEquals(collect, Arrays.asList(1, 1, 1)); } @Test public void givenFunction_whenAppliedPartially_shouldAddNumberToPartialArgument() { - //given + // given Function2 addTwoNumbers = (v1, v2) -> v1 + v2; addTwoNumbers.toBiFunction(); Function1 addToTwo = addTwoNumbers.applyPartially(2); - //when + // when Integer result = addToTwo.apply(5); - //then + // then assertEquals(result, (Integer) 7); } @Test public void givenSeqOfTuples_whenTransformToLowerNumberOfTuples_shouldHaveProperResult() { - //given + // given Seq> personDetails = Seq.of(tuple("michael", "similar", 49), tuple("jodie", "variable", 43)); Tuple2 tuple = tuple("winter", "summer"); - //when + // when List> result = personDetails.map(t -> t.limit2().concat(tuple)).toList(); - //then - assertEquals( - result, - Arrays.asList(tuple("michael", "similar", "winter", "summer"), tuple("jodie", "variable", "winter", "summer")) - ); + // then + assertEquals(result, Arrays.asList(tuple("michael", "similar", "winter", "summer"), tuple("jodie", "variable", "winter", "summer"))); } } diff --git a/libraries/src/test/java/com/baeldung/jsonassert/JsonAssertUnitTest.java b/libraries/src/test/java/com/baeldung/jsonassert/JsonAssertUnitTest.java index bdbc101b15..ce9638c4af 100644 --- a/libraries/src/test/java/com/baeldung/jsonassert/JsonAssertUnitTest.java +++ b/libraries/src/test/java/com/baeldung/jsonassert/JsonAssertUnitTest.java @@ -1,6 +1,5 @@ package com.baeldung.jsonassert; - import org.json.JSONException; import org.json.JSONObject; import org.junit.Test; @@ -62,10 +61,8 @@ public class JsonAssertUnitTest { @Test public void givenNestedObjects_whenAssertEquals_thenPass() throws JSONException { - String result = "{id:1,name:\"Juergen\", address:{city:\"Hollywood\", " - + "state:\"LA\", zip:91601}}"; - JSONAssert.assertEquals("{id:1,name:\"Juergen\", address:{city:\"Hollywood\", " - + "state:\"LA\", zip:91601}}", result, false); + String result = "{id:1,name:\"Juergen\", address:{city:\"Hollywood\", " + "state:\"LA\", zip:91601}}"; + JSONAssert.assertEquals("{id:1,name:\"Juergen\", address:{city:\"Hollywood\", " + "state:\"LA\", zip:91601}}", result, false); } @Test @@ -98,32 +95,19 @@ public class JsonAssertUnitTest { @Test public void whenComparingSizeOfArray_thenPass() throws JSONException { String names = "{names:[Alex, Barbera, Charlie, Xavier]}"; - JSONAssert.assertEquals( - "{names:[4]}", - names, - new ArraySizeComparator(JSONCompareMode.LENIENT)); + JSONAssert.assertEquals("{names:[4]}", names, new ArraySizeComparator(JSONCompareMode.LENIENT)); } @Test public void whenComparingContentsOfArray_thenPass() throws JSONException { String ratings = "{ratings:[3.2,3.5,4.1,5,1]}"; - JSONAssert.assertEquals( - "{ratings:[1,5]}", - ratings, - new ArraySizeComparator(JSONCompareMode.LENIENT)); + JSONAssert.assertEquals("{ratings:[1,5]}", ratings, new ArraySizeComparator(JSONCompareMode.LENIENT)); } @Test public void givenValueMatcher_whenComparingUsingRegex_thenPass() throws IllegalArgumentException, JSONException { - JSONAssert.assertEquals("{entry:{id:x}}", "{entry:{id:1, id:2}}", - new CustomComparator( - JSONCompareMode.STRICT, - new Customization("entry.id", - new RegularExpressionValueMatcher("\\d")))); + JSONAssert.assertEquals("{entry:{id:x}}", "{entry:{id:1, id:2}}", new CustomComparator(JSONCompareMode.STRICT, new Customization("entry.id", new RegularExpressionValueMatcher("\\d")))); - JSONAssert.assertNotEquals("{entry:{id:x}}", "{entry:{id:1, id:as}}", - new CustomComparator(JSONCompareMode.STRICT, - new Customization("entry.id", - new RegularExpressionValueMatcher("\\d")))); + JSONAssert.assertNotEquals("{entry:{id:x}}", "{entry:{id:1, id:as}}", new CustomComparator(JSONCompareMode.STRICT, new Customization("entry.id", new RegularExpressionValueMatcher("\\d")))); } } diff --git a/libraries/src/test/java/com/baeldung/junitparams/SafeAdditionUtilTest.java b/libraries/src/test/java/com/baeldung/junitparams/SafeAdditionUtilTest.java index 1c95956761..c8718aef8d 100644 --- a/libraries/src/test/java/com/baeldung/junitparams/SafeAdditionUtilTest.java +++ b/libraries/src/test/java/com/baeldung/junitparams/SafeAdditionUtilTest.java @@ -14,7 +14,7 @@ public class SafeAdditionUtilTest { private SafeAdditionUtil serviceUnderTest = new SafeAdditionUtil(); @Test - @Parameters({"1, 2, 3", "-10, 30, 20", "15, -5, 10", "-5, -10, -15"}) + @Parameters({ "1, 2, 3", "-10, 30, 20", "15, -5, 10", "-5, -10, -15" }) public void whenCalledWithAnnotationProvidedParams_thenSafeAddAndReturn(int a, int b, int expectedValue) { assertEquals(expectedValue, serviceUnderTest.safeAdd(a, b)); } @@ -26,7 +26,7 @@ public class SafeAdditionUtilTest { } private Object[] parametersToTestAdd() { - return new Object[]{new Object[]{1, 2, 3}, new Object[]{-10, 30, 20}, new Object[]{Integer.MAX_VALUE, 2, Integer.MAX_VALUE}, new Object[]{Integer.MIN_VALUE, -8, Integer.MIN_VALUE}}; + return new Object[] { new Object[] { 1, 2, 3 }, new Object[] { -10, 30, 20 }, new Object[] { Integer.MAX_VALUE, 2, Integer.MAX_VALUE }, new Object[] { Integer.MIN_VALUE, -8, Integer.MIN_VALUE } }; } @Test @@ -36,7 +36,7 @@ public class SafeAdditionUtilTest { } private Object[] parametersForWhenCalledWithnoParam_thenLoadByNameSafeAddAndReturn() { - return new Object[]{new Object[]{1, 2, 3}, new Object[]{-10, 30, 20}, new Object[]{Integer.MAX_VALUE, 2, Integer.MAX_VALUE}, new Object[]{Integer.MIN_VALUE, -8, Integer.MIN_VALUE}}; + return new Object[] { new Object[] { 1, 2, 3 }, new Object[] { -10, 30, 20 }, new Object[] { Integer.MAX_VALUE, 2, Integer.MAX_VALUE }, new Object[] { Integer.MIN_VALUE, -8, Integer.MIN_VALUE } }; } @Test diff --git a/libraries/src/test/java/com/baeldung/junitparams/TestDataProvider.java b/libraries/src/test/java/com/baeldung/junitparams/TestDataProvider.java index 08a472502e..d318345a56 100644 --- a/libraries/src/test/java/com/baeldung/junitparams/TestDataProvider.java +++ b/libraries/src/test/java/com/baeldung/junitparams/TestDataProvider.java @@ -3,11 +3,11 @@ package com.baeldung.junitparams; public class TestDataProvider { public static Object[] provideBasicData() { - return new Object[]{new Object[]{1, 2, 3}, new Object[]{-10, 30, 20}, new Object[]{15, -5, 10}, new Object[]{-5, -10, -15}}; + return new Object[] { new Object[] { 1, 2, 3 }, new Object[] { -10, 30, 20 }, new Object[] { 15, -5, 10 }, new Object[] { -5, -10, -15 } }; } public static Object[] provideEdgeCaseData() { - return new Object[]{new Object[]{Integer.MAX_VALUE, 2, Integer.MAX_VALUE}, new Object[]{Integer.MIN_VALUE, -2, Integer.MIN_VALUE},}; + return new Object[] { new Object[] { Integer.MAX_VALUE, 2, Integer.MAX_VALUE }, new Object[] { Integer.MIN_VALUE, -2, Integer.MIN_VALUE }, }; } } diff --git a/libraries/src/test/java/com/baeldung/kafkastreams/KafkaStreamsLiveTest.java b/libraries/src/test/java/com/baeldung/kafkastreams/KafkaStreamsLiveTest.java index 32568e9ea5..4406494d30 100644 --- a/libraries/src/test/java/com/baeldung/kafkastreams/KafkaStreamsLiveTest.java +++ b/libraries/src/test/java/com/baeldung/kafkastreams/KafkaStreamsLiveTest.java @@ -22,7 +22,7 @@ public class KafkaStreamsLiveTest { @Test @Ignore("it needs to have kafka broker running on local") public void shouldTestKafkaStreams() throws InterruptedException { - //given + // given String inputTopic = "inputTopic"; Properties streamsConfiguration = new Properties(); @@ -35,15 +35,12 @@ public class KafkaStreamsLiveTest { // Use a temporary directory for storing state, which will be automatically removed after the test. streamsConfiguration.put(StreamsConfig.STATE_DIR_CONFIG, TestUtils.tempDirectory().getAbsolutePath()); - //when + // when KStreamBuilder builder = new KStreamBuilder(); KStream textLines = builder.stream(inputTopic); Pattern pattern = Pattern.compile("\\W+", Pattern.UNICODE_CHARACTER_CLASS); - KTable wordCounts = textLines - .flatMapValues(value -> Arrays.asList(pattern.split(value.toLowerCase()))) - .groupBy((key, word) -> word) - .count(); + KTable wordCounts = textLines.flatMapValues(value -> Arrays.asList(pattern.split(value.toLowerCase()))).groupBy((key, word) -> word).count(); wordCounts.foreach((word, count) -> System.out.println("word: " + word + " -> " + count)); @@ -55,7 +52,7 @@ public class KafkaStreamsLiveTest { KafkaStreams streams = new KafkaStreams(builder, streamsConfiguration); streams.start(); - //then + // then Thread.sleep(30000); streams.close(); } diff --git a/libraries/src/test/java/com/baeldung/lsh/LocalSensitiveHashingUnitTest.java b/libraries/src/test/java/com/baeldung/lsh/LocalSensitiveHashingUnitTest.java index fbcda2a70d..5928765aaa 100644 --- a/libraries/src/test/java/com/baeldung/lsh/LocalSensitiveHashingUnitTest.java +++ b/libraries/src/test/java/com/baeldung/lsh/LocalSensitiveHashingUnitTest.java @@ -8,16 +8,15 @@ import java.util.Arrays; import static org.assertj.core.api.Assertions.assertThat; - public class LocalSensitiveHashingUnitTest { @Ignore("for simplicity of the example number of input vectors is very low, that's why LSH may yield non deterministic results") @Test() public void givenNVectors_whenPerformLSH_thenShouldCalculateSameHashForSimilarVectors() { - //given - boolean[] vector1 = new boolean[]{true, true, true, true, true}; - boolean[] vector2 = new boolean[]{false, false, false, true, false}; - boolean[] vector3 = new boolean[]{false, false, true, true, false}; + // given + boolean[] vector1 = new boolean[] { true, true, true, true, true }; + boolean[] vector2 = new boolean[] { false, false, false, true, false }; + boolean[] vector3 = new boolean[] { false, false, true, true, false }; int sizeOfVectors = 5; int numberOfBuckets = 10; @@ -25,7 +24,7 @@ public class LocalSensitiveHashingUnitTest { LSHMinHash lsh = new LSHMinHash(stages, numberOfBuckets, sizeOfVectors); - //when + // when int[] firstHash = lsh.hash(vector1); int[] secondHash = lsh.hash(vector2); int[] thirdHash = lsh.hash(vector3); @@ -34,7 +33,7 @@ public class LocalSensitiveHashingUnitTest { System.out.println(Arrays.toString(secondHash)); System.out.println(Arrays.toString(thirdHash)); - //then + // then int lastIndexOfResult = stages - 1; assertThat(firstHash[lastIndexOfResult]).isNotEqualTo(secondHash[lastIndexOfResult]); assertThat(firstHash[lastIndexOfResult]).isNotEqualTo(thirdHash[lastIndexOfResult]); @@ -45,4 +44,3 @@ public class LocalSensitiveHashingUnitTest { return Math.abs(secondHash - thirdHash) < numberOfBuckets / 2; } } - diff --git a/libraries/src/test/java/com/baeldung/mbassador/MBassadorAsyncInvocationTest.java b/libraries/src/test/java/com/baeldung/mbassador/MBassadorAsyncInvocationTest.java index d0b1cafd71..99ea1aab71 100644 --- a/libraries/src/test/java/com/baeldung/mbassador/MBassadorAsyncInvocationTest.java +++ b/libraries/src/test/java/com/baeldung/mbassador/MBassadorAsyncInvocationTest.java @@ -5,8 +5,6 @@ import net.engio.mbassy.listener.Handler; import net.engio.mbassy.listener.Invoke; import org.junit.Before; import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.util.concurrent.atomic.AtomicBoolean; diff --git a/libraries/src/test/java/com/baeldung/mbassador/MBassadorConfigurationTest.java b/libraries/src/test/java/com/baeldung/mbassador/MBassadorConfigurationTest.java index fe9c130d93..9d9a58aee9 100644 --- a/libraries/src/test/java/com/baeldung/mbassador/MBassadorConfigurationTest.java +++ b/libraries/src/test/java/com/baeldung/mbassador/MBassadorConfigurationTest.java @@ -6,8 +6,6 @@ import net.engio.mbassy.bus.error.PublicationError; import net.engio.mbassy.listener.Handler; import org.junit.Before; import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.util.*; @@ -25,8 +23,8 @@ public class MBassadorConfigurationTest implements IPublicationErrorHandler { @Before public void prepareTests() { - dispatcher = new MBassador(this); - dispatcher.subscribe(this); + dispatcher = new MBassador(this); + dispatcher.subscribe(this); } @Test diff --git a/libraries/src/test/java/com/baeldung/neuroph/XORIntegrationTest.java b/libraries/src/test/java/com/baeldung/neuroph/XORIntegrationTest.java index 5da1d166f6..ea5c09a4d8 100644 --- a/libraries/src/test/java/com/baeldung/neuroph/XORIntegrationTest.java +++ b/libraries/src/test/java/com/baeldung/neuroph/XORIntegrationTest.java @@ -10,7 +10,7 @@ import static org.junit.Assert.*; public class XORIntegrationTest { private NeuralNetwork ann = null; - private void print(String input, double output, double actual) { + private void print(String input, double output, double actual) { System.out.println("Testing: " + input + " Expected: " + actual + " Result: " + output); } diff --git a/libraries/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java b/libraries/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java index b152b22964..70d3e41579 100644 --- a/libraries/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java +++ b/libraries/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java @@ -1,6 +1,5 @@ package com.baeldung.pact; - import au.com.dius.pact.consumer.Pact; import au.com.dius.pact.consumer.PactProviderRuleMk2; import au.com.dius.pact.consumer.PactVerification; @@ -23,63 +22,37 @@ import static org.assertj.core.api.Assertions.assertThat; public class PactConsumerDrivenContractUnitTest { @Rule - public PactProviderRuleMk2 mockProvider - = new PactProviderRuleMk2("test_provider", "localhost", 8080, this); + public PactProviderRuleMk2 mockProvider = new PactProviderRuleMk2("test_provider", "localhost", 8080, this); @Pact(consumer = "test_consumer") public RequestResponsePact createPact(PactDslWithProvider builder) { Map headers = new HashMap(); headers.put("Content-Type", "application/json"); - return builder - .given("test GET ") - .uponReceiving("GET REQUEST") - .path("/") - .method("GET") - .willRespondWith() - .status(200) - .headers(headers) - .body("{\"condition\": true, \"name\": \"tom\"}") - .given("test POST") - .uponReceiving("POST REQUEST") - .method("POST") - .headers(headers) - .body("{\"name\": \"Michael\"}") - .path("/create") - .willRespondWith() - .status(201) - .headers(headers) - .body("") - .toPact(); + return builder.given("test GET").uponReceiving("GET REQUEST").path("/pact").method("GET").willRespondWith().status(200).headers(headers).body("{\"condition\": true, \"name\": \"tom\"}").given("test POST").uponReceiving("POST REQUEST").method("POST") + .headers(headers).body("{\"name\": \"Michael\"}").path("/pact").willRespondWith().status(201).toPact(); } - @Test @PactVerification() public void givenGet_whenSendRequest_shouldReturn200WithProperHeaderAndBody() { - //when - ResponseEntity response - = new RestTemplate().getForEntity(mockProvider.getUrl(), String.class); + // when + ResponseEntity response = new RestTemplate().getForEntity(mockProvider.getUrl() + "/pact", String.class); - //then + // then assertThat(response.getStatusCode().value()).isEqualTo(200); assertThat(response.getHeaders().get("Content-Type").contains("application/json")).isTrue(); assertThat(response.getBody()).contains("condition", "true", "name", "tom"); - //and + // and HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_JSON); String jsonBody = "{\"name\": \"Michael\"}"; - //when - ResponseEntity postResponse = new RestTemplate().exchange( - mockProvider.getUrl() + "/create", - HttpMethod.POST, - new HttpEntity<>(jsonBody, httpHeaders), - String.class - ); + // when + ResponseEntity postResponse = new RestTemplate().exchange(mockProvider.getUrl() + "/pact", HttpMethod.POST, new HttpEntity<>(jsonBody, httpHeaders), String.class); - //then + // then assertThat(postResponse.getStatusCode().value()).isEqualTo(201); } diff --git a/libraries/src/test/java/com/baeldung/pairs/ApacheCommonsPairUnitTest.java b/libraries/src/test/java/com/baeldung/pairs/ApacheCommonsPairUnitTest.java index 0e6242b8a3..205f0e545e 100644 --- a/libraries/src/test/java/com/baeldung/pairs/ApacheCommonsPairUnitTest.java +++ b/libraries/src/test/java/com/baeldung/pairs/ApacheCommonsPairUnitTest.java @@ -46,5 +46,4 @@ public class ApacheCommonsPairUnitTest { immutablePair.setValue("Another One"); } - } diff --git a/libraries/src/test/java/com/baeldung/pairs/CoreJavaSimpleEntryUnitTest.java b/libraries/src/test/java/com/baeldung/pairs/CoreJavaSimpleEntryUnitTest.java index ca425339fa..4271d4e003 100644 --- a/libraries/src/test/java/com/baeldung/pairs/CoreJavaSimpleEntryUnitTest.java +++ b/libraries/src/test/java/com/baeldung/pairs/CoreJavaSimpleEntryUnitTest.java @@ -17,15 +17,15 @@ public class CoreJavaSimpleEntryUnitTest { assertEquals(key.intValue(), 1); assertEquals(value, "one"); } - + @Test(expected = UnsupportedOperationException.class) public void givenSimpleImmutableEntry_whenSetValue_thenException() { AbstractMap.SimpleImmutableEntry entry = new AbstractMap.SimpleImmutableEntry(1, "one"); - + entry.setValue("two"); - + } - + @Test public void givenSimpleImmutableEntry_whenGetValue_thenOk() { AbstractMap.SimpleImmutableEntry entry = new AbstractMap.SimpleImmutableEntry(1, "one"); diff --git a/libraries/src/test/java/com/baeldung/pcollections/PCollectionsUnitTest.java b/libraries/src/test/java/com/baeldung/pcollections/PCollectionsUnitTest.java index acc7718ea8..1a75624439 100644 --- a/libraries/src/test/java/com/baeldung/pcollections/PCollectionsUnitTest.java +++ b/libraries/src/test/java/com/baeldung/pcollections/PCollectionsUnitTest.java @@ -86,8 +86,7 @@ public class PCollectionsUnitTest { @Test public void whenMapPSetMethods_thenPerformOperations() { - MapPSet pSet = HashTreePSet.empty() - .plusAll(Arrays.asList("e1", "e2", "e3", "e4")); + MapPSet pSet = HashTreePSet.empty().plusAll(Arrays.asList("e1", "e2", "e3", "e4")); assertEquals(pSet.size(), 4); MapPSet pSet1 = pSet.minus("e4"); diff --git a/libraries/src/test/java/com/baeldung/protonpack/CollectorUtilsTests.java b/libraries/src/test/java/com/baeldung/protonpack/CollectorUtilsTests.java index cf6a1e5ec5..e9d5b8ede5 100644 --- a/libraries/src/test/java/com/baeldung/protonpack/CollectorUtilsTests.java +++ b/libraries/src/test/java/com/baeldung/protonpack/CollectorUtilsTests.java @@ -35,32 +35,21 @@ public class CollectorUtilsTests { @Test public void givenEmptyStream_withCollectorUnique_shouldReturnEmpty() { - assertThat(Stream - .empty() - .collect(CollectorUtils.unique()), equalTo(Optional.empty())); + assertThat(Stream.empty().collect(CollectorUtils.unique()), equalTo(Optional.empty())); } @Test public void givenIntegerStream_withCollectorUnique_shouldReturnUniqueValue() { - assertThat(Stream - .of(1, 2, 3) - .filter(i -> i > 2) - .collect(CollectorUtils.unique()), equalTo(Optional.of(3))); + assertThat(Stream.of(1, 2, 3).filter(i -> i > 2).collect(CollectorUtils.unique()), equalTo(Optional.of(3))); } @Test public void givenIntegerStream_withUniqueNullable_shouldReturnUniqueValue() { - assertThat(Stream - .of(1, 2, 3) - .filter(i -> i > 2) - .collect(CollectorUtils.uniqueNullable()), equalTo(3)); + assertThat(Stream.of(1, 2, 3).filter(i -> i > 2).collect(CollectorUtils.uniqueNullable()), equalTo(3)); } @Test(expected = NonUniqueValueException.class) public void givenIntegerStream_withCollectorUnique_shouldThrowNonUniqueValueException() { - Stream - .of(1, 2, 3) - .filter(i -> i > 1) - .collect(CollectorUtils.unique()); + Stream.of(1, 2, 3).filter(i -> i > 1).collect(CollectorUtils.unique()); } } diff --git a/libraries/src/test/java/com/baeldung/protonpack/StreamUtilsTests.java b/libraries/src/test/java/com/baeldung/protonpack/StreamUtilsTests.java index 37ca71287f..ccd43b7777 100644 --- a/libraries/src/test/java/com/baeldung/protonpack/StreamUtilsTests.java +++ b/libraries/src/test/java/com/baeldung/protonpack/StreamUtilsTests.java @@ -24,9 +24,7 @@ public class StreamUtilsTests { public void givenStream_whenZipWithIndex_shouldReturnZippedStreamWithIndex() { Stream source = Stream.of("Foo", "Bar", "Baz"); - List> zipped = StreamUtils - .zipWithIndex(source) - .collect(Collectors.toList()); + List> zipped = StreamUtils.zipWithIndex(source).collect(Collectors.toList()); assertThat(zipped, contains(Indexed.index(0, "Foo"), Indexed.index(1, "Bar"), Indexed.index(2, "Baz"))); } @@ -36,9 +34,7 @@ public class StreamUtilsTests { Stream streamA = Stream.of("A", "B", "C"); Stream streamB = Stream.of("Apple", "Banana", "Carrot"); - List zipped = StreamUtils - .zip(streamA, streamB, (a, b) -> a + " is for " + b) - .collect(Collectors.toList()); + List zipped = StreamUtils.zip(streamA, streamB, (a, b) -> a + " is for " + b).collect(Collectors.toList()); assertThat(zipped, contains("A is for Apple", "B is for Banana", "C is for Carrot")); } @@ -49,15 +45,13 @@ public class StreamUtilsTests { Stream streamB = Stream.of("aggravating", "banausic", "complaisant"); Stream streamC = Stream.of("Apple", "Banana", "Carrot"); - List zipped = StreamUtils - .zip(streamA, streamB, streamC, (a, b, c) -> a + " is for " + b + " " + c) - .collect(Collectors.toList()); + List zipped = StreamUtils.zip(streamA, streamB, streamC, (a, b, c) -> a + " is for " + b + " " + c).collect(Collectors.toList()); assertThat(zipped, contains("A is for aggravating Apple", "B is for banausic Banana", "C is for complaisant Carrot")); } @Test - //givenThreeStreams_whenMerge_shouldReturnMergedStream + // givenThreeStreams_whenMerge_shouldReturnMergedStream public void givenThreeStreams_whenMerge_shouldReturnMergedStream() { Stream streamA = Stream.of("A", "B", "C"); Stream streamB = Stream.of("apple", "banana", "carrot", "date"); @@ -69,7 +63,7 @@ public class StreamUtilsTests { } @Test - //givenThreeStreams_whenInterleave_shouldReturnRoundRobinInterleavingStream + // givenThreeStreams_whenInterleave_shouldReturnRoundRobinInterleavingStream public void givenThreeStreams_whenInterleave_shouldReturnRoundRobinInterleavingStream() { Stream streamA = Stream.of("Peter", "Paul", "Mary"); Stream streamB = Stream.of("A", "B", "C", "D", "E"); @@ -81,7 +75,7 @@ public class StreamUtilsTests { } @Test - //givenInfiniteStream_whenTakeWhile10_shouldReturnStreamOfSize10 + // givenInfiniteStream_whenTakeWhile10_shouldReturnStreamOfSize10 public void givenInfiniteStream_whenTakeWhile10_shouldReturnStream() { Stream infiniteInts = Stream.iterate(0, i -> i + 1); Stream finiteInts = StreamUtils.takeWhile(infiniteInts, i -> i < 10); @@ -125,9 +119,7 @@ public class StreamUtilsTests { @Test public void giveIntegerStream_whenGroupRuns_shouldReturnListGroupItems() { Stream integerStream = Stream.of(1, 1, 2, 2, 3, 4, 5); - List> runs = StreamUtils - .groupRuns(integerStream) - .collect(toList()); + List> runs = StreamUtils.groupRuns(integerStream).collect(toList()); assertThat(runs, contains(asList(1, 1), asList(2, 2), asList(3), asList(4), asList(5))); } @@ -143,21 +135,17 @@ public class StreamUtilsTests { public void givenIntegerStream_whenWindowed_shouldReturnListOfListOfItemsOfWindowSize() { Stream integerStream = Stream.of(1, 2, 3, 4, 5); - List> windows = StreamUtils - .windowed(integerStream, 2) - .collect(toList()); + List> windows = StreamUtils.windowed(integerStream, 2).collect(toList()); assertThat(windows, contains(asList(1, 2), asList(2, 3), asList(3, 4), asList(4, 5))); } @Test - //givenIntegerStream_whenWindowedWithWindowSizeAndSkip_shouldReturnListOfListOfWindowSizeAddingASkip + // givenIntegerStream_whenWindowedWithWindowSizeAndSkip_shouldReturnListOfListOfWindowSizeAddingASkip public void givenIntegerStream_whenWindowedWithWindowSizeAndSkip_shouldReturnListOfListOfWindowSizeAddingASkip() { Stream integerStream = Stream.of(1, 2, 3, 4, 5); - List> windows = StreamUtils - .windowed(integerStream, 3, 2) - .collect(toList()); + List> windows = StreamUtils.windowed(integerStream, 3, 2).collect(toList()); assertThat(windows, contains(asList(1, 2, 3), asList(3, 4, 5))); } @@ -166,15 +154,9 @@ public class StreamUtilsTests { public void givenEmptyStream_whenWindowed_shouldReturnIterableWithSizeZero() { ArrayList ints = new ArrayList<>(); - ints - .stream() - .collect(maxBy((a, b) -> a - .toString() - .compareTo(b.toString()))); + ints.stream().collect(maxBy((a, b) -> a.toString().compareTo(b.toString()))); - List> windows = StreamUtils - .windowed(ints.stream(), 2) - .collect(toList()); + List> windows = StreamUtils.windowed(ints.stream(), 2).collect(toList()); assertThat(windows, iterableWithSize(0)); } @@ -183,18 +165,14 @@ public class StreamUtilsTests { public void givenIntegerStream_whenWindowedWithWindowSizeAndSkipAndAllowLesserSize_shouldReturnListOfListOfInteger() { Stream integerStream = Stream.of(1, 2, 3, 4, 5); - List> windows = StreamUtils - .windowed(integerStream, 2, 2, true) - .collect(toList()); + List> windows = StreamUtils.windowed(integerStream, 2, 2, true).collect(toList()); assertThat(windows, contains(asList(1, 2), asList(3, 4), asList(5))); } @Test public void givenLimit_withIndices_shouldReturnLongStreamUptoLimit() { - LongStream indices = StreamUtils - .indices() - .limit(500); + LongStream indices = StreamUtils.indices().limit(500); assertThat(indices.count(), equalTo(500)); } diff --git a/libraries/src/test/java/com/baeldung/retrofit/basic/GitHubBasicApiLiveTest.java b/libraries/src/test/java/com/baeldung/retrofit/basic/GitHubBasicApiLiveTest.java index 7170d35aa5..46325d4d23 100644 --- a/libraries/src/test/java/com/baeldung/retrofit/basic/GitHubBasicApiLiveTest.java +++ b/libraries/src/test/java/com/baeldung/retrofit/basic/GitHubBasicApiLiveTest.java @@ -17,46 +17,33 @@ import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; public class GitHubBasicApiLiveTest { - + GitHubBasicApi gitHub; - + @Before public void init() { - Retrofit retrofit = new Retrofit.Builder() - .baseUrl("https://api.github.com/") - .addConverterFactory(GsonConverterFactory.create()) - .build(); - + Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.github.com/").addConverterFactory(GsonConverterFactory.create()).build(); + gitHub = retrofit.create(GitHubBasicApi.class); } - + @Test public void whenListRepos_thenExpectReposThatContainTutorials() { try { - List repos = gitHub - .listRepos("eugenp") - .execute() - .body(); - - assertThat(repos) - .isNotEmpty() - .extracting(Repository::getName).contains("tutorials"); + List repos = gitHub.listRepos("eugenp").execute().body(); + + assertThat(repos).isNotEmpty().extracting(Repository::getName).contains("tutorials"); } catch (IOException e) { fail("Can not communicate with GitHub API"); } } - + @Test public void whenListRepoContributers_thenExpectContributorsThatContainEugenp() { try { - List contributors = gitHub - .listRepoContributors("eugenp", "tutorials") - .execute() - .body(); - - assertThat(contributors) - .isNotEmpty() - .extracting(Contributor::getName).contains("eugenp"); + List contributors = gitHub.listRepoContributors("eugenp", "tutorials").execute().body(); + + assertThat(contributors).isNotEmpty().extracting(Contributor::getName).contains("eugenp"); } catch (IOException e) { fail("Can not communicate with GitHub API"); } diff --git a/libraries/src/test/java/com/baeldung/retrofit/rx/GitHubRxApiTest.java b/libraries/src/test/java/com/baeldung/retrofit/rx/GitHubRxApiTest.java deleted file mode 100644 index c2fbd9bf60..0000000000 --- a/libraries/src/test/java/com/baeldung/retrofit/rx/GitHubRxApiTest.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.baeldung.retrofit.rx; - -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.retrofit.models.Contributor; -import com.baeldung.retrofit.models.Repository; -import com.baeldung.retrofit.rx.GitHubRxApi; - -import retrofit2.Retrofit; -import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory; -import retrofit2.converter.gson.GsonConverterFactory; - -public class GitHubRxApiTest { - - GitHubRxApi gitHub; - - @Before - public void init() { - Retrofit retrofit = new Retrofit.Builder() - .baseUrl("https://api.github.com/") - .addConverterFactory(GsonConverterFactory.create()) - .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) - .build(); - - gitHub = retrofit.create(GitHubRxApi.class); - } - - @Test - public void whenListRepos_thenExpectReposThatContainTutorials() { - gitHub - .listRepos("eugenp") - .subscribe( repos -> { - assertThat(repos) - .isNotEmpty() - .extracting(Repository::getName).contains("tutorials"); - }); - } - - @Test - public void whenListRepoContributers_thenExpectContributorsThatContainEugenp() { - gitHub - .listRepoContributors("eugenp", "tutorials") - .subscribe(contributors -> { - assertThat(contributors) - .isNotEmpty() - .extracting(Contributor::getName).contains("eugenp"); - }); - } - -} diff --git a/libraries/src/test/java/com/baeldung/retrofit/rx/GitHubRxLiveTest.java b/libraries/src/test/java/com/baeldung/retrofit/rx/GitHubRxLiveTest.java new file mode 100644 index 0000000000..7ab4af3af2 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/retrofit/rx/GitHubRxLiveTest.java @@ -0,0 +1,41 @@ +package com.baeldung.retrofit.rx; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.retrofit.models.Contributor; +import com.baeldung.retrofit.models.Repository; +import com.baeldung.retrofit.rx.GitHubRxApi; + +import retrofit2.Retrofit; +import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory; +import retrofit2.converter.gson.GsonConverterFactory; + +public class GitHubRxLiveTest { + + GitHubRxApi gitHub; + + @Before + public void init() { + Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.github.com/").addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build(); + + gitHub = retrofit.create(GitHubRxApi.class); + } + + @Test + public void whenListRepos_thenExpectReposThatContainTutorials() { + gitHub.listRepos("eugenp").subscribe(repos -> { + assertThat(repos).isNotEmpty().extracting(Repository::getName).contains("tutorials"); + }); + } + + @Test + public void whenListRepoContributers_thenExpectContributorsThatContainEugenp() { + gitHub.listRepoContributors("eugenp", "tutorials").subscribe(contributors -> { + assertThat(contributors).isNotEmpty().extracting(Contributor::getName).contains("eugenp"); + }); + } + +} diff --git a/libraries/src/test/java/com/baeldung/serenity/GoogleSearchLiveTest.java b/libraries/src/test/java/com/baeldung/serenity/GoogleSearchLiveTest.java index 1ebbd49e79..57bb7c1242 100644 --- a/libraries/src/test/java/com/baeldung/serenity/GoogleSearchLiveTest.java +++ b/libraries/src/test/java/com/baeldung/serenity/GoogleSearchLiveTest.java @@ -23,15 +23,11 @@ public class GoogleSearchLiveTest { public void whenGoogleBaeldungThenShouldSeeEugen() { browser.get("https://www.google.com/ncr"); - browser - .findElement(By.name("q")) - .sendKeys("baeldung", Keys.ENTER); + browser.findElement(By.name("q")).sendKeys("baeldung", Keys.ENTER); new WebDriverWait(browser, 5).until(visibilityOfElementLocated(By.cssSelector("._ksh"))); - assertThat(browser - .findElement(By.cssSelector("._ksh")) - .getText(), containsString("Eugen (Baeldung)")); + assertThat(browser.findElement(By.cssSelector("._ksh")).getText(), containsString("Eugen (Baeldung)")); } } diff --git a/libraries/src/test/java/com/baeldung/serenity/github/GithubRestUserAPISteps.java b/libraries/src/test/java/com/baeldung/serenity/github/GithubRestUserAPISteps.java index 2ba5b1c8ed..887b4cde7d 100644 --- a/libraries/src/test/java/com/baeldung/serenity/github/GithubRestUserAPISteps.java +++ b/libraries/src/test/java/com/baeldung/serenity/github/GithubRestUserAPISteps.java @@ -43,9 +43,6 @@ public class GithubRestUserAPISteps { private static HttpResponse getGithubUserProfile(String api, String username) throws IOException { HttpUriRequest request = new HttpGet(String.format(api, username)); - return HttpClientBuilder - .create() - .build() - .execute(request); + return HttpClientBuilder.create().build().execute(request); } } diff --git a/libraries/src/test/java/com/baeldung/serenity/membership/MemberStatusSteps.java b/libraries/src/test/java/com/baeldung/serenity/membership/MemberStatusSteps.java index 49ed8cae7d..a398c614c4 100644 --- a/libraries/src/test/java/com/baeldung/serenity/membership/MemberStatusSteps.java +++ b/libraries/src/test/java/com/baeldung/serenity/membership/MemberStatusSteps.java @@ -33,7 +33,7 @@ public class MemberStatusSteps { @Pending @Step("When the member exchange {}") public void aMemberExchangeA(Commodity commodity) { - //TODO + // TODO } @Pending diff --git a/libraries/src/test/java/com/baeldung/serenity/pageobjects/GoogleSearchPageObject.java b/libraries/src/test/java/com/baeldung/serenity/pageobjects/GoogleSearchPageObject.java index bdba8a69bc..d922ea8c85 100644 --- a/libraries/src/test/java/com/baeldung/serenity/pageobjects/GoogleSearchPageObject.java +++ b/libraries/src/test/java/com/baeldung/serenity/pageobjects/GoogleSearchPageObject.java @@ -24,9 +24,7 @@ public class GoogleSearchPageObject extends PageObject { } public void resultMatches(String expected) { - withTimeoutOf(5, SECONDS) - .waitFor(result) - .waitUntilVisible(); + withTimeoutOf(5, SECONDS).waitFor(result).waitUntilVisible(); assertThat(result.getText(), containsString(expected)); } diff --git a/libraries/src/test/java/com/baeldung/serenity/screenplay/GoogleSearchPage.java b/libraries/src/test/java/com/baeldung/serenity/screenplay/GoogleSearchPage.java index b60c929c05..5663484a5b 100644 --- a/libraries/src/test/java/com/baeldung/serenity/screenplay/GoogleSearchPage.java +++ b/libraries/src/test/java/com/baeldung/serenity/screenplay/GoogleSearchPage.java @@ -10,12 +10,8 @@ import net.thucydides.core.annotations.DefaultUrl; @DefaultUrl("https://www.google.com/ncr") class GoogleSearchPage extends PageObject { - static final Target SEARCH_RESULT_TITLES = Target - .the("search results") - .locatedBy("._ksh"); + static final Target SEARCH_RESULT_TITLES = Target.the("search results").locatedBy("._ksh"); - static final Target SEARCH_INPUT_BOX = Target - .the("search input box") - .locatedBy("#lst-ib"); + static final Target SEARCH_INPUT_BOX = Target.the("search input box").locatedBy("#lst-ib"); } diff --git a/libraries/src/test/java/com/baeldung/serenity/screenplay/GoogleSearchResults.java b/libraries/src/test/java/com/baeldung/serenity/screenplay/GoogleSearchResults.java index 38990e13b6..67a27e5855 100644 --- a/libraries/src/test/java/com/baeldung/serenity/screenplay/GoogleSearchResults.java +++ b/libraries/src/test/java/com/baeldung/serenity/screenplay/GoogleSearchResults.java @@ -13,9 +13,6 @@ public class GoogleSearchResults implements Question> { } public List answeredBy(Actor actor) { - return Text - .of(GoogleSearchPage.SEARCH_RESULT_TITLES) - .viewedBy(actor) - .asList(); + return Text.of(GoogleSearchPage.SEARCH_RESULT_TITLES).viewedBy(actor).asList(); } } diff --git a/libraries/src/test/java/com/baeldung/serenity/screenplay/SearchForKeyword.java b/libraries/src/test/java/com/baeldung/serenity/screenplay/SearchForKeyword.java index 1628ef8ed7..2464c439cf 100644 --- a/libraries/src/test/java/com/baeldung/serenity/screenplay/SearchForKeyword.java +++ b/libraries/src/test/java/com/baeldung/serenity/screenplay/SearchForKeyword.java @@ -11,10 +11,7 @@ public class SearchForKeyword implements Task { @Step("{0} searches for '#keyword'") public void performAs(T actor) { - actor.attemptsTo(Enter - .theValue(keyword) - .into(GoogleSearchPage.SEARCH_INPUT_BOX) - .thenHit(Keys.RETURN)); + actor.attemptsTo(Enter.theValue(keyword).into(GoogleSearchPage.SEARCH_INPUT_BOX).thenHit(Keys.RETURN)); } private String keyword; @@ -24,9 +21,7 @@ public class SearchForKeyword implements Task { } public static Task of(String keyword) { - return Instrumented - .instanceOf(SearchForKeyword.class) - .withProperties(keyword); + return Instrumented.instanceOf(SearchForKeyword.class).withProperties(keyword); } } diff --git a/libraries/src/test/java/com/baeldung/serenity/screenplay/StartWith.java b/libraries/src/test/java/com/baeldung/serenity/screenplay/StartWith.java index 52c6d07fda..d6e45beb26 100644 --- a/libraries/src/test/java/com/baeldung/serenity/screenplay/StartWith.java +++ b/libraries/src/test/java/com/baeldung/serenity/screenplay/StartWith.java @@ -17,9 +17,7 @@ public class StartWith implements Task { @Step("{0} starts a google search") public void performAs(T t) { - t.attemptsTo(Open - .browserOn() - .the(googleSearchPage)); + t.attemptsTo(Open.browserOn().the(googleSearchPage)); } } diff --git a/libraries/src/test/java/com/baeldung/serenity/spring/AdderClassDirtiesContextIntegrationTest.java b/libraries/src/test/java/com/baeldung/serenity/spring/AdderClassDirtiesContextIntegrationTest.java index 82dbad0f11..07b60df264 100644 --- a/libraries/src/test/java/com/baeldung/serenity/spring/AdderClassDirtiesContextIntegrationTest.java +++ b/libraries/src/test/java/com/baeldung/serenity/spring/AdderClassDirtiesContextIntegrationTest.java @@ -15,9 +15,7 @@ import static com.baeldung.serenity.spring.RandomNumberUtil.randomInt; import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS; @RunWith(Suite.class) -@Suite.SuiteClasses({ - AdderClassDirtiesContextIntegrationTest.DirtiesContextTest.class, AdderClassDirtiesContextIntegrationTest.AnotherDirtiesContextTest.class -}) +@Suite.SuiteClasses({ AdderClassDirtiesContextIntegrationTest.DirtiesContextIntegrationTest.class, AdderClassDirtiesContextIntegrationTest.AnotherDirtiesContextIntegrationTest.class }) public class AdderClassDirtiesContextIntegrationTest { @RunWith(SerenityRunner.class) @@ -48,11 +46,11 @@ public class AdderClassDirtiesContextIntegrationTest { } @DirtiesContext(classMode = AFTER_CLASS) - public static class AnotherDirtiesContextTest extends Base { + public static class AnotherDirtiesContextIntegrationTest extends Base { @Test public void givenNumber_whenAdd_thenSumWrong() { - super.whenAdd_thenSummedUp(); //expecting zero + super.whenAdd_thenSummedUp(); // expecting zero adderServiceSteps.givenBaseAndAdder(randomInt(), randomInt()); super.whenAccumulate_thenSummedUp(); super.whenAdd_thenSumWrong(); @@ -60,11 +58,11 @@ public class AdderClassDirtiesContextIntegrationTest { } @DirtiesContext(classMode = AFTER_CLASS) - public static class DirtiesContextTest extends Base { + public static class DirtiesContextIntegrationTest extends Base { @Test public void givenNumber_whenAdd_thenSumWrong() { - super.whenAdd_thenSummedUp(); //expecting zero + super.whenAdd_thenSummedUp(); // expecting zero adderServiceSteps.givenBaseAndAdder(randomInt(), randomInt()); super.whenAccumulate_thenSummedUp(); super.whenAdd_thenSumWrong(); diff --git a/libraries/src/test/java/com/baeldung/serenity/spring/AdderTest.java b/libraries/src/test/java/com/baeldung/serenity/spring/AdderIntegrationTest.java similarity index 78% rename from libraries/src/test/java/com/baeldung/serenity/spring/AdderTest.java rename to libraries/src/test/java/com/baeldung/serenity/spring/AdderIntegrationTest.java index 771f389cb1..b30496462e 100644 --- a/libraries/src/test/java/com/baeldung/serenity/spring/AdderTest.java +++ b/libraries/src/test/java/com/baeldung/serenity/spring/AdderIntegrationTest.java @@ -6,8 +6,8 @@ import org.jbehave.core.annotations.BeforeStory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; -@ContextConfiguration(classes = {AdderController.class, AdderService.class}) -public class AdderTest extends SerenityStory { +@ContextConfiguration(classes = { AdderController.class, AdderService.class }) +public class AdderIntegrationTest extends SerenityStory { @Autowired private AdderService adderService; diff --git a/libraries/src/test/java/com/baeldung/serenity/spring/AdderMethodDirtiesContextDependencyWorkaroundIntegrationTest.java b/libraries/src/test/java/com/baeldung/serenity/spring/AdderMethodDirtiesContextDependencyWorkaroundIntegrationTest.java index 6524ade190..ecb601b94b 100644 --- a/libraries/src/test/java/com/baeldung/serenity/spring/AdderMethodDirtiesContextDependencyWorkaroundIntegrationTest.java +++ b/libraries/src/test/java/com/baeldung/serenity/spring/AdderMethodDirtiesContextDependencyWorkaroundIntegrationTest.java @@ -25,7 +25,8 @@ public class AdderMethodDirtiesContextDependencyWorkaroundIntegrationTest { private AdderConstructorDependencySteps adderSteps; - @Autowired private AdderService adderService; + @Autowired + private AdderService adderService; @Before public void init() { @@ -38,7 +39,8 @@ public class AdderMethodDirtiesContextDependencyWorkaroundIntegrationTest { adderSteps.summedUp(); } - @Rule public SpringIntegrationMethodRule springIntegration = new SpringIntegrationMethodRule(); + @Rule + public SpringIntegrationMethodRule springIntegration = new SpringIntegrationMethodRule(); @DirtiesContext @Test diff --git a/libraries/src/test/java/com/baeldung/serenity/spring/AdderMethodDirtiesContextInitWorkaroundIntegrationTest.java b/libraries/src/test/java/com/baeldung/serenity/spring/AdderMethodDirtiesContextInitWorkaroundIntegrationTest.java index 87c66f03d9..7221aa7a17 100644 --- a/libraries/src/test/java/com/baeldung/serenity/spring/AdderMethodDirtiesContextInitWorkaroundIntegrationTest.java +++ b/libraries/src/test/java/com/baeldung/serenity/spring/AdderMethodDirtiesContextInitWorkaroundIntegrationTest.java @@ -23,7 +23,8 @@ import static com.baeldung.serenity.spring.RandomNumberUtil.randomInt; @ContextConfiguration(classes = AdderService.class) public class AdderMethodDirtiesContextInitWorkaroundIntegrationTest { - @Steps private AdderServiceSteps adderServiceSteps; + @Steps + private AdderServiceSteps adderServiceSteps; @Before public void init() { @@ -36,7 +37,8 @@ public class AdderMethodDirtiesContextInitWorkaroundIntegrationTest { adderServiceSteps.summedUp(); } - @Rule public SpringIntegrationMethodRule springIntegration = new SpringIntegrationMethodRule(); + @Rule + public SpringIntegrationMethodRule springIntegration = new SpringIntegrationMethodRule(); @DirtiesContext @Test diff --git a/libraries/src/test/java/com/baeldung/serenity/spring/AdderMethodDirtiesContextIntegrationTest.java b/libraries/src/test/java/com/baeldung/serenity/spring/AdderMethodDirtiesContextIntegrationTest.java index 263ffc9854..fc7067520d 100644 --- a/libraries/src/test/java/com/baeldung/serenity/spring/AdderMethodDirtiesContextIntegrationTest.java +++ b/libraries/src/test/java/com/baeldung/serenity/spring/AdderMethodDirtiesContextIntegrationTest.java @@ -22,7 +22,8 @@ import static com.baeldung.serenity.spring.RandomNumberUtil.randomInt; @ContextConfiguration(classes = AdderService.class) public class AdderMethodDirtiesContextIntegrationTest { - @Steps private AdderServiceSteps adderServiceSteps; + @Steps + private AdderServiceSteps adderServiceSteps; @Test public void _1_givenNumber_whenAdd_thenSumWrong() { @@ -30,7 +31,8 @@ public class AdderMethodDirtiesContextIntegrationTest { adderServiceSteps.sumWrong(); } - @Rule public SpringIntegrationMethodRule springIntegration = new SpringIntegrationMethodRule(); + @Rule + public SpringIntegrationMethodRule springIntegration = new SpringIntegrationMethodRule(); @DirtiesContext @Test diff --git a/libraries/src/test/java/com/baeldung/serenity/spring/AdderMethodRuleIntegrationTest.java b/libraries/src/test/java/com/baeldung/serenity/spring/AdderMethodRuleIntegrationTest.java index bbf07a2b95..1b3668b756 100644 --- a/libraries/src/test/java/com/baeldung/serenity/spring/AdderMethodRuleIntegrationTest.java +++ b/libraries/src/test/java/com/baeldung/serenity/spring/AdderMethodRuleIntegrationTest.java @@ -41,11 +41,14 @@ public class AdderMethodRuleIntegrationTest { LOG.info("adder after test: {}", adder); } - @Rule public SpringIntegrationMethodRule springMethodIntegration = new SpringIntegrationMethodRule(); + @Rule + public SpringIntegrationMethodRule springMethodIntegration = new SpringIntegrationMethodRule(); - @Steps private AdderSteps adderSteps; + @Steps + private AdderSteps adderSteps; - @Value("#{props['adder']}") private int adder; + @Value("#{props['adder']}") + private int adder; private static int staticAdder; diff --git a/libraries/src/test/java/com/baeldung/serenity/spring/AdderMockMvcIntegrationTest.java b/libraries/src/test/java/com/baeldung/serenity/spring/AdderMockMvcIntegrationTest.java index 2b2777f0ed..d4cf9fc924 100644 --- a/libraries/src/test/java/com/baeldung/serenity/spring/AdderMockMvcIntegrationTest.java +++ b/libraries/src/test/java/com/baeldung/serenity/spring/AdderMockMvcIntegrationTest.java @@ -21,7 +21,8 @@ public class AdderMockMvcIntegrationTest { RestAssuredMockMvc.standaloneSetup(new PlainAdderController()); } - @Steps AdderRestSteps steps; + @Steps + AdderRestSteps steps; @Test public void givenNumber_whenAdd_thenSummedUp() throws Exception { diff --git a/libraries/src/test/java/com/baeldung/serenity/spring/AdderServiceIntegrationTest.java b/libraries/src/test/java/com/baeldung/serenity/spring/AdderServiceIntegrationTest.java index 5f2aae8e3f..8b307973ba 100644 --- a/libraries/src/test/java/com/baeldung/serenity/spring/AdderServiceIntegrationTest.java +++ b/libraries/src/test/java/com/baeldung/serenity/spring/AdderServiceIntegrationTest.java @@ -14,7 +14,8 @@ import static com.baeldung.serenity.spring.RandomNumberUtil.randomInt; @RunWith(SerenityRunner.class) public class AdderServiceIntegrationTest { - @Steps private AdderServiceSteps adderServiceSteps; + @Steps + private AdderServiceSteps adderServiceSteps; @Test public void givenNumber_whenAdd_thenSummedUp() { diff --git a/libraries/src/test/java/com/baeldung/serenity/spring/AdderSpringSerenityRunnerIntegrationTest.java b/libraries/src/test/java/com/baeldung/serenity/spring/AdderSpringSerenityRunnerIntegrationTest.java index cdabc17980..ddbfd0cfc2 100644 --- a/libraries/src/test/java/com/baeldung/serenity/spring/AdderSpringSerenityRunnerIntegrationTest.java +++ b/libraries/src/test/java/com/baeldung/serenity/spring/AdderSpringSerenityRunnerIntegrationTest.java @@ -15,9 +15,11 @@ import org.springframework.test.context.ContextConfiguration; @ContextConfiguration(locations = "classpath:adder-beans.xml") public class AdderSpringSerenityRunnerIntegrationTest { - @Steps private AdderSteps adderSteps; + @Steps + private AdderSteps adderSteps; - @Value("#{props['adder']}") private int adder; + @Value("#{props['adder']}") + private int adder; @Test public void givenNumber_whenAdd_thenSummedUp() { diff --git a/libraries/src/test/java/com/baeldung/serenity/spring/steps/AdderRestSteps.java b/libraries/src/test/java/com/baeldung/serenity/spring/steps/AdderRestSteps.java index 0d77ed0849..a38a584645 100644 --- a/libraries/src/test/java/com/baeldung/serenity/spring/steps/AdderRestSteps.java +++ b/libraries/src/test/java/com/baeldung/serenity/spring/steps/AdderRestSteps.java @@ -18,29 +18,18 @@ public class AdderRestSteps { @Step("get the current number") public void givenCurrentNumber() throws UnsupportedEncodingException { - currentNum = Integer.valueOf(given() - .when() - .get("/adder/current") - .mvcResult() - .getResponse() - .getContentAsString()); + currentNum = Integer.valueOf(given().when().get("/adder/current").mvcResult().getResponse().getContentAsString()); } @Step("adding {0}") public void whenAddNumber(int num) { - mockMvcResponse = given() - .queryParam("num", num) - .when() - .post("/adder"); + mockMvcResponse = given().queryParam("num", num).when().post("/adder"); currentNum += num; } @Step("got the sum") public void thenSummedUp() { - mockMvcResponse - .then() - .statusCode(200) - .body(equalTo(currentNum + "")); + mockMvcResponse.then().statusCode(200).body(equalTo(currentNum + "")); } } diff --git a/libraries/src/test/java/com/baeldung/serenity/spring/steps/AdderServiceSteps.java b/libraries/src/test/java/com/baeldung/serenity/spring/steps/AdderServiceSteps.java index b8c2854bf0..227526b8b4 100644 --- a/libraries/src/test/java/com/baeldung/serenity/spring/steps/AdderServiceSteps.java +++ b/libraries/src/test/java/com/baeldung/serenity/spring/steps/AdderServiceSteps.java @@ -13,7 +13,8 @@ import static org.junit.Assert.assertNotEquals; @ContextConfiguration(classes = AdderService.class) public class AdderServiceSteps { - @Autowired private AdderService adderService; + @Autowired + private AdderService adderService; private int givenNumber; private int base; diff --git a/libraries/src/test/java/com/baeldung/serenity/spring/stories/AdderStory.java b/libraries/src/test/java/com/baeldung/serenity/spring/stories/AdderStory.java index b9fa8f1ae0..491c11a38c 100644 --- a/libraries/src/test/java/com/baeldung/serenity/spring/stories/AdderStory.java +++ b/libraries/src/test/java/com/baeldung/serenity/spring/stories/AdderStory.java @@ -11,7 +11,8 @@ import org.jbehave.core.annotations.When; */ public class AdderStory { - @Steps AdderRestSteps restSteps; + @Steps + AdderRestSteps restSteps; @Given("a number") public void givenANumber() throws Exception { diff --git a/libraries/src/test/java/com/baeldung/smooks/converter/SmooksIntegrationTest.java b/libraries/src/test/java/com/baeldung/smooks/converter/SmooksIntegrationTest.java index 4d2cb71329..69af042427 100644 --- a/libraries/src/test/java/com/baeldung/smooks/converter/SmooksIntegrationTest.java +++ b/libraries/src/test/java/com/baeldung/smooks/converter/SmooksIntegrationTest.java @@ -10,22 +10,11 @@ import java.text.SimpleDateFormat; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; - public class SmooksIntegrationTest { - private static final String EDIFACT_MESSAGE = - "UNA:+.? '\r\n" + - "UNH+771+IN_PROGRESS+2018-01-14'\r\n" + - "CTA+CompanyX+1234567'\r\n" + - "LIN+1+PX1234+9.99'\r\n" + - "LIN+2+RX990+120.32'\r\n"; - private static final String EMAIL_MESSAGE = - "Hi,\r\n" + - "Order number #771 created on 2018-01-14 is currently in IN_PROGRESS status.\r\n" + - "Consider contact supplier \"CompanyX\" with phone number: \"1234567\".\r\n" + - "Order items:\r\n" + - "1 X PX1234 (total price 9.99)\r\n" + - "2 X RX990 (total price 240.64)\r\n"; + private static final String EDIFACT_MESSAGE = "UNA:+.? '\r\n" + "UNH+771+IN_PROGRESS+2018-01-14'\r\n" + "CTA+CompanyX+1234567'\r\n" + "LIN+1+PX1234+9.99'\r\n" + "LIN+2+RX990+120.32'\r\n"; + private static final String EMAIL_MESSAGE = "Hi,\r\n" + "Order number #771 created on 2018-01-14 is currently in IN_PROGRESS status.\r\n" + "Consider contact supplier \"CompanyX\" with phone number: \"1234567\".\r\n" + "Order items:\r\n" + + "1 X PX1234 (total price 9.99)\r\n" + "2 X RX990 (total price 240.64)\r\n"; @Test public void givenOrderXML_whenConvert_thenPOJOsConstructedCorrectly() throws Exception { @@ -33,14 +22,11 @@ public class SmooksIntegrationTest { OrderConverter xmlToJavaOrderConverter = new OrderConverter(); Order order = xmlToJavaOrderConverter.convertOrderXMLToOrderObject("/smooks/order.xml"); - assertThat(order.getNumber(),is(771L)); - assertThat(order.getStatus(),is(Status.IN_PROGRESS)); - assertThat(order.getCreationDate(),is(new SimpleDateFormat("yyyy-MM-dd").parse("2018-01-14"))); - assertThat(order.getSupplier(),is(new Supplier("CompanyX","1234567"))); - assertThat(order.getItems(),containsInAnyOrder( - new Item("PX1234",9.99,1), - new Item("RX990",120.32,2)) - ); + assertThat(order.getNumber(), is(771L)); + assertThat(order.getStatus(), is(Status.IN_PROGRESS)); + assertThat(order.getCreationDate(), is(new SimpleDateFormat("yyyy-MM-dd").parse("2018-01-14"))); + assertThat(order.getSupplier(), is(new Supplier("CompanyX", "1234567"))); + assertThat(order.getItems(), containsInAnyOrder(new Item("PX1234", 9.99, 1), new Item("RX990", 120.32, 2))); } @@ -51,20 +37,20 @@ public class SmooksIntegrationTest { assertThat(validationResult.getErrors(), hasSize(1)); // 1234567 didn't match ^[0-9\\-\\+]{9,15}$ - assertThat(validationResult.getErrors().get(0).getFailRuleResult().getRuleName(),is("supplierPhone")); + assertThat(validationResult.getErrors().get(0).getFailRuleResult().getRuleName(), is("supplierPhone")); } @Test public void givenOrderXML_whenApplyEDITemplate_thenConvertedToEDIFACT() throws Exception { OrderConverter orderConverter = new OrderConverter(); String edifact = orderConverter.convertOrderXMLtoEDIFACT("/smooks/order.xml"); - assertThat(edifact,is(EDIFACT_MESSAGE)); + assertThat(edifact, is(EDIFACT_MESSAGE)); } @Test public void givenOrderXML_whenApplyEmailTemplate_thenConvertedToEmailMessage() throws Exception { OrderConverter orderConverter = new OrderConverter(); String emailMessage = orderConverter.convertOrderXMLtoEmailMessage("/smooks/order.xml"); - assertThat(emailMessage,is(EMAIL_MESSAGE)); + assertThat(emailMessage, is(EMAIL_MESSAGE)); } } \ No newline at end of file diff --git a/libraries/src/test/java/com/baeldung/stm/AccountTest.java b/libraries/src/test/java/com/baeldung/stm/AccountTest.java index be3edf058c..eb8693b096 100644 --- a/libraries/src/test/java/com/baeldung/stm/AccountTest.java +++ b/libraries/src/test/java/com/baeldung/stm/AccountTest.java @@ -1,6 +1,5 @@ package com.baeldung.stm; - import org.junit.Test; import java.util.concurrent.CountDownLatch; @@ -16,34 +15,34 @@ public class AccountTest { @Test public void givenAccount_whenDecrement_thenShouldReturnProperValue() { - //given + // given Account a = new Account(10); - //when + // when a.adjustBy(-5); - //then + // then assertThat(a.getBalance()).isEqualTo(5); } @Test(expected = IllegalArgumentException.class) public void givenAccount_whenDecrementTooMuch_thenShouldThrow() { - //given + // given Account a = new Account(10); - //when + // when a.adjustBy(-11); } @Test public void givenTwoThreads_whenBothApplyOperation_thenShouldThrow() throws InterruptedException { - //given + // given ExecutorService ex = Executors.newFixedThreadPool(2); Account a = new Account(10); CountDownLatch countDownLatch = new CountDownLatch(1); AtomicBoolean exceptionThrown = new AtomicBoolean(false); - //when + // when ex.submit(() -> { try { countDownLatch.await(); @@ -74,44 +73,44 @@ public class AccountTest { ex.awaitTermination(1, TimeUnit.SECONDS); ex.shutdown(); - //then + // then assertTrue(exceptionThrown.get()); } @Test public void givenTwoAccounts_whenFailedWhileTransferring_thenShouldRollbackTransaction() { - //given + // given final Account a = new Account(10); final Account b = new Account(10); - //when + // when a.transferTo(b, 5); - //then + // then assertThat(a.getBalance()).isEqualTo(5); assertThat(b.getBalance()).isEqualTo(15); - //and + // and try { a.transferTo(b, 20); } catch (final IllegalArgumentException e) { System.out.println("failed to transfer money"); } - //then + // then assertThat(a.getBalance()).isEqualTo(5); assertThat(b.getBalance()).isEqualTo(15); } @Test public void givenTwoThreads_whenBothTryToTransfer_thenShouldNotDeadlock() throws InterruptedException { - //given + // given ExecutorService ex = Executors.newFixedThreadPool(2); final Account a = new Account(10); final Account b = new Account(10); CountDownLatch countDownLatch = new CountDownLatch(1); - //when + // when ex.submit(() -> { try { countDownLatch.await(); @@ -134,10 +133,9 @@ public class AccountTest { ex.awaitTermination(1, TimeUnit.SECONDS); ex.shutdown(); - //then + // then assertThat(a.getBalance()).isEqualTo(1); assertThat(b.getBalance()).isEqualTo(19); } - } \ No newline at end of file diff --git a/libraries/src/test/java/com/baeldung/stream/JoolMergeStreamsTest.java b/libraries/src/test/java/com/baeldung/stream/JoolMergeStreamsTest.java index e14a91ce67..4cda0b5940 100644 --- a/libraries/src/test/java/com/baeldung/stream/JoolMergeStreamsTest.java +++ b/libraries/src/test/java/com/baeldung/stream/JoolMergeStreamsTest.java @@ -16,11 +16,9 @@ public class JoolMergeStreamsTest { Stream seq1 = Stream.of(1, 3, 5); Stream seq2 = Stream.of(2, 4, 6); - Stream resultingSeq = Seq.ofType(seq1, Integer.class) - .append(seq2); + Stream resultingSeq = Seq.ofType(seq1, Integer.class).append(seq2); - assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6), - resultingSeq.collect(Collectors.toList())); + assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6), resultingSeq.collect(Collectors.toList())); } @Test @@ -29,11 +27,8 @@ public class JoolMergeStreamsTest { Stream openingBracketSeq = Stream.of("["); Stream closingBracketSeq = Stream.of("]"); - Stream resultingStream = Seq.ofType(seq, String.class) - .append(closingBracketSeq) - .prepend(openingBracketSeq); + Stream resultingStream = Seq.ofType(seq, String.class).append(closingBracketSeq).prepend(openingBracketSeq); - Assert.assertEquals(Arrays.asList("[", "foo", "bar", "]"), - resultingStream.collect(Collectors.toList())); + Assert.assertEquals(Arrays.asList("[", "foo", "bar", "]"), resultingStream.collect(Collectors.toList())); } } \ No newline at end of file diff --git a/libraries/src/test/java/com/baeldung/stream/MergeStreamsTest.java b/libraries/src/test/java/com/baeldung/stream/MergeStreamsTest.java index 23110947b6..b8748abe03 100644 --- a/libraries/src/test/java/com/baeldung/stream/MergeStreamsTest.java +++ b/libraries/src/test/java/com/baeldung/stream/MergeStreamsTest.java @@ -16,11 +16,9 @@ public class MergeStreamsTest { Stream stream1 = Stream.of(1, 3, 5); Stream stream2 = Stream.of(2, 4, 6); - Stream resultingStream = Stream.concat(stream1, - stream2); + Stream resultingStream = Stream.concat(stream1, stream2); - assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6), - resultingStream.collect(Collectors.toList())); + assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6), resultingStream.collect(Collectors.toList())); } @Test @@ -29,12 +27,9 @@ public class MergeStreamsTest { Stream stream2 = Stream.of(2, 4, 6); Stream stream3 = Stream.of(18, 15, 36); - Stream resultingStream = Stream.concat(Stream.concat(stream1, - stream2), - stream3); + Stream resultingStream = Stream.concat(Stream.concat(stream1, stream2), stream3); - assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6, 18, 15, 36), - resultingStream.collect(Collectors.toList())); + assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6, 18, 15, 36), resultingStream.collect(Collectors.toList())); } @Test @@ -46,8 +41,7 @@ public class MergeStreamsTest { Stream resultingStream = Stream.of(stream1, stream2, stream3, stream4).flatMap(Function.identity()); - assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6, 18, 15, 36, 99), - resultingStream.collect(Collectors.toList())); + assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6, 18, 15, 36, 99), resultingStream.collect(Collectors.toList())); } } \ No newline at end of file diff --git a/libraries/src/test/java/com/baeldung/stream/StreamExMergeStreamsTest.java b/libraries/src/test/java/com/baeldung/stream/StreamExMergeStreamsTest.java index 5104ec0682..e5392dff2a 100644 --- a/libraries/src/test/java/com/baeldung/stream/StreamExMergeStreamsTest.java +++ b/libraries/src/test/java/com/baeldung/stream/StreamExMergeStreamsTest.java @@ -18,8 +18,7 @@ public class StreamExMergeStreamsTest { Stream resultingStream = StreamEx.of(stream1).append(stream2); - assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6), - resultingStream.collect(Collectors.toList())); + assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6), resultingStream.collect(Collectors.toList())); } @Test @@ -29,13 +28,9 @@ public class StreamExMergeStreamsTest { Stream stream3 = Stream.of(18, 15, 36); Stream stream4 = Stream.of(99); - Stream resultingStream = StreamEx.of(stream1) - .append(stream2) - .append(stream3) - .append(stream4); + Stream resultingStream = StreamEx.of(stream1).append(stream2).append(stream3).append(stream4); - assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6, 18, 15, 36, 99), - resultingStream.collect(Collectors.toList())); + assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6, 18, 15, 36, 99), resultingStream.collect(Collectors.toList())); } @@ -45,11 +40,8 @@ public class StreamExMergeStreamsTest { Stream openingBracketStream = Stream.of("["); Stream closingBracketStream = Stream.of("]"); - Stream resultingStream = StreamEx.of(stream1) - .append(closingBracketStream) - .prepend(openingBracketStream); + Stream resultingStream = StreamEx.of(stream1).append(closingBracketStream).prepend(openingBracketStream); - assertEquals(Arrays.asList("[", "foo", "bar", "]"), - resultingStream.collect(Collectors.toList())); + assertEquals(Arrays.asList("[", "foo", "bar", "]"), resultingStream.collect(Collectors.toList())); } } diff --git a/libraries/src/test/java/com/baeldung/streamutils/CopyStreamTest.java b/libraries/src/test/java/com/baeldung/streamutils/CopyStreamTest.java index 9a65075e5b..3ed797ccaa 100644 --- a/libraries/src/test/java/com/baeldung/streamutils/CopyStreamTest.java +++ b/libraries/src/test/java/com/baeldung/streamutils/CopyStreamTest.java @@ -18,83 +18,83 @@ import static com.baeldung.streamutils.CopyStream.getStringFromInputStream; public class CopyStreamTest { - @Test - public void whenCopyInputStreamToOutputStream_thenCorrect() throws IOException { - String inputFileName = "src/test/resources/input.txt"; - String outputFileName = "src/test/resources/output.txt"; - File outputFile = new File(outputFileName); - InputStream in = new FileInputStream(inputFileName); - OutputStream out = new FileOutputStream(outputFileName); + @Test + public void whenCopyInputStreamToOutputStream_thenCorrect() throws IOException { + String inputFileName = "src/test/resources/input.txt"; + String outputFileName = "src/test/resources/output.txt"; + File outputFile = new File(outputFileName); + InputStream in = new FileInputStream(inputFileName); + OutputStream out = new FileOutputStream(outputFileName); - StreamUtils.copy(in, out); + StreamUtils.copy(in, out); - assertTrue(outputFile.exists()); - String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName)); - String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName)); - Assert.assertEquals(inputFileContent, outputFileContent); - } + assertTrue(outputFile.exists()); + String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName)); + String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName)); + Assert.assertEquals(inputFileContent, outputFileContent); + } - @Test - public void whenCopyRangeOfInputStreamToOutputStream_thenCorrect() throws IOException { - String inputFileName = "src/test/resources/input.txt"; - String outputFileName = "src/test/resources/output.txt"; - File outputFile = new File(outputFileName); - InputStream in = new FileInputStream(inputFileName); - OutputStream out = new FileOutputStream(outputFileName); + @Test + public void whenCopyRangeOfInputStreamToOutputStream_thenCorrect() throws IOException { + String inputFileName = "src/test/resources/input.txt"; + String outputFileName = "src/test/resources/output.txt"; + File outputFile = new File(outputFileName); + InputStream in = new FileInputStream(inputFileName); + OutputStream out = new FileOutputStream(outputFileName); - StreamUtils.copyRange(in, out, 1, 10); + StreamUtils.copyRange(in, out, 1, 10); - assertTrue(outputFile.exists()); - String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName)); - String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName)); - Assert.assertEquals(inputFileContent.substring(1, 11), outputFileContent); - } + assertTrue(outputFile.exists()); + String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName)); + String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName)); + Assert.assertEquals(inputFileContent.substring(1, 11), outputFileContent); + } - @Test - public void whenCopyStringToOutputStream_thenCorrect() throws IOException { - String string = "Should be copied to OutputStream."; - String outputFileName = "src/test/resources/output.txt"; - File outputFile = new File(outputFileName); - OutputStream out = new FileOutputStream("src/test/resources/output.txt"); + @Test + public void whenCopyStringToOutputStream_thenCorrect() throws IOException { + String string = "Should be copied to OutputStream."; + String outputFileName = "src/test/resources/output.txt"; + File outputFile = new File(outputFileName); + OutputStream out = new FileOutputStream("src/test/resources/output.txt"); - StreamUtils.copy(string, StandardCharsets.UTF_8, out); + StreamUtils.copy(string, StandardCharsets.UTF_8, out); - assertTrue(outputFile.exists()); - String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName)); - Assert.assertEquals(outputFileContent, string); - } + assertTrue(outputFile.exists()); + String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName)); + Assert.assertEquals(outputFileContent, string); + } - @Test - public void whenCopyInputStreamToString_thenCorrect() throws IOException { - String inputFileName = "src/test/resources/input.txt"; - InputStream is = new FileInputStream(inputFileName); - String content = StreamUtils.copyToString(is, StandardCharsets.UTF_8); + @Test + public void whenCopyInputStreamToString_thenCorrect() throws IOException { + String inputFileName = "src/test/resources/input.txt"; + InputStream is = new FileInputStream(inputFileName); + String content = StreamUtils.copyToString(is, StandardCharsets.UTF_8); - String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName)); - Assert.assertEquals(inputFileContent, content); - } + String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName)); + Assert.assertEquals(inputFileContent, content); + } - @Test - public void whenCopyByteArrayToOutputStream_thenCorrect() throws IOException { - String outputFileName = "src/test/resources/output.txt"; - String string = "Should be copied to OutputStream."; - byte[] byteArray = string.getBytes(); - OutputStream out = new FileOutputStream("src/test/resources/output.txt"); + @Test + public void whenCopyByteArrayToOutputStream_thenCorrect() throws IOException { + String outputFileName = "src/test/resources/output.txt"; + String string = "Should be copied to OutputStream."; + byte[] byteArray = string.getBytes(); + OutputStream out = new FileOutputStream("src/test/resources/output.txt"); - StreamUtils.copy(byteArray, out); - String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName)); - Assert.assertEquals(outputFileContent, string); - } + StreamUtils.copy(byteArray, out); + String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName)); + Assert.assertEquals(outputFileContent, string); + } - @Test - public void whenCopyInputStreamToByteArray_thenCorrect() throws IOException { - String inputFileName = "src/test/resources/input.txt"; - InputStream in = new FileInputStream(inputFileName); - byte[] out = StreamUtils.copyToByteArray(in); + @Test + public void whenCopyInputStreamToByteArray_thenCorrect() throws IOException { + String inputFileName = "src/test/resources/input.txt"; + InputStream in = new FileInputStream(inputFileName); + byte[] out = StreamUtils.copyToByteArray(in); - String content = new String(out); - String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName)); - Assert.assertEquals(inputFileContent, content); - } + String content = new String(out); + String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName)); + Assert.assertEquals(inputFileContent, content); + } } diff --git a/libraries/src/test/java/com/baeldung/text/DiffTest.java b/libraries/src/test/java/com/baeldung/text/DiffTest.java index 95370013b6..932fc96f21 100644 --- a/libraries/src/test/java/com/baeldung/text/DiffTest.java +++ b/libraries/src/test/java/com/baeldung/text/DiffTest.java @@ -6,13 +6,13 @@ import org.junit.Assert; import org.junit.Test; public class DiffTest { - + @Test public void whenEditScript_thenCorrect() { StringsComparator cmp = new StringsComparator("ABCFGH", "BCDEFG"); EditScript script = cmp.getScript(); int mod = script.getModifications(); - + Assert.assertEquals(4, mod); } } diff --git a/libraries/src/test/java/com/baeldung/text/LongestCommonSubsequenceTest.java b/libraries/src/test/java/com/baeldung/text/LongestCommonSubsequenceTest.java index 80ca0cfbba..e0a00afd84 100644 --- a/libraries/src/test/java/com/baeldung/text/LongestCommonSubsequenceTest.java +++ b/libraries/src/test/java/com/baeldung/text/LongestCommonSubsequenceTest.java @@ -11,15 +11,15 @@ public class LongestCommonSubsequenceTest { public void whenCompare_thenCorrect() { LongestCommonSubsequence lcs = new LongestCommonSubsequence(); int countLcs = lcs.apply("New York", "New Hampshire"); - + Assert.assertEquals(5, countLcs); } - + @Test public void whenCalculateDistance_thenCorrect() { LongestCommonSubsequenceDistance lcsd = new LongestCommonSubsequenceDistance(); int countLcsd = lcsd.apply("New York", "New Hampshire"); - + Assert.assertEquals(11, countLcsd); } } diff --git a/libraries/src/test/java/com/baeldung/text/StrBuilderTest.java b/libraries/src/test/java/com/baeldung/text/StrBuilderTest.java index f08b43f69b..4ebf00e1ed 100644 --- a/libraries/src/test/java/com/baeldung/text/StrBuilderTest.java +++ b/libraries/src/test/java/com/baeldung/text/StrBuilderTest.java @@ -13,12 +13,12 @@ public class StrBuilderTest { Assert.assertEquals(new StrBuilder("new StrBuilder!"), strBuilder); } - + @Test public void whenCleared_thenEmpty() { StrBuilder strBuilder = new StrBuilder("example StrBuilder!"); strBuilder.clear(); - + Assert.assertEquals(new StrBuilder(""), strBuilder); } } \ No newline at end of file diff --git a/libraries/src/test/java/com/baeldung/text/WordUtilsTest.java b/libraries/src/test/java/com/baeldung/text/WordUtilsUnitTest.java similarity index 94% rename from libraries/src/test/java/com/baeldung/text/WordUtilsTest.java rename to libraries/src/test/java/com/baeldung/text/WordUtilsUnitTest.java index fafba2fbb5..23712a2a3d 100644 --- a/libraries/src/test/java/com/baeldung/text/WordUtilsTest.java +++ b/libraries/src/test/java/com/baeldung/text/WordUtilsUnitTest.java @@ -4,7 +4,7 @@ import org.apache.commons.text.WordUtils; import org.junit.Assert; import org.junit.Test; -public class WordUtilsTest { +public class WordUtilsUnitTest { @Test public void whenCapitalized_thenCorrect() { diff --git a/libraries/src/test/java/com/baeldung/tomcat/ProgrammaticTomcatIntegrationTest.java b/libraries/src/test/java/com/baeldung/tomcat/ProgrammaticTomcatIntegrationTest.java new file mode 100644 index 0000000000..9224561341 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/tomcat/ProgrammaticTomcatIntegrationTest.java @@ -0,0 +1,55 @@ +package com.baeldung.tomcat; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.BlockJUnit4ClassRunner; + +import static junit.framework.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * Created by adi on 1/14/18. + */ +@RunWith(BlockJUnit4ClassRunner.class) +public class ProgrammaticTomcatIntegrationTest { + + private ProgrammaticTomcat tomcat = new ProgrammaticTomcat(); + + @Before + public void setUp() throws Exception { + tomcat.startTomcat(); + } + + @After + public void tearDown() throws Exception { + tomcat.stopTomcat(); + } + + @Test + public void givenTomcatStarted_whenAccessServlet_responseIsTestAndResponseHeaderIsSet() throws Exception { + CloseableHttpClient httpClient = HttpClientBuilder.create().build(); + HttpGet getServlet = new HttpGet("http://localhost:8080/my-servlet"); + + HttpResponse response = httpClient.execute(getServlet); + assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode()); + + String myHeaderValue = response.getFirstHeader("myHeader").getValue(); + assertEquals("myHeaderValue", myHeaderValue); + + HttpEntity responseEntity = response.getEntity(); + assertNotNull(responseEntity); + + String responseString = EntityUtils.toString(responseEntity, "UTF-8"); + assertEquals("test", responseString); + } + +} diff --git a/libraries/src/test/resources/jetty-embedded-demo-app.war b/libraries/src/test/resources/jetty-embedded-demo-app.war new file mode 100644 index 0000000000..7f8bc37df0 Binary files /dev/null and b/libraries/src/test/resources/jetty-embedded-demo-app.war differ diff --git a/logging-modules/README.md b/logging-modules/README.md index 23458cf30b..6de71adb43 100644 --- a/logging-modules/README.md +++ b/logging-modules/README.md @@ -1,3 +1,7 @@ ## Logging Modules +### Relevant Articles: + +- [Creating a Custom Logback Appender](http://www.baeldung.com/custom-logback-appender) +- [Get Log Output in JSON Format](http://www.baeldung.com/java-log-json-output) diff --git a/logging-modules/log4j/src/main/resources/logback.xml b/logging-modules/log4j/src/main/resources/logback.xml index f567962cb6..097f6694f0 100644 --- a/logging-modules/log4j/src/main/resources/logback.xml +++ b/logging-modules/log4j/src/main/resources/logback.xml @@ -78,7 +78,7 @@ - + diff --git a/logging-modules/log4j2/pom.xml b/logging-modules/log4j2/pom.xml index 48608fbc80..46b8b80597 100644 --- a/logging-modules/log4j2/pom.xml +++ b/logging-modules/log4j2/pom.xml @@ -59,10 +59,10 @@ - 2.8.5 + 2.9.3 1.4.193 2.1.1 - 2.7 + 2.10.0 diff --git a/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/JSONLayoutTest.java b/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/JSONLayoutTest.java new file mode 100644 index 0000000000..9493c32094 --- /dev/null +++ b/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/JSONLayoutTest.java @@ -0,0 +1,45 @@ +package com.baeldung.logging.log4j2.tests; + +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.Before; +import org.junit.Test; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class JSONLayoutTest { + + private static Logger logger; + private ByteArrayOutputStream consoleOutput = new ByteArrayOutputStream(); + private PrintStream ps = new PrintStream(consoleOutput); + + @Before + public void setUp() { + // Redirect console output to our stream + System.setOut(ps); + logger = LogManager.getLogger("CONSOLE_JSON_APPENDER"); + } + + @Test + public void whenLogLayoutInJSON_thenOutputIsCorrectJSON() { + logger.debug("Debug message"); + String currentLog = consoleOutput.toString(); + assertTrue(!currentLog.isEmpty() && isValidJSON(currentLog)); + } + + public static boolean isValidJSON(String jsonInString) { + try { + final ObjectMapper mapper = new ObjectMapper(); + mapper.readTree(jsonInString); + return true; + } catch (IOException e) { + return false; + } + } +} \ No newline at end of file diff --git a/logging-modules/log4j2/src/test/resources/log4j2.xml b/logging-modules/log4j2/src/test/resources/log4j2.xml index 21fd1da446..4dcb7cce5a 100644 --- a/logging-modules/log4j2/src/test/resources/log4j2.xml +++ b/logging-modules/log4j2/src/test/resources/log4j2.xml @@ -1,16 +1,25 @@ - + - + - + - + + + + + + @@ -19,53 +28,58 @@ - + - + - + - + - + - + - + - + - + + + + diff --git a/logging-modules/logback/pom.xml b/logging-modules/logback/pom.xml index 8169134442..cd0d3758cc 100644 --- a/logging-modules/logback/pom.xml +++ b/logging-modules/logback/pom.xml @@ -1,6 +1,6 @@ 4.0.0 @@ -12,6 +12,8 @@ UTF-8 1.2.3 + 0.1.5 + 2.9.3 @@ -28,7 +30,21 @@ logback-classic ${logback.version} - + + ch.qos.logback.contrib + logback-json-classic + ${logback.contrib.version} + + + ch.qos.logback.contrib + logback-jackson + ${logback.contrib.version} + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + diff --git a/logging-modules/logback/src/test/java/com/baeldung/logback/JSONLayoutTest.java b/logging-modules/logback/src/test/java/com/baeldung/logback/JSONLayoutTest.java new file mode 100644 index 0000000000..ca3c4b3457 --- /dev/null +++ b/logging-modules/logback/src/test/java/com/baeldung/logback/JSONLayoutTest.java @@ -0,0 +1,45 @@ +package com.baeldung.logback; + +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; + +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class JSONLayoutTest { + + private static Logger logger; + private ByteArrayOutputStream consoleOutput = new ByteArrayOutputStream(); + private PrintStream ps = new PrintStream(consoleOutput); + + @Before + public void setUp() { + // Redirect console output to our stream + System.setOut(ps); + logger = LoggerFactory.getLogger("jsonLogger"); + } + + @Test + public void whenLogLayoutInJSON_thenOutputIsCorrectJSON() { + logger.debug("Debug message"); + String currentLog = consoleOutput.toString(); + assertTrue(!currentLog.isEmpty() && isValidJSON(currentLog)); + } + + public static boolean isValidJSON(String jsonInString) { + try { + final ObjectMapper mapper = new ObjectMapper(); + mapper.readTree(jsonInString); + return true; + } catch (IOException e) { + return false; + } + } +} diff --git a/logging-modules/logback/src/test/resources/logback-test.xml b/logging-modules/logback/src/test/resources/logback-test.xml index 8254e6ac80..df81f18cc2 100644 --- a/logging-modules/logback/src/test/resources/logback-test.xml +++ b/logging-modules/logback/src/test/resources/logback-test.xml @@ -1,14 +1,31 @@ - + + + test - + + + # JSON appender + + + + true + + yyyy-MM-dd' 'HH:mm:ss.SSS + + + + + + - - + + \ No newline at end of file diff --git a/lucene/README.md b/lucene/README.md new file mode 100644 index 0000000000..b1d33472f4 --- /dev/null +++ b/lucene/README.md @@ -0,0 +1,4 @@ +### Relevant Articles: + +- [Introduction to Apache Lucene](http://www.baeldung.com/lucene) +- [A Simple File Search with Lucene](http://www.baeldung.com/lucene-file-search) diff --git a/mesos-marathon/README.md b/mesos-marathon/README.md index 3223eb2478..1d4d4995a8 100644 --- a/mesos-marathon/README.md +++ b/mesos-marathon/README.md @@ -1,3 +1,5 @@ ### Relevant articles - [Simple Jenkins Pipeline with Marathon and Mesos](http://www.baeldung.com/jenkins-pipeline-with-marathon-mesos) + + To run the pipeline, please modify the dockerise.sh file with your own useranema and password for docker login. diff --git a/microprofile/pom.xml b/microprofile/pom.xml new file mode 100644 index 0000000000..ce8a2d13ca --- /dev/null +++ b/microprofile/pom.xml @@ -0,0 +1,87 @@ + + + 4.0.0 + + com.baeldung + microprofile + 1.0-SNAPSHOT + war + + + UTF-8 + UTF-8 + 1.8 + 1.8 + library + ${project.build.directory}/${app.name}-service.jar + runnable + + + + + org.eclipse.microprofile + microprofile + 1.2 + provided + pom + + + + + + + maven-war-plugin + + false + pom.xml + + + + net.wasdev.wlp.maven.plugins + liberty-maven-plugin + 2.1.2 + + + io.openliberty + openliberty-runtime + 17.0.0.4 + zip + + ${basedir}/src/main/liberty/config/server.xml + ${package.file} + ${packaging.type} + false + project + + / + ${project.artifactId}-${project.version}.war + 9080 + 9443 + + + + + install-server + prepare-package + + install-server + create-server + install-feature + + + + package-server-with-apps + package + + install-apps + package-server + + + + + + + + diff --git a/microprofile/src/main/java/com/baeldung/microprofile/LibraryApplication.java b/microprofile/src/main/java/com/baeldung/microprofile/LibraryApplication.java new file mode 100644 index 0000000000..f5eccf969e --- /dev/null +++ b/microprofile/src/main/java/com/baeldung/microprofile/LibraryApplication.java @@ -0,0 +1,8 @@ +package com.baeldung.microprofile; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; + +@ApplicationPath("/library") +public class LibraryApplication extends Application { +} diff --git a/microprofile/src/main/java/com/baeldung/microprofile/model/Book.java b/microprofile/src/main/java/com/baeldung/microprofile/model/Book.java new file mode 100644 index 0000000000..44b7f5428d --- /dev/null +++ b/microprofile/src/main/java/com/baeldung/microprofile/model/Book.java @@ -0,0 +1,50 @@ +package com.baeldung.microprofile.model; + +public class Book { + + private String id; + private String isbn; + private String name; + private String author; + private Integer pages; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getIsbn() { + return isbn; + } + + public void setIsbn(String isbn) { + this.isbn = isbn; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public Integer getPages() { + return pages; + } + + public void setPages(Integer pages) { + this.pages = pages; + } +} diff --git a/microprofile/src/main/java/com/baeldung/microprofile/providers/BookListMessageBodyWriter.java b/microprofile/src/main/java/com/baeldung/microprofile/providers/BookListMessageBodyWriter.java new file mode 100644 index 0000000000..f7d0bfc5f7 --- /dev/null +++ b/microprofile/src/main/java/com/baeldung/microprofile/providers/BookListMessageBodyWriter.java @@ -0,0 +1,42 @@ +package com.baeldung.microprofile.providers; + +import com.baeldung.microprofile.model.Book; +import com.baeldung.microprofile.util.BookMapper; + +import javax.json.Json; +import javax.json.JsonArray; +import javax.json.JsonWriter; +import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyWriter; +import javax.ws.rs.ext.Provider; +import java.io.IOException; +import java.io.OutputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.util.List; + +@Provider +@Produces(MediaType.APPLICATION_JSON) +public class BookListMessageBodyWriter implements MessageBodyWriter> { + + @Override + public boolean isWriteable(Class clazz, Type genericType, Annotation[] annotations, MediaType mediaType) { + return true; + } + + @Override + public long getSize(List books, Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return 0; + } + + @Override + public void writeTo(List books, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { + JsonWriter jsonWriter = Json.createWriter(entityStream); + JsonArray jsonArray = BookMapper.map(books); + jsonWriter.writeArray(jsonArray); + jsonWriter.close(); + } +} diff --git a/microprofile/src/main/java/com/baeldung/microprofile/providers/BookMessageBodyReader.java b/microprofile/src/main/java/com/baeldung/microprofile/providers/BookMessageBodyReader.java new file mode 100644 index 0000000000..26ce4c1b64 --- /dev/null +++ b/microprofile/src/main/java/com/baeldung/microprofile/providers/BookMessageBodyReader.java @@ -0,0 +1,30 @@ +package com.baeldung.microprofile.providers; + +import com.baeldung.microprofile.model.Book; +import com.baeldung.microprofile.util.BookMapper; + +import javax.ws.rs.Consumes; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyReader; +import javax.ws.rs.ext.Provider; +import java.io.IOException; +import java.io.InputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +@Provider +@Consumes(MediaType.APPLICATION_JSON) +public class BookMessageBodyReader implements MessageBodyReader { + + @Override + public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return type.equals(Book.class); + } + + @Override + public Book readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { + return BookMapper.map(entityStream); + } +} \ No newline at end of file diff --git a/microprofile/src/main/java/com/baeldung/microprofile/providers/BookMessageBodyWriter.java b/microprofile/src/main/java/com/baeldung/microprofile/providers/BookMessageBodyWriter.java new file mode 100644 index 0000000000..9bc6e89958 --- /dev/null +++ b/microprofile/src/main/java/com/baeldung/microprofile/providers/BookMessageBodyWriter.java @@ -0,0 +1,57 @@ +package com.baeldung.microprofile.providers; + +import com.baeldung.microprofile.model.Book; +import com.baeldung.microprofile.util.BookMapper; + +import javax.json.Json; +import javax.json.JsonObject; +import javax.json.JsonWriter; +import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyWriter; +import javax.ws.rs.ext.Provider; +import java.io.IOException; +import java.io.OutputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +@Provider +@Produces(MediaType.APPLICATION_JSON) +public class BookMessageBodyWriter implements MessageBodyWriter { + @Override + public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return type.equals(Book.class); + } + + /* + Deprecated in JAX RS 2.0 + */ + @Override + public long getSize(Book book, Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return 0; + } + + /** + * Marsahl Book to OutputStream + * + * @param book + * @param type + * @param genericType + * @param annotations + * @param mediaType + * @param httpHeaders + * @param entityStream + * @throws IOException + * @throws WebApplicationException + */ + @Override + public void writeTo(Book book, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { + JsonWriter jsonWriter = Json.createWriter(entityStream); + JsonObject jsonObject = BookMapper.map(book); + jsonWriter.writeObject(jsonObject); + jsonWriter.close(); + } + +} diff --git a/microprofile/src/main/java/com/baeldung/microprofile/repo/BookManager.java b/microprofile/src/main/java/com/baeldung/microprofile/repo/BookManager.java new file mode 100644 index 0000000000..924cf0ce71 --- /dev/null +++ b/microprofile/src/main/java/com/baeldung/microprofile/repo/BookManager.java @@ -0,0 +1,53 @@ +package com.baeldung.microprofile.repo; + +import com.baeldung.microprofile.model.Book; + +import javax.enterprise.context.ApplicationScoped; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.atomic.AtomicInteger; + +@ApplicationScoped +public class BookManager { + + private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM"); + private AtomicInteger bookIdGenerator = new AtomicInteger(0); + + private ConcurrentMap inMemoryStore = new ConcurrentHashMap<>(); + + public BookManager() { + Book book = new Book(); + book.setId(getNextId()); + book.setName("Building Microservice With Eclipse MicroProfile"); + book.setIsbn("1"); + book.setAuthor("baeldung"); + book.setPages(420); + inMemoryStore.put(book.getId(), book); + } + + private String getNextId() { + String date = LocalDate.now().format(formatter); + return String.format("%04d-%s", bookIdGenerator.incrementAndGet(), date); + } + + public String add(Book book) { + String id = getNextId(); + book.setId(id); + inMemoryStore.put(id, book); + return id; + } + + public Book get(String id) { + return inMemoryStore.get(id); + } + + public List getAll() { + List books = new ArrayList<>(); + books.addAll(inMemoryStore.values()); + return books; + } +} diff --git a/microprofile/src/main/java/com/baeldung/microprofile/util/BookMapper.java b/microprofile/src/main/java/com/baeldung/microprofile/util/BookMapper.java new file mode 100644 index 0000000000..861b172299 --- /dev/null +++ b/microprofile/src/main/java/com/baeldung/microprofile/util/BookMapper.java @@ -0,0 +1,72 @@ +package com.baeldung.microprofile.util; + +import com.baeldung.microprofile.model.Book; + +import javax.json.*; +import java.io.InputStream; +import java.util.List; + +public class BookMapper { + + public static JsonObject map(Book book) { + JsonObjectBuilder builder = Json.createObjectBuilder(); + addValue(builder, "id", book.getId()); + addValue(builder, "isbn", book.getIsbn()); + addValue(builder, "name", book.getName()); + addValue(builder, "author", book.getAuthor()); + addValue(builder, "pages", book.getPages()); + return builder.build(); + } + + private static void addValue(JsonObjectBuilder builder, String key, Object value) { + if (value != null) { + builder.add(key, value.toString()); + } else { + builder.addNull(key); + } + } + + public static JsonArray map(List books) { + final JsonArrayBuilder arrayBuilder = Json.createArrayBuilder(); + books.forEach(book -> { + JsonObject jsonObject = map(book); + arrayBuilder.add(jsonObject); + }); + return arrayBuilder.build(); + } + + public static Book map(InputStream is) { + try(JsonReader jsonReader = Json.createReader(is)) { + JsonObject jsonObject = jsonReader.readObject(); + Book book = new Book(); + book.setId(getStringFromJson("id", jsonObject)); + book.setIsbn(getStringFromJson("isbn", jsonObject)); + book.setName(getStringFromJson("name", jsonObject)); + book.setAuthor(getStringFromJson("author", jsonObject)); + book.setPages(getIntFromJson("pages",jsonObject)); + return book; + } + } + + private static String getStringFromJson(String key, JsonObject json) { + String returnedString = null; + if (json.containsKey(key)) { + JsonString value = json.getJsonString(key); + if (value != null) { + returnedString = value.getString(); + } + } + return returnedString; + } + + private static Integer getIntFromJson(String key, JsonObject json) { + Integer returnedValue = null; + if (json.containsKey(key)) { + JsonNumber value = json.getJsonNumber(key); + if (value != null) { + returnedValue = value.intValue(); + } + } + return returnedValue; + } +} diff --git a/microprofile/src/main/java/com/baeldung/microprofile/web/BookEndpoint.java b/microprofile/src/main/java/com/baeldung/microprofile/web/BookEndpoint.java new file mode 100644 index 0000000000..13143a5644 --- /dev/null +++ b/microprofile/src/main/java/com/baeldung/microprofile/web/BookEndpoint.java @@ -0,0 +1,42 @@ +package com.baeldung.microprofile.web; + +import com.baeldung.microprofile.model.Book; +import com.baeldung.microprofile.repo.BookManager; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriBuilder; + +@Path("books") +@RequestScoped +public class BookEndpoint { + + @Inject + private BookManager bookManager; + + @GET + @Path("{id}") + @Produces(MediaType.APPLICATION_JSON) + public Response getBook(@PathParam("id") String id) { + Book book = bookManager.get(id); + return Response.ok(book).build(); + } + + @GET + @Produces(MediaType.APPLICATION_JSON) + public Response getAllBooks() { + return Response.ok(bookManager.getAll()).build(); + } + + @POST + @Consumes(MediaType.APPLICATION_JSON) + public Response add(Book book) { + String bookId = bookManager.add(book); + return Response.created( + UriBuilder.fromResource(this.getClass()).path(bookId).build()) + .build(); + } +} diff --git a/microprofile/src/main/liberty/config/server.xml b/microprofile/src/main/liberty/config/server.xml new file mode 100644 index 0000000000..2b855bee05 --- /dev/null +++ b/microprofile/src/main/liberty/config/server.xml @@ -0,0 +1,11 @@ + + + jaxrs-2.0 + cdi-1.2 + jsonp-1.0 + + + + + diff --git a/muleesb/README.md b/muleesb/README.md new file mode 100644 index 0000000000..8da4e595e3 --- /dev/null +++ b/muleesb/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: + +- [Getting Started With Mule ESB](http://www.baeldung.com/mule-esb) diff --git a/orientdb/README.md b/orientdb/README.md index 152ad392dd..56dfe0ab11 100644 --- a/orientdb/README.md +++ b/orientdb/README.md @@ -22,4 +22,7 @@ $ mvn clean install Before launching unit tests: - Install OrientDB - Create BaeldungDB, BaeldungDBTwo and BaeldungDBThree databases -- Uncomment annotations on the test files \ No newline at end of file +- Uncomment annotations on the test files + +### Relevant Articles: +- [Introduction to the OrientDB Java APIs](http://www.baeldung.com/java-orientdb) diff --git a/out/production/generated-sources8/src.main.resources.reladomo.ReladomoClassList.xml.log b/out/production/generated-sources8/src.main.resources.reladomo.ReladomoClassList.xml.log new file mode 100644 index 0000000000..0bd4e29a45 --- /dev/null +++ b/out/production/generated-sources8/src.main.resources.reladomo.ReladomoClassList.xml.log @@ -0,0 +1,2 @@ +f5a6ba3b942a82fcbfb72e61502d5c30 +9201deea diff --git a/out/production/introduction/views/index.scala.html b/out/production/introduction/views/index.scala.html new file mode 100644 index 0000000000..4539f5a10b --- /dev/null +++ b/out/production/introduction/views/index.scala.html @@ -0,0 +1,20 @@ +@* + * This template takes a single argument, a String containing a + * message to display. + *@ +@(message: String) + +@* + * Call the `main` template with two arguments. The first + * argument is a `String` with the title of the page, the second + * argument is an `Html` object containing the body of the page. + *@ +@main("Welcome to Play") { + + @* + * Get an `Html` object by calling the built-in Play welcome + * template and passing a `String` message. + *@ + @play20.welcome(message, style = "Java") + +} diff --git a/out/production/introduction/views/main.scala.html b/out/production/introduction/views/main.scala.html new file mode 100644 index 0000000000..9414f4be6e --- /dev/null +++ b/out/production/introduction/views/main.scala.html @@ -0,0 +1,23 @@ +@* + * This template is called from the `index` template. This template + * handles the rendering of the page header and body tags. It takes + * two arguments, a `String` for the title of the page and an `Html` + * object to insert into the body of the page. + *@ +@(title: String)(content: Html) + + + + + @* Here's where we render the page title `String`. *@ + @title + + + + + + @* And here's where we render the `Html` object containing + * the page content. *@ + @content + + diff --git a/out/production/main122/.gitignore b/out/production/main122/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/out/production/main122/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/out/production/main151/com/baeldung/.gitignore b/out/production/main151/com/baeldung/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/out/production/main151/com/baeldung/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/out/production/main151/com/baeldung/README.md b/out/production/main151/com/baeldung/README.md new file mode 100644 index 0000000000..51809b2882 --- /dev/null +++ b/out/production/main151/com/baeldung/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [SHA-256 Hashing in Java](http://www.baeldung.com/sha-256-hashing-java) diff --git a/out/production/main155/com/baeldung/git/README.md b/out/production/main155/com/baeldung/git/README.md new file mode 100644 index 0000000000..7e6a597c28 --- /dev/null +++ b/out/production/main155/com/baeldung/git/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Injecting Git Information Into Spring](http://www.baeldung.com/spring-git-information) diff --git a/out/production/main173/log4j.properties b/out/production/main173/log4j.properties new file mode 100644 index 0000000000..5fe42d854c --- /dev/null +++ b/out/production/main173/log4j.properties @@ -0,0 +1,9 @@ +# Set root logger level to DEBUG and its only appender to A1. +log4j.rootLogger=DEBUG, A1 + +# A1 is set to be a ConsoleAppender. +log4j.appender.A1=org.apache.log4j.ConsoleAppender + +# A1 uses PatternLayout. +log4j.appender.A1.layout=org.apache.log4j.PatternLayout +log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n diff --git a/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/README.txt b/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/README.txt new file mode 100644 index 0000000000..bffe24e485 --- /dev/null +++ b/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/README.txt @@ -0,0 +1,76 @@ +About the application +--------------------- +This application demonstrates the usage of JavaEE Web Annotations. + + +Contents of the application +--------------------------- +1. AccountServlet.java - Demonstrates the @WebServlet and @ServletSecurity annotation. + +NOTES: @WebServlet annotation designates the AccountServlet class as a Servlet component. + The usage of its parameters 'urlPatterns' & 'initParams' can be observed. + An initialization parameter 'type' is being set to denote the type of the bank account. + + @ServletSecurity annotation imposes security constraints on the AccountServlet based on + the tomcat-users.xml. +   + This code assumes that your tomcat-users.xml looks as follows: + + + + + + + +   +N.B : To see @ServletSecurity annotation in action, please uncomment the annotation code + for @ServletSecurity. + + +2. BankAppServletContextListener.java - Demonstrates the @WebListener annotation for denoting a class as a ServletContextListener. + +NOTES: Sets a Servlet context attribute ATTR_DEFAULT_LANGUAGE to 'english' on web application start up, + which can then be used throughout the application. + + +3. LogInFilter.java - Demonstrates the @WebFilter annotation. + +NOTES: @WebFilter annotation designates the LogInFilter class as a Filter component. + It filters all requests to the bank account servlet and redirects them to + the login page. + +N.B : To see @WebFilter annotation in action, please uncomment the annotation code for @WebFilter. + + +4. UploadCustomerDocumentsServlet.java - Demonstrates the @MultipartConfig annotation. + +NOTES: @MultipartConfig anotation designates the UploadCustomerDocumentsServlet Servlet component, + to handle multipart/form-data requests. + To see it in action, deploy the web application an access the url: http://:/JavaEEAnnotationsSample/upload.jsp + Once you upload a file from here, it will get uploaded to D:/custDocs (assuming such a folder exists). + + +5. index.jsp - This is the welcome page. + +NOTES: You can enter a deposit amount here and click on the 'Deposit' button to see the AccountServlet in action. + +6. login.jsp - All requests to the AccountServlet are redirected to this page, if the LogInFilter is imposed. + +7. upload.jsp - Demonstrates the usage of handling multipart/form-data requests by the UploadCustomerDocumentsServlet. + + +Building and Running the application +------------------------------------ +To build the application: + +1. Open the project in eclipse +2. Right click on it in eclispe and choose Run As > Maven build +3. Give 'clean install' under Goals +4. This should build the WAR file of the application + +To run the application: + +1. Right click on the project +2. Run as > Run on Server +3. This will start you Tomcat server and deploy the application (Provided that you have configured Tomcat in your eclipse) +4. You should now be able to access the url : http://:/JavaEEAnnotationsSample diff --git a/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/pom.xml b/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/pom.xml new file mode 100644 index 0000000000..de69efa43a --- /dev/null +++ b/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/pom.xml @@ -0,0 +1,57 @@ + + 4.0.0 + com.baeldung.javaeeannotations + JavaEEAnnotationsSample + 0.0.1-SNAPSHOT + war + JavaEEAnnotationsSample + JavaEEAnnotationsSample + + + + + javax.annotation + javax.annotation-api + 1.3 + + + + javax.servlet + javax.servlet-api + 3.1.0 + + + + javax.servlet.jsp + jsp-api + 2.1 + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.7.0 + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-war-plugin + 2.4 + + src/main/webapp + SpringFieldConstructorInjection + false + + + + + JavaEEAnnotationsSample + + \ No newline at end of file diff --git a/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/WEB-INF/web.xml b/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..a92885ec11 --- /dev/null +++ b/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,10 @@ + + + + BASIC + default + + diff --git a/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/index.jsp b/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/index.jsp new file mode 100644 index 0000000000..c49dec859e --- /dev/null +++ b/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/index.jsp @@ -0,0 +1,16 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +My Account + + +
+ Amount: +    + +
+ + \ No newline at end of file diff --git a/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/login.jsp b/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/login.jsp new file mode 100644 index 0000000000..6892cb0420 --- /dev/null +++ b/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/login.jsp @@ -0,0 +1,12 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +Login + + +Login Here... + + \ No newline at end of file diff --git a/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/upload.jsp b/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/upload.jsp new file mode 100644 index 0000000000..3601322ef0 --- /dev/null +++ b/out/production/main180/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/upload.jsp @@ -0,0 +1,16 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +Insert title here + + +
+ +
+ +
+ + \ No newline at end of file diff --git a/out/production/main180/com/baeldung/jaxws/wsdl/employeeservicetopdown.wsdl b/out/production/main180/com/baeldung/jaxws/wsdl/employeeservicetopdown.wsdl new file mode 100644 index 0000000000..426717f90e --- /dev/null +++ b/out/production/main180/com/baeldung/jaxws/wsdl/employeeservicetopdown.wsdl @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/main195/com/baeldung/java/nio/selector/README.md b/out/production/main195/com/baeldung/java/nio/selector/README.md new file mode 100644 index 0000000000..b28aae1397 --- /dev/null +++ b/out/production/main195/com/baeldung/java/nio/selector/README.md @@ -0,0 +1,2 @@ +###Relevant Articles: +- [Introduction to the Java NIO Selector](http://www.baeldung.com/java-nio-selector) diff --git a/out/production/main216/com/baeldung/googlehttpclientguide/logging.properties b/out/production/main216/com/baeldung/googlehttpclientguide/logging.properties new file mode 100644 index 0000000000..02489378df --- /dev/null +++ b/out/production/main216/com/baeldung/googlehttpclientguide/logging.properties @@ -0,0 +1,10 @@ +# Properties file which configures the operation of the JDK logging facility. +# The system will look for this config file to be specified as a system property: +# -Djava.util.logging.config.file=${project_loc:dailymotion-simple-cmdline-sample}/logging.properties + +# Set up the console handler (uncomment "level" to show more fine-grained messages) +handlers = java.util.logging.ConsoleHandler +java.util.logging.ConsoleHandler.level = ALL + +# Set up logging of HTTP requests and responses (uncomment "level" to show) +com.google.api.client.http.level = ALL diff --git a/out/production/main231/com/baeldung/wicket/examples/HelloWorld.html b/out/production/main231/com/baeldung/wicket/examples/HelloWorld.html new file mode 100644 index 0000000000..497e98e01a --- /dev/null +++ b/out/production/main231/com/baeldung/wicket/examples/HelloWorld.html @@ -0,0 +1,52 @@ + + + + +Wicket Intro Examples + + + +
+
+
+

Wicket Introduction Examples:

+ + Hello World! +
+
+ Cafes +
+
+
+
+ + diff --git a/out/production/main231/com/baeldung/wicket/examples/cafeaddress/CafeAddress.html b/out/production/main231/com/baeldung/wicket/examples/cafeaddress/CafeAddress.html new file mode 100644 index 0000000000..c5ada2323d --- /dev/null +++ b/out/production/main231/com/baeldung/wicket/examples/cafeaddress/CafeAddress.html @@ -0,0 +1,15 @@ + + + + +Cafes + + +
+ +

+ Address: address +

+
+ + diff --git a/out/production/main231/com/baeldung/wicket/examples/helloworld/HelloWorld.html b/out/production/main231/com/baeldung/wicket/examples/helloworld/HelloWorld.html new file mode 100644 index 0000000000..c56d07fc10 --- /dev/null +++ b/out/production/main231/com/baeldung/wicket/examples/helloworld/HelloWorld.html @@ -0,0 +1,5 @@ + + + + + diff --git a/out/production/main234/com/baeldung/activiti/security.rar b/out/production/main234/com/baeldung/activiti/security.rar new file mode 100644 index 0000000000..38c4946168 Binary files /dev/null and b/out/production/main234/com/baeldung/activiti/security.rar differ diff --git a/out/production/main237/com/baeldung/datetime/README.md b/out/production/main237/com/baeldung/datetime/README.md new file mode 100644 index 0000000000..1e4adbb612 --- /dev/null +++ b/out/production/main237/com/baeldung/datetime/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Introduction to the Java 8 Date/Time API](http://www.baeldung.com/java-8-date-time-intro) diff --git a/out/production/main291/xml-bean-config.xml b/out/production/main291/xml-bean-config.xml new file mode 100644 index 0000000000..3b880bbd70 --- /dev/null +++ b/out/production/main291/xml-bean-config.xml @@ -0,0 +1,12 @@ + + + + + + + + + + \ No newline at end of file diff --git a/out/production/main30/com/baeldung/factorybean/README.md b/out/production/main30/com/baeldung/factorybean/README.md new file mode 100644 index 0000000000..13f9f379e0 --- /dev/null +++ b/out/production/main30/com/baeldung/factorybean/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [How to use the Spring FactoryBean?](http://www.baeldung.com/spring-factorybean) diff --git a/out/production/main330/com/baeldung/.gitignore b/out/production/main330/com/baeldung/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/out/production/main330/com/baeldung/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/out/production/main330/com/baeldung/README.md b/out/production/main330/com/baeldung/README.md new file mode 100644 index 0000000000..51809b2882 --- /dev/null +++ b/out/production/main330/com/baeldung/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [SHA-256 Hashing in Java](http://www.baeldung.com/sha-256-hashing-java) diff --git a/out/production/main330/com/baeldung/enums/README.md b/out/production/main330/com/baeldung/enums/README.md new file mode 100644 index 0000000000..6ccfa725f5 --- /dev/null +++ b/out/production/main330/com/baeldung/enums/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [A Guide to Java Enums](http://www.baeldung.com/a-guide-to-java-enums) diff --git a/out/production/main330/com/baeldung/networking/README.md b/out/production/main330/com/baeldung/networking/README.md new file mode 100644 index 0000000000..b9e827f085 --- /dev/null +++ b/out/production/main330/com/baeldung/networking/README.md @@ -0,0 +1,5 @@ +### Relevant Articles: +- [A Guide To UDP In Java](http://www.baeldung.com/udp-in-java) +- [A Guide To HTTP Cookies In Java](http://www.baeldung.com/cookies-java) +- [A Guide to the Java URL](http://www.baeldung.com/java-url) +- [Working with Network Interfaces in Java](http://www.baeldung.com/java-network-interfaces) diff --git a/out/production/main330/com/baeldung/printscreen/README.md b/out/production/main330/com/baeldung/printscreen/README.md new file mode 100644 index 0000000000..7b3b40c102 --- /dev/null +++ b/out/production/main330/com/baeldung/printscreen/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [How to Print Screen in Java](http://www.baeldung.com/print-screen-in-java) diff --git a/out/production/main330/log4j.properties b/out/production/main330/log4j.properties new file mode 100644 index 0000000000..5fe42d854c --- /dev/null +++ b/out/production/main330/log4j.properties @@ -0,0 +1,9 @@ +# Set root logger level to DEBUG and its only appender to A1. +log4j.rootLogger=DEBUG, A1 + +# A1 is set to be a ConsoleAppender. +log4j.appender.A1=org.apache.log4j.ConsoleAppender + +# A1 uses PatternLayout. +log4j.appender.A1.layout=org.apache.log4j.PatternLayout +log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n diff --git a/out/production/main351/com/baeldung/produceimage/README.md b/out/production/main351/com/baeldung/produceimage/README.md new file mode 100644 index 0000000000..acd546598d --- /dev/null +++ b/out/production/main351/com/baeldung/produceimage/README.md @@ -0,0 +1,3 @@ +### Relevant articles + +- [Returning an Image or a File with Spring](http://www.baeldung.com/spring-controller-return-image-file) diff --git a/out/production/main96/com/baeldung/git/README.md b/out/production/main96/com/baeldung/git/README.md new file mode 100644 index 0000000000..7e6a597c28 --- /dev/null +++ b/out/production/main96/com/baeldung/git/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Injecting Git Information Into Spring](http://www.baeldung.com/spring-git-information) diff --git a/out/production/routing-in-play/views/index.scala.html b/out/production/routing-in-play/views/index.scala.html new file mode 100644 index 0000000000..4539f5a10b --- /dev/null +++ b/out/production/routing-in-play/views/index.scala.html @@ -0,0 +1,20 @@ +@* + * This template takes a single argument, a String containing a + * message to display. + *@ +@(message: String) + +@* + * Call the `main` template with two arguments. The first + * argument is a `String` with the title of the page, the second + * argument is an `Html` object containing the body of the page. + *@ +@main("Welcome to Play") { + + @* + * Get an `Html` object by calling the built-in Play welcome + * template and passing a `String` message. + *@ + @play20.welcome(message, style = "Java") + +} diff --git a/out/production/routing-in-play/views/main.scala.html b/out/production/routing-in-play/views/main.scala.html new file mode 100644 index 0000000000..9414f4be6e --- /dev/null +++ b/out/production/routing-in-play/views/main.scala.html @@ -0,0 +1,23 @@ +@* + * This template is called from the `index` template. This template + * handles the rendering of the page header and body tags. It takes + * two arguments, a `String` for the title of the page and an `Html` + * object to insert into the body of the page. + *@ +@(title: String)(content: Html) + + + + + @* Here's where we render the page title `String`. *@ + @title + + + + + + @* And here's where we render the `Html` object containing + * the page content. *@ + @content + + diff --git a/out/test/test105/com/baeldung/cglib/proxy/README.md b/out/test/test105/com/baeldung/cglib/proxy/README.md new file mode 100644 index 0000000000..abeabc6162 --- /dev/null +++ b/out/test/test105/com/baeldung/cglib/proxy/README.md @@ -0,0 +1,3 @@ +### Relevant articles + +- [Introduction to cglib](http://www.baeldung.com/cglib) diff --git a/out/test/test143/com/baeldung/java9/README.MD b/out/test/test143/com/baeldung/java9/README.MD new file mode 100644 index 0000000000..2f44a2336b --- /dev/null +++ b/out/test/test143/com/baeldung/java9/README.MD @@ -0,0 +1,2 @@ +### Relevant Artiles: +- [Filtering a Stream of Optionals in Java](http://www.baeldung.com/java-filter-stream-of-optional) diff --git a/out/test/test174/org/baeldung/hamcrest/README.md b/out/test/test174/org/baeldung/hamcrest/README.md new file mode 100644 index 0000000000..7266ecda3a --- /dev/null +++ b/out/test/test174/org/baeldung/hamcrest/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Testing with Hamcrest](http://www.baeldung.com/java-junit-hamcrest-guide) diff --git a/out/test/test191/com/baeldung/web/controller/README.md b/out/test/test191/com/baeldung/web/controller/README.md new file mode 100644 index 0000000000..9923962dde --- /dev/null +++ b/out/test/test191/com/baeldung/web/controller/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [WebAppConfiguration in Spring Tests](http://www.baeldung.com/spring-webappconfiguration) diff --git a/out/test/test197/com/baeldung/java/nio2/README.md b/out/test/test197/com/baeldung/java/nio2/README.md new file mode 100644 index 0000000000..569be82d27 --- /dev/null +++ b/out/test/test197/com/baeldung/java/nio2/README.md @@ -0,0 +1,11 @@ +### Relevant Articles: +- [Introduction to the Java NIO2 File API](http://www.baeldung.com/java-nio-2-file-api) +- [Java NIO2 Path API](http://www.baeldung.com/java-nio-2-path) +- [A Guide To NIO2 Asynchronous File Channel](http://www.baeldung.com/java-nio2-async-file-channel) +- [Guide to Selenium with JUnit / TestNG](http://www.baeldung.com/java-selenium-with-junit-and-testng) +- [A Guide to NIO2 Asynchronous Socket Channel](http://www.baeldung.com/java-nio2-async-socket-channel) +- [A Guide To NIO2 FileVisitor](http://www.baeldung.com/java-nio2-file-visitor) +- [A Guide To NIO2 File Attribute APIs](http://www.baeldung.com/java-nio2-file-attribute) +- [How to use the Spring FactoryBean?](http://www.baeldung.com/spring-factorybean) +- [A Guide to WatchService in Java NIO2](http://www.baeldung.com/java-nio2-watchservice) +- [Guide to Java NIO2 Asynchronous Channel APIs](http://www.baeldung.com/java-nio-2-async-channels) diff --git a/out/test/test237/META-INF/persistence.xml b/out/test/test237/META-INF/persistence.xml new file mode 100644 index 0000000000..922aedbc39 --- /dev/null +++ b/out/test/test237/META-INF/persistence.xml @@ -0,0 +1,20 @@ + + + + org.baeldung.persistence.model.Foo + org.baeldung.persistence.model.Bar + + + + + + + + + + + + + diff --git a/out/test/test95/com/baeldung/hexToAscii/README.md b/out/test/test95/com/baeldung/hexToAscii/README.md new file mode 100644 index 0000000000..c6d5826333 --- /dev/null +++ b/out/test/test95/com/baeldung/hexToAscii/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Convert Hex to ASCII in Java](http://www.baeldung.com/java-convert-hex-to-ascii) diff --git a/out/test/test95/com/baeldung/java/conversion/README.md b/out/test/test95/com/baeldung/java/conversion/README.md new file mode 100644 index 0000000000..7c81180249 --- /dev/null +++ b/out/test/test95/com/baeldung/java/conversion/README.md @@ -0,0 +1,2 @@ +Relevant Articles: +- [Java String Conversions](http://www.baeldung.com/java-string-conversions) diff --git a/out/test/test95/org/baeldung/java/collections/README.md b/out/test/test95/org/baeldung/java/collections/README.md new file mode 100644 index 0000000000..317d81fae7 --- /dev/null +++ b/out/test/test95/org/baeldung/java/collections/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: +- [Join and Split Arrays and Collections in Java](http://www.baeldung.com/java-join-and-split) +- [Introduction to Java Servlets](http://www.baeldung.com/intro-to-servlets) diff --git a/out/test/test95/org/baeldung/java/lists/README.md b/out/test/test95/org/baeldung/java/lists/README.md new file mode 100644 index 0000000000..2a1e8aeeaa --- /dev/null +++ b/out/test/test95/org/baeldung/java/lists/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Check If Two Lists are Equal in Java](http://www.baeldung.com/java-test-a-list-for-ordinality-and-equality) diff --git a/out/test/test98/com/baeldung/applicationcontext/README.md b/out/test/test98/com/baeldung/applicationcontext/README.md new file mode 100644 index 0000000000..211007e0cf --- /dev/null +++ b/out/test/test98/com/baeldung/applicationcontext/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: +- [Introduction to Java Servlets](http://www.baeldung.com/intro-to-servlets) +- [Intro to the Spring ClassPathXmlApplicationContext](http://www.baeldung.com/spring-classpathxmlapplicationcontext) diff --git a/out/test/test98/com/baeldung/beanfactory/README.md b/out/test/test98/com/baeldung/beanfactory/README.md new file mode 100644 index 0000000000..cff20a184b --- /dev/null +++ b/out/test/test98/com/baeldung/beanfactory/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Guide to the Spring BeanFactory](http://www.baeldung.com/spring-beanfactory) diff --git a/parent-boot-5/pom.xml b/parent-boot-5/pom.xml index 55ac0957ff..0e3936a73a 100644 --- a/parent-boot-5/pom.xml +++ b/parent-boot-5/pom.xml @@ -21,7 +21,7 @@ spring-boot-starter-parent org.springframework.boot - 1.5.9.RELEASE + 1.5.10.RELEASE @@ -60,9 +60,6 @@ **/*IntegrationTest.java **/*LongRunningUnitTest.java **/*ManualTest.java - **/JdbcTest.java - **/AutoconfigurationTest.java - **/*EntryPointsTest.java **/*LiveTest.java
diff --git a/patterns/behavioral-patterns/src/main/java/com/baeldung/pattern/chainofresponsibility/AuthenticationProcessor.java b/patterns/behavioral-patterns/src/main/java/com/baeldung/pattern/chainofresponsibility/AuthenticationProcessor.java new file mode 100644 index 0000000000..374de31ba9 --- /dev/null +++ b/patterns/behavioral-patterns/src/main/java/com/baeldung/pattern/chainofresponsibility/AuthenticationProcessor.java @@ -0,0 +1,13 @@ +package com.baeldung.pattern.chainofresponsibility; + +public abstract class AuthenticationProcessor { + + // next element in chain or responsibility + public AuthenticationProcessor nextProcessor; + + public AuthenticationProcessor(AuthenticationProcessor nextProcessor) { + this.nextProcessor = nextProcessor; + } + + public abstract boolean isAuthorized(AuthenticationProvider authProvider); +} diff --git a/patterns/behavioral-patterns/src/main/java/com/baeldung/pattern/chainofresponsibility/AuthenticationProvider.java b/patterns/behavioral-patterns/src/main/java/com/baeldung/pattern/chainofresponsibility/AuthenticationProvider.java new file mode 100644 index 0000000000..7b8771ca41 --- /dev/null +++ b/patterns/behavioral-patterns/src/main/java/com/baeldung/pattern/chainofresponsibility/AuthenticationProvider.java @@ -0,0 +1,5 @@ +package com.baeldung.pattern.chainofresponsibility; + +public interface AuthenticationProvider { + +} diff --git a/patterns/behavioral-patterns/src/main/java/com/baeldung/pattern/chainofresponsibility/OAuthAuthenticationProcessor.java b/patterns/behavioral-patterns/src/main/java/com/baeldung/pattern/chainofresponsibility/OAuthAuthenticationProcessor.java new file mode 100644 index 0000000000..3bf20cfc85 --- /dev/null +++ b/patterns/behavioral-patterns/src/main/java/com/baeldung/pattern/chainofresponsibility/OAuthAuthenticationProcessor.java @@ -0,0 +1,21 @@ +package com.baeldung.pattern.chainofresponsibility; + +public class OAuthAuthenticationProcessor extends AuthenticationProcessor { + + public OAuthAuthenticationProcessor(AuthenticationProcessor nextProcessor) { + super(nextProcessor); + } + + @Override + public boolean isAuthorized(AuthenticationProvider authProvider) { + + if (authProvider instanceof OAuthTokenProvider) { + return Boolean.TRUE; + } else if (nextProcessor != null) { + return nextProcessor.isAuthorized(authProvider); + } else { + return Boolean.FALSE; + } + } + +} diff --git a/patterns/behavioral-patterns/src/main/java/com/baeldung/pattern/chainofresponsibility/OAuthTokenProvider.java b/patterns/behavioral-patterns/src/main/java/com/baeldung/pattern/chainofresponsibility/OAuthTokenProvider.java new file mode 100644 index 0000000000..92d5f94245 --- /dev/null +++ b/patterns/behavioral-patterns/src/main/java/com/baeldung/pattern/chainofresponsibility/OAuthTokenProvider.java @@ -0,0 +1,5 @@ +package com.baeldung.pattern.chainofresponsibility; + +public class OAuthTokenProvider implements AuthenticationProvider { + +} diff --git a/patterns/behavioral-patterns/src/main/java/com/baeldung/pattern/chainofresponsibility/SamlAuthenticationProvider.java b/patterns/behavioral-patterns/src/main/java/com/baeldung/pattern/chainofresponsibility/SamlAuthenticationProvider.java new file mode 100644 index 0000000000..cd927932ad --- /dev/null +++ b/patterns/behavioral-patterns/src/main/java/com/baeldung/pattern/chainofresponsibility/SamlAuthenticationProvider.java @@ -0,0 +1,5 @@ +package com.baeldung.pattern.chainofresponsibility; + +public class SamlAuthenticationProvider implements AuthenticationProvider { + +} diff --git a/patterns/behavioral-patterns/src/main/java/com/baeldung/pattern/chainofresponsibility/UsernamePasswordAuthenticationProcessor.java b/patterns/behavioral-patterns/src/main/java/com/baeldung/pattern/chainofresponsibility/UsernamePasswordAuthenticationProcessor.java new file mode 100644 index 0000000000..3885b2b79b --- /dev/null +++ b/patterns/behavioral-patterns/src/main/java/com/baeldung/pattern/chainofresponsibility/UsernamePasswordAuthenticationProcessor.java @@ -0,0 +1,20 @@ +package com.baeldung.pattern.chainofresponsibility; + +public class UsernamePasswordAuthenticationProcessor extends AuthenticationProcessor { + + public UsernamePasswordAuthenticationProcessor(AuthenticationProcessor nextProcessor) { + super(nextProcessor); + } + + @Override + public boolean isAuthorized(AuthenticationProvider authProvider) { + if (authProvider instanceof UsernamePasswordProvider) { + return Boolean.TRUE; + } else if (nextProcessor != null) { + return nextProcessor.isAuthorized(authProvider); + } else { + return Boolean.FALSE; + } + } + +} diff --git a/patterns/behavioral-patterns/src/main/java/com/baeldung/pattern/chainofresponsibility/UsernamePasswordProvider.java b/patterns/behavioral-patterns/src/main/java/com/baeldung/pattern/chainofresponsibility/UsernamePasswordProvider.java new file mode 100644 index 0000000000..9877039446 --- /dev/null +++ b/patterns/behavioral-patterns/src/main/java/com/baeldung/pattern/chainofresponsibility/UsernamePasswordProvider.java @@ -0,0 +1,5 @@ +package com.baeldung.pattern.chainofresponsibility; + +public class UsernamePasswordProvider implements AuthenticationProvider { + +} diff --git a/patterns/behavioral-patterns/src/test/java/com/baeldung/pattern/chainofresponsibility/ChainOfResponsibilityTest.java b/patterns/behavioral-patterns/src/test/java/com/baeldung/pattern/chainofresponsibility/ChainOfResponsibilityTest.java new file mode 100644 index 0000000000..a84f9dd8e5 --- /dev/null +++ b/patterns/behavioral-patterns/src/test/java/com/baeldung/pattern/chainofresponsibility/ChainOfResponsibilityTest.java @@ -0,0 +1,37 @@ +package com.baeldung.pattern.chainofresponsibility; + +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +public class ChainOfResponsibilityTest { + + private static AuthenticationProcessor getChainOfAuthProcessor() { + + AuthenticationProcessor oAuthProcessor = new OAuthAuthenticationProcessor(null); + AuthenticationProcessor unamePasswordProcessor = new UsernamePasswordAuthenticationProcessor(oAuthProcessor); + return unamePasswordProcessor; + } + + @Test + public void givenOAuthProvider_whenCheckingAuthorized_thenSuccess() { + AuthenticationProcessor authProcessorChain = getChainOfAuthProcessor(); + boolean isAuthorized = authProcessorChain.isAuthorized(new OAuthTokenProvider()); + assertTrue(isAuthorized); + } + + @Test + public void givenUsernamePasswordProvider_whenCheckingAuthorized_thenSuccess() { + AuthenticationProcessor authProcessorChain = getChainOfAuthProcessor(); + boolean isAuthorized = authProcessorChain.isAuthorized(new UsernamePasswordProvider()); + assertTrue(isAuthorized); + } + + @Test + public void givenSamlAuthProvider_whenCheckingAuthorized_thenFailure() { + AuthenticationProcessor authProcessorChain = getChainOfAuthProcessor(); + boolean isAuthorized = authProcessorChain.isAuthorized(new SamlAuthenticationProvider()); + assertTrue(!isAuthorized); + } + +} diff --git a/persistence-modules/README.md b/persistence-modules/README.md index 6c2b1d2ca9..f05a822c30 100644 --- a/persistence-modules/README.md +++ b/persistence-modules/README.md @@ -1,3 +1,9 @@ ## Persistence Modules + +### Relevant Articles: + +- [Introduction to Hibernate Search](http://www.baeldung.com/hibernate-search) +- [Bootstrapping Hibernate 5 with Spring](http://www.baeldung.com/hibernate-5-spring) +- [Introduction to Lettuce – the Java Redis Client](http://www.baeldung.com/java-redis-lettuce) diff --git a/persistence-modules/java-jdbi/README.md b/persistence-modules/java-jdbi/README.md new file mode 100644 index 0000000000..3bab6faa29 --- /dev/null +++ b/persistence-modules/java-jdbi/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Guide to CockroachDB in Java](http://www.baeldung.com/cockroachdb-java) diff --git a/persistence-modules/java-jdbi/pom.xml b/persistence-modules/java-jdbi/pom.xml new file mode 100644 index 0000000000..392f0bdcbf --- /dev/null +++ b/persistence-modules/java-jdbi/pom.xml @@ -0,0 +1,40 @@ + + + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + ../../ + + + 4.0.0 + + java-jdbi + 1.0-SNAPSHOT + + + + org.jdbi + jdbi3-core + 3.1.0 + + + org.hsqldb + hsqldb + 2.4.0 + test + + + + + + Central + Central + http://repo1.maven.org/maven2/ + default + + + \ No newline at end of file diff --git a/persistence-modules/java-jdbi/src/test/java/com/baeldung/persistence/jdbi/JdbiTest.java b/persistence-modules/java-jdbi/src/test/java/com/baeldung/persistence/jdbi/JdbiTest.java new file mode 100644 index 0000000000..503bf90fdb --- /dev/null +++ b/persistence-modules/java-jdbi/src/test/java/com/baeldung/persistence/jdbi/JdbiTest.java @@ -0,0 +1,338 @@ +package com.baeldung.persistence.jdbi; + +import org.jdbi.v3.core.Handle; +import org.jdbi.v3.core.Jdbi; +import org.jdbi.v3.core.result.ResultBearing; +import org.jdbi.v3.core.result.ResultProducer; +import org.jdbi.v3.core.statement.Query; +import org.jdbi.v3.core.statement.StatementContext; +import org.jdbi.v3.core.statement.Update; +import org.junit.Test; + +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.*; +import java.util.function.Supplier; +import java.util.stream.Stream; + +import static org.junit.Assert.*; + +public class JdbiTest { + + @Test + public void whenJdbiCreated_thenSuccess() { + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); + + Jdbi.create("WRONG"); + } + + @Test + public void whenJdbiWithProperties_thenSuccess() { + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); + jdbi.open().close(); + Properties properties = new Properties(); + properties.setProperty("username", "sa"); + properties.setProperty("password", ""); + jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", properties); + jdbi.open().close(); + } + + @Test + public void whenHandle_thenBoh() { + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); + final Handle[] handleRef = new Handle[1]; + boolean closed = jdbi.withHandle(handle -> { + handleRef[0] = handle; + return handle.isClosed(); + }); + + assertFalse(closed); + assertTrue(handleRef[0].isClosed()); + } + + @Test + public void whenTableCreated_thenInsertIsPossible() { + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); + jdbi.useHandle(handle -> { + int updateCount = handle.execute("create table PROJECT_1 (id integer identity, name varchar(50), url varchar(100))"); + + assertEquals(0, updateCount); + + updateCount = handle.execute("INSERT INTO PROJECT_1 VALUES (1, 'tutorials', 'github.com/eugenp/tutorials')"); + + assertEquals(1, updateCount); + }); + } + + @Test + public void whenIdentityColumn_thenInsertReturnsNewId() { + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); + jdbi.useHandle(handle -> { + handle.execute("create table PROJECT_2 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); + Update update = handle.createUpdate( + "INSERT INTO PROJECT_2 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')"); + ResultBearing generatedKeys = update.executeAndReturnGeneratedKeys(); + + assertEquals(0, generatedKeys.mapToMap().findOnly().get("id")); + + update = handle.createUpdate( + "INSERT INTO PROJECT_2 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')"); + + assertEquals(1, generatedKeys.mapToMap().findOnly().get("id")); + }); + } + + @Test + public void whenSelectMapToMap_thenResultsAreMapEntries() { + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); + jdbi.useHandle(handle -> { + handle.execute("create table PROJECT_3 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); + handle.execute("INSERT INTO PROJECT_3 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')"); + handle.execute("INSERT INTO PROJECT_3 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')"); + Query query = handle.createQuery("select * from PROJECT_3 order by id"); + List> list = query.mapToMap().list(); + + assertEquals("tutorials", list.get(0).get("name")); + assertEquals("REST with Spring", list.get(1).get("name")); + }); + } + + @Test + public void whenSelectMapToString_thenResultIsString() { + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); + jdbi.useHandle(handle -> { + handle.execute("create table PROJECT_4 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); + handle.execute("INSERT INTO PROJECT_4 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')"); + handle.execute("INSERT INTO PROJECT_4 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')"); + Query query = handle.createQuery("select name, url from PROJECT_4 order by id"); + List list = query.mapTo(String.class).list(); + + assertEquals("tutorials", list.get(0)); + assertEquals("REST with Spring", list.get(1)); + }); + } + + @Test + public void whenSelectMapToString_thenStream() { + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); + jdbi.useHandle(handle -> { + handle.execute("create table PROJECT_5 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); + handle.execute("INSERT INTO PROJECT_5 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')"); + handle.execute("INSERT INTO PROJECT_5 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')"); + Query query = handle.createQuery("select name, url from PROJECT_5 order by id"); + query.mapTo(String.class).useStream((Stream stream) -> assertEquals("tutorials", stream.findFirst().get())); + }); + } + + @Test + public void whenNoResults_thenFindFirstReturnsNone() { + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); + jdbi.useHandle(handle -> { + handle.execute("create table PROJECT_6 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); + + assertFalse(handle.createQuery("select * from project_6").mapToMap().findFirst().isPresent()); + }); + } + + @Test + public void whenMultipleResults_thenFindFirstReturnsFirst() { + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); + jdbi.useHandle(handle -> { + handle.execute("create table PROJECT_7 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); + handle.execute("INSERT INTO PROJECT_7 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')"); + handle.execute("INSERT INTO PROJECT_7 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')"); + + Query query = handle.createQuery("select * from project_7 order by id"); + Optional> first = query.mapToMap().findFirst(); + + assertTrue(first.isPresent()); + assertEquals("tutorials", first.get().get("name")); + }); + } + + @Test + public void whenNoResults_thenFindOnlyThrows() { + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); + jdbi.useHandle(handle -> { + handle.execute("create table PROJECT_8 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); + + try { + handle.createQuery("select * from project_8").mapToMap().findOnly(); + + fail("Exception expected"); + } catch (Exception e) { + e.printStackTrace(); + } + }); + } + + @Test + public void whenMultipleResults_thenFindOnlyThrows() { + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); + jdbi.useHandle(handle -> { + handle.execute("create table PROJECT_9 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); + handle.execute("INSERT INTO PROJECT_9 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')"); + handle.execute("INSERT INTO PROJECT_9 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')"); + + try { + Query query = handle.createQuery("select * from project_9"); + Map onlyResult = query.mapToMap().findOnly(); + + fail("Exception expected"); + } catch (Exception e) { + e.printStackTrace(); + } + }); + } + + @Test + public void whenParameters_thenReplacement() { + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); + jdbi.useHandle(handle -> { + handle.execute("create table PROJECT_10 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); + Update update1 = handle.createUpdate("INSERT INTO PROJECT_10 (NAME, URL) VALUES (?, ?)"); + update1.bind(0, "tutorials"); + update1.bind(1, "github.com/eugenp/tutorials"); + int rows = update1.execute(); + + assertEquals(1, rows); + + Update update2 = handle.createUpdate("INSERT INTO PROJECT_10 (NAME, URL) VALUES (:name, :url)"); + update2.bind("name", "REST with Spring"); + update2.bind("url", "github.com/eugenp/REST-With-Spring"); + rows = update2.execute(); + + assertEquals(1, rows); + + List> list = + handle.select("SELECT * FROM PROJECT_10 WHERE NAME = 'tutorials'").mapToMap().list(); + + assertEquals(1, list.size()); + + list = handle.select("SELECT * FROM PROJECT_10 WHERE NAME = 'REST with Spring'").mapToMap().list(); + + assertEquals(1, list.size()); + }); + } + + @Test + public void whenMultipleParameters_thenReplacement() { + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); + jdbi.useHandle(handle -> { + handle.execute("create table PROJECT_11 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); + Update update = handle.createUpdate("INSERT INTO PROJECT_11 (NAME, URL) VALUES (:name, :url)"); + Map params = new HashMap<>(); + params.put("name", "REST with Spring"); + params.put("url", "github.com/eugenp/REST-With-Spring"); + update.bindMap(params); + int rows = update.execute(); + + assertEquals(1, rows); + + List> list = + handle.select("SELECT * FROM PROJECT_11").mapToMap().list(); + + assertEquals(1, list.size()); + + class Params { + private String name; + private String url; + + public Params(String name, String url) { + this.name = name; + this.url = url; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + } + + update.bindBean(new Params("tutorials", "github.com/eugenp/tutorials")); + rows = update.execute(); + + assertEquals(1, rows); + + list = handle.select("SELECT * FROM PROJECT_11").mapToMap().list(); + + assertEquals(2, list.size()); + }); + } + + @Test + public void whenTransactionRollback_thenNoDataInserted() { + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); + jdbi.useHandle(handle -> { + handle.useTransaction(h -> { + handle.execute("create table PROJECT_12 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); + handle.execute("INSERT INTO PROJECT_12 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')"); + handle.execute("INSERT INTO PROJECT_12 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')"); + handle.rollback(); + List> list = handle.select("SELECT * FROM PROJECT_12").mapToMap().list(); + + assertEquals(0, list.size()); + }); + }); + } + + @Test + public void whenTransactionRollbackThenCommit_thenOnlyLastInserted() { + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); + jdbi.useHandle(handle -> { + handle.useTransaction((Handle h) -> { + assertEquals(handle, h); + + handle.execute("create table PROJECT_13 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); + handle.execute("INSERT INTO PROJECT_13 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')"); + handle.rollback(); + handle.begin(); + handle.execute("INSERT INTO PROJECT_13 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')"); + handle.rollback(); + handle.begin(); + handle.execute("INSERT INTO PROJECT_13 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')"); + handle.execute("INSERT INTO PROJECT_13 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')"); + handle.commit(); + }); + List> list = handle.select("SELECT * FROM PROJECT_13").mapToMap().list(); + + assertEquals(2, list.size()); + }); + } + + + @Test + public void whenException_thenTransactionIsRolledBack() { + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); + jdbi.useHandle(handle -> { + try { + handle.useTransaction(h -> { + h.execute("create table PROJECT_14 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); + h.execute("INSERT INTO PROJECT_14 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')"); + List> list = handle.select("SELECT * FROM PROJECT_14").mapToMap().list(); + + assertTrue(h.isInTransaction()); + assertEquals(1, list.size()); + + throw new Exception("rollback"); + }); + } catch (Exception ignored) {} + List> list = handle.select("SELECT * FROM PROJECT_14").mapToMap().list(); + + assertFalse(handle.isInTransaction()); + assertEquals(0, list.size()); + }); + } + +} diff --git a/persistence-modules/redis/README.md b/persistence-modules/redis/README.md index d179b80c33..dd655ca7aa 100644 --- a/persistence-modules/redis/README.md +++ b/persistence-modules/redis/README.md @@ -1,3 +1,5 @@ ### Relevant Articles: - [Intro to Jedis – the Java Redis Client Library](http://www.baeldung.com/jedis-java-redis-client-library) - [A Guide to Redis with Redisson](http://www.baeldung.com/redis-redisson) +- [Intro to Lettuce – the Java Redis Client Library](http://www.baeldung.com/lettuce-java-redis-client-library) + diff --git a/persistence-modules/redis/pom.xml b/persistence-modules/redis/pom.xml index 4321b491eb..1f27faa09a 100644 --- a/persistence-modules/redis/pom.xml +++ b/persistence-modules/redis/pom.xml @@ -36,6 +36,13 @@ redisson 3.3.0 + + + io.lettuce + lettuce-core + 5.0.1.RELEASE + + diff --git a/persistence-modules/redis/src/test/java/com/baeldung/LettuceIntegrationLiveTest.java b/persistence-modules/redis/src/test/java/com/baeldung/LettuceIntegrationLiveTest.java new file mode 100644 index 0000000000..eb879d1d21 --- /dev/null +++ b/persistence-modules/redis/src/test/java/com/baeldung/LettuceIntegrationLiveTest.java @@ -0,0 +1,312 @@ +package com.baeldung; + +import io.lettuce.core.LettuceFutures; +import io.lettuce.core.RedisClient; +import io.lettuce.core.RedisFuture; +import io.lettuce.core.TransactionResult; +import io.lettuce.core.api.StatefulRedisConnection; +import io.lettuce.core.api.async.RedisAsyncCommands; +import io.lettuce.core.api.sync.RedisCommands; +import io.lettuce.core.pubsub.RedisPubSubListener; +import io.lettuce.core.pubsub.StatefulRedisPubSubConnection; +import io.lettuce.core.pubsub.api.async.RedisPubSubAsyncCommands; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertTrue; + +public class LettuceIntegrationLiveTest { + + private static Logger log = LoggerFactory.getLogger(LettuceIntegrationLiveTest.class); + + private static StatefulRedisConnection redisConnection; + + private static RedisClient redisClient; + + @BeforeClass + public static void setUp() { + // Docker defaults to mapping redis port to 32768 + redisClient = RedisClient.create("redis://localhost:32768/"); + redisConnection = redisClient.connect(); + } + + @AfterClass + public static void destroy() { + redisConnection.close(); + } + + @Test + public void givenAString_thenSaveItAsRedisStringsSync() { + + RedisCommands syncCommands = redisConnection.sync(); + + String key = "key"; + String value = "value"; + + syncCommands.set(key, value); + String response = syncCommands.get(key); + + Assert.assertEquals(value, response); + } + + @Test + public void givenValues_thenSaveAsRedisHashSync() { + + RedisCommands syncCommands = redisConnection.sync(); + + String recordName = "record1"; + String name = "FirstName"; + String value = "John"; + String surname = "LastName"; + String value1 = "Smith"; + + syncCommands.hset(recordName, name, value); + syncCommands.hset(recordName, surname, value1); + Map record = syncCommands.hgetall(recordName); + + Assert.assertEquals(record.get(name), value); + Assert.assertEquals(record.get(surname), value1); + } + + @Test + public void givenAString_thenSaveItAsRedisStringsAsync() throws Exception { + + RedisAsyncCommands asyncCommands = redisConnection.async(); + + String key = "key"; + String value = "value"; + + asyncCommands.set(key, value); + RedisFuture redisFuture = asyncCommands.get(key); + + String response = redisFuture.get(); + + Assert.assertEquals(value, response); + } + + @Test + public void givenValues_thenSaveAsRedisHashAsync() throws Exception { + + RedisAsyncCommands asyncCommands = redisConnection.async(); + + String recordName = "record1"; + String name = "FirstName"; + String value = "John"; + String surname = "LastName"; + String value1 = "Smith"; + + asyncCommands.hset(recordName, name, value); + asyncCommands.hset(recordName, surname, value1); + RedisFuture> redisFuture = asyncCommands.hgetall(recordName); + + Map record = redisFuture.get(); + + Assert.assertEquals(record.get(name), value); + Assert.assertEquals(record.get(surname), value1); + } + + @Test + public void givenValues_thenSaveAsRedisListAsync() throws Exception { + + RedisAsyncCommands asyncCommands = redisConnection.async(); + + String listName = "tasks"; + String firstTask = "firstTask"; + String secondTask = "secondTask"; + + asyncCommands.del(listName); + + asyncCommands.lpush(listName, firstTask); + asyncCommands.lpush(listName, secondTask); + RedisFuture redisFuture = asyncCommands.rpop(listName); + + String nextTask = redisFuture.get(); + + Assert.assertEquals(firstTask, nextTask); + + asyncCommands.del(listName); + + asyncCommands.lpush(listName, firstTask); + asyncCommands.lpush(listName, secondTask); + + redisFuture = asyncCommands.lpop(listName); + + nextTask = redisFuture.get(); + + Assert.assertEquals(secondTask, nextTask); + + } + + @Test + public void givenSetElements_thenSaveThemInRedisSetAsync() throws Exception { + + RedisAsyncCommands asyncCommands = redisConnection.async(); + + String countries = "countries"; + + String countryOne = "Spain"; + String countryTwo = "Ireland"; + String countryThree = "Ireland"; + + asyncCommands.sadd(countries, countryOne); + + RedisFuture> countriesSetFuture = asyncCommands.smembers(countries); + Assert.assertEquals(2, countriesSetFuture.get().size()); + + asyncCommands.sadd(countries, countryTwo); + countriesSetFuture = asyncCommands.smembers(countries); + Assert.assertEquals(2, countriesSetFuture.get().size()); + + asyncCommands.sadd(countries, countryThree); + countriesSetFuture = asyncCommands.smembers(countries); + Assert.assertEquals(2, countriesSetFuture.get().size()); + + RedisFuture exists = asyncCommands.sismember(countries, countryThree); + assertTrue(exists.get()); + } + + @Test + public void givenARanking_thenSaveItInRedisSortedSetAsync() throws Exception { + + RedisAsyncCommands asyncCommands = redisConnection.async(); + + String key = "sortedset"; + + asyncCommands.zadd(key, 1, "one"); + asyncCommands.zadd(key, 4, "zero"); + asyncCommands.zadd(key, 2, "two"); + + RedisFuture> values = asyncCommands.zrevrange(key, 0, 3); + Assert.assertEquals("zero", values.get().get(0)); + + values = asyncCommands.zrange(key, 0, 3); + Assert.assertEquals("one", values.get().get(0)); + } + + @Test + public void givenMultipleOperationsThatNeedToBeExecutedAtomically_thenWrapThemInATransaction() throws Exception { + + RedisAsyncCommands asyncCommands = redisConnection.async(); + + // Start a transaction + asyncCommands.multi(); + + // Add three sets to it, and save the future responses + RedisFuture result1 = asyncCommands.set("key1", "value1"); + RedisFuture result2 = asyncCommands.set("key2", "value2"); + RedisFuture result3 = asyncCommands.set("key3", "value3"); + + // Execute it + RedisFuture execResult = asyncCommands.exec(); + + TransactionResult transactionResult = execResult.get(); + + // Get the three results in the transaction return + String firstResult = transactionResult.get(0); + String secondResult = transactionResult.get(0); + String thirdResult = transactionResult.get(0); + + // Our results are in both! + assertTrue(firstResult.equals("OK")); + assertTrue(secondResult.equals("OK")); + assertTrue(thirdResult.equals("OK")); + + assertTrue(result1.get().equals("OK")); + assertTrue(result2.get().equals("OK")); + assertTrue(result3.get().equals("OK")); + } + + @Test + public void givenMultipleIndependentOperations_whenNetworkOptimizationIsImportant_thenFlushManually() throws Exception { + + int iterations = 50; + + RedisAsyncCommands asyncCommands = redisConnection.async(); + + asyncCommands.setAutoFlushCommands(false); + + List> futures = new ArrayList<>(); + for (int i = 0; i < iterations; i++) { + futures.add(asyncCommands.set("key" + i, "value" + i)); + } + + asyncCommands.flushCommands(); + + // Wait until all futures complete + boolean result = LettuceFutures.awaitAll(5, TimeUnit.SECONDS, futures.toArray(new RedisFuture[futures.size()])); + + asyncCommands.setAutoFlushCommands(true); + + } + + @Test + public void givenPubSubChannel_whenMessage_thenMessageReceived() throws Exception { + + Listener listener = new Listener(); + StatefulRedisPubSubConnection connection = redisClient.connectPubSub(); + StatefulRedisPubSubConnection pubconnection = redisClient.connectPubSub(); + connection.addListener(listener); + + RedisPubSubAsyncCommands async = connection.async(); + async.subscribe("channel"); + + RedisPubSubAsyncCommands pubasync = pubconnection.async(); + RedisFuture result = pubasync.publish("channel", "hithere"); + + // Need a long wait for publish to complete, depending on system. + result.get(15, TimeUnit.SECONDS); + assertTrue(listener.getMessage().equals("hithere")); + + } + + private static class Listener implements RedisPubSubListener { + + private String message; + + String getMessage() { + return message; + } + + @Override + public void message(String channel, String message) { + log.debug("Got {} on {}", message, channel); + this.message = message; + } + + @Override + public void message(String pattern, String channel, String message) { + + } + + @Override + public void subscribed(String channel, long count) { + log.debug("Subscribed to {}", channel); + } + + @Override + public void psubscribed(String pattern, long count) { + + } + + @Override + public void unsubscribed(String channel, long count) { + + } + + @Override + public void punsubscribed(String pattern, long count) { + + } + } + +} diff --git a/persistence-modules/spring-data-dynamodb/pom.xml b/persistence-modules/spring-data-dynamodb/pom.xml index bf90779c29..c3ea9abf08 100644 --- a/persistence-modules/spring-data-dynamodb/pom.xml +++ b/persistence-modules/spring-data-dynamodb/pom.xml @@ -23,6 +23,10 @@ 4.4.1 1.11.64 3.3.7-1 + 1.0.392 + 1.11.106 + 1.11.86 + https://s3-us-west-2.amazonaws.com/dynamodb-local/release @@ -103,6 +107,57 @@ httpclient ${httpclient.version} + + + + + com.amazonaws + DynamoDBLocal + ${dynamodblocal.version} + test + + + com.almworks.sqlite4java + sqlite4java + ${sqlite4java.version} + test + + + com.almworks.sqlite4java + sqlite4java-win32-x86 + ${sqlite4java.version} + dll + test + + + com.almworks.sqlite4java + sqlite4java-win32-x64 + ${sqlite4java.version} + dll + test + + + com.almworks.sqlite4java + libsqlite4java-osx + ${sqlite4java.version} + dylib + test + + + com.almworks.sqlite4java + libsqlite4java-linux-i386 + ${sqlite4java.version} + so + test + + + com.almworks.sqlite4java + libsqlite4java-linux-amd64 + ${sqlite4java.version} + so + test + + @@ -120,6 +175,25 @@ org.apache.maven.plugins maven-war-plugin + + org.apache.maven.plugins + maven-dependency-plugin + 2.10 + + + copy-dependencies + test-compile + + copy-dependencies + + + test + so,dll,dylib + ${project.basedir}/native-libs + + + + @@ -142,6 +216,11 @@ false
+ + dynamodb-local + DynamoDB Local Release Repository + ${dynamodblocal.repository.url} + diff --git a/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java b/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java index 2ff418b4ec..6cbd5b0a5a 100644 --- a/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java @@ -8,8 +8,9 @@ import com.amazonaws.services.dynamodbv2.model.ResourceInUseException; import com.baeldung.Application; import com.baeldung.spring.data.dynamodb.model.ProductInfo; import com.baeldung.spring.data.dynamodb.repositories.ProductInfoRepository; +import com.baeldung.spring.data.dynamodb.repository.rule.LocalDbCreationRule; import org.junit.Before; -import org.junit.Ignore; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -21,7 +22,10 @@ import org.springframework.test.context.web.WebAppConfiguration; import java.util.List; -import static org.junit.Assert.assertTrue; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = Application.class) @@ -30,6 +34,9 @@ import static org.junit.Assert.assertTrue; @TestPropertySource(properties = { "amazon.dynamodb.endpoint=http://localhost:8000/", "amazon.aws.accesskey=test1", "amazon.aws.secretkey=test231" }) public class ProductInfoRepositoryIntegrationTest { + @ClassRule + public static LocalDbCreationRule dynamoDB = new LocalDbCreationRule(); + private DynamoDBMapper dynamoDBMapper; @Autowired @@ -42,7 +49,6 @@ public class ProductInfoRepositoryIntegrationTest { private static final String EXPECTED_PRICE = "50"; @Before - @Ignore // TODO Remove Ignore annotations when running locally with Local DynamoDB instance public void setup() throws Exception { try { @@ -57,19 +63,18 @@ public class ProductInfoRepositoryIntegrationTest { // Do nothing, table already created } - // TODO How to handle different environments. i.e. AVOID deleting all entries in ProductInfoion table + // TODO How to handle different environments. i.e. AVOID deleting all entries in ProductInfo on table dynamoDBMapper.batchDelete((List) repository.findAll()); } @Test - @Ignore // TODO Remove Ignore annotations when running locally with Local DynamoDB instance public void givenItemWithExpectedCost_whenRunFindAll_thenItemIsFound() { ProductInfo productInfo = new ProductInfo(EXPECTED_COST, EXPECTED_PRICE); repository.save(productInfo); List result = (List) repository.findAll(); - assertTrue("Not empty", result.size() > 0); - assertTrue("Contains item with expected cost", result.get(0).getCost().equals(EXPECTED_COST)); + assertThat(result.size(), is(greaterThan(0))); + assertThat(result.get(0).getCost(), is(equalTo(EXPECTED_COST))); } } diff --git a/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/rule/LocalDbCreationRule.java b/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/rule/LocalDbCreationRule.java new file mode 100644 index 0000000000..555d558b06 --- /dev/null +++ b/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/rule/LocalDbCreationRule.java @@ -0,0 +1,37 @@ +package com.baeldung.spring.data.dynamodb.repository.rule; + +import com.amazonaws.services.dynamodbv2.local.main.ServerRunner; +import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer; +import org.junit.rules.ExternalResource; + +import java.util.Optional; + +public class LocalDbCreationRule extends ExternalResource { + + protected DynamoDBProxyServer server; + + public LocalDbCreationRule() { + System.setProperty("sqlite4java.library.path", "native-libs"); + } + + @Override + protected void before() throws Exception { + String port = "8000"; + this.server = ServerRunner.createServerFromCommandLineArgs(new String[]{"-inMemory", "-port", port}); + server.start(); + } + + @Override + protected void after() { + this.stopUnchecked(server); + } + + protected void stopUnchecked(DynamoDBProxyServer dynamoDbServer) { + try { + dynamoDbServer.stop(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + +} diff --git a/persistence-modules/spring-data-redis/pom.xml b/persistence-modules/spring-data-redis/pom.xml index 6cb49f11cf..0b9075147d 100644 --- a/persistence-modules/spring-data-redis/pom.xml +++ b/persistence-modules/spring-data-redis/pom.xml @@ -16,11 +16,13 @@ UTF-8 - 4.3.7.RELEASE - 1.8.1.RELEASE + 5.0.3.RELEASE + 2.0.3.RELEASE 3.2.4 2.9.0 0.10.0 + 2.0.3.RELEASE + @@ -73,6 +75,12 @@ nosqlunit-redis ${nosqlunit.version} + + + org.springframework.data + spring-data-commons + ${spring-data-commons.version} + diff --git a/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/config/RedisConfig.java b/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/config/RedisConfig.java index 4fd83a2bb6..4ea8bb4bc0 100644 --- a/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/config/RedisConfig.java +++ b/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/config/RedisConfig.java @@ -1,8 +1,5 @@ package com.baeldung.spring.data.redis.config; -import com.baeldung.spring.data.redis.queue.MessagePublisher; -import com.baeldung.spring.data.redis.queue.RedisMessagePublisher; -import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @@ -11,10 +8,16 @@ import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.listener.ChannelTopic; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; +import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; import org.springframework.data.redis.serializer.GenericToStringSerializer; +import com.baeldung.spring.data.redis.queue.MessagePublisher; +import com.baeldung.spring.data.redis.queue.RedisMessagePublisher; +import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber; + @Configuration @ComponentScan("com.baeldung.spring.data.redis") +@EnableRedisRepositories(basePackages = "com.baeldung.spring.data.redis.repo") public class RedisConfig { @Bean diff --git a/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/model/Student.java b/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/model/Student.java index 10ba0f5ef4..b97ed23387 100644 --- a/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/model/Student.java +++ b/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/model/Student.java @@ -2,6 +2,9 @@ package com.baeldung.spring.data.redis.model; import java.io.Serializable; +import org.springframework.data.redis.core.RedisHash; + +@RedisHash("Student") public class Student implements Serializable { public enum Gender { diff --git a/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/repo/StudentRepository.java b/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/repo/StudentRepository.java index 250c227f00..39f13bb6a7 100644 --- a/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/repo/StudentRepository.java +++ b/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/repo/StudentRepository.java @@ -1,18 +1,9 @@ package com.baeldung.spring.data.redis.repo; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + import com.baeldung.spring.data.redis.model.Student; -import java.util.Map; - -public interface StudentRepository { - - void saveStudent(Student person); - - void updateStudent(Student student); - - Student findStudent(String id); - - Map findAllStudents(); - - void deleteStudent(String id); -} +@Repository +public interface StudentRepository extends CrudRepository {} diff --git a/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/repo/StudentRepositoryImpl.java b/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/repo/StudentRepositoryImpl.java deleted file mode 100644 index f13bef0f54..0000000000 --- a/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/repo/StudentRepositoryImpl.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.baeldung.spring.data.redis.repo; - -import com.baeldung.spring.data.redis.model.Student; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.redis.core.HashOperations; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.stereotype.Repository; - -import javax.annotation.PostConstruct; -import java.util.Map; - -@Repository -public class StudentRepositoryImpl implements StudentRepository { - - private static final String KEY = "Student"; - - private RedisTemplate redisTemplate; - private HashOperations hashOperations; - - @Autowired - public StudentRepositoryImpl(RedisTemplate redisTemplate) { - this.redisTemplate = redisTemplate; - } - - @PostConstruct - private void init() { - hashOperations = redisTemplate.opsForHash(); - } - - public void saveStudent(final Student student) { - hashOperations.put(KEY, student.getId(), student); - } - - public void updateStudent(final Student student) { - hashOperations.put(KEY, student.getId(), student); - } - - public Student findStudent(final String id) { - return (Student) hashOperations.get(KEY, id); - } - - public Map findAllStudents() { - return hashOperations.entries(KEY); - } - - public void deleteStudent(final String id) { - hashOperations.delete(KEY, id); - } -} diff --git a/persistence-modules/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/repo/StudentRepositoryIntegrationTest.java b/persistence-modules/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/repo/StudentRepositoryIntegrationTest.java index 1028c0bc24..66ef3c21b2 100644 --- a/persistence-modules/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/repo/StudentRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/repo/StudentRepositoryIntegrationTest.java @@ -1,17 +1,20 @@ package com.baeldung.spring.data.redis.repo; -import com.baeldung.spring.data.redis.config.RedisConfig; -import com.baeldung.spring.data.redis.model.Student; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import java.util.Map; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import com.baeldung.spring.data.redis.config.RedisConfig; +import com.baeldung.spring.data.redis.model.Student; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = RedisConfig.class) @@ -23,18 +26,18 @@ public class StudentRepositoryIntegrationTest { @Test public void whenSavingStudent_thenAvailableOnRetrieval() throws Exception { final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); - studentRepository.saveStudent(student); - final Student retrievedStudent = studentRepository.findStudent(student.getId()); + studentRepository.save(student); + final Student retrievedStudent = studentRepository.findById(student.getId()).get(); assertEquals(student.getId(), retrievedStudent.getId()); } @Test public void whenUpdatingStudent_thenAvailableOnRetrieval() throws Exception { final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); - studentRepository.saveStudent(student); + studentRepository.save(student); student.setName("Richard Watson"); - studentRepository.saveStudent(student); - final Student retrievedStudent = studentRepository.findStudent(student.getId()); + studentRepository.save(student); + final Student retrievedStudent = studentRepository.findById(student.getId()).get(); assertEquals(student.getName(), retrievedStudent.getName()); } @@ -42,18 +45,19 @@ public class StudentRepositoryIntegrationTest { public void whenSavingStudents_thenAllShouldAvailableOnRetrieval() throws Exception { final Student engStudent = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); final Student medStudent = new Student("Med2015001", "Gareth Houston", Student.Gender.MALE, 2); - studentRepository.saveStudent(engStudent); - studentRepository.saveStudent(medStudent); - final Map retrievedStudent = studentRepository.findAllStudents(); - assertEquals(retrievedStudent.size(), 2); + studentRepository.save(engStudent); + studentRepository.save(medStudent); + List students = new ArrayList<>(); + studentRepository.findAll().forEach(students::add); + assertEquals(students.size(), 2); } @Test public void whenDeletingStudent_thenNotAvailableOnRetrieval() throws Exception { final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); - studentRepository.saveStudent(student); - studentRepository.deleteStudent(student.getId()); - final Student retrievedStudent = studentRepository.findStudent(student.getId()); + studentRepository.save(student); + studentRepository.deleteById(student.getId()); + final Student retrievedStudent = studentRepository.findById(student.getId()).orElse(null); assertNull(retrievedStudent); } } \ No newline at end of file diff --git a/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/bootstrap/HibernateBootstrapIntegrationTest.java b/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/bootstrap/HibernateBootstrapIntegrationTest.java index ffe82b7ced..c41423643a 100644 --- a/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/bootstrap/HibernateBootstrapIntegrationTest.java +++ b/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/bootstrap/HibernateBootstrapIntegrationTest.java @@ -8,11 +8,16 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.annotation.Commit; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.AnnotationConfigContextLoader; +import org.springframework.test.context.transaction.TestTransaction; import org.springframework.transaction.annotation.Transactional; +import static junit.framework.TestCase.assertFalse; +import static junit.framework.TestCase.assertTrue; + @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { HibernateConf.class }) @Transactional @@ -35,4 +40,146 @@ public class HibernateBootstrapIntegrationTest { Assert.assertNotNull(searchEntity); } + @Test + public void whenProgrammaticTransactionCommit_thenEntityIsInDatabase() { + assertTrue(TestTransaction.isActive()); + + //Save an entity and commit. + Session session = sessionFactory.getCurrentSession(); + + TestEntity newEntity = new TestEntity(); + newEntity.setId(1); + session.save(newEntity); + + TestEntity searchEntity = session.find(TestEntity.class, 1); + + Assert.assertNotNull(searchEntity); + assertTrue(TestTransaction.isFlaggedForRollback()); + + TestTransaction.flagForCommit(); + TestTransaction.end(); + + assertFalse(TestTransaction.isFlaggedForRollback()); + assertFalse(TestTransaction.isActive()); + + //Check that the entity is still there in a new transaction, + //then delete it, but don't commit. + TestTransaction.start(); + + assertTrue(TestTransaction.isFlaggedForRollback()); + assertTrue(TestTransaction.isActive()); + + session = sessionFactory.getCurrentSession(); + searchEntity = session.find(TestEntity.class, 1); + + Assert.assertNotNull(searchEntity); + + session.delete(searchEntity); + session.flush(); + + TestTransaction.end(); + + assertFalse(TestTransaction.isActive()); + + //Check that the entity is still there in a new transaction, + //then delete it and commit. + TestTransaction.start(); + + session = sessionFactory.getCurrentSession(); + searchEntity = session.find(TestEntity.class, 1); + + Assert.assertNotNull(searchEntity); + + session.delete(searchEntity); + session.flush(); + + assertTrue(TestTransaction.isActive()); + + TestTransaction.flagForCommit(); + TestTransaction.end(); + + assertFalse(TestTransaction.isActive()); + + //Check that the entity is no longer there in a new transaction. + TestTransaction.start(); + + assertTrue(TestTransaction.isActive()); + + session = sessionFactory.getCurrentSession(); + searchEntity = session.find(TestEntity.class, 1); + + Assert.assertNull(searchEntity); + } + + @Test + @Commit + public void givenTransactionCommitDefault_whenProgrammaticTransactionCommit_thenEntityIsInDatabase() { + assertTrue(TestTransaction.isActive()); + + //Save an entity and commit. + Session session = sessionFactory.getCurrentSession(); + + TestEntity newEntity = new TestEntity(); + newEntity.setId(1); + session.save(newEntity); + + TestEntity searchEntity = session.find(TestEntity.class, 1); + + Assert.assertNotNull(searchEntity); + assertFalse(TestTransaction.isFlaggedForRollback()); + + TestTransaction.end(); + + assertFalse(TestTransaction.isFlaggedForRollback()); + assertFalse(TestTransaction.isActive()); + + //Check that the entity is still there in a new transaction, + //then delete it, but don't commit. + TestTransaction.start(); + + assertFalse(TestTransaction.isFlaggedForRollback()); + assertTrue(TestTransaction.isActive()); + + session = sessionFactory.getCurrentSession(); + searchEntity = session.find(TestEntity.class, 1); + + Assert.assertNotNull(searchEntity); + + session.delete(searchEntity); + session.flush(); + + TestTransaction.flagForRollback(); + TestTransaction.end(); + + assertFalse(TestTransaction.isActive()); + + //Check that the entity is still there in a new transaction, + //then delete it and commit. + TestTransaction.start(); + + session = sessionFactory.getCurrentSession(); + searchEntity = session.find(TestEntity.class, 1); + + Assert.assertNotNull(searchEntity); + + session.delete(searchEntity); + session.flush(); + + assertTrue(TestTransaction.isActive()); + + TestTransaction.end(); + + assertFalse(TestTransaction.isActive()); + + //Check that the entity is no longer there in a new transaction. + TestTransaction.start(); + + assertTrue(TestTransaction.isActive()); + + session = sessionFactory.getCurrentSession(); + searchEntity = session.find(TestEntity.class, 1); + + Assert.assertNull(searchEntity); + } + } diff --git a/persistence-modules/spring-jpa/pom.xml b/persistence-modules/spring-jpa/pom.xml index 04c64fafc3..bc0b2381f3 100644 --- a/persistence-modules/spring-jpa/pom.xml +++ b/persistence-modules/spring-jpa/pom.xml @@ -13,7 +13,7 @@ com.baeldung parent-modules 1.0.0-SNAPSHOT - ../ + ../../ diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/ManyStudentRepository.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/ManyStudentRepository.java new file mode 100644 index 0000000000..a03b2950a0 --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/ManyStudentRepository.java @@ -0,0 +1,10 @@ +package org.baeldung.inmemory.persistence.dao; + +import org.baeldung.inmemory.persistence.model.ManyStudent; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface ManyStudentRepository extends JpaRepository { + List findByManyTags_Name(String name); +} diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/ManyTagRepository.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/ManyTagRepository.java new file mode 100644 index 0000000000..b7d991de32 --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/ManyTagRepository.java @@ -0,0 +1,7 @@ +package org.baeldung.inmemory.persistence.dao; + +import org.baeldung.inmemory.persistence.model.ManyTag; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ManyTagRepository extends JpaRepository { +} diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java index bfcf6f5cdc..ffe1a68558 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java @@ -2,6 +2,23 @@ package org.baeldung.inmemory.persistence.dao; import org.baeldung.inmemory.persistence.model.Student; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +import java.util.List; public interface StudentRepository extends JpaRepository { + + @Query("SELECT s FROM Student s JOIN s.tags t WHERE t = LOWER(:tag)") + List retrieveByTag(@Param("tag") String tag); + + @Query("SELECT s FROM Student s JOIN s.tags t WHERE s.name = LOWER(:name) AND t = LOWER(:tag)") + List retrieveByNameFilterByTag(@Param("name") String name, @Param("tag") String tag); + + @Query("SELECT s FROM Student s JOIN s.skillTags t WHERE t.name = LOWER(:tagName) AND t.value > :tagValue") + List retrieveByNameFilterByMinimumSkillTag(@Param("tagName") String tagName, @Param("tagValue") int tagValue); + + @Query("SELECT s FROM Student s JOIN s.kvTags t WHERE t.key = LOWER(:key)") + List retrieveByKeyTag(@Param("key") String key); + } diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/KVTag.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/KVTag.java new file mode 100644 index 0000000000..ba0071e37b --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/KVTag.java @@ -0,0 +1,33 @@ +package org.baeldung.inmemory.persistence.model; + +import javax.persistence.Embeddable; + +@Embeddable +public class KVTag { + private String key; + private String value; + + public KVTag(){} + + public KVTag(String key, String value) { + super(); + this.key = key; + this.value = value; + } + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/LocationTag.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/LocationTag.java new file mode 100644 index 0000000000..071dc24806 --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/LocationTag.java @@ -0,0 +1,39 @@ +package org.baeldung.inmemory.persistence.model; + +import javax.persistence.Embeddable; + +@Embeddable +public class LocationTag { + private String name; + private int xPos; + private int yPos; + + public LocationTag(){} + + public LocationTag(String name, int xPos, int yPos) { + super(); + this.name = name; + this.xPos = xPos; + this.yPos = yPos; + } + + public String getName() { + return name; + } + + public int getxPos() { + return xPos; + } + + public void setxPos(int xPos) { + this.xPos = xPos; + } + + public int getyPos() { + return yPos; + } + + public void setyPos(int yPos) { + this.yPos = yPos; + } +} diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/ManyStudent.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/ManyStudent.java new file mode 100644 index 0000000000..98778b8f75 --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/ManyStudent.java @@ -0,0 +1,34 @@ +package org.baeldung.inmemory.persistence.model; + +import javax.persistence.*; +import java.util.HashSet; +import java.util.Set; + +@Entity +public class ManyStudent { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int id; + private String name; + + @ManyToMany(cascade = CascadeType.ALL) + @JoinTable(name = "manystudent_manytags", + joinColumns = @JoinColumn(name = "manystudent_id", referencedColumnName = "id"), + inverseJoinColumns = @JoinColumn(name = "manytag_id", referencedColumnName = "id")) + private Set manyTags = new HashSet<>(); + + public ManyStudent() {} + + public ManyStudent(String name) { + this.name = name; + } + + public Set getManyTags() { + return manyTags; + } + + public void setManyTags(Set manyTags) { + this.manyTags.addAll(manyTags); + } +} diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/ManyTag.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/ManyTag.java new file mode 100644 index 0000000000..96f9534d43 --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/ManyTag.java @@ -0,0 +1,39 @@ +package org.baeldung.inmemory.persistence.model; + +import javax.persistence.*; +import java.util.HashSet; +import java.util.Set; + +@Entity +public class ManyTag { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int id; + private String name; + + @ManyToMany(mappedBy = "manyTags") + private Set students = new HashSet<>(); + + public ManyTag() {} + + public ManyTag(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Set getStudents() { + return students; + } + + public void setStudents(Set students) { + this.students.addAll(students); + } +} diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/SkillTag.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/SkillTag.java new file mode 100644 index 0000000000..0300d83d50 --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/SkillTag.java @@ -0,0 +1,29 @@ +package org.baeldung.inmemory.persistence.model; + +import javax.persistence.Embeddable; + +@Embeddable +public class SkillTag { + private String name; + private int value; + + public SkillTag(){} + + public SkillTag(String name, int value) { + super(); + this.name = name; + this.value = value; + } + + public int getValue() { + return value; + } + + public void setValue(int value) { + this.value = value; + } + + public String getName() { + return name; + } +} diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java index b50fe9122e..07aa3ef9ef 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java @@ -1,5 +1,8 @@ package org.baeldung.inmemory.persistence.model; +import java.util.ArrayList; +import java.util.List; +import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.Id; @@ -10,6 +13,15 @@ public class Student { private long id; private String name; + @ElementCollection + private List tags = new ArrayList<>(); + + @ElementCollection + private List skillTags = new ArrayList<>(); + + @ElementCollection + private List kvTags = new ArrayList<>(); + public Student() { } @@ -35,4 +47,28 @@ public class Student { this.name = name; } + public List getTags() { + return tags; + } + + public void setTags(List tags) { + this.tags.addAll(tags); + } + + public List getSkillTags() { + return skillTags; + } + + public void setSkillTags(List skillTags) { + this.skillTags.addAll(skillTags); + } + + public List getKVTags() { + return this.kvTags; + } + + public void setKVTags(List kvTags) { + this.kvTags.addAll(kvTags); + } + } diff --git a/persistence-modules/spring-jpa/src/test/java/org/baeldung/dsrouting/DataSourceRoutingTestConfiguration.java b/persistence-modules/spring-jpa/src/test/java/org/baeldung/dsrouting/DataSourceRoutingTestConfiguration.java index dee9d58722..abda9d3ee3 100644 --- a/persistence-modules/spring-jpa/src/test/java/org/baeldung/dsrouting/DataSourceRoutingTestConfiguration.java +++ b/persistence-modules/spring-jpa/src/test/java/org/baeldung/dsrouting/DataSourceRoutingTestConfiguration.java @@ -7,7 +7,6 @@ import javax.sql.DataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; diff --git a/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/deletion/config/PersistenceJPAConfigDeletion.java b/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/deletion/config/PersistenceJPAConfigDeletion.java index 37388d1c51..09d118fedc 100644 --- a/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/deletion/config/PersistenceJPAConfigDeletion.java +++ b/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/deletion/config/PersistenceJPAConfigDeletion.java @@ -2,8 +2,6 @@ package org.baeldung.persistence.deletion.config; import org.baeldung.config.PersistenceJPAConfigL2Cache; -import java.util.Properties; - public class PersistenceJPAConfigDeletion extends PersistenceJPAConfigL2Cache { public PersistenceJPAConfigDeletion() { diff --git a/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/deletion/model/Baz.java b/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/deletion/model/Baz.java index 2dad3e6654..4fb3f8965e 100644 --- a/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/deletion/model/Baz.java +++ b/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/deletion/model/Baz.java @@ -1,7 +1,6 @@ package org.baeldung.persistence.deletion.model; import javax.persistence.*; -import java.util.List; @Entity @Table(name = "BAZ") diff --git a/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/AdvancedTaggingIntegrationTest.java b/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/AdvancedTaggingIntegrationTest.java new file mode 100644 index 0000000000..2e4f1a0e23 --- /dev/null +++ b/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/AdvancedTaggingIntegrationTest.java @@ -0,0 +1,81 @@ +package org.baeldung.persistence.repository; + +import org.baeldung.config.StudentJpaConfig; +import org.baeldung.inmemory.persistence.dao.ManyStudentRepository; +import org.baeldung.inmemory.persistence.dao.ManyTagRepository; +import org.baeldung.inmemory.persistence.dao.StudentRepository; +import org.baeldung.inmemory.persistence.model.KVTag; +import org.baeldung.inmemory.persistence.model.ManyStudent; +import org.baeldung.inmemory.persistence.model.ManyTag; +import org.baeldung.inmemory.persistence.model.SkillTag; +import org.baeldung.inmemory.persistence.model.Student; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.Resource; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { StudentJpaConfig.class }, loader = AnnotationConfigContextLoader.class) +@Transactional +public class AdvancedTaggingIntegrationTest { + @Resource + private StudentRepository studentRepository; + + @Resource + private ManyStudentRepository manyStudentRepository; + + @Resource + private ManyTagRepository manyTagRepository; + + @Test + public void givenStudentWithSkillTags_whenSave_thenGetByNameAndSkillTag() { + Student student = new Student(1, "Will"); + SkillTag skill1 = new SkillTag("java", 5); + student.setSkillTags(Arrays.asList(skill1)); + studentRepository.save(student); + + Student student2 = new Student(2, "Joe"); + SkillTag skill2 = new SkillTag("java", 1); + student2.setSkillTags(Arrays.asList(skill2)); + studentRepository.save(student2); + + List students = studentRepository.retrieveByNameFilterByMinimumSkillTag("java", 3); + assertEquals("size incorrect", 1, students.size()); + } + + @Test + public void givenStudentWithKVTags_whenSave_thenGetByTagOk(){ + Student student = new Student(0, "John"); + student.setKVTags(Arrays.asList(new KVTag("department", "computer science"))); + studentRepository.save(student); + + Student student2 = new Student(1, "James"); + student2.setKVTags(Arrays.asList(new KVTag("department", "humanities"))); + studentRepository.save(student2); + + List students = studentRepository.retrieveByKeyTag("department"); + assertEquals("size incorrect", 2, students.size()); + } + + @Test + public void givenStudentWithManyTags_whenSave_theyGetByTagOk() { + ManyTag tag = new ManyTag("full time"); + manyTagRepository.save(tag); + + ManyStudent student = new ManyStudent("John"); + student.setManyTags(Collections.singleton(tag)); + manyStudentRepository.save(student); + + List students = manyStudentRepository.findByManyTags_Name("full time"); + assertEquals("size incorrect", 1, students.size()); + } +} diff --git a/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java b/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java index 8380ab5434..28d7e3772c 100644 --- a/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java +++ b/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java @@ -12,6 +12,9 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; +import java.util.Arrays; +import java.util.List; + import static org.junit.Assert.assertEquals; @RunWith(SpringJUnit4ClassRunner.class) @@ -34,4 +37,46 @@ public class InMemoryDBIntegrationTest { assertEquals("name incorrect", NAME, student2.getName()); } + @Test + public void givenStudentWithTags_whenSave_thenGetByTagOk(){ + Student student = new Student(ID, NAME); + student.setTags(Arrays.asList("full time", "computer science")); + studentRepository.save(student); + + Student student2 = studentRepository.retrieveByTag("full time").get(0); + assertEquals("name incorrect", NAME, student2.getName()); + } + + @Test + public void givenMultipleStudentsWithTags_whenSave_thenGetByTagReturnsCorrectCount(){ + Student student = new Student(0, "Larry"); + student.setTags(Arrays.asList("full time", "computer science")); + studentRepository.save(student); + + Student student2 = new Student(1, "Curly"); + student2.setTags(Arrays.asList("part time", "rocket science")); + studentRepository.save(student2); + + Student student3 = new Student(2, "Moe"); + student3.setTags(Arrays.asList("full time", "philosophy")); + studentRepository.save(student3); + + Student student4 = new Student(3, "Shemp"); + student4.setTags(Arrays.asList("part time", "mathematics")); + studentRepository.save(student4); + + List students = studentRepository.retrieveByTag("full time"); + assertEquals("size incorrect", 2, students.size()); + } + + @Test + public void givenStudentWithTags_whenSave_thenGetByNameAndTagOk(){ + Student student = new Student(ID, NAME); + student.setTags(Arrays.asList("full time", "computer science")); + studentRepository.save(student); + + Student student2 = studentRepository.retrieveByNameFilterByTag("John", "full time").get(0); + assertEquals("name incorrect", NAME, student2.getName()); + } + } diff --git a/pom.xml b/pom.xml index 9bf12725c0..70a3bbc5b4 100644 --- a/pom.xml +++ b/pom.xml @@ -1,6 +1,5 @@ - + 4.0.0 com.baeldung parent-modules @@ -15,7 +14,7 @@ true false false - + 4.12 1.3 2.8.9 @@ -29,7 +28,7 @@ parent-boot-5 - asm + asm atomix apache-cayenne aws @@ -40,6 +39,8 @@ apache-fop apache-poi apache-thrift + apache-curator + apache-zookeeper autovalue axon bootique @@ -49,7 +50,9 @@ core-java core-java-io core-java-8 + core-groovy core-java-concurrency + couchbase deltaspike @@ -57,7 +60,7 @@ ethereumj - + feign @@ -65,6 +68,7 @@ geotools testing-modules/groovy-spock + google-cloud gson guava guava-modules/guava-18 @@ -76,22 +80,24 @@ handling-spring-static-resources hazelcast hbase - + httpclient hystrix - image-processing + immutables influxdb jackson - + vavr - java-lite - java-vavr-stream + java-lite + java-rmi + java-vavr-stream javax-servlets javaxval jaxb + jgroups jee-7 jjwt @@ -105,21 +111,21 @@ libraries libraries-data - linkrest + linkrest logging-modules/log-mdc logging-modules/log4j logging-modules/log4j2 logging-modules/logback lombok - + mapstruct - metrics + mesos-marathon testing-modules/mockito testing-modules/mockito-2 testing-modules/mocks mustache - mvn-wrapper + mvn-wrapper noexception orientdb osgi @@ -139,14 +145,13 @@ resteasy rxjava spring-swagger-codegen - testing-modules/selenium-junit-testng persistence-modules/solr spark-java - + spring-5 spring-5-reactive spring-5-mvc - spring-5-security + spring-activiti spring-akka spring-amqp @@ -156,7 +161,7 @@ spring-batch spring-bom spring-boot - spring-boot-keycloak + spring-boot-keycloak spring-boot-bootstrap spring-boot-admin spring-boot-security @@ -185,7 +190,7 @@ spring-integration spring-jenkins-pipeline spring-jersey - jmeter + spring-jms spring-jooq persistence-modules/spring-jpa @@ -243,9 +248,10 @@ spring-zuul spring-reactor spring-vertx - - spring-rest-embedded-tomcat - + spring-jinq + + spring-rest-embedded-tomcat + testing-modules/testing testing-modules/testng @@ -272,9 +278,12 @@ saas deeplearning4j lucene - vraptor + vraptor persistence-modules/java-cockroachdb - + persistence-modules/java-jdbi + jersey + java-spi + @@ -299,7 +308,7 @@ ${org.slf4j.version} - + junit junit @@ -377,4 +386,4 @@ - + \ No newline at end of file diff --git a/reactor-core/pom.xml b/reactor-core/pom.xml index 12f481c96f..d387471d56 100644 --- a/reactor-core/pom.xml +++ b/reactor-core/pom.xml @@ -26,11 +26,17 @@ ${assertj.version} test - + + + io.projectreactor + reactor-test + ${reactor-core.version} + test + - 3.0.5.RELEASE + 3.1.3.RELEASE 3.6.1 diff --git a/reactor-core/src/test/java/com/baeldung/reactor/core/CombiningPublishersTest.java b/reactor-core/src/test/java/com/baeldung/reactor/core/CombiningPublishersTest.java new file mode 100644 index 0000000000..41769b1b35 --- /dev/null +++ b/reactor-core/src/test/java/com/baeldung/reactor/core/CombiningPublishersTest.java @@ -0,0 +1,180 @@ +package com.baeldung.reactor.core; + +import java.time.Duration; + +import org.junit.Test; + +import reactor.core.publisher.Flux; +import reactor.test.StepVerifier; + +public class CombiningPublishersTest { + + private static Integer min = 1; + private static Integer max = 5; + + private static Flux evenNumbers = Flux.range(min, max).filter(x -> x % 2 == 0); + private static Flux oddNumbers = Flux.range(min, max).filter(x -> x % 2 > 0); + + @Test + public void givenFluxes_whenMergeDelayErrorIsInvoked_thenMergeDelayError() { + Flux fluxOfIntegers = Flux.mergeDelayError(1, + evenNumbers.delayElements(Duration.ofMillis(2000L)), + oddNumbers.delayElements(Duration.ofMillis(1000L))); + + StepVerifier.create(fluxOfIntegers) + .expectNext(1) + .expectNext(2) + .expectNext(3) + .expectNext(5) + .expectNext(4) + .expectComplete() + .verify(); + } + + + @Test + public void givenFluxes_whenMergeWithDelayedElementsIsInvoked_thenMergeWithDelayedElements() { + Flux fluxOfIntegers = Flux.merge( + evenNumbers.delayElements(Duration.ofMillis(2000L)), + oddNumbers.delayElements(Duration.ofMillis(1000L))); + + StepVerifier.create(fluxOfIntegers) + .expectNext(1) + .expectNext(2) + .expectNext(3) + .expectNext(5) + .expectNext(4) + .expectComplete() + .verify(); + } + + @Test + public void givenFluxes_whenConcatIsInvoked_thenConcat() { + Flux fluxOfIntegers = Flux.concat( + evenNumbers.delayElements(Duration.ofMillis(2000L)), + oddNumbers.delayElements(Duration.ofMillis(1000L))); + + StepVerifier.create(fluxOfIntegers) + .expectNext(2) + .expectNext(4) + .expectNext(1) + .expectNext(3) + .expectNext(5) + .expectComplete() + .verify(); + } + + @Test + public void givenFluxes_whenMergeIsInvoked_thenMerge() { + Flux fluxOfIntegers = Flux.merge( + evenNumbers, + oddNumbers); + + StepVerifier.create(fluxOfIntegers) + .expectNext(2) + .expectNext(4) + .expectNext(1) + .expectNext(3) + .expectNext(5) + .expectComplete() + .verify(); + } + + @Test + public void givenFluxes_whenConcatWithIsInvoked_thenConcatWith() { + Flux fluxOfIntegers = evenNumbers + .concatWith(oddNumbers); + + StepVerifier.create(fluxOfIntegers) + .expectNext(2) + .expectNext(4) + .expectNext(1) + .expectNext(3) + .expectNext(5) + .expectComplete() + .verify(); + } + + @Test + public void givenFluxes_whenCombineLatestIsInvoked_thenCombineLatest() { + Flux fluxOfIntegers = Flux.combineLatest( + evenNumbers, + oddNumbers, + (a, b) -> a + b); + + StepVerifier.create(fluxOfIntegers) + .expectNext(5) + .expectNext(7) + .expectNext(9) + .expectComplete() + .verify(); + + } + + @Test + public void givenFluxes_whenCombineLatestIsInvoked_thenCombineLatest1() { + StepVerifier.create(Flux.combineLatest(obj -> (int) obj[1], evenNumbers, oddNumbers)) + .expectNext(1) + .expectNext(3) + .expectNext(5) + .verifyComplete(); + } + + @Test + public void givenFluxes_whenMergeSequentialIsInvoked_thenMergeSequential() { + Flux fluxOfIntegers = Flux.mergeSequential( + evenNumbers, + oddNumbers); + + StepVerifier.create(fluxOfIntegers) + .expectNext(2) + .expectNext(4) + .expectNext(1) + .expectNext(3) + .expectNext(5) + .expectComplete() + .verify(); + } + + + @Test + public void givenFluxes_whenMergeWithIsInvoked_thenMergeWith() { + Flux fluxOfIntegers = evenNumbers.mergeWith(oddNumbers); + + StepVerifier.create(fluxOfIntegers) + .expectNext(2) + .expectNext(4) + .expectNext(1) + .expectNext(3) + .expectNext(5) + .expectComplete() + .verify(); + } + + @Test + public void givenFluxes_whenZipIsInvoked_thenZip() { + Flux fluxOfIntegers = Flux.zip( + evenNumbers, + oddNumbers, + (a, b) -> a + b); + + StepVerifier.create(fluxOfIntegers) + .expectNext(3) + .expectNext(7) + .expectComplete() + .verify(); + } + + @Test + public void givenFluxes_whenZipWithIsInvoked_thenZipWith() { + Flux fluxOfIntegers = evenNumbers + .zipWith(oddNumbers, + (a, b) -> a * b); + + StepVerifier.create(fluxOfIntegers) + .expectNext(2) + .expectNext(12) + .expectComplete() + .verify(); + } +} \ No newline at end of file diff --git a/rxjava/pom.xml b/rxjava/pom.xml index 0f950914ff..d88dfcaa9b 100644 --- a/rxjava/pom.xml +++ b/rxjava/pom.xml @@ -31,6 +31,19 @@ 1.0.0 + + io.reactivex + rxjava-string + 1.1.1 + + + + junit + junit + 4.12 + test + + com.jayway.awaitility awaitility diff --git a/rxjava/src/test/java/com/baeldung/rxjava/FlowableTest.java b/rxjava/src/test/java/com/baeldung/rxjava/FlowableTest.java new file mode 100644 index 0000000000..b9d1d64c24 --- /dev/null +++ b/rxjava/src/test/java/com/baeldung/rxjava/FlowableTest.java @@ -0,0 +1,93 @@ +package com.baeldung.rxjava; + +import io.reactivex.BackpressureStrategy; +import io.reactivex.Flowable; +import io.reactivex.FlowableOnSubscribe; +import io.reactivex.Observable; +import io.reactivex.exceptions.MissingBackpressureException; +import io.reactivex.schedulers.Schedulers; +import io.reactivex.subscribers.TestSubscriber; + +import org.junit.Test; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class FlowableTest { + + @Test public void whenFlowableIsCreated_thenItIsProperlyInitialized() { + Flowable integerFlowable = Flowable.just(1, 2, 3, 4); + assertNotNull(integerFlowable); + } + + @Test public void whenFlowableIsCreatedFromObservable_thenItIsProperlyInitialized() throws InterruptedException { + Observable integerObservable = Observable.just(1, 2, 3); + Flowable integerFlowable = integerObservable.toFlowable(BackpressureStrategy.BUFFER); + assertNotNull(integerFlowable); + + } + + @Test public void whenFlowableIsCreatedFromFlowableOnSubscribe_thenItIsProperlyInitialized() throws InterruptedException { + FlowableOnSubscribe flowableOnSubscribe = flowableEmitter -> flowableEmitter.onNext(1); + Flowable integerFlowable = Flowable.create(flowableOnSubscribe, BackpressureStrategy.BUFFER); + assertNotNull(integerFlowable); + } + + @Test public void thenAllValuesAreBufferedAndReceived() { + List testList = IntStream.range(0, 100000).boxed().collect(Collectors.toList()); + Observable observable = Observable.fromIterable(testList); + TestSubscriber testSubscriber = observable.toFlowable(BackpressureStrategy.BUFFER).observeOn(Schedulers.computation()).test(); + + testSubscriber.awaitTerminalEvent(); + + List receivedInts = testSubscriber.getEvents().get(0).stream().mapToInt(object -> (int) object).boxed().collect(Collectors.toList()); + + assertEquals(testList, receivedInts); + } + + @Test public void whenDropStrategyUsed_thenOnBackpressureDropped() { + List testList = IntStream.range(0, 100000).boxed().collect(Collectors.toList()); + + Observable observable = Observable.fromIterable(testList); + TestSubscriber testSubscriber = observable.toFlowable(BackpressureStrategy.DROP).observeOn(Schedulers.computation()).test(); + testSubscriber.awaitTerminalEvent(); + List receivedInts = testSubscriber.getEvents().get(0).stream().mapToInt(object -> (int) object).boxed().collect(Collectors.toList()); + + assertThat(receivedInts.size() < testList.size()); + assertThat(!receivedInts.contains(100000)); + } + + @Test public void whenMissingStrategyUsed_thenException() { + Observable observable = Observable.range(1, 100000); + TestSubscriber subscriber = observable.toFlowable(BackpressureStrategy.MISSING).observeOn(Schedulers.computation()).test(); + + subscriber.awaitTerminalEvent(); + subscriber.assertError(MissingBackpressureException.class); + } + + @Test public void whenErrorStrategyUsed_thenExceptionIsThrown() { + Observable observable = Observable.range(1, 100000); + TestSubscriber subscriber = observable.toFlowable(BackpressureStrategy.ERROR).observeOn(Schedulers.computation()).test(); + + subscriber.awaitTerminalEvent(); + subscriber.assertError(MissingBackpressureException.class); + } + + @Test public void whenLatestStrategyUsed_thenTheLastElementReceived() { + List testList = IntStream.range(0, 100000).boxed().collect(Collectors.toList()); + Observable observable = Observable.fromIterable(testList); + TestSubscriber testSubscriber = observable.toFlowable(BackpressureStrategy.LATEST).observeOn(Schedulers.computation()).test(); + + testSubscriber.awaitTerminalEvent(); + List receivedInts = testSubscriber.getEvents().get(0).stream().mapToInt(object -> (int) object).boxed().collect(Collectors.toList()); + + assertThat(receivedInts.size() < testList.size()); + assertThat(receivedInts.contains(100000)); + } + +} \ No newline at end of file diff --git a/rxjava/src/test/java/com/baeldung/rxjava/MaybeTest.java b/rxjava/src/test/java/com/baeldung/rxjava/MaybeTest.java new file mode 100644 index 0000000000..501ee1f196 --- /dev/null +++ b/rxjava/src/test/java/com/baeldung/rxjava/MaybeTest.java @@ -0,0 +1,40 @@ +package com.baeldung.rxjava; + +import org.junit.Test; + +import io.reactivex.Flowable; +import io.reactivex.Maybe; + +public class MaybeTest { + @Test + public void whenEmitsSingleValue_thenItIsObserved() { + Maybe maybe = Flowable.just(1, 2, 3, 4, 5) + .firstElement(); + + maybe.map(x -> x + 7) + .filter(x -> x > 0) + .test() + .assertResult(8) + .assertComplete(); + } + + @Test + public void whenEmitsNoValue_thenSignalsCompletionAndNoValueObserved() { + Maybe maybe = Flowable.just(1, 2, 3, 4, 5) + .skip(5) + .firstElement(); + + maybe.test() + .assertComplete() + .assertNoValues(); + } + + @Test + public void whenThrowsError_thenErrorIsRaised() { + Maybe maybe = Flowable. error(new Exception("msg")) + .firstElement(); + + maybe.test() + .assertErrorMessage("msg"); + } +} diff --git a/rxjava/src/test/java/com/baeldung/rxjava/combine/ObservableCombineUnitTest.java b/rxjava/src/test/java/com/baeldung/rxjava/combine/ObservableCombineUnitTest.java new file mode 100644 index 0000000000..693608d116 --- /dev/null +++ b/rxjava/src/test/java/com/baeldung/rxjava/combine/ObservableCombineUnitTest.java @@ -0,0 +1,121 @@ +package com.baeldung.rxjava.combine; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import rx.Observable; +import rx.observers.TestSubscriber; + +public class ObservableCombineUnitTest { + + private static ExecutorService executor; + + @BeforeClass + public static void setupClass() { + executor = Executors.newFixedThreadPool(10); + } + + @AfterClass + public static void tearDownClass() { + executor.shutdown(); + } + + @Test + public void givenTwoObservables_whenMerged_shouldEmitCombinedResults() { + List results = new ArrayList<>(); + + //@formatter:off + Observable.merge( + Observable.from(new String[] {"Hello", "World"}), + Observable.from(new String[]{ "I love", "RxJava"}) + ).subscribe(data -> { + results.add(data); + }); + //@formatter:on + + assertThat(results).isNotEmpty(); + assertThat(results.size()).isEqualTo(4); + assertThat(results).contains("Hello", "World", "I love", "RxJava"); + } + + @Test + public void givenAnObservable_whenStartWith_thenPrependEmittedResults() { + StringBuffer buffer = new StringBuffer(); + + //@formatter:off + Observable + .from(new String[] { "RxJava", "Observables" }) + .startWith("Buzzwords of Reactive Programming") + .subscribe(data -> { + buffer.append(data).append(" "); + }); + //@formatter:on + + assertThat(buffer.toString().trim()).isEqualTo("Buzzwords of Reactive Programming RxJava Observables"); + } + + @Test + public void givenTwoObservables_whenZipped_thenReturnCombinedResults() { + List zippedStrings = new ArrayList<>(); + + //@formatter:off + Observable.zip( + Observable.from(new String[] { "Simple", "Moderate", "Complex" }), + Observable.from(new String[] { "Solutions", "Success", "Heirarchy"}), + (str1, str2) -> { + return str1 + " " + str2; + }).subscribe(zipped -> { + zippedStrings.add(zipped); + }); + //formatter:on + + assertThat(zippedStrings).isNotEmpty(); + assertThat(zippedStrings.size()).isEqualTo(3); + assertThat(zippedStrings).contains("Simple Solutions", "Moderate Success", "Complex Heirarchy"); + } + + @Test + public void givenMutipleObservablesOneThrows_whenMerged_thenCombineBeforePropagatingError() { + Future f1 = executor.submit(createCallable("Hello")); + Future f2 = executor.submit(createCallable("World")); + Future f3 = executor.submit(createCallable(null)); + Future f4 = executor.submit(createCallable("RxJava")); + + TestSubscriber testSubscriber = new TestSubscriber<>(); + + //@formatter:off + Observable.mergeDelayError( + Observable.from(f1), + Observable.from(f2), + Observable.from(f3), + Observable.from(f4) + ).subscribe(testSubscriber); + //@formatter:on + + testSubscriber.assertValues("hello", "world", "rxjava"); + testSubscriber.assertError(ExecutionException.class); + } + + private Callable createCallable(final String data) { + return new Callable() { + @Override + public String call() throws Exception { + if (data == null) { + throw new IllegalArgumentException("Data should not be null."); + } + return data.toLowerCase(); + } + }; + } +} diff --git a/rxjava/src/test/java/com/baeldung/rxjava/operators/RxStringOperatorsTest.java b/rxjava/src/test/java/com/baeldung/rxjava/operators/RxStringOperatorsTest.java new file mode 100644 index 0000000000..5e58b32d8b --- /dev/null +++ b/rxjava/src/test/java/com/baeldung/rxjava/operators/RxStringOperatorsTest.java @@ -0,0 +1,145 @@ +package com.baeldung.rxjava.operators; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; + +import org.junit.Assert; +import org.junit.Test; + +import rx.Observable; +import rx.observables.StringObservable; +import rx.observers.TestSubscriber; + + +public class RxStringOperatorsTest +{ + + @Test + public void givenStringObservable_whenFromInputStream_ThenSuccessfull() + { + //given + ByteArrayInputStream is = new ByteArrayInputStream("Lorem ipsum loream, Lorem ipsum lore".getBytes(StandardCharsets.UTF_8)); + TestSubscriber subscriber = TestSubscriber.create(); + + // when + StringObservable.decode(StringObservable.from(is), StandardCharsets.UTF_8) + .subscribe(subscriber); + + // then + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(1); + subscriber.assertValues("Lorem ipsum loream, Lorem ipsum lore"); + } + @Test + public void givenStringObservable_whenEncodingString_ThenSuccessfullObtainingByteStream() + { + //given + Observable sourceObservable = Observable.just("Lorem ipsum loream"); + TestSubscriber subscriber = TestSubscriber.create(); + + // when + StringObservable.encode(sourceObservable, StandardCharsets.UTF_8) + .subscribe(subscriber); + + // then + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(1); + subscriber.getOnNextEvents() + .stream() + .forEach(bytes -> Assert.assertTrue(Arrays.equals(bytes, "Lorem ipsum loream".getBytes(StandardCharsets.UTF_8)))); + } + + @Test + public void givenStringObservable_whenConcatenatingStrings_ThenSuccessfullObtainingSingleString() + { + //given + Observable sourceObservable = Observable.just("Lorem ipsum loream","Lorem ipsum lore"); + TestSubscriber subscriber = TestSubscriber.create(); + + // when + StringObservable.stringConcat(sourceObservable) + .subscribe(subscriber); + + // then + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(1); + subscriber.assertValues("Lorem ipsum loreamLorem ipsum lore"); + } + + + @Test + public void givenStringObservable_whenDecodingByteArray_ThenSuccessfullObtainingStringStream() + { + //given + Observable sourceObservable = Observable.just("Lorem ipsum loream".getBytes(StandardCharsets.UTF_8)); + TestSubscriber subscriber = TestSubscriber.create(); + + // when + StringObservable.decode(sourceObservable, StandardCharsets.UTF_8) + .subscribe(subscriber); + + // then + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(1); + subscriber.assertValues("Lorem ipsum loream"); + } + + @Test + public void givenStringObservable_whenStringSplitted_ThenSuccessfullObtainingStringsStream() + { + //given + Observable sourceObservable = Observable.just("Lorem ipsum loream,Lorem ipsum lore"); + TestSubscriber subscriber = TestSubscriber.create(); + + // when + StringObservable.split(sourceObservable,",") + .subscribe(subscriber); + + // then + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(2); + subscriber.assertValues("Lorem ipsum loream", "Lorem ipsum lore"); + } + + @Test + public void givenStringObservable_whenSplittingByLine_ThenSuccessfullObtainingStringsStream() { + //given + Observable sourceObservable = Observable.just("Lorem ipsum loream\nLorem ipsum lore"); + TestSubscriber subscriber = TestSubscriber.create(); + + // when + StringObservable.byLine(sourceObservable) + .subscribe(subscriber); + + // then + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(2); + subscriber.assertValues("Lorem ipsum loream", "Lorem ipsum lore"); + } + + + @Test + public void givenStringObservable_whenJoiningStrings_ThenSuccessfullObtainingSingleString() { + //given + Observable sourceObservable = Observable.just("Lorem ipsum loream","Lorem ipsum lore"); + TestSubscriber subscriber = TestSubscriber.create(); + + // when + StringObservable.join(sourceObservable,",") + .subscribe(subscriber); + + // then + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(1); + subscriber.assertValues("Lorem ipsum loream,Lorem ipsum lore"); + } + +} diff --git a/spring-5-reactive-client/pom.xml b/spring-5-reactive-client/pom.xml index 22a027a58e..8e84c0f364 100644 --- a/spring-5-reactive-client/pom.xml +++ b/spring-5-reactive-client/pom.xml @@ -7,7 +7,7 @@ 0.0.1-SNAPSHOT jar - spring-5 + spring-5-reactive-client spring 5 sample project about new features diff --git a/spring-5-reactive/README.md b/spring-5-reactive/README.md index 400e343263..d8b9f89223 100644 --- a/spring-5-reactive/README.md +++ b/spring-5-reactive/README.md @@ -13,3 +13,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5](http://www.baeldung.com/spring-5-junit-config) - [Spring Security 5 for Reactive Applications](http://www.baeldung.com/spring-security-5-reactive) - [Spring 5 Testing with @EnabledIf Annotation](https://github.com/eugenp/tutorials/tree/master/spring-5) +- [Reactive WebSockets with Spring 5](http://www.baeldung.com/spring-5-reactive-websockets) diff --git a/spring-5-reactive/pom.xml b/spring-5-reactive/pom.xml index 36eaaa1530..abd8e42cf5 100644 --- a/spring-5-reactive/pom.xml +++ b/spring-5-reactive/pom.xml @@ -7,7 +7,7 @@ 0.0.1-SNAPSHOT jar - spring-5 + spring-5-reactive spring 5 sample project about new features @@ -60,7 +60,13 @@ - + + + org.projectlombok + lombok + compile + + org.apache.geronimo.specs geronimo-json_1.1_spec diff --git a/spring-reactive-websocket/src/main/java/com/baeldung/Event.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/websocket/Event.java similarity index 79% rename from spring-reactive-websocket/src/main/java/com/baeldung/Event.java rename to spring-5-reactive/src/main/java/com/baeldung/reactive/websocket/Event.java index 20d678c214..90f83a566f 100644 --- a/spring-reactive-websocket/src/main/java/com/baeldung/Event.java +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/websocket/Event.java @@ -1,4 +1,4 @@ -package com.baeldung; +package com.baeldung.reactive.websocket; import lombok.AllArgsConstructor; import lombok.Data; diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/websocket/ReactiveJavaClientWebSocket.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/websocket/ReactiveJavaClientWebSocket.java new file mode 100644 index 0000000000..c9a333c044 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/websocket/ReactiveJavaClientWebSocket.java @@ -0,0 +1,26 @@ +package com.baeldung.reactive.websocket; + +import java.net.URI; +import java.time.Duration; +import org.springframework.web.reactive.socket.WebSocketMessage; +import org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient; +import org.springframework.web.reactive.socket.client.WebSocketClient; + +import reactor.core.publisher.Mono; + +public class ReactiveJavaClientWebSocket { + public static void main(String[] args) throws InterruptedException { + + WebSocketClient client = new ReactorNettyWebSocketClient(); + client.execute( + URI.create("ws://localhost:8080/event-emitter"), + session -> session.send( + Mono.just(session.textMessage("event-spring-reactive-client-websocket"))) + .thenMany(session.receive() + .map(WebSocketMessage::getPayloadAsText) + .log()) + .then()) + .block(Duration.ofSeconds(10L)); + } + +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/websocket/ReactiveWebSocketApplication.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/websocket/ReactiveWebSocketApplication.java new file mode 100644 index 0000000000..43b5e50387 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/websocket/ReactiveWebSocketApplication.java @@ -0,0 +1,11 @@ +package com.baeldung.reactive.websocket; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ReactiveWebSocketApplication { + public static void main(String[] args) { + SpringApplication.run(ReactiveWebSocketApplication.class, args); + } +} diff --git a/spring-reactive-websocket/src/main/java/com/baeldung/ReactiveWebSocketConfiguration.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/websocket/ReactiveWebSocketConfiguration.java similarity index 96% rename from spring-reactive-websocket/src/main/java/com/baeldung/ReactiveWebSocketConfiguration.java rename to spring-5-reactive/src/main/java/com/baeldung/reactive/websocket/ReactiveWebSocketConfiguration.java index 6729e09273..974def5a91 100644 --- a/spring-reactive-websocket/src/main/java/com/baeldung/ReactiveWebSocketConfiguration.java +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/websocket/ReactiveWebSocketConfiguration.java @@ -1,4 +1,4 @@ -package com.baeldung; +package com.baeldung.reactive.websocket; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/websocket/ReactiveWebSocketHandler.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/websocket/ReactiveWebSocketHandler.java new file mode 100644 index 0000000000..2e93c0c0dc --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/websocket/ReactiveWebSocketHandler.java @@ -0,0 +1,41 @@ +package com.baeldung.reactive.websocket; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.stereotype.Component; +import org.springframework.web.reactive.socket.WebSocketHandler; +import org.springframework.web.reactive.socket.WebSocketMessage; +import org.springframework.web.reactive.socket.WebSocketSession; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import java.time.Duration; + +import static java.time.LocalDateTime.now; +import static java.util.UUID.randomUUID; + +@Component +public class ReactiveWebSocketHandler implements WebSocketHandler { + + private static final ObjectMapper json = new ObjectMapper(); + + private Flux eventFlux = Flux.generate(sink -> { + Event event = new Event(randomUUID().toString(), now().toString()); + try { + sink.next(json.writeValueAsString(event)); + } catch (JsonProcessingException e) { + sink.error(e); + } + }); + + private Flux intervalFlux = Flux.interval(Duration.ofMillis(1000L)) + .zipWith(eventFlux, (time, event) -> event); + + @Override + public Mono handle(WebSocketSession webSocketSession) { + return webSocketSession.send(intervalFlux + .map(webSocketSession::textMessage)) + .and(webSocketSession.receive() + .map(WebSocketMessage::getPayloadAsText).log()); + } +} diff --git a/spring-reactive-websocket/src/main/resources/static/client-websocket.html b/spring-5-reactive/src/main/resources/static/client-websocket.html similarity index 100% rename from spring-reactive-websocket/src/main/resources/static/client-websocket.html rename to spring-5-reactive/src/main/resources/static/client-websocket.html diff --git a/spring-5-security/README.md b/spring-5-security/README.md new file mode 100644 index 0000000000..d6573ef8b0 --- /dev/null +++ b/spring-5-security/README.md @@ -0,0 +1,4 @@ +## Relevant articles: + +- [Spring Security 5 -OAuth2 Login](http://www.baeldung.com/spring-security-5-oauth2-login) +- [Extra Login Fields with Spring Security](https://github.com/eugenp/tutorials/tree/master/spring-5-security) diff --git a/spring-5-security/pom.xml b/spring-5-security/pom.xml index c0f73b1bdd..ffe6865704 100644 --- a/spring-5-security/pom.xml +++ b/spring-5-security/pom.xml @@ -12,7 +12,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.M7 + 2.0.0.RC2 @@ -30,6 +30,10 @@ org.springframework.boot spring-boot-starter-thymeleaf + + org.thymeleaf.extras + thymeleaf-extras-springsecurity4 + @@ -40,6 +44,21 @@ org.springframework.security spring-security-oauth2-jose + + + org.springframework + spring-test + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.security + spring-security-test + test + diff --git a/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingConfigurer.java b/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingConfigurer.java new file mode 100644 index 0000000000..5a9479b664 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingConfigurer.java @@ -0,0 +1,32 @@ +package com.baeldung.dsl; + +import java.util.List; + +import org.springframework.http.HttpStatus; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; +import org.springframework.security.web.access.intercept.FilterSecurityInterceptor; + +public class ClientErrorLoggingConfigurer extends AbstractHttpConfigurer { + + private List errorCodes; + + public ClientErrorLoggingConfigurer(List errorCodes) { + this.errorCodes = errorCodes; + } + + public ClientErrorLoggingConfigurer() { + + } + + @Override + public void init(HttpSecurity http) throws Exception { + // initialization code + } + + @Override + public void configure(HttpSecurity http) throws Exception { + http.addFilterAfter(new ClientErrorLoggingFilter(errorCodes), FilterSecurityInterceptor.class); + } + +} diff --git a/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingFilter.java b/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingFilter.java new file mode 100644 index 0000000000..56f7cea3b8 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingFilter.java @@ -0,0 +1,54 @@ +package com.baeldung.dsl; + +import java.io.IOException; +import java.util.List; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletResponse; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.http.HttpStatus; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.web.filter.GenericFilterBean; + +public class ClientErrorLoggingFilter extends GenericFilterBean { + + private static final Logger logger = LogManager.getLogger(ClientErrorLoggingFilter.class); + + private List errorCodes; + + public ClientErrorLoggingFilter(List errorCodes) { + this.errorCodes = errorCodes; + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + + Authentication auth = SecurityContextHolder.getContext() + .getAuthentication(); + + if (auth != null) { + int status = ((HttpServletResponse) response).getStatus(); + if (status >= 400 && status < 500) { + if (errorCodes == null) { + logger.debug("User " + auth.getName() + " encountered error " + status); + } else { + if (errorCodes.stream() + .filter(s -> s.value() == status) + .findFirst() + .isPresent()) { + logger.debug("User " + auth.getName() + " encountered error " + status); + } + } + } + } + + chain.doFilter(request, response); + } + +} diff --git a/spring-5-security/src/main/java/com/baeldung/dsl/CustomConfigurerApplication.java b/spring-5-security/src/main/java/com/baeldung/dsl/CustomConfigurerApplication.java new file mode 100644 index 0000000000..2cd3d7fc7f --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/dsl/CustomConfigurerApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.dsl; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class CustomConfigurerApplication { + + public static void main(String[] args) { + SpringApplication.run(CustomConfigurerApplication.class, args); + } + +} diff --git a/spring-5-security/src/main/java/com/baeldung/dsl/MyController.java b/spring-5-security/src/main/java/com/baeldung/dsl/MyController.java new file mode 100644 index 0000000000..c69046afb5 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/dsl/MyController.java @@ -0,0 +1,14 @@ +package com.baeldung.dsl; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class MyController { + + @GetMapping("/admin") + public String getAdminPage() { + return "Hello Admin"; + } + +} diff --git a/spring-5-security/src/main/java/com/baeldung/dsl/SecurityConfig.java b/spring-5-security/src/main/java/com/baeldung/dsl/SecurityConfig.java new file mode 100644 index 0000000000..382e222f64 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/dsl/SecurityConfig.java @@ -0,0 +1,50 @@ +package com.baeldung.dsl; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; + +@Configuration +public class SecurityConfig extends WebSecurityConfigurerAdapter { + + @Override + protected void configure(HttpSecurity http) throws Exception { + http.authorizeRequests() + .antMatchers("/admin*") + .hasAnyRole("ADMIN") + .anyRequest() + .authenticated() + .and() + .formLogin() + .and() + .apply(clientErrorLogging()); + } + + @Bean + public ClientErrorLoggingConfigurer clientErrorLogging() { + return new ClientErrorLoggingConfigurer(); + } + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.inMemoryAuthentication() + .passwordEncoder(passwordEncoder()) + .withUser("user1") + .password(passwordEncoder().encode("user")) + .roles("USER") + .and() + .withUser("admin") + .password(passwordEncoder().encode("admin")) + .roles("ADMIN"); + } + + @Bean + public PasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } + +} diff --git a/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/CustomAuthenticationFilter.java b/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/CustomAuthenticationFilter.java new file mode 100644 index 0000000000..2a5c5f0368 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/CustomAuthenticationFilter.java @@ -0,0 +1,50 @@ +package com.baeldung.loginextrafieldscustom; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.security.authentication.AuthenticationServiceException; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; + +public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFilter { + + public static final String SPRING_SECURITY_FORM_DOMAIN_KEY = "domain"; + + @Override + public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) + throws AuthenticationException { + + if (!request.getMethod().equals("POST")) { + throw new AuthenticationServiceException("Authentication method not supported: " + + request.getMethod()); + } + + CustomAuthenticationToken authRequest = getAuthRequest(request); + setDetails(request, authRequest); + return this.getAuthenticationManager().authenticate(authRequest); + } + + private CustomAuthenticationToken getAuthRequest(HttpServletRequest request) { + String username = obtainUsername(request); + String password = obtainPassword(request); + String domain = obtainDomain(request); + + if (username == null) { + username = ""; + } + if (password == null) { + password = ""; + } + if (domain == null) { + domain = ""; + } + + return new CustomAuthenticationToken(username, password, domain); + } + + private String obtainDomain(HttpServletRequest request) { + return request.getParameter(SPRING_SECURITY_FORM_DOMAIN_KEY); + } +} diff --git a/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/CustomAuthenticationToken.java b/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/CustomAuthenticationToken.java new file mode 100644 index 0000000000..50995169a1 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/CustomAuthenticationToken.java @@ -0,0 +1,28 @@ +package com.baeldung.loginextrafieldscustom; + +import java.util.Collection; + +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.GrantedAuthority; + +public class CustomAuthenticationToken extends UsernamePasswordAuthenticationToken { + + private String domain; + + public CustomAuthenticationToken(Object principal, Object credentials, String domain) { + super(principal, credentials); + this.domain = domain; + super.setAuthenticated(false); + } + + public CustomAuthenticationToken(Object principal, Object credentials, String domain, + Collection authorities) { + super(principal, credentials, authorities); + this.domain = domain; + super.setAuthenticated(true); // must use super, as we override + } + + public String getDomain() { + return this.domain; + } +} diff --git a/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/CustomUserDetailsAuthenticationProvider.java b/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/CustomUserDetailsAuthenticationProvider.java new file mode 100644 index 0000000000..693900d843 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/CustomUserDetailsAuthenticationProvider.java @@ -0,0 +1,92 @@ +package com.baeldung.loginextrafieldscustom; + +import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.security.authentication.InternalAuthenticationServiceException; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.util.Assert; + +public class CustomUserDetailsAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider { + + /** + * The plaintext password used to perform + * PasswordEncoder#matches(CharSequence, String)} on when the user is + * not found to avoid SEC-2056. + */ + private static final String USER_NOT_FOUND_PASSWORD = "userNotFoundPassword"; + + private PasswordEncoder passwordEncoder; + private CustomUserDetailsService userDetailsService; + + /** + * The password used to perform + * {@link PasswordEncoder#matches(CharSequence, String)} on when the user is + * not found to avoid SEC-2056. This is necessary, because some + * {@link PasswordEncoder} implementations will short circuit if the password is not + * in a valid format. + */ + private String userNotFoundEncodedPassword; + + public CustomUserDetailsAuthenticationProvider(PasswordEncoder passwordEncoder, CustomUserDetailsService userDetailsService) { + this.passwordEncoder = passwordEncoder; + this.userDetailsService = userDetailsService; + } + + @Override + protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) + throws AuthenticationException { + + if (authentication.getCredentials() == null) { + logger.debug("Authentication failed: no credentials provided"); + throw new BadCredentialsException( + messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials")); + } + + String presentedPassword = authentication.getCredentials() + .toString(); + + if (!passwordEncoder.matches(presentedPassword, userDetails.getPassword())) { + logger.debug("Authentication failed: password does not match stored value"); + throw new BadCredentialsException( + messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials")); + } + } + + @Override + protected void doAfterPropertiesSet() throws Exception { + Assert.notNull(this.userDetailsService, "A UserDetailsService must be set"); + this.userNotFoundEncodedPassword = this.passwordEncoder.encode(USER_NOT_FOUND_PASSWORD); + } + + @Override + protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) + throws AuthenticationException { + CustomAuthenticationToken auth = (CustomAuthenticationToken) authentication; + UserDetails loadedUser; + + try { + loadedUser = this.userDetailsService.loadUserByUsernameAndDomain(auth.getPrincipal() + .toString(), auth.getDomain()); + } catch (UsernameNotFoundException notFound) { + if (authentication.getCredentials() != null) { + String presentedPassword = authentication.getCredentials() + .toString(); + passwordEncoder.matches(presentedPassword, userNotFoundEncodedPassword); + } + throw notFound; + } catch (Exception repositoryProblem) { + throw new InternalAuthenticationServiceException(repositoryProblem.getMessage(), repositoryProblem); + } + + if (loadedUser == null) { + throw new InternalAuthenticationServiceException("UserDetailsService returned null, " + + "which is an interface contract violation"); + } + return loadedUser; + } + +} diff --git a/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/CustomUserDetailsService.java b/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/CustomUserDetailsService.java new file mode 100644 index 0000000000..358129173d --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/CustomUserDetailsService.java @@ -0,0 +1,10 @@ +package com.baeldung.loginextrafieldscustom; + +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UsernameNotFoundException; + +public interface CustomUserDetailsService { + + UserDetails loadUserByUsernameAndDomain(String username, String domain) throws UsernameNotFoundException; + +} diff --git a/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/CustomUserDetailsServiceImpl.java b/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/CustomUserDetailsServiceImpl.java new file mode 100644 index 0000000000..ea979e2fab --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/CustomUserDetailsServiceImpl.java @@ -0,0 +1,30 @@ +package com.baeldung.loginextrafieldscustom; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.stereotype.Service; + +@Service("userDetailsService") +public class CustomUserDetailsServiceImpl implements CustomUserDetailsService { + + private UserRepository userRepository; + + public CustomUserDetailsServiceImpl(UserRepository userRepository) { + this.userRepository = userRepository; + } + + @Override + public UserDetails loadUserByUsernameAndDomain(String username, String domain) throws UsernameNotFoundException { + if (StringUtils.isAnyBlank(username, domain)) { + throw new UsernameNotFoundException("Username and domain must be provided"); + } + User user = userRepository.findUser(username, domain); + if (user == null) { + throw new UsernameNotFoundException( + String.format("Username not found for domain, username=%s, domain=%s", + username, domain)); + } + return user; + } +} diff --git a/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/CustomUserRepository.java b/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/CustomUserRepository.java new file mode 100644 index 0000000000..428c8bf532 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/CustomUserRepository.java @@ -0,0 +1,26 @@ +package com.baeldung.loginextrafieldscustom; + +import java.util.ArrayList; +import java.util.Collection; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.stereotype.Repository; + +@Repository("userRepository") +public class CustomUserRepository implements UserRepository { + + @Override + public User findUser(String username, String domain) { + if (StringUtils.isAnyBlank(username, domain)) { + return null; + } else { + Collection authorities = new ArrayList<>(); + User user = new User(username, domain, + "$2a$10$U3GhSMpsMSOE8Kqsbn58/edxDBKlVuYMh7qk/7ErApYFjJzi2VG5K", true, + true, true, true, authorities); + return user; + } + } + +} diff --git a/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/ExtraLoginFieldsApplication.java b/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/ExtraLoginFieldsApplication.java new file mode 100644 index 0000000000..0cf934f288 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/ExtraLoginFieldsApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.loginextrafieldscustom; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ExtraLoginFieldsApplication { + + public static void main(String[] args) { + SpringApplication.run(ExtraLoginFieldsApplication.class, args); + } + +} diff --git a/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/SecurityConfig.java b/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/SecurityConfig.java new file mode 100644 index 0000000000..def85ab978 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/SecurityConfig.java @@ -0,0 +1,62 @@ +package com.baeldung.loginextrafieldscustom; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.PropertySource; +import org.springframework.security.authentication.AuthenticationProvider; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler; +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; + +@EnableWebSecurity +@PropertySource("classpath:/application-extrafields.properties") +public class SecurityConfig extends WebSecurityConfigurerAdapter { + + @Autowired + private CustomUserDetailsService userDetailsService; + + @Override + protected void configure(HttpSecurity http) throws Exception { + + http + .addFilterBefore(authenticationFilter(), UsernamePasswordAuthenticationFilter.class) + .authorizeRequests() + .antMatchers("/css/**", "/index").permitAll() + .antMatchers("/user/**").authenticated() + .and() + .formLogin().loginPage("/login") + .and() + .logout() + .logoutUrl("/logout"); + } + + public CustomAuthenticationFilter authenticationFilter() throws Exception { + CustomAuthenticationFilter filter = new CustomAuthenticationFilter(); + filter.setAuthenticationManager(authenticationManagerBean()); + filter.setAuthenticationFailureHandler(failureHandler()); + return filter; + } + + @Autowired + public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { + auth.authenticationProvider(authProvider()); + } + + public AuthenticationProvider authProvider() { + CustomUserDetailsAuthenticationProvider provider + = new CustomUserDetailsAuthenticationProvider(passwordEncoder(), userDetailsService); + return provider; + } + + public SimpleUrlAuthenticationFailureHandler failureHandler() { + return new SimpleUrlAuthenticationFailureHandler("/login?error=true"); + } + + public PasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } +} diff --git a/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/User.java b/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/User.java new file mode 100644 index 0000000000..aa03f15b6a --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/User.java @@ -0,0 +1,23 @@ +package com.baeldung.loginextrafieldscustom; + +import java.util.Collection; + +import org.springframework.security.core.GrantedAuthority; + +public class User extends org.springframework.security.core.userdetails.User { + + private static final long serialVersionUID = 1L; + + private String domain; + + public User(String username, String domain, String password, boolean enabled, + boolean accountNonExpired, boolean credentialsNonExpired, + boolean accountNonLocked, Collection authorities) { + super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities); + this.domain = domain; + } + + public String getDomain() { + return domain; + } +} diff --git a/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/UserRepository.java b/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/UserRepository.java new file mode 100644 index 0000000000..e2358e055b --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/UserRepository.java @@ -0,0 +1,7 @@ +package com.baeldung.loginextrafieldscustom; + +public interface UserRepository { + + public User findUser(String username, String domain); + +} diff --git a/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/WebController.java b/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/WebController.java new file mode 100644 index 0000000000..b5e0b511ac --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/WebController.java @@ -0,0 +1,51 @@ +package com.baeldung.loginextrafieldscustom; + +import java.util.Optional; + +import org.springframework.security.authentication.AnonymousAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class WebController { + + @RequestMapping("/") + public String root() { + return "redirect:/index"; + } + + @RequestMapping("/index") + public String index(Model model) { + getDomain().ifPresent(d -> { + model.addAttribute("domain", d); + }); + return "index"; + } + + @RequestMapping("/user/index") + public String userIndex(Model model) { + getDomain().ifPresent(d -> { + model.addAttribute("domain", d); + }); + return "user/index"; + } + + @RequestMapping("/login") + public String login() { + return "login"; + } + + private Optional getDomain() { + Authentication auth = SecurityContextHolder.getContext() + .getAuthentication(); + String domain = null; + if (auth != null && !auth.getClass().equals(AnonymousAuthenticationToken.class)) { + User user = (User) auth.getPrincipal(); + domain = user.getDomain(); + } + return Optional.ofNullable(domain); + } +} diff --git a/spring-5-security/src/main/java/com/baeldung/loginextrafieldssimple/ExtraLoginFieldsApplication.java b/spring-5-security/src/main/java/com/baeldung/loginextrafieldssimple/ExtraLoginFieldsApplication.java new file mode 100644 index 0000000000..c82a13de1a --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/loginextrafieldssimple/ExtraLoginFieldsApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.loginextrafieldssimple; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ExtraLoginFieldsApplication { + + public static void main(String[] args) { + SpringApplication.run(ExtraLoginFieldsApplication.class, args); + } + +} diff --git a/spring-5-security/src/main/java/com/baeldung/loginextrafieldssimple/SecurityConfig.java b/spring-5-security/src/main/java/com/baeldung/loginextrafieldssimple/SecurityConfig.java new file mode 100644 index 0000000000..d8c5ea8147 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/loginextrafieldssimple/SecurityConfig.java @@ -0,0 +1,65 @@ +package com.baeldung.loginextrafieldssimple; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.PropertySource; +import org.springframework.security.authentication.AuthenticationProvider; +import org.springframework.security.authentication.dao.DaoAuthenticationProvider; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler; +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; + +@EnableWebSecurity +@PropertySource("classpath:/application-extrafields.properties") +public class SecurityConfig extends WebSecurityConfigurerAdapter { + + @Autowired + private UserDetailsService userDetailsService; + + @Override + protected void configure(HttpSecurity http) throws Exception { + + http + .addFilterBefore(authenticationFilter(), UsernamePasswordAuthenticationFilter.class) + .authorizeRequests() + .antMatchers("/css/**", "/index").permitAll() + .antMatchers("/user/**").authenticated() + .and() + .formLogin().loginPage("/login") + .and() + .logout() + .logoutUrl("/logout"); + } + + public SimpleAuthenticationFilter authenticationFilter() throws Exception { + SimpleAuthenticationFilter filter = new SimpleAuthenticationFilter(); + filter.setAuthenticationManager(authenticationManagerBean()); + filter.setAuthenticationFailureHandler(failureHandler()); + return filter; + } + + @Autowired + public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { + auth.authenticationProvider(authProvider()); + } + + public AuthenticationProvider authProvider() { + DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); + provider.setUserDetailsService(userDetailsService); + provider.setPasswordEncoder(passwordEncoder()); + return provider; + } + + public SimpleUrlAuthenticationFailureHandler failureHandler() { + return new SimpleUrlAuthenticationFailureHandler("/login?error=true"); + } + + public PasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } +} diff --git a/spring-5-security/src/main/java/com/baeldung/loginextrafieldssimple/SimpleAuthenticationFilter.java b/spring-5-security/src/main/java/com/baeldung/loginextrafieldssimple/SimpleAuthenticationFilter.java new file mode 100644 index 0000000000..9dcb524157 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/loginextrafieldssimple/SimpleAuthenticationFilter.java @@ -0,0 +1,54 @@ +package com.baeldung.loginextrafieldssimple; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.security.authentication.AuthenticationServiceException; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; + +public class SimpleAuthenticationFilter extends UsernamePasswordAuthenticationFilter { + + public static final String SPRING_SECURITY_FORM_DOMAIN_KEY = "domain"; + + @Override + public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) + throws AuthenticationException { + + if (!request.getMethod() + .equals("POST")) { + throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod()); + } + + UsernamePasswordAuthenticationToken authRequest = getAuthRequest(request); + setDetails(request, authRequest); + return this.getAuthenticationManager() + .authenticate(authRequest); + } + + private UsernamePasswordAuthenticationToken getAuthRequest(HttpServletRequest request) { + String username = obtainUsername(request); + String password = obtainPassword(request); + String domain = obtainDomain(request); + + if (username == null) { + username = ""; + } + if (password == null) { + password = ""; + } + if (domain == null) { + domain = ""; + } + + String usernameDomain = String.format("%s%s%s", username.trim(), + String.valueOf(Character.LINE_SEPARATOR), domain); + return new UsernamePasswordAuthenticationToken(usernameDomain, password); + } + + private String obtainDomain(HttpServletRequest request) { + return request.getParameter(SPRING_SECURITY_FORM_DOMAIN_KEY); + } +} diff --git a/spring-5-security/src/main/java/com/baeldung/loginextrafieldssimple/SimpleUserDetailsService.java b/spring-5-security/src/main/java/com/baeldung/loginextrafieldssimple/SimpleUserDetailsService.java new file mode 100644 index 0000000000..2fad50ad01 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/loginextrafieldssimple/SimpleUserDetailsService.java @@ -0,0 +1,32 @@ +package com.baeldung.loginextrafieldssimple; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.stereotype.Service; + +@Service("userDetailsService") +public class SimpleUserDetailsService implements UserDetailsService { + + private UserRepository userRepository; + + public SimpleUserDetailsService(UserRepository userRepository) { + this.userRepository = userRepository; + } + + @Override + public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { + String[] usernameAndDomain = StringUtils.split(username, String.valueOf(Character.LINE_SEPARATOR)); + if (usernameAndDomain == null || usernameAndDomain.length != 2) { + throw new UsernameNotFoundException("Username and domain must be provided"); + } + User user = userRepository.findUser(usernameAndDomain[0], usernameAndDomain[1]); + if (user == null) { + throw new UsernameNotFoundException( + String.format("Username not found for domain, username=%s, domain=%s", + usernameAndDomain[0], usernameAndDomain[1])); + } + return user; + } +} diff --git a/spring-5-security/src/main/java/com/baeldung/loginextrafieldssimple/SimpleUserRepository.java b/spring-5-security/src/main/java/com/baeldung/loginextrafieldssimple/SimpleUserRepository.java new file mode 100644 index 0000000000..e8aaa774a1 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/loginextrafieldssimple/SimpleUserRepository.java @@ -0,0 +1,26 @@ +package com.baeldung.loginextrafieldssimple; + +import java.util.ArrayList; +import java.util.Collection; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.stereotype.Repository; + +@Repository("userRepository") +public class SimpleUserRepository implements UserRepository { + + @Override + public User findUser(String username, String domain) { + if (StringUtils.isAnyBlank(username, domain)) { + return null; + } else { + Collection authorities = new ArrayList<>(); + User user = new User(username, domain, + "$2a$10$U3GhSMpsMSOE8Kqsbn58/edxDBKlVuYMh7qk/7ErApYFjJzi2VG5K", true, + true, true, true, authorities); + return user; + } + } + +} diff --git a/spring-5-security/src/main/java/com/baeldung/loginextrafieldssimple/User.java b/spring-5-security/src/main/java/com/baeldung/loginextrafieldssimple/User.java new file mode 100644 index 0000000000..b76da65638 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/loginextrafieldssimple/User.java @@ -0,0 +1,21 @@ +package com.baeldung.loginextrafieldssimple; + +import java.util.Collection; + +import org.springframework.security.core.GrantedAuthority; + +public class User extends org.springframework.security.core.userdetails.User { + + private String domain; + + public User(String username, String domain, String password, boolean enabled, + boolean accountNonExpired, boolean credentialsNonExpired, + boolean accountNonLocked, Collection authorities) { + super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities); + this.domain = domain; + } + + public String getDomain() { + return domain; + } +} diff --git a/spring-5-security/src/main/java/com/baeldung/loginextrafieldssimple/UserRepository.java b/spring-5-security/src/main/java/com/baeldung/loginextrafieldssimple/UserRepository.java new file mode 100644 index 0000000000..919e611b9c --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/loginextrafieldssimple/UserRepository.java @@ -0,0 +1,7 @@ +package com.baeldung.loginextrafieldssimple; + +public interface UserRepository { + + public User findUser(String username, String domain); + +} diff --git a/spring-5-security/src/main/java/com/baeldung/loginextrafieldssimple/WebController.java b/spring-5-security/src/main/java/com/baeldung/loginextrafieldssimple/WebController.java new file mode 100644 index 0000000000..1b17de7bec --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/loginextrafieldssimple/WebController.java @@ -0,0 +1,51 @@ +package com.baeldung.loginextrafieldssimple; + +import java.util.Optional; + +import org.springframework.security.authentication.AnonymousAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class WebController { + + @RequestMapping("/") + public String root() { + return "redirect:/index"; + } + + @RequestMapping("/index") + public String index(Model model) { + getDomain().ifPresent(d -> { + model.addAttribute("domain", d); + }); + return "index"; + } + + @RequestMapping("/user/index") + public String userIndex(Model model) { + getDomain().ifPresent(d -> { + model.addAttribute("domain", d); + }); + return "user/index"; + } + + @RequestMapping("/login") + public String login() { + return "login"; + } + + private Optional getDomain() { + Authentication auth = SecurityContextHolder.getContext() + .getAuthentication(); + String domain = null; + if (auth != null && !auth.getClass().equals(AnonymousAuthenticationToken.class)) { + User user = (User) auth.getPrincipal(); + domain = user.getDomain(); + } + return Optional.ofNullable(domain); + } +} diff --git a/spring-5-security/src/main/java/com/baeldung/passwordstorage/BaeldungPasswordEncoderSetup.java b/spring-5-security/src/main/java/com/baeldung/passwordstorage/BaeldungPasswordEncoderSetup.java new file mode 100644 index 0000000000..94987029db --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/passwordstorage/BaeldungPasswordEncoderSetup.java @@ -0,0 +1,35 @@ +package com.baeldung.passwordstorage; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationListener; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.authentication.event.AuthenticationSuccessEvent; +import org.springframework.security.core.Authentication; +import org.springframework.security.crypto.password.PasswordEncoder; + +@Configuration +public class BaeldungPasswordEncoderSetup { + + private final static Logger LOG = LoggerFactory.getLogger(BaeldungPasswordEncoderSetup.class); + + @Bean + public ApplicationListener authenticationSuccessListener(final PasswordEncoder encoder) { + + return (AuthenticationSuccessEvent event) -> { + final Authentication auth = event.getAuthentication(); + + if (auth instanceof UsernamePasswordAuthenticationToken && auth.getCredentials() != null) { + + final CharSequence clearTextPass = (CharSequence) auth.getCredentials(); // 1 + final String newPasswordHash = encoder.encode(clearTextPass); // 2 + + LOG.info("New password hash {} for user {}", newPasswordHash, auth.getName()); + + ((UsernamePasswordAuthenticationToken) auth).eraseCredentials(); // 3 + } + }; + } +} diff --git a/spring-5-security/src/main/java/com/baeldung/passwordstorage/PasswordStorageApplication.java b/spring-5-security/src/main/java/com/baeldung/passwordstorage/PasswordStorageApplication.java new file mode 100644 index 0000000000..173d979a45 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/passwordstorage/PasswordStorageApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.passwordstorage; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class PasswordStorageApplication { + + public static void main(String[] args) { + SpringApplication.run(PasswordStorageApplication.class, args); + } + +} diff --git a/spring-5-security/src/main/java/com/baeldung/passwordstorage/PasswordStorageWebSecurityConfigurer.java b/spring-5-security/src/main/java/com/baeldung/passwordstorage/PasswordStorageWebSecurityConfigurer.java new file mode 100644 index 0000000000..22ef2f0835 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/passwordstorage/PasswordStorageWebSecurityConfigurer.java @@ -0,0 +1,53 @@ +package com.baeldung.passwordstorage; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.DelegatingPasswordEncoder; +import org.springframework.security.crypto.password.NoOpPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.security.crypto.password.StandardPasswordEncoder; +import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder; +import org.springframework.security.provisioning.InMemoryUserDetailsManager; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +@Configuration +public class PasswordStorageWebSecurityConfigurer extends WebSecurityConfigurerAdapter { + + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.eraseCredentials(false) // 4 + .userDetailsService(getUserDefaultDetailsService()) + .passwordEncoder(passwordEncoder()); + } + + @Bean + public UserDetailsService getUserDefaultDetailsService() { + User testUser = new User("baeldung", "{noop}SpringSecurity5", Collections.emptyList()); + return new InMemoryUserDetailsManager(testUser); + } + + @Bean + public PasswordEncoder passwordEncoder() { + // set up the list of supported encoders and their prefixes + PasswordEncoder defaultEncoder = new StandardPasswordEncoder(); + Map encoders = new HashMap<>(); + encoders.put("bcrypt", new BCryptPasswordEncoder()); + encoders.put("scrypt", new SCryptPasswordEncoder()); + encoders.put("noop", NoOpPasswordEncoder.getInstance()); + + DelegatingPasswordEncoder passwordEncoder = new DelegatingPasswordEncoder("bcrypt", encoders); + passwordEncoder.setDefaultPasswordEncoderForMatches(defaultEncoder); + + return passwordEncoder; + } + +} diff --git a/spring-5-security/src/main/resources/application-extrafields.properties b/spring-5-security/src/main/resources/application-extrafields.properties new file mode 100644 index 0000000000..ab4134ce3e --- /dev/null +++ b/spring-5-security/src/main/resources/application-extrafields.properties @@ -0,0 +1 @@ +spring.thymeleaf.prefix = classpath:/templatesextrafields/ \ No newline at end of file diff --git a/spring-5-security/src/main/resources/application.properties b/spring-5-security/src/main/resources/application.properties index ccec014c2b..781ee76826 100644 --- a/spring-5-security/src/main/resources/application.properties +++ b/spring-5-security/src/main/resources/application.properties @@ -1,3 +1,5 @@ server.port=8081 -logging.level.root=INFO \ No newline at end of file +logging.level.root=INFO + +logging.level.com.baeldung.dsl.ClientErrorLoggingFilter=DEBUG diff --git a/spring-5-security/src/main/resources/static/css/main.css b/spring-5-security/src/main/resources/static/css/main.css new file mode 100644 index 0000000000..febc353af7 --- /dev/null +++ b/spring-5-security/src/main/resources/static/css/main.css @@ -0,0 +1,8 @@ +p.error { + font-weight: bold; + color: red; +} + +div.logout { + margin-right: 2em;; +} diff --git a/spring-5-security/src/main/resources/templatesextrafields/index.html b/spring-5-security/src/main/resources/templatesextrafields/index.html new file mode 100644 index 0000000000..37833ff0d2 --- /dev/null +++ b/spring-5-security/src/main/resources/templatesextrafields/index.html @@ -0,0 +1,32 @@ + + + + Spring Security with Extra Fields + + + + + + + + + +
+
+

Logged in: | Some Domain +

+
+
+ +
+
+
+ +

Hello Spring Security

+

This is an unsecured page, but you can access the secured pages after authenticating.

+ +
+ + diff --git a/spring-5-security/src/main/resources/templatesextrafields/login.html b/spring-5-security/src/main/resources/templatesextrafields/login.html new file mode 100644 index 0000000000..5c51ea3b2c --- /dev/null +++ b/spring-5-security/src/main/resources/templatesextrafields/login.html @@ -0,0 +1,36 @@ + + + + Login page + + + + + + + + + +
+ +
+ + diff --git a/spring-5-security/src/main/resources/templatesextrafields/user/index.html b/spring-5-security/src/main/resources/templatesextrafields/user/index.html new file mode 100644 index 0000000000..9c41f0e78c --- /dev/null +++ b/spring-5-security/src/main/resources/templatesextrafields/user/index.html @@ -0,0 +1,20 @@ + + + + Secured Page + + + + + + + + + +
+
+

This is a secured page!

+

Back to home page

+
+ + diff --git a/spring-5-security/src/test/java/com/baeldung/loginextrafields/AbstractExtraLoginFieldsTest.java b/spring-5-security/src/test/java/com/baeldung/loginextrafields/AbstractExtraLoginFieldsTest.java new file mode 100644 index 0000000000..30b869714f --- /dev/null +++ b/spring-5-security/src/test/java/com/baeldung/loginextrafields/AbstractExtraLoginFieldsTest.java @@ -0,0 +1,46 @@ +package com.baeldung.loginextrafields; + +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrlPattern; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.web.FilterChainProxy; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +public abstract class AbstractExtraLoginFieldsTest { + + @Autowired + private FilterChainProxy springSecurityFilterChain; + + @Autowired + private WebApplicationContext wac; + + protected MockMvc mockMvc; + + @Before + public void setup() { + this.mockMvc = MockMvcBuilders.webAppContextSetup(wac) + .apply(springSecurity(springSecurityFilterChain)) + .build(); + } + + @Test + public void givenRootPathAccess_thenRedirectToIndex() throws Exception { + this.mockMvc.perform(get("/")) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrlPattern("/index*")); + } + + @Test + public void givenSecuredResource_whenAccessUnauthenticated_thenRequiresAuthentication() throws Exception { + this.mockMvc.perform(get("/user/index")) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrlPattern("**/login")); + } +} diff --git a/spring-5-security/src/test/java/com/baeldung/loginextrafields/LoginFieldsFullTest.java b/spring-5-security/src/test/java/com/baeldung/loginextrafields/LoginFieldsFullTest.java new file mode 100644 index 0000000000..38c219cb5e --- /dev/null +++ b/spring-5-security/src/test/java/com/baeldung/loginextrafields/LoginFieldsFullTest.java @@ -0,0 +1,72 @@ +package com.baeldung.loginextrafields; + +import static org.junit.Assert.assertEquals; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrlPattern; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.util.ArrayList; +import java.util.Collection; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.mock.web.MockHttpSession; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.web.context.HttpSessionSecurityContextRepository; +import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; + +import com.baeldung.loginextrafieldscustom.ExtraLoginFieldsApplication; +import com.baeldung.loginextrafieldscustom.User; + +@RunWith(SpringRunner.class) +@SpringJUnitWebConfig +@SpringBootTest(classes = ExtraLoginFieldsApplication.class) +public class LoginFieldsFullTest extends AbstractExtraLoginFieldsTest { + + @Test + public void givenAccessSecuredResource_whenAuthenticated_thenAuthHasExtraFields() throws Exception { + MockHttpServletRequestBuilder securedResourceAccess = get("/user/index"); + MvcResult unauthenticatedResult = mockMvc.perform(securedResourceAccess) + .andExpect(status().is3xxRedirection()) + .andReturn(); + + MockHttpSession session = (MockHttpSession) unauthenticatedResult.getRequest() + .getSession(); + String loginUrl = unauthenticatedResult.getResponse() + .getRedirectedUrl(); + + User user = getUser(); + + mockMvc.perform(post(loginUrl) + .param("username", user.getUsername()) + .param("password", user.getPassword()) + .param("domain", user.getDomain()) + .session(session) + .with(csrf())) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrlPattern("**/user/index")) + .andReturn(); + + mockMvc.perform(securedResourceAccess.session(session)) + .andExpect(status().isOk()); + + SecurityContext securityContext + = (SecurityContext) session.getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY); + Authentication auth = securityContext.getAuthentication(); + assertEquals(((User)auth.getPrincipal()).getDomain(), user.getDomain()); + } + + private User getUser() { + Collection authorities = new ArrayList<>(); + return new User("myusername", "mydomain", "password", true, true, true, true, authorities); + } + +} diff --git a/spring-5-security/src/test/java/com/baeldung/loginextrafields/LoginFieldsSimpleTest.java b/spring-5-security/src/test/java/com/baeldung/loginextrafields/LoginFieldsSimpleTest.java new file mode 100644 index 0000000000..5c0d462772 --- /dev/null +++ b/spring-5-security/src/test/java/com/baeldung/loginextrafields/LoginFieldsSimpleTest.java @@ -0,0 +1,72 @@ +package com.baeldung.loginextrafields; + +import static org.junit.Assert.assertEquals; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrlPattern; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.util.ArrayList; +import java.util.Collection; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.mock.web.MockHttpSession; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.web.context.HttpSessionSecurityContextRepository; +import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; + +import com.baeldung.loginextrafieldssimple.ExtraLoginFieldsApplication; +import com.baeldung.loginextrafieldssimple.User; + +@RunWith(SpringRunner.class) +@SpringJUnitWebConfig +@SpringBootTest(classes = ExtraLoginFieldsApplication.class) +public class LoginFieldsSimpleTest extends AbstractExtraLoginFieldsTest { + + @Test + public void givenAccessSecuredResource_whenAuthenticated_thenAuthHasExtraFields() throws Exception { + MockHttpServletRequestBuilder securedResourceAccess = get("/user/index"); + MvcResult unauthenticatedResult = mockMvc.perform(securedResourceAccess) + .andExpect(status().is3xxRedirection()) + .andReturn(); + + MockHttpSession session = (MockHttpSession) unauthenticatedResult.getRequest() + .getSession(); + String loginUrl = unauthenticatedResult.getResponse() + .getRedirectedUrl(); + + User user = getUser(); + + mockMvc.perform(post(loginUrl) + .param("username", user.getUsername()) + .param("password", user.getPassword()) + .param("domain", user.getDomain()) + .session(session) + .with(csrf())) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrlPattern("**/user/index")) + .andReturn(); + + mockMvc.perform(securedResourceAccess.session(session)) + .andExpect(status().isOk()); + + SecurityContext securityContext + = (SecurityContext) session.getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY); + Authentication auth = securityContext.getAuthentication(); + assertEquals(((User)auth.getPrincipal()).getDomain(), user.getDomain()); + } + + private User getUser() { + Collection authorities = new ArrayList<>(); + return new User("myusername", "mydomain", "password", true, true, true, true, authorities); + } + +} diff --git a/spring-5/README.md b/spring-5/README.md index 8249fe3813..1ac3cf4577 100644 --- a/spring-5/README.md +++ b/spring-5/README.md @@ -14,3 +14,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Spring Security 5 for Reactive Applications](http://www.baeldung.com/spring-security-5-reactive) - [Spring 5 Testing with @EnabledIf Annotation](http://www.baeldung.com/sring-5-enabledif) - [Introduction to Spring REST Docs](http://www.baeldung.com/spring-rest-docs) +- [Spring Security 5 – OAuth2 Login](http://www.baeldung.com/spring-security-5-oauth2-login) diff --git a/spring-5/pom.xml b/spring-5/pom.xml index 19dd65d78f..b4e6e684ad 100644 --- a/spring-5/pom.xml +++ b/spring-5/pom.xml @@ -1,250 +1,215 @@ - - - 4.0.0 - - com.baeldung - spring-5 - 0.0.1-SNAPSHOT - jar - - spring-5 - spring 5 sample project about new features - - - org.springframework.boot - spring-boot-starter-parent - 2.0.0.M7 - - - - - - org.springframework.boot - spring-boot-starter-data-jpa - - - org.springframework.boot - spring-boot-starter-security - - - org.springframework.boot - spring-boot-starter-validation - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-webflux - - - org.springframework.boot - spring-boot-starter-hateoas - - - org.projectreactor - reactor-spring - ${reactor-spring.version} - - - javax.json.bind - javax.json.bind-api - ${jsonb-api.version} - - - - - - - - - - - - - - - org.apache.geronimo.specs - geronimo-json_1.1_spec - ${geronimo-json_1.1_spec.version} - - - org.apache.johnzon - johnzon-jsonb - ${johnzon.version} - - - - org.apache.commons - commons-lang3 - - - - - - org.springframework.boot - spring-boot-devtools - runtime - - - com.h2database - h2 - runtime - - - - org.springframework - spring-test - - - org.springframework.boot - spring-boot-starter-test - test - - - org.springframework.security - spring-security-test - test - - - - org.apache.commons - commons-collections4 - 4.1 - test - - - - org.junit.jupiter - junit-jupiter-api - ${junit.jupiter.version} - - - org.junit.jupiter - junit-jupiter-engine - ${junit.jupiter.version} - test - - - org.junit.platform - junit-platform-surefire-provider - ${junit.platform.version} - test - - - org.junit.platform - junit-platform-runner - ${junit.platform.version} - test - - - - org.springframework.restdocs - spring-restdocs-mockmvc - test - - - org.springframework.restdocs - spring-restdocs-webtestclient - test - - - org.springframework.restdocs - spring-restdocs-restassured - test - - - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - com.baeldung.Spring5Application - JAR - - - - - org.apache.maven.plugins - maven-surefire-plugin - - 3 - true - methods - true - - **/*IntegrationTest.java - **/*LiveTest.java - - - - - org.asciidoctor - asciidoctor-maven-plugin - ${asciidoctor-plugin.version} - - - generate-docs - package - - process-asciidoc - - - html - book - - ${snippetsDirectory} - - src/docs/asciidocs - target/generated-docs - - - - - - - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - - UTF-8 - UTF-8 - 1.8 - 1.0.0 - 5.0.0 - 2.20 - 5.0.2.RELEASE - 1.0.1.RELEASE - 1.1.3 - 1.0 - 1.0 - 1.5.6 - ${project.build.directory}/generated-snippets - - - + + + 4.0.0 + + com.baeldung + spring-5 + 0.0.1-SNAPSHOT + jar + + spring-5 + spring 5 sample project about new features + + + org.springframework.boot + spring-boot-starter-parent + 2.0.0.RELEASE + + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-validation + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-webflux + + + org.springframework.boot + spring-boot-starter-hateoas + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.projectreactor + reactor-spring + ${reactor-spring.version} + + + javax.json.bind + javax.json.bind-api + + + org.apache.geronimo.specs + geronimo-json_1.1_spec + ${geronimo-json_1.1_spec.version} + + + org.apache.johnzon + johnzon-jsonb + ${johnzon.version} + + + + org.apache.commons + commons-lang3 + + + + + + org.springframework.boot + spring-boot-devtools + runtime + + + com.h2database + h2 + runtime + + + + org.springframework + spring-test + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.security + spring-security-test + test + + + + org.apache.commons + commons-collections4 + 4.1 + test + + + + org.junit.jupiter + junit-jupiter-api + + + org.junit.jupiter + junit-jupiter-engine + test + + + org.junit.platform + junit-platform-surefire-provider + ${junit.platform.version} + test + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + + + + org.springframework.restdocs + spring-restdocs-mockmvc + test + + + org.springframework.restdocs + spring-restdocs-webtestclient + test + + + org.springframework.restdocs + spring-restdocs-restassured + test + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + com.baeldung.Spring5Application + JAR + + + + + org.apache.maven.plugins + maven-surefire-plugin + + 3 + true + methods + true + + **/*IntegrationTest.java + **/*LiveTest.java + + + + + org.asciidoctor + asciidoctor-maven-plugin + ${asciidoctor-plugin.version} + + + generate-docs + package + + process-asciidoc + + + html + book + + ${snippetsDirectory} + + src/docs/asciidocs + target/generated-docs + + + + + + + + + UTF-8 + UTF-8 + 1.8 + 1.0.0 + 2.20 + 1.0.1.RELEASE + 1.1.3 + 1.0 + 1.5.6 + ${project.build.directory}/generated-snippets + + + diff --git a/spring-5/src/main/java/com/baeldung/execption/ActorController.java b/spring-5/src/main/java/com/baeldung/execption/ActorController.java new file mode 100644 index 0000000000..6c9c46253a --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/execption/ActorController.java @@ -0,0 +1,41 @@ +package com.baeldung.execption; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ResponseStatusException; + +@RestController +public class ActorController { + + @Autowired + ActorService actorService; + + @GetMapping("/actor/{id}") + public String getActorName(@PathVariable("id") int id) { + try { + return actorService.getActor(id); + } catch (ActorNotFoundException ex) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Actor Not Found", ex); + } + } + + @DeleteMapping("/actor/{id}") + public String getActor(@PathVariable("id") int id) { + return actorService.removeActor(id); + } + + @PutMapping("/actor/{id}/{name}") + public String updateActorName(@PathVariable("id") int id, @PathVariable("name") String name) { + try { + return actorService.updateActor(id, name); + } catch (ActorNotFoundException ex) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Provide correct Actor Id", ex); + } + } + +} diff --git a/spring-5/src/main/java/com/baeldung/execption/ActorNotFoundException.java b/spring-5/src/main/java/com/baeldung/execption/ActorNotFoundException.java new file mode 100644 index 0000000000..642c075b5d --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/execption/ActorNotFoundException.java @@ -0,0 +1,13 @@ +package com.baeldung.execption; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(code = HttpStatus.NOT_FOUND, reason = "Actor Not Found") +public class ActorNotFoundException extends Exception { + private static final long serialVersionUID = 1L; + + public ActorNotFoundException(String errorMessage) { + super(errorMessage); + } +} diff --git a/spring-5/src/main/java/com/baeldung/execption/ActorService.java b/spring-5/src/main/java/com/baeldung/execption/ActorService.java new file mode 100644 index 0000000000..956fa92015 --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/execption/ActorService.java @@ -0,0 +1,35 @@ +package com.baeldung.execption; + +import java.util.Arrays; +import java.util.List; + +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Service; +import org.springframework.web.server.ResponseStatusException; + +@Service +public class ActorService { + List actors = Arrays.asList("Jack Nicholson", "Marlon Brando", "Robert De Niro", "Al Pacino", "Tom Hanks"); + + public String getActor(int index) throws ActorNotFoundException { + if (index >= actors.size()) { + throw new ActorNotFoundException("Actor Not Found in Repsoitory"); + } + return actors.get(index); + } + + public String updateActor(int index, String actorName) throws ActorNotFoundException { + if (index >= actors.size()) { + throw new ActorNotFoundException("Actor Not Found in Repsoitory"); + } + actors.set(index, actorName); + return actorName; + } + + public String removeActor(int index) { + if (index >= actors.size()) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Actor Not Found in Repsoitory"); + } + return actors.remove(index); + } +} diff --git a/spring-5/src/main/java/com/baeldung/execption/SpringExceptionApplication.java b/spring-5/src/main/java/com/baeldung/execption/SpringExceptionApplication.java new file mode 100644 index 0000000000..1670da54c3 --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/execption/SpringExceptionApplication.java @@ -0,0 +1,14 @@ +package com.baeldung.execption; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; +import org.springframework.context.annotation.ComponentScan; + +@SpringBootApplication(exclude = SecurityAutoConfiguration.class) +@ComponentScan(basePackages = { "com.baeldung.execption" }) +public class SpringExceptionApplication { + public static void main(String[] args) { + SpringApplication.run(SpringExceptionApplication.class, args); + } +} \ No newline at end of file diff --git a/spring-5/src/main/java/com/baeldung/restdocs/CRUDController.java b/spring-5/src/main/java/com/baeldung/restdocs/CRUDController.java index f6183bc79e..429d3f433a 100644 --- a/spring-5/src/main/java/com/baeldung/restdocs/CRUDController.java +++ b/spring-5/src/main/java/com/baeldung/restdocs/CRUDController.java @@ -9,7 +9,6 @@ import javax.validation.Valid; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; -import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PatchMapping; diff --git a/spring-5/src/main/java/com/baeldung/sessionattrs/Config.java b/spring-5/src/main/java/com/baeldung/sessionattrs/Config.java new file mode 100644 index 0000000000..9d5c9d9f42 --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/sessionattrs/Config.java @@ -0,0 +1,44 @@ +package com.baeldung.sessionattrs; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Scope; +import org.springframework.context.annotation.ScopedProxyMode; +import org.springframework.core.Ordered; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.thymeleaf.templatemode.TemplateMode; +import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; +import org.thymeleaf.templateresolver.ITemplateResolver; + +@EnableWebMvc +@Configuration +public class Config implements WebMvcConfigurer { + + @Override + public void addViewControllers(ViewControllerRegistry registry) { + registry.addViewController("/").setViewName("index"); + registry.setOrder(Ordered.HIGHEST_PRECEDENCE); + } + + @Bean + @Scope( + value = WebApplicationContext.SCOPE_SESSION, + proxyMode = ScopedProxyMode.TARGET_CLASS) + public TodoList todos() { + return new TodoList(); + } + + @Bean + public ITemplateResolver templateResolver() { + ClassLoaderTemplateResolver resolver + = new ClassLoaderTemplateResolver(); + resolver.setPrefix("templates/sessionattrs/"); + resolver.setSuffix(".html"); + resolver.setTemplateMode(TemplateMode.HTML); + resolver.setCharacterEncoding("UTF-8"); + return resolver; + } +} diff --git a/spring-5/src/main/java/com/baeldung/sessionattrs/SessionAttrsApplication.java b/spring-5/src/main/java/com/baeldung/sessionattrs/SessionAttrsApplication.java new file mode 100644 index 0000000000..7b9f8a700f --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/sessionattrs/SessionAttrsApplication.java @@ -0,0 +1,16 @@ +package com.baeldung.sessionattrs; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; + +@SpringBootApplication( + exclude = {SecurityAutoConfiguration.class, + DataSourceAutoConfiguration.class}) +public class SessionAttrsApplication { + + public static void main(String[] args) { + SpringApplication.run(SessionAttrsApplication.class, args); + } +} diff --git a/spring-5/src/main/java/com/baeldung/sessionattrs/TodoControllerWithScopedProxy.java b/spring-5/src/main/java/com/baeldung/sessionattrs/TodoControllerWithScopedProxy.java new file mode 100644 index 0000000000..0c3bd6c8b6 --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/sessionattrs/TodoControllerWithScopedProxy.java @@ -0,0 +1,45 @@ +package com.baeldung.sessionattrs; + +import java.time.LocalDateTime; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +@RequestMapping("/scopedproxy") +public class TodoControllerWithScopedProxy { + + private TodoList todos; + + public TodoControllerWithScopedProxy(TodoList todos) { + this.todos = todos; + } + + @GetMapping("/form") + public String showForm(Model model) { + if (!todos.isEmpty()) { + model.addAttribute("todo", todos.peekLast()); + } else { + model.addAttribute("todo", new TodoItem()); + } + + return "scopedproxyform"; + } + + @PostMapping("/form") + public String create(@ModelAttribute TodoItem todo) { + todo.setCreateDate(LocalDateTime.now()); + todos.add(todo); + return "redirect:/scopedproxy/todos.html"; + } + + @GetMapping("/todos.html") + public String list(Model model) { + model.addAttribute("todos", todos); + return "scopedproxytodos"; + } +} diff --git a/spring-5/src/main/java/com/baeldung/sessionattrs/TodoControllerWithSessionAttributes.java b/spring-5/src/main/java/com/baeldung/sessionattrs/TodoControllerWithSessionAttributes.java new file mode 100644 index 0000000000..fc7e57b1db --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/sessionattrs/TodoControllerWithSessionAttributes.java @@ -0,0 +1,55 @@ +package com.baeldung.sessionattrs; + +import java.time.LocalDateTime; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.SessionAttributes; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; +import org.springframework.web.servlet.view.RedirectView; + +@Controller +@RequestMapping("/sessionattributes") +@SessionAttributes("todos") +public class TodoControllerWithSessionAttributes { + + @GetMapping("/form") + public String showForm( + Model model, + @ModelAttribute("todos") TodoList todos) { + if (!todos.isEmpty()) { + model.addAttribute("todo", todos.peekLast()); + } else { + model.addAttribute("todo", new TodoItem()); + } + return "sessionattributesform"; + } + + @PostMapping("/form") + public RedirectView create( + @ModelAttribute TodoItem todo, + @ModelAttribute("todos") TodoList todos, + RedirectAttributes attributes) { + todo.setCreateDate(LocalDateTime.now()); + todos.add(todo); + attributes.addFlashAttribute("todos", todos); + return new RedirectView("/sessionattributes/todos.html"); + } + + @GetMapping("/todos.html") + public String list( + Model model, + @ModelAttribute("todos") TodoList todos) { + model.addAttribute("todos", todos); + return "sessionattributestodos"; + } + + @ModelAttribute("todos") + public TodoList todos() { + return new TodoList(); + } +} diff --git a/spring-5/src/main/java/com/baeldung/sessionattrs/TodoItem.java b/spring-5/src/main/java/com/baeldung/sessionattrs/TodoItem.java new file mode 100644 index 0000000000..619d61d5f0 --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/sessionattrs/TodoItem.java @@ -0,0 +1,39 @@ +package com.baeldung.sessionattrs; + +import java.time.LocalDateTime; + +public class TodoItem { + + private String description; + private LocalDateTime createDate; + + public TodoItem(String description, LocalDateTime createDate) { + this.description = description; + this.createDate = createDate; + } + + public TodoItem() { + // default no arg constructor + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public LocalDateTime getCreateDate() { + return createDate; + } + + public void setCreateDate(LocalDateTime createDate) { + this.createDate = createDate; + } + + @Override + public String toString() { + return "TodoItem [description=" + description + ", createDate=" + createDate + "]"; + } +} diff --git a/spring-5/src/main/java/com/baeldung/sessionattrs/TodoList.java b/spring-5/src/main/java/com/baeldung/sessionattrs/TodoList.java new file mode 100644 index 0000000000..cad7811da4 --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/sessionattrs/TodoList.java @@ -0,0 +1,8 @@ +package com.baeldung.sessionattrs; + +import java.util.ArrayDeque; + +@SuppressWarnings("serial") +public class TodoList extends ArrayDeque{ + +} diff --git a/spring-5/src/main/java/com/baeldung/web/reactive/client/WebClientController.java b/spring-5/src/main/java/com/baeldung/web/reactive/client/WebClientController.java index 1082765c63..a719259328 100644 --- a/spring-5/src/main/java/com/baeldung/web/reactive/client/WebClientController.java +++ b/spring-5/src/main/java/com/baeldung/web/reactive/client/WebClientController.java @@ -3,9 +3,7 @@ package com.baeldung.web.reactive.client; import org.reactivestreams.Publisher; import org.reactivestreams.Subscriber; import org.springframework.http.*; -import org.springframework.http.client.reactive.ClientHttpRequest; import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; diff --git a/spring-5/src/main/resources/templates/sessionattrs/index.html b/spring-5/src/main/resources/templates/sessionattrs/index.html new file mode 100644 index 0000000000..72427cd62b --- /dev/null +++ b/spring-5/src/main/resources/templates/sessionattrs/index.html @@ -0,0 +1,27 @@ + + + + Session Scope in Spring MVC + + + + + + + +
+

+

Session Scope in Spring MVC - Example

+

+
+ + + \ No newline at end of file diff --git a/spring-5/src/main/resources/templates/sessionattrs/scopedproxyform.html b/spring-5/src/main/resources/templates/sessionattrs/scopedproxyform.html new file mode 100644 index 0000000000..e72651556b --- /dev/null +++ b/spring-5/src/main/resources/templates/sessionattrs/scopedproxyform.html @@ -0,0 +1,27 @@ + + + + Session Scope in Spring MVC + + + + + + + +
+

+

Scoped Proxy Example

+

+
+
+
Enter a TODO
+
+
+ + +
+
+
+ + \ No newline at end of file diff --git a/spring-5/src/main/resources/templates/sessionattrs/scopedproxytodos.html b/spring-5/src/main/resources/templates/sessionattrs/scopedproxytodos.html new file mode 100644 index 0000000000..5493b5cf64 --- /dev/null +++ b/spring-5/src/main/resources/templates/sessionattrs/scopedproxytodos.html @@ -0,0 +1,48 @@ + + + + Session Scope in Spring MVC + + + + + + + +
+

+

Scoped Proxy Example

+

+
+
+
+
+ Add New +
+
+
+
+
+
TODO List
+ + + + + + + + + + + +
DescriptionCreate Date
DescriptionCreate Date
+
+
+
+
+ + \ No newline at end of file diff --git a/spring-5/src/main/resources/templates/sessionattrs/sessionattributesform.html b/spring-5/src/main/resources/templates/sessionattrs/sessionattributesform.html new file mode 100644 index 0000000000..28e1d5d2c1 --- /dev/null +++ b/spring-5/src/main/resources/templates/sessionattrs/sessionattributesform.html @@ -0,0 +1,27 @@ + + + + Session Scope in Spring MVC + + + + + + + +
+

+

Session Attributes Example

+

+
+
+
Enter a TODO
+
+
+ + +
+
+
+ + \ No newline at end of file diff --git a/spring-5/src/main/resources/templates/sessionattrs/sessionattributestodos.html b/spring-5/src/main/resources/templates/sessionattrs/sessionattributestodos.html new file mode 100644 index 0000000000..4bae12ffb9 --- /dev/null +++ b/spring-5/src/main/resources/templates/sessionattrs/sessionattributestodos.html @@ -0,0 +1,48 @@ + + + + Session Scope in Spring MVC + + + + + + + +
+

+

Session Attributes Example

+

+
+
+
+
+ Add New +
+
+
+
+
+
TODO List
+ + + + + + + + + + + +
DescriptionCreate Date
DescriptionCreate Date
+
+
+
+
+ + \ No newline at end of file diff --git a/spring-5/src/test/java/com/baeldung/functional/BeanRegistrationTest.java b/spring-5/src/test/java/com/baeldung/functional/BeanRegistrationIntegrationTest.java similarity index 96% rename from spring-5/src/test/java/com/baeldung/functional/BeanRegistrationTest.java rename to spring-5/src/test/java/com/baeldung/functional/BeanRegistrationIntegrationTest.java index 0b1542dbbc..5392a59343 100644 --- a/spring-5/src/test/java/com/baeldung/functional/BeanRegistrationTest.java +++ b/spring-5/src/test/java/com/baeldung/functional/BeanRegistrationIntegrationTest.java @@ -13,7 +13,7 @@ import com.baeldung.Spring5Application; @RunWith(SpringRunner.class) @SpringBootTest(classes = Spring5Application.class) -public class BeanRegistrationTest { +public class BeanRegistrationIntegrationTest { @Autowired private GenericWebApplicationContext context; diff --git a/spring-5/src/test/java/com/baeldung/security/SecurityTest.java b/spring-5/src/test/java/com/baeldung/security/SecurityIntegrationTest.java similarity index 65% rename from spring-5/src/test/java/com/baeldung/security/SecurityTest.java rename to spring-5/src/test/java/com/baeldung/security/SecurityIntegrationTest.java index a1c940a17a..5680625496 100644 --- a/spring-5/src/test/java/com/baeldung/security/SecurityTest.java +++ b/spring-5/src/test/java/com/baeldung/security/SecurityIntegrationTest.java @@ -13,7 +13,7 @@ import org.springframework.test.web.reactive.server.WebTestClient; @RunWith(SpringRunner.class) @ContextConfiguration(classes = SpringSecurity5Application.class) -public class SecurityTest { +public class SecurityIntegrationTest { @Autowired ApplicationContext context; @@ -22,27 +22,17 @@ public class SecurityTest { @Before public void setup() { - this.rest = WebTestClient - .bindToApplicationContext(this.context) - .configureClient() - .build(); + this.rest = WebTestClient.bindToApplicationContext(this.context).configureClient().build(); } @Test public void whenNoCredentials_thenRedirectToLogin() { - this.rest.get() - .uri("/") - .exchange() - .expectStatus().is3xxRedirection(); + this.rest.get().uri("/").exchange().expectStatus().is3xxRedirection(); } @Test @WithMockUser public void whenHasCredentials_thenSeesGreeting() { - this.rest.get() - .uri("/") - .exchange() - .expectStatus().isOk() - .expectBody(String.class).isEqualTo("Hello, user"); + this.rest.get().uri("/").exchange().expectStatus().isOk().expectBody(String.class).isEqualTo("Hello, user"); } } diff --git a/spring-5/src/test/java/com/baeldung/sessionattrs/SessionAttrsApplicationIntegrationTest.java b/spring-5/src/test/java/com/baeldung/sessionattrs/SessionAttrsApplicationIntegrationTest.java new file mode 100644 index 0000000000..05e9cdd21e --- /dev/null +++ b/spring-5/src/test/java/com/baeldung/sessionattrs/SessionAttrsApplicationIntegrationTest.java @@ -0,0 +1,16 @@ +package com.baeldung.sessionattrs; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class SessionAttrsApplicationIntegrationTest { + + @Test + public void contextLoads() { + } + +} diff --git a/spring-5/src/test/java/com/baeldung/sessionattrs/TestConfig.java b/spring-5/src/test/java/com/baeldung/sessionattrs/TestConfig.java new file mode 100644 index 0000000000..07d65dd7c2 --- /dev/null +++ b/spring-5/src/test/java/com/baeldung/sessionattrs/TestConfig.java @@ -0,0 +1,17 @@ +package com.baeldung.sessionattrs; + +import org.springframework.beans.factory.config.CustomScopeConfigurer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.support.SimpleThreadScope; + +@Configuration +public class TestConfig { + + @Bean + public CustomScopeConfigurer customScopeConfigurer() { + CustomScopeConfigurer configurer = new CustomScopeConfigurer(); + configurer.addScope("session", new SimpleThreadScope()); + return configurer; + } +} diff --git a/spring-5/src/test/java/com/baeldung/sessionattrs/TodoControllerWithScopedProxyIntegrationTest.java b/spring-5/src/test/java/com/baeldung/sessionattrs/TodoControllerWithScopedProxyIntegrationTest.java new file mode 100644 index 0000000000..5b2f653f8a --- /dev/null +++ b/spring-5/src/test/java/com/baeldung/sessionattrs/TodoControllerWithScopedProxyIntegrationTest.java @@ -0,0 +1,68 @@ +package com.baeldung.sessionattrs; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.util.StringUtils; +import org.springframework.web.context.WebApplicationContext; + +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureMockMvc +@Import(TestConfig.class) +public class TodoControllerWithScopedProxyIntegrationTest { + + @Autowired + private MockMvc mockMvc; + + @Autowired + private WebApplicationContext wac; + + @Before + public void setup() { + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac) + .build(); + } + + @Test + public void whenFirstRequest_thenContainsAllCategoriesAndUnintializedTodo() throws Exception { + MvcResult result = mockMvc.perform(get("/scopedproxy/form")) + .andExpect(status().isOk()) + .andExpect(model().attributeExists("todo")) + .andReturn(); + + TodoItem item = (TodoItem) result.getModelAndView().getModel().get("todo"); + assertTrue(StringUtils.isEmpty(item.getDescription())); + } + + @Test + public void whenSubmit_thenSubsequentFormRequestContainsMostRecentTodo() throws Exception { + mockMvc.perform(post("/scopedproxy/form") + .param("description", "newtodo")) + .andExpect(status().is3xxRedirection()) + .andReturn(); + + MvcResult result = mockMvc.perform(get("/scopedproxy/form")) + .andExpect(status().isOk()) + .andExpect(model().attributeExists("todo")) + .andReturn(); + TodoItem item = (TodoItem) result.getModelAndView().getModel().get("todo"); + assertEquals("newtodo", item.getDescription()); + } + +} diff --git a/spring-5/src/test/java/com/baeldung/sessionattrs/TodoControllerWithSessionAttributesIntegrationTest.java b/spring-5/src/test/java/com/baeldung/sessionattrs/TodoControllerWithSessionAttributesIntegrationTest.java new file mode 100644 index 0000000000..c97fc79f29 --- /dev/null +++ b/spring-5/src/test/java/com/baeldung/sessionattrs/TodoControllerWithSessionAttributesIntegrationTest.java @@ -0,0 +1,68 @@ +package com.baeldung.sessionattrs; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.util.StringUtils; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.servlet.FlashMap; + +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureMockMvc +public class TodoControllerWithSessionAttributesIntegrationTest { + + @Autowired + private MockMvc mockMvc; + + @Autowired + private WebApplicationContext wac; + + @Before + public void setup() { + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac) + .build(); + } + + @Test + public void whenFirstRequest_thenContainsUnintializedTodo() throws Exception { + MvcResult result = mockMvc.perform(get("/sessionattributes/form")) + .andExpect(status().isOk()) + .andExpect(model().attributeExists("todo")) + .andReturn(); + + TodoItem item = (TodoItem) result.getModelAndView().getModel().get("todo"); + assertTrue(StringUtils.isEmpty(item.getDescription())); + } + + @Test + public void whenSubmit_thenSubsequentFormRequestContainsMostRecentTodo() throws Exception { + FlashMap flashMap = mockMvc.perform(post("/sessionattributes/form") + .param("description", "newtodo")) + .andExpect(status().is3xxRedirection()) + .andReturn().getFlashMap(); + + MvcResult result = mockMvc.perform(get("/sessionattributes/form") + .sessionAttrs(flashMap)) + .andExpect(status().isOk()) + .andExpect(model().attributeExists("todo")) + .andReturn(); + TodoItem item = (TodoItem) result.getModelAndView().getModel().get("todo"); + assertEquals("newtodo", item.getDescription()); + } + +} diff --git a/spring-5/src/test/java/com/baeldung/web/client/WebTestClientTest.java b/spring-5/src/test/java/com/baeldung/web/client/WebTestClientIntegrationTest.java similarity index 97% rename from spring-5/src/test/java/com/baeldung/web/client/WebTestClientTest.java rename to spring-5/src/test/java/com/baeldung/web/client/WebTestClientIntegrationTest.java index 43114b5b50..9a6e997ca1 100644 --- a/spring-5/src/test/java/com/baeldung/web/client/WebTestClientTest.java +++ b/spring-5/src/test/java/com/baeldung/web/client/WebTestClientIntegrationTest.java @@ -16,7 +16,7 @@ import reactor.core.publisher.Mono; @RunWith(SpringRunner.class) @SpringBootTest(classes = Spring5Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -public class WebTestClientTest { +public class WebTestClientIntegrationTest { @LocalServerPort private int port; diff --git a/spring-batch/pom.xml b/spring-batch/pom.xml index f372ebd724..f72024d32b 100644 --- a/spring-batch/pom.xml +++ b/spring-batch/pom.xml @@ -18,9 +18,10 @@ UTF-8 - 4.3.4.RELEASE - 3.0.7.RELEASE + 5.0.3.RELEASE + 4.0.0.RELEASE 3.15.1 + 4.1 @@ -51,6 +52,16 @@ spring-batch-core ${spring.batch.version}
+ + org.springframework.batch + spring-batch-test + ${spring.batch.version} + + + com.opencsv + opencsv + ${opencsv.version} + diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LineProcessor.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LineProcessor.java new file mode 100644 index 0000000000..5b6cd9add3 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LineProcessor.java @@ -0,0 +1,36 @@ +package org.baeldung.taskletsvschunks.chunks; + +import org.baeldung.taskletsvschunks.model.Line; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.StepExecutionListener; +import org.springframework.batch.item.ItemProcessor; + +import java.time.LocalDate; +import java.time.temporal.ChronoUnit; + +public class LineProcessor implements ItemProcessor, StepExecutionListener { + + private final Logger logger = LoggerFactory.getLogger(LineProcessor.class); + + @Override + public void beforeStep(StepExecution stepExecution) { + logger.debug("Line Processor initialized."); + } + + @Override + public Line process(Line line) throws Exception { + long age = ChronoUnit.YEARS.between(line.getDob(), LocalDate.now()); + logger.debug("Calculated age " + age + " for line " + line.toString()); + line.setAge(age); + return line; + } + + @Override + public ExitStatus afterStep(StepExecution stepExecution) { + logger.debug("Line Processor ended."); + return ExitStatus.COMPLETED; + } +} diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LineReader.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LineReader.java new file mode 100644 index 0000000000..5f6b6de218 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LineReader.java @@ -0,0 +1,36 @@ +package org.baeldung.taskletsvschunks.chunks; + +import org.baeldung.taskletsvschunks.model.Line; +import org.baeldung.taskletsvschunks.utils.FileUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.StepExecutionListener; +import org.springframework.batch.item.ItemReader; + +public class LineReader implements ItemReader, StepExecutionListener { + + private final Logger logger = LoggerFactory.getLogger(LineReader.class); + private FileUtils fu; + + @Override + public void beforeStep(StepExecution stepExecution) { + fu = new FileUtils("taskletsvschunks/input/tasklets-vs-chunks.csv"); + logger.debug("Line Reader initialized."); + } + + @Override + public Line read() throws Exception { + Line line = fu.readLine(); + if (line != null) logger.debug("Read line: " + line.toString()); + return line; + } + + @Override + public ExitStatus afterStep(StepExecution stepExecution) { + fu.closeReader(); + logger.debug("Line Reader ended."); + return ExitStatus.COMPLETED; + } +} diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LinesWriter.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LinesWriter.java new file mode 100644 index 0000000000..66f9103a56 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LinesWriter.java @@ -0,0 +1,39 @@ +package org.baeldung.taskletsvschunks.chunks; + +import org.baeldung.taskletsvschunks.model.Line; +import org.baeldung.taskletsvschunks.utils.FileUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.StepExecutionListener; +import org.springframework.batch.item.ItemWriter; + +import java.util.List; + +public class LinesWriter implements ItemWriter, StepExecutionListener { + + private final Logger logger = LoggerFactory.getLogger(LinesWriter.class); + private FileUtils fu; + + @Override + public void beforeStep(StepExecution stepExecution) { + fu = new FileUtils("output.csv"); + logger.debug("Line Writer initialized."); + } + + @Override + public ExitStatus afterStep(StepExecution stepExecution) { + fu.closeWriter(); + logger.debug("Line Writer ended."); + return ExitStatus.COMPLETED; + } + + @Override + public void write(List lines) throws Exception { + for (Line line : lines) { + fu.writeLine(line); + logger.debug("Wrote line " + line.toString()); + } + } +} diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/config/ChunksConfig.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/config/ChunksConfig.java new file mode 100644 index 0000000000..601ffb7f27 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/config/ChunksConfig.java @@ -0,0 +1,90 @@ +package org.baeldung.taskletsvschunks.config; + +import org.baeldung.taskletsvschunks.chunks.LineProcessor; +import org.baeldung.taskletsvschunks.chunks.LineReader; +import org.baeldung.taskletsvschunks.chunks.LinesWriter; +import org.baeldung.taskletsvschunks.model.Line; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.Step; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.batch.core.launch.support.SimpleJobLauncher; +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean; +import org.springframework.batch.item.ItemProcessor; +import org.springframework.batch.item.ItemReader; +import org.springframework.batch.item.ItemWriter; +import org.springframework.batch.support.transaction.ResourcelessTransactionManager; +import org.springframework.batch.test.JobLauncherTestUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.PlatformTransactionManager; + +@Configuration +@EnableBatchProcessing +public class ChunksConfig { + + @Autowired private JobBuilderFactory jobs; + + @Autowired private StepBuilderFactory steps; + + @Bean + public JobLauncherTestUtils jobLauncherTestUtils() { + return new JobLauncherTestUtils(); + } + + @Bean + public JobRepository jobRepository() throws Exception { + MapJobRepositoryFactoryBean factory = new MapJobRepositoryFactoryBean(); + factory.setTransactionManager(transactionManager()); + return (JobRepository) factory.getObject(); + } + + @Bean + public PlatformTransactionManager transactionManager() { + return new ResourcelessTransactionManager(); + } + + @Bean + public JobLauncher jobLauncher() throws Exception { + SimpleJobLauncher jobLauncher = new SimpleJobLauncher(); + jobLauncher.setJobRepository(jobRepository()); + return jobLauncher; + } + + @Bean + public ItemReader itemReader() { + return new LineReader(); + } + + @Bean + public ItemProcessor itemProcessor() { + return new LineProcessor(); + } + + @Bean + public ItemWriter itemWriter() { + return new LinesWriter(); + } + + @Bean + protected Step processLines(ItemReader reader, ItemProcessor processor, ItemWriter writer) { + return steps.get("processLines"). chunk(2) + .reader(reader) + .processor(processor) + .writer(writer) + .build(); + } + + @Bean + public Job job() { + return jobs + .get("chunksJob") + .start(processLines(itemReader(), itemProcessor(), itemWriter())) + .build(); + } + +} diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/config/TaskletsConfig.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/config/TaskletsConfig.java new file mode 100644 index 0000000000..b9d06d2639 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/config/TaskletsConfig.java @@ -0,0 +1,103 @@ +package org.baeldung.taskletsvschunks.config; + +import org.baeldung.taskletsvschunks.tasklets.LinesProcessor; +import org.baeldung.taskletsvschunks.tasklets.LinesReader; +import org.baeldung.taskletsvschunks.tasklets.LinesWriter; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.Step; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.batch.core.launch.support.SimpleJobLauncher; +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean; +import org.springframework.batch.support.transaction.ResourcelessTransactionManager; +import org.springframework.batch.test.JobLauncherTestUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.PlatformTransactionManager; + +@Configuration +@EnableBatchProcessing +public class TaskletsConfig { + + @Autowired private JobBuilderFactory jobs; + + @Autowired private StepBuilderFactory steps; + + @Bean + public JobLauncherTestUtils jobLauncherTestUtils() { + return new JobLauncherTestUtils(); + } + + @Bean + public JobRepository jobRepository() throws Exception { + MapJobRepositoryFactoryBean factory = new MapJobRepositoryFactoryBean(); + factory.setTransactionManager(transactionManager()); + return (JobRepository) factory.getObject(); + } + + @Bean + public PlatformTransactionManager transactionManager() { + return new ResourcelessTransactionManager(); + } + + @Bean + public JobLauncher jobLauncher() throws Exception { + SimpleJobLauncher jobLauncher = new SimpleJobLauncher(); + jobLauncher.setJobRepository(jobRepository()); + return jobLauncher; + } + + @Bean + public LinesReader linesReader() { + return new LinesReader(); + } + + @Bean + public LinesProcessor linesProcessor() { + return new LinesProcessor(); + } + + @Bean + public LinesWriter linesWriter() { + return new LinesWriter(); + } + + @Bean + protected Step readLines() { + return steps + .get("readLines") + .tasklet(linesReader()) + .build(); + } + + @Bean + protected Step processLines() { + return steps + .get("processLines") + .tasklet(linesProcessor()) + .build(); + } + + @Bean + protected Step writeLines() { + return steps + .get("writeLines") + .tasklet(linesWriter()) + .build(); + } + + @Bean + public Job job() { + return jobs + .get("taskletsJob") + .start(readLines()) + .next(processLines()) + .next(writeLines()) + .build(); + } + +} diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/model/Line.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/model/Line.java new file mode 100644 index 0000000000..fee6fd31a6 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/model/Line.java @@ -0,0 +1,55 @@ +package org.baeldung.taskletsvschunks.model; + +import java.io.Serializable; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +public class Line implements Serializable { + + private String name; + private LocalDate dob; + private Long age; + + public Line(String name, LocalDate dob) { + this.name = name; + this.dob = dob; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public LocalDate getDob() { + return dob; + } + + public void setDob(LocalDate dob) { + this.dob = dob; + } + + public Long getAge() { + return age; + } + + public void setAge(Long age) { + this.age = age; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("["); + sb.append(this.name); + sb.append(","); + sb.append(this.dob.format(DateTimeFormatter.ofPattern("MM/dd/yyyy"))); + if (this.age != null) { + sb.append(","); + sb.append(this.age); + } + sb.append("]"); + return sb.toString(); + } +} diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesProcessor.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesProcessor.java new file mode 100644 index 0000000000..ba7a3088d5 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesProcessor.java @@ -0,0 +1,49 @@ +package org.baeldung.taskletsvschunks.tasklets; + +import org.baeldung.taskletsvschunks.model.Line; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.StepExecutionListener; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.item.ExecutionContext; +import org.springframework.batch.repeat.RepeatStatus; + +import java.time.LocalDate; +import java.time.temporal.ChronoUnit; +import java.util.List; + +public class LinesProcessor implements Tasklet, StepExecutionListener { + + private final Logger logger = LoggerFactory.getLogger(LinesProcessor.class); + + private List lines; + + @Override + public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception { + for (Line line : lines) { + long age = ChronoUnit.YEARS.between(line.getDob(), LocalDate.now()); + logger.debug("Calculated age " + age + " for line " + line.toString()); + line.setAge(age); + } + return RepeatStatus.FINISHED; + } + + @Override + public void beforeStep(StepExecution stepExecution) { + ExecutionContext executionContext = stepExecution + .getJobExecution() + .getExecutionContext(); + this.lines = (List) executionContext.get("lines"); + logger.debug("Lines Processor initialized."); + } + + @Override + public ExitStatus afterStep(StepExecution stepExecution) { + logger.debug("Lines Processor ended."); + return ExitStatus.COMPLETED; + } +} diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesReader.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesReader.java new file mode 100644 index 0000000000..9905ee8f8a --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesReader.java @@ -0,0 +1,53 @@ +package org.baeldung.taskletsvschunks.tasklets; + +import org.baeldung.taskletsvschunks.model.Line; +import org.baeldung.taskletsvschunks.utils.FileUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.StepExecutionListener; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.repeat.RepeatStatus; + +import java.util.ArrayList; +import java.util.List; + +public class LinesReader implements Tasklet, StepExecutionListener { + + private final Logger logger = LoggerFactory.getLogger(LinesReader.class); + + private List lines; + private FileUtils fu; + + @Override + public void beforeStep(StepExecution stepExecution) { + lines = new ArrayList(); + fu = new FileUtils("taskletsvschunks/input/tasklets-vs-chunks.csv"); + logger.debug("Lines Reader initialized."); + } + + @Override + public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception { + Line line = fu.readLine(); + while (line != null) { + lines.add(line); + logger.debug("Read line: " + line.toString()); + line = fu.readLine(); + } + return RepeatStatus.FINISHED; + } + + @Override + public ExitStatus afterStep(StepExecution stepExecution) { + fu.closeReader(); + stepExecution + .getJobExecution() + .getExecutionContext() + .put("lines", this.lines); + logger.debug("Lines Reader ended."); + return ExitStatus.COMPLETED; + } +} diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesWriter.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesWriter.java new file mode 100644 index 0000000000..937288a890 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesWriter.java @@ -0,0 +1,50 @@ +package org.baeldung.taskletsvschunks.tasklets; + +import org.baeldung.taskletsvschunks.model.Line; +import org.baeldung.taskletsvschunks.utils.FileUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.StepExecutionListener; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.item.ExecutionContext; +import org.springframework.batch.repeat.RepeatStatus; + +import java.util.List; + +public class LinesWriter implements Tasklet, StepExecutionListener { + + private final Logger logger = LoggerFactory.getLogger(LinesWriter.class); + + private List lines; + private FileUtils fu; + + @Override + public void beforeStep(StepExecution stepExecution) { + ExecutionContext executionContext = stepExecution + .getJobExecution() + .getExecutionContext(); + this.lines = (List) executionContext.get("lines"); + fu = new FileUtils("output.csv"); + logger.debug("Lines Writer initialized."); + } + + @Override + public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception { + for (Line line : lines) { + fu.writeLine(line); + logger.debug("Wrote line " + line.toString()); + } + return RepeatStatus.FINISHED; + } + + @Override + public ExitStatus afterStep(StepExecution stepExecution) { + fu.closeWriter(); + logger.debug("Lines Writer ended."); + return ExitStatus.COMPLETED; + } +} diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/utils/FileUtils.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/utils/FileUtils.java new file mode 100644 index 0000000000..df36d5c756 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/utils/FileUtils.java @@ -0,0 +1,95 @@ +package org.baeldung.taskletsvschunks.utils; + +import com.opencsv.CSVReader; +import com.opencsv.CSVWriter; +import org.baeldung.taskletsvschunks.model.Line; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +public class FileUtils { + + private final Logger logger = LoggerFactory.getLogger(FileUtils.class); + + private String fileName; + private CSVReader CSVReader; + private CSVWriter CSVWriter; + private FileReader fileReader; + private FileWriter fileWriter; + private File file; + + public FileUtils(String fileName) { + this.fileName = fileName; + } + + public Line readLine() { + try { + if (CSVReader == null) initReader(); + String[] line = CSVReader.readNext(); + if (line == null) return null; + return new Line(line[0], LocalDate.parse(line[1], DateTimeFormatter.ofPattern("MM/dd/yyyy"))); + } catch (Exception e) { + logger.error("Error while reading line in file: " + this.fileName); + return null; + } + } + + public void writeLine(Line line) { + try { + if (CSVWriter == null) initWriter(); + String[] lineStr = new String[2]; + lineStr[0] = line.getName(); + lineStr[1] = line + .getAge() + .toString(); + CSVWriter.writeNext(lineStr); + } catch (Exception e) { + logger.error("Error while writing line in file: " + this.fileName); + } + } + + private void initReader() throws Exception { + ClassLoader classLoader = this + .getClass() + .getClassLoader(); + if (file == null) file = new File(classLoader + .getResource(fileName) + .getFile()); + if (fileReader == null) fileReader = new FileReader(file); + if (CSVReader == null) CSVReader = new CSVReader(fileReader); + } + + private void initWriter() throws Exception { + if (file == null) { + file = new File(fileName); + file.createNewFile(); + } + if (fileWriter == null) fileWriter = new FileWriter(file, true); + if (CSVWriter == null) CSVWriter = new CSVWriter(fileWriter); + } + + public void closeWriter() { + try { + CSVWriter.close(); + fileWriter.close(); + } catch (IOException e) { + logger.error("Error while closing writer."); + } + } + + public void closeReader() { + try { + CSVReader.close(); + fileReader.close(); + } catch (IOException e) { + logger.error("Error while closing reader."); + } + } + +} diff --git a/spring-batch/src/main/resources/logback.xml b/spring-batch/src/main/resources/logback.xml new file mode 100644 index 0000000000..b110d1c226 --- /dev/null +++ b/spring-batch/src/main/resources/logback.xml @@ -0,0 +1,20 @@ + + + + + + %d{yyyy-MM-dd HH:mm:ss} [%thread] %level %logger{35} - %msg%n + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-batch/src/main/resources/taskletsvschunks/input/tasklets-vs-chunks.csv b/spring-batch/src/main/resources/taskletsvschunks/input/tasklets-vs-chunks.csv new file mode 100644 index 0000000000..214bd3cb70 --- /dev/null +++ b/spring-batch/src/main/resources/taskletsvschunks/input/tasklets-vs-chunks.csv @@ -0,0 +1,6 @@ +Mae Hodges,10/22/1972 +Gary Potter,02/22/1953 +Betty Wise,02/17/1968 +Wayne Rose,04/06/1977 +Adam Caldwell,09/27/1995 +Lucille Phillips,05/14/1992 \ No newline at end of file diff --git a/spring-batch/src/test/java/org/baeldung/taskletsvschunks/chunks/ChunksTest.java b/spring-batch/src/test/java/org/baeldung/taskletsvschunks/chunks/ChunksTest.java new file mode 100644 index 0000000000..2a71970637 --- /dev/null +++ b/spring-batch/src/test/java/org/baeldung/taskletsvschunks/chunks/ChunksTest.java @@ -0,0 +1,25 @@ +package org.baeldung.taskletsvschunks.chunks; + +import org.baeldung.taskletsvschunks.config.ChunksConfig; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.test.JobLauncherTestUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = ChunksConfig.class) +public class ChunksTest { + + @Autowired private JobLauncherTestUtils jobLauncherTestUtils; + + @Test + public void givenChunksJob_WhenJobEnds_ThenStatusCompleted() throws Exception { + JobExecution jobExecution = jobLauncherTestUtils.launchJob(); + Assert.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus()); + } +} \ No newline at end of file diff --git a/spring-batch/src/test/java/org/baeldung/taskletsvschunks/tasklets/TaskletsTest.java b/spring-batch/src/test/java/org/baeldung/taskletsvschunks/tasklets/TaskletsTest.java new file mode 100644 index 0000000000..20379b58fe --- /dev/null +++ b/spring-batch/src/test/java/org/baeldung/taskletsvschunks/tasklets/TaskletsTest.java @@ -0,0 +1,25 @@ +package org.baeldung.taskletsvschunks.tasklets; + +import org.baeldung.taskletsvschunks.config.TaskletsConfig; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.test.JobLauncherTestUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = TaskletsConfig.class) +public class TaskletsTest { + + @Autowired private JobLauncherTestUtils jobLauncherTestUtils; + + @Test + public void givenTaskletsJob_WhenJobEnds_ThenStatusCompleted() throws Exception { + JobExecution jobExecution = jobLauncherTestUtils.launchJob(); + Assert.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus()); + } +} \ No newline at end of file diff --git a/spring-boot-bootstrap/pom.xml b/spring-boot-bootstrap/pom.xml index 5ad8330a89..21c0ea60a8 100644 --- a/spring-boot-bootstrap/pom.xml +++ b/spring-boot-bootstrap/pom.xml @@ -24,12 +24,12 @@ org.springframework.boot spring-boot-starter-data-jpa - 1.5.5.RELEASE + 1.5.10.RELEASE org.springframework.boot spring-boot-dependencies - 1.5.6.RELEASE + 1.5.10.RELEASE pom import diff --git a/spring-boot-property-exp/pom.xml b/spring-boot-property-exp/pom.xml index 0c54d57db1..1a1e31385e 100644 --- a/spring-boot-property-exp/pom.xml +++ b/spring-boot-property-exp/pom.xml @@ -1,6 +1,11 @@ - + + 4.0.0 + spring-boot-property-exp + + com.baeldung + spring-boot-property-exp + 0.0.1-SNAPSHOT + pom parent-modules @@ -8,17 +13,6 @@ 1.0.0-SNAPSHOT - 4.0.0 - - com.baeldung - spring-boot-property-exp - 0.0.1-SNAPSHOT - - pom - - spring-boot-property-exp - http://maven.apache.org - UTF-8 @@ -28,5 +22,4 @@ property-exp-custom-config - diff --git a/spring-boot-property-exp/property-exp-custom-config/pom.xml b/spring-boot-property-exp/property-exp-custom-config/pom.xml index 7822b31cf2..019e2362a1 100644 --- a/spring-boot-property-exp/property-exp-custom-config/pom.xml +++ b/spring-boot-property-exp/property-exp-custom-config/pom.xml @@ -1,32 +1,26 @@ - - - spring-boot-property-exp - com.baeldung - 0.0.1-SNAPSHOT - + 4.0.0 + property-exp-custom com.baeldung property-exp-custom-config 0.0.1-SNAPSHOT jar - property-exp-custom - http://maven.apache.org - - - UTF-8 - Custom Property Value - + + spring-boot-property-exp + com.baeldung + 0.0.1-SNAPSHOT + + org.springframework.boot spring-boot-starter - 1.5.4.RELEASE + 1.5.10.RELEASE + @@ -73,5 +67,9 @@ + + UTF-8 + Custom Property Value + diff --git a/spring-boot-property-exp/property-exp-default-config/pom.xml b/spring-boot-property-exp/property-exp-default-config/pom.xml index 0625916d32..5dc47d287d 100644 --- a/spring-boot-property-exp/property-exp-default-config/pom.xml +++ b/spring-boot-property-exp/property-exp-default-config/pom.xml @@ -1,27 +1,17 @@ - - + 4.0.0 - - - org.springframework.boot - spring-boot-starter-parent - 1.5.4.RELEASE - - + property-exp-default + com.baeldung property-exp-default-config 0.0.1-SNAPSHOT jar - property-exp-default - http://maven.apache.org - - - UTF-8 - Custom Property Value - + + org.springframework.boot + spring-boot-starter-parent + 1.5.10.RELEASE + @@ -42,4 +32,9 @@ + + UTF-8 + Custom Property Value + + diff --git a/spring-boot-security/README.md b/spring-boot-security/README.md index 26ab8b2337..a0ddb8de7b 100644 --- a/spring-boot-security/README.md +++ b/spring-boot-security/README.md @@ -1,6 +1,8 @@ ### Spring Boot Security Auto-Configuration - mvn clean install -- uncomment in application.properties spring.profiles.active=basic # for basic auth config -- uncomment in application.properties spring.profiles.active=form # for form based auth config -- uncomment actuator dependency simultaneously with the line from main class +- uncomment actuator dependency simultaneously with the line from basic auth main class +- uncomment security properties for easy testing. If not random will be generated. + +### CURL commands +- curl -X POST -u baeldung-admin:baeldung -d grant_type=client_credentials -d username=baeldung-admin -d password=baeldung http://localhost:8080/oauth/token diff --git a/spring-boot-security/pom.xml b/spring-boot-security/pom.xml index c35191a7fc..c1ec14ff64 100644 --- a/spring-boot-security/pom.xml +++ b/spring-boot-security/pom.xml @@ -43,6 +43,10 @@ org.springframework.boot spring-boot-starter-security + + org.springframework.security.oauth + spring-security-oauth2 + org.springframework.boot spring-boot-starter-web diff --git a/spring-boot-security/src/main/java/com/baeldung/springbootsecurity/SpringBootSecurityApplication.java b/spring-boot-security/src/main/java/com/baeldung/springbootsecurity/basic_auth/SpringBootSecurityApplication.java similarity index 80% rename from spring-boot-security/src/main/java/com/baeldung/springbootsecurity/SpringBootSecurityApplication.java rename to spring-boot-security/src/main/java/com/baeldung/springbootsecurity/basic_auth/SpringBootSecurityApplication.java index 3a85da72e5..2ecad4ae35 100644 --- a/spring-boot-security/src/main/java/com/baeldung/springbootsecurity/SpringBootSecurityApplication.java +++ b/spring-boot-security/src/main/java/com/baeldung/springbootsecurity/basic_auth/SpringBootSecurityApplication.java @@ -1,4 +1,4 @@ -package com.baeldung.springbootsecurity; +package com.baeldung.springbootsecurity.basic_auth; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -7,7 +7,7 @@ import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration @SpringBootApplication(exclude = { SecurityAutoConfiguration.class // ,ManagementWebSecurityAutoConfiguration.class -}) +}, scanBasePackages = "com.baeldung.springbootsecurity.basic_auth") public class SpringBootSecurityApplication { public static void main(String[] args) { diff --git a/spring-boot-security/src/main/java/com/baeldung/springbootsecurity/config/BasicConfiguration.java b/spring-boot-security/src/main/java/com/baeldung/springbootsecurity/basic_auth/config/BasicAuthConfiguration.java similarity index 84% rename from spring-boot-security/src/main/java/com/baeldung/springbootsecurity/config/BasicConfiguration.java rename to spring-boot-security/src/main/java/com/baeldung/springbootsecurity/basic_auth/config/BasicAuthConfiguration.java index 1b08e5ee22..993c573fb0 100644 --- a/spring-boot-security/src/main/java/com/baeldung/springbootsecurity/config/BasicConfiguration.java +++ b/spring-boot-security/src/main/java/com/baeldung/springbootsecurity/basic_auth/config/BasicAuthConfiguration.java @@ -1,7 +1,6 @@ -package com.baeldung.springbootsecurity.config; +package com.baeldung.springbootsecurity.basic_auth.config; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Profile; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; @@ -9,8 +8,7 @@ import org.springframework.security.config.annotation.web.configuration.WebSecur @Configuration @EnableWebSecurity -@Profile("basic") -public class BasicConfiguration extends WebSecurityConfigurerAdapter { +public class BasicAuthConfiguration extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { diff --git a/spring-boot-security/src/main/java/com/baeldung/springbootsecurity/config/FormLoginConfiguration.java b/spring-boot-security/src/main/java/com/baeldung/springbootsecurity/config/FormLoginConfiguration.java deleted file mode 100644 index b4902a9ffc..0000000000 --- a/spring-boot-security/src/main/java/com/baeldung/springbootsecurity/config/FormLoginConfiguration.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.baeldung.springbootsecurity.config; - -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Profile; -import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; -import org.springframework.security.config.annotation.web.builders.HttpSecurity; -import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; -import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; - -@Configuration -@EnableWebSecurity -@Profile("form") -public class FormLoginConfiguration extends WebSecurityConfigurerAdapter { - - @Override - protected void configure(AuthenticationManagerBuilder auth) throws Exception { - auth - .inMemoryAuthentication() - .withUser("user") - .password("password") - .roles("USER") - .and() - .withUser("admin") - .password("password") - .roles("USER", "ADMIN"); - } - - @Override - protected void configure(HttpSecurity http) throws Exception { - http - .authorizeRequests() - .anyRequest() - .authenticated() - .and() - .formLogin() - .and() - .httpBasic(); - } -} diff --git a/spring-boot-security/src/main/java/com/baeldung/springbootsecurity/oauth2resource/SpringBootOAuth2ResourceApplication.java b/spring-boot-security/src/main/java/com/baeldung/springbootsecurity/oauth2resource/SpringBootOAuth2ResourceApplication.java new file mode 100644 index 0000000000..56231a28bd --- /dev/null +++ b/spring-boot-security/src/main/java/com/baeldung/springbootsecurity/oauth2resource/SpringBootOAuth2ResourceApplication.java @@ -0,0 +1,30 @@ +package com.baeldung.springbootsecurity.oauth2resource; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@EnableResourceServer +@SpringBootApplication(scanBasePackages = "com.baeldung.springbootsecurity.oauth2resource") +public class SpringBootOAuth2ResourceApplication { + + public static void main(String[] args) { + new SpringApplicationBuilder() + .profiles("resource") + .sources(SpringBootOAuth2ResourceApplication.class) + .build() + .run(args); + } + + @RestController + class SecuredResourceController { + + @GetMapping("/securedResource") + public String securedResource() { + return "Baeldung Secured Resource OK"; + } + + } +} diff --git a/spring-boot-security/src/main/java/com/baeldung/springbootsecurity/oauth2server/SpringBootAuthorizationServerApplication.java b/spring-boot-security/src/main/java/com/baeldung/springbootsecurity/oauth2server/SpringBootAuthorizationServerApplication.java new file mode 100644 index 0000000000..44dabefbb8 --- /dev/null +++ b/spring-boot-security/src/main/java/com/baeldung/springbootsecurity/oauth2server/SpringBootAuthorizationServerApplication.java @@ -0,0 +1,30 @@ +package com.baeldung.springbootsecurity.oauth2server; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer; +import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.security.Principal; + +@EnableResourceServer +@EnableAuthorizationServer +@SpringBootApplication(scanBasePackages = "com.baeldung.springbootsecurity.oauth2server") +public class SpringBootAuthorizationServerApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootAuthorizationServerApplication.class, args); + } + + @RestController + class UserController { + + @GetMapping("/user") + public Principal user(Principal user) { + return user; + } + + } +} diff --git a/spring-boot-security/src/main/java/com/baeldung/springbootsecurity/oauth2server/config/AuthorizationServerConfig.java b/spring-boot-security/src/main/java/com/baeldung/springbootsecurity/oauth2server/config/AuthorizationServerConfig.java new file mode 100644 index 0000000000..b403feb5c1 --- /dev/null +++ b/spring-boot-security/src/main/java/com/baeldung/springbootsecurity/oauth2server/config/AuthorizationServerConfig.java @@ -0,0 +1,39 @@ +package com.baeldung.springbootsecurity.oauth2server.config; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer; +import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter; +import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer; + +@Configuration +@Profile("authz") +public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { + + @Autowired + private AuthenticationManager authenticationManager; + + @Override + public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { + endpoints.authenticationManager(authenticationManager); + } + + @Override + public void configure(ClientDetailsServiceConfigurer clients) throws Exception { + clients + .inMemory() + .withClient("baeldung") + .secret("baeldung") + .authorizedGrantTypes("client_credentials", "password", "authorization_code") + .scopes("openid", "read") + .autoApprove(true) + .and() + .withClient("baeldung-admin") + .secret("baeldung") + .authorizedGrantTypes("authorization_code", "client_credentials", "refresh_token") + .scopes("read", "write") + .autoApprove(true); + } +} diff --git a/spring-boot-security/src/main/java/com/baeldung/springbootsecurity/oauth2sso/SpringBootOAuth2SsoApplication.java b/spring-boot-security/src/main/java/com/baeldung/springbootsecurity/oauth2sso/SpringBootOAuth2SsoApplication.java new file mode 100644 index 0000000000..b1cd580f08 --- /dev/null +++ b/spring-boot-security/src/main/java/com/baeldung/springbootsecurity/oauth2sso/SpringBootOAuth2SsoApplication.java @@ -0,0 +1,18 @@ +package com.baeldung.springbootsecurity.oauth2sso; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso; +import org.springframework.boot.builder.SpringApplicationBuilder; + +@EnableOAuth2Sso +@SpringBootApplication(scanBasePackages = "com.baeldung.springbootsecurity.oauth2sso") +public class SpringBootOAuth2SsoApplication { + + public static void main(String[] args) { + new SpringApplicationBuilder() + .profiles("sso") + .sources(SpringBootOAuth2SsoApplication.class) + .build() + .run(args); + } +} diff --git a/spring-boot-security/src/main/resources/application-authz.properties b/spring-boot-security/src/main/resources/application-authz.properties new file mode 100644 index 0000000000..d29b0cdd3c --- /dev/null +++ b/spring-boot-security/src/main/resources/application-authz.properties @@ -0,0 +1,3 @@ +security.user.password=password +security.oauth2.client.client-id=client +security.oauth2.client.client-secret=secret diff --git a/spring-boot-security/src/main/resources/application-resource.properties b/spring-boot-security/src/main/resources/application-resource.properties new file mode 100644 index 0000000000..b157b01d51 --- /dev/null +++ b/spring-boot-security/src/main/resources/application-resource.properties @@ -0,0 +1,2 @@ +server.port=8081 +security.oauth2.resource.userInfoUri=http://localhost:8080/user \ No newline at end of file diff --git a/spring-boot-security/src/main/resources/application-sso.properties b/spring-boot-security/src/main/resources/application-sso.properties new file mode 100644 index 0000000000..ac6ae0cc93 --- /dev/null +++ b/spring-boot-security/src/main/resources/application-sso.properties @@ -0,0 +1,9 @@ +server.port=8082 +security.oauth2.client.clientId= +security.oauth2.client.clientSecret= +security.oauth2.client.accessTokenUri=https://graph.facebook.com/oauth/access_token +security.oauth2.client.userAuthorizationUri=https://www.facebook.com/dialog/oauth +security.oauth2.client.tokenName=oauth_token +security.oauth2.client.authenticationScheme=query +security.oauth2.client.clientAuthenticationScheme=form +security.oauth2.resource.userInfoUri=https://graph.facebook.com/me \ No newline at end of file diff --git a/spring-boot-security/src/main/resources/application.properties b/spring-boot-security/src/main/resources/application.properties index 6ca2edb175..c2b8d70dc6 100644 --- a/spring-boot-security/src/main/resources/application.properties +++ b/spring-boot-security/src/main/resources/application.properties @@ -1,4 +1,4 @@ #spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration -#spring.profiles.active=form -#spring.profiles.active=basic -#security.user.password=password \ No newline at end of file +#security.user.password=password +#security.oauth2.client.client-id=client +#security.oauth2.client.client-secret=secret diff --git a/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/FormConfigurationIntegrationTest.java b/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/FormConfigurationIntegrationTest.java deleted file mode 100644 index 697a4f2868..0000000000 --- a/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/FormConfigurationIntegrationTest.java +++ /dev/null @@ -1,106 +0,0 @@ -package com.baeldung.springbootsecurity; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.context.embedded.LocalServerPort; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.web.client.TestRestTemplate; -import org.springframework.http.*; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; - -import java.util.Collections; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import static org.junit.Assert.*; -import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; - -@RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = RANDOM_PORT) -@ActiveProfiles("form") -public class FormConfigurationIntegrationTest { - - @Autowired TestRestTemplate restTemplate; - @LocalServerPort int port; - - @Test - public void whenLoginPageIsRequested_ThenSuccess() { - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.setAccept(Collections.singletonList(MediaType.TEXT_HTML)); - ResponseEntity responseEntity = restTemplate.exchange("/login", HttpMethod.GET, new HttpEntity(httpHeaders), String.class); - assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); - assertTrue(responseEntity - .getBody() - .contains("_csrf")); - } - - @Test - public void whenTryingToLoginWithCorrectCredentials_ThenAuthenticateWithSuccess() { - HttpHeaders httpHeaders = getHeaders(); - httpHeaders.setAccept(Collections.singletonList(MediaType.TEXT_HTML)); - httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); - MultiValueMap form = getFormSubmitCorrectCredentials(); - ResponseEntity responseEntity = this.restTemplate.exchange("/login", HttpMethod.POST, new HttpEntity<>(form, httpHeaders), String.class); - assertEquals(responseEntity.getStatusCode(), HttpStatus.FOUND); - assertTrue(responseEntity - .getHeaders() - .getLocation() - .toString() - .endsWith(this.port + "/")); - assertNotNull(responseEntity - .getHeaders() - .get("Set-Cookie")); - } - - @Test - public void whenTryingToLoginWithInorrectCredentials_ThenAuthenticationFailed() { - HttpHeaders httpHeaders = getHeaders(); - httpHeaders.setAccept(Collections.singletonList(MediaType.TEXT_HTML)); - httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); - MultiValueMap form = getFormSubmitIncorrectCredentials(); - ResponseEntity responseEntity = this.restTemplate.exchange("/login", HttpMethod.POST, new HttpEntity<>(form, httpHeaders), String.class); - assertEquals(responseEntity.getStatusCode(), HttpStatus.FOUND); - assertTrue(responseEntity - .getHeaders() - .getLocation() - .toString() - .endsWith(this.port + "/login?error")); - assertNull(responseEntity - .getHeaders() - .get("Set-Cookie")); - } - - private MultiValueMap getFormSubmitCorrectCredentials() { - MultiValueMap form = new LinkedMultiValueMap<>(); - form.set("username", "user"); - form.set("password", "password"); - return form; - } - - private MultiValueMap getFormSubmitIncorrectCredentials() { - MultiValueMap form = new LinkedMultiValueMap<>(); - form.set("username", "user"); - form.set("password", "wrongpassword"); - return form; - } - - private HttpHeaders getHeaders() { - HttpHeaders headers = new HttpHeaders(); - ResponseEntity page = this.restTemplate.getForEntity("/login", String.class); - assertEquals(page.getStatusCode(), HttpStatus.OK); - String cookie = page - .getHeaders() - .getFirst("Set-Cookie"); - headers.set("Cookie", cookie); - Pattern pattern = Pattern.compile("(?s).*name=\"_csrf\".*?value=\"([^\"]+).*"); - Matcher matcher = pattern.matcher(page.getBody()); - assertTrue(matcher.matches()); - headers.set("X-CSRF-TOKEN", matcher.group(1)); - return headers; - } - -} diff --git a/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/BasicConfigurationIntegrationTest.java b/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/basic_auth/BasicAuthConfigurationIntegrationTest.java similarity index 86% rename from spring-boot-security/src/test/java/com/baeldung/springbootsecurity/BasicConfigurationIntegrationTest.java rename to spring-boot-security/src/test/java/com/baeldung/springbootsecurity/basic_auth/BasicAuthConfigurationIntegrationTest.java index 63e1c2ac73..32c3fbdef4 100644 --- a/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/BasicConfigurationIntegrationTest.java +++ b/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/basic_auth/BasicAuthConfigurationIntegrationTest.java @@ -1,5 +1,6 @@ -package com.baeldung.springbootsecurity; +package com.baeldung.springbootsecurity.basic_auth; +import com.baeldung.springbootsecurity.basic_auth.SpringBootSecurityApplication; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -8,7 +9,6 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; import java.io.IOException; @@ -20,9 +20,8 @@ import static org.junit.Assert.assertTrue; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = RANDOM_PORT) -@ActiveProfiles("basic") -public class BasicConfigurationIntegrationTest { +@SpringBootTest(webEnvironment = RANDOM_PORT, classes = SpringBootSecurityApplication.class) +public class BasicAuthConfigurationIntegrationTest { TestRestTemplate restTemplate; URL base; @@ -38,6 +37,7 @@ public class BasicConfigurationIntegrationTest { @Test public void whenLoggedUserRequestsHomePage_ThenSuccess() throws IllegalStateException, IOException { ResponseEntity response = restTemplate.getForEntity(base.toString(), String.class); + assertEquals(HttpStatus.OK, response.getStatusCode()); assertTrue(response .getBody() @@ -48,6 +48,7 @@ public class BasicConfigurationIntegrationTest { public void whenUserWithWrongCredentialsRequestsHomePage_ThenUnauthorizedPage() throws IllegalStateException, IOException { restTemplate = new TestRestTemplate("user", "wrongpassword"); ResponseEntity response = restTemplate.getForEntity(base.toString(), String.class); + assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); assertTrue(response .getBody() diff --git a/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/oauth2server/CustomConfigAuthorizationServerIntegrationTest.java b/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/oauth2server/CustomConfigAuthorizationServerIntegrationTest.java new file mode 100644 index 0000000000..cc953eef5a --- /dev/null +++ b/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/oauth2server/CustomConfigAuthorizationServerIntegrationTest.java @@ -0,0 +1,52 @@ +package com.baeldung.springbootsecurity.oauth2server; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.security.oauth2.client.OAuth2RestTemplate; +import org.springframework.security.oauth2.client.resource.OAuth2AccessDeniedException; +import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails; +import org.springframework.security.oauth2.common.OAuth2AccessToken; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +import static java.util.Collections.singletonList; +import static org.junit.Assert.assertNotNull; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = RANDOM_PORT, classes = SpringBootAuthorizationServerApplication.class) +@ActiveProfiles("authz") +public class CustomConfigAuthorizationServerIntegrationTest extends OAuth2IntegrationTestSupport { + + @Test + public void givenOAuth2Context_whenAccessTokenIsRequested_ThenAccessTokenValueIsNotNull() { + ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("baeldung", singletonList("read")); + OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails); + + OAuth2AccessToken accessToken = restTemplate.getAccessToken(); + + assertNotNull(accessToken); + + } + + @Test(expected = OAuth2AccessDeniedException.class) + public void givenOAuth2Context_whenAccessTokenIsRequestedWithInvalidException_ThenExceptionIsThrown() { + ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("baeldung", singletonList("write")); + OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails); + + restTemplate.getAccessToken(); + } + + @Test + public void givenOAuth2Context_whenAccessTokenIsRequestedByClientWithWriteScope_ThenAccessTokenIsNotNull() { + ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("baeldung-admin", singletonList("write")); + OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails); + + OAuth2AccessToken accessToken = restTemplate.getAccessToken(); + + assertNotNull(accessToken); + } + +} + diff --git a/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/oauth2server/DefaultConfigAuthorizationServerIntegrationTest.java b/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/oauth2server/DefaultConfigAuthorizationServerIntegrationTest.java new file mode 100644 index 0000000000..4d7b449380 --- /dev/null +++ b/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/oauth2server/DefaultConfigAuthorizationServerIntegrationTest.java @@ -0,0 +1,32 @@ +package com.baeldung.springbootsecurity.oauth2server; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.security.oauth2.client.OAuth2RestTemplate; +import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails; +import org.springframework.security.oauth2.common.OAuth2AccessToken; +import org.springframework.test.context.junit4.SpringRunner; + +import static java.util.Arrays.asList; +import static org.junit.Assert.assertNotNull; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = RANDOM_PORT, classes = SpringBootAuthorizationServerApplication.class, + properties = { "security.oauth2.client.client-id=client", "security.oauth2.client.client-secret=baeldung" }) +public class DefaultConfigAuthorizationServerIntegrationTest extends OAuth2IntegrationTestSupport { + + @Test + public void givenOAuth2Context_whenAccessTokenIsRequested_ThenAccessTokenValueIsNotNull() { + ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("client", asList("read", "write")); + OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails); + + OAuth2AccessToken accessToken = restTemplate.getAccessToken(); + + assertNotNull(accessToken); + + } + +} + diff --git a/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/oauth2server/OAuth2IntegrationTestSupport.java b/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/oauth2server/OAuth2IntegrationTestSupport.java new file mode 100644 index 0000000000..3eef206c7d --- /dev/null +++ b/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/oauth2server/OAuth2IntegrationTestSupport.java @@ -0,0 +1,34 @@ +package com.baeldung.springbootsecurity.oauth2server; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext; +import org.springframework.security.oauth2.client.OAuth2RestTemplate; +import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails; + +import java.util.List; + +import static java.lang.String.format; +import static java.util.Collections.singletonList; + +public class OAuth2IntegrationTestSupport { + + @Value("${local.server.port}") protected int port; + + protected ClientCredentialsResourceDetails getClientCredentialsResourceDetails(final String clientId, final List scopes) { + ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails(); + resourceDetails.setAccessTokenUri(format("http://localhost:%d/oauth/token", port)); + resourceDetails.setClientId(clientId); + resourceDetails.setClientSecret("baeldung"); + resourceDetails.setScope(scopes); + resourceDetails.setGrantType("client_credentials"); + return resourceDetails; + } + + protected OAuth2RestTemplate getOAuth2RestTemplate(final ClientCredentialsResourceDetails resourceDetails) { + DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext(); + OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails, clientContext); + restTemplate.setMessageConverters(singletonList(new MappingJackson2HttpMessageConverter())); + return restTemplate; + } +} diff --git a/spring-boot/.factorypath b/spring-boot/.factorypath index aa15485f5c..60dbd696eb 100644 --- a/spring-boot/.factorypath +++ b/spring-boot/.factorypath @@ -1,149 +1,154 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + - - - - - - - - + + + + + + + + + + + + + + + - + - - + + + + + - - - - - - + + + + + + + - - + - + - - - - + + + + + - - - - - - - - + + + + + + + + + + + + - + - + + + + + + + + + + + + + + + + + + - - - - + + + + - - - - - + + + + + - - - - + + + + + + + + + + - + - - - + + + + - + diff --git a/spring-boot/README.MD b/spring-boot/README.MD index 7b5fb3c880..e78756cf08 100644 --- a/spring-boot/README.MD +++ b/spring-boot/README.MD @@ -29,3 +29,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Getting Started with GraphQL and Spring Boot](http://www.baeldung.com/spring-graphql) - [Guide to Spring Type Conversions](http://www.baeldung.com/spring-type-conversions) - [Quick Guide on data.sql and schema.sql Files in Spring Boot](http://www.baeldung.com/spring-boot-data-sql-and-schema-sql) +- [Spring Data Java 8 Support](http://www.baeldung.com/spring-data-java-8) +- [A Quick Guide to Maven Wrapper](http://www.baeldung.com/maven-wrapper) +- [An Introduction to Kong](http://www.baeldung.com/kong) diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index d6ee022522..a557604bf3 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -167,6 +167,12 @@ org.apache.activemq artemis-server + + + com.rometools + rome + ${rome.version} + @@ -276,6 +282,7 @@ 8.5.11 1.4.194 2.4.1.Final + 1.9.0 \ No newline at end of file diff --git a/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/AttrListener.java b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/AttrListener.java index 95b4a1a191..b1bdc7d781 100644 --- a/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/AttrListener.java +++ b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/AttrListener.java @@ -9,8 +9,7 @@ public class AttrListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent servletContextEvent) { - servletContextEvent.getServletContext() - .setAttribute("servlet-context-attr", "test"); + servletContextEvent.getServletContext().setAttribute("servlet-context-attr", "test"); System.out.println("context init"); } diff --git a/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/EchoServlet.java b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/EchoServlet.java index a2995d1532..d8192c2cb1 100644 --- a/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/EchoServlet.java +++ b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/EchoServlet.java @@ -16,8 +16,7 @@ public class EchoServlet extends HttpServlet { @Override public void doPost(HttpServletRequest request, HttpServletResponse response) { try { - Path path = File.createTempFile("echo", "tmp") - .toPath(); + Path path = File.createTempFile("echo", "tmp").toPath(); Files.copy(request.getInputStream(), path, StandardCopyOption.REPLACE_EXISTING); Files.copy(path, response.getOutputStream()); Files.delete(path); diff --git a/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/HelloFilter.java b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/HelloFilter.java index 650dae9806..146e5ae386 100644 --- a/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/HelloFilter.java +++ b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/HelloFilter.java @@ -18,8 +18,7 @@ public class HelloFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { - servletResponse.getOutputStream() - .print(filterConfig.getInitParameter("msg")); + servletResponse.getOutputStream().print(filterConfig.getInitParameter("msg")); filterChain.doFilter(servletRequest, servletResponse); } diff --git a/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/HelloServlet.java b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/HelloServlet.java index 3c27b8416f..5269c1bf29 100644 --- a/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/HelloServlet.java +++ b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/HelloServlet.java @@ -21,9 +21,7 @@ public class HelloServlet extends HttpServlet { @Override public void doGet(HttpServletRequest request, HttpServletResponse response) { try { - response.getOutputStream() - .write(servletConfig.getInitParameter("msg") - .getBytes()); + response.getOutputStream().write(servletConfig.getInitParameter("msg").getBytes()); } catch (IOException e) { e.printStackTrace(); } diff --git a/spring-boot/src/main/java/com/baeldung/autoconfiguration/MySQLAutoconfiguration.java b/spring-boot/src/main/java/com/baeldung/autoconfiguration/MySQLAutoconfiguration.java index 6dd127a8c3..4f4f6fb6af 100644 --- a/spring-boot/src/main/java/com/baeldung/autoconfiguration/MySQLAutoconfiguration.java +++ b/spring-boot/src/main/java/com/baeldung/autoconfiguration/MySQLAutoconfiguration.java @@ -98,13 +98,8 @@ public class MySQLAutoconfiguration { public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { ConditionMessage.Builder message = ConditionMessage.forCondition("Hibernate"); - return Arrays.stream(CLASS_NAMES) - .filter(className -> ClassUtils.isPresent(className, context.getClassLoader())) - .map(className -> ConditionOutcome.match(message.found("class") - .items(Style.NORMAL, className))) - .findAny() - .orElseGet(() -> ConditionOutcome.noMatch(message.didNotFind("class", "classes") - .items(Style.NORMAL, Arrays.asList(CLASS_NAMES)))); + return Arrays.stream(CLASS_NAMES).filter(className -> ClassUtils.isPresent(className, context.getClassLoader())).map(className -> ConditionOutcome.match(message.found("class").items(Style.NORMAL, className))).findAny() + .orElseGet(() -> ConditionOutcome.noMatch(message.didNotFind("class", "classes").items(Style.NORMAL, Arrays.asList(CLASS_NAMES)))); } } diff --git a/spring-boot/src/main/java/com/baeldung/dynamicvalidation/ContactInfoValidator.java b/spring-boot/src/main/java/com/baeldung/dynamicvalidation/ContactInfoValidator.java index c12c28d00a..0f8300a797 100644 --- a/spring-boot/src/main/java/com/baeldung/dynamicvalidation/ContactInfoValidator.java +++ b/spring-boot/src/main/java/com/baeldung/dynamicvalidation/ContactInfoValidator.java @@ -28,9 +28,7 @@ public class ContactInfoValidator implements ConstraintValidator getAuthor(String id) { - return authors.stream() - .filter(author -> id.equals(author.getId())) - .findFirst(); + return authors.stream().filter(author -> id.equals(author.getId())).findFirst(); } } diff --git a/spring-boot/src/main/java/com/baeldung/graphql/Mutation.java b/spring-boot/src/main/java/com/baeldung/graphql/Mutation.java index 5ccc80dad1..0e16e3c8b7 100644 --- a/spring-boot/src/main/java/com/baeldung/graphql/Mutation.java +++ b/spring-boot/src/main/java/com/baeldung/graphql/Mutation.java @@ -13,8 +13,7 @@ public class Mutation implements GraphQLMutationResolver { public Post writePost(String title, String text, String category, String author) { Post post = new Post(); - post.setId(UUID.randomUUID() - .toString()); + post.setId(UUID.randomUUID().toString()); post.setTitle(title); post.setText(text); post.setCategory(category); diff --git a/spring-boot/src/main/java/com/baeldung/graphql/PostDao.java b/spring-boot/src/main/java/com/baeldung/graphql/PostDao.java index f8d243ee3a..0a755a7cf4 100644 --- a/spring-boot/src/main/java/com/baeldung/graphql/PostDao.java +++ b/spring-boot/src/main/java/com/baeldung/graphql/PostDao.java @@ -11,16 +11,11 @@ public class PostDao { } public List getRecentPosts(int count, int offset) { - return posts.stream() - .skip(offset) - .limit(count) - .collect(Collectors.toList()); + return posts.stream().skip(offset).limit(count).collect(Collectors.toList()); } public List getAuthorPosts(String author) { - return posts.stream() - .filter(post -> author.equals(post.getAuthorId())) - .collect(Collectors.toList()); + return posts.stream().filter(post -> author.equals(post.getAuthorId())).collect(Collectors.toList()); } public void savePost(Post post) { diff --git a/spring-boot/src/main/java/com/baeldung/internationalization/config/MvcConfig.java b/spring-boot/src/main/java/com/baeldung/internationalization/config/MvcConfig.java index 59f7fd3ba5..8a0b709e69 100644 --- a/spring-boot/src/main/java/com/baeldung/internationalization/config/MvcConfig.java +++ b/spring-boot/src/main/java/com/baeldung/internationalization/config/MvcConfig.java @@ -6,14 +6,12 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.LocaleResolver; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import org.springframework.web.servlet.i18n.SessionLocaleResolver; @Configuration -@EnableWebMvc @ComponentScan(basePackages = "com.baeldung.internationalization.config") public class MvcConfig extends WebMvcConfigurerAdapter { diff --git a/spring-boot/src/main/java/com/baeldung/kong/QueryController.java b/spring-boot/src/main/java/com/baeldung/kong/QueryController.java new file mode 100644 index 0000000000..ec04d0b9ce --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/kong/QueryController.java @@ -0,0 +1,31 @@ +package com.baeldung.kong; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author aiet + */ +@RestController +@RequestMapping("/stock") +public class QueryController { + + private static int REQUEST_COUNTER = 0; + + @GetMapping("/reqcount") + public int getReqCount() { + return REQUEST_COUNTER; + } + + @GetMapping("/{code}") + public String getStockPrice(@PathVariable String code) { + REQUEST_COUNTER++; + if ("BTC".equalsIgnoreCase(code)) + return "10000"; + else + return "N/A"; + } + +} diff --git a/spring-boot/src/main/java/com/baeldung/kong/StockApp.java b/spring-boot/src/main/java/com/baeldung/kong/StockApp.java new file mode 100644 index 0000000000..f901592938 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/kong/StockApp.java @@ -0,0 +1,13 @@ +package com.baeldung.kong; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class StockApp { + + public static void main(String[] args) { + SpringApplication.run(StockApp.class, args); + } + +} diff --git a/spring-boot/src/main/java/com/baeldung/rss/ArticleFeedView.java b/spring-boot/src/main/java/com/baeldung/rss/ArticleFeedView.java new file mode 100644 index 0000000000..3efa619409 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/rss/ArticleFeedView.java @@ -0,0 +1,54 @@ +package com.baeldung.rss; + +import com.rometools.rome.feed.rss.Channel; +import com.rometools.rome.feed.rss.Description; +import com.rometools.rome.feed.rss.Item; +import org.springframework.stereotype.Service; +import org.springframework.web.servlet.view.feed.AbstractRssFeedView; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Map; + +@Service("articleFeedView") +public class ArticleFeedView extends AbstractRssFeedView { + + protected Channel newFeed() { + Channel channel = new Channel("rss_2.0"); + channel.setLink("http://localhost:8080/rss"); + channel.setTitle("Article Feed"); + channel.setDescription("Article Feed Description"); + channel.setPubDate(new Date()); + return channel; + } + + @Override + protected List buildFeedItems(Map map, HttpServletRequest httpStRequest, HttpServletResponse httpStResponse) throws Exception { + List list = new ArrayList(); + + Item item1 = new Item(); + item1.setLink("http://www.baeldung.com/netty-exception-handling"); + item1.setTitle("Exceptions in Netty"); + Description description1 = new Description(); + description1.setValue("In this quick article, we’ll be looking at exception handling in Netty."); + item1.setDescription(description1); + item1.setPubDate(new Date()); + item1.setAuthor("Carlos"); + + Item item2 = new Item(); + item2.setLink("http://www.baeldung.com/cockroachdb-java"); + item2.setTitle("Guide to CockroachDB in Java"); + Description description2 = new Description(); + description2.setValue("This tutorial is an introductory guide to using CockroachDB with Java."); + item2.setDescription(description2); + item2.setPubDate(new Date()); + item2.setAuthor("Baeldung"); + + list.add(item1); + list.add(item2); + return list; + } +} diff --git a/spring-boot/src/main/java/com/baeldung/rss/ArticleRssController.java b/spring-boot/src/main/java/com/baeldung/rss/ArticleRssController.java new file mode 100644 index 0000000000..a3fbc4a37e --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/rss/ArticleRssController.java @@ -0,0 +1,16 @@ +package com.baeldung.rss; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +@Controller +@RequestMapping(value = "/rss", produces = "application/*") +public class ArticleRssController { + + @RequestMapping(method = RequestMethod.GET) + public String articleFeed() { + return "articleFeedView"; + } + +} diff --git a/spring-boot/src/main/java/com/baeldung/rss/CustomContainer.java b/spring-boot/src/main/java/com/baeldung/rss/CustomContainer.java new file mode 100644 index 0000000000..ee36ecdc51 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/rss/CustomContainer.java @@ -0,0 +1,16 @@ +package com.baeldung.rss; + +import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; +import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; +import org.springframework.stereotype.Component; + +@Component +public class CustomContainer implements EmbeddedServletContainerCustomizer { + + @Override + public void customize(ConfigurableEmbeddedServletContainer container) { + container.setPort(8080); + container.setContextPath(""); + } + +} \ No newline at end of file diff --git a/spring-boot/src/main/java/com/baeldung/rss/RssApp.java b/spring-boot/src/main/java/com/baeldung/rss/RssApp.java new file mode 100644 index 0000000000..2add7ed421 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/rss/RssApp.java @@ -0,0 +1,20 @@ +package com.baeldung.rss; + +import com.baeldung.autoconfiguration.MySQLAutoconfiguration; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; + +import javax.annotation.security.RolesAllowed; + +@SpringBootApplication(exclude = MySQLAutoconfiguration.class) +@ComponentScan(basePackages = "com.baeldung.rss") +public class RssApp { + + @RolesAllowed("*") + public static void main(String[] args) { + System.setProperty("security.basic.enabled", "false"); + SpringApplication.run(RssApp.class, args); + } + +} diff --git a/spring-boot/src/main/java/com/baeldung/servlets/configuration/WebMvcConfigure.java b/spring-boot/src/main/java/com/baeldung/servlets/configuration/WebMvcConfigure.java index 2d8298338c..8dea814bc7 100644 --- a/spring-boot/src/main/java/com/baeldung/servlets/configuration/WebMvcConfigure.java +++ b/spring-boot/src/main/java/com/baeldung/servlets/configuration/WebMvcConfigure.java @@ -28,11 +28,7 @@ public class WebMvcConfigure extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { - registry.addResourceHandler("/resources/**") - .addResourceLocations("/resources/") - .setCachePeriod(3600) - .resourceChain(true) - .addResolver(new PathResourceResolver()); + registry.addResourceHandler("/resources/**").addResourceLocations("/resources/").setCachePeriod(3600).resourceChain(true).addResolver(new PathResourceResolver()); } @Bean diff --git a/spring-boot/src/main/java/com/baeldung/servlets/servlets/javaee/AnnotationServlet.java b/spring-boot/src/main/java/com/baeldung/servlets/servlets/javaee/AnnotationServlet.java index 358ff5af2d..992976ca0e 100644 --- a/spring-boot/src/main/java/com/baeldung/servlets/servlets/javaee/AnnotationServlet.java +++ b/spring-boot/src/main/java/com/baeldung/servlets/servlets/javaee/AnnotationServlet.java @@ -13,7 +13,6 @@ public class AnnotationServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - request.getRequestDispatcher("/annotationservlet.jsp") - .forward(request, response); + request.getRequestDispatcher("/annotationservlet.jsp").forward(request, response); } } diff --git a/spring-boot/src/main/java/com/baeldung/toggle/FeaturesAspect.java b/spring-boot/src/main/java/com/baeldung/toggle/FeaturesAspect.java index 9bc643fccc..ed99f65006 100644 --- a/spring-boot/src/main/java/com/baeldung/toggle/FeaturesAspect.java +++ b/spring-boot/src/main/java/com/baeldung/toggle/FeaturesAspect.java @@ -14,12 +14,10 @@ public class FeaturesAspect { @Around(value = "@within(featureAssociation) || @annotation(featureAssociation)") public Object checkAspect(ProceedingJoinPoint joinPoint, FeatureAssociation featureAssociation) throws Throwable { - if (featureAssociation.value() - .isActive()) { + if (featureAssociation.value().isActive()) { return joinPoint.proceed(); } else { - LOG.info("Feature " + featureAssociation.value() - .name() + " is not enabled!"); + LOG.info("Feature " + featureAssociation.value().name() + " is not enabled!"); return null; } } diff --git a/spring-boot/src/main/java/com/baeldung/toggle/MyFeatures.java b/spring-boot/src/main/java/com/baeldung/toggle/MyFeatures.java index b05ec2bf52..a88ec2166e 100644 --- a/spring-boot/src/main/java/com/baeldung/toggle/MyFeatures.java +++ b/spring-boot/src/main/java/com/baeldung/toggle/MyFeatures.java @@ -17,8 +17,7 @@ public enum MyFeatures implements Feature { EMPLOYEE_MANAGEMENT_FEATURE; public boolean isActive() { - return FeatureContext.getFeatureManager() - .isActive(this); + return FeatureContext.getFeatureManager().isActive(this); } } diff --git a/spring-boot/src/main/java/com/baeldung/utils/controller/UtilsController.java b/spring-boot/src/main/java/com/baeldung/utils/controller/UtilsController.java index 7c66391bd2..8c7f2f932a 100644 --- a/spring-boot/src/main/java/com/baeldung/utils/controller/UtilsController.java +++ b/spring-boot/src/main/java/com/baeldung/utils/controller/UtilsController.java @@ -4,7 +4,6 @@ import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.web.bind.ServletRequestBindingException; import org.springframework.web.bind.ServletRequestUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; diff --git a/spring-boot/src/main/java/com/baeldung/webjar/WebjarsdemoApplication.java b/spring-boot/src/main/java/com/baeldung/webjar/WebjarsdemoApplication.java index 8704b42166..5038c7e5f7 100644 --- a/spring-boot/src/main/java/com/baeldung/webjar/WebjarsdemoApplication.java +++ b/spring-boot/src/main/java/com/baeldung/webjar/WebjarsdemoApplication.java @@ -2,7 +2,6 @@ package com.baeldung.webjar; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.ComponentScan; import com.baeldung.autoconfiguration.MySQLAutoconfiguration; diff --git a/spring-boot/src/main/java/org/baeldung/boot/config/H2JpaConfig.java b/spring-boot/src/main/java/org/baeldung/boot/config/H2JpaConfig.java index 4e4b2e06bd..92a6ed7ab0 100644 --- a/spring-boot/src/main/java/org/baeldung/boot/config/H2JpaConfig.java +++ b/spring-boot/src/main/java/org/baeldung/boot/config/H2JpaConfig.java @@ -18,7 +18,7 @@ import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration -@EnableJpaRepositories(basePackages = { "org.baeldung.boot.repository", "org.baeldung.boot.boottest","org.baeldung.repository" }) +@EnableJpaRepositories(basePackages = { "org.baeldung.boot.repository", "org.baeldung.boot.boottest", "org.baeldung.repository" }) @PropertySource("classpath:persistence-generic-entity.properties") @EnableTransactionManagement public class H2JpaConfig { diff --git a/spring-boot/src/main/java/org/baeldung/boot/controller/GenericEntityController.java b/spring-boot/src/main/java/org/baeldung/boot/controller/GenericEntityController.java index 8b038e0335..817bae8d01 100644 --- a/spring-boot/src/main/java/org/baeldung/boot/controller/GenericEntityController.java +++ b/spring-boot/src/main/java/org/baeldung/boot/controller/GenericEntityController.java @@ -39,31 +39,21 @@ public class GenericEntityController { @RequestMapping("/entity/findby/{id}") public GenericEntity findById(@PathVariable Long id) { - return entityList.stream() - .filter(entity -> entity.getId() - .equals(id)) - .findFirst() - .get(); + return entityList.stream().filter(entity -> entity.getId().equals(id)).findFirst().get(); } @GetMapping("/entity/findbydate/{date}") public GenericEntity findByDate(@PathVariable("date") LocalDateTime date) { - return entityList.stream() - .findFirst() - .get(); + return entityList.stream().findFirst().get(); } @GetMapping("/entity/findbymode/{mode}") public GenericEntity findByEnum(@PathVariable("mode") Modes mode) { - return entityList.stream() - .findFirst() - .get(); + return entityList.stream().findFirst().get(); } @GetMapping("/entity/findbyversion") public ResponseEntity findByVersion(@Version String version) { - return version != null ? new ResponseEntity(entityList.stream() - .findFirst() - .get(), HttpStatus.OK) : new ResponseEntity(HttpStatus.NOT_FOUND); + return version != null ? new ResponseEntity(entityList.stream().findFirst().get(), HttpStatus.OK) : new ResponseEntity(HttpStatus.NOT_FOUND); } } diff --git a/spring-boot/src/main/java/org/baeldung/boot/converter/GenericBigDecimalConverter.java b/spring-boot/src/main/java/org/baeldung/boot/converter/GenericBigDecimalConverter.java index decd8ac5db..f756a25f67 100644 --- a/spring-boot/src/main/java/org/baeldung/boot/converter/GenericBigDecimalConverter.java +++ b/spring-boot/src/main/java/org/baeldung/boot/converter/GenericBigDecimalConverter.java @@ -5,28 +5,24 @@ import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.converter.GenericConverter; import java.math.BigDecimal; -import java.math.MathContext; import java.util.Set; public class GenericBigDecimalConverter implements GenericConverter { @Override - public Set getConvertibleTypes () { + public Set getConvertibleTypes() { - ConvertiblePair[] pairs = new ConvertiblePair[] { - new ConvertiblePair(Number.class, BigDecimal.class), - new ConvertiblePair(String.class, BigDecimal.class)}; + ConvertiblePair[] pairs = new ConvertiblePair[] { new ConvertiblePair(Number.class, BigDecimal.class), new ConvertiblePair(String.class, BigDecimal.class) }; return ImmutableSet.copyOf(pairs); } @Override - public Object convert (Object source, TypeDescriptor sourceType, - TypeDescriptor targetType) { + public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (sourceType.getType() == BigDecimal.class) { return source; } - if(sourceType.getType() == String.class) { + if (sourceType.getType() == String.class) { String number = (String) source; return new BigDecimal(number); } else { diff --git a/spring-boot/src/main/java/org/baeldung/boot/converter/StringToEmployeeConverter.java b/spring-boot/src/main/java/org/baeldung/boot/converter/StringToEmployeeConverter.java index de9cf3f55a..b07e11e01a 100644 --- a/spring-boot/src/main/java/org/baeldung/boot/converter/StringToEmployeeConverter.java +++ b/spring-boot/src/main/java/org/baeldung/boot/converter/StringToEmployeeConverter.java @@ -1,6 +1,5 @@ package org.baeldung.boot.converter; - import com.baeldung.toggle.Employee; import org.springframework.core.convert.converter.Converter; diff --git a/spring-boot/src/main/java/org/baeldung/boot/converter/controller/StringToEmployeeConverterController.java b/spring-boot/src/main/java/org/baeldung/boot/converter/controller/StringToEmployeeConverterController.java index ad921c2c43..a6e0400845 100644 --- a/spring-boot/src/main/java/org/baeldung/boot/converter/controller/StringToEmployeeConverterController.java +++ b/spring-boot/src/main/java/org/baeldung/boot/converter/controller/StringToEmployeeConverterController.java @@ -1,7 +1,6 @@ package org.baeldung.boot.converter.controller; import com.baeldung.toggle.Employee; -import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; diff --git a/spring-boot/src/main/java/org/baeldung/boot/jsoncomponent/UserCombinedSerializer.java b/spring-boot/src/main/java/org/baeldung/boot/jsoncomponent/UserCombinedSerializer.java index 4d3a2cf99e..f4d7505342 100644 --- a/spring-boot/src/main/java/org/baeldung/boot/jsoncomponent/UserCombinedSerializer.java +++ b/spring-boot/src/main/java/org/baeldung/boot/jsoncomponent/UserCombinedSerializer.java @@ -36,8 +36,7 @@ public class UserCombinedSerializer { public static class UserJsonDeserializer extends JsonDeserializer { @Override public User deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { - TreeNode treeNode = jsonParser.getCodec() - .readTree(jsonParser); + TreeNode treeNode = jsonParser.getCodec().readTree(jsonParser); TextNode favoriteColor = (TextNode) treeNode.get("favoriteColor"); return new User(Color.web(favoriteColor.asText())); } diff --git a/spring-boot/src/main/java/org/baeldung/boot/jsoncomponent/UserJsonDeserializer.java b/spring-boot/src/main/java/org/baeldung/boot/jsoncomponent/UserJsonDeserializer.java index ad82ca06c0..f7bd822c8a 100644 --- a/spring-boot/src/main/java/org/baeldung/boot/jsoncomponent/UserJsonDeserializer.java +++ b/spring-boot/src/main/java/org/baeldung/boot/jsoncomponent/UserJsonDeserializer.java @@ -15,8 +15,7 @@ import java.io.IOException; public class UserJsonDeserializer extends JsonDeserializer { @Override public User deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { - TreeNode treeNode = jsonParser.getCodec() - .readTree(jsonParser); + TreeNode treeNode = jsonParser.getCodec().readTree(jsonParser); TextNode favoriteColor = (TextNode) treeNode.get("favoriteColor"); return new User(Color.web(favoriteColor.asText())); } diff --git a/spring-boot/src/main/java/org/baeldung/boot/monitor/jmx/MonitoringConfig.java b/spring-boot/src/main/java/org/baeldung/boot/monitor/jmx/MonitoringConfig.java index d2e8fc228f..28c6ac5953 100644 --- a/spring-boot/src/main/java/org/baeldung/boot/monitor/jmx/MonitoringConfig.java +++ b/spring-boot/src/main/java/org/baeldung/boot/monitor/jmx/MonitoringConfig.java @@ -14,8 +14,7 @@ public class MonitoringConfig { @Bean public JmxReporter jmxReporter() { - JmxReporter reporter = JmxReporter.forRegistry(registry) - .build(); + JmxReporter reporter = JmxReporter.forRegistry(registry).build(); reporter.start(); return reporter; } diff --git a/spring-boot/src/main/java/org/baeldung/endpoints/MyHealthCheck.java b/spring-boot/src/main/java/org/baeldung/endpoints/MyHealthCheck.java index 68fbffda6e..1a175aed48 100644 --- a/spring-boot/src/main/java/org/baeldung/endpoints/MyHealthCheck.java +++ b/spring-boot/src/main/java/org/baeldung/endpoints/MyHealthCheck.java @@ -10,13 +10,9 @@ public class MyHealthCheck implements HealthIndicator { public Health health() { int errorCode = check(); // perform some specific health check if (errorCode != 0) { - return Health.down() - .withDetail("Error Code", errorCode) - .withDetail("Description", "You custom MyHealthCheck endpoint is down") - .build(); + return Health.down().withDetail("Error Code", errorCode).withDetail("Description", "You custom MyHealthCheck endpoint is down").build(); } - return Health.up() - .build(); + return Health.up().build(); } public int check() { diff --git a/spring-boot/src/main/java/org/baeldung/main/SpringBootApplication.java b/spring-boot/src/main/java/org/baeldung/main/SpringBootApplication.java index 0ab4ecb128..5cc697be65 100644 --- a/spring-boot/src/main/java/org/baeldung/main/SpringBootApplication.java +++ b/spring-boot/src/main/java/org/baeldung/main/SpringBootApplication.java @@ -11,7 +11,6 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; -import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -22,7 +21,7 @@ import java.util.concurrent.Executors; @RestController @EnableAutoConfiguration(exclude = MySQLAutoconfiguration.class) -@ComponentScan({ "org.baeldung.common.error", "org.baeldung.common.error.controller", "org.baeldung.common.properties", "org.baeldung.common.resources","org.baeldung.endpoints", "org.baeldung.service", "org.baeldung.monitor.jmx", "org.baeldung.boot.config"}) +@ComponentScan({ "org.baeldung.common.error", "org.baeldung.common.error.controller", "org.baeldung.common.properties", "org.baeldung.common.resources", "org.baeldung.endpoints", "org.baeldung.service", "org.baeldung.monitor.jmx", "org.baeldung.boot.config" }) public class SpringBootApplication { private static ApplicationContext applicationContext; diff --git a/spring-boot/src/main/java/org/baeldung/service/LoginServiceImpl.java b/spring-boot/src/main/java/org/baeldung/service/LoginServiceImpl.java index 6d89f7f695..ed0090f8e4 100644 --- a/spring-boot/src/main/java/org/baeldung/service/LoginServiceImpl.java +++ b/spring-boot/src/main/java/org/baeldung/service/LoginServiceImpl.java @@ -16,8 +16,7 @@ public class LoginServiceImpl implements LoginService { public boolean login(String userName, char[] password) { boolean success; - if (userName.equals("admin") && "secret".toCharArray() - .equals(password)) { + if (userName.equals("admin") && "secret".toCharArray().equals(password)) { counterService.increment("counter.login.success"); success = true; } else { diff --git a/spring-boot/src/main/java/org/baeldung/session/exception/repository/FooRepositoryImpl.java b/spring-boot/src/main/java/org/baeldung/session/exception/repository/FooRepositoryImpl.java index 11df542d05..52407a2117 100644 --- a/spring-boot/src/main/java/org/baeldung/session/exception/repository/FooRepositoryImpl.java +++ b/spring-boot/src/main/java/org/baeldung/session/exception/repository/FooRepositoryImpl.java @@ -14,14 +14,12 @@ public class FooRepositoryImpl implements FooRepository { @Override public void save(Foo foo) { - sessionFactory.getCurrentSession() - .saveOrUpdate(foo); + sessionFactory.getCurrentSession().saveOrUpdate(foo); } @Override public Foo get(Integer id) { - return sessionFactory.getCurrentSession() - .get(Foo.class, id); + return sessionFactory.getCurrentSession().get(Foo.class, id); } } \ No newline at end of file diff --git a/spring-boot/src/main/resources/application.properties b/spring-boot/src/main/resources/application.properties index 458b4e0d46..059a6c96be 100644 --- a/spring-boot/src/main/resources/application.properties +++ b/spring-boot/src/main/resources/application.properties @@ -1,4 +1,4 @@ -server.port=8080 +server.port=9090 server.contextPath=/springbootapp management.port=8081 management.address=127.0.0.1 diff --git a/spring-boot/src/main/resources/static/internationalization.js b/spring-boot/src/main/resources/static/internationalization.js new file mode 100644 index 0000000000..ae416f083d --- /dev/null +++ b/spring-boot/src/main/resources/static/internationalization.js @@ -0,0 +1,8 @@ +$(document).ready(function() { + $("#locales").change(function () { + var selectedOption = $('#locales').val(); + if (selectedOption != ''){ + window.location.replace('international?lang=' + selectedOption); + } + }); +}); \ No newline at end of file diff --git a/spring-boot/src/main/resources/templates/international.html b/spring-boot/src/main/resources/templates/international.html index a2a5fbb591..e0cfb5143b 100644 --- a/spring-boot/src/main/resources/templates/international.html +++ b/spring-boot/src/main/resources/templates/international.html @@ -4,16 +4,7 @@ Home - +

diff --git a/spring-boot/src/test/java/com/baeldung/annotation/servletcomponentscan/SpringBootWithServletComponentIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/annotation/servletcomponentscan/SpringBootWithServletComponentIntegrationTest.java index b2b14f766e..6d958ea637 100644 --- a/spring-boot/src/test/java/com/baeldung/annotation/servletcomponentscan/SpringBootWithServletComponentIntegrationTest.java +++ b/spring-boot/src/test/java/com/baeldung/annotation/servletcomponentscan/SpringBootWithServletComponentIntegrationTest.java @@ -40,8 +40,7 @@ public class SpringBootWithServletComponentIntegrationTest { FilterRegistration filterRegistration = servletContext.getFilterRegistration("hello filter"); assertNotNull(filterRegistration); - assertTrue(filterRegistration.getServletNameMappings() - .contains("echo servlet")); + assertTrue(filterRegistration.getServletNameMappings().contains("echo servlet")); } @Autowired diff --git a/spring-boot/src/test/java/com/baeldung/displayallbeans/DisplayBeanIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/displayallbeans/DisplayBeanIntegrationTest.java index 8bd9c20c5e..afe7f8df31 100644 --- a/spring-boot/src/test/java/com/baeldung/displayallbeans/DisplayBeanIntegrationTest.java +++ b/spring-boot/src/test/java/com/baeldung/displayallbeans/DisplayBeanIntegrationTest.java @@ -60,11 +60,8 @@ public class DisplayBeanIntegrationTest { @SuppressWarnings("rawtypes") ResponseEntity entity = this.testRestTemplate.getForEntity("http://localhost:" + this.mgt + "/springbeans", List.class); - List> allBeans = (List) ((Map) entity.getBody() - .get(0)).get("beans"); - List beanNamesList = allBeans.stream() - .map(x -> (String) x.get("bean")) - .collect(Collectors.toList()); + List> allBeans = (List) ((Map) entity.getBody().get(0)).get("beans"); + List beanNamesList = allBeans.stream().map(x -> (String) x.get("bean")).collect(Collectors.toList()); assertThat(beanNamesList, hasItem("fooController")); assertThat(beanNamesList, hasItem("fooService")); diff --git a/spring-boot/src/test/java/com/baeldung/intro/AppLiveTest.java b/spring-boot/src/test/java/com/baeldung/intro/AppLiveTest.java index 4856c17c7d..2c0152b97f 100644 --- a/spring-boot/src/test/java/com/baeldung/intro/AppLiveTest.java +++ b/spring-boot/src/test/java/com/baeldung/intro/AppLiveTest.java @@ -26,18 +26,12 @@ public class AppLiveTest { @Test public void getIndex() throws Exception { - mvc.perform(MockMvcRequestBuilders.get("/") - .accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andExpect(content().string(equalTo("Index Page"))); + mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().string(equalTo("Index Page"))); } @Test public void getLocal() throws Exception { - mvc.perform(MockMvcRequestBuilders.get("/local") - .accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andExpect(content().string(equalTo("/local"))); + mvc.perform(MockMvcRequestBuilders.get("/local").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().string(equalTo("/local"))); } } \ No newline at end of file diff --git a/spring-boot/src/test/java/com/baeldung/kong/KongAdminAPILiveTest.java b/spring-boot/src/test/java/com/baeldung/kong/KongAdminAPILiveTest.java new file mode 100644 index 0000000000..5cf19dd1db --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/kong/KongAdminAPILiveTest.java @@ -0,0 +1,166 @@ +package com.baeldung.kong; + +import com.baeldung.kong.domain.APIObject; +import com.baeldung.kong.domain.ConsumerObject; +import com.baeldung.kong.domain.KeyAuthObject; +import com.baeldung.kong.domain.PluginObject; +import org.junit.Before; +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.boot.test.web.client.TestRestTemplate; +import org.springframework.http.*; +import org.springframework.test.context.junit4.SpringRunner; + +import java.net.URI; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT; + +/** + * @author aiet + */ +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = DEFINED_PORT, classes = StockApp.class) +public class KongAdminAPILiveTest { + + private String getStockPrice(String code) { + try { + return restTemplate.getForObject(new URI("http://localhost:8080/stock/" + code), String.class); + } catch (Exception ignored) { + } + return null; + } + + @Before + public void init() { + System.setProperty("sun.net.http.allowRestrictedHeaders", "true"); + } + + @Autowired + TestRestTemplate restTemplate; + + @Test + public void givenEndpoint_whenQueryStockPrice_thenPriceCorrect() { + String response = getStockPrice("btc"); + assertEquals("10000", response); + + response = getStockPrice("eth"); + assertEquals("N/A", response); + } + + @Test + public void givenKongAdminAPI_whenAddAPI_thenAPIAccessibleViaKong() throws Exception { + restTemplate.delete("http://localhost:8001/apis/stock-api"); + + APIObject stockAPI = new APIObject("stock-api", "stock.api", "http://localhost:8080", "/"); + HttpEntity apiEntity = new HttpEntity<>(stockAPI); + ResponseEntity addAPIResp = restTemplate.postForEntity("http://localhost:8001/apis", apiEntity, String.class); + + assertEquals(HttpStatus.CREATED, addAPIResp.getStatusCode()); + + addAPIResp = restTemplate.postForEntity("http://localhost:8001/apis", apiEntity, String.class); + assertEquals(HttpStatus.CONFLICT, addAPIResp.getStatusCode()); + String apiListResp = restTemplate.getForObject("http://localhost:8001/apis/", String.class); + + assertTrue(apiListResp.contains("stock-api")); + + HttpHeaders headers = new HttpHeaders(); + headers.set("Host", "stock.api"); + RequestEntity requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc")); + ResponseEntity stockPriceResp = restTemplate.exchange(requestEntity, String.class); + + assertEquals("10000", stockPriceResp.getBody()); + } + + @Test + public void givenKongAdminAPI_whenAddAPIConsumer_thenAdded() { + restTemplate.delete("http://localhost:8001/consumers/eugenp"); + + ConsumerObject consumer = new ConsumerObject("eugenp"); + HttpEntity addConsumerEntity = new HttpEntity<>(consumer); + ResponseEntity addConsumerResp = restTemplate.postForEntity("http://localhost:8001/consumers/", addConsumerEntity, String.class); + + assertEquals(HttpStatus.CREATED, addConsumerResp.getStatusCode()); + + addConsumerResp = restTemplate.postForEntity("http://localhost:8001/consumers", addConsumerEntity, String.class); + assertEquals(HttpStatus.CONFLICT, addConsumerResp.getStatusCode()); + + String consumerListResp = restTemplate.getForObject("http://localhost:8001/consumers/", String.class); + assertTrue(consumerListResp.contains("eugenp")); + } + + @Test + public void givenAPI_whenEnableAuth_thenAnonymousDenied() throws Exception { + String apiListResp = restTemplate.getForObject("http://localhost:8001/apis/", String.class); + if (!apiListResp.contains("stock-api")) { + givenKongAdminAPI_whenAddAPI_thenAPIAccessibleViaKong(); + } + + PluginObject authPlugin = new PluginObject("key-auth"); + ResponseEntity enableAuthResp = restTemplate.postForEntity("http://localhost:8001/apis/stock-api/plugins", new HttpEntity<>(authPlugin), String.class); + + assertTrue(HttpStatus.CREATED == enableAuthResp.getStatusCode() || HttpStatus.CONFLICT == enableAuthResp.getStatusCode()); + + String pluginsResp = restTemplate.getForObject("http://localhost:8001/apis/stock-api/plugins", String.class); + assertTrue(pluginsResp.contains("key-auth")); + + HttpHeaders headers = new HttpHeaders(); + headers.set("Host", "stock.api"); + RequestEntity requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc")); + ResponseEntity stockPriceResp = restTemplate.exchange(requestEntity, String.class); + assertEquals(HttpStatus.UNAUTHORIZED, stockPriceResp.getStatusCode()); + } + + @Test + public void givenAPIAuthEnabled_whenAddKey_thenAccessAllowed() throws Exception { + String apiListResp = restTemplate.getForObject("http://localhost:8001/apis/", String.class); + if (!apiListResp.contains("stock-api")) { + givenKongAdminAPI_whenAddAPI_thenAPIAccessibleViaKong(); + } + + String consumerListResp = restTemplate.getForObject("http://localhost:8001/consumers/", String.class); + if (!consumerListResp.contains("eugenp")) { + givenKongAdminAPI_whenAddAPIConsumer_thenAdded(); + } + + final String consumerKey = "eugenp.pass"; + KeyAuthObject keyAuth = new KeyAuthObject(consumerKey); + ResponseEntity keyAuthResp = restTemplate.postForEntity("http://localhost:8001/consumers/eugenp/key-auth", new HttpEntity<>(keyAuth), String.class); + + assertTrue(HttpStatus.CREATED == keyAuthResp.getStatusCode() || HttpStatus.CONFLICT == keyAuthResp.getStatusCode()); + + HttpHeaders headers = new HttpHeaders(); + headers.set("Host", "stock.api"); + headers.set("apikey", consumerKey); + RequestEntity requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc")); + ResponseEntity stockPriceResp = restTemplate.exchange(requestEntity, String.class); + + assertEquals("10000", stockPriceResp.getBody()); + + headers.set("apikey", "wrongpass"); + requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc")); + stockPriceResp = restTemplate.exchange(requestEntity, String.class); + assertEquals(HttpStatus.FORBIDDEN, stockPriceResp.getStatusCode()); + } + + @Test + public void givenAdminAPIProxy_whenAddAPIViaProxy_thenAPIAdded() throws Exception { + APIObject adminAPI = new APIObject("admin-api", "admin.api", "http://localhost:8001", "/admin-api"); + HttpEntity apiEntity = new HttpEntity<>(adminAPI); + ResponseEntity addAPIResp = restTemplate.postForEntity("http://localhost:8001/apis", apiEntity, String.class); + + assertTrue(HttpStatus.CREATED == addAPIResp.getStatusCode() || HttpStatus.CONFLICT == addAPIResp.getStatusCode()); + + HttpHeaders headers = new HttpHeaders(); + headers.set("Host", "admin.api"); + APIObject baeldungAPI = new APIObject("baeldung-api", "baeldung.com", "http://ww.baeldung.com", "/"); + RequestEntity requestEntity = new RequestEntity<>(baeldungAPI, headers, HttpMethod.POST, new URI("http://localhost:8000/admin-api/apis")); + addAPIResp = restTemplate.exchange(requestEntity, String.class); + + assertTrue(HttpStatus.CREATED == addAPIResp.getStatusCode() || HttpStatus.CONFLICT == addAPIResp.getStatusCode()); + } + +} diff --git a/spring-boot/src/test/java/com/baeldung/kong/KongLoadBalanceLiveTest.java b/spring-boot/src/test/java/com/baeldung/kong/KongLoadBalanceLiveTest.java new file mode 100644 index 0000000000..abc7151720 --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/kong/KongLoadBalanceLiveTest.java @@ -0,0 +1,69 @@ +package com.baeldung.kong; + +import com.baeldung.kong.domain.APIObject; +import com.baeldung.kong.domain.TargetObject; +import com.baeldung.kong.domain.UpstreamObject; +import org.junit.Before; +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.boot.test.web.client.TestRestTemplate; +import org.springframework.http.*; +import org.springframework.test.context.junit4.SpringRunner; + +import java.net.URI; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT; + +/** + * @author aiet + */ +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = DEFINED_PORT, classes = StockApp.class) +public class KongLoadBalanceLiveTest { + + @Before + public void init() { + System.setProperty("sun.net.http.allowRestrictedHeaders", "true"); + } + + @Autowired + TestRestTemplate restTemplate; + + @Test + public void givenKongAdminAPI_whenAddAPI_thenAPIAccessibleViaKong() throws Exception { + UpstreamObject upstream = new UpstreamObject("stock.api.service"); + ResponseEntity addUpstreamResp = restTemplate.postForEntity("http://localhost:8001/upstreams", new HttpEntity<>(upstream), String.class); + assertTrue(HttpStatus.CREATED == addUpstreamResp.getStatusCode() || HttpStatus.CONFLICT == addUpstreamResp.getStatusCode()); + + TargetObject testTarget = new TargetObject("localhost:8080", 10); + ResponseEntity addTargetResp = restTemplate.postForEntity("http://localhost:8001/upstreams/stock.api.service/targets", new HttpEntity<>(testTarget), String.class); + assertTrue(HttpStatus.CREATED == addTargetResp.getStatusCode() || HttpStatus.CONFLICT == addTargetResp.getStatusCode()); + + TargetObject releaseTarget = new TargetObject("localhost:9090", 40); + addTargetResp = restTemplate.postForEntity("http://localhost:8001/upstreams/stock.api.service/targets", new HttpEntity<>(releaseTarget), String.class); + assertTrue(HttpStatus.CREATED == addTargetResp.getStatusCode() || HttpStatus.CONFLICT == addTargetResp.getStatusCode()); + + APIObject stockAPI = new APIObject("balanced-stock-api", "balanced.stock.api", "http://stock.api.service", "/"); + HttpEntity apiEntity = new HttpEntity<>(stockAPI); + ResponseEntity addAPIResp = restTemplate.postForEntity("http://localhost:8001/apis", apiEntity, String.class); + assertTrue(HttpStatus.CREATED == addAPIResp.getStatusCode() || HttpStatus.CONFLICT == addAPIResp.getStatusCode()); + + HttpHeaders headers = new HttpHeaders(); + headers.set("Host", "balanced.stock.api"); + for (int i = 0; i < 1000; i++) { + RequestEntity requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc")); + ResponseEntity stockPriceResp = restTemplate.exchange(requestEntity, String.class); + assertEquals("10000", stockPriceResp.getBody()); + } + + int releaseCount = restTemplate.getForObject("http://localhost:9090/stock/reqcount", Integer.class); + int testCount = restTemplate.getForObject("http://localhost:8080/stock/reqcount", Integer.class); + + assertTrue(Math.round(releaseCount * 1.0 / testCount) == 4); + } + +} diff --git a/spring-boot/src/test/java/com/baeldung/kong/domain/APIObject.java b/spring-boot/src/test/java/com/baeldung/kong/domain/APIObject.java new file mode 100644 index 0000000000..f386712444 --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/kong/domain/APIObject.java @@ -0,0 +1,54 @@ +package com.baeldung.kong.domain; + +/** + * @author aiet + */ +public class APIObject { + + public APIObject() { + } + + public APIObject(String name, String hosts, String upstream_url, String uris) { + this.name = name; + this.hosts = hosts; + this.upstream_url = upstream_url; + this.uris = uris; + } + + private String name; + private String hosts; + private String upstream_url; + private String uris; + + public String getUris() { + return uris; + } + + public void setUris(String uris) { + this.uris = uris; + } + + public String getUpstream_url() { + return upstream_url; + } + + public void setUpstream_url(String upstream_url) { + this.upstream_url = upstream_url; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getHosts() { + return hosts; + } + + public void setHosts(String hosts) { + this.hosts = hosts; + } +} diff --git a/spring-boot/src/test/java/com/baeldung/kong/domain/ConsumerObject.java b/spring-boot/src/test/java/com/baeldung/kong/domain/ConsumerObject.java new file mode 100644 index 0000000000..74bef8f2d1 --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/kong/domain/ConsumerObject.java @@ -0,0 +1,35 @@ +package com.baeldung.kong.domain; + +/** + * @author aiet + */ +public class ConsumerObject { + + private String username; + private String custom_id; + + public ConsumerObject(String username) { + this.username = username; + } + + public ConsumerObject(String username, String custom_id) { + this.username = username; + this.custom_id = custom_id; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getCustom_id() { + return custom_id; + } + + public void setCustom_id(String custom_id) { + this.custom_id = custom_id; + } +} diff --git a/spring-boot/src/test/java/com/baeldung/kong/domain/KeyAuthObject.java b/spring-boot/src/test/java/com/baeldung/kong/domain/KeyAuthObject.java new file mode 100644 index 0000000000..80de6bfcd9 --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/kong/domain/KeyAuthObject.java @@ -0,0 +1,21 @@ +package com.baeldung.kong.domain; + +/** + * @author aiet + */ +public class KeyAuthObject { + + public KeyAuthObject(String key) { + this.key = key; + } + + private String key; + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } +} diff --git a/spring-boot/src/test/java/com/baeldung/kong/domain/PluginObject.java b/spring-boot/src/test/java/com/baeldung/kong/domain/PluginObject.java new file mode 100644 index 0000000000..c161fc9b54 --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/kong/domain/PluginObject.java @@ -0,0 +1,30 @@ +package com.baeldung.kong.domain; + +/** + * @author aiet + */ +public class PluginObject { + + private String name; + private String consumer_id; + + public PluginObject(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getConsumer_id() { + return consumer_id; + } + + public void setConsumer_id(String consumer_id) { + this.consumer_id = consumer_id; + } +} diff --git a/spring-boot/src/test/java/com/baeldung/kong/domain/TargetObject.java b/spring-boot/src/test/java/com/baeldung/kong/domain/TargetObject.java new file mode 100644 index 0000000000..79653e2846 --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/kong/domain/TargetObject.java @@ -0,0 +1,31 @@ +package com.baeldung.kong.domain; + +/** + * @author aiet + */ +public class TargetObject { + + public TargetObject(String target, int weight) { + this.target = target; + this.weight = weight; + } + + private String target; + private int weight; + + public String getTarget() { + return target; + } + + public void setTarget(String target) { + this.target = target; + } + + public int getWeight() { + return weight; + } + + public void setWeight(int weight) { + this.weight = weight; + } +} diff --git a/spring-boot/src/test/java/com/baeldung/kong/domain/UpstreamObject.java b/spring-boot/src/test/java/com/baeldung/kong/domain/UpstreamObject.java new file mode 100644 index 0000000000..6461381ac5 --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/kong/domain/UpstreamObject.java @@ -0,0 +1,21 @@ +package com.baeldung.kong.domain; + +/** + * @author aiet + */ +public class UpstreamObject { + + public UpstreamObject(String name) { + this.name = name; + } + + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/spring-boot/src/test/java/com/baeldung/toggle/ToggleIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/toggle/ToggleIntegrationTest.java index 5b70fa3cbe..ca6230e8f5 100644 --- a/spring-boot/src/test/java/com/baeldung/toggle/ToggleIntegrationTest.java +++ b/spring-boot/src/test/java/com/baeldung/toggle/ToggleIntegrationTest.java @@ -35,8 +35,7 @@ public class ToggleIntegrationTest { @Before public void setup() { - this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac) - .build(); + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); } @Test @@ -46,8 +45,7 @@ public class ToggleIntegrationTest { System.setProperty("employee.feature", "false"); - mockMvc.perform(post("/increaseSalary").param("id", emp.getId() + "")) - .andExpect(status().is(200)); + mockMvc.perform(post("/increaseSalary").param("id", emp.getId() + "")).andExpect(status().is(200)); emp = employeeRepository.findOne(1L); assertEquals("salary incorrect", 2000, emp.getSalary(), 0.5); @@ -60,8 +58,7 @@ public class ToggleIntegrationTest { System.setProperty("employee.feature", "true"); - mockMvc.perform(post("/increaseSalary").param("id", emp.getId() + "")) - .andExpect(status().is(200)); + mockMvc.perform(post("/increaseSalary").param("id", emp.getId() + "")).andExpect(status().is(200)); emp = employeeRepository.findOne(1L); assertEquals("salary incorrect", 2200, emp.getSalary(), 0.5); diff --git a/spring-boot/src/test/java/com/baeldung/utils/UtilsControllerIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/utils/UtilsControllerIntegrationTest.java index 5a80e13791..080f660c40 100644 --- a/spring-boot/src/test/java/com/baeldung/utils/UtilsControllerIntegrationTest.java +++ b/spring-boot/src/test/java/com/baeldung/utils/UtilsControllerIntegrationTest.java @@ -21,17 +21,14 @@ public class UtilsControllerIntegrationTest { @Before public void setup() { MockitoAnnotations.initMocks(this); - this.mockMvc = MockMvcBuilders.standaloneSetup(utilsController) - .build(); + this.mockMvc = MockMvcBuilders.standaloneSetup(utilsController).build(); } @Test public void givenParameter_setRequestParam_andSetSessionAttribute() throws Exception { String param = "testparam"; - this.mockMvc.perform(post("/setParam").param("param", param) - .sessionAttr("parameter", param)) - .andExpect(status().isOk()); + this.mockMvc.perform(post("/setParam").param("param", param).sessionAttr("parameter", param)).andExpect(status().isOk()); } } diff --git a/spring-boot/src/test/java/com/baeldung/websocket/client/MyStompSessionHandlerIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/websocket/client/MyStompSessionHandlerIntegrationTest.java index 4dc4793ce2..bb1b5e254e 100644 --- a/spring-boot/src/test/java/com/baeldung/websocket/client/MyStompSessionHandlerIntegrationTest.java +++ b/spring-boot/src/test/java/com/baeldung/websocket/client/MyStompSessionHandlerIntegrationTest.java @@ -14,9 +14,7 @@ public class MyStompSessionHandlerIntegrationTest { StompHeaders mockHeader = Mockito.mock(StompHeaders.class); MyStompSessionHandler sessionHandler = new MyStompSessionHandler(); sessionHandler.afterConnected(mockSession, mockHeader); - Mockito.verify(mockSession) - .subscribe("/topic/messages", sessionHandler); - Mockito.verify(mockSession) - .send(Mockito.anyString(), Mockito.anyObject()); + Mockito.verify(mockSession).subscribe("/topic/messages", sessionHandler); + Mockito.verify(mockSession).send(Mockito.anyString(), Mockito.anyObject()); } } diff --git a/spring-boot/src/test/java/org/baeldung/SpringBootApplicationIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/SpringBootApplicationIntegrationTest.java index 823625f811..5627543b62 100644 --- a/spring-boot/src/test/java/org/baeldung/SpringBootApplicationIntegrationTest.java +++ b/spring-boot/src/test/java/org/baeldung/SpringBootApplicationIntegrationTest.java @@ -34,56 +34,36 @@ public class SpringBootApplicationIntegrationTest { @Before public void setupMockMvc() { - mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) - .build(); + mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); } @Test public void givenRequestHasBeenMade_whenMeetsAllOfGivenConditions_thenCorrect() throws Exception { MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); - mockMvc.perform(MockMvcRequestBuilders.get("/entity/all")) - .andExpect(MockMvcResultMatchers.status() - .isOk()) - .andExpect(MockMvcResultMatchers.content() - .contentType(contentType)) - .andExpect(jsonPath("$", hasSize(4))); + mockMvc.perform(MockMvcRequestBuilders.get("/entity/all")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType)).andExpect(jsonPath("$", hasSize(4))); } @Test public void givenRequestHasBeenMade_whenMeetsFindByDateOfGivenConditions_thenCorrect() throws Exception { MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); - mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbydate/{date}", "2011-12-03T10:15:30")) - .andExpect(MockMvcResultMatchers.status() - .isOk()) - .andExpect(MockMvcResultMatchers.content() - .contentType(contentType)) - .andExpect(jsonPath("$.id", equalTo(1))); + mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbydate/{date}", "2011-12-03T10:15:30")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType)) + .andExpect(jsonPath("$.id", equalTo(1))); } @Test public void givenRequestHasBeenMade_whenMeetsFindByModeOfGivenConditions_thenCorrect() throws Exception { MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); - mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbymode/{mode}", Modes.ALPHA.name())) - .andExpect(MockMvcResultMatchers.status() - .isOk()) - .andExpect(MockMvcResultMatchers.content() - .contentType(contentType)) - .andExpect(jsonPath("$.id", equalTo(1))); + mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbymode/{mode}", Modes.ALPHA.name())).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType)).andExpect(jsonPath("$.id", equalTo(1))); } @Test public void givenRequestHasBeenMade_whenMeetsFindByVersionOfGivenConditions_thenCorrect() throws Exception { MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); - mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbyversion") - .header("Version", "1.0.0")) - .andExpect(MockMvcResultMatchers.status() - .isOk()) - .andExpect(MockMvcResultMatchers.content() - .contentType(contentType)) - .andExpect(jsonPath("$.id", equalTo(1))); + mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbyversion").header("Version", "1.0.0")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType)) + .andExpect(jsonPath("$.id", equalTo(1))); } } \ No newline at end of file diff --git a/spring-boot/src/test/java/org/baeldung/SpringBootMailIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/SpringBootMailIntegrationTest.java index 17e7d2d9e0..a6a84184fa 100644 --- a/spring-boot/src/test/java/org/baeldung/SpringBootMailIntegrationTest.java +++ b/spring-boot/src/test/java/org/baeldung/SpringBootMailIntegrationTest.java @@ -10,8 +10,6 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; -import org.springframework.web.context.WebApplicationContext; import org.subethamail.wiser.Wiser; import org.subethamail.wiser.WiserMessage; @@ -63,15 +61,11 @@ public class SpringBootMailIntegrationTest { } private String getMessage(WiserMessage wiserMessage) throws MessagingException, IOException { - return wiserMessage.getMimeMessage() - .getContent() - .toString() - .trim(); + return wiserMessage.getMimeMessage().getContent().toString().trim(); } private String getSubject(WiserMessage wiserMessage) throws MessagingException { - return wiserMessage.getMimeMessage() - .getSubject(); + return wiserMessage.getMimeMessage().getSubject(); } private SimpleMailMessage composeEmailMessage() { diff --git a/spring-boot/src/test/java/org/baeldung/boot/client/DetailsServiceClientIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/boot/client/DetailsServiceClientIntegrationTest.java index 6f1cc66979..37fc202e8a 100644 --- a/spring-boot/src/test/java/org/baeldung/boot/client/DetailsServiceClientIntegrationTest.java +++ b/spring-boot/src/test/java/org/baeldung/boot/client/DetailsServiceClientIntegrationTest.java @@ -33,8 +33,7 @@ public class DetailsServiceClientIntegrationTest { @Before public void setUp() throws Exception { String detailsString = objectMapper.writeValueAsString(new Details("John Smith", "john")); - this.server.expect(requestTo("/john/details")) - .andRespond(withSuccess(detailsString, MediaType.APPLICATION_JSON)); + this.server.expect(requestTo("/john/details")).andRespond(withSuccess(detailsString, MediaType.APPLICATION_JSON)); } @Test diff --git a/spring-boot/src/test/java/org/baeldung/config/H2TestProfileJPAConfig.java b/spring-boot/src/test/java/org/baeldung/config/H2TestProfileJPAConfig.java index 499a755ae7..d0b92a7a93 100644 --- a/spring-boot/src/test/java/org/baeldung/config/H2TestProfileJPAConfig.java +++ b/spring-boot/src/test/java/org/baeldung/config/H2TestProfileJPAConfig.java @@ -41,7 +41,7 @@ public class H2TestProfileJPAConfig { public LocalContainerEntityManagerFactoryBean entityManagerFactory() { final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource()); - em.setPackagesToScan(new String[] { "org.baeldung.domain", "org.baeldung.boot.domain", "org.baeldung.boot.boottest","org.baeldung.model" }); + em.setPackagesToScan(new String[] { "org.baeldung.domain", "org.baeldung.boot.domain", "org.baeldung.boot.boottest", "org.baeldung.model" }); em.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); em.setJpaProperties(additionalProperties()); return em; diff --git a/spring-boot/src/test/java/org/baeldung/converter/CustomConverterIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/converter/CustomConverterIntegrationTest.java index fc94fe7d7d..bd1ae2c8fa 100644 --- a/spring-boot/src/test/java/org/baeldung/converter/CustomConverterIntegrationTest.java +++ b/spring-boot/src/test/java/org/baeldung/converter/CustomConverterIntegrationTest.java @@ -33,8 +33,7 @@ public class CustomConverterIntegrationTest { public void whenConvertStringToEmployee_thenSuccess() { Employee employee = conversionService.convert("1,50000.00", Employee.class); Employee actualEmployee = new Employee(1, 50000.00); - assertThat(conversionService.convert("1,50000.00", Employee.class)) - .isEqualToComparingFieldByField(actualEmployee); + assertThat(conversionService.convert("1,50000.00", Employee.class)).isEqualToComparingFieldByField(actualEmployee); } @Test @@ -44,11 +43,8 @@ public class CustomConverterIntegrationTest { @Test public void whenConvertingToBigDecimalUsingGenericConverter_thenSuccess() { - assertThat(conversionService.convert(Integer.valueOf(11), BigDecimal.class)) - .isEqualTo(BigDecimal.valueOf(11.00).setScale(2, BigDecimal.ROUND_HALF_EVEN)); - assertThat(conversionService.convert(Double.valueOf(25.23), BigDecimal.class)) - .isEqualByComparingTo(BigDecimal.valueOf(Double.valueOf(25.23))); - assertThat(conversionService.convert("2.32", BigDecimal.class)) - .isEqualTo(BigDecimal.valueOf(2.32)); + assertThat(conversionService.convert(Integer.valueOf(11), BigDecimal.class)).isEqualTo(BigDecimal.valueOf(11.00).setScale(2, BigDecimal.ROUND_HALF_EVEN)); + assertThat(conversionService.convert(Double.valueOf(25.23), BigDecimal.class)).isEqualByComparingTo(BigDecimal.valueOf(Double.valueOf(25.23))); + assertThat(conversionService.convert("2.32", BigDecimal.class)).isEqualTo(BigDecimal.valueOf(2.32)); } } \ No newline at end of file diff --git a/spring-boot/src/test/java/org/baeldung/converter/controller/StringToEmployeeConverterControllerIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/converter/controller/StringToEmployeeConverterControllerIntegrationTest.java index 3310eb9984..466d81e658 100644 --- a/spring-boot/src/test/java/org/baeldung/converter/controller/StringToEmployeeConverterControllerIntegrationTest.java +++ b/spring-boot/src/test/java/org/baeldung/converter/controller/StringToEmployeeConverterControllerIntegrationTest.java @@ -26,10 +26,6 @@ public class StringToEmployeeConverterControllerIntegrationTest { @Test public void getStringToEmployeeTest() throws Exception { - mockMvc.perform(get("/string-to-employee?employee=1,2000")) - .andDo(print()) - .andExpect(jsonPath("$.id", is(1))) - .andExpect(jsonPath("$.salary", is(2000.0))) - .andExpect(status().isOk()); + mockMvc.perform(get("/string-to-employee?employee=1,2000")).andDo(print()).andExpect(jsonPath("$.id", is(1))).andExpect(jsonPath("$.salary", is(2000.0))).andExpect(status().isOk()); } } diff --git a/spring-boot/src/test/java/org/baeldung/demo/boottest/EmployeeControllerIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/demo/boottest/EmployeeControllerIntegrationTest.java index f06c144908..640a8b322a 100644 --- a/spring-boot/src/test/java/org/baeldung/demo/boottest/EmployeeControllerIntegrationTest.java +++ b/spring-boot/src/test/java/org/baeldung/demo/boottest/EmployeeControllerIntegrationTest.java @@ -47,10 +47,7 @@ public class EmployeeControllerIntegrationTest { Employee alex = new Employee("alex"); given(service.save(Mockito.anyObject())).willReturn(alex); - mvc.perform(post("/api/employees").contentType(MediaType.APPLICATION_JSON) - .content(JsonUtil.toJson(alex))) - .andExpect(status().isCreated()) - .andExpect(jsonPath("$.name", is("alex"))); + mvc.perform(post("/api/employees").contentType(MediaType.APPLICATION_JSON).content(JsonUtil.toJson(alex))).andExpect(status().isCreated()).andExpect(jsonPath("$.name", is("alex"))); verify(service, VerificationModeFactory.times(1)).save(Mockito.anyObject()); reset(service); } @@ -65,12 +62,8 @@ public class EmployeeControllerIntegrationTest { given(service.getAllEmployees()).willReturn(allEmployees); - mvc.perform(get("/api/employees").contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andExpect(jsonPath("$", hasSize(3))) - .andExpect(jsonPath("$[0].name", is(alex.getName()))) - .andExpect(jsonPath("$[1].name", is(john.getName()))) - .andExpect(jsonPath("$[2].name", is(bob.getName()))); + mvc.perform(get("/api/employees").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(jsonPath("$", hasSize(3))).andExpect(jsonPath("$[0].name", is(alex.getName()))).andExpect(jsonPath("$[1].name", is(john.getName()))) + .andExpect(jsonPath("$[2].name", is(bob.getName()))); verify(service, VerificationModeFactory.times(1)).getAllEmployees(); reset(service); } diff --git a/spring-boot/src/test/java/org/baeldung/demo/boottest/EmployeeRepositoryIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/demo/boottest/EmployeeRepositoryIntegrationTest.java index 221beda900..f581052596 100644 --- a/spring-boot/src/test/java/org/baeldung/demo/boottest/EmployeeRepositoryIntegrationTest.java +++ b/spring-boot/src/test/java/org/baeldung/demo/boottest/EmployeeRepositoryIntegrationTest.java @@ -66,8 +66,6 @@ public class EmployeeRepositoryIntegrationTest { List allEmployees = employeeRepository.findAll(); - assertThat(allEmployees).hasSize(3) - .extracting(Employee::getName) - .containsOnly(alex.getName(), ron.getName(), bob.getName()); + assertThat(allEmployees).hasSize(3).extracting(Employee::getName).containsOnly(alex.getName(), ron.getName(), bob.getName()); } } diff --git a/spring-boot/src/test/java/org/baeldung/demo/boottest/EmployeeRestControllerIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/demo/boottest/EmployeeRestControllerIntegrationTest.java index e6f2203476..da9df4db7d 100644 --- a/spring-boot/src/test/java/org/baeldung/demo/boottest/EmployeeRestControllerIntegrationTest.java +++ b/spring-boot/src/test/java/org/baeldung/demo/boottest/EmployeeRestControllerIntegrationTest.java @@ -50,12 +50,10 @@ public class EmployeeRestControllerIntegrationTest { @Test public void whenValidInput_thenCreateEmployee() throws IOException, Exception { Employee bob = new Employee("bob"); - mvc.perform(post("/api/employees").contentType(MediaType.APPLICATION_JSON) - .content(JsonUtil.toJson(bob))); + mvc.perform(post("/api/employees").contentType(MediaType.APPLICATION_JSON).content(JsonUtil.toJson(bob))); List found = repository.findAll(); - assertThat(found).extracting(Employee::getName) - .containsOnly("bob"); + assertThat(found).extracting(Employee::getName).containsOnly("bob"); } @Test @@ -64,13 +62,8 @@ public class EmployeeRestControllerIntegrationTest { createTestEmployee("bob"); createTestEmployee("alex"); - mvc.perform(get("/api/employees").contentType(MediaType.APPLICATION_JSON)) - .andDo(print()) - .andExpect(status().isOk()) - .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$", hasSize(greaterThanOrEqualTo(2)))) - .andExpect(jsonPath("$[0].name", is("bob"))) - .andExpect(jsonPath("$[1].name", is("alex"))); + mvc.perform(get("/api/employees").contentType(MediaType.APPLICATION_JSON)).andDo(print()).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andExpect(jsonPath("$", hasSize(greaterThanOrEqualTo(2)))) + .andExpect(jsonPath("$[0].name", is("bob"))).andExpect(jsonPath("$[1].name", is("alex"))); } private void createTestEmployee(String name) { diff --git a/spring-boot/src/test/java/org/baeldung/demo/boottest/EmployeeServiceImplIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/demo/boottest/EmployeeServiceImplIntegrationTest.java index 58ef3d4081..f004536c49 100644 --- a/spring-boot/src/test/java/org/baeldung/demo/boottest/EmployeeServiceImplIntegrationTest.java +++ b/spring-boot/src/test/java/org/baeldung/demo/boottest/EmployeeServiceImplIntegrationTest.java @@ -47,18 +47,12 @@ public class EmployeeServiceImplIntegrationTest { List allEmployees = Arrays.asList(john, bob, alex); - Mockito.when(employeeRepository.findByName(john.getName())) - .thenReturn(john); - Mockito.when(employeeRepository.findByName(alex.getName())) - .thenReturn(alex); - Mockito.when(employeeRepository.findByName("wrong_name")) - .thenReturn(null); - Mockito.when(employeeRepository.findById(john.getId())) - .thenReturn(john); - Mockito.when(employeeRepository.findAll()) - .thenReturn(allEmployees); - Mockito.when(employeeRepository.findById(-99L)) - .thenReturn(null); + Mockito.when(employeeRepository.findByName(john.getName())).thenReturn(john); + Mockito.when(employeeRepository.findByName(alex.getName())).thenReturn(alex); + Mockito.when(employeeRepository.findByName("wrong_name")).thenReturn(null); + Mockito.when(employeeRepository.findById(john.getId())).thenReturn(john); + Mockito.when(employeeRepository.findAll()).thenReturn(allEmployees); + Mockito.when(employeeRepository.findById(-99L)).thenReturn(null); } @Test @@ -116,26 +110,21 @@ public class EmployeeServiceImplIntegrationTest { List allEmployees = employeeService.getAllEmployees(); verifyFindAllEmployeesIsCalledOnce(); - assertThat(allEmployees).hasSize(3) - .extracting(Employee::getName) - .contains(alex.getName(), john.getName(), bob.getName()); + assertThat(allEmployees).hasSize(3).extracting(Employee::getName).contains(alex.getName(), john.getName(), bob.getName()); } private void verifyFindByNameIsCalledOnce(String name) { - Mockito.verify(employeeRepository, VerificationModeFactory.times(1)) - .findByName(name); + Mockito.verify(employeeRepository, VerificationModeFactory.times(1)).findByName(name); Mockito.reset(employeeRepository); } private void verifyFindByIdIsCalledOnce() { - Mockito.verify(employeeRepository, VerificationModeFactory.times(1)) - .findById(Mockito.anyLong()); + Mockito.verify(employeeRepository, VerificationModeFactory.times(1)).findById(Mockito.anyLong()); Mockito.reset(employeeRepository); } private void verifyFindAllEmployeesIsCalledOnce() { - Mockito.verify(employeeRepository, VerificationModeFactory.times(1)) - .findAll(); + Mockito.verify(employeeRepository, VerificationModeFactory.times(1)).findAll(); Mockito.reset(employeeRepository); } } diff --git a/spring-boot/src/test/java/org/baeldung/properties/ConfigPropertiesIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/properties/ConfigPropertiesIntegrationTest.java index ffd5bf55d6..3f3b558db9 100644 --- a/spring-boot/src/test/java/org/baeldung/properties/ConfigPropertiesIntegrationTest.java +++ b/spring-boot/src/test/java/org/baeldung/properties/ConfigPropertiesIntegrationTest.java @@ -23,30 +23,21 @@ public class ConfigPropertiesIntegrationTest { @Test public void whenListPropertyQueriedthenReturnsProperty() throws Exception { - Assert.assertTrue("Couldn't bind list property!", properties.getDefaultRecipients() - .size() == 2); - Assert.assertTrue("Incorrectly bound list property. Expected 2 entries!", properties.getDefaultRecipients() - .size() == 2); + Assert.assertTrue("Couldn't bind list property!", properties.getDefaultRecipients().size() == 2); + Assert.assertTrue("Incorrectly bound list property. Expected 2 entries!", properties.getDefaultRecipients().size() == 2); } @Test public void whenMapPropertyQueriedthenReturnsProperty() throws Exception { Assert.assertTrue("Couldn't bind map property!", properties.getAdditionalHeaders() != null); - Assert.assertTrue("Incorrectly bound map property. Expected 3 Entries!", properties.getAdditionalHeaders() - .size() == 3); + Assert.assertTrue("Incorrectly bound map property. Expected 3 Entries!", properties.getAdditionalHeaders().size() == 3); } @Test public void whenObjectPropertyQueriedthenReturnsProperty() throws Exception { Assert.assertTrue("Couldn't bind map property!", properties.getCredentials() != null); - Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials() - .getAuthMethod() - .equals("SHA1")); - Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials() - .getUsername() - .equals("john")); - Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials() - .getPassword() - .equals("password")); + Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getAuthMethod().equals("SHA1")); + Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getUsername().equals("john")); + Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getPassword().equals("password")); } } diff --git a/spring-boot/src/test/java/org/baeldung/repository/UserRepositoryTest.java b/spring-boot/src/test/java/org/baeldung/repository/UserRepositoryIntegrationTest.java similarity index 92% rename from spring-boot/src/test/java/org/baeldung/repository/UserRepositoryTest.java rename to spring-boot/src/test/java/org/baeldung/repository/UserRepositoryIntegrationTest.java index 227bb02db2..2b61aa6252 100644 --- a/spring-boot/src/test/java/org/baeldung/repository/UserRepositoryTest.java +++ b/spring-boot/src/test/java/org/baeldung/repository/UserRepositoryIntegrationTest.java @@ -23,7 +23,7 @@ import static org.hamcrest.MatcherAssert.assertThat; */ @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) -public class UserRepositoryTest { +public class UserRepositoryIntegrationTest { private final String USER_NAME_ADAM = "Adam"; private final Integer ACTIVE_STATUS = 1; @@ -47,8 +47,7 @@ public class UserRepositoryTest { Optional foundUser = userRepository.findOneByName(USER_NAME_ADAM); assertThat(foundUser.isPresent(), equalTo(true)); - assertThat(foundUser.get() - .getName(), equalTo(USER_NAME_ADAM)); + assertThat(foundUser.get().getName(), equalTo(USER_NAME_ADAM)); } @Test @@ -84,8 +83,7 @@ public class UserRepositoryTest { CompletableFuture userByStatus = userRepository.findOneByStatus(ACTIVE_STATUS); - assertThat(userByStatus.get() - .getName(), equalTo(USER_NAME_ADAM)); + assertThat(userByStatus.get().getName(), equalTo(USER_NAME_ADAM)); } diff --git a/spring-cloud/README.md b/spring-cloud/README.md index b24e5b0296..8ad750a809 100644 --- a/spring-cloud/README.md +++ b/spring-cloud/README.md @@ -12,11 +12,13 @@ Client-side service discovery allows services to find and communicate with each other without hardcoding hostname and port. The only ‘fixed point’ in such an architecture consists of a service registry with which each service has to register. +### Relevant Articles: - [Intro to Spring Cloud Netflix - Hystrix](http://www.baeldung.com/spring-cloud-netflix-hystrix) - [Dockerizing a Spring Boot Application](http://www.baeldung.com/dockerizing-spring-boot-application) - [Introduction to Spring Cloud Rest Client with Netflix Ribbon](http://www.baeldung.com/spring-cloud-rest-client-with-netflix-ribbon) - [A Quick Guide to Spring Cloud Consul](http://www.baeldung.com/spring-cloud-consul) - -### Relevant Articles: -- [Introduction to Spring Cloud Rest Client with Netflix Ribbon](http://www.baeldung.com/spring-cloud-rest-client-with-netflix-ribbon) - [An Introduction to Spring Cloud Zookeeper](http://www.baeldung.com/spring-cloud-zookeeper) +- [An Introduction to Spring Cloud Zookeeper](http://www.baeldung.com/spring-cloud-zookeeper) +- [Using a Spring Cloud App Starter](http://www.baeldung.com/using-a-spring-cloud-app-starter) +- [Spring Cloud Connectors and Heroku](http://www.baeldung.com/spring-cloud-heroku) +- [An Example of Load Balancing with Zuul and Eureka](http://www.baeldung.com/zuul-load-balancing) +- [An Intro to Spring Cloud Contract](http://www.baeldung.com/spring-cloud-contract) diff --git a/spring-cloud/pom.xml b/spring-cloud/pom.xml index d94b334bc8..c093b87be3 100644 --- a/spring-cloud/pom.xml +++ b/spring-cloud/pom.xml @@ -21,7 +21,8 @@ spring-cloud-aws spring-cloud-consul spring-cloud-zuul-eureka-integration - + spring-cloud-contract + pom spring-cloud diff --git a/spring-cloud/spring-cloud-aws/README.md b/spring-cloud/spring-cloud-aws/README.md index c5f58159c8..1340712c7a 100644 --- a/spring-cloud/spring-cloud-aws/README.md +++ b/spring-cloud/spring-cloud-aws/README.md @@ -19,3 +19,8 @@ to write the following in `application.properties`: cloud.aws.rds.spring-cloud-test-db cloud.aws.rds.spring-cloud-test-db.password=se3retpass ``` +Multiple application classes are available under this project. To launch InstanceProfileAwsApplication application, replace `start-class` under `pom.xml`: + +``` +com.baeldung.spring.cloud.aws.InstanceProfileAwsApplication +``` \ No newline at end of file diff --git a/spring-cloud/spring-cloud-aws/pom.xml b/spring-cloud/spring-cloud-aws/pom.xml index 632e050d92..b27b6c0d18 100644 --- a/spring-cloud/spring-cloud-aws/pom.xml +++ b/spring-cloud/spring-cloud-aws/pom.xml @@ -19,6 +19,7 @@ + com.baeldung.spring.cloud.aws.SpringCloudAwsApplication UTF-8 UTF-8 1.8 diff --git a/spring-cloud/spring-cloud-aws/src/main/java/com/baeldung/spring/cloud/aws/InstanceProfileAwsApplication.java b/spring-cloud/spring-cloud-aws/src/main/java/com/baeldung/spring/cloud/aws/InstanceProfileAwsApplication.java new file mode 100644 index 0000000000..80c0851a6f --- /dev/null +++ b/spring-cloud/spring-cloud-aws/src/main/java/com/baeldung/spring/cloud/aws/InstanceProfileAwsApplication.java @@ -0,0 +1,60 @@ +package com.baeldung.spring.cloud.aws; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.UUID; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.spring.cloud.aws.s3.SpringCloudS3Service; + +@Configuration +@EnableAutoConfiguration +@ComponentScan("com.baeldung.spring.cloud.aws.s3") +public class InstanceProfileAwsApplication { + + private static final Logger logger = LoggerFactory.getLogger(InstanceProfileAwsApplication.class); + private static final String applicationConfig = "spring.config.name:application-instance-profile"; + + private static String bucketName; + private static String fileName = "sample-file.txt"; + + private static void setupResources() { + bucketName = "baeldung-test-" + UUID.randomUUID() + .toString(); + try { + Files.write(Paths.get(fileName), "Hello World!".getBytes()); + } catch (IOException e) { + logger.error(e.getMessage(), e); + } + } + + public static void main(String[] args) { + setupResources(); + if (!new File(fileName).exists()) { + logger.warn("Not able to create {} file. Check your folder permissions.", fileName); + System.exit(1); + } + + SpringApplication application = new SpringApplicationBuilder(InstanceProfileAwsApplication.class).properties(applicationConfig) + .build(); + ConfigurableApplicationContext context = application.run(args); + SpringCloudS3Service service = context.getBean(SpringCloudS3Service.class); + + // S3 bucket operations + service.createBucket(bucketName); + service.uploadObject(bucketName, fileName); + service.downloadObject(bucketName, fileName); + service.deleteBucket(bucketName); + } + +} diff --git a/spring-cloud/spring-cloud-aws/src/main/java/com/baeldung/spring/cloud/aws/s3/SpringCloudS3Service.java b/spring-cloud/spring-cloud-aws/src/main/java/com/baeldung/spring/cloud/aws/s3/SpringCloudS3Service.java new file mode 100644 index 0000000000..a0fdce2143 --- /dev/null +++ b/spring-cloud/spring-cloud-aws/src/main/java/com/baeldung/spring/cloud/aws/s3/SpringCloudS3Service.java @@ -0,0 +1,64 @@ +package com.baeldung.spring.cloud.aws.s3; + +import java.io.File; +import java.io.IOException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.model.ListObjectsV2Result; +import com.amazonaws.services.s3.model.S3ObjectSummary; + +@Component +public class SpringCloudS3Service { + + private static final Logger logger = LoggerFactory.getLogger(SpringCloudS3Service.class); + + @Autowired + AmazonS3 amazonS3; + + @Autowired + SpringCloudS3 springCloudS3; + + public void createBucket(String bucketName) { + logger.debug("Creating S3 bucket: {}", bucketName); + amazonS3.createBucket(bucketName); + logger.info("{} bucket created successfully", bucketName); + } + + public void downloadObject(String bucketName, String objectName) { + String s3Url = "s3://" + bucketName + "/" + objectName; + try { + springCloudS3.downloadS3Object(s3Url); + logger.info("{} file download result: {}", objectName, new File(objectName).exists()); + } catch (IOException e) { + logger.error(e.getMessage(), e); + } + } + + public void uploadObject(String bucketName, String objectName) { + String s3Url = "s3://" + bucketName + "/" + objectName; + File file = new File(objectName); + try { + springCloudS3.uploadFileToS3(file, s3Url); + logger.info("{} file uploaded to S3", objectName); + } catch (IOException e) { + logger.error(e.getMessage(), e); + } + } + + public void deleteBucket(String bucketName) { + logger.trace("Deleting S3 objects under {} bucket...", bucketName); + ListObjectsV2Result listObjectsV2Result = amazonS3.listObjectsV2(bucketName); + for (S3ObjectSummary objectSummary : listObjectsV2Result.getObjectSummaries()) { + logger.info("Deleting S3 object: {}", objectSummary.getKey()); + amazonS3.deleteObject(bucketName, objectSummary.getKey()); + } + logger.info("Deleting S3 bucket: {}", bucketName); + amazonS3.deleteBucket(bucketName); + } + +} diff --git a/spring-cloud/spring-cloud-aws/src/main/resources/InstanceProfileFormation.yaml b/spring-cloud/spring-cloud-aws/src/main/resources/InstanceProfileFormation.yaml new file mode 100644 index 0000000000..f8c3d3c915 --- /dev/null +++ b/spring-cloud/spring-cloud-aws/src/main/resources/InstanceProfileFormation.yaml @@ -0,0 +1,78 @@ +AWSTemplateFormatVersion: 2010-09-09 +Metadata: + 'AWS::CloudFormation::Designer': + 157e7d5f-5cb3-4a23-a50c-97e7f6c57173: + size: + width: 60 + height: 60 + position: + x: 450 + 'y': 90 + z: 0 + embeds: [] + 9bbaaa55-9cba-4555-a7c6-fb6ac248fd3a: + size: + width: 60 + height: 60 + position: + x: 260 + 'y': 90 + z: 0 + embeds: [] + isassociatedwith: + - 157e7d5f-5cb3-4a23-a50c-97e7f6c57173 + a7348729-a594-4dca-9b0a-e1c8d777dc3b: + size: + width: 60 + height: 60 + position: + x: 70 + 'y': 90 + z: 0 + embeds: [] +Resources: + IAMRoleBaeldung: + Type: 'AWS::IAM::Role' + Properties: + AssumeRolePolicyDocument: + Version: 2012-10-17 + Statement: + - Effect: Allow + Principal: + Service: + - ec2.amazonaws.com + Action: + - 'sts:AssumeRole' + ManagedPolicyArns: + - 'arn:aws:iam::aws:policy/AmazonS3FullAccess' + Metadata: + 'AWS::CloudFormation::Designer': + id: 157e7d5f-5cb3-4a23-a50c-97e7f6c57173 + InstanceProfileBaeldung: + Type: 'AWS::IAM::InstanceProfile' + Properties: + Roles: + - !Ref IAMRoleBaeldung + Metadata: + 'AWS::CloudFormation::Designer': + id: 9bbaaa55-9cba-4555-a7c6-fb6ac248fd3a + EC2Instance: + Type: 'AWS::EC2::Instance' + Properties: + ImageId: ami-2581aa40 + InstanceType: t2.micro + IamInstanceProfile: !Ref InstanceProfileBaeldung + KeyName: Satish-Ohio + UserData: !Base64 + 'Fn::Join': + - '' + - - | + #!/bin/bash + - | + apt -y install openjdk-8-jre-headless + Metadata: + 'AWS::CloudFormation::Designer': + id: a7348729-a594-4dca-9b0a-e1c8d777dc3b + DependsOn: + - InstanceProfileBaeldung + diff --git a/spring-cloud/spring-cloud-aws/src/main/resources/application-instance-profile.properties b/spring-cloud/spring-cloud-aws/src/main/resources/application-instance-profile.properties new file mode 100644 index 0000000000..23ca09c85c --- /dev/null +++ b/spring-cloud/spring-cloud-aws/src/main/resources/application-instance-profile.properties @@ -0,0 +1,14 @@ +# Don't try to create DataSouce when running tests which don't need a DataSource +spring.autoconfigure.exclude=\ + org.springframework.cloud.aws.autoconfigure.jdbc.AmazonRdsDatabaseAutoConfiguration,\ + org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration +cloud.aws.region.auto=true + +# Load instance profile credentials +cloud.aws.credentials.instanceProfile=true + +# Disable auto cloud formation +cloud.aws.stack.auto=false + +# Disable web environment +spring.main.web-environment=false \ No newline at end of file diff --git a/spring-cloud/spring-cloud-contract/pom.xml b/spring-cloud/spring-cloud-contract/pom.xml new file mode 100644 index 0000000000..3981aae2ac --- /dev/null +++ b/spring-cloud/spring-cloud-contract/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + + com.baeldung.spring.cloud + spring-cloud + 1.0.0-SNAPSHOT + + + spring-cloud-contract-producer + spring-cloud-contract-consumer + + pom + + com.baeldung.spring.cloud + spring-cloud-contract + 1.0.0-SNAPSHOT + + + \ No newline at end of file diff --git a/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/pom.xml b/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/pom.xml new file mode 100644 index 0000000000..67fea178eb --- /dev/null +++ b/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/pom.xml @@ -0,0 +1,68 @@ + + + 4.0.0 + + com.baeldung.spring.cloud + spring-cloud-contract-consumer + 1.0.0-SNAPSHOT + jar + + spring-cloud-contract-consumer + Spring Cloud Consumer Sample + + + com.baeldung.spring.cloud + spring-cloud-contract + 1.0.0-SNAPSHOT + .. + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.cloud + spring-cloud-contract-wiremock + 1.2.2.RELEASE + test + + + org.springframework.cloud + spring-cloud-contract-stub-runner + 1.2.2.RELEASE + test + + + org.springframework.boot + spring-boot-starter-web + 1.5.9.RELEASE + + + org.springframework.boot + spring-boot-starter-data-rest + 1.5.9.RELEASE + + + com.baeldung.spring.cloud + spring-cloud-contract-producer + 1.0.0-SNAPSHOT + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/main/java/com/baeldung/spring/cloud/springcloudcontractconsumer/SpringCloudContractConsumerApplication.java b/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/main/java/com/baeldung/spring/cloud/springcloudcontractconsumer/SpringCloudContractConsumerApplication.java new file mode 100644 index 0000000000..7383ae5afa --- /dev/null +++ b/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/main/java/com/baeldung/spring/cloud/springcloudcontractconsumer/SpringCloudContractConsumerApplication.java @@ -0,0 +1,18 @@ +package com.baeldung.spring.cloud.springcloudcontractconsumer; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.web.client.RestTemplate; + +@SpringBootApplication +public class SpringCloudContractConsumerApplication { + public static void main(String[] args) { + SpringApplication.run(SpringCloudContractConsumerApplication.class, args); + } + + @Bean + RestTemplate restTemplate() { + return new RestTemplate(); + } +} diff --git a/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/main/java/com/baeldung/spring/cloud/springcloudcontractconsumer/controller/BasicMathController.java b/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/main/java/com/baeldung/spring/cloud/springcloudcontractconsumer/controller/BasicMathController.java new file mode 100644 index 0000000000..58e6d5d5b8 --- /dev/null +++ b/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/main/java/com/baeldung/spring/cloud/springcloudcontractconsumer/controller/BasicMathController.java @@ -0,0 +1,31 @@ +package com.baeldung.spring.cloud.springcloudcontractconsumer.controller; + + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + + +@RestController +public class BasicMathController { + + @Autowired + private RestTemplate restTemplate; + + @GetMapping("/calculate") + public String checkOddAndEven(@RequestParam("number") Integer number) { + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.add("Content-Type", "application/json"); + + ResponseEntity responseEntity = restTemplate.exchange( + "http://localhost:8090/validate/prime-number?number=" + number, + HttpMethod.GET, + new HttpEntity<>(httpHeaders), + String.class); + + return responseEntity.getBody(); + } +} diff --git a/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/main/resources/application.yml b/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/main/resources/application.yml new file mode 100644 index 0000000000..e69de29bb2 diff --git a/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/test/java/com/baeldung/spring/cloud/springcloudcontractconsumer/controller/BasicMathControllerIntegrationTest.java b/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/test/java/com/baeldung/spring/cloud/springcloudcontractconsumer/controller/BasicMathControllerIntegrationTest.java new file mode 100644 index 0000000000..5cf5c6d3b8 --- /dev/null +++ b/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/test/java/com/baeldung/spring/cloud/springcloudcontractconsumer/controller/BasicMathControllerIntegrationTest.java @@ -0,0 +1,44 @@ +package com.baeldung.spring.cloud.springcloudcontractconsumer.controller; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.contract.stubrunner.spring.AutoConfigureStubRunner; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK) +@AutoConfigureMockMvc +@AutoConfigureJsonTesters +@AutoConfigureStubRunner(workOffline = true, + ids = "com.baeldung.spring.cloud:spring-cloud-contract-producer:+:stubs:8090") +public class BasicMathControllerIntegrationTest { + + @Autowired + private MockMvc mockMvc; + + @Test + public void given_WhenPassEvenNumberInQueryParam_ThenReturnEven() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/calculate?number=2") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string("Even")); + } + + @Test + public void given_WhenPassOddNumberInQueryParam_ThenReturnOdd() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/calculate?number=1") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string("Odd")); + } +} diff --git a/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/pom.xml b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/pom.xml new file mode 100644 index 0000000000..ac27dbb645 --- /dev/null +++ b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/pom.xml @@ -0,0 +1,74 @@ + + + 4.0.0 + + com.baeldung.spring.cloud + spring-cloud-contract-producer + 1.0.0-SNAPSHOT + jar + + spring-cloud-contract-producer + Spring Cloud Producer Sample + + + com.baeldung.spring.cloud + spring-cloud-contract + 1.0.0-SNAPSHOT + .. + + + + UTF-8 + UTF-8 + 1.8 + Edgware.SR1 + + + + + org.springframework.cloud + spring-cloud-starter-contract-verifier + test + + + org.springframework.boot + spring-boot-starter-web + 1.5.9.RELEASE + + + org.springframework.boot + spring-boot-starter-data-rest + 1.5.9.RELEASE + + + + + + org.springframework.cloud + spring-cloud-contract-maven-plugin + 1.2.2.RELEASE + true + + com.baeldung.spring.cloud.springcloudcontractproducer.BaseTestClass + + + + + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + diff --git a/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/main/java/com/baeldung/spring/cloud/springcloudcontractproducer/SpringCloudContractProducerApplication.java b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/main/java/com/baeldung/spring/cloud/springcloudcontractproducer/SpringCloudContractProducerApplication.java new file mode 100644 index 0000000000..770c68c817 --- /dev/null +++ b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/main/java/com/baeldung/spring/cloud/springcloudcontractproducer/SpringCloudContractProducerApplication.java @@ -0,0 +1,11 @@ +package com.baeldung.spring.cloud.springcloudcontractproducer; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringCloudContractProducerApplication { + public static void main(String[] args) { + SpringApplication.run(SpringCloudContractProducerApplication.class, args); + } +} diff --git a/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/main/java/com/baeldung/spring/cloud/springcloudcontractproducer/controller/EvenOddController.java b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/main/java/com/baeldung/spring/cloud/springcloudcontractproducer/controller/EvenOddController.java new file mode 100644 index 0000000000..b8cc002fb4 --- /dev/null +++ b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/main/java/com/baeldung/spring/cloud/springcloudcontractproducer/controller/EvenOddController.java @@ -0,0 +1,15 @@ +package com.baeldung.spring.cloud.springcloudcontractproducer.controller; + + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class EvenOddController { + + @GetMapping("/validate/prime-number") + public String isNumberPrime(@RequestParam("number") Integer number) { + return number % 2 == 0 ? "Even" : "Odd"; + } +} diff --git a/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/main/resources/application.properties b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/main/resources/application.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/test/java/com/baeldung/spring/cloud/springcloudcontractproducer/BaseTestClass.java b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/test/java/com/baeldung/spring/cloud/springcloudcontractproducer/BaseTestClass.java new file mode 100644 index 0000000000..253924b247 --- /dev/null +++ b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/test/java/com/baeldung/spring/cloud/springcloudcontractproducer/BaseTestClass.java @@ -0,0 +1,29 @@ +package com.baeldung.spring.cloud.springcloudcontractproducer; + +import com.baeldung.spring.cloud.springcloudcontractproducer.controller.EvenOddController; +import io.restassured.module.mockmvc.RestAssuredMockMvc; +import org.junit.Before; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.contract.verifier.messaging.boot.AutoConfigureMessageVerifier; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK) +@DirtiesContext +@AutoConfigureMessageVerifier +public class BaseTestClass { + + @Autowired + private EvenOddController evenOddController; + + @Before + public void setup() { + StandaloneMockMvcBuilder standaloneMockMvcBuilder = MockMvcBuilders.standaloneSetup(evenOddController); + RestAssuredMockMvc.standaloneSetup(standaloneMockMvcBuilder); + } +} diff --git a/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/test/resources/contracts/shouldReturnEvenWhenRequestParamIsEven.groovy b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/test/resources/contracts/shouldReturnEvenWhenRequestParamIsEven.groovy new file mode 100644 index 0000000000..78c36d7334 --- /dev/null +++ b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/test/resources/contracts/shouldReturnEvenWhenRequestParamIsEven.groovy @@ -0,0 +1,17 @@ +import org.springframework.cloud.contract.spec.Contract + +Contract.make { + description "should return even when number input is even" + request { + method GET() + url("/validate/prime-number") { + queryParameters { + parameter("number", "2") + } + } + } + response { + body("Even") + status 200 + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/test/resources/contracts/shouldReturnOddWhenRequestParamIsOdd.groovy b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/test/resources/contracts/shouldReturnOddWhenRequestParamIsOdd.groovy new file mode 100644 index 0000000000..215f987bbf --- /dev/null +++ b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/test/resources/contracts/shouldReturnOddWhenRequestParamIsOdd.groovy @@ -0,0 +1,17 @@ +import org.springframework.cloud.contract.spec.Contract + +Contract.make { + description "should return odd when number input is odd" + request { + method GET() + url("/validate/prime-number") { + queryParameters { + parameter("number", "1") + } + } + } + response { + body("Odd") + status 200 + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-gateway/gateway-service/pom.xml b/spring-cloud/spring-cloud-gateway/gateway-service/pom.xml deleted file mode 100644 index 14cde4901a..0000000000 --- a/spring-cloud/spring-cloud-gateway/gateway-service/pom.xml +++ /dev/null @@ -1,79 +0,0 @@ - - 4.0.0 - - gateway-service - 1.0.0-SNAPSHOT - jar - - Spring Cloud Gateway Service - - - com.baeldung.spring.cloud - spring-cloud-gateway - 1.0.0-SNAPSHOT - .. - - - - 2.0.0.M2 - - - - - org.springframework.boot - spring-boot-actuator - ${version} - - - org.springframework.boot - spring-boot-starter-webflux - ${version} - - - org.springframework.cloud - spring-cloud-gateway-core - ${version} - - - org.springframework.cloud - spring-cloud-starter-eureka - ${version} - - - org.hibernate - hibernate-validator-cdi - 6.0.2.Final - - - javax.validation - validation-api - 2.0.0.Final - - - io.projectreactor.ipc - reactor-netty - 0.7.0.M1 - - - - - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - \ No newline at end of file diff --git a/spring-cloud/spring-cloud-gateway/pom.xml b/spring-cloud/spring-cloud-gateway/pom.xml index 5142b25400..90737f369d 100644 --- a/spring-cloud/spring-cloud-gateway/pom.xml +++ b/spring-cloud/spring-cloud-gateway/pom.xml @@ -4,13 +4,8 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.baeldung.spring.cloud spring-cloud-gateway - 1.0.0-SNAPSHOT - - gateway-service - - pom + jar Spring Cloud Gateway @@ -25,7 +20,61 @@ UTF-8 3.7.0 1.4.2.RELEASE + 2.0.0.M6 + + + + org.springframework.boot + spring-boot-actuator + ${version} + + + org.springframework.boot + spring-boot-starter-webflux + ${version} + + + org.springframework.cloud + spring-cloud-gateway-core + ${version} + + + + org.hibernate + hibernate-validator-cdi + 6.0.2.Final + + + javax.validation + validation-api + 2.0.0.Final + + + io.projectreactor.ipc + reactor-netty + 0.7.0.M1 + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + diff --git a/spring-cloud/spring-cloud-gateway/gateway-service/src/main/java/com/baeldung/spring/cloud/GatewayApplication.java b/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/spring/cloud/GatewayApplication.java similarity index 100% rename from spring-cloud/spring-cloud-gateway/gateway-service/src/main/java/com/baeldung/spring/cloud/GatewayApplication.java rename to spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/spring/cloud/GatewayApplication.java diff --git a/spring-cloud/spring-cloud-gateway/gateway-service/src/main/resources/application.yml b/spring-cloud/spring-cloud-gateway/src/main/resources/application.yml similarity index 100% rename from spring-cloud/spring-cloud-gateway/gateway-service/src/main/resources/application.yml rename to spring-cloud/spring-cloud-gateway/src/main/resources/application.yml diff --git a/spring-cloud/spring-cloud-security/README.md b/spring-cloud/spring-cloud-security/README.md new file mode 100644 index 0000000000..39af52c077 --- /dev/null +++ b/spring-cloud/spring-cloud-security/README.md @@ -0,0 +1,29 @@ +# README # + +This README would normally document whatever steps are necessary to get your application up and running. + +### What is this repository for? ### + +* Quick summary +* Version +* [Learn Markdown](https://bitbucket.org/tutorials/markdowndemo) + +### How do I get set up? ### + +* Summary of set up +* Configuration +* Dependencies +* Database configuration +* How to run tests +* Deployment instructions + +### Contribution guidelines ### + +* Writing tests +* Code review +* Other guidelines + +### Who do I talk to? ### + +* Repo owner or admin +* Other community or team contact \ No newline at end of file diff --git a/spring-cloud/spring-cloud-security/alias.rtf b/spring-cloud/spring-cloud-security/alias.rtf new file mode 100644 index 0000000000..15509e1c83 --- /dev/null +++ b/spring-cloud/spring-cloud-security/alias.rtf @@ -0,0 +1,28 @@ +myauthkey + + +security: + oauth2: + resource: + jwt: + keyValue: | + -----BEGIN PUBLIC KEY----- + MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjj4JDMgT4OoaXisEd8Nz + uiLwum9mh8BH1l9Atpe+uZkepf3Vnv0Bhxn0BGR+kYGwEHZPVpWsHEyTfIRdinaQ + vlPaxWJquQW25yYstrCuQTKJvFjSO/cX/V4OGi1RUj76mOpwzkm1Kui3R7Sfh8Zo + WO0GiWIFJqNBsZ9b1wOfBMXnge+A+u/qxVNnTFpwCVj6k2Yb4YUsmLNCmND7E3Ra + BnrNQWqMU2numhV+ADpmVH08m/+pWdZ896uYu/tvQnz3agvZPcFsEst7LcNAWQFT + eNLkfwVfepKWa9jPELemtTLf1MkMppU+Lj1UNCr8x4Y6EupRDZhplVNtqYsPNDpO + 7wIDAQAB + -----END PUBLIC KEY----- + + + +jwt: + certificate: + store: + file: classpath:/certificate/my-auth-server.jks + password: storepassword + key: + alias: myauthserver + password: keypassword \ No newline at end of file diff --git a/spring-cloud/spring-cloud-security/auth-client/pom.xml b/spring-cloud/spring-cloud-security/auth-client/pom.xml new file mode 100644 index 0000000000..5213b93f9a --- /dev/null +++ b/spring-cloud/spring-cloud-security/auth-client/pom.xml @@ -0,0 +1,102 @@ + + + 4.0.0 + com.baeldung + auth-client + 0.0.1-SNAPSHOT + jar + + auth-client + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 1.5.9.RELEASE + + + + + + + org.springframework.cloud + spring-cloud-dependencies + Dalston.SR4 + pom + import + + + + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.cloud + spring-cloud-starter-oauth2 + + + org.springframework.cloud + spring-cloud-starter-zuul + + + org.springframework.boot + spring-boot-starter-test + test + + + org.webjars + jquery + + + org.webjars + bootstrap + + + org.webjars + webjars-locator + + + + org.springframework.boot + spring-boot-starter-security + + + org.webjars + js-cookie + 2.1.0 + + + + org.springframework.boot + spring-boot-devtools + true + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/CloudSite.java b/spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/CloudSite.java new file mode 100644 index 0000000000..a072605ec0 --- /dev/null +++ b/spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/CloudSite.java @@ -0,0 +1,22 @@ +package com.baeldung; + + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +import com.baeldung.filters.SimpleFilter; + + +@SpringBootApplication +public class CloudSite { + public static void main(String[] args) { + SpringApplication.run(CloudSite.class, args); + } + + @Bean + public SimpleFilter simpleFilter() { + return new SimpleFilter(); + } + +} diff --git a/spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/config/SiteSecurityConfigurer.java b/spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/config/SiteSecurityConfigurer.java new file mode 100644 index 0000000000..217edb22fb --- /dev/null +++ b/spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/config/SiteSecurityConfigurer.java @@ -0,0 +1,49 @@ +package com.baeldung.config; + +import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso; +import org.springframework.cloud.netflix.zuul.EnableZuulProxy; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.oauth2.client.OAuth2ClientContext; +import org.springframework.security.oauth2.client.OAuth2RestOperations; +import org.springframework.security.oauth2.client.OAuth2RestTemplate; +import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails; +import org.springframework.security.web.csrf.CookieCsrfTokenRepository; + +@EnableZuulProxy +@Configuration +@EnableOAuth2Sso +public class SiteSecurityConfigurer + extends + WebSecurityConfigurerAdapter { + + @Override + protected void configure(HttpSecurity http) + throws Exception { + http.antMatcher("/**") + .authorizeRequests() + .antMatchers("/", "/webjars/**") + .permitAll() + .anyRequest() + .authenticated() + .and() + .logout() + .logoutSuccessUrl("/") + .permitAll() + .and() + .csrf() + .csrfTokenRepository( + CookieCsrfTokenRepository + .withHttpOnlyFalse()); + } + + @Bean + public OAuth2RestOperations restOperations( + OAuth2ProtectedResourceDetails resource, + OAuth2ClientContext context) { + return new OAuth2RestTemplate(resource, context); + } + +} diff --git a/spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/controller/CloudSiteController.java b/spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/controller/CloudSiteController.java new file mode 100644 index 0000000000..c77a5e02c5 --- /dev/null +++ b/spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/controller/CloudSiteController.java @@ -0,0 +1,30 @@ +package com.baeldung.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestOperations; +import org.springframework.web.servlet.ModelAndView; + +@RestController +public class CloudSiteController { + + @Autowired + private RestOperations restOperations; + + @GetMapping("/") + @ResponseBody + public String helloFromBaeldung() { + return "Hello From Baeldung!"; + } + + @GetMapping("/personInfo") + public ModelAndView person() { + ModelAndView mav = new ModelAndView("personinfo"); + String personResourceUrl = "http://localhost:9000/person"; + mav.addObject("person", restOperations.getForObject(personResourceUrl, String.class)); + return mav; + } + +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/filters/SimpleFilter.java b/spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/filters/SimpleFilter.java new file mode 100644 index 0000000000..98e25ac9c4 --- /dev/null +++ b/spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/filters/SimpleFilter.java @@ -0,0 +1,39 @@ +package com.baeldung.filters; + +import javax.servlet.http.HttpServletRequest; +import com.netflix.zuul.context.RequestContext; +import com.netflix.zuul.ZuulFilter; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SimpleFilter extends ZuulFilter { + + private static Logger log = LoggerFactory.getLogger(SimpleFilter.class); + + @Override + public String filterType() { + return "pre"; + } + + @Override + public int filterOrder() { + return 1; + } + + @Override + public boolean shouldFilter() { + return true; + } + + @Override + public Object run() { + RequestContext ctx = RequestContext.getCurrentContext(); + HttpServletRequest request = ctx.getRequest(); + + log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString())); + + return null; + } + +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-security/auth-client/src/main/resources/application.properties b/spring-cloud/spring-cloud-security/auth-client/src/main/resources/application.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/spring-cloud/spring-cloud-security/auth-client/src/main/resources/application.yml b/spring-cloud/spring-cloud-security/auth-client/src/main/resources/application.yml new file mode 100644 index 0000000000..2a758faeae --- /dev/null +++ b/spring-cloud/spring-cloud-security/auth-client/src/main/resources/application.yml @@ -0,0 +1,37 @@ +# Make the application available at http://localhost:8080 +# These are default settings, but we add them for clarity. +server: + port: 8080 + contextPath: / + +# Configure the Authorization Server and User Info Resource Server details +security: + oauth2: + client: + accessTokenUri: http://localhost:7070/authserver/oauth/token + userAuthorizationUri: http://localhost:7070/authserver/oauth/authorize + clientId: authserver + clientSecret: passwordforauthserver + resource: + userInfoUri: http://localhost:9000/user + +person: + url: http://localhost:9000/person + +# Proxies the calls to http://localhost:8080/api/* to our REST service at http://localhost:8081/* +# and automatically includes our OAuth2 token in the request headers +zuul: + routes: + resource: + path: /api/** + url: http://localhost:9000 + user: + path: /user/** + url: http://localhost:9000/user + +# Make sure the OAuth2 token is only relayed when using the internal API, +# do not pass any authentication to the external API +proxy: + auth: + routes: + api: oauth2 \ No newline at end of file diff --git a/spring-cloud/spring-cloud-security/auth-client/src/main/resources/templates/personinfo.html b/spring-cloud/spring-cloud-security/auth-client/src/main/resources/templates/personinfo.html new file mode 100644 index 0000000000..bc3197e93a --- /dev/null +++ b/spring-cloud/spring-cloud-security/auth-client/src/main/resources/templates/personinfo.html @@ -0,0 +1,13 @@ + + + + +Getting Personal Information + + +

Providing Person Information

+

+ Person's information: +

+ + \ No newline at end of file diff --git a/spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationTests.java b/spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationTests.java new file mode 100644 index 0000000000..5fa51a61c3 --- /dev/null +++ b/spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationTests.java @@ -0,0 +1,16 @@ +package com.example.springoath2; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class Springoath2ApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/spring-cloud/spring-cloud-security/auth-resource/pom.xml b/spring-cloud/spring-cloud-security/auth-resource/pom.xml new file mode 100644 index 0000000000..2c54d24e7d --- /dev/null +++ b/spring-cloud/spring-cloud-security/auth-resource/pom.xml @@ -0,0 +1,70 @@ + + + 4.0.0 + + com.baeldung + auth-resource + 0.0.1-SNAPSHOT + jar + + auth-resource + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 1.5.9.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + Edgware.RELEASE + + + + + org.springframework.security.oauth + spring-security-oauth2 + + + org.springframework.cloud + spring-cloud-starter-security + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.security + spring-security-jwt + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/Application.java b/spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/Application.java new file mode 100644 index 0000000000..8c087476bf --- /dev/null +++ b/spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/Application.java @@ -0,0 +1,12 @@ +package com.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/config/ResourceConfigurer.java b/spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/config/ResourceConfigurer.java new file mode 100644 index 0000000000..977d74093a --- /dev/null +++ b/spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/config/ResourceConfigurer.java @@ -0,0 +1,25 @@ +package com.baeldung.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; +import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter; + +/** + * REST API Resource Server. + */ +@Configuration +@EnableWebSecurity +@EnableResourceServer +@EnableGlobalMethodSecurity(prePostEnabled = true) // Allow method annotations like @PreAuthorize +public class ResourceConfigurer extends ResourceServerConfigurerAdapter { + + @Override + public void configure(HttpSecurity http) throws Exception { + http.httpBasic().disable(); + http.authorizeRequests().anyRequest().authenticated(); + } + +} diff --git a/spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/controller/PersonInfoController.java b/spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/controller/PersonInfoController.java new file mode 100644 index 0000000000..1958c0ebb8 --- /dev/null +++ b/spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/controller/PersonInfoController.java @@ -0,0 +1,18 @@ +package com.baeldung.controller; + +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.model.Person; + +@RestController +public class PersonInfoController { + + @GetMapping("/person") + @PreAuthorize("hasAnyRole('ADMIN', 'USER')") + public @ResponseBody Person personInfo() { + return new Person("abir", "Dhaka", "Bangladesh", 29, "Male"); + } +} diff --git a/spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/controller/ResourceController.java b/spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/controller/ResourceController.java new file mode 100644 index 0000000000..739a6f6240 --- /dev/null +++ b/spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/controller/ResourceController.java @@ -0,0 +1,20 @@ +package com.baeldung.controller; + +import java.security.Principal; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * Because this application is also a User Info Resource Server, we expose info about the logged in user at: + * + * http://localhost:9090/auth/user + */ +@RestController +public class ResourceController { + + @RequestMapping("/user") + public Principal user(Principal user) { + return user; + } + +} diff --git a/spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/model/Person.java b/spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/model/Person.java new file mode 100644 index 0000000000..b568291da3 --- /dev/null +++ b/spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/model/Person.java @@ -0,0 +1,59 @@ +package com.baeldung.model; + +public class Person { + + private String name; + private String city; + private String country; + private Integer age; + private String sex; + + public Person(String name, String city, String country, Integer age, String sex) { + this.name = name; + this.city = city; + this.country = country; + this.age = age; + this.sex = sex; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-security/auth-resource/src/main/resources/application.yml b/spring-cloud/spring-cloud-security/auth-resource/src/main/resources/application.yml new file mode 100644 index 0000000000..52e02ba41b --- /dev/null +++ b/spring-cloud/spring-cloud-security/auth-resource/src/main/resources/application.yml @@ -0,0 +1,21 @@ +# Make the application available at http://localhost:9000 + +server: + port: 9000 + +# Configure the public key to use for verifying the incoming JWT tokens +security: + sessions: NEVER + oauth2: + resource: + jwt: + keyValue: | + -----BEGIN PUBLIC KEY----- + MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhiiifKv6Otf5PyqIE+LQ + EiJRRh6q8piPY9Okq+RfRu9Bue0D8hq7aFxcgkLZ6Bg9CAS+w1KdaE5MMeOCVVxv + rpRETzVpAsh6GL5nBc679jSqMzjr3V4uty46ilL4VHKSxlZh5Nmz5EMHPI5iwpNs + 8U5n3QiwsTk514FXad54xPSPH3i/pDzGSZHrVcwDVaOKn7gFiIqP86vkJB47JZv8 + T6P5RK7Rj06zoG45DMGWG3DQv6o1/Jm4IJQWj0AUD3bSHqzXkPr7qyMYvkE4kyMH + 6aVAsAYMxilZFlJMv2b8N883gdi3LEeOJo8zZr5IWyyROfepdeOL7UkAXddAj+dL + WQIDAQAB + -----END PUBLIC KEY----- \ No newline at end of file diff --git a/spring-cloud/spring-cloud-security/auth-resource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationTests.java b/spring-cloud/spring-cloud-security/auth-resource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationTests.java new file mode 100644 index 0000000000..e0fe7006d9 --- /dev/null +++ b/spring-cloud/spring-cloud-security/auth-resource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationTests.java @@ -0,0 +1,16 @@ +package com.baeldung.service.personservice; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class PersonserviceApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/spring-cloud/spring-cloud-security/auth-server/pom.xml b/spring-cloud/spring-cloud-security/auth-server/pom.xml new file mode 100644 index 0000000000..ab30f3f2ec --- /dev/null +++ b/spring-cloud/spring-cloud-security/auth-server/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + + com.baeldung + auth-server + 0.0.1-SNAPSHOT + + + + org.springframework.boot + spring-boot-starter-parent + 1.5.9.RELEASE + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-devtools + true + + + org.springframework.boot + spring-boot-starter-tomcat + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.cloud + spring-cloud-starter-oauth2 + 1.1.2.RELEASE + + + \ No newline at end of file diff --git a/spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/AuthServer.java b/spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/AuthServer.java new file mode 100644 index 0000000000..c5b330b0e3 --- /dev/null +++ b/spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/AuthServer.java @@ -0,0 +1,12 @@ +package com.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.*; + +@SpringBootApplication +public class AuthServer { + + public static void main(String[] args) { + SpringApplication.run(AuthServer.class, args); + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/AuthServerConfigurer.java b/spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/AuthServerConfigurer.java new file mode 100644 index 0000000000..32e445f998 --- /dev/null +++ b/spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/AuthServerConfigurer.java @@ -0,0 +1,77 @@ +package com.baeldung.config; + +import java.security.KeyPair; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.annotation.Order; +import org.springframework.core.io.Resource; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer; +import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter; +import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer; +import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer; +import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; +import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory; + +@Configuration +@EnableAuthorizationServer +@Order(6) +public class AuthServerConfigurer + extends + AuthorizationServerConfigurerAdapter { + + @Value("${jwt.certificate.store.file}") + private Resource keystore; + + @Value("${jwt.certificate.store.password}") + private String keystorePassword; + + @Value("${jwt.certificate.key.alias}") + private String keyAlias; + + @Value("${jwt.certificate.key.password}") + private String keyPassword; + + @Autowired + private UserDetailsService userDetailsService; + + @Override + public void configure( + ClientDetailsServiceConfigurer clients) + throws Exception { + clients + .inMemory() + .withClient("authserver") + .secret("passwordforauthserver") + .redirectUris("http://localhost:8080/") + .authorizedGrantTypes("authorization_code", + "refresh_token") + .scopes("myscope") + .autoApprove(true) + .accessTokenValiditySeconds(30) + .refreshTokenValiditySeconds(1800); + } + + @Override + public void configure( + AuthorizationServerEndpointsConfigurer endpoints) + throws Exception { + endpoints + .accessTokenConverter(jwtAccessTokenConverter()) + .userDetailsService(userDetailsService); + } + + @Bean + public JwtAccessTokenConverter jwtAccessTokenConverter() { + KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory( + keystore, keystorePassword.toCharArray()); + KeyPair keyPair = keyStoreKeyFactory.getKeyPair( + keyAlias, keyPassword.toCharArray()); + JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); + converter.setKeyPair(keyPair); + return converter; + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/WebMvcConfigurer.java b/spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/WebMvcConfigurer.java new file mode 100644 index 0000000000..23b56151e7 --- /dev/null +++ b/spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/WebMvcConfigurer.java @@ -0,0 +1,14 @@ +package com.baeldung.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; + +@Configuration +public class WebMvcConfigurer extends WebMvcConfigurerAdapter { + + @Override + public void addViewControllers(ViewControllerRegistry registry) { + registry.addViewController("login").setViewName("login"); + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/WebSecurityConfigurer.java b/spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/WebSecurityConfigurer.java new file mode 100644 index 0000000000..44406b8fa0 --- /dev/null +++ b/spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/WebSecurityConfigurer.java @@ -0,0 +1,52 @@ +package com.baeldung.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client; + +@Configuration +@EnableWebSecurity +@EnableOAuth2Client +public class WebSecurityConfigurer + extends + WebSecurityConfigurerAdapter { + + + @Override + protected void configure(HttpSecurity http) + throws Exception { + http + .authorizeRequests() + .antMatchers("/login**").permitAll() + .anyRequest().authenticated() + .and().csrf() + .and().formLogin().loginPage("/login"); + } + + @Override + protected void configure( + AuthenticationManagerBuilder auth) throws Exception { + auth + .inMemoryAuthentication() + .withUser("user").password("user") + .roles("USER") + .and() + .withUser("admin").password("admin") + .roles("USER", "ADMIN"); + } + + @Override + @Bean(name = "userDetailsService") + public UserDetailsService userDetailsServiceBean() + throws Exception { + return super.userDetailsServiceBean(); + } + + +} diff --git a/spring-cloud/spring-cloud-security/auth-server/src/main/resources/application.yml b/spring-cloud/spring-cloud-security/auth-server/src/main/resources/application.yml new file mode 100644 index 0000000000..1dc63d3f0e --- /dev/null +++ b/spring-cloud/spring-cloud-security/auth-server/src/main/resources/application.yml @@ -0,0 +1,21 @@ +# Make the application available at http://localhost:7070/authserver +server: + port: 7070 + contextPath: /authserver + +# Our certificate settings for enabling JWT tokens +jwt: + certificate: + store: + file: classpath:/certificate/mykeystore.jks + password: abirkhan04 + key: + alias: myauthkey + password: abirkhan04 + + +security: + oauth2: + resource: + filter-order: 3 + \ No newline at end of file diff --git a/spring-cloud/spring-cloud-security/auth-server/src/main/resources/certificate/mykeystore.jks b/spring-cloud/spring-cloud-security/auth-server/src/main/resources/certificate/mykeystore.jks new file mode 100644 index 0000000000..9cf25e3224 Binary files /dev/null and b/spring-cloud/spring-cloud-security/auth-server/src/main/resources/certificate/mykeystore.jks differ diff --git a/spring-cloud/spring-cloud-security/auth-server/src/main/resources/templates/login.html b/spring-cloud/spring-cloud-security/auth-server/src/main/resources/templates/login.html new file mode 100644 index 0000000000..f5ab5a6f26 --- /dev/null +++ b/spring-cloud/spring-cloud-security/auth-server/src/main/resources/templates/login.html @@ -0,0 +1,29 @@ + + + + + Baeldung Spring cloud Security + + + +

Login

+ + +
+
+

Username and Password:

+

+ + +

+

+ + +

+

+ +

+
+
+ + \ No newline at end of file diff --git a/spring-cloud/spring-cloud-security/mykeystore.jks b/spring-cloud/spring-cloud-security/mykeystore.jks new file mode 100644 index 0000000000..9cf25e3224 Binary files /dev/null and b/spring-cloud/spring-cloud-security/mykeystore.jks differ diff --git a/spring-cloud/spring-cloud-security/pubkey.txt b/spring-cloud/spring-cloud-security/pubkey.txt new file mode 100644 index 0000000000..2c391ba2dd --- /dev/null +++ b/spring-cloud/spring-cloud-security/pubkey.txt @@ -0,0 +1,30 @@ +-----BEGIN PUBLIC KEY----- +MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhiiifKv6Otf5PyqIE+LQ +EiJRRh6q8piPY9Okq+RfRu9Bue0D8hq7aFxcgkLZ6Bg9CAS+w1KdaE5MMeOCVVxv +rpRETzVpAsh6GL5nBc679jSqMzjr3V4uty46ilL4VHKSxlZh5Nmz5EMHPI5iwpNs +8U5n3QiwsTk514FXad54xPSPH3i/pDzGSZHrVcwDVaOKn7gFiIqP86vkJB47JZv8 +T6P5RK7Rj06zoG45DMGWG3DQv6o1/Jm4IJQWj0AUD3bSHqzXkPr7qyMYvkE4kyMH +6aVAsAYMxilZFlJMv2b8N883gdi3LEeOJo8zZr5IWyyROfepdeOL7UkAXddAj+dL +WQIDAQAB +-----END PUBLIC KEY----- +-----BEGIN CERTIFICATE----- +MIIDfzCCAmegAwIBAgIEDqsC7jANBgkqhkiG9w0BAQsFADBwMQswCQYDVQQGEwI4 +ODETMBEGA1UECBMKQmFuZ2xhZGVzaDEOMAwGA1UEBxMFRGhha2ExETAPBgNVBAoT +CEJhZWxkdW5nMRUwEwYDVQQLEwxCYWVsZHVuZ2Jsb2cxEjAQBgNVBAMTCWxvY2Fs +aG9zdDAeFw0xNzEyMjUxNDE0MDhaFw0xODAzMjUxNDE0MDhaMHAxCzAJBgNVBAYT +Ajg4MRMwEQYDVQQIEwpCYW5nbGFkZXNoMQ4wDAYDVQQHEwVEaGFrYTERMA8GA1UE +ChMIQmFlbGR1bmcxFTATBgNVBAsTDEJhZWxkdW5nYmxvZzESMBAGA1UEAxMJbG9j +YWxob3N0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhiiifKv6Otf5 +PyqIE+LQEiJRRh6q8piPY9Okq+RfRu9Bue0D8hq7aFxcgkLZ6Bg9CAS+w1KdaE5M +MeOCVVxvrpRETzVpAsh6GL5nBc679jSqMzjr3V4uty46ilL4VHKSxlZh5Nmz5EMH +PI5iwpNs8U5n3QiwsTk514FXad54xPSPH3i/pDzGSZHrVcwDVaOKn7gFiIqP86vk +JB47JZv8T6P5RK7Rj06zoG45DMGWG3DQv6o1/Jm4IJQWj0AUD3bSHqzXkPr7qyMY +vkE4kyMH6aVAsAYMxilZFlJMv2b8N883gdi3LEeOJo8zZr5IWyyROfepdeOL7UkA +XddAj+dLWQIDAQABoyEwHzAdBgNVHQ4EFgQUHLFYkq36Wami5qsVRe/1eQedmdgw +DQYJKoZIhvcNAQELBQADggEBABL3lYyuRd6Hv8DPus/zQL0bRl6gVsEzczwmWMUA +3NJZbUHAD/KC732aArvKIKykkbLG6K/Mhnfuu8YBfWzTvGgY3Ww+ka2sJFOsUW7r +sa6OBtNHh4zhDYN2Weza+4jnRLxtkzFbm6v2sheFkyB1NywCwFE/6p1Z6KTG8RyJ +gw/OHl6rb+Y/T6cOeeTCFUN/v+qRVSB9I/MjSK5wRNbFT+MyNUeL6gsiyIvxSZbj +y4vrjGHkXasSmwkfvgw67mJMk4XTGrVLjIXUTyzbdSmodcv8N6nrsIk4SBYCnTrI +E/5NtNgbOFGwovde5yNrZIjjAC1VGOmVFhcxFJpwT6ZkSks= +-----END CERTIFICATE----- diff --git a/spring-cloud/spring-cloud-stream-starters/bash/hadoop.sh b/spring-cloud/spring-cloud-stream-starters/bash/hadoop.sh new file mode 100644 index 0000000000..ca8298430b --- /dev/null +++ b/spring-cloud/spring-cloud-stream-starters/bash/hadoop.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash + +# For Ubuntu 14.04 +# Inspired from: https://github.com/curran/setupHadoop/blob/master/setupHadoop.sh +# Use from the user directory + +# Install Java +sudo apt-get update +sudo add-apt-repository -y ppa:webupd8team/java +sudo apt-get install -y oracle-java8-installer + +# Install Hadoop +curl -O http://mirror.cogentco.com/pub/apache/hadoop/common/hadoop-2.8.2/hadoop-2.8.2.tar.gz +tar xfz hadoop-2.8.2.tar.gz +sudo mv hadoop-2.8.2 /usr/local/hadoop +rm hadoop-2.8.2.tar.gz + +# Environmental Variables +echo export JAVA_HOME=/usr/lib/jvm/java-8-oracle >> ~/.bashrc +echo export HADOOP_PREFIX=/usr/local/hadoop >> ~/.bashrc +echo export PATH=\$PATH:/usr/local/hadoop/bin >> ~/.bashrc +echo export PATH=\$PATH:/usr/local/hadoop/sbin >> ~/.bashrc +source ~/.bashrc + +# Copy configuration files +cp master/* /usr/local/hadoop/etc/hadoop/ + +# Format HDFS +hdfs namenode -format + +# SSH keys for Hadoop to use. +ssh-keygen -t rsa -P 'password' -f ~/.ssh/id_rsa.pub +sudo mv ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys + +# SSH +ssh localhost +# authenticate with osboxes.org + +# Start NameNode daemon and DataNode daemon +start-dfs.sh +# stop-dfs.sh + +# Install Maven +sudo apt-get install maven + +# Access Hadoop - http://localhost:50070 \ No newline at end of file diff --git a/spring-cloud/spring-cloud-stream-starters/boot/.gitignore b/spring-cloud/spring-cloud-stream-starters/boot/.gitignore new file mode 100644 index 0000000000..e4b82e1c0f --- /dev/null +++ b/spring-cloud/spring-cloud-stream-starters/boot/.gitignore @@ -0,0 +1,3 @@ +.idea +*/target/* +*.iml diff --git a/spring-cloud/spring-cloud-stream-starters/boot/pom.xml b/spring-cloud/spring-cloud-stream-starters/boot/pom.xml new file mode 100644 index 0000000000..3e6bc134e3 --- /dev/null +++ b/spring-cloud/spring-cloud-stream-starters/boot/pom.xml @@ -0,0 +1,55 @@ + + + 4.0.0 + com.baeldung.twitterhdfs + twitterhdfs + jar + 1.0.0 + + twitterhdfs + + + UTF-8 + UTF-8 + 1.8 + + + + org.springframework.boot + spring-boot-starter-parent + 1.5.8.RELEASE + + + + + + org.springframework.cloud.stream.app + spring-cloud-starter-stream-source-twitterstream + 1.3.1.RELEASE + + + org.springframework.cloud.stream.app + spring-cloud-starter-stream-sink-hdfs + 1.3.1.RELEASE + + + + + javax.servlet + jstl + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + twitterhdfs + + \ No newline at end of file diff --git a/spring-cloud/spring-cloud-stream-starters/boot/src/main/java/com/baeldung/twitterhdfs/aggregate/AggregateApp.java b/spring-cloud/spring-cloud-stream-starters/boot/src/main/java/com/baeldung/twitterhdfs/aggregate/AggregateApp.java new file mode 100644 index 0000000000..8b9ca6dc62 --- /dev/null +++ b/spring-cloud/spring-cloud-stream-starters/boot/src/main/java/com/baeldung/twitterhdfs/aggregate/AggregateApp.java @@ -0,0 +1,18 @@ +package com.baeldung.twitterhdfs.aggregate; + +import com.baeldung.twitterhdfs.processor.ProcessorApp; +import com.baeldung.twitterhdfs.source.SourceApp; +import com.baeldung.twitterhdfs.sink.SinkApp; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.stream.aggregate.AggregateApplicationBuilder; + +@SpringBootApplication +public class AggregateApp { + public static void main(String[] args) { + new AggregateApplicationBuilder() + .from(SourceApp.class).args("--fixedDelay=5000") + .via(ProcessorApp.class) + .to(SinkApp.class).args("--debug=true") + .run(args); + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-stream-starters/boot/src/main/java/com/baeldung/twitterhdfs/processor/ProcessorApp.java b/spring-cloud/spring-cloud-stream-starters/boot/src/main/java/com/baeldung/twitterhdfs/processor/ProcessorApp.java new file mode 100644 index 0000000000..e3bd1197f6 --- /dev/null +++ b/spring-cloud/spring-cloud-stream-starters/boot/src/main/java/com/baeldung/twitterhdfs/processor/ProcessorApp.java @@ -0,0 +1,20 @@ +package com.baeldung.twitterhdfs.processor; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.stream.annotation.EnableBinding; +import org.springframework.cloud.stream.messaging.Processor; +import org.springframework.integration.annotation.Transformer; + +@SpringBootApplication +@EnableBinding(Processor.class) +public class ProcessorApp { + Logger log = LoggerFactory.getLogger(ProcessorApp.class); + + @Transformer(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT) + public String processMessage(String payload) { + log.info("Payload received!"); + return payload; + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-stream-starters/boot/src/main/java/com/baeldung/twitterhdfs/sink/SinkApp.java b/spring-cloud/spring-cloud-stream-starters/boot/src/main/java/com/baeldung/twitterhdfs/sink/SinkApp.java new file mode 100644 index 0000000000..c0c1e287d3 --- /dev/null +++ b/spring-cloud/spring-cloud-stream-starters/boot/src/main/java/com/baeldung/twitterhdfs/sink/SinkApp.java @@ -0,0 +1,22 @@ +package com.baeldung.twitterhdfs.sink; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.stream.annotation.EnableBinding; +import org.springframework.cloud.stream.app.hdfs.sink.HdfsSinkConfiguration; +import org.springframework.cloud.stream.messaging.Sink; +import org.springframework.context.annotation.Import; +import org.springframework.integration.annotation.ServiceActivator; + +@SpringBootApplication +@EnableBinding(Sink.class) +@Import(HdfsSinkConfiguration.class) +public class SinkApp { + Logger log = LoggerFactory.getLogger(SinkApp.class); + + @ServiceActivator(inputChannel= Sink.INPUT) + public void loggerSink(Object payload) { + log.info("Received: " + payload); + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-stream-starters/boot/src/main/java/com/baeldung/twitterhdfs/source/SourceApp.java b/spring-cloud/spring-cloud-stream-starters/boot/src/main/java/com/baeldung/twitterhdfs/source/SourceApp.java new file mode 100644 index 0000000000..f9b220561b --- /dev/null +++ b/spring-cloud/spring-cloud-stream-starters/boot/src/main/java/com/baeldung/twitterhdfs/source/SourceApp.java @@ -0,0 +1,26 @@ +package com.baeldung.twitterhdfs.source; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.stream.annotation.EnableBinding; +import org.springframework.cloud.stream.app.twitterstream.source.TwitterstreamSourceConfiguration; +import org.springframework.cloud.stream.messaging.Source; +import org.springframework.context.annotation.Import; +import org.springframework.integration.annotation.InboundChannelAdapter; + +import java.text.SimpleDateFormat; +import java.util.Date; + +@SpringBootApplication +@EnableBinding(Source.class) +@Import(TwitterstreamSourceConfiguration.class) +public class SourceApp { + Logger log = LoggerFactory.getLogger(SourceApp.class); + + @InboundChannelAdapter(value = Source.OUTPUT) + public String timerMessageSource() { + return new SimpleDateFormat().format(new Date()); + } + +} diff --git a/spring-cloud/spring-cloud-stream-starters/boot/src/main/resources/application.properties b/spring-cloud/spring-cloud-stream-starters/boot/src/main/resources/application.properties new file mode 100644 index 0000000000..298a8ebf4d --- /dev/null +++ b/spring-cloud/spring-cloud-stream-starters/boot/src/main/resources/application.properties @@ -0,0 +1,6 @@ +hdfs.fs-uri=hdfs://127.0.0.1:50010/ + +twitter.credentials.access-token= +twitter.credentials.access-token-secret= +twitter.credentials.consumer-key= +twitter.credentials.consumer-secret= \ No newline at end of file diff --git a/spring-cloud/spring-cloud-stream-starters/hdfs/application.properties b/spring-cloud/spring-cloud-stream-starters/hdfs/application.properties new file mode 100644 index 0000000000..1f4aaf88dd --- /dev/null +++ b/spring-cloud/spring-cloud-stream-starters/hdfs/application.properties @@ -0,0 +1 @@ +hdfs.fs-uri=hdfs://127.0.0.1:50010/ \ No newline at end of file diff --git a/spring-cloud/spring-cloud-stream-starters/hdfs/hdfs.sh b/spring-cloud/spring-cloud-stream-starters/hdfs/hdfs.sh new file mode 100644 index 0000000000..f1c6bfcf3f --- /dev/null +++ b/spring-cloud/spring-cloud-stream-starters/hdfs/hdfs.sh @@ -0,0 +1,11 @@ +# Git spring-cloud-stream-app-starters +# https://github.com/spring-cloud-stream-app-starters/hdfs/blob/master/spring-cloud-starter-stream-sink-hdfs/README.adoc +git clone https://github.com/spring-cloud-stream-app-starters/hdfs.git + +# Build it +./mvnw clean install -PgenerateApps + +# Run it +cd target +# Optionally inject application.properties prior to build +java -jar hdfs-sink.jar --fsUri=hdfs://127.0.0.1:50010/ \ No newline at end of file diff --git a/spring-cloud/spring-cloud-stream-starters/twitter/application.properties b/spring-cloud/spring-cloud-stream-starters/twitter/application.properties new file mode 100644 index 0000000000..e38612d25e --- /dev/null +++ b/spring-cloud/spring-cloud-stream-starters/twitter/application.properties @@ -0,0 +1,4 @@ +twitter.credentials.access-token= +twitter.credentials.access-token-secret= +twitter.credentials.consumer-key= +twitter.credentials.consumer-secret= \ No newline at end of file diff --git a/spring-cloud/spring-cloud-stream-starters/twitter/twitter.sh b/spring-cloud/spring-cloud-stream-starters/twitter/twitter.sh new file mode 100644 index 0000000000..967cb54dfe --- /dev/null +++ b/spring-cloud/spring-cloud-stream-starters/twitter/twitter.sh @@ -0,0 +1,12 @@ +# Git spring-cloud-stream-app-starters +# https://github.com/spring-cloud-stream-app-starters/hdfs/blob/master/spring-cloud-starter-stream-sink-hdfs/README.adoc +git clone https://github.com/spring-cloud-stream-app-starters/twitter.git + +# Build it +./mvnw clean install -PgenerateApps + +# Run it +cd target +# Optionally inject application.properties prior to build +java -jar twitter_stream_source.jar --consumerKey= --consumerSecret= \ + --accessToken= --accessTokenSecret= \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/.gitignore b/spring-cloud/spring-cloud-task/springcloudtaskbatch/.gitignore new file mode 100644 index 0000000000..f675857856 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/.gitignore @@ -0,0 +1,2 @@ +/target/ +/.settings/ \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/pom.xml b/spring-cloud/spring-cloud-task/springcloudtaskbatch/pom.xml new file mode 100644 index 0000000000..4805f5296c --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/pom.xml @@ -0,0 +1,81 @@ + + 4.0.0 + org.baeldung.cloud + springcloudtask + 0.0.1-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 1.5.10.RELEASE + + + + + com.baeldung.TaskDemo + UTF-8 + UTF-8 + 1.8 + 1.2.2.RELEASE + + + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-data-jpa + + + mysql + mysql-connector-java + + + org.springframework.cloud + spring-cloud-starter-task + + + + org.springframework.cloud + spring-cloud-task-core + + + + org.springframework.boot + spring-boot-starter-batch + + + + org.springframework.cloud + spring-cloud-task-batch + + + + + + + org.springframework.cloud + spring-cloud-task-dependencies + ${spring-cloud-task.version} + pom + import + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/HelloWorldTaskConfigurer.java b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/HelloWorldTaskConfigurer.java new file mode 100644 index 0000000000..dc2ba53138 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/HelloWorldTaskConfigurer.java @@ -0,0 +1,14 @@ +package com.baeldung.task; + +import javax.sql.DataSource; + +import org.springframework.cloud.task.configuration.DefaultTaskConfigurer; + +public class HelloWorldTaskConfigurer + extends + DefaultTaskConfigurer { + + public HelloWorldTaskConfigurer(DataSource dataSource) { + super(dataSource); + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/JobConfiguration.java b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/JobConfiguration.java new file mode 100644 index 0000000000..6c215ad53d --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/JobConfiguration.java @@ -0,0 +1,105 @@ +package com.baeldung.task; + +import java.util.Arrays; +import java.util.List; +import java.util.logging.Logger; + +import org.springframework.batch.core.Job; +import org.springframework.batch.core.Step; +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.item.ItemProcessor; +import org.springframework.batch.item.ItemWriter; +import org.springframework.batch.item.support.ListItemReader; +import org.springframework.batch.repeat.RepeatStatus; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class JobConfiguration { + + private final static Logger LOGGER = Logger + .getLogger(JobConfiguration.class.getName()); + + @Autowired + private JobBuilderFactory jobBuilderFactory; + + @Autowired + private StepBuilderFactory stepBuilderFactory; + + @Bean + public Step step1() { + return this.stepBuilderFactory.get("job1step1") + .tasklet(new Tasklet() { + @Override + public RepeatStatus execute( + StepContribution contribution, + ChunkContext chunkContext) + throws Exception { + LOGGER.info("Tasklet has run"); + return RepeatStatus.FINISHED; + } + }).build(); + } + + @Bean + public Step step2() { + return this.stepBuilderFactory + .get("job1step2") + . chunk(3) + .reader( + new ListItemReader<>(Arrays.asList("7", + "2", "3", "10", "5", "6"))) + .processor( + new ItemProcessor() { + @Override + public String process(String item) + throws Exception { + LOGGER.info("Processing of chunks"); + return String.valueOf(Integer + .parseInt(item) * -1); + } + }) + .writer(new ItemWriter() { + @Override + public void write( + List items) + throws Exception { + for (String item : items) { + LOGGER.info(">> " + item); + } + } + }).build(); + } + + @Bean + public Job job1() { + return this.jobBuilderFactory.get("job1") + .start(step1()) + .next(step2()) + .build(); + } + + @Bean + public Job job2() { + return jobBuilderFactory.get("job2") + .start(stepBuilderFactory.get("job2step1") + .tasklet(new Tasklet() { + @Override + public RepeatStatus execute( + StepContribution contribution, + ChunkContext chunkContext) + throws Exception { + LOGGER + .info("This job is from Baeldung"); + return RepeatStatus.FINISHED; + } + }) + .build()) + .build(); + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/TaskDemo.java b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/TaskDemo.java new file mode 100644 index 0000000000..be2a173589 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/TaskDemo.java @@ -0,0 +1,56 @@ +package com.baeldung.task; + +import java.util.logging.Logger; + +import javax.sql.DataSource; + +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.task.configuration.EnableTask; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; + +@SpringBootApplication +@EnableTask +@EnableBatchProcessing +public class TaskDemo { + + private final static Logger LOGGER = Logger + .getLogger(TaskDemo.class.getName()); + + @Autowired + private DataSource dataSource; + + @Bean + public HelloWorldTaskConfigurer getTaskConfigurer() + { + return new HelloWorldTaskConfigurer(dataSource); + } + + @Bean + public TaskListener taskListener() { + return new TaskListener(); + } + + public static void main(String[] args) { + SpringApplication.run(TaskDemo.class, args); + } + + @Component + public static class HelloWorldApplicationRunner + implements + ApplicationRunner { + + @Override + public void run(ApplicationArguments arg0) + throws Exception { + // TODO Auto-generated method stub + LOGGER + .info("Hello World from Spring Cloud Task!"); + } + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/TaskListener.java b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/TaskListener.java new file mode 100644 index 0000000000..8250153b66 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/TaskListener.java @@ -0,0 +1,31 @@ +package com.baeldung.task; + +import java.util.logging.Logger; + +import org.springframework.cloud.task.listener.TaskExecutionListener; +import org.springframework.cloud.task.repository.TaskExecution; + +public class TaskListener implements TaskExecutionListener { + + private final static Logger LOGGER = Logger + .getLogger(TaskListener.class.getName()); + + @Override + public void onTaskEnd(TaskExecution arg0) { + // TODO Auto-generated method stub + LOGGER.info("End of Task"); + } + + @Override + public void onTaskFailed(TaskExecution arg0, + Throwable arg1) { + // TODO Auto-generated method stub + + } + + @Override + public void onTaskStartup(TaskExecution arg0) { + // TODO Auto-generated method stub + LOGGER.info("Task Startup"); + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/resources/application.yml b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/resources/application.yml new file mode 100644 index 0000000000..1187c12fe7 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/resources/application.yml @@ -0,0 +1,26 @@ +logging: + level: + org: + springframework: + cloud: + task=DEBUG + +spring: + application: + name=helloWorld + datasource: + url: jdbc:mysql://localhost:3306/springcloud?useSSL=false + username: root + password: + jpa: + hibernate: + ddl-auto: create-drop + properties: + hibernate: + dialect: org.hibernate.dialect.MySQL5Dialect + batch: + initialize-schema: always +maven: + remoteRepositories: + springRepo: + url: https://repo.spring.io/libs-snapshot \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtasksink/.gitignore b/spring-cloud/spring-cloud-task/springcloudtasksink/.gitignore new file mode 100644 index 0000000000..2af7cefb0a --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtasksink/.gitignore @@ -0,0 +1,24 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtasksink/pom.xml b/spring-cloud/spring-cloud-task/springcloudtasksink/pom.xml new file mode 100644 index 0000000000..b717fffc7c --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtasksink/pom.xml @@ -0,0 +1,88 @@ + + + 4.0.0 + + com.baeldung + SpringCloudTaskSink + 0.0.1-SNAPSHOT + jar + + SpringCloudTaskSink + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 1.5.10.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + 1.2.2.RELEASE + Edgware.SR2 + + + + + org.springframework.cloud + spring-cloud-starter-stream-rabbit + + + org.springframework.cloud + spring-cloud-starter-task + + + org.springframework.cloud + spring-cloud-stream-test-support + test + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.cloud + spring-cloud-deployer-local + 1.3.0.RELEASE + + + + + + + org.springframework.cloud + spring-cloud-task-dependencies + ${spring-cloud-task.version} + pom + import + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/java/com/baeldung/SpringCloudTaskFinal/SpringCloudTaskSinkApplication.java b/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/java/com/baeldung/SpringCloudTaskFinal/SpringCloudTaskSinkApplication.java new file mode 100644 index 0000000000..62085e5fa5 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/java/com/baeldung/SpringCloudTaskFinal/SpringCloudTaskSinkApplication.java @@ -0,0 +1,16 @@ +package com.baeldung.SpringCloudTaskFinal; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.task.launcher.annotation.EnableTaskLauncher; + +@SpringBootApplication +@EnableTaskLauncher +public class SpringCloudTaskSinkApplication { + + public static void main(String[] args) { + SpringApplication.run( + SpringCloudTaskSinkApplication.class, args); + } + +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/java/com/baeldung/SpringCloudTaskFinal/TaskSinkConfiguration.java b/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/java/com/baeldung/SpringCloudTaskFinal/TaskSinkConfiguration.java new file mode 100644 index 0000000000..638de58ecb --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/java/com/baeldung/SpringCloudTaskFinal/TaskSinkConfiguration.java @@ -0,0 +1,18 @@ +package com.baeldung.SpringCloudTaskFinal; + +import static org.mockito.Mockito.mock; + +import org.springframework.cloud.deployer.spi.task.TaskLauncher; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + + +@Configuration +public class TaskSinkConfiguration { + + @Bean + public TaskLauncher taskLauncher() { + return mock(TaskLauncher.class); + } + +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/resources/application.properties b/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/resources/application.properties new file mode 100644 index 0000000000..1660dc8516 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/resources/application.properties @@ -0,0 +1 @@ +maven.remoteRepositories.springRepo.url=https://repo.spring.io/libs-snapshot \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtasksink/src/test/java/com/baeldung/SpringCloudTaskFinal/SpringCloudTaskSinkApplicationTests.java b/spring-cloud/spring-cloud-task/springcloudtasksink/src/test/java/com/baeldung/SpringCloudTaskFinal/SpringCloudTaskSinkApplicationTests.java new file mode 100644 index 0000000000..1f47eada14 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtasksink/src/test/java/com/baeldung/SpringCloudTaskFinal/SpringCloudTaskSinkApplicationTests.java @@ -0,0 +1,72 @@ +package com.baeldung.SpringCloudTaskFinal; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.deployer.spi.core.AppDeploymentRequest; +import org.springframework.cloud.deployer.spi.task.TaskLauncher; +import org.springframework.cloud.stream.messaging.Sink; +import org.springframework.cloud.task.launcher.TaskLaunchRequest; +import org.springframework.context.ApplicationContext; +import org.springframework.messaging.support.GenericMessage; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.verify; + +@RunWith(SpringRunner.class) +@SpringBootTest( + classes = SpringCloudTaskSinkApplication.class) +public class SpringCloudTaskSinkApplicationTests { + + @Autowired + ApplicationContext context; + + @Autowired + private Sink sink; + + @Test + public void testTaskLaunch() throws IOException { + + TaskLauncher taskLauncher = + context.getBean(TaskLauncher.class); + + Map prop = new HashMap(); + prop.put("server.port", "0"); + TaskLaunchRequest request = new TaskLaunchRequest( + "maven://org.springframework.cloud.task.app:" + + "timestamp-task:jar:1.0.1.RELEASE", null, + prop, + null, null); + GenericMessage message = new GenericMessage( + request); + this.sink.input().send(message); + + ArgumentCaptor deploymentRequest = ArgumentCaptor + .forClass(AppDeploymentRequest.class); + + verify(taskLauncher).launch( + deploymentRequest.capture()); + + AppDeploymentRequest actualRequest = deploymentRequest + .getValue(); + + // Verifying the co-ordinate of launched Task here. + assertTrue(actualRequest.getCommandlineArguments() + .isEmpty()); + assertEquals("0", actualRequest.getDefinition() + .getProperties().get("server.port")); + assertTrue(actualRequest + .getResource() + .toString() + .contains( + "org.springframework.cloud.task.app:timestamp-task:jar:1.0.1.RELEASE")); + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-zuul-eureka-integration/eureka-client/src/main/java/com/baeldung/spring/cloud/eureka/client/EurekaClientApplication.java b/spring-cloud/spring-cloud-zuul-eureka-integration/eureka-client/src/main/java/com/baeldung/spring/cloud/eureka/client/EurekaClientApplication.java index 906d6e4cfd..9bbb121408 100644 --- a/spring-cloud/spring-cloud-zuul-eureka-integration/eureka-client/src/main/java/com/baeldung/spring/cloud/eureka/client/EurekaClientApplication.java +++ b/spring-cloud/spring-cloud-zuul-eureka-integration/eureka-client/src/main/java/com/baeldung/spring/cloud/eureka/client/EurekaClientApplication.java @@ -21,12 +21,17 @@ public class EurekaClientApplication implements GreetingController { @Value("${spring.application.name}") private String appName; + @Value("${server.port}") + private String portNumber; + public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } @Override public String greeting() { - return String.format("Hello from '%s'!", eurekaClient.getApplication(appName).getName()); + System.out.println("Request received on port number " + portNumber); + return String.format("Hello from '%s with Port Number %s'!", eurekaClient.getApplication(appName) + .getName(), portNumber); } } diff --git a/spring-cloud/spring-cloud-zuul-eureka-integration/eureka-client/src/main/resources/application.yml b/spring-cloud/spring-cloud-zuul-eureka-integration/eureka-client/src/main/resources/application.yml index 08624aa159..9fa929e16b 100644 --- a/spring-cloud/spring-cloud-zuul-eureka-integration/eureka-client/src/main/resources/application.yml +++ b/spring-cloud/spring-cloud-zuul-eureka-integration/eureka-client/src/main/resources/application.yml @@ -3,7 +3,7 @@ spring: name: spring-cloud-eureka-client server: - port: 0 + port: 8081 eureka: client: diff --git a/spring-data-elasticsearch/README.md b/spring-data-elasticsearch/README.md index 0095d66377..5d0b3b84c4 100644 --- a/spring-data-elasticsearch/README.md +++ b/spring-data-elasticsearch/README.md @@ -5,6 +5,7 @@ - [Elasticsearch Queries with Spring Data](http://www.baeldung.com/spring-data-elasticsearch-queries) - [Guide to Elasticsearch in Java](http://www.baeldung.com/elasticsearch-java) +- [Geospatial Support in ElasticSearch](http://www.baeldung.com/elasticsearch-geo-spatial) ### Build the Project with Tests Running ``` diff --git a/spring-data-elasticsearch/pom.xml b/spring-data-elasticsearch/pom.xml index 520707a432..688506450f 100644 --- a/spring-data-elasticsearch/pom.xml +++ b/spring-data-elasticsearch/pom.xml @@ -45,6 +45,24 @@ ${spring-data-elasticsearch.version} + + com.spatial4j + spatial4j + 0.4.1 + + + + com.vividsolutions + jts + 1.13 + + + xerces + xercesImpl + + + + org.springframework spring-test diff --git a/spring-data-elasticsearch/src/main/java/com/baeldung/elasticsearch/Person.java b/spring-data-elasticsearch/src/main/java/com/baeldung/elasticsearch/Person.java index b8ad59e2e2..09b971fdc2 100644 --- a/spring-data-elasticsearch/src/main/java/com/baeldung/elasticsearch/Person.java +++ b/spring-data-elasticsearch/src/main/java/com/baeldung/elasticsearch/Person.java @@ -11,10 +11,9 @@ public class Person { private Date dateOfBirth; public Person() { - } - public Person(int age, String fullName, Date dateOfBirth) { + Person(int age, String fullName, Date dateOfBirth) { super(); this.age = age; this.fullName = fullName; diff --git a/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/repository/ArticleRepository.java b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/repository/ArticleRepository.java index 8aef865401..93812a3cea 100644 --- a/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/repository/ArticleRepository.java +++ b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/repository/ArticleRepository.java @@ -1,12 +1,13 @@ package com.baeldung.spring.data.es.repository; -import com.baeldung.spring.data.es.model.Article; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.annotations.Query; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository; +import com.baeldung.spring.data.es.model.Article; + @Repository public interface ArticleRepository extends ElasticsearchRepository { @@ -14,4 +15,10 @@ public interface ArticleRepository extends ElasticsearchRepository findByAuthorsNameUsingCustomQuery(String name, Pageable pageable); + + @Query("{\"bool\": {\"must\": {\"match_all\": {}}, \"filter\": {\"term\": {\"tags\": \"?0\" }}}}") + Page
findByFilteredTagQuery(String tag, Pageable pageable); + + @Query("{\"bool\": {\"must\": {\"match\": {\"authors.name\": \"?0\"}}, \"filter\": {\"term\": {\"tags\": \"?1\" }}}}") + Page
findByAuthorsNameAndFilteredTagQuery(String name, String tag, Pageable pageable); } diff --git a/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleService.java b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleService.java index b5a8fde633..63e2d91fa7 100644 --- a/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleService.java +++ b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleService.java @@ -1,9 +1,10 @@ package com.baeldung.spring.data.es.service; -import com.baeldung.spring.data.es.model.Article; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import com.baeldung.spring.data.es.model.Article; + public interface ArticleService { Article save(Article article); @@ -15,6 +16,10 @@ public interface ArticleService { Page
findByAuthorNameUsingCustomQuery(String name, Pageable pageable); + Page
findByFilteredTagQuery(String tag, Pageable pageable); + + Page
findByAuthorsNameAndFilteredTagQuery(String name, String tag, Pageable pageable); + long count(); void delete(Article article); diff --git a/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleServiceImpl.java b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleServiceImpl.java index 2a31b52602..0908ffa70e 100644 --- a/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleServiceImpl.java +++ b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleServiceImpl.java @@ -1,12 +1,13 @@ package com.baeldung.spring.data.es.service; -import com.baeldung.spring.data.es.repository.ArticleRepository; -import com.baeldung.spring.data.es.model.Article; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; +import com.baeldung.spring.data.es.model.Article; +import com.baeldung.spring.data.es.repository.ArticleRepository; + @Service public class ArticleServiceImpl implements ArticleService { @@ -42,6 +43,16 @@ public class ArticleServiceImpl implements ArticleService { return articleRepository.findByAuthorsNameUsingCustomQuery(name, pageable); } + @Override + public Page
findByFilteredTagQuery(String tag, Pageable pageable) { + return articleRepository.findByFilteredTagQuery(tag, pageable); + } + + @Override + public Page
findByAuthorsNameAndFilteredTagQuery(String name, String tag, Pageable pageable) { + return articleRepository.findByAuthorsNameAndFilteredTagQuery(name, tag, pageable); + } + @Override public long count() { return articleRepository.count(); diff --git a/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java b/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java index 1fb1ae76f7..285c164869 100644 --- a/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java +++ b/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java @@ -19,6 +19,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; +import java.util.stream.Collectors; import static org.elasticsearch.node.NodeBuilder.nodeBuilder; import static org.junit.Assert.assertEquals; @@ -78,12 +79,9 @@ public class ElasticSearchManualTest { SearchHit[] searchHits = response .getHits() .getHits(); - List results = new ArrayList<>(); - for (SearchHit hit : searchHits) { - String sourceAsString = hit.getSourceAsString(); - Person person = JSON.parseObject(sourceAsString, Person.class); - results.add(person); - } + List results = Arrays.stream(searchHits) + .map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) + .collect(Collectors.toList()); } @Test @@ -125,11 +123,10 @@ public class ElasticSearchManualTest { .actionGet(); response2.getHits(); response3.getHits(); - List searchHits = Arrays.asList(response - .getHits() - .getHits()); - final List results = new ArrayList<>(); - searchHits.forEach(hit -> results.add(JSON.parseObject(hit.getSourceAsString(), Person.class))); + + final List results = Arrays.stream(response.getHits().getHits()) + .map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) + .collect(Collectors.toList()); } @Test diff --git a/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesTest.java b/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesTest.java new file mode 100644 index 0000000000..aa20913637 --- /dev/null +++ b/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesTest.java @@ -0,0 +1,171 @@ +package com.baeldung.elasticsearch; + +import com.baeldung.spring.data.es.config.Config; +import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; +import org.elasticsearch.action.index.IndexResponse; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.client.Client; +import org.elasticsearch.common.geo.ShapeRelation; +import org.elasticsearch.common.geo.builders.ShapeBuilder; +import org.elasticsearch.common.unit.DistanceUnit; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.search.SearchHit; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import static org.junit.Assert.assertTrue; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = Config.class) +public class GeoQueriesTest { + + private static final String WONDERS_OF_WORLD = "wonders-of-world"; + private static final String WONDERS = "Wonders"; + + @Autowired + private ElasticsearchTemplate elasticsearchTemplate; + + @Autowired + private Client client; + + @Before + public void setUp() { + String jsonObject = "{\"Wonders\":{\"properties\":{\"name\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"region\":{\"type\":\"geo_shape\",\"tree\":\"quadtree\",\"precision\":\"1m\"},\"location\":{\"type\":\"geo_point\"}}}}"; + CreateIndexRequest req = new CreateIndexRequest(WONDERS_OF_WORLD); + req.mapping(WONDERS, jsonObject); + client.admin() + .indices() + .create(req) + .actionGet(); + } + + @Test + public void givenGeoShapeData_whenExecutedGeoShapeQuery_thenResultNonEmpty() { + String jsonObject = "{\"name\":\"Agra\",\"region\":{\"type\":\"envelope\",\"coordinates\":[[75,25],[80.1,30.2]]}}"; + IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS) + .setSource(jsonObject) + .get(); + String tajMahalId = response.getId(); + client.admin() + .indices() + .prepareRefresh(WONDERS_OF_WORLD) + .get(); + + QueryBuilder qb = QueryBuilders.geoShapeQuery("region", ShapeBuilder.newEnvelope() + .topLeft(74.00, 24.0) + .bottomRight(81.1, 31.2)) + .relation(ShapeRelation.WITHIN); + + SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD) + .setTypes(WONDERS) + .setQuery(qb) + .execute() + .actionGet(); + List ids = Arrays.stream(searchResponse.getHits() + .getHits()) + .map(SearchHit::getId) + .collect(Collectors.toList()); + assertTrue(ids.contains(tajMahalId)); + } + + @Test + public void givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty() { + String jsonObject = "{\"name\":\"Pyramids of Giza\",\"location\":[31.131302,29.976480]}"; + IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS) + .setSource(jsonObject) + .get(); + String pyramidsOfGizaId = response.getId(); + client.admin() + .indices() + .prepareRefresh(WONDERS_OF_WORLD) + .get(); + + QueryBuilder qb = QueryBuilders.geoBoundingBoxQuery("location") + .bottomLeft(28, 30) + .topRight(31, 32); + + SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD) + .setTypes(WONDERS) + .setQuery(qb) + .execute() + .actionGet(); + List ids = Arrays.stream(searchResponse.getHits() + .getHits()) + .map(SearchHit::getId) + .collect(Collectors.toList()); + assertTrue(ids.contains(pyramidsOfGizaId)); + } + + @Test + public void givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty() { + String jsonObject = "{\"name\":\"Lighthouse of alexandria\",\"location\":[31.131302,29.976480]}"; + IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS) + .setSource(jsonObject) + .get(); + String lighthouseOfAlexandriaId = response.getId(); + client.admin() + .indices() + .prepareRefresh(WONDERS_OF_WORLD) + .get(); + + QueryBuilder qb = QueryBuilders.geoDistanceQuery("location") + .point(29.976, 31.131) + .distance(10, DistanceUnit.MILES); + + SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD) + .setTypes(WONDERS) + .setQuery(qb) + .execute() + .actionGet(); + List ids = Arrays.stream(searchResponse.getHits() + .getHits()) + .map(SearchHit::getId) + .collect(Collectors.toList()); + assertTrue(ids.contains(lighthouseOfAlexandriaId)); + } + + @Test + public void givenGeoPointData_whenExecutedGeoPolygonQuery_thenResultNonEmpty() { + String jsonObject = "{\"name\":\"The Great Rann of Kutch\",\"location\":[69.859741,23.733732]}"; + IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS) + .setSource(jsonObject) + .get(); + String greatRannOfKutchid = response.getId(); + client.admin() + .indices() + .prepareRefresh(WONDERS_OF_WORLD) + .get(); + + QueryBuilder qb = QueryBuilders.geoPolygonQuery("location") + .addPoint(22.733, 68.859) + .addPoint(24.733, 68.859) + .addPoint(23, 70.859); + + SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD) + .setTypes(WONDERS) + .setQuery(qb) + .execute() + .actionGet(); + List ids = Arrays.stream(searchResponse.getHits() + .getHits()) + .map(SearchHit::getId) + .collect(Collectors.toList()); + assertTrue(ids.contains(greatRannOfKutchid)); + } + + @After + public void destroy() { + elasticsearchTemplate.deleteIndex(WONDERS_OF_WORLD); + } +} diff --git a/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchIntegrationTest.java b/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchIntegrationTest.java index 1280c8e1de..41965fbb83 100644 --- a/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchIntegrationTest.java +++ b/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchIntegrationTest.java @@ -1,15 +1,9 @@ package com.baeldung.spring.data.es; -import static java.util.Arrays.asList; -import static org.elasticsearch.index.query.MatchQueryBuilder.Operator.AND; -import static org.elasticsearch.index.query.QueryBuilders.fuzzyQuery; -import static org.elasticsearch.index.query.QueryBuilders.matchQuery; -import static org.elasticsearch.index.query.QueryBuilders.regexpQuery; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import java.util.List; - +import com.baeldung.spring.data.es.config.Config; +import com.baeldung.spring.data.es.model.Article; +import com.baeldung.spring.data.es.model.Author; +import com.baeldung.spring.data.es.service.ArticleService; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -22,10 +16,15 @@ import org.springframework.data.elasticsearch.core.query.SearchQuery; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import com.baeldung.spring.data.es.config.Config; -import com.baeldung.spring.data.es.model.Article; -import com.baeldung.spring.data.es.model.Author; -import com.baeldung.spring.data.es.service.ArticleService; +import java.util.List; + +import static java.util.Arrays.asList; +import static org.elasticsearch.index.query.MatchQueryBuilder.Operator.AND; +import static org.elasticsearch.index.query.QueryBuilders.fuzzyQuery; +import static org.elasticsearch.index.query.QueryBuilders.matchQuery; +import static org.elasticsearch.index.query.QueryBuilders.regexpQuery; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) @@ -47,14 +46,22 @@ public class ElasticSearchIntegrationTest { Article article = new Article("Spring Data Elasticsearch"); article.setAuthors(asList(johnSmith, johnDoe)); + article.setTags("elasticsearch", "spring data"); articleService.save(article); article = new Article("Search engines"); article.setAuthors(asList(johnDoe)); + article.setTags("search engines", "tutorial"); articleService.save(article); article = new Article("Second Article About Elasticsearch"); article.setAuthors(asList(johnSmith)); + article.setTags("elasticsearch", "spring data"); + articleService.save(article); + + article = new Article("Elasticsearch Tutorial"); + article.setAuthors(asList(johnDoe)); + article.setTags("elasticsearch"); articleService.save(article); } @@ -72,21 +79,34 @@ public class ElasticSearchIntegrationTest { @Test public void givenPersistedArticles_whenSearchByAuthorsName_thenRightFound() { - final Page
articleByAuthorName = articleService.findByAuthorName(johnSmith.getName(), new PageRequest(0, 10)); + final Page
articleByAuthorName = articleService + .findByAuthorName(johnSmith.getName(), new PageRequest(0, 10)); assertEquals(2L, articleByAuthorName.getTotalElements()); } @Test public void givenCustomQuery_whenSearchByAuthorsName_thenArticleIsFound() { + final Page
articleByAuthorName = articleService.findByAuthorNameUsingCustomQuery("Smith", new PageRequest(0, 10)); + assertEquals(2L, articleByAuthorName.getTotalElements()); + } - final Page
articleByAuthorName = articleService.findByAuthorNameUsingCustomQuery("John Smith", new PageRequest(0, 10)); + @Test + public void givenTagFilterQuery_whenSearchByTag_thenArticleIsFound() { + final Page
articleByAuthorName = articleService.findByFilteredTagQuery("elasticsearch", new PageRequest(0, 10)); assertEquals(3L, articleByAuthorName.getTotalElements()); } + @Test + public void givenTagFilterQuery_whenSearchByAuthorsName_thenArticleIsFound() { + final Page
articleByAuthorName = articleService.findByAuthorsNameAndFilteredTagQuery("Doe", "elasticsearch", new PageRequest(0, 10)); + assertEquals(2L, articleByAuthorName.getTotalElements()); + } + @Test public void givenPersistedArticles_whenUseRegexQuery_thenRightArticlesFound() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*")).build(); + final SearchQuery searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*")) + .build(); final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); assertEquals(1, articles.size()); @@ -112,7 +132,8 @@ public class ElasticSearchIntegrationTest { final String articleTitle = "Spring Data Elasticsearch"; - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build(); + final SearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build(); final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); assertEquals(1, articles.size()); final long count = articleService.count(); @@ -124,7 +145,8 @@ public class ElasticSearchIntegrationTest { @Test public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(AND)).build(); + final SearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery(matchQuery("title", "Search engines").operator(AND)).build(); final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); assertEquals(1, articles.size()); } diff --git a/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryIntegrationTest.java b/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryIntegrationTest.java index cc4bce0c75..c6af93bb62 100644 --- a/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryIntegrationTest.java +++ b/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryIntegrationTest.java @@ -1,20 +1,9 @@ package com.baeldung.spring.data.es; -import static java.util.Arrays.asList; -import static java.util.stream.Collectors.toList; -import static org.elasticsearch.index.query.MatchQueryBuilder.Operator.AND; -import static org.elasticsearch.index.query.QueryBuilders.boolQuery; -import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery; -import static org.elasticsearch.index.query.QueryBuilders.matchQuery; -import static org.elasticsearch.index.query.QueryBuilders.multiMatchQuery; -import static org.elasticsearch.index.query.QueryBuilders.nestedQuery; -import static org.elasticsearch.index.query.QueryBuilders.termQuery; -import static org.junit.Assert.assertEquals; - -import java.util.Collections; -import java.util.List; -import java.util.Map; - +import com.baeldung.spring.data.es.config.Config; +import com.baeldung.spring.data.es.model.Article; +import com.baeldung.spring.data.es.model.Author; +import com.baeldung.spring.data.es.service.ArticleService; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; import org.elasticsearch.common.unit.Fuzziness; @@ -22,6 +11,7 @@ import org.elasticsearch.index.query.MultiMatchQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilders; +import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation; import org.elasticsearch.search.aggregations.bucket.terms.StringTerms; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder; @@ -35,10 +25,20 @@ import org.springframework.data.elasticsearch.core.query.SearchQuery; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import com.baeldung.spring.data.es.config.Config; -import com.baeldung.spring.data.es.model.Article; -import com.baeldung.spring.data.es.model.Author; -import com.baeldung.spring.data.es.service.ArticleService; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import static java.util.Arrays.asList; +import static java.util.stream.Collectors.toList; +import static org.elasticsearch.index.query.MatchQueryBuilder.Operator.AND; +import static org.elasticsearch.index.query.QueryBuilders.boolQuery; +import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery; +import static org.elasticsearch.index.query.QueryBuilders.matchQuery; +import static org.elasticsearch.index.query.QueryBuilders.multiMatchQuery; +import static org.elasticsearch.index.query.QueryBuilders.nestedQuery; +import static org.elasticsearch.index.query.QueryBuilders.termQuery; +import static org.junit.Assert.assertEquals; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) @@ -86,14 +86,16 @@ public class ElasticSearchQueryIntegrationTest { @Test public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(AND)).build(); + final SearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery(matchQuery("title", "Search engines").operator(AND)).build(); final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); assertEquals(1, articles.size()); } @Test public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions")).build(); + final SearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery(matchQuery("title", "Engines Solutions")).build(); final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); assertEquals(1, articles.size()); assertEquals("Search engines", articles.get(0).getTitle()); @@ -101,18 +103,21 @@ public class ElasticSearchQueryIntegrationTest { @Test public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data")).build(); + final SearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery(matchQuery("title", "elasticsearch data")).build(); final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); assertEquals(3, articles.size()); } @Test public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() { - SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch")).build(); + SearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch")).build(); List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); assertEquals(1, articles.size()); - searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About")).build(); + searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About")) + .build(); articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); assertEquals(0, articles.size()); } @@ -130,38 +135,48 @@ public class ElasticSearchQueryIntegrationTest { @Test public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() { final TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("title"); - final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation).execute().actionGet(); + final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation) + .execute().actionGet(); final Map results = response.getAggregations().asMap(); final StringTerms topTags = (StringTerms) results.get("top_tags"); - final List keys = topTags.getBuckets().stream().map(b -> b.getKeyAsString()).collect(toList()); - Collections.sort(keys); + final List keys = topTags.getBuckets().stream() + .map(MultiBucketsAggregation.Bucket::getKeyAsString) + .sorted() + .collect(toList()); assertEquals(asList("about", "article", "data", "elasticsearch", "engines", "search", "second", "spring", "tutorial"), keys); } @Test public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() { - final TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags").order(Terms.Order.aggregation("_count", false)); - final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation).execute().actionGet(); + final TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags") + .order(Terms.Order.aggregation("_count", false)); + final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation) + .execute().actionGet(); final Map results = response.getAggregations().asMap(); final StringTerms topTags = (StringTerms) results.get("top_tags"); - final List keys = topTags.getBuckets().stream().map(b -> b.getKeyAsString()).collect(toList()); + final List keys = topTags.getBuckets().stream() + .map(MultiBucketsAggregation.Bucket::getKeyAsString) + .collect(toList()); assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys); } @Test public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)).build(); + final SearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)).build(); final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); assertEquals(1, articles.size()); } @Test public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "spring date elasticserch").operator(AND).fuzziness(Fuzziness.ONE).prefixLength(3)).build(); + final SearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery(matchQuery("title", "spring date elasticserch").operator(AND).fuzziness(Fuzziness.ONE) + .prefixLength(3)).build(); final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); assertEquals(1, articles.size()); @@ -169,9 +184,23 @@ public class ElasticSearchQueryIntegrationTest { @Test public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(multiMatchQuery("tutorial").field("title").field("tags").type(MultiMatchQueryBuilder.Type.BEST_FIELDS)).build(); + final SearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery(multiMatchQuery("tutorial").field("title").field("tags") + .type(MultiMatchQueryBuilder.Type.BEST_FIELDS)).build(); final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); assertEquals(2, articles.size()); } + + @Test + public void givenBoolQuery_whenQueryByAuthorsName_thenFoundArticlesByThatAuthorAndFilteredTag() { + final QueryBuilder builder = boolQuery().must(nestedQuery("authors", boolQuery().must(termQuery("authors.name", "doe")))) + .filter(termQuery("tags", "elasticsearch")); + + final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder) + .build(); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + + assertEquals(2, articles.size()); + } } diff --git a/spring-data-spring-security/README.md b/spring-data-spring-security/README.md new file mode 100644 index 0000000000..15b4b50870 --- /dev/null +++ b/spring-data-spring-security/README.md @@ -0,0 +1,14 @@ +# About this project +This project contains examples from the [Spring Data with Spring Security](http://www.baeldung.com/spring-data-with-spring-security) article from Baeldung. + +# Running the project +The application uses [Spring Boot](http://projects.spring.io/spring-boot/), so it is easy to run. You can start it any of a few ways: +* Run the `main` method from `SpringDataRestApplication` +* Use the Maven Spring Boot plugin: `mvn spring-boot:run` +* Package the application as a JAR and run it using `java -jar spring-data-spring-security.jar` + +# Viewing the running application +To view the running application, visit [http://localhost:8080](http://localhost:8080) in your browser + +###Relevant Articles: +- [Spring Data with Spring Security](http://www.baeldung.com/spring-data-with-spring-security) diff --git a/spring-data-spring-security/pom.xml b/spring-data-spring-security/pom.xml new file mode 100644 index 0000000000..d6b671ee57 --- /dev/null +++ b/spring-data-spring-security/pom.xml @@ -0,0 +1,67 @@ + + + 4.0.0 + + com.baeldung + spring-data-spring-security + 1.0 + jar + + intro-spring-data-spring-security + Spring Data with Spring Security + + + parent-boot-5 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-5 + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.security + spring-security-data + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.security + spring-security-test + test + + + org.apache.tomcat.embed + tomcat-embed-jasper + + + + com.h2database + h2 + + + javax.servlet + jstl + + + + + ${project.artifactId} + + + + diff --git a/spring-data-spring-security/src/main/java/com/baeldung/AppConfig.java b/spring-data-spring-security/src/main/java/com/baeldung/AppConfig.java new file mode 100644 index 0000000000..16bbe8b326 --- /dev/null +++ b/spring-data-spring-security/src/main/java/com/baeldung/AppConfig.java @@ -0,0 +1,64 @@ +package com.baeldung; + +import java.util.Properties; + +import javax.sql.DataSource; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; + +@SpringBootApplication +@PropertySource("classpath:persistence-h2.properties") +@EnableJpaRepositories(basePackages = { "com.baeldung.data.repositories" }) +@EnableWebMvc +@Import(SpringSecurityConfig.class) +public class AppConfig extends WebMvcConfigurerAdapter { + + @Autowired + private Environment env; + + @Bean + public DataSource dataSource() { + final DriverManagerDataSource dataSource = new DriverManagerDataSource(); + dataSource.setDriverClassName(env.getProperty("driverClassName")); + dataSource.setUrl(env.getProperty("url")); + dataSource.setUsername(env.getProperty("user")); + dataSource.setPassword(env.getProperty("password")); + return dataSource; + } + + @Bean + public LocalContainerEntityManagerFactoryBean entityManagerFactory() { + final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); + em.setDataSource(dataSource()); + em.setPackagesToScan(new String[] { "com.baeldung.models" }); + em.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); + em.setJpaProperties(additionalProperties()); + return em; + } + + final Properties additionalProperties() { + final Properties hibernateProperties = new Properties(); + if (env.getProperty("hibernate.hbm2ddl.auto") != null) { + hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); + } + if (env.getProperty("hibernate.dialect") != null) { + hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); + } + if (env.getProperty("hibernate.show_sql") != null) { + hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql")); + } + return hibernateProperties; + } + +} diff --git a/spring-data-spring-security/src/main/java/com/baeldung/SpringSecurityConfig.java b/spring-data-spring-security/src/main/java/com/baeldung/SpringSecurityConfig.java new file mode 100644 index 0000000000..ee13678a24 --- /dev/null +++ b/spring-data-spring-security/src/main/java/com/baeldung/SpringSecurityConfig.java @@ -0,0 +1,89 @@ +package com.baeldung; + +import javax.annotation.PostConstruct; +import javax.sql.DataSource; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.authentication.dao.DaoAuthenticationProvider; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.builders.WebSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.security.data.repository.query.SecurityEvaluationContextExtension; +import org.springframework.web.context.WebApplicationContext; + +import com.baeldung.security.AuthenticationSuccessHandlerImpl; +import com.baeldung.security.CustomUserDetailsService; + +@Configuration +@EnableWebSecurity +@ComponentScan("com.baeldung.security") +public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { + + @Autowired + private WebApplicationContext applicationContext; + private CustomUserDetailsService userDetailsService; + @Autowired + private AuthenticationSuccessHandlerImpl successHandler; + @Autowired + private DataSource dataSource; + + @PostConstruct + public void completeSetup() { + userDetailsService = applicationContext.getBean(CustomUserDetailsService.class); + } + + @Override + protected void configure(final AuthenticationManagerBuilder auth) throws Exception { + auth.userDetailsService(userDetailsService) + .passwordEncoder(encoder()) + .and() + .authenticationProvider(authenticationProvider()) + .jdbcAuthentication() + .dataSource(dataSource); + } + + @Override + public void configure(WebSecurity web) throws Exception { + web.ignoring() + .antMatchers("/resources/**"); + } + + @Override + protected void configure(final HttpSecurity http) throws Exception { + http.authorizeRequests() + .antMatchers("/login") + .permitAll() + .and() + .formLogin() + .permitAll() + .successHandler(successHandler) + .and() + .csrf() + .disable(); + } + + @Bean + public DaoAuthenticationProvider authenticationProvider() { + final DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider(); + authProvider.setUserDetailsService(userDetailsService); + authProvider.setPasswordEncoder(encoder()); + return authProvider; + } + + @Bean + public PasswordEncoder encoder() { + return new BCryptPasswordEncoder(11); + } + + @Bean + public SecurityEvaluationContextExtension securityEvaluationContextExtension() { + return new SecurityEvaluationContextExtension(); + } +} diff --git a/spring-data-spring-security/src/main/java/com/baeldung/data/repositories/TweetRepository.java b/spring-data-spring-security/src/main/java/com/baeldung/data/repositories/TweetRepository.java new file mode 100644 index 0000000000..7d6446ed0d --- /dev/null +++ b/spring-data-spring-security/src/main/java/com/baeldung/data/repositories/TweetRepository.java @@ -0,0 +1,14 @@ +package com.baeldung.data.repositories; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.PagingAndSortingRepository; + +import com.baeldung.models.Tweet; + +public interface TweetRepository extends PagingAndSortingRepository { + + @Query("select twt from Tweet twt JOIN twt.likes as lk where lk = ?#{ principal?.username } or twt.owner = ?#{ principal?.username }") + Page getMyTweetsAndTheOnesILiked(Pageable pageable); +} diff --git a/spring-data-spring-security/src/main/java/com/baeldung/data/repositories/UserRepository.java b/spring-data-spring-security/src/main/java/com/baeldung/data/repositories/UserRepository.java new file mode 100644 index 0000000000..9f13c3197e --- /dev/null +++ b/spring-data-spring-security/src/main/java/com/baeldung/data/repositories/UserRepository.java @@ -0,0 +1,27 @@ +package com.baeldung.data.repositories; + +import java.util.Date; +import java.util.List; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +import com.baeldung.models.AppUser; +import com.baeldung.models.Tweet; + +public interface UserRepository extends CrudRepository { + AppUser findByUsername(String username); + + List findByName(String name); + + @Query("UPDATE AppUser u SET u.lastLogin=:lastLogin WHERE u.username = ?#{ principal?.username }") + @Modifying + @Transactional + public void updateLastLogin(@Param("lastLogin") Date lastLogin); +} diff --git a/spring-data-spring-security/src/main/java/com/baeldung/models/AppUser.java b/spring-data-spring-security/src/main/java/com/baeldung/models/AppUser.java new file mode 100644 index 0000000000..e48233f90a --- /dev/null +++ b/spring-data-spring-security/src/main/java/com/baeldung/models/AppUser.java @@ -0,0 +1,83 @@ +package com.baeldung.models; + +import java.util.Date; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "users") +public class AppUser { + + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE) + private long id; + + private String name; + @Column(unique = true) + private String username; + private String password; + private boolean enabled = true; + private Date lastLogin; + + private AppUser() { + } + + public AppUser(String name, String email, String password) { + this.username = email; + this.name = name; + this.password = password; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + 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; + } + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public Date getLastLogin() { + return lastLogin; + } + + public void setLastLogin(Date lastLogin) { + this.lastLogin = lastLogin; + } +} diff --git a/spring-data-spring-security/src/main/java/com/baeldung/models/Tweet.java b/spring-data-spring-security/src/main/java/com/baeldung/models/Tweet.java new file mode 100644 index 0000000000..b2e45009f6 --- /dev/null +++ b/spring-data-spring-security/src/main/java/com/baeldung/models/Tweet.java @@ -0,0 +1,63 @@ +package com.baeldung.models; + +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.ElementCollection; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Tweet { + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE) + private long id; + private String tweet; + private String owner; + @ElementCollection(targetClass = String.class, fetch = FetchType.EAGER) + private Set likes = new HashSet(); + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + private Tweet() { + } + + public Tweet(String tweet, String owner) { + this.tweet = tweet; + this.owner = owner; + } + + public String getTweet() { + return tweet; + } + + public void setTweet(String tweet) { + this.tweet = tweet; + } + + public String getOwner() { + return owner; + } + + public void setOwner(String owner) { + this.owner = owner; + } + + public Set getLikes() { + return likes; + } + + public void setLikes(Set likes) { + this.likes = likes; + } + +} diff --git a/spring-data-spring-security/src/main/java/com/baeldung/security/AppUserPrincipal.java b/spring-data-spring-security/src/main/java/com/baeldung/security/AppUserPrincipal.java new file mode 100644 index 0000000000..195f9f7bf6 --- /dev/null +++ b/spring-data-spring-security/src/main/java/com/baeldung/security/AppUserPrincipal.java @@ -0,0 +1,67 @@ +package com.baeldung.security; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.userdetails.UserDetails; + +import com.baeldung.models.AppUser; + +public class AppUserPrincipal implements UserDetails { + + private final AppUser user; + + // + + public AppUserPrincipal(AppUser user) { + this.user = user; + } + + // + + @Override + public String getUsername() { + return user.getUsername(); + } + + @Override + public String getPassword() { + return user.getPassword(); + } + + @Override + public Collection getAuthorities() { + final List authorities = Collections.singletonList(new SimpleGrantedAuthority("User")); + return authorities; + } + + @Override + public boolean isAccountNonExpired() { + return true; + } + + @Override + public boolean isAccountNonLocked() { + return true; + } + + @Override + public boolean isCredentialsNonExpired() { + return true; + } + + @Override + public boolean isEnabled() { + return true; + } + + // + + public AppUser getAppUser() { + return user; + } + +} diff --git a/spring-data-spring-security/src/main/java/com/baeldung/security/AuthenticationSuccessHandlerImpl.java b/spring-data-spring-security/src/main/java/com/baeldung/security/AuthenticationSuccessHandlerImpl.java new file mode 100644 index 0000000000..3fc2bc6559 --- /dev/null +++ b/spring-data-spring-security/src/main/java/com/baeldung/security/AuthenticationSuccessHandlerImpl.java @@ -0,0 +1,28 @@ +package com.baeldung.security; + +import java.io.IOException; +import java.util.Date; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.Authentication; +import org.springframework.security.web.authentication.AuthenticationSuccessHandler; +import org.springframework.stereotype.Component; + +import com.baeldung.data.repositories.UserRepository; + +@Component +public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler { + + @Autowired + private UserRepository userRepository; + + @Override + public void onAuthenticationSuccess(HttpServletRequest arg0, HttpServletResponse arg1, Authentication arg2) throws IOException, ServletException { + userRepository.updateLastLogin(new Date()); + } + +} diff --git a/spring-data-spring-security/src/main/java/com/baeldung/security/CustomUserDetailsService.java b/spring-data-spring-security/src/main/java/com/baeldung/security/CustomUserDetailsService.java new file mode 100644 index 0000000000..016f4f7fa9 --- /dev/null +++ b/spring-data-spring-security/src/main/java/com/baeldung/security/CustomUserDetailsService.java @@ -0,0 +1,40 @@ +package com.baeldung.security; + +import javax.annotation.PostConstruct; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.stereotype.Service; +import org.springframework.web.context.WebApplicationContext; + +import com.baeldung.data.repositories.UserRepository; +import com.baeldung.models.AppUser; + +@Service +public class CustomUserDetailsService implements UserDetailsService { + + @Autowired + private WebApplicationContext applicationContext; + private UserRepository userRepository; + + public CustomUserDetailsService() { + super(); + } + + @PostConstruct + public void completeSetup() { + userRepository = applicationContext.getBean(UserRepository.class); + } + + @Override + public UserDetails loadUserByUsername(final String username) { + final AppUser appUser = userRepository.findByUsername(username); + if (appUser == null) { + throw new UsernameNotFoundException(username); + } + return new AppUserPrincipal(appUser); + } + +} diff --git a/spring-data-spring-security/src/main/java/com/baeldung/util/DummyContentUtil.java b/spring-data-spring-security/src/main/java/com/baeldung/util/DummyContentUtil.java new file mode 100644 index 0000000000..f1640264d2 --- /dev/null +++ b/spring-data-spring-security/src/main/java/com/baeldung/util/DummyContentUtil.java @@ -0,0 +1,63 @@ +package com.baeldung.util; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Random; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; + +import com.baeldung.models.AppUser; +import com.baeldung.models.Tweet; + +public class DummyContentUtil { + + public static final List generateDummyUsers() { + List appUsers = new ArrayList<>(); + BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); + appUsers.add(new AppUser("Lionel Messi", "lionel@messi.com", passwordEncoder.encode("li1234"))); + appUsers.add(new AppUser("Cristiano Ronaldo", "cristiano@ronaldo.com", passwordEncoder.encode("c1234"))); + appUsers.add(new AppUser("Neymar Dos Santos", "neymar@neymar.com", passwordEncoder.encode("n1234"))); + appUsers.add(new AppUser("Luiz Suarez", "luiz@suarez.com", passwordEncoder.encode("lu1234"))); + appUsers.add(new AppUser("Andres Iniesta", "andres@iniesta.com", passwordEncoder.encode("a1234"))); + appUsers.add(new AppUser("Ivan Rakitic", "ivan@rakitic.com", passwordEncoder.encode("i1234"))); + appUsers.add(new AppUser("Ousman Dembele", "ousman@dembele.com", passwordEncoder.encode("o1234"))); + appUsers.add(new AppUser("Sergio Busquet", "sergio@busquet.com", passwordEncoder.encode("s1234"))); + appUsers.add(new AppUser("Gerard Pique", "gerard@pique.com", passwordEncoder.encode("g1234"))); + appUsers.add(new AppUser("Ter Stergen", "ter@stergen.com", passwordEncoder.encode("t1234"))); + return appUsers; + } + + public static final List generateDummyTweets(List users) { + List tweets = new ArrayList<>(); + Random random = new Random(); + IntStream.range(0, 9) + .sequential() + .forEach(i -> { + Tweet twt = new Tweet(String.format("Tweet %d", i), users.get(random.nextInt(users.size())) + .getUsername()); + twt.getLikes() + .addAll(users.subList(0, random.nextInt(users.size())) + .stream() + .map(AppUser::getUsername) + .collect(Collectors.toSet())); + tweets.add(twt); + }); + return tweets; + } + + public static Collection getAuthorities() { + Collection grantedAuthorities = new ArrayList(); + GrantedAuthority grantedAuthority = new GrantedAuthority() { + public String getAuthority() { + return "ROLE_USER"; + } + }; + grantedAuthorities.add(grantedAuthority); + return grantedAuthorities; + } + +} diff --git a/spring-data-spring-security/src/main/resources/application.properties b/spring-data-spring-security/src/main/resources/application.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/spring-data-spring-security/src/main/resources/persistence-h2.properties b/spring-data-spring-security/src/main/resources/persistence-h2.properties new file mode 100644 index 0000000000..a4b2af6361 --- /dev/null +++ b/spring-data-spring-security/src/main/resources/persistence-h2.properties @@ -0,0 +1,8 @@ +driverClassName=org.h2.Driver +url=jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1 +username=sa +password= + +hibernate.dialect=org.hibernate.dialect.H2Dialect +hibernate.show_sql=false +hibernate.hbm2ddl.auto=create-drop \ No newline at end of file diff --git a/spring-data-spring-security/src/test/java/com/baeldung/relationships/SpringDataWithSecurityTest.java b/spring-data-spring-security/src/test/java/com/baeldung/relationships/SpringDataWithSecurityTest.java new file mode 100644 index 0000000000..dbbfe7e85e --- /dev/null +++ b/spring-data-spring-security/src/test/java/com/baeldung/relationships/SpringDataWithSecurityTest.java @@ -0,0 +1,100 @@ +package com.baeldung.relationships; + +import static org.springframework.util.Assert.isTrue; + +import java.util.Date; +import java.util.List; + +import javax.servlet.ServletContext; + +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.InvalidDataAccessApiUsageException; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; + +import com.baeldung.AppConfig; +import com.baeldung.data.repositories.TweetRepository; +import com.baeldung.data.repositories.UserRepository; +import com.baeldung.models.AppUser; +import com.baeldung.models.Tweet; +import com.baeldung.security.AppUserPrincipal; +import com.baeldung.util.DummyContentUtil; + +@RunWith(SpringRunner.class) +@WebAppConfiguration +@ContextConfiguration +@DirtiesContext +public class SpringDataWithSecurityTest { + AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); + @Autowired + private ServletContext servletContext; + private static UserRepository userRepository; + private static TweetRepository tweetRepository; + + @Before + public void testInit() { + ctx.register(AppConfig.class); + ctx.setServletContext(servletContext); + ctx.refresh(); + userRepository = ctx.getBean(UserRepository.class); + tweetRepository = ctx.getBean(TweetRepository.class); + List appUsers = (List) userRepository.save(DummyContentUtil.generateDummyUsers()); + tweetRepository.save(DummyContentUtil.generateDummyTweets(appUsers)); + } + + @AfterClass + public static void tearDown() { + tweetRepository.deleteAll(); + userRepository.deleteAll(); + } + + @Test + public void givenAppUser_whenLoginSuccessful_shouldUpdateLastLogin() { + AppUser appUser = userRepository.findByUsername("lionel@messi.com"); + Authentication auth = new UsernamePasswordAuthenticationToken(new AppUserPrincipal(appUser), null, DummyContentUtil.getAuthorities()); + SecurityContextHolder.getContext() + .setAuthentication(auth); + userRepository.updateLastLogin(new Date()); + } + + @Test(expected = InvalidDataAccessApiUsageException.class) + public void givenNoAppUserInSecurityContext_whenUpdateLastLoginAttempted_shouldFail() { + userRepository.updateLastLogin(new Date()); + } + + @Test + public void givenAppUser_whenLoginSuccessful_shouldReadMyPagedTweets() { + AppUser appUser = userRepository.findByUsername("lionel@messi.com"); + Authentication auth = new UsernamePasswordAuthenticationToken(new AppUserPrincipal(appUser), null, DummyContentUtil.getAuthorities()); + SecurityContextHolder.getContext() + .setAuthentication(auth); + Page page = null; + do { + page = tweetRepository.getMyTweetsAndTheOnesILiked(new PageRequest(page != null ? page.getNumber() + 1 : 0, 5)); + for (Tweet twt : page.getContent()) { + isTrue((twt.getOwner() == appUser.getUsername()) || (twt.getLikes() + .contains(appUser.getUsername())), "I do not have any Tweets"); + } + } while (page.hasNext()); + } + + @Test(expected = InvalidDataAccessApiUsageException.class) + public void givenNoAppUser_whenPaginatedResultsRetrievalAttempted_shouldFail() { + Page page = null; + do { + page = tweetRepository.getMyTweetsAndTheOnesILiked(new PageRequest(page != null ? page.getNumber() + 1 : 0, 5)); + } while (page != null && page.hasNext()); + } +} diff --git a/spring-dispatcher-servlet/src/main/java/com/baeldung/springdispatcherservlet/controller/MultipartController.java b/spring-dispatcher-servlet/src/main/java/com/baeldung/springdispatcherservlet/controller/MultipartController.java index 1cc8261f7c..a693bf039f 100644 --- a/spring-dispatcher-servlet/src/main/java/com/baeldung/springdispatcherservlet/controller/MultipartController.java +++ b/spring-dispatcher-servlet/src/main/java/com/baeldung/springdispatcherservlet/controller/MultipartController.java @@ -25,14 +25,20 @@ public class MultipartController { try { InputStream in = file.getInputStream(); String path = new File(".").getAbsolutePath(); - FileOutputStream f = new FileOutputStream(path.substring(0, path.length()-1)+ "/uploads/" + file.getOriginalFilename()); - int ch; - while ((ch = in.read()) != -1) { - f.write(ch); + FileOutputStream f = new FileOutputStream(path.substring(0, path.length() - 1) + "/uploads/" + file.getOriginalFilename()); + try { + int ch; + while ((ch = in.read()) != -1) { + f.write(ch); + } + modelAndView.getModel().put("message", "File uploaded successfully!"); + } catch (Exception e) { + System.out.println("Exception uploading multipart: " + e); + } finally { + f.flush(); + f.close(); + in.close(); } - f.flush(); - f.close(); - modelAndView.getModel().put("message", "File uploaded successfully!"); } catch (Exception e) { System.out.println("Exception uploading multipart: " + e); } diff --git a/spring-ejb/README.md b/spring-ejb/README.md new file mode 100644 index 0000000000..8fa8833f3a --- /dev/null +++ b/spring-ejb/README.md @@ -0,0 +1,3 @@ +### Relevant Articles + +- [Integration Guide for Spring and EJB](http://www.baeldung.com/spring-ejb) diff --git a/spring-integration/pom.xml b/spring-integration/pom.xml index 4e210122b0..e3dd0d3f9a 100644 --- a/spring-integration/pom.xml +++ b/spring-integration/pom.xml @@ -18,7 +18,7 @@ UTF-8 - 4.3.5.RELEASE + 5.0.3.RELEASE 1.1.4.RELEASE 1.4.7 1.1.1 @@ -68,7 +68,7 @@ org.springframework.integration spring-integration-core - ${spring.integration.version} + ${spring.version} javax.activation @@ -84,17 +84,17 @@ org.springframework.integration spring-integration-twitter - ${spring.integration.version} + ${spring.version} org.springframework.integration spring-integration-mail - ${spring.integration.version} + ${spring.version} org.springframework.integration spring-integration-ftp - ${spring.integration.version} + ${spring.version} org.springframework.social @@ -104,7 +104,24 @@ org.springframework.integration spring-integration-file - ${spring.integration.version} + ${spring.version} + + + org.springframework.security + spring-security-config + ${spring.version} + + + org.springframework.integration + spring-integration-security + ${spring.version} + + + + org.springframework.security + spring-security-test + ${spring.version} + test junit diff --git a/spring-integration/src/main/java/com/baeldung/si/security/MessageConsumer.java b/spring-integration/src/main/java/com/baeldung/si/security/MessageConsumer.java new file mode 100644 index 0000000000..af925c63a7 --- /dev/null +++ b/spring-integration/src/main/java/com/baeldung/si/security/MessageConsumer.java @@ -0,0 +1,45 @@ +package com.baeldung.si.security; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Logger; + +import org.springframework.integration.annotation.ServiceActivator; +import org.springframework.messaging.Message; +import org.springframework.stereotype.Service; + +@Service +public class MessageConsumer { + + private String messageContent; + + private Map messagePSContent = new ConcurrentHashMap<>(); + + public String getMessageContent() { + return messageContent; + } + + public void setMessageContent(String messageContent) { + this.messageContent = messageContent; + } + + public Map getMessagePSContent() { + return messagePSContent; + } + + public void setMessagePSContent(Map messagePSContent) { + this.messagePSContent = messagePSContent; + } + + @ServiceActivator(inputChannel = "endDirectChannel") + public void endDirectFlow(Message message) { + setMessageContent(message.getPayload().toString()); + } + + @ServiceActivator(inputChannel = "finalPSResult") + public void endPSFlow(Message message) { + Logger.getAnonymousLogger().info(Thread.currentThread().getName() + " has completed ---------------------------"); + messagePSContent.put(Thread.currentThread().getName(), (String) message.getPayload()); + } + +} diff --git a/spring-integration/src/main/java/com/baeldung/si/security/SecuredDirectChannel.java b/spring-integration/src/main/java/com/baeldung/si/security/SecuredDirectChannel.java new file mode 100644 index 0000000000..964a07d7d7 --- /dev/null +++ b/spring-integration/src/main/java/com/baeldung/si/security/SecuredDirectChannel.java @@ -0,0 +1,50 @@ +package com.baeldung.si.security; + +import java.util.logging.Logger; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.integration.annotation.ServiceActivator; +import org.springframework.integration.channel.DirectChannel; +import org.springframework.integration.config.EnableIntegration; +import org.springframework.integration.security.channel.ChannelSecurityInterceptor; +import org.springframework.integration.security.channel.SecuredChannel; +import org.springframework.messaging.Message; +import org.springframework.security.access.AccessDecisionManager; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.security.authentication.AuthenticationManager; + +@Configuration +@EnableIntegration +public class SecuredDirectChannel { + + @Bean(name = "startDirectChannel") + @SecuredChannel(interceptor = "channelSecurityInterceptor", sendAccess = { "ROLE_VIEWER", "jane" }) + public DirectChannel startDirectChannel() { + return new DirectChannel(); + } + + @ServiceActivator(inputChannel = "startDirectChannel", outputChannel = "endDirectChannel") + @PreAuthorize("hasRole('ROLE_LOGGER')") + public Message logMessage(Message message) { + Logger.getAnonymousLogger().info(message.toString()); + return message; + } + + @Bean(name = "endDirectChannel") + @SecuredChannel(interceptor = "channelSecurityInterceptor", sendAccess = { "ROLE_EDITOR" }) + public DirectChannel endDirectChannel() { + return new DirectChannel(); + } + + @Autowired + @Bean + public ChannelSecurityInterceptor channelSecurityInterceptor(AuthenticationManager authenticationManager, AccessDecisionManager customAccessDecisionManager) { + ChannelSecurityInterceptor channelSecurityInterceptor = new ChannelSecurityInterceptor(); + channelSecurityInterceptor.setAuthenticationManager(authenticationManager); + channelSecurityInterceptor.setAccessDecisionManager(customAccessDecisionManager); + return channelSecurityInterceptor; + } + +} diff --git a/spring-integration/src/main/java/com/baeldung/si/security/SecurityConfig.java b/spring-integration/src/main/java/com/baeldung/si/security/SecurityConfig.java new file mode 100644 index 0000000000..9c5b38b909 --- /dev/null +++ b/spring-integration/src/main/java/com/baeldung/si/security/SecurityConfig.java @@ -0,0 +1,46 @@ +package com.baeldung.si.security; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.integration.security.channel.ChannelSecurityInterceptor; +import org.springframework.security.access.AccessDecisionManager; +import org.springframework.security.access.AccessDecisionVoter; +import org.springframework.security.access.vote.AffirmativeBased; +import org.springframework.security.access.vote.RoleVoter; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; +import org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration; + +@Configuration +@EnableGlobalMethodSecurity(prePostEnabled = true) +public class SecurityConfig extends GlobalMethodSecurityConfiguration { + + @Override + @Bean + public AuthenticationManager authenticationManager() throws Exception { + return super.authenticationManager(); + } + + @Bean + public AccessDecisionManager customAccessDecisionManager() { + List> decisionVoters = new ArrayList<>(); + decisionVoters.add(new RoleVoter()); + decisionVoters.add(new UsernameAccessDecisionVoter()); + AccessDecisionManager accessDecisionManager = new AffirmativeBased(decisionVoters); + return accessDecisionManager; + } + + @Autowired + @Bean + public ChannelSecurityInterceptor channelSecurityInterceptor(AuthenticationManager authenticationManager, AccessDecisionManager customAccessDecisionManager) { + ChannelSecurityInterceptor channelSecurityInterceptor = new ChannelSecurityInterceptor(); + channelSecurityInterceptor.setAuthenticationManager(authenticationManager); + channelSecurityInterceptor.setAccessDecisionManager(customAccessDecisionManager); + return channelSecurityInterceptor; + } + +} diff --git a/spring-integration/src/main/java/com/baeldung/si/security/SecurityPubSubChannel.java b/spring-integration/src/main/java/com/baeldung/si/security/SecurityPubSubChannel.java new file mode 100644 index 0000000000..11409bb89b --- /dev/null +++ b/spring-integration/src/main/java/com/baeldung/si/security/SecurityPubSubChannel.java @@ -0,0 +1,82 @@ +package com.baeldung.si.security; + +import java.util.stream.Collectors; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.integration.annotation.ServiceActivator; +import org.springframework.integration.channel.DirectChannel; +import org.springframework.integration.channel.PublishSubscribeChannel; +import org.springframework.integration.config.EnableIntegration; +import org.springframework.integration.config.GlobalChannelInterceptor; +import org.springframework.integration.security.channel.SecuredChannel; +import org.springframework.integration.security.channel.SecurityContextPropagationChannelInterceptor; +import org.springframework.integration.support.DefaultMessageBuilderFactory; +import org.springframework.integration.support.MessageBuilder; +import org.springframework.messaging.Message; +import org.springframework.messaging.support.ChannelInterceptor; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.core.context.SecurityContextHolder; + +@Configuration +@EnableIntegration +public class SecurityPubSubChannel { + + @Bean(name = "startPSChannel") + @SecuredChannel(interceptor = "channelSecurityInterceptor", sendAccess = "ROLE_VIEWER") + public PublishSubscribeChannel startChannel() { + return new PublishSubscribeChannel(executor()); + } + + @ServiceActivator(inputChannel = "startPSChannel", outputChannel = "finalPSResult") + @PreAuthorize("hasRole('ROLE_LOGGER')") + public Message changeMessageToRole(Message message) { + return buildNewMessage(getRoles(), message); + } + + @ServiceActivator(inputChannel = "startPSChannel", outputChannel = "finalPSResult") + @PreAuthorize("hasRole('ROLE_VIEWER')") + public Message changeMessageToUserName(Message message) { + return buildNewMessage(getUsername(), message); + } + + @Bean(name = "finalPSResult") + public DirectChannel finalPSResult() { + return new DirectChannel(); + } + + @Bean + @GlobalChannelInterceptor(patterns = { "startPSChannel", "endDirectChannel" }) + public ChannelInterceptor securityContextPropagationInterceptor() { + return new SecurityContextPropagationChannelInterceptor(); + } + + @Bean + public ThreadPoolTaskExecutor executor() { + ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor(); + pool.setCorePoolSize(10); + pool.setMaxPoolSize(10); + pool.setWaitForTasksToCompleteOnShutdown(true); + return pool; + } + + public String getRoles() { + SecurityContext securityContext = SecurityContextHolder.getContext(); + return securityContext.getAuthentication().getAuthorities().stream().map(auth -> auth.getAuthority()).collect(Collectors.joining(",")); + } + + public String getUsername() { + SecurityContext securityContext = SecurityContextHolder.getContext(); + return securityContext.getAuthentication().getName(); + } + + public Message buildNewMessage(String content, Message message) { + DefaultMessageBuilderFactory builderFactory = new DefaultMessageBuilderFactory(); + MessageBuilder messageBuilder = builderFactory.withPayload(content); + messageBuilder.copyHeaders(message.getHeaders()); + return messageBuilder.build(); + } + +} diff --git a/spring-integration/src/main/java/com/baeldung/si/security/UsernameAccessDecisionVoter.java b/spring-integration/src/main/java/com/baeldung/si/security/UsernameAccessDecisionVoter.java new file mode 100644 index 0000000000..052f79ddf7 --- /dev/null +++ b/spring-integration/src/main/java/com/baeldung/si/security/UsernameAccessDecisionVoter.java @@ -0,0 +1,45 @@ +package com.baeldung.si.security; + +import java.util.Collection; + +import org.springframework.security.access.AccessDecisionVoter; +import org.springframework.security.access.ConfigAttribute; +import org.springframework.security.core.Authentication; + +public class UsernameAccessDecisionVoter implements AccessDecisionVoter { + private String rolePrefix = "ROLE_"; + + @Override + public boolean supports(ConfigAttribute attribute) { + if ((attribute.getAttribute() != null) + && !attribute.getAttribute().startsWith(rolePrefix)) { + return true; + }else { + return false; + } + } + + @Override + public boolean supports(Class clazz) { + return true; + } + + @Override + public int vote(Authentication authentication, Object object, Collection attributes) { + if (authentication == null) { + return ACCESS_DENIED; + } + String name = authentication.getName(); + int result = ACCESS_ABSTAIN; + for (ConfigAttribute attribute : attributes) { + if (this.supports(attribute)) { + result = ACCESS_DENIED; + if (attribute.getAttribute().equals(name)) { + return ACCESS_GRANTED; + } + } + } + return result; + } + +} diff --git a/spring-integration/src/test/java/com/baeldung/si/TestSpringIntegrationSecurity.java b/spring-integration/src/test/java/com/baeldung/si/TestSpringIntegrationSecurity.java new file mode 100644 index 0000000000..9ae82af2dc --- /dev/null +++ b/spring-integration/src/test/java/com/baeldung/si/TestSpringIntegrationSecurity.java @@ -0,0 +1,81 @@ +package com.baeldung.si; + +import static org.junit.Assert.assertEquals; + +import org.hamcrest.core.IsInstanceOf; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.messaging.SubscribableChannel; +import org.springframework.messaging.support.GenericMessage; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.si.security.MessageConsumer; +import com.baeldung.si.security.SecuredDirectChannel; +import com.baeldung.si.security.SecurityConfig; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { SecurityConfig.class, SecuredDirectChannel.class, MessageConsumer.class }) +public class TestSpringIntegrationSecurity { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Autowired + SubscribableChannel startDirectChannel; + + @Autowired + MessageConsumer messageConsumer; + + final String DIRECT_CHANNEL_MESSAGE = "Direct channel message"; + + @Test(expected = AuthenticationCredentialsNotFoundException.class) + public void givenNoUser_whenSendToDirectChannel_thenCredentialNotFound() { + startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); + } + + @Test + @WithMockUser(username = "jane", roles = { "LOGGER" }) + public void givenRoleLogger_whenSendMessageToDirectChannel_thenAccessDenied() { + expectedException.expectCause(IsInstanceOf. instanceOf(AccessDeniedException.class)); + + startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); + } + + @Test + @WithMockUser(username = "jane") + public void givenJane_whenSendMessageToDirectChannel_thenAccessDenied() { + expectedException.expectCause(IsInstanceOf. instanceOf(AccessDeniedException.class)); + + startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); + } + + @Test + @WithMockUser(roles = { "VIEWER" }) + public void givenRoleViewer_whenSendToDirectChannel_thenAccessDenied() { + expectedException.expectCause(IsInstanceOf. instanceOf(AccessDeniedException.class)); + + startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); + } + + @Test + @WithMockUser(roles = { "LOGGER", "VIEWER", "EDITOR" }) + public void givenRoleLoggerAndUser_whenSendMessageToDirectChannel_thenFlowCompletedSuccessfully() { + startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); + assertEquals(DIRECT_CHANNEL_MESSAGE, messageConsumer.getMessageContent()); + } + + @Test + @WithMockUser(username = "jane", roles = { "LOGGER", "EDITOR" }) + public void givenJaneLoggerEditor_whenSendToDirectChannel_thenFlowCompleted() { + startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); + assertEquals(DIRECT_CHANNEL_MESSAGE, messageConsumer.getMessageContent()); + } + +} \ No newline at end of file diff --git a/spring-integration/src/test/java/com/baeldung/si/TestSpringIntegrationSecurityExecutor.java b/spring-integration/src/test/java/com/baeldung/si/TestSpringIntegrationSecurityExecutor.java new file mode 100644 index 0000000000..b06136a7ca --- /dev/null +++ b/spring-integration/src/test/java/com/baeldung/si/TestSpringIntegrationSecurityExecutor.java @@ -0,0 +1,68 @@ +package com.baeldung.si; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.messaging.SubscribableChannel; +import org.springframework.messaging.support.GenericMessage; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.si.security.MessageConsumer; +import com.baeldung.si.security.SecurityConfig; +import com.baeldung.si.security.SecurityPubSubChannel; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { SecurityPubSubChannel.class, MessageConsumer.class, SecurityConfig.class }) +public class TestSpringIntegrationSecurityExecutor { + + @Autowired + SubscribableChannel startPSChannel; + + @Autowired + MessageConsumer messageConsumer; + + @Autowired + ThreadPoolTaskExecutor executor; + + final String DIRECT_CHANNEL_MESSAGE = "Direct channel message"; + + @Before + public void clearData() { + messageConsumer.setMessagePSContent(new ConcurrentHashMap<>()); + executor.setWaitForTasksToCompleteOnShutdown(true); + } + + @Test + @WithMockUser(username = "user", roles = { "VIEWER" }) + public void givenRoleUser_whenSendMessageToPSChannel_thenNoMessageArrived() throws IllegalStateException, InterruptedException { + startPSChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); + + executor.getThreadPoolExecutor().awaitTermination(2, TimeUnit.SECONDS); + + assertEquals(1, messageConsumer.getMessagePSContent().size()); + assertTrue(messageConsumer.getMessagePSContent().values().contains("user")); + } + + @Test + @WithMockUser(username = "user", roles = { "LOGGER", "VIEWER" }) + public void givenRoleUserAndLogger_whenSendMessageToPSChannel_then2GetMessages() throws IllegalStateException, InterruptedException { + startPSChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); + + executor.getThreadPoolExecutor().awaitTermination(2, TimeUnit.SECONDS); + + assertEquals(2, messageConsumer.getMessagePSContent().size()); + assertTrue(messageConsumer.getMessagePSContent().values().contains("user")); + assertTrue(messageConsumer.getMessagePSContent().values().contains("ROLE_LOGGER,ROLE_VIEWER")); + } + +} diff --git a/spring-integration/src/test/resources/logback-test.xml b/spring-integration/src/test/resources/logback-test.xml new file mode 100644 index 0000000000..a807be0ca2 --- /dev/null +++ b/spring-integration/src/test/resources/logback-test.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/spring-jinq/README.md b/spring-jinq/README.md new file mode 100644 index 0000000000..10d7903f49 --- /dev/null +++ b/spring-jinq/README.md @@ -0,0 +1,4 @@ +## Relevant articles: + +- [Introduction to Jinq with Spring](http://www.baeldung.com/spring-jinq) + diff --git a/spring-jinq/pom.xml b/spring-jinq/pom.xml new file mode 100644 index 0000000000..a895ae8dd4 --- /dev/null +++ b/spring-jinq/pom.xml @@ -0,0 +1,83 @@ + + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + 4.0.0 + spring-jinq + 0.1-SNAPSHOT + + spring-jinq + + jar + + + UTF-8 + 1.8 + + 1.8.22 + + + + + + org.springframework.boot + spring-boot-dependencies + 1.5.9.RELEASE + pom + import + + + + + + + org.jinq + jinq-jpa + ${jinq.version} + + + + + com.h2database + h2 + + + + org.hibernate + hibernate-entitymanager + + + + + org.springframework + spring-orm + + + + + org.springframework.boot + spring-boot-starter-test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${java.version} + ${java.version} + ${project.build.sourceEncoding} + false + + + + + + diff --git a/spring-jinq/src/main/java/com/baeldung/spring/jinq/JinqApplication.java b/spring-jinq/src/main/java/com/baeldung/spring/jinq/JinqApplication.java new file mode 100644 index 0000000000..d53b585d36 --- /dev/null +++ b/spring-jinq/src/main/java/com/baeldung/spring/jinq/JinqApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.spring.jinq; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class JinqApplication { + + public static void main(String[] args) { + SpringApplication.run(JinqApplication.class, args); + } +} diff --git a/spring-jinq/src/main/java/com/baeldung/spring/jinq/config/JinqProviderConfiguration.java b/spring-jinq/src/main/java/com/baeldung/spring/jinq/config/JinqProviderConfiguration.java new file mode 100644 index 0000000000..6d921045b7 --- /dev/null +++ b/spring-jinq/src/main/java/com/baeldung/spring/jinq/config/JinqProviderConfiguration.java @@ -0,0 +1,18 @@ +package com.baeldung.spring.jinq.config; + +import javax.persistence.EntityManagerFactory; + +import org.jinq.jpa.JinqJPAStreamProvider; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class JinqProviderConfiguration { + + @Bean + @Autowired + JinqJPAStreamProvider jinqProvider(EntityManagerFactory emf) { + return new JinqJPAStreamProvider(emf); + } +} diff --git a/spring-jinq/src/main/java/com/baeldung/spring/jinq/entities/Car.java b/spring-jinq/src/main/java/com/baeldung/spring/jinq/entities/Car.java new file mode 100644 index 0000000000..263e6c7622 --- /dev/null +++ b/spring-jinq/src/main/java/com/baeldung/spring/jinq/entities/Car.java @@ -0,0 +1,58 @@ +package com.baeldung.spring.jinq.entities; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToOne; + +@Entity(name = "CAR") +public class Car { + private String model; + private String description; + private int year; + private String engine; + private Manufacturer manufacturer; + + @Id + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public int getYear() { + return year; + } + + public void setYear(int year) { + this.year = year; + } + + public String getEngine() { + return engine; + } + + public void setEngine(String engine) { + this.engine = engine; + } + + @OneToOne + @JoinColumn(name = "name") + public Manufacturer getManufacturer() { + return manufacturer; + } + + public void setManufacturer(Manufacturer manufacturer) { + this.manufacturer = manufacturer; + } +} diff --git a/spring-jinq/src/main/java/com/baeldung/spring/jinq/entities/Manufacturer.java b/spring-jinq/src/main/java/com/baeldung/spring/jinq/entities/Manufacturer.java new file mode 100644 index 0000000000..f6e5fd23de --- /dev/null +++ b/spring-jinq/src/main/java/com/baeldung/spring/jinq/entities/Manufacturer.java @@ -0,0 +1,42 @@ +package com.baeldung.spring.jinq.entities; + +import java.util.List; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToMany; + +@Entity(name = "MANUFACTURER") +public class Manufacturer { + + private String name; + private String city; + private List cars; + + @Id + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + @OneToMany(mappedBy = "model") + public List getCars() { + return cars; + } + + public void setCars(List cars) { + this.cars = cars; + } + +} diff --git a/spring-jinq/src/main/java/com/baeldung/spring/jinq/repositories/BaseJinqRepositoryImpl.java b/spring-jinq/src/main/java/com/baeldung/spring/jinq/repositories/BaseJinqRepositoryImpl.java new file mode 100644 index 0000000000..42b81ecc59 --- /dev/null +++ b/spring-jinq/src/main/java/com/baeldung/spring/jinq/repositories/BaseJinqRepositoryImpl.java @@ -0,0 +1,26 @@ +package com.baeldung.spring.jinq.repositories; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import org.jinq.jpa.JPAJinqStream; +import org.jinq.jpa.JinqJPAStreamProvider; +import org.springframework.beans.factory.annotation.Autowired; + +public abstract class BaseJinqRepositoryImpl { + @Autowired + private JinqJPAStreamProvider jinqDataProvider; + + @PersistenceContext + private EntityManager entityManager; + + protected abstract Class entityType(); + + public JPAJinqStream stream() { + return streamOf(entityType()); + } + + protected JPAJinqStream streamOf(Class clazz) { + return jinqDataProvider.streamAll(entityManager, clazz); + } +} diff --git a/spring-jinq/src/main/java/com/baeldung/spring/jinq/repositories/CarRepository.java b/spring-jinq/src/main/java/com/baeldung/spring/jinq/repositories/CarRepository.java new file mode 100644 index 0000000000..56f6106e08 --- /dev/null +++ b/spring-jinq/src/main/java/com/baeldung/spring/jinq/repositories/CarRepository.java @@ -0,0 +1,27 @@ +package com.baeldung.spring.jinq.repositories; + +import java.util.List; +import java.util.Optional; + +import org.jinq.tuples.Pair; +import org.jinq.tuples.Tuple3; + +import com.baeldung.spring.jinq.entities.Car; +import com.baeldung.spring.jinq.entities.Manufacturer; + +public interface CarRepository { + + Optional findByModel(String model); + + List findByModelAndDescription(String model, String desc); + + List> findWithModelYearAndEngine(); + + Optional findManufacturerByModel(String model); + + List> findCarsPerManufacturer(); + + long countCarsByModel(String model); + + List findAll(int skip, int limit); +} diff --git a/spring-jinq/src/main/java/com/baeldung/spring/jinq/repositories/CarRepositoryImpl.java b/spring-jinq/src/main/java/com/baeldung/spring/jinq/repositories/CarRepositoryImpl.java new file mode 100644 index 0000000000..bf16c87461 --- /dev/null +++ b/spring-jinq/src/main/java/com/baeldung/spring/jinq/repositories/CarRepositoryImpl.java @@ -0,0 +1,72 @@ +package com.baeldung.spring.jinq.repositories; + +import java.util.List; +import java.util.Optional; + +import org.jinq.orm.stream.JinqStream; +import org.jinq.tuples.Pair; +import org.jinq.tuples.Tuple3; +import org.springframework.stereotype.Repository; + +import com.baeldung.spring.jinq.entities.Car; +import com.baeldung.spring.jinq.entities.Manufacturer; + +@Repository +public class CarRepositoryImpl extends BaseJinqRepositoryImpl implements CarRepository { + + @Override + public Optional findByModel(String model) { + return stream().where(c -> c.getModel() + .equals(model)) + .findFirst(); + } + + @Override + public List findByModelAndDescription(String model, String desc) { + return stream().where(c -> c.getModel() + .equals(model) + && c.getDescription() + .contains(desc)) + .toList(); + } + + @Override + public List> findWithModelYearAndEngine() { + return stream().select(c -> new Tuple3<>(c.getModel(), c.getYear(), c.getEngine())) + .toList(); + } + + @Override + public Optional findManufacturerByModel(String model) { + return stream().where(c -> c.getModel() + .equals(model)) + .select(c -> c.getManufacturer()) + .findFirst(); + } + + @Override + public List> findCarsPerManufacturer() { + return streamOf(Manufacturer.class).join(m -> JinqStream.from(m.getCars())) + .toList(); + } + + @Override + public long countCarsByModel(String model) { + return stream().where(c -> c.getModel() + .equals(model)) + .count(); + } + + @Override + public List findAll(int skip, int limit) { + return stream().skip(skip) + .limit(limit) + .toList(); + } + + @Override + protected Class entityType() { + return Car.class; + } + +} diff --git a/spring-jinq/src/main/resources/application.properties b/spring-jinq/src/main/resources/application.properties new file mode 100644 index 0000000000..dc73bed0c5 --- /dev/null +++ b/spring-jinq/src/main/resources/application.properties @@ -0,0 +1,7 @@ +spring.datasource.url=jdbc:h2:~/jinq +spring.datasource.username=sa +spring.datasource.password= + +spring.jpa.hibernate.ddl-auto=create-drop +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.format_sql=true \ No newline at end of file diff --git a/spring-jinq/src/test/java/com/baeldung/spring/jinq/repositories/CarRepositoryIntegrationTest.java b/spring-jinq/src/test/java/com/baeldung/spring/jinq/repositories/CarRepositoryIntegrationTest.java new file mode 100644 index 0000000000..9cb126cbaa --- /dev/null +++ b/spring-jinq/src/test/java/com/baeldung/spring/jinq/repositories/CarRepositoryIntegrationTest.java @@ -0,0 +1,42 @@ +package com.baeldung.spring.jinq.repositories; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.spring.jinq.JinqApplication; + +@ContextConfiguration(classes = JinqApplication.class) +@RunWith(SpringJUnit4ClassRunner.class) +public class CarRepositoryIntegrationTest { + + @Autowired + private CarRepository repository; + + @Test + public void givenACar_whenFilter_thenShouldBeFound() { + assertThat(repository.findByModel("model1") + .isPresent()).isFalse(); + } + + @Test + public void givenACar_whenMultipleFilters_thenShouldBeFound() { + assertThat(repository.findByModelAndDescription("model1", "desc") + .isEmpty()).isTrue(); + } + + @Test + public void whenUseASelectClause() { + assertThat(repository.findWithModelYearAndEngine() + .isEmpty()).isTrue(); + } + + @Test + public void whenUsingOneToOneRelationship() { + assertThat(repository.findManufacturerByModel("model1")).isNotNull(); + } +} diff --git a/spring-mvc-java/pom.xml b/spring-mvc-java/pom.xml index b939f0496d..9d90ba2dbf 100644 --- a/spring-mvc-java/pom.xml +++ b/spring-mvc-java/pom.xml @@ -297,7 +297,7 @@ 4.3.4.RELEASE 4.2.0.RELEASE 2.1.5.RELEASE - 2.8.5 + 2.9.4 5.2.5.Final diff --git a/spring-mvc-kotlin/pom.xml b/spring-mvc-kotlin/pom.xml index 2d1dac5e29..8f9c58854d 100644 --- a/spring-mvc-kotlin/pom.xml +++ b/spring-mvc-kotlin/pom.xml @@ -17,38 +17,64 @@ war + + UTF-8 + 5.2.13.Final + 1.2.30 + 4.3.10.RELEASE + 3.0.7.RELEASE + 1.4.196 + + org.jetbrains.kotlin - kotlin-stdlib-jre8 - 1.1.4 + kotlin-stdlib-jdk8 + ${kotlin.version} org.springframework spring-web - 4.3.10.RELEASE + ${spring.version} org.springframework spring-webmvc - 4.3.10.RELEASE + ${spring.version} org.thymeleaf thymeleaf - 3.0.7.RELEASE + ${thymeleaf.version} org.thymeleaf thymeleaf-spring4 - 3.0.7.RELEASE + ${thymeleaf.version} + + + org.hibernate + hibernate-core + ${hibernate.version} + + + org.hibernate + hibernate-testing + ${hibernate.version} + test + + + com.h2database + h2 + ${h2.version} + test org.springframework spring-test - 4.3.10.RELEASE + ${spring.version} test @@ -60,10 +86,11 @@ kotlin-maven-plugin org.jetbrains.kotlin - 1.1.4 + ${kotlin.version} spring + jpa 1.8 @@ -87,7 +114,12 @@ org.jetbrains.kotlin kotlin-maven-allopen - 1.1.4-3 + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-maven-noarg + ${kotlin.version} diff --git a/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/jpa/Person.kt b/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/jpa/Person.kt new file mode 100644 index 0000000000..076dcdac46 --- /dev/null +++ b/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/jpa/Person.kt @@ -0,0 +1,24 @@ +package com.baeldung.jpa + +import javax.persistence.CascadeType +import javax.persistence.Column +import javax.persistence.Entity +import javax.persistence.FetchType +import javax.persistence.GeneratedValue +import javax.persistence.GenerationType +import javax.persistence.Id +import javax.persistence.OneToMany +import javax.persistence.Table + +@Entity +@Table(name = "person") +data class Person( + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + val id: Int, + @Column(nullable = false) + val name: String, + @Column(nullable = true) + val email: String?, + @Column(nullable = true) + @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.ALL]) val phoneNumbers: List?) \ No newline at end of file diff --git a/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/jpa/PhoneNumber.kt b/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/jpa/PhoneNumber.kt new file mode 100644 index 0000000000..25894275f6 --- /dev/null +++ b/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/jpa/PhoneNumber.kt @@ -0,0 +1,17 @@ +package com.baeldung.jpa + +import javax.persistence.Column +import javax.persistence.Entity +import javax.persistence.GeneratedValue +import javax.persistence.GenerationType +import javax.persistence.Id +import javax.persistence.Table + +@Entity +@Table(name = "phone_number") +data class PhoneNumber( + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + val id: Int, + @Column(nullable = false) + val number: String) \ No newline at end of file diff --git a/spring-mvc-kotlin/src/test/kotlin/com/baeldung/kotlin/jpa/HibernateKotlinIntegrationTest.kt b/spring-mvc-kotlin/src/test/kotlin/com/baeldung/kotlin/jpa/HibernateKotlinIntegrationTest.kt new file mode 100644 index 0000000000..94559812fa --- /dev/null +++ b/spring-mvc-kotlin/src/test/kotlin/com/baeldung/kotlin/jpa/HibernateKotlinIntegrationTest.kt @@ -0,0 +1,54 @@ +package com.baeldung.kotlin.jpa + +import com.baeldung.jpa.Person +import com.baeldung.jpa.PhoneNumber +import org.hibernate.cfg.Configuration +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase +import org.hibernate.testing.transaction.TransactionUtil.doInHibernate +import org.junit.Assert.assertTrue +import org.junit.Test +import java.io.IOException +import java.util.* + + +class HibernateKotlinIntegrationTest : BaseCoreFunctionalTestCase() { + + private val properties: Properties + @Throws(IOException::class) + get() { + val properties = Properties() + properties.load(javaClass.classLoader.getResourceAsStream("hibernate.properties")) + return properties + } + + override fun getAnnotatedClasses(): Array> { + return arrayOf(Person::class.java, PhoneNumber::class.java) + } + + override fun configure(configuration: Configuration) { + super.configure(configuration) + configuration.properties = properties + } + + @Test + fun givenPerson_whenSaved_thenFound() { + doInHibernate(({ this.sessionFactory() }), { session -> + val personToSave = Person(0, "John", "jhon@test.com", Arrays.asList(PhoneNumber(0, "202-555-0171"), PhoneNumber(0, "202-555-0102"))) + session.persist(personToSave) + val personFound = session.find(Person::class.java, personToSave.id) + session.refresh(personFound) + assertTrue(personToSave == personFound) + }) + } + + @Test + fun givenPersonWithNullFields_whenSaved_thenFound() { + doInHibernate(({ this.sessionFactory() }), { session -> + val personToSave = Person(0, "John", null, null) + session.persist(personToSave) + val personFound = session.find(Person::class.java, personToSave.id) + session.refresh(personFound) + assertTrue(personToSave == personFound) + }) + } +} \ No newline at end of file diff --git a/spring-mvc-kotlin/src/test/resources/hibernate.properties b/spring-mvc-kotlin/src/test/resources/hibernate.properties new file mode 100644 index 0000000000..7b8764637b --- /dev/null +++ b/spring-mvc-kotlin/src/test/resources/hibernate.properties @@ -0,0 +1,9 @@ +hibernate.connection.driver_class=org.h2.Driver +hibernate.connection.url=jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1 +hibernate.connection.username=sa +hibernate.connection.autocommit=true +jdbc.password= + +hibernate.dialect=org.hibernate.dialect.H2Dialect +hibernate.show_sql=true +hibernate.hbm2ddl.auto=create-drop \ No newline at end of file diff --git a/spring-mvc-push/.gitignore b/spring-mvc-push/.gitignore deleted file mode 100644 index 448298d47f..0000000000 --- a/spring-mvc-push/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/.tern-project diff --git a/spring-mvc-push/pom.xml b/spring-mvc-push/pom.xml deleted file mode 100644 index 2eb10381be..0000000000 --- a/spring-mvc-push/pom.xml +++ /dev/null @@ -1,91 +0,0 @@ - - 4.0.0 - com.baeldung - spring-mvc-push - war - 0.0.1-SNAPSHOT - spring-mvc-push - - 1.8 - 1.8 - 2.20 - 3.7.0 - 3.2.0 - UTF-8 - 5.0.2 - 5.0.2.RELEASE - 4.0.0 - 1.2 - 2.3.2-b02 - 5.0.2 - 1.0.2 - - - - org.springframework - spring-webmvc - ${spring.version} - - - javax.servlet - javax.servlet-api - ${servlet.version} - provided - - - javax.servlet - jstl - ${jstl.version} - - - javax.servlet.jsp - javax.servlet.jsp-api - ${jsp-api.version} - - - - org.springframework - spring-test - ${spring.version} - test - - - org.junit.jupiter - junit-jupiter-engine - ${junit.jupiter.version} - test - - - - - - org.apache.maven.plugins - maven-compiler-plugin - ${maven.compiler.version} - - - org.apache.maven.plugins - maven-war-plugin - ${maven-war-plugin.version} - - spring-mvc-push - false - ${deploy-path} - - - - maven-surefire-plugin - ${maven-surefire-plugin.version} - - - org.junit.platform - junit-platform-surefire-provider - ${junit.platform.version} - - - - - spring-mvc-push - - diff --git a/spring-mvc-push/src/main/java/com/baeldung/config/PushConfiguration.java b/spring-mvc-push/src/main/java/com/baeldung/config/PushConfiguration.java deleted file mode 100644 index e6188da92d..0000000000 --- a/spring-mvc-push/src/main/java/com/baeldung/config/PushConfiguration.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.baeldung.config; - -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletRegistration; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.WebApplicationInitializer; -import org.springframework.web.context.ContextLoaderListener; -import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; -import org.springframework.web.servlet.DispatcherServlet; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -import org.springframework.web.servlet.view.InternalResourceViewResolver; - -@Configuration -@EnableWebMvc -@ComponentScan(basePackages = "com.baeldung.controller") -public class PushConfiguration implements WebApplicationInitializer, WebMvcConfigurer { - - @Override - public void onStartup(ServletContext container) throws ServletException { - AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); - context.register(PushConfiguration.class); - container.addListener(new ContextLoaderListener(context)); - ServletRegistration.Dynamic dispatcher = container.addServlet("DispatcherServlet", new DispatcherServlet(context)); - dispatcher.setLoadOnStartup(1); - dispatcher.addMapping("/"); - } - - @Bean - public InternalResourceViewResolver jspViewResolver() { - InternalResourceViewResolver bean = new InternalResourceViewResolver(); - bean.setPrefix("/WEB-INF/views/"); - bean.setSuffix(".jsp"); - return bean; - } - - @Override - public void addResourceHandlers(ResourceHandlerRegistry registry) { - registry.addResourceHandler("/resources/**") - .addResourceLocations("/resources/"); - } - -} diff --git a/spring-mvc-push/src/main/webapp/index.jsp b/spring-mvc-push/src/main/webapp/index.jsp deleted file mode 100644 index 82ecb68003..0000000000 --- a/spring-mvc-push/src/main/webapp/index.jsp +++ /dev/null @@ -1,14 +0,0 @@ -<%@ page language="java" contentType="text/html; charset=UTF-8" - pageEncoding="UTF-8"%> - - - -PushBuilder demo - - -

- Go to PushBuilder demo
Go to - Simple demo -

- - \ No newline at end of file diff --git a/spring-mvc-push/src/main/webapp/resources/script.js b/spring-mvc-push/src/main/webapp/resources/script.js deleted file mode 100644 index 9bc97c006a..0000000000 --- a/spring-mvc-push/src/main/webapp/resources/script.js +++ /dev/null @@ -1 +0,0 @@ -console.log('Script') \ No newline at end of file diff --git a/spring-mvc-push/src/main/webapp/resources/style.css b/spring-mvc-push/src/main/webapp/resources/style.css deleted file mode 100644 index d5fc158135..0000000000 --- a/spring-mvc-push/src/main/webapp/resources/style.css +++ /dev/null @@ -1,9 +0,0 @@ -.single-title { - font-size: 30px; - color: #535353; - font-weight: 200; - letter-spacing: -1.5px; - line-height: 64px; - max-width: 750px; - font-family: "Helvetica Neue", Helvetica, Arial, sans-serif -} \ No newline at end of file diff --git a/spring-mvc-simple/README.md b/spring-mvc-simple/README.md index 197a22cbac..69a9027280 100644 --- a/spring-mvc-simple/README.md +++ b/spring-mvc-simple/README.md @@ -2,3 +2,4 @@ - [HandlerAdapters in Spring MVC](http://www.baeldung.com/spring-mvc-handler-adapters) - [Template Engines for Spring](http://www.baeldung.com/spring-template-engines) +- [Spring 5 and Servlet 4 – The PushBuilder](http://www.baeldung.com/spring-5-push) diff --git a/spring-mvc-simple/pom.xml b/spring-mvc-simple/pom.xml index 2cc6aec906..a2ba2188d6 100644 --- a/spring-mvc-simple/pom.xml +++ b/spring-mvc-simple/pom.xml @@ -8,83 +8,72 @@ Spring MVC simple Maven Webapp http://maven.apache.org - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - - - 4.3.10.RELEASE - 3.1.0 + 1.8 + 1.8 + UTF-8 + 5.0.2.RELEASE + 3.2.0 + 3.7.0 + 2.20 1.2 - 2.3.1 - 3.1.0 + 2.3.2-b02 + 4.0.0 5.4.1.Final enter-location-of-server 1.3.2 1.8 3.0.7.RELEASE 2.4.12 - 2.3.23 + 2.3.27-incubating 1.2.5 + 5.0.2 + 5.0.2 + 1.0.2 + 1.9.0 + 2.9.4 + 1.4.9 + 5.1.0 + 20180130 + + org.springframework + spring-oxm + 5.0.2.RELEASE + javax.servlet javax.servlet-api - 3.1.0 - - - org.springframework - spring-webmvc - ${springframework.version} - - - org.springframework - spring-context - ${springframework.version} - - - org.springframework - spring-core - ${springframework.version} - - - commons-logging - commons-logging - - + ${javax.servlet-api.version} javax.servlet.jsp javax.servlet.jsp-api ${javax.servlet.jsp-api.version} - javax.servlet jstl ${jstl.version} - org.hibernate hibernate-validator ${hibernate-validator.version} - - org.springframework - spring-webmvc - ${springframework.version} - commons-fileupload commons-fileupload ${fileupload.version} - + + org.springframework + spring-webmvc + ${springframework.version} + + org.thymeleaf @@ -115,7 +104,7 @@ groovy-templates ${groovy.version} - + de.neuland-bfi @@ -123,6 +112,39 @@ ${jade.version} + + + org.springframework + spring-test + ${springframework.version} + test + + + org.junit.jupiter + junit-jupiter-engine + ${junit.jupiter.version} + test + + + com.rometools + rome + ${rome.version} + + + com.thoughtworks.xstream + xstream + ${xstream.version} + + + com.github.scribejava + scribejava-apis + ${scribejava.version} + + + org.json + json + ${json.version} + @@ -141,11 +163,18 @@ org.apache.maven.plugins maven-compiler-plugin - 3.7.0 - - 1.8 - 1.8 - + ${maven.compiler.version} + + + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + org.junit.platform + junit-platform-surefire-provider + ${junit.platform.version} + + diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java index b62ccae465..c4c6791f9a 100644 --- a/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java @@ -1,19 +1,30 @@ package com.baeldung.spring.configuration; +import com.baeldung.spring.controller.rss.ArticleRssFeedViewResolver; +import com.baeldung.spring.controller.rss.JsonChannelHttpMessageConverter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.StringHttpMessageConverter; +import org.springframework.http.converter.feed.RssChannelHttpMessageConverter; +import org.springframework.web.accept.ContentNegotiationManager; import org.springframework.web.multipart.MultipartResolver; import org.springframework.web.multipart.commons.CommonsMultipartResolver; +import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.view.ContentNegotiatingViewResolver; import org.springframework.web.servlet.view.InternalResourceViewResolver; +import java.util.ArrayList; +import java.util.List; + @Configuration @EnableWebMvc @ComponentScan(basePackages = { "com.baeldung.springmvcforms", "com.baeldung.spring.controller", "com.baeldung.spring.validator" }) -class ApplicationConfiguration extends WebMvcConfigurerAdapter { +public class ApplicationConfiguration extends WebMvcConfigurerAdapter { @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { @@ -21,11 +32,19 @@ class ApplicationConfiguration extends WebMvcConfigurerAdapter { } @Bean - public InternalResourceViewResolver jspViewResolver() { - InternalResourceViewResolver bean = new InternalResourceViewResolver(); - bean.setPrefix("/WEB-INF/views/"); - bean.setSuffix(".jsp"); - return bean; + public ContentNegotiatingViewResolver viewResolver(ContentNegotiationManager cnManager) { + ContentNegotiatingViewResolver cnvResolver = new ContentNegotiatingViewResolver(); + cnvResolver.setContentNegotiationManager(cnManager); + List resolvers = new ArrayList<>(); + + InternalResourceViewResolver bean = new InternalResourceViewResolver("/WEB-INF/views/",".jsp"); + ArticleRssFeedViewResolver articleRssFeedViewResolver = new ArticleRssFeedViewResolver(); + + resolvers.add(bean); + resolvers.add(articleRssFeedViewResolver); + + cnvResolver.setViewResolvers(resolvers); + return cnvResolver; } @Bean @@ -34,4 +53,13 @@ class ApplicationConfiguration extends WebMvcConfigurerAdapter { multipartResolver.setMaxUploadSize(5242880); return multipartResolver; } + + @Override + public void configureMessageConverters(List> converters) { + converters.add(new StringHttpMessageConverter()); + converters.add(new RssChannelHttpMessageConverter()); + converters.add(new JsonChannelHttpMessageConverter()); + + super.configureMessageConverters(converters); + } } diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/JadeTemplateConfiguration.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/JadeTemplateConfiguration.java index 10345bac58..4c95e5a0bd 100644 --- a/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/JadeTemplateConfiguration.java +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/JadeTemplateConfiguration.java @@ -16,24 +16,24 @@ import de.neuland.jade4j.spring.view.JadeViewResolver; public class JadeTemplateConfiguration { @Bean public SpringTemplateLoader templateLoader() { - SpringTemplateLoader templateLoader = new SpringTemplateLoader(); - templateLoader.setBasePath("/WEB-INF/views/"); - templateLoader.setSuffix(".jade"); - return templateLoader; + SpringTemplateLoader templateLoader = new SpringTemplateLoader(); + templateLoader.setBasePath("/WEB-INF/views/"); + templateLoader.setSuffix(".jade"); + return templateLoader; } @Bean public JadeConfiguration jadeConfiguration() { - JadeConfiguration configuration = new JadeConfiguration(); - configuration.setCaching(false); - configuration.setTemplateLoader(templateLoader()); - return configuration; + JadeConfiguration configuration = new JadeConfiguration(); + configuration.setCaching(false); + configuration.setTemplateLoader(templateLoader()); + return configuration; } @Bean public ViewResolver viewResolver() { - JadeViewResolver viewResolver = new JadeViewResolver(); - viewResolver.setConfiguration(jadeConfiguration()); - return viewResolver; + JadeViewResolver viewResolver = new JadeViewResolver(); + viewResolver.setConfiguration(jadeConfiguration()); + return viewResolver; } } diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/PushConfiguration.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/PushConfiguration.java new file mode 100644 index 0000000000..3072501cfa --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/PushConfiguration.java @@ -0,0 +1,30 @@ +package com.baeldung.spring.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.view.InternalResourceViewResolver; + +@Configuration +@EnableWebMvc +@ComponentScan(basePackages = "com.baeldung.spring.controller.push") +public class PushConfiguration implements WebMvcConfigurer { + + @Bean + public InternalResourceViewResolver jspViewResolver() { + InternalResourceViewResolver bean = new InternalResourceViewResolver(); + bean.setPrefix("/WEB-INF/views/"); + bean.setSuffix(".jsp"); + return bean; + } + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/resources/**") + .addResourceLocations("/resources/"); + } + +} diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/WebInitializer.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/WebInitializer.java index d57d2c621a..09030a8347 100644 --- a/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/WebInitializer.java +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/WebInitializer.java @@ -11,42 +11,23 @@ import javax.servlet.ServletRegistration; public class WebInitializer implements WebApplicationInitializer { - public void onStartup(ServletContext container) throws ServletException { + public void onStartup(ServletContext container) throws ServletException { - AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); - ctx.register(ApplicationConfiguration.class); - //ctx.register(ThymeleafConfiguration.class); - //ctx.register(FreemarkerConfiguration.class); - //ctx.register(GroovyConfiguration.class); - //ctx.register(JadeTemplateConfiguration.class); - ctx.setServletContext(container); + AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); + ctx.register(ApplicationConfiguration.class); + // ctx.register(ThymeleafConfiguration.class); + // ctx.register(FreemarkerConfiguration.class); + // ctx.register(GroovyConfiguration.class); + // ctx.register(JadeTemplateConfiguration.class); + // ctx.register(PushConfiguration.class); + // ctx.setServletContext(container); - // Manage the lifecycle of the root application context - container.addListener(new ContextLoaderListener(ctx)); + // Manage the lifecycle of the root application context + container.addListener(new ContextLoaderListener(ctx)); - ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx)); + ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx)); - servlet.setLoadOnStartup(1); - servlet.addMapping("/"); - - } -// @Override -// public void onStartup(ServletContext container) { -// // Create the 'root' Spring application context -// AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); -// rootContext.register(ServiceConfig.class, JPAConfig.class, SecurityConfig.class); -// -// // Manage the lifecycle of the root application context -// container.addListener(new ContextLoaderListener(rootContext)); -// -// // Create the dispatcher servlet's Spring application context -// AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext(); -// dispatcherServlet.register(MvcConfig.class); -// -// // Register and map the dispatcher servlet -// ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherServlet)); -// dispatcher.setLoadOnStartup(1); -// dispatcher.addMapping("/"); -// -// } + servlet.setLoadOnStartup(1); + servlet.addMapping("/"); + } } diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/AnnotationMethodHandlerAdapterExample.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/AnnotationMethodHandlerAdapterExample.java index 164830cd6a..89f5b2501b 100644 --- a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/AnnotationMethodHandlerAdapterExample.java +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/AnnotationMethodHandlerAdapterExample.java @@ -6,10 +6,10 @@ import org.springframework.web.servlet.ModelAndView; @Controller public class AnnotationMethodHandlerAdapterExample { - @RequestMapping("/annotedName") - public ModelAndView getEmployeeName() { - ModelAndView model = new ModelAndView("Greeting"); - model.addObject("message", "Dinesh"); - return model; - } + @RequestMapping("/annotedName") + public ModelAndView getEmployeeName() { + ModelAndView model = new ModelAndView("Greeting"); + model.addObject("message", "Dinesh"); + return model; + } } \ No newline at end of file diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/FileUploadController.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/FileUploadController.java index 47af2ab50d..2ca9e2b135 100644 --- a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/FileUploadController.java +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/FileUploadController.java @@ -23,13 +23,13 @@ public class FileUploadController implements HandlerExceptionResolver { } @RequestMapping(value = "/uploadFile", method = RequestMethod.POST) - public ModelAndView uploadFile(MultipartFile file) throws IOException{ + public ModelAndView uploadFile(MultipartFile file) throws IOException { ModelAndView modelAndView = new ModelAndView("file"); - - InputStream in = file.getInputStream(); + + InputStream in = file.getInputStream(); File currDir = new File("."); String path = currDir.getAbsolutePath(); - FileOutputStream f = new FileOutputStream(path.substring(0, path.length()-1)+ file.getOriginalFilename()); + FileOutputStream f = new FileOutputStream(path.substring(0, path.length() - 1) + file.getOriginalFilename()); int ch = 0; while ((ch = in.read()) != -1) { f.write(ch); @@ -37,15 +37,17 @@ public class FileUploadController implements HandlerExceptionResolver { f.flush(); f.close(); - modelAndView.getModel().put("message", "File uploaded successfully!"); + modelAndView.getModel() + .put("message", "File uploaded successfully!"); return modelAndView; } - + @Override - public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object object, Exception exc) { + public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object object, Exception exc) { ModelAndView modelAndView = new ModelAndView("file"); if (exc instanceof MaxUploadSizeExceededException) { - modelAndView.getModel().put("message", "File size exceeds limit!"); + modelAndView.getModel() + .put("message", "File size exceeds limit!"); } return modelAndView; } diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/RequestMappingHandlerAdapterExample.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/RequestMappingHandlerAdapterExample.java index 76ac3e2806..754bea79f1 100644 --- a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/RequestMappingHandlerAdapterExample.java +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/RequestMappingHandlerAdapterExample.java @@ -6,10 +6,10 @@ import org.springframework.web.servlet.ModelAndView; @Controller public class RequestMappingHandlerAdapterExample { - @RequestMapping("/requestName") - public ModelAndView getEmployeeName() { - ModelAndView model = new ModelAndView("Greeting"); - model.addObject("message", "Madhwal"); - return model; - } + @RequestMapping("/requestName") + public ModelAndView getEmployeeName() { + ModelAndView model = new ModelAndView("Greeting"); + model.addObject("message", "Madhwal"); + return model; + } } \ No newline at end of file diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/SimpleControllerHandlerAdapterExample.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/SimpleControllerHandlerAdapterExample.java index bac091ffeb..17c4ab689e 100644 --- a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/SimpleControllerHandlerAdapterExample.java +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/SimpleControllerHandlerAdapterExample.java @@ -6,13 +6,11 @@ import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; -public class SimpleControllerHandlerAdapterExample extends - AbstractController { - @Override - protected ModelAndView handleRequestInternal(HttpServletRequest request, - HttpServletResponse response) throws Exception { - ModelAndView model = new ModelAndView("Greeting"); - model.addObject("message", "Dinesh Madhwal"); - return model; - } +public class SimpleControllerHandlerAdapterExample extends AbstractController { + @Override + protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { + ModelAndView model = new ModelAndView("Greeting"); + model.addObject("message", "Dinesh Madhwal"); + return model; + } } \ No newline at end of file diff --git a/spring-mvc-push/src/main/java/com/baeldung/controller/PushController.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/push/PushController.java similarity index 92% rename from spring-mvc-push/src/main/java/com/baeldung/controller/PushController.java rename to spring-mvc-simple/src/main/java/com/baeldung/spring/controller/push/PushController.java index c4698fe976..b557c65c93 100644 --- a/spring-mvc-push/src/main/java/com/baeldung/controller/PushController.java +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/push/PushController.java @@ -1,4 +1,4 @@ -package com.baeldung.controller; +package com.baeldung.spring.controller.push; import javax.servlet.http.PushBuilder; diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/Article.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/Article.java new file mode 100644 index 0000000000..e6e83f95ff --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/Article.java @@ -0,0 +1,63 @@ +package com.baeldung.spring.controller.rss; + +import java.io.Serializable; +import java.util.Date; + +public class Article implements Serializable { + private String link; + private String title; + private String description; + private Date publishedDate; + private String author; + + public String getLink() { + return link; + } + + public void setLink(String link) { + this.link = link; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Date getPublishedDate() { + return publishedDate; + } + + public void setPublishedDate(Date publishedDate) { + this.publishedDate = publishedDate; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + @Override + public String toString() { + return "Article{" + + "link='" + link + '\'' + + ", title='" + title + '\'' + + ", description='" + description + '\'' + + ", publishedDate=" + publishedDate + + ", author='" + author + '\'' + + '}'; + } +} diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleFeedView.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleFeedView.java new file mode 100644 index 0000000000..cfbd33cd57 --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleFeedView.java @@ -0,0 +1,54 @@ +package com.baeldung.spring.controller.rss; + +import com.rometools.rome.feed.rss.Channel; +import com.rometools.rome.feed.rss.Description; +import com.rometools.rome.feed.rss.Item; +import org.springframework.stereotype.Service; +import org.springframework.web.servlet.view.feed.AbstractRssFeedView; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Map; + +@Service("articleFeedView") +public class ArticleFeedView extends AbstractRssFeedView { + + protected Channel newFeed() { + Channel channel = new Channel("rss_2.0"); + channel.setLink("http://localhost:8080/spring-mvc-simple/rss"); + channel.setTitle("Article Feed"); + channel.setDescription("Article Feed Description"); + channel.setPubDate(new Date()); + return channel; + } + + @Override + protected List buildFeedItems(Map map, HttpServletRequest httpStRequest, HttpServletResponse httpStResponse) throws Exception { + List list = new ArrayList(); + + Item item1 = new Item(); + item1.setLink("http://www.baeldung.com/netty-exception-handling"); + item1.setTitle("Exceptions in Netty"); + Description description1 = new Description(); + description1.setValue("In this quick article, we’ll be looking at exception handling in Netty."); + item1.setDescription(description1); + item1.setPubDate(new Date()); + item1.setAuthor("Carlos"); + + Item item2 = new Item(); + item2.setLink("http://www.baeldung.com/cockroachdb-java"); + item2.setTitle("Guide to CockroachDB in Java"); + Description description2 = new Description(); + description2.setValue("This tutorial is an introductory guide to using CockroachDB with Java."); + item2.setDescription(description2); + item2.setPubDate(new Date()); + item2.setAuthor("Baeldung"); + + list.add(item1); + list.add(item2); + return list; + } +} diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssController.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssController.java new file mode 100644 index 0000000000..23a6b8700e --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssController.java @@ -0,0 +1,71 @@ +package com.baeldung.spring.controller.rss; + +import com.rometools.rome.feed.rss.Channel; +import com.rometools.rome.feed.rss.Description; +import com.rometools.rome.feed.rss.Item; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +@Controller +public class ArticleRssController { + + @GetMapping(value = "/rss1") + public String articleMvcFeed() { + return "articleFeedView"; + } + + @GetMapping(value = "/rss2", produces = {"application/rss+xml", "application/rss+json"}) + @ResponseBody + public Channel articleHttpFeed() { + List
items = new ArrayList<>(); + Article item1 = new Article(); + item1.setLink("http://www.baeldung.com/netty-exception-handling"); + item1.setTitle("Exceptions in Netty"); + item1.setDescription("In this quick article, we’ll be looking at exception handling in Netty."); + item1.setPublishedDate(new Date()); + item1.setAuthor("Carlos"); + + Article item2 = new Article(); + item2.setLink("http://www.baeldung.com/cockroachdb-java"); + item2.setTitle("Guide to CockroachDB in Java"); + item2.setDescription("This tutorial is an introductory guide to using CockroachDB with Java."); + item2.setPublishedDate(new Date()); + item2.setAuthor("Baeldung"); + + items.add(item1); + items.add(item2); + Channel channelData = buildChannel(items); + + return channelData; + } + + private Channel buildChannel(List
articles){ + Channel channel = new Channel("rss_2.0"); + channel.setLink("http://localhost:8080/spring-mvc-simple/rss"); + channel.setTitle("Article Feed"); + channel.setDescription("Article Feed Description"); + channel.setPubDate(new Date()); + + List items = new ArrayList<>(); + for (Article article : articles) { + Item item = new Item(); + item.setLink(article.getLink()); + item.setTitle(article.getTitle()); + Description description1 = new Description(); + description1.setValue(article.getDescription()); + item.setDescription(description1); + item.setPubDate(article.getPublishedDate()); + item.setAuthor(article.getAuthor()); + items.add(item); + } + + channel.setItems(items); + return channel; + } + +} diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssFeedViewResolver.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssFeedViewResolver.java new file mode 100644 index 0000000000..6be06c4812 --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssFeedViewResolver.java @@ -0,0 +1,15 @@ +package com.baeldung.spring.controller.rss; + +import org.springframework.web.servlet.View; +import org.springframework.web.servlet.ViewResolver; + +import java.util.Locale; + +public class ArticleRssFeedViewResolver implements ViewResolver { + + @Override + public View resolveViewName(String s, Locale locale) throws Exception { + ArticleFeedView articleFeedView = new ArticleFeedView(); + return articleFeedView; + } +} diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/JsonChannelHttpMessageConverter.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/JsonChannelHttpMessageConverter.java new file mode 100644 index 0000000000..a32cbbdb7f --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/JsonChannelHttpMessageConverter.java @@ -0,0 +1,47 @@ +package com.baeldung.spring.controller.rss; + +import com.rometools.rome.feed.rss.Channel; +import com.rometools.rome.io.FeedException; +import com.rometools.rome.io.WireFeedOutput; +import org.json.JSONException; +import org.json.JSONObject; +import org.json.XML; +import org.springframework.http.HttpInputMessage; +import org.springframework.http.HttpOutputMessage; +import org.springframework.http.MediaType; +import org.springframework.http.converter.AbstractHttpMessageConverter; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.http.converter.HttpMessageNotWritableException; + +import java.io.IOException; + +public class JsonChannelHttpMessageConverter extends AbstractHttpMessageConverter { + public JsonChannelHttpMessageConverter(){ + super(new MediaType("application", "rss+json")); + } + + @Override + protected boolean supports(Class aClass) { + return Channel.class.isAssignableFrom(aClass); + } + + @Override + protected Channel readInternal(Class aClass, HttpInputMessage httpInputMessage) throws IOException, HttpMessageNotReadableException { + return null; + } + + @Override + protected void writeInternal(Channel wireFeed, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { + WireFeedOutput feedOutput = new WireFeedOutput(); + + try { + String xmlStr = feedOutput.outputString(wireFeed, true); + JSONObject xmlJSONObj = XML.toJSONObject(xmlStr); + String jsonPrettyPrintString = xmlJSONObj.toString(4); + + outputMessage.getBody().write(jsonPrettyPrintString.getBytes()); + } catch (JSONException | FeedException e) { + e.printStackTrace(); + } + } +} diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/scribe/GithubController.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/scribe/GithubController.java new file mode 100644 index 0000000000..61b91cbd4d --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/scribe/GithubController.java @@ -0,0 +1,58 @@ +package com.baeldung.spring.controller.scribe; + +import com.github.scribejava.apis.GitHubApi; +import com.github.scribejava.core.builder.ServiceBuilder; +import com.github.scribejava.core.model.*; +import com.github.scribejava.core.oauth.OAuth20Service; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.servlet.view.RedirectView; + +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; +import java.util.Random; +import java.util.concurrent.ExecutionException; + +@Controller +@RequestMapping("github") +public class GithubController { + private OAuth20Service createService(String state) { + return new ServiceBuilder("e1f8d4f1a5c71467a159") + .apiSecret("4851597541a8f33a4f1bf1c70f3cedcfefbeb13b") + .state(state) + .callback("http://localhost:8080/spring-mvc-simple/github/callback") + .build(GitHubApi.instance()); + } + + @GetMapping(value = "/authorization") + public RedirectView authorization(HttpServletRequest servletReq) throws InterruptedException, ExecutionException, IOException { + String state = String.valueOf(new Random().nextInt(999_999)); + OAuth20Service githubService = createService(state); + servletReq.getSession().setAttribute("state", state); + + String authorizationUrl = githubService.getAuthorizationUrl(); + RedirectView redirectView = new RedirectView(); + redirectView.setUrl(authorizationUrl); + return redirectView; + } + + @GetMapping(value = "/callback", produces = "text/plain") + @ResponseBody + public String callback(HttpServletRequest servletReq, @RequestParam("code") String code, @RequestParam("state") String state) throws InterruptedException, ExecutionException, IOException { + String initialState = (String) servletReq.getSession().getAttribute("state"); + if(initialState.equals(state)) { + OAuth20Service githubService = createService(initialState); + OAuth2AccessToken accessToken = githubService.getAccessToken(code); + + OAuthRequest request = new OAuthRequest(Verb.GET, "https://api.github.com/user"); + githubService.signRequest(accessToken, request); + Response response = githubService.execute(request); + + return response.getBody(); + } + return "Error"; + } +} diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/scribe/TwitterController.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/scribe/TwitterController.java new file mode 100644 index 0000000000..af47797ac5 --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/scribe/TwitterController.java @@ -0,0 +1,55 @@ +package com.baeldung.spring.controller.scribe; + +import com.github.scribejava.apis.TwitterApi; +import com.github.scribejava.core.builder.ServiceBuilder; +import com.github.scribejava.core.model.*; +import com.github.scribejava.core.oauth.OAuth10aService; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.servlet.view.RedirectView; + +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +@Controller +@RequestMapping("twitter") +public class TwitterController { + + private OAuth10aService createService() { + return new ServiceBuilder("PSRszoHhRDVhyo2RIkThEbWko") + .apiSecret("prpJbz03DcGRN46sb4ucdSYtVxG8unUKhcnu3an5ItXbEOuenL") + .callback("http://localhost:8080/spring-mvc-simple/twitter/callback") + .build(TwitterApi.instance()); + } + + @GetMapping(value = "/authorization") + public RedirectView authorization(HttpServletRequest servletReq) throws InterruptedException, ExecutionException, IOException { + OAuth10aService twitterService = createService(); + + OAuth1RequestToken requestToken = twitterService.getRequestToken(); + String authorizationUrl = twitterService.getAuthorizationUrl(requestToken); + servletReq.getSession().setAttribute("requestToken", requestToken); + + RedirectView redirectView = new RedirectView(); + redirectView.setUrl(authorizationUrl); + return redirectView; + } + + @GetMapping(value = "/callback", produces = "text/plain") + @ResponseBody + public String callback(HttpServletRequest servletReq, @RequestParam("oauth_verifier") String oauthV) throws InterruptedException, ExecutionException, IOException { + OAuth10aService twitterService = createService(); + OAuth1RequestToken requestToken = (OAuth1RequestToken) servletReq.getSession().getAttribute("requestToken"); + OAuth1AccessToken accessToken = twitterService.getAccessToken(requestToken, oauthV); + + OAuthRequest request = new OAuthRequest(Verb.GET, "https://api.twitter.com/1.1/account/verify_credentials.json"); + twitterService.signRequest(accessToken, request); + Response response = twitterService.execute(request); + + return response.getBody(); + } +} diff --git a/spring-mvc-simple/src/main/resources/spring-servlet_AnnotationMethodHandlerAdapter.xml b/spring-mvc-simple/src/main/resources/spring-servlet_AnnotationMethodHandlerAdapter.xml deleted file mode 100644 index 430b849012..0000000000 --- a/spring-mvc-simple/src/main/resources/spring-servlet_AnnotationMethodHandlerAdapter.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/spring-mvc-simple/src/main/resources/spring-servlet_RequestMappingHandlerAdapter.xml b/spring-mvc-simple/src/main/resources/spring-servlet_RequestMappingHandlerAdapter.xml deleted file mode 100644 index d3783c2e67..0000000000 --- a/spring-mvc-simple/src/main/resources/spring-servlet_RequestMappingHandlerAdapter.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/spring-mvc-simple/src/main/resources/spring-servlet_SimpleControllerHandlerAdapter.xml b/spring-mvc-simple/src/main/resources/spring-servlet_SimpleControllerHandlerAdapter.xml deleted file mode 100644 index 1d6e5628df..0000000000 --- a/spring-mvc-simple/src/main/resources/spring-servlet_SimpleControllerHandlerAdapter.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/spring-mvc-push/src/main/webapp/WEB-INF/views/demo.jsp b/spring-mvc-simple/src/main/webapp/WEB-INF/views/demo.jsp similarity index 87% rename from spring-mvc-push/src/main/webapp/WEB-INF/views/demo.jsp rename to spring-mvc-simple/src/main/webapp/WEB-INF/views/demo.jsp index 28b27322ae..d5579debf7 100644 --- a/spring-mvc-push/src/main/webapp/WEB-INF/views/demo.jsp +++ b/spring-mvc-simple/src/main/webapp/WEB-INF/views/demo.jsp @@ -12,7 +12,6 @@ " alt="Logo" height="126" width="411">
-
Go to - index + \ No newline at end of file diff --git a/spring-mvc-simple/src/main/webapp/WEB-INF/views/registration-jade.jade b/spring-mvc-simple/src/main/webapp/WEB-INF/views/registration-jade.jade index 44b6293ff0..f271ac6852 100644 --- a/spring-mvc-simple/src/main/webapp/WEB-INF/views/registration-jade.jade +++ b/spring-mvc-simple/src/main/webapp/WEB-INF/views/registration-jade.jade @@ -4,7 +4,7 @@ html title User Registration body form(action="register" method="post" ) - label(for="email") Emailaaaaaaaa: + label(for="email") Email: input(type="text" name="email") label(for="password") Password: input(type="password" name="password") diff --git a/spring-mvc-push/src/main/webapp/resources/logo.png b/spring-mvc-simple/src/main/webapp/resources/logo.png similarity index 100% rename from spring-mvc-push/src/main/webapp/resources/logo.png rename to spring-mvc-simple/src/main/webapp/resources/logo.png diff --git a/spring-mvc-push/src/test/java/com/baeldung/controller/PushControllerIntegrationTest.java b/spring-mvc-simple/src/test/java/com/baeldung/controller/push/PushControllerIntegrationTest.java similarity index 90% rename from spring-mvc-push/src/test/java/com/baeldung/controller/PushControllerIntegrationTest.java rename to spring-mvc-simple/src/test/java/com/baeldung/controller/push/PushControllerIntegrationTest.java index 570e05cad6..a03d02895f 100644 --- a/spring-mvc-push/src/test/java/com/baeldung/controller/PushControllerIntegrationTest.java +++ b/spring-mvc-simple/src/test/java/com/baeldung/controller/push/PushControllerIntegrationTest.java @@ -1,10 +1,9 @@ -package com.baeldung.controller; +package com.baeldung.controller.push; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; @@ -12,9 +11,8 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import com.baeldung.config.PushConfiguration; +import com.baeldung.spring.configuration.PushConfiguration; -@Disabled @SpringJUnitWebConfig(PushConfiguration.class) public class PushControllerIntegrationTest { @Autowired diff --git a/spring-mvc-simple/src/test/java/com/baeldung/controller/rss/ArticleRssIntegrationTest.java b/spring-mvc-simple/src/test/java/com/baeldung/controller/rss/ArticleRssIntegrationTest.java new file mode 100644 index 0000000000..fe7aeeb570 --- /dev/null +++ b/spring-mvc-simple/src/test/java/com/baeldung/controller/rss/ArticleRssIntegrationTest.java @@ -0,0 +1,45 @@ +package com.baeldung.controller.rss; + +import com.baeldung.spring.configuration.ApplicationConfiguration; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringJUnitWebConfig(ApplicationConfiguration.class) +public class ArticleRssIntegrationTest { + public static final String APPLICATION_RSS_XML = "application/rss+xml"; + public static final String APPLICATION_RSS_JSON = "application/rss+json"; + public static final String APPLICATION_RSS_XML_CHARSET_UTF_8 = "application/rss+xml;charset=UTF-8"; + + @Autowired + private WebApplicationContext webAppContext; + private MockMvc mockMvc; + + @BeforeEach + public void setup() { + mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext) + .build(); + } + + @Test + public void whenRequestingXMLFeed_thenContentTypeIsOk() throws Exception { + mockMvc.perform(get("/rss2").accept(APPLICATION_RSS_XML)) + .andExpect(status().isOk()) + .andExpect(content().contentType(APPLICATION_RSS_XML_CHARSET_UTF_8)); + } + + @Test + public void whenRequestingJSONFeed_thenContentTypeIsOk() throws Exception { + mockMvc.perform(get("/rss2").accept(APPLICATION_RSS_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType(APPLICATION_RSS_JSON)); + } +} \ No newline at end of file diff --git a/spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml b/spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml index 41422d8954..75d5c1ecd6 100644 --- a/spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml +++ b/spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml @@ -4,11 +4,11 @@ xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans - http://www.springframework.org/schema/beans/spring-beans-4.2.xsd + http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context - http://www.springframework.org/schema/context/spring-context-4.2.xsd + http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc - http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> + http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> diff --git a/spring-mvc-xml/src/main/resources/webMvcConfig.xml b/spring-mvc-xml/src/main/resources/webMvcConfig.xml index c471adf331..37aebe1d1d 100644 --- a/spring-mvc-xml/src/main/resources/webMvcConfig.xml +++ b/spring-mvc-xml/src/main/resources/webMvcConfig.xml @@ -3,11 +3,11 @@ xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans - http://www.springframework.org/schema/beans/spring-beans-4.2.xsd + http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context - http://www.springframework.org/schema/context/spring-context-4.2.xsd + http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc - http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd" + http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd" > diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/mvc-servlet.xml b/spring-mvc-xml/src/main/webapp/WEB-INF/mvc-servlet.xml index 47e2769902..032457da43 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/mvc-servlet.xml +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/mvc-servlet.xml @@ -2,9 +2,9 @@ + http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> @@ -58,4 +58,4 @@ - \ No newline at end of file + diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml index 1ea3051426..6ff435b84b 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml @@ -1,10 +1,9 @@ - - Spring MVC XML Application @@ -65,4 +64,4 @@ /errors - \ No newline at end of file + diff --git a/spring-reactive-websocket/pom.xml b/spring-reactive-websocket/pom.xml deleted file mode 100644 index 846cece177..0000000000 --- a/spring-reactive-websocket/pom.xml +++ /dev/null @@ -1,92 +0,0 @@ - - - 4.0.0 - - spring-reactive-websocket - 0.0.1-SNAPSHOT - jar - - spring-reactive-websocket - Reactive WebSockets with Spring 5 - - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - - - - UTF-8 - UTF-8 - 1.8 - - - - - org.springframework.boot - spring-boot-starter-integration - 2.0.0.M7 - - - org.springframework.boot - spring-boot-starter-webflux - 2.0.0.M7 - - - org.projectlombok - lombok - compile - RELEASE - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - - - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - - diff --git a/spring-reactive-websocket/src/main/java/com/baeldung/ReactiveWebSocketApplication.java b/spring-reactive-websocket/src/main/java/com/baeldung/ReactiveWebSocketApplication.java deleted file mode 100644 index f8952d750d..0000000000 --- a/spring-reactive-websocket/src/main/java/com/baeldung/ReactiveWebSocketApplication.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.baeldung; - -import java.net.URI; -import java.time.Duration; - -import org.springframework.boot.CommandLineRunner; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Bean; -import org.springframework.web.reactive.socket.WebSocketMessage; -import org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient; -import org.springframework.web.reactive.socket.client.WebSocketClient; - -import reactor.core.publisher.Mono; - -@SpringBootApplication -public class ReactiveWebSocketApplication { - public static void main(String[] args) { - SpringApplication.run(ReactiveWebSocketApplication.class, args); - } - - /** - * Spring Reactive WebSocket Client - * **/ - @Bean - CommandLineRunner runner() { - return run -> { - WebSocketClient client = new ReactorNettyWebSocketClient(); - client.execute(URI.create("ws://localhost:8080/event-emitter"), session -> session.send(Mono.just(session.textMessage("event-me-from-spring-reactive-client"))) - .thenMany(session.receive() - .map(WebSocketMessage::getPayloadAsText) - .log()) - .then()) - .block(); -// .block(Duration.ofSeconds(10L));//force timeout after given duration - }; - } -} diff --git a/spring-reactive-websocket/src/main/java/com/baeldung/ReactiveWebSocketHandler.java b/spring-reactive-websocket/src/main/java/com/baeldung/ReactiveWebSocketHandler.java deleted file mode 100644 index 4a548322b3..0000000000 --- a/spring-reactive-websocket/src/main/java/com/baeldung/ReactiveWebSocketHandler.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.baeldung; - -import org.springframework.web.reactive.socket.WebSocketSession; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; - -import org.springframework.stereotype.Component; -import org.springframework.web.reactive.socket.WebSocketHandler; -import org.springframework.web.reactive.socket.WebSocketMessage; - -import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; - -import javax.annotation.PostConstruct; -import java.time.Duration; -import java.time.LocalDateTime; -import java.util.UUID; - -@Component -public class ReactiveWebSocketHandler implements WebSocketHandler { - - private Flux eventFlux; - private Flux intervalFlux; - - /** - * Here we prepare a Flux that will emit a message every second - */ - @PostConstruct - private void init() throws InterruptedException { - - eventFlux = Flux.generate(e -> { - Event event = new Event(UUID.randomUUID() - .toString(), - LocalDateTime.now() - .toString()); - e.next(event); - }); - - intervalFlux = Flux.interval(Duration.ofMillis(1000L)) - .zipWith(eventFlux, (time, event) -> event); - - } - - /** - * On each new client session, send the message flux to the client. - * Spring subscribes to the flux and send every new flux event to the WebSocketSession object - * @param session - * @return Mono - */ - @Override - public Mono handle(WebSocketSession webSocketSession) { - ObjectMapper json = new ObjectMapper(); - return webSocketSession.send(intervalFlux.map(event -> { - try { - String jsonEvent = json.writeValueAsString(event); - System.out.println(jsonEvent); - return jsonEvent; - } catch (JsonProcessingException e) { - e.printStackTrace(); - return ""; - } - }) - .map(webSocketSession::textMessage)) - - .and(webSocketSession.receive() - .map(WebSocketMessage::getPayloadAsText) - .log()); - } - -} diff --git a/spring-rest-embedded-tomcat/pom.xml b/spring-rest-embedded-tomcat/pom.xml index cee9933c0d..3622e84101 100644 --- a/spring-rest-embedded-tomcat/pom.xml +++ b/spring-rest-embedded-tomcat/pom.xml @@ -84,14 +84,13 @@ **/JdbcTest.java **/*LiveTest.java - - 5.0.1.RELEASE + 5.0.2.RELEASE 2.19.1 4.12 2.9.2 diff --git a/spring-rest-simple/README.md b/spring-rest-simple/README.md index 916676feb5..982e16d5a5 100644 --- a/spring-rest-simple/README.md +++ b/spring-rest-simple/README.md @@ -5,3 +5,4 @@ - [The Guide to RestTemplate](http://www.baeldung.com/rest-template) - [Spring RequestMapping](http://www.baeldung.com/spring-requestmapping) - [ETags for REST with Spring](http://www.baeldung.com/etags-for-rest-with-spring) +- [Spring and Apache FileUpload](http://www.baeldung.com/spring-apache-file-upload) diff --git a/spring-rest/pom.xml b/spring-rest/pom.xml index 6da891b054..50ac00e1c3 100644 --- a/spring-rest/pom.xml +++ b/spring-rest/pom.xml @@ -169,6 +169,12 @@ commons-io 2.4 + + + au.com.dius + pact-jvm-provider-junit_2.11 + ${pact.version} + @@ -324,6 +330,8 @@ 3.4.1 2.2.0 + + 3.5.11 diff --git a/spring-rest/src/main/java/org/baeldung/web/controller/PactController.java b/spring-rest/src/main/java/org/baeldung/web/controller/PactController.java new file mode 100644 index 0000000000..4f586479ef --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/web/controller/PactController.java @@ -0,0 +1,32 @@ +package org.baeldung.web.controller; + +import java.util.ArrayList; +import java.util.List; + +import org.baeldung.web.dto.PactDto; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class PactController { + + List pacts = new ArrayList<>(); + + @GetMapping(value = "/pact", produces = MediaType.APPLICATION_JSON_VALUE) + @ResponseBody + public PactDto getPact() { + return new PactDto(true, "tom"); + } + + @PostMapping("/pact") + @ResponseStatus(HttpStatus.CREATED) + public void createPact(PactDto pact) { + pacts.add(pact); + } + +} diff --git a/spring-rest/src/main/java/org/baeldung/web/dto/PactDto.java b/spring-rest/src/main/java/org/baeldung/web/dto/PactDto.java new file mode 100644 index 0000000000..e34e2bdf3b --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/web/dto/PactDto.java @@ -0,0 +1,33 @@ +package org.baeldung.web.dto; + +public class PactDto { + + private boolean condition; + private String name; + + public PactDto() { + } + + public PactDto(boolean condition, String name) { + super(); + this.condition = condition; + this.name = name; + } + + public boolean isCondition() { + return condition; + } + + public void setCondition(boolean condition) { + this.condition = condition; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/spring-rest/src/main/resources/pacts/test_consumer-test_provider.json b/spring-rest/src/main/resources/pacts/test_consumer-test_provider.json new file mode 100644 index 0000000000..c8082798f4 --- /dev/null +++ b/spring-rest/src/main/resources/pacts/test_consumer-test_provider.json @@ -0,0 +1,61 @@ +{ + "provider": { + "name": "test_provider" + }, + "consumer": { + "name": "test_consumer" + }, + "interactions": [ + { + "description": "GET REQUEST", + "request": { + "method": "GET", + "path": "/pact" + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "application/json" + }, + "body": { + "condition": true, + "name": "tom" + } + }, + "providerStates": [ + { + "name": "test GET" + } + ] + }, + { + "description": "POST REQUEST", + "request": { + "method": "POST", + "path": "/pact", + "headers": { + "Content-Type": "application/json" + }, + "body": { + "name": "Michael" + } + }, + "response": { + "status": 201 + }, + "providerStates": [ + { + "name": "test POST" + } + ] + } + ], + "metadata": { + "pact-specification": { + "version": "3.0.0" + }, + "pact-jvm": { + "version": "3.5.0" + } + } +} \ No newline at end of file diff --git a/spring-rest/src/test/java/org/baeldung/pact/PactProviderTest.java b/spring-rest/src/test/java/org/baeldung/pact/PactProviderTest.java new file mode 100644 index 0000000000..0456b0d3e7 --- /dev/null +++ b/spring-rest/src/test/java/org/baeldung/pact/PactProviderTest.java @@ -0,0 +1,40 @@ +package org.baeldung.pact; + +import org.baeldung.config.MainApplication; +import org.junit.BeforeClass; +import org.junit.runner.RunWith; +import org.springframework.boot.SpringApplication; +import org.springframework.web.context.ConfigurableWebApplicationContext; + +import au.com.dius.pact.provider.junit.PactRunner; +import au.com.dius.pact.provider.junit.Provider; +import au.com.dius.pact.provider.junit.State; +import au.com.dius.pact.provider.junit.loader.PactFolder; +import au.com.dius.pact.provider.junit.target.HttpTarget; +import au.com.dius.pact.provider.junit.target.Target; +import au.com.dius.pact.provider.junit.target.TestTarget; + +@RunWith(PactRunner.class) +@Provider("test_provider") +@PactFolder("pacts") +public class PactProviderTest { + + @TestTarget + public final Target target = new HttpTarget("http", "localhost", 8082, "/spring-rest"); + + private static ConfigurableWebApplicationContext application; + + @BeforeClass + public static void start() { + application = (ConfigurableWebApplicationContext) SpringApplication.run(MainApplication.class); + } + + @State("test GET") + public void toGetState() { + } + + @State("test POST") + public void toPostState() { + } + +} diff --git a/spring-security-core/README.md b/spring-security-core/README.md index 3675e7e160..f7b3f6dffe 100644 --- a/spring-security-core/README.md +++ b/spring-security-core/README.md @@ -9,3 +9,4 @@ mvn clean install ### Relevant Articles: - [Intro to @PreFilter and @PostFilter in Spring Security](http://www.baeldung.com/spring-security-prefilter-postfilter) - [Spring Boot Authentication Auditing Support](http://www.baeldung.com/spring-boot-authentication-audit) +- [Introduction to Spring Method Security](http://www.baeldung.com/spring-security-method-security) diff --git a/spring-security-mvc-custom/README.md b/spring-security-mvc-custom/README.md index 2c0be4768e..ca0731a0c3 100644 --- a/spring-security-mvc-custom/README.md +++ b/spring-security-mvc-custom/README.md @@ -12,6 +12,7 @@ The "REST With Spring" Classes: http://github.learnspringsecurity.com - [Introduction to Spring MVC HandlerInterceptor](http://www.baeldung.com/spring-mvc-handlerinterceptor) - [Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions](http://www.baeldung.com/spring-mvc-custom-handler-interceptor) - [A Guide to CSRF Protection in Spring Security](http://www.baeldung.com/spring-security-csrf) +- [How to Manually Authenticate User with Spring Security](http://www.baeldung.com/manually-set-user-authentication-spring-security) ### Build the Project ``` diff --git a/spring-security-mvc-custom/src/main/java/org/baeldung/web/controller/LoginController.java b/spring-security-mvc-custom/src/main/java/org/baeldung/web/controller/LoginController.java new file mode 100644 index 0000000000..c67a6f667e --- /dev/null +++ b/spring-security-mvc-custom/src/main/java/org/baeldung/web/controller/LoginController.java @@ -0,0 +1,43 @@ +package org.baeldung.web.controller; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; + +@Controller +@RequestMapping(value = "/custom") +public class LoginController { + + @Autowired + private AuthenticationManager authManager; + + public LoginController() { + super(); + } + + // API + + // custom login + + @RequestMapping(value = "/login", method = RequestMethod.POST) + public void login(@RequestParam("username") final String username, @RequestParam("password") final String password, final HttpServletRequest request) { + UsernamePasswordAuthenticationToken authReq = + new UsernamePasswordAuthenticationToken(username, password); + Authentication auth = authManager.authenticate(authReq); + SecurityContext sc = SecurityContextHolder.getContext(); + sc.setAuthentication(auth); + HttpSession session = request.getSession(true); + session.setAttribute("SPRING_SECURITY_CONTEXT", sc); + } + +} \ No newline at end of file diff --git a/spring-security-mvc-custom/src/main/java/org/baeldung/web/controller/PrintUserController.java b/spring-security-mvc-custom/src/main/java/org/baeldung/web/controller/PrintUserController.java new file mode 100644 index 0000000000..78f164c7f1 --- /dev/null +++ b/spring-security-mvc-custom/src/main/java/org/baeldung/web/controller/PrintUserController.java @@ -0,0 +1,27 @@ +package org.baeldung.web.controller; + +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +@Controller +@RequestMapping(value = "/custom") +public class PrintUserController { + + public PrintUserController() { + super(); + } + + // API + + // print user + + @RequestMapping(value = "/print", method = RequestMethod.GET) + public void printUser() { + SecurityContext sc = SecurityContextHolder.getContext(); + System.out.println("Logged User: "+sc.getAuthentication().getName()); + } + +} \ No newline at end of file diff --git a/spring-security-mvc-custom/src/test/java/org/baeldung/security/spring/ManualSecurityConfig.java b/spring-security-mvc-custom/src/test/java/org/baeldung/security/spring/ManualSecurityConfig.java new file mode 100644 index 0000000000..874856095c --- /dev/null +++ b/spring-security-mvc-custom/src/test/java/org/baeldung/security/spring/ManualSecurityConfig.java @@ -0,0 +1,58 @@ +package org.baeldung.security.spring; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.config.BeanIds; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.builders.WebSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +@Configuration +@EnableWebSecurity +@EnableGlobalMethodSecurity(prePostEnabled = true) +public class ManualSecurityConfig extends WebSecurityConfigurerAdapter { + + public ManualSecurityConfig() { + super(); + } + + // java config + + @Override + protected void configure(final AuthenticationManagerBuilder auth) throws Exception { + auth.inMemoryAuthentication().withUser("user1").password("user1Pass").authorities("ROLE_USER").and().withUser("admin").password("adminPass").authorities("ROLE_ADMIN"); + } + + @Override + public void configure(final WebSecurity web) throws Exception { + web.ignoring().antMatchers("/resources/**"); + } + + @Bean(name = BeanIds.AUTHENTICATION_MANAGER) + @Override + public AuthenticationManager authenticationManagerBean() throws Exception { + return super.authenticationManagerBean(); + } + + @Override + protected void configure(final HttpSecurity http) throws Exception { + // @formatter:off + http + .authorizeRequests() + .mvcMatchers("/custom/login").permitAll() + .anyRequest().authenticated() + .and() + .httpBasic() + .and() + .headers().cacheControl().disable() + .and() + .csrf().disable() + ; + // @formatter:on + } + +} diff --git a/spring-security-mvc-custom/src/test/java/org/baeldung/security/spring/ManualSecurityIntegrationTest.java b/spring-security-mvc-custom/src/test/java/org/baeldung/security/spring/ManualSecurityIntegrationTest.java new file mode 100644 index 0000000000..afc86bd74c --- /dev/null +++ b/spring-security-mvc-custom/src/test/java/org/baeldung/security/spring/ManualSecurityIntegrationTest.java @@ -0,0 +1,58 @@ +package org.baeldung.security.spring; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import javax.servlet.http.HttpSession; + +import org.baeldung.spring.MvcConfig; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mock.web.MockHttpSession; +import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration(classes = { MvcConfig.class, ManualSecurityConfig.class }) +public class ManualSecurityIntegrationTest { + + @Autowired + WebApplicationContext wac; + + private MockMvc mockMvc; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + mockMvc = MockMvcBuilders.webAppContextSetup(wac).apply(SecurityMockMvcConfigurers.springSecurity()).build(); + } + + /** + * Execute custom login and access the endpoint + */ + @Test + public void whenLoginIsSuccessFulThenEndpointCanBeAccessedAndCurrentUserPrinted() throws Exception { + + mockMvc.perform(get("/custom/print")) + .andExpect(status().isUnauthorized()); + + HttpSession session = mockMvc.perform(post("/custom/login").param("username", "user1").param("password", "user1Pass")) + .andExpect(status().isOk()) + .andReturn() + .getRequest() + .getSession(); + + mockMvc.perform(get("/custom/print").session((MockHttpSession) session)) + .andExpect(status().is2xxSuccessful()); + } + +} diff --git a/spring-security-openid/pom.xml b/spring-security-openid/pom.xml index 2149334a5c..aeabb161c9 100644 --- a/spring-security-openid/pom.xml +++ b/spring-security-openid/pom.xml @@ -11,12 +11,12 @@ spring-security-openid Spring OpenID sample project - - parent-boot-5 - com.baeldung - 0.0.1-SNAPSHOT - ../parent-boot-5 - + + org.springframework.boot + spring-boot-starter-parent + 2.0.0.M7 + + @@ -37,13 +37,41 @@ org.springframework.security.oauth spring-security-oauth2 + 2.2.1.RELEASE org.springframework.security spring-security-jwt + 1.0.9.RELEASE + + + + com.auth0 + jwks-rsa + 0.3.0 - + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + diff --git a/spring-security-openid/src/main/java/org/baeldung/config/SpringOpenidApplication.java b/spring-security-openid/src/main/java/org/baeldung/config/SpringOpenidApplication.java index ed57088c56..1acdba0623 100644 --- a/spring-security-openid/src/main/java/org/baeldung/config/SpringOpenidApplication.java +++ b/spring-security-openid/src/main/java/org/baeldung/config/SpringOpenidApplication.java @@ -2,7 +2,7 @@ package org.baeldung.config; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.web.support.SpringBootServletInitializer; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; @SpringBootApplication public class SpringOpenidApplication extends SpringBootServletInitializer { diff --git a/spring-security-openid/src/main/java/org/baeldung/security/OpenIdConnectFilter.java b/spring-security-openid/src/main/java/org/baeldung/security/OpenIdConnectFilter.java index c0970ab3cf..f12169cb27 100644 --- a/spring-security-openid/src/main/java/org/baeldung/security/OpenIdConnectFilter.java +++ b/spring-security-openid/src/main/java/org/baeldung/security/OpenIdConnectFilter.java @@ -1,12 +1,16 @@ package org.baeldung.security; import java.io.IOException; +import java.net.URL; +import java.security.interfaces.RSAPublicKey; +import java.util.Date; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.springframework.beans.factory.annotation.Value; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; @@ -14,16 +18,28 @@ import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.jwt.Jwt; import org.springframework.security.jwt.JwtHelper; +import org.springframework.security.jwt.crypto.sign.RsaVerifier; import org.springframework.security.oauth2.client.OAuth2RestOperations; import org.springframework.security.oauth2.client.OAuth2RestTemplate; import org.springframework.security.oauth2.common.OAuth2AccessToken; -import org.springframework.security.oauth2.common.exceptions.InvalidTokenException; import org.springframework.security.oauth2.common.exceptions.OAuth2Exception; import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter; +import com.auth0.jwk.Jwk; +import com.auth0.jwk.JwkProvider; +import com.auth0.jwk.UrlJwkProvider; import com.fasterxml.jackson.databind.ObjectMapper; public class OpenIdConnectFilter extends AbstractAuthenticationProcessingFilter { + @Value("${google.clientId}") + private String clientId; + + @Value("${google.issuer}") + private String issuer; + + @Value("${google.jwkUrl}") + private String jwkUrl; + public OAuth2RestOperations restTemplate; public OpenIdConnectFilter(String defaultFilterProcessesUrl) { @@ -42,19 +58,35 @@ public class OpenIdConnectFilter extends AbstractAuthenticationProcessingFilter } try { final String idToken = accessToken.getAdditionalInformation().get("id_token").toString(); - final Jwt tokenDecoded = JwtHelper.decode(idToken); - System.out.println("===== : " + tokenDecoded.getClaims()); - + String kid = JwtHelper.headers(idToken) + .get("kid"); + final Jwt tokenDecoded = JwtHelper.decodeAndVerify(idToken, verifier(kid)); final Map authInfo = new ObjectMapper().readValue(tokenDecoded.getClaims(), Map.class); - + verifyClaims(authInfo); final OpenIdConnectUserDetails user = new OpenIdConnectUserDetails(authInfo, accessToken); return new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities()); - } catch (final InvalidTokenException e) { + } catch (final Exception e) { throw new BadCredentialsException("Could not obtain user details from token", e); } } + public void verifyClaims(Map claims) { + int exp = (int) claims.get("exp"); + Date expireDate = new Date(exp * 1000L); + Date now = new Date(); + if (expireDate.before(now) || !claims.get("iss").equals(issuer) || !claims.get("aud").equals(clientId)) { + throw new RuntimeException("Invalid claims"); + } + } + + + private RsaVerifier verifier(String kid) throws Exception { + JwkProvider provider = new UrlJwkProvider(new URL(jwkUrl)); + Jwk jwk = provider.get(kid); + return new RsaVerifier((RSAPublicKey) jwk.getPublicKey()); + } + public void setRestTemplate(OAuth2RestTemplate restTemplate2) { restTemplate = restTemplate2; diff --git a/spring-security-openid/src/main/resources/application.properties.sample.properties b/spring-security-openid/src/main/resources/application.properties.sample.properties index 3b4f7716f0..49022bf280 100644 --- a/spring-security-openid/src/main/resources/application.properties.sample.properties +++ b/spring-security-openid/src/main/resources/application.properties.sample.properties @@ -3,4 +3,6 @@ google.clientId=TODO google.clientSecret=TODO google.accessTokenUri=https://www.googleapis.com/oauth2/v3/token google.userAuthorizationUri=https://accounts.google.com/o/oauth2/auth -google.redirectUri=http://localhost:8081/google-login \ No newline at end of file +google.redirectUri=http://localhost:8081/google-login +google.issuer=accounts.google.com +google.jwkUrl=https://www.googleapis.com/oauth2/v2/certs \ No newline at end of file diff --git a/spring-swagger-codegen/spring-swagger-codegen-api-client/src/main/gen/com/baeldung/petstore/client/invoker/BuildConfig.java b/spring-swagger-codegen/spring-swagger-codegen-api-client/src/main/gen/com/baeldung/petstore/client/invoker/BuildConfig.java new file mode 100644 index 0000000000..85233bc278 --- /dev/null +++ b/spring-swagger-codegen/spring-swagger-codegen-api-client/src/main/gen/com/baeldung/petstore/client/invoker/BuildConfig.java @@ -0,0 +1,8 @@ +/*___Generated_by_IDEA___*/ + +package com.baeldung.petstore.client.invoker; + +/* This stub is only used by the IDE. It is NOT the BuildConfig class actually packed into the APK */ +public final class BuildConfig { + public final static boolean DEBUG = Boolean.parseBoolean(null); +} \ No newline at end of file diff --git a/spring-swagger-codegen/spring-swagger-codegen-api-client/src/main/gen/com/baeldung/petstore/client/invoker/Manifest.java b/spring-swagger-codegen/spring-swagger-codegen-api-client/src/main/gen/com/baeldung/petstore/client/invoker/Manifest.java new file mode 100644 index 0000000000..06c202e733 --- /dev/null +++ b/spring-swagger-codegen/spring-swagger-codegen-api-client/src/main/gen/com/baeldung/petstore/client/invoker/Manifest.java @@ -0,0 +1,7 @@ +/*___Generated_by_IDEA___*/ + +package com.baeldung.petstore.client.invoker; + +/* This stub is only used by the IDE. It is NOT the Manifest class actually packed into the APK */ +public final class Manifest { +} \ No newline at end of file diff --git a/spring-swagger-codegen/spring-swagger-codegen-api-client/src/main/gen/com/baeldung/petstore/client/invoker/R.java b/spring-swagger-codegen/spring-swagger-codegen-api-client/src/main/gen/com/baeldung/petstore/client/invoker/R.java new file mode 100644 index 0000000000..41e2b545fb --- /dev/null +++ b/spring-swagger-codegen/spring-swagger-codegen-api-client/src/main/gen/com/baeldung/petstore/client/invoker/R.java @@ -0,0 +1,7 @@ +/*___Generated_by_IDEA___*/ + +package com.baeldung.petstore.client.invoker; + +/* This stub is only used by the IDE. It is NOT the R class actually packed into the APK */ +public final class R { +} \ No newline at end of file diff --git a/spring-swagger-codegen/spring-swagger-codegen-app/pom.xml b/spring-swagger-codegen/spring-swagger-codegen-app/pom.xml index f42fc5c2e2..b7b898237f 100644 --- a/spring-swagger-codegen/spring-swagger-codegen-app/pom.xml +++ b/spring-swagger-codegen/spring-swagger-codegen-app/pom.xml @@ -9,7 +9,7 @@ org.springframework.boot spring-boot-starter-parent - 1.5.6.RELEASE + 1.5.10.RELEASE diff --git a/testing-modules/README.md b/testing-modules/README.md index 3fbeea6188..ccf63a9815 100644 --- a/testing-modules/README.md +++ b/testing-modules/README.md @@ -1,3 +1,6 @@ ## Testing Modules +### Relevant Articles: + +- [Quick Guide to BDDMockito](http://www.baeldung.com/bdd-mockito) diff --git a/testing-modules/junit-5/pom.xml b/testing-modules/junit-5/pom.xml index 585232b3fe..28a29d8545 100644 --- a/testing-modules/junit-5/pom.xml +++ b/testing-modules/junit-5/pom.xml @@ -20,7 +20,7 @@ UTF-8 1.8 - 5.0.2 + 5.1.0 1.0.1 4.12.1 2.8.2 diff --git a/testing-modules/mockito/pom.xml b/testing-modules/mockito/pom.xml index 0e83c46926..cdd73e6efe 100644 --- a/testing-modules/mockito/pom.xml +++ b/testing-modules/mockito/pom.xml @@ -47,6 +47,13 @@ test + + org.hamcrest + java-hamcrest + ${hamcrest.version} + test + + @@ -67,6 +74,7 @@ 1.7.0 + 2.0.0.0 diff --git a/testing-modules/mockito/src/test/java/org/baeldung/hamcrest/HamcrestFileUnitTest.java b/testing-modules/mockito/src/test/java/org/baeldung/hamcrest/HamcrestFileUnitTest.java new file mode 100644 index 0000000000..652e315651 --- /dev/null +++ b/testing-modules/mockito/src/test/java/org/baeldung/hamcrest/HamcrestFileUnitTest.java @@ -0,0 +1,64 @@ +package org.baeldung.hamcrest; + +import static org.hamcrest.core.StringContains.containsString; +import static org.hamcrest.io.FileMatchers.aFileNamed; +import static org.hamcrest.io.FileMatchers.aFileWithAbsolutePath; +import static org.hamcrest.io.FileMatchers.aFileWithCanonicalPath; +import static org.hamcrest.io.FileMatchers.aFileWithSize; +import static org.hamcrest.io.FileMatchers.aReadableFile; +import static org.hamcrest.io.FileMatchers.aWritableFile; +import static org.hamcrest.io.FileMatchers.anExistingDirectory; +import static org.hamcrest.io.FileMatchers.anExistingFile; +import static org.hamcrest.io.FileMatchers.anExistingFileOrDirectory; +import static org.hamcrest.number.OrderingComparison.greaterThan; +import static org.hamcrest.text.IsEqualIgnoringCase.equalToIgnoringCase; +import static org.junit.Assert.assertThat; + +import java.io.File; + +import org.junit.Test; + +public class HamcrestFileUnitTest { + + @Test + public final void whenVerifyingFileName_thenCorrect() { + File file = new File("src/test/resources/test1.in"); + + assertThat(file, aFileNamed(equalToIgnoringCase("test1.in"))); + } + + @Test + public final void whenVerifyingFileOrDirExist_thenCorrect() { + File file = new File("src/test/resources/test1.in"); + File dir = new File("src/test/resources"); + + assertThat(file, anExistingFile()); + assertThat(dir, anExistingDirectory()); + assertThat(file, anExistingFileOrDirectory()); + assertThat(dir, anExistingFileOrDirectory()); + } + + @Test + public final void whenVerifyingFileIsReadableAndWritable_thenCorrect() { + File file = new File("src/test/resources/test1.in"); + + assertThat(file, aReadableFile()); + assertThat(file, aWritableFile()); + } + + @Test + public final void whenVerifyingFileSize_thenCorrect() { + File file = new File("src/test/resources/test1.in"); + + assertThat(file, aFileWithSize(11)); + assertThat(file, aFileWithSize(greaterThan(1L)));; + } + + @Test + public final void whenVerifyingFilePath_thenCorrect() { + File file = new File("src/test/resources/test1.in"); + + assertThat(file, aFileWithCanonicalPath(containsString("src/test/resources"))); + assertThat(file, aFileWithAbsolutePath(containsString("src/test/resources"))); + } +} diff --git a/testing-modules/mockito/src/test/java/org/baeldung/hamcrest/HamcrestNumberUnitTest.java b/testing-modules/mockito/src/test/java/org/baeldung/hamcrest/HamcrestNumberUnitTest.java new file mode 100644 index 0000000000..fbba6b94d2 --- /dev/null +++ b/testing-modules/mockito/src/test/java/org/baeldung/hamcrest/HamcrestNumberUnitTest.java @@ -0,0 +1,178 @@ +package org.baeldung.hamcrest; + +import org.junit.Test; + +import java.math.BigDecimal; +import java.time.LocalDate; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.closeTo; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.greaterThanOrEqualTo; +import static org.hamcrest.Matchers.lessThan; +import static org.hamcrest.Matchers.lessThanOrEqualTo; +import static org.hamcrest.Matchers.comparesEqualTo; +import static org.hamcrest.Matchers.notANumber; + +public class HamcrestNumberUnitTest { + + @Test + public void givenADouble_whenCloseTo_thenCorrect() { + double actual = 1.3; + double operand = 1; + double error = 0.5; + assertThat(actual, is(closeTo(operand, error))); + } + + @Test + public void givenADouble_whenNotCloseTo_thenCorrect() { + double actual = 1.6; + double operand = 1; + double error = 0.5; + assertThat(actual, is(not(closeTo(operand, error)))); + } + + @Test + public void givenABigDecimal_whenCloseTo_thenCorrect() { + BigDecimal actual = new BigDecimal("1.0003"); + BigDecimal operand = new BigDecimal("1"); + BigDecimal error = new BigDecimal("0.0005"); + assertThat(actual, is(closeTo(operand, error))); + } + + @Test + public void givenABigDecimal_whenNotCloseTo_thenCorrect() { + BigDecimal actual = new BigDecimal("1.0006"); + BigDecimal operand = new BigDecimal("1"); + BigDecimal error = new BigDecimal("0.0005"); + assertThat(actual, is(not(closeTo(operand, error)))); + } + + @Test + public void given5_whenComparesEqualTo5_thenCorrect() { + Integer five = 5; + assertThat(five, comparesEqualTo(five)); + } + + @Test + public void given5_whenNotComparesEqualTo7_thenCorrect() { + Integer seven = 7; + Integer five = 5; + assertThat(five, not(comparesEqualTo(seven))); + } + + @Test + public void given7_whenGreaterThan5_thenCorrect() { + Integer seven = 7; + Integer five = 5; + assertThat(seven, is(greaterThan(five))); + } + + @Test + public void given7_whenGreaterThanOrEqualTo5_thenCorrect() { + Integer seven = 7; + Integer five = 5; + assertThat(seven, is(greaterThanOrEqualTo(five))); + } + + @Test + public void given5_whenGreaterThanOrEqualTo5_thenCorrect() { + Integer five = 5; + assertThat(five, is(greaterThanOrEqualTo(five))); + } + + @Test + public void given3_whenLessThan5_thenCorrect() { + Integer three = 3; + Integer five = 5; + assertThat(three, is(lessThan(five))); + } + + @Test + public void given3_whenLessThanOrEqualTo5_thenCorrect() { + Integer three = 3; + Integer five = 5; + assertThat(three, is(lessThanOrEqualTo(five))); + } + + @Test + public void given5_whenLessThanOrEqualTo5_thenCorrect() { + Integer five = 5; + assertThat(five, is(lessThanOrEqualTo(five))); + } + + @Test + public void givenBenjamin_whenGreaterThanAmanda_thenCorrect() { + String amanda = "Amanda"; + String benjamin = "Benjamin"; + assertThat(benjamin, is(greaterThan(amanda))); + } + + @Test + public void givenAmanda_whenLessThanBenajmin_thenCorrect() { + String amanda = "Amanda"; + String benjamin = "Benjamin"; + assertThat(amanda, is(lessThan(benjamin))); + } + + @Test + public void givenToday_whenGreaterThanYesterday_thenCorrect() { + LocalDate today = LocalDate.now(); + LocalDate yesterday = today.minusDays(1); + assertThat(today, is(greaterThan(yesterday))); + } + + @Test + public void givenToday_whenLessThanTomorrow_thenCorrect() { + LocalDate today = LocalDate.now(); + LocalDate tomorrow = today.plusDays(1); + assertThat(today, is(lessThan(tomorrow))); + } + + @Test + public void givenAmanda_whenOlderThanBenjamin_thenCorrect() { + Person amanda = new Person("Amanda", 20); + Person benjamin = new Person("Benjamin", 18); + assertThat(amanda, is(greaterThan(benjamin))); + } + + @Test + public void givenBenjamin_whenYoungerThanAmanda_thenCorrect() { + Person amanda = new Person("Amanda", 20); + Person benjamin = new Person("Benjamin", 18); + assertThat(benjamin, is(lessThan(amanda))); + } + + class Person implements Comparable { + String name; + int age; + + public Person(String name, int age) { + this.name = name; + this.age = age; + } + + public String getName() { + return name; + } + + public int getAge() { + return age; + } + + @Override + public int compareTo(Person o) { + if (this.age == o.getAge()) return 0; + if (this.age > o.age) return 1; + else return -1; + } + } + + @Test + public void givenNaN_whenIsNotANumber_thenCorrect() { + double zero = 0d; + assertThat(zero / zero, is(notANumber())); + } +} diff --git a/testing-modules/mockito/src/test/java/org/baeldung/hamcrest/HamcrestTextUnitTest.java b/testing-modules/mockito/src/test/java/org/baeldung/hamcrest/HamcrestTextUnitTest.java new file mode 100644 index 0000000000..e336b1bba3 --- /dev/null +++ b/testing-modules/mockito/src/test/java/org/baeldung/hamcrest/HamcrestTextUnitTest.java @@ -0,0 +1,97 @@ +package org.baeldung.hamcrest; + +import static org.hamcrest.core.StringContains.containsString; +import static org.hamcrest.core.StringContains.containsStringIgnoringCase; +import static org.hamcrest.core.StringEndsWith.endsWith; +import static org.hamcrest.core.StringEndsWith.endsWithIgnoringCase; +import static org.hamcrest.core.StringStartsWith.startsWith; +import static org.hamcrest.core.StringStartsWith.startsWithIgnoringCase; +import static org.hamcrest.text.IsBlankString.blankOrNullString; +import static org.hamcrest.text.IsBlankString.blankString; +import static org.hamcrest.text.IsEmptyString.emptyOrNullString; +import static org.hamcrest.text.IsEmptyString.emptyString; +import static org.hamcrest.text.IsEqualIgnoringCase.equalToIgnoringCase; +import static org.hamcrest.text.IsEqualIgnoringWhiteSpace.equalToIgnoringWhiteSpace; +import static org.hamcrest.text.MatchesPattern.matchesPattern; +import static org.hamcrest.text.StringContainsInOrder.stringContainsInOrder; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +public class HamcrestTextUnitTest { + + @Test + public final void whenTwoStringsAreEqual_thenCorrect() { + String first = "hello"; + String second = "Hello"; + + assertThat(first, equalToIgnoringCase(second)); + } + + @Test + public final void whenTwoStringsAreEqualWithWhiteSpace_thenCorrect() { + String first = "hello"; + String second = " Hello "; + + assertThat(first, equalToIgnoringWhiteSpace(second)); + } + + @Test + public final void whenStringIsBlank_thenCorrect() { + String first = " "; + String second = null; + + assertThat(first, blankString()); + assertThat(first, blankOrNullString()); + assertThat(second, blankOrNullString()); + } + + @Test + public final void whenStringIsEmpty_thenCorrect() { + String first = ""; + String second = null; + + assertThat(first, emptyString()); + assertThat(first, emptyOrNullString()); + assertThat(second, emptyOrNullString()); + } + + @Test + public final void whenStringMatchPattern_thenCorrect() { + String first = "hello"; + + assertThat(first, matchesPattern("[a-z]+")); + } + + @Test + public final void whenVerifyStringContains_thenCorrect() { + String first = "hello"; + + assertThat(first, containsString("lo")); + assertThat(first, containsStringIgnoringCase("EL")); + } + + @Test + public final void whenVerifyStringContainsInOrder_thenCorrect() { + String first = "hello"; + + assertThat(first, stringContainsInOrder("e","l","o")); + } + + @Test + public final void whenVerifyStringStartsWith_thenCorrect() { + String first = "hello"; + + assertThat(first, startsWith("he")); + assertThat(first, startsWithIgnoringCase("HEL")); + } + + @Test + public final void whenVerifyStringEndsWith_thenCorrect() { + String first = "hello"; + + assertThat(first, endsWith("lo")); + assertThat(first, endsWithIgnoringCase("LO")); + } + +} diff --git a/core-java/src/test/resources/testFolder/sample_file_1.in b/testing-modules/mockito/src/test/resources/test1.in similarity index 100% rename from core-java/src/test/resources/testFolder/sample_file_1.in rename to testing-modules/mockito/src/test/resources/test1.in diff --git a/testing-modules/testing/README.md b/testing-modules/testing/README.md index 3511bb1bb9..c9c656dde9 100644 --- a/testing-modules/testing/README.md +++ b/testing-modules/testing/README.md @@ -17,3 +17,5 @@ - [Introduction to Jukito](http://www.baeldung.com/jukito) - [Custom JUnit 4 Test Runners](http://www.baeldung.com/junit-4-custom-runners) - [Guide to JSpec](http://www.baeldung.com/jspec) +- [Custom Assertions with AssertJ](http://www.baeldung.com/assertj-custom-assertion) +- [Using Conditions with AssertJ](http://www.baeldung.com/assertj-conditions) diff --git a/testing-modules/testing/pom.xml b/testing-modules/testing/pom.xml index 1fd6357b87..91792a4681 100644 --- a/testing-modules/testing/pom.xml +++ b/testing-modules/testing/pom.xml @@ -154,6 +154,16 @@ + + org.assertj + assertj-assertions-generator-maven-plugin + ${assertj-generator.version} + + + com.baeldung.testing.assertj.custom.Person + + + @@ -163,7 +173,8 @@ 0.7.7.201606060606 21.0 3.1.0 - 3.6.1 + 3.9.0 + 2.1.0 0.32 1.1.0 0.12 diff --git a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/Member.java b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/Member.java new file mode 100644 index 0000000000..a0b3d0daac --- /dev/null +++ b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/Member.java @@ -0,0 +1,19 @@ +package com.baeldung.testing.assertj; + +public class Member { + private String name; + private int age; + + public Member(String name, int age) { + this.name = name; + this.age = age; + } + + public String getName() { + return name; + } + + public int getAge() { + return age; + } +} diff --git a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Person.java b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Person.java new file mode 100644 index 0000000000..34afc480e4 --- /dev/null +++ b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Person.java @@ -0,0 +1,32 @@ +package com.baeldung.testing.assertj.custom; + +import java.util.ArrayList; +import java.util.List; + +public class Person { + private String fullName; + private int age; + private List nicknames; + + public Person(String fullName, int age) { + this.fullName = fullName; + this.age = age; + this.nicknames = new ArrayList<>(); + } + + public void addNickname(String nickname) { + nicknames.add(nickname); + } + + public String getFullName() { + return fullName; + } + + public int getAge() { + return age; + } + + public List getNicknames() { + return nicknames; + } +} diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/AssertJConditionUnitTest.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/AssertJConditionUnitTest.java new file mode 100644 index 0000000000..153af828f1 --- /dev/null +++ b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/AssertJConditionUnitTest.java @@ -0,0 +1,72 @@ +package com.baeldung.testing.assertj; + +import static org.assertj.core.api.Assertions.allOf; +import static org.assertj.core.api.Assertions.anyOf; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.not; +import static org.junit.Assert.fail; + +import java.util.ArrayList; +import java.util.List; + +import org.assertj.core.api.Condition; +import org.junit.Test; + +public class AssertJConditionUnitTest { + private Condition senior = new Condition<>(m -> m.getAge() >= 60, "senior"); + private Condition nameJohn = new Condition<>(m -> m.getName().equalsIgnoreCase("John"), "name John"); + + @Test + public void whenUsingMemberAgeCondition_thenCorrect() { + Member member = new Member("John", 65); + assertThat(member).is(senior); + + try { + assertThat(member).isNot(senior); + fail(); + } catch (AssertionError e) { + assertThat(e).hasMessageContaining("not to be "); + } + } + + @Test + public void whenUsingMemberNameCondition_thenCorrect() { + Member member = new Member("Jane", 60); + assertThat(member).doesNotHave(nameJohn); + + try { + assertThat(member).has(nameJohn); + fail(); + } catch (AssertionError e) { + assertThat(e).hasMessageContaining("to have:\n "); + } + } + + @Test + public void whenCollectionConditionsAreSatisfied_thenCorrect() { + List members = new ArrayList<>(); + members.add(new Member("Alice", 50)); + members.add(new Member("Bob", 60)); + + assertThat(members).haveExactly(1, senior); + assertThat(members).doNotHave(nameJohn); + } + + @Test + public void whenCombiningAllOfConditions_thenCorrect() { + Member john = new Member("John", 60); + Member jane = new Member("Jane", 50); + + assertThat(john).is(allOf(senior, nameJohn)); + assertThat(jane).is(allOf(not(nameJohn), not(senior))); + } + + @Test + public void whenCombiningAnyOfConditions_thenCorrect() { + Member john = new Member("John", 50); + Member jane = new Member("Jane", 60); + + assertThat(john).is(anyOf(senior, nameJohn)); + assertThat(jane).is(anyOf(nameJohn, senior)); + } +} diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java new file mode 100644 index 0000000000..4c09311bac --- /dev/null +++ b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java @@ -0,0 +1,44 @@ +package com.baeldung.testing.assertj.custom; + +import static com.baeldung.testing.assertj.custom.Assertions.assertThat; +import static org.junit.Assert.fail; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class AssertJCustomAssertionsUnitTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void whenPersonNameMatches_thenCorrect() { + Person person = new Person("John Doe", 20); + assertThat(person).hasFullName("John Doe"); + } + + @Test + public void whenPersonAgeLessThanEighteen_thenNotAdult() { + Person person = new Person("Jane Roe", 16); + + try { + assertThat(person).isAdult(); + fail(); + } catch (AssertionError e) { + org.assertj.core.api.Assertions.assertThat(e).hasMessage("Expected person to be adult"); + } + } + + @Test + public void whenPersonDoesNotHaveAMatchingNickname_thenIncorrect() { + Person person = new Person("John Doe", 20); + person.addNickname("Nick"); + + try { + assertThat(person).hasNickname("John"); + fail(); + } catch (AssertionError e) { + org.assertj.core.api.Assertions.assertThat(e).hasMessage("Expected person to have nickname John"); + } + } +} diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/Assertions.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/Assertions.java new file mode 100644 index 0000000000..fcffb8fc6c --- /dev/null +++ b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/Assertions.java @@ -0,0 +1,9 @@ +package com.baeldung.testing.assertj.custom; + +public class Assertions { + public static PersonAssert assertThat(Person actual) { + return new PersonAssert(actual); + } + + // static factory methods of other assertion classes +} diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/PersonAssert.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/PersonAssert.java new file mode 100644 index 0000000000..d6cc585e96 --- /dev/null +++ b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/PersonAssert.java @@ -0,0 +1,38 @@ +package com.baeldung.testing.assertj.custom; + +import org.assertj.core.api.AbstractAssert; + +public class PersonAssert extends AbstractAssert { + + public PersonAssert(Person actual) { + super(actual, PersonAssert.class); + } + + public static PersonAssert assertThat(Person actual) { + return new PersonAssert(actual); + } + + public PersonAssert hasFullName(String fullName) { + isNotNull(); + if (!actual.getFullName().equals(fullName)) { + failWithMessage("Expected person to have full name %s but was %s", fullName, actual.getFullName()); + } + return this; + } + + public PersonAssert isAdult() { + isNotNull(); + if (actual.getAge() < 18) { + failWithMessage("Expected person to be adult"); + } + return this; + } + + public PersonAssert hasNickname(String nickName) { + isNotNull(); + if (!actual.getNicknames().contains(nickName)) { + failWithMessage("Expected person to have nickname %s", nickName); + } + return this; + } +} diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java7StyleAssertions.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java7StyleAssertions.java new file mode 100644 index 0000000000..07a5be1118 --- /dev/null +++ b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java7StyleAssertions.java @@ -0,0 +1,24 @@ +package com.baeldung.testing.assertj.exceptions; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; +import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown; + +import org.junit.Test; + +public class Java7StyleAssertions { + + @Test + public void whenDividingByZero_thenArithmeticException() { + try { + int numerator = 10; + int denominator = 0; + int quotient = numerator / denominator; + fail("ArithmeticException expected because dividing by zero yields an ArithmeticException."); + failBecauseExceptionWasNotThrown(ArithmeticException.class); + } catch (Exception e) { + assertThat(e).hasMessage("/ by zero"); + assertThat(e).isInstanceOf(ArithmeticException.class); + } + } +} diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java8StyleAssertions.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java8StyleAssertions.java new file mode 100644 index 0000000000..973b921654 --- /dev/null +++ b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java8StyleAssertions.java @@ -0,0 +1,66 @@ +package com.baeldung.testing.assertj.exceptions; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.catchThrowable; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import org.junit.Test; + +public class Java8StyleAssertions { + + @Test + public void whenGettingOutOfBoundsItem_thenIndexOutOfBoundsException() { + assertThatThrownBy(() -> { + ArrayList myStringList = new ArrayList(Arrays.asList("Strine one", "String two")); + myStringList.get(2); + }).isInstanceOf(IndexOutOfBoundsException.class) + .hasMessageStartingWith("Index: 2") + .hasMessageContaining("2") + .hasMessageEndingWith("Size: 2") + .hasMessageContaining("Index: 2, Size: 2") + .hasMessage("Index: %s, Size: %s", 2, 2) + .hasMessageMatching("Index: \\d+, Size: \\d+") + .hasNoCause(); + } + + @Test + public void whenWrappingException_thenCauseInstanceOfWrappedExceptionType() { + assertThatThrownBy(() -> { + try { + throw new IOException(); + } catch (IOException e) { + throw new RuntimeException(e); + } + }).isInstanceOf(RuntimeException.class) + .hasCauseInstanceOf(IOException.class) + .hasStackTraceContaining("IOException"); + } + + @Test + public void whenDividingByZero_thenArithmeticException() { + assertThatExceptionOfType(ArithmeticException.class).isThrownBy(() -> { + int numerator = 10; + int denominator = 0; + int quotient = numerator / denominator; + }) + .withMessageContaining("/ by zero"); + + // Alternatively: + + // when + Throwable thrown = catchThrowable(() -> { + int numerator = 10; + int denominator = 0; + int quotient = numerator / denominator; + }); + + // then + assertThat(thrown).isInstanceOf(ArithmeticException.class) + .hasMessageContaining("/ by zero"); + + } +} diff --git a/vavr/README.md b/vavr/README.md index d39c9f36a1..373f897486 100644 --- a/vavr/README.md +++ b/vavr/README.md @@ -9,4 +9,5 @@ - [Guide to Collections API in Vavr](http://www.baeldung.com/vavr-collections) - [Collection Factory Methods for Vavr](http://www.baeldung.com/vavr-collection-factory-methods) - [Introduction to Future in Vavr](http://www.baeldung.com/vavr-future) +- [Introduction to VRaptor in Java](http://www.baeldung.com/vraptor) diff --git a/vavr/pom.xml b/vavr/pom.xml index f9fed7d4fc..28747af3ee 100644 --- a/vavr/pom.xml +++ b/vavr/pom.xml @@ -1,17 +1,15 @@ - + 4.0.0 com.baeldung vavr 1.0 vavr - - org.springframework.boot - spring-boot-starter-parent - 1.5.6.RELEASE - - + + parent-boot-5 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-5 @@ -21,12 +19,6 @@ ${vavr.version} - - junit - junit - ${junit.version} - - org.springframework.boot spring-boot-starter-data-jpa @@ -42,15 +34,15 @@ spring-boot-starter-test test - + - org.awaitility - awaitility - ${awaitility.version} - test - + org.awaitility + awaitility + ${awaitility.version} + test + - + spring-snapshot @@ -66,20 +58,13 @@ https://repo.spring.io/libs-snapshot - - + + spring-snapshots http://repo.spring.io/snapshot - - - 1.8 - 0.9.1 - 4.12 - 3.0.0 - @@ -97,10 +82,16 @@ **/JdbcTest.java **/*LiveTest.java - + + 1.8 + 0.9.1 + 4.12 + 3.0.0 + + \ No newline at end of file diff --git a/vavr/src/main/java/com/baeldung/samples/java/vavr/VavrSampler.java b/vavr/src/main/java/com/baeldung/samples/java/vavr/VavrSampler.java new file mode 100644 index 0000000000..f4e0728f32 --- /dev/null +++ b/vavr/src/main/java/com/baeldung/samples/java/vavr/VavrSampler.java @@ -0,0 +1,99 @@ +package com.baeldung.samples.java.vavr; + +import java.util.ArrayList; +import java.util.List; + +import io.vavr.collection.Stream; + +/** + * + * @author baeldung + */ +public class VavrSampler { + + static int[] intArray = new int[] { 1, 2, 4 }; + static List intList = new ArrayList<>(); + static int[][] intOfInts = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; + + public static void main(String[] args) { + vavrStreamElementAccess(); + System.out.println("===================================="); + vavrParallelStreamAccess(); + System.out.println("===================================="); + vavrFlatMapping(); + System.out.println("===================================="); + vavrStreamManipulation(); + System.out.println("===================================="); + vavrStreamDistinct(); + } + + public static void vavrStreamElementAccess() { + System.out.println("Vavr Element Access"); + System.out.println("===================================="); + Stream vavredStream = Stream.ofAll(intArray); + System.out.println("Vavr index access: " + vavredStream.get(2)); + System.out.println("Vavr head element access: " + vavredStream.get()); + + Stream vavredStringStream = Stream.of("foo", "bar", "baz"); + System.out.println("Find foo " + vavredStringStream.indexOf("foo")); + } + + public static void vavrParallelStreamAccess() { + + System.out.println("Vavr Stream Concurrent Modification"); + System.out.println("===================================="); + Stream vavrStream = Stream.ofAll(intList); + // intList.add(5); + vavrStream.forEach(i -> System.out.println("in a Vavr Stream: " + i)); + + // Stream wrapped = Stream.ofAll(intArray); + // intArray[2] = 5; + // wrapped.forEach(i -> System.out.println("Vavr looped " + i)); + } + + public static void jdkFlatMapping() { + System.out.println("Java flatMapping"); + System.out.println("===================================="); + java.util.stream.Stream.of(42).flatMap(i -> java.util.stream.Stream.generate(() -> { + System.out.println("nested call"); + return 42; + })).findAny(); + } + + public static void vavrFlatMapping() { + System.out.println("Vavr flatMapping"); + System.out.println("===================================="); + Stream.of(42) + .flatMap(i -> Stream.continually(() -> { + System.out.println("nested call"); + return 42; + })) + .get(0); + } + + public static void vavrStreamManipulation() { + System.out.println("Vavr Stream Manipulation"); + System.out.println("===================================="); + List stringList = new ArrayList<>(); + stringList.add("foo"); + stringList.add("bar"); + stringList.add("baz"); + Stream vavredStream = Stream.ofAll(stringList); + vavredStream.forEach(item -> System.out.println("Vavr Stream item: " + item)); + Stream vavredStream2 = vavredStream.insert(2, "buzz"); + vavredStream2.forEach(item -> System.out.println("Vavr Stream item after stream addition: " + item)); + stringList.forEach(item -> System.out.println("List item after stream addition: " + item)); + Stream deletionStream = vavredStream.remove("bar"); + deletionStream.forEach(item -> System.out.println("Vavr Stream item after stream item deletion: " + item)); + + } + + public static void vavrStreamDistinct() { + Stream vavredStream = Stream.of("foo", "bar", "baz", "buxx", "bar", "bar", "foo"); + Stream distinctVavrStream = vavredStream.distinctBy((y, z) -> { + return y.compareTo(z); + }); + distinctVavrStream.forEach(item -> System.out.println("Vavr Stream item after distinct query " + item)); + + } +} diff --git a/vavr/src/test/java/com/baeldung/vavr/VavrUnitTest.java b/vavr/src/test/java/com/baeldung/vavr/VavrUnitTest.java index 08a5e20b40..7beb75632e 100644 --- a/vavr/src/test/java/com/baeldung/vavr/VavrUnitTest.java +++ b/vavr/src/test/java/com/baeldung/vavr/VavrUnitTest.java @@ -216,11 +216,11 @@ public class VavrUnitTest { assertEquals(-1, errorSentinel); } - // @Test(expected = ArithmeticException.class) - // public void givenBadCode_whenTryHandles_thenCorrect3() { - // Try result = Try.of(() -> 1 / 0); - // result.getOrElseThrow(ArithmeticException::new); - // } + @Test(expected = RuntimeException.class) + public void givenBadCode_whenTryHandles_thenCorrect3() { + Try result = Try.of(() -> 1 / 0); + result.getOrElseThrow(e->new RuntimeException(e));//re-throw different ex type + } // lazy @Test