diff --git a/JGit/pom.xml b/JGit/pom.xml index d1ebd364da..176d55d321 100644 --- a/JGit/pom.xml +++ b/JGit/pom.xml @@ -3,10 +3,11 @@ 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 - JGitSnippets + JGit 1.0-SNAPSHOT jar http://maven.apache.org + JGit com.baeldung diff --git a/Twitter4J/pom.xml b/Twitter4J/pom.xml index 80236a2cd4..982c1adc23 100644 --- a/Twitter4J/pom.xml +++ b/Twitter4J/pom.xml @@ -1,13 +1,9 @@ 4.0.0 - - com.mabsisa Twitter4J jar - 1.0-SNAPSHOT Twitter4J - http://maven.apache.org com.baeldung @@ -23,27 +19,6 @@ - - ${project.artifactId} - - - src/main/resources - - - - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - - - **/ApplicationTest.java - - - - - - 4.0.6 diff --git a/activejdbc/pom.xml b/activejdbc/pom.xml index 0c8047f10b..542e674ff3 100644 --- a/activejdbc/pom.xml +++ b/activejdbc/pom.xml @@ -1,7 +1,6 @@ 4.0.0 - com.baeldung activejdbc 1.0-SNAPSHOT jar @@ -79,55 +78,11 @@ - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - - brief - true - false - - **/*Spec*.java - **/*Test*.java - - - **/helpers/* - **/*$* - - - - - - snapshots1 - JavaLite Snapshots1 - http://repo.javalite.io/ - - true - always - warn - - - - - - - snapshots2 - JavaLite Snapshots2 - http://repo.javalite.io/ - - true - always - warn - - - - - 1.4.13 + 2.0 development.test,development 5.1.34 diff --git a/activejdbc/src/main/java/com/baeldung/model/Employee.java b/activejdbc/src/main/java/com/baeldung/model/Employee.java index b7fa8aaf1f..e6e9be2aac 100644 --- a/activejdbc/src/main/java/com/baeldung/model/Employee.java +++ b/activejdbc/src/main/java/com/baeldung/model/Employee.java @@ -16,4 +16,8 @@ public class Employee extends Model { set("created_by",createdBy); } + public String getLastName() { + return getString("last_name"); + } + } diff --git a/activejdbc/src/main/java/com/baeldung/model/Role.java b/activejdbc/src/main/java/com/baeldung/model/Role.java index 3f425dbe6b..bbd5a7d169 100644 --- a/activejdbc/src/main/java/com/baeldung/model/Role.java +++ b/activejdbc/src/main/java/com/baeldung/model/Role.java @@ -15,4 +15,8 @@ public class Role extends Model { set("role_name",role); set("created_by",createdBy); } + + public String getRoleName() { + return getString("role_name"); + } } diff --git a/activejdbc/src/test/java/com/baeldung/ActiveJDBCAppTest.java b/activejdbc/src/test/java/com/baeldung/ActiveJDBCAppManualTest.java similarity index 97% rename from activejdbc/src/test/java/com/baeldung/ActiveJDBCAppTest.java rename to activejdbc/src/test/java/com/baeldung/ActiveJDBCAppManualTest.java index 316dc34712..22eb87f930 100644 --- a/activejdbc/src/test/java/com/baeldung/ActiveJDBCAppTest.java +++ b/activejdbc/src/test/java/com/baeldung/ActiveJDBCAppManualTest.java @@ -7,7 +7,7 @@ import org.junit.Test; import java.util.List; -public class ActiveJDBCAppTest extends DBSpec +public class ActiveJDBCAppManualTest extends DBSpec { @Test public void ifEmployeeCreated_thenIsValid() { diff --git a/algorithms/roundUpToHundred/.gitignore b/algorithms/roundUpToHundred/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/algorithms/roundUpToHundred/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundred.java b/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundred.java new file mode 100644 index 0000000000..f5024c227d --- /dev/null +++ b/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundred.java @@ -0,0 +1,20 @@ +package com.java.src; + +import java.util.Scanner; + +public class RoundUpToHundred { + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + double input = scanner.nextDouble(); + scanner.close(); + + RoundUpToHundred.round(input); + } + + static long round(double input) { + long i = (long) Math.ceil(input); + return ((i + 99) / 100) * 100; + }; + +} diff --git a/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundredTest.java b/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundredTest.java new file mode 100644 index 0000000000..f35a9a249f --- /dev/null +++ b/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundredTest.java @@ -0,0 +1,14 @@ +package com.java.src; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class RoundUpToHundredTest { + @Test + public void givenInput_whenRound_thenRoundUpToTheNearestHundred() { + assertEquals("Rounded up to hundred", 100, RoundUpToHundred.round(99)); + assertEquals("Rounded up to three hundred ", 300, RoundUpToHundred.round(200.2)); + assertEquals("Returns same rounded value", 400, RoundUpToHundred.round(400)); + } +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/distancebetweenpoints/DistanceBetweenPointsService.java b/algorithms/src/main/java/com/baeldung/algorithms/distancebetweenpoints/DistanceBetweenPointsService.java new file mode 100644 index 0000000000..0c8eb86a38 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/distancebetweenpoints/DistanceBetweenPointsService.java @@ -0,0 +1,38 @@ +package com.baeldung.algorithms.distancebetweenpoints; + +import java.awt.geom.Point2D; + +public class DistanceBetweenPointsService { + + public double calculateDistanceBetweenPoints( + double x1, + double y1, + double x2, + double y2) { + + return Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1)); + } + + public double calculateDistanceBetweenPointsWithHypot( + double x1, + double y1, + double x2, + double y2) { + + double ac = Math.abs(y2 - y1); + double cb = Math.abs(x2 - x1); + + return Math.hypot(ac, cb); + } + + public double calculateDistanceBetweenPointsWithPoint2D( + double x1, + double y1, + double x2, + double y2) { + + return Point2D.distance(x1, y1, x2, y2); + + } + +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/rectanglesoverlap/Point.java b/algorithms/src/main/java/com/baeldung/algorithms/rectanglesoverlap/Point.java new file mode 100644 index 0000000000..68b1e7c594 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/rectanglesoverlap/Point.java @@ -0,0 +1,29 @@ +package com.baeldung.algorithms.rectanglesoverlap; + +public class Point { + + private int x; + private int y; + + public Point(int x, int y) { + this.x = x; + this.y = y; + } + + public int getX() { + return x; + } + + public void setX(int x) { + this.x = x; + } + + public int getY() { + return y; + } + + public void setY(int y) { + this.y = y; + } + +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/rectanglesoverlap/Rectangle.java b/algorithms/src/main/java/com/baeldung/algorithms/rectanglesoverlap/Rectangle.java new file mode 100644 index 0000000000..38f5edec61 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/rectanglesoverlap/Rectangle.java @@ -0,0 +1,40 @@ +package com.baeldung.algorithms.rectanglesoverlap; + +public class Rectangle { + + private Point bottomLeft; + private Point topRight; + + public Rectangle(Point bottomLeft, Point topRight) { + this.bottomLeft = bottomLeft; + this.topRight = topRight; + } + + public Point getBottomLeft() { + return bottomLeft; + } + + public void setBottomLeft(Point bottomLeft) { + this.bottomLeft = bottomLeft; + } + + public Point getTopRight() { + return topRight; + } + + public void setTopRight(Point topRight) { + this.topRight = topRight; + } + + public boolean isOverlapping(Rectangle other) { + // one rectangle is to the top of the other + if (this.topRight.getY() < other.bottomLeft.getY() || this.bottomLeft.getY() > other.topRight.getY()) { + return false; + } + // one rectangle is to the left of the other + if (this.topRight.getX() < other.bottomLeft.getX() || this.bottomLeft.getX() > other.topRight.getX()) { + return false; + } + return true; + } +} diff --git a/algorithms/src/test/java/com/baeldung/algorithms/distancebetweenpoints/DistanceBetweenPointsServiceUnitTest.java b/algorithms/src/test/java/com/baeldung/algorithms/distancebetweenpoints/DistanceBetweenPointsServiceUnitTest.java new file mode 100644 index 0000000000..785afdbb2b --- /dev/null +++ b/algorithms/src/test/java/com/baeldung/algorithms/distancebetweenpoints/DistanceBetweenPointsServiceUnitTest.java @@ -0,0 +1,54 @@ +package com.baeldung.algorithms.distancebetweenpoints; + +import org.junit.Test; + +import com.baeldung.algorithms.distancebetweenpoints.DistanceBetweenPointsService; + +import static org.junit.Assert.assertEquals; + +public class DistanceBetweenPointsServiceUnitTest { + + private DistanceBetweenPointsService service = new DistanceBetweenPointsService(); + + @Test + public void givenTwoPoints_whenCalculateDistanceByFormula_thenCorrect() { + + double x1 = 3; + double y1 = 4; + double x2 = 7; + double y2 = 1; + + double distance = service.calculateDistanceBetweenPoints(x1, y1, x2, y2); + + assertEquals(distance, 5, 0.001); + + } + + @Test + public void givenTwoPoints_whenCalculateDistanceWithHypot_thenCorrect() { + + double x1 = 3; + double y1 = 4; + double x2 = 7; + double y2 = 1; + + double distance = service.calculateDistanceBetweenPointsWithHypot(x1, y1, x2, y2); + + assertEquals(distance, 5, 0.001); + + } + + @Test + public void givenTwoPoints_whenCalculateDistanceWithPoint2D_thenCorrect() { + + double x1 = 3; + double y1 = 4; + double x2 = 7; + double y2 = 1; + + double distance = service.calculateDistanceBetweenPointsWithPoint2D(x1, y1, x2, y2); + + assertEquals(distance, 5, 0.001); + + } +} diff --git a/algorithms/src/test/java/com/baeldung/algorithms/rectanglesoverlap/RectangleUnitTest.java b/algorithms/src/test/java/com/baeldung/algorithms/rectanglesoverlap/RectangleUnitTest.java new file mode 100644 index 0000000000..6707b34477 --- /dev/null +++ b/algorithms/src/test/java/com/baeldung/algorithms/rectanglesoverlap/RectangleUnitTest.java @@ -0,0 +1,42 @@ +package com.baeldung.algorithms.rectanglesoverlap; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; +import org.junit.Test; + +import com.baeldung.algorithms.rectanglesoverlap.Point; +import com.baeldung.algorithms.rectanglesoverlap.Rectangle; + +public class RectangleUnitTest { + + @Test + public void givenTwoOverlappingRectangles_whenisOverlappingCalled_shouldReturnTrue() { + Rectangle rectangle1 = new Rectangle(new Point(2, 1), new Point(4, 3)); + Rectangle rectangle2 = new Rectangle(new Point(1, 1), new Point(6, 4)); + assertTrue(rectangle1.isOverlapping(rectangle2)); + + rectangle1 = new Rectangle(new Point(-5, -2), new Point(2, 3)); + rectangle2 = new Rectangle(new Point(-2, -1), new Point(5, 2)); + assertTrue(rectangle1.isOverlapping(rectangle2)); + + rectangle1 = new Rectangle(new Point(-5, 1), new Point(2, 4)); + rectangle2 = new Rectangle(new Point(-2, -2), new Point(5, 5)); + assertTrue(rectangle1.isOverlapping(rectangle2)); + } + + @Test + public void givenTwoNonOverlappingRectangles_whenisOverlappingCalled_shouldReturnFalse() { + Rectangle rectangle1 = new Rectangle(new Point(-5, 1), new Point(-3, 4)); + Rectangle rectangle2 = new Rectangle(new Point(-2, -2), new Point(5, 5)); + assertFalse(rectangle1.isOverlapping(rectangle2)); + + rectangle1 = new Rectangle(new Point(-5, 1), new Point(3, 4)); + rectangle2 = new Rectangle(new Point(-2, -2), new Point(5, -1)); + assertFalse(rectangle1.isOverlapping(rectangle2)); + + rectangle1 = new Rectangle(new Point(-2, 1), new Point(0, 3)); + rectangle2 = new Rectangle(new Point(3, 1), new Point(5, 4)); + assertFalse(rectangle1.isOverlapping(rectangle2)); + } + +} diff --git a/apache-avro/pom.xml b/apache-avro/pom.xml index 39da518269..3e3234ff96 100644 --- a/apache-avro/pom.xml +++ b/apache-avro/pom.xml @@ -4,8 +4,9 @@ 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 - apache-avro-tutorial + apache-avro 0.0.1-SNAPSHOT + Apache Avro UTF-8 diff --git a/apache-cxf/README.md b/apache-cxf/README.md index 1e66ce5da8..d03999dce3 100644 --- a/apache-cxf/README.md +++ b/apache-cxf/README.md @@ -3,3 +3,4 @@ - [Apache CXF Support for RESTful Web Services](http://www.baeldung.com/apache-cxf-rest-api) - [A Guide to Apache CXF with Spring](http://www.baeldung.com/apache-cxf-with-spring) - [Introduction to Apache CXF](http://www.baeldung.com/introduction-to-apache-cxf) +- [Server-Sent Events (SSE) In JAX-RS](https://www.baeldung.com/java-ee-jax-rs-sse) diff --git a/apache-cxf/sse-jaxrs/sse-jaxrs-server/pom.xml b/apache-cxf/sse-jaxrs/sse-jaxrs-server/pom.xml index 46572a2b75..2e82dc3829 100644 --- a/apache-cxf/sse-jaxrs/sse-jaxrs-server/pom.xml +++ b/apache-cxf/sse-jaxrs/sse-jaxrs-server/pom.xml @@ -20,7 +20,7 @@ - ${artifactId} + ${project.artifactId} net.wasdev.wlp.maven.plugins diff --git a/apache-shiro/pom.xml b/apache-shiro/pom.xml index 3a72804ab1..98d9563284 100644 --- a/apache-shiro/pom.xml +++ b/apache-shiro/pom.xml @@ -3,7 +3,6 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.baeldung apache-shiro 1.0-SNAPSHOT @@ -36,13 +35,11 @@ org.slf4j jcl-over-slf4j - ${slf4j-version} runtime org.slf4j slf4j-log4j12 - ${slf4j-version} runtime @@ -53,26 +50,9 @@ - - - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - ${java.version} - ${java.version} - - - - - 1.4.0 - 3.7.0 - 1.8 1.2.17 - 1.7.25 \ No newline at end of file diff --git a/asm/pom.xml b/asm/pom.xml index 7bbaa2a8f1..5aad2a0e37 100644 --- a/asm/pom.xml +++ b/asm/pom.xml @@ -42,14 +42,6 @@ - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - - -javaagent:"C:\asm-1.0.jar" - - diff --git a/aws-lambda/pom.xml b/aws-lambda/pom.xml index 3a2be70da6..c47c3cd86f 100644 --- a/aws-lambda/pom.xml +++ b/aws-lambda/pom.xml @@ -95,7 +95,6 @@ 2.8.2 1.11.241 3.0.0 - 2.10 \ No newline at end of file diff --git a/aws/.gitignore b/aws/.gitignore index b83d22266a..bf11a4cc38 100644 --- a/aws/.gitignore +++ b/aws/.gitignore @@ -1 +1,2 @@ /target/ +.idea/ \ No newline at end of file diff --git a/aws/pom.xml b/aws/pom.xml index 26bc0c8037..ab63f6afa1 100644 --- a/aws/pom.xml +++ b/aws/pom.xml @@ -100,26 +100,6 @@ - - - org.apache.maven.plugins - maven-dependency-plugin - ${maven-dependency-plugin.version} - - - copy-dependencies - test-compile - - copy-dependencies - - - test - so,dll,dylib - ${project.basedir}/native-libs - - - - @@ -144,7 +124,6 @@ 1.10.L001 0.9.4.0006L 3.0.0 - 2.10 \ No newline at end of file diff --git a/aws/src/main/java/com/baeldung/rds/AWSRDSService.java b/aws/src/main/java/com/baeldung/rds/AWSRDSService.java new file mode 100644 index 0000000000..d4da92f30f --- /dev/null +++ b/aws/src/main/java/com/baeldung/rds/AWSRDSService.java @@ -0,0 +1,184 @@ +package com.baeldung.rds; + +import com.amazonaws.auth.AWSCredentialsProvider; +import com.amazonaws.auth.AWSStaticCredentialsProvider; +import com.amazonaws.auth.BasicAWSCredentials; +import com.amazonaws.regions.Regions; +import com.amazonaws.services.rds.AmazonRDS; +import com.amazonaws.services.rds.AmazonRDSClientBuilder; +import com.amazonaws.services.rds.model.*; + +import java.io.IOException; +import java.io.InputStream; +import java.sql.*; +import java.util.List; +import java.util.Properties; +import java.util.UUID; +import java.util.logging.Logger; + +public class AWSRDSService { + + + final static Logger logger = Logger.getLogger(AWSRDSService.class.getName()); + private AWSCredentialsProvider credentials; + private AmazonRDS amazonRDS; + private String db_username; + private String db_password; + private String db_database; + private String db_hostname; + + /* + * User access key and secret key must be set before execute any operation. + * Follow the link on how to get the user access and secret key + * https://aws.amazon.com/blogs/security/wheres-my-secret-access-key/ + * **/ + public AWSRDSService() throws IOException { + //Init RDS client with credentials and region. + credentials = new + AWSStaticCredentialsProvider(new + BasicAWSCredentials("", + "")); + amazonRDS = AmazonRDSClientBuilder.standard().withCredentials(credentials) + .withRegion(Regions.AP_SOUTHEAST_2).build(); + Properties prop = new Properties(); + InputStream input = AWSRDSService.class.getClassLoader().getResourceAsStream("db.properties"); + prop.load(input); + db_username = prop.getProperty("db_username"); + db_password = prop.getProperty("db_password"); + db_database = prop.getProperty("db_database"); + } + + public AWSRDSService(AmazonRDS amazonRDS){ + this.amazonRDS = amazonRDS; + } + + /** + * create the RDS instance. + * After instance creation, update the db_hostname with endpoint in db.properties. + * */ + + public String launchInstance() { + + String identifier = ""; + CreateDBInstanceRequest request = new CreateDBInstanceRequest(); + // RDS instance name + request.setDBInstanceIdentifier("Sydney"); + request.setEngine("postgres"); + request.setMultiAZ(false); + request.setMasterUsername(db_username); + request.setMasterUserPassword(db_password); + request.setDBName(db_database); + request.setStorageType("gp2"); + request.setAllocatedStorage(10); + + DBInstance instance = amazonRDS.createDBInstance(request); + + // Information about the new RDS instance + identifier = instance.getDBInstanceIdentifier(); + String status = instance.getDBInstanceStatus(); + Endpoint endpoint = instance.getEndpoint(); + String endpoint_url = "Endpoint URL not available yet."; + if (endpoint != null) { + endpoint_url = endpoint.toString(); + } + logger.info(identifier + "\t" + status); + logger.info(endpoint_url); + + return identifier; + + } + + // Describe DB instances + public void listInstances() { + DescribeDBInstancesResult result = amazonRDS.describeDBInstances(); + List instances = result.getDBInstances(); + for (DBInstance instance : instances) { + // Information about each RDS instance + String identifier = instance.getDBInstanceIdentifier(); + String engine = instance.getEngine(); + String status = instance.getDBInstanceStatus(); + Endpoint endpoint = instance.getEndpoint(); + String endpoint_url = "Endpoint URL not available yet."; + if (endpoint != null) { + endpoint_url = endpoint.toString(); + } + logger.info(identifier + "\t" + engine + "\t" + status); + logger.info("\t" + endpoint_url); + } + + } + + //Delete RDS instance + public void terminateInstance(String identifier) { + + DeleteDBInstanceRequest request = new DeleteDBInstanceRequest(); + request.setDBInstanceIdentifier(identifier); + request.setSkipFinalSnapshot(true); + + // Delete the RDS instance + DBInstance instance = amazonRDS.deleteDBInstance(request); + + // Information about the RDS instance being deleted + String status = instance.getDBInstanceStatus(); + Endpoint endpoint = instance.getEndpoint(); + String endpoint_url = "Endpoint URL not available yet."; + if (endpoint != null) { + endpoint_url = endpoint.toString(); + } + + logger.info(identifier + "\t" + status); + logger.info(endpoint_url); + + } + + + public void runJdbcTests() throws SQLException, IOException { + + // Getting database properties from db.properties + Properties prop = new Properties(); + InputStream input = AWSRDSService.class.getClassLoader().getResourceAsStream("db.properties"); + prop.load(input); + db_hostname = prop.getProperty("db_hostname"); + String jdbc_url = "jdbc:postgresql://" + db_hostname + ":5432/" + db_database; + + // Create a connection using the JDBC driver + try (Connection conn = DriverManager.getConnection(jdbc_url, db_username, db_password)) { + + // Create the test table if not exists + Statement statement = conn.createStatement(); + String sql = "CREATE TABLE IF NOT EXISTS jdbc_test (id SERIAL PRIMARY KEY, content VARCHAR(80))"; + statement.executeUpdate(sql); + + // Do some INSERT + PreparedStatement preparedStatement = conn.prepareStatement("INSERT INTO jdbc_test (content) VALUES (?)"); + String content = "" + UUID.randomUUID(); + preparedStatement.setString(1, content); + preparedStatement.executeUpdate(); + logger.info("INSERT: " + content); + + // Do some SELECT + sql = "SELECT * FROM jdbc_test"; + ResultSet resultSet = statement.executeQuery(sql); + while (resultSet.next()) { + String count = resultSet.getString("content"); + logger.info("Total Records: " + count); + } + } + + + } + + public static void main(String[] args) throws IOException, SQLException { + AWSRDSService awsrdsService = new AWSRDSService(); + + String instanceName = awsrdsService.launchInstance(); + + //Add some wait for instance creation. + + awsrdsService.listInstances(); + + awsrdsService.runJdbcTests(); + + awsrdsService.terminateInstance(instanceName); + } +} diff --git a/aws/src/main/resources/db.properties b/aws/src/main/resources/db.properties new file mode 100644 index 0000000000..e934611a2b --- /dev/null +++ b/aws/src/main/resources/db.properties @@ -0,0 +1,4 @@ +db_hostname= +db_username=username +db_password=password +db_database=mydb \ No newline at end of file diff --git a/cas/cas-secured-app/pom.xml b/cas/cas-secured-app/pom.xml index 22d1522fbe..2291da9084 100644 --- a/cas/cas-secured-app/pom.xml +++ b/cas/cas-secured-app/pom.xml @@ -2,12 +2,10 @@ 4.0.0 - com.baeldung cas-secured-app - 0.0.1-SNAPSHOT jar cas-secured-app - Demo project for Spring Boot + Demo project for CAS parent-boot-1 @@ -60,10 +58,4 @@ - - UTF-8 - UTF-8 - 1.8 - - diff --git a/cas/cas-server/pom.xml b/cas/cas-server/pom.xml index a15b4b58d5..9b61aaec3d 100644 --- a/cas/cas-server/pom.xml +++ b/cas/cas-server/pom.xml @@ -2,11 +2,17 @@ 4.0.0 - com.baeldung cas-server war 1.0 + + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../../parent-boot-1 + + org.apereo.cas @@ -39,7 +45,6 @@ org.springframework.boot spring-boot-maven-plugin - ${springboot.version} ${mainClassName} true @@ -74,42 +79,10 @@ - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - cas - - - sonatype-releases - http://oss.sonatype.org/content/repositories/releases/ - - false - - - true - - - - sonatype-snapshots - https://oss.sonatype.org/content/repositories/snapshots/ - - true - - - false - - - - shibboleth-releases - https://build.shibboleth.net/nexus/content/repositories/releases - - - @@ -214,8 +187,7 @@ - 5.3.0-SNAPSHOT - 1.5.13.RELEASE + 5.3.3 -tomcat @@ -223,14 +195,12 @@ false ${project.build.directory}/war/work/org.apereo.cas/cas-server-webapp${app.server}/META-INF/MANIFEST.MF - 1.8 - 1.8 - UTF-8 0.0.4 2.6 3.3 + 0.3.0 1.1.0 - \ No newline at end of file + diff --git a/checker-plugin/pom.xml b/checker-plugin/pom.xml index 7538340f69..45f0939e77 100644 --- a/checker-plugin/pom.xml +++ b/checker-plugin/pom.xml @@ -41,27 +41,6 @@ - - - org.apache.maven.plugins - maven-dependency-plugin - - - - - properties - - - - - maven-compiler-plugin ${maven-compiler-plugin.version} diff --git a/core-java-8/README.md b/core-java-8/README.md index e23a2a6d1e..64d423aafe 100644 --- a/core-java-8/README.md +++ b/core-java-8/README.md @@ -29,3 +29,6 @@ - [Java 8 Unsigned Arithmetic Support](http://www.baeldung.com/java-unsigned-arithmetic) - [Generalized Target-Type Inference in Java](http://www.baeldung.com/java-generalized-target-type-inference) - [Overriding System Time for Testing in Java](http://www.baeldung.com/java-override-system-time) +- [Set the Time Zone of a Date in Java](https://www.baeldung.com/java-set-date-time-zone) +- [An Overview of Regular Expressions Performance in Java](https://www.baeldung.com/java-regex-performance) +- [Java Primitives versus Objects](https://www.baeldung.com/java-primitives-vs-objects) diff --git a/core-java-8/pom.xml b/core-java-8/pom.xml index fa0d79e405..18bdaa15f4 100644 --- a/core-java-8/pom.xml +++ b/core-java-8/pom.xml @@ -116,23 +116,6 @@ - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - - org.apache.maven.plugins maven-compiler-plugin diff --git a/core-java-9/README.md b/core-java-9/README.md index 6fa2c24f9e..bf07cfcc86 100644 --- a/core-java-9/README.md +++ b/core-java-9/README.md @@ -24,3 +24,5 @@ - [Optional orElse Optional](http://www.baeldung.com/java-optional-or-else-optional) - [Java 9 java.lang.Module API](http://www.baeldung.com/java-9-module-api) - [Iterate Through a Range of Dates in Java](https://www.baeldung.com/java-iterate-date-range) +- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap) +- [Java 9 Platform Logging API](https://www.baeldung.com/java-9-logging-api) diff --git a/core-java-9/logging.sh b/core-java-9/logging.sh new file mode 100755 index 0000000000..c9b502f300 --- /dev/null +++ b/core-java-9/logging.sh @@ -0,0 +1,15 @@ +# compile logging module +# javac --module-path mods -d mods/com.baeldung.logging src/modules/com.baeldung.logging/module-info.java src/modules/com.baeldung.logging/com/baeldung/logging/*.java + +# compile logging slf4j module +javac --module-path mods -d mods/com.baeldung.logging.slf4j src/modules/com.baeldung.logging.slf4j/module-info.java src/modules/com.baeldung.logging.slf4j/com/baeldung/logging/slf4j/*.java + + +# compile logging main app module +javac --module-path mods -d mods/com.baeldung.logging.app src/modules/com.baeldung.logging.app/module-info.java src/modules/com.baeldung.logging.app/com/baeldung/logging/app/*.java + +# run logging main app +# java --module-path mods -m com.baeldung.logging.app/com.baeldung.logging.app.MainApp + +# run looging main app using logback +java --module-path mods -Dlogback.configurationFile=mods/logback.xml -m com.baeldung.logging.app/com.baeldung.logging.app.MainApp diff --git a/core-java-9/mods/logback.xml b/core-java-9/mods/logback.xml new file mode 100644 index 0000000000..c22c7a7130 --- /dev/null +++ b/core-java-9/mods/logback.xml @@ -0,0 +1,16 @@ + + + + + + + %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} -- %msg%n + + + + + + + + + \ No newline at end of file diff --git a/core-java-9/src/modules/com.baeldung.logging.app/com/baeldung/logging/app/MainApp.java b/core-java-9/src/modules/com.baeldung.logging.app/com/baeldung/logging/app/MainApp.java new file mode 100644 index 0000000000..01f53d59e9 --- /dev/null +++ b/core-java-9/src/modules/com.baeldung.logging.app/com/baeldung/logging/app/MainApp.java @@ -0,0 +1,13 @@ +package com.baeldung.logging.app; + +import static java.lang.System.Logger.*; + +public class MainApp { + + private static System.Logger LOGGER = System.getLogger("MainApp"); + + public static void main(String[] args) { + LOGGER.log(Level.ERROR, "error test"); + LOGGER.log(Level.INFO, "info test"); + } +} diff --git a/core-java-9/src/modules/com.baeldung.logging.app/module-info.java b/core-java-9/src/modules/com.baeldung.logging.app/module-info.java new file mode 100644 index 0000000000..037c72b755 --- /dev/null +++ b/core-java-9/src/modules/com.baeldung.logging.app/module-info.java @@ -0,0 +1,2 @@ +module com.baeldung.logging.app { +} \ No newline at end of file diff --git a/core-java-9/src/modules/com.baeldung.logging.slf4j/com/baeldung/logging/slf4j/Slf4jLogger.java b/core-java-9/src/modules/com.baeldung.logging.slf4j/com/baeldung/logging/slf4j/Slf4jLogger.java new file mode 100644 index 0000000000..df41e071fd --- /dev/null +++ b/core-java-9/src/modules/com.baeldung.logging.slf4j/com/baeldung/logging/slf4j/Slf4jLogger.java @@ -0,0 +1,99 @@ +package com.baeldung.logging.slf4j; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ResourceBundle; + +public class Slf4jLogger implements System.Logger { + + private final String name; + private final Logger logger; + + public Slf4jLogger(String name) { + this.name = name; + logger = LoggerFactory.getLogger(name); + } + + @Override + public String getName() { + return name; + } + + @Override + public boolean isLoggable(Level level) { + switch (level) { + case OFF: + return false; + case TRACE: + return logger.isTraceEnabled(); + case DEBUG: + return logger.isDebugEnabled(); + case INFO: + return logger.isInfoEnabled(); + case WARNING: + return logger.isWarnEnabled(); + case ERROR: + return logger.isErrorEnabled(); + case ALL: + default: + return true; + } + } + + @Override + public void log(Level level, ResourceBundle bundle, String msg, Throwable thrown) { + if (!isLoggable(level)) { + return; + } + + switch (level) { + case TRACE: + logger.trace(msg, thrown); + break; + case DEBUG: + logger.debug(msg, thrown); + break; + case INFO: + logger.info(msg, thrown); + break; + case WARNING: + logger.warn(msg, thrown); + break; + case ERROR: + logger.error(msg, thrown); + break; + case ALL: + default: + logger.info(msg, thrown); + } + } + + @Override + public void log(Level level, ResourceBundle bundle, String format, Object... params) { + if (!isLoggable(level)) { + return; + } + + switch (level) { + case TRACE: + logger.trace(format, params); + break; + case DEBUG: + logger.debug(format, params); + break; + case INFO: + logger.info(format, params); + break; + case WARNING: + logger.warn(format, params); + break; + case ERROR: + logger.error(format, params); + break; + case ALL: + default: + logger.info(format, params); + } + } +} diff --git a/core-java-9/src/modules/com.baeldung.logging.slf4j/com/baeldung/logging/slf4j/Slf4jLoggerFinder.java b/core-java-9/src/modules/com.baeldung.logging.slf4j/com/baeldung/logging/slf4j/Slf4jLoggerFinder.java new file mode 100644 index 0000000000..97f7cccb92 --- /dev/null +++ b/core-java-9/src/modules/com.baeldung.logging.slf4j/com/baeldung/logging/slf4j/Slf4jLoggerFinder.java @@ -0,0 +1,8 @@ +package com.baeldung.logging.slf4j; + +public class Slf4jLoggerFinder extends System.LoggerFinder { + @Override + public System.Logger getLogger(String name, Module module) { + return new Slf4jLogger(name); + } +} diff --git a/core-java-9/src/modules/com.baeldung.logging.slf4j/module-info.java b/core-java-9/src/modules/com.baeldung.logging.slf4j/module-info.java new file mode 100644 index 0000000000..311ca22f5b --- /dev/null +++ b/core-java-9/src/modules/com.baeldung.logging.slf4j/module-info.java @@ -0,0 +1,6 @@ +module com.baeldung.logging.slf4j { + requires org.slf4j; + provides java.lang.System.LoggerFinder + with com.baeldung.logging.slf4j.Slf4jLoggerFinder; + exports com.baeldung.logging.slf4j; +} \ No newline at end of file diff --git a/core-java-9/src/modules/com.baeldung.logging/com/baeldung/logging/ConsoleLogger.java b/core-java-9/src/modules/com.baeldung.logging/com/baeldung/logging/ConsoleLogger.java new file mode 100644 index 0000000000..a495e6979d --- /dev/null +++ b/core-java-9/src/modules/com.baeldung.logging/com/baeldung/logging/ConsoleLogger.java @@ -0,0 +1,27 @@ +package com.baeldung.logging; + +import java.text.MessageFormat; +import java.util.ResourceBundle; + +public class ConsoleLogger implements System.Logger { + + @Override + public String getName() { + return "ConsoleLogger"; + } + + @Override + public boolean isLoggable(Level level) { + return true; + } + + @Override + public void log(Level level, ResourceBundle bundle, String msg, Throwable thrown) { + System.out.printf("ConsoleLogger [%s]: %s - %s%n", level, msg, thrown); + } + + @Override + public void log(Level level, ResourceBundle bundle, String format, Object... params) { + System.out.printf("ConsoleLogger [%s]: %s%n", level, MessageFormat.format(format, params)); + } +} diff --git a/core-java-9/src/modules/com.baeldung.logging/com/baeldung/logging/CustomLoggerFinder.java b/core-java-9/src/modules/com.baeldung.logging/com/baeldung/logging/CustomLoggerFinder.java new file mode 100644 index 0000000000..b38955f199 --- /dev/null +++ b/core-java-9/src/modules/com.baeldung.logging/com/baeldung/logging/CustomLoggerFinder.java @@ -0,0 +1,9 @@ +package com.baeldung.logging; + +public class CustomLoggerFinder extends System.LoggerFinder { + + @Override + public System.Logger getLogger(String name, Module module) { + return new ConsoleLogger(); + } +} diff --git a/core-java-9/src/modules/com.baeldung.logging/module-info.java b/core-java-9/src/modules/com.baeldung.logging/module-info.java new file mode 100644 index 0000000000..4ca0af0225 --- /dev/null +++ b/core-java-9/src/modules/com.baeldung.logging/module-info.java @@ -0,0 +1,5 @@ +module com.baeldung.logging { + provides java.lang.System.LoggerFinder + with com.baeldung.logging.CustomLoggerFinder; + exports com.baeldung.logging; +} \ No newline at end of file diff --git a/core-java-9/src/test/java/com/baeldung/java9/process/ProcessUnderstandingTest.java b/core-java-9/src/test/java/com/baeldung/java9/process/ProcessUnderstandingTest.java index 2f61846c1c..2c5525b9ed 100644 --- a/core-java-9/src/test/java/com/baeldung/java9/process/ProcessUnderstandingTest.java +++ b/core-java-9/src/test/java/com/baeldung/java9/process/ProcessUnderstandingTest.java @@ -48,7 +48,7 @@ class ProcessUnderstandingTest { } //@Test - windows specific - public void givenSubProcess_thenStartSuccessIsAlive() throws IOException { + public void givenSubProcess_whenStarted_thenStartSuccessIsAlive() throws IOException { ProcessBuilder builder = new ProcessBuilder("notepad.exe"); assertTrue(builder.start().isAlive()); } @@ -75,7 +75,7 @@ class ProcessUnderstandingTest { } //@Test - windows specific - public void givenSubProcess_checkAlive() throws IOException, InterruptedException { + public void givenSubProcess_whenDestroyed_thenCheckIfAlive() throws IOException, InterruptedException { ProcessBuilder builder = new ProcessBuilder("notepad.exe"); Process process = builder.start(); Thread.sleep(10000); diff --git a/core-java-collections/README.md b/core-java-collections/README.md index ab13ba7c01..f187141712 100644 --- a/core-java-collections/README.md +++ b/core-java-collections/README.md @@ -43,3 +43,9 @@ - [An Introduction to Java.util.Hashtable Class](http://www.baeldung.com/java-hash-table) - [Copy a List to Another List in Java](http://www.baeldung.com/java-copy-list-to-another) - [Finding Max/Min of a List or Collection](http://www.baeldung.com/java-collection-min-max) +- [Java Null-Safe Streams from Collections](https://www.baeldung.com/java-null-safe-streams-from-collections) +- [Remove All Occurrences of a Specific Value from a List](https://www.baeldung.com/java-remove-value-from-list) +- [Thread Safe LIFO Data Structure Implementations](https://www.baeldung.com/java-lifo-thread-safe) +- [Collections.emptyList() vs. New List Instance](https://www.baeldung.com/java-collections-emptylist-new-list) +- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap) +- [](https://www.baeldung.com/java-hashset-arraylist-contains-performance) diff --git a/core-java-collections/src/main/java/com/baeldung/java/map/MapUtil.java b/core-java-collections/src/main/java/com/baeldung/java/map/MapUtil.java new file mode 100644 index 0000000000..688c7592f3 --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/java/map/MapUtil.java @@ -0,0 +1,44 @@ +/** + * + */ +package com.baeldung.java.map; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.Map.Entry; +import java.util.stream.Stream; + +/** + * @author swpraman + * + */ +public class MapUtil { + + public static Stream keys(Map map, V value) { + return map.entrySet() + .stream() + .filter(entry -> value.equals(entry.getValue())) + .map(Map.Entry::getKey); + } + + public static Set getKeys(Map map, V value) { + Set keys = new HashSet<>(); + for (Entry entry : map.entrySet()) { + if (entry.getValue().equals(value)) { + keys.add(entry.getKey()); + } + } + return keys; + } + + public static K getKey(Map map, V value) { + for (Entry entry : map.entrySet()) { + if (entry.getValue().equals(value)) { + return entry.getKey(); + } + } + return null; + } + +} diff --git a/core-java-collections/src/main/java/com/baeldung/performance/ArrayListBenchmark.java b/core-java-collections/src/main/java/com/baeldung/performance/ArrayListBenchmark.java new file mode 100644 index 0000000000..dddd85007d --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/performance/ArrayListBenchmark.java @@ -0,0 +1,77 @@ +package com.baeldung.performance; + +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import java.util.*; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Warmup(iterations = 10) +public class ArrayListBenchmark { + + @State(Scope.Thread) + public static class MyState { + + List employeeList = new ArrayList<>(); + //LinkedList employeeList = new LinkedList<>(); + + long iterations = 100000; + + Employee employee = new Employee(100L, "Harry"); + + int employeeIndex = -1; + + @Setup(Level.Trial) + public void setUp() { + for (long i = 0; i < iterations; i++) { + employeeList.add(new Employee(i, "John")); + } + + employeeList.add(employee); + employeeIndex = employeeList.indexOf(employee); + } + } + + @Benchmark + public void testAddAt(ArrayListBenchmark.MyState state) { + state.employeeList.add((int) (state.iterations), new Employee(state.iterations, "John")); + } + + @Benchmark + public boolean testContains(ArrayListBenchmark.MyState state) { + return state.employeeList.contains(state.employee); + } + + @Benchmark + public int testIndexOf(ArrayListBenchmark.MyState state) { + return state.employeeList.indexOf(state.employee); + } + + @Benchmark + public Employee testGet(ArrayListBenchmark.MyState state) { + return state.employeeList.get(state.employeeIndex); + } + + @Benchmark + public boolean testRemove(ArrayListBenchmark.MyState state) { + return state.employeeList.remove(state.employee); + } + +// @Benchmark +// public void testAdd(ArrayListBenchmark.MyState state) { +// state.employeeList.add(new Employee(state.iterations + 1, "John")); +// } + + public static void main(String[] args) throws Exception { + Options options = new OptionsBuilder() + .include(ArrayListBenchmark.class.getSimpleName()).threads(1) + .forks(1).shouldFailOnError(true) + .shouldDoGC(true) + .jvmArgs("-server").build(); + new Runner(options).run(); + } +} diff --git a/core-java-collections/src/main/java/com/baeldung/performance/CopyOnWriteBenchmark.java b/core-java-collections/src/main/java/com/baeldung/performance/CopyOnWriteBenchmark.java new file mode 100644 index 0000000000..30c50291dd --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/performance/CopyOnWriteBenchmark.java @@ -0,0 +1,78 @@ +package com.baeldung.performance; + +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Warmup(iterations = 10) +public class CopyOnWriteBenchmark { + + @State(Scope.Thread) + public static class MyState { + + CopyOnWriteArrayList employeeList = new CopyOnWriteArrayList<>(); + + long iterations = 100000; + + Employee employee = new Employee(100L, "Harry"); + + int employeeIndex = -1; + + @Setup(Level.Trial) + public void setUp() { + for (long i = 0; i < iterations; i++) { + employeeList.add(new Employee(i, "John")); + } + + employeeList.add(employee); + + employeeIndex = employeeList.indexOf(employee); + } + } + + @Benchmark + public void testAdd(CopyOnWriteBenchmark.MyState state) { + state.employeeList.add(new Employee(state.iterations + 1, "John")); + } + + @Benchmark + public void testAddAt(CopyOnWriteBenchmark.MyState state) { + state.employeeList.add((int) (state.iterations), new Employee(state.iterations, "John")); + } + + @Benchmark + public boolean testContains(CopyOnWriteBenchmark.MyState state) { + return state.employeeList.contains(state.employee); + } + + @Benchmark + public int testIndexOf(CopyOnWriteBenchmark.MyState state) { + return state.employeeList.indexOf(state.employee); + } + + @Benchmark + public Employee testGet(CopyOnWriteBenchmark.MyState state) { + return state.employeeList.get(state.employeeIndex); + } + + @Benchmark + public boolean testRemove(CopyOnWriteBenchmark.MyState state) { + return state.employeeList.remove(state.employee); + } + + + public static void main(String[] args) throws Exception { + Options options = new OptionsBuilder() + .include(CopyOnWriteBenchmark.class.getSimpleName()).threads(1) + .forks(1).shouldFailOnError(true) + .shouldDoGC(true) + .jvmArgs("-server").build(); + new Runner(options).run(); + } +} diff --git a/core-java-collections/src/main/java/com/baeldung/performance/Employee.java b/core-java-collections/src/main/java/com/baeldung/performance/Employee.java index daa68ae2f1..d811cfbb7d 100644 --- a/core-java-collections/src/main/java/com/baeldung/performance/Employee.java +++ b/core-java-collections/src/main/java/com/baeldung/performance/Employee.java @@ -10,6 +10,22 @@ public class Employee { this.id = id; } + 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; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/core-java-collections/src/main/java/com/baeldung/performance/HashMapBenchmark.java b/core-java-collections/src/main/java/com/baeldung/performance/HashMapBenchmark.java new file mode 100644 index 0000000000..085daaceb7 --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/performance/HashMapBenchmark.java @@ -0,0 +1,73 @@ +package com.baeldung.performance; + +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import java.util.*; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Warmup(iterations = 10) +public class HashMapBenchmark { + + @State(Scope.Thread) + public static class MyState { + + Map employeeMap = new HashMap<>(); + //LinkedHashMap employeeMap = new LinkedHashMap<>(); + //IdentityHashMap employeeMap = new IdentityHashMap<>(); + //WeakHashMap employeeMap = new WeakHashMap<>(); + //ConcurrentHashMap employeeMap = new ConcurrentHashMap<>(); + //ConcurrentSkipListMap employeeMap = new ConcurrentSkipListMap <>(); + + // TreeMap + + long iterations = 100000; + + Employee employee = new Employee(100L, "Harry"); + + int employeeIndex = -1; + + @Setup(Level.Trial) + public void setUp() { + for (long i = 0; i < iterations; i++) { + employeeMap.put(i, new Employee(i, "John")); + } + + //employeeMap.put(iterations, employee); + } + } + + @Benchmark + public Employee testGet(HashMapBenchmark.MyState state) { + return state.employeeMap.get(state.iterations); + } + + @Benchmark + public Employee testRemove(HashMapBenchmark.MyState state) { + return state.employeeMap.remove(state.iterations); + } + + @Benchmark + public Employee testPut(HashMapBenchmark.MyState state) { + return state.employeeMap.put(state.employee.getId(), state.employee); + } + + @Benchmark + public Boolean testContainsKey(HashMapBenchmark.MyState state) { + return state.employeeMap.containsKey(state.employee.getId()); + } + + + public static void main(String[] args) throws Exception { + Options options = new OptionsBuilder() + .include(HashMapBenchmark.class.getSimpleName()).threads(1) + .forks(1).shouldFailOnError(true) + .shouldDoGC(true) + .jvmArgs("-server").build(); + new Runner(options).run(); + } +} diff --git a/core-java-collections/src/main/java/com/baeldung/performance/SetBenchMark.java b/core-java-collections/src/main/java/com/baeldung/performance/SetBenchMark.java new file mode 100644 index 0000000000..f70bab4fcf --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/performance/SetBenchMark.java @@ -0,0 +1,66 @@ +package com.baeldung.performance; + +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.Set; +import java.util.TreeSet; +import java.util.concurrent.ConcurrentSkipListSet; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Warmup(iterations = 10) +public class SetBenchMark { + + @State(Scope.Thread) + public static class MyState { + + //Set employeeSet = new HashSet<>(); + LinkedHashSet employeeSet = new LinkedHashSet<>(); + //ConcurrentSkipListSet employeeSet = new ConcurrentSkipListSet <>(); + + // TreeSet  + + long iterations = 1000; + Employee employee = new Employee(100L, "Harry"); + + @Setup(Level.Trial) + public void setUp() { + for (long i = 0; i < iterations; i++) { + employeeSet.add(new Employee(i, "John")); + } + + //employeeSet.add(employee); + } + } + + @Benchmark + public boolean testAdd(SetBenchMark.MyState state) { + return state.employeeSet.add(state.employee); + } + + @Benchmark + public Boolean testContains(SetBenchMark.MyState state) { + return state.employeeSet.contains(state.employee); + } + + @Benchmark + public boolean testRemove(SetBenchMark.MyState state) { + return state.employeeSet.remove(state.employee); + } + + + public static void main(String[] args) throws Exception { + Options options = new OptionsBuilder() + .include(SetBenchMark.class.getSimpleName()).threads(1) + .forks(1).shouldFailOnError(true) + .shouldDoGC(true) + .jvmArgs("-server").build(); + new Runner(options).run(); + } +} diff --git a/core-java-collections/src/test/java/com/baeldung/collection/ClearVsRemoveAllUnitTest.java b/core-java-collections/src/test/java/com/baeldung/collection/ClearVsRemoveAllUnitTest.java new file mode 100644 index 0000000000..8b0a7ef0db --- /dev/null +++ b/core-java-collections/src/test/java/com/baeldung/collection/ClearVsRemoveAllUnitTest.java @@ -0,0 +1,40 @@ +package com.baeldung.collection; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Unit tests demonstrating differences between ArrayList#clear() and ArrayList#removeAll() + */ +class ClearVsRemoveAllUnitTest { + + /* + * Tests + */ + @Test + void givenArrayListWithElements_whenClear_thenListBecomesEmpty() { + Collection collection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)); + + collection.clear(); + + assertTrue(collection.isEmpty()); + } + + @Test + void givenTwoArrayListsWithCommonElements_whenRemoveAll_thenFirstListMissElementsFromSecondList() { + Collection firstCollection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)); + Collection secondCollection = new ArrayList<>(Arrays.asList(3, 4, 5, 6, 7)); + + firstCollection.removeAll(secondCollection); + + assertEquals(Arrays.asList(1, 2), firstCollection); + assertEquals(Arrays.asList(3, 4, 5, 6, 7), secondCollection); + } + +} diff --git a/core-java-collections/src/test/java/com/baeldung/java/map/MapUtilUnitTest.java b/core-java-collections/src/test/java/com/baeldung/java/map/MapUtilUnitTest.java new file mode 100644 index 0000000000..e31385e972 --- /dev/null +++ b/core-java-collections/src/test/java/com/baeldung/java/map/MapUtilUnitTest.java @@ -0,0 +1,104 @@ +/** + * + */ +package com.baeldung.java.map; + +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.stream.Collectors; + +import org.apache.commons.collections4.BidiMap; +import org.apache.commons.collections4.bidimap.DualHashBidiMap; +import org.junit.Test; + +import com.google.common.collect.HashBiMap; + +/** + * @author swpraman + * + */ +public class MapUtilUnitTest { + + + @Test + public void whenUsingImperativeWayForSingleKey_shouldReturnSingleKey() { + Map capitalCountryMap = new HashMap<>(); + capitalCountryMap.put("Tokyo", "Japan"); + capitalCountryMap.put("New Delhi", "India"); + assertEquals("New Delhi", MapUtil.getKey(capitalCountryMap, "India")); + } + + @Test + public void whenUsingImperativeWayForAllKeys_shouldReturnAllKeys() { + Map capitalCountryMap = new HashMap<>(); + capitalCountryMap.put("Tokyo", "Japan"); + capitalCountryMap.put("Berlin", "Germany"); + capitalCountryMap.put("Cape Town", "South Africa"); + capitalCountryMap.put("Pretoria", "South Africa"); + capitalCountryMap.put("Bloemfontein", "South Africa"); + + assertEquals(new HashSet(Arrays.asList( + new String[] {"Cape Town", "Pretoria", "Bloemfontein"})), + MapUtil.getKeys(capitalCountryMap, "South Africa")); + } + + @Test + public void whenUsingFunctionalWayForSingleKey_shouldReturnSingleKey() { + Map capitalCountryMap = new HashMap<>(); + capitalCountryMap.put("Tokyo", "Japan"); + capitalCountryMap.put("Berlin", "Germany"); + assertEquals("Berlin", MapUtil.keys(capitalCountryMap, "Germany").findFirst().get()); + } + + @Test + public void whenUsingFunctionalWayForAllKeys_shouldReturnAllKeys() { + Map capitalCountryMap = new HashMap<>(); + capitalCountryMap.put("Tokyo", "Japan"); + capitalCountryMap.put("Berlin", "Germany"); + capitalCountryMap.put("Cape Town", "South Africa"); + capitalCountryMap.put("Pretoria", "South Africa"); + capitalCountryMap.put("Bloemfontein", "South Africa"); + assertEquals(new HashSet(Arrays.asList( + new String[] {"Cape Town", "Pretoria", "Bloemfontein"})), + MapUtil.keys(capitalCountryMap, "South Africa").collect(Collectors.toSet())); + } + + @Test + public void whenUsingBidiMap_shouldReturnKey() { + BidiMap capitalCountryMap = new DualHashBidiMap(); + capitalCountryMap.put("Berlin", "Germany"); + capitalCountryMap.put("Cape Town", "South Africa"); + assertEquals("Berlin", capitalCountryMap.getKey("Germany")); + } + + @Test + public void whenUsingBidiMapAddDuplicateValue_shouldRemoveOldEntry() { + BidiMap capitalCountryMap = new DualHashBidiMap(); + capitalCountryMap.put("Berlin", "Germany"); + capitalCountryMap.put("Cape Town", "South Africa"); + capitalCountryMap.put("Pretoria", "South Africa"); + assertEquals("Pretoria", capitalCountryMap.getKey("South Africa")); + } + + @Test + public void whenUsingBiMap_shouldReturnKey() { + HashBiMap capitalCountryMap = HashBiMap.create(); + capitalCountryMap.put("Berlin", "Germany"); + capitalCountryMap.put("Cape Town", "South Africa"); + assertEquals("Berlin", capitalCountryMap.inverse().get("Germany")); + } + + @Test(expected=IllegalArgumentException.class) + public void whenUsingBiMapAddDuplicateValue_shouldThrowException() { + HashBiMap capitalCountryMap = HashBiMap.create(); + capitalCountryMap.put("Berlin", "Germany"); + capitalCountryMap.put("Cape Town", "South Africa"); + capitalCountryMap.put("Pretoria", "South Africa"); + assertEquals("Berlin", capitalCountryMap.inverse().get("Germany")); + } + +} diff --git a/core-java-concurrency-collections/pom.xml b/core-java-concurrency-collections/pom.xml index 5e0a80d33c..9473de8c51 100644 --- a/core-java-concurrency-collections/pom.xml +++ b/core-java-concurrency-collections/pom.xml @@ -57,26 +57,6 @@ true - - - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - - - diff --git a/core-java-concurrency/pom.xml b/core-java-concurrency/pom.xml index eb81983a2a..bd22253c2c 100644 --- a/core-java-concurrency/pom.xml +++ b/core-java-concurrency/pom.xml @@ -57,26 +57,6 @@ true - - - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - - - diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/yield/ThreadYield.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/yield/ThreadYield.java new file mode 100644 index 0000000000..b31a04015f --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/yield/ThreadYield.java @@ -0,0 +1,17 @@ +package com.baeldung.concurrent.yield; + +public class ThreadYield { + public static void main(String[] args) { + Runnable r = () -> { + int counter = 0; + while (counter < 2) { + System.out.println(Thread.currentThread() + .getName()); + counter++; + Thread.yield(); + } + }; + new Thread(r).start(); + new Thread(r).start(); + } +} diff --git a/core-java-io/README.md b/core-java-io/README.md index 5e5ddf42b4..17c93d9739 100644 --- a/core-java-io/README.md +++ b/core-java-io/README.md @@ -30,3 +30,4 @@ - [Download a File From an URL in Java](http://www.baeldung.com/java-download-file) - [Create a Symbolic Link with Java](http://www.baeldung.com/java-symlink) - [Quick Use of FilenameFilter](http://www.baeldung.com/java-filename-filter) +- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap) diff --git a/core-java-io/pom.xml b/core-java-io/pom.xml index bc71fb8838..cf3e950cb8 100644 --- a/core-java-io/pom.xml +++ b/core-java-io/pom.xml @@ -1,7 +1,6 @@ 4.0.0 - com.baeldung core-java-io 0.1.0-SNAPSHOT jar @@ -166,36 +165,6 @@ - - org.apache.maven.plugins - maven-surefire-plugin - - - **/*LiveTest.java - **/*IntegrationTest.java - **/*IntTest.java - **/*LongRunningUnitTest.java - **/*ManualTest.java - - true - - - - org.springframework.boot - spring-boot-maven-plugin - ${spring-boot-maven-plugin.version} - - - - repackage - - - spring-boot - org.baeldung.executable.ExecutableMavenJar - - - - org.codehaus.mojo exec-maven-plugin @@ -229,32 +198,6 @@ integration - - org.apache.maven.plugins - maven-surefire-plugin - - - integration-test - - test - - - - **/*ManualTest.java - - - **/*IntegrationTest.java - **/*IntTest.java - - - - - - - json - - - org.codehaus.mojo exec-maven-plugin @@ -310,14 +253,11 @@ 1.7.0 - 1.8 - 1.8 3.0.0-M1 2.4.0 2.1.0.1 1.19 2.4.5 - 2.0.4.RELEASE \ No newline at end of file diff --git a/core-java-io/src/main/java/com/baeldung/zip/ZipDirectory.java b/core-java-io/src/main/java/com/baeldung/zip/ZipDirectory.java index 7da71a093d..42147b07db 100644 --- a/core-java-io/src/main/java/com/baeldung/zip/ZipDirectory.java +++ b/core-java-io/src/main/java/com/baeldung/zip/ZipDirectory.java @@ -24,6 +24,13 @@ public class ZipDirectory { return; } if (fileToZip.isDirectory()) { + if (fileName.endsWith("/")) { + zipOut.putNextEntry(new ZipEntry(fileName)); + zipOut.closeEntry(); + } else { + zipOut.putNextEntry(new ZipEntry(fileName + "/")); + zipOut.closeEntry(); + } final File[] children = fileToZip.listFiles(); for (final File childFile : children) { zipFile(childFile, fileName + "/" + childFile.getName(), zipOut); 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 7968967679..6a020f5eae 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 @@ -1,6 +1,7 @@ package com.baeldung.file; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; import org.hamcrest.CoreMatchers; import org.hamcrest.Matchers; import org.junit.Test; @@ -120,4 +121,14 @@ public class FileOperationsManualTest { return resultStringBuilder.toString(); } + + @Test + public void givenFileName_whenUsingIOUtils_thenFileData() throws IOException { + String expectedData = "This is a content of the file"; + + FileInputStream fis = new FileInputStream("src/test/resources/fileToRead.txt"); + String data = IOUtils.toString(fis, "UTF-8"); + + assertEquals(expectedData, data.trim()); + } } \ No newline at end of file diff --git a/core-java-io/src/test/java/com/baeldung/symlink/SymLinkExampleUnitTest.java b/core-java-io/src/test/java/com/baeldung/symlink/SymLinkExampleManualTest.java similarity index 96% rename from core-java-io/src/test/java/com/baeldung/symlink/SymLinkExampleUnitTest.java rename to core-java-io/src/test/java/com/baeldung/symlink/SymLinkExampleManualTest.java index 803d8881b4..caa7049475 100644 --- a/core-java-io/src/test/java/com/baeldung/symlink/SymLinkExampleUnitTest.java +++ b/core-java-io/src/test/java/com/baeldung/symlink/SymLinkExampleManualTest.java @@ -9,7 +9,7 @@ import java.nio.file.Paths; import org.junit.Test; -public class SymLinkExampleUnitTest { +public class SymLinkExampleManualTest { @Test public void whenUsingFiles_thenCreateSymbolicLink() throws IOException { diff --git a/core-java-persistence/README.md b/core-java-persistence/README.md index 08afcadb72..ca0ce81eef 100644 --- a/core-java-persistence/README.md +++ b/core-java-persistence/README.md @@ -5,4 +5,5 @@ ### Relevant Articles: - [Introduction to JDBC](http://www.baeldung.com/java-jdbc) - [Batch Processing in JDBC](http://www.baeldung.com/jdbc-batch-processing) -- [Introduction to the JDBC RowSet Interface in Java](http://www.baeldung.com/java-jdbc-rowset) \ No newline at end of file +- [Introduction to the JDBC RowSet Interface in Java](http://www.baeldung.com/java-jdbc-rowset) +- [A Simple Guide to Connection Pooling in Java](https://www.baeldung.com/java-connection-pooling) diff --git a/core-java-sun/pom.xml b/core-java-sun/pom.xml index 7292335232..57d5e9da5b 100644 --- a/core-java-sun/pom.xml +++ b/core-java-sun/pom.xml @@ -166,22 +166,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 diff --git a/core-java/README.md b/core-java/README.md index e74f6cf815..ade7f46504 100644 --- a/core-java/README.md +++ b/core-java/README.md @@ -145,3 +145,9 @@ - [Common Java Exceptions](http://www.baeldung.com/java-common-exceptions) - [Java Constructors vs Static Factory Methods](https://www.baeldung.com/java-constructors-vs-static-factory-methods) - [Differences Between Final, Finally and Finalize in Java](https://www.baeldung.com/java-final-finally-finalize) +- [Static and Dynamic Binding in Java](https://www.baeldung.com/java-static-dynamic-binding) +- [Java List Initialization in One Line](https://www.baeldung.com/java-init-list-one-line) +- [Difference Between Throw and Throws in Java](https://www.baeldung.com/java-throw-throws) +- [ClassCastException: Arrays$ArrayList cannot be cast to ArrayList](https://www.baeldung.com/java-classcastexception-arrays-arraylist) +- [Throw Exception in Optional in Java 8](https://www.baeldung.com/java-optional-throw-exception) +- [Add a Character to a String at a Given Position](https://www.baeldung.com/java-add-character-to-string) diff --git a/core-java/src/main/java/com/baeldung/doubles/SplitFloatingPointNumbers.java b/core-java/src/main/java/com/baeldung/doubles/SplitFloatingPointNumbers.java new file mode 100644 index 0000000000..a28ac3d5a1 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/doubles/SplitFloatingPointNumbers.java @@ -0,0 +1,40 @@ +package com.baeldung.doubles; + +import java.math.BigDecimal; + +public class SplitFloatingPointNumbers { + + public static void main(String[] args) { + + double doubleNumber = 24.04; + splitUsingFloatingTypes(doubleNumber); + splitUsingString(doubleNumber); + splitUsingBigDecimal(doubleNumber); + } + + private static void splitUsingFloatingTypes(double doubleNumber) { + System.out.println("Using Floating Point Arithmetics:"); + int intPart = (int) doubleNumber; + System.out.println("Double Number: "+doubleNumber); + System.out.println("Integer Part: "+ intPart); + System.out.println("Decimal Part: "+ (doubleNumber - intPart)); + } + + private static void splitUsingString(double doubleNumber) { + System.out.println("Using String Operations:"); + String doubleAsString = String.valueOf(doubleNumber); + int indexOfDecimal = doubleAsString.indexOf("."); + System.out.println("Double Number: "+doubleNumber); + System.out.println("Integer Part: "+ doubleAsString.substring(0, indexOfDecimal)); + System.out.println("Decimal Part: "+ doubleAsString.substring(indexOfDecimal)); + } + + private static void splitUsingBigDecimal(double doubleNumber) { + System.out.println("Using BigDecimal Operations:"); + BigDecimal bigDecimal = new BigDecimal(String.valueOf(doubleNumber)); + int intValue = bigDecimal.intValue(); + System.out.println("Double Number: "+bigDecimal.toPlainString()); + System.out.println("Integer Part: "+intValue); + System.out.println("Decimal Part: "+bigDecimal.subtract(new BigDecimal(intValue)).toPlainString()); + } +} diff --git a/core-java/src/main/java/com/baeldung/heapdump/HeapDump.java b/core-java/src/main/java/com/baeldung/heapdump/HeapDump.java new file mode 100644 index 0000000000..8cce20de8d --- /dev/null +++ b/core-java/src/main/java/com/baeldung/heapdump/HeapDump.java @@ -0,0 +1,25 @@ +package com.baeldung.heapdump; + +import com.sun.management.HotSpotDiagnosticMXBean; + +import javax.management.MBeanServer; + +import java.io.IOException; +import java.lang.management.ManagementFactory; +import java.nio.file.Paths; + +public class HeapDump { + + public static void dumpHeap(String filePath, boolean live) throws IOException { + MBeanServer server = ManagementFactory.getPlatformMBeanServer(); + HotSpotDiagnosticMXBean mxBean = ManagementFactory.newPlatformMXBeanProxy( + server, "com.sun.management:type=HotSpotDiagnostic", HotSpotDiagnosticMXBean.class); + mxBean.dumpHeap(filePath, live); + } + + public static void main(String[] args) throws IOException { + String file = Paths.get("dump.hprof").toFile().getPath(); + + dumpHeap(file, true); + } +} diff --git a/core-java/src/main/java/com/baeldung/passwordhashing/PBKDF2Hasher.java b/core-java/src/main/java/com/baeldung/passwordhashing/PBKDF2Hasher.java new file mode 100644 index 0000000000..e2259e4249 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/passwordhashing/PBKDF2Hasher.java @@ -0,0 +1,149 @@ +package com.baeldung.passwordhashing; + +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.KeySpec; +import java.util.Arrays; +import java.util.Base64; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.PBEKeySpec; + +/** + * Hash passwords for storage, and test passwords against password tokens. + * + * Instances of this class can be used concurrently by multiple threads. + * + * @author erickson + * @see StackOverflow + */ +public final class PBKDF2Hasher +{ + + /** + * Each token produced by this class uses this identifier as a prefix. + */ + public static final String ID = "$31$"; + + /** + * The minimum recommended cost, used by default + */ + public static final int DEFAULT_COST = 16; + + private static final String ALGORITHM = "PBKDF2WithHmacSHA1"; + + private static final int SIZE = 128; + + private static final Pattern layout = Pattern.compile("\\$31\\$(\\d\\d?)\\$(.{43})"); + + private final SecureRandom random; + + private final int cost; + + public PBKDF2Hasher() + { + this(DEFAULT_COST); + } + + /** + * Create a password manager with a specified cost + * + * @param cost the exponential computational cost of hashing a password, 0 to 30 + */ + public PBKDF2Hasher(int cost) + { + iterations(cost); /* Validate cost */ + this.cost = cost; + this.random = new SecureRandom(); + } + + private static int iterations(int cost) + { + if ((cost < 0) || (cost > 30)) + throw new IllegalArgumentException("cost: " + cost); + return 1 << cost; + } + + /** + * Hash a password for storage. + * + * @return a secure authentication token to be stored for later authentication + */ + public String hash(char[] password) + { + byte[] salt = new byte[SIZE / 8]; + random.nextBytes(salt); + byte[] dk = pbkdf2(password, salt, 1 << cost); + byte[] hash = new byte[salt.length + dk.length]; + System.arraycopy(salt, 0, hash, 0, salt.length); + System.arraycopy(dk, 0, hash, salt.length, dk.length); + Base64.Encoder enc = Base64.getUrlEncoder().withoutPadding(); + return ID + cost + '$' + enc.encodeToString(hash); + } + + /** + * Authenticate with a password and a stored password token. + * + * @return true if the password and token match + */ + public boolean checkPassword(char[] password, String token) + { + Matcher m = layout.matcher(token); + if (!m.matches()) + throw new IllegalArgumentException("Invalid token format"); + int iterations = iterations(Integer.parseInt(m.group(1))); + byte[] hash = Base64.getUrlDecoder().decode(m.group(2)); + byte[] salt = Arrays.copyOfRange(hash, 0, SIZE / 8); + byte[] check = pbkdf2(password, salt, iterations); + int zero = 0; + for (int idx = 0; idx < check.length; ++idx) + zero |= hash[salt.length + idx] ^ check[idx]; + return zero == 0; + } + + private static byte[] pbkdf2(char[] password, byte[] salt, int iterations) + { + KeySpec spec = new PBEKeySpec(password, salt, iterations, SIZE); + try { + SecretKeyFactory f = SecretKeyFactory.getInstance(ALGORITHM); + return f.generateSecret(spec).getEncoded(); + } + catch (NoSuchAlgorithmException ex) { + throw new IllegalStateException("Missing algorithm: " + ALGORITHM, ex); + } + catch (InvalidKeySpecException ex) { + throw new IllegalStateException("Invalid SecretKeyFactory", ex); + } + } + + /** + * Hash a password in an immutable {@code String}. + * + *

Passwords should be stored in a {@code char[]} so that it can be filled + * with zeros after use instead of lingering on the heap and elsewhere. + * + * @deprecated Use {@link #hash(char[])} instead + */ + @Deprecated + public String hash(String password) + { + return hash(password.toCharArray()); + } + + /** + * Authenticate with a password in an immutable {@code String} and a stored + * password token. + * + * @deprecated Use {@link #checkPassword(char[],String)} instead. + * @see #hash(String) + */ + @Deprecated + public boolean checkPassword(String password, String token) + { + return checkPassword(password.toCharArray(), token); + } + +} diff --git a/core-java/src/main/java/com/baeldung/passwordhashing/SHA512Hasher.java b/core-java/src/main/java/com/baeldung/passwordhashing/SHA512Hasher.java new file mode 100644 index 0000000000..4f5337f963 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/passwordhashing/SHA512Hasher.java @@ -0,0 +1,35 @@ +package com.baeldung.passwordhashing; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + + +/** A really simple SHA_512 Encryption example. + * + */ +public class SHA512Hasher { + + public String hash(String passwordToHash, byte[] salt){ + String generatedPassword = null; + try { + MessageDigest md = MessageDigest.getInstance("SHA-512"); + md.update(salt); + byte[] bytes = md.digest(passwordToHash.getBytes(StandardCharsets.UTF_8)); + StringBuilder sb = new StringBuilder(); + for(int i=0; i< bytes.length ;i++){ + sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1)); + } + generatedPassword = sb.toString(); + } + catch (NoSuchAlgorithmException e){ + e.printStackTrace(); + } + return generatedPassword; + } + + public boolean checkPassword(String hash, String attempt, byte[] salt){ + String generatedHash = hash(attempt, salt); + return hash.equals(generatedHash); + } +} diff --git a/core-java/src/main/java/com/baeldung/passwordhashing/SimplePBKDF2Hasher.java b/core-java/src/main/java/com/baeldung/passwordhashing/SimplePBKDF2Hasher.java new file mode 100644 index 0000000000..36c9b65070 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/passwordhashing/SimplePBKDF2Hasher.java @@ -0,0 +1,18 @@ +package com.baeldung.passwordhashing; + +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.PBEKeySpec; +import java.security.spec.KeySpec; + +/** A really simple SimplePBKDF2 Encryption example. + * + */ +public class SimplePBKDF2Hasher { + + public static String hashSimple(String password, byte[] salt) throws Exception{ + KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 128); + SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); + byte[] hash = f.generateSecret(spec).getEncoded(); + return String.valueOf(hash); + } +} diff --git a/core-java/src/main/java/com/baeldung/switchstatement/SwitchStatement.java b/core-java/src/main/java/com/baeldung/switchstatement/SwitchStatement.java new file mode 100644 index 0000000000..69e151bfcb --- /dev/null +++ b/core-java/src/main/java/com/baeldung/switchstatement/SwitchStatement.java @@ -0,0 +1,70 @@ +package com.baeldung.switchstatement; + +public class SwitchStatement { + + public String exampleOfIF(String animal) { + + String result; + + if (animal.equals("DOG") || animal.equals("CAT")) { + result = "domestic animal"; + } else if (animal.equals("TIGER")) { + result = "wild animal"; + } else { + result = "unknown animal"; + } + return result; + } + + public String exampleOfSwitch(String animal) { + + String result; + + switch (animal) { + case "DOG": + case "CAT": + result = "domestic animal"; + break; + case "TIGER": + result = "wild animal"; + break; + default: + result = "unknown animal"; + break; + } + return result; + } + + public String forgetBreakInSwitch(String animal) { + + String result; + + switch (animal) { + + case "DOG": + System.out.println("domestic animal"); + result = "domestic animal"; + + default: + System.out.println("unknown animal"); + result = "unknown animal"; + + } + return result; + } + + public String constantCaseValue(String animal) { + + String result = ""; + + final String dog = "DOG"; + + switch (animal) { + + case dog: + result = "domestic animal"; + } + return result; + } + +} diff --git a/core-java/src/main/java/com/baeldung/synthetic/BridgeMethodDemo.java b/core-java/src/main/java/com/baeldung/synthetic/BridgeMethodDemo.java new file mode 100644 index 0000000000..bdf6684f78 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/synthetic/BridgeMethodDemo.java @@ -0,0 +1,23 @@ +package com.baeldung.synthetic; + +import java.util.Comparator; + +/** + * Class which contains a synthetic bridge method. + * + * @author Donato Rimenti + * + */ +public class BridgeMethodDemo implements Comparator { + + /* + * (non-Javadoc) + * + * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) + */ + @Override + public int compare(Integer o1, Integer o2) { + return 0; + } + +} diff --git a/core-java/src/main/java/com/baeldung/synthetic/SyntheticConstructorDemo.java b/core-java/src/main/java/com/baeldung/synthetic/SyntheticConstructorDemo.java new file mode 100644 index 0000000000..d3d75ac05e --- /dev/null +++ b/core-java/src/main/java/com/baeldung/synthetic/SyntheticConstructorDemo.java @@ -0,0 +1,34 @@ +package com.baeldung.synthetic; + +/** + * Wrapper for a class which contains a synthetic constructor. + * + * @author Donato Rimenti + * + */ +public class SyntheticConstructorDemo { + + /** + * We need to instantiate the {@link NestedClass} using a private + * constructor from the enclosing instance in order to generate a synthetic + * constructor. + */ + private NestedClass nestedClass = new NestedClass(); + + /** + * Class which contains a synthetic constructor. + * + * @author Donato Rimenti + * + */ + class NestedClass { + + /** + * In order to generate a synthetic constructor, this class must have a + * private constructor. + */ + private NestedClass() { + } + } + +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/synthetic/SyntheticFieldDemo.java b/core-java/src/main/java/com/baeldung/synthetic/SyntheticFieldDemo.java new file mode 100644 index 0000000000..1813e03953 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/synthetic/SyntheticFieldDemo.java @@ -0,0 +1,22 @@ +package com.baeldung.synthetic; + +/** + * Wrapper for a class which contains a synthetic field reference to the outer + * class. + * + * @author Donato Rimenti + * + */ +public class SyntheticFieldDemo { + + /** + * Class which contains a synthetic field reference to the outer class. + * + * @author Donato Rimenti + * + */ + class NestedClass { + + } + +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/synthetic/SyntheticMethodDemo.java b/core-java/src/main/java/com/baeldung/synthetic/SyntheticMethodDemo.java new file mode 100644 index 0000000000..59be4e1429 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/synthetic/SyntheticMethodDemo.java @@ -0,0 +1,48 @@ +package com.baeldung.synthetic; + +/** + * Wrapper for a class which contains two synthetic methods accessors to a + * private field. + * + * @author Donato Rimenti + * + */ +public class SyntheticMethodDemo { + + /** + * Class which contains two synthetic methods accessors to a private field. + * + * @author Donato Rimenti + * + */ + class NestedClass { + + /** + * Field for which will be generated synthetic methods accessors. It's + * important that this field is private for this purpose. + */ + private String nestedField; + } + + /** + * Gets the private nested field. We need to read the nested field in order + * to generate the synthetic getter. + * + * @return the {@link NestedClass#nestedField} + */ + public String getNestedField() { + return new NestedClass().nestedField; + } + + /** + * Sets the private nested field. We need to write the nested field in order + * to generate the synthetic setter. + * + * @param nestedField + * the {@link NestedClass#nestedField} + */ + public void setNestedField(String nestedField) { + new NestedClass().nestedField = nestedField; + } + +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/zoneddatetime/OffsetDateTimeExample.java b/core-java/src/main/java/com/baeldung/zoneddatetime/OffsetDateTimeExample.java new file mode 100644 index 0000000000..fb92eb8d0d --- /dev/null +++ b/core-java/src/main/java/com/baeldung/zoneddatetime/OffsetDateTimeExample.java @@ -0,0 +1,13 @@ +package com.baeldung.zoneddatetime; + +import java.time.OffsetDateTime; +import java.time.ZoneOffset; + +public class OffsetDateTimeExample { + + public OffsetDateTime getCurrentTimeByZoneOffset(String offset) { + ZoneOffset zoneOffSet= ZoneOffset.of(offset); + OffsetDateTime date = OffsetDateTime.now(zoneOffSet); + return date; + } +} diff --git a/core-java/src/main/java/com/baeldung/zoneddatetime/OffsetTimeExample.java b/core-java/src/main/java/com/baeldung/zoneddatetime/OffsetTimeExample.java new file mode 100644 index 0000000000..58e2d4d5ad --- /dev/null +++ b/core-java/src/main/java/com/baeldung/zoneddatetime/OffsetTimeExample.java @@ -0,0 +1,13 @@ +package com.baeldung.zoneddatetime; + +import java.time.OffsetTime; +import java.time.ZoneOffset; + +public class OffsetTimeExample { + + public OffsetTime getCurrentTimeByZoneOffset(String offset) { + ZoneOffset zoneOffSet = ZoneOffset.of(offset); + OffsetTime time = OffsetTime.now(zoneOffSet); + return time; + } +} diff --git a/core-java/src/main/java/com/baeldung/zoneddatetime/ZoneDateTimeExample.java b/core-java/src/main/java/com/baeldung/zoneddatetime/ZoneDateTimeExample.java new file mode 100644 index 0000000000..b54b8c5225 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/zoneddatetime/ZoneDateTimeExample.java @@ -0,0 +1,21 @@ +package com.baeldung.zoneddatetime; + +import java.time.ZoneId; +import java.time.ZonedDateTime; + +public class ZoneDateTimeExample { + + public ZonedDateTime getCurrentTimeByZoneId(String region) { + ZoneId zone = ZoneId.of(region); + ZonedDateTime date = ZonedDateTime.now(zone); + return date; + } + + public ZonedDateTime convertZonedDateTime(ZonedDateTime sourceDate, String destZone) { + + ZoneId destZoneId = ZoneId.of(destZone); + ZonedDateTime destDate = sourceDate.withZoneSameInstant(destZoneId); + + return destDate; + } +} diff --git a/core-java/src/test/java/com/baeldung/passwordhashing/PBKDF2HasherUnitTest.java b/core-java/src/test/java/com/baeldung/passwordhashing/PBKDF2HasherUnitTest.java new file mode 100644 index 0000000000..8e90725c77 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/passwordhashing/PBKDF2HasherUnitTest.java @@ -0,0 +1,41 @@ +package com.baeldung.passwordhashing; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + + +public class PBKDF2HasherUnitTest { + + private PBKDF2Hasher mPBKDF2Hasher; + + @Before + public void setUp() throws Exception { + mPBKDF2Hasher = new PBKDF2Hasher(); + } + + @Test + public void givenCorrectMessageAndHash_whenAuthenticated_checkAuthenticationSucceeds() throws Exception { + String message1 = "password123"; + + String hash1 = mPBKDF2Hasher.hash(message1.toCharArray()); + + assertTrue(mPBKDF2Hasher.checkPassword(message1.toCharArray(), hash1)); + + } + + @Test + public void givenWrongMessage_whenAuthenticated_checkAuthenticationFails() throws Exception { + String message1 = "password123"; + + String hash1 = mPBKDF2Hasher.hash(message1.toCharArray()); + + String wrongPasswordAttempt = "IamWrong"; + + assertFalse(mPBKDF2Hasher.checkPassword(wrongPasswordAttempt.toCharArray(), hash1)); + + } + + +} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/passwordhashing/SHA512HasherUnitTest.java b/core-java/src/test/java/com/baeldung/passwordhashing/SHA512HasherUnitTest.java new file mode 100644 index 0000000000..3acfb0ba9d --- /dev/null +++ b/core-java/src/test/java/com/baeldung/passwordhashing/SHA512HasherUnitTest.java @@ -0,0 +1,70 @@ +package com.baeldung.passwordhashing; + +import org.junit.Before; +import org.junit.Test; + +import java.security.SecureRandom; + +import static org.junit.Assert.*; + +/** + * Created by PhysicsSam on 06-Sep-18. + */ +public class SHA512HasherUnitTest { + + private SHA512Hasher hasher; + private SecureRandom secureRandom; + + @Before + public void setUp() throws Exception { + hasher = new SHA512Hasher(); + secureRandom = new SecureRandom(); + } + + @Test + public void givenSamePasswordAndSalt_whenHashed_checkResultingHashesAreEqual() throws Exception { + + byte[] salt = new byte[16]; + secureRandom.nextBytes(salt); + + String hash1 = hasher.hash("password", salt); + String hash2 = hasher.hash("password", salt); + + assertEquals(hash1, hash2); + + } + + @Test + public void givenSamePasswordAndDifferentSalt_whenHashed_checkResultingHashesNotEqual() throws Exception { + + byte[] salt = new byte[16]; + secureRandom.nextBytes(salt); + String hash1 = hasher.hash("password", salt); + //generate a second salt + byte[] secondSalt = new byte[16]; + String hash2 = hasher.hash("password", secondSalt); + + assertNotEquals(hash1, hash2); + + } + + @Test + public void givenPredefinedHash_whenCorrectAttemptGiven_checkAuthenticationSucceeds() throws Exception { + byte[] salt = new byte[16]; + secureRandom.nextBytes(salt); + + String originalHash = hasher.hash("password123", salt); + + assertTrue(hasher.checkPassword(originalHash, "password123", salt)); + } + + @Test + public void givenPredefinedHash_whenIncorrectAttemptGiven_checkAuthenticationFails() throws Exception { + byte[] salt = new byte[16]; + secureRandom.nextBytes(salt); + + String originalHash = hasher.hash("password123", salt); + + assertFalse(hasher.checkPassword(originalHash, "password124", salt)); + } +} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/removingdecimals/RemovingDecimalsManualTest.java b/core-java/src/test/java/com/baeldung/removingdecimals/RemovingDecimalsManualTest.java new file mode 100644 index 0000000000..8dbfcaf5e7 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/removingdecimals/RemovingDecimalsManualTest.java @@ -0,0 +1,100 @@ +package com.baeldung.removingdecimals; + +import org.junit.Test; +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.util.concurrent.TimeUnit; + +/** + * This benchmark compares some of the approaches to formatting a floating-point + * value into a {@link String} while removing the decimal part. + * + * To run, simply run the {@link RemovingDecimalsManualTest#runBenchmarks()} test + * at the end of this class. + * + * The benchmark takes about 15 minutes to run. Since it is using {@link Mode#Throughput}, + * higher numbers mean better performance. + */ +@BenchmarkMode(Mode.Throughput) +@Warmup(iterations = 5) +@Measurement(iterations = 20) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@State(Scope.Benchmark) +public class RemovingDecimalsManualTest { + @Param(value = {"345.56", "345345345.56", "345345345345345345.56"}) double doubleValue; + + NumberFormat nf; + DecimalFormat df; + + @Setup + public void readyFormatters() { + nf = NumberFormat.getInstance(); + nf.setMaximumFractionDigits(0); + df = new DecimalFormat("#,###"); + } + + @Benchmark + public String whenCastToInt_thenValueIsTruncated() { + return String.valueOf((int) doubleValue); + } + + @Benchmark + public String whenUsingStringFormat_thenValueIsRounded() { + return String.format("%.0f", doubleValue); + } + + @Benchmark + public String whenUsingNumberFormat_thenValueIsRounded() { + nf.setRoundingMode(RoundingMode.HALF_UP); + return nf.format(doubleValue); + } + + @Benchmark + public String whenUsingNumberFormatWithFloor_thenValueIsTruncated() { + nf.setRoundingMode(RoundingMode.FLOOR); + return nf.format(doubleValue); + } + + @Benchmark + public String whenUsingDecimalFormat_thenValueIsRounded() { + df.setRoundingMode(RoundingMode.HALF_UP); + return df.format(doubleValue); + } + + @Benchmark + public String whenUsingDecimalFormatWithFloor_thenValueIsTruncated() { + df.setRoundingMode(RoundingMode.FLOOR); + return df.format(doubleValue); + } + + @Benchmark + public String whenUsingBigDecimalDoubleValue_thenValueIsTruncated() { + BigDecimal big = new BigDecimal(doubleValue); + big = big.setScale(0, RoundingMode.FLOOR); + return big.toString(); + } + + @Benchmark + public String whenUsingBigDecimalDoubleValueWithHalfUp_thenValueIsRounded() { + BigDecimal big = new BigDecimal(doubleValue); + big = big.setScale(0, RoundingMode.HALF_UP); + return big.toString(); + } + + @Test + public void runBenchmarks() throws Exception { + Options options = new OptionsBuilder() + .include(this.getClass().getSimpleName()).threads(1) + .forks(1).shouldFailOnError(true).shouldDoGC(true) + .jvmArgs("-server").build(); + + new Runner(options).run(); + } +} diff --git a/core-java/src/test/java/com/baeldung/removingdecimals/RemovingDecimalsUnitTest.java b/core-java/src/test/java/com/baeldung/removingdecimals/RemovingDecimalsUnitTest.java new file mode 100644 index 0000000000..2f634b553b --- /dev/null +++ b/core-java/src/test/java/com/baeldung/removingdecimals/RemovingDecimalsUnitTest.java @@ -0,0 +1,95 @@ +package com.baeldung.removingdecimals; + +import org.junit.Test; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.text.DecimalFormat; +import java.text.NumberFormat; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +/** + * Tests that demonstrate some different approaches for formatting a + * floating-point value into a {@link String} while removing the decimal part. + */ +public class RemovingDecimalsUnitTest { + private final double doubleValue = 345.56; + + @Test + public void whenCastToInt_thenValueIsTruncated() { + String truncated = String.valueOf((int) doubleValue); + assertEquals("345", truncated); + } + + @Test + public void givenALargeDouble_whenCastToInt_thenValueIsNotTruncated() { + double outOfIntRange = 6_000_000_000.56; + String truncationAttempt = String.valueOf((int) outOfIntRange); + assertNotEquals("6000000000", truncationAttempt); + } + + @Test + public void whenUsingStringFormat_thenValueIsRounded() { + String rounded = String.format("%.0f", doubleValue); + assertEquals("346", rounded); + } + + @Test + public void givenALargeDouble_whenUsingStringFormat_thenValueIsStillRounded() { + double outOfIntRange = 6_000_000_000.56; + String rounded = String.format("%.0f", outOfIntRange); + assertEquals("6000000001", rounded); + } + + @Test + public void whenUsingNumberFormat_thenValueIsRounded() { + NumberFormat nf = NumberFormat.getInstance(); + nf.setMaximumFractionDigits(0); + nf.setRoundingMode(RoundingMode.HALF_UP); + String rounded = nf.format(doubleValue); + assertEquals("346", rounded); + } + + @Test + public void whenUsingNumberFormatWithFloor_thenValueIsTruncated() { + NumberFormat nf = NumberFormat.getInstance(); + nf.setMaximumFractionDigits(0); + nf.setRoundingMode(RoundingMode.FLOOR); + String truncated = nf.format(doubleValue); + assertEquals("345", truncated); + } + + @Test + public void whenUsingDecimalFormat_thenValueIsRounded() { + DecimalFormat df = new DecimalFormat("#,###"); + df.setRoundingMode(RoundingMode.HALF_UP); + String rounded = df.format(doubleValue); + assertEquals("346", rounded); + } + + @Test + public void whenUsingDecimalFormatWithFloor_thenValueIsTruncated() { + DecimalFormat df = new DecimalFormat("#,###"); + df.setRoundingMode(RoundingMode.FLOOR); + String truncated = df.format(doubleValue); + assertEquals("345", truncated); + } + + @Test + public void whenUsingBigDecimalDoubleValue_thenValueIsTruncated() { + BigDecimal big = new BigDecimal(doubleValue); + big = big.setScale(0, RoundingMode.FLOOR); + String truncated = big.toString(); + assertEquals("345", truncated); + } + + @Test + public void whenUsingBigDecimalDoubleValueWithHalfUp_thenValueIsRounded() { + BigDecimal big = new BigDecimal(doubleValue); + big = big.setScale(0, RoundingMode.HALF_UP); + String truncated = big.toString(); + assertEquals("346", truncated); + } +} diff --git a/core-java/src/test/java/com/baeldung/switchstatement/SwitchStatementUnitTest.java b/core-java/src/test/java/com/baeldung/switchstatement/SwitchStatementUnitTest.java new file mode 100644 index 0000000000..e8ac645531 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/switchstatement/SwitchStatementUnitTest.java @@ -0,0 +1,38 @@ +package com.baeldung.switchstatement; + +import org.junit.Test; + +import org.junit.Assert; + +public class SwitchStatementUnitTest { + private SwitchStatement s = new SwitchStatement(); + + + @Test + public void whenDog_thenDomesticAnimal() { + + String animal = "DOG"; + Assert.assertEquals("domestic animal", s.exampleOfSwitch(animal)); + } + + @Test + public void whenNoBreaks_thenGoThroughBlocks() { + String animal = "DOG"; + Assert.assertEquals("unknown animal", s.forgetBreakInSwitch(animal)); + } + + @Test(expected=NullPointerException.class) + public void whenSwitchAgumentIsNull_thenNullPointerException() { + String animal = null; + Assert.assertEquals("domestic animal", s.exampleOfSwitch(animal)); + } + + + @Test + public void whenCompareStrings_thenByEqual() { + String animal = new String("DOG"); + Assert.assertEquals("domestic animal", s.exampleOfSwitch(animal)); + } + + +} diff --git a/core-java/src/test/java/com/baeldung/synthetic/SyntheticUnitTest.java b/core-java/src/test/java/com/baeldung/synthetic/SyntheticUnitTest.java new file mode 100644 index 0000000000..20f7647f48 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/synthetic/SyntheticUnitTest.java @@ -0,0 +1,99 @@ +package com.baeldung.synthetic; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Unit test for {@link SyntheticFieldDemo}, {@link SyntheticMethodDemo}, + * {@link SyntheticConstructorDemo} and {@link BridgeMethodDemo} classes. + * + * @author Donato Rimenti + * + */ +public class SyntheticUnitTest { + + /** + * Tests that the {@link SyntheticMethodDemo.NestedClass} contains two synthetic + * methods. + */ + @Test + public void givenSyntheticMethod_whenIsSinthetic_thenTrue() { + // Checks that the nested class contains exactly two synthetic methods. + Method[] methods = SyntheticMethodDemo.NestedClass.class.getDeclaredMethods(); + Assert.assertEquals("This class should contain only two methods", 2, methods.length); + + for (Method m : methods) { + System.out.println("Method: " + m.getName() + ", isSynthetic: " + m.isSynthetic()); + Assert.assertTrue("All the methods of this class should be synthetic", m.isSynthetic()); + } + } + + /** + * Tests that {@link SyntheticConstructorDemo.NestedClass} contains a synthetic + * constructor. + */ + @Test + public void givenSyntheticConstructor_whenIsSinthetic_thenTrue() { + // Checks that the nested class contains exactly a synthetic + // constructor. + int syntheticConstructors = 0; + Constructor[] constructors = SyntheticConstructorDemo.NestedClass.class.getDeclaredConstructors(); + Assert.assertEquals("This class should contain only two constructors", 2, constructors.length); + + for (Constructor c : constructors) { + System.out.println("Constructor: " + c.getName() + ", isSynthetic: " + c.isSynthetic()); + + // Counts the synthetic constructors. + if (c.isSynthetic()) { + syntheticConstructors++; + } + } + + // Checks that there's exactly one synthetic constructor. + Assert.assertEquals(1, syntheticConstructors); + } + + /** + * Tests that {@link SyntheticFieldDemo.NestedClass} contains a synthetic field. + */ + @Test + public void givenSyntheticField_whenIsSinthetic_thenTrue() { + // This class should contain exactly one synthetic field. + Field[] fields = SyntheticFieldDemo.NestedClass.class.getDeclaredFields(); + Assert.assertEquals("This class should contain only one field", 1, fields.length); + + for (Field f : fields) { + System.out.println("Field: " + f.getName() + ", isSynthetic: " + f.isSynthetic()); + Assert.assertTrue("All the fields of this class should be synthetic", f.isSynthetic()); + } + } + + /** + * Tests that {@link BridgeMethodDemo} contains a synthetic bridge method. + */ + @Test + public void givenBridgeMethod_whenIsBridge_thenTrue() { + // This class should contain exactly one synthetic bridge method. + int syntheticMethods = 0; + Method[] methods = BridgeMethodDemo.class.getDeclaredMethods(); + for (Method m : methods) { + System.out.println( + "Method: " + m.getName() + ", isSynthetic: " + m.isSynthetic() + ", isBridge: " + m.isBridge()); + + // Counts the synthetic methods and checks that they are also bridge + // methods. + if (m.isSynthetic()) { + syntheticMethods++; + Assert.assertTrue("The synthetic method in this class should also be a bridge method", m.isBridge()); + } + } + + // Checks that there's exactly one synthetic bridge method. + Assert.assertEquals("There should be exactly 1 synthetic bridge method in this class", 1, syntheticMethods); + } + +} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/zoneddatetime/OffsetDateTimeExampleUnitTest.java b/core-java/src/test/java/com/baeldung/zoneddatetime/OffsetDateTimeExampleUnitTest.java new file mode 100644 index 0000000000..a08d3737cd --- /dev/null +++ b/core-java/src/test/java/com/baeldung/zoneddatetime/OffsetDateTimeExampleUnitTest.java @@ -0,0 +1,22 @@ +package com.baeldung.zoneddatetime; + +import static org.junit.Assert.assertTrue; + +import java.time.OffsetDateTime; +import java.time.ZoneOffset; + +import org.junit.Test; + +public class OffsetDateTimeExampleUnitTest { + + OffsetDateTimeExample offsetDateTimeExample = new OffsetDateTimeExample(); + + @Test + public void givenZoneOffset_whenGetCurrentTime_thenResultHasZone() { + String offset = "+02:00"; + OffsetDateTime time = offsetDateTimeExample.getCurrentTimeByZoneOffset(offset); + + assertTrue(time.getOffset() + .equals(ZoneOffset.of(offset))); + } +} diff --git a/core-java/src/test/java/com/baeldung/zoneddatetime/OffsetTimeExampleUnitTest.java b/core-java/src/test/java/com/baeldung/zoneddatetime/OffsetTimeExampleUnitTest.java new file mode 100644 index 0000000000..488f934179 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/zoneddatetime/OffsetTimeExampleUnitTest.java @@ -0,0 +1,22 @@ +package com.baeldung.zoneddatetime; + +import static org.junit.Assert.assertTrue; + +import java.time.OffsetTime; +import java.time.ZoneOffset; + +import org.junit.Test; + +public class OffsetTimeExampleUnitTest { + + OffsetTimeExample offsetTimeExample = new OffsetTimeExample(); + + @Test + public void givenZoneOffset_whenGetCurrentTime_thenResultHasZone() { + String offset = "+02:00"; + OffsetTime time = offsetTimeExample.getCurrentTimeByZoneOffset(offset); + + assertTrue(time.getOffset() + .equals(ZoneOffset.of(offset))); + } +} diff --git a/core-java/src/test/java/com/baeldung/zoneddatetime/ZoneDateTimeExampleUnitTest.java b/core-java/src/test/java/com/baeldung/zoneddatetime/ZoneDateTimeExampleUnitTest.java new file mode 100644 index 0000000000..e78ff3e3fd --- /dev/null +++ b/core-java/src/test/java/com/baeldung/zoneddatetime/ZoneDateTimeExampleUnitTest.java @@ -0,0 +1,33 @@ +package com.baeldung.zoneddatetime; + +import static org.junit.Assert.assertTrue; + +import java.time.ZoneId; +import java.time.ZonedDateTime; + +import org.junit.Test; + +public class ZoneDateTimeExampleUnitTest { + + ZoneDateTimeExample zoneDateTimeExample = new ZoneDateTimeExample(); + + @Test + public void givenZone_whenGetCurrentTime_thenResultHasZone() { + String zone = "Europe/Berlin"; + ZonedDateTime time = zoneDateTimeExample.getCurrentTimeByZoneId(zone); + + assertTrue(time.getZone() + .equals(ZoneId.of(zone))); + } + + @Test + public void givenZones_whenConvertDateByZone_thenGetConstantDiff() { + String sourceZone = "Europe/Berlin"; + String destZone = "Asia/Tokyo"; + ZonedDateTime sourceDate = zoneDateTimeExample.getCurrentTimeByZoneId(sourceZone); + ZonedDateTime destDate = zoneDateTimeExample.convertZonedDateTime(sourceDate, destZone); + + assertTrue(sourceDate.toInstant() + .compareTo(destDate.toInstant()) == 0); + } +} diff --git a/core-kotlin/README.md b/core-kotlin/README.md index 734b2c08b3..f63451bc02 100644 --- a/core-kotlin/README.md +++ b/core-kotlin/README.md @@ -8,7 +8,6 @@ - [Generics in Kotlin](http://www.baeldung.com/kotlin-generics) - [Introduction to Kotlin Coroutines](http://www.baeldung.com/kotlin-coroutines) - [Destructuring Declarations in Kotlin](http://www.baeldung.com/kotlin-destructuring-declarations) -- [Kotlin with Mockito](http://www.baeldung.com/kotlin-mockito) - [Lazy Initialization in Kotlin](http://www.baeldung.com/kotlin-lazy-initialization) - [Overview of Kotlin Collections API](http://www.baeldung.com/kotlin-collections-api) - [Converting a List to Map in Kotlin](http://www.baeldung.com/kotlin-list-to-map) @@ -19,8 +18,6 @@ - [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) - [Regular Expressions in Kotlin](http://www.baeldung.com/kotlin-regular-expressions) - [Objects in Kotlin](http://www.baeldung.com/kotlin-objects) - [Reading from a File in Kotlin](http://www.baeldung.com/kotlin-read-file) @@ -28,13 +25,12 @@ - [Filtering Kotlin Collections](http://www.baeldung.com/kotlin-filter-collection) - [Writing to a File in Kotlin](http://www.baeldung.com/kotlin-write-file) - [Lambda Expressions in Kotlin](http://www.baeldung.com/kotlin-lambda-expressions) -- [Writing Specifications with Kotlin and Spek](http://www.baeldung.com/kotlin-spek) -- [Processing JSON with Kotlin and Klaxson](http://www.baeldung.com/kotlin-json-klaxson) - [Kotlin String Templates](http://www.baeldung.com/kotlin-string-template) -- [Java EE 8 Security API](http://www.baeldung.com/java-ee-8-security) -- [Kotlin with Ktor](http://www.baeldung.com/kotlin-ktor) - [Working with Enums in Kotlin](http://www.baeldung.com/kotlin-enum) - [Create a Java and Kotlin Project with Maven](http://www.baeldung.com/kotlin-maven-java-project) - [Reflection with Kotlin](http://www.baeldung.com/kotlin-reflection) - [Get a Random Number in Kotlin](http://www.baeldung.com/kotlin-random-number) - [Idiomatic Logging in Kotlin](http://www.baeldung.com/kotlin-logging) +- [Kotlin Constructors](https://www.baeldung.com/kotlin-constructors) +- [Creational Design Patterns in Kotlin: Builder](https://www.baeldung.com/kotlin-builder-pattern) +- [Kotlin Nested and Inner Classes](https://www.baeldung.com/kotlin-inner-classes) \ No newline at end of file diff --git a/core-kotlin/build.gradle b/core-kotlin/build.gradle index 6c1e06aa25..2b6527fca7 100755 --- a/core-kotlin/build.gradle +++ b/core-kotlin/build.gradle @@ -6,7 +6,6 @@ version '1.0-SNAPSHOT' buildscript { ext.kotlin_version = '1.2.41' - ext.ktor_version = '0.9.2' repositories { mavenCentral() @@ -44,14 +43,6 @@ sourceSets { } dependencies { - compile "io.ktor:ktor-server-netty:$ktor_version" compile "ch.qos.logback:logback-classic:1.2.1" - compile "io.ktor:ktor-gson:$ktor_version" testCompile group: 'junit', name: 'junit', version: '4.12' - implementation 'com.beust:klaxon:3.0.1' - -} -task runServer(type: JavaExec) { - main = 'APIServer' - classpath = sourceSets.main.runtimeClasspath } \ No newline at end of file diff --git a/core-kotlin/kotlin-ktor/webapp/WEB-INF/web.xml b/core-kotlin/kotlin-ktor/webapp/WEB-INF/web.xml deleted file mode 100755 index 513a80cb27..0000000000 --- a/core-kotlin/kotlin-ktor/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - io.ktor.ktor.config - application.conf - - - - KtorServlet - KtorServlet - io.ktor.server.servlet.ServletApplicationEngine - - - true - - - - 304857600 - 304857600 - 0 - - - - - KtorServlet - / - - - \ No newline at end of file diff --git a/core-kotlin/pom.xml b/core-kotlin/pom.xml index 6fdc7c7c1a..0894a57320 100644 --- a/core-kotlin/pom.xml +++ b/core-kotlin/pom.xml @@ -12,33 +12,7 @@ ../parent-kotlin - - - exposed - exposed - https://dl.bintray.com/kotlin/exposed - - - - - org.jetbrains.spek - spek-api - 1.1.5 - test - - - org.jetbrains.spek - spek-subject-extension - 1.1.5 - test - - - org.jetbrains.spek - spek-junit-platform-engine - 1.1.5 - test - org.apache.commons commons-math3 @@ -50,56 +24,52 @@ ${junit.platform.version} test - - khttp - khttp - ${khttp.version} - - - com.nhaarman - mockito-kotlin - ${mockito-kotlin.version} - test - - - com.github.salomonbrys.kodein - kodein - ${kodein.version} - org.assertj assertj-core ${assertj.version} test - - com.beust - klaxon - ${klaxon.version} - - - org.jetbrains.exposed - exposed - ${exposed.version} - com.h2database h2 ${h2database.version} + + com.github.kittinunf.fuel + fuel + ${fuel.version} + + + com.github.kittinunf.fuel + fuel-gson + ${fuel.version} + + + com.github.kittinunf.fuel + fuel-rxjava + ${fuel.version} + + + com.github.kittinunf.fuel + fuel-coroutines + ${fuel.version} + + + nl.komponents.kovenant + kovenant + 3.3.0 + pom + - 1.5.0 - 4.1.0 - 3.0.4 - 0.1.0 3.6.1 1.1.1 5.2.0 3.10.0 1.4.197 - 0.10.4 + 1.15.0 - \ No newline at end of file + diff --git a/core-kotlin/src/main/kotlin/com/baeldung/fuel/Interceptors.kt b/core-kotlin/src/main/kotlin/com/baeldung/fuel/Interceptors.kt new file mode 100644 index 0000000000..377ef979dc --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/fuel/Interceptors.kt @@ -0,0 +1,11 @@ +package com.baeldung.fuel + +import com.github.kittinunf.fuel.core.Request + +fun tokenInterceptor() = { + next: (Request) -> Request -> + { req: Request -> + req.header(mapOf("Authorization" to "Bearer AbCdEf123456")) + next(req) + } +} \ No newline at end of file diff --git a/core-kotlin/src/main/kotlin/com/baeldung/fuel/Post.kt b/core-kotlin/src/main/kotlin/com/baeldung/fuel/Post.kt new file mode 100644 index 0000000000..035dfe7aa0 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/fuel/Post.kt @@ -0,0 +1,15 @@ +package com.baeldung.fuel + +import com.github.kittinunf.fuel.core.ResponseDeserializable +import com.google.gson.Gson + +data class Post(var userId:Int, + var id:Int, + var title:String, + var body:String){ + + + class Deserializer : ResponseDeserializable> { + override fun deserialize(content: String): Array = Gson().fromJson(content, Array::class.java) + } +} \ No newline at end of file diff --git a/core-kotlin/src/main/kotlin/com/baeldung/fuel/PostRoutingAPI.kt b/core-kotlin/src/main/kotlin/com/baeldung/fuel/PostRoutingAPI.kt new file mode 100644 index 0000000000..8238c41e56 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/fuel/PostRoutingAPI.kt @@ -0,0 +1,42 @@ +package com.baeldung.fuel + +import com.github.kittinunf.fuel.core.Method +import com.github.kittinunf.fuel.util.FuelRouting + +sealed class PostRoutingAPI : FuelRouting { + + override val basePath = "https://jsonplaceholder.typicode.com" + + class posts(val id: String, override val body: String?): PostRoutingAPI() + + class comments(val postId: String, override val body: String?): PostRoutingAPI() + + override val method: Method + get() { + return when(this) { + is PostRoutingAPI.posts -> Method.GET + is PostRoutingAPI.comments -> Method.GET + } + } + + override val path: String + get() { + return when(this) { + is PostRoutingAPI.posts -> "/posts" + is PostRoutingAPI.comments -> "/comments" + } + } + + override val params: List>? + get() { + return when(this) { + is PostRoutingAPI.posts -> listOf("id" to this.id) + is PostRoutingAPI.comments -> listOf("postId" to this.postId) + } + } + + override val headers: Map? + get() { + return null + } +} diff --git a/core-kotlin/src/main/kotlin/com/baeldung/ktor/APIServer.kt b/core-kotlin/src/main/kotlin/com/baeldung/ktor/APIServer.kt deleted file mode 100755 index a12182ccc8..0000000000 --- a/core-kotlin/src/main/kotlin/com/baeldung/ktor/APIServer.kt +++ /dev/null @@ -1,73 +0,0 @@ -@file:JvmName("APIServer") - - -import io.ktor.application.call -import io.ktor.application.install -import io.ktor.features.CallLogging -import io.ktor.features.ContentNegotiation -import io.ktor.features.DefaultHeaders -import io.ktor.gson.gson -import io.ktor.request.path -import io.ktor.request.receive -import io.ktor.response.respond -import io.ktor.routing.* -import io.ktor.server.engine.embeddedServer -import io.ktor.server.netty.Netty -import org.slf4j.event.Level - -data class Author(val name: String, val website: String) -data class ToDo(var id: Int, val name: String, val description: String, val completed: Boolean) - -fun main(args: Array) { - - val toDoList = ArrayList(); - val jsonResponse = """{ - "id": 1, - "task": "Pay waterbill", - "description": "Pay water bill today", - }""" - - - embeddedServer(Netty, 8080) { - install(DefaultHeaders) { - header("X-Developer", "Baeldung") - } - install(CallLogging) { - level = Level.DEBUG - filter { call -> call.request.path().startsWith("/todo") } - filter { call -> call.request.path().startsWith("/author") } - } - install(ContentNegotiation) { - gson { - setPrettyPrinting() - } - } - routing() { - route("/todo") { - post { - var toDo = call.receive(); - toDo.id = toDoList.size; - toDoList.add(toDo); - call.respond("Added") - - } - delete("/{id}") { - call.respond(toDoList.removeAt(call.parameters["id"]!!.toInt())); - } - get("/{id}") { - - call.respond(toDoList[call.parameters["id"]!!.toInt()]); - } - get { - call.respond(toDoList); - } - } - get("/author"){ - call.respond(Author("Baeldung","baeldung.com")); - - } - - - } - }.start(wait = true) -} \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/builder/BuilderPatternUnitTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/builder/BuilderPatternUnitTest.kt index 17f5a43479..a6687b6e0a 100644 --- a/core-kotlin/src/test/kotlin/com/baeldung/builder/BuilderPatternUnitTest.kt +++ b/core-kotlin/src/test/kotlin/com/baeldung/builder/BuilderPatternUnitTest.kt @@ -93,6 +93,7 @@ internal class BuilderPatternUnitTest { Assertions.assertNull(foodOrder.fish) } + @Test fun whenBuildingFoodOrderApplySettingValues_thenFieldsNotNull() { diff --git a/core-kotlin/src/test/kotlin/com/baeldung/fuel/FuelHttpUnitTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/fuel/FuelHttpUnitTest.kt new file mode 100644 index 0000000000..f0f9267618 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/fuel/FuelHttpUnitTest.kt @@ -0,0 +1,286 @@ +package com.baeldung.fuel + +import awaitObjectResult +import awaitStringResponse +import com.github.kittinunf.fuel.Fuel +import com.github.kittinunf.fuel.core.FuelManager +import com.github.kittinunf.fuel.core.Request +import com.github.kittinunf.fuel.core.interceptors.cUrlLoggingRequestInterceptor +import com.github.kittinunf.fuel.gson.responseObject +import com.github.kittinunf.fuel.httpGet +import com.github.kittinunf.fuel.rx.rx_object +import com.google.gson.Gson +import kotlinx.coroutines.experimental.runBlocking +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test +import java.io.File +import java.util.concurrent.CountDownLatch + +internal class FuelHttpUnitTest { + + @Test + fun whenMakingAsyncHttpGetRequest_thenResponseNotNullAndErrorNullAndStatusCode200() { + + val latch = CountDownLatch(1) + + "http://httpbin.org/get".httpGet().response{ + request, response, result -> + + val (data, error) = result + + Assertions.assertNull(error) + Assertions.assertNotNull(data) + Assertions.assertEquals(200,response.statusCode) + + latch.countDown() + } + + latch.await() + + } + + @Test + fun whenMakingSyncHttpGetRequest_thenResponseNotNullAndErrorNullAndStatusCode200() { + + val (request, response, result) = "http://httpbin.org/get".httpGet().response() + val (data, error) = result + + Assertions.assertNull(error) + Assertions.assertNotNull(data) + Assertions.assertEquals(200,response.statusCode) + + } + + @Test + fun whenMakingSyncHttpGetURLEncodedRequest_thenResponseNotNullAndErrorNullAndStatusCode200() { + + val (request, response, result) = + "https://jsonplaceholder.typicode.com/posts" + .httpGet(listOf("id" to "1")).response() + val (data, error) = result + + Assertions.assertNull(error) + Assertions.assertNotNull(data) + Assertions.assertEquals(200,response.statusCode) + + } + + @Test + fun whenMakingAsyncHttpPostRequest_thenResponseNotNullAndErrorNullAndStatusCode200() { + + val latch = CountDownLatch(1) + + Fuel.post("http://httpbin.org/post").response{ + request, response, result -> + + val (data, error) = result + + Assertions.assertNull(error) + Assertions.assertNotNull(data) + Assertions.assertEquals(200,response.statusCode) + + latch.countDown() + } + + latch.await() + + } + + @Test + fun whenMakingSyncHttpPostRequest_thenResponseNotNullAndErrorNullAndStatusCode200() { + + val (request, response, result) = Fuel.post("http://httpbin.org/post").response() + val (data, error) = result + + Assertions.assertNull(error) + Assertions.assertNotNull(data) + Assertions.assertEquals(200,response.statusCode) + } + + @Test + fun whenMakingSyncHttpPostRequestwithBody_thenResponseNotNullAndErrorNullAndStatusCode200() { + + val (request, response, result) = Fuel.post("https://jsonplaceholder.typicode.com/posts") + .body("{ \"title\" : \"foo\",\"body\" : \"bar\",\"id\" : \"1\"}") + .response() + + val (data, error) = result + + Assertions.assertNull(error) + Assertions.assertNotNull(data) + Assertions.assertEquals(201,response.statusCode) + } + + @Test + fun givenFuelInstance_whenMakingSyncHttpGetRequest_thenResponseNotNullAndErrorNullAndStatusCode200() { + + FuelManager.instance.basePath = "http://httpbin.org" + FuelManager.instance.baseHeaders = mapOf("OS" to "macOS High Sierra") + + FuelManager.instance.addRequestInterceptor(cUrlLoggingRequestInterceptor()) + FuelManager.instance.addRequestInterceptor(tokenInterceptor()) + + + val (request, response, result) = "/get" + .httpGet().response() + val (data, error) = result + + Assertions.assertNull(error) + Assertions.assertNotNull(data) + Assertions.assertEquals(200,response.statusCode) + } + + @Test + fun givenInterceptors_whenMakingSyncHttpGetRequest_thenResponseNotNullAndErrorNullAndStatusCode200() { + + FuelManager.instance.basePath = "http://httpbin.org" + FuelManager.instance.addRequestInterceptor(cUrlLoggingRequestInterceptor()) + FuelManager.instance.addRequestInterceptor(tokenInterceptor()) + + val (request, response, result) = "/get" + .httpGet().response() + val (data, error) = result + + Assertions.assertNull(error) + Assertions.assertNotNull(data) + Assertions.assertEquals(200,response.statusCode) + } + + @Test + fun whenDownloadFile_thenCreateFileResponseNotNullAndErrorNullAndStatusCode200() { + + Fuel.download("http://httpbin.org/bytes/32768").destination { response, url -> + File.createTempFile("temp", ".tmp") + }.response{ + request, response, result -> + + val (data, error) = result + Assertions.assertNull(error) + Assertions.assertNotNull(data) + Assertions.assertEquals(200,response.statusCode) + } + } + + @Test + fun whenDownloadFilewithProgressHandler_thenCreateFileResponseNotNullAndErrorNullAndStatusCode200() { + + val (request, response, result) = Fuel.download("http://httpbin.org/bytes/327680") + .destination { response, url -> File.createTempFile("temp", ".tmp") + }.progress { readBytes, totalBytes -> + val progress = readBytes.toFloat() / totalBytes.toFloat() + }.response () + + val (data, error) = result + Assertions.assertNull(error) + Assertions.assertNotNull(data) + Assertions.assertEquals(200,response.statusCode) + + + } + + @Test + fun whenMakeGetRequest_thenDeserializePostwithGson() { + + val latch = CountDownLatch(1) + + "https://jsonplaceholder.typicode.com/posts/1".httpGet().responseObject { _,_, result -> + val post = result.component1() + Assertions.assertEquals(1, post?.userId) + latch.countDown() + } + + latch.await() + + } + + @Test + fun whenMakePOSTRequest_thenSerializePostwithGson() { + + val post = Post(1,1, "Lorem", "Lorem Ipse dolor sit amet") + + val (request, response, result) = Fuel.post("https://jsonplaceholder.typicode.com/posts") + .header("Content-Type" to "application/json") + .body(Gson().toJson(post).toString()) + .response() + + Assertions.assertEquals(201,response.statusCode) + + } + + @Test + fun whenMakeGETRequestWithRxJava_thenDeserializePostwithGson() { + + val latch = CountDownLatch(1) + + + "https://jsonplaceholder.typicode.com/posts?id=1" + .httpGet().rx_object(Post.Deserializer()).subscribe{ + res, throwable -> + + val post = res.component1() + Assertions.assertEquals(1, post?.get(0)?.userId) + latch.countDown() + } + + latch.await() + + } + + @Test + fun whenMakeGETRequestUsingCoroutines_thenResponseStatusCode200() { + + runBlocking { + val (request, response, result) = Fuel.get("http://httpbin.org/get").awaitStringResponse() + + result.fold({ data -> + Assertions.assertEquals(200, response.statusCode) + + }, { error -> }) + } + + } + + @Test + fun whenMakeGETRequestUsingCoroutines_thenDeserializeResponse() { + + + runBlocking { + Fuel.get("https://jsonplaceholder.typicode.com/posts?id=1").awaitObjectResult(Post.Deserializer()) + .fold({ data -> + Assertions.assertEquals(1, data.get(0).userId) + }, { error -> }) + } + + } + + @Test + fun whenMakeGETPostRequestUsingRoutingAPI_thenDeserializeResponse() { + + val latch = CountDownLatch(1) + + Fuel.request(PostRoutingAPI.posts("1",null)) + .responseObject(Post.Deserializer()) { + request, response, result -> + Assertions.assertEquals(1, result.component1()?.get(0)?.userId) + latch.countDown() + } + + latch.await() + } + + @Test + fun whenMakeGETCommentRequestUsingRoutingAPI_thenResponseStausCode200() { + + val latch = CountDownLatch(1) + + Fuel.request(PostRoutingAPI.comments("1",null)) + .responseString { request, response, result -> + Assertions.assertEquals(200, response.statusCode) + latch.countDown() + } + + latch.await() + } + + +} \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/CoroutinesTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/CoroutinesUnitTest.kt similarity index 100% rename from core-kotlin/src/test/kotlin/com/baeldung/kotlin/CoroutinesTest.kt rename to core-kotlin/src/test/kotlin/com/baeldung/kotlin/CoroutinesUnitTest.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/KovenantTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/KovenantTest.kt new file mode 100644 index 0000000000..469118f0f6 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/KovenantTest.kt @@ -0,0 +1,191 @@ +package com.baeldung.kotlin + +import nl.komponents.kovenant.* +import nl.komponents.kovenant.Kovenant.deferred +import nl.komponents.kovenant.combine.and +import nl.komponents.kovenant.combine.combine +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import java.io.IOException +import java.util.* +import java.util.concurrent.TimeUnit + +class KovenantTest { + @Before + fun setupTestMode() { + Kovenant.testMode { error -> + println("An unexpected error occurred") + Assert.fail(error.message) + } + } + + @Test + fun testSuccessfulDeferred() { + val def = deferred() + Assert.assertFalse(def.promise.isDone()) + + def.resolve(1L) + Assert.assertTrue(def.promise.isDone()) + Assert.assertTrue(def.promise.isSuccess()) + Assert.assertFalse(def.promise.isFailure()) + } + + @Test + fun testFailedDeferred() { + val def = deferred() + Assert.assertFalse(def.promise.isDone()) + + def.reject(RuntimeException()) + Assert.assertTrue(def.promise.isDone()) + Assert.assertFalse(def.promise.isSuccess()) + Assert.assertTrue(def.promise.isFailure()) + } + + @Test + fun testResolveDeferredTwice() { + val def = deferred() + def.resolve(1L) + try { + def.resolve(1L) + } catch (e: AssertionError) { + // Expected. + // This is slightly unusual. The AssertionError comes from Assert.fail() from setupTestMode() + } + } + + @Test + fun testSuccessfulTask() { + val promise = task { 1L } + Assert.assertTrue(promise.isDone()) + Assert.assertTrue(promise.isSuccess()) + Assert.assertFalse(promise.isFailure()) + } + + @Test + fun testFailedTask() { + val promise = task { throw RuntimeException() } + Assert.assertTrue(promise.isDone()) + Assert.assertFalse(promise.isSuccess()) + Assert.assertTrue(promise.isFailure()) + } + + @Test + fun testCallbacks() { + val promise = task { 1L } + + promise.success { + println("This was a success") + Assert.assertEquals(1L, it) + } + + promise.fail { + println(it) + Assert.fail("This shouldn't happen") + } + + promise.always { + println("This always happens") + } + } + + @Test + fun testGetValues() { + val promise = task { 1L } + Assert.assertEquals(1L, promise.get()) + } + + @Test + fun testAllSucceed() { + val numbers = all( + task { 1L }, + task { 2L }, + task { 3L } + ) + + Assert.assertEquals(listOf(1L, 2L, 3L), numbers.get()) + } + + @Test + fun testOneFails() { + val runtimeException = RuntimeException() + + val numbers = all( + task { 1L }, + task { 2L }, + task { throw runtimeException } + ) + + Assert.assertEquals(runtimeException, numbers.getError()) + } + + @Test + fun testAnySucceeds() { + val promise = any( + task { + TimeUnit.SECONDS.sleep(3) + 1L + }, + task { + TimeUnit.SECONDS.sleep(2) + 2L + }, + task { + TimeUnit.SECONDS.sleep(1) + 3L + } + ) + + Assert.assertTrue(promise.isDone()) + Assert.assertTrue(promise.isSuccess()) + Assert.assertFalse(promise.isFailure()) + } + + @Test + fun testAllFails() { + val runtimeException = RuntimeException() + val ioException = IOException() + val illegalStateException = IllegalStateException() + val promise = any( + task { + TimeUnit.SECONDS.sleep(3) + throw runtimeException + }, + task { + TimeUnit.SECONDS.sleep(2) + throw ioException + }, + task { + TimeUnit.SECONDS.sleep(1) + throw illegalStateException + } + ) + + Assert.assertTrue(promise.isDone()) + Assert.assertFalse(promise.isSuccess()) + Assert.assertTrue(promise.isFailure()) + } + + @Test + fun testSimpleCombine() { + val promise = task { 1L } and task { "Hello" } + val result = promise.get() + + Assert.assertEquals(1L, result.first) + Assert.assertEquals("Hello", result.second) + } + + @Test + fun testLongerCombine() { + val promise = combine( + task { 1L }, + task { "Hello" }, + task { Currency.getInstance("USD") } + ) + val result = promise.get() + + Assert.assertEquals(1L, result.first) + Assert.assertEquals("Hello", result.second) + Assert.assertEquals(Currency.getInstance("USD"), result.third) + } +} diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/KovenantTimeoutTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/KovenantTimeoutTest.kt new file mode 100644 index 0000000000..e37d2cc2fa --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/KovenantTimeoutTest.kt @@ -0,0 +1,38 @@ +package com.baeldung.kotlin + +import nl.komponents.kovenant.Promise +import nl.komponents.kovenant.any +import nl.komponents.kovenant.task +import org.junit.Assert +import org.junit.Ignore +import org.junit.Test + +@Ignore +// Note that this can not run in the same test run if KovenantTest has already been executed +class KovenantTimeoutTest { + @Test + fun testTimeout() { + val promise = timedTask(1000) { "Hello" } + val result = promise.get() + Assert.assertEquals("Hello", result) + } + + @Test + fun testTimeoutExpired() { + val promise = timedTask(1000) { + Thread.sleep(3000) + "Hello" + } + val result = promise.get() + Assert.assertNull(result) + } + + fun timedTask(millis: Long, body: () -> T) : Promise> { + val timeoutTask = task { + Thread.sleep(millis) + null + } + val activeTask = task(body = body) + return any(activeTask, timeoutTask) + } +} diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/StringConcatenationTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/StringConcatenationTest.kt new file mode 100644 index 0000000000..9c371614a4 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/StringConcatenationTest.kt @@ -0,0 +1,48 @@ +package com.baeldung.kotlin + +import org.junit.Test +import kotlin.test.assertEquals + +class StringConcatenationTest { + + @Test + fun givenTwoStrings_concatenateWithTemplates_thenEquals() { + val a = "Hello" + val b = "Baeldung" + val c = "$a $b" + + assertEquals("Hello Baeldung", c) + } + + @Test + fun givenTwoStrings_concatenateWithPlusOperator_thenEquals() { + val a = "Hello" + val b = "Baeldung" + val c = a + " " + b + + assertEquals("Hello Baeldung", c) + } + + @Test + fun givenTwoStrings_concatenateWithStringBuilder_thenEquals() { + val a = "Hello" + val b = "Baeldung" + + val builder = StringBuilder() + builder.append(a).append(" ").append(b) + + val c = builder.toString() + + assertEquals("Hello Baeldung", c) + } + + @Test + fun givenTwoStrings_concatenateWithPlusMethod_thenEquals() { + val a = "Hello" + val b = "Baeldung" + val c = a.plus(" ").plus(b) + + assertEquals("Hello Baeldung", c) + } + +} diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/gson/GsonUnitTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/gson/GsonUnitTest.kt new file mode 100644 index 0000000000..bdf44d3b49 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/gson/GsonUnitTest.kt @@ -0,0 +1,30 @@ +package com.baeldung.kotlin.gson + +import com.google.gson.Gson + +import org.junit.Assert +import org.junit.Test + +class GsonUnitTest { + + var gson = Gson() + + @Test + fun givenObject_thenGetJSONString() { + var jsonString = gson.toJson(TestModel(1,"Test")) + Assert.assertEquals(jsonString, "{\"id\":1,\"description\":\"Test\"}") + } + + @Test + fun givenJSONString_thenGetObject() { + var jsonString = "{\"id\":1,\"description\":\"Test\"}"; + var testModel = gson.fromJson(jsonString, TestModel::class.java) + Assert.assertEquals(testModel.id, 1) + Assert.assertEquals(testModel.description, "Test") + } + + data class TestModel( + val id: Int, + val description: String + ) +} \ No newline at end of file diff --git a/couchbase/pom.xml b/couchbase/pom.xml index f6397fe309..4f0f8787ca 100644 --- a/couchbase/pom.xml +++ b/couchbase/pom.xml @@ -3,11 +3,11 @@ 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 - couchbase-sdk + couchbase 0.1-SNAPSHOT jar couchbase - Couchbase SDK Tutorials + Couchbase Tutorials com.baeldung diff --git a/disruptor/pom.xml b/disruptor/pom.xml index d3cef3bd9b..c26dcc0cd4 100644 --- a/disruptor/pom.xml +++ b/disruptor/pom.xml @@ -36,22 +36,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 diff --git a/drools/pom.xml b/drools/pom.xml index 631bc8c792..009ac8acec 100644 --- a/drools/pom.xml +++ b/drools/pom.xml @@ -48,12 +48,18 @@ poi-ooxml ${apache-poi-version} + + org.optaplanner + optaplanner-core + ${opta-planner-version} + 4.4.6 7.4.1.Final 3.13 + 7.10.0.Final diff --git a/drools/src/main/java/com/baeldung/drools/optaplanner/CourseSchedule.java b/drools/src/main/java/com/baeldung/drools/optaplanner/CourseSchedule.java new file mode 100644 index 0000000000..7b2ba117a1 --- /dev/null +++ b/drools/src/main/java/com/baeldung/drools/optaplanner/CourseSchedule.java @@ -0,0 +1,63 @@ +package com.baeldung.drools.optaplanner; + +import java.util.ArrayList; +import java.util.List; + +import org.optaplanner.core.api.domain.solution.PlanningEntityCollectionProperty; +import org.optaplanner.core.api.domain.solution.PlanningScore; +import org.optaplanner.core.api.domain.solution.PlanningSolution; +import org.optaplanner.core.api.domain.solution.drools.ProblemFactCollectionProperty; +import org.optaplanner.core.api.domain.valuerange.ValueRangeProvider; +import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@PlanningSolution +public class CourseSchedule { + + Logger logger = LoggerFactory.getLogger("CourseSchedule"); + + private List roomList; + private List periodList; + private List lectureList; + private HardSoftScore score; + + public CourseSchedule(){ + roomList = new ArrayList<>(); + periodList = new ArrayList<>(); + lectureList = new ArrayList<>(); + } + + @ValueRangeProvider(id = "availableRooms") + @ProblemFactCollectionProperty + public List getRoomList() { + return roomList; + } + + @ValueRangeProvider(id = "availablePeriods") + @ProblemFactCollectionProperty + public List getPeriodList() { + return periodList; + } + + @PlanningEntityCollectionProperty + public List getLectureList() { + return lectureList; + } + + @PlanningScore + public HardSoftScore getScore() { + return score; + } + + public void setScore(HardSoftScore score) { + this.score = score; + } + + public void printCourseSchedule() { + lectureList.stream() + .map(c -> "Lecture in Room " + c.getRoomNumber().toString() + " during Period " + c.getPeriod().toString()) + .forEach(k -> logger.info(k)); + } + +} diff --git a/drools/src/main/java/com/baeldung/drools/optaplanner/Lecture.java b/drools/src/main/java/com/baeldung/drools/optaplanner/Lecture.java new file mode 100644 index 0000000000..193cdb08b1 --- /dev/null +++ b/drools/src/main/java/com/baeldung/drools/optaplanner/Lecture.java @@ -0,0 +1,30 @@ +package com.baeldung.drools.optaplanner; + +import org.optaplanner.core.api.domain.entity.PlanningEntity; +import org.optaplanner.core.api.domain.variable.PlanningVariable; + +@PlanningEntity +public class Lecture { + + private Integer roomNumber; + private Integer period; + + @PlanningVariable(valueRangeProviderRefs = {"availablePeriods"}) + public Integer getPeriod() { + return period; + } + + @PlanningVariable(valueRangeProviderRefs = {"availableRooms"}) + public Integer getRoomNumber() { + return roomNumber; + } + + public void setPeriod(Integer period) { + this.period = period; + } + + public void setRoomNumber(Integer roomNumber) { + this.roomNumber = roomNumber; + } + +} diff --git a/drools/src/main/java/com/baeldung/drools/optaplanner/ScoreCalculator.java b/drools/src/main/java/com/baeldung/drools/optaplanner/ScoreCalculator.java new file mode 100644 index 0000000000..86501cdccd --- /dev/null +++ b/drools/src/main/java/com/baeldung/drools/optaplanner/ScoreCalculator.java @@ -0,0 +1,32 @@ +package com.baeldung.drools.optaplanner; + +import java.util.HashSet; + +import org.optaplanner.core.api.score.Score; +import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore; +import org.optaplanner.core.impl.score.director.easy.EasyScoreCalculator; + +public class ScoreCalculator implements EasyScoreCalculator { + + @Override + public Score calculateScore(CourseSchedule courseSchedule) { + int hardScore = 0; + int softScore = 0; + + HashSet occupiedRooms = new HashSet<>(); + for (Lecture lecture : courseSchedule.getLectureList()) { + if(lecture.getPeriod() != null && lecture.getRoomNumber() != null) { + String roomInUse = lecture.getPeriod().toString() + ":" + lecture.getRoomNumber().toString(); + if (occupiedRooms.contains(roomInUse)) { + hardScore += -1; + } else { + occupiedRooms.add(roomInUse); + } + } else { + hardScore += -1; + } + } + + return HardSoftScore.valueOf(hardScore, softScore); + } +} diff --git a/drools/src/main/resources/courseSchedule.drl b/drools/src/main/resources/courseSchedule.drl new file mode 100644 index 0000000000..35dd57df84 --- /dev/null +++ b/drools/src/main/resources/courseSchedule.drl @@ -0,0 +1,14 @@ +package com.baeldung.drools.optaplanner + +import com.baeldung.drools.optaplanner.Lecture; +import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScoreHolder; + +global HardSoftScoreHolder scoreHolder; + +rule "noNullRoomPeriod" + when + Lecture( roomNumber == null ); + Lecture( period == null ); + then + scoreHolder.addHardConstraintMatch(kcontext, -1); +end \ No newline at end of file diff --git a/drools/src/main/resources/courseScheduleSolverConfigDrools.xml b/drools/src/main/resources/courseScheduleSolverConfigDrools.xml new file mode 100644 index 0000000000..7cf95fdcd3 --- /dev/null +++ b/drools/src/main/resources/courseScheduleSolverConfigDrools.xml @@ -0,0 +1,12 @@ + + + + + + courseSchedule.drl + + + + 10 + + \ No newline at end of file diff --git a/drools/src/main/resources/courseScheduleSolverConfiguration.xml b/drools/src/main/resources/courseScheduleSolverConfiguration.xml new file mode 100644 index 0000000000..dfedb8f2fd --- /dev/null +++ b/drools/src/main/resources/courseScheduleSolverConfiguration.xml @@ -0,0 +1,12 @@ + + + + + + com.baeldung.drools.optaplanner.ScoreCalculator + + + + 10 + + \ No newline at end of file diff --git a/drools/src/test/java/com/baeldung/drools/optaplanner/OptaPlannerUnitTest.java b/drools/src/test/java/com/baeldung/drools/optaplanner/OptaPlannerUnitTest.java new file mode 100644 index 0000000000..1880e30b86 --- /dev/null +++ b/drools/src/test/java/com/baeldung/drools/optaplanner/OptaPlannerUnitTest.java @@ -0,0 +1,49 @@ +package com.baeldung.drools.optaplanner; + +import java.util.Arrays; + +import org.junit.Assert; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.optaplanner.core.api.solver.Solver; +import org.optaplanner.core.api.solver.SolverFactory; + +public class OptaPlannerUnitTest { + + static CourseSchedule unsolvedCourseSchedule; + + @BeforeAll + public static void setUp() { + + unsolvedCourseSchedule = new CourseSchedule(); + + for(int i = 0; i < 10; i++){ + unsolvedCourseSchedule.getLectureList().add(new Lecture()); + } + + unsolvedCourseSchedule.getPeriodList().addAll(Arrays.asList(new Integer[] { 1, 2, 3 })); + unsolvedCourseSchedule.getRoomList().addAll(Arrays.asList(new Integer[] { 1, 2 })); + } + + @Test + public void test_whenCustomJavaSolver() { + + SolverFactory solverFactory = SolverFactory.createFromXmlResource("courseScheduleSolverConfiguration.xml"); + Solver solver = solverFactory.buildSolver(); + CourseSchedule solvedCourseSchedule = solver.solve(unsolvedCourseSchedule); + + Assert.assertNotNull(solvedCourseSchedule.getScore()); + Assert.assertEquals(-4, solvedCourseSchedule.getScore().getHardScore()); + } + + @Test + public void test_whenDroolsSolver() { + + SolverFactory solverFactory = SolverFactory.createFromXmlResource("courseScheduleSolverConfigDrools.xml"); + Solver solver = solverFactory.buildSolver(); + CourseSchedule solvedCourseSchedule = solver.solve(unsolvedCourseSchedule); + + Assert.assertNotNull(solvedCourseSchedule.getScore()); + Assert.assertEquals(0, solvedCourseSchedule.getScore().getHardScore()); + } +} diff --git a/ejb/ejb-client/pom.xml b/ejb/ejb-client/pom.xml index 44112a4396..6231030cec 100755 --- a/ejb/ejb-client/pom.xml +++ b/ejb/ejb-client/pom.xml @@ -16,7 +16,6 @@ org.wildfly wildfly-ejb-client-bom pom - import com.baeldung.ejb diff --git a/ejb/pom.xml b/ejb/pom.xml index a188d43272..4cb700d087 100755 --- a/ejb/pom.xml +++ b/ejb/pom.xml @@ -17,7 +17,6 @@ ejb-remote - ejb-client ejb-session-beans diff --git a/ejb/wildfly/pom.xml b/ejb/wildfly/pom.xml index 7159096f3c..53d10a90ed 100644 --- a/ejb/wildfly/pom.xml +++ b/ejb/wildfly/pom.xml @@ -2,9 +2,10 @@ 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.wildfly - wildfly-example + wildfly 0.0.1-SNAPSHOT pom + wildfly com.baeldung.ejb diff --git a/ejb/wildfly/wildfly-mdb/pom.xml b/ejb/wildfly/wildfly-mdb/pom.xml index 0b2ec7d5a3..186ddc50c0 100644 --- a/ejb/wildfly/wildfly-mdb/pom.xml +++ b/ejb/wildfly/wildfly-mdb/pom.xml @@ -1,8 +1,9 @@ 4.0.0 - widlfly-mdb - + wildfly-mdb + wildfly-mdb + com.baeldung.wildfly wildfly-example diff --git a/enterprise-patterns/README.md b/enterprise-patterns/README.md deleted file mode 100644 index ff12555376..0000000000 --- a/enterprise-patterns/README.md +++ /dev/null @@ -1 +0,0 @@ -## Relevant articles: diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/AuditFilter.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/AuditFilter.java deleted file mode 100644 index d24c0a94b3..0000000000 --- a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/AuditFilter.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.baeldung.enterprise.patterns.front.controller.filters; - -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpSession; -import java.io.IOException; - -public class AuditFilter extends BaseFilter { - @Override - public void doFilter( - ServletRequest request, - ServletResponse response, - FilterChain chain - ) throws IOException, ServletException { - HttpServletRequest httpServletRequest = (HttpServletRequest) request; - HttpSession session = httpServletRequest.getSession(false); - if (session != null && session.getAttribute("username") != null) { - request.setAttribute("username", session.getAttribute("username")); - } - chain.doFilter(request, response); - } -} diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/VisitorCounterFilter.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/VisitorCounterFilter.java deleted file mode 100644 index 0ae7cd73fd..0000000000 --- a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/VisitorCounterFilter.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.baeldung.enterprise.patterns.front.controller.filters; - -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.annotation.WebFilter; -import java.io.IOException; - -@WebFilter(servletNames = "front-controller") -public class VisitorCounterFilter extends BaseFilter { - private int counter; - - @Override - public void doFilter( - ServletRequest request, - ServletResponse response, - FilterChain chain - ) throws IOException, ServletException { - request.setAttribute("counter", ++counter); - chain.doFilter(request, response); - } -} diff --git a/enterprise-patterns/pom.xml b/enterprise-patterns/pom.xml deleted file mode 100644 index ffd0b66aad..0000000000 --- a/enterprise-patterns/pom.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - 4.0.0 - com.baeldung.enterprise.patterns - enterprise-patterns-parent - pom - - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - - - - spring-dispatcher-servlet - - - diff --git a/feign/pom.xml b/feign/pom.xml index 29c2a784bc..ea645383c1 100644 --- a/feign/pom.xml +++ b/feign/pom.xml @@ -3,7 +3,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.feign - feign-client + feign + feign com.baeldung diff --git a/google-web-toolkit/pom.xml b/google-web-toolkit/pom.xml index b2f7cab355..db9ce2eac0 100644 --- a/google-web-toolkit/pom.xml +++ b/google-web-toolkit/pom.xml @@ -6,10 +6,11 @@ 4.0.0 com.baeldung - google_web_toolkit + google-web-toolkit war 1.0-SNAPSHOT - + google-web-toolkit + com.baeldung parent-modules diff --git a/grpc/pom.xml b/grpc/pom.xml index 218e2df008..949f26d376 100644 --- a/grpc/pom.xml +++ b/grpc/pom.xml @@ -1,11 +1,11 @@ 4.0.0 - grpc - grpc-demo + grpc 0.0.1-SNAPSHOT jar - + grpc + com.baeldung parent-modules diff --git a/gson/README.md b/gson/README.md index bedfbd206c..4122b21431 100644 --- a/gson/README.md +++ b/gson/README.md @@ -7,3 +7,4 @@ - [Gson Deserialization Cookbook](http://www.baeldung.com/gson-deserialization-guide) - [Jackson vs Gson](http://www.baeldung.com/jackson-vs-gson) - [Exclude Fields from Serialization in Gson](http://www.baeldung.com/gson-exclude-fields-serialization) +- [Save Data to a JSON File with Gson](https://www.baeldung.com/gson-save-file) diff --git a/guava/README.md b/guava/README.md index bb4e225649..fe1a347d72 100644 --- a/guava/README.md +++ b/guava/README.md @@ -33,3 +33,4 @@ - [Using Guava CountingOutputStream](http://www.baeldung.com/guava-counting-outputstream) - [Hamcrest Text Matchers](http://www.baeldung.com/hamcrest-text-matchers) - [Quick Guide to the Guava RateLimiter](http://www.baeldung.com/guava-rate-limiter) +- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap) diff --git a/guest/remote-debugging/pom.xml b/guest/remote-debugging/pom.xml index 67fed3f1a1..974421de97 100644 --- a/guest/remote-debugging/pom.xml +++ b/guest/remote-debugging/pom.xml @@ -3,9 +3,10 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.stackify - java-remote-debugging + remote-debugging 0.0.1-SNAPSHOT war + remote-debugging org.springframework.boot diff --git a/hazelcast/README.md b/hazelcast/README.md index b90f66a8d0..7adb13f2af 100644 --- a/hazelcast/README.md +++ b/hazelcast/README.md @@ -1,2 +1,3 @@ ### Relevant Articles: - [Guide to Hazelcast with Java](http://www.baeldung.com/java-hazelcast) +- [Introduction to Hazelcast Jet](https://www.baeldung.com/hazelcast-jet) diff --git a/hibernate5/README.md b/hibernate5/README.md index 1bce52bd5e..8f2f8eb469 100644 --- a/hibernate5/README.md +++ b/hibernate5/README.md @@ -13,3 +13,4 @@ - [Pessimistic Locking in JPA](http://www.baeldung.com/jpa-pessimistic-locking) - [Bootstrapping JPA Programmatically in Java](http://www.baeldung.com/java-bootstrap-jpa) - [Optimistic Locking in JPA](http://www.baeldung.com/jpa-optimistic-locking) +- [Hibernate Entity Lifecycle](https://www.baeldung.com/hibernate-entity-lifecycle) diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java b/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java index 23d7d2e201..2212e736ab 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java @@ -1,5 +1,11 @@ package com.baeldung.hibernate; +import java.io.FileInputStream; +import java.io.IOException; +import java.net.URL; +import java.util.Properties; + +import com.baeldung.hibernate.entities.DeptEmployee; import com.baeldung.hibernate.optimisticlocking.OptimisticLockingCourse; import com.baeldung.hibernate.optimisticlocking.OptimisticLockingStudent; import com.baeldung.hibernate.pessimisticlocking.Individual; @@ -16,10 +22,30 @@ import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.service.ServiceRegistry; -import java.io.FileInputStream; -import java.io.IOException; -import java.net.URL; -import java.util.Properties; +import com.baeldung.hibernate.pojo.Course; +import com.baeldung.hibernate.pojo.Employee; +import com.baeldung.hibernate.pojo.EntityDescription; +import com.baeldung.hibernate.pojo.OrderEntry; +import com.baeldung.hibernate.pojo.OrderEntryIdClass; +import com.baeldung.hibernate.pojo.OrderEntryPK; +import com.baeldung.hibernate.pojo.Person; +import com.baeldung.hibernate.pojo.Phone; +import com.baeldung.hibernate.pojo.PointEntity; +import com.baeldung.hibernate.pojo.PolygonEntity; +import com.baeldung.hibernate.pojo.Product; +import com.baeldung.hibernate.pojo.Student; +import com.baeldung.hibernate.pojo.TemporalValues; +import com.baeldung.hibernate.pojo.User; +import com.baeldung.hibernate.pojo.UserProfile; +import com.baeldung.hibernate.pojo.inheritance.Animal; +import com.baeldung.hibernate.pojo.inheritance.Bag; +import com.baeldung.hibernate.pojo.inheritance.Book; +import com.baeldung.hibernate.pojo.inheritance.Car; +import com.baeldung.hibernate.pojo.inheritance.MyEmployee; +import com.baeldung.hibernate.pojo.inheritance.MyProduct; +import com.baeldung.hibernate.pojo.inheritance.Pen; +import com.baeldung.hibernate.pojo.inheritance.Pet; +import com.baeldung.hibernate.pojo.inheritance.Vehicle; public class HibernateUtil { private static SessionFactory sessionFactory; @@ -72,6 +98,8 @@ public class HibernateUtil { metadataSources.addAnnotatedClass(PessimisticLockingCourse.class); metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Customer.class); metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Address.class); + metadataSources.addAnnotatedClass(DeptEmployee.class); + metadataSources.addAnnotatedClass(com.baeldung.hibernate.entities.Department.class); metadataSources.addAnnotatedClass(OptimisticLockingCourse.class); metadataSources.addAnnotatedClass(OptimisticLockingStudent.class); diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/entities/Department.java b/hibernate5/src/main/java/com/baeldung/hibernate/entities/Department.java new file mode 100644 index 0000000000..ff94f4f849 --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/entities/Department.java @@ -0,0 +1,45 @@ +package com.baeldung.hibernate.entities; + +import java.util.List; + +import javax.persistence.*; + +@Entity +public class Department { + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE) + private long id; + + private String name; + + @OneToMany(mappedBy="department") + private List employees; + + public Department(String name) { + this.name = name; + } + + 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 List getEmployees() { + return employees; + } + + public void setEmployees(List employees) { + this.employees = employees; + } +} diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/entities/DeptEmployee.java b/hibernate5/src/main/java/com/baeldung/hibernate/entities/DeptEmployee.java new file mode 100644 index 0000000000..7a51009b62 --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/entities/DeptEmployee.java @@ -0,0 +1,65 @@ +package com.baeldung.hibernate.entities; + +import javax.persistence.*; + +@Entity +public class DeptEmployee { + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE) + private long id; + + private String employeeNumber; + + private String designation; + + private String name; + + @ManyToOne + private Department department; + + public DeptEmployee(String name, String employeeNumber, Department department) { + this.name = name; + this.employeeNumber = employeeNumber; + this.department = department; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getEmployeeNumber() { + return employeeNumber; + } + + public void setEmployeeNumber(String employeeNumber) { + this.employeeNumber = employeeNumber; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Department getDepartment() { + return department; + } + + public void setDepartment(Department department) { + this.department = department; + } + + public String getDesignation() { + return designation; + } + + public void setDesignation(String designation) { + this.designation = designation; + } +} diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/pojo/Result.java b/hibernate5/src/main/java/com/baeldung/hibernate/pojo/Result.java new file mode 100644 index 0000000000..607269a267 --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/pojo/Result.java @@ -0,0 +1,31 @@ +package com.baeldung.hibernate.pojo; + +public class Result { + private String employeeName; + + private String departmentName; + + public Result(String employeeName, String departmentName) { + this.employeeName = employeeName; + this.departmentName = departmentName; + } + + public Result() { + } + + public String getEmployeeName() { + return employeeName; + } + + public void setEmployeeName(String employeeName) { + this.employeeName = employeeName; + } + + public String getDepartmentName() { + return departmentName; + } + + public void setDepartmentName(String departmentName) { + this.departmentName = departmentName; + } +} diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/proxy/BatchEmployee.java b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/BatchEmployee.java new file mode 100644 index 0000000000..00643ab3dd --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/BatchEmployee.java @@ -0,0 +1,56 @@ +package com.baeldung.hibernate.proxy; + +import org.hibernate.annotations.BatchSize; + +import javax.persistence.*; +import java.io.Serializable; + +@Entity +@BatchSize(size = 5) +public class BatchEmployee implements Serializable { + + @Id + @GeneratedValue (strategy = GenerationType.SEQUENCE) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + private Boss boss; + + @Column(name = "name") + private String name; + + @Column(name = "surname") + private String surname; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Boss getBoss() { + return boss; + } + + public void setBoss(Boss boss) { + this.boss = boss; + } + + 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/main/java/com/baeldung/hibernate/proxy/Boss.java b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Boss.java new file mode 100644 index 0000000000..b6e01814d0 --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Boss.java @@ -0,0 +1,49 @@ +package com.baeldung.hibernate.proxy; + +import javax.persistence.*; +import java.io.Serializable; + +@Entity +public class Boss implements Serializable { + + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE) + private Long id; + + @Column(name = "name") + private String name; + + @Column(name = "surname") + private String surname; + + public Boss() { } + + public Boss(String name, String surname) { + this.name = name; + this.surname = surname; + } + + 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 getSurname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } +} diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Employee.java b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Employee.java new file mode 100644 index 0000000000..6bc64c35ef --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Employee.java @@ -0,0 +1,53 @@ +package com.baeldung.hibernate.proxy; + +import javax.persistence.*; +import java.io.Serializable; + +@Entity +public class Employee implements Serializable { + + @Id + @GeneratedValue (strategy = GenerationType.SEQUENCE) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + private Boss boss; + + @Column(name = "name") + private String name; + + @Column(name = "surname") + private String surname; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Boss getBoss() { + return boss; + } + + public void setBoss(Boss boss) { + this.boss = boss; + } + + 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/main/java/com/baeldung/hibernate/proxy/HibernateUtil.java b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/HibernateUtil.java new file mode 100644 index 0000000000..e6ad0432bd --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/HibernateUtil.java @@ -0,0 +1,57 @@ +package com.baeldung.hibernate.proxy; + +import org.apache.commons.lang3.StringUtils; +import org.hibernate.SessionFactory; +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.SessionFactoryBuilder; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.service.ServiceRegistry; + +import java.io.FileInputStream; +import java.io.IOException; +import java.net.URL; +import java.util.Properties; + +public class HibernateUtil { + + private static SessionFactory sessionFactory; + private static String PROPERTY_FILE_NAME; + + public static SessionFactory getSessionFactory(String propertyFileName) throws IOException { + PROPERTY_FILE_NAME = propertyFileName; + if (sessionFactory == null) { + ServiceRegistry serviceRegistry = configureServiceRegistry(); + sessionFactory = getSessionFactoryBuilder(serviceRegistry).build(); + } + return sessionFactory; + } + + private static SessionFactoryBuilder getSessionFactoryBuilder(ServiceRegistry serviceRegistry) { + MetadataSources metadataSources = new MetadataSources(serviceRegistry); + metadataSources.addPackage("com.baeldung.hibernate.proxy"); + metadataSources.addAnnotatedClass(Boss.class); + metadataSources.addAnnotatedClass(Employee.class); + + Metadata metadata = metadataSources.buildMetadata(); + return metadata.getSessionFactoryBuilder(); + + } + + private static ServiceRegistry configureServiceRegistry() throws IOException { + Properties properties = getProperties(); + return new StandardServiceRegistryBuilder().applySettings(properties) + .build(); + } + + private static Properties getProperties() throws IOException { + Properties properties = new Properties(); + URL propertiesURL = Thread.currentThread() + .getContextClassLoader() + .getResource(StringUtils.defaultString(PROPERTY_FILE_NAME, "hibernate.properties")); + try (FileInputStream inputStream = new FileInputStream(propertiesURL.getFile())) { + properties.load(inputStream); + } + return properties; + } +} diff --git a/hibernate5/src/test/java/com/baeldung/hibernate/CustomClassIntegrationTest.java b/hibernate5/src/test/java/com/baeldung/hibernate/CustomClassIntegrationTest.java new file mode 100644 index 0000000000..29ae55b773 --- /dev/null +++ b/hibernate5/src/test/java/com/baeldung/hibernate/CustomClassIntegrationTest.java @@ -0,0 +1,77 @@ +package com.baeldung.hibernate; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; +import java.util.List; + +import com.baeldung.hibernate.entities.DeptEmployee; +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.query.Query; +import org.hibernate.transform.Transformers; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.baeldung.hibernate.entities.Department; +import com.baeldung.hibernate.pojo.Result; + +public class CustomClassIntegrationTest { + + private Session session; + + private Transaction transaction; + + @BeforeEach + public void setUp() throws IOException { + session = HibernateUtil.getSessionFactory().openSession(); + transaction = session.beginTransaction(); + session.createNativeQuery("delete from manager").executeUpdate(); + session.createNativeQuery("delete from department").executeUpdate(); + Department department = new Department("Sales"); + DeptEmployee employee = new DeptEmployee("John Smith", "001", department); + session.persist(department); + session.persist(employee); + transaction.commit(); + transaction = session.beginTransaction(); + } + + @Test + public void whenAllManagersAreSelected_ThenObjectGraphIsReturned() { + Query query = session.createQuery("from com.baeldung.hibernate.entities.DeptEmployee"); + List deptEmployees = query.list(); + DeptEmployee deptEmployee = deptEmployees.get(0); + assertEquals("John Smith", deptEmployee.getName()); + assertEquals("Sales", deptEmployee.getDepartment().getName()); + } + + @Test + public void whenIndividualPropertiesAreSelected_ThenObjectArrayIsReturned() { + Query query = session.createQuery("select m.name, m.department.name from com.baeldung.hibernate.entities.DeptEmployee m"); + List managers = query.list(); + Object[] manager = (Object[]) managers.get(0); + assertEquals("John Smith", manager[0]); + assertEquals("Sales", manager[1]); + } + + @Test + public void whenResultConstructorInSelect_ThenListOfResultIsReturned() { + Query query = session.createQuery("select new com.baeldung.hibernate.pojo.Result(m.name, m.department.name) " + + "from DeptEmployee m"); + List results = query.list(); + Result result = results.get(0); + assertEquals("John Smith", result.getEmployeeName()); + assertEquals("Sales", result.getDepartmentName()); + } + + @Test + public void whenResultTransformerOnQuery_ThenListOfResultIsReturned() { + Query query = session.createQuery("select m.name as employeeName, m.department.name as departmentName " + + "from com.baeldung.hibernate.entities.DeptEmployee m"); + query.setResultTransformer(Transformers.aliasToBean(Result.class)); + List results = query.list(); + Result result = results.get(0); + assertEquals("John Smith", result.getEmployeeName()); + assertEquals("Sales", result.getDepartmentName()); + } +} diff --git a/hibernate5/src/test/java/com/baeldung/hibernate/proxy/HibernateProxyUnitTest.java b/hibernate5/src/test/java/com/baeldung/hibernate/proxy/HibernateProxyUnitTest.java new file mode 100644 index 0000000000..fa41797dd2 --- /dev/null +++ b/hibernate5/src/test/java/com/baeldung/hibernate/proxy/HibernateProxyUnitTest.java @@ -0,0 +1,75 @@ +package com.baeldung.hibernate.proxy; + +import org.hibernate.*; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.util.List; + +import static org.junit.Assert.fail; + +public class HibernateProxyUnitTest { + + private Session session; + + @Before + public void init(){ + try { + session = HibernateUtil.getSessionFactory("hibernate.properties") + .openSession(); + } catch (HibernateException | IOException e) { + fail("Failed to initiate Hibernate Session [Exception:" + e.toString() + "]"); + } + + Boss boss = new Boss("Eduard", "Freud"); + session.save(boss); + } + + @After + public void close(){ + if(session != null) { + session.close(); + } + } + + @Test(expected = NullPointerException.class) + public void givenAnInexistentEmployeeId_whenUseGetMethod_thenReturnNull() { + Employee employee = session.get(Employee.class, new Long(14)); + assertNull(employee); + employee.getId(); + } + + @Test + public void givenAnInexistentEmployeeId_whenUseLoadMethod_thenReturnAProxy() { + Employee employee = session.load(Employee.class, new Long(14)); + assertNotNull(employee); + } + + @Test + public void givenABatchEmployeeList_whenSaveOne_thenSaveTheWholeBatch() { + Transaction transaction = session.beginTransaction(); + + for (long i = 1; i <= 5; i++) { + Employee employee = new Employee(); + employee.setName("Employee " + i); + session.save(employee); + } + + //After this line is possible to see all the insertions in the logs + session.flush(); + session.clear(); + transaction.commit(); + + transaction = session.beginTransaction(); + + List employeeList = session.createQuery("from Employee") + .setCacheMode(CacheMode.IGNORE).getResultList(); + + assertEquals(employeeList.size(), 5); + transaction.commit(); + } +} diff --git a/intelliJ/README.md b/intelliJ/README.md new file mode 100644 index 0000000000..d45bd0cee5 --- /dev/null +++ b/intelliJ/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: + +- [Writing IntelliJ IDEA Plugins](https://www.baeldung.com/intellij-new-custom-plugin) diff --git a/java-dates/pom.xml b/java-dates/pom.xml index 877dd615a8..13e2a077e1 100644 --- a/java-dates/pom.xml +++ b/java-dates/pom.xml @@ -61,23 +61,6 @@ - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - - org.apache.maven.plugins maven-compiler-plugin diff --git a/java-numbers/pom.xml b/java-numbers/pom.xml index bf4d3e8792..bb63c8cfe1 100644 --- a/java-numbers/pom.xml +++ b/java-numbers/pom.xml @@ -83,23 +83,6 @@ - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - - org.apache.maven.plugins maven-javadoc-plugin diff --git a/java-numbers/src/main/java/com/baeldung/maths/BigDecimalDemo.java b/java-numbers/src/main/java/com/baeldung/maths/BigDecimalDemo.java new file mode 100644 index 0000000000..7de0197769 --- /dev/null +++ b/java-numbers/src/main/java/com/baeldung/maths/BigDecimalDemo.java @@ -0,0 +1,29 @@ +package com.baeldung.maths; + +import java.math.BigDecimal; +import java.math.RoundingMode; + +public class BigDecimalDemo { + + /** Calculate total amount to be paid for an item rounded to cents.. + * @param quantity + * @param unitPrice + * @param discountRate + * @param taxRate + * @return + */ + public static BigDecimal calculateTotalAmount(BigDecimal quantity, + BigDecimal unitPrice, BigDecimal discountRate, BigDecimal taxRate) { + BigDecimal amount = quantity.multiply(unitPrice); + BigDecimal discount = amount.multiply(discountRate); + BigDecimal discountedAmount = amount.subtract(discount); + BigDecimal tax = discountedAmount.multiply(taxRate); + BigDecimal total = discountedAmount.add(tax); + + // round to 2 decimal places using HALF_EVEN + BigDecimal roundedTotal = total.setScale(2, RoundingMode.HALF_EVEN); + + return roundedTotal; + } + +} diff --git a/java-numbers/src/test/java/com/baeldung/maths/BigDecimalDemoUnitTest.java b/java-numbers/src/test/java/com/baeldung/maths/BigDecimalDemoUnitTest.java new file mode 100644 index 0000000000..2bf9872bec --- /dev/null +++ b/java-numbers/src/test/java/com/baeldung/maths/BigDecimalDemoUnitTest.java @@ -0,0 +1,120 @@ +package com.baeldung.maths; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.math.MathContext; +import java.math.RoundingMode; +import java.util.Random; + +import org.junit.jupiter.api.Test; + +public class BigDecimalDemoUnitTest { + + @Test + public void whenBigDecimalCreated_thenValueMatches() { + BigDecimal bdFromString = new BigDecimal("0.1"); + BigDecimal bdFromCharArray = new BigDecimal( + new char[] { '3', '.', '1', '6', '1', '5' }); + BigDecimal bdlFromInt = new BigDecimal(42); + BigDecimal bdFromLong = new BigDecimal(123412345678901L); + BigInteger bigInteger = BigInteger.probablePrime(100, new Random()); + BigDecimal bdFromBigInteger = new BigDecimal(bigInteger); + + assertEquals("0.1", bdFromString.toString()); + assertEquals("3.1615", bdFromCharArray.toString()); + assertEquals("42", bdlFromInt.toString()); + assertEquals("123412345678901", bdFromLong.toString()); + assertEquals(bigInteger.toString(), bdFromBigInteger.toString()); + } + + @Test + public void whenBigDecimalCreatedFromDouble_thenValueMayNotMatch() { + BigDecimal bdFromDouble = new BigDecimal(0.1d); + assertNotEquals("0.1", bdFromDouble.toString()); + } + + @Test + public void whenBigDecimalCreatedUsingValueOf_thenValueMatches() { + BigDecimal bdFromLong1 = BigDecimal.valueOf(123412345678901L); + BigDecimal bdFromLong2 = BigDecimal.valueOf(123412345678901L, 2); + BigDecimal bdFromDouble = BigDecimal.valueOf(0.1d); + + assertEquals("123412345678901", bdFromLong1.toString()); + assertEquals("1234123456789.01", bdFromLong2.toString()); + assertEquals("0.1", bdFromDouble.toString()); + } + + @Test + public void whenEqualsCalled_thenSizeAndScaleMatched() { + BigDecimal bd1 = new BigDecimal("1.0"); + BigDecimal bd2 = new BigDecimal("1.00"); + + assertFalse(bd1.equals(bd2)); + } + + @Test + public void whenComparingBigDecimals_thenExpectedResult() { + BigDecimal bd1 = new BigDecimal("1.0"); + BigDecimal bd2 = new BigDecimal("1.00"); + BigDecimal bd3 = new BigDecimal("2.0"); + + assertTrue(bd1.compareTo(bd3) < 0); + assertTrue(bd3.compareTo(bd1) > 0); + assertTrue(bd1.compareTo(bd2) == 0); + assertTrue(bd1.compareTo(bd3) <= 0); + assertTrue(bd1.compareTo(bd2) >= 0); + assertTrue(bd1.compareTo(bd3) != 0); + } + + @Test + public void whenPerformingArithmetic_thenExpectedResult() { + BigDecimal bd1 = new BigDecimal("4.0"); + BigDecimal bd2 = new BigDecimal("2.0"); + + BigDecimal sum = bd1.add(bd2); + BigDecimal difference = bd1.subtract(bd2); + BigDecimal quotient = bd1.divide(bd2); + BigDecimal product = bd1.multiply(bd2); + + assertTrue(sum.compareTo(new BigDecimal("6.0")) == 0); + assertTrue(difference.compareTo(new BigDecimal("2.0")) == 0); + assertTrue(quotient.compareTo(new BigDecimal("2.0")) == 0); + assertTrue(product.compareTo(new BigDecimal("8.0")) == 0); + } + + @Test + public void whenGettingAttributes_thenExpectedResult() { + BigDecimal bd = new BigDecimal("-12345.6789"); + + assertEquals(9, bd.precision()); + assertEquals(4, bd.scale()); + assertEquals(-1, bd.signum()); + } + + @Test + public void whenRoundingDecimal_thenExpectedResult() { + BigDecimal bd = new BigDecimal("2.5"); + // Round to 1 digit using HALF_EVEN + BigDecimal rounded = bd + .round(new MathContext(1, RoundingMode.HALF_EVEN)); + + assertEquals("2", rounded.toString()); + } + + @Test + public void givenPurchaseTxn_whenCalculatingTotalAmount_thenExpectedResult() { + BigDecimal quantity = new BigDecimal("4.5"); + BigDecimal unitPrice = new BigDecimal("2.69"); + BigDecimal discountRate = new BigDecimal("0.10"); + BigDecimal taxRate = new BigDecimal("0.0725"); + + BigDecimal amountToBePaid = BigDecimalDemo + .calculateTotalAmount(quantity, unitPrice, discountRate, taxRate); + assertEquals("11.68", amountToBePaid.toString()); + } +} diff --git a/java-numbers/src/test/java/com/baeldung/maths/BigIntegerDemoUnitTest.java b/java-numbers/src/test/java/com/baeldung/maths/BigIntegerDemoUnitTest.java new file mode 100644 index 0000000000..3537ccb3a3 --- /dev/null +++ b/java-numbers/src/test/java/com/baeldung/maths/BigIntegerDemoUnitTest.java @@ -0,0 +1,128 @@ +package com.baeldung.maths; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.math.BigInteger; +import java.util.Random; + +import org.junit.jupiter.api.Test; + +public class BigIntegerDemoUnitTest { + + @Test + public void whenBigIntegerCreatedFromConstructor_thenExpectedResult() { + BigInteger biFromString = new BigInteger("1234567890987654321"); + BigInteger biFromByteArray = new BigInteger( + new byte[] { 64, 64, 64, 64, 64, 64 }); + BigInteger biFromSignMagnitude = new BigInteger(-1, + new byte[] { 64, 64, 64, 64, 64, 64 }); + + assertEquals("1234567890987654321", biFromString.toString()); + assertEquals("70644700037184", biFromByteArray.toString()); + assertEquals("-70644700037184", biFromSignMagnitude.toString()); + } + + @Test + public void whenLongConvertedToBigInteger_thenValueMatches() { + BigInteger bi = BigInteger.valueOf(2305843009213693951L); + + assertEquals("2305843009213693951", bi.toString()); + } + + @Test + public void givenBigIntegers_whentCompared_thenExpectedResult() { + BigInteger i = new BigInteger("123456789012345678901234567890"); + BigInteger j = new BigInteger("123456789012345678901234567891"); + BigInteger k = new BigInteger("123456789012345678901234567892"); + + assertTrue(i.compareTo(i) == 0); + assertTrue(j.compareTo(i) > 0); + assertTrue(j.compareTo(k) < 0); + } + + @Test + public void givenBigIntegers_whenPerformingArithmetic_thenExpectedResult() { + BigInteger i = new BigInteger("4"); + BigInteger j = new BigInteger("2"); + + BigInteger sum = i.add(j); + BigInteger difference = i.subtract(j); + BigInteger quotient = i.divide(j); + BigInteger product = i.multiply(j); + + assertEquals(new BigInteger("6"), sum); + assertEquals(new BigInteger("2"), difference); + assertEquals(new BigInteger("2"), quotient); + assertEquals(new BigInteger("8"), product); + } + + @Test + public void givenBigIntegers_whenPerformingBitOperations_thenExpectedResult() { + BigInteger i = new BigInteger("17"); + BigInteger j = new BigInteger("7"); + + BigInteger and = i.and(j); + BigInteger or = i.or(j); + BigInteger not = j.not(); + BigInteger xor = i.xor(j); + BigInteger andNot = i.andNot(j); + BigInteger shiftLeft = i.shiftLeft(1); + BigInteger shiftRight = i.shiftRight(1); + + assertEquals(new BigInteger("1"), and); + assertEquals(new BigInteger("23"), or); + assertEquals(new BigInteger("-8"), not); + assertEquals(new BigInteger("22"), xor); + assertEquals(new BigInteger("16"), andNot); + assertEquals(new BigInteger("34"), shiftLeft); + assertEquals(new BigInteger("8"), shiftRight); + } + + @Test + public void givenBigIntegers_whenPerformingBitManipulations_thenExpectedResult() { + BigInteger i = new BigInteger("1018"); + + int bitCount = i.bitCount(); + int bitLength = i.bitLength(); + int getLowestSetBit = i.getLowestSetBit(); + boolean testBit3 = i.testBit(3); + BigInteger setBit12 = i.setBit(12); + BigInteger flipBit0 = i.flipBit(0); + BigInteger clearBit3 = i.clearBit(3); + + assertEquals(8, bitCount); + assertEquals(10, bitLength); + assertEquals(1, getLowestSetBit); + assertEquals(true, testBit3); + assertEquals(new BigInteger("5114"), setBit12); + assertEquals(new BigInteger("1019"), flipBit0); + assertEquals(new BigInteger("1010"), clearBit3); + } + + @Test + public void givenBigIntegers_whenModularCalculation_thenExpectedResult() { + BigInteger i = new BigInteger("31"); + BigInteger j = new BigInteger("24"); + BigInteger k = new BigInteger("16"); + + BigInteger gcd = j.gcd(k); + BigInteger multiplyAndmod = j.multiply(k) + .mod(i); + BigInteger modInverse = j.modInverse(i); + BigInteger modPow = j.modPow(k, i); + + assertEquals(new BigInteger("8"), gcd); + assertEquals(new BigInteger("12"), multiplyAndmod); + assertEquals(new BigInteger("22"), modInverse); + assertEquals(new BigInteger("7"), modPow); + } + + @Test + public void givenBigIntegers_whenPrimeOperations_thenExpectedResult() { + BigInteger i = BigInteger.probablePrime(100, new Random()); + + boolean isProbablePrime = i.isProbablePrime(1000); + assertEquals(true, isProbablePrime); + } +} diff --git a/java-streams/README.md b/java-streams/README.md index 4bfcabb7cf..548d4b6a33 100644 --- a/java-streams/README.md +++ b/java-streams/README.md @@ -12,3 +12,4 @@ - [Iterable to Stream in Java](http://www.baeldung.com/java-iterable-to-stream) - [How to Iterate Over a Stream With Indices](http://www.baeldung.com/java-stream-indices) - [Primitive Type Streams in Java 8](http://www.baeldung.com/java-8-primitive-streams) +- [Stream Ordering in Java](https://www.baeldung.com/java-stream-ordering) diff --git a/java-streams/pom.xml b/java-streams/pom.xml index 4f8651a756..e4670c268d 100644 --- a/java-streams/pom.xml +++ b/java-streams/pom.xml @@ -86,23 +86,6 @@ - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - - org.apache.maven.plugins maven-compiler-plugin @@ -122,7 +105,7 @@ 3.5 1.16.12 0.9.0 - 1.13 + 1.15 0.6.5 2.10 diff --git a/java-streams/src/test/java/com/baeldung/protonpack/ProtonpackUnitTest.java b/java-streams/src/test/java/com/baeldung/protonpack/ProtonpackUnitTest.java new file mode 100644 index 0000000000..1b64c8924a --- /dev/null +++ b/java-streams/src/test/java/com/baeldung/protonpack/ProtonpackUnitTest.java @@ -0,0 +1,209 @@ +package com.baeldung.protonpack; + +import com.codepoetics.protonpack.Indexed; +import com.codepoetics.protonpack.StreamUtils; +import com.codepoetics.protonpack.collectors.CollectorUtils; +import com.codepoetics.protonpack.collectors.NonUniqueValueException; +import com.codepoetics.protonpack.selectors.Selector; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static java.util.Arrays.asList; +import static java.util.Arrays.stream; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +@SuppressWarnings("unchecked") +public class ProtonpackUnitTest { + @Test + public void whenTakeWhile_thenTakenWhile() { + Stream streamOfInt = Stream.iterate(1, i -> i + 1); + List result = StreamUtils.takeWhile(streamOfInt, i -> i < 5).collect(Collectors.toList()); + assertThat(result).contains(1, 2, 3, 4); + } + + @Test + public void whenTakeUntil_thenTakenUntil() { + Stream streamOfInt = Stream.iterate(1, i -> i + 1); + List result = StreamUtils.takeUntil(streamOfInt, i -> i > 50).collect(Collectors.toList()); + assertThat(result).contains(10, 20, 30, 40); + } + + @Test + public void givenMultipleStream_whenZipped_thenZipped() { + String[] clubs = { "Juventus", "Barcelona", "Liverpool", "PSG" }; + String[] players = { "Ronaldo", "Messi", "Salah" }; + Set zippedFrom2Sources = StreamUtils.zip(stream(clubs), stream(players), (club, player) -> club + " " + player) + .collect(Collectors.toSet()); + assertThat(zippedFrom2Sources).contains("Juventus Ronaldo", "Barcelona Messi", "Liverpool Salah"); + + String[] leagues = { "Serie A", "La Liga", "Premier League" }; + Set zippedFrom3Sources = StreamUtils.zip(stream(clubs), stream(players), stream(leagues), + (club, player, league) -> club + " " + player + " " + league).collect(Collectors.toSet()); + assertThat(zippedFrom3Sources).contains("Juventus Ronaldo Serie A", "Barcelona Messi La Liga", + "Liverpool Salah Premier League"); + } + + @Test + public void whenZippedWithIndex_thenZippedWithIndex() { + Stream streamOfClubs = Stream.of("Juventus", "Barcelona", "Liverpool"); + Set> zipsWithIndex = StreamUtils.zipWithIndex(streamOfClubs).collect(Collectors.toSet()); + assertThat(zipsWithIndex).contains(Indexed.index(0, "Juventus"), Indexed.index(1, "Barcelona"), + Indexed.index(2, "Liverpool")); + } + + @Test + public void givenMultipleStream_whenMerged_thenMerged() { + Stream streamOfClubs = Stream.of("Juventus", "Barcelona", "Liverpool", "PSG"); + Stream streamOfPlayers = Stream.of("Ronaldo", "Messi", "Salah"); + Stream streamOfLeagues = Stream.of("Serie A", "La Liga", "Premier League"); + + Set merged = StreamUtils.merge(() -> "", (valOne, valTwo) -> valOne + " " + valTwo, streamOfClubs, + streamOfPlayers, streamOfLeagues).collect(Collectors.toSet()); + + assertThat(merged).contains(" Juventus Ronaldo Serie A", " Barcelona Messi La Liga", " Liverpool Salah Premier League", + " PSG"); + } + + @Test + public void givenMultipleStream_whenMergedToList_thenMergedToList() { + Stream streamOfClubs = Stream.of("Juventus", "Barcelona", "PSG"); + Stream streamOfPlayers = Stream.of("Ronaldo", "Messi"); + + List> mergedListOfList = StreamUtils.mergeToList(streamOfClubs, streamOfPlayers) + .collect(Collectors.toList()); + assertThat(mergedListOfList.get(0)).isInstanceOf(List.class); + assertThat(mergedListOfList.get(0)).containsExactly("Juventus", "Ronaldo"); + assertThat(mergedListOfList.get(1)).containsExactly("Barcelona", "Messi"); + assertThat(mergedListOfList.get(2)).containsExactly("PSG"); + } + + @Test + public void givenMultipleStream_whenInterleaved_thenInterleaved() { + Stream streamOfClubs = Stream.of("Juventus", "Barcelona", "Liverpool"); + Stream streamOfPlayers = Stream.of("Ronaldo", "Messi"); + Stream streamOfLeagues = Stream.of("Serie A", "La Liga"); + + AtomicInteger counter = new AtomicInteger(0); + Selector roundRobinSelector = (o) -> { + Object[] vals = (Object[]) o; + while (counter.get() >= vals.length || vals[counter.get()] == null) { + if (counter.incrementAndGet() >= vals.length) + counter.set(0); + } + return counter.getAndIncrement(); + }; + Stream interleavedStream = StreamUtils.interleave(roundRobinSelector, streamOfClubs, streamOfPlayers, + streamOfLeagues); + List interleavedList = interleavedStream.collect(Collectors.toList()); + assertThat(interleavedList).containsExactly("Juventus", "Ronaldo", "Serie A", "Barcelona", "Messi", "La Liga", + "Liverpool"); + } + + @Test + public void whenSkippedUntil_thenSkippedUntil() { + Integer[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + List skippedUntilGreaterThan5 = StreamUtils.skipUntil(stream(numbers), i -> i > 5).collect(Collectors.toList()); + assertThat(skippedUntilGreaterThan5).containsExactly(6, 7, 8, 9, 10); + + List skippedUntilLessThanEquals5 = StreamUtils.skipUntil(stream(numbers), i -> i <= 5) + .collect(Collectors.toList()); + assertThat(skippedUntilLessThanEquals5).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + } + + @Test + public void whenSkippedWhile_thenSkippedWhile() { + Integer[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + List skippedWhileLessThanEquals5 = StreamUtils.skipWhile(stream(numbers), i -> i <= 5) + .collect(Collectors.toList()); + assertThat(skippedWhileLessThanEquals5).containsExactly(6, 7, 8, 9, 10); + + List skippedWhileGreaterThan5 = StreamUtils.skipWhile(stream(numbers), i -> i > 5).collect(Collectors.toList()); + assertThat(skippedWhileGreaterThan5).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + } + + @Test + public void givenFibonacciGenerator_whenUnfolded_thenUnfolded() { + AtomicInteger lastValue = new AtomicInteger(0); + Function> fibonacciGenerator = (i) -> (i < 10) ? + Optional.of(i + lastValue.getAndSet(i)) : + Optional.empty(); + + List fib = StreamUtils.unfold(1, fibonacciGenerator).collect(Collectors.toList()); + assertThat(fib).containsExactly(1, 1, 2, 3, 5, 8, 13); + } + + @Test + public void whenWindowed_thenWindowed() { + Integer[] numbers = { 1, 2, 3, 4, 5, 6, 7 }; + + List> windowedWithSkip1 = StreamUtils.windowed(stream(numbers), 3, 1).collect(Collectors.toList()); + assertThat(windowedWithSkip1).containsExactly(asList(1, 2, 3), asList(2, 3, 4), asList(3, 4, 5), asList(4, 5, 6), + asList(5, 6, 7)); + + List> windowedWithSkip2 = StreamUtils.windowed(stream(numbers), 3, 2).collect(Collectors.toList()); + assertThat(windowedWithSkip2).containsExactly(asList(1, 2, 3), asList(3, 4, 5), asList(5, 6, 7)); + } + + @Test + public void whenAggregated_thenAggregated() { + Integer[] numbers = { 1, 2, 2, 3, 4, 4, 4, 5 }; + List> aggregated = StreamUtils.aggregate(stream(numbers), (int1, int2) -> int1.compareTo(int2) == 0) + .collect(Collectors.toList()); + assertThat(aggregated).containsExactly(asList(1), asList(2, 2), asList(3), asList(4, 4, 4), asList(5)); + + List> aggregatedFixSize = StreamUtils.aggregate(stream(numbers), 5).collect(Collectors.toList()); + assertThat(aggregatedFixSize).containsExactly(asList(1, 2, 2, 3, 4), asList(4, 4, 5)); + } + + @Test + public void whenGroupedRun_thenGroupedRun() { + Integer[] numbers = { 1, 1, 2, 3, 4, 4, 5 }; + List> grouped = StreamUtils.groupRuns(stream(numbers)).collect(Collectors.toList()); + assertThat(grouped).containsExactly(asList(1, 1), asList(2), asList(3), asList(4, 4), asList(5)); + + Integer[] numbers2 = { 1, 2, 3, 1 }; + List> grouped2 = StreamUtils.groupRuns(stream(numbers2)).collect(Collectors.toList()); + assertThat(grouped2).containsExactly(asList(1), asList(2), asList(3), asList(1)); + } + + @Test + public void whenAggregatedOnListCondition_thenAggregatedOnListCondition() { + Integer[] numbers = { 1, 1, 2, 3, 4, 4, 5 }; + Stream> aggregated = StreamUtils.aggregateOnListCondition(stream(numbers), + (currentList, nextInt) -> currentList.stream().mapToInt(Integer::intValue).sum() + nextInt <= 5); + assertThat(aggregated).containsExactly(asList(1, 1, 2), asList(3), asList(4), asList(4), asList(5)); + } + + @Test + public void givenProjectionFunction_whenMaxedBy_thenMaxedBy() { + Stream clubs = Stream.of("Juventus", "Barcelona", "PSG"); + Optional longestName = clubs.collect(CollectorUtils.maxBy(String::length)); + assertThat(longestName.get()).isEqualTo("Barcelona"); + } + + @Test + public void givenStreamOfMultipleElem_whenUniqueCollector_thenValueReturned() { + Stream singleElement = Stream.of(1); + Optional unique = singleElement.collect(CollectorUtils.unique()); + assertThat(unique.get()).isEqualTo(1); + + } + + @Test + public void givenStreamOfMultipleElem_whenUniqueCollector_thenExceptionThrown() { + Stream multipleElement = Stream.of(1, 2, 3); + assertThatExceptionOfType(NonUniqueValueException.class).isThrownBy(() -> { + multipleElement.collect(CollectorUtils.unique()); + }); + } + +} diff --git a/java-streams/src/test/java/com/baeldung/streamordering/BenchmarkUnitTest.java b/java-streams/src/test/java/com/baeldung/streamordering/BenchmarkManualTest.java similarity index 98% rename from java-streams/src/test/java/com/baeldung/streamordering/BenchmarkUnitTest.java rename to java-streams/src/test/java/com/baeldung/streamordering/BenchmarkManualTest.java index ba1cb1f726..656a6d95f9 100644 --- a/java-streams/src/test/java/com/baeldung/streamordering/BenchmarkUnitTest.java +++ b/java-streams/src/test/java/com/baeldung/streamordering/BenchmarkManualTest.java @@ -15,7 +15,7 @@ import java.util.concurrent.TimeUnit; import java.util.stream.IntStream; -public class BenchmarkUnitTest +public class BenchmarkManualTest { public void diff --git a/java-strings/README.md b/java-strings/README.md index d5b9b45b20..233d986d98 100644 --- a/java-strings/README.md +++ b/java-strings/README.md @@ -24,4 +24,6 @@ - [Check If a String Is Numeric in Java](http://www.baeldung.com/java-check-string-number) - [Why Use char[] Array Over a String for Storing Passwords in Java?](http://www.baeldung.com/java-storing-passwords) - [Convert a String to Title Case](http://www.baeldung.com/java-string-title-case) -- [Compact Strings in Java 9](http://www.baeldung.com/java-9-compact-string) \ No newline at end of file +- [Compact Strings in Java 9](http://www.baeldung.com/java-9-compact-string) +- [Java Check a String for Lowercase/Uppercase Letter, Special Character and Digit](https://www.baeldung.com/java-lowercase-uppercase-special-character-digit-regex) +- [Convert java.util.Date to String](https://www.baeldung.com/java-util-date-to-string) diff --git a/java-strings/pom.xml b/java-strings/pom.xml index 0c83b4d9e7..b1ba49b33a 100644 --- a/java-strings/pom.xml +++ b/java-strings/pom.xml @@ -47,11 +47,21 @@ jmh-core ${jmh-core.version} + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh-core.version} + com.ibm.icu icu4j ${icu4j.version} + + com.google.guava + guava + ${guava.version} + com.vdurmont @@ -71,23 +81,6 @@ - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - - org.apache.maven.plugins maven-compiler-plugin @@ -109,6 +102,7 @@ 3.6.1 1.19 61.1 + 26.0-jre \ No newline at end of file diff --git a/java-strings/src/main/java/com/baeldung/string/JoinerSplitter.java b/java-strings/src/main/java/com/baeldung/string/JoinerSplitter.java index 085be66801..cdbba1ef53 100644 --- a/java-strings/src/main/java/com/baeldung/string/JoinerSplitter.java +++ b/java-strings/src/main/java/com/baeldung/string/JoinerSplitter.java @@ -2,6 +2,7 @@ package com.baeldung.string; import java.util.Arrays; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -32,5 +33,12 @@ public class JoinerSplitter { .mapToObj(item -> (char) item) .collect(Collectors.toList()); } + + public static Map arrayToMap(String[] arrayOfString) { + return Arrays.asList(arrayOfString) + .stream() + .map(str -> str.split(":")) + .collect(Collectors.toMap(str -> str[0], str -> str[1])); + } } diff --git a/java-strings/src/main/java/com/baeldung/string/StringPerformance.java b/java-strings/src/main/java/com/baeldung/string/StringPerformance.java new file mode 100644 index 0000000000..4873bd320c --- /dev/null +++ b/java-strings/src/main/java/com/baeldung/string/StringPerformance.java @@ -0,0 +1,163 @@ +package com.baeldung.string; + +import org.apache.commons.lang3.StringUtils; +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import java.util.List; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.SingleShotTime) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@Measurement(batchSize = 10000, iterations = 10) +@Warmup(batchSize = 10000, iterations = 10) +public class StringPerformance extends StringPerformanceHints { + + @Benchmark + public String benchmarkStringDynamicConcat() { + return dynamicConcat(); + } + + @Benchmark + public StringBuilder benchmarkStringBuilder() { + StringBuilder stringBuilder = new StringBuilder(result); + stringBuilder.append(baeldung); + return stringBuilder; + } + + @Benchmark + public StringBuffer benchmarkStringBuffer() { + StringBuffer stringBuffer = new StringBuffer(result); + stringBuffer.append(baeldung); + return stringBuffer; + } + + @Benchmark + public String benchmarkStringConstructor() { + return stringConstructor(); + } + + @Benchmark + public String benchmarkStringLiteral() { + return stringLiteral(); + } + + @Benchmark + public String benchmarkStringFormat_s() { + return stringFormat_s(); + } + + @Benchmark + public String benchmarkStringConcat() { + return stringConcat(); + } + + @Benchmark + public String benchmarkStringIntern() { + return stringIntern(); + } + + @Benchmark + public String benchmarkStringReplace() { + return longString.replace("average", " average !!!"); + } + + @Benchmark + public String benchmarkStringUtilsReplace() { + return StringUtils.replace(longString, "average", " average !!!"); + } + + @Benchmark + public List benchmarkGuavaSplitter() { + return guavaSplitter(); + } + + @Benchmark + public String [] benchmarkStringSplit() { + return stringSplit(); + } + + @Benchmark + public String [] benchmarkStringSplitPattern() { + return stringSplitPattern(); + } + + @Benchmark + public List benchmarkStringTokenizer() { + return stringTokenizer(); + } + + @Benchmark + public List benchmarkStringIndexOf() { + return stringIndexOf(); + } + + + @Benchmark + public String benchmarkIntegerToString() { + return stringIntegerToString(); + } + + @Benchmark + public String benchmarkStringValueOf() { + return stringValueOf(); + } + + + @Benchmark + public String benchmarkStringConvertPlus() { + return stringConvertPlus(); + } + + @Benchmark + public String benchmarkStringFormat_d() { + return stringFormat_d(); + } + + @Benchmark + public boolean benchmarkStringEquals() { + return stringEquals(); + } + + + @Benchmark + public boolean benchmarkStringEqualsIgnoreCase() { + return stringEqualsIgnoreCase(); + } + + @Benchmark + public boolean benchmarkStringMatches() { + return stringIsMatch(); + } + + @Benchmark + public boolean benchmarkPrecompiledMatches() { + return precompiledMatches(); + } + + @Benchmark + public int benchmarkStringCompareTo() { + return stringCompareTo(); + } + + @Benchmark + public boolean benchmarkStringIsEmpty() { + return stringIsEmpty(); + } + + @Benchmark + public boolean benchmarkStringLengthZero() { + return stringLengthZero(); + } + + public static void main(String[] args) throws Exception { + Options options = new OptionsBuilder() + .include(StringPerformance.class.getSimpleName()).threads(1) + .forks(1).shouldFailOnError(true) + .shouldDoGC(true) + .jvmArgs("-server").build(); + new Runner(options).run(); + } +} diff --git a/java-strings/src/main/java/com/baeldung/string/StringPerformanceHints.java b/java-strings/src/main/java/com/baeldung/string/StringPerformanceHints.java new file mode 100644 index 0000000000..509222136f --- /dev/null +++ b/java-strings/src/main/java/com/baeldung/string/StringPerformanceHints.java @@ -0,0 +1,133 @@ +package com.baeldung.string; + +import com.google.common.base.Splitter; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; + +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; +import java.util.regex.Pattern; + +@State(Scope.Thread) +public class StringPerformanceHints { + + protected String baeldung = "baeldung"; + protected String longString = "Hello baeldung, I am a bit longer than other Strings"; + protected String formatString = "hello %s, nice to meet you"; + protected String formatDigit = "%d"; + protected String emptyString = " "; + protected String result = ""; + + protected int sampleNumber = 100; + + protected Pattern spacePattern = Pattern.compile(emptyString); + protected Pattern longPattern = Pattern.compile(longString); + protected List stringSplit = new ArrayList<>(); + protected List stringTokenizer = new ArrayList<>(); + + protected String dynamicConcat() { + result += baeldung; + return result; + } + + protected String stringConstructor() { + return new String(baeldung); + } + + protected String stringLiteral() { + result = baeldung; + return result; + } + + protected String stringFormat_s() { + return String.format(formatString, baeldung); + } + + protected String stringFormat_d() { + return String.format(formatDigit, sampleNumber); + } + + protected String stringConcat() { + result = result.concat(baeldung); + return result; + } + + protected List stringTokenizer() { + StringTokenizer st = new StringTokenizer(longString); + while (st.hasMoreTokens()) { + stringTokenizer.add(st.nextToken()); + } + return stringTokenizer; + } + + protected List stringIndexOf() { + int pos = 0, end; + while ((end = longString.indexOf(' ', pos)) >= 0) { + stringSplit.add(longString.substring(pos, end)); + pos = end + 1; + } + return stringSplit; + } + + protected String stringIntegerToString() { + return Integer.toString(sampleNumber); + } + + protected String stringValueOf() { + return String.valueOf(sampleNumber); + } + + + protected String stringConvertPlus() { + return sampleNumber + ""; + } + + + protected boolean stringEquals() { + return longString.equals(baeldung); + } + + + protected boolean stringEqualsIgnoreCase() { + return longString.equalsIgnoreCase(baeldung); + } + + protected boolean stringIsMatch() { + return longString.matches(baeldung); + } + + protected boolean precompiledMatches() { + return longPattern.matcher(baeldung).matches(); + } + + protected int stringCompareTo() { + return longString.compareTo(baeldung); + } + + protected boolean stringIsEmpty() { + return longString.isEmpty(); + } + + protected boolean stringLengthZero() { + return longString.length() == 0; + } + + protected String [] stringSplitPattern() { + return spacePattern.split(longString, 0); + } + + protected String [] stringSplit() { + return longString.split(emptyString); + } + + protected List guavaSplitter() { + return Splitter.on(" ").trimResults() + .omitEmptyStrings() + .splitToList(longString); + } + + protected String stringIntern() { + return baeldung.intern(); + } +} diff --git a/java-strings/src/test/java/com/baeldung/string/JoinerSplitterUnitTest.java b/java-strings/src/test/java/com/baeldung/string/JoinerSplitterUnitTest.java index 8901bcf9db..a9488e27a4 100644 --- a/java-strings/src/test/java/com/baeldung/string/JoinerSplitterUnitTest.java +++ b/java-strings/src/test/java/com/baeldung/string/JoinerSplitterUnitTest.java @@ -3,7 +3,9 @@ package com.baeldung.string; import org.junit.Test; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import static org.junit.Assert.assertEquals; @@ -62,5 +64,20 @@ public class JoinerSplitterUnitTest { assertEquals(result, expectation); } + + @Test + public void givenStringArray_transformedToStream_convertToMap() { + + String[] programming_languages = new String[] {"language:java","os:linux","editor:emacs"}; + + Map expectation=new HashMap<>(); + expectation.put("language", "java"); + expectation.put("os", "linux"); + expectation.put("editor", "emacs"); + + Map result = JoinerSplitter.arrayToMap(programming_languages); + assertEquals(result, expectation); + + } } diff --git a/java-strings/src/test/java/com/baeldung/string/RemovingEmojiFromStringUnitTest.java b/java-strings/src/test/java/com/baeldung/string/RemovingEmojiFromStringUnitTest.java index 163f28d0d8..8688f9dcf5 100644 --- a/java-strings/src/test/java/com/baeldung/string/RemovingEmojiFromStringUnitTest.java +++ b/java-strings/src/test/java/com/baeldung/string/RemovingEmojiFromStringUnitTest.java @@ -1,8 +1,6 @@ package com.baeldung.string; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.not; -import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertEquals; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -12,35 +10,28 @@ import org.junit.Test; import com.vdurmont.emoji.EmojiParser; public class RemovingEmojiFromStringUnitTest { - String text = "la conférence, commencera à 10 heures 😅 ✿"; + String text = "la conférence, commencera à 10 heures 😅"; String regex = "[^\\p{L}\\p{N}\\p{P}\\p{Z}]"; @Test public void whenRemoveEmojiUsingLibrary_thenSuccess() { String result = EmojiParser.removeAllEmojis(text); System.out.println(result); - assertThat(result, not(containsString("😅"))); - assertThat(result, containsString("à")); - assertThat(result, containsString("la")); - assertThat(result, containsString("10")); + assertEquals(result, "la conférence, commencera à 10 heures "); } @Test public void whenReplaceEmojiUsingLibrary_thenSuccess() { String result = EmojiParser.parseToAliases(text); System.out.println(result); - assertThat(result, not(containsString("😅"))); - assertThat(result, containsString("sweat_smile")); + assertEquals(result, "la conférence, commencera à 10 heures :sweat_smile:"); } @Test public void whenRemoveEmojiUsingRegex_thenSuccess() { String result = text.replaceAll(regex, ""); System.out.println(result); - assertThat(result, not(containsString("😅"))); - assertThat(result, containsString("à")); - assertThat(result, containsString("la")); - assertThat(result, containsString("10")); + assertEquals(result, "la conférence, commencera à 10 heures "); } @Test @@ -50,29 +41,20 @@ public class RemovingEmojiFromStringUnitTest { String result = matcher.replaceAll(""); System.out.println(result); - assertThat(result, not(containsString("😅"))); - assertThat(result, containsString("à")); - assertThat(result, containsString("la")); - assertThat(result, containsString("10")); + assertEquals(result, "la conférence, commencera à 10 heures "); } @Test public void whenRemoveEmojiUsingCodepoints_thenSuccess() { String result = text.replaceAll("[\\x{0001f300}-\\x{0001f64f}]|[\\x{0001f680}-\\x{0001f6ff}]", ""); System.out.println(result); - assertThat(result, not(containsString("😅"))); - assertThat(result, containsString("à")); - assertThat(result, containsString("la")); - assertThat(result, containsString("10")); + assertEquals(result, "la conférence, commencera à 10 heures "); } @Test public void whenRemoveEmojiUsingUnicode_thenSuccess() { String result = text.replaceAll("[\ud83c\udf00-\ud83d\ude4f]|[\ud83d\ude80-\ud83d\udeff]", ""); System.out.println(result); - assertThat(result, not(containsString("😅"))); - assertThat(result, containsString("à")); - assertThat(result, containsString("la")); - assertThat(result, containsString("10")); + assertEquals(result, "la conférence, commencera à 10 heures "); } } diff --git a/java-strings/src/test/java/com/baeldung/string/SplitUnitTest.java b/java-strings/src/test/java/com/baeldung/string/SplitUnitTest.java index 3859a2b26b..6157640a4a 100644 --- a/java-strings/src/test/java/com/baeldung/string/SplitUnitTest.java +++ b/java-strings/src/test/java/com/baeldung/string/SplitUnitTest.java @@ -2,10 +2,11 @@ package com.baeldung.string; import static org.assertj.core.api.Assertions.assertThat; +import java.util.Arrays; import java.util.List; import org.apache.commons.lang3.StringUtils; -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.google.common.base.Splitter; @@ -54,4 +55,18 @@ public class SplitUnitTest { assertThat(resultList) .containsExactly("car", "jeep", "scooter"); } + + @Test + public void givenStringContainsSpaces_whenSplitAndTrim_thenReturnsArray_using_Regex() { + assertThat(" car , jeep, scooter ".trim() + .split("\\s*,\\s*")).containsExactly("car", "jeep", "scooter"); + + } + + @Test + public void givenStringContainsSpaces_whenSplitAndTrim_thenReturnsArray_using_java_8() { + assertThat(Arrays.stream(" car , jeep, scooter ".split(",")) + .map(String::trim) + .toArray(String[]::new)).containsExactly("car", "jeep", "scooter"); + } } diff --git a/java-strings/src/test/java/com/baeldung/string/StringEmptyUnitTest.java b/java-strings/src/test/java/com/baeldung/string/StringEmptyUnitTest.java new file mode 100644 index 0000000000..17b13f89de --- /dev/null +++ b/java-strings/src/test/java/com/baeldung/string/StringEmptyUnitTest.java @@ -0,0 +1,51 @@ +package com.baeldung.string; + +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.text.IsEmptyString.isEmptyOrNullString; +import static org.hamcrest.text.IsEmptyString.isEmptyString; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import org.apache.commons.lang3.StringUtils; +import org.assertj.core.api.Assertions; +import org.junit.Test; + +import com.google.common.base.Strings; + +public class StringEmptyUnitTest { + + private String text = "baeldung"; + + @Test + public void givenAString_whenCheckedForEmptyUsingJunit_shouldAssertSuccessfully() { + assertTrue(!text.isEmpty()); + assertFalse(text.isEmpty()); + assertNotEquals("", text); + assertNotSame("", text); + } + + @Test + public void givenAString_whenCheckedForEmptyUsingHamcrest_shouldAssertSuccessfully() { + assertThat(text, not(isEmptyString())); + assertThat(text, not(isEmptyOrNullString())); + } + + @Test + public void givenAString_whenCheckedForEmptyUsingCommonsLang_shouldAssertSuccessfully() { + assertTrue(StringUtils.isNotBlank(text)); + } + + @Test + public void givenAString_whenCheckedForEmptyUsingAssertJ_shouldAssertSuccessfully() { + Assertions.assertThat(text).isNotEmpty(); + } + + @Test + public void givenAString_whenCheckedForEmptyUsingGuava_shouldAssertSuccessfully() { + assertFalse(Strings.isNullOrEmpty(text)); + } + +} diff --git a/java-strings/src/test/java/com/baeldung/string/SubstringUnitTest.java b/java-strings/src/test/java/com/baeldung/string/SubstringUnitTest.java new file mode 100644 index 0000000000..3a4e231828 --- /dev/null +++ b/java-strings/src/test/java/com/baeldung/string/SubstringUnitTest.java @@ -0,0 +1,62 @@ +package com.baeldung.string; + +import java.util.Scanner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.commons.lang3.StringUtils; +import org.junit.Assert; +import org.junit.Test; + +public class SubstringUnitTest { + + String text = "Julia Evans was born on 25-09-1984. She is currently living in the USA (United States of America)."; + + @Test + public void givenAString_whenUsedStringUtils_ShouldReturnProperSubstring() { + Assert.assertEquals("United States of America", StringUtils.substringBetween(text, "(", ")")); + Assert.assertEquals("the USA (United States of America).", StringUtils.substringAfter(text, "living in ")); + Assert.assertEquals("Julia Evans", StringUtils.substringBefore(text, " was born")); + } + + @Test + public void givenAString_whenUsedScanner_ShouldReturnProperSubstring() { + try (Scanner scanner = new Scanner(text)) { + scanner.useDelimiter("\\."); + Assert.assertEquals("Julia Evans was born on 25-09-1984", scanner.next()); + } + } + + @Test + public void givenAString_whenUsedSplit_ShouldReturnProperSubstring() { + String[] sentences = text.split("\\."); + Assert.assertEquals("Julia Evans was born on 25-09-1984", sentences[0]); + } + + @Test + public void givenAString_whenUsedRegex_ShouldReturnProperSubstring() { + Pattern pattern = Pattern.compile("\\d{2}\\-\\d{2}-\\d{4}"); + Matcher matcher = pattern.matcher(text); + + if (matcher.find()) { + Assert.assertEquals("25-09-1984", matcher.group()); + } + } + + @Test + public void givenAString_whenUsedSubSequence_ShouldReturnProperSubstring() { + Assert.assertEquals("USA (United States of America)", text.subSequence(67, text.length() - 1)); + } + + @Test + public void givenAString_whenUsedSubstring_ShouldReturnProperSubstring() { + Assert.assertEquals("USA (United States of America).", text.substring(67)); + Assert.assertEquals("USA (United States of America)", text.substring(67, text.length() - 1)); + } + + @Test + public void givenAString_whenUsedSubstringWithIndexOf_ShouldReturnProperSubstring() { + Assert.assertEquals("United States of America", text.substring(text.indexOf('(') + 1, text.indexOf(')'))); + } + +} diff --git a/javaxval/bin/.gitignore b/javaxval/bin/.gitignore deleted file mode 100644 index 55a6bcf596..0000000000 --- a/javaxval/bin/.gitignore +++ /dev/null @@ -1,6 +0,0 @@ -.classpath -.project -.settings/ -target/ - - diff --git a/javaxval/bin/pom.xml b/javaxval/bin/pom.xml deleted file mode 100644 index a16072bb1a..0000000000 --- a/javaxval/bin/pom.xml +++ /dev/null @@ -1,44 +0,0 @@ - - 4.0.0 - com.baeldung - javaxval - 0.1-SNAPSHOT - - - - - - javax.validation - validation-api - 1.1.0.Final - - - - org.hibernate - hibernate-validator - 5.2.1.Final - - - - org.hibernate - hibernate-validator-annotation-processor - 5.2.1.Final - - - - javax.el - javax.el-api - 2.2.4 - - - - org.glassfish.web - javax.el - 2.2.4 - - - - - - \ No newline at end of file diff --git a/jee-7/pom.xml b/jee-7/pom.xml index fbf102185d..08c8b5a068 100644 --- a/jee-7/pom.xml +++ b/jee-7/pom.xml @@ -384,10 +384,6 @@ - - ${maven.min.version} - - bintray-mvc-spec-maven diff --git a/jersey/README.md b/jersey/README.md index a4c8c52d68..3121c560cf 100644 --- a/jersey/README.md +++ b/jersey/README.md @@ -1 +1,2 @@ -- [Jersey Filters and Interceptors] (http://www.baeldung.com/jersey-filters-interceptors) +- [Jersey Filters and Interceptors](http://www.baeldung.com/jersey-filters-interceptors) +- [Jersey MVC Support](https://www.baeldung.com/jersey-mvc) diff --git a/jersey/pom.xml b/jersey/pom.xml index e248f9cf90..b55bdc5330 100644 --- a/jersey/pom.xml +++ b/jersey/pom.xml @@ -39,6 +39,11 @@ jersey-mvc-freemarker ${jersey.version} + + org.glassfish.jersey.ext + jersey-bean-validation + ${jersey.version} + org.glassfish.jersey.test-framework jersey-test-framework-core diff --git a/jersey/src/main/java/com/baeldung/jersey/server/config/ViewApplicationConfig.java b/jersey/src/main/java/com/baeldung/jersey/server/config/ViewApplicationConfig.java index d4744066c4..b6b9853aae 100644 --- a/jersey/src/main/java/com/baeldung/jersey/server/config/ViewApplicationConfig.java +++ b/jersey/src/main/java/com/baeldung/jersey/server/config/ViewApplicationConfig.java @@ -1,12 +1,14 @@ package com.baeldung.jersey.server.config; import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.server.ServerProperties; import org.glassfish.jersey.server.mvc.freemarker.FreemarkerMvcFeature; public class ViewApplicationConfig extends ResourceConfig { public ViewApplicationConfig() { packages("com.baeldung.jersey.server"); + property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true); property(FreemarkerMvcFeature.TEMPLATE_BASE_PATH, "templates/freemarker"); register(FreemarkerMvcFeature.class);; } diff --git a/jersey/src/main/java/com/baeldung/jersey/server/constraints/SerialNumber.java b/jersey/src/main/java/com/baeldung/jersey/server/constraints/SerialNumber.java new file mode 100644 index 0000000000..ca49797e31 --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/server/constraints/SerialNumber.java @@ -0,0 +1,35 @@ +package com.baeldung.jersey.server.constraints; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.util.regex.Pattern; + +import javax.validation.Constraint; +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import javax.validation.Payload; + +@Retention(RetentionPolicy.RUNTIME) +@Constraint(validatedBy = { SerialNumber.Validator.class }) +public @interface SerialNumber { + + String message() + + default "Fruit serial number is not valid"; + + Class[] groups() default {}; + + Class[] payload() default {}; + + public class Validator implements ConstraintValidator { + @Override + public void initialize(final SerialNumber serial) { + } + + @Override + public boolean isValid(final String serial, final ConstraintValidatorContext constraintValidatorContext) { + final String serialNumRegex = "^\\d{3}-\\d{3}-\\d{4}$"; + return Pattern.matches(serialNumRegex, serial); + } + } +} diff --git a/jersey/src/main/java/com/baeldung/jersey/server/model/Fruit.java b/jersey/src/main/java/com/baeldung/jersey/server/model/Fruit.java index 30620e331d..c55362487b 100644 --- a/jersey/src/main/java/com/baeldung/jersey/server/model/Fruit.java +++ b/jersey/src/main/java/com/baeldung/jersey/server/model/Fruit.java @@ -1,20 +1,57 @@ package com.baeldung.jersey.server.model; +import javax.validation.constraints.Min; +import javax.validation.constraints.Size; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement public class Fruit { - private final String name; - private final String colour; + @Min(value = 10, message = "Fruit weight must be 10 or greater") + private Integer weight; + @Size(min = 5, max = 200) + private String name; + @Size(min = 5, max = 200) + private String colour; + private String serial; + + public Fruit() { + } public Fruit(String name, String colour) { this.name = name; this.colour = colour; } - + public String getName() { return name; } + + public void setName(String name) { + this.name = name; + } + + public void setColour(String colour) { + this.colour = colour; + } public String getColour() { return colour; } + + public Integer getWeight() { + return weight; + } + + public void setWeight(Integer weight) { + this.weight = weight; + } + + public String getSerial() { + return serial; + } + + public void setSerial(String serial) { + this.serial = serial; + } } diff --git a/jersey/src/main/java/com/baeldung/jersey/server/providers/FruitExceptionMapper.java b/jersey/src/main/java/com/baeldung/jersey/server/providers/FruitExceptionMapper.java new file mode 100644 index 0000000000..cea61c897b --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/server/providers/FruitExceptionMapper.java @@ -0,0 +1,26 @@ +package com.baeldung.jersey.server.providers; + +import javax.validation.ConstraintViolation; +import javax.validation.ConstraintViolationException; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; + +public class FruitExceptionMapper implements ExceptionMapper { + + @Override + public Response toResponse(final ConstraintViolationException exception) { + return Response.status(Response.Status.BAD_REQUEST) + .entity(prepareMessage(exception)) + .type("text/plain") + .build(); + } + + private String prepareMessage(ConstraintViolationException exception) { + final StringBuilder message = new StringBuilder(); + for (ConstraintViolation cv : exception.getConstraintViolations()) { + message.append(cv.getPropertyPath() + " " + cv.getMessage() + "\n"); + } + return message.toString(); + } + +} diff --git a/jersey/src/main/java/com/baeldung/jersey/server/rest/FruitResource.java b/jersey/src/main/java/com/baeldung/jersey/server/rest/FruitResource.java index 4e1fa4aa11..ee34cdd3ca 100644 --- a/jersey/src/main/java/com/baeldung/jersey/server/rest/FruitResource.java +++ b/jersey/src/main/java/com/baeldung/jersey/server/rest/FruitResource.java @@ -5,7 +5,13 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import javax.validation.Valid; +import javax.validation.constraints.NotNull; +import javax.ws.rs.Consumes; +import javax.ws.rs.FormParam; import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; @@ -15,7 +21,9 @@ import org.glassfish.jersey.server.mvc.ErrorTemplate; import org.glassfish.jersey.server.mvc.Template; import org.glassfish.jersey.server.mvc.Viewable; +import com.baeldung.jersey.server.constraints.SerialNumber; import com.baeldung.jersey.server.model.Fruit; +import com.baeldung.jersey.service.SimpleStorageService; @Path("/fruit") public class FruitResource { @@ -52,4 +60,49 @@ public class FruitResource { return name; } + @POST + @Path("/create") + @Consumes(MediaType.APPLICATION_FORM_URLENCODED) + public void createFruit( + @NotNull(message = "Fruit name must not be null") @FormParam("name") String name, + @NotNull(message = "Fruit colour must not be null") @FormParam("colour") String colour) { + + Fruit fruit = new Fruit(name, colour); + SimpleStorageService.storeFruit(fruit); + } + + @PUT + @Path("/update") + @Consumes(MediaType.APPLICATION_FORM_URLENCODED) + public void updateFruit(@SerialNumber @FormParam("serial") String serial) { + Fruit fruit = new Fruit(); + fruit.setSerial(serial); + SimpleStorageService.storeFruit(fruit); + } + + @POST + @Path("/create") + @Consumes(MediaType.APPLICATION_JSON) + public void createFruit(@Valid Fruit fruit) { + SimpleStorageService.storeFruit(fruit); + } + + @GET + @Valid + @Produces(MediaType.APPLICATION_JSON) + @Path("/search/{name}") + public Fruit findFruitByName(@PathParam("name") String name) { + return SimpleStorageService.findByName(name); + } + + @GET + @Produces(MediaType.TEXT_HTML) + @Path("/exception") + @Valid + public Fruit exception() { + Fruit fruit = new Fruit(); + fruit.setName("a"); + fruit.setColour("b"); + return fruit; + } } diff --git a/jersey/src/main/java/com/baeldung/jersey/service/SimpleStorageService.java b/jersey/src/main/java/com/baeldung/jersey/service/SimpleStorageService.java new file mode 100644 index 0000000000..e21dd584a1 --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/service/SimpleStorageService.java @@ -0,0 +1,25 @@ +package com.baeldung.jersey.service; + +import java.util.HashMap; +import java.util.Map; + +import com.baeldung.jersey.server.model.Fruit; + +public class SimpleStorageService { + + private static final Map fruits = new HashMap(); + + public static void storeFruit(final Fruit fruit) { + fruits.put(fruit.getName(), fruit); + } + + public static Fruit findByName(final String name) { + return fruits.entrySet() + .stream() + .filter(map -> name.equals(map.getKey())) + .map(map -> map.getValue()) + .findFirst() + .get(); + } + +} diff --git a/jersey/src/test/java/com/baeldung/jersey/server/rest/FruitResourceIntegrationTest.java b/jersey/src/test/java/com/baeldung/jersey/server/rest/FruitResourceIntegrationTest.java index a0b6daed51..2eeb5710cb 100644 --- a/jersey/src/test/java/com/baeldung/jersey/server/rest/FruitResourceIntegrationTest.java +++ b/jersey/src/test/java/com/baeldung/jersey/server/rest/FruitResourceIntegrationTest.java @@ -2,41 +2,116 @@ package com.baeldung.jersey.server.rest; import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import javax.ws.rs.client.Entity; import javax.ws.rs.core.Application; +import javax.ws.rs.core.Form; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import org.glassfish.jersey.test.JerseyTest; -import org.junit.Assert; +import org.glassfish.jersey.test.TestProperties; import org.junit.Test; import com.baeldung.jersey.server.config.ViewApplicationConfig; +import com.baeldung.jersey.server.model.Fruit; +import com.baeldung.jersey.server.providers.FruitExceptionMapper; public class FruitResourceIntegrationTest extends JerseyTest { @Override protected Application configure() { - return new ViewApplicationConfig(); + enable(TestProperties.LOG_TRAFFIC); + enable(TestProperties.DUMP_ENTITY); + + ViewApplicationConfig config = new ViewApplicationConfig(); + config.register(FruitExceptionMapper.class); + return config; } @Test - public void testAllFruit() { + public void givenGetAllFruit_whenCorrectRequest_thenAllTemplateInvoked() { final String response = target("/fruit/all").request() .get(String.class); - Assert.assertThat(response, allOf(containsString("banana"), containsString("apple"), containsString("kiwi"))); + assertThat(response, allOf(containsString("banana"), containsString("apple"), containsString("kiwi"))); } @Test - public void testIndex() { + public void givenGetFruit_whenCorrectRequest_thenIndexTemplateInvoked() { final String response = target("/fruit").request() .get(String.class); - Assert.assertThat(response, containsString("Welcome Fruit Index Page!")); + assertThat(response, containsString("Welcome Fruit Index Page!")); } @Test - public void testErrorTemplate() { + public void givenGetFruitByName_whenFruitUnknown_thenErrorTemplateInvoked() { final String response = target("/fruit/orange").request() .get(String.class); - Assert.assertThat(response, containsString("Error - Fruit not found: orange!")); + assertThat(response, containsString("Error - Fruit not found: orange!")); + } + + @Test + public void givenCreateFruit_whenFormContainsNullParam_thenResponseCodeIsBadRequest() { + Form form = new Form(); + form.param("name", "apple"); + form.param("colour", null); + Response response = target("fruit/create").request(MediaType.APPLICATION_FORM_URLENCODED) + .post(Entity.form(form)); + + assertEquals("Http Response should be 400 ", 400, response.getStatus()); + assertThat(response.readEntity(String.class), containsString("Fruit colour must not be null")); + } + + @Test + public void givenUpdateFruit_whenFormContainsBadSerialParam_thenResponseCodeIsBadRequest() { + Form form = new Form(); + form.param("serial", "2345-2345"); + + Response response = target("fruit/update").request(MediaType.APPLICATION_FORM_URLENCODED) + .put(Entity.form(form)); + + assertEquals("Http Response should be 400 ", 400, response.getStatus()); + assertThat(response.readEntity(String.class), containsString("Fruit serial number is not valid")); + } + + @Test + public void givenCreateFruit_whenFruitIsInvalid_thenResponseCodeIsBadRequest() { + Fruit fruit = new Fruit("Blueberry", "purple"); + fruit.setWeight(1); + + Response response = target("fruit/create").request(MediaType.APPLICATION_JSON_TYPE) + .post(Entity.entity(fruit, MediaType.APPLICATION_JSON_TYPE)); + + assertEquals("Http Response should be 400 ", 400, response.getStatus()); + assertThat(response.readEntity(String.class), containsString("Fruit weight must be 10 or greater")); + } + + @Test + public void givenFruitExists_whenSearching_thenResponseContainsFruit() { + Fruit fruit = new Fruit(); + fruit.setName("strawberry"); + fruit.setWeight(20); + Response response = target("fruit/create").request(MediaType.APPLICATION_JSON_TYPE) + .post(Entity.entity(fruit, MediaType.APPLICATION_JSON_TYPE)); + + assertEquals("Http Response should be 204 ", 204, response.getStatus()); + + final String json = target("fruit/search/strawberry").request() + .get(String.class); + assertThat(json, containsString("{\"name\":\"strawberry\",\"weight\":20}")); + } + + @Test + public void givenFruit_whenFruitIsInvalid_thenReponseContainsCustomExceptions() { + final Response response = target("fruit/exception").request() + .get(); + + assertEquals("Http Response should be 400 ", 400, response.getStatus()); + String responseString = response.readEntity(String.class); + assertThat(responseString, containsString("exception..colour size must be between 5 and 200")); + assertThat(responseString, containsString("exception..name size must be between 5 and 200")); } } diff --git a/jhipster/jhipster-microservice/car-app/pom.xml b/jhipster/jhipster-microservice/car-app/pom.xml index 77fddf9c72..c52def554f 100644 --- a/jhipster/jhipster-microservice/car-app/pom.xml +++ b/jhipster/jhipster-microservice/car-app/pom.xml @@ -10,10 +10,10 @@ ../../../parent-boot-1 com.car.app - carapp + car-app 0.0.1-SNAPSHOT war - Carapp + car-app ${maven.version} diff --git a/jhipster/jhipster-microservice/dealer-app/pom.xml b/jhipster/jhipster-microservice/dealer-app/pom.xml index 5f6485a203..a9366e9bd3 100644 --- a/jhipster/jhipster-microservice/dealer-app/pom.xml +++ b/jhipster/jhipster-microservice/dealer-app/pom.xml @@ -3,10 +3,10 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 com.dealer.app - dealerapp + dealer-app 0.0.1-SNAPSHOT war - Dealerapp + dealer-app parent-boot-1 diff --git a/jhipster/jhipster-microservice/gateway-app/pom.xml b/jhipster/jhipster-microservice/gateway-app/pom.xml index 85b3688efa..0ae74e11bb 100644 --- a/jhipster/jhipster-microservice/gateway-app/pom.xml +++ b/jhipster/jhipster-microservice/gateway-app/pom.xml @@ -3,10 +3,10 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 com.gateway - gateway + gateway-app 0.0.1-SNAPSHOT war - Gateway + gateway-app parent-boot-1 diff --git a/jhipster/jhipster-monolithic/pom.xml b/jhipster/jhipster-monolithic/pom.xml index 24956745e4..9708b83a70 100644 --- a/jhipster/jhipster-monolithic/pom.xml +++ b/jhipster/jhipster-monolithic/pom.xml @@ -412,6 +412,7 @@ org.apache.maven.plugins maven-eclipse-plugin + ${maven-eclipse-plugin.version} true true @@ -879,10 +880,6 @@ - - ${maven.version} - - -Djava.security.egd=file:/dev/./urandom -Xmx256m 3.6.2 @@ -909,6 +906,7 @@ 1.1.0.Final 1.4.1 3.0.1 + 2.10 yyyyMMddHHmmss 3.0.0 3.1.3 diff --git a/jjwt/pom.xml b/jjwt/pom.xml index 189e957e42..6bf9f4426a 100644 --- a/jjwt/pom.xml +++ b/jjwt/pom.xml @@ -3,10 +3,10 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 io.jsonwebtoken - jjwtfun + jjwt 0.0.1-SNAPSHOT jar - jjwtfun + jjwt Exercising the JJWT diff --git a/jnosql/jnosql-artemis/pom.xml b/jnosql/jnosql-artemis/pom.xml index 6186b3510c..9c2bfd1ba0 100644 --- a/jnosql/jnosql-artemis/pom.xml +++ b/jnosql/jnosql-artemis/pom.xml @@ -19,7 +19,7 @@ - ${artifactId} + ${project.artifactId} net.wasdev.wlp.maven.plugins diff --git a/jsf/pom.xml b/jsf/pom.xml index 19ea4b569f..2ac8a9f7c2 100644 --- a/jsf/pom.xml +++ b/jsf/pom.xml @@ -36,11 +36,6 @@ ${jstl.version} - - org.springframework - spring-web - ${org.springframework.version} - org.springframework spring-webmvc diff --git a/json/README.md b/json/README.md index 8c2abc3e47..e0679bc60b 100644 --- a/json/README.md +++ b/json/README.md @@ -9,3 +9,4 @@ - [Introduction to JsonPath](http://www.baeldung.com/guide-to-jayway-jsonpath) - [Introduction to JSON-Java (org.json)](http://www.baeldung.com/java-org-json) - [Overview of JSON Pointer](https://www.baeldung.com/json-pointer) +- [Introduction to the JSON Binding API (JSR 367) in Java](http://www.baeldung.com/java-json-binding-api) \ No newline at end of file diff --git a/json/pom.xml b/json/pom.xml index fa3fcafa65..b688baec06 100644 --- a/json/pom.xml +++ b/json/pom.xml @@ -33,7 +33,11 @@ json 20171018 - + + javax.json.bind + javax.json.bind-api + ${jsonb-api.version} + junit junit @@ -46,12 +50,26 @@ javax.json 1.1.2 - + + org.eclipse + yasson + ${yasson.version} + + + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + test + 1.4.1 1.2.21 + 1.0 + 4.1 + 1.0.1 diff --git a/jsonb/src/main/java/com/baeldung/adapter/PersonAdapter.java b/json/src/main/java/com/baeldung/adapter/PersonAdapter.java similarity index 100% rename from jsonb/src/main/java/com/baeldung/adapter/PersonAdapter.java rename to json/src/main/java/com/baeldung/adapter/PersonAdapter.java diff --git a/jsonb/src/main/java/com/baeldung/jsonb/Person.java b/json/src/main/java/com/baeldung/jsonb/Person.java similarity index 100% rename from jsonb/src/main/java/com/baeldung/jsonb/Person.java rename to json/src/main/java/com/baeldung/jsonb/Person.java diff --git a/jsonb/src/test/java/com/baeldung/jsonb/JsonbTest.java b/json/src/test/java/com/baeldung/jsonb/JsonbUnitTest.java similarity index 99% rename from jsonb/src/test/java/com/baeldung/jsonb/JsonbTest.java rename to json/src/test/java/com/baeldung/jsonb/JsonbUnitTest.java index 67beda77e1..f258700c6b 100644 --- a/jsonb/src/test/java/com/baeldung/jsonb/JsonbTest.java +++ b/json/src/test/java/com/baeldung/jsonb/JsonbUnitTest.java @@ -18,8 +18,9 @@ import org.apache.commons.collections4.ListUtils; import org.junit.Test; import com.baeldung.adapter.PersonAdapter; +import com.baeldung.jsonb.Person; -public class JsonbTest { +public class JsonbUnitTest { @Test public void givenPersonList_whenSerializeWithJsonb_thenGetPersonJsonArray() { diff --git a/jsonb/README.md b/jsonb/README.md deleted file mode 100644 index 9293a44808..0000000000 --- a/jsonb/README.md +++ /dev/null @@ -1,4 +0,0 @@ -## JSON B - -## Relevant articles: -- [Introduction to the JSON Binding API (JSR 367) in Java](http://www.baeldung.com/java-json-binding-api) diff --git a/jsonb/pom.xml b/jsonb/pom.xml deleted file mode 100644 index 160b9d5df7..0000000000 --- a/jsonb/pom.xml +++ /dev/null @@ -1,101 +0,0 @@ - - - 4.0.0 - com.baeldung - json-b - 0.0.1-SNAPSHOT - jar - json-b - json-b sample project - - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - - - - - javax.json.bind - javax.json.bind-api - ${jsonb-api.version} - - - - org.apache.commons - commons-collections4 - ${commons-collections4.version} - test - - - - org.junit.jupiter - junit-jupiter-api - ${junit.jupiter.version} - - - org.junit.platform - junit-platform-surefire-provider - ${junit.platform.version} - test - - - org.junit.platform - junit-platform-runner - ${junit.platform.version} - test - - - - - - yasson - - true - - - - - org.eclipse - yasson - ${yasson.version} - - - org.glassfish - javax.json - ${javax.json.version} - - - - - johnzon - - - - org.apache.geronimo.specs - geronimo-json_1.1_spec - ${geronimo-json_1.1_spec.version} - - - org.apache.johnzon - johnzon-jsonb - ${johnzon.version} - - - - - - - 1.8 - 1.8 - 1.0.0 - 1.0 - 1.1.3 - 1.0 - 1.0.1 - 1.1.2 - 4.1 - - - \ No newline at end of file diff --git a/jta/pom.xml b/jta/pom.xml new file mode 100644 index 0000000000..89bdccf25e --- /dev/null +++ b/jta/pom.xml @@ -0,0 +1,88 @@ + + + 4.0.0 + com.baeldung + jta-demo + 1.0-SNAPSHOT + jar + + JEE JTA demo + + + org.springframework.boot + spring-boot-starter-parent + 2.0.4.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-jta-bitronix + + + org.springframework.boot + spring-boot-starter-jdbc + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.hsqldb + hsqldb + 2.4.1 + + + + + + autoconfiguration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + **/*LiveTest.java + **/*IntegrationTest.java + **/*IntTest.java + + + **/AutoconfigurationTest.java + + + + + + + json + + + + + + + + diff --git a/jta/src/main/java/com/baeldung/jtademo/JtaDemoApplication.java b/jta/src/main/java/com/baeldung/jtademo/JtaDemoApplication.java new file mode 100644 index 0000000000..4d8779efe5 --- /dev/null +++ b/jta/src/main/java/com/baeldung/jtademo/JtaDemoApplication.java @@ -0,0 +1,48 @@ +package com.baeldung.jtademo; + +import org.hsqldb.jdbc.pool.JDBCXADataSource; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.jta.bitronix.BitronixXADataSourceWrapper; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import javax.sql.DataSource; + +@EnableAutoConfiguration +@EnableTransactionManagement +@Configuration +@ComponentScan +public class JtaDemoApplication { + + @Bean("dataSourceAccount") + public DataSource dataSource() throws Exception { + return createHsqlXADatasource("jdbc:hsqldb:mem:accountDb"); + } + + @Bean("dataSourceAudit") + public DataSource dataSourceAudit() throws Exception { + return createHsqlXADatasource("jdbc:hsqldb:mem:auditDb"); + } + + private DataSource createHsqlXADatasource(String connectionUrl) throws Exception { + JDBCXADataSource dataSource = new JDBCXADataSource(); + dataSource.setUrl(connectionUrl); + dataSource.setUser("sa"); + BitronixXADataSourceWrapper wrapper = new BitronixXADataSourceWrapper(); + return wrapper.wrapDataSource(dataSource); + } + + @Bean("jdbcTemplateAccount") + public JdbcTemplate jdbcTemplate(@Qualifier("dataSourceAccount") DataSource dataSource) { + return new JdbcTemplate(dataSource); + } + + @Bean("jdbcTemplateAudit") + public JdbcTemplate jdbcTemplateAudit(@Qualifier("dataSourceAudit") DataSource dataSource) { + return new JdbcTemplate(dataSource); + } +} diff --git a/jta/src/main/java/com/baeldung/jtademo/dto/TransferLog.java b/jta/src/main/java/com/baeldung/jtademo/dto/TransferLog.java new file mode 100644 index 0000000000..cc1474ce81 --- /dev/null +++ b/jta/src/main/java/com/baeldung/jtademo/dto/TransferLog.java @@ -0,0 +1,27 @@ +package com.baeldung.jtademo.dto; + +import java.math.BigDecimal; + +public class TransferLog { + private String fromAccountId; + private String toAccountId; + private BigDecimal amount; + + public TransferLog(String fromAccountId, String toAccountId, BigDecimal amount) { + this.fromAccountId = fromAccountId; + this.toAccountId = toAccountId; + this.amount = amount; + } + + public String getFromAccountId() { + return fromAccountId; + } + + public String getToAccountId() { + return toAccountId; + } + + public BigDecimal getAmount() { + return amount; + } +} diff --git a/jta/src/main/java/com/baeldung/jtademo/services/AuditService.java b/jta/src/main/java/com/baeldung/jtademo/services/AuditService.java new file mode 100644 index 0000000000..f6810e15c8 --- /dev/null +++ b/jta/src/main/java/com/baeldung/jtademo/services/AuditService.java @@ -0,0 +1,33 @@ +package com.baeldung.jtademo.services; + +import com.baeldung.jtademo.dto.TransferLog; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.ResultSetExtractor; +import org.springframework.stereotype.Service; + +import java.math.BigDecimal; + +@Service +public class AuditService { + + final JdbcTemplate jdbcTemplate; + + @Autowired + public AuditService(@Qualifier("jdbcTemplateAudit") JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + public void log(String fromAccount, String toAccount, BigDecimal amount) { + jdbcTemplate.update("insert into AUDIT_LOG(FROM_ACCOUNT, TO_ACCOUNT, AMOUNT) values ?,?,?", fromAccount, toAccount, amount); + } + + public TransferLog lastTransferLog() { + return jdbcTemplate.query("select FROM_ACCOUNT,TO_ACCOUNT,AMOUNT from AUDIT_LOG order by ID desc", (ResultSetExtractor) (rs) -> { + if (!rs.next()) + return null; + return new TransferLog(rs.getString(1), rs.getString(2), BigDecimal.valueOf(rs.getDouble(3))); + }); + } +} diff --git a/jta/src/main/java/com/baeldung/jtademo/services/BankAccountService.java b/jta/src/main/java/com/baeldung/jtademo/services/BankAccountService.java new file mode 100644 index 0000000000..0c881edbaa --- /dev/null +++ b/jta/src/main/java/com/baeldung/jtademo/services/BankAccountService.java @@ -0,0 +1,32 @@ +package com.baeldung.jtademo.services; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.ResultSetExtractor; +import org.springframework.stereotype.Service; + +import java.math.BigDecimal; + +@Service +public class BankAccountService { + + final JdbcTemplate jdbcTemplate; + + @Autowired + public BankAccountService(@Qualifier("jdbcTemplateAccount") JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + public void transfer(String fromAccountId, String toAccountId, BigDecimal amount) { + jdbcTemplate.update("update ACCOUNT set BALANCE=BALANCE-? where ID=?", amount, fromAccountId); + jdbcTemplate.update("update ACCOUNT set BALANCE=BALANCE+? where ID=?", amount, toAccountId); + } + + public BigDecimal balanceOf(String accountId) { + return jdbcTemplate.query("select BALANCE from ACCOUNT where ID=?", new Object[] { accountId }, (ResultSetExtractor) (rs) -> { + rs.next(); + return new BigDecimal(rs.getDouble(1)); + }); + } +} diff --git a/jta/src/main/java/com/baeldung/jtademo/services/TellerService.java b/jta/src/main/java/com/baeldung/jtademo/services/TellerService.java new file mode 100644 index 0000000000..d3bd80a2ee --- /dev/null +++ b/jta/src/main/java/com/baeldung/jtademo/services/TellerService.java @@ -0,0 +1,45 @@ +package com.baeldung.jtademo.services; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.transaction.Transactional; +import javax.transaction.UserTransaction; +import java.math.BigDecimal; + +@Service +public class TellerService { + private final BankAccountService bankAccountService; + private final AuditService auditService; + private final UserTransaction userTransaction; + + @Autowired + public TellerService(BankAccountService bankAccountService, AuditService auditService, UserTransaction userTransaction) { + this.bankAccountService = bankAccountService; + this.auditService = auditService; + this.userTransaction = userTransaction; + } + + @Transactional + public void executeTransfer(String fromAccontId, String toAccountId, BigDecimal amount) { + bankAccountService.transfer(fromAccontId, toAccountId, amount); + auditService.log(fromAccontId, toAccountId, amount); + BigDecimal balance = bankAccountService.balanceOf(fromAccontId); + if (balance.compareTo(BigDecimal.ZERO) <= 0) { + throw new RuntimeException("Insufficient fund."); + } + } + + public void executeTransferProgrammaticTx(String fromAccontId, String toAccountId, BigDecimal amount) throws Exception { + userTransaction.begin(); + bankAccountService.transfer(fromAccontId, toAccountId, amount); + auditService.log(fromAccontId, toAccountId, amount); + BigDecimal balance = bankAccountService.balanceOf(fromAccontId); + if (balance.compareTo(BigDecimal.ZERO) <= 0) { + userTransaction.rollback(); + throw new RuntimeException("Insufficient fund."); + } else { + userTransaction.commit(); + } + } +} diff --git a/jta/src/main/java/com/baeldung/jtademo/services/TestHelper.java b/jta/src/main/java/com/baeldung/jtademo/services/TestHelper.java new file mode 100644 index 0000000000..c1e8e355ec --- /dev/null +++ b/jta/src/main/java/com/baeldung/jtademo/services/TestHelper.java @@ -0,0 +1,43 @@ +package com.baeldung.jtademo.services; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.core.io.DefaultResourceLoader; +import org.springframework.core.io.Resource; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.init.ScriptUtils; +import org.springframework.stereotype.Component; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; + +@Component +public class TestHelper { + final JdbcTemplate jdbcTemplateAccount; + + final JdbcTemplate jdbcTemplateAudit; + + @Autowired + public TestHelper(@Qualifier("jdbcTemplateAccount") JdbcTemplate jdbcTemplateAccount, @Qualifier("jdbcTemplateAudit") JdbcTemplate jdbcTemplateAudit) { + this.jdbcTemplateAccount = jdbcTemplateAccount; + this.jdbcTemplateAudit = jdbcTemplateAudit; + } + + public void runAccountDbInit() throws SQLException { + runScript("account.sql", jdbcTemplateAccount.getDataSource()); + } + + public void runAuditDbInit() throws SQLException { + runScript("audit.sql", jdbcTemplateAudit.getDataSource()); + } + + private void runScript(String scriptName, DataSource dataSouorce) throws SQLException { + DefaultResourceLoader resourceLoader = new DefaultResourceLoader(); + Resource script = resourceLoader.getResource(scriptName); + try (Connection con = dataSouorce.getConnection()) { + ScriptUtils.executeSqlScript(con, script); + } + } + +} diff --git a/jta/src/main/resources/account.sql b/jta/src/main/resources/account.sql new file mode 100644 index 0000000000..af14f89b01 --- /dev/null +++ b/jta/src/main/resources/account.sql @@ -0,0 +1,9 @@ +DROP SCHEMA PUBLIC CASCADE; + +create table ACCOUNT ( +ID char(8) PRIMARY KEY, +BALANCE NUMERIC(28,10) +); + +insert into ACCOUNT(ID, BALANCE) values ('a0000001', 1000); +insert into ACCOUNT(ID, BALANCE) values ('a0000002', 2000); \ No newline at end of file diff --git a/spring-boot-logging-log4j2/src/main/resources/application.properties b/jta/src/main/resources/application.properties similarity index 100% rename from spring-boot-logging-log4j2/src/main/resources/application.properties rename to jta/src/main/resources/application.properties diff --git a/jta/src/main/resources/audit.sql b/jta/src/main/resources/audit.sql new file mode 100644 index 0000000000..aa5845f402 --- /dev/null +++ b/jta/src/main/resources/audit.sql @@ -0,0 +1,8 @@ +DROP SCHEMA PUBLIC CASCADE; + +create table AUDIT_LOG ( +ID INTEGER IDENTITY PRIMARY KEY, +FROM_ACCOUNT varchar(8), +TO_ACCOUNT varchar(8), +AMOUNT numeric(28,10) +); \ No newline at end of file diff --git a/jta/src/test/java/com/baeldung/jtademo/JtaDemoUnitTest.java b/jta/src/test/java/com/baeldung/jtademo/JtaDemoUnitTest.java new file mode 100644 index 0000000000..3f6004262b --- /dev/null +++ b/jta/src/test/java/com/baeldung/jtademo/JtaDemoUnitTest.java @@ -0,0 +1,91 @@ +package com.baeldung.jtademo; + +import com.baeldung.jtademo.dto.TransferLog; +import com.baeldung.jtademo.services.AuditService; +import com.baeldung.jtademo.services.BankAccountService; +import com.baeldung.jtademo.services.TellerService; +import com.baeldung.jtademo.services.TestHelper; +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.test.context.junit4.SpringRunner; + +import java.math.BigDecimal; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = JtaDemoApplication.class) +public class JtaDemoUnitTest { + @Autowired + TestHelper testHelper; + + @Autowired + TellerService tellerService; + + @Autowired + BankAccountService accountService; + + @Autowired + AuditService auditService; + + @Before + public void beforeTest() throws Exception { + testHelper.runAuditDbInit(); + testHelper.runAccountDbInit(); + } + + @Test + public void givenAnnotationTx_whenNoException_thenAllCommitted() throws Exception { + tellerService.executeTransfer("a0000001", "a0000002", BigDecimal.valueOf(500)); + + assertThat(accountService.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(500)); + assertThat(accountService.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2500)); + + TransferLog lastTransferLog = auditService.lastTransferLog(); + assertThat(lastTransferLog).isNotNull(); + assertThat(lastTransferLog.getFromAccountId()).isEqualTo("a0000001"); + assertThat(lastTransferLog.getToAccountId()).isEqualTo("a0000002"); + assertThat(lastTransferLog.getAmount()).isEqualByComparingTo(BigDecimal.valueOf(500)); + } + + @Test + public void givenAnnotationTx_whenException_thenAllRolledBack() throws Exception { + assertThatThrownBy(() -> { + tellerService.executeTransfer("a0000002", "a0000001", BigDecimal.valueOf(100000)); + }).hasMessage("Insufficient fund."); + + assertThat(accountService.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(1000)); + assertThat(accountService.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2000)); + assertThat(auditService.lastTransferLog()).isNull(); + } + + @Test + public void givenProgrammaticTx_whenCommit_thenAllCommitted() throws Exception { + tellerService.executeTransferProgrammaticTx("a0000001", "a0000002", BigDecimal.valueOf(500)); + + BigDecimal result = accountService.balanceOf("a0000001"); + assertThat(accountService.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(500)); + assertThat(accountService.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2500)); + + TransferLog lastTransferLog = auditService.lastTransferLog(); + assertThat(lastTransferLog).isNotNull(); + assertThat(lastTransferLog.getFromAccountId()).isEqualTo("a0000001"); + assertThat(lastTransferLog.getToAccountId()).isEqualTo("a0000002"); + assertThat(lastTransferLog.getAmount()).isEqualByComparingTo(BigDecimal.valueOf(500)); + } + + @Test + public void givenProgrammaticTx_whenRollback_thenAllRolledBack() throws Exception { + assertThatThrownBy(() -> { + tellerService.executeTransferProgrammaticTx("a0000002", "a0000001", BigDecimal.valueOf(100000)); + }).hasMessage("Insufficient fund."); + + assertThat(accountService.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(1000)); + assertThat(accountService.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2000)); + assertThat(auditService.lastTransferLog()).isNull(); + } +} diff --git a/jws/pom.xml b/jws/pom.xml index 01d9820b42..1970ab9921 100644 --- a/jws/pom.xml +++ b/jws/pom.xml @@ -13,20 +13,23 @@ 1.0.0-SNAPSHOT + + + OpenNMS Repository + http://repo.opennms.org/maven2/ + + + - javax.jnlp + javax.samples.jnlp jnlp-servlet ${jnlp-servlet.version} - system - ${project.basedir}/java-core-samples-lib/jnlp-servlet.jar - javax.jnlp - jardiff - ${jardiff.version} - system - ${project.basedir}/java-core-samples-lib/jardiff.jar + javax.samples.jnlp + jnlp-jardiff + ${jnlp-jardiff.version} @@ -81,8 +84,8 @@ 3.0.2 3.0.0 - 7.0 - 7.0 + 1.6.0 + 1.6.0 diff --git a/core-kotlin/kotlin-ktor/.gitignore b/kotlin-libraries/.gitignore similarity index 100% rename from core-kotlin/kotlin-ktor/.gitignore rename to kotlin-libraries/.gitignore diff --git a/kotlin-libraries/README.md b/kotlin-libraries/README.md new file mode 100644 index 0000000000..30c4d03ded --- /dev/null +++ b/kotlin-libraries/README.md @@ -0,0 +1,9 @@ +## Relevant articles: + +- [Kotlin with Mockito](http://www.baeldung.com/kotlin-mockito) +- [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) +- [Writing Specifications with Kotlin and Spek](http://www.baeldung.com/kotlin-spek) +- [Processing JSON with Kotlin and Klaxson](http://www.baeldung.com/kotlin-json-klaxson) +- [Kotlin with Ktor](http://www.baeldung.com/kotlin-ktor) +- [Guide to the Kotlin Exposed Framework](https://www.baeldung.com/kotlin-exposed-persistence) diff --git a/core-kotlin/kotlin-ktor/build.gradle b/kotlin-libraries/build.gradle old mode 100755 new mode 100644 similarity index 83% rename from core-kotlin/kotlin-ktor/build.gradle rename to kotlin-libraries/build.gradle index 5c8f523cf1..b244df34b0 --- a/core-kotlin/kotlin-ktor/build.gradle +++ b/kotlin-libraries/build.gradle @@ -1,47 +1,57 @@ - - -group 'com.baeldung.ktor' -version '1.0-SNAPSHOT' - - -buildscript { - ext.kotlin_version = '1.2.41' - ext.ktor_version = '0.9.2' - - repositories { - mavenCentral() - } - dependencies { - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" - } -} - -apply plugin: 'java' -apply plugin: 'kotlin' -apply plugin: 'application' - -mainClassName = 'APIServer.kt' - -sourceCompatibility = 1.8 -compileKotlin { kotlinOptions.jvmTarget = "1.8" } -compileTestKotlin { kotlinOptions.jvmTarget = "1.8" } - -kotlin { experimental { coroutines "enable" } } - -repositories { - mavenCentral() - jcenter() - maven { url "https://dl.bintray.com/kotlin/ktor" } -} - -dependencies { - compile "io.ktor:ktor-server-netty:$ktor_version" - compile "ch.qos.logback:logback-classic:1.2.1" - compile "io.ktor:ktor-gson:$ktor_version" - testCompile group: 'junit', name: 'junit', version: '4.12' - -} -task runServer(type: JavaExec) { - main = 'APIServer' - classpath = sourceSets.main.runtimeClasspath + + +group 'com.baeldung.ktor' +version '1.0-SNAPSHOT' + + +buildscript { + ext.kotlin_version = '1.2.41' + ext.ktor_version = '0.9.2' + + repositories { + mavenCentral() + } + dependencies { + + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + } +} + +apply plugin: 'java' +apply plugin: 'kotlin' +apply plugin: 'application' + +mainClassName = 'APIServer.kt' + +sourceCompatibility = 1.8 +compileKotlin { kotlinOptions.jvmTarget = "1.8" } +compileTestKotlin { kotlinOptions.jvmTarget = "1.8" } + +kotlin { experimental { coroutines "enable" } } + +repositories { + mavenCentral() + jcenter() + maven { url "https://dl.bintray.com/kotlin/ktor" } +} +sourceSets { + main{ + kotlin{ + srcDirs 'com/baeldung/ktor' + } + } + +} + +dependencies { + compile "io.ktor:ktor-server-netty:$ktor_version" + compile "ch.qos.logback:logback-classic:1.2.1" + compile "io.ktor:ktor-gson:$ktor_version" + testCompile group: 'junit', name: 'junit', version: '4.12' + implementation 'com.beust:klaxon:3.0.1' + +} +task runServer(type: JavaExec) { + main = 'APIServer' + classpath = sourceSets.main.runtimeClasspath } \ No newline at end of file diff --git a/core-kotlin/kotlin-ktor/gradle/wrapper/gradle-wrapper.jar b/kotlin-libraries/gradle/wrapper/gradle-wrapper.jar old mode 100755 new mode 100644 similarity index 100% rename from core-kotlin/kotlin-ktor/gradle/wrapper/gradle-wrapper.jar rename to kotlin-libraries/gradle/wrapper/gradle-wrapper.jar diff --git a/core-kotlin/kotlin-ktor/gradle/wrapper/gradle-wrapper.properties b/kotlin-libraries/gradle/wrapper/gradle-wrapper.properties old mode 100755 new mode 100644 similarity index 97% rename from core-kotlin/kotlin-ktor/gradle/wrapper/gradle-wrapper.properties rename to kotlin-libraries/gradle/wrapper/gradle-wrapper.properties index 0b83b5a3e3..933b6473ce --- a/core-kotlin/kotlin-ktor/gradle/wrapper/gradle-wrapper.properties +++ b/kotlin-libraries/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ -distributionBase=GRADLE_USER_HOME -distributionPath=wrapper/dists -zipStoreBase=GRADLE_USER_HOME -zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-bin.zip +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-bin.zip diff --git a/core-kotlin/kotlin-ktor/gradlew b/kotlin-libraries/gradlew old mode 100755 new mode 100644 similarity index 100% rename from core-kotlin/kotlin-ktor/gradlew rename to kotlin-libraries/gradlew diff --git a/core-kotlin/kotlin-ktor/gradlew.bat b/kotlin-libraries/gradlew.bat old mode 100755 new mode 100644 similarity index 96% rename from core-kotlin/kotlin-ktor/gradlew.bat rename to kotlin-libraries/gradlew.bat index e95643d6a2..f9553162f1 --- a/core-kotlin/kotlin-ktor/gradlew.bat +++ b/kotlin-libraries/gradlew.bat @@ -1,84 +1,84 @@ -@if "%DEBUG%" == "" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS= - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto init - -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto init - -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:init -@rem Get command-line arguments, handling Windows variants - -if not "%OS%" == "Windows_NT" goto win9xME_args - -:win9xME_args -@rem Slurp the command line arguments. -set CMD_LINE_ARGS= -set _SKIP=2 - -:win9xME_args_slurp -if "x%~1" == "x" goto execute - -set CMD_LINE_ARGS=%* - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% - -:end -@rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/kotlin-libraries/pom.xml b/kotlin-libraries/pom.xml new file mode 100644 index 0000000000..c5b7fed951 --- /dev/null +++ b/kotlin-libraries/pom.xml @@ -0,0 +1,105 @@ + + + 4.0.0 + kotlin-libraries + jar + + + com.baeldung + parent-kotlin + 1.0.0-SNAPSHOT + ../parent-kotlin + + + + + exposed + exposed + https://dl.bintray.com/kotlin/exposed + + + + + + org.jetbrains.spek + spek-api + 1.1.5 + test + + + org.jetbrains.spek + spek-subject-extension + 1.1.5 + test + + + org.jetbrains.spek + spek-junit-platform-engine + 1.1.5 + test + + + org.apache.commons + commons-math3 + ${commons-math3.version} + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + + + khttp + khttp + ${khttp.version} + + + com.nhaarman + mockito-kotlin + ${mockito-kotlin.version} + test + + + com.github.salomonbrys.kodein + kodein + ${kodein.version} + + + org.assertj + assertj-core + ${assertj.version} + test + + + com.beust + klaxon + ${klaxon.version} + + + org.jetbrains.exposed + exposed + ${exposed.version} + + + com.h2database + h2 + ${h2database.version} + + + + + 1.5.0 + 4.1.0 + 3.0.4 + 0.1.0 + 3.6.1 + 1.1.1 + 5.2.0 + 3.10.0 + 1.4.197 + 0.10.4 + + + \ No newline at end of file diff --git a/core-kotlin/kotlin-ktor/resources/logback.xml b/kotlin-libraries/resources/logback.xml old mode 100755 new mode 100644 similarity index 96% rename from core-kotlin/kotlin-ktor/resources/logback.xml rename to kotlin-libraries/resources/logback.xml index 274cdcdb02..9452207268 --- a/core-kotlin/kotlin-ktor/resources/logback.xml +++ b/kotlin-libraries/resources/logback.xml @@ -1,11 +1,11 @@ - - - - %d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - + + + + %d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + \ No newline at end of file diff --git a/core-kotlin/kotlin-ktor/settings.gradle b/kotlin-libraries/settings.gradle old mode 100755 new mode 100644 similarity index 94% rename from core-kotlin/kotlin-ktor/settings.gradle rename to kotlin-libraries/settings.gradle index 13bbce9583..c91c993971 --- a/core-kotlin/kotlin-ktor/settings.gradle +++ b/kotlin-libraries/settings.gradle @@ -1,2 +1,2 @@ -rootProject.name = 'KtorWithKotlin' - +rootProject.name = 'KtorWithKotlin' + diff --git a/core-kotlin/src/main/kotlin/com/baeldung/klaxon/CustomProduct.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/klaxon/CustomProduct.kt similarity index 100% rename from core-kotlin/src/main/kotlin/com/baeldung/klaxon/CustomProduct.kt rename to kotlin-libraries/src/main/kotlin/com/baeldung/klaxon/CustomProduct.kt diff --git a/core-kotlin/src/main/kotlin/com/baeldung/klaxon/Product.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/klaxon/Product.kt similarity index 100% rename from core-kotlin/src/main/kotlin/com/baeldung/klaxon/Product.kt rename to kotlin-libraries/src/main/kotlin/com/baeldung/klaxon/Product.kt diff --git a/core-kotlin/src/main/kotlin/com/baeldung/klaxon/ProductData.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/klaxon/ProductData.kt similarity index 100% rename from core-kotlin/src/main/kotlin/com/baeldung/klaxon/ProductData.kt rename to kotlin-libraries/src/main/kotlin/com/baeldung/klaxon/ProductData.kt diff --git a/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/Controller.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/kodein/Controller.kt similarity index 100% rename from core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/Controller.kt rename to kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/kodein/Controller.kt diff --git a/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/Dao.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/kodein/Dao.kt similarity index 100% rename from core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/Dao.kt rename to kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/kodein/Dao.kt diff --git a/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/JdbcDao.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/kodein/JdbcDao.kt similarity index 100% rename from core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/JdbcDao.kt rename to kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/kodein/JdbcDao.kt diff --git a/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/MongoDao.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/kodein/MongoDao.kt similarity index 100% rename from core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/MongoDao.kt rename to kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/kodein/MongoDao.kt diff --git a/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/Service.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/kodein/Service.kt similarity index 100% rename from core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/Service.kt rename to kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/kodein/Service.kt diff --git a/core-kotlin/src/main/kotlin/com/baeldung/kotlin/mockito/BookService.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/mockito/BookService.kt similarity index 100% rename from core-kotlin/src/main/kotlin/com/baeldung/kotlin/mockito/BookService.kt rename to kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/mockito/BookService.kt diff --git a/core-kotlin/src/main/kotlin/com/baeldung/kotlin/mockito/LendBookManager.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/mockito/LendBookManager.kt similarity index 100% rename from core-kotlin/src/main/kotlin/com/baeldung/kotlin/mockito/LendBookManager.kt rename to kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/mockito/LendBookManager.kt diff --git a/core-kotlin/kotlin-ktor/src/main/kotlin/APIServer.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/ktor/APIServer.kt similarity index 100% rename from core-kotlin/kotlin-ktor/src/main/kotlin/APIServer.kt rename to kotlin-libraries/src/main/kotlin/com/baeldung/ktor/APIServer.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/klaxon/KlaxonUnitTest.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/klaxon/KlaxonUnitTest.kt similarity index 100% rename from core-kotlin/src/test/kotlin/com/baeldung/klaxon/KlaxonUnitTest.kt rename to kotlin-libraries/src/test/kotlin/com/baeldung/klaxon/KlaxonUnitTest.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/exposed/ExposedTest.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/exposed/ExposedTest.kt similarity index 100% rename from core-kotlin/src/test/kotlin/com/baeldung/kotlin/exposed/ExposedTest.kt rename to kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/exposed/ExposedTest.kt diff --git a/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/junit5/Calculator.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/junit5/Calculator.kt new file mode 100644 index 0000000000..1b61c05887 --- /dev/null +++ b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/junit5/Calculator.kt @@ -0,0 +1,17 @@ +package com.baeldung.kotlin.junit5 + +class Calculator { + fun add(a: Int, b: Int) = a + b + + fun divide(a: Int, b: Int) = if (b == 0) { + throw DivideByZeroException(a) + } else { + a / b + } + + fun square(a: Int) = a * a + + fun squareRoot(a: Int) = Math.sqrt(a.toDouble()) + + fun log(base: Int, value: Int) = Math.log(value.toDouble()) / Math.log(base.toDouble()) +} diff --git a/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/junit5/DivideByZeroException.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/junit5/DivideByZeroException.kt new file mode 100644 index 0000000000..60bc4e2944 --- /dev/null +++ b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/junit5/DivideByZeroException.kt @@ -0,0 +1,3 @@ +package com.baeldung.kotlin.junit5 + +class DivideByZeroException(val numerator: Int) : Exception() diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/khttp/KhttpLiveTest.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/khttp/KhttpLiveTest.kt similarity index 100% rename from core-kotlin/src/test/kotlin/com/baeldung/kotlin/khttp/KhttpLiveTest.kt rename to kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/khttp/KhttpLiveTest.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/kodein/KodeinUnitTest.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/kodein/KodeinUnitTest.kt similarity index 100% rename from core-kotlin/src/test/kotlin/com/baeldung/kotlin/kodein/KodeinUnitTest.kt rename to kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/kodein/KodeinUnitTest.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/mockito/LendBookManagerTest.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/mockito/LendBookManagerTest.kt similarity index 100% rename from core-kotlin/src/test/kotlin/com/baeldung/kotlin/mockito/LendBookManagerTest.kt rename to kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/mockito/LendBookManagerTest.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/mockito/LendBookManagerTestMockitoKotlin.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/mockito/LendBookManagerTestMockitoKotlin.kt similarity index 100% rename from core-kotlin/src/test/kotlin/com/baeldung/kotlin/mockito/LendBookManagerTestMockitoKotlin.kt rename to kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/mockito/LendBookManagerTestMockitoKotlin.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/CalculatorSubjectTest5.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/spek/CalculatorSubjectTest5.kt similarity index 100% rename from core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/CalculatorSubjectTest5.kt rename to kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/spek/CalculatorSubjectTest5.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/CalculatorTest5.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/spek/CalculatorTest5.kt similarity index 100% rename from core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/CalculatorTest5.kt rename to kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/spek/CalculatorTest5.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/DataDrivenTest5.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/spek/DataDrivenTest5.kt similarity index 100% rename from core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/DataDrivenTest5.kt rename to kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/spek/DataDrivenTest5.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/GroupTest5.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/spek/GroupTest5.kt similarity index 100% rename from core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/GroupTest5.kt rename to kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/spek/GroupTest5.kt diff --git a/libraries-data/README.md b/libraries-data/README.md index 32d34f63be..652ae0e04c 100644 --- a/libraries-data/README.md +++ b/libraries-data/README.md @@ -10,4 +10,4 @@ - [A Guide to Apache Ignite](http://www.baeldung.com/apache-ignite) - [Apache Ignite with Spring Data](http://www.baeldung.com/apache-ignite-spring-data) - [Guide to JMapper](https://www.baeldung.com/jmapper) -- [A Guide to Apache Crunch](https://www.baeldung.com/crunch) +- [A Guide to Apache Crunch](https://www.baeldung.com/apache-crunch) diff --git a/libraries-data/pom.xml b/libraries-data/pom.xml index 2b83328295..5b34a903ce 100644 --- a/libraries-data/pom.xml +++ b/libraries-data/pom.xml @@ -147,7 +147,6 @@ jmapper-core ${jmapper.version} - org.apache.crunch @@ -185,7 +184,72 @@ - + + org.apache.flink + flink-connector-kafka-0.11_2.11 + ${flink.version} + + + org.apache.flink + flink-streaming-java_2.11 + ${flink.version} + + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 + ${jackson.version} + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + org.apache.flink + flink-core + ${flink.version} + + + commons-logging + commons-logging + + + + + org.apache.flink + flink-java + ${flink.version} + + + commons-logging + commons-logging + + + + + org.apache.flink + flink-test-utils_2.11 + ${flink.version} + test + + + org.assertj + assertj-core + ${assertj.version} + + + org.awaitility + awaitility + ${awaitility.version} + test + + + org.awaitility + awaitility-proxy + ${awaitility.version} + test + + @@ -336,6 +400,10 @@ 2.4.0 2.8.2 1.1.0 + 1.5.0 + 2.8.5 + 3.0.0 + 3.6.2 3.8.4 1.8 3.0.0 diff --git a/libraries-data/src/main/java/com/baeldung/flink/FlinkDataPipeline.java b/libraries-data/src/main/java/com/baeldung/flink/FlinkDataPipeline.java new file mode 100644 index 0000000000..d02b1bcb83 --- /dev/null +++ b/libraries-data/src/main/java/com/baeldung/flink/FlinkDataPipeline.java @@ -0,0 +1,82 @@ +package com.baeldung.flink; + + +import com.baeldung.flink.model.Backup; +import com.baeldung.flink.model.InputMessage; +import com.baeldung.flink.operator.BackupAggregator; +import com.baeldung.flink.operator.InputMessageTimestampAssigner; +import com.baeldung.flink.operator.WordsCapitalizer; +import org.apache.flink.streaming.api.TimeCharacteristic; +import org.apache.flink.streaming.api.datastream.DataStream; +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; +import org.apache.flink.streaming.api.windowing.time.Time; +import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer011; +import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer011; + +import static com.baeldung.flink.connector.Consumers.*; +import static com.baeldung.flink.connector.Producers.*; + +public class FlinkDataPipeline { + + public static void capitalize() throws Exception { + String inputTopic = "flink_input"; + String outputTopic = "flink_output"; + String consumerGroup = "baeldung"; + String address = "localhost:9092"; + + StreamExecutionEnvironment environment = + StreamExecutionEnvironment.getExecutionEnvironment(); + + FlinkKafkaConsumer011 flinkKafkaConsumer = + createStringConsumerForTopic(inputTopic, address, consumerGroup); + flinkKafkaConsumer.setStartFromEarliest(); + + DataStream stringInputStream = + environment.addSource(flinkKafkaConsumer); + + FlinkKafkaProducer011 flinkKafkaProducer = + createStringProducer(outputTopic, address); + + stringInputStream + .map(new WordsCapitalizer()) + .addSink(flinkKafkaProducer); + + environment.execute(); + } + +public static void createBackup () throws Exception { + String inputTopic = "flink_input"; + String outputTopic = "flink_output"; + String consumerGroup = "baeldung"; + String kafkaAddress = "localhost:9092"; + + StreamExecutionEnvironment environment = + StreamExecutionEnvironment.getExecutionEnvironment(); + + environment.setStreamTimeCharacteristic(TimeCharacteristic.EventTime); + + FlinkKafkaConsumer011 flinkKafkaConsumer = + createInputMessageConsumer(inputTopic, kafkaAddress, consumerGroup); + flinkKafkaConsumer.setStartFromEarliest(); + + flinkKafkaConsumer + .assignTimestampsAndWatermarks(new InputMessageTimestampAssigner()); + FlinkKafkaProducer011 flinkKafkaProducer = + createBackupProducer(outputTopic, kafkaAddress); + + DataStream inputMessagesStream = + environment.addSource(flinkKafkaConsumer); + + inputMessagesStream + .timeWindowAll(Time.hours(24)) + .aggregate(new BackupAggregator()) + .addSink(flinkKafkaProducer); + + environment.execute(); +} + + public static void main(String[] args) throws Exception { + createBackup(); + } + +} diff --git a/libraries/src/main/java/com/baeldung/flink/LineSplitter.java b/libraries-data/src/main/java/com/baeldung/flink/LineSplitter.java similarity index 100% rename from libraries/src/main/java/com/baeldung/flink/LineSplitter.java rename to libraries-data/src/main/java/com/baeldung/flink/LineSplitter.java diff --git a/libraries/src/main/java/com/baeldung/flink/WordCount.java b/libraries-data/src/main/java/com/baeldung/flink/WordCount.java similarity index 100% rename from libraries/src/main/java/com/baeldung/flink/WordCount.java rename to libraries-data/src/main/java/com/baeldung/flink/WordCount.java diff --git a/libraries-data/src/main/java/com/baeldung/flink/connector/Consumers.java b/libraries-data/src/main/java/com/baeldung/flink/connector/Consumers.java new file mode 100644 index 0000000000..514085f9c4 --- /dev/null +++ b/libraries-data/src/main/java/com/baeldung/flink/connector/Consumers.java @@ -0,0 +1,32 @@ +package com.baeldung.flink.connector; + +import com.baeldung.flink.model.InputMessage; +import com.baeldung.flink.schema.InputMessageDeserializationSchema; +import org.apache.flink.api.common.serialization.SimpleStringSchema; +import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer011; + +import java.util.Properties; + +public class Consumers { + +public static FlinkKafkaConsumer011 createStringConsumerForTopic( + String topic, String kafkaAddress, String kafkaGroup ) { + Properties props = new Properties(); + props.setProperty("bootstrap.servers", kafkaAddress); + props.setProperty("group.id",kafkaGroup); + FlinkKafkaConsumer011 consumer = + new FlinkKafkaConsumer011<>(topic, new SimpleStringSchema(),props); + + return consumer; +} + + public static FlinkKafkaConsumer011 createInputMessageConsumer(String topic, String kafkaAddress, String kafkaGroup ) { + Properties properties = new Properties(); + properties.setProperty("bootstrap.servers", kafkaAddress); + properties.setProperty("group.id",kafkaGroup); + FlinkKafkaConsumer011 consumer = new FlinkKafkaConsumer011( + topic, new InputMessageDeserializationSchema(),properties); + + return consumer; + } +} diff --git a/libraries-data/src/main/java/com/baeldung/flink/connector/Producers.java b/libraries-data/src/main/java/com/baeldung/flink/connector/Producers.java new file mode 100644 index 0000000000..8e6f3f8f37 --- /dev/null +++ b/libraries-data/src/main/java/com/baeldung/flink/connector/Producers.java @@ -0,0 +1,17 @@ +package com.baeldung.flink.connector; + +import com.baeldung.flink.model.Backup; +import com.baeldung.flink.schema.BackupSerializationSchema; +import org.apache.flink.api.common.serialization.SimpleStringSchema; +import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer011; + +public class Producers { + + public static FlinkKafkaProducer011 createStringProducer(String topic, String kafkaAddress) { + return new FlinkKafkaProducer011<>(kafkaAddress, topic, new SimpleStringSchema()); + } + + public static FlinkKafkaProducer011 createBackupProducer(String topic, String kafkaAddress) { + return new FlinkKafkaProducer011(kafkaAddress, topic, new BackupSerializationSchema()); + } +} diff --git a/libraries-data/src/main/java/com/baeldung/flink/model/Backup.java b/libraries-data/src/main/java/com/baeldung/flink/model/Backup.java new file mode 100644 index 0000000000..268ceec7f3 --- /dev/null +++ b/libraries-data/src/main/java/com/baeldung/flink/model/Backup.java @@ -0,0 +1,27 @@ +package com.baeldung.flink.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.UUID; + +public class Backup { + + @JsonProperty("inputMessages") + List inputMessages; + @JsonProperty("backupTimestamp") + LocalDateTime backupTimestamp; + @JsonProperty("uuid") + UUID uuid; + + public Backup(List inputMessages, LocalDateTime backupTimestamp) { + this.inputMessages = inputMessages; + this.backupTimestamp = backupTimestamp; + this.uuid = UUID.randomUUID(); + } + + public List getInputMessages() { + return inputMessages; + } +} diff --git a/libraries-data/src/main/java/com/baeldung/flink/model/InputMessage.java b/libraries-data/src/main/java/com/baeldung/flink/model/InputMessage.java new file mode 100644 index 0000000000..b3f75256ae --- /dev/null +++ b/libraries-data/src/main/java/com/baeldung/flink/model/InputMessage.java @@ -0,0 +1,71 @@ +package com.baeldung.flink.model; + +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.google.common.base.Objects; + +import java.time.LocalDateTime; + +@JsonSerialize +public class InputMessage { + String sender; + String recipient; + LocalDateTime sentAt; + String message; + + public InputMessage() { + } + + public String getSender() { + return sender; + } + public void setSender(String sender) { + this.sender = sender; + } + + public String getRecipient() { + return recipient; + } + + public void setRecipient(String recipient) { + this.recipient = recipient; + } + + public LocalDateTime getSentAt() { + return sentAt; + } + + public void setSentAt(LocalDateTime sentAt) { + this.sentAt = sentAt; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public InputMessage(String sender, String recipient, LocalDateTime sentAt, String message) { + this.sender = sender; + this.recipient = recipient; + this.sentAt = sentAt; + this.message = message; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + InputMessage message1 = (InputMessage) o; + return Objects.equal(sender, message1.sender) && + Objects.equal(recipient, message1.recipient) && + Objects.equal(sentAt, message1.sentAt) && + Objects.equal(message, message1.message); + } + + @Override + public int hashCode() { + return Objects.hashCode(sender, recipient, sentAt, message); + } +} diff --git a/libraries-data/src/main/java/com/baeldung/flink/operator/BackupAggregator.java b/libraries-data/src/main/java/com/baeldung/flink/operator/BackupAggregator.java new file mode 100644 index 0000000000..c39b8413d1 --- /dev/null +++ b/libraries-data/src/main/java/com/baeldung/flink/operator/BackupAggregator.java @@ -0,0 +1,34 @@ +package com.baeldung.flink.operator; + +import com.baeldung.flink.model.Backup; +import com.baeldung.flink.model.InputMessage; +import org.apache.flink.api.common.functions.AggregateFunction; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; + + public class BackupAggregator implements AggregateFunction, Backup> { + @Override + public List createAccumulator() { + return new ArrayList<>(); + } + + @Override + public List add(InputMessage inputMessage, List inputMessages) { + inputMessages.add(inputMessage); + return inputMessages; + } + + @Override + public Backup getResult(List inputMessages) { + Backup backup = new Backup(inputMessages, LocalDateTime.now()); + return backup; + } + + @Override + public List merge(List inputMessages, List acc1) { + inputMessages.addAll(acc1); + return inputMessages; + } + } diff --git a/libraries-data/src/main/java/com/baeldung/flink/operator/InputMessageTimestampAssigner.java b/libraries-data/src/main/java/com/baeldung/flink/operator/InputMessageTimestampAssigner.java new file mode 100644 index 0000000000..05828d9588 --- /dev/null +++ b/libraries-data/src/main/java/com/baeldung/flink/operator/InputMessageTimestampAssigner.java @@ -0,0 +1,23 @@ +package com.baeldung.flink.operator; + +import com.baeldung.flink.model.InputMessage; +import org.apache.flink.streaming.api.functions.AssignerWithPunctuatedWatermarks; +import org.apache.flink.streaming.api.watermark.Watermark; + +import javax.annotation.Nullable; +import java.time.ZoneId; + +public class InputMessageTimestampAssigner implements AssignerWithPunctuatedWatermarks { + + @Override + public long extractTimestamp(InputMessage element, long previousElementTimestamp) { + ZoneId zoneId = ZoneId.systemDefault(); + return element.getSentAt().atZone(zoneId).toEpochSecond() * 1000; + } + + @Nullable + @Override + public Watermark checkAndGetNextWatermark(InputMessage lastElement, long extractedTimestamp) { + return new Watermark(extractedTimestamp - 15); + } +} diff --git a/libraries-data/src/main/java/com/baeldung/flink/operator/WordsCapitalizer.java b/libraries-data/src/main/java/com/baeldung/flink/operator/WordsCapitalizer.java new file mode 100644 index 0000000000..f9103d225c --- /dev/null +++ b/libraries-data/src/main/java/com/baeldung/flink/operator/WordsCapitalizer.java @@ -0,0 +1,11 @@ +package com.baeldung.flink.operator; + +import org.apache.flink.api.common.functions.MapFunction; + +public class WordsCapitalizer implements MapFunction { + + @Override + public String map(String s) { + return s.toUpperCase(); + } +} diff --git a/libraries-data/src/main/java/com/baeldung/flink/schema/BackupSerializationSchema.java b/libraries-data/src/main/java/com/baeldung/flink/schema/BackupSerializationSchema.java new file mode 100644 index 0000000000..967b266bb6 --- /dev/null +++ b/libraries-data/src/main/java/com/baeldung/flink/schema/BackupSerializationSchema.java @@ -0,0 +1,33 @@ +package com.baeldung.flink.schema; + +import com.baeldung.flink.model.Backup; +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import org.apache.flink.api.common.serialization.SerializationSchema; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class BackupSerializationSchema + implements SerializationSchema { + + static ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule()); + + Logger logger = LoggerFactory.getLogger(BackupSerializationSchema.class); + + @Override + public byte[] serialize(Backup backupMessage) { + if(objectMapper == null) { + objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); + objectMapper = new ObjectMapper().registerModule(new JavaTimeModule()); + } + try { + String json = objectMapper.writeValueAsString(backupMessage); + return json.getBytes(); + } catch (com.fasterxml.jackson.core.JsonProcessingException e) { + logger.error("Failed to parse JSON", e); + } + return new byte[0]; + } +} diff --git a/libraries-data/src/main/java/com/baeldung/flink/schema/InputMessageDeserializationSchema.java b/libraries-data/src/main/java/com/baeldung/flink/schema/InputMessageDeserializationSchema.java new file mode 100644 index 0000000000..9aaf8b9877 --- /dev/null +++ b/libraries-data/src/main/java/com/baeldung/flink/schema/InputMessageDeserializationSchema.java @@ -0,0 +1,32 @@ +package com.baeldung.flink.schema; + +import com.baeldung.flink.model.InputMessage; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import org.apache.flink.api.common.serialization.DeserializationSchema; +import org.apache.flink.api.common.typeinfo.TypeInformation; + +import java.io.IOException; + +public class InputMessageDeserializationSchema implements + DeserializationSchema { + + static ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule()); + + + @Override + public InputMessage deserialize(byte[] bytes) throws IOException { + + return objectMapper.readValue(bytes, InputMessage.class); + } + + @Override + public boolean isEndOfStream(InputMessage inputMessage) { + return false; + } + + @Override + public TypeInformation getProducedType() { + return TypeInformation.of(InputMessage.class); + } +} diff --git a/libraries-data/src/test/java/com/baeldung/flink/BackupCreatorIntegrationTest.java b/libraries-data/src/test/java/com/baeldung/flink/BackupCreatorIntegrationTest.java new file mode 100644 index 0000000000..ab7d119c16 --- /dev/null +++ b/libraries-data/src/test/java/com/baeldung/flink/BackupCreatorIntegrationTest.java @@ -0,0 +1,103 @@ +package com.baeldung.flink; + +import com.baeldung.flink.model.Backup; +import com.baeldung.flink.model.InputMessage; +import com.baeldung.flink.operator.BackupAggregator; +import com.baeldung.flink.operator.InputMessageTimestampAssigner; +import com.baeldung.flink.schema.BackupSerializationSchema; +import com.baeldung.flink.schema.InputMessageDeserializationSchema; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import org.apache.commons.collections.ListUtils; +import org.apache.flink.api.common.serialization.DeserializationSchema; +import org.apache.flink.api.common.serialization.SerializationSchema; +import org.apache.flink.streaming.api.TimeCharacteristic; +import org.apache.flink.streaming.api.datastream.DataStreamSource; +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; +import org.apache.flink.streaming.api.functions.sink.SinkFunction; +import org.apache.flink.streaming.api.windowing.time.Time; +import org.awaitility.Awaitility; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class BackupCreatorIntegrationTest { + public static ObjectMapper mapper; + + @Before + public void setup() { + mapper = new ObjectMapper().registerModule(new JavaTimeModule()); + } + + @Test + public void givenProperJson_whenDeserializeIsInvoked_thenProperObjectIsReturned() throws IOException { + InputMessage message = new InputMessage("Me", "User", LocalDateTime.now(), "Test Message"); + byte[] messageSerialized = mapper.writeValueAsBytes(message); + DeserializationSchema deserializationSchema = new InputMessageDeserializationSchema(); + InputMessage messageDeserialized = deserializationSchema.deserialize(messageSerialized); + + assertEquals(message, messageDeserialized); + } + + @Test + public void givenMultipleInputMessagesFromDifferentDays_whenBackupCreatorIsUser_thenMessagesAreGroupedProperly() throws Exception { + LocalDateTime currentTime = LocalDateTime.now(); + InputMessage message = new InputMessage("Me", "User", currentTime, "First TestMessage"); + InputMessage secondMessage = new InputMessage("Me", "User", currentTime.plusHours(1), "First TestMessage"); + InputMessage thirdMessage = new InputMessage("Me", "User", currentTime.plusHours(2), "First TestMessage"); + InputMessage fourthMessage = new InputMessage("Me", "User", currentTime.plusHours(3), "First TestMessage"); + InputMessage fifthMessage = new InputMessage("Me", "User", currentTime.plusHours(25), "First TestMessage"); + InputMessage sixthMessage = new InputMessage("Me", "User", currentTime.plusHours(26), "First TestMessage"); + + List firstBackupMessages = Arrays.asList(message, secondMessage, thirdMessage, fourthMessage); + List secondBackupMessages = Arrays.asList(fifthMessage, sixthMessage); + List inputMessages = ListUtils.union(firstBackupMessages, secondBackupMessages); + + StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); + env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime); + env.setParallelism(1); + DataStreamSource testDataSet = env.fromCollection(inputMessages); + CollectingSink sink = new CollectingSink(); + testDataSet.assignTimestampsAndWatermarks(new InputMessageTimestampAssigner()) + .timeWindowAll(Time.hours(24)) + .aggregate(new BackupAggregator()) + .addSink(sink); + + env.execute(); + + Awaitility.await().until(() -> sink.backups.size() == 2); + assertEquals(2, sink.backups.size()); + assertEquals(firstBackupMessages, sink.backups.get(0).getInputMessages()); + assertEquals(secondBackupMessages, sink.backups.get(1).getInputMessages()); + + } + + @Test + public void givenProperBackupObject_whenSerializeIsInvoked_thenObjectIsProperlySerialized() throws IOException { + InputMessage message = new InputMessage("Me", "User", LocalDateTime.now(), "Test Message"); + List messages = Arrays.asList(message); + Backup backup = new Backup(messages, LocalDateTime.now()); + byte[] backupSerialized = mapper.writeValueAsBytes(backup); + SerializationSchema serializationSchema = new BackupSerializationSchema(); + byte[] backupProcessed = serializationSchema.serialize(backup); + + assertEquals(backupSerialized, backupProcessed); + } + + private static class CollectingSink implements SinkFunction { + + public static List backups = new ArrayList<>(); + + @Override + public synchronized void invoke(Backup value, Context context) throws Exception { + backups.add(value); + } + } +} diff --git a/libraries-data/src/test/java/com/baeldung/flink/WordCapitalizerIntegrationTest.java b/libraries-data/src/test/java/com/baeldung/flink/WordCapitalizerIntegrationTest.java new file mode 100644 index 0000000000..8a98dae4b5 --- /dev/null +++ b/libraries-data/src/test/java/com/baeldung/flink/WordCapitalizerIntegrationTest.java @@ -0,0 +1,34 @@ +package com.baeldung.flink; + +import com.baeldung.flink.operator.WordsCapitalizer; +import org.apache.flink.api.java.DataSet; +import org.apache.flink.api.java.ExecutionEnvironment; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class WordCapitalizerIntegrationTest { + + @Test + public void givenDataSet_whenExecuteWordCapitalizer_thenReturnCapitalizedWords() throws Exception { + ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); + List data = Arrays.asList("dog", "cat", "wolf", "pig"); + + DataSet testDataSet = env.fromCollection(data); + + + List dataProcessed = testDataSet + .map(new WordsCapitalizer()) + .collect(); + + List testDataCapitalized = data.stream() + .map(String::toUpperCase) + .collect(Collectors.toList()); + + Assert.assertEquals(testDataCapitalized, dataProcessed); + } + +} diff --git a/libraries/src/test/java/com/baeldung/flink/WordCountIntegrationTest.java b/libraries-data/src/test/java/com/baeldung/flink/WordCountIntegrationTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/flink/WordCountIntegrationTest.java rename to libraries-data/src/test/java/com/baeldung/flink/WordCountIntegrationTest.java diff --git a/jsonb/.gitignore b/libraries-security/.gitignore similarity index 83% rename from jsonb/.gitignore rename to libraries-security/.gitignore index dec013dfa4..71881ad3ca 100644 --- a/jsonb/.gitignore +++ b/libraries-security/.gitignore @@ -1,5 +1,6 @@ +*.class + #folders# -.idea /target /neoDb* /data @@ -9,4 +10,5 @@ # Packaged files # *.jar *.war -*.ear \ No newline at end of file +*.ear +/bin/ diff --git a/libraries-security/pom.xml b/libraries-security/pom.xml new file mode 100644 index 0000000000..8f8506172f --- /dev/null +++ b/libraries-security/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + libraries-security + libraries-security + jar + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + + junit + junit + ${junit.version} + test + + + + + + 4.12 + + + diff --git a/libraries-server/README.md b/libraries-server/README.md index c25e9a10ab..28f963ade8 100644 --- a/libraries-server/README.md +++ b/libraries-server/README.md @@ -6,4 +6,4 @@ - [Programatically Create, Configure, and Run a Tomcat Server](http://www.baeldung.com/tomcat-programmatic-setup) - [Creating and Configuring Jetty 9 Server in Java](http://www.baeldung.com/jetty-java-programmatic) - [Testing Netty with EmbeddedChannel](http://www.baeldung.com/testing-netty-embedded-channel) - +- [MQTT Client in Java](https://www.baeldung.com/java-mqtt-client) diff --git a/libraries/pom.xml b/libraries/pom.xml index 80e3303ba5..91c54b6113 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -154,34 +154,7 @@ commons-dbutils ${commons.dbutils.version} - - org.apache.flink - flink-core - ${flink.version} - - - commons-logging - commons-logging - - - - - org.apache.flink - flink-java - ${flink.version} - - - commons-logging - commons-logging - - - - - org.apache.flink - flink-test-utils_2.10 - ${flink.version} - test - + org.apache.commons commons-math3 @@ -228,6 +201,7 @@ jackson-databind ${jackson.version} + org.datanucleus @@ -700,11 +674,6 @@ resilience4j-timelimiter ${resilience4j.version} - - org.apache.commons - commons-math3 - ${common-math3-version} - org.knowm.xchart xchart @@ -766,19 +735,6 @@ - - org.apache.maven.plugins - maven-dependency-plugin - - - org.apache.felix - maven-bundle-plugin - ${maven-bundle-plugin.version} - maven-plugin - - - true - maven-failsafe-plugin ${maven-failsafe-plugin.version} @@ -902,7 +858,6 @@ 4.5.3 2.5 - 1.2.0 2.8.5 2.92 1.9.26 @@ -935,7 +890,7 @@ 2.5.5 1.23.0 v4-rev493-1.21.0 - 1.0.0 + 2.0.0 1.7.0 3.0.14 2.2.0 @@ -948,7 +903,6 @@ 2.1.2 2.5.11 0.12.1 - 3.6.1 3.5.2 3.6 2.7.1 diff --git a/libraries/src/main/java/com/baeldung/commons/lang3/application/Application.java b/libraries/src/main/java/com/baeldung/commons/lang3/application/Application.java new file mode 100644 index 0000000000..7d4316e3ec --- /dev/null +++ b/libraries/src/main/java/com/baeldung/commons/lang3/application/Application.java @@ -0,0 +1,8 @@ +package com.baeldung.commons.lang3.application; + +public class Application { + + public static void main(String[] args) { + + } +} diff --git a/libraries/src/main/java/com/baeldung/commons/lang3/beans/User.java b/libraries/src/main/java/com/baeldung/commons/lang3/beans/User.java new file mode 100644 index 0000000000..68aab46c33 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/commons/lang3/beans/User.java @@ -0,0 +1,25 @@ +package com.baeldung.commons.lang3.beans; + +public class User { + + private final String name; + private final String email; + + public User(String name, String email) { + this.name = name; + this.email = email; + } + + public String getName() { + return name; + } + + public String getEmail() { + return email; + } + + @Override + public String toString() { + return "User{" + "name=" + name + ", email=" + email + '}'; + } +} diff --git a/libraries/src/main/java/com/baeldung/commons/lang3/beans/UserInitializer.java b/libraries/src/main/java/com/baeldung/commons/lang3/beans/UserInitializer.java new file mode 100644 index 0000000000..ae5d80244c --- /dev/null +++ b/libraries/src/main/java/com/baeldung/commons/lang3/beans/UserInitializer.java @@ -0,0 +1,11 @@ +package com.baeldung.commons.lang3.beans; + +import org.apache.commons.lang3.concurrent.LazyInitializer; + +public class UserInitializer extends LazyInitializer { + + @Override + protected User initialize() { + return new User("John", "john@domain.com"); + } +} diff --git a/libraries/src/main/java/com/baeldung/kafka/TransactionalApp.java b/libraries/src/main/java/com/baeldung/kafka/TransactionalApp.java new file mode 100644 index 0000000000..1e95041a0d --- /dev/null +++ b/libraries/src/main/java/com/baeldung/kafka/TransactionalApp.java @@ -0,0 +1,102 @@ +package com.baeldung.kafka; + +import org.apache.kafka.clients.consumer.ConsumerRecord; +import org.apache.kafka.clients.consumer.ConsumerRecords; +import org.apache.kafka.clients.consumer.KafkaConsumer; +import org.apache.kafka.clients.consumer.OffsetAndMetadata; +import org.apache.kafka.clients.producer.KafkaProducer; +import org.apache.kafka.clients.producer.ProducerConfig; +import org.apache.kafka.clients.producer.ProducerRecord; +import org.apache.kafka.common.KafkaException; +import org.apache.kafka.common.TopicPartition; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +import static java.time.Duration.ofSeconds; +import static java.util.Collections.singleton; +import static org.apache.kafka.clients.consumer.ConsumerConfig.*; +import static org.apache.kafka.clients.consumer.ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG; +import static org.apache.kafka.clients.producer.ProducerConfig.*; + +public class TransactionalApp { + + private static final String CONSUMER_GROUP_ID = "test"; + private static final String OUTPUT_TOPIC = "output"; + private static final String INPUT_TOPIC = "input"; + + public static void main(String[] args) { + + KafkaConsumer consumer = initConsumer(); + KafkaProducer producer = initProducer(); + + producer.initTransactions(); + + try { + + while (true) { + + ConsumerRecords records = consumer.poll(ofSeconds(20)); + + producer.beginTransaction(); + + for (ConsumerRecord record : records) + producer.send(new ProducerRecord(OUTPUT_TOPIC, record)); + + Map offsetsToCommit = new HashMap<>(); + + for (TopicPartition partition : records.partitions()) { + List> partitionedRecords = records.records(partition); + long offset = partitionedRecords.get(partitionedRecords.size() - 1).offset(); + + offsetsToCommit.put(partition, new OffsetAndMetadata(offset)); + } + + producer.sendOffsetsToTransaction(offsetsToCommit, CONSUMER_GROUP_ID); + producer.commitTransaction(); + + } + + } catch (KafkaException e) { + + producer.abortTransaction(); + + } + + + } + + private static KafkaConsumer initConsumer() { + Properties props = new Properties(); + props.put(BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); + props.put(GROUP_ID_CONFIG, CONSUMER_GROUP_ID); + props.put(ENABLE_AUTO_COMMIT_CONFIG, "false"); + props.put(KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer"); + props.put(VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer"); + + KafkaConsumer consumer = new KafkaConsumer<>(props); + consumer.subscribe(singleton(INPUT_TOPIC)); + return consumer; + } + + private static KafkaProducer initProducer() { + + Properties props = new Properties(); + props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); + props.put(ACKS_CONFIG, "all"); + props.put(RETRIES_CONFIG, 3); + props.put(BATCH_SIZE_CONFIG, 16384); + props.put(LINGER_MS_CONFIG, 1); + props.put(BUFFER_MEMORY_CONFIG, 33554432); + props.put(ENABLE_IDEMPOTENCE_CONFIG, "true"); + props.put(TRANSACTIONAL_ID_CONFIG, "prod-1"); + props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer"); + props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer"); + + return new KafkaProducer(props); + + } + +} diff --git a/libraries/src/main/java/com/baeldung/opencsv/examples/sync/BeanExamples.java b/libraries/src/main/java/com/baeldung/opencsv/examples/sync/BeanExamples.java index 76d044ca60..086f32677e 100644 --- a/libraries/src/main/java/com/baeldung/opencsv/examples/sync/BeanExamples.java +++ b/libraries/src/main/java/com/baeldung/opencsv/examples/sync/BeanExamples.java @@ -18,16 +18,19 @@ import java.util.List; public class BeanExamples { public static List beanBuilderExample(Path path, Class clazz) { + ColumnPositionMappingStrategy ms = new ColumnPositionMappingStrategy(); + return beanBuilderExample(path, clazz, ms); + } + + public static List beanBuilderExample(Path path, Class clazz, MappingStrategy ms) { CsvTransfer csvTransfer = new CsvTransfer(); try { - ColumnPositionMappingStrategy ms = new ColumnPositionMappingStrategy(); ms.setType(clazz); Reader reader = Files.newBufferedReader(path); - CsvToBean cb = new CsvToBeanBuilder(reader) - .withType(clazz) - .withMappingStrategy(ms) - .build(); + CsvToBean cb = new CsvToBeanBuilder(reader).withType(clazz) + .withMappingStrategy(ms) + .build(); csvTransfer.setCsvList(cb.parse()); reader.close(); @@ -40,11 +43,10 @@ public class BeanExamples { public static String writeCsvFromBean(Path path) { try { - Writer writer = new FileWriter(path.toString()); + Writer writer = new FileWriter(path.toString()); - StatefulBeanToCsv sbc = new StatefulBeanToCsvBuilder(writer) - .withSeparator(CSVWriter.DEFAULT_SEPARATOR) - .build(); + StatefulBeanToCsv sbc = new StatefulBeanToCsvBuilder(writer).withSeparator(CSVWriter.DEFAULT_SEPARATOR) + .build(); List list = new ArrayList<>(); list.add(new WriteExampleBean("Test1", "sfdsf", "fdfd")); diff --git a/libraries/src/test/java/com/baeldung/commons/collections/BidiMapUnitTest.java b/libraries/src/test/java/com/baeldung/commons/collections/BidiMapUnitTest.java index e46d8654a2..64e13a277e 100644 --- a/libraries/src/test/java/com/baeldung/commons/collections/BidiMapUnitTest.java +++ b/libraries/src/test/java/com/baeldung/commons/collections/BidiMapUnitTest.java @@ -42,4 +42,13 @@ public class BidiMapUnitTest { map.put("key1", "value1"); assertEquals(map.getKey("value1"), "key1"); } + + @Test + public void givenKeyValue_whenAddValue_thenReplaceFirstKey() { + BidiMap map = new DualHashBidiMap<>(); + map.put("key1", "value1"); + map.put("key2", "value1"); + assertEquals(map.size(), 1); + assertFalse(map.containsKey("key1")); + } } diff --git a/libraries/src/test/java/com/baeldung/commons/lang3/test/ArrayUtilsUnitTest.java b/libraries/src/test/java/com/baeldung/commons/lang3/test/ArrayUtilsUnitTest.java new file mode 100644 index 0000000000..5eba736b4a --- /dev/null +++ b/libraries/src/test/java/com/baeldung/commons/lang3/test/ArrayUtilsUnitTest.java @@ -0,0 +1,84 @@ +package com.baeldung.commons.lang3.test; + +import java.util.HashMap; +import java.util.Map; +import org.apache.commons.lang3.ArrayUtils; +import static org.assertj.core.api.Assertions.assertThat; +import org.junit.Test; + +public class ArrayUtilsUnitTest { + + @Test + public void givenArrayUtilsClass_whenCalledtoString_thenCorrect() { + String[] array = {"a", "b", "c"}; + assertThat(ArrayUtils.toString(array)).isEqualTo("{a,b,c}"); + } + + @Test + public void givenArrayUtilsClass_whenCalledtoStringIfArrayisNull_thenCorrect() { + String[] array = null; + assertThat(ArrayUtils.toString(array, "Array is null")).isEqualTo("Array is null"); + } + + @Test + public void givenArrayUtilsClass_whenCalledhashCode_thenCorrect() { + String[] array = {"a", "b", "c"}; + assertThat(ArrayUtils.hashCode(array)).isEqualTo(997619); + } + + @Test + public void givenArrayUtilsClass_whenCalledtoMap_thenCorrect() { + String[][] array = {{"1", "one", }, {"2", "two", }, {"3", "three"}}; + Map map = new HashMap(); + map.put("1", "one"); + map.put("2", "two"); + map.put("3", "three"); + assertThat(ArrayUtils.toMap(array)).isEqualTo(map); + } + + @Test + public void givenArrayUtilsClass_whenCallednullToEmptyStringArray_thenCorrect() { + String[] array = null; + assertThat(ArrayUtils.nullToEmpty(array)).isEmpty(); + } + + @Test + public void givenArrayUtilsClass_whenCallednullToEmptyObjectArray_thenCorrect() { + Object[] array = null; + assertThat(ArrayUtils.nullToEmpty(array)).isEmpty(); + } + + @Test + public void givenArrayUtilsClass_whenCalledsubarray_thenCorrect() { + int[] array = {1, 2, 3}; + int[] expected = {1}; + assertThat(ArrayUtils.subarray(array, 0, 1)).isEqualTo(expected); + } + + @Test + public void givenArrayUtilsClass_whenCalledisSameLength_thenCorrect() { + int[] array1 = {1, 2, 3}; + int[] array2 = {1, 2, 3}; + assertThat(ArrayUtils.isSameLength(array1, array2)).isTrue(); + } + + @Test + public void givenArrayUtilsClass_whenCalledreverse_thenCorrect() { + int[] array1 = {1, 2, 3}; + int[] array2 = {3, 2, 1}; + ArrayUtils.reverse(array1); + assertThat(array1).isEqualTo(array2); + } + + @Test + public void givenArrayUtilsClass_whenCalledIndexOf_thenCorrect() { + int[] array = {1, 2, 3}; + assertThat(ArrayUtils.indexOf(array, 1, 0)).isEqualTo(0); + } + + @Test + public void givenArrayUtilsClass_whenCalledcontains_thenCorrect() { + int[] array = {1, 2, 3}; + assertThat(ArrayUtils.contains(array, 1)).isTrue(); + } +} diff --git a/libraries/src/test/java/com/baeldung/commons/lang3/test/BasicThreadFactoryUnitTest.java b/libraries/src/test/java/com/baeldung/commons/lang3/test/BasicThreadFactoryUnitTest.java new file mode 100644 index 0000000000..f32980269a --- /dev/null +++ b/libraries/src/test/java/com/baeldung/commons/lang3/test/BasicThreadFactoryUnitTest.java @@ -0,0 +1,18 @@ +package com.baeldung.commons.lang3.test; + +import org.apache.commons.lang3.concurrent.BasicThreadFactory; +import static org.assertj.core.api.Assertions.assertThat; +import org.junit.Test; + +public class BasicThreadFactoryUnitTest { + + @Test + public void givenBasicThreadFactoryInstance_whenCalledBuilder_thenCorrect() { + BasicThreadFactory factory = new BasicThreadFactory.Builder() + .namingPattern("workerthread-%d") + .daemon(true) + .priority(Thread.MAX_PRIORITY) + .build(); + assertThat(factory).isInstanceOf(BasicThreadFactory.class); + } +} diff --git a/libraries/src/test/java/com/baeldung/commons/lang3/test/ConstructorUtilsUnitTest.java b/libraries/src/test/java/com/baeldung/commons/lang3/test/ConstructorUtilsUnitTest.java new file mode 100644 index 0000000000..f55f239d75 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/commons/lang3/test/ConstructorUtilsUnitTest.java @@ -0,0 +1,28 @@ +package com.baeldung.commons.lang3.test; + +import com.baeldung.commons.lang3.beans.User; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import org.apache.commons.lang3.reflect.ConstructorUtils; +import static org.assertj.core.api.Assertions.assertThat; +import org.junit.Test; + +public class ConstructorUtilsUnitTest { + + @Test + public void givenConstructorUtilsClass_whenCalledgetAccessibleConstructor_thenCorrect() { + assertThat(ConstructorUtils.getAccessibleConstructor(User.class, String.class, String.class)).isInstanceOf(Constructor.class); + } + + @Test + public void givenConstructorUtilsClass_whenCalledinvokeConstructor_thenCorrect() throws Exception { + assertThat(ConstructorUtils.invokeConstructor(User.class, "name", "email")).isInstanceOf(User.class); + } + + @Test + public void givenConstructorUtilsClass_whenCalledinvokeExactConstructor_thenCorrect() throws Exception { + String[] args = {"name", "email"}; + Class[] parameterTypes= {String.class, String.class}; + assertThat(ConstructorUtils.invokeExactConstructor(User.class, args, parameterTypes)).isInstanceOf(User.class); + } +} diff --git a/libraries/src/test/java/com/baeldung/commons/lang3/test/FieldUtilsUnitTest.java b/libraries/src/test/java/com/baeldung/commons/lang3/test/FieldUtilsUnitTest.java new file mode 100644 index 0000000000..8abb373e30 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/commons/lang3/test/FieldUtilsUnitTest.java @@ -0,0 +1,60 @@ +package com.baeldung.commons.lang3.test; + +import com.baeldung.commons.lang3.beans.User; +import org.apache.commons.lang3.reflect.FieldUtils; +import static org.assertj.core.api.Assertions.assertThat; +import org.junit.BeforeClass; +import org.junit.Test; + +public class FieldUtilsUnitTest { + + private static User user; + + @BeforeClass + public static void setUpUserInstance() { + user = new User("Julie", "julie@domain.com"); + } + + @Test + public void givenFieldUtilsClass_whenCalledgetField_thenCorrect() { + assertThat(FieldUtils.getField(User.class, "name", true).getName()).isEqualTo("name"); + } + + @Test + public void givenFieldUtilsClass_whenCalledgetFieldForceAccess_thenCorrect() { + assertThat(FieldUtils.getField(User.class, "name", true).getName()).isEqualTo("name"); + } + + @Test + public void givenFieldUtilsClass_whenCalledgetDeclaredFieldForceAccess_thenCorrect() { + assertThat(FieldUtils.getDeclaredField(User.class, "name", true).getName()).isEqualTo("name"); + } + + @Test + public void givenFieldUtilsClass_whenCalledgetAllField_thenCorrect() { + assertThat(FieldUtils.getAllFields(User.class).length).isEqualTo(2); + } + + @Test + public void givenFieldUtilsClass_whenCalledreadField_thenCorrect() throws IllegalAccessException { + assertThat(FieldUtils.readField(user, "name", true)).isEqualTo("Julie"); + } + + @Test + public void givenFieldUtilsClass_whenCalledreadDeclaredField_thenCorrect() throws IllegalAccessException { + assertThat(FieldUtils.readDeclaredField(user, "name", true)).isEqualTo("Julie"); + } + + @Test + public void givenFieldUtilsClass_whenCalledwriteField_thenCorrect() throws IllegalAccessException { + FieldUtils.writeField(user, "name", "Julie", true); + assertThat(FieldUtils.readField(user, "name", true)).isEqualTo("Julie"); + + } + + @Test + public void givenFieldUtilsClass_whenCalledwriteDeclaredField_thenCorrect() throws IllegalAccessException { + FieldUtils.writeDeclaredField(user, "name", "Julie", true); + assertThat(FieldUtils.readField(user, "name", true)).isEqualTo("Julie"); + } +} diff --git a/libraries/src/test/java/com/baeldung/commons/lang3/test/FractionUnitTest.java b/libraries/src/test/java/com/baeldung/commons/lang3/test/FractionUnitTest.java new file mode 100644 index 0000000000..1a5094a16d --- /dev/null +++ b/libraries/src/test/java/com/baeldung/commons/lang3/test/FractionUnitTest.java @@ -0,0 +1,34 @@ +package com.baeldung.commons.lang3.test; + +import org.apache.commons.lang3.math.Fraction; +import static org.assertj.core.api.Assertions.assertThat; +import org.junit.Test; + +public class FractionUnitTest { + + @Test + public void givenFractionClass_whenCalledgetFraction_thenCorrect() { + assertThat(Fraction.getFraction(5, 6)).isInstanceOf(Fraction.class); + } + + @Test + public void givenTwoFractionInstances_whenCalledadd_thenCorrect() { + Fraction fraction1 = Fraction.getFraction(1, 4); + Fraction fraction2 = Fraction.getFraction(3, 4); + assertThat(fraction1.add(fraction2).toString()).isEqualTo("1/1"); + } + + @Test + public void givenTwoFractionInstances_whenCalledsubstract_thenCorrect() { + Fraction fraction1 = Fraction.getFraction(3, 4); + Fraction fraction2 = Fraction.getFraction(1, 4); + assertThat(fraction1.subtract(fraction2).toString()).isEqualTo("1/2"); + } + + @Test + public void givenTwoFractionInstances_whenCalledmultiply_thenCorrect() { + Fraction fraction1 = Fraction.getFraction(3, 4); + Fraction fraction2 = Fraction.getFraction(1, 4); + assertThat(fraction1.multiplyBy(fraction2).toString()).isEqualTo("3/16"); + } +} diff --git a/libraries/src/test/java/com/baeldung/commons/lang3/test/HashCodeBuilderUnitTest.java b/libraries/src/test/java/com/baeldung/commons/lang3/test/HashCodeBuilderUnitTest.java new file mode 100644 index 0000000000..bbd24c8207 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/commons/lang3/test/HashCodeBuilderUnitTest.java @@ -0,0 +1,17 @@ +package com.baeldung.commons.lang3.test; + +import org.apache.commons.lang3.builder.HashCodeBuilder; +import static org.assertj.core.api.Assertions.assertThat; +import org.junit.Test; + +public class HashCodeBuilderUnitTest { + + @Test + public void givenHashCodeBuilderInstance_whenCalledtoHashCode_thenCorrect() { + int hashcode = new HashCodeBuilder(17, 37) + .append("John") + .append("john@domain.com") + .toHashCode(); + assertThat(hashcode).isEqualTo(1269178828); + } +} diff --git a/libraries/src/test/java/com/baeldung/commons/lang3/test/ImmutablePairUnitTest.java b/libraries/src/test/java/com/baeldung/commons/lang3/test/ImmutablePairUnitTest.java new file mode 100644 index 0000000000..28ed8d2514 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/commons/lang3/test/ImmutablePairUnitTest.java @@ -0,0 +1,36 @@ +package com.baeldung.commons.lang3.test; + +import org.apache.commons.lang3.tuple.ImmutablePair; +import static org.assertj.core.api.Assertions.assertThat; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ImmutablePairUnitTest { + + private static ImmutablePair immutablePair; + + @BeforeClass + public static void setUpImmutablePairInstance() { + immutablePair = new ImmutablePair<>("leftElement", "rightElement"); + } + + @Test + public void givenImmutablePairInstance_whenCalledgetLeft_thenCorrect() { + assertThat(immutablePair.getLeft()).isEqualTo("leftElement"); + } + + @Test + public void givenImmutablePairInstance_whenCalledgetRight_thenCorrect() { + assertThat(immutablePair.getRight()).isEqualTo("rightElement"); + } + + @Test + public void givenImmutablePairInstance_whenCalledof_thenCorrect() { + assertThat(ImmutablePair.of("leftElement", "rightElement")).isInstanceOf(ImmutablePair.class); + } + + @Test(expected = UnsupportedOperationException.class) + public void givenImmutablePairInstance_whenCalledSetValue_thenThrowUnsupportedOperationException() { + immutablePair.setValue("newValue"); + } +} diff --git a/libraries/src/test/java/com/baeldung/commons/lang3/test/ImmutableTripleUnitTest.java b/libraries/src/test/java/com/baeldung/commons/lang3/test/ImmutableTripleUnitTest.java new file mode 100644 index 0000000000..a9dd3fa7c0 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/commons/lang3/test/ImmutableTripleUnitTest.java @@ -0,0 +1,31 @@ +package com.baeldung.commons.lang3.test; + +import org.apache.commons.lang3.tuple.ImmutableTriple; +import static org.assertj.core.api.Assertions.assertThat; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ImmutableTripleUnitTest { + + private static ImmutableTriple immutableTriple; + + @BeforeClass + public static void setUpImmutableTripleInstance() { + immutableTriple = new ImmutableTriple<>("leftElement", "middleElement", "rightElement"); + } + + @Test + public void givenImmutableTripleInstance_whenCalledgetLeft_thenCorrect() { + assertThat(immutableTriple.getLeft()).isEqualTo("leftElement"); + } + + @Test + public void givenImmutableTripleInstance_whenCalledgetMiddle_thenCorrect() { + assertThat(immutableTriple.getMiddle()).isEqualTo("middleElement"); + } + + @Test + public void givenImmutableInstance_whenCalledgetRight_thenCorrect() { + assertThat(immutableTriple.getRight()).isEqualTo("rightElement"); + } +} diff --git a/libraries/src/test/java/com/baeldung/commons/lang3/test/LazyInitializerUnitTest.java b/libraries/src/test/java/com/baeldung/commons/lang3/test/LazyInitializerUnitTest.java new file mode 100644 index 0000000000..a08399f8de --- /dev/null +++ b/libraries/src/test/java/com/baeldung/commons/lang3/test/LazyInitializerUnitTest.java @@ -0,0 +1,16 @@ +package com.baeldung.commons.lang3.test; + +import com.baeldung.commons.lang3.beans.User; +import com.baeldung.commons.lang3.beans.UserInitializer; +import org.apache.commons.lang3.concurrent.ConcurrentException; +import static org.assertj.core.api.Assertions.assertThat; +import org.junit.Test; + +public class LazyInitializerUnitTest { + + @Test + public void givenLazyInitializerInstance_whenCalledget_thenCorrect() throws ConcurrentException { + UserInitializer userInitializer = new UserInitializer(); + assertThat(userInitializer.get()).isInstanceOf(User.class); + } +} diff --git a/libraries/src/test/java/com/baeldung/commons/lang3/test/MethodUtilsUnitTest.java b/libraries/src/test/java/com/baeldung/commons/lang3/test/MethodUtilsUnitTest.java new file mode 100644 index 0000000000..5e1d15f2f8 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/commons/lang3/test/MethodUtilsUnitTest.java @@ -0,0 +1,15 @@ +package com.baeldung.commons.lang3.test; + +import com.baeldung.commons.lang3.beans.User; +import java.lang.reflect.Method; +import org.apache.commons.lang3.reflect.MethodUtils; +import static org.assertj.core.api.Assertions.assertThat; +import org.junit.Test; + +public class MethodUtilsUnitTest { + + @Test + public void givenMethodUtilsClass_whenCalledgetAccessibleMethod_thenCorrect() { + assertThat(MethodUtils.getAccessibleMethod(User.class, "getName")).isInstanceOf(Method.class); + } +} diff --git a/libraries/src/test/java/com/baeldung/commons/lang3/test/MutableObjectUnitTest.java b/libraries/src/test/java/com/baeldung/commons/lang3/test/MutableObjectUnitTest.java new file mode 100644 index 0000000000..02041f51e8 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/commons/lang3/test/MutableObjectUnitTest.java @@ -0,0 +1,32 @@ +package com.baeldung.commons.lang3.test; + +import org.apache.commons.lang3.mutable.MutableObject; +import static org.assertj.core.api.Assertions.assertThat; +import org.junit.BeforeClass; +import org.junit.Test; + +public class MutableObjectUnitTest { + + private static MutableObject mutableObject; + + @BeforeClass + public static void setUpMutableObject() { + mutableObject = new MutableObject("Initial value"); + } + + @Test + public void givenMutableObject_whenCalledgetValue_thenCorrect() { + assertThat(mutableObject.getValue()).isInstanceOf(String.class); + } + + @Test + public void givenMutableObject_whenCalledsetValue_thenCorrect() { + mutableObject.setValue("Another value"); + assertThat(mutableObject.getValue()).isEqualTo("Another value"); + } + + @Test + public void givenMutableObject_whenCalledtoString_thenCorrect() { + assertThat(mutableObject.toString()).isEqualTo("Another value"); + } +} diff --git a/libraries/src/test/java/com/baeldung/commons/lang3/test/MutablePairUnitTest.java b/libraries/src/test/java/com/baeldung/commons/lang3/test/MutablePairUnitTest.java new file mode 100644 index 0000000000..174ec70c4d --- /dev/null +++ b/libraries/src/test/java/com/baeldung/commons/lang3/test/MutablePairUnitTest.java @@ -0,0 +1,32 @@ +package com.baeldung.commons.lang3.test; + +import org.apache.commons.lang3.tuple.MutablePair; +import static org.assertj.core.api.Assertions.assertThat; +import org.junit.BeforeClass; +import org.junit.Test; + +public class MutablePairUnitTest { + + private static MutablePair mutablePair; + + @BeforeClass + public static void setUpMutablePairInstance() { + mutablePair = new MutablePair<>("leftElement", "rightElement"); + } + + @Test + public void givenMutablePairInstance_whenCalledgetLeft_thenCorrect() { + assertThat(mutablePair.getLeft()).isEqualTo("leftElement"); + } + + @Test + public void givenMutablePairInstance_whenCalledgetRight_thenCorrect() { + assertThat(mutablePair.getRight()).isEqualTo("rightElement"); + } + + @Test + public void givenMutablePairInstance_whenCalledsetLeft_thenCorrect() { + mutablePair.setLeft("newLeftElement"); + assertThat(mutablePair.getLeft()).isEqualTo("newLeftElement"); + } +} diff --git a/libraries/src/test/java/com/baeldung/commons/lang3/test/NumberUtilsUnitTest.java b/libraries/src/test/java/com/baeldung/commons/lang3/test/NumberUtilsUnitTest.java new file mode 100644 index 0000000000..bdf254a102 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/commons/lang3/test/NumberUtilsUnitTest.java @@ -0,0 +1,46 @@ +package com.baeldung.commons.lang3.test; + +import org.apache.commons.lang3.math.NumberUtils; +import static org.assertj.core.api.Assertions.assertThat; +import org.junit.Test; + +public class NumberUtilsUnitTest { + + @Test + public void givenNumberUtilsClass_whenCalledcompareWithIntegers_thenCorrect() { + assertThat(NumberUtils.compare(1, 1)).isEqualTo(0); + } + + @Test + public void givenNumberUtilsClass_whenCalledcompareWithLongs_thenCorrect() { + assertThat(NumberUtils.compare(1L, 1L)).isEqualTo(0); + } + + @Test + public void givenNumberUtilsClass_whenCalledcreateNumber_thenCorrect() { + assertThat(NumberUtils.createNumber("123456")).isEqualTo(123456); + } + + @Test + public void givenNumberUtilsClass_whenCalledisDigits_thenCorrect() { + assertThat(NumberUtils.isDigits("123456")).isTrue(); + } + + @Test + public void givenNumberUtilsClass_whenCallemaxwithIntegerArray_thenCorrect() { + int[] array = {1, 2, 3, 4, 5, 6}; + assertThat(NumberUtils.max(array)).isEqualTo(6); + } + + @Test + public void givenNumberUtilsClass_whenCalleminwithIntegerArray_thenCorrect() { + int[] array = {1, 2, 3, 4, 5, 6}; + assertThat(NumberUtils.min(array)).isEqualTo(1); + } + + @Test + public void givenNumberUtilsClass_whenCalleminwithByteArray_thenCorrect() { + byte[] array = {1, 2, 3, 4, 5, 6}; + assertThat(NumberUtils.min(array)).isEqualTo((byte) 1); + } +} diff --git a/libraries/src/test/java/com/baeldung/commons/lang3/test/StringUtilsUnitTest.java b/libraries/src/test/java/com/baeldung/commons/lang3/test/StringUtilsUnitTest.java new file mode 100644 index 0000000000..ab39ba7bd9 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/commons/lang3/test/StringUtilsUnitTest.java @@ -0,0 +1,73 @@ +package com.baeldung.commons.lang3.test; + +import org.apache.commons.lang3.StringUtils; +import static org.assertj.core.api.Assertions.assertThat; +import org.junit.Test; + +public class StringUtilsUnitTest { + + @Test + public void givenStringUtilsClass_whenCalledisBlank_thenCorrect() { + assertThat(StringUtils.isBlank(" ")).isTrue(); + } + + @Test + public void givenStringUtilsClass_whenCalledisEmpty_thenCorrect() { + assertThat(StringUtils.isEmpty("")).isTrue(); + } + + @Test + public void givenStringUtilsClass_whenCalledisAllLowerCase_thenCorrect() { + assertThat(StringUtils.isAllLowerCase("abd")).isTrue(); + } + + @Test + public void givenStringUtilsClass_whenCalledisAllUpperCase_thenCorrect() { + assertThat(StringUtils.isAllUpperCase("ABC")).isTrue(); + } + + @Test + public void givenStringUtilsClass_whenCalledisMixedCase_thenCorrect() { + assertThat(StringUtils.isMixedCase("abC")).isTrue(); + } + + @Test + public void givenStringUtilsClass_whenCalledisAlpha_thenCorrect() { + assertThat(StringUtils.isAlpha("abc")).isTrue(); + } + + @Test + public void givenStringUtilsClass_whenCalledisAlphanumeric_thenCorrect() { + assertThat(StringUtils.isAlphanumeric("abc123")).isTrue(); + } + + @Test + public void givenStringUtilsClass_whenCalledcontains_thenCorrect() { + assertThat(StringUtils.contains("abc", "ab")).isTrue(); + } + + @Test + public void givenStringUtilsClass_whenCalledcontainsAny_thenCorrect() { + assertThat(StringUtils.containsAny("abc", 'a', 'b', 'c')).isTrue(); + } + + @Test + public void givenStringUtilsClass_whenCalledcontainsIgnoreCase_thenCorrect() { + assertThat(StringUtils.containsIgnoreCase("abc", "ABC")).isTrue(); + } + + @Test + public void givenStringUtilsClass_whenCalledswapCase_thenCorrect() { + assertThat(StringUtils.swapCase("abc")).isEqualTo("ABC"); + } + + @Test + public void givenStringUtilsClass_whenCalledreverse_thenCorrect() { + assertThat(StringUtils.reverse("abc")).isEqualTo("cba"); + } + + @Test + public void givenStringUtilsClass_whenCalleddifference_thenCorrect() { + assertThat(StringUtils.difference("abc", "abd")).isEqualTo("d"); + } +} diff --git a/libraries/src/test/java/com/baeldung/commons/lang3/test/SystemsUtilsUnitTest.java b/libraries/src/test/java/com/baeldung/commons/lang3/test/SystemsUtilsUnitTest.java new file mode 100644 index 0000000000..0efe97f912 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/commons/lang3/test/SystemsUtilsUnitTest.java @@ -0,0 +1,25 @@ +package com.baeldung.commons.lang3.test; + +import java.io.File; +import org.apache.commons.lang3.JavaVersion; +import org.apache.commons.lang3.SystemUtils; +import static org.assertj.core.api.Assertions.assertThat; +import org.junit.Test; + +public class SystemsUtilsUnitTest { + + @Test + public void givenSystemUtilsClass_whenCalledgetJavaHome_thenCorrect() { + assertThat(SystemUtils.getJavaHome()).isEqualTo(new File("/usr/lib/jvm/java-8-oracle/jre")); + } + + @Test + public void givenSystemUtilsClass_whenCalledgetUserHome_thenCorrect() { + assertThat(SystemUtils.getUserHome()).isEqualTo(new File("/home/travis")); + } + + @Test + public void givenSystemUtilsClass_whenCalledisJavaVersionAtLeast_thenCorrect() { + assertThat(SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_RECENT)).isTrue(); + } +} diff --git a/libraries/src/test/java/com/baeldung/commons/lang3/test/TripleUnitTest.java b/libraries/src/test/java/com/baeldung/commons/lang3/test/TripleUnitTest.java new file mode 100644 index 0000000000..7d506bbbab --- /dev/null +++ b/libraries/src/test/java/com/baeldung/commons/lang3/test/TripleUnitTest.java @@ -0,0 +1,31 @@ +package com.baeldung.commons.lang3.test; + +import org.apache.commons.lang3.tuple.Triple; +import static org.assertj.core.api.Assertions.assertThat; +import org.junit.BeforeClass; +import org.junit.Test; + +public class TripleUnitTest { + + private static Triple triple; + + @BeforeClass + public static void setUpTripleInstance() { + triple = Triple.of("leftElement", "middleElement", "rightElement"); + } + + @Test + public void givenTripleInstance_whenCalledgetLeft_thenCorrect() { + assertThat(triple.getLeft()).isEqualTo("leftElement"); + } + + @Test + public void givenTripleInstance_whenCalledgetMiddle_thenCorrect() { + assertThat(triple.getMiddle()).isEqualTo("middleElement"); + } + + @Test + public void givenTripleInstance_whenCalledgetRight_thenCorrect() { + assertThat(triple.getRight()).isEqualTo("rightElement"); + } +} diff --git a/libraries/src/test/java/com/baeldung/kafkastreams/KafkaStreamsLiveTest.java b/libraries/src/test/java/com/baeldung/kafkastreams/KafkaStreamsLiveTest.java index 4406494d30..e61f4158a7 100644 --- a/libraries/src/test/java/com/baeldung/kafkastreams/KafkaStreamsLiveTest.java +++ b/libraries/src/test/java/com/baeldung/kafkastreams/KafkaStreamsLiveTest.java @@ -4,10 +4,12 @@ import org.apache.kafka.clients.consumer.ConsumerConfig; import org.apache.kafka.common.serialization.Serde; import org.apache.kafka.common.serialization.Serdes; import org.apache.kafka.streams.KafkaStreams; +import org.apache.kafka.streams.StreamsBuilder; import org.apache.kafka.streams.StreamsConfig; +import org.apache.kafka.streams.Topology; import org.apache.kafka.streams.kstream.KStream; -import org.apache.kafka.streams.kstream.KStreamBuilder; import org.apache.kafka.streams.kstream.KTable; +import org.apache.kafka.streams.kstream.Produced; import org.apache.kafka.test.TestUtils; import org.junit.Ignore; import org.junit.Test; @@ -36,20 +38,20 @@ public class KafkaStreamsLiveTest { streamsConfiguration.put(StreamsConfig.STATE_DIR_CONFIG, TestUtils.tempDirectory().getAbsolutePath()); // when - KStreamBuilder builder = new KStreamBuilder(); + StreamsBuilder builder = new StreamsBuilder(); 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(); - wordCounts.foreach((word, count) -> System.out.println("word: " + word + " -> " + count)); + textLines.foreach((word, count) -> System.out.println("word: " + word + " -> " + count)); String outputTopic = "outputTopic"; final Serde stringSerde = Serdes.String(); - final Serde longSerde = Serdes.Long(); - wordCounts.to(stringSerde, longSerde, outputTopic); + final Serde longSerde = Serdes.String(); + textLines.to(outputTopic, Produced.with(stringSerde,longSerde)); - KafkaStreams streams = new KafkaStreams(builder, streamsConfiguration); + KafkaStreams streams = new KafkaStreams(new Topology(), streamsConfiguration); streams.start(); // then diff --git a/logging-modules/log4j/README.md b/logging-modules/log4j/README.md index 8aae1b5826..9f4184ccba 100644 --- a/logging-modules/log4j/README.md +++ b/logging-modules/log4j/README.md @@ -4,3 +4,4 @@ - [Generate equals() and hashCode() with Eclipse](http://www.baeldung.com/java-eclipse-equals-and-hashcode) - [Introduction to SLF4J](http://www.baeldung.com/slf4j-with-log4j2-logback) - [A Guide to Rolling File Appenders](http://www.baeldung.com/java-logging-rolling-file-appenders) +- [Logging Exceptions Using SLF4J](https://www.baeldung.com/slf4j-log-exceptions) diff --git a/logging-modules/log4j2/README.md b/logging-modules/log4j2/README.md index e209c6f035..2cf6f9768f 100644 --- a/logging-modules/log4j2/README.md +++ b/logging-modules/log4j2/README.md @@ -3,3 +3,4 @@ - [Intro to Log4j2 – Appenders, Layouts and Filters](http://www.baeldung.com/log4j2-appenders-layouts-filters) - [Log4j 2 and Lambda Expressions](http://www.baeldung.com/log4j-2-lazy-logging) - [Programmatic Configuration with Log4j 2](http://www.baeldung.com/log4j2-programmatic-config) +- [Creating a Custom Log4j2 Appender](https://www.baeldung.com/log4j2-custom-appender) diff --git a/lombok/src/main/java/com/baeldung/lombok/builder/Child.java b/lombok/src/main/java/com/baeldung/lombok/builder/Child.java new file mode 100644 index 0000000000..70f6d9c46e --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/builder/Child.java @@ -0,0 +1,19 @@ +package com.baeldung.lombok.builder; + +import lombok.Builder; +import lombok.Getter; + +@Getter +public class Child extends Parent { + + private final String childName; + private final int childAge; + + @Builder(builderMethodName = "childBuilder") + public Child(String parentName, int parentAge, String childName, int childAge) { + super(parentName, parentAge); + this.childName = childName; + this.childAge = childAge; + } + +} diff --git a/lombok/src/main/java/com/baeldung/lombok/builder/Parent.java b/lombok/src/main/java/com/baeldung/lombok/builder/Parent.java new file mode 100644 index 0000000000..0cf76d4b00 --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/builder/Parent.java @@ -0,0 +1,11 @@ +package com.baeldung.lombok.builder; + +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class Parent { + private final String parentName; + private final int parentAge; +} \ No newline at end of file diff --git a/lombok/src/main/java/com/baeldung/lombok/getter/GetterBoolean.java b/lombok/src/main/java/com/baeldung/lombok/getter/GetterBoolean.java new file mode 100644 index 0000000000..2191396e5d --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/getter/GetterBoolean.java @@ -0,0 +1,15 @@ +package com.baeldung.lombok.getter; + + +import lombok.Getter; + +/** + * Related Article Sections: + * 4. Using @Getter on a Boolean Field + * + */ +public class GetterBoolean { + + @Getter + private Boolean running = true; +} diff --git a/lombok/src/main/java/com/baeldung/lombok/getter/GetterBooleanPrimitive.java b/lombok/src/main/java/com/baeldung/lombok/getter/GetterBooleanPrimitive.java new file mode 100644 index 0000000000..5601f85b8b --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/getter/GetterBooleanPrimitive.java @@ -0,0 +1,16 @@ +package com.baeldung.lombok.getter; + + +import lombok.Getter; + +/** + * Related Article Sections: + * 3. Using @Getter on a boolean Field + * + */ +public class GetterBooleanPrimitive { + + @Getter + private boolean running; + +} diff --git a/lombok/src/main/java/com/baeldung/lombok/getter/GetterBooleanPrimitiveSameAccessor.java b/lombok/src/main/java/com/baeldung/lombok/getter/GetterBooleanPrimitiveSameAccessor.java new file mode 100644 index 0000000000..af29a33c20 --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/getter/GetterBooleanPrimitiveSameAccessor.java @@ -0,0 +1,18 @@ +package com.baeldung.lombok.getter; + + +import lombok.Getter; + +/** + * Related Article Sections: + * 3.2. Two boolean Fields With the Same Accessor Name + * + */ +public class GetterBooleanPrimitiveSameAccessor { + + @Getter + boolean running = true; + + @Getter + boolean isRunning = false; +} diff --git a/lombok/src/main/java/com/baeldung/lombok/getter/GetterBooleanSameAccessor.java b/lombok/src/main/java/com/baeldung/lombok/getter/GetterBooleanSameAccessor.java new file mode 100644 index 0000000000..d972273b71 --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/getter/GetterBooleanSameAccessor.java @@ -0,0 +1,13 @@ +package com.baeldung.lombok.getter; + +import lombok.Getter; + +/** + * Related Article Sections: + * 3.1. A boolean Field Having the Same Name With Its Accessor + * + */ +public class GetterBooleanSameAccessor { + @Getter + private boolean isRunning = true; +} diff --git a/lombok/src/main/java/com/baeldung/lombok/getter/GetterBooleanType.java b/lombok/src/main/java/com/baeldung/lombok/getter/GetterBooleanType.java new file mode 100644 index 0000000000..0d3b9a928a --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/getter/GetterBooleanType.java @@ -0,0 +1,15 @@ +package com.baeldung.lombok.getter; + + +import lombok.Getter; + +/** + * Related Article Sections: + * 4. Using @Getter on a Boolean Field + * + */ +public class GetterBooleanType { + + @Getter + private Boolean running = true; +} diff --git a/lombok/src/test/java/com/baeldung/lombok/builder/BuilderUnitTest.java b/lombok/src/test/java/com/baeldung/lombok/builder/BuilderUnitTest.java index acad7d6c04..56a380569d 100644 --- a/lombok/src/test/java/com/baeldung/lombok/builder/BuilderUnitTest.java +++ b/lombok/src/test/java/com/baeldung/lombok/builder/BuilderUnitTest.java @@ -1,42 +1,59 @@ package com.baeldung.lombok.builder; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.*; - -public class BuilderUnitTest -{ +public class BuilderUnitTest { @Test public void givenBuilder_WidgetIsBuilt() { - Widget testWidget = Widget.builder().name("foo").id(1).build(); - assertThat(testWidget.getName()) - .isEqualTo("foo"); - assertThat(testWidget.getId()) - .isEqualTo(1); + Widget testWidget = Widget.builder() + .name("foo") + .id(1) + .build(); + assertThat(testWidget.getName()).isEqualTo("foo"); + assertThat(testWidget.getId()).isEqualTo(1); } @Test public void givenToBuilder_whenToBuilder_BuilderIsCreated() { - Widget testWidget = Widget.builder().name("foo").id(1).build(); + Widget testWidget = Widget.builder() + .name("foo") + .id(1) + .build(); Widget.WidgetBuilder widgetBuilder = testWidget.toBuilder(); - Widget newWidget = widgetBuilder.id(2).build(); - assertThat(newWidget.getName()) - .isEqualTo("foo"); - assertThat(newWidget.getId()) - .isEqualTo(2); + Widget newWidget = widgetBuilder.id(2) + .build(); + assertThat(newWidget.getName()).isEqualTo("foo"); + assertThat(newWidget.getId()).isEqualTo(2); } - - @Test public void givenBuilderMethod_ClientIsBuilt() { - ImmutableClient testImmutableClient = ClientBuilder.builder().name("foo").id(1).build(); - assertThat(testImmutableClient.getName()) - .isEqualTo("foo"); - assertThat(testImmutableClient.getId()) - .isEqualTo(1); + ImmutableClient testImmutableClient = ClientBuilder.builder() + .name("foo") + .id(1) + .build(); + assertThat(testImmutableClient.getName()).isEqualTo("foo"); + assertThat(testImmutableClient.getId()).isEqualTo(1); } + + @Test + public void givenBuilderAtMethodLevel_ChildInheritingParentIsBuilt() { + Child child = Child.childBuilder() + .parentName("Andrea") + .parentAge(38) + .childName("Emma") + .childAge(6) + .build(); + + assertThat(child.getChildName()).isEqualTo("Emma"); + assertThat(child.getChildAge()).isEqualTo(6); + assertThat(child.getParentName()).isEqualTo("Andrea"); + assertThat(child.getParentAge()).isEqualTo(38); + } + } diff --git a/lombok/src/test/java/com/baeldung/lombok/getter/GetterBooleanUnitTest.java b/lombok/src/test/java/com/baeldung/lombok/getter/GetterBooleanUnitTest.java new file mode 100644 index 0000000000..632594d575 --- /dev/null +++ b/lombok/src/test/java/com/baeldung/lombok/getter/GetterBooleanUnitTest.java @@ -0,0 +1,34 @@ +package com.baeldung.lombok.getter; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class GetterBooleanUnitTest { + + @Test + public void whenBasicBooleanField_thenMethodNamePrefixedWithIsFollowedByFieldName() { + GetterBooleanPrimitive lombokExamples = new GetterBooleanPrimitive(); + assertFalse(lombokExamples.isRunning()); + } + + @Test + public void whenBooleanFieldPrefixedWithIs_thenMethodNameIsSameAsFieldName() { + GetterBooleanSameAccessor lombokExamples = new GetterBooleanSameAccessor(); + assertTrue(lombokExamples.isRunning()); + } + + @Test + public void whenTwoBooleanFieldsCauseNamingConflict_thenLombokMapsToFirstDeclaredField() { + GetterBooleanPrimitiveSameAccessor lombokExamples = new GetterBooleanPrimitiveSameAccessor(); + assertTrue(lombokExamples.isRunning() == lombokExamples.running); + assertFalse(lombokExamples.isRunning() == lombokExamples.isRunning); + } + + @Test + public void whenFieldOfBooleanType_thenLombokPrefixesMethodWithGetInsteadOfIs() { + GetterBooleanType lombokExamples = new GetterBooleanType(); + assertTrue(lombokExamples.getRunning()); + } +} diff --git a/lucene/README.md b/lucene/README.md index b1d33472f4..ea7f715480 100644 --- a/lucene/README.md +++ b/lucene/README.md @@ -2,3 +2,4 @@ - [Introduction to Apache Lucene](http://www.baeldung.com/lucene) - [A Simple File Search with Lucene](http://www.baeldung.com/lucene-file-search) +- [Guide to Lucene Analyzers](https://www.baeldung.com/lucene-analyzers) diff --git a/maven/README.md b/maven/README.md index c5b875ce02..2d838bcb76 100644 --- a/maven/README.md +++ b/maven/README.md @@ -8,3 +8,5 @@ - [The Maven Verifier Plugin](http://www.baeldung.com/maven-verifier-plugin) - [The Maven Clean Plugin](http://www.baeldung.com/maven-clean-plugin) - [Build a Jar with Maven and Ignore the Test Results](http://www.baeldung.com/maven-ignore-test-results) +- [Maven Project with Multiple Source Directories](https://www.baeldung.com/maven-project-multiple-src-directories) +- [Integration Testing with Maven](https://www.baeldung.com/maven-integration-test) diff --git a/meecrowave/pom.xml b/meecrowave/pom.xml deleted file mode 100644 index cf13aa1c1b..0000000000 --- a/meecrowave/pom.xml +++ /dev/null @@ -1,57 +0,0 @@ - - 4.0.0 - com.baeldung - apache-meecrowave - 0.0.1 - apache-meecrowave - A sample REST API application with Meecrowave - - - 1.8 - 1.8 - - - - - org.apache.meecrowave - meecrowave-core - 1.2.1 - - - - org.apache.meecrowave - meecrowave-jpa - 1.2.1 - - - - com.squareup.okhttp3 - okhttp - 3.10.0 - - - org.apache.meecrowave - meecrowave-junit - 1.2.0 - test - - - - junit - junit - 4.10 - test - - - - - - - org.apache.meecrowave - meecrowave-maven-plugin - 1.2.1 - - - - \ No newline at end of file diff --git a/meecrowave/src/main/java/com/baeldung/meecrowave/Article.java b/meecrowave/src/main/java/com/baeldung/meecrowave/Article.java deleted file mode 100644 index 7925e8ff99..0000000000 --- a/meecrowave/src/main/java/com/baeldung/meecrowave/Article.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.baeldung.meecrowave; - -public class Article { - private String name; - private String author; - - public Article() { - } - - public Article(String name, String author) { - this.author = author; - this.name = name; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getAuthor() { - return author; - } - - public void setAuthor(String author) { - this.author = author; - } -} \ No newline at end of file diff --git a/meecrowave/src/main/java/com/baeldung/meecrowave/ArticleEndpoints.java b/meecrowave/src/main/java/com/baeldung/meecrowave/ArticleEndpoints.java deleted file mode 100644 index 6cb7012c64..0000000000 --- a/meecrowave/src/main/java/com/baeldung/meecrowave/ArticleEndpoints.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.meecrowave; - -import javax.enterprise.context.RequestScoped; -import javax.inject.Inject; -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; - -@RequestScoped -@Path("article") -public class ArticleEndpoints { - - @Inject - ArticleService articleService; - - @GET - public Response getArticle() { - return Response.ok() - .entity(new Article("name", "author")) - .build(); - - } - - @POST - public Response createArticle(Article article) { - return Response.status(Status.CREATED) - .entity(articleService.createArticle(article)) - .build(); - } -} \ No newline at end of file diff --git a/meecrowave/src/main/java/com/baeldung/meecrowave/ArticleService.java b/meecrowave/src/main/java/com/baeldung/meecrowave/ArticleService.java deleted file mode 100644 index 7bd6b87345..0000000000 --- a/meecrowave/src/main/java/com/baeldung/meecrowave/ArticleService.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.baeldung.meecrowave; - -import javax.enterprise.context.ApplicationScoped; - -@ApplicationScoped -public class ArticleService { - public Article createArticle(Article article) { - return article; - } -} diff --git a/meecrowave/src/main/java/com/baeldung/meecrowave/Server.java b/meecrowave/src/main/java/com/baeldung/meecrowave/Server.java deleted file mode 100644 index 2aa7d0556f..0000000000 --- a/meecrowave/src/main/java/com/baeldung/meecrowave/Server.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.meecrowave; - -import org.apache.meecrowave.Meecrowave; - -public class Server { - public static void main(String[] args) { - final Meecrowave.Builder builder = new Meecrowave.Builder(); - builder.setScanningPackageIncludes("com.baeldung.meecrowave"); - builder.setJaxrsMapping("/api/*"); - builder.setJsonpPrettify(true); - - try (Meecrowave meecrowave = new Meecrowave(builder)) { - meecrowave.bake().await(); - } - } -} diff --git a/meecrowave/src/test/java/com/baeldung/meecrowave/ArticleEndpointsTest.java b/meecrowave/src/test/java/com/baeldung/meecrowave/ArticleEndpointsTest.java deleted file mode 100644 index 0dc9773490..0000000000 --- a/meecrowave/src/test/java/com/baeldung/meecrowave/ArticleEndpointsTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.baeldung.meecrowave; - - -import static org.junit.Assert.assertEquals; - -import java.io.IOException; - -import org.apache.meecrowave.Meecrowave; -import org.apache.meecrowave.junit.MonoMeecrowave; -import org.apache.meecrowave.testing.ConfigurationInject; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; - -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; - -@RunWith(MonoMeecrowave.Runner.class) -public class ArticleEndpointsTest { - - @ConfigurationInject - private Meecrowave.Builder config; - private static OkHttpClient client; - - @BeforeClass - public static void setup() { - client = new OkHttpClient(); - } - - @Test - public void whenRetunedArticle_thenCorrect() throws IOException { - final String base = "http://localhost:"+config.getHttpPort(); - - Request request = new Request.Builder() - .url(base+"/article") - .build(); - Response response = client.newCall(request).execute(); - assertEquals(200, response.code()); - } -} diff --git a/metrics/pom.xml b/metrics/pom.xml index 86f197240b..9cf1ec588f 100644 --- a/metrics/pom.xml +++ b/metrics/pom.xml @@ -84,6 +84,13 @@ spectator-api ${spectator-api.version} + + + org.assertj + assertj-core + test + + diff --git a/metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasIntegrationTest.java b/metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasIntegrationTest.java index e7f3d7ec72..689e23ad6d 100644 --- a/metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasIntegrationTest.java +++ b/metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasIntegrationTest.java @@ -1,8 +1,12 @@ package com.baeldung.metrics.micrometer; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.within; +import static org.assertj.core.api.Assertions.withinPercentage; import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.collection.IsMapContaining.hasEntry; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import io.micrometer.atlas.AtlasMeterRegistry; @@ -31,8 +35,10 @@ import java.util.Optional; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; +import org.assertj.core.data.Percentage; import org.junit.Before; import org.junit.Test; +import org.junit.jupiter.api.Assertions; import com.netflix.spectator.atlas.AtlasConfig; @@ -156,7 +162,8 @@ public class MicrometerAtlasIntegrationTest { timer.record(30, TimeUnit.MILLISECONDS); assertTrue(2 == timer.count()); - assertTrue(50 > timer.totalTime(TimeUnit.MILLISECONDS) && 45 <= timer.totalTime(TimeUnit.MILLISECONDS)); + + assertThat(timer.totalTime(TimeUnit.MILLISECONDS)).isBetween(40.0, 55.0); } @Test @@ -173,7 +180,7 @@ public class MicrometerAtlasIntegrationTest { } long timeElapsed = longTaskTimer.stop(currentTaskId); - assertTrue(timeElapsed / (int) 1e6 == 2); + assertEquals(2L, timeElapsed/((int) 1e6),1L); } @Test diff --git a/metrics/src/test/java/com/baeldung/metrics/servo/AtlasObserverLiveTest.java b/metrics/src/test/java/com/baeldung/metrics/servo/AtlasObserverLiveTest.java index 134c3c91cf..b8a5b93e49 100644 --- a/metrics/src/test/java/com/baeldung/metrics/servo/AtlasObserverLiveTest.java +++ b/metrics/src/test/java/com/baeldung/metrics/servo/AtlasObserverLiveTest.java @@ -27,6 +27,9 @@ import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.not; import static org.junit.Assert.assertThat; +/** + * Atlas server needs to be up and running for this live test to work. + */ public class AtlasObserverLiveTest { private final String atlasUri = "http://localhost:7101/api/v1"; diff --git a/metrics/src/test/java/com/baeldung/metrics/servo/MetricTypeManualTest.java b/metrics/src/test/java/com/baeldung/metrics/servo/MetricTypeManualTest.java index d810de155a..92bbd615b9 100644 --- a/metrics/src/test/java/com/baeldung/metrics/servo/MetricTypeManualTest.java +++ b/metrics/src/test/java/com/baeldung/metrics/servo/MetricTypeManualTest.java @@ -109,20 +109,20 @@ public class MetricTypeManualTest { .build(), MILLISECONDS); Stopwatch stopwatch = timer.start(); - MILLISECONDS.sleep(1); - timer.record(2, MILLISECONDS); + SECONDS.sleep(1); + timer.record(2, SECONDS); stopwatch.stop(); - assertEquals("timer should count 1 millisecond", 1, timer + assertEquals("timer should count 1 second", 1000, timer .getValue() - .intValue()); - assertEquals("timer should count 3 millisecond in total", 3, timer.getTotalTime() - .intValue()); + .intValue(),1000); + assertEquals("timer should count 3 second in total", 3000, timer.getTotalTime() + .intValue(),1000); assertEquals("timer should record 2 updates", 2, timer .getCount() .intValue()); - assertEquals("timer should have max 2", 2, timer.getMax(), 0.01); + assertEquals("timer should have max 2", 2000, timer.getMax(), 0.01); } @Test @@ -161,7 +161,6 @@ public class MetricTypeManualTest { } @Test - //== public void givenStatsTimer_whenExecuteTask_thenStatsCalculated() throws Exception { System.setProperty("netflix.servo", "1000"); StatsTimer timer = new StatsTimer(MonitorConfig @@ -178,21 +177,21 @@ public class MetricTypeManualTest { .build(), MILLISECONDS); Stopwatch stopwatch = timer.start(); - MILLISECONDS.sleep(1); - timer.record(3, MILLISECONDS); + SECONDS.sleep(1); + timer.record(3, SECONDS); stopwatch.stop(); stopwatch = timer.start(); - timer.record(6, MILLISECONDS); - MILLISECONDS.sleep(2); + timer.record(6, SECONDS); + SECONDS.sleep(2); stopwatch.stop(); - assertEquals("timer should count 12 milliseconds in total", 12, timer.getTotalTime()); - assertEquals("timer should count 12 milliseconds in total", 12, timer.getTotalMeasurement()); + assertEquals("timer should count 12 seconds in total", 12000, timer.getTotalTime(),500); + assertEquals("timer should count 12 seconds in total", 12000, timer.getTotalMeasurement(),500); assertEquals("timer should record 4 updates", 4, timer.getCount()); - assertEquals("stats timer value time-cost/update should be 2", 3, timer + assertEquals("stats timer value time-cost/update should be 2", 3000, timer .getValue() - .intValue()); + .intValue(),500); final Map metricMap = timer .getMonitors() @@ -235,4 +234,4 @@ public class MetricTypeManualTest { assertEquals("information collected", informational.getValue()); } -} +} \ No newline at end of file diff --git a/micronaut/pom.xml b/micronaut/pom.xml index 56d051e506..7b722306b6 100644 --- a/micronaut/pom.xml +++ b/micronaut/pom.xml @@ -1,8 +1,10 @@ 4.0.0 com.baeldung.micronaut - hello-world + micronaut 0.1 + micronaut + com.baeldung.micronaut.helloworld.server.ServerApplication 1.0.0.M2 diff --git a/msf4j/pom.xml b/msf4j/pom.xml index cca8281764..9a6483772b 100644 --- a/msf4j/pom.xml +++ b/msf4j/pom.xml @@ -6,12 +6,22 @@ msf4j 0.0.1-SNAPSHOT - - org.wso2.msf4j - msf4j-service - 2.6.0 - + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + + + + org.wso2.msf4j + msf4j-service + ${msf4j.version} + pom + + + org.wso2.msf4j @@ -27,7 +37,7 @@ com.baeldung.msf4j.msf4jintro.Application - 2.6.1 + 2.6.3 \ No newline at end of file diff --git a/optaplanner/README.md b/optaplanner/README.md new file mode 100644 index 0000000000..d793be1f2a --- /dev/null +++ b/optaplanner/README.md @@ -0,0 +1,3 @@ +### Relevant articles + +- [Guide to OptaPlanner](https://www.baeldung.com/opta-planner) diff --git a/osgi/osgi-intro-sample-activator/pom.xml b/osgi/osgi-intro-sample-activator/pom.xml index 42008dafdd..77d7198698 100644 --- a/osgi/osgi-intro-sample-activator/pom.xml +++ b/osgi/osgi-intro-sample-activator/pom.xml @@ -8,7 +8,7 @@ - osgi-intro + osgi com.baeldung 1.0-SNAPSHOT diff --git a/osgi/osgi-intro-sample-client/pom.xml b/osgi/osgi-intro-sample-client/pom.xml index f0cf0a357a..7f5faedb30 100644 --- a/osgi/osgi-intro-sample-client/pom.xml +++ b/osgi/osgi-intro-sample-client/pom.xml @@ -8,7 +8,7 @@ bundle - osgi-intro + osgi com.baeldung 1.0-SNAPSHOT diff --git a/osgi/osgi-intro-sample-service/pom.xml b/osgi/osgi-intro-sample-service/pom.xml index 29741e7eb2..8f81645ad4 100644 --- a/osgi/osgi-intro-sample-service/pom.xml +++ b/osgi/osgi-intro-sample-service/pom.xml @@ -8,7 +8,7 @@ - osgi-intro + osgi com.baeldung 1.0-SNAPSHOT diff --git a/osgi/pom.xml b/osgi/pom.xml index 7c59fbaff4..9ebcb09091 100644 --- a/osgi/pom.xml +++ b/osgi/pom.xml @@ -2,9 +2,10 @@ 4.0.0 - osgi-intro + osgi pom 1.0-SNAPSHOT + osgi com.baeldung diff --git a/parent-boot-1/pom.xml b/parent-boot-1/pom.xml index c52327a42e..7742841d07 100644 --- a/parent-boot-1/pom.xml +++ b/parent-boot-1/pom.xml @@ -37,16 +37,17 @@ - - - org.springframework.boot - spring-boot-maven-plugin - 1.5.15.RELEASE - - + + + + org.springframework.boot + spring-boot-maven-plugin + 1.5.15.RELEASE + + + - 3.1.0 diff --git a/parent-boot-2/pom.xml b/parent-boot-2/pom.xml index 86a77a90db..de6cb5d068 100644 --- a/parent-boot-2/pom.xml +++ b/parent-boot-2/pom.xml @@ -36,13 +36,15 @@ - - - org.springframework.boot - spring-boot-maven-plugin - 2.0.4.RELEASE - - + + + + org.springframework.boot + spring-boot-maven-plugin + 2.0.4.RELEASE + + + diff --git a/patterns/design-patterns/README.md b/patterns/design-patterns/README.md index 5fb8f735ab..75f7cec73a 100644 --- a/patterns/design-patterns/README.md +++ b/patterns/design-patterns/README.md @@ -11,3 +11,4 @@ - [Visitor Design Pattern in Java](http://www.baeldung.com/java-visitor-pattern) - [The DAO Pattern in Java](http://www.baeldung.com/java-dao-pattern) - [Interpreter Design Pattern in Java](http://www.baeldung.com/java-interpreter-pattern) +- [State Design Pattern in Java](https://www.baeldung.com/java-state-design-pattern) diff --git a/patterns/design-patterns/pom.xml b/patterns/design-patterns/pom.xml index 22fc88b75a..dc2631b36e 100644 --- a/patterns/design-patterns/pom.xml +++ b/patterns/design-patterns/pom.xml @@ -2,13 +2,12 @@ 4.0.0 - com.baeldung design-patterns 1.0 jar com.baeldung - patterns-parent + patterns 1.0.0-SNAPSHOT .. diff --git a/patterns/front-controller/pom.xml b/patterns/front-controller/pom.xml index 96ed86d975..435b0dd9cd 100644 --- a/patterns/front-controller/pom.xml +++ b/patterns/front-controller/pom.xml @@ -6,7 +6,7 @@ war - patterns-parent + patterns com.baeldung 1.0.0-SNAPSHOT .. diff --git a/patterns/intercepting-filter/pom.xml b/patterns/intercepting-filter/pom.xml index ee6fef30c9..fa94d5d1fd 100644 --- a/patterns/intercepting-filter/pom.xml +++ b/patterns/intercepting-filter/pom.xml @@ -8,7 +8,7 @@ com.baeldung - patterns-parent + patterns 1.0.0-SNAPSHOT .. diff --git a/patterns/pom.xml b/patterns/pom.xml index c8f44227a3..df09f1836a 100644 --- a/patterns/pom.xml +++ b/patterns/pom.xml @@ -2,8 +2,7 @@ 4.0.0 - com.baeldung - patterns-parent + patterns pom diff --git a/performance-tests/pom.xml b/performance-tests/pom.xml index 0240b2c2ea..8c9d027724 100644 --- a/performance-tests/pom.xml +++ b/performance-tests/pom.xml @@ -3,7 +3,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - performancetests + performance-tests parent-modules diff --git a/persistence-modules/spring-data-dynamodb/pom.xml b/persistence-modules/spring-data-dynamodb/pom.xml index e5bd78d208..4cb805131a 100644 --- a/persistence-modules/spring-data-dynamodb/pom.xml +++ b/persistence-modules/spring-data-dynamodb/pom.xml @@ -2,10 +2,10 @@ 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-boot-dynamodb + spring-data-dynamodb 0.0.1-SNAPSHOT jar - spring-boot-dynamodb + spring-data-dynamodb This is simple boot application for Spring boot dynamodb test @@ -154,25 +154,6 @@ org.apache.maven.plugins maven-war-plugin - - org.apache.maven.plugins - maven-dependency-plugin - ${maven-dependency-plugin.version} - - - copy-dependencies - test-compile - - copy-dependencies - - - test - so,dll,dylib - ${project.basedir}/native-libs - - - - @@ -196,7 +177,6 @@ 1.11.106 1.11.86 https://s3-us-west-2.amazonaws.com/dynamodb-local/release - 2.10 diff --git a/persistence-modules/spring-data-eclipselink/pom.xml b/persistence-modules/spring-data-eclipselink/pom.xml index 8a55425fc2..618fa83ee5 100644 --- a/persistence-modules/spring-data-eclipselink/pom.xml +++ b/persistence-modules/spring-data-eclipselink/pom.xml @@ -2,9 +2,7 @@ 4.0.0 - com.baeldung spring-data-eclipselink - 1.0.0-SNAPSHOT spring-data-eclipselink @@ -20,7 +18,6 @@ spring-boot-dependencies ${spring.version} pom - import org.springframework.boot diff --git a/persistence-modules/spring-data-neo4j/pom.xml b/persistence-modules/spring-data-neo4j/pom.xml index bd7d783646..2be4df71be 100644 --- a/persistence-modules/spring-data-neo4j/pom.xml +++ b/persistence-modules/spring-data-neo4j/pom.xml @@ -1,7 +1,6 @@ 4.0.0 - com.baeldung spring-data-neo4j 1.0 @@ -74,11 +73,6 @@ ${neo4j-ogm.version} test - - org.neo4j - neo4j-ogm-embedded-driver - ${neo4j-ogm.version} - org.neo4j.test neo4j-harness diff --git a/persistence-modules/spring-jpa/README.md b/persistence-modules/spring-jpa/README.md index 1c89f2453d..299a5a1e51 100644 --- a/persistence-modules/spring-jpa/README.md +++ b/persistence-modules/spring-jpa/README.md @@ -18,6 +18,7 @@ - [Testing REST with multiple MIME types](http://www.baeldung.com/testing-rest-api-with-multiple-media-types) - [Obtaining Auto-generated Keys in Spring JDBC](http://www.baeldung.com/spring-jdbc-autogenerated-keys) - [Transactions with Spring 4 and JPA](http://www.baeldung.com/transaction-configuration-with-jpa-and-spring) +- [Use Criteria Queries in a Spring Data Application](https://www.baeldung.com/spring-data-criteria-queries) ### Eclipse Config After importing the project into Eclipse, you may see the following error: diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/config/PersistenceJPAConfig.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/config/PersistenceJPAConfig.java index 78c4116c67..ec0d4bca3c 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/config/PersistenceJPAConfig.java +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/config/PersistenceJPAConfig.java @@ -78,9 +78,9 @@ public class PersistenceJPAConfig { final Properties hibernateProperties = new Properties(); hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); - hibernateProperties.setProperty("hibernate.cache.use_second_level_cache", env.getProperty("hibernate.cache.use_second_level_cache")); - hibernateProperties.setProperty("hibernate.cache.use_query_cache", env.getProperty("hibernate.cache.use_query_cache")); - // hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true"); + hibernateProperties.setProperty("hibernate.cache.use_second_level_cache", "false"); + + return hibernateProperties; } diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/config/WebInitializer.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/config/WebInitializer.java index 1c8ce5400f..cf6e69eb39 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/config/WebInitializer.java +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/config/WebInitializer.java @@ -5,7 +5,7 @@ import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatche public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class[] getRootConfigClasses() { - return new Class[] { PersistenceJNDIConfig.class }; + return new Class[] { PersistenceJPAConfig.class }; } @Override diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/criteria/repository/BookRepository.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookRepository.java similarity index 71% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/criteria/repository/BookRepository.java rename to persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookRepository.java index af30ae461e..114cf48c7c 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/criteria/repository/BookRepository.java +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookRepository.java @@ -1,6 +1,6 @@ -package org.baeldung.persistence.criteria.repository; +package org.baeldung.persistence.dao; -import org.baeldung.persistence.criteria.model.Book; +import org.baeldung.persistence.model.Book; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/criteria/repository/BookRepositoryCustom.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookRepositoryCustom.java similarity index 58% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/criteria/repository/BookRepositoryCustom.java rename to persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookRepositoryCustom.java index 35330cfa3c..b939907572 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/criteria/repository/BookRepositoryCustom.java +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookRepositoryCustom.java @@ -1,8 +1,8 @@ -package org.baeldung.persistence.criteria.repository; +package org.baeldung.persistence.dao; import java.util.List; -import org.baeldung.persistence.criteria.model.Book; +import org.baeldung.persistence.model.Book; public interface BookRepositoryCustom { diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/criteria/dao/BookRepositoryImpl.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookRepositoryImpl.java similarity index 87% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/criteria/dao/BookRepositoryImpl.java rename to persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookRepositoryImpl.java index f782d69e1e..6e9aa998d7 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/criteria/dao/BookRepositoryImpl.java +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookRepositoryImpl.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.criteria.dao; +package org.baeldung.persistence.dao; import java.util.ArrayList; import java.util.List; @@ -10,8 +10,7 @@ import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; -import org.baeldung.persistence.criteria.model.Book; -import org.baeldung.persistence.criteria.repository.BookRepositoryCustom; +import org.baeldung.persistence.model.Book; import org.springframework.stereotype.Repository; @Repository diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/criteria/repository/BookService.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookService.java similarity index 63% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/criteria/repository/BookService.java rename to persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookService.java index 7b1aff857e..88b769e9bf 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/criteria/repository/BookService.java +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookService.java @@ -1,12 +1,12 @@ -package org.baeldung.persistence.criteria.repository; +package org.baeldung.persistence.dao; -import static org.baeldung.persistence.criteria.repository.BookSpecifications.hasAuthor; -import static org.baeldung.persistence.criteria.repository.BookSpecifications.titleContains; +import static org.baeldung.persistence.dao.BookSpecifications.hasAuthor; +import static org.baeldung.persistence.dao.BookSpecifications.titleContains; import static org.springframework.data.jpa.domain.Specifications.where; import java.util.List; -import org.baeldung.persistence.criteria.model.Book; +import org.baeldung.persistence.model.Book; import org.springframework.stereotype.Service; @Service diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/criteria/repository/BookSpecifications.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookSpecifications.java similarity index 78% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/criteria/repository/BookSpecifications.java rename to persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookSpecifications.java index 392b750977..ed9540060d 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/criteria/repository/BookSpecifications.java +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/dao/BookSpecifications.java @@ -1,6 +1,6 @@ -package org.baeldung.persistence.criteria.repository; +package org.baeldung.persistence.dao; -import org.baeldung.persistence.criteria.model.Book; +import org.baeldung.persistence.model.Book; import org.springframework.data.jpa.domain.Specification; public class BookSpecifications { diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/criteria/model/Book.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/model/Book.java similarity index 91% rename from persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/criteria/model/Book.java rename to persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/model/Book.java index beb6c0190c..754bd179d1 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/criteria/model/Book.java +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/persistence/model/Book.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.criteria.model; +package org.baeldung.persistence.model; import javax.persistence.Entity; import javax.persistence.Id; diff --git a/persistence-modules/spring-jpa/src/main/resources/persistence.xml b/persistence-modules/spring-jpa/src/main/resources/persistence.xml index 65bad29cdc..6304fa0a65 100644 --- a/persistence-modules/spring-jpa/src/main/resources/persistence.xml +++ b/persistence-modules/spring-jpa/src/main/resources/persistence.xml @@ -21,8 +21,6 @@ ${hibernate.hbm2ddl.auto} ${hibernate.dialect} - ${hibernate.cache.use_second_level_cache} - ${hibernate.cache.use_query_cache} diff --git a/pom.xml b/pom.xml index 7088d88cd8..3cacc70da2 100644 --- a/pom.xml +++ b/pom.xml @@ -352,6 +352,7 @@ java-streams core-java-persistence core-kotlin + kotlin-libraries core-groovy core-java-concurrency core-java-concurrency-collections @@ -360,6 +361,7 @@ dozer ethereum ejb + ejb/ejb-client feign flips testing-modules/gatling @@ -403,9 +405,10 @@ json jsoup testing-modules/junit-5 - jws + libraries libraries-data + libraries-security libraries-server linkrest logging-modules/log-mdc @@ -444,6 +447,7 @@ spark-java spring-4 spring-5 + spring-5-data-reactive spring-5-reactive spring-5-reactive-security spring-5-reactive-client @@ -462,11 +466,12 @@ spring-boot-keycloak spring-boot-bootstrap spring-boot-admin + spring-boot-camel spring-boot-ops spring-boot-persistence spring-boot-security spring-boot-mvc - spring-boot-vue + spring-boot-vue spring-boot-logging-log4j2 spring-cloud-data-flow spring-cloud @@ -552,6 +557,7 @@ spring-thymeleaf spring-userservice spring-zuul + spring-remoting spring-reactor spring-vertx spring-jinq @@ -575,6 +581,7 @@ testing-modules/mockserver testing-modules/test-containers undertow + vaadin vertx-and-rxjava saas deeplearning4j @@ -598,6 +605,81 @@ jnosql spring-boot-angular-ecommerce cdi-portable-extension + jta + + java-websocket + activejdbc + + + apache-bval + apache-shiro + apache-spark + + checker-plugin + + + core-java-sun + custom-pmd + dagger + data-structures + dubbo + + + + + jni + jooby + + + + ratpack + + spring-boot-autoconfiguration + spring-boot-custom-starter + spring-boot-jasypt + + spring-data-rest-querydsl + + spring-mobile + + spring-mvc-simple + + spring-rest-hal-browser + spring-rest-shell + spring-rest-template + spring-roo + spring-security-stormpath + sse-jaxrs + + stripe + + Twitter4J + wicket + xstream + cas/cas-secured-app + cas/cas-server + + + + + + + + + + + + + jenkins/hello-world + + + + spring-boot-custom-starter/greeter + spring-boot-h2/spring-boot-h2-database + + + + @@ -708,6 +790,7 @@ spark-java spring-4 spring-5 + spring-5-data-reactive spring-5-reactive spring-5-reactive-security spring-5-reactive-client @@ -732,6 +815,7 @@ spring-boot-keycloak spring-boot-bootstrap spring-boot-admin + spring-boot-camel spring-boot-ops spring-boot-persistence spring-boot-security @@ -839,7 +923,80 @@ - + + java-websocket + + + + apache-bval + apache-shiro + apache-spark + + checker-plugin + + + core-java-sun + custom-pmd + dagger + data-structures + dubbo + + + + + jni + jooby + + + + ratpack + + spring-boot-autoconfiguration + spring-boot-custom-starter + + + spring-data-rest-querydsl + + spring-mobile + + spring-mvc-simple + + spring-rest-hal-browser + spring-rest-shell + spring-rest-template + spring-roo + spring-security-stormpath + sse-jaxrs + + stripe + + + wicket + xstream + cas/cas-secured-app + + + + + + + + + + + + + + jenkins/hello-world + + + + spring-boot-custom-starter/greeter + spring-boot-h2/spring-boot-h2-database + + + + @@ -955,6 +1112,7 @@ json-path json jsoup + jta testing-modules/junit-5 testing-modules/junit5-migration jws @@ -995,6 +1153,7 @@ persistence-modules/solr spark-java spring-4 + spring-5-data-reactive spring-5-reactive spring-5-reactive-security spring-5-reactive-client @@ -1011,6 +1170,7 @@ spring-boot-keycloak spring-boot-bootstrap spring-boot-admin + spring-boot-camel spring-boot-persistence spring-boot-security spring-boot-mvc @@ -1095,8 +1255,10 @@ spring-thymeleaf spring-userservice spring-zuul + spring-remoting spring-reactor spring-vertx + spring-vault spring-jinq spring-rest-embedded-tomcat testing-modules/testing @@ -1118,6 +1280,7 @@ testing-modules/mockserver testing-modules/test-containers undertow + vaadin vertx-and-rxjava saas deeplearning4j @@ -1227,6 +1390,7 @@ spring-boot-ops spring-5 core-kotlin + kotlin-libraries core-java google-web-toolkit spring-security-mvc-custom @@ -1298,4 +1462,4 @@ 3.8 - \ No newline at end of file + diff --git a/ratpack/build.gradle b/ratpack/build.gradle index aeddd5f9f9..25af3ddb51 100644 --- a/ratpack/build.gradle +++ b/ratpack/build.gradle @@ -1,35 +1,35 @@ -buildscript { - repositories { - jcenter() - } - dependencies { - classpath "io.ratpack:ratpack-gradle:1.4.5" - classpath "com.h2database:h2:1.4.193" - } -} - -if (!JavaVersion.current().java8Compatible) { - throw new IllegalStateException("Must be built with Java 8 or higher") -} - -apply plugin: "io.ratpack.ratpack-java" -apply plugin: 'java' - -repositories { - jcenter() -} - -dependencies { - compile ratpack.dependency('hikari') - compile 'com.h2database:h2:1.4.193' - testCompile 'junit:junit:4.11' - runtime "org.slf4j:slf4j-simple:1.7.21" -} - -test { - testLogging { - events 'started', 'passed' - } -} - -mainClassName = "com.baeldung.Application" +buildscript { + repositories { + jcenter() + } + dependencies { + classpath "io.ratpack:ratpack-gradle:1.5.4" + classpath "com.h2database:h2:1.4.193" + } +} + +if (!JavaVersion.current().java8Compatible) { + throw new IllegalStateException("Must be built with Java 8 or higher") +} + +apply plugin: "io.ratpack.ratpack-java" +apply plugin: 'java' + +repositories { + jcenter() +} + +dependencies { + compile ratpack.dependency('hikari') + compile 'com.h2database:h2:1.4.193' + testCompile 'junit:junit:4.11' + runtime "org.slf4j:slf4j-simple:1.7.21" +} + +test { + testLogging { + events 'started', 'passed' + } +} + +mainClassName = "com.baeldung.Application" diff --git a/ratpack/pom.xml b/ratpack/pom.xml index bbde3d1cd7..7de11e6955 100644 --- a/ratpack/pom.xml +++ b/ratpack/pom.xml @@ -35,6 +35,17 @@ io.ratpack ratpack-hystrix ${ratpack.version} + + + com.netflix.hystrix + hystrix-core + + + + + com.netflix.hystrix + hystrix-core + ${hystrix.version} io.ratpack @@ -76,10 +87,11 @@ 1.8 1.8 - 1.4.6 + 1.5.4 4.5.3 4.4.6 1.4.193 + 1.5.12 diff --git a/ratpack/src/main/java/com/baeldung/Application.java b/ratpack/src/main/java/com/baeldung/Application.java index 7f46b241ea..4a391d04a1 100644 --- a/ratpack/src/main/java/com/baeldung/Application.java +++ b/ratpack/src/main/java/com/baeldung/Application.java @@ -1,49 +1,81 @@ -package com.baeldung; - -import java.util.ArrayList; -import java.util.List; - -import com.baeldung.filter.RequestValidatorFilter; -import com.baeldung.model.Employee; - -import ratpack.guice.Guice; -import ratpack.hikari.HikariModule; -import ratpack.http.MutableHeaders; -import ratpack.jackson.Jackson; -import ratpack.http.MutableHeaders; -import ratpack.server.RatpackServer; - -public class Application { - - public static void main(String[] args) throws Exception { - - List employees = new ArrayList(); - employees.add(new Employee(1L, "Mr", "John Doe")); - employees.add(new Employee(2L, "Mr", "White Snow")); - - - RatpackServer.start( - server -> server.registry(Guice.registry(bindings -> bindings.module(HikariModule.class, config -> { - config.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource"); - config.addDataSourceProperty("URL", - "jdbc:h2:mem:baeldung;INIT=RUNSCRIPT FROM 'classpath:/DDL.sql'"); - }))).handlers(chain -> chain - .all( - // ctx -> { - // MutableHeaders headers = - // ctx.getResponse().getHeaders(); - // headers.set("Access-Control-Allow-Origin","*"); - // headers.set("Accept-Language", "en-us"); - // headers.set("Accept-Charset", "UTF-8"); - // ctx.next(); - // } - new RequestValidatorFilter()) - .get(ctx -> ctx.render("Welcome to baeldung ratpack!!!")) - .get("data/employees", ctx -> ctx.render(Jackson.json(employees))) - .get(":name", ctx -> ctx.render("Hello " + ctx.getPathTokens().get("name") + "!!!")) - .post(":amount", ctx -> ctx - .render(" Amount $" + ctx.getPathTokens().get("amount") + " added successfully !!!")))); - } - -} - +package com.baeldung; + +import com.baeldung.filter.RequestValidatorFilter; +import com.baeldung.handler.EmployeeHandler; +import com.baeldung.handler.RedirectHandler; +import com.baeldung.model.Employee; +import com.baeldung.repository.EmployeeRepository; +import com.baeldung.repository.EmployeeRepositoryImpl; +import com.zaxxer.hikari.HikariConfig; +import io.netty.buffer.PooledByteBufAllocator; +import ratpack.func.Action; +import ratpack.func.Function; +import ratpack.guice.BindingsSpec; +import ratpack.guice.Guice; +import ratpack.handling.Chain; +import ratpack.hikari.HikariModule; +import ratpack.http.client.HttpClient; +import ratpack.jackson.Jackson; +import ratpack.registry.Registry; +import ratpack.server.RatpackServer; +import ratpack.server.RatpackServerSpec; +import ratpack.server.ServerConfig; + +import java.time.Duration; +import java.time.temporal.ChronoUnit; +import java.util.ArrayList; +import java.util.List; + +public class Application { + + public static void main(String[] args) throws Exception { + + final Action hikariConfigAction = hikariConfig -> { + hikariConfig.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource"); + hikariConfig.addDataSourceProperty("URL", "jdbc:h2:mem:baeldung;INIT=RUNSCRIPT FROM 'classpath:/DDL.sql'"); + }; + + final Action bindingsSpecAction = bindings -> bindings.module(HikariModule.class, hikariConfigAction); + final HttpClient httpClient = HttpClient.of(httpClientSpec -> { + httpClientSpec.poolSize(10) + .connectTimeout(Duration.of(60, ChronoUnit.SECONDS)) + .maxContentLength(ServerConfig.DEFAULT_MAX_CONTENT_LENGTH) + .responseMaxChunkSize(16384) + .readTimeout(Duration.of(60, ChronoUnit.SECONDS)) + .byteBufAllocator(PooledByteBufAllocator.DEFAULT); + }); + final Function registryFunction = Guice.registry(bindingsSpecAction); + + final Action chainAction = chain -> chain.all(new RequestValidatorFilter()) + .get(ctx -> ctx.render("Welcome to baeldung ratpack!!!")) + .get("data/employees", ctx -> ctx.render(Jackson.json(createEmpList()))) + .get(":name", ctx -> ctx.render("Hello " + ctx.getPathTokens() + .get("name") + "!!!")) + .post(":amount", ctx -> ctx.render(" Amount $" + ctx.getPathTokens() + .get("amount") + " added successfully !!!")); + + final Action routerChainAction = routerChain -> { + routerChain.path("redirect", new RedirectHandler()) + .prefix("employee", empChain -> { + empChain.get(":id", new EmployeeHandler()); + }); + }; + final Action ratpackServerSpecAction = serverSpec -> serverSpec.registry(registryFunction) + .registryOf(registrySpec -> { + registrySpec.add(EmployeeRepository.class, new EmployeeRepositoryImpl()); + registrySpec.add(HttpClient.class, httpClient); + }) + .handlers(chain -> chain.insert(routerChainAction) + .insert(chainAction)); + + RatpackServer.start(ratpackServerSpecAction); + } + + private static List createEmpList() { + List employees = new ArrayList<>(); + employees.add(new Employee(1L, "Mr", "John Doe")); + employees.add(new Employee(2L, "Mr", "White Snow")); + return employees; + } + +} diff --git a/ratpack/src/main/java/com/baeldung/handler/EmployeeHandler.java b/ratpack/src/main/java/com/baeldung/handler/EmployeeHandler.java new file mode 100644 index 0000000000..9a6b79259c --- /dev/null +++ b/ratpack/src/main/java/com/baeldung/handler/EmployeeHandler.java @@ -0,0 +1,20 @@ +package com.baeldung.handler; + +import com.baeldung.repository.EmployeeRepository; +import com.baeldung.model.Employee; +import ratpack.exec.Promise; +import ratpack.handling.Context; +import ratpack.handling.Handler; + +public class EmployeeHandler implements Handler { + @Override + public void handle(Context ctx) throws Exception { + EmployeeRepository repository = ctx.get(EmployeeRepository.class); + Long id = Long.valueOf(ctx.getPathTokens() + .get("id")); + Promise employeePromise = repository.findEmployeeById(id); + employeePromise.map(employee -> employee.getName()) + .then(name -> ctx.getResponse() + .send(name)); + } +} diff --git a/ratpack/src/main/java/com/baeldung/handler/FooHandler.java b/ratpack/src/main/java/com/baeldung/handler/FooHandler.java new file mode 100644 index 0000000000..d2c4dbdce5 --- /dev/null +++ b/ratpack/src/main/java/com/baeldung/handler/FooHandler.java @@ -0,0 +1,12 @@ +package com.baeldung.handler; + +import ratpack.handling.Context; +import ratpack.handling.Handler; + +public class FooHandler implements Handler { + @Override + public void handle(Context ctx) throws Exception { + ctx.getResponse() + .send("Hello Foo!"); + } +} diff --git a/ratpack/src/main/java/com/baeldung/handler/RedirectHandler.java b/ratpack/src/main/java/com/baeldung/handler/RedirectHandler.java new file mode 100644 index 0000000000..c95543a961 --- /dev/null +++ b/ratpack/src/main/java/com/baeldung/handler/RedirectHandler.java @@ -0,0 +1,23 @@ +package com.baeldung.handler; + +import ratpack.exec.Promise; +import ratpack.handling.Context; +import ratpack.handling.Handler; +import ratpack.http.client.HttpClient; +import ratpack.http.client.ReceivedResponse; + +import java.net.URI; + +public class RedirectHandler implements Handler { + @Override + public void handle(Context ctx) throws Exception { + HttpClient client = ctx.get(HttpClient.class); + URI uri = URI.create("http://localhost:5050/employee/1"); + Promise responsePromise = client.get(uri); + responsePromise.map(response -> response.getBody() + .getText() + .toUpperCase()) + .then(responseText -> ctx.getResponse() + .send(responseText)); + } +} \ No newline at end of file diff --git a/ratpack/src/main/java/com/baeldung/repository/EmployeeRepository.java b/ratpack/src/main/java/com/baeldung/repository/EmployeeRepository.java new file mode 100644 index 0000000000..de04c23da5 --- /dev/null +++ b/ratpack/src/main/java/com/baeldung/repository/EmployeeRepository.java @@ -0,0 +1,10 @@ +package com.baeldung.repository; + +import com.baeldung.model.Employee; +import ratpack.exec.Promise; + +public interface EmployeeRepository { + + Promise findEmployeeById(Long id) throws Exception; + +} diff --git a/ratpack/src/main/java/com/baeldung/repository/EmployeeRepositoryImpl.java b/ratpack/src/main/java/com/baeldung/repository/EmployeeRepositoryImpl.java new file mode 100644 index 0000000000..6d15012ccb --- /dev/null +++ b/ratpack/src/main/java/com/baeldung/repository/EmployeeRepositoryImpl.java @@ -0,0 +1,25 @@ +package com.baeldung.repository; + +import com.baeldung.model.Employee; +import ratpack.exec.Promise; + +import java.util.HashMap; +import java.util.Map; + +public class EmployeeRepositoryImpl implements EmployeeRepository { + + private static final Map EMPLOYEE_MAP = new HashMap<>(); + + public EmployeeRepositoryImpl() { + EMPLOYEE_MAP.put(1L, new Employee(1L, "Ms", "Jane Doe")); + EMPLOYEE_MAP.put(2L, new Employee(2L, "Mr", "NY")); + } + + @Override + public Promise findEmployeeById(Long id) throws Exception { + return Promise.async(downstream -> { + Thread.sleep(500); + downstream.success(EMPLOYEE_MAP.get(id)); + }); + } +} diff --git a/ratpack/src/test/java/com/baeldung/AppHttpUnitTest.java b/ratpack/src/test/java/com/baeldung/AppHttpUnitTest.java new file mode 100644 index 0000000000..a97516dd74 --- /dev/null +++ b/ratpack/src/test/java/com/baeldung/AppHttpUnitTest.java @@ -0,0 +1,58 @@ +package com.baeldung; + +import com.baeldung.model.Employee; +import org.junit.Test; +import ratpack.exec.Promise; +import ratpack.func.Action; +import ratpack.handling.Chain; +import ratpack.handling.Handler; +import ratpack.registry.Registry; +import ratpack.test.embed.EmbeddedApp; +import ratpack.test.exec.ExecHarness; + +import static org.junit.Assert.assertEquals; + +public class AppHttpUnitTest { + + @Test + public void givenAnyUri_GetEmployeeFromSameRegistry() throws Exception { + Handler allHandler = ctx -> { + Long id = Long.valueOf(ctx.getPathTokens() + .get("id")); + Employee employee = new Employee(id, "Mr", "NY"); + ctx.next(Registry.single(Employee.class, employee)); + }; + Handler empNameHandler = ctx -> { + Employee employee = ctx.get(Employee.class); + ctx.getResponse() + .send("Name of employee with ID " + employee.getId() + " is " + employee.getName()); + }; + Handler empTitleHandler = ctx -> { + Employee employee = ctx.get(Employee.class); + ctx.getResponse() + .send("Title of employee with ID " + employee.getId() + " is " + employee.getTitle()); + }; + + Action chainAction = chain -> chain.prefix("employee/:id", empChain -> { + empChain.all(allHandler) + .get("name", empNameHandler) + .get("title", empTitleHandler); + }); + EmbeddedApp.fromHandlers(chainAction) + .test(testHttpClient -> { + assertEquals("Name of employee with ID 1 is NY", testHttpClient.get("employee/1/name") + .getBody() + .getText()); + assertEquals("Title of employee with ID 1 is Mr", testHttpClient.get("employee/1/title") + .getBody() + .getText()); + }); + } + + @Test + public void givenSyncDataSource_GetDataFromPromise() throws Exception { + String value = ExecHarness.yieldSingle(execution -> Promise.sync(() -> "Foo")) + .getValueOrThrow(); + assertEquals("Foo", value); + } +} diff --git a/reactor-core/src/test/java/com/baeldung/reactor/ReactorIntegrationTest.java b/reactor-core/src/test/java/com/baeldung/reactor/ReactorIntegrationTest.java index 56650ba306..e3060b8e02 100644 --- a/reactor-core/src/test/java/com/baeldung/reactor/ReactorIntegrationTest.java +++ b/reactor-core/src/test/java/com/baeldung/reactor/ReactorIntegrationTest.java @@ -37,14 +37,14 @@ public class ReactorIntegrationTest { Flux.just(1, 2, 3, 4) .log() .map(i -> i * 2) - .zipWith(Flux.range(0, Integer.MAX_VALUE).log(), (two, one) -> String.format("First Flux: %d, Second Flux: %d", one, two)) + .zipWith(Flux.range(0, Integer.MAX_VALUE).log(), (one, two) -> String.format("First Flux: %d, Second Flux: %d", one, two)) .subscribe(elements::add); assertThat(elements).containsExactly( - "First Flux: 0, Second Flux: 2", - "First Flux: 1, Second Flux: 4", - "First Flux: 2, Second Flux: 6", - "First Flux: 3, Second Flux: 8"); + "First Flux: 2, Second Flux: 0", + "First Flux: 4, Second Flux: 1", + "First Flux: 6, Second Flux: 2", + "First Flux: 8, Second Flux: 3"); } @Test diff --git a/resteasy/pom.xml b/resteasy/pom.xml index 867c1f4c1b..31a6ed485a 100644 --- a/resteasy/pom.xml +++ b/resteasy/pom.xml @@ -3,9 +3,10 @@ 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 - resteasy-tutorial + resteasy 1.0 war + resteasy com.baeldung diff --git a/rxjava/README.md b/rxjava/README.md index 76135797a6..f8a4ca1dbe 100644 --- a/rxjava/README.md +++ b/rxjava/README.md @@ -12,3 +12,4 @@ - [RxJava StringObservable](http://www.baeldung.com/rxjava-string) - [Filtering Observables in RxJava](http://www.baeldung.com/rxjava-filtering) - [RxJava One Observable, Multiple Subscribers](http://www.baeldung.com/rxjava-multiple-subscribers-observable) +- [Difference Between Flatmap and Switchmap in RxJava](https://www.baeldung.com/rxjava-flatmap-switchmap) diff --git a/spring-4/README.md b/spring-4/README.md index 9855a9254d..4600a603ef 100644 --- a/spring-4/README.md +++ b/spring-4/README.md @@ -1,3 +1,4 @@ ### Relevant Articles: - [Guide to Flips For Spring](http://www.baeldung.com/guide-to-flips-for-spring/) - [A Guide to Flips for Spring](http://www.baeldung.com/flips-spring) +- [Configuring a Hikari Connection Pool with Spring Boot](https://www.baeldung.com/spring-boot-hikari) diff --git a/spring-4/pom.xml b/spring-4/pom.xml index cf10f64aa2..78939bba95 100644 --- a/spring-4/pom.xml +++ b/spring-4/pom.xml @@ -71,10 +71,8 @@ - UTF-8 1.0.1 1.16.18 - 1.8 1.4.197 diff --git a/spring-data-5-reactive/README.md b/spring-5-data-reactive/README.md similarity index 100% rename from spring-data-5-reactive/README.md rename to spring-5-data-reactive/README.md diff --git a/spring-data-5-reactive/pom.xml b/spring-5-data-reactive/pom.xml similarity index 96% rename from spring-data-5-reactive/pom.xml rename to spring-5-data-reactive/pom.xml index 36ace53da2..e4dd454635 100644 --- a/spring-data-5-reactive/pom.xml +++ b/spring-5-data-reactive/pom.xml @@ -2,9 +2,7 @@ 4.0.0 - com.baeldung spring-5-data-reactive - 0.0.1-SNAPSHOT jar @@ -44,11 +42,6 @@ reactor-test test - - org.jetbrains.kotlin - kotlin-stdlib-jdk8 - ${kotlin.version} - org.jetbrains.kotlin kotlin-test @@ -101,6 +94,7 @@ kotlin-maven-plugin + ${kotlin-maven-plugin.version} compile @@ -204,6 +198,7 @@ UTF-8 1.8 1.2.40 + 1.2.40 1.0.0 5.0.2 diff --git a/spring-data-5-reactive/src/main/java/com/baeldung/reactive/Spring5ReactiveApplication.java b/spring-5-data-reactive/src/main/java/com/baeldung/reactive/Spring5ReactiveApplication.java similarity index 100% rename from spring-data-5-reactive/src/main/java/com/baeldung/reactive/Spring5ReactiveApplication.java rename to spring-5-data-reactive/src/main/java/com/baeldung/reactive/Spring5ReactiveApplication.java diff --git a/spring-data-5-reactive/src/main/java/com/baeldung/reactive/model/Account.java b/spring-5-data-reactive/src/main/java/com/baeldung/reactive/model/Account.java similarity index 100% rename from spring-data-5-reactive/src/main/java/com/baeldung/reactive/model/Account.java rename to spring-5-data-reactive/src/main/java/com/baeldung/reactive/model/Account.java diff --git a/spring-data-5-reactive/src/main/java/com/baeldung/reactive/repository/AccountCrudRepository.java b/spring-5-data-reactive/src/main/java/com/baeldung/reactive/repository/AccountCrudRepository.java similarity index 100% rename from spring-data-5-reactive/src/main/java/com/baeldung/reactive/repository/AccountCrudRepository.java rename to spring-5-data-reactive/src/main/java/com/baeldung/reactive/repository/AccountCrudRepository.java diff --git a/spring-data-5-reactive/src/main/java/com/baeldung/reactive/repository/AccountMongoRepository.java b/spring-5-data-reactive/src/main/java/com/baeldung/reactive/repository/AccountMongoRepository.java similarity index 100% rename from spring-data-5-reactive/src/main/java/com/baeldung/reactive/repository/AccountMongoRepository.java rename to spring-5-data-reactive/src/main/java/com/baeldung/reactive/repository/AccountMongoRepository.java diff --git a/spring-data-5-reactive/src/main/java/com/baeldung/reactive/repository/AccountRxJavaRepository.java b/spring-5-data-reactive/src/main/java/com/baeldung/reactive/repository/AccountRxJavaRepository.java similarity index 100% rename from spring-data-5-reactive/src/main/java/com/baeldung/reactive/repository/AccountRxJavaRepository.java rename to spring-5-data-reactive/src/main/java/com/baeldung/reactive/repository/AccountRxJavaRepository.java diff --git a/spring-data-5-reactive/src/main/java/com/baeldung/reactive/template/AccountTemplateOperations.java b/spring-5-data-reactive/src/main/java/com/baeldung/reactive/template/AccountTemplateOperations.java similarity index 100% rename from spring-data-5-reactive/src/main/java/com/baeldung/reactive/template/AccountTemplateOperations.java rename to spring-5-data-reactive/src/main/java/com/baeldung/reactive/template/AccountTemplateOperations.java diff --git a/spring-data-5-reactive/src/main/kotlin/com/baeldung/Application.kt b/spring-5-data-reactive/src/main/kotlin/com/baeldung/Application.kt similarity index 100% rename from spring-data-5-reactive/src/main/kotlin/com/baeldung/Application.kt rename to spring-5-data-reactive/src/main/kotlin/com/baeldung/Application.kt diff --git a/spring-data-5-reactive/src/main/kotlin/com/baeldung/Event.kt b/spring-5-data-reactive/src/main/kotlin/com/baeldung/Event.kt similarity index 100% rename from spring-data-5-reactive/src/main/kotlin/com/baeldung/Event.kt rename to spring-5-data-reactive/src/main/kotlin/com/baeldung/Event.kt diff --git a/spring-data-5-reactive/src/main/kotlin/com/baeldung/EventRepository.kt b/spring-5-data-reactive/src/main/kotlin/com/baeldung/EventRepository.kt similarity index 100% rename from spring-data-5-reactive/src/main/kotlin/com/baeldung/EventRepository.kt rename to spring-5-data-reactive/src/main/kotlin/com/baeldung/EventRepository.kt diff --git a/spring-data-5-reactive/src/main/kotlin/com/baeldung/MongoConfig.kt b/spring-5-data-reactive/src/main/kotlin/com/baeldung/MongoConfig.kt similarity index 100% rename from spring-data-5-reactive/src/main/kotlin/com/baeldung/MongoConfig.kt rename to spring-5-data-reactive/src/main/kotlin/com/baeldung/MongoConfig.kt diff --git a/spring-data-5-reactive/src/main/kotlin/com/baeldung/SendEmitter.kt b/spring-5-data-reactive/src/main/kotlin/com/baeldung/SendEmitter.kt similarity index 100% rename from spring-data-5-reactive/src/main/kotlin/com/baeldung/SendEmitter.kt rename to spring-5-data-reactive/src/main/kotlin/com/baeldung/SendEmitter.kt diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/resources/logback.xml b/spring-5-data-reactive/src/main/resources/logback.xml similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/resources/logback.xml rename to spring-5-data-reactive/src/main/resources/logback.xml diff --git a/spring-data-5-reactive/src/main/resources/static/index.html b/spring-5-data-reactive/src/main/resources/static/index.html similarity index 100% rename from spring-data-5-reactive/src/main/resources/static/index.html rename to spring-5-data-reactive/src/main/resources/static/index.html diff --git a/spring-data-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountCrudRepositoryIntegrationTest.java b/spring-5-data-reactive/src/test/java/com/baeldung/reactive/repository/AccountCrudRepositoryIntegrationTest.java similarity index 100% rename from spring-data-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountCrudRepositoryIntegrationTest.java rename to spring-5-data-reactive/src/test/java/com/baeldung/reactive/repository/AccountCrudRepositoryIntegrationTest.java diff --git a/spring-data-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountMongoRepositoryIntegrationTest.java b/spring-5-data-reactive/src/test/java/com/baeldung/reactive/repository/AccountMongoRepositoryIntegrationTest.java similarity index 100% rename from spring-data-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountMongoRepositoryIntegrationTest.java rename to spring-5-data-reactive/src/test/java/com/baeldung/reactive/repository/AccountMongoRepositoryIntegrationTest.java diff --git a/spring-data-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountRxJavaRepositoryIntegrationTest.java b/spring-5-data-reactive/src/test/java/com/baeldung/reactive/repository/AccountRxJavaRepositoryIntegrationTest.java similarity index 100% rename from spring-data-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountRxJavaRepositoryIntegrationTest.java rename to spring-5-data-reactive/src/test/java/com/baeldung/reactive/repository/AccountRxJavaRepositoryIntegrationTest.java diff --git a/spring-data-5-reactive/src/test/java/com/baeldung/reactive/template/AccountTemplateOperationsIntegrationTest.java b/spring-5-data-reactive/src/test/java/com/baeldung/reactive/template/AccountTemplateOperationsIntegrationTest.java similarity index 100% rename from spring-data-5-reactive/src/test/java/com/baeldung/reactive/template/AccountTemplateOperationsIntegrationTest.java rename to spring-5-data-reactive/src/test/java/com/baeldung/reactive/template/AccountTemplateOperationsIntegrationTest.java diff --git a/spring-5-mvc/README.md b/spring-5-mvc/README.md index 71b7a19b43..7e83077f54 100644 --- a/spring-5-mvc/README.md +++ b/spring-5-mvc/README.md @@ -1,3 +1,4 @@ ### Relevant Articles: - [Spring Boot and Kotlin](http://www.baeldung.com/spring-boot-kotlin) -- [Spring MVC Streaming and SSE Request Processing](http://www.baeldung.com/spring-mvc-sse-streams) +- [Spring MVC Streaming and SSE Request Processing](https://www.baeldung.com/spring-mvc-sse-streams) +- [Overview and Need for DelegatingFilterProxy in Spring](https://www.baeldung.com/spring-delegating-filter-proxy) diff --git a/spring-5-mvc/src/main/java/com/baeldung/web/SseEmitterController.java b/spring-5-mvc/src/main/java/com/baeldung/web/SseEmitterController.java index b11c93fb08..00113c5ff7 100644 --- a/spring-5-mvc/src/main/java/com/baeldung/web/SseEmitterController.java +++ b/spring-5-mvc/src/main/java/com/baeldung/web/SseEmitterController.java @@ -1,12 +1,16 @@ package com.baeldung.web; -import com.baeldung.Constants; +import java.time.LocalTime; import java.util.Date; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; + import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; +import org.springframework.web.servlet.mvc.method.annotation.SseEmitter.SseEventBuilder; + +import com.baeldung.Constants; @Controller public class SseEmitterController { @@ -29,4 +33,27 @@ public class SseEmitterController { return emitter; } + @GetMapping("/stream-sse-mvc") + public SseEmitter streamSseMvc() { + SseEmitter emitter = new SseEmitter(); + ExecutorService sseMvcExecutor = Executors.newSingleThreadExecutor(); + + sseMvcExecutor.execute(() -> { + try { + for (int i = 0; true; i++) { + SseEventBuilder event = SseEmitter.event() + .data("SSE MVC - " + LocalTime.now() + .toString()) + .id(String.valueOf(i)) + .name("sse event - mvc"); + emitter.send(event); + Thread.sleep(1000); + } + } catch (Exception ex) { + emitter.completeWithError(ex); + } + }); + return emitter; + } + } diff --git a/spring-5-reactive-client/pom.xml b/spring-5-reactive-client/pom.xml index f2f7dd1729..6e39743ed0 100644 --- a/spring-5-reactive-client/pom.xml +++ b/spring-5-reactive-client/pom.xml @@ -3,9 +3,7 @@ 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-5-reactive-client - 0.0.1-SNAPSHOT jar spring-5-reactive-client spring 5 sample project about new features @@ -92,11 +90,6 @@ org.projectlombok lombok - - org.apache.commons - commons-lang3 - - diff --git a/spring-5-reactive-security/src/main/java/com/baeldung/reactive/functional/EmployeeFunctionalConfig.java b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/functional/EmployeeFunctionalConfig.java new file mode 100644 index 0000000000..8d7860ccdc --- /dev/null +++ b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/functional/EmployeeFunctionalConfig.java @@ -0,0 +1,75 @@ +package com.baeldung.reactive.functional; + +import static org.springframework.web.reactive.function.BodyExtractors.toMono; +import static org.springframework.web.reactive.function.server.RequestPredicates.GET; +import static org.springframework.web.reactive.function.server.RequestPredicates.POST; +import static org.springframework.web.reactive.function.server.RouterFunctions.route; +import static org.springframework.web.reactive.function.server.ServerResponse.ok; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.web.server.ServerHttpSecurity; +import org.springframework.security.web.server.SecurityWebFilterChain; +import org.springframework.web.reactive.function.server.RouterFunction; +import org.springframework.web.reactive.function.server.ServerResponse; + +import com.baeldung.webflux.Employee; +import com.baeldung.webflux.EmployeeRepository; + +@Configuration +public class EmployeeFunctionalConfig { + + @Bean + EmployeeRepository employeeRepository() { + return new EmployeeRepository(); + } + + @Bean + RouterFunction getAllEmployeesRoute() { + return route(GET("/employees"), + req -> ok().body( + employeeRepository().findAllEmployees(), Employee.class)); + } + + @Bean + RouterFunction getEmployeeByIdRoute() { + return route(GET("/employees/{id}"), + req -> ok().body( + employeeRepository().findEmployeeById(req.pathVariable("id")), Employee.class)); + } + + @Bean + RouterFunction updateEmployee() { + return route(POST("/employees/update"), + req -> req.body(toMono(Employee.class)) + .doOnNext(employeeRepository()::updateEmployee) + .then(ok().build())); + } + + @Bean + RouterFunction composedRoutes() { + return + route(GET("/employees"), + req -> ok().body( + employeeRepository().findAllEmployees(), Employee.class)) + + .and(route(GET("/employees/{id}"), + req -> ok().body( + employeeRepository().findEmployeeById(req.pathVariable("id")), Employee.class))) + + .and(route(POST("/employees/update"), + req -> req.body(toMono(Employee.class)) + .doOnNext(employeeRepository()::updateEmployee) + .then(ok().build()))); + } + + @Bean + public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { + http.csrf() + .disable() + .authorizeExchange() + .anyExchange() + .permitAll(); + return http.build(); + } +} diff --git a/spring-5-reactive-security/src/main/java/com/baeldung/reactive/functional/EmployeeSpringFunctionalApplication.java b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/functional/EmployeeSpringFunctionalApplication.java new file mode 100644 index 0000000000..1f2bd871fc --- /dev/null +++ b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/functional/EmployeeSpringFunctionalApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.reactive.functional; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class EmployeeSpringFunctionalApplication { + + public static void main(String[] args) { + SpringApplication.run(EmployeeSpringFunctionalApplication.class, args); + } + +} diff --git a/spring-5-reactive-security/src/test/java/com/baeldung/reactive/functional/EmployeeSpringFunctionalIntegrationTest.java b/spring-5-reactive-security/src/test/java/com/baeldung/reactive/functional/EmployeeSpringFunctionalIntegrationTest.java new file mode 100644 index 0000000000..1197820066 --- /dev/null +++ b/spring-5-reactive-security/src/test/java/com/baeldung/reactive/functional/EmployeeSpringFunctionalIntegrationTest.java @@ -0,0 +1,28 @@ +package com.baeldung.reactive.functional; + +import org.junit.Test; +import org.springframework.test.web.reactive.server.WebTestClient; + +import com.baeldung.webflux.Employee; + +public class EmployeeSpringFunctionalIntegrationTest { + + private static EmployeeFunctionalConfig config = new EmployeeFunctionalConfig(); + + @Test + public void givenEmployeeId_whenGetEmployeeById_thenCorrectEmployee() { + WebTestClient client = WebTestClient + .bindToRouterFunction(config.getEmployeeByIdRoute()) + .build(); + + Employee expected = new Employee("1", "Employee 1"); + + client.get() + .uri("/employees/1") + .exchange() + .expectStatus() + .isOk() + .expectBody(Employee.class) + .isEqualTo(expected); + } +} diff --git a/spring-5-reactive/pom.xml b/spring-5-reactive/pom.xml index e81d3d8b79..5f455c3906 100644 --- a/spring-5-reactive/pom.xml +++ b/spring-5-reactive/pom.xml @@ -94,6 +94,12 @@ ${project-reactor-test} test + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + @@ -117,6 +123,7 @@ 1.0 4.1 3.1.6.RELEASE + 1.2.0 diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/serversentevents/consumer/ConsumerSSEApplication.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/serversentevents/consumer/ConsumerSSEApplication.java new file mode 100644 index 0000000000..3997607ef0 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/serversentevents/consumer/ConsumerSSEApplication.java @@ -0,0 +1,19 @@ +package com.baeldung.reactive.serversentevents.consumer; + +import java.util.Collections; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableAsync; + +@SpringBootApplication +@EnableAsync +public class ConsumerSSEApplication { + + public static void main(String[] args) { + SpringApplication app = new SpringApplication(ConsumerSSEApplication.class); + app.setDefaultProperties(Collections.singletonMap("server.port", "8082")); + app.run(args); + } + +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/serversentevents/consumer/controller/ClientController.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/serversentevents/consumer/controller/ClientController.java new file mode 100644 index 0000000000..69a6bc396c --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/serversentevents/consumer/controller/ClientController.java @@ -0,0 +1,83 @@ +package com.baeldung.reactive.serversentevents.consumer.controller; + +import java.time.LocalTime; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.MediaType; +import org.springframework.http.codec.ServerSentEvent; +import org.springframework.scheduling.annotation.Async; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.reactive.function.client.WebClient; + +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +@RestController +@RequestMapping("/sse-consumer") +public class ClientController { + + private static Logger logger = LoggerFactory.getLogger(ClientController.class); + private WebClient client = WebClient.create("http://localhost:8081/sse-server"); + + @GetMapping("/launch-sse-client") + public String launchSSEFromSSEWebClient() { + consumeSSE(); + return "LAUNCHED EVENT CLIENT!!! Check the logs..."; + } + + @GetMapping("/launch-flux-client") + public String launchcFluxFromSSEWebClient() { + consumeFlux(); + return "LAUNCHED EVENT CLIENT!!! Check the logs..."; + } + + @GetMapping("/launch-sse-from-flux-endpoint-client") + public String launchFluxFromFluxWebClient() { + consumeSSEFromFluxEndpoint(); + return "LAUNCHED EVENT CLIENT!!! Check the logs..."; + } + + @Async + public void consumeSSE() { + ParameterizedTypeReference> type = new ParameterizedTypeReference>() { + }; + + Flux> eventStream = client.get() + .uri("/stream-sse") + .retrieve() + .bodyToFlux(type); + + eventStream.subscribe(content -> logger.info("Current time: {} - Received SSE: name[{}], id [{}], content[{}] ", LocalTime.now(), content.event(), content.id(), content.data()), error -> logger.error("Error receiving SSE: {}", error), + () -> logger.info("Completed!!!")); + } + + @Async + public void consumeFlux() { + Flux stringStream = client.get() + .uri("/stream-flux") + .accept(MediaType.TEXT_EVENT_STREAM) + .retrieve() + .bodyToFlux(String.class); + + stringStream.subscribe(content -> logger.info("Current time: {} - Received content: {} ", LocalTime.now(), content), error -> logger.error("Error retrieving content: {}", error), () -> logger.info("Completed!!!")); + } + + @Async + public void consumeSSEFromFluxEndpoint() { + ParameterizedTypeReference> type = new ParameterizedTypeReference>() { + }; + + Flux> eventStream = client.get() + .uri("/stream-flux") + .accept(MediaType.TEXT_EVENT_STREAM) + .retrieve() + .bodyToFlux(type); + + eventStream.subscribe(content -> logger.info("Current time: {} - Received SSE: name[{}], id [{}], content[{}] ", LocalTime.now(), content.event(), content.id(), content.data()), error -> logger.error("Error receiving SSE: {}", error), + () -> logger.info("Completed!!!")); + } +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/serversentevents/server/ServerSSEApplication.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/serversentevents/server/ServerSSEApplication.java new file mode 100644 index 0000000000..2750e6616d --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/serversentevents/server/ServerSSEApplication.java @@ -0,0 +1,17 @@ +package com.baeldung.reactive.serversentevents.server; + +import java.util.Collections; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ServerSSEApplication { + + public static void main(String[] args) { + SpringApplication app = new SpringApplication(ServerSSEApplication.class); + app.setDefaultProperties(Collections.singletonMap("server.port", "8081")); + app.run(args); + } + +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/serversentevents/server/controllers/ServerController.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/serversentevents/server/controllers/ServerController.java new file mode 100644 index 0000000000..1ad8e848cf --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/serversentevents/server/controllers/ServerController.java @@ -0,0 +1,35 @@ +package com.baeldung.reactive.serversentevents.server.controllers; + +import java.time.Duration; +import java.time.LocalTime; + +import org.springframework.http.MediaType; +import org.springframework.http.codec.ServerSentEvent; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import reactor.core.publisher.Flux; + +@RestController +@RequestMapping("/sse-server") +public class ServerController { + + @GetMapping("/stream-sse") + public Flux> streamEvents() { + return Flux.interval(Duration.ofSeconds(1)) + .map(sequence -> ServerSentEvent. builder() + .id(String.valueOf(sequence)) + .event("periodic-event") + .data("SSE - " + LocalTime.now() + .toString()) + .build()); + } + + @GetMapping(path = "/stream-flux", produces = MediaType.TEXT_EVENT_STREAM_VALUE) + public Flux streamFlux() { + return Flux.interval(Duration.ofSeconds(1)) + .map(sequence -> "Flux - " + LocalTime.now() + .toString()); + } +} diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/serversentsevents/ServiceSentEventLiveTest.java b/spring-5-reactive/src/test/java/com/baeldung/reactive/serversentsevents/ServiceSentEventLiveTest.java new file mode 100644 index 0000000000..53f4a3b1bb --- /dev/null +++ b/spring-5-reactive/src/test/java/com/baeldung/reactive/serversentsevents/ServiceSentEventLiveTest.java @@ -0,0 +1,49 @@ +package com.baeldung.reactive.serversentsevents; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; +import org.junit.platform.runner.JUnitPlatform; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.reactive.server.WebTestClient; + +@RunWith(JUnitPlatform.class) +@SpringBootTest +public class ServiceSentEventLiveTest { + + private WebTestClient client = WebTestClient.bindToServer() + .baseUrl("http://localhost:8081/sse-server") + .build(); + + @Test + public void whenSSEEndpointIsCalled_thenEventStreamingBegins() { + + Executable sseStreamingCall = () -> client.get() + .uri("/stream-sse") + .exchange() + .expectStatus() + .isOk() + .expectHeader() + .contentTypeCompatibleWith(MediaType.TEXT_EVENT_STREAM) + .expectBody(String.class); + + Assertions.assertThrows(IllegalStateException.class, sseStreamingCall, "Expected test to timeout and throw IllegalStateException, but it didn't"); + } + + @Test + public void whenFluxEndpointIsCalled_thenEventStreamingBegins() { + + Executable sseStreamingCall = () -> client.get() + .uri("/stream-flux") + .exchange() + .expectStatus() + .isOk() + .expectHeader() + .contentTypeCompatibleWith(MediaType.TEXT_EVENT_STREAM) + .expectBody(String.class); + + Assertions.assertThrows(IllegalStateException.class, sseStreamingCall, "Expected test to timeout and throw IllegalStateException, but it didn't"); + } +} diff --git a/spring-5-security/README.md b/spring-5-security/README.md index 564dcd3c96..55fa7ab042 100644 --- a/spring-5-security/README.md +++ b/spring-5-security/README.md @@ -5,3 +5,4 @@ - [A Custom Spring SecurityConfigurer](http://www.baeldung.com/spring-security-custom-configurer) - [New Password Storage In Spring Security 5](http://www.baeldung.com/spring-security-5-password-storage) - [Default Password Encoder in Spring Security 5](https://www.baeldung.com/spring-security-5-default-password-encoder) +- [Extracting Principal and Authorities using Spring Security OAuth](https://www.baeldung.com/spring-security-oauth-principal-authorities-extractor) diff --git a/spring-5/README.md b/spring-5/README.md index baf03fb3b3..d3c1decbc7 100644 --- a/spring-5/README.md +++ b/spring-5/README.md @@ -12,3 +12,5 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Introduction to Spring REST Docs](http://www.baeldung.com/spring-rest-docs) - [Spring ResponseStatusException](http://www.baeldung.com/spring-response-status-exception) - [Spring Assert Statements](http://www.baeldung.com/spring-assert) +- [Configuring a Hikari Connection Pool with Spring Boot](https://www.baeldung.com/spring-boot-hikari) + diff --git a/spring-5/pom.xml b/spring-5/pom.xml index 9f60b8a364..293edb5bda 100644 --- a/spring-5/pom.xml +++ b/spring-5/pom.xml @@ -1,5 +1,6 @@ - 4.0.0 com.baeldung @@ -134,7 +135,26 @@ + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + methods + true + + **/*IntegrationTest.java + **/*IntTest.java + **/*LongRunningUnitTest.java + **/*ManualTest.java + **/JdbcTest.java + **/*LiveTest.java + + + + @@ -142,6 +162,8 @@ 1.5.6 4.1 ${project.build.directory}/generated-snippets + 2.21.0 + diff --git a/spring-all/pom.xml b/spring-all/pom.xml index c4c4cf7963..bab2e431ec 100644 --- a/spring-all/pom.xml +++ b/spring-all/pom.xml @@ -45,6 +45,15 @@ spring-shell ${org.springframework.shell.version} + + org.springframework + spring-websocket + + + org.springframework + spring-messaging + + org.springframework @@ -190,7 +199,24 @@ - + + + dev + + true + + + dev + + + + prod + + prod + + + + org.baeldung.sample.App diff --git a/spring-all/src/main/java/org/baeldung/profiles/SpringProfilesConfig.java b/spring-all/src/main/java/org/baeldung/profiles/SpringProfilesConfig.java index eb5543e3db..2d1905ee9c 100644 --- a/spring-all/src/main/java/org/baeldung/profiles/SpringProfilesConfig.java +++ b/spring-all/src/main/java/org/baeldung/profiles/SpringProfilesConfig.java @@ -2,9 +2,11 @@ package org.baeldung.profiles; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; @Configuration @ComponentScan("org.baeldung.profiles") +@PropertySource(value = "classpath:application.properties") public class SpringProfilesConfig { } diff --git a/spring-all/src/main/java/org/baeldung/scopes/ScopesController.java b/spring-all/src/main/java/org/baeldung/scopes/ScopesController.java index 73df386aff..9257a3aa3a 100644 --- a/spring-all/src/main/java/org/baeldung/scopes/ScopesController.java +++ b/spring-all/src/main/java/org/baeldung/scopes/ScopesController.java @@ -13,21 +13,36 @@ import org.springframework.web.bind.annotation.RequestMapping; public class ScopesController { public static final Logger LOG = LoggerFactory.getLogger(ScopesController.class); - @Resource(name = "requestMessage") - HelloMessageGenerator requestMessage; + @Resource(name = "requestScopedBean") + HelloMessageGenerator requestScopedBean; - @Resource(name = "sessionMessage") - HelloMessageGenerator sessionMessage; + @Resource(name = "sessionScopedBean") + HelloMessageGenerator sessionScopedBean; - @RequestMapping("/scopes") - public String getScopes(final Model model) { - LOG.info("Request Message:" + requestMessage.getMessage()); - LOG.info("Session Message" + sessionMessage.getMessage()); - requestMessage.setMessage("Good morning!"); - sessionMessage.setMessage("Good afternoon!"); - model.addAttribute("requestMessage", requestMessage.getMessage()); - model.addAttribute("sessionMessage", sessionMessage.getMessage()); + @Resource(name = "applicationScopedBean") + HelloMessageGenerator applicationScopedBean; + + @RequestMapping("/scopes/request") + public String getRequestScopeMessage(final Model model) { + model.addAttribute("previousMessage", requestScopedBean.getMessage()); + requestScopedBean.setMessage("Request Scope Message!"); + model.addAttribute("currentMessage", requestScopedBean.getMessage()); return "scopesExample"; } + @RequestMapping("/scopes/session") + public String getSessionScopeMessage(final Model model) { + model.addAttribute("previousMessage", sessionScopedBean.getMessage()); + sessionScopedBean.setMessage("Session Scope Message!"); + model.addAttribute("currentMessage", sessionScopedBean.getMessage()); + return "scopesExample"; + } + + @RequestMapping("/scopes/application") + public String getApplicationScopeMessage(final Model model) { + model.addAttribute("previousMessage", applicationScopedBean.getMessage()); + applicationScopedBean.setMessage("Application Scope Message!"); + model.addAttribute("currentMessage", applicationScopedBean.getMessage()); + return "scopesExample"; + } } diff --git a/spring-all/src/main/java/org/baeldung/spring/config/ScopesConfig.java b/spring-all/src/main/java/org/baeldung/spring/config/ScopesConfig.java index fb34725508..b5fe494ee2 100644 --- a/spring-all/src/main/java/org/baeldung/spring/config/ScopesConfig.java +++ b/spring-all/src/main/java/org/baeldung/spring/config/ScopesConfig.java @@ -27,19 +27,25 @@ public class ScopesConfig { @Bean @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS) - public HelloMessageGenerator requestMessage() { + public HelloMessageGenerator requestScopedBean() { return new HelloMessageGenerator(); } @Bean @Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS) - public HelloMessageGenerator sessionMessage() { + public HelloMessageGenerator sessionScopedBean() { return new HelloMessageGenerator(); } @Bean @Scope(value = WebApplicationContext.SCOPE_APPLICATION, proxyMode = ScopedProxyMode.TARGET_CLASS) - public HelloMessageGenerator globalSessionMessage() { + public HelloMessageGenerator applicationScopedBean() { + return new HelloMessageGenerator(); + } + + @Bean + @Scope(scopeName = "websocket", proxyMode = ScopedProxyMode.TARGET_CLASS) + public HelloMessageGenerator websocketScopedBean() { return new HelloMessageGenerator(); } diff --git a/spring-all/src/main/resources/application.properties b/spring-all/src/main/resources/application.properties new file mode 100644 index 0000000000..cbfe3f2df2 --- /dev/null +++ b/spring-all/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.profiles.active=@spring.profiles.active@ \ No newline at end of file diff --git a/spring-all/src/main/webapp/WEB-INF/view/scopesExample.jsp b/spring-all/src/main/webapp/WEB-INF/view/scopesExample.jsp index 7974cf0220..0946f1b5ef 100644 --- a/spring-all/src/main/webapp/WEB-INF/view/scopesExample.jsp +++ b/spring-all/src/main/webapp/WEB-INF/view/scopesExample.jsp @@ -2,9 +2,9 @@ -

Bean Scopes Examples

-
- Request Message: ${requestMessage }
- Session Message: ${sessionMessage } +

Bean Scopes Examples

+
Previous Message: ${previousMessage } +
Current Message: ${currentMessage } +
\ No newline at end of file diff --git a/spring-all/src/test/java/org/baeldung/profiles/SpringProfilesWithMavenPropertiesIntegrationTest.java b/spring-all/src/test/java/org/baeldung/profiles/SpringProfilesWithMavenPropertiesIntegrationTest.java new file mode 100644 index 0000000000..929d088a14 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/profiles/SpringProfilesWithMavenPropertiesIntegrationTest.java @@ -0,0 +1,22 @@ +package org.baeldung.profiles; + +import org.junit.Assert; +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 org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { SpringProfilesConfig.class }, loader = AnnotationConfigContextLoader.class) +public class SpringProfilesWithMavenPropertiesIntegrationTest { + + @Autowired + DatasourceConfig datasourceConfig; + + @Test + public void testSpringProfiles() { + Assert.assertTrue(datasourceConfig instanceof DevDatasourceConfig); + } +} \ No newline at end of file diff --git a/spring-amqp/pom.xml b/spring-amqp/pom.xml index 065fce7d35..e08a4243b3 100755 --- a/spring-amqp/pom.xml +++ b/spring-amqp/pom.xml @@ -2,10 +2,10 @@ 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 - springamqp + spring-amqp 0.1-SNAPSHOT jar - springamqp + spring-amqp Introduction to Spring-AMQP diff --git a/spring-boot-angular-ecommerce/README.md b/spring-boot-angular-ecommerce/README.md index 933059bc45..c6564643e2 100644 --- a/spring-boot-angular-ecommerce/README.md +++ b/spring-boot-angular-ecommerce/README.md @@ -1,2 +1,2 @@ ### Relevant Articles: -- [A Simple E-Commerce Implementation with Spring]() +- [A Simple E-Commerce Implementation with Spring](https://www.baeldung.com/spring-angular-ecommerce) diff --git a/camel-api/README.md b/spring-boot-camel/README.md similarity index 100% rename from camel-api/README.md rename to spring-boot-camel/README.md diff --git a/camel-api/pom.xml b/spring-boot-camel/pom.xml similarity index 100% rename from camel-api/pom.xml rename to spring-boot-camel/pom.xml diff --git a/camel-api/src/main/java/com/baeldung/camel/Application.java b/spring-boot-camel/src/main/java/com/baeldung/camel/Application.java similarity index 100% rename from camel-api/src/main/java/com/baeldung/camel/Application.java rename to spring-boot-camel/src/main/java/com/baeldung/camel/Application.java diff --git a/camel-api/src/main/java/com/baeldung/camel/ExampleServices.java b/spring-boot-camel/src/main/java/com/baeldung/camel/ExampleServices.java similarity index 100% rename from camel-api/src/main/java/com/baeldung/camel/ExampleServices.java rename to spring-boot-camel/src/main/java/com/baeldung/camel/ExampleServices.java diff --git a/camel-api/src/main/java/com/baeldung/camel/MyBean.java b/spring-boot-camel/src/main/java/com/baeldung/camel/MyBean.java similarity index 100% rename from camel-api/src/main/java/com/baeldung/camel/MyBean.java rename to spring-boot-camel/src/main/java/com/baeldung/camel/MyBean.java diff --git a/camel-api/src/main/resources/application.properties b/spring-boot-camel/src/main/resources/application.properties similarity index 100% rename from camel-api/src/main/resources/application.properties rename to spring-boot-camel/src/main/resources/application.properties diff --git a/camel-api/src/main/resources/application.yml b/spring-boot-camel/src/main/resources/application.yml similarity index 100% rename from camel-api/src/main/resources/application.yml rename to spring-boot-camel/src/main/resources/application.yml diff --git a/camel-api/src/main/resources/logback.xml b/spring-boot-camel/src/main/resources/logback.xml similarity index 100% rename from camel-api/src/main/resources/logback.xml rename to spring-boot-camel/src/main/resources/logback.xml diff --git a/spring-boot-h2/spring-boot-h2-database/.gitignore b/spring-boot-h2/spring-boot-h2-database/.gitignore new file mode 100644 index 0000000000..82eca336e3 --- /dev/null +++ b/spring-boot-h2/spring-boot-h2-database/.gitignore @@ -0,0 +1,25 @@ +/target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/build/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ \ No newline at end of file diff --git a/vaadin-spring/pom.xml b/spring-boot-h2/spring-boot-h2-database/pom.xml similarity index 51% rename from vaadin-spring/pom.xml rename to spring-boot-h2/spring-boot-h2-database/pom.xml index 09f79f2676..94f2293c34 100644 --- a/vaadin-spring/pom.xml +++ b/spring-boot-h2/spring-boot-h2-database/pom.xml @@ -1,22 +1,29 @@ - + 4.0.0 - com.baeldung - vaadin-spring - 0.1.0 + com.baeldung.h2db + spring-boot-h2-database + 0.0.1-SNAPSHOT + jar + + Demo Spring Boot applications that starts H2 in memory database - parent-boot-2 - com.baeldung - 0.0.1-SNAPSHOT - ../parent-boot-2 + org.springframework.boot + spring-boot-starter-parent + 2.0.4.RELEASE + + UTF-8 + UTF-8 1.8 + + com.mycorp.starter.HelloWorldApplication @@ -24,15 +31,11 @@ org.springframework.boot spring-boot-starter-data-jpa - - com.vaadin - vaadin-spring-boot-starter - + com.h2database h2 - org.springframework.boot spring-boot-starter-test @@ -40,18 +43,6 @@ - - - - com.vaadin - vaadin-bom - 10.0.1 - pom - import - - - - @@ -60,5 +51,4 @@ - diff --git a/spring-boot-h2/spring-boot-h2-database/src/main/java/com/baeldung/h2db/demo/client/ClientSpringBootApp.java b/spring-boot-h2/spring-boot-h2-database/src/main/java/com/baeldung/h2db/demo/client/ClientSpringBootApp.java new file mode 100644 index 0000000000..7402312e1c --- /dev/null +++ b/spring-boot-h2/spring-boot-h2-database/src/main/java/com/baeldung/h2db/demo/client/ClientSpringBootApp.java @@ -0,0 +1,53 @@ +package com.baeldung.h2db.demo.client; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Arrays; +import javax.annotation.PostConstruct; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.PropertySource; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; + +@SpringBootApplication +@ComponentScan("com.baeldung.h2db.demo.client") +public class ClientSpringBootApp { + + @Autowired + private JdbcTemplate jdbcTemplate; + + public static void main(String[] args) { + System.setProperty("spring.datasource.url","jdbc:h2:tcp://localhost:9091/mem:mydb"); + SpringApplication.run(ClientSpringBootApp.class, args); + } + + @PostConstruct + private void initDb() { + System.out.println("****** Inserting more sample data in the table: Employees ******"); + String sqlStatements[] = { + "insert into employees(first_name, last_name) values('Donald','Trump')", + "insert into employees(first_name, last_name) values('Barack','Obama')" + }; + + Arrays.asList(sqlStatements).stream().forEach(sql -> { + System.out.println(sql); + jdbcTemplate.execute(sql); + }); + + System.out.println(String.format("****** Fetching from table: %s ******", "Employees")); + jdbcTemplate.query("select id,first_name,last_name from employees", + new RowMapper() { + @Override + public Object mapRow(ResultSet rs, int i) throws SQLException { + System.out.println(String.format("id:%s,first_name:%s,last_name:%s", + rs.getString("id"), + rs.getString("first_name"), + rs.getString("last_name"))); + return null; + } + }); + } +} diff --git a/spring-boot-h2/spring-boot-h2-database/src/main/java/com/baeldung/h2db/demo/server/SpringBootApp.java b/spring-boot-h2/spring-boot-h2-database/src/main/java/com/baeldung/h2db/demo/server/SpringBootApp.java new file mode 100644 index 0000000000..e75b42a934 --- /dev/null +++ b/spring-boot-h2/spring-boot-h2-database/src/main/java/com/baeldung/h2db/demo/server/SpringBootApp.java @@ -0,0 +1,61 @@ +package com.baeldung.h2db.demo.server; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Arrays; +import javax.annotation.PostConstruct; +import org.h2.tools.Server; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; + +@SpringBootApplication +@ComponentScan("com.baeldung.h2db.demo.server") +public class SpringBootApp { + + @Autowired + private JdbcTemplate jdbcTemplate; + + public static void main(String[] args) { + SpringApplication.run(SpringBootApp.class, args); + } + + @PostConstruct + private void initDb() { + System.out.println(String.format("****** Creating table: %s, and Inserting test data ******", "Employees")); + + String sqlStatements[] = { + "drop table employees if exists", + "create table employees(id serial,first_name varchar(255),last_name varchar(255))", + "insert into employees(first_name, last_name) values('Eugen','Paraschiv')", + "insert into employees(first_name, last_name) values('Scott','Tiger')" + }; + + Arrays.asList(sqlStatements).stream().forEach(sql -> { + System.out.println(sql); + jdbcTemplate.execute(sql); + }); + + System.out.println(String.format("****** Fetching from table: %s ******", "Employees")); + jdbcTemplate.query("select id,first_name,last_name from employees", + new RowMapper() { + @Override + public Object mapRow(ResultSet rs, int i) throws SQLException { + System.out.println(String.format("id:%s,first_name:%s,last_name:%s", + rs.getString("id"), + rs.getString("first_name"), + rs.getString("last_name"))); + return null; + } + }); + } + + @Bean(initMethod = "start", destroyMethod = "stop") + public Server inMemoryH2DatabaseServer() throws SQLException { + return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9091"); + } +} diff --git a/spring-boot-h2/spring-boot-h2-database/src/main/resources/application.properties b/spring-boot-h2/spring-boot-h2-database/src/main/resources/application.properties new file mode 100644 index 0000000000..0591cc9e0f --- /dev/null +++ b/spring-boot-h2/spring-boot-h2-database/src/main/resources/application.properties @@ -0,0 +1,7 @@ +spring.datasource.url=jdbc:h2:mem:mydb +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username=sa +spring.datasource.password= +spring.jpa.hibernate.ddl-auto=create +spring.h2.console.enabled=true +spring.h2.console.path=/h2-console \ No newline at end of file diff --git a/spring-boot-jasypt/pom.xml b/spring-boot-jasypt/pom.xml index 8b7a475824..de0df92678 100644 --- a/spring-boot-jasypt/pom.xml +++ b/spring-boot-jasypt/pom.xml @@ -4,11 +4,9 @@ 4.0.0 com.example.jasypt - JasyptDemo - 0.0.1-SNAPSHOT + spring-boot-jasypt jar - - JasyptDemo + spring-boot-jasypt Demo project for Spring Boot @@ -53,9 +51,6 @@ - UTF-8 - UTF-8 - 1.8 2.0.0 diff --git a/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/CustomJasyptTest.java b/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/CustomJasyptIntegrationTest.java similarity index 94% rename from spring-boot-jasypt/src/test/java/com/baeldung/jasypt/CustomJasyptTest.java rename to spring-boot-jasypt/src/test/java/com/baeldung/jasypt/CustomJasyptIntegrationTest.java index 58c2dc7bb2..c24cfe6efa 100644 --- a/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/CustomJasyptTest.java +++ b/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/CustomJasyptIntegrationTest.java @@ -14,7 +14,7 @@ import com.baeldung.jasypt.Main; @RunWith(SpringRunner.class) @SpringBootTest(classes = {Main.class}) -public class CustomJasyptTest { +public class CustomJasyptIntegrationTest { @Autowired ApplicationContext appCtx; diff --git a/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptSimpleTest.java b/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptSimpleIntegrationTest.java similarity index 95% rename from spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptSimpleTest.java rename to spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptSimpleIntegrationTest.java index f9b66b5044..e8dda73b4a 100644 --- a/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptSimpleTest.java +++ b/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptSimpleIntegrationTest.java @@ -13,7 +13,7 @@ import com.baeldung.jasypt.simple.PropertyServiceForJasyptSimple; @RunWith(SpringRunner.class) @SpringBootTest -public class JasyptSimpleTest { +public class JasyptSimpleIntegrationTest { @Autowired ApplicationContext appCtx; diff --git a/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptWithStarterTest.java b/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptWithStarterIntegrationTest.java similarity index 95% rename from spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptWithStarterTest.java rename to spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptWithStarterIntegrationTest.java index d246c21036..5f5d409ab9 100644 --- a/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptWithStarterTest.java +++ b/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptWithStarterIntegrationTest.java @@ -14,7 +14,7 @@ import com.baeldung.jasypt.starter.PropertyServiceForJasyptStarter; @RunWith(SpringRunner.class) @SpringBootTest -public class JasyptWithStarterTest { +public class JasyptWithStarterIntegrationTest { @Autowired ApplicationContext appCtx; diff --git a/spring-boot-logging-log4j2/README.md b/spring-boot-logging-log4j2/README.md index 7676bd1919..305957ed8d 100644 --- a/spring-boot-logging-log4j2/README.md +++ b/spring-boot-logging-log4j2/README.md @@ -1,2 +1,3 @@ ### Relevant Articles: - [Logging in Spring Boot](http://www.baeldung.com/spring-boot-logging) +- [How to Disable Console Logging in Spring Boot](https://www.baeldung.com/spring-boot-disable-console-logging) diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/pom.xml b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/pom.xml new file mode 100644 index 0000000000..c3bad74352 --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/pom.xml @@ -0,0 +1,46 @@ + + 4.0.0 + disabling-console-jul + + + com.baeldung + disabling-console-logging + 0.0.1-SNAPSHOT + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-logging + + + + + org.slf4j + jul-to-slf4j + + + org.slf4j + slf4j-simple + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/src/main/java/com/baeldung/springbootlogging/disablingconsole/jul/properties/DisablingConsoleJulApp.java b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/src/main/java/com/baeldung/springbootlogging/disablingconsole/jul/properties/DisablingConsoleJulApp.java new file mode 100644 index 0000000000..c9187e9ace --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/src/main/java/com/baeldung/springbootlogging/disablingconsole/jul/properties/DisablingConsoleJulApp.java @@ -0,0 +1,11 @@ +package com.baeldung.springbootlogging.disablingconsole.jul.properties; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DisablingConsoleJulApp { + public static void main(String[] args) { + SpringApplication.run(DisablingConsoleJulApp.class, args); + } +} diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/src/main/java/com/baeldung/springbootlogging/disablingconsole/jul/properties/controllers/DisabledConsoleRestController.java b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/src/main/java/com/baeldung/springbootlogging/disablingconsole/jul/properties/controllers/DisabledConsoleRestController.java new file mode 100644 index 0000000000..e33089ed36 --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/src/main/java/com/baeldung/springbootlogging/disablingconsole/jul/properties/controllers/DisabledConsoleRestController.java @@ -0,0 +1,19 @@ +package com.baeldung.springbootlogging.disablingconsole.jul.properties.controllers; + +import java.time.LocalTime; +import java.util.logging.Logger; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class DisabledConsoleRestController { + + private static final Logger LOG = Logger.getLogger(DisabledConsoleRestController.class.getName()); + + @GetMapping("/disabled-console-jul-properties") + public String logTime() { + LOG.info("Current time: " + LocalTime.now()); + return "finished!"; + } +} diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/src/main/resources/application.properties b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/src/main/resources/application.properties new file mode 100644 index 0000000000..d81eed95d3 --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/src/main/resources/application.properties @@ -0,0 +1 @@ +logging.file=true \ No newline at end of file diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/src/main/resources/logging.properties b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/src/main/resources/logging.properties new file mode 100644 index 0000000000..0136354868 --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/src/main/resources/logging.properties @@ -0,0 +1,4 @@ +handlers= java.util.logging.FileHandler + +java.util.logging.FileHandler.pattern=baeldung.log +java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter \ No newline at end of file diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-log4j2/pom.xml b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-log4j2/pom.xml new file mode 100644 index 0000000000..f9b34ec7d9 --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-log4j2/pom.xml @@ -0,0 +1,42 @@ + + 4.0.0 + disabling-console-log4j2 + + + com.baeldung + disabling-console-logging + 0.0.1-SNAPSHOT + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-logging + + + + + org.springframework.boot + spring-boot-starter-log4j2 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-log4j2/src/main/java/com/baeldung/springbootlogging/disablingconsole/log4j2/xml/DisablingConsoleLog4j2App.java b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-log4j2/src/main/java/com/baeldung/springbootlogging/disablingconsole/log4j2/xml/DisablingConsoleLog4j2App.java new file mode 100644 index 0000000000..ae59f3b3e1 --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-log4j2/src/main/java/com/baeldung/springbootlogging/disablingconsole/log4j2/xml/DisablingConsoleLog4j2App.java @@ -0,0 +1,11 @@ +package com.baeldung.springbootlogging.disablingconsole.log4j2.xml; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DisablingConsoleLog4j2App { + public static void main(String[] args) { + SpringApplication.run(DisablingConsoleLog4j2App.class, args); + } +} diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-log4j2/src/main/java/com/baeldung/springbootlogging/disablingconsole/log4j2/xml/controllers/DisabledConsoleRestController.java b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-log4j2/src/main/java/com/baeldung/springbootlogging/disablingconsole/log4j2/xml/controllers/DisabledConsoleRestController.java new file mode 100644 index 0000000000..f6485b63d4 --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-log4j2/src/main/java/com/baeldung/springbootlogging/disablingconsole/log4j2/xml/controllers/DisabledConsoleRestController.java @@ -0,0 +1,20 @@ +package com.baeldung.springbootlogging.disablingconsole.log4j2.xml.controllers; + +import java.time.LocalTime; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class DisabledConsoleRestController { + + private static final Logger LOG = LoggerFactory.getLogger(DisabledConsoleRestController.class); + + @GetMapping("/disabled-console-log4j2-xml") + public String logTime() { + LOG.info("Current time: {}", LocalTime.now()); + return "finished!"; + } +} diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-log4j2/src/main/resources/log4j2.xml b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-log4j2/src/main/resources/log4j2.xml new file mode 100644 index 0000000000..20975f40d9 --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-log4j2/src/main/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/pom.xml b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/pom.xml new file mode 100644 index 0000000000..6f2170390b --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/pom.xml @@ -0,0 +1,9 @@ + + 4.0.0 + + com.baeldung + disabling-console-logging + 0.0.1-SNAPSHOT + + disabling-console-logback + \ No newline at end of file diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/src/main/java/com/baeldung/springbootlogging/disablingconsole/logback/xml/DisablingConsoleLogbackApp.java b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/src/main/java/com/baeldung/springbootlogging/disablingconsole/logback/xml/DisablingConsoleLogbackApp.java new file mode 100644 index 0000000000..9b38d42477 --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/src/main/java/com/baeldung/springbootlogging/disablingconsole/logback/xml/DisablingConsoleLogbackApp.java @@ -0,0 +1,11 @@ +package com.baeldung.springbootlogging.disablingconsole.logback.xml; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DisablingConsoleLogbackApp { + public static void main(String[] args) { + SpringApplication.run(DisablingConsoleLogbackApp.class, args); + } +} diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/src/main/java/com/baeldung/springbootlogging/disablingconsole/logback/xml/controllers/DisabledConsoleRestController.java b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/src/main/java/com/baeldung/springbootlogging/disablingconsole/logback/xml/controllers/DisabledConsoleRestController.java new file mode 100644 index 0000000000..e1d6b1d45e --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/src/main/java/com/baeldung/springbootlogging/disablingconsole/logback/xml/controllers/DisabledConsoleRestController.java @@ -0,0 +1,20 @@ +package com.baeldung.springbootlogging.disablingconsole.logback.xml.controllers; + +import java.time.LocalTime; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class DisabledConsoleRestController { + + private static final Logger LOG = LoggerFactory.getLogger(DisabledConsoleRestController.class); + + @GetMapping("/disabled-console-logback-xml") + public String logTime() { + LOG.info("Current time: {}", LocalTime.now()); + return "finished!"; + } +} diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/src/main/resources/application.properties b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/src/main/resources/application.properties new file mode 100644 index 0000000000..70c2f97194 --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/src/main/resources/application.properties @@ -0,0 +1,7 @@ +logging.file=disabled-console.log + +### we could potentially 'disable' console logging by setting a blank log pattern: +#logging.pattern.console= + +### or disable the logs completely by setting the root logging level as 'OFF': +#logging.level.root=OFF diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/src/main/resources/logback-spring.xml b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/src/main/resources/logback-spring.xml new file mode 100644 index 0000000000..5c751e994f --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/src/main/resources/logback-spring.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/spring-boot-logging-log4j2/disabling-console-logging/pom.xml b/spring-boot-logging-log4j2/disabling-console-logging/pom.xml new file mode 100644 index 0000000000..39a4a40f12 --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/pom.xml @@ -0,0 +1,19 @@ + + 4.0.0 + disabling-console-logging + pom + Projects for Disabling Spring Boot Console Logging tutorials + + + com.baeldung + spring-boot-logging-log4j2 + 0.0.1-SNAPSHOT + + + + disabling-console-logback + disabling-console-log4j2 + disabling-console-jul + + \ No newline at end of file diff --git a/spring-boot-logging-log4j2/pom.xml b/spring-boot-logging-log4j2/pom.xml index 61f949b820..7caf1e8690 100644 --- a/spring-boot-logging-log4j2/pom.xml +++ b/spring-boot-logging-log4j2/pom.xml @@ -3,11 +3,9 @@ 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-boot-logging-log4j2 - 0.0.1-SNAPSHOT - jar - Demo project for Spring Boot Logging with Log4J2 + pom + Projects for Spring Boot Logging tutorials parent-boot-2 @@ -20,16 +18,6 @@ org.springframework.boot spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-logging - - - - - org.springframework.boot - spring-boot-starter-log4j2 @@ -38,6 +26,11 @@ test + + + spring-boot-logging-log4j2-app + disabling-console-logging + @@ -48,10 +41,4 @@ - - UTF-8 - UTF-8 - 1.8 - - diff --git a/spring-boot-logging-log4j2/spring-boot-logging-log4j2-app/pom.xml b/spring-boot-logging-log4j2/spring-boot-logging-log4j2-app/pom.xml new file mode 100644 index 0000000000..571794167e --- /dev/null +++ b/spring-boot-logging-log4j2/spring-boot-logging-log4j2-app/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + + spring-boot-logging-log4j2-app + jar + Demo project for Spring Boot Logging with Log4J2 + + + spring-boot-logging-log4j2 + com.baeldung + 0.0.1-SNAPSHOT + .. + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-logging + + + + + org.springframework.boot + spring-boot-starter-log4j2 + + + diff --git a/spring-boot-logging-log4j2/src/main/java/com/baeldung/springbootlogging/LoggingController.java b/spring-boot-logging-log4j2/spring-boot-logging-log4j2-app/src/main/java/com/baeldung/springbootlogging/LoggingController.java similarity index 100% rename from spring-boot-logging-log4j2/src/main/java/com/baeldung/springbootlogging/LoggingController.java rename to spring-boot-logging-log4j2/spring-boot-logging-log4j2-app/src/main/java/com/baeldung/springbootlogging/LoggingController.java diff --git a/spring-boot-logging-log4j2/src/main/java/com/baeldung/springbootlogging/SpringBootLoggingApplication.java b/spring-boot-logging-log4j2/spring-boot-logging-log4j2-app/src/main/java/com/baeldung/springbootlogging/SpringBootLoggingApplication.java similarity index 100% rename from spring-boot-logging-log4j2/src/main/java/com/baeldung/springbootlogging/SpringBootLoggingApplication.java rename to spring-boot-logging-log4j2/spring-boot-logging-log4j2-app/src/main/java/com/baeldung/springbootlogging/SpringBootLoggingApplication.java diff --git a/spring-security-mvc-socket/src/main/webapp/resources/styles/style.css b/spring-boot-logging-log4j2/spring-boot-logging-log4j2-app/src/main/resources/application.properties similarity index 100% rename from spring-security-mvc-socket/src/main/webapp/resources/styles/style.css rename to spring-boot-logging-log4j2/spring-boot-logging-log4j2-app/src/main/resources/application.properties diff --git a/spring-boot-logging-log4j2/src/main/resources/log4j2-spring.xml b/spring-boot-logging-log4j2/spring-boot-logging-log4j2-app/src/main/resources/log4j2-spring.xml similarity index 100% rename from spring-boot-logging-log4j2/src/main/resources/log4j2-spring.xml rename to spring-boot-logging-log4j2/spring-boot-logging-log4j2-app/src/main/resources/log4j2-spring.xml diff --git a/spring-boot-mvc/README.md b/spring-boot-mvc/README.md index d1987e105f..b46dbe3bae 100644 --- a/spring-boot-mvc/README.md +++ b/spring-boot-mvc/README.md @@ -1,3 +1,10 @@ ### Relevant Articles: - [Guide to the Favicon in Spring Boot](http://www.baeldung.com/spring-boot-favicon) +- [Custom Validation MessageSource in Spring Boot](https://www.baeldung.com/spring-custom-validation-message-source) +- [Spring Boot Annotations](http://www.baeldung.com/spring-boot-annotations) +- [Spring Scheduling Annotations](http://www.baeldung.com/spring-scheduling-annotations) +- [Spring Web Annotations](http://www.baeldung.com/spring-mvc-annotations) +- [Spring Core Annotations](http://www.baeldung.com/spring-core-annotations) +- [Display RSS Feed with Spring MVC](http://www.baeldung.com/spring-mvc-rss-feed) + diff --git a/spring-boot-mvc/pom.xml b/spring-boot-mvc/pom.xml index d0fce26bb5..06712e4791 100644 --- a/spring-boot-mvc/pom.xml +++ b/spring-boot-mvc/pom.xml @@ -2,12 +2,10 @@ 4.0.0 - com.baeldung spring-boot-mvc - 0.0.1-SNAPSHOT jar spring-boot-mvc - Demo project for Spring Boot + Module For Spring Boot MVC parent-boot-2 @@ -21,6 +19,32 @@ org.springframework.boot spring-boot-starter-web + + + com.sun.faces + jsf-api + 2.2.9 + + + org.apache.tomcat.embed + tomcat-embed-jasper + + + javax.faces + javax.faces-api + 2.1 + + + javax.servlet + jstl + 1.2 + + + com.sun.faces + jsf-impl + 2.2.8-02 + + org.springframework.boot @@ -32,6 +56,13 @@ org.springframework.boot spring-boot-starter-validation + + + + com.rometools + rome + ${rome.version} + @@ -39,14 +70,18 @@ org.springframework.boot spring-boot-maven-plugin + + com.baeldung.springbootmvc.SpringBootMvcApplication + JAR + - UTF-8 - UTF-8 - 1.8 + + 1.10.0 + com.baeldung.springbootmvc.SpringBootMvcApplication diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/Bike.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/Bike.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/Bike.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/Bike.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/Biker.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/Biker.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/Biker.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/Biker.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/Car.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/Car.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/Car.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/Car.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/CarMechanic.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/CarMechanic.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/CarMechanic.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/CarMechanic.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/CarUtility.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/CarUtility.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/CarUtility.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/CarUtility.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/CustomException.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/CustomException.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/CustomException.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/CustomException.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/CustomResponseController.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/CustomResponseController.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/CustomResponseController.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/CustomResponseController.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/CustomResponseWithBuilderController.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/CustomResponseWithBuilderController.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/CustomResponseWithBuilderController.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/CustomResponseWithBuilderController.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/Driver.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/Driver.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/Driver.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/Driver.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/Engine.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/Engine.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/Engine.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/Engine.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/Vehicle.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/Vehicle.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/Vehicle.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/Vehicle.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleController.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleController.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleController.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleController.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleFactoryApplication.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleFactoryApplication.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleFactoryApplication.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleFactoryApplication.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleFactoryConfig.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleFactoryConfig.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleFactoryConfig.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleFactoryConfig.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleRepository.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleRepository.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleRepository.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleRepository.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleRestController.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleRestController.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleRestController.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleRestController.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleService.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleService.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleService.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleService.java diff --git a/spring-boot-mvc/src/main/java/com/baeldung/nosuchbeandefinitionexception/BeanA.java b/spring-boot-mvc/src/main/java/com/baeldung/nosuchbeandefinitionexception/BeanA.java new file mode 100644 index 0000000000..030216b747 --- /dev/null +++ b/spring-boot-mvc/src/main/java/com/baeldung/nosuchbeandefinitionexception/BeanA.java @@ -0,0 +1,12 @@ +package com.baeldung.nosuchbeandefinitionexception; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class BeanA { + + @Autowired + BeanB dependency; + +} \ No newline at end of file diff --git a/spring-boot-mvc/src/main/java/com/baeldung/nosuchbeandefinitionexception/BeanB.java b/spring-boot-mvc/src/main/java/com/baeldung/nosuchbeandefinitionexception/BeanB.java new file mode 100644 index 0000000000..f0cc263504 --- /dev/null +++ b/spring-boot-mvc/src/main/java/com/baeldung/nosuchbeandefinitionexception/BeanB.java @@ -0,0 +1,5 @@ +package com.baeldung.nosuchbeandefinitionexception; + +public class BeanB { + +} diff --git a/spring-boot-mvc/src/main/java/com/baeldung/nosuchbeandefinitionexception/NoSuchBeanDefinitionDemoApp.java b/spring-boot-mvc/src/main/java/com/baeldung/nosuchbeandefinitionexception/NoSuchBeanDefinitionDemoApp.java new file mode 100644 index 0000000000..8709cf85ac --- /dev/null +++ b/spring-boot-mvc/src/main/java/com/baeldung/nosuchbeandefinitionexception/NoSuchBeanDefinitionDemoApp.java @@ -0,0 +1,12 @@ +package com.baeldung.nosuchbeandefinitionexception; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class NoSuchBeanDefinitionDemoApp { + + public static void main(String[] args) { + SpringApplication.run(NoSuchBeanDefinitionDemoApp.class, args); + } +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/rss/RssFeedApplication.java b/spring-boot-mvc/src/main/java/com/baeldung/rss/RssFeedApplication.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/rss/RssFeedApplication.java rename to spring-boot-mvc/src/main/java/com/baeldung/rss/RssFeedApplication.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/rss/RssFeedController.java b/spring-boot-mvc/src/main/java/com/baeldung/rss/RssFeedController.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/rss/RssFeedController.java rename to spring-boot-mvc/src/main/java/com/baeldung/rss/RssFeedController.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/rss/RssFeedView.java b/spring-boot-mvc/src/main/java/com/baeldung/rss/RssFeedView.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/rss/RssFeedView.java rename to spring-boot-mvc/src/main/java/com/baeldung/rss/RssFeedView.java diff --git a/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/jsfapplication/JsfApplication.java b/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/jsfapplication/JsfApplication.java new file mode 100644 index 0000000000..5b4250d5e3 --- /dev/null +++ b/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/jsfapplication/JsfApplication.java @@ -0,0 +1,31 @@ +package com.baeldung.springbootmvc.jsfapplication; + +import javax.faces.webapp.FacesServlet; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.ServletRegistrationBean; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; + +import com.baeldung.springbootmvc.jsfapplication.controller.JsfController; +import com.baeldung.springbootmvc.jsfapplication.model.TodoDao; +import com.baeldung.springbootmvc.jsfapplication.service.TodoService; + +@SpringBootApplication +@ComponentScan(basePackageClasses = { JsfController.class, TodoDao.class, TodoService.class }) +public class JsfApplication extends SpringBootServletInitializer { + + public static void main(String[] args) { + SpringApplication.run(JsfApplication.class, args); + } + + @Bean + public ServletRegistrationBean servletRegistrationBean() { + FacesServlet servlet = new FacesServlet(); + ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(servlet, "*.jsf"); + return servletRegistrationBean; + } + +} diff --git a/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/jsfapplication/controller/JsfController.java b/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/jsfapplication/controller/JsfController.java new file mode 100644 index 0000000000..a9d21175c7 --- /dev/null +++ b/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/jsfapplication/controller/JsfController.java @@ -0,0 +1,19 @@ +package com.baeldung.springbootmvc.jsfapplication.controller; + +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +@Scope(value = "session") +@Component(value = "jsfController") +public class JsfController { + + public String loadTodoPage() { + checkPermission(); + return "/todo.xhtml"; + } + + private void checkPermission() { + // Details omitted + } + +} diff --git a/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/jsfapplication/model/Dao.java b/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/jsfapplication/model/Dao.java new file mode 100644 index 0000000000..0b97c5a78e --- /dev/null +++ b/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/jsfapplication/model/Dao.java @@ -0,0 +1,17 @@ +package com.baeldung.springbootmvc.jsfapplication.model; + +import java.util.Collection; +import java.util.Optional; + +public interface Dao { + + Optional get(int id); + + Collection getAll(); + + int save(T t); + + void update(T t); + + void delete(T t); +} \ No newline at end of file diff --git a/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/jsfapplication/model/Todo.java b/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/jsfapplication/model/Todo.java new file mode 100644 index 0000000000..7aa8480f43 --- /dev/null +++ b/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/jsfapplication/model/Todo.java @@ -0,0 +1,32 @@ +package com.baeldung.springbootmvc.jsfapplication.model; + +public class Todo { + + private int id; + private String message; + private int priority; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public int getPriority() { + return priority; + } + + public void setPriority(int priority) { + this.priority = priority; + } +} diff --git a/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/jsfapplication/model/TodoDao.java b/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/jsfapplication/model/TodoDao.java new file mode 100644 index 0000000000..d33f5e5da0 --- /dev/null +++ b/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/jsfapplication/model/TodoDao.java @@ -0,0 +1,48 @@ +package com.baeldung.springbootmvc.jsfapplication.model; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import java.util.stream.Collectors; + +import org.springframework.stereotype.Component; + +@Component +public class TodoDao implements Dao { + + private List todoList = new ArrayList<>(); + + @Override + public Optional get(int id) { + return Optional.ofNullable(todoList.get(id)); + } + + @Override + public Collection getAll() { + return Collections.unmodifiableCollection(todoList.stream() + .filter(Objects::nonNull) + .collect(Collectors.toList())); + } + + @Override + public int save(Todo todo) { + todoList.add(todo); + int index = todoList.size() - 1; + todo.setId(index); + return index; + } + + @Override + public void update(Todo todo) { + todoList.set(todo.getId(), todo); + } + + @Override + public void delete(Todo todo) { + todoList.set(todo.getId(), null); + } + +} \ No newline at end of file diff --git a/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/jsfapplication/service/TodoService.java b/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/jsfapplication/service/TodoService.java new file mode 100644 index 0000000000..89af3c66b9 --- /dev/null +++ b/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/jsfapplication/service/TodoService.java @@ -0,0 +1,50 @@ +package com.baeldung.springbootmvc.jsfapplication.service; + +import java.util.Collection; +import java.util.Comparator; +import java.util.stream.Collectors; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +import com.baeldung.springbootmvc.jsfapplication.model.Dao; +import com.baeldung.springbootmvc.jsfapplication.model.Todo; + +@Scope(value = "session") +@Component(value = "todoService") +public class TodoService { + + @Autowired + private Dao todoDao; + private Todo todo = new Todo(); + + public void save() { + todoDao.save(todo); + todo = new Todo(); + } + + public Collection getAllTodo() { + return todoDao.getAll(); + } + + public Collection getAllTodoSortedByPriority() { + return todoDao.getAll() + .stream() + .sorted(Comparator.comparingInt(Todo::getId)) + .collect(Collectors.toList()); + } + + public int saveTodo(Todo todo) { + validate(todo); + return todoDao.save(todo); + } + + private void validate(Todo todo) { + // Details omitted + } + + public Todo getTodo() { + return todo; + } +} diff --git a/spring-boot-mvc/src/main/webapp/WEB-INF/faces-config.xml b/spring-boot-mvc/src/main/webapp/WEB-INF/faces-config.xml new file mode 100644 index 0000000000..9e31a2e09d --- /dev/null +++ b/spring-boot-mvc/src/main/webapp/WEB-INF/faces-config.xml @@ -0,0 +1,9 @@ + + + + org.springframework.web.jsf.el.SpringBeanFacesELResolver + + \ No newline at end of file diff --git a/spring-boot-mvc/src/main/webapp/WEB-INF/web.xml b/spring-boot-mvc/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..e0cd4d8850 --- /dev/null +++ b/spring-boot-mvc/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,21 @@ + + + + + Faces Servlet + javax.faces.webapp.FacesServlet + 1 + + + Faces Servlet + *.jsf + + + com.sun.faces.forceLoadConfiguration + true + + \ No newline at end of file diff --git a/spring-boot-mvc/src/main/webapp/index.xhtml b/spring-boot-mvc/src/main/webapp/index.xhtml new file mode 100644 index 0000000000..40042b812c --- /dev/null +++ b/spring-boot-mvc/src/main/webapp/index.xhtml @@ -0,0 +1,20 @@ + + + + + TO-DO application + + +
+

Welcome in the TO-DO application!

+

+ This is a static message rendered from xhtml. + + + +

+
+
+
\ No newline at end of file diff --git a/spring-boot-mvc/src/main/webapp/todo.xhtml b/spring-boot-mvc/src/main/webapp/todo.xhtml new file mode 100644 index 0000000000..426e101908 --- /dev/null +++ b/spring-boot-mvc/src/main/webapp/todo.xhtml @@ -0,0 +1,38 @@ + + + + + TO-DO application + + +
+
+ List of TO-DO items +
+ + + Message + #{item.message} + + + Priority + #{item.priority} + + +
+
+
+ Add new to-do item: +
+ + + + + + + +
+
+
\ No newline at end of file diff --git a/spring-mvc-java/src/test/java/com/baeldung/rss/RssFeedUnitTest.java b/spring-boot-mvc/src/test/java/com/baeldung/rss/RssFeedUnitTest.java similarity index 100% rename from spring-mvc-java/src/test/java/com/baeldung/rss/RssFeedUnitTest.java rename to spring-boot-mvc/src/test/java/com/baeldung/rss/RssFeedUnitTest.java diff --git a/spring-boot-ops/pom.xml b/spring-boot-ops/pom.xml index 223fe72bd5..57779c3f8e 100644 --- a/spring-boot-ops/pom.xml +++ b/spring-boot-ops/pom.xml @@ -3,9 +3,7 @@ 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-boot-ops - 0.0.1-SNAPSHOT war spring-boot-ops Demo project for Spring Boot @@ -19,10 +17,6 @@ - - org.springframework.boot spring-boot-starter-web @@ -95,13 +89,8 @@ - ${artifactId} + ${project.artifactId} - - org.springframework.boot - spring-boot-maven-plugin - - org.springframework.boot spring-boot-maven-plugin diff --git a/spring-boot-persistence/README.MD b/spring-boot-persistence/README.MD index 72fdca74fa..6cf172426a 100644 --- a/spring-boot-persistence/README.MD +++ b/spring-boot-persistence/README.MD @@ -3,3 +3,4 @@ - [Spring Boot with Multiple SQL Import Files](http://www.baeldung.com/spring-boot-sql-import-files) - [Configuring Separate Spring DataSource for Tests](http://www.baeldung.com/spring-testing-separate-data-source) - [Quick Guide on data.sql and schema.sql Files in Spring Boot](http://www.baeldung.com/spring-boot-data-sql-and-schema-sql) +- [Configuring a Tomcat Connection Pool in Spring Boot](https://www.baeldung.com/spring-boot-tomcat-connection-pool) diff --git a/spring-boot-security/pom.xml b/spring-boot-security/pom.xml index 18d292c8a1..6bc013d1ed 100644 --- a/spring-boot-security/pom.xml +++ b/spring-boot-security/pom.xml @@ -2,7 +2,6 @@ 4.0.0 - com.baeldung spring-boot-security 0.0.1-SNAPSHOT jar @@ -28,10 +27,6 @@ - - - - org.springframework.boot spring-boot-starter-security @@ -72,11 +67,6 @@ spring-security-test test - - org.springframework.security - spring-security-test - test - diff --git a/spring-boot/README.MD b/spring-boot/README.MD index f11ea1b7ed..54382992d4 100644 --- a/spring-boot/README.MD +++ b/spring-boot/README.MD @@ -31,3 +31,5 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Spring Shutdown Callbacks](http://www.baeldung.com/spring-shutdown-callbacks) - [Spring Boot Integration Testing with Embedded MongoDB](http://www.baeldung.com/spring-boot-embedded-mongodb) - [Container Configuration in Spring Boot 2](http://www.baeldung.com/embeddedservletcontainercustomizer-configurableembeddedservletcontainer-spring-boot) +- [Introduction to Chaos Monkey](https://www.baeldung.com/spring-boot-chaos-monkey) +- [Spring Component Scanning](https://www.baeldung.com/spring-component-scanning) diff --git a/spring-boot/src/main/java/com/baeldung/properties/ConfigProperties.java b/spring-boot/src/main/java/com/baeldung/properties/ConfigProperties.java new file mode 100644 index 0000000000..863510738b --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/properties/ConfigProperties.java @@ -0,0 +1,117 @@ +package com.baeldung.properties; + +import java.util.List; +import java.util.Map; + +import javax.validation.constraints.Max; +import javax.validation.constraints.Min; +import javax.validation.constraints.Pattern; + +import org.hibernate.validator.constraints.Length; +import org.hibernate.validator.constraints.NotBlank; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.validation.annotation.Validated; + +@Configuration +@PropertySource("classpath:configprops.properties") +@ConfigurationProperties(prefix = "mail") +@Validated +public class ConfigProperties { + + @Validated + public static class Credentials { + + @Length(max = 4, min = 1) + private String authMethod; + private String username; + private String password; + + public String getAuthMethod() { + return authMethod; + } + + public void setAuthMethod(String authMethod) { + this.authMethod = authMethod; + } + + 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; + } + } + + @NotBlank + private String host; + + @Min(1025) + @Max(65536) + private int port; + + @Pattern(regexp = "^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,6}$") + private String from; + + private Credentials credentials; + private List defaultRecipients; + private Map additionalHeaders; + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public String getFrom() { + return from; + } + + public void setFrom(String from) { + this.from = from; + } + + public Credentials getCredentials() { + return credentials; + } + + public void setCredentials(Credentials credentials) { + this.credentials = credentials; + } + + public List getDefaultRecipients() { + return defaultRecipients; + } + + public void setDefaultRecipients(List defaultRecipients) { + this.defaultRecipients = defaultRecipients; + } + + public Map getAdditionalHeaders() { + return additionalHeaders; + } + + public void setAdditionalHeaders(Map additionalHeaders) { + this.additionalHeaders = additionalHeaders; + } +} diff --git a/spring-boot/src/main/java/com/baeldung/properties/ConfigPropertiesDemoApplication.java b/spring-boot/src/main/java/com/baeldung/properties/ConfigPropertiesDemoApplication.java new file mode 100644 index 0000000000..ee9671c755 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/properties/ConfigPropertiesDemoApplication.java @@ -0,0 +1,15 @@ +package com.baeldung.properties; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.annotation.ComponentScan; + +@SpringBootApplication +@ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class }) +public class ConfigPropertiesDemoApplication { + public static void main(String[] args) { + new SpringApplicationBuilder(ConfigPropertiesDemoApplication.class).initializers(new JsonPropertyContextInitializer()) + .run(); + } + +} diff --git a/spring-boot/src/main/java/com/baeldung/properties/CustomJsonProperties.java b/spring-boot/src/main/java/com/baeldung/properties/CustomJsonProperties.java new file mode 100644 index 0000000000..084138ec6f --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/properties/CustomJsonProperties.java @@ -0,0 +1,71 @@ +package com.baeldung.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +@Component +@ConfigurationProperties(prefix = "custom") +public class CustomJsonProperties { + + private String host; + + private int port; + + private boolean resend; + + private Person sender; + + public static class Person { + + private String name; + private String address; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public boolean isResend() { + return resend; + } + + public void setResend(boolean resend) { + this.resend = resend; + } + + public Person getSender() { + return sender; + } + + public void setSender(Person sender) { + this.sender = sender; + } +} diff --git a/spring-boot/src/main/java/com/baeldung/properties/JsonProperties.java b/spring-boot/src/main/java/com/baeldung/properties/JsonProperties.java new file mode 100644 index 0000000000..31b3be14b4 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/properties/JsonProperties.java @@ -0,0 +1,64 @@ +package com.baeldung.properties; + +import java.util.LinkedHashMap; +import java.util.List; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.PropertySource; +import org.springframework.stereotype.Component; + +@Component +@PropertySource(value = "classpath:configprops.json", factory = JsonPropertySourceFactory.class) +@ConfigurationProperties +public class JsonProperties { + + private String host; + + private int port; + + private boolean resend; + + private List topics; + + private LinkedHashMap sender; + + public LinkedHashMap getSender() { + return sender; + } + + public void setSender(LinkedHashMap sender) { + this.sender = sender; + } + + public List getTopics() { + return topics; + } + + public void setTopics(List topics) { + this.topics = topics; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public boolean isResend() { + return resend; + } + + public void setResend(boolean resend) { + this.resend = resend; + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } +} diff --git a/spring-boot/src/main/java/com/baeldung/properties/JsonPropertyContextInitializer.java b/spring-boot/src/main/java/com/baeldung/properties/JsonPropertyContextInitializer.java new file mode 100644 index 0000000000..0aee149123 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/properties/JsonPropertyContextInitializer.java @@ -0,0 +1,68 @@ +package com.baeldung.properties; + +import java.io.IOException; +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.PropertySource; +import org.springframework.core.io.Resource; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class JsonPropertyContextInitializer implements ApplicationContextInitializer { + + private final static String CUSTOM_PREFIX = "custom."; + + @Override + @SuppressWarnings("unchecked") + public void initialize(ConfigurableApplicationContext configurableApplicationContext) { + try { + Resource resource = configurableApplicationContext.getResource("classpath:configprops.json"); + Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class); + Set set = readValue.entrySet(); + List propertySources = convertEntrySet(set, Optional.empty()); + for (PropertySource propertySource : propertySources) { + configurableApplicationContext.getEnvironment() + .getPropertySources() + .addFirst(propertySource); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private static List convertEntrySet(Set entrySet, Optional parentKey) { + return entrySet.stream() + .map((Map.Entry e) -> convertToPropertySourceList(e, parentKey)) + .flatMap(Collection::stream) + .collect(Collectors.toList()); + } + + private static List convertToPropertySourceList(Map.Entry e, Optional parentKey) { + String key = parentKey.map(s -> s + ".") + .orElse("") + (String) e.getKey(); + Object value = e.getValue(); + return covertToPropertySourceList(key, value); + } + + @SuppressWarnings("unchecked") + private static List covertToPropertySourceList(String key, Object value) { + if (value instanceof LinkedHashMap) { + LinkedHashMap map = (LinkedHashMap) value; + Set entrySet = map.entrySet(); + return convertEntrySet(entrySet, Optional.ofNullable(key)); + } + String finalKey = CUSTOM_PREFIX + key; + return Collections.singletonList(new MapPropertySource(finalKey, Collections.singletonMap(finalKey, value))); + } + +} \ No newline at end of file diff --git a/spring-boot/src/main/java/com/baeldung/properties/JsonPropertySourceFactory.java b/spring-boot/src/main/java/com/baeldung/properties/JsonPropertySourceFactory.java new file mode 100644 index 0000000000..c14d3faea5 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/properties/JsonPropertySourceFactory.java @@ -0,0 +1,21 @@ +package com.baeldung.properties; + +import java.io.IOException; +import java.util.Map; + +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.PropertySource; +import org.springframework.core.io.support.EncodedResource; +import org.springframework.core.io.support.PropertySourceFactory; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class JsonPropertySourceFactory implements PropertySourceFactory { + + @Override + public PropertySource createPropertySource(String name, EncodedResource resource) throws IOException { + Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class); + return new MapPropertySource("json-property", readValue); + } + +} \ No newline at end of file diff --git a/spring-boot/src/main/java/org/baeldung/properties/ConfigPropertiesDemoApplication.java b/spring-boot/src/main/java/org/baeldung/properties/ConfigPropertiesDemoApplication.java index cb0304fc41..395d68060b 100644 --- a/spring-boot/src/main/java/org/baeldung/properties/ConfigPropertiesDemoApplication.java +++ b/spring-boot/src/main/java/org/baeldung/properties/ConfigPropertiesDemoApplication.java @@ -1,13 +1,15 @@ package org.baeldung.properties; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.annotation.ComponentScan; -@EnableAutoConfiguration -@ComponentScan(basePackageClasses = ConfigProperties.class) +@SpringBootApplication +@ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class }) public class ConfigPropertiesDemoApplication { public static void main(String[] args) { - SpringApplication.run(ConfigPropertiesDemoApplication.class); + new SpringApplicationBuilder(ConfigPropertiesDemoApplication.class).initializers(new JsonPropertyContextInitializer()) + .run(); } + } diff --git a/spring-boot/src/main/java/org/baeldung/properties/CustomJsonProperties.java b/spring-boot/src/main/java/org/baeldung/properties/CustomJsonProperties.java new file mode 100644 index 0000000000..3fae8a8e98 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/properties/CustomJsonProperties.java @@ -0,0 +1,71 @@ +package org.baeldung.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +@Component +@ConfigurationProperties(prefix = "custom") +public class CustomJsonProperties { + + private String host; + + private int port; + + private boolean resend; + + private Person sender; + + public static class Person { + + private String name; + private String address; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public boolean isResend() { + return resend; + } + + public void setResend(boolean resend) { + this.resend = resend; + } + + public Person getSender() { + return sender; + } + + public void setSender(Person sender) { + this.sender = sender; + } +} diff --git a/spring-boot/src/main/java/org/baeldung/properties/JsonProperties.java b/spring-boot/src/main/java/org/baeldung/properties/JsonProperties.java new file mode 100644 index 0000000000..5c31cd1344 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/properties/JsonProperties.java @@ -0,0 +1,64 @@ +package org.baeldung.properties; + +import java.util.LinkedHashMap; +import java.util.List; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.PropertySource; +import org.springframework.stereotype.Component; + +@Component +@PropertySource(value = "classpath:configprops.json", factory = JsonPropertySourceFactory.class) +@ConfigurationProperties +public class JsonProperties { + + private String host; + + private int port; + + private boolean resend; + + private List topics; + + private LinkedHashMap sender; + + public LinkedHashMap getSender() { + return sender; + } + + public void setSender(LinkedHashMap sender) { + this.sender = sender; + } + + public List getTopics() { + return topics; + } + + public void setTopics(List topics) { + this.topics = topics; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public boolean isResend() { + return resend; + } + + public void setResend(boolean resend) { + this.resend = resend; + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } +} diff --git a/spring-boot/src/main/java/org/baeldung/properties/JsonPropertyContextInitializer.java b/spring-boot/src/main/java/org/baeldung/properties/JsonPropertyContextInitializer.java new file mode 100644 index 0000000000..fd9b3f35a5 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/properties/JsonPropertyContextInitializer.java @@ -0,0 +1,68 @@ +package org.baeldung.properties; + +import java.io.IOException; +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.PropertySource; +import org.springframework.core.io.Resource; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class JsonPropertyContextInitializer implements ApplicationContextInitializer { + + private final static String CUSTOM_PREFIX = "custom."; + + @Override + @SuppressWarnings("unchecked") + public void initialize(ConfigurableApplicationContext configurableApplicationContext) { + try { + Resource resource = configurableApplicationContext.getResource("classpath:configprops.json"); + Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class); + Set set = readValue.entrySet(); + List propertySources = convertEntrySet(set, Optional.empty()); + for (PropertySource propertySource : propertySources) { + configurableApplicationContext.getEnvironment() + .getPropertySources() + .addFirst(propertySource); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private static List convertEntrySet(Set entrySet, Optional parentKey) { + return entrySet.stream() + .map((Map.Entry e) -> convertToPropertySourceList(e, parentKey)) + .flatMap(Collection::stream) + .collect(Collectors.toList()); + } + + private static List convertToPropertySourceList(Map.Entry e, Optional parentKey) { + String key = parentKey.map(s -> s + ".") + .orElse("") + (String) e.getKey(); + Object value = e.getValue(); + return covertToPropertySourceList(key, value); + } + + @SuppressWarnings("unchecked") + private static List covertToPropertySourceList(String key, Object value) { + if (value instanceof LinkedHashMap) { + LinkedHashMap map = (LinkedHashMap) value; + Set entrySet = map.entrySet(); + return convertEntrySet(entrySet, Optional.ofNullable(key)); + } + String finalKey = CUSTOM_PREFIX + key; + return Collections.singletonList(new MapPropertySource(finalKey, Collections.singletonMap(finalKey, value))); + } + +} \ No newline at end of file diff --git a/spring-boot/src/main/java/org/baeldung/properties/JsonPropertySourceFactory.java b/spring-boot/src/main/java/org/baeldung/properties/JsonPropertySourceFactory.java new file mode 100644 index 0000000000..9578179519 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/properties/JsonPropertySourceFactory.java @@ -0,0 +1,21 @@ +package org.baeldung.properties; + +import java.io.IOException; +import java.util.Map; + +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.PropertySource; +import org.springframework.core.io.support.EncodedResource; +import org.springframework.core.io.support.PropertySourceFactory; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class JsonPropertySourceFactory implements PropertySourceFactory { + + @Override + public PropertySource createPropertySource(String name, EncodedResource resource) throws IOException { + Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class); + return new MapPropertySource("json-property", readValue); + } + +} \ No newline at end of file diff --git a/spring-boot/src/main/resources/configprops.json b/spring-boot/src/main/resources/configprops.json new file mode 100644 index 0000000000..1602663775 --- /dev/null +++ b/spring-boot/src/main/resources/configprops.json @@ -0,0 +1,10 @@ +{ + "host" : "mailer@mail.com", + "port" : 9090, + "resend" : true, + "topics" : ["spring", "boot"], + "sender" : { + "name": "sender", + "address": "street" + } +} diff --git a/spring-boot/src/test/java/com/baeldung/properties/ConfigPropertiesIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/properties/ConfigPropertiesIntegrationTest.java new file mode 100644 index 0000000000..3a4b6551b1 --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/properties/ConfigPropertiesIntegrationTest.java @@ -0,0 +1,45 @@ +package com.baeldung.properties; + +import org.baeldung.properties.ConfigProperties; +import org.baeldung.properties.ConfigPropertiesDemoApplication; +import org.junit.Assert; +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.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = ConfigPropertiesDemoApplication.class) +@TestPropertySource("classpath:configprops-test.properties") +public class ConfigPropertiesIntegrationTest { + + @Autowired + private ConfigProperties properties; + + @Test + public void whenSimplePropertyQueriedthenReturnsProperty() throws Exception { + Assert.assertTrue("From address is read as null!", properties.getFrom() != null); + } + + @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); + } + + @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); + } + + @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")); + } +} diff --git a/spring-boot/src/test/java/com/baeldung/properties/JsonPropertiesIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/properties/JsonPropertiesIntegrationTest.java new file mode 100644 index 0000000000..48c551d1dd --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/properties/JsonPropertiesIntegrationTest.java @@ -0,0 +1,63 @@ +package com.baeldung.properties; + +import java.util.Arrays; + +import org.baeldung.properties.ConfigPropertiesDemoApplication; +import org.baeldung.properties.CustomJsonProperties; +import org.baeldung.properties.JsonProperties; +import org.baeldung.properties.JsonPropertyContextInitializer; +import org.hamcrest.Matchers; +import org.junit.Assert; +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.SpringRunner; + +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = ConfigPropertiesDemoApplication.class, initializers = JsonPropertyContextInitializer.class) +public class JsonPropertiesIntegrationTest { + + @Autowired + private JsonProperties jsonProperties; + + @Autowired + private CustomJsonProperties customJsonProperties; + + @Test + public void whenPropertiesLoadedViaJsonPropertySource_thenLoadFlatValues() { + Assert.assertEquals("mailer@mail.com", jsonProperties.getHost()); + Assert.assertEquals(9090, jsonProperties.getPort()); + Assert.assertTrue(jsonProperties.isResend()); + } + + @Test + public void whenPropertiesLoadedViaJsonPropertySource_thenLoadListValues() { + Assert.assertThat(jsonProperties.getTopics(), Matchers.is(Arrays.asList("spring", "boot"))); + } + + @Test + public void whenPropertiesLoadedViaJsonPropertySource_thenNestedLoadedAsMap() { + Assert.assertEquals("sender", jsonProperties.getSender() + .get("name")); + Assert.assertEquals("street", jsonProperties.getSender() + .get("address")); + } + + @Test + public void whenLoadedIntoEnvironment_thenFlatValuesPopulated() { + Assert.assertEquals("mailer@mail.com", customJsonProperties.getHost()); + Assert.assertEquals(9090, customJsonProperties.getPort()); + Assert.assertTrue(customJsonProperties.isResend()); + } + + @Test + public void whenLoadedIntoEnvironment_thenValuesLoadedIntoClassObject() { + Assert.assertNotNull(customJsonProperties.getSender()); + Assert.assertEquals("sender", customJsonProperties.getSender() + .getName()); + Assert.assertEquals("street", customJsonProperties.getSender() + .getAddress()); + } + +} diff --git a/spring-boot/src/test/java/org/baeldung/properties/JsonPropertiesIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/properties/JsonPropertiesIntegrationTest.java new file mode 100644 index 0000000000..e3d4c62953 --- /dev/null +++ b/spring-boot/src/test/java/org/baeldung/properties/JsonPropertiesIntegrationTest.java @@ -0,0 +1,59 @@ +package org.baeldung.properties; + +import java.util.Arrays; + +import org.hamcrest.Matchers; +import org.junit.Assert; +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.SpringRunner; + +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = ConfigPropertiesDemoApplication.class, initializers = JsonPropertyContextInitializer.class) +public class JsonPropertiesIntegrationTest { + + @Autowired + private JsonProperties jsonProperties; + + @Autowired + private CustomJsonProperties customJsonProperties; + + @Test + public void whenPropertiesLoadedViaJsonPropertySource_thenLoadFlatValues() { + Assert.assertEquals("mailer@mail.com", jsonProperties.getHost()); + Assert.assertEquals(9090, jsonProperties.getPort()); + Assert.assertTrue(jsonProperties.isResend()); + } + + @Test + public void whenPropertiesLoadedViaJsonPropertySource_thenLoadListValues() { + Assert.assertThat(jsonProperties.getTopics(), Matchers.is(Arrays.asList("spring", "boot"))); + } + + @Test + public void whenPropertiesLoadedViaJsonPropertySource_thenNestedLoadedAsMap() { + Assert.assertEquals("sender", jsonProperties.getSender() + .get("name")); + Assert.assertEquals("street", jsonProperties.getSender() + .get("address")); + } + + @Test + public void whenLoadedIntoEnvironment_thenFlatValuesPopulated() { + Assert.assertEquals("mailer@mail.com", customJsonProperties.getHost()); + Assert.assertEquals(9090, customJsonProperties.getPort()); + Assert.assertTrue(customJsonProperties.isResend()); + } + + @Test + public void whenLoadedIntoEnvironment_thenValuesLoadedIntoClassObject() { + Assert.assertNotNull(customJsonProperties.getSender()); + Assert.assertEquals("sender", customJsonProperties.getSender() + .getName()); + Assert.assertEquals("street", customJsonProperties.getSender() + .getAddress()); + } + +} diff --git a/spring-cloud/README.md b/spring-cloud/README.md index 31d9ffb684..805052e4db 100644 --- a/spring-cloud/README.md +++ b/spring-cloud/README.md @@ -27,3 +27,4 @@ - [An Intro to Spring Cloud Security](http://www.baeldung.com/spring-cloud-security) - [An Intro to Spring Cloud Task](http://www.baeldung.com/spring-cloud-task) - [Running Spring Boot Applications With Minikube](http://www.baeldung.com/spring-boot-minikube) +- [Introduction to Netflix Archaius with Spring Cloud](https://www.baeldung.com/netflix-archaius-spring-cloud-integration) diff --git a/spring-cloud/pom.xml b/spring-cloud/pom.xml index 1fdab4213c..d0095e9a8a 100644 --- a/spring-cloud/pom.xml +++ b/spring-cloud/pom.xml @@ -25,6 +25,7 @@ spring-cloud-zookeeper spring-cloud-gateway spring-cloud-stream + spring-cloud-stream-starters/twitterhdfs spring-cloud-connectors-heroku spring-cloud-aws spring-cloud-consul @@ -32,6 +33,9 @@ spring-cloud-contract spring-cloud-kubernetes spring-cloud-archaius + spring-cloud-functions + spring-cloud-vault + spring-cloud-security diff --git a/spring-cloud/spring-cloud-archaius/dynamodb-config/pom.xml b/spring-cloud/spring-cloud-archaius/dynamodb-config/pom.xml new file mode 100644 index 0000000000..3ffbf8f619 --- /dev/null +++ b/spring-cloud/spring-cloud-archaius/dynamodb-config/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + dynamodb-config + jar + + + com.baeldung.spring.cloud + spring-cloud-archaius + 1.0.0-SNAPSHOT + .. + + + + + org.springframework.boot + spring-boot-starter-web + + + com.amazonaws + aws-java-sdk-dynamodb + ${aws.sdk.dynamo.version} + + + com.github.derjust + spring-data-dynamodb + ${spring.dynamo.version} + + + com.netflix.archaius + archaius-aws + ${archaius.version} + + + org.projectlombok + lombok + provided + + + + + 1.11.407 + 5.0.3 + 0.7.6 + + diff --git a/spring-cloud/spring-cloud-archaius/dynamodb-config/src/main/java/com/baeldung/spring/cloud/archaius/dynamosources/DynamoSourcesApplication.java b/spring-cloud/spring-cloud-archaius/dynamodb-config/src/main/java/com/baeldung/spring/cloud/archaius/dynamosources/DynamoSourcesApplication.java new file mode 100644 index 0000000000..ee08f2b1fb --- /dev/null +++ b/spring-cloud/spring-cloud-archaius/dynamodb-config/src/main/java/com/baeldung/spring/cloud/archaius/dynamosources/DynamoSourcesApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.spring.cloud.archaius.dynamosources; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DynamoSourcesApplication { + + public static void main(String[] args) { + SpringApplication.run(DynamoSourcesApplication.class, args); + } +} diff --git a/spring-cloud/spring-cloud-archaius/dynamodb-config/src/main/java/com/baeldung/spring/cloud/archaius/dynamosources/config/ApplicationPropertiesConfigurations.java b/spring-cloud/spring-cloud-archaius/dynamodb-config/src/main/java/com/baeldung/spring/cloud/archaius/dynamosources/config/ApplicationPropertiesConfigurations.java new file mode 100644 index 0000000000..7b533e8021 --- /dev/null +++ b/spring-cloud/spring-cloud-archaius/dynamodb-config/src/main/java/com/baeldung/spring/cloud/archaius/dynamosources/config/ApplicationPropertiesConfigurations.java @@ -0,0 +1,52 @@ +package com.baeldung.spring.cloud.archaius.dynamosources.config; + +import java.util.Arrays; + +import org.apache.commons.configuration.AbstractConfiguration; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper; +import com.amazonaws.services.dynamodbv2.model.CreateTableRequest; +import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput; +import com.amazonaws.services.dynamodbv2.util.TableUtils; +import com.baeldung.spring.cloud.archaius.dynamosources.dynamodb.ArchaiusProperties; +import com.baeldung.spring.cloud.archaius.dynamosources.dynamodb.ArchaiusPropertiesRepository; +import com.netflix.config.DynamicConfiguration; +import com.netflix.config.FixedDelayPollingScheduler; +import com.netflix.config.PolledConfigurationSource; +import com.netflix.config.sources.DynamoDbConfigurationSource; + +@Configuration +public class ApplicationPropertiesConfigurations { + + @Autowired + AmazonDynamoDB amazonDynamoDb; + + @Autowired + private ArchaiusPropertiesRepository repository; + + @Bean + public AbstractConfiguration addApplicationPropertiesSource() { + // Normally, the DB Table would be already created and populated. + // In this case, we'll do it just before creating the archaius config source that uses it + initDatabase(); + PolledConfigurationSource source = new DynamoDbConfigurationSource(amazonDynamoDb); + return new DynamicConfiguration(source, new FixedDelayPollingScheduler()); + } + + private void initDatabase() { + // Create the table + DynamoDBMapper mapper = new DynamoDBMapper(amazonDynamoDb); + CreateTableRequest tableRequest = mapper.generateCreateTableRequest(ArchaiusProperties.class); + tableRequest.setProvisionedThroughput(new ProvisionedThroughput(1L, 1L)); + TableUtils.createTableIfNotExists(amazonDynamoDb, tableRequest); + + // Populate the table + ArchaiusProperties property = new ArchaiusProperties("baeldung.archaius.properties.one", "one FROM:dynamoDB"); + ArchaiusProperties property3 = new ArchaiusProperties("baeldung.archaius.properties.three", "three FROM:dynamoDB"); + repository.saveAll(Arrays.asList(property, property3)); + } +} diff --git a/spring-cloud/spring-cloud-archaius/dynamodb-config/src/main/java/com/baeldung/spring/cloud/archaius/dynamosources/config/DynamoDbConfiguration.java b/spring-cloud/spring-cloud-archaius/dynamodb-config/src/main/java/com/baeldung/spring/cloud/archaius/dynamosources/config/DynamoDbConfiguration.java new file mode 100644 index 0000000000..8f12051a7f --- /dev/null +++ b/spring-cloud/spring-cloud-archaius/dynamodb-config/src/main/java/com/baeldung/spring/cloud/archaius/dynamosources/config/DynamoDbConfiguration.java @@ -0,0 +1,46 @@ +package com.baeldung.spring.cloud.archaius.dynamosources.config; + +import org.socialsignin.spring.data.dynamodb.repository.config.EnableDynamoDBRepositories; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.amazonaws.auth.AWSCredentialsProvider; +import com.amazonaws.auth.AWSStaticCredentialsProvider; +import com.amazonaws.auth.BasicAWSCredentials; +import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration; +import com.amazonaws.regions.Regions; +import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; +import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; + +@Configuration +@EnableDynamoDBRepositories(basePackages = "com.baeldung.spring.cloud.archaius.dynamosources.dynamodb") +public class DynamoDbConfiguration { + + @Value("${amazon.dynamodb.endpoint}") + private String amazonDynamoDBEndpoint; + + @Value("${aws.accessKeyId}") + private String amazonDynamoDBAccessKeyId; + + @Value("${aws.secretKey}") + private String amazonDynamoDBSecretKey; + + @Bean + public AmazonDynamoDB amazonDynamoDB() { + AmazonDynamoDB amazonDynamoDB = AmazonDynamoDBClientBuilder.standard() + .withCredentials(amazonAWSCredentials()) + .withEndpointConfiguration(setupEndpointConfiguration()) + .build(); + + return amazonDynamoDB; + } + + private AWSCredentialsProvider amazonAWSCredentials() { + return new AWSStaticCredentialsProvider(new BasicAWSCredentials(amazonDynamoDBAccessKeyId, amazonDynamoDBSecretKey)); + } + + private EndpointConfiguration setupEndpointConfiguration() { + return new EndpointConfiguration(amazonDynamoDBEndpoint, Regions.DEFAULT_REGION.getName()); + } +} diff --git a/spring-cloud/spring-cloud-archaius/dynamodb-config/src/main/java/com/baeldung/spring/cloud/archaius/dynamosources/controller/ConfigPropertiesController.java b/spring-cloud/spring-cloud-archaius/dynamodb-config/src/main/java/com/baeldung/spring/cloud/archaius/dynamosources/controller/ConfigPropertiesController.java new file mode 100644 index 0000000000..2ac5055fe3 --- /dev/null +++ b/spring-cloud/spring-cloud-archaius/dynamodb-config/src/main/java/com/baeldung/spring/cloud/archaius/dynamosources/controller/ConfigPropertiesController.java @@ -0,0 +1,32 @@ +package com.baeldung.spring.cloud.archaius.dynamosources.controller; + +import java.util.HashMap; +import java.util.Map; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.netflix.config.DynamicPropertyFactory; +import com.netflix.config.DynamicStringProperty; + +@RestController +public class ConfigPropertiesController { + + private DynamicStringProperty propertyOneWithDynamic = DynamicPropertyFactory.getInstance() + .getStringProperty("baeldung.archaius.properties.one", "not found!"); + + private DynamicStringProperty propertyTwoWithDynamic = DynamicPropertyFactory.getInstance() + .getStringProperty("baeldung.archaius.properties.two", "not found!"); + + private DynamicStringProperty propertyThreeWithDynamic = DynamicPropertyFactory.getInstance() + .getStringProperty("baeldung.archaius.properties.three", "not found!"); + + @GetMapping("/properties-from-dynamic") + public Map getPropertiesFromDynamic() { + Map properties = new HashMap<>(); + properties.put(propertyOneWithDynamic.getName(), propertyOneWithDynamic.get()); + properties.put(propertyTwoWithDynamic.getName(), propertyTwoWithDynamic.get()); + properties.put(propertyThreeWithDynamic.getName(), propertyThreeWithDynamic.get()); + return properties; + } +} diff --git a/spring-cloud/spring-cloud-archaius/dynamodb-config/src/main/java/com/baeldung/spring/cloud/archaius/dynamosources/dynamodb/ArchaiusProperties.java b/spring-cloud/spring-cloud-archaius/dynamodb-config/src/main/java/com/baeldung/spring/cloud/archaius/dynamosources/dynamodb/ArchaiusProperties.java new file mode 100644 index 0000000000..016f8433cf --- /dev/null +++ b/spring-cloud/spring-cloud-archaius/dynamodb-config/src/main/java/com/baeldung/spring/cloud/archaius/dynamosources/dynamodb/ArchaiusProperties.java @@ -0,0 +1,23 @@ +package com.baeldung.spring.cloud.archaius.dynamosources.dynamodb; + +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute; +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey; +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@AllArgsConstructor +@DynamoDBTable(tableName = "archaiusProperties") +public class ArchaiusProperties { + + @DynamoDBHashKey + @DynamoDBAttribute + private String key; + + @DynamoDBAttribute + private String value; +} diff --git a/spring-cloud/spring-cloud-archaius/dynamodb-config/src/main/java/com/baeldung/spring/cloud/archaius/dynamosources/dynamodb/ArchaiusPropertiesRepository.java b/spring-cloud/spring-cloud-archaius/dynamodb-config/src/main/java/com/baeldung/spring/cloud/archaius/dynamosources/dynamodb/ArchaiusPropertiesRepository.java new file mode 100644 index 0000000000..fbcc953261 --- /dev/null +++ b/spring-cloud/spring-cloud-archaius/dynamodb-config/src/main/java/com/baeldung/spring/cloud/archaius/dynamosources/dynamodb/ArchaiusPropertiesRepository.java @@ -0,0 +1,7 @@ +package com.baeldung.spring.cloud.archaius.dynamosources.dynamodb; + +import org.springframework.data.repository.CrudRepository; + +public interface ArchaiusPropertiesRepository extends CrudRepository { + +} diff --git a/spring-cloud/spring-cloud-archaius/dynamodb-config/src/main/resources/application.properties b/spring-cloud/spring-cloud-archaius/dynamodb-config/src/main/resources/application.properties new file mode 100644 index 0000000000..3823d2685f --- /dev/null +++ b/spring-cloud/spring-cloud-archaius/dynamodb-config/src/main/resources/application.properties @@ -0,0 +1,6 @@ +server.port=8082 +baeldung.archaius.properties.one=one FROM:application.properties +baeldung.archaius.properties.two=two FROM:application.properties +amazon.dynamodb.endpoint=http://localhost:8000/ +aws.accessKeyId=key +aws.secretKey=key2 diff --git a/spring-cloud/spring-cloud-archaius/dynamodb-config/src/test/java/com/baeldung/spring/cloud/archaius/dynamosources/ArchaiusDynamoDbLiveTest.java b/spring-cloud/spring-cloud-archaius/dynamodb-config/src/test/java/com/baeldung/spring/cloud/archaius/dynamosources/ArchaiusDynamoDbLiveTest.java new file mode 100644 index 0000000000..3802cb99cd --- /dev/null +++ b/spring-cloud/spring-cloud-archaius/dynamodb-config/src/test/java/com/baeldung/spring/cloud/archaius/dynamosources/ArchaiusDynamoDbLiveTest.java @@ -0,0 +1,53 @@ +package com.baeldung.spring.cloud.archaius.dynamosources; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.HashMap; +import java.util.Map; + +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.context.ConfigurableApplicationContext; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.HttpMethod; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class ArchaiusDynamoDbLiveTest { + + private static final String BASE_URL = "http://localhost:8082"; + + private static final String DYNAMIC_PROPERTIES_URL = "/properties-from-dynamic"; + private static final Map EXPECTED_ARCHAIUS_PROPERTIES = createExpectedArchaiusProperties(); + + private static Map createExpectedArchaiusProperties() { + Map map = new HashMap<>(); + map.put("baeldung.archaius.properties.one", "one FROM:dynamoDB"); + map.put("baeldung.archaius.properties.two", "two FROM:application.properties"); + map.put("baeldung.archaius.properties.three", "three FROM:dynamoDB"); + return map; + } + + @Autowired + ConfigurableApplicationContext context; + + @Autowired + private TestRestTemplate template; + + private Map exchangeAsMap(String uri, ParameterizedTypeReference> responseType) { + return template.exchange(uri, HttpMethod.GET, null, responseType) + .getBody(); + } + + @Test + public void givenNonDefaultConfigurationFilesSetup_whenRequestProperties_thenEndpointRetrievesValuesInFiles() { + Map initialResponse = this.exchangeAsMap(BASE_URL + DYNAMIC_PROPERTIES_URL, new ParameterizedTypeReference>() { + }); + + assertThat(initialResponse).containsAllEntriesOf(EXPECTED_ARCHAIUS_PROPERTIES); + } +} diff --git a/spring-cloud/spring-cloud-archaius/jdbc-config/pom.xml b/spring-cloud/spring-cloud-archaius/jdbc-config/pom.xml new file mode 100644 index 0000000000..bb283576d8 --- /dev/null +++ b/spring-cloud/spring-cloud-archaius/jdbc-config/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + jdbc-config + jar + + + com.baeldung.spring.cloud + spring-cloud-archaius + 1.0.0-SNAPSHOT + .. + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + runtime + + + diff --git a/spring-cloud/spring-cloud-archaius/jdbc-config/src/main/java/com/baeldung/spring/cloud/archaius/jdbconfig/JdbcSourcesApplication.java b/spring-cloud/spring-cloud-archaius/jdbc-config/src/main/java/com/baeldung/spring/cloud/archaius/jdbconfig/JdbcSourcesApplication.java new file mode 100644 index 0000000000..ee7beec87b --- /dev/null +++ b/spring-cloud/spring-cloud-archaius/jdbc-config/src/main/java/com/baeldung/spring/cloud/archaius/jdbconfig/JdbcSourcesApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.spring.cloud.archaius.jdbconfig; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class JdbcSourcesApplication { + + public static void main(String[] args) { + SpringApplication.run(JdbcSourcesApplication.class, args); + } + +} diff --git a/spring-cloud/spring-cloud-archaius/jdbc-config/src/main/java/com/baeldung/spring/cloud/archaius/jdbconfig/config/ApplicationPropertiesConfigurations.java b/spring-cloud/spring-cloud-archaius/jdbc-config/src/main/java/com/baeldung/spring/cloud/archaius/jdbconfig/config/ApplicationPropertiesConfigurations.java new file mode 100644 index 0000000000..f3eed0c2b5 --- /dev/null +++ b/spring-cloud/spring-cloud-archaius/jdbc-config/src/main/java/com/baeldung/spring/cloud/archaius/jdbconfig/config/ApplicationPropertiesConfigurations.java @@ -0,0 +1,27 @@ +package com.baeldung.spring.cloud.archaius.jdbconfig.config; + +import javax.sql.DataSource; + +import org.apache.commons.configuration.AbstractConfiguration; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.netflix.config.DynamicConfiguration; +import com.netflix.config.FixedDelayPollingScheduler; +import com.netflix.config.PolledConfigurationSource; +import com.netflix.config.sources.JDBCConfigurationSource; + +@Configuration +public class ApplicationPropertiesConfigurations { + + @Autowired + DataSource h2DataSource; + + @Bean + public AbstractConfiguration addApplicationPropertiesSource() { + PolledConfigurationSource source = new JDBCConfigurationSource(h2DataSource, "select distinct key, value from properties", "key", "value"); + return new DynamicConfiguration(source, new FixedDelayPollingScheduler()); + } + +} diff --git a/spring-cloud/spring-cloud-archaius/jdbc-config/src/main/java/com/baeldung/spring/cloud/archaius/jdbconfig/controller/ConfigPropertiesController.java b/spring-cloud/spring-cloud-archaius/jdbc-config/src/main/java/com/baeldung/spring/cloud/archaius/jdbconfig/controller/ConfigPropertiesController.java new file mode 100644 index 0000000000..a793f39b29 --- /dev/null +++ b/spring-cloud/spring-cloud-archaius/jdbc-config/src/main/java/com/baeldung/spring/cloud/archaius/jdbconfig/controller/ConfigPropertiesController.java @@ -0,0 +1,32 @@ +package com.baeldung.spring.cloud.archaius.jdbconfig.controller; + +import java.util.HashMap; +import java.util.Map; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.netflix.config.DynamicPropertyFactory; +import com.netflix.config.DynamicStringProperty; + +@RestController +public class ConfigPropertiesController { + + private DynamicStringProperty propertyOneWithDynamic = DynamicPropertyFactory.getInstance() + .getStringProperty("baeldung.archaius.properties.one", "not found!"); + + private DynamicStringProperty propertyTwoWithDynamic = DynamicPropertyFactory.getInstance() + .getStringProperty("baeldung.archaius.properties.two", "not found!"); + + private DynamicStringProperty propertyThreeWithDynamic = DynamicPropertyFactory.getInstance() + .getStringProperty("baeldung.archaius.properties.three", "not found!"); + + @GetMapping("/properties-from-dynamic") + public Map getPropertiesFromDynamic() { + Map properties = new HashMap<>(); + properties.put(propertyOneWithDynamic.getName(), propertyOneWithDynamic.get()); + properties.put(propertyTwoWithDynamic.getName(), propertyTwoWithDynamic.get()); + properties.put(propertyThreeWithDynamic.getName(), propertyThreeWithDynamic.get()); + return properties; + } +} diff --git a/spring-cloud/spring-cloud-archaius/jdbc-config/src/main/java/com/baeldung/spring/cloud/archaius/jdbconfig/jdbc/Properties.java b/spring-cloud/spring-cloud-archaius/jdbc-config/src/main/java/com/baeldung/spring/cloud/archaius/jdbconfig/jdbc/Properties.java new file mode 100644 index 0000000000..cc2783b506 --- /dev/null +++ b/spring-cloud/spring-cloud-archaius/jdbc-config/src/main/java/com/baeldung/spring/cloud/archaius/jdbconfig/jdbc/Properties.java @@ -0,0 +1,14 @@ +package com.baeldung.spring.cloud.archaius.jdbconfig.jdbc; + +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +public class Properties { + + @Id + private String key; + + @SuppressWarnings("unused") + private String value; +} diff --git a/spring-cloud/spring-cloud-archaius/jdbc-config/src/main/resources/application.properties b/spring-cloud/spring-cloud-archaius/jdbc-config/src/main/resources/application.properties new file mode 100644 index 0000000000..eca873f897 --- /dev/null +++ b/spring-cloud/spring-cloud-archaius/jdbc-config/src/main/resources/application.properties @@ -0,0 +1,4 @@ +server.port=8082 +baeldung.archaius.properties.one=one FROM:application.properties +baeldung.archaius.properties.two=two FROM:application.properties +spring.h2.console.enabled=true diff --git a/spring-cloud/spring-cloud-archaius/jdbc-config/src/main/resources/data.sql b/spring-cloud/spring-cloud-archaius/jdbc-config/src/main/resources/data.sql new file mode 100644 index 0000000000..eb80126d48 --- /dev/null +++ b/spring-cloud/spring-cloud-archaius/jdbc-config/src/main/resources/data.sql @@ -0,0 +1,5 @@ +insert into properties +values('baeldung.archaius.properties.one', 'one FROM:jdbc_source'); + +insert into properties +values('baeldung.archaius.properties.three', 'three FROM:jdbc_source'); \ No newline at end of file diff --git a/spring-cloud/spring-cloud-archaius/jdbc-config/src/test/java/com/baeldung/spring/cloud/archaius/jdbconfig/ArchaiusJDBCSourceLiveTest.java b/spring-cloud/spring-cloud-archaius/jdbc-config/src/test/java/com/baeldung/spring/cloud/archaius/jdbconfig/ArchaiusJDBCSourceLiveTest.java new file mode 100644 index 0000000000..91d5f5754e --- /dev/null +++ b/spring-cloud/spring-cloud-archaius/jdbc-config/src/test/java/com/baeldung/spring/cloud/archaius/jdbconfig/ArchaiusJDBCSourceLiveTest.java @@ -0,0 +1,53 @@ +package com.baeldung.spring.cloud.archaius.jdbconfig; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.HashMap; +import java.util.Map; + +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.context.ConfigurableApplicationContext; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.HttpMethod; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class ArchaiusJDBCSourceLiveTest { + + private static final String BASE_URL = "http://localhost:8082"; + + private static final String DYNAMIC_PROPERTIES_URL = "/properties-from-dynamic"; + private static final Map EXPECTED_ARCHAIUS_PROPERTIES = createExpectedArchaiusProperties(); + + private static Map createExpectedArchaiusProperties() { + Map map = new HashMap<>(); + map.put("baeldung.archaius.properties.one", "one FROM:jdbc_source"); + map.put("baeldung.archaius.properties.two", "two FROM:application.properties"); + map.put("baeldung.archaius.properties.three", "three FROM:jdbc_source"); + return map; + } + + @Autowired + ConfigurableApplicationContext context; + + @Autowired + private TestRestTemplate template; + + private Map exchangeAsMap(String uri, ParameterizedTypeReference> responseType) { + return template.exchange(uri, HttpMethod.GET, null, responseType) + .getBody(); + } + + @Test + public void givenNonDefaultConfigurationFilesSetup_whenRequestProperties_thenEndpointRetrievesValuesInFiles() { + Map initialResponse = this.exchangeAsMap(BASE_URL + DYNAMIC_PROPERTIES_URL, new ParameterizedTypeReference>() { + }); + + assertThat(initialResponse).containsAllEntriesOf(EXPECTED_ARCHAIUS_PROPERTIES); + } +} diff --git a/spring-cloud/spring-cloud-archaius/pom.xml b/spring-cloud/spring-cloud-archaius/pom.xml index cd102f86cd..fd3e34e463 100644 --- a/spring-cloud/spring-cloud-archaius/pom.xml +++ b/spring-cloud/spring-cloud-archaius/pom.xml @@ -21,6 +21,9 @@ basic-config additional-sources-simple extra-configs + jdbc-config + dynamodb-config + zookeeper-config diff --git a/spring-cloud/spring-cloud-archaius/zookeeper-config/pom.xml b/spring-cloud/spring-cloud-archaius/zookeeper-config/pom.xml new file mode 100644 index 0000000000..8f8ec99ad8 --- /dev/null +++ b/spring-cloud/spring-cloud-archaius/zookeeper-config/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + zookeeper-config + jar + + + com.baeldung.spring.cloud + spring-cloud-archaius + 1.0.0-SNAPSHOT + .. + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.cloud + spring-cloud-starter-zookeeper-config + ${cloud.zookeeper.version} + + + org.apache.zookeeper + zookeeper + + + + + org.apache.zookeeper + zookeeper + ${zookeeper.version} + + + org.slf4j + slf4j-log4j12 + + + + + + + 2.0.0.RELEASE + 3.4.13 + + diff --git a/spring-cloud/spring-cloud-archaius/zookeeper-config/src/main/java/com/baeldung/spring/cloud/archaius/zookeeperconfig/ZookeeperConfigApplication.java b/spring-cloud/spring-cloud-archaius/zookeeper-config/src/main/java/com/baeldung/spring/cloud/archaius/zookeeperconfig/ZookeeperConfigApplication.java new file mode 100644 index 0000000000..12119e3e29 --- /dev/null +++ b/spring-cloud/spring-cloud-archaius/zookeeper-config/src/main/java/com/baeldung/spring/cloud/archaius/zookeeperconfig/ZookeeperConfigApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.spring.cloud.archaius.zookeeperconfig; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ZookeeperConfigApplication { + + public static void main(String[] args) { + SpringApplication.run(ZookeeperConfigApplication.class, args); + } +} diff --git a/spring-cloud/spring-cloud-archaius/zookeeper-config/src/main/java/com/baeldung/spring/cloud/archaius/zookeeperconfig/config/ZookeeperConfigsInitializer.java b/spring-cloud/spring-cloud-archaius/zookeeper-config/src/main/java/com/baeldung/spring/cloud/archaius/zookeeperconfig/config/ZookeeperConfigsInitializer.java new file mode 100644 index 0000000000..59ef10aaf7 --- /dev/null +++ b/spring-cloud/spring-cloud-archaius/zookeeper-config/src/main/java/com/baeldung/spring/cloud/archaius/zookeeperconfig/config/ZookeeperConfigsInitializer.java @@ -0,0 +1,58 @@ +package com.baeldung.spring.cloud.archaius.zookeeperconfig.config; + +import org.apache.curator.framework.CuratorFramework; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.context.event.EventListener; +import org.springframework.stereotype.Component; + +/** + * + * Ideally, we wouldn't need to initialize the zookeeper config values. + * Here we do it to verify that configurations are being retrieved. + * + */ +@Component +public class ZookeeperConfigsInitializer { + + private static final String CONFIG_BASE_NODE_PATH = "/config"; + private static final String APPLICATION_BASE_NODE_PATH = CONFIG_BASE_NODE_PATH + "/application"; + + @Autowired + CuratorFramework client; + + @EventListener + public void appReady(ApplicationReadyEvent event) throws Exception { + String pathOne = APPLICATION_BASE_NODE_PATH + "/baeldung.archaius.properties.one"; + String valueOne = "one FROM:zookeeper"; + String pathThree = APPLICATION_BASE_NODE_PATH + "/baeldung.archaius.properties.three"; + String valueThree = "three FROM:zookeeper"; + createBaseNodes(); + setValue(pathOne, valueOne); + setValue(pathThree, valueThree); + } + + private void setValue(String path, String value) throws Exception { + if (client.checkExists() + .forPath(path) == null) { + client.create() + .forPath(path, value.getBytes()); + } else { + client.setData() + .forPath(path, value.getBytes()); + } + } + + private void createBaseNodes() throws Exception { + if (client.checkExists() + .forPath(CONFIG_BASE_NODE_PATH) == null) { + client.create() + .forPath(CONFIG_BASE_NODE_PATH); + } + if (client.checkExists() + .forPath(APPLICATION_BASE_NODE_PATH) == null) { + client.create() + .forPath(APPLICATION_BASE_NODE_PATH); + } + } +} diff --git a/spring-cloud/spring-cloud-archaius/zookeeper-config/src/main/java/com/baeldung/spring/cloud/archaius/zookeeperconfig/controller/ConfigPropertiesController.java b/spring-cloud/spring-cloud-archaius/zookeeper-config/src/main/java/com/baeldung/spring/cloud/archaius/zookeeperconfig/controller/ConfigPropertiesController.java new file mode 100644 index 0000000000..c8a5e53ee4 --- /dev/null +++ b/spring-cloud/spring-cloud-archaius/zookeeper-config/src/main/java/com/baeldung/spring/cloud/archaius/zookeeperconfig/controller/ConfigPropertiesController.java @@ -0,0 +1,51 @@ +package com.baeldung.spring.cloud.archaius.zookeeperconfig.controller; + +import java.util.HashMap; +import java.util.Map; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.netflix.config.DynamicPropertyFactory; +import com.netflix.config.DynamicStringProperty; + +@RestController +public class ConfigPropertiesController { + + @Value("${baeldung.archaius.properties.one:not found!}") + private String propertyOneWithValue; + + @Value("${baeldung.archaius.properties.two:not found!}") + private String propertyTwoWithValue; + + @Value("${baeldung.archaius.properties.three:not found!}") + private String propertyThreeWithValue; + + private DynamicStringProperty propertyOneWithDynamic = DynamicPropertyFactory.getInstance() + .getStringProperty("baeldung.archaius.properties.one", "not found!"); + + private DynamicStringProperty propertyTwoWithDynamic = DynamicPropertyFactory.getInstance() + .getStringProperty("baeldung.archaius.properties.two", "not found!"); + + private DynamicStringProperty propertyThreeWithDynamic = DynamicPropertyFactory.getInstance() + .getStringProperty("baeldung.archaius.properties.three", "not found!"); + + @GetMapping("/properties-from-dynamic") + public Map getPropertiesFromDynamic() { + Map properties = new HashMap<>(); + properties.put(propertyOneWithDynamic.getName(), propertyOneWithDynamic.get()); + properties.put(propertyTwoWithDynamic.getName(), propertyTwoWithDynamic.get()); + properties.put(propertyThreeWithDynamic.getName(), propertyThreeWithDynamic.get()); + return properties; + } + + @GetMapping("/properties-from-value") + public Map getPropertiesFromValue() { + Map properties = new HashMap<>(); + properties.put("baeldung.archaius.properties.one", propertyOneWithValue); + properties.put("baeldung.archaius.properties.two", propertyTwoWithValue); + properties.put("baeldung.archaius.properties.three", propertyThreeWithValue); + return properties; + } +} diff --git a/spring-cloud/spring-cloud-archaius/zookeeper-config/src/main/resources/application.properties b/spring-cloud/spring-cloud-archaius/zookeeper-config/src/main/resources/application.properties new file mode 100644 index 0000000000..91ad18a932 --- /dev/null +++ b/spring-cloud/spring-cloud-archaius/zookeeper-config/src/main/resources/application.properties @@ -0,0 +1,4 @@ +server.port=8082 +baeldung.archaius.properties.one=one FROM:application.properties +baeldung.archaius.properties.two=two FROM:application.properties +spring.application.name=zookeeper-config diff --git a/spring-cloud/spring-cloud-archaius/zookeeper-config/src/test/java/com/baeldung/spring/cloud/archaius/zookeeperconfig/ArchaiusZookeeperLiveTest.java b/spring-cloud/spring-cloud-archaius/zookeeper-config/src/test/java/com/baeldung/spring/cloud/archaius/zookeeperconfig/ArchaiusZookeeperLiveTest.java new file mode 100644 index 0000000000..ff4358cb8f --- /dev/null +++ b/spring-cloud/spring-cloud-archaius/zookeeper-config/src/test/java/com/baeldung/spring/cloud/archaius/zookeeperconfig/ArchaiusZookeeperLiveTest.java @@ -0,0 +1,53 @@ +package com.baeldung.spring.cloud.archaius.zookeeperconfig; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.HashMap; +import java.util.Map; + +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.context.ConfigurableApplicationContext; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.HttpMethod; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class ArchaiusZookeeperLiveTest { + + private static final String BASE_URL = "http://localhost:8082"; + + private static final String DYNAMIC_PROPERTIES_URL = "/properties-from-dynamic"; + private static final Map EXPECTED_ARCHAIUS_PROPERTIES = createExpectedArchaiusProperties(); + + private static Map createExpectedArchaiusProperties() { + Map map = new HashMap<>(); + map.put("baeldung.archaius.properties.one", "one FROM:zookeeper"); + map.put("baeldung.archaius.properties.two", "two FROM:application.properties"); + map.put("baeldung.archaius.properties.three", "three FROM:zookeeper"); + return map; + } + + @Autowired + ConfigurableApplicationContext context; + + @Autowired + private TestRestTemplate template; + + private Map exchangeAsMap(String uri, ParameterizedTypeReference> responseType) { + return template.exchange(uri, HttpMethod.GET, null, responseType) + .getBody(); + } + + @Test + public void givenNonDefaultConfigurationFilesSetup_whenRequestProperties_thenEndpointRetrievesValuesInFiles() { + Map initialResponse = this.exchangeAsMap(BASE_URL + DYNAMIC_PROPERTIES_URL, new ParameterizedTypeReference>() { + }); + + assertThat(initialResponse).containsAllEntriesOf(EXPECTED_ARCHAIUS_PROPERTIES); + } +} diff --git a/spring-cloud/spring-cloud-functions/README.MD b/spring-cloud/spring-cloud-functions/README.MD new file mode 100644 index 0000000000..c766dd1dc6 --- /dev/null +++ b/spring-cloud/spring-cloud-functions/README.MD @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Serverless Functions with Spring Cloud Function](https://www.baeldung.com/spring-cloud-function) diff --git a/spring-cloud/spring-cloud-functions/pom.xml b/spring-cloud/spring-cloud-functions/pom.xml new file mode 100644 index 0000000000..8b2b0ad385 --- /dev/null +++ b/spring-cloud/spring-cloud-functions/pom.xml @@ -0,0 +1,94 @@ + + + 4.0.0 + + com.baeldung.spring + cloudfunction-aws + 0.0.1-SNAPSHOT + jar + + cloudfunction-aws + Demo project for Spring Cloud Function + + + org.springframework.boot + spring-boot-starter-parent + 2.0.4.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + 1.0.1.RELEASE + 2.0.2 + + + + + org.springframework.cloud + spring-cloud-function-adapter-aws + ${spring-cloud-function.version} + + + + + org.springframework.cloud + spring-cloud-starter-function-web + 1.0.1.RELEASE + + + com.amazonaws + aws-lambda-java-events + ${aws-lambda-events.version} + provided + + + com.amazonaws + aws-lambda-java-core + 1.1.0 + provided + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.apache.maven.plugins + maven-deploy-plugin + + true + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.springframework.boot.experimental + spring-boot-thin-layout + 1.0.10.RELEASE + + + + + org.apache.maven.plugins + maven-shade-plugin + + false + true + aws + + + + + + diff --git a/spring-cloud/spring-cloud-functions/src/main/java/com/baeldung/spring/cloudfunction/aws/CloudFunctionAwsApplication.java b/spring-cloud/spring-cloud-functions/src/main/java/com/baeldung/spring/cloudfunction/aws/CloudFunctionAwsApplication.java new file mode 100644 index 0000000000..34e97d3b6c --- /dev/null +++ b/spring-cloud/spring-cloud-functions/src/main/java/com/baeldung/spring/cloudfunction/aws/CloudFunctionAwsApplication.java @@ -0,0 +1,23 @@ +package com.baeldung.spring.cloudfunction.aws; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Supplier; + +@SpringBootApplication +public class CloudFunctionAwsApplication { + + public static void main(String[] args) { + SpringApplication.run(CloudFunctionAwsApplication.class, args); + } + + @Bean + public Function reverseString() { + return value -> new StringBuilder(value).reverse().toString(); + } + +} diff --git a/spring-cloud/spring-cloud-functions/src/main/java/com/baeldung/spring/cloudfunction/aws/StringReverseHandler.java b/spring-cloud/spring-cloud-functions/src/main/java/com/baeldung/spring/cloudfunction/aws/StringReverseHandler.java new file mode 100644 index 0000000000..52514dcd12 --- /dev/null +++ b/spring-cloud/spring-cloud-functions/src/main/java/com/baeldung/spring/cloudfunction/aws/StringReverseHandler.java @@ -0,0 +1,7 @@ +package com.baeldung.spring.cloudfunction.aws; + +import org.springframework.cloud.function.adapter.aws.SpringBootRequestHandler; + +public class StringReverseHandler extends SpringBootRequestHandler { + +} diff --git a/spring-cloud/spring-cloud-functions/src/main/java/com/baeldung/spring/cloudfunction/aws/functions/Greeter.java b/spring-cloud/spring-cloud-functions/src/main/java/com/baeldung/spring/cloudfunction/aws/functions/Greeter.java new file mode 100644 index 0000000000..c443b98c18 --- /dev/null +++ b/spring-cloud/spring-cloud-functions/src/main/java/com/baeldung/spring/cloudfunction/aws/functions/Greeter.java @@ -0,0 +1,11 @@ +package com.baeldung.spring.cloudfunction.functions.aws; + +import java.util.function.Function; + +public class Greeter implements Function { + + @Override + public String apply(String s) { + return "Hello " + s + ", and welcome to Spring Cloud Function!!!"; + } +} diff --git a/spring-cloud/spring-cloud-functions/src/main/resources/application.properties b/spring-cloud/spring-cloud-functions/src/main/resources/application.properties new file mode 100644 index 0000000000..b445bfa4ed --- /dev/null +++ b/spring-cloud/spring-cloud-functions/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.cloud.function.scan.packages: com.baeldung.spring.cloudfunction.functions.aws \ No newline at end of file diff --git a/spring-cloud/spring-cloud-functions/src/test/java/com/baeldung/spring/cloudfunction/aws/CloudFunctionApplicationTests.java b/spring-cloud/spring-cloud-functions/src/test/java/com/baeldung/spring/cloudfunction/aws/CloudFunctionApplicationTests.java new file mode 100644 index 0000000000..6039debe3f --- /dev/null +++ b/spring-cloud/spring-cloud-functions/src/test/java/com/baeldung/spring/cloudfunction/aws/CloudFunctionApplicationTests.java @@ -0,0 +1,33 @@ +package com.baeldung.spring.cloudfunction.aws; + +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.test.context.junit4.SpringRunner; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.web.server.LocalServerPort; + +import static org.assertj.core.api.Java6Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class CloudFunctionApplicationTests { + + @LocalServerPort + private int port; + + @Autowired + private TestRestTemplate testRestTemplate; + + @Test + public void givenAString_whenReverseStringCloudFunctionInvoked_thenStringIsReversed() { + assertThat(this.testRestTemplate.getForObject("http://localhost:" + port + "/reverseString/HelloWorld", String.class)).isEqualTo("dlroWolleH"); + } + + @Test + public void givenAString_whenGreeterCloudFunctionInvoked_thenPrintsGreeting() { + assertThat(this.testRestTemplate.getForObject("http://localhost:" + port + "/greeter/BaeldungUser", String.class)).isEqualTo("Hello BaeldungUser, and welcome to Spring Cloud Function!!!"); + } + +} diff --git a/spring-cloud/spring-cloud-ribbon-client/pom.xml b/spring-cloud/spring-cloud-ribbon-client/pom.xml index fd69dbe043..12125f4cf3 100644 --- a/spring-cloud/spring-cloud-ribbon-client/pom.xml +++ b/spring-cloud/spring-cloud-ribbon-client/pom.xml @@ -2,7 +2,7 @@ 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-ribbon + spring-cloud-ribbon-client 0.0.1-SNAPSHOT jar spring-cloud-ribbon-client diff --git a/spring-cloud/spring-cloud-security/README.md b/spring-cloud/spring-cloud-security/README.md deleted file mode 100644 index 39af52c077..0000000000 --- a/spring-cloud/spring-cloud-security/README.md +++ /dev/null @@ -1,29 +0,0 @@ -# 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/auth-client/pom.xml b/spring-cloud/spring-cloud-security/auth-client/pom.xml index f2d6308374..340c276c2d 100644 --- a/spring-cloud/spring-cloud-security/auth-client/pom.xml +++ b/spring-cloud/spring-cloud-security/auth-client/pom.xml @@ -2,18 +2,15 @@ 4.0.0 - com.baeldung auth-client - 0.0.1-SNAPSHOT jar auth-client - Demo project for Spring Boot - + Spring Cloud Security APP Client Module + - parent-boot-1 + spring-cloud-security com.baeldung - 0.0.1-SNAPSHOT - ../../../parent-boot-1 + 1.0.0-SNAPSHOT @@ -88,9 +85,6 @@ - UTF-8 - UTF-8 - 1.8 2.1.0 Dalston.SR4 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/Springoath2ApplicationIntegrationTest.java similarity index 87% rename from spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationTests.java rename to spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationIntegrationTest.java index ca89575ee0..37cff095db 100644 --- 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/Springoath2ApplicationIntegrationTest.java @@ -9,7 +9,7 @@ import com.baeldung.CloudSite; @RunWith(SpringRunner.class) @SpringBootTest(classes = CloudSite.class) -public class Springoath2ApplicationTests { +public class Springoath2ApplicationIntegrationTest { @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 index 0115259108..09474b2a4d 100644 --- a/spring-cloud/spring-cloud-security/auth-resource/pom.xml +++ b/spring-cloud/spring-cloud-security/auth-resource/pom.xml @@ -3,21 +3,18 @@ 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 auth-resource - 0.0.1-SNAPSHOT jar auth-resource - Demo project for Spring Boot + Spring Cloud Security APP Resource Module - parent-boot-1 + spring-cloud-security com.baeldung - 0.0.1-SNAPSHOT - ../../../parent-boot-1 + 1.0.0-SNAPSHOT - + org.springframework.security.oauth @@ -60,9 +57,6 @@ - UTF-8 - UTF-8 - 1.8 Edgware.RELEASE 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/PersonserviceApplicationIntegrationTest.java similarity index 85% rename from spring-cloud/spring-cloud-security/auth-resource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationTests.java rename to spring-cloud/spring-cloud-security/auth-resource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationIntegrationTest.java index e0fe7006d9..3caa06ba4d 100644 --- 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/PersonserviceApplicationIntegrationTest.java @@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest -public class PersonserviceApplicationTests { +public class PersonserviceApplicationIntegrationTest { @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 index b4367935e3..92f92808f6 100644 --- a/spring-cloud/spring-cloud-security/auth-server/pom.xml +++ b/spring-cloud/spring-cloud-security/auth-server/pom.xml @@ -2,15 +2,13 @@ 4.0.0 - com.baeldung auth-server - 0.0.1-SNAPSHOT - + Spring Cloud Security APP Server Module + - parent-boot-1 + spring-cloud-security com.baeldung - 0.0.1-SNAPSHOT - ../../../parent-boot-1 + 1.0.0-SNAPSHOT diff --git a/spring-cloud/spring-cloud-security/pom.xml b/spring-cloud/spring-cloud-security/pom.xml new file mode 100644 index 0000000000..1cf8751548 --- /dev/null +++ b/spring-cloud/spring-cloud-security/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + spring-cloud-security + pom + 1.0.0-SNAPSHOT + spring-cloud-security + + + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../../parent-boot-1 + + + + auth-client + auth-resource + auth-server + + + diff --git a/spring-cloud/spring-cloud-stream-starters/boot/.gitignore b/spring-cloud/spring-cloud-stream-starters/twitterhdfs/.gitignore similarity index 100% rename from spring-cloud/spring-cloud-stream-starters/boot/.gitignore rename to spring-cloud/spring-cloud-stream-starters/twitterhdfs/.gitignore diff --git a/spring-cloud/spring-cloud-stream-starters/boot/pom.xml b/spring-cloud/spring-cloud-stream-starters/twitterhdfs/pom.xml similarity index 100% rename from spring-cloud/spring-cloud-stream-starters/boot/pom.xml rename to spring-cloud/spring-cloud-stream-starters/twitterhdfs/pom.xml 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/twitterhdfs/src/main/java/com/baeldung/twitterhdfs/aggregate/AggregateApp.java similarity index 100% rename from spring-cloud/spring-cloud-stream-starters/boot/src/main/java/com/baeldung/twitterhdfs/aggregate/AggregateApp.java rename to spring-cloud/spring-cloud-stream-starters/twitterhdfs/src/main/java/com/baeldung/twitterhdfs/aggregate/AggregateApp.java 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/twitterhdfs/src/main/java/com/baeldung/twitterhdfs/processor/ProcessorApp.java similarity index 100% rename from spring-cloud/spring-cloud-stream-starters/boot/src/main/java/com/baeldung/twitterhdfs/processor/ProcessorApp.java rename to spring-cloud/spring-cloud-stream-starters/twitterhdfs/src/main/java/com/baeldung/twitterhdfs/processor/ProcessorApp.java 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/twitterhdfs/src/main/java/com/baeldung/twitterhdfs/sink/SinkApp.java similarity index 100% rename from spring-cloud/spring-cloud-stream-starters/boot/src/main/java/com/baeldung/twitterhdfs/sink/SinkApp.java rename to spring-cloud/spring-cloud-stream-starters/twitterhdfs/src/main/java/com/baeldung/twitterhdfs/sink/SinkApp.java 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/twitterhdfs/src/main/java/com/baeldung/twitterhdfs/source/SourceApp.java similarity index 100% rename from spring-cloud/spring-cloud-stream-starters/boot/src/main/java/com/baeldung/twitterhdfs/source/SourceApp.java rename to spring-cloud/spring-cloud-stream-starters/twitterhdfs/src/main/java/com/baeldung/twitterhdfs/source/SourceApp.java diff --git a/spring-cloud/spring-cloud-stream-starters/boot/src/main/resources/application.properties b/spring-cloud/spring-cloud-stream-starters/twitterhdfs/src/main/resources/application.properties similarity index 100% rename from spring-cloud/spring-cloud-stream-starters/boot/src/main/resources/application.properties rename to spring-cloud/spring-cloud-stream-starters/twitterhdfs/src/main/resources/application.properties diff --git a/jsonb/src/main/resources/logback.xml b/spring-cloud/spring-cloud-stream-starters/twitterhdfs/src/main/resources/logback.xml similarity index 100% rename from jsonb/src/main/resources/logback.xml rename to spring-cloud/spring-cloud-stream-starters/twitterhdfs/src/main/resources/logback.xml diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/pom.xml b/spring-cloud/spring-cloud-task/springcloudtaskbatch/pom.xml index afe609288d..f97d78099f 100644 --- a/spring-cloud/spring-cloud-task/springcloudtaskbatch/pom.xml +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/pom.xml @@ -2,8 +2,9 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.baeldung.cloud - springcloudtask + springcloudtaskbatch 0.0.1-SNAPSHOT + springcloudtaskbatch parent-boot-1 diff --git a/spring-cloud/spring-cloud-vault/.gitignore b/spring-cloud/spring-cloud-vault/.gitignore new file mode 100644 index 0000000000..e6237b6f81 --- /dev/null +++ b/spring-cloud/spring-cloud-vault/.gitignore @@ -0,0 +1,29 @@ +/target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/build/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +## Extra +/vault-data/ +*.log diff --git a/spring-cloud/spring-cloud-vault/database-setup.sql b/spring-cloud/spring-cloud-vault/database-setup.sql new file mode 100644 index 0000000000..4bb1293f74 --- /dev/null +++ b/spring-cloud/spring-cloud-vault/database-setup.sql @@ -0,0 +1,20 @@ +-- +-- Sample schema for testing vault database secrets +-- +create schema fakebank; +use fakebank; +create table account( + id decimal(16,0), + name varchar(30), + branch_id decimal(16,0), + customer_id decimal(16,0), + primary key (id)); + +-- +-- MySQL user that will be used by Vault to create other users on demand +-- +create user 'fakebank-admin'@'%' identified by 'Sup&rSecre7!' +grant all privileges on fakebank.* to 'fakebank-admin'@'%' with grant option; +grant create user on *.* to 'fakebank-admin' with grant option; + +flush privileges; diff --git a/spring-cloud/spring-cloud-vault/pom.xml b/spring-cloud/spring-cloud-vault/pom.xml new file mode 100644 index 0000000000..68b8e44875 --- /dev/null +++ b/spring-cloud/spring-cloud-vault/pom.xml @@ -0,0 +1,87 @@ + + + 4.0.0 + + org.baeldung.spring.cloud + spring-cloud-vault + jar + + spring-cloud-vault + Demo project for Spring Boot + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + + UTF-8 + UTF-8 + 1.8 + Finchley.SR1 + + + + + + + + + org.springframework.cloud + spring-cloud-starter-vault-config + + + + org.springframework.cloud + spring-cloud-vault-config-databases + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + mysql + mysql-connector-java + + + + + org.springframework.boot + spring-boot-starter-jdbc + + + + + + + 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-vault/sample-policy.hcl b/spring-cloud/spring-cloud-vault/sample-policy.hcl new file mode 100644 index 0000000000..554888661e --- /dev/null +++ b/spring-cloud/spring-cloud-vault/sample-policy.hcl @@ -0,0 +1,6 @@ +path "secret/fakebank" { + capabilities = ["read"] + allowed_parameters = { + "api_key" = [] + } +} diff --git a/spring-cloud/spring-cloud-vault/src/main/java/org/baeldung/spring/cloud/vaultsample/VaultSampleApplication.java b/spring-cloud/spring-cloud-vault/src/main/java/org/baeldung/spring/cloud/vaultsample/VaultSampleApplication.java new file mode 100644 index 0000000000..81ece1ca4c --- /dev/null +++ b/spring-cloud/spring-cloud-vault/src/main/java/org/baeldung/spring/cloud/vaultsample/VaultSampleApplication.java @@ -0,0 +1,12 @@ +package org.baeldung.spring.cloud.vaultsample; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class VaultSampleApplication { + + public static void main(String[] args) { + SpringApplication.run(VaultSampleApplication.class, args); + } +} diff --git a/spring-cloud/spring-cloud-vault/src/main/resources/application.yml b/spring-cloud/spring-cloud-vault/src/main/resources/application.yml new file mode 100644 index 0000000000..3d347ec855 --- /dev/null +++ b/spring-cloud/spring-cloud-vault/src/main/resources/application.yml @@ -0,0 +1,6 @@ +spring: + application: + name: fakebank + + datasource: + url: jdbc:mysql://localhost:3306/fakebank diff --git a/spring-cloud/spring-cloud-vault/src/main/resources/bootstrap.yml b/spring-cloud/spring-cloud-vault/src/main/resources/bootstrap.yml new file mode 100644 index 0000000000..1e837c4920 --- /dev/null +++ b/spring-cloud/spring-cloud-vault/src/main/resources/bootstrap.yml @@ -0,0 +1,37 @@ +spring: + cloud: + vault: + uri: https://localhost:8200 + connection-timeout: 5000 + read-timeout: 15000 + config: + order: -10 + + ssl: + trust-store: classpath:/vault.jks + trust-store-password: changeit + + generic: + enabled: true + application-name: fakebank + + kv: + enabled: false + backend: kv + application-name: fakebank + + database: + enabled: true + role: fakebank-accounts-rw +# backend: database +# username-property: spring.datasource.username +# password-property: spring.datasource.password + + + + + + + + + \ No newline at end of file diff --git a/spring-cloud/spring-cloud-vault/src/main/resources/vault.jks b/spring-cloud/spring-cloud-vault/src/main/resources/vault.jks new file mode 100644 index 0000000000..7090707543 Binary files /dev/null and b/spring-cloud/spring-cloud-vault/src/main/resources/vault.jks differ diff --git a/spring-cloud/spring-cloud-vault/src/test/java/org/baeldung/spring/cloud/vaultsample/VaultSampleApplicationLiveTest.java b/spring-cloud/spring-cloud-vault/src/test/java/org/baeldung/spring/cloud/vaultsample/VaultSampleApplicationLiveTest.java new file mode 100644 index 0000000000..7a9c5ba11a --- /dev/null +++ b/spring-cloud/spring-cloud-vault/src/test/java/org/baeldung/spring/cloud/vaultsample/VaultSampleApplicationLiveTest.java @@ -0,0 +1,66 @@ +package org.baeldung.spring.cloud.vaultsample; + +import static org.junit.Assert.assertEquals; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; + +import javax.sql.DataSource; + +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.core.env.Environment; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class VaultSampleApplicationLiveTest { + + @Autowired + Environment env; + + @Autowired + DataSource datasource; + + + @Test + public void whenGenericBackendEnabled_thenEnvHasAccessToVaultSecrets() { + + String fooValue = env.getProperty("foo"); + assertEquals("test-bar", fooValue); + + } + + @Test + public void whenKvBackendEnabled_thenEnvHasAccessToVaultSecrets() { + + String fooValue = env.getProperty("foo.versioned"); + assertEquals("bar1", fooValue); + + + } + + + @Test + public void whenDatabaseBackendEnabled_thenDatasourceUsesVaultCredentials() { + + try (Connection c = datasource.getConnection()) { + + ResultSet rs = c.createStatement() + .executeQuery("select 1"); + + rs.next(); + Long value = rs.getLong(1); + + assertEquals(Long.valueOf(1), value); + + } catch (SQLException sex) { + throw new RuntimeException(sex); + } + + } + +} diff --git a/spring-cloud/spring-cloud-vault/src/test/resources/bootstrap.properties b/spring-cloud/spring-cloud-vault/src/test/resources/bootstrap.properties new file mode 100644 index 0000000000..8c54227dda --- /dev/null +++ b/spring-cloud/spring-cloud-vault/src/test/resources/bootstrap.properties @@ -0,0 +1,3 @@ +spring.cloud.vault.token=b93d1b0d-15b5-f69e-d311-352a65fa7bc8 + +logging.level.org.springframework=INFO \ No newline at end of file diff --git a/spring-cloud/spring-cloud-vault/src/test/resources/vault.jks b/spring-cloud/spring-cloud-vault/src/test/resources/vault.jks new file mode 100644 index 0000000000..7090707543 Binary files /dev/null and b/spring-cloud/spring-cloud-vault/src/test/resources/vault.jks differ diff --git a/spring-cloud/spring-cloud-vault/src/test/vault-config/localhost.cert b/spring-cloud/spring-cloud-vault/src/test/vault-config/localhost.cert new file mode 100644 index 0000000000..6a598df419 --- /dev/null +++ b/spring-cloud/spring-cloud-vault/src/test/vault-config/localhost.cert @@ -0,0 +1,18 @@ +-----BEGIN CERTIFICATE----- +MIIC+zCCAeOgAwIBAgIJAKoy5OBgOKYwMA0GCSqGSIb3DQEBBQUAMBQxEjAQBgNV +BAMMCWxvY2FsaG9zdDAeFw0xODA4MDkwMTM1MzJaFw0yODA4MDYwMTM1MzJaMBQx +EjAQBgNVBAMMCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC +ggEBAMXiHqB5dYdxJ1+abSG55gb3NNo3fzNbkjp/tAIl1FUeyCyyP/yERrkUkhFj +4gg/q1YHUO/ftc0PdL/JBaVBTKnzsxgp7hY/dUEkZqXZ649X0UrJIRd13w5N71cL +P1+PjCrqokMVceU18kK7CyaOmiTKYFmt/RTJQLmFQspmJXNSiq7zUvAgyvoY5TzJ +n7MuSobHXq17pnlm+XbnAgDJUt9yR6BC2dFF20iZU4uTXy2VRngfLey3p+6in0TO +jD4cEMJqwgUbjiI8m/hESCketVkq0W0qkkVfWBNzz5qqGHNRbhZBwT7SM0MuXum+ +qEY7n7jcQAk5BDb613liVQjQ0tkCAwEAAaNQME4wHQYDVR0OBBYEFHYjQ0/HJgXd +BnqM4jLPjmygfi8fMB8GA1UdIwQYMBaAFHYjQ0/HJgXdBnqM4jLPjmygfi8fMAwG +A1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggEBABSf++sinLT9dFnC+B6ut5Zp +haTL7PA1/CdmhTdE2vlFPGGw2BD4c/gphBsHKSNHE96irTqFXI/kl6labQpZ5P8G +JORLfaAyl58UT1FayxL4ISzwsp+UrqO60vxkYyLkbEJjuaxIv11oOoFDIp5oBTqe +BVoCfcTjYtTr+IwwlypLPrVTnDNGX5oPIBbTUFvR0t5RaLZgmXLT78ERhWOLINqh +Yi6j7fYaRm/C5IQ8N/TASot7V0SMH2Rt6PrzJb5SLV8r+yozg2BSfU6hZUyKwABR +N3zppKvKzdhlVo9OuSW3x4Tb3V+CVE/8CmTwRfhab9SCmvmaa2FxI+8/2OPVWDU= +-----END CERTIFICATE----- diff --git a/spring-cloud/spring-cloud-vault/src/test/vault-config/localhost.key b/spring-cloud/spring-cloud-vault/src/test/vault-config/localhost.key new file mode 100644 index 0000000000..eba611823a --- /dev/null +++ b/spring-cloud/spring-cloud-vault/src/test/vault-config/localhost.key @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEAxeIeoHl1h3EnX5ptIbnmBvc02jd/M1uSOn+0AiXUVR7ILLI/ +/IRGuRSSEWPiCD+rVgdQ79+1zQ90v8kFpUFMqfOzGCnuFj91QSRmpdnrj1fRSskh +F3XfDk3vVws/X4+MKuqiQxVx5TXyQrsLJo6aJMpgWa39FMlAuYVCymYlc1KKrvNS +8CDK+hjlPMmfsy5KhsderXumeWb5ducCAMlS33JHoELZ0UXbSJlTi5NfLZVGeB8t +7Len7qKfRM6MPhwQwmrCBRuOIjyb+ERIKR61WSrRbSqSRV9YE3PPmqoYc1FuFkHB +PtIzQy5e6b6oRjufuNxACTkENvrXeWJVCNDS2QIDAQABAoIBACEyB5VACtlHwCUn +kLshplbwzWr1+F6zM9qgZaAenHoTCd2FoXpI7lxJ+R71tItRsvphi9BRpPvbZehu +XoYUaDnyac7Z6djNmGvvIVEdN4j6YF+9UdHPsjWCGW5uspjjSc5BQisiw9KBtDxB +iGNVdMJLONKSf2wnPrZgho3RiOLJX/poPyGTkMHuhBVvo4oy7Ax3XalaAcufgqwm +YBQJ1Tka+33EUiLkxzJTXxNbIAI2scP8jhGn6mokS0V4gZPxJKUZEyydXRWwi6ex +ua/7q76ELJS5b+xKRYfGsvavFDx8R+LqX8oegALD33ki3rm1MQW7GmikRL98+EVW +Q9mQsqECgYEA/IrP8vycbJOgn1vriNItFcZtczSBlrXCRF0up2cqKMs9c+T5i51x +ZKXK5lo3DfMT+YDM+iiGZ9+vM0UA2VxbFD3XV9mQDBaNC+Duknqxx+OLmWva9YwR +nMaevqVV9LCn+GgUcK+IygEnpzpdP4q8YcXAfGAnZgnihN/AUYAaB70CgYEAyJe4 +yO0S9gAH5aoDdooL0YXrH/Dzd+fAgNsawLhoOltcoZqZFWeAllM0GsrCpfTRltuy +dn9ca3YK0GlWl7h5rDle1HO3nhp1FcpeG1oxmkeQta3PG66uUuMccTAljCLFrEe3 +DguH8+qdjhLk+ZnUB9AVkS79pzdwuEHVljCK600CgYB6mMygkh9B2lzkX9Q0xItc +gcqKXdf3GN9pHq9SVxOxYBDCHUtDirgMeyvHrc4COJneyrc3TcsJzB4aToo9+sbA +SdErdZOnOp9YP+axN1zsw7r2TNSr1UaLjCRuOodC1SuFvMkHdz95iRv946h2+1u+ +PyjVeDxIHc5YYOLU7dI1JQKBgQDF5KDBYNm25brkwcCe3nvgXfzjyyN25KUOupn/ +DS6Oe/m72Lgz3KOIKleaIvS7IvbunJnIu8dioNb0Wye5kJ5A4WyDrhG1IabnM3l6 +BJYw/W9vPSS4y7FhRnuV0wkH4nofh7S5X3jlk02Sj2NkN3Vtq8TLMY++uzwyG4jq +ncM/dQKBgQC+6mA5OfbVN4lRn+zrSiIH5gpvZYPh9wXeTnDWHa13sJsu3e8AQxtk +TfE0W13UV5jhGL8Wvyyxn+doGFTdcZapOlwuoQ6RcgHcVQm2sOl60GAa4idmm0A6 +TcgnIOTyVRlNBoWLCfN83BlGz4gcDpnuZZ/0JuguixgLS323hQlLvg== +-----END RSA PRIVATE KEY----- diff --git a/spring-cloud/spring-cloud-vault/src/test/vault-config/vault-test.hcl b/spring-cloud/spring-cloud-vault/src/test/vault-config/vault-test.hcl new file mode 100644 index 0000000000..c880f2d744 --- /dev/null +++ b/spring-cloud/spring-cloud-vault/src/test/vault-config/vault-test.hcl @@ -0,0 +1,20 @@ +/* + * Sample configuration file for tests + */ + +// Enable UI +ui = true + +// Filesystem storage +storage "file" { + path = "./vault-data" +} + +// TCP Listener using a self-signed certificate +listener "tcp" { + address = "127.0.0.1:8200" + tls_cert_file = "./src/test/vault-config/localhost.cert" + tls_key_file = "./src/test/vault-config/localhost.key" +} + + diff --git a/spring-cloud/spring-cloud-vault/vault-cheatsheet.txt b/spring-cloud/spring-cloud-vault/vault-cheatsheet.txt new file mode 100644 index 0000000000..b965a95321 --- /dev/null +++ b/spring-cloud/spring-cloud-vault/vault-cheatsheet.txt @@ -0,0 +1,82 @@ +== Vault server bootstrap + +1. Run vaul-start in one shell + +2. Open another shell and execute the command below: +> vault operator init + +Vault will output the unseal keys and root token: STORE THEM SAFELY !!! + +Example output: +Unseal Key 1: OfCseaSZzjTZmrxhfx+5clKobwLGCNiJdAlfixSG9E3o +Unseal Key 2: iplVLPTHW0n0WL5XuI6QWwyNtWbKTek1SoKcG0gR7vdT +Unseal Key 3: K0TleK3OYUvWFF+uIDsQuf5a+/gkv1PtZ3O47ornzRoF +Unseal Key 4: +5zhysLAO4hIdZs0kiZpkrRovw11uQacfloiBwnZBJA/ +Unseal Key 5: GDwSq18lXV3Cw4MoHsKIH137kuI0mdl36UiD9WxOdulc + +Initial Root Token: d341fdaf-1cf9-936a-3c38-cf5eec94b5c0 + +... + +== Admin token setup + +1. Set the VAULT_TOKEN environment variable with the root token value +export VAULT_TOKEN=d341fdaf-1cf9-936a-3c38-cf5eec94b5c0 (Linux) +set VAULT_TOKEN=d341fdaf-1cf9-936a-3c38-cf5eec94b5c0 (Windows) + +2. Create another admin token + +>vault token create -display-name=admin +Key Value +--- ----- +token 3779c3ca-9f5e-1d8f-3842-efa96d88de43 <=== this is the new root token +token_accessor 2dfa4031-973b-cf88-c749-ee6f520ecaea +token_duration ∞ +token_renewable false +token_policies ["root"] +identity_policies [] +policies ["root"] + +3. Create ~/.vault-secret with your root token +4. Unset the VAULT_TOKEN environment variable ! + +=== Test DB setup (MySQL only, for now) + +1. Create test db +2. Create admin account used to create dynamic accounts: + +create schema fakebank; +create user 'fakebank-admin'@'%' identified by 'Sup&rSecre7!' +grant all privileges on fakebank.* to 'fakebank-admin'@'%' with grant option; +grant create user on *.* to 'fakebank-admin' with grant option; +flush privileges; + + +=== Database secret backend setup +> vault secrets enable database + +==== Create db configuration +> vault write database/config/mysql-fakebank ^ + plugin_name=mysql-legacy-database-plugin ^ + connection_url="{{username}}:{{password}}@tcp(127.0.0.1:3306)/fakebank" ^ + allowed_roles="*" ^ + username="fakebank-admin" ^ + password="Sup&rSecre7!" + +==== Create roles +> vault write database/roles/fakebank-accounts-ro ^ + db_name=mysql-fakebank ^ + creation_statements="CREATE USER '{{name}}'@'%' IDENTIFIED BY '{{password}}';GRANT SELECT ON fakebank.* TO '{{name}}'@'%';" ^ + default_ttl="1h" ^ + max_ttl="24h" + +> vault write database/roles/fakebank-accounts-rw ^ + db_name=mysql-fakebank ^ + creation_statements="CREATE USER '{{name}}'@'%' IDENTIFIED BY '{{password}}';GRANT SELECT,INSERT,UPDATE ON fakebank.* TO '{{name}}'@'%';" ^ + default_ttl="1m" ^ + max_ttl="2m" + +=== Get credentials +> vault read database/creds/fakebank-accounts-rw + + diff --git a/spring-cloud/spring-cloud-vault/vault-env.bat b/spring-cloud/spring-cloud-vault/vault-env.bat new file mode 100644 index 0000000000..d7aa5da215 --- /dev/null +++ b/spring-cloud/spring-cloud-vault/vault-env.bat @@ -0,0 +1,5 @@ +@echo off +echo Setting environment variables to access local vault.. +set VAULT_ADDR=https://localhost:8200 +set VAULT_CACERT=%~dp0%/src/test/vault-config/localhost.cert +rem set VAULT_TLS_SERVER_NAME=localhost diff --git a/spring-cloud/spring-cloud-vault/vault-env.sh b/spring-cloud/spring-cloud-vault/vault-env.sh new file mode 100644 index 0000000000..06077cee2e --- /dev/null +++ b/spring-cloud/spring-cloud-vault/vault-env.sh @@ -0,0 +1,6 @@ +#!/bin/bash +echo Setting environment variables to access local vault.. +SCRIPTPATH=$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd ) +export VAULT_ADDR=https://localhost:8200 +export VAULT_CACERT=$SCRIPTPATH/src/test/vault-config/localhost.cert +export VAULT_TLS_SERVER_NAME=localhost diff --git a/spring-cloud/spring-cloud-vault/vault-start.bat b/spring-cloud/spring-cloud-vault/vault-start.bat new file mode 100644 index 0000000000..e8f6ce4c9f --- /dev/null +++ b/spring-cloud/spring-cloud-vault/vault-start.bat @@ -0,0 +1,3 @@ +echo Starting vault server... +pushd %~dp0% +vault server -config %~dp0%/src/test/vault-config/vault-test.hcl diff --git a/spring-cloud/spring-cloud-vault/vault-start.sh b/spring-cloud/spring-cloud-vault/vault-start.sh new file mode 100644 index 0000000000..d5af7cfee6 --- /dev/null +++ b/spring-cloud/spring-cloud-vault/vault-start.sh @@ -0,0 +1,5 @@ +#!/bin/bash +SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" +pushd $SCRIPTPATH +echo Starting vault server... +vault server -config $SCRIPTPATH/src/test/vault-config/vault-test.hcl diff --git a/spring-cloud/spring-cloud-vault/vault-unseal.bat b/spring-cloud/spring-cloud-vault/vault-unseal.bat new file mode 100644 index 0000000000..8133f90892 --- /dev/null +++ b/spring-cloud/spring-cloud-vault/vault-unseal.bat @@ -0,0 +1,7 @@ + +call %~dp0%/vault-env.bat + +vault operator unseal OfCseaSZzjTZmrxhfx+5clKobwLGCNiJdAlfixSG9E3o +vault operator unseal iplVLPTHW0n0WL5XuI6QWwyNtWbKTek1SoKcG0gR7vdT +vault operator unseal iplVLPTHW0n0WL5XuI6QWwyNtWbKTek1SoKcG0gR7vdT +vault operator unseal K0TleK3OYUvWFF+uIDsQuf5a+/gkv1PtZ3O47ornzRoF diff --git a/spring-cloud/spring-cloud-vault/vault-unseal.sh b/spring-cloud/spring-cloud-vault/vault-unseal.sh new file mode 100644 index 0000000000..699b383801 --- /dev/null +++ b/spring-cloud/spring-cloud-vault/vault-unseal.sh @@ -0,0 +1,8 @@ +#!/bin/bash +SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" +. $SCRIPTPATH/vault-env.sh + +# Please replace the unseal keys below for your own +vault operator unseal OfCseaSZzjTZmrxhfx+5clKobwLGCNiJdAlfixSG9E3o +vault operator unseal iplVLPTHW0n0WL5XuI6QWwyNtWbKTek1SoKcG0gR7vdT +vault operator unseal K0TleK3OYUvWFF+uIDsQuf5a+/gkv1PtZ3O47ornzRoF diff --git a/spring-core/README.md b/spring-core/README.md index adc21ffdaf..c0577b4ebc 100644 --- a/spring-core/README.md +++ b/spring-core/README.md @@ -18,3 +18,5 @@ - [Spring – Injecting Collections](http://www.baeldung.com/spring-injecting-collections) - [Access a File from the Classpath in a Spring Application](http://www.baeldung.com/spring-classpath-file-access) - [Controlling Bean Creation Order with @DependsOn Annotation](http://www.baeldung.com/spring-depends-on) +- [Spring Autowiring of Generic Types](https://www.baeldung.com/spring-autowire-generics) +- [Spring Application Context Events](https://www.baeldung.com/spring-context-events) diff --git a/spring-core/src/main/java/com/baeldung/definition/Config.java b/spring-core/src/main/java/com/baeldung/definition/Config.java new file mode 100644 index 0000000000..126e6259ca --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/definition/Config.java @@ -0,0 +1,16 @@ +package com.baeldung.definition; + +import com.baeldung.definition.domain.Address; +import com.baeldung.definition.domain.Company; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan(basePackageClasses = Company.class) +public class Config { + @Bean + public Address getAddress() { + return new Address("High Street", 1000); + } +} diff --git a/spring-core/src/main/java/com/baeldung/definition/domain/Address.java b/spring-core/src/main/java/com/baeldung/definition/domain/Address.java new file mode 100644 index 0000000000..91be18398e --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/definition/domain/Address.java @@ -0,0 +1,14 @@ +package com.baeldung.definition.domain; + +import lombok.Data; + +@Data +public class Address { + private String street; + private int number; + + public Address(String street, int number) { + this.street = street; + this.number = number; + } +} diff --git a/spring-core/src/main/java/com/baeldung/definition/domain/Company.java b/spring-core/src/main/java/com/baeldung/definition/domain/Company.java new file mode 100644 index 0000000000..eabde8afdf --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/definition/domain/Company.java @@ -0,0 +1,14 @@ +package com.baeldung.definition.domain; + +import lombok.Data; +import org.springframework.stereotype.Component; + +@Data +@Component +public class Company { + private Address address; + + public Company(Address address) { + this.address = address; + } +} diff --git a/spring-core/src/main/java/com/baeldung/dependency/exception/app/PurchaseDeptService.java b/spring-core/src/main/java/com/baeldung/dependency/exception/app/PurchaseDeptService.java index 1e6fad63aa..e0fe01acdd 100644 --- a/spring-core/src/main/java/com/baeldung/dependency/exception/app/PurchaseDeptService.java +++ b/spring-core/src/main/java/com/baeldung/dependency/exception/app/PurchaseDeptService.java @@ -1,13 +1,14 @@ package com.baeldung.dependency.exception.app; import com.baeldung.dependency.exception.repository.InventoryRepository; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; @Service public class PurchaseDeptService { private InventoryRepository repository; - public PurchaseDeptService(InventoryRepository repository) { + public PurchaseDeptService(@Qualifier("dresses") InventoryRepository repository) { this.repository = repository; } } \ No newline at end of file diff --git a/spring-core/src/main/java/com/baeldung/dependency/exception/app/CustomConfiguration.java b/spring-core/src/main/java/com/baeldung/dependency/exception/app/SpringDependenciesExampleApplication.java similarity index 73% rename from spring-core/src/main/java/com/baeldung/dependency/exception/app/CustomConfiguration.java rename to spring-core/src/main/java/com/baeldung/dependency/exception/app/SpringDependenciesExampleApplication.java index 4366cb617a..e45438a9fe 100644 --- a/spring-core/src/main/java/com/baeldung/dependency/exception/app/CustomConfiguration.java +++ b/spring-core/src/main/java/com/baeldung/dependency/exception/app/SpringDependenciesExampleApplication.java @@ -6,8 +6,8 @@ import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages = "com.baeldung.dependency.exception") -public class CustomConfiguration { +public class SpringDependenciesExampleApplication { public static void main(String[] args) { - SpringApplication.run(CustomConfiguration.class, args); + SpringApplication.run(SpringDependenciesExampleApplication.class, args); } } diff --git a/spring-core/src/main/java/com/baeldung/dependency/exception/repository/DressRepository.java b/spring-core/src/main/java/com/baeldung/dependency/exception/repository/DressRepository.java index 4a6c836143..5a1371ce04 100644 --- a/spring-core/src/main/java/com/baeldung/dependency/exception/repository/DressRepository.java +++ b/spring-core/src/main/java/com/baeldung/dependency/exception/repository/DressRepository.java @@ -1,9 +1,10 @@ package com.baeldung.dependency.exception.repository; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Repository; -@Primary +@Qualifier("dresses") @Repository public class DressRepository implements InventoryRepository { } diff --git a/spring-core/src/main/java/com/baeldung/dependency/exception/repository/ShoeRepository.java b/spring-core/src/main/java/com/baeldung/dependency/exception/repository/ShoeRepository.java index 60495914cd..227d8934b6 100644 --- a/spring-core/src/main/java/com/baeldung/dependency/exception/repository/ShoeRepository.java +++ b/spring-core/src/main/java/com/baeldung/dependency/exception/repository/ShoeRepository.java @@ -1,7 +1,9 @@ package com.baeldung.dependency.exception.repository; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Repository; +@Qualifier("shoes") @Repository public class ShoeRepository implements InventoryRepository { } diff --git a/spring-core/src/test/java/com/baeldung/definition/SpringBeanIntegrationTest.java b/spring-core/src/test/java/com/baeldung/definition/SpringBeanIntegrationTest.java new file mode 100644 index 0000000000..0057611308 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/definition/SpringBeanIntegrationTest.java @@ -0,0 +1,18 @@ +package com.baeldung.definition; + +import com.baeldung.definition.domain.Company; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import static org.junit.Assert.assertEquals; + +public class SpringBeanIntegrationTest { + @Test + public void whenUsingIoC_thenDependenciesAreInjected() { + ApplicationContext context = new AnnotationConfigApplicationContext(Config.class); + Company company = context.getBean("company", Company.class); + assertEquals("High Street", company.getAddress().getStreet()); + assertEquals(1000, company.getAddress().getNumber()); + } +} diff --git a/spring-data-jpa/README.md b/spring-data-jpa/README.md index f54764e05c..20e9d41839 100644 --- a/spring-data-jpa/README.md +++ b/spring-data-jpa/README.md @@ -10,6 +10,7 @@ - [Spring Data Annotations](http://www.baeldung.com/spring-data-annotations) - [Spring Data Java 8 Support](http://www.baeldung.com/spring-data-java-8) - [A Simple Tagging Implementation with JPA](http://www.baeldung.com/jpa-tagging) +- [Spring Data Composable Repositories](https://www.baeldung.com/spring-data-composable-repositories) ### Eclipse Config After importing the project into Eclipse, you may see the following error: diff --git a/spring-data-jpa/pom.xml b/spring-data-jpa/pom.xml index c4893df759..8691ce1f09 100644 --- a/spring-data-jpa/pom.xml +++ b/spring-data-jpa/pom.xml @@ -41,6 +41,26 @@ guava 21.0 + + + + org.junit.jupiter + junit-jupiter-api + test + + + org.junit.jupiter + junit-jupiter-engine + test + + + + + org.junit.platform + junit-platform-launcher + ${junit-platform.version} + test + \ No newline at end of file diff --git a/spring-data-jpa/src/main/java/com/baeldung/Application.java b/spring-data-jpa/src/main/java/com/baeldung/Application.java index 4e14f94311..72d29d9fa5 100644 --- a/spring-data-jpa/src/main/java/com/baeldung/Application.java +++ b/spring-data-jpa/src/main/java/com/baeldung/Application.java @@ -1,11 +1,11 @@ package com.baeldung; -import com.baeldung.dao.repositories.impl.ExtendedRepositoryImpl; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import com.baeldung.dao.repositories.impl.ExtendedRepositoryImpl; + @SpringBootApplication @EnableJpaRepositories(repositoryBaseClass = ExtendedRepositoryImpl.class) public class Application { diff --git a/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/InventoryRepository.java b/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/InventoryRepository.java new file mode 100644 index 0000000000..a575f0b915 --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/InventoryRepository.java @@ -0,0 +1,7 @@ +package com.baeldung.dao.repositories; + +import com.baeldung.domain.MerchandiseEntity; +import org.springframework.data.repository.CrudRepository; + +public interface InventoryRepository extends CrudRepository { +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate.java b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate.java new file mode 100644 index 0000000000..bf6ff0a0b9 --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate.java @@ -0,0 +1,46 @@ +/** + * + */ +package com.baeldung.ddd.event; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Transient; + +import org.springframework.context.ApplicationEventPublisher; + +@Entity +class Aggregate { + @Transient + private ApplicationEventPublisher eventPublisher; + @Id + private long id; + + private Aggregate() { + } + + Aggregate(long id, ApplicationEventPublisher eventPublisher) { + this.id = id; + this.eventPublisher = eventPublisher; + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "DomainEntity [id=" + id + "]"; + } + + void domainOperation() { + // some business logic + if (eventPublisher != null) { + eventPublisher.publishEvent(new DomainEvent()); + } + } + + long getId() { + return id; + } + +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate2.java b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate2.java new file mode 100644 index 0000000000..3d2816299a --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate2.java @@ -0,0 +1,44 @@ +/** + * + */ +package com.baeldung.ddd.event; + +import java.util.ArrayList; +import java.util.Collection; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Transient; + +import org.springframework.data.domain.AfterDomainEventPublication; +import org.springframework.data.domain.DomainEvents; + +@Entity +public class Aggregate2 { + @Transient + private final Collection domainEvents; + @Id + @GeneratedValue + private long id; + + public Aggregate2() { + domainEvents = new ArrayList<>(); + } + + @AfterDomainEventPublication + public void clearEvents() { + domainEvents.clear(); + } + + public void domainOperation() { + // some domain operation + domainEvents.add(new DomainEvent()); + } + + @DomainEvents + public Collection events() { + return domainEvents; + } + +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate2Repository.java b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate2Repository.java new file mode 100644 index 0000000000..2a95abe347 --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate2Repository.java @@ -0,0 +1,10 @@ +/** + * + */ +package com.baeldung.ddd.event; + +import org.springframework.data.repository.CrudRepository; + +public interface Aggregate2Repository extends CrudRepository { + +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate3.java b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate3.java new file mode 100644 index 0000000000..e0c3131b06 --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate3.java @@ -0,0 +1,23 @@ +/** + * + */ +package com.baeldung.ddd.event; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +import org.springframework.data.domain.AbstractAggregateRoot; + +@Entity +public class Aggregate3 extends AbstractAggregateRoot { + @Id + @GeneratedValue + private long id; + + public void domainOperation() { + // some domain operation + registerEvent(new DomainEvent()); + } + +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate3Repository.java b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate3Repository.java new file mode 100644 index 0000000000..e442bdb210 --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate3Repository.java @@ -0,0 +1,14 @@ +/** + * + */ +package com.baeldung.ddd.event; + +import org.springframework.data.repository.CrudRepository; + +/** + * @author goobar + * + */ +public interface Aggregate3Repository extends CrudRepository { + +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/ddd/event/AggregateRepository.java b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/AggregateRepository.java new file mode 100644 index 0000000000..5a15156d03 --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/AggregateRepository.java @@ -0,0 +1,10 @@ +/** + * + */ +package com.baeldung.ddd.event; + +import org.springframework.data.repository.CrudRepository; + +public interface AggregateRepository extends CrudRepository { + +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DddConfig.java b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DddConfig.java new file mode 100644 index 0000000000..1315b11875 --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DddConfig.java @@ -0,0 +1,15 @@ +/** + * + */ +package com.baeldung.ddd.event; + +import org.springframework.boot.SpringBootConfiguration; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.PropertySource; + +@SpringBootConfiguration +@EnableAutoConfiguration +@PropertySource("classpath:/ddd.properties") +public class DddConfig { + +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DomainEvent.java b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DomainEvent.java new file mode 100644 index 0000000000..1e6479d4fc --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DomainEvent.java @@ -0,0 +1,8 @@ +/** + * + */ +package com.baeldung.ddd.event; + +class DomainEvent { + +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DomainService.java b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DomainService.java new file mode 100644 index 0000000000..082c9bd88e --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DomainService.java @@ -0,0 +1,31 @@ +/** + * + */ +package com.baeldung.ddd.event; + +import javax.transaction.Transactional; + +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.stereotype.Service; + +@Service +public class DomainService { + private final ApplicationEventPublisher eventPublisher; + private final AggregateRepository repository; + + public DomainService(AggregateRepository repository, ApplicationEventPublisher eventPublisher) { + this.repository = repository; + this.eventPublisher = eventPublisher; + } + + @Transactional + public void serviceDomainOperation(long entityId) { + repository.findById(entityId) + .ifPresent(entity -> { + entity.domainOperation(); + repository.save(entity); + eventPublisher.publishEvent(new DomainEvent()); + }); + } + +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/domain/MerchandiseEntity.java b/spring-data-jpa/src/main/java/com/baeldung/domain/MerchandiseEntity.java new file mode 100644 index 0000000000..bfc690e0e2 --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/domain/MerchandiseEntity.java @@ -0,0 +1,66 @@ +package com.baeldung.domain; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import java.math.BigDecimal; + +@Entity +public class MerchandiseEntity { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + private String title; + + private BigDecimal price; + + private String brand; + + public MerchandiseEntity() { + } + + public MerchandiseEntity(String title, BigDecimal price) { + this.title = title; + this.price = price; + } + + public Long getId() { + return id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public BigDecimal getPrice() { + return price; + } + + public void setPrice(BigDecimal price) { + this.price = price; + } + + public String getBrand() { + return brand; + } + + public void setBrand(String brand) { + this.brand = brand; + } + + @Override + public String toString() { + return "MerchandiseEntity{" + + "id=" + id + + ", title='" + title + '\'' + + ", price=" + price + + ", brand='" + brand + '\'' + + '}'; + } +} diff --git a/spring-data-jpa/src/main/resources/ddd.properties b/spring-data-jpa/src/main/resources/ddd.properties new file mode 100644 index 0000000000..e5126b694b --- /dev/null +++ b/spring-data-jpa/src/main/resources/ddd.properties @@ -0,0 +1 @@ +spring.datasource.initialization-mode=never \ No newline at end of file diff --git a/spring-data-jpa/src/main/resources/persistence-multiple-db.properties b/spring-data-jpa/src/main/resources/persistence-multiple-db.properties index 53c6cecf8b..75534e8a54 100644 --- a/spring-data-jpa/src/main/resources/persistence-multiple-db.properties +++ b/spring-data-jpa/src/main/resources/persistence-multiple-db.properties @@ -3,7 +3,7 @@ jdbc.driverClassName=org.h2.Driver user.jdbc.url=jdbc:h2:mem:spring_jpa_user;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS USERS product.jdbc.url=jdbc:h2:mem:spring_jpa_product;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS PRODUCTS jdbc.user=sa -jdbc.pass= +jdbc.pass=sa # hibernate.X hibernate.dialect=org.hibernate.dialect.H2Dialect diff --git a/spring-data-jpa/src/main/resources/persistence.properties b/spring-data-jpa/src/main/resources/persistence.properties index 5e83653401..3543e1b52b 100644 --- a/spring-data-jpa/src/main/resources/persistence.properties +++ b/spring-data-jpa/src/main/resources/persistence.properties @@ -2,7 +2,7 @@ jdbc.driverClassName=org.h2.Driver jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS USERS jdbc.user=sa -jdbc.pass= +jdbc.pass=sa # hibernate.X hibernate.dialect=org.hibernate.dialect.H2Dialect diff --git a/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/InventoryRepositoryIntegrationTest.java b/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/InventoryRepositoryIntegrationTest.java new file mode 100644 index 0000000000..9d6334445c --- /dev/null +++ b/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/InventoryRepositoryIntegrationTest.java @@ -0,0 +1,60 @@ +package com.baeldung.dao.repositories; + +import com.baeldung.config.PersistenceConfiguration; +import com.baeldung.domain.MerchandiseEntity; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.math.BigDecimal; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; + +@RunWith(SpringRunner.class) +@DataJpaTest(excludeAutoConfiguration = {PersistenceConfiguration.class}) +public class InventoryRepositoryIntegrationTest { + + private static final String ORIGINAL_TITLE = "Pair of Pants"; + private static final String UPDATED_TITLE = "Branded Luxury Pants"; + private static final String UPDATED_BRAND = "Armani"; + private static final String ORIGINAL_SHORTS_TITLE = "Pair of Shorts"; + + @Autowired + private InventoryRepository repository; + + @Test + public void shouldCreateNewEntryInDB() { + MerchandiseEntity pants = new MerchandiseEntity(ORIGINAL_TITLE, BigDecimal.ONE); + pants = repository.save(pants); + + MerchandiseEntity shorts = new MerchandiseEntity(ORIGINAL_SHORTS_TITLE, new BigDecimal(3)); + shorts = repository.save(shorts); + + assertNotNull(pants.getId()); + assertNotNull(shorts.getId()); + assertNotEquals(pants.getId(), shorts.getId()); + } + + @Test + public void shouldUpdateExistingEntryInDB() { + MerchandiseEntity pants = new MerchandiseEntity(ORIGINAL_TITLE, BigDecimal.ONE); + pants = repository.save(pants); + + Long originalId = pants.getId(); + + pants.setTitle(UPDATED_TITLE); + pants.setPrice(BigDecimal.TEN); + pants.setBrand(UPDATED_BRAND); + + MerchandiseEntity result = repository.save(pants); + + assertEquals(originalId, result.getId()); + assertEquals(UPDATED_TITLE, result.getTitle()); + assertEquals(BigDecimal.TEN, result.getPrice()); + assertEquals(UPDATED_BRAND, result.getBrand()); + } +} diff --git a/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/JpaRepositoriesIntegrationTest.java b/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/JpaRepositoriesIntegrationTest.java index eaadb9e44a..01405c0b8a 100644 --- a/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/JpaRepositoriesIntegrationTest.java +++ b/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/JpaRepositoriesIntegrationTest.java @@ -9,28 +9,22 @@ import static junit.framework.TestCase.assertTrue; import java.util.List; import java.util.Optional; -import com.baeldung.config.PersistenceConfiguration; -import com.baeldung.config.PersistenceProductConfiguration; -import com.baeldung.config.PersistenceUserConfiguration; -import com.baeldung.dao.repositories.ItemTypeRepository; -import com.baeldung.dao.repositories.LocationRepository; -import com.baeldung.dao.repositories.ReadOnlyLocationRepository; -import com.baeldung.dao.repositories.StoreRepository; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; +import com.baeldung.config.PersistenceConfiguration; +import com.baeldung.config.PersistenceProductConfiguration; +import com.baeldung.config.PersistenceUserConfiguration; import com.baeldung.domain.Item; import com.baeldung.domain.ItemType; import com.baeldung.domain.Location; import com.baeldung.domain.Store; @RunWith(SpringRunner.class) -@DataJpaTest(excludeAutoConfiguration = {PersistenceConfiguration.class, PersistenceUserConfiguration.class, PersistenceProductConfiguration.class}) +@DataJpaTest(excludeAutoConfiguration = { PersistenceConfiguration.class, PersistenceUserConfiguration.class, PersistenceProductConfiguration.class }) public class JpaRepositoriesIntegrationTest { @Autowired private LocationRepository locationRepository; diff --git a/spring-data-jpa/src/test/java/com/baeldung/ddd/event/Aggregate2EventsIntegrationTest.java b/spring-data-jpa/src/test/java/com/baeldung/ddd/event/Aggregate2EventsIntegrationTest.java new file mode 100644 index 0000000000..3f650d4d63 --- /dev/null +++ b/spring-data-jpa/src/test/java/com/baeldung/ddd/event/Aggregate2EventsIntegrationTest.java @@ -0,0 +1,68 @@ +/** + * + */ +package com.baeldung.ddd.event; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +@SpringJUnitConfig +@SpringBootTest +class Aggregate2EventsIntegrationTest { + @MockBean + private TestEventHandler eventHandler; + @Autowired + private Aggregate2Repository repository; + + // @formatter:off + @DisplayName("given aggregate with @AfterDomainEventPublication," + + " when do domain operation and save twice," + + " then an event is published only for the first time") + // @formatter:on + @Test + void afterDomainEvents() { + // given + Aggregate2 aggregate = new Aggregate2(); + + // when + aggregate.domainOperation(); + repository.save(aggregate); + repository.save(aggregate); + + // then + verify(eventHandler, times(1)).handleEvent(any(DomainEvent.class)); + } + + @BeforeEach + void beforeEach() { + repository.deleteAll(); + } + + // @formatter:off + @DisplayName("given aggregate with @DomainEvents," + + " when do domain operation and save," + + " then an event is published") + // @formatter:on + @Test + void domainEvents() { + // given + Aggregate2 aggregate = new Aggregate2(); + + // when + aggregate.domainOperation(); + repository.save(aggregate); + + // then + verify(eventHandler, times(1)).handleEvent(any(DomainEvent.class)); + } + +} diff --git a/spring-data-jpa/src/test/java/com/baeldung/ddd/event/Aggregate3EventsIntegrationTest.java b/spring-data-jpa/src/test/java/com/baeldung/ddd/event/Aggregate3EventsIntegrationTest.java new file mode 100644 index 0000000000..893dcac3f8 --- /dev/null +++ b/spring-data-jpa/src/test/java/com/baeldung/ddd/event/Aggregate3EventsIntegrationTest.java @@ -0,0 +1,63 @@ +/** + * + */ +package com.baeldung.ddd.event; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +@SpringJUnitConfig +@SpringBootTest +class Aggregate3EventsIntegrationTest { + + @MockBean + private TestEventHandler eventHandler; + @Autowired + private Aggregate3Repository repository; + + // @formatter:off + @DisplayName("given aggregate extending AbstractAggregateRoot," + + " when do domain operation and save twice," + + " then an event is published only for the first time") + // @formatter:on + @Test + void afterDomainEvents() { + // given + Aggregate3 aggregate = new Aggregate3(); + + // when + aggregate.domainOperation(); + repository.save(aggregate); + repository.save(aggregate); + + // then + verify(eventHandler, times(1)).handleEvent(any(DomainEvent.class)); + } + + // @formatter:off + @DisplayName("given aggregate extending AbstractAggregateRoot," + + " when do domain operation and save," + + " then an event is published") + // @formatter:on + @Test + void domainEvents() { + // given + Aggregate3 aggregate = new Aggregate3(); + + // when + aggregate.domainOperation(); + repository.save(aggregate); + + // then + verify(eventHandler, times(1)).handleEvent(any(DomainEvent.class)); + } + +} diff --git a/spring-data-jpa/src/test/java/com/baeldung/ddd/event/AggregateEventsIntegrationTest.java b/spring-data-jpa/src/test/java/com/baeldung/ddd/event/AggregateEventsIntegrationTest.java new file mode 100644 index 0000000000..f0e1147245 --- /dev/null +++ b/spring-data-jpa/src/test/java/com/baeldung/ddd/event/AggregateEventsIntegrationTest.java @@ -0,0 +1,82 @@ +package com.baeldung.ddd.event; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.annotation.Bean; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +@SpringJUnitConfig +@SpringBootTest +class AggregateEventsIntegrationTest { + + @Autowired + private DomainService domainService; + + @MockBean + private TestEventHandler eventHandler; + @Autowired + private ApplicationEventPublisher eventPublisher; + @Autowired + private AggregateRepository repository; + + // @formatter:off + @DisplayName("given existing aggregate," + + " when do domain operation directly on aggregate," + + " then domain event is NOT published") + // @formatter:on + @Test + void aggregateEventsTest() { + Aggregate existingDomainEntity = new Aggregate(0, eventPublisher); + repository.save(existingDomainEntity); + + // when + repository.findById(existingDomainEntity.getId()) + .get() + .domainOperation(); + + // then + verifyZeroInteractions(eventHandler); + } + + @BeforeEach + void beforeEach() { + repository.deleteAll(); + } + + // @formatter:off + @DisplayName("given existing aggregate," + + " when do domain operation on service," + + " then domain event is published") + // @formatter:on + @Test + void serviceEventsTest() { + Aggregate existingDomainEntity = new Aggregate(1, eventPublisher); + repository.save(existingDomainEntity); + + // when + domainService.serviceDomainOperation(existingDomainEntity.getId()); + + // then + verify(eventHandler, times(1)).handleEvent(any(DomainEvent.class)); + } + + @TestConfiguration + public static class TestConfig { + @Bean + public DomainService domainService(AggregateRepository repository, ApplicationEventPublisher eventPublisher) { + return new DomainService(repository, eventPublisher); + } + } + +} diff --git a/spring-data-jpa/src/test/java/com/baeldung/ddd/event/TestEventHandler.java b/spring-data-jpa/src/test/java/com/baeldung/ddd/event/TestEventHandler.java new file mode 100644 index 0000000000..721402c17a --- /dev/null +++ b/spring-data-jpa/src/test/java/com/baeldung/ddd/event/TestEventHandler.java @@ -0,0 +1,12 @@ +/** + * + */ +package com.baeldung.ddd.event; + +import org.springframework.transaction.event.TransactionalEventListener; + +interface TestEventHandler { + @TransactionalEventListener + void handleEvent(DomainEvent event); + +} diff --git a/spring-data-jpa/src/test/java/com/baeldung/services/JpaMultipleDBIntegrationTest.java b/spring-data-jpa/src/test/java/com/baeldung/services/JpaMultipleDBIntegrationTest.java index 29b96ae597..71a3fb0b44 100644 --- a/spring-data-jpa/src/test/java/com/baeldung/services/JpaMultipleDBIntegrationTest.java +++ b/spring-data-jpa/src/test/java/com/baeldung/services/JpaMultipleDBIntegrationTest.java @@ -1,31 +1,33 @@ package com.baeldung.services; -import com.baeldung.config.PersistenceProductConfiguration; -import com.baeldung.config.PersistenceUserConfiguration; -import com.baeldung.dao.repositories.user.PossessionRepository; -import com.baeldung.dao.repositories.product.ProductRepository; -import com.baeldung.dao.repositories.user.UserRepository; -import com.baeldung.domain.user.Possession; -import com.baeldung.domain.product.Product; -import com.baeldung.domain.user.User; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Collections; +import java.util.Optional; + import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.Transactional; -import java.util.Collections; -import java.util.Optional; - -import static org.junit.Assert.*; +import com.baeldung.config.PersistenceProductConfiguration; +import com.baeldung.config.PersistenceUserConfiguration; +import com.baeldung.dao.repositories.product.ProductRepository; +import com.baeldung.dao.repositories.user.PossessionRepository; +import com.baeldung.dao.repositories.user.UserRepository; +import com.baeldung.domain.product.Product; +import com.baeldung.domain.user.Possession; +import com.baeldung.domain.user.User; @RunWith(SpringRunner.class) -@ContextConfiguration(classes = {PersistenceUserConfiguration.class, PersistenceProductConfiguration.class}) +@ContextConfiguration(classes = { PersistenceUserConfiguration.class, PersistenceProductConfiguration.class }) @EnableTransactionManagement @DirtiesContext public class JpaMultipleDBIntegrationTest { diff --git a/spring-data-jpa/src/test/java/com/baeldung/services/SpringDataJPABarAuditIntegrationTest.java b/spring-data-jpa/src/test/java/com/baeldung/services/SpringDataJPABarAuditIntegrationTest.java index 3c36f43192..f3b857c73d 100644 --- a/spring-data-jpa/src/test/java/com/baeldung/services/SpringDataJPABarAuditIntegrationTest.java +++ b/spring-data-jpa/src/test/java/com/baeldung/services/SpringDataJPABarAuditIntegrationTest.java @@ -1,8 +1,16 @@ package com.baeldung.services; -import com.baeldung.config.PersistenceConfiguration; -import com.baeldung.domain.Bar; -import org.junit.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -10,15 +18,11 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.support.AnnotationConfigContextLoader; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import com.baeldung.config.PersistenceConfiguration; +import com.baeldung.domain.Bar; @RunWith(SpringRunner.class) @ContextConfiguration(classes = { PersistenceConfiguration.class }, loader = AnnotationConfigContextLoader.class) diff --git a/spring-ejb/ejb-beans/pom.xml b/spring-ejb/ejb-beans/pom.xml index 7a569ae4fb..76c0afadee 100644 --- a/spring-ejb/ejb-beans/pom.xml +++ b/spring-ejb/ejb-beans/pom.xml @@ -2,13 +2,13 @@ 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.singletonsession - singleton-ejb-bean + ejb-beans 1.0.0-SNAPSHOT - EJB Singleton Session Bean + EJB Beans com.baeldung.spring.ejb - ejb-for-spring + spring-ejb 1.0.1 diff --git a/spring-ejb/ejb-remote-for-spring/pom.xml b/spring-ejb/ejb-remote-for-spring/pom.xml index 5f4869968f..21256fa801 100755 --- a/spring-ejb/ejb-remote-for-spring/pom.xml +++ b/spring-ejb/ejb-remote-for-spring/pom.xml @@ -7,7 +7,7 @@ com.baeldung.spring.ejb - ejb-for-spring + spring-ejb 1.0.1 diff --git a/spring-ejb/pom.xml b/spring-ejb/pom.xml index d976bf4bfe..31cde720f9 100755 --- a/spring-ejb/pom.xml +++ b/spring-ejb/pom.xml @@ -3,10 +3,10 @@ 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.ejb - ejb-for-spring + spring-ejb 1.0.1 pom - ejb + spring-ejb Spring EJB Tutorial diff --git a/spring-freemarker/pom.xml b/spring-freemarker/pom.xml index 9c694256f3..f0626195fd 100644 --- a/spring-freemarker/pom.xml +++ b/spring-freemarker/pom.xml @@ -1,8 +1,8 @@ 4.0.0 - com.freemarker.example - spring4-freemarker-example + com.baeldung.freemarker + spring-freemarker war 1.0-SNAPSHOT Spring Freemarker Example @@ -48,10 +48,10 @@ - 4.3.4.RELEASE - 2.3.23 + 5.0.8.RELEASE + 2.3.28 3.1.0 false - \ No newline at end of file + diff --git a/spring-integration/README.md b/spring-integration/README.md index d782862901..e116f934c8 100644 --- a/spring-integration/README.md +++ b/spring-integration/README.md @@ -1,6 +1,7 @@ ### Relevant Articles: - [Introduction to Spring Integration](http://www.baeldung.com/spring-integration) - [Security In Spring Integration](http://www.baeldung.com/spring-integration-security) +- [Spring Integration Java DSL](https://www.baeldung.com/spring-integration-java-dsl) ### Running the Sample Executing the `mvn exec:java` maven command (either from the command line or from an IDE) will start up the application. Follow the command prompt for further instructions. diff --git a/spring-jersey/README.md b/spring-jersey/README.md index 8b2eecc0e1..37928620d3 100644 --- a/spring-jersey/README.md +++ b/spring-jersey/README.md @@ -3,3 +3,4 @@ ## REST API with Jersey & Spring Example Project - [REST API with Jersey and Spring](http://www.baeldung.com/jersey-rest-api-with-spring) - [JAX-RS Client with Jersey](http://www.baeldung.com/jersey-jax-rs-client) +- [Reactive JAX-RS Client API](https://www.baeldung.com/jax-rs-reactive-client) diff --git a/spring-jooq/pom.xml b/spring-jooq/pom.xml index e1c13ef774..744ae34cc8 100644 --- a/spring-jooq/pom.xml +++ b/spring-jooq/pom.xml @@ -2,8 +2,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 spring-jooq - 0.0.1-SNAPSHOT - + parent-boot-1 com.baeldung @@ -54,11 +53,6 @@ spring-test test - - org.springframework.boot - spring-boot-starter-test - - diff --git a/spring-mvc-java/README.md b/spring-mvc-java/README.md index c4a0e3579c..44b1d65bc8 100644 --- a/spring-mvc-java/README.md +++ b/spring-mvc-java/README.md @@ -28,10 +28,5 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [The Spring @Controller and @RestController Annotations](http://www.baeldung.com/spring-controller-vs-restcontroller) - [Spring MVC @PathVariable with a dot (.) gets truncated](http://www.baeldung.com/spring-mvc-pathvariable-dot) - [A Quick Example of Spring Websockets’ @SendToUser Annotation](http://www.baeldung.com/spring-websockets-sendtouser) -- [Spring Boot Annotations](http://www.baeldung.com/spring-boot-annotations) -- [Spring Scheduling Annotations](http://www.baeldung.com/spring-scheduling-annotations) -- [Spring Web Annotations](http://www.baeldung.com/spring-mvc-annotations) -- [Spring Core Annotations](http://www.baeldung.com/spring-core-annotations) - [Using Spring ResponseEntity to Manipulate the HTTP Response](http://www.baeldung.com/spring-response-entity) - [Using Spring @ResponseStatus to Set HTTP Status Code](http://www.baeldung.com/spring-response-status) -- [Display RSS Feed with Spring MVC](http://www.baeldung.com/spring-mvc-rss-feed) diff --git a/spring-mvc-java/pom.xml b/spring-mvc-java/pom.xml index 83f2556fe0..9d3e0ca1b2 100644 --- a/spring-mvc-java/pom.xml +++ b/spring-mvc-java/pom.xml @@ -5,70 +5,54 @@ spring-mvc-java 0.1-SNAPSHOT spring-mvc-java + war - parent-boot-2 + parent-spring-5 com.baeldung 0.0.1-SNAPSHOT - ../parent-boot-2 + ../parent-spring-5 - - - org.springframework.boot - spring-boot-starter-thymeleaf + + org.springframework + spring-webmvc + ${spring.version} - - org.springframework.boot - spring-boot-starter-actuator - - - org.springframework.boot - spring-boot-devtools - - - org.springframework.boot - spring-boot-test - test - - - org.springframework.boot - spring-boot-starter-aop - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-websocket - - - - - com.fasterxml.jackson.core - jackson-databind - - - javax.servlet javax.servlet-api + 4.0.1 + + + javax.servlet.jsp + javax.servlet.jsp-api + 2.3.3 javax.servlet jstl + ${jstl.version} + + + + + com.fasterxml.jackson.core + jackson-databind + ${jackson-databind.version} org.aspectj aspectjrt + 1.9.1 org.aspectj aspectjweaver + 1.9.1 @@ -80,6 +64,7 @@ net.sourceforge.htmlunit htmlunit + 2.32 commons-logging @@ -104,10 +89,12 @@ com.jayway.jsonpath json-path test + 2.4.0 org.springframework spring-test + ${spring.version} test @@ -120,34 +107,29 @@ - org.hibernate + org.hibernate.validator hibernate-validator ${hibernate-validator.version} - - javax.el - javax.el-api - ${javax.el.version} - - - org.glassfish.web - javax.el - ${javax.el.version} - com.google.code.gson gson + 2.8.5 - - - - com.rometools - rome - ${rome.version} - + + org.springframework + spring-websocket + ${spring.version} + + + + org.springframework + spring-messaging + ${spring.version} + @@ -169,6 +151,7 @@ org.apache.maven.plugins maven-war-plugin + ${maven-war-plugin.version} false @@ -262,7 +245,7 @@ 3.0.9.RELEASE - 5.2.5.Final + 6.0.10.Final 5.1.40 @@ -281,24 +264,19 @@ 2.23 - 2.6 + 3.2.2 2.7 1.6.1 3.1.0 - 1.8.9 + 1.9.1 3.16-beta1 - - 1.10.0 - - 2.2.4 + 3.0.1-b06 - - com.baeldung.app.Application diff --git a/spring-mvc-java/src/main/java/com/baeldung/app/Application.java b/spring-mvc-java/src/main/java/com/baeldung/app/Application.java deleted file mode 100644 index 68d078de78..0000000000 --- a/spring-mvc-java/src/main/java/com/baeldung/app/Application.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.baeldung.app; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; -import org.springframework.context.annotation.ComponentScan; - -@EnableAutoConfiguration -@ComponentScan(value = {"com.baeldung.web.controller"}, resourcePattern = "**/FileUploadController.class") -@SpringBootApplication -public class Application extends SpringBootServletInitializer { - - public static void main(final String[] args) { - SpringApplication.run(Application.class, args); - } -} \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/com/baeldung/config/AppInitializer.java b/spring-mvc-java/src/main/java/com/baeldung/config/AppInitializer.java index 9cf6e384f1..eec12f466f 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/config/AppInitializer.java +++ b/spring-mvc-java/src/main/java/com/baeldung/config/AppInitializer.java @@ -14,13 +14,20 @@ public class AppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) throws ServletException { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); - context.setConfigLocation("org.baeldung.config"); + + context.scan("com.baeldung"); container.addListener(new ContextLoaderListener(context)); - ServletRegistration.Dynamic dispatcher = container.addServlet("java-servlet", new DispatcherServlet(context)); + ServletRegistration.Dynamic dispatcher = container.addServlet("mvc", new DispatcherServlet(context)); dispatcher.setLoadOnStartup(1); - dispatcher.addMapping("/java-servlet/*"); + dispatcher.addMapping("/"); + + // final MultipartConfigElement multipartConfigElement = new + // MultipartConfigElement(TMP_FOLDER, MAX_UPLOAD_SIZE, + // MAX_UPLOAD_SIZE * 2, MAX_UPLOAD_SIZE / 2); + // + // appServlet.setMultipartConfig(multipartConfigElement); } } diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ApplicationConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ApplicationConfig.java deleted file mode 100644 index 261d5793dc..0000000000 --- a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ApplicationConfig.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.baeldung.spring.web.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.ViewResolver; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; -import org.springframework.web.servlet.view.InternalResourceViewResolver; -import org.springframework.web.servlet.view.JstlView; - -@EnableWebMvc -@Configuration -@ComponentScan(basePackages = { "com.baeldung.web.controller" }) -public class ApplicationConfig extends WebMvcConfigurerAdapter { - - public ApplicationConfig() { - super(); - } - - @Override - public void addViewControllers(final ViewControllerRegistry registry) { - super.addViewControllers(registry); - registry.addViewController("/").setViewName("index"); - } - - @Bean - public ViewResolver viewResolver() { - final InternalResourceViewResolver bean = new InternalResourceViewResolver(); - bean.setViewClass(JstlView.class); - bean.setPrefix("/WEB-INF/jsp/"); - bean.setSuffix(".jsp"); - return bean; - } -} \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ClientWebConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ClientWebConfig.java deleted file mode 100644 index de47f9f69e..0000000000 --- a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ClientWebConfig.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.baeldung.spring.web.config; - -import javax.servlet.ServletContext; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.MessageSource; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Description; -import org.springframework.context.support.ResourceBundleMessageSource; -import org.springframework.web.servlet.ViewResolver; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; -import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -import org.springframework.web.servlet.view.InternalResourceViewResolver; -import org.springframework.web.servlet.view.JstlView; -import org.thymeleaf.spring4.SpringTemplateEngine; -import org.thymeleaf.spring4.view.ThymeleafViewResolver; -import org.thymeleaf.templateresolver.ServletContextTemplateResolver; - -@EnableWebMvc -@Configuration -public class ClientWebConfig implements WebMvcConfigurer { - - public ClientWebConfig() { - super(); - } - - // API - - @Autowired - private ServletContext ctx; - - @Override - public void addViewControllers(final ViewControllerRegistry registry) { - - registry.addViewController("/sample.html"); - } - - @Bean - public ViewResolver thymeleafViewResolver() { - final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); - viewResolver.setTemplateEngine(templateEngine()); - viewResolver.setOrder(1); - return viewResolver; - } - - @Bean - public ViewResolver viewResolver() { - final InternalResourceViewResolver bean = new InternalResourceViewResolver(); - bean.setViewClass(JstlView.class); - bean.setPrefix("/WEB-INF/view/"); - bean.setSuffix(".jsp"); - bean.setOrder(0); - return bean; - } - - @Bean - @Description("Thymeleaf template resolver serving HTML 5") - public ServletContextTemplateResolver templateResolver() { - final ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(ctx); - templateResolver.setPrefix("/WEB-INF/templates/"); - templateResolver.setSuffix(".html"); - templateResolver.setTemplateMode("HTML5"); - return templateResolver; - } - - @Bean - @Description("Thymeleaf template engine with Spring integration") - public SpringTemplateEngine templateEngine() { - final SpringTemplateEngine templateEngine = new SpringTemplateEngine(); - templateEngine.setTemplateResolver(templateResolver()); - return templateEngine; - } - - @Bean - @Description("Spring message resolver") - public MessageSource messageSource() { - final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); - messageSource.setBasename("messages"); - return messageSource; - } - - @Override - public void addResourceHandlers(final ResourceHandlerRegistry registry) { - registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); - } - -} \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ContentManagementWebConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ContentManagementWebConfig.java deleted file mode 100644 index 498105ded1..0000000000 --- a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ContentManagementWebConfig.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.baeldung.spring.web.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.http.MediaType; -import org.springframework.web.servlet.ViewResolver; -import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; -import org.springframework.web.servlet.view.InternalResourceViewResolver; -import org.springframework.web.servlet.view.JstlView; - -@EnableWebMvc -@Configuration -public class ContentManagementWebConfig extends WebMvcConfigurerAdapter { - - public ContentManagementWebConfig() { - super(); - } - - // API - - @Override - public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) { - configurer.favorPathExtension(false).favorParameter(true).parameterName("mediaType").ignoreAcceptHeader(true).useJaf(false).defaultContentType(MediaType.APPLICATION_JSON).mediaType("xml", MediaType.APPLICATION_XML).mediaType("json", - MediaType.APPLICATION_JSON); - } - - @Override - public void addViewControllers(final ViewControllerRegistry registry) { - super.addViewControllers(registry); - registry.addViewController("/sample.html"); - } - - @Bean - public ViewResolver viewResolver() { - final InternalResourceViewResolver bean = new InternalResourceViewResolver(); - bean.setViewClass(JstlView.class); - bean.setPrefix("/WEB-INF/view/"); - bean.setSuffix(".jsp"); - bean.setOrder(0); - return bean; - } - -} diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/MainWebAppInitializer.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/MainWebAppInitializer.java deleted file mode 100644 index 80ce22edd6..0000000000 --- a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/MainWebAppInitializer.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.baeldung.spring.web.config; - -import org.springframework.web.WebApplicationInitializer; -import org.springframework.web.context.ContextLoaderListener; -import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; -import org.springframework.web.context.support.GenericWebApplicationContext; -import org.springframework.web.servlet.DispatcherServlet; - -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletRegistration; -import java.util.Set; - -public class MainWebAppInitializer implements WebApplicationInitializer { - - private static final String TMP_FOLDER = "/tmp"; - private static final int MAX_UPLOAD_SIZE = 5 * 1024 * 1024; // 5 MB - - /** - * Register and configure all Servlet container components necessary to power the web application. - */ - @Override - public void onStartup(final ServletContext sc) throws ServletException { - - // Create the 'root' Spring application context - final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); - root.scan("com.baeldung.spring.web.config"); - // root.getEnvironment().setDefaultProfiles("embedded"); - - sc.addListener(new ContextLoaderListener(root)); - - // Handles requests into the application - final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext())); - appServlet.setLoadOnStartup(1); - - // final MultipartConfigElement multipartConfigElement = new - // MultipartConfigElement(TMP_FOLDER, MAX_UPLOAD_SIZE, - // MAX_UPLOAD_SIZE * 2, MAX_UPLOAD_SIZE / 2); - // - // appServlet.setMultipartConfig(multipartConfigElement); - - final Set mappingConflicts = appServlet.addMapping("/"); - if (!mappingConflicts.isEmpty()) { - throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278"); - } - } - -} diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java index 11be08a79d..191d721dfb 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java @@ -3,40 +3,105 @@ package com.baeldung.spring.web.config; import java.util.ArrayList; import java.util.List; +import javax.servlet.ServletContext; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Description; +import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.core.io.ClassPathResource; import org.springframework.http.MediaType; import org.springframework.http.converter.ByteArrayHttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.multipart.commons.CommonsMultipartResolver; import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; import org.springframework.web.servlet.view.ResourceBundleViewResolver; import org.springframework.web.servlet.view.XmlViewResolver; import org.springframework.web.util.UrlPathHelper; -import com.baeldung.excel.*; +import org.thymeleaf.spring4.SpringTemplateEngine; +import org.thymeleaf.spring4.view.ThymeleafViewResolver; +import org.thymeleaf.templateresolver.ServletContextTemplateResolver; + +import com.baeldung.excel.ExcelPOIHelper; -@Configuration @EnableWebMvc -@ComponentScan("com.baeldung.web") -public class WebConfig extends WebMvcConfigurerAdapter { +@Configuration +@ComponentScan(basePackages = { "com.baeldung.web.controller" }) +public class WebConfig implements WebMvcConfigurer { - public WebConfig() { - super(); + @Autowired + private ServletContext ctx; + + @Override + public void addViewControllers(final ViewControllerRegistry registry) { + registry.addViewController("/").setViewName("index"); + } + + @Bean + public ViewResolver thymeleafViewResolver() { + final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); + viewResolver.setTemplateEngine(templateEngine()); + viewResolver.setOrder(1); + return viewResolver; } - // @Bean - // public StandardServletMultipartResolver multipartResolver() { - // return new StandardServletMultipartResolver(); - // } + @Bean + public ViewResolver viewResolver() { + final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + bean.setViewClass(JstlView.class); + bean.setPrefix("/WEB-INF/view/"); + bean.setSuffix(".jsp"); + bean.setOrder(0); + return bean; + } + @Bean + @Description("Thymeleaf template resolver serving HTML 5") + public ServletContextTemplateResolver templateResolver() { + final ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(ctx); + templateResolver.setPrefix("/WEB-INF/templates/"); + templateResolver.setSuffix(".html"); + templateResolver.setTemplateMode("HTML5"); + return templateResolver; + } + + @Bean + @Description("Thymeleaf template engine with Spring integration") + public SpringTemplateEngine templateEngine() { + final SpringTemplateEngine templateEngine = new SpringTemplateEngine(); + templateEngine.setTemplateResolver(templateResolver()); + return templateEngine; + } + + @Bean + @Description("Spring message resolver") + public MessageSource messageSource() { + final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); + messageSource.setBasename("messages"); + return messageSource; + } + + @Override + public void addResourceHandlers(final ResourceHandlerRegistry registry) { + registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); + } + + @Override + public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) { + configurer.favorPathExtension(false).favorParameter(true).parameterName("mediaType").ignoreAcceptHeader(true).useRegisteredExtensionsOnly(false).defaultContentType(MediaType.APPLICATION_JSON).mediaType("xml", MediaType.APPLICATION_XML).mediaType("json", + MediaType.APPLICATION_JSON); + } @Bean(name = "multipartResolver") public CommonsMultipartResolver multipartResolver() { @@ -45,23 +110,7 @@ public class WebConfig extends WebMvcConfigurerAdapter { return multipartResolver; } - - @Override - public void addViewControllers(final ViewControllerRegistry registry) { - super.addViewControllers(registry); - registry.addViewController("/sample.html"); - } - - @Bean - public ViewResolver internalResourceViewResolver() { - final InternalResourceViewResolver bean = new InternalResourceViewResolver(); - bean.setViewClass(JstlView.class); - bean.setPrefix("/WEB-INF/view/"); - bean.setSuffix(".jsp"); - bean.setOrder(2); - return bean; - } - + @Bean public ViewResolver xmlViewResolver() { final XmlViewResolver bean = new XmlViewResolver(); @@ -112,5 +161,4 @@ public class WebConfig extends WebMvcConfigurerAdapter { public ExcelPOIHelper excelPOIHelper() { return new ExcelPOIHelper(); } - -} +} \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebSocketConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebSocketConfig.java index 93ec13da58..0793658e90 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebSocketConfig.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebSocketConfig.java @@ -2,13 +2,13 @@ package com.baeldung.spring.web.config; import org.springframework.context.annotation.Configuration; import org.springframework.messaging.simp.config.MessageBrokerRegistry; -import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; +import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; @Configuration @EnableWebSocketMessageBroker -public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { +public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { @Override public void configureMessageBroker(final MessageBrokerRegistry config) { diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebSocketSendToUserConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebSocketSendToUserConfig.java index 7f14380e5e..dbd52e20ba 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebSocketSendToUserConfig.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebSocketSendToUserConfig.java @@ -6,9 +6,9 @@ import org.springframework.http.server.ServerHttpResponse; import org.springframework.http.server.ServletServerHttpRequest; import org.springframework.messaging.simp.config.MessageBrokerRegistry; import org.springframework.web.socket.WebSocketHandler; -import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; +import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; import org.springframework.web.socket.server.support.DefaultHandshakeHandler; import javax.servlet.http.HttpSession; @@ -16,7 +16,7 @@ import java.util.Map; @Configuration @EnableWebSocketMessageBroker -public class WebSocketSendToUserConfig extends AbstractWebSocketMessageBrokerConfigurer { +public class WebSocketSendToUserConfig implements WebSocketMessageBrokerConfigurer { @Override public void configureMessageBroker(MessageBrokerRegistry config) { diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/SampleController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/SampleController.java new file mode 100644 index 0000000000..c13986e005 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/SampleController.java @@ -0,0 +1,13 @@ +package com.baeldung.web.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; + +@Controller +public class SampleController { + @GetMapping("/sample") + public String showForm() { + return "sample"; + } + +} diff --git a/spring-mvc-java/src/test/java/com/baeldung/web/controller/GreetControllerIntegrationTest.java b/spring-mvc-java/src/test/java/com/baeldung/web/controller/GreetControllerIntegrationTest.java index b628228b7e..384bd85ec6 100644 --- a/spring-mvc-java/src/test/java/com/baeldung/web/controller/GreetControllerIntegrationTest.java +++ b/spring-mvc-java/src/test/java/com/baeldung/web/controller/GreetControllerIntegrationTest.java @@ -20,12 +20,12 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import com.baeldung.spring.web.config.ApplicationConfig; +import com.baeldung.spring.web.config.WebConfig; import com.baeldung.spring.web.config.WebConfig; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration -@ContextConfiguration(classes = { ApplicationConfig.class, WebConfig.class }) +@ContextConfiguration(classes = { WebConfig.class, WebConfig.class }) public class GreetControllerIntegrationTest { @Autowired diff --git a/spring-reactive-kotlin/pom.xml b/spring-reactive-kotlin/pom.xml index 409457995a..bff39984e0 100644 --- a/spring-reactive-kotlin/pom.xml +++ b/spring-reactive-kotlin/pom.xml @@ -2,10 +2,9 @@ 4.0.0 - springreactivekotlin + spring-reactive-kotlin jar - - springreactivekotlin + Spring Reactive Kotlin Demo project for Spring Boot diff --git a/spring-remoting/pom.xml b/spring-remoting/pom.xml index 8db51e2a16..9f05bc91d2 100644 --- a/spring-remoting/pom.xml +++ b/spring-remoting/pom.xml @@ -32,7 +32,7 @@ remoting-hessian-burlap remoting-amqp remoting-jms - spring-remoting-rmi + remoting-rmi \ No newline at end of file diff --git a/spring-remoting/remoting-amqp/remoting-amqp-client/pom.xml b/spring-remoting/remoting-amqp/remoting-amqp-client/pom.xml index b715327c28..98e0bc5df5 100644 --- a/spring-remoting/remoting-amqp/remoting-amqp-client/pom.xml +++ b/spring-remoting/remoting-amqp/remoting-amqp-client/pom.xml @@ -26,7 +26,7 @@ com.baeldung - api + remoting-http-api ${project.version} diff --git a/spring-remoting/remoting-amqp/remoting-amqp-server/pom.xml b/spring-remoting/remoting-amqp/remoting-amqp-server/pom.xml index a456ba8d2c..7fe276f824 100644 --- a/spring-remoting/remoting-amqp/remoting-amqp-server/pom.xml +++ b/spring-remoting/remoting-amqp/remoting-amqp-server/pom.xml @@ -16,7 +16,7 @@ com.baeldung - api + remoting-http-api ${project.version} @@ -25,13 +25,13 @@ com.baeldung - spring-remoting-http-server - ${spring-remoting-http-server.version} + remoting-http-server + ${remoting-http-server.version} - 1.0-SNAPSHOT + 1.0-SNAPSHOT diff --git a/spring-remoting/remoting-hessian-burlap/pom.xml b/spring-remoting/remoting-hessian-burlap/pom.xml index 682e460880..6cb5a51812 100644 --- a/spring-remoting/remoting-hessian-burlap/pom.xml +++ b/spring-remoting/remoting-hessian-burlap/pom.xml @@ -2,19 +2,18 @@ - + 4.0.0 + remoting-hessian-burlap + pom + + spring-remoting com.baeldung 1.0-SNAPSHOT - pom + - server - client + remoting-hessian-burlap-server + remoting-hessian-burlap-client - 4.0.0 - - remoting-hessian-burlap - - \ No newline at end of file diff --git a/spring-remoting/remoting-hessian-burlap/client/pom.xml b/spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/pom.xml similarity index 95% rename from spring-remoting/remoting-hessian-burlap/client/pom.xml rename to spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/pom.xml index 1ae9b10019..f337d5e51f 100644 --- a/spring-remoting/remoting-hessian-burlap/client/pom.xml +++ b/spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/pom.xml @@ -24,7 +24,8 @@ ${project.groupId} - api + remoting-http-api + ${project.version} com.caucho diff --git a/spring-remoting/remoting-hessian-burlap/client/src/main/java/com/baeldung/client/BurlapClient.java b/spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/src/main/java/com/baeldung/client/BurlapClient.java similarity index 100% rename from spring-remoting/remoting-hessian-burlap/client/src/main/java/com/baeldung/client/BurlapClient.java rename to spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/src/main/java/com/baeldung/client/BurlapClient.java diff --git a/spring-remoting/remoting-hessian-burlap/client/src/main/java/com/baeldung/client/HessianClient.java b/spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/src/main/java/com/baeldung/client/HessianClient.java similarity index 100% rename from spring-remoting/remoting-hessian-burlap/client/src/main/java/com/baeldung/client/HessianClient.java rename to spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/src/main/java/com/baeldung/client/HessianClient.java diff --git a/meecrowave/src/main/resources/logback.xml b/spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/src/main/resources/logback.xml similarity index 100% rename from meecrowave/src/main/resources/logback.xml rename to spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/src/main/resources/logback.xml diff --git a/spring-remoting/remoting-hessian-burlap/client/src/test/java/com/baeldung/client/CabBookingServiceIntegrationTest.java b/spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/src/test/java/com/baeldung/client/CabBookingServiceIntegrationTest.java similarity index 100% rename from spring-remoting/remoting-hessian-burlap/client/src/test/java/com/baeldung/client/CabBookingServiceIntegrationTest.java rename to spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/src/test/java/com/baeldung/client/CabBookingServiceIntegrationTest.java diff --git a/spring-remoting/remoting-hessian-burlap/client/src/test/resources/application.properties b/spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/src/test/resources/application.properties similarity index 100% rename from spring-remoting/remoting-hessian-burlap/client/src/test/resources/application.properties rename to spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/src/test/resources/application.properties diff --git a/spring-remoting/remoting-hessian-burlap/server/pom.xml b/spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-server/pom.xml similarity index 94% rename from spring-remoting/remoting-hessian-burlap/server/pom.xml rename to spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-server/pom.xml index f1fed73ed8..5b6e03348f 100644 --- a/spring-remoting/remoting-hessian-burlap/server/pom.xml +++ b/spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-server/pom.xml @@ -14,7 +14,7 @@ com.baeldung - spring-remoting-http-server + remoting-http-server ${project.version} diff --git a/spring-remoting/remoting-hessian-burlap/server/src/main/java/com/baeldung/server/Server.java b/spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-server/src/main/java/com/baeldung/server/Server.java similarity index 100% rename from spring-remoting/remoting-hessian-burlap/server/src/main/java/com/baeldung/server/Server.java rename to spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-server/src/main/java/com/baeldung/server/Server.java diff --git a/spring-cloud/spring-cloud-stream-starters/boot/src/main/resources/logback.xml b/spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-server/src/main/resources/logback.xml similarity index 100% rename from spring-cloud/spring-cloud-stream-starters/boot/src/main/resources/logback.xml rename to spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-server/src/main/resources/logback.xml diff --git a/spring-remoting/remoting-http/pom.xml b/spring-remoting/remoting-http/pom.xml index 439f6782ba..3262736ec8 100644 --- a/spring-remoting/remoting-http/pom.xml +++ b/spring-remoting/remoting-http/pom.xml @@ -3,17 +3,19 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 + remoting-http + Parent for all modules related to HTTP Spring Remoting. + pom + com.baeldung spring-remoting 1.0-SNAPSHOT - spring-remoting-http - Parent for all modules related to HTTP Spring Remoting. - pom + - server - client - api + remoting-http-server + remoting-http-client + remoting-http-api \ No newline at end of file diff --git a/spring-remoting/remoting-http/api/pom.xml b/spring-remoting/remoting-http/remoting-http-api/pom.xml similarity index 77% rename from spring-remoting/remoting-http/api/pom.xml rename to spring-remoting/remoting-http/remoting-http-api/pom.xml index 633217f7de..579255060d 100644 --- a/spring-remoting/remoting-http/api/pom.xml +++ b/spring-remoting/remoting-http/remoting-http-api/pom.xml @@ -2,13 +2,15 @@ - - spring-remoting-http + 4.0.0 + remoting-http-api + remoting-http-api + API definition shared between client and server. + + + remoting-http com.baeldung 1.0-SNAPSHOT - 4.0.0 - api - spring-remoting-http-api - API definition shared between client and server. + \ No newline at end of file diff --git a/spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/Booking.java b/spring-remoting/remoting-http/remoting-http-api/src/main/java/com/baeldung/api/Booking.java similarity index 100% rename from spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/Booking.java rename to spring-remoting/remoting-http/remoting-http-api/src/main/java/com/baeldung/api/Booking.java diff --git a/spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/BookingException.java b/spring-remoting/remoting-http/remoting-http-api/src/main/java/com/baeldung/api/BookingException.java similarity index 100% rename from spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/BookingException.java rename to spring-remoting/remoting-http/remoting-http-api/src/main/java/com/baeldung/api/BookingException.java diff --git a/spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/CabBookingService.java b/spring-remoting/remoting-http/remoting-http-api/src/main/java/com/baeldung/api/CabBookingService.java similarity index 100% rename from spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/CabBookingService.java rename to spring-remoting/remoting-http/remoting-http-api/src/main/java/com/baeldung/api/CabBookingService.java diff --git a/spring-remoting/remoting-http/client/pom.xml b/spring-remoting/remoting-http/remoting-http-client/pom.xml similarity index 83% rename from spring-remoting/remoting-http/client/pom.xml rename to spring-remoting/remoting-http/remoting-http-client/pom.xml index 94a536fc18..9a6e98ffdd 100644 --- a/spring-remoting/remoting-http/client/pom.xml +++ b/spring-remoting/remoting-http/remoting-http-client/pom.xml @@ -3,14 +3,15 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - + remoting-http-client + Shows how to invoke a remote service using Spring Remoting HTTP. + com.baeldung - spring-remoting-http + remoting-http 1.0-SNAPSHOT - spring-remoting-http-client - Shows how to invoke a remote service using Spring Remoting HTTP. + org.springframework.boot @@ -24,7 +25,8 @@ ${project.groupId} - api + remoting-http-api + ${project.version} \ No newline at end of file diff --git a/spring-remoting/remoting-http/client/src/main/java/com/baeldug/client/Client.java b/spring-remoting/remoting-http/remoting-http-client/src/main/java/com/baeldug/client/Client.java similarity index 100% rename from spring-remoting/remoting-http/client/src/main/java/com/baeldug/client/Client.java rename to spring-remoting/remoting-http/remoting-http-client/src/main/java/com/baeldug/client/Client.java diff --git a/spring-data-5-reactive/src/main/resources/logback.xml b/spring-remoting/remoting-http/remoting-http-client/src/main/resources/logback.xml similarity index 100% rename from spring-data-5-reactive/src/main/resources/logback.xml rename to spring-remoting/remoting-http/remoting-http-client/src/main/resources/logback.xml diff --git a/spring-remoting/remoting-http/server/pom.xml b/spring-remoting/remoting-http/remoting-http-server/pom.xml similarity index 79% rename from spring-remoting/remoting-http/server/pom.xml rename to spring-remoting/remoting-http/remoting-http-server/pom.xml index 517f4451f7..53d27a5efa 100644 --- a/spring-remoting/remoting-http/server/pom.xml +++ b/spring-remoting/remoting-http/remoting-http-server/pom.xml @@ -3,13 +3,15 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 + remoting-http-server + Shows how to expose a service using Spring Remoting HTTP. + com.baeldung - spring-remoting-http + remoting-http 1.0-SNAPSHOT - spring-remoting-http-server - Shows how to expose a service using Spring Remoting HTTP. + org.springframework.boot @@ -17,7 +19,8 @@ ${project.groupId} - api + remoting-http-api + ${project.version} \ No newline at end of file diff --git a/spring-remoting/remoting-http/server/readme.md b/spring-remoting/remoting-http/remoting-http-server/readme.md similarity index 100% rename from spring-remoting/remoting-http/server/readme.md rename to spring-remoting/remoting-http/remoting-http-server/readme.md diff --git a/spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java b/spring-remoting/remoting-http/remoting-http-server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java similarity index 100% rename from spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java rename to spring-remoting/remoting-http/remoting-http-server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java diff --git a/spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/Server.java b/spring-remoting/remoting-http/remoting-http-server/src/main/java/com/baeldung/server/Server.java similarity index 100% rename from spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/Server.java rename to spring-remoting/remoting-http/remoting-http-server/src/main/java/com/baeldung/server/Server.java diff --git a/spring-remoting/remoting-hessian-burlap/client/src/main/resources/logback.xml b/spring-remoting/remoting-http/remoting-http-server/src/main/resources/logback.xml similarity index 100% rename from spring-remoting/remoting-hessian-burlap/client/src/main/resources/logback.xml rename to spring-remoting/remoting-http/remoting-http-server/src/main/resources/logback.xml diff --git a/spring-remoting/remoting-http/server/src/main/resources/logback.xml b/spring-remoting/remoting-http/server/src/main/resources/logback.xml deleted file mode 100644 index 7d900d8ea8..0000000000 --- a/spring-remoting/remoting-http/server/src/main/resources/logback.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - - \ No newline at end of file diff --git a/spring-remoting/remoting-jms/remoting-jms-client/pom.xml b/spring-remoting/remoting-jms/remoting-jms-client/pom.xml index 01b7bea657..ed27282a38 100644 --- a/spring-remoting/remoting-jms/remoting-jms-client/pom.xml +++ b/spring-remoting/remoting-jms/remoting-jms-client/pom.xml @@ -28,7 +28,8 @@ com.baeldung - api + remoting-http-api + ${project.version} diff --git a/spring-remoting/remoting-jms/remoting-jms-server/pom.xml b/spring-remoting/remoting-jms/remoting-jms-server/pom.xml index 8feac4df57..7657267f1d 100644 --- a/spring-remoting/remoting-jms/remoting-jms-server/pom.xml +++ b/spring-remoting/remoting-jms/remoting-jms-server/pom.xml @@ -28,11 +28,7 @@ com.baeldung - api - - - com.baeldung - api + remoting-http-api ${project.version} diff --git a/spring-remoting/spring-remoting-rmi/pom.xml b/spring-remoting/remoting-rmi/pom.xml similarity index 88% rename from spring-remoting/spring-remoting-rmi/pom.xml rename to spring-remoting/remoting-rmi/pom.xml index 723158775f..cd15a6ec60 100644 --- a/spring-remoting/spring-remoting-rmi/pom.xml +++ b/spring-remoting/remoting-rmi/pom.xml @@ -2,18 +2,18 @@ - + 4.0.0 + pom + remoting-rmi + + spring-remoting com.baeldung 1.0-SNAPSHOT - 4.0.0 - pom - + + remoting-rmi-server remoting-rmi-client - spring-remoting-rmi - - \ No newline at end of file diff --git a/spring-remoting/spring-remoting-rmi/remoting-rmi-client/pom.xml b/spring-remoting/remoting-rmi/remoting-rmi-client/pom.xml similarity index 86% rename from spring-remoting/spring-remoting-rmi/remoting-rmi-client/pom.xml rename to spring-remoting/remoting-rmi/remoting-rmi-client/pom.xml index 003976dc7b..1e3cd4b977 100644 --- a/spring-remoting/spring-remoting-rmi/remoting-rmi-client/pom.xml +++ b/spring-remoting/remoting-rmi/remoting-rmi-client/pom.xml @@ -3,7 +3,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - spring-remoting-rmi + remoting-rmi com.baeldung 1.0-SNAPSHOT @@ -24,7 +24,8 @@ com.baeldung - api + remoting-http-api + ${project.version} \ No newline at end of file diff --git a/spring-remoting/spring-remoting-rmi/remoting-rmi-client/src/main/java/com/baeldung/client/RmiClient.java b/spring-remoting/remoting-rmi/remoting-rmi-client/src/main/java/com/baeldung/client/RmiClient.java similarity index 100% rename from spring-remoting/spring-remoting-rmi/remoting-rmi-client/src/main/java/com/baeldung/client/RmiClient.java rename to spring-remoting/remoting-rmi/remoting-rmi-client/src/main/java/com/baeldung/client/RmiClient.java diff --git a/spring-remoting/remoting-hessian-burlap/server/src/main/resources/logback.xml b/spring-remoting/remoting-rmi/remoting-rmi-client/src/main/resources/logback.xml similarity index 100% rename from spring-remoting/remoting-hessian-burlap/server/src/main/resources/logback.xml rename to spring-remoting/remoting-rmi/remoting-rmi-client/src/main/resources/logback.xml diff --git a/spring-remoting/spring-remoting-rmi/remoting-rmi-server/pom.xml b/spring-remoting/remoting-rmi/remoting-rmi-server/pom.xml similarity index 83% rename from spring-remoting/spring-remoting-rmi/remoting-rmi-server/pom.xml rename to spring-remoting/remoting-rmi/remoting-rmi-server/pom.xml index f1633e5dc2..68b01829e1 100644 --- a/spring-remoting/spring-remoting-rmi/remoting-rmi-server/pom.xml +++ b/spring-remoting/remoting-rmi/remoting-rmi-server/pom.xml @@ -3,7 +3,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - spring-remoting-rmi + remoting-rmi com.baeldung 1.0-SNAPSHOT @@ -28,11 +28,7 @@ com.baeldung - api - - - com.baeldung - api + remoting-http-api ${project.version} diff --git a/spring-remoting/spring-remoting-rmi/remoting-rmi-server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java b/spring-remoting/remoting-rmi/remoting-rmi-server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java similarity index 100% rename from spring-remoting/spring-remoting-rmi/remoting-rmi-server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java rename to spring-remoting/remoting-rmi/remoting-rmi-server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java diff --git a/spring-remoting/spring-remoting-rmi/remoting-rmi-server/src/main/java/com/baeldung/server/RmiServer.java b/spring-remoting/remoting-rmi/remoting-rmi-server/src/main/java/com/baeldung/server/RmiServer.java similarity index 100% rename from spring-remoting/spring-remoting-rmi/remoting-rmi-server/src/main/java/com/baeldung/server/RmiServer.java rename to spring-remoting/remoting-rmi/remoting-rmi-server/src/main/java/com/baeldung/server/RmiServer.java diff --git a/spring-remoting/remoting-http/client/src/main/resources/logback.xml b/spring-remoting/remoting-rmi/remoting-rmi-server/src/main/resources/logback.xml similarity index 100% rename from spring-remoting/remoting-http/client/src/main/resources/logback.xml rename to spring-remoting/remoting-rmi/remoting-rmi-server/src/main/resources/logback.xml diff --git a/spring-remoting/spring-remoting-rmi/remoting-rmi-client/src/main/resources/logback.xml b/spring-remoting/spring-remoting-rmi/remoting-rmi-client/src/main/resources/logback.xml deleted file mode 100644 index 7d900d8ea8..0000000000 --- a/spring-remoting/spring-remoting-rmi/remoting-rmi-client/src/main/resources/logback.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - - \ No newline at end of file diff --git a/spring-remoting/spring-remoting-rmi/remoting-rmi-server/src/main/resources/logback.xml b/spring-remoting/spring-remoting-rmi/remoting-rmi-server/src/main/resources/logback.xml deleted file mode 100644 index 7d900d8ea8..0000000000 --- a/spring-remoting/spring-remoting-rmi/remoting-rmi-server/src/main/resources/logback.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - - \ No newline at end of file diff --git a/spring-rest-embedded-tomcat/pom.xml b/spring-rest-embedded-tomcat/pom.xml index 1a1adce6db..3cbbf63d94 100644 --- a/spring-rest-embedded-tomcat/pom.xml +++ b/spring-rest-embedded-tomcat/pom.xml @@ -2,8 +2,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.baeldung.embedded - SpringRestTomcat - 0.0.1-SNAPSHOT + spring-rest-embedded-tomcat spring-rest-embedded-tomcat war @@ -61,33 +60,8 @@ - - spring-rest-embedded-tomcat - - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - - 3 - true - - **/*IntegrationTest.java - **/*IntTest.java - **/*LongRunningUnitTest.java - **/*ManualTest.java - **/JdbcTest.java - **/*LiveTest.java - - - - - - 2.9.2 - 1.8 - 1.8 4.0.0 9.0.1 4.5.3 diff --git a/spring-rest-shell/pom.xml b/spring-rest-shell/pom.xml index 2f7d1c8933..7a604946b6 100644 --- a/spring-rest-shell/pom.xml +++ b/spring-rest-shell/pom.xml @@ -2,9 +2,7 @@ 4.0.0 - com.baeldung spring-rest-shell - 0.0.1-SNAPSHOT jar spring-rest-shell A simple project to demonstrate Spring REST Shell features. @@ -48,10 +46,4 @@ - - UTF-8 - UTF-8 - 1.8 - - diff --git a/spring-roo/pom.xml b/spring-roo/pom.xml index e689ab11aa..c205c98dc0 100644 --- a/spring-roo/pom.xml +++ b/spring-roo/pom.xml @@ -4,9 +4,9 @@ 4.0.0 com.baeldung - roo + spring-roo 1.0.0.BUILD-SNAPSHOT - roo + Spring Roo jar diff --git a/spring-security-anguar/client/anguarjs/app.js b/spring-security-anguar/client/anguarjs/app.js deleted file mode 100644 index 4b7268da3f..0000000000 --- a/spring-security-anguar/client/anguarjs/app.js +++ /dev/null @@ -1,40 +0,0 @@ -(function () { - 'use strict'; - - angular - .module('app', ['ngRoute']) - .config(config) - .run(run); - - config.$inject = ['$routeProvider', '$locationProvider']; - function config($routeProvider, $locationProvider) { - $routeProvider - .when('/', { - controller: 'HomeController', - templateUrl: 'home/home.view.html', - controllerAs: 'vm' - }) - .when('/login', { - controller: 'LoginController', - templateUrl: 'login/login.view.html', - controllerAs: 'vm' - }) - .otherwise({ redirectTo: '/login' }); - } - - run.$inject = ['$rootScope', '$location', '$http', '$window']; - function run($rootScope, $location, $http, $window) { - var userData = $window.sessionStorage.getItem('userData'); - if (userData) { - $http.defaults.headers.common['Authorization'] = 'Basic ' + JSON.parse(userData).authData; - } - - $rootScope.$on('$locationChangeStart', function (event, next, current) { - var restrictedPage = $.inArray($location.path(), ['/login']) === -1; - var loggedIn = $window.sessionStorage.getItem('userData');; - if (restrictedPage && !loggedIn) { - $location.path('/login'); - } - }); - } -})(); \ No newline at end of file diff --git a/spring-security-anguar/client/anguarjs/home/home.controller.js b/spring-security-anguar/client/anguarjs/home/home.controller.js deleted file mode 100644 index 6449029ec2..0000000000 --- a/spring-security-anguar/client/anguarjs/home/home.controller.js +++ /dev/null @@ -1,34 +0,0 @@ -(function () { - 'use strict'; - - angular - .module('app') - .controller('HomeController', HomeController); - - HomeController.$inject = ['$window', '$http', '$scope']; - function HomeController($window, $http, $scope) { - var vm = this; - - vm.user = null; - - initController(); - - function initController() { - - $http({ - url: 'http://localhost:8082/user', - method: "GET" - }).then(function (response) { - vm.user = response.data.name; - },function(error){ - console.log(error); - }); - }; - - $scope.logout = function(){ - $window.sessionStorage.setItem('userData', ''); - $http.defaults.headers.common['Authorization'] = 'Basic'; - } - } - -})(); \ No newline at end of file diff --git a/spring-security-anguar/client/anguarjs/home/home.view.html b/spring-security-anguar/client/anguarjs/home/home.view.html deleted file mode 100644 index bb5c2d3dbf..0000000000 --- a/spring-security-anguar/client/anguarjs/home/home.view.html +++ /dev/null @@ -1,3 +0,0 @@ -

Hi {{vm.user}}!

-

You're logged in!!

-

Logout

\ No newline at end of file diff --git a/spring-security-anguar/client/anguarjs/index.html b/spring-security-anguar/client/anguarjs/index.html deleted file mode 100644 index a5584d53be..0000000000 --- a/spring-security-anguar/client/anguarjs/index.html +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - -
- - - - - - - - - - \ No newline at end of file diff --git a/spring-security-anguar/client/anguarjs/login/login.controller.js b/spring-security-anguar/client/anguarjs/login/login.controller.js deleted file mode 100644 index 2da509ec83..0000000000 --- a/spring-security-anguar/client/anguarjs/login/login.controller.js +++ /dev/null @@ -1,38 +0,0 @@ -(function () { - 'use strict'; - - angular - .module('app') - .controller('LoginController', LoginController); - - LoginController.$inject = ['$location', '$window', '$http']; - function LoginController($location, $window, $http) { - var vm = this; - vm.login = login; - - (function initController() { - $window.localStorage.setItem('token', ''); - })(); - - function login() { - $http({ - url: 'http://localhost:8082/login', - method: "POST", - data: { 'userName': vm.username, 'password': vm.password } - }).then(function (response) { - if (response.data) { - var token = $window.btoa(vm.username + ':' + vm.password); - var userData = { - userName: vm.username, - authData: token - } - $window.sessionStorage.setItem('userData', JSON.stringify(userData)); - $http.defaults.headers.common['Authorization'] = 'Basic ' + token; - $location.path('/'); - } else { - alert("Authentication failed.") - } - }); - }; - } -})(); diff --git a/spring-security-anguar/client/anguarjs/login/login.view.html b/spring-security-anguar/client/anguarjs/login/login.view.html deleted file mode 100644 index 0d42f29b54..0000000000 --- a/spring-security-anguar/client/anguarjs/login/login.view.html +++ /dev/null @@ -1,18 +0,0 @@ -
-

Login

-
-
- - - Username is required -
-
- - - Password is required -
-
- -
-
-
\ No newline at end of file diff --git a/spring-security-anguar/client/angular2/app.css b/spring-security-anguar/client/angular2/app.css deleted file mode 100644 index cdd2d591d8..0000000000 --- a/spring-security-anguar/client/angular2/app.css +++ /dev/null @@ -1,7 +0,0 @@ -a { - cursor: pointer; -} - -.help-block { - font-size: 12px; -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular2/app/app.component.html b/spring-security-anguar/client/angular2/app/app.component.html deleted file mode 100644 index 7f77adea7b..0000000000 --- a/spring-security-anguar/client/angular2/app/app.component.html +++ /dev/null @@ -1,7 +0,0 @@ -
-
-
- -
-
-
diff --git a/spring-security-anguar/client/angular2/app/app.component.ts b/spring-security-anguar/client/angular2/app/app.component.ts deleted file mode 100644 index 0aae2b6992..0000000000 --- a/spring-security-anguar/client/angular2/app/app.component.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'app', - templateUrl: './app/app.component.html' -}) - -export class AppComponent { } \ No newline at end of file diff --git a/spring-security-anguar/client/angular2/app/app.module.ts b/spring-security-anguar/client/angular2/app/app.module.ts deleted file mode 100644 index 4d484b49f8..0000000000 --- a/spring-security-anguar/client/angular2/app/app.module.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { NgModule } from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; -import { FormsModule } from '@angular/forms'; -import { HttpModule } from '@angular/http'; - -import { AppComponent } from './app.component'; -import { routing } from './app.routing'; - -import { HomeComponent } from './home/home.component'; -import { LoginComponent } from './login/login.component'; - -@NgModule({ - imports: [ - BrowserModule, - FormsModule, - HttpModule, - routing - ], - declarations: [ - AppComponent, - HomeComponent, - LoginComponent - ], - bootstrap: [AppComponent] -}) - -export class AppModule { } \ No newline at end of file diff --git a/spring-security-anguar/client/angular2/app/app.routing.ts b/spring-security-anguar/client/angular2/app/app.routing.ts deleted file mode 100644 index c794fc5c50..0000000000 --- a/spring-security-anguar/client/angular2/app/app.routing.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Routes, RouterModule } from '@angular/router'; - -import { HomeComponent } from './home/home.component'; -import { LoginComponent } from './login/login.component'; - -const appRoutes: Routes = [ - { path: '', component: HomeComponent}, - { path: 'login', component: LoginComponent }, - { path: '**', redirectTo: '' } -]; - -export const routing = RouterModule.forRoot(appRoutes); \ No newline at end of file diff --git a/spring-security-anguar/client/angular2/app/home/home.component.html b/spring-security-anguar/client/angular2/app/home/home.component.html deleted file mode 100644 index 5f3b24be3a..0000000000 --- a/spring-security-anguar/client/angular2/app/home/home.component.html +++ /dev/null @@ -1,4 +0,0 @@ -
-

Hi {{userName}}!

-

Logout

-
\ No newline at end of file diff --git a/spring-security-anguar/client/angular2/app/home/home.component.ts b/spring-security-anguar/client/angular2/app/home/home.component.ts deleted file mode 100644 index 1b168bfb30..0000000000 --- a/spring-security-anguar/client/angular2/app/home/home.component.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { Component, OnInit } from '@angular/core'; -import { Http, RequestOptions, Headers } from '@angular/http'; -import 'rxjs/add/operator/map' - -@Component({ - selector:'home', - templateUrl: './app/home/home.component.html' -}) - -export class HomeComponent implements OnInit { - - userName: string; - - constructor(private http: Http) { } - - ngOnInit() { - let url = 'http://localhost:8082/user'; - let headers = new Headers({ - 'Authorization': 'Basic ' + sessionStorage.getItem('token') - }); - let options = new RequestOptions({ headers: headers }); - this.http.post(url,{}, options). - map(res => res.json()). - subscribe( - principal => this.userName = principal.name, - error => { - if(error.status == 401) - alert('Unauthorized'); - } - ); - } - - logout() { - sessionStorage.setItem('token', ''); - } -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular2/app/login/login.component.html b/spring-security-anguar/client/angular2/app/login/login.component.html deleted file mode 100644 index d87b91a7bb..0000000000 --- a/spring-security-anguar/client/angular2/app/login/login.component.html +++ /dev/null @@ -1,18 +0,0 @@ -
-

Login

-
-
- - -
Username is required
-
-
- - -
Password is required
-
-
- -
-
-
diff --git a/spring-security-anguar/client/angular2/app/login/login.component.ts b/spring-security-anguar/client/angular2/app/login/login.component.ts deleted file mode 100644 index 81d6c1d7ae..0000000000 --- a/spring-security-anguar/client/angular2/app/login/login.component.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { Component, OnInit } from '@angular/core'; -import { Router, ActivatedRoute } from '@angular/router'; -import { Http } from '@angular/http'; - -@Component({ - selector: 'login', - templateUrl: './app/login/login.component.html' -}) - -export class LoginComponent implements OnInit { - model: any = {}; - - constructor( - private route: ActivatedRoute, - private router: Router, - private http: Http) { } - - ngOnInit() { - sessionStorage.setItem('token', ''); - } - - login() { - let url = 'http://localhost:8082/login'; - let result = this.http.post(url, { - userName: this.model.username, - password: this.model.password - }).map(res => res.json()).subscribe(isValid => { - if (isValid) { - sessionStorage.setItem('token', btoa(this.model.username + ':' + this.model.password)); - this.router.navigate(['']); - } else { - alert("Authentication failed."); - } - }); - } -} diff --git a/spring-security-anguar/client/angular2/app/main.ts b/spring-security-anguar/client/angular2/app/main.ts deleted file mode 100644 index 3a6c754786..0000000000 --- a/spring-security-anguar/client/angular2/app/main.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; - -import { AppModule } from './app.module'; - -platformBrowserDynamic().bootstrapModule(AppModule); \ No newline at end of file diff --git a/spring-security-anguar/client/angular2/index.html b/spring-security-anguar/client/angular2/index.html deleted file mode 100644 index c9730a8568..0000000000 --- a/spring-security-anguar/client/angular2/index.html +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - Loading... - - diff --git a/spring-security-anguar/client/angular2/package.json b/spring-security-anguar/client/angular2/package.json deleted file mode 100644 index 9b36652fac..0000000000 --- a/spring-security-anguar/client/angular2/package.json +++ /dev/null @@ -1,37 +0,0 @@ -{ - "name": "angular2-quickstart", - "version": "1.0.0", - "description": "Sample of how easy and fast to start hacking with Angular2 and ng2-bootstrap", - "scripts": { - "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ", - "lite": "lite-server", - "tsc": "tsc", - "tsc:w": "tsc -w" - }, - "license": "ISC", - "dependencies": { - "@angular/common": "2.4.4", - "@angular/compiler": "2.4.4", - "@angular/core": "2.4.4", - "@angular/forms": "2.4.4", - "@angular/http": "2.4.4", - "@angular/platform-browser": "2.4.4", - "@angular/platform-browser-dynamic": "2.4.4", - "@angular/router": "3.4.4", - "@angular/upgrade": "2.4.4", - "angular2-in-memory-web-api": "0.0.21", - "bootstrap": "^3.3.7", - "core-js": "^2.4.1", - "ng2-bootstrap": "1.3.0", - "reflect-metadata": "^0.1.8", - "rxjs": "5.0.3", - "systemjs": "0.20.0", - "zone.js": "0.7.6" - }, - "devDependencies": { - "concurrently": "3.1.0", - "lite-server": "^2.2.0", - "typescript": "2.1.5" - }, - "repository": {} -} diff --git a/spring-security-anguar/client/angular2/systemjs.config.js b/spring-security-anguar/client/angular2/systemjs.config.js deleted file mode 100644 index ea4c543fdd..0000000000 --- a/spring-security-anguar/client/angular2/systemjs.config.js +++ /dev/null @@ -1,41 +0,0 @@ -/** - * System configuration for Angular 2 samples - * Adjust as necessary for your application needs. - */ -(function (global) { - System.config({ - paths: { - // paths serve as alias - 'npm:': 'node_modules/' - }, - // map tells the System loader where to look for things - map: { - // our app is within the app folder - app: 'app', - - // angular bundles - '@angular/core': 'npm:@angular/core/bundles/core.umd.js', - '@angular/common': 'npm:@angular/common/bundles/common.umd.js', - '@angular/http': 'npm:@angular/http/bundles/http.umd.js', - '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js', - '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js', - '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js', - '@angular/router': 'npm:@angular/router/bundles/router.umd.js', - '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js', - - // other libraries - 'rxjs': 'npm:rxjs', - 'tslib': 'npm:tslib/tslib.js' - }, - // packages tells the System loader how to load when no filename and/or no extension - packages: { - app: { - main: './main.js', - defaultExtension: 'js' - }, - rxjs: { - defaultExtension: 'js' - } - } - }); -})(this); diff --git a/spring-security-anguar/client/angular2/tsconfig.json b/spring-security-anguar/client/angular2/tsconfig.json deleted file mode 100644 index 55f9ad70c3..0000000000 --- a/spring-security-anguar/client/angular2/tsconfig.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "compilerOptions": { - "emitDecoratorMetadata": true, - "experimentalDecorators": true, - "lib": [ "es2015", "dom" ], - "module": "commonjs", - "moduleResolution": "node", - "noImplicitAny": true, - "sourceMap": true, - "suppressImplicitAnyIndexErrors": true, - "target": "es5" - }, - "exclude": [ - "node_modules/*" - ] -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/.angular-cli.json b/spring-security-anguar/client/angular4/.angular-cli.json deleted file mode 100644 index 967934b2f7..0000000000 --- a/spring-security-anguar/client/angular4/.angular-cli.json +++ /dev/null @@ -1,65 +0,0 @@ -{ - "$schema": "./node_modules/@angular/cli/lib/config/schema.json", - "project": { - "name": "angular-crud" - }, - "apps": [ - { - "root": "src", - "outDir": "dist", - "assets": [ - "assets", - "favicon.ico" - ], - "index": "index.html", - "main": "main.ts", - "polyfills": "polyfills.ts", - "test": "test.ts", - "tsconfig": "tsconfig.app.json", - "testTsconfig": "tsconfig.spec.json", - "prefix": "app", - "styles": [ - "../node_modules/bootstrap/dist/css/bootstrap.min.css", - "styles.css", - "../node_modules/font-awesome/css/font-awesome.min.css" - ], - "scripts": [ - "../node_modules/jquery/dist/jquery.js", - "../node_modules/bootstrap/dist/js/bootstrap.js" - ], - "environmentSource": "environments/environment.ts", - "environments": { - "dev": "environments/environment.ts", - "prod": "environments/environment.prod.ts" - } - } - ], - "e2e": { - "protractor": { - "config": "./protractor.conf.js" - } - }, - "lint": [ - { - "project": "src/tsconfig.app.json", - "exclude": "**/node_modules/**" - }, - { - "project": "src/tsconfig.spec.json", - "exclude": "**/node_modules/**" - }, - { - "project": "e2e/tsconfig.e2e.json", - "exclude": "**/node_modules/**" - } - ], - "test": { - "karma": { - "config": "./karma.conf.js" - } - }, - "defaults": { - "styleExt": "css", - "component": {} - } -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/package.json b/spring-security-anguar/client/angular4/package.json deleted file mode 100644 index 8c6ddcaf1f..0000000000 --- a/spring-security-anguar/client/angular4/package.json +++ /dev/null @@ -1,54 +0,0 @@ -{ - "name": "angular-crud", - "version": "0.0.0", - "license": "MIT", - "scripts": { - "ng": "ng", - "start": "ng serve", - "build": "ng build", - "test": "ng test", - "lint": "ng lint", - "e2e": "ng e2e" - }, - "private": true, - "dependencies": { - "@angular/animations": "^4.0.0", - "@angular/common": "^4.0.0", - "@angular/compiler": "^4.0.0", - "@angular/core": "^4.0.0", - "@angular/forms": "^4.0.0", - "@angular/http": "^4.0.0", - "@angular/platform-browser": "^4.0.0", - "@angular/platform-browser-dynamic": "^4.0.0", - "@angular/router": "^4.0.0", - "bootstrap": "^3.3.7", - "core-js": "^2.4.1", - "font-awesome": "^4.7.0", - "jquery": "^3.2.1", - "lodash": "^4.17.4", - "rxjs": "^5.4.1", - "zone.js": "^0.8.14" - }, - "devDependencies": { - "@angular/cli": "1.2.3", - "@angular/compiler-cli": "^4.0.0", - "@angular/language-service": "^4.0.0", - "@types/jasmine": "~2.5.53", - "@types/jasminewd2": "~2.0.2", - "@types/lodash": "^4.14.66", - "@types/node": "~6.0.60", - "codelyzer": "~3.0.1", - "jasmine-core": "~2.6.2", - "jasmine-spec-reporter": "~4.1.0", - "karma": "~1.7.0", - "karma-chrome-launcher": "~2.1.1", - "karma-cli": "~1.0.1", - "karma-coverage-istanbul-reporter": "^1.2.1", - "karma-jasmine": "~1.1.0", - "karma-jasmine-html-reporter": "^0.2.2", - "protractor": "~5.1.2", - "ts-node": "~3.0.4", - "tslint": "~5.3.2", - "typescript": "~2.3.3" - } -} diff --git a/spring-security-anguar/client/angular4/src/app/app.component.html b/spring-security-anguar/client/angular4/src/app/app.component.html deleted file mode 100644 index 7f77adea7b..0000000000 --- a/spring-security-anguar/client/angular4/src/app/app.component.html +++ /dev/null @@ -1,7 +0,0 @@ -
-
-
- -
-
-
diff --git a/spring-security-anguar/client/angular4/src/app/app.component.ts b/spring-security-anguar/client/angular4/src/app/app.component.ts deleted file mode 100644 index 36f986c63f..0000000000 --- a/spring-security-anguar/client/angular4/src/app/app.component.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'app-root', - templateUrl: './app.component.html' -}) - -export class AppComponent { } \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/app/app.module.ts b/spring-security-anguar/client/angular4/src/app/app.module.ts deleted file mode 100644 index 9e840e6f13..0000000000 --- a/spring-security-anguar/client/angular4/src/app/app.module.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { NgModule } from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; -import { FormsModule } from '@angular/forms'; -import { HttpModule } from '@angular/http'; - -// used to create fake backend - -import { AppComponent } from './app.component'; -import { routing } from './app.routing'; - -import { HomeComponent } from './home/home.component'; -import { LoginComponent } from './login/login.component'; - -@NgModule({ - imports: [ - BrowserModule, - FormsModule, - HttpModule, - routing - ], - declarations: [ - AppComponent, - HomeComponent, - LoginComponent - ], - bootstrap: [AppComponent] -}) - -export class AppModule { } \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/app/app.routing.ts b/spring-security-anguar/client/angular4/src/app/app.routing.ts deleted file mode 100644 index c794fc5c50..0000000000 --- a/spring-security-anguar/client/angular4/src/app/app.routing.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Routes, RouterModule } from '@angular/router'; - -import { HomeComponent } from './home/home.component'; -import { LoginComponent } from './login/login.component'; - -const appRoutes: Routes = [ - { path: '', component: HomeComponent}, - { path: 'login', component: LoginComponent }, - { path: '**', redirectTo: '' } -]; - -export const routing = RouterModule.forRoot(appRoutes); \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/app/home/home.component.html b/spring-security-anguar/client/angular4/src/app/home/home.component.html deleted file mode 100644 index 7ccd2c2a3a..0000000000 --- a/spring-security-anguar/client/angular4/src/app/home/home.component.html +++ /dev/null @@ -1,4 +0,0 @@ -
-

Hi {{userName}}!

-

Logout

-
\ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/app/home/home.component.ts b/spring-security-anguar/client/angular4/src/app/home/home.component.ts deleted file mode 100644 index 0f66f42ce2..0000000000 --- a/spring-security-anguar/client/angular4/src/app/home/home.component.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { Component, OnInit } from '@angular/core'; -import { Http, RequestOptions, Headers } from '@angular/http'; -import 'rxjs/add/operator/map' - -@Component({ - selector:'home', - templateUrl: './home.component.html' -}) - -export class HomeComponent implements OnInit { - - userName: string; - - constructor(private http: Http) { } - - ngOnInit() { - let url = 'http://localhost:8082/user'; - let headers = new Headers({ - 'Authorization': 'Basic ' + sessionStorage.getItem('token') - }); - let options = new RequestOptions({ headers: headers }); - this.http.post(url,{}, options). - map( - res => res.json(), - error => { - if(error.status == 401) - alert('Unauthorized'); - } - ).subscribe(principal => { - this.userName = principal.name; - }); - } - - logout() { - sessionStorage.setItem('token', ''); - } -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/app/login/login.component.html b/spring-security-anguar/client/angular4/src/app/login/login.component.html deleted file mode 100644 index d87b91a7bb..0000000000 --- a/spring-security-anguar/client/angular4/src/app/login/login.component.html +++ /dev/null @@ -1,18 +0,0 @@ -
-

Login

-
-
- - -
Username is required
-
-
- - -
Password is required
-
-
- -
-
-
diff --git a/spring-security-anguar/client/angular4/src/app/login/login.component.ts b/spring-security-anguar/client/angular4/src/app/login/login.component.ts deleted file mode 100644 index 2a2dc102af..0000000000 --- a/spring-security-anguar/client/angular4/src/app/login/login.component.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { Component, OnInit } from '@angular/core'; -import { Router, ActivatedRoute } from '@angular/router'; -import { Http } from '@angular/http'; - -@Component({ - selector: 'login', - templateUrl: './login.component.html' -}) - -export class LoginComponent implements OnInit { - model: any = {}; - - constructor( - private route: ActivatedRoute, - private router: Router, - private http: Http) { } - - ngOnInit() { - sessionStorage.setItem('token', ''); - } - - - login() { - let url = 'http://localhost:8082/login'; - let result = this.http.post(url, { - userName: this.model.username, - password: this.model.password - }). - map(res => res.json()). - subscribe(isValid => { - if (isValid) { - sessionStorage.setItem('token', btoa(this.model.username + ':' + this.model.password)); - this.router.navigate(['']); - } else { - alert("Authentication failed.") - } - }); - } -} diff --git a/spring-security-anguar/client/angular4/src/index.html b/spring-security-anguar/client/angular4/src/index.html deleted file mode 100644 index c716820396..0000000000 --- a/spring-security-anguar/client/angular4/src/index.html +++ /dev/null @@ -1,14 +0,0 @@ - - - - - AngularCRUD - - - - - - - - - \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/main.ts b/spring-security-anguar/client/angular4/src/main.ts deleted file mode 100644 index 49db98ae89..0000000000 --- a/spring-security-anguar/client/angular4/src/main.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; - -import { AppModule } from './app/app.module'; - -platformBrowserDynamic().bootstrapModule(AppModule); \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/polyfills.ts b/spring-security-anguar/client/angular4/src/polyfills.ts deleted file mode 100644 index a4984ced57..0000000000 --- a/spring-security-anguar/client/angular4/src/polyfills.ts +++ /dev/null @@ -1,68 +0,0 @@ -/** - * This file includes polyfills needed by Angular and is loaded before the app. - * You can add your own extra polyfills to this file. - * - * This file is divided into 2 sections: - * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. - * 2. Application imports. Files imported after ZoneJS that should be loaded before your main - * file. - * - * The current setup is for so-called "evergreen" browsers; the last versions of browsers that - * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), - * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. - * - * Learn more in https://angular.io/docs/ts/latest/guide/browser-support.html - */ - -/*************************************************************************************************** - * BROWSER POLYFILLS - */ - -/** IE9, IE10 and IE11 requires all of the following polyfills. **/ -// import 'core-js/es6/symbol'; -// import 'core-js/es6/object'; -// import 'core-js/es6/function'; -// import 'core-js/es6/parse-int'; -// import 'core-js/es6/parse-float'; -// import 'core-js/es6/number'; -// import 'core-js/es6/math'; -// import 'core-js/es6/string'; -// import 'core-js/es6/date'; -// import 'core-js/es6/array'; -// import 'core-js/es6/regexp'; -// import 'core-js/es6/map'; -// import 'core-js/es6/weak-map'; -// import 'core-js/es6/set'; -/** IE10 and IE11 requires the following for NgClass support on SVG elements */ -// import 'classlist.js'; // Run `npm install --save classlist.js`. -/** Evergreen browsers require these. **/ -import 'core-js/es6/reflect'; -import 'core-js/es7/reflect'; - - -/** - * Required to support Web Animations `@angular/animation`. - * Needed for: All but Chrome, Firefox and Opera. http://caniuse.com/#feat=web-animation - **/ -// import 'web-animations-js'; // Run `npm install --save web-animations-js`. - - -/*************************************************************************************************** - * Zone JS is required by Angular itself. - */ -import 'zone.js/dist/zone'; // Included with Angular CLI. - - -/*************************************************************************************************** - * APPLICATION IMPORTS - */ - -/** - * Date, currency, decimal and percent pipes. - * Needed for: All but Chrome, Firefox, Edge, IE11 and Safari 10 - */ -// import 'intl'; // Run `npm install --save intl`. -/** - * Need to import at least one locale-data with intl. - */ -// import 'intl/locale-data/jsonp/en'; \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/styles.css b/spring-security-anguar/client/angular4/src/styles.css deleted file mode 100644 index cdd2d591d8..0000000000 --- a/spring-security-anguar/client/angular4/src/styles.css +++ /dev/null @@ -1,7 +0,0 @@ -a { - cursor: pointer; -} - -.help-block { - font-size: 12px; -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/tsconfig.app.json b/spring-security-anguar/client/angular4/src/tsconfig.app.json deleted file mode 100644 index 213ce42a1b..0000000000 --- a/spring-security-anguar/client/angular4/src/tsconfig.app.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "extends": "../tsconfig.json", - "compilerOptions": { - "outDir": "../out-tsc/app", - "baseUrl": "./", - "module": "es2015", - "types": [] - }, - "exclude": [ - "test.ts", - "**/*.spec.ts" - ] -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/tsconfig.json b/spring-security-anguar/client/angular4/tsconfig.json deleted file mode 100644 index ef44e2862b..0000000000 --- a/spring-security-anguar/client/angular4/tsconfig.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "compileOnSave": false, - "compilerOptions": { - "baseUrl": "./", - "outDir": "./dist/out-tsc", - "sourceMap": true, - "declaration": false, - "moduleResolution": "node", - "emitDecoratorMetadata": true, - "experimentalDecorators": true, - "target": "es5", - "typeRoots": [ - "node_modules/@types" - ], - "lib": [ - "es2017", - "dom" - ] - } -} diff --git a/spring-security-anguar/client/angular4/tslint.json b/spring-security-anguar/client/angular4/tslint.json deleted file mode 100644 index 3ea984c776..0000000000 --- a/spring-security-anguar/client/angular4/tslint.json +++ /dev/null @@ -1,130 +0,0 @@ -{ - "rulesDirectory": [ - "node_modules/codelyzer" - ], - "rules": { - "arrow-return-shorthand": true, - "callable-types": true, - "class-name": true, - "comment-format": [ - true, - "check-space" - ], - "curly": true, - "deprecation": { - "severity": "warn" - }, - "eofline": true, - "forin": true, - "import-blacklist": [ - true, - "rxjs/Rx" - ], - "import-spacing": true, - "indent": [ - true, - "spaces" - ], - "interface-over-type-literal": true, - "label-position": true, - "max-line-length": [ - true, - 140 - ], - "member-access": false, - "member-ordering": [ - true, - { - "order": [ - "static-field", - "instance-field", - "static-method", - "instance-method" - ] - } - ], - "no-arg": true, - "no-bitwise": true, - "no-console": [ - true, - "debug", - "info", - "time", - "timeEnd", - "trace" - ], - "no-construct": true, - "no-debugger": true, - "no-duplicate-super": true, - "no-empty": false, - "no-empty-interface": true, - "no-eval": true, - "no-inferrable-types": [ - true, - "ignore-params" - ], - "no-misused-new": true, - "no-non-null-assertion": true, - "no-shadowed-variable": true, - "no-string-literal": false, - "no-string-throw": true, - "no-switch-case-fall-through": true, - "no-trailing-whitespace": true, - "no-unnecessary-initializer": true, - "no-unused-expression": true, - "no-use-before-declare": true, - "no-var-keyword": true, - "object-literal-sort-keys": false, - "one-line": [ - true, - "check-open-brace", - "check-catch", - "check-else", - "check-whitespace" - ], - "prefer-const": true, - "quotemark": [ - true, - "single" - ], - "radix": true, - "semicolon": [ - true, - "always" - ], - "triple-equals": [ - true, - "allow-null-check" - ], - "typedef-whitespace": [ - true, - { - "call-signature": "nospace", - "index-signature": "nospace", - "parameter": "nospace", - "property-declaration": "nospace", - "variable-declaration": "nospace" - } - ], - "unified-signatures": true, - "variable-name": false, - "whitespace": [ - true, - "check-branch", - "check-decl", - "check-operator", - "check-separator", - "check-type" - ], - "no-output-on-prefix": true, - "use-input-property-decorator": true, - "use-output-property-decorator": true, - "use-host-property-decorator": true, - "no-input-rename": true, - "no-output-rename": true, - "use-life-cycle-interface": true, - "use-pipe-transform-interface": true, - "component-class-suffix": true, - "directive-class-suffix": true - } -} diff --git a/spring-security-anguar/client/angular5/.angular-cli.json b/spring-security-anguar/client/angular5/.angular-cli.json deleted file mode 100644 index d390652214..0000000000 --- a/spring-security-anguar/client/angular5/.angular-cli.json +++ /dev/null @@ -1,61 +0,0 @@ -{ - "$schema": "./node_modules/@angular/cli/lib/config/schema.json", - "project": { - "name": "angular5" - }, - "apps": [ - { - "root": "src", - "outDir": "dist", - "assets": [ - "assets", - "favicon.ico" - ], - "index": "index.html", - "main": "main.ts", - "polyfills": "polyfills.ts", - "test": "test.ts", - "tsconfig": "tsconfig.app.json", - "testTsconfig": "tsconfig.spec.json", - "prefix": "app", - "styles": [ - "styles.css", - "../node_modules/ngx-toastr/toastr.css" - ], - "scripts": [], - "environmentSource": "environments/environment.ts", - "environments": { - "dev": "environments/environment.ts", - "prod": "environments/environment.prod.ts" - } - } - ], - "e2e": { - "protractor": { - "config": "./protractor.conf.js" - } - }, - "lint": [ - { - "project": "src/tsconfig.app.json", - "exclude": "**/node_modules/**" - }, - { - "project": "src/tsconfig.spec.json", - "exclude": "**/node_modules/**" - }, - { - "project": "e2e/tsconfig.e2e.json", - "exclude": "**/node_modules/**" - } - ], - "test": { - "karma": { - "config": "./karma.conf.js" - } - }, - "defaults": { - "styleExt": "css", - "component": {} - } -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/package.json b/spring-security-anguar/client/angular5/package.json deleted file mode 100644 index 45f455d191..0000000000 --- a/spring-security-anguar/client/angular5/package.json +++ /dev/null @@ -1,50 +0,0 @@ -{ - "name": "angular5", - "version": "0.0.0", - "license": "MIT", - "scripts": { - "ng": "ng", - "start": "ng serve", - "build": "ng build --prod", - "test": "ng test", - "lint": "ng lint", - "e2e": "ng e2e" - }, - "private": true, - "dependencies": { - "@angular/animations": "^5.0.0", - "@angular/common": "^5.0.0", - "@angular/compiler": "^5.0.0", - "@angular/core": "^5.0.0", - "@angular/forms": "^5.0.0", - "@angular/http": "^5.0.0", - "@angular/platform-browser": "^5.0.0", - "@angular/platform-browser-dynamic": "^5.0.0", - "@angular/router": "^5.0.0", - "core-js": "^2.4.1", - "ngx-toastr": "^8.1.1", - "rxjs": "^5.5.2", - "zone.js": "^0.8.14" - }, - "devDependencies": { - "@angular/cli": "^1.6.6", - "@angular/compiler-cli": "^5.0.0", - "@angular/language-service": "^5.0.0", - "@types/jasmine": "~2.5.53", - "@types/jasminewd2": "~2.0.2", - "@types/node": "~6.0.60", - "codelyzer": "^4.0.1", - "jasmine-core": "~2.6.2", - "jasmine-spec-reporter": "~4.1.0", - "karma": "~1.7.0", - "karma-chrome-launcher": "~2.1.1", - "karma-cli": "~1.0.1", - "karma-coverage-istanbul-reporter": "^1.2.1", - "karma-jasmine": "~1.1.0", - "karma-jasmine-html-reporter": "^0.2.2", - "protractor": "~5.1.2", - "ts-node": "~3.2.0", - "tslint": "~5.7.0", - "typescript": "~2.4.2" - } -} diff --git a/spring-security-anguar/client/angular5/src/app/app.component.html b/spring-security-anguar/client/angular5/src/app/app.component.html deleted file mode 100644 index 7f77adea7b..0000000000 --- a/spring-security-anguar/client/angular5/src/app/app.component.html +++ /dev/null @@ -1,7 +0,0 @@ -
-
-
- -
-
-
diff --git a/spring-security-anguar/client/angular5/src/app/app.component.ts b/spring-security-anguar/client/angular5/src/app/app.component.ts deleted file mode 100644 index 36f986c63f..0000000000 --- a/spring-security-anguar/client/angular5/src/app/app.component.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'app-root', - templateUrl: './app.component.html' -}) - -export class AppComponent { } \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/app/app.module.ts b/spring-security-anguar/client/angular5/src/app/app.module.ts deleted file mode 100644 index 9e840e6f13..0000000000 --- a/spring-security-anguar/client/angular5/src/app/app.module.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { NgModule } from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; -import { FormsModule } from '@angular/forms'; -import { HttpModule } from '@angular/http'; - -// used to create fake backend - -import { AppComponent } from './app.component'; -import { routing } from './app.routing'; - -import { HomeComponent } from './home/home.component'; -import { LoginComponent } from './login/login.component'; - -@NgModule({ - imports: [ - BrowserModule, - FormsModule, - HttpModule, - routing - ], - declarations: [ - AppComponent, - HomeComponent, - LoginComponent - ], - bootstrap: [AppComponent] -}) - -export class AppModule { } \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/app/app.routing.ts b/spring-security-anguar/client/angular5/src/app/app.routing.ts deleted file mode 100644 index c794fc5c50..0000000000 --- a/spring-security-anguar/client/angular5/src/app/app.routing.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Routes, RouterModule } from '@angular/router'; - -import { HomeComponent } from './home/home.component'; -import { LoginComponent } from './login/login.component'; - -const appRoutes: Routes = [ - { path: '', component: HomeComponent}, - { path: 'login', component: LoginComponent }, - { path: '**', redirectTo: '' } -]; - -export const routing = RouterModule.forRoot(appRoutes); \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/app/home/home.component.html b/spring-security-anguar/client/angular5/src/app/home/home.component.html deleted file mode 100644 index 7ccd2c2a3a..0000000000 --- a/spring-security-anguar/client/angular5/src/app/home/home.component.html +++ /dev/null @@ -1,4 +0,0 @@ -
-

Hi {{userName}}!

-

Logout

-
\ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/app/home/home.component.ts b/spring-security-anguar/client/angular5/src/app/home/home.component.ts deleted file mode 100644 index 7fe1ac4eff..0000000000 --- a/spring-security-anguar/client/angular5/src/app/home/home.component.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { Component, OnInit } from '@angular/core'; -import { Http, Headers, RequestOptions } from '@angular/http'; -import 'rxjs/add/operator/map' - -@Component({ - selector:'home', - templateUrl: './home.component.html' -}) - -export class HomeComponent implements OnInit { - - userName: string; - - constructor(private http: Http) { } - - ngOnInit() { - let url = 'http://localhost:8082/user'; - - let headers:Headers = new Headers({ - 'Authorization': 'Basic ' + sessionStorage.getItem('token') - }) - let options = new RequestOptions({headers: headers}); - this.http.post(url,{}, options).map( - res => res.json(), - error => { - if(error.status == 401) - alert('Unauthorized'); - } - ).subscribe(principal => { - this.userName = principal.name; - }); - } - - logout() { - sessionStorage.setItem('token', ''); - } -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/app/login/login.component.html b/spring-security-anguar/client/angular5/src/app/login/login.component.html deleted file mode 100644 index d87b91a7bb..0000000000 --- a/spring-security-anguar/client/angular5/src/app/login/login.component.html +++ /dev/null @@ -1,18 +0,0 @@ -
-

Login

-
-
- - -
Username is required
-
-
- - -
Password is required
-
-
- -
-
-
diff --git a/spring-security-anguar/client/angular5/src/app/login/login.component.ts b/spring-security-anguar/client/angular5/src/app/login/login.component.ts deleted file mode 100644 index 2db8f32871..0000000000 --- a/spring-security-anguar/client/angular5/src/app/login/login.component.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { Component, OnInit } from '@angular/core'; -import { Router, ActivatedRoute } from '@angular/router'; -import { Http } from '@angular/http'; - -@Component({ - selector: 'login', - templateUrl: './login.component.html' -}) - -export class LoginComponent implements OnInit { - model: any = {}; - - constructor( - private route: ActivatedRoute, - private router: Router, - private http: Http) { } - - ngOnInit() { - sessionStorage.setItem('token', ''); - } - - - login() { - let url = 'http://localhost:8082/login'; - let result = this.http.post(url, { - userName: this.model.username, - password: this.model.password - }).map(res => res.json()).subscribe(isValid => { - if (isValid) { - sessionStorage.setItem('token', btoa(this.model.username + ':' + this.model.password)); - this.router.navigate(['']); - } else { - alert("Authentication failed.") - } - }); - } -} diff --git a/spring-security-anguar/client/angular5/src/index.html b/spring-security-anguar/client/angular5/src/index.html deleted file mode 100644 index c716820396..0000000000 --- a/spring-security-anguar/client/angular5/src/index.html +++ /dev/null @@ -1,14 +0,0 @@ - - - - - AngularCRUD - - - - - - - - - \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/main.ts b/spring-security-anguar/client/angular5/src/main.ts deleted file mode 100644 index 49db98ae89..0000000000 --- a/spring-security-anguar/client/angular5/src/main.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; - -import { AppModule } from './app/app.module'; - -platformBrowserDynamic().bootstrapModule(AppModule); \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/polyfills.ts b/spring-security-anguar/client/angular5/src/polyfills.ts deleted file mode 100644 index a4984ced57..0000000000 --- a/spring-security-anguar/client/angular5/src/polyfills.ts +++ /dev/null @@ -1,68 +0,0 @@ -/** - * This file includes polyfills needed by Angular and is loaded before the app. - * You can add your own extra polyfills to this file. - * - * This file is divided into 2 sections: - * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. - * 2. Application imports. Files imported after ZoneJS that should be loaded before your main - * file. - * - * The current setup is for so-called "evergreen" browsers; the last versions of browsers that - * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), - * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. - * - * Learn more in https://angular.io/docs/ts/latest/guide/browser-support.html - */ - -/*************************************************************************************************** - * BROWSER POLYFILLS - */ - -/** IE9, IE10 and IE11 requires all of the following polyfills. **/ -// import 'core-js/es6/symbol'; -// import 'core-js/es6/object'; -// import 'core-js/es6/function'; -// import 'core-js/es6/parse-int'; -// import 'core-js/es6/parse-float'; -// import 'core-js/es6/number'; -// import 'core-js/es6/math'; -// import 'core-js/es6/string'; -// import 'core-js/es6/date'; -// import 'core-js/es6/array'; -// import 'core-js/es6/regexp'; -// import 'core-js/es6/map'; -// import 'core-js/es6/weak-map'; -// import 'core-js/es6/set'; -/** IE10 and IE11 requires the following for NgClass support on SVG elements */ -// import 'classlist.js'; // Run `npm install --save classlist.js`. -/** Evergreen browsers require these. **/ -import 'core-js/es6/reflect'; -import 'core-js/es7/reflect'; - - -/** - * Required to support Web Animations `@angular/animation`. - * Needed for: All but Chrome, Firefox and Opera. http://caniuse.com/#feat=web-animation - **/ -// import 'web-animations-js'; // Run `npm install --save web-animations-js`. - - -/*************************************************************************************************** - * Zone JS is required by Angular itself. - */ -import 'zone.js/dist/zone'; // Included with Angular CLI. - - -/*************************************************************************************************** - * APPLICATION IMPORTS - */ - -/** - * Date, currency, decimal and percent pipes. - * Needed for: All but Chrome, Firefox, Edge, IE11 and Safari 10 - */ -// import 'intl'; // Run `npm install --save intl`. -/** - * Need to import at least one locale-data with intl. - */ -// import 'intl/locale-data/jsonp/en'; \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/styles.css b/spring-security-anguar/client/angular5/src/styles.css deleted file mode 100644 index cdd2d591d8..0000000000 --- a/spring-security-anguar/client/angular5/src/styles.css +++ /dev/null @@ -1,7 +0,0 @@ -a { - cursor: pointer; -} - -.help-block { - font-size: 12px; -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/tsconfig.app.json b/spring-security-anguar/client/angular5/src/tsconfig.app.json deleted file mode 100644 index 213ce42a1b..0000000000 --- a/spring-security-anguar/client/angular5/src/tsconfig.app.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "extends": "../tsconfig.json", - "compilerOptions": { - "outDir": "../out-tsc/app", - "baseUrl": "./", - "module": "es2015", - "types": [] - }, - "exclude": [ - "test.ts", - "**/*.spec.ts" - ] -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/tsconfig.json b/spring-security-anguar/client/angular5/tsconfig.json deleted file mode 100644 index 0fdb5c817d..0000000000 --- a/spring-security-anguar/client/angular5/tsconfig.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "compileOnSave": false, - "compilerOptions": { - "outDir": "./dist/out-tsc", - "sourceMap": true, - "declaration": false, - "moduleResolution": "node", - "emitDecoratorMetadata": true, - "experimentalDecorators": true, - "target": "es5", - "typeRoots": [ - "node_modules/@types" - ], - "lib": [ - "es2017", - "dom" - ] - } -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/tslint.json b/spring-security-anguar/client/angular5/tslint.json deleted file mode 100644 index 1c1d53b0d9..0000000000 --- a/spring-security-anguar/client/angular5/tslint.json +++ /dev/null @@ -1,144 +0,0 @@ -{ - "rulesDirectory": [ - "node_modules/codelyzer" - ], - "rules": { - "arrow-return-shorthand": true, - "callable-types": true, - "class-name": true, - "comment-format": [ - true, - "check-space" - ], - "curly": true, - "deprecation": { - "severity": "warn" - }, - "eofline": true, - "forin": true, - "import-blacklist": [ - true, - "rxjs", - "rxjs/Rx" - ], - "import-spacing": true, - "indent": [ - true, - "spaces" - ], - "interface-over-type-literal": true, - "label-position": true, - "max-line-length": [ - true, - 140 - ], - "member-access": false, - "member-ordering": [ - true, - { - "order": [ - "static-field", - "instance-field", - "static-method", - "instance-method" - ] - } - ], - "no-arg": true, - "no-bitwise": true, - "no-console": [ - true, - "debug", - "info", - "time", - "timeEnd", - "trace" - ], - "no-construct": true, - "no-debugger": true, - "no-duplicate-super": true, - "no-empty": false, - "no-empty-interface": true, - "no-eval": true, - "no-inferrable-types": [ - true, - "ignore-params" - ], - "no-misused-new": true, - "no-non-null-assertion": true, - "no-shadowed-variable": true, - "no-string-literal": false, - "no-string-throw": true, - "no-switch-case-fall-through": true, - "no-trailing-whitespace": true, - "no-unnecessary-initializer": true, - "no-unused-expression": true, - "no-use-before-declare": true, - "no-var-keyword": true, - "object-literal-sort-keys": false, - "one-line": [ - true, - "check-open-brace", - "check-catch", - "check-else", - "check-whitespace" - ], - "prefer-const": true, - "quotemark": [ - true, - "single" - ], - "radix": true, - "semicolon": [ - true, - "always" - ], - "triple-equals": [ - true, - "allow-null-check" - ], - "typedef-whitespace": [ - true, - { - "call-signature": "nospace", - "index-signature": "nospace", - "parameter": "nospace", - "property-declaration": "nospace", - "variable-declaration": "nospace" - } - ], - "typeof-compare": true, - "unified-signatures": true, - "variable-name": false, - "whitespace": [ - true, - "check-branch", - "check-decl", - "check-operator", - "check-separator", - "check-type" - ], - "directive-selector": [ - true, - "attribute", - "app", - "camelCase" - ], - "component-selector": [ - true, - "element", - "app", - "kebab-case" - ], - "no-output-on-prefix": true, - "use-input-property-decorator": true, - "use-output-property-decorator": true, - "use-host-property-decorator": true, - "no-input-rename": true, - "no-output-rename": true, - "use-life-cycle-interface": true, - "use-pipe-transform-interface": true, - "component-class-suffix": true, - "directive-class-suffix": true - } -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/angular.json b/spring-security-anguar/client/angular6/angular.json deleted file mode 100644 index 0168c58817..0000000000 --- a/spring-security-anguar/client/angular6/angular.json +++ /dev/null @@ -1,127 +0,0 @@ -{ - "$schema": "./node_modules/@angular/cli/lib/config/schema.json", - "version": 1, - "newProjectRoot": "projects", - "projects": { - "angular6-login": { - "root": "", - "sourceRoot": "src", - "projectType": "application", - "prefix": "app", - "schematics": {}, - "architect": { - "build": { - "builder": "@angular-devkit/build-angular:browser", - "options": { - "outputPath": "dist/angular6-login", - "index": "src/index.html", - "main": "src/main.ts", - "polyfills": "src/polyfills.ts", - "tsConfig": "src/tsconfig.app.json", - "assets": [ - "src/favicon.ico", - "src/assets" - ], - "styles": [ - "src/styles.css" - ], - "scripts": [] - }, - "configurations": { - "production": { - "fileReplacements": [ - { - "replace": "src/environments/environment.ts", - "with": "src/environments/environment.prod.ts" - } - ], - "optimization": true, - "outputHashing": "all", - "sourceMap": false, - "extractCss": true, - "namedChunks": false, - "aot": true, - "extractLicenses": true, - "vendorChunk": false, - "buildOptimizer": true - } - } - }, - "serve": { - "builder": "@angular-devkit/build-angular:dev-server", - "options": { - "browserTarget": "angular6-login:build" - }, - "configurations": { - "production": { - "browserTarget": "angular6-login:build:production" - } - } - }, - "extract-i18n": { - "builder": "@angular-devkit/build-angular:extract-i18n", - "options": { - "browserTarget": "angular6-login:build" - } - }, - "test": { - "builder": "@angular-devkit/build-angular:karma", - "options": { - "main": "src/test.ts", - "polyfills": "src/polyfills.ts", - "tsConfig": "src/tsconfig.spec.json", - "karmaConfig": "src/karma.conf.js", - "styles": [ - "src/styles.css" - ], - "scripts": [], - "assets": [ - "src/favicon.ico", - "src/assets" - ] - } - }, - "lint": { - "builder": "@angular-devkit/build-angular:tslint", - "options": { - "tsConfig": [ - "src/tsconfig.app.json", - "src/tsconfig.spec.json" - ], - "exclude": [ - "**/node_modules/**" - ] - } - } - } - }, - "angular6-login-e2e": { - "root": "e2e/", - "projectType": "application", - "architect": { - "e2e": { - "builder": "@angular-devkit/build-angular:protractor", - "options": { - "protractorConfig": "e2e/protractor.conf.js", - "devServerTarget": "angular6-login:serve" - }, - "configurations": { - "production": { - "devServerTarget": "angular6-login:serve:production" - } - } - }, - "lint": { - "builder": "@angular-devkit/build-angular:tslint", - "options": { - "tsConfig": "e2e/tsconfig.e2e.json", - "exclude": [ - "**/node_modules/**" - ] - } - } - } - } - }, - "defaultProject": "angular6-login" -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/package.json b/spring-security-anguar/client/angular6/package.json deleted file mode 100644 index a0adcd03f3..0000000000 --- a/spring-security-anguar/client/angular6/package.json +++ /dev/null @@ -1,48 +0,0 @@ -{ - "name": "angular6-login", - "version": "0.0.0", - "scripts": { - "ng": "ng", - "start": "ng serve", - "build": "ng build", - "test": "ng test", - "lint": "ng lint", - "e2e": "ng e2e" - }, - "private": true, - "dependencies": { - "@angular/animations": "^6.0.3", - "@angular/common": "^6.0.3", - "@angular/compiler": "^6.0.3", - "@angular/core": "^6.0.3", - "@angular/forms": "^6.0.3", - "@angular/http": "^6.0.3", - "@angular/platform-browser": "^6.0.3", - "@angular/platform-browser-dynamic": "^6.0.3", - "@angular/router": "^6.0.3", - "core-js": "^2.5.4", - "rxjs": "^6.0.0", - "zone.js": "^0.8.26" - }, - "devDependencies": { - "@angular/compiler-cli": "^6.0.3", - "@angular-devkit/build-angular": "~0.6.8", - "typescript": "~2.7.2", - "@angular/cli": "~6.0.8", - "@angular/language-service": "^6.0.3", - "@types/jasmine": "~2.8.6", - "@types/jasminewd2": "~2.0.3", - "@types/node": "~8.9.4", - "codelyzer": "~4.2.1", - "jasmine-core": "~2.99.1", - "jasmine-spec-reporter": "~4.2.1", - "karma": "~1.7.1", - "karma-chrome-launcher": "~2.2.0", - "karma-coverage-istanbul-reporter": "~2.0.0", - "karma-jasmine": "~1.1.1", - "karma-jasmine-html-reporter": "^0.2.2", - "protractor": "~5.3.0", - "ts-node": "~5.0.1", - "tslint": "~5.9.1" - } -} diff --git a/spring-security-anguar/client/angular6/src/app/app.component.html b/spring-security-anguar/client/angular6/src/app/app.component.html deleted file mode 100644 index 6bf00f3018..0000000000 --- a/spring-security-anguar/client/angular6/src/app/app.component.html +++ /dev/null @@ -1,2 +0,0 @@ - - diff --git a/spring-security-anguar/client/angular6/src/app/app.component.ts b/spring-security-anguar/client/angular6/src/app/app.component.ts deleted file mode 100644 index 36f986c63f..0000000000 --- a/spring-security-anguar/client/angular6/src/app/app.component.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'app-root', - templateUrl: './app.component.html' -}) - -export class AppComponent { } \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/app/app.module.ts b/spring-security-anguar/client/angular6/src/app/app.module.ts deleted file mode 100644 index 4f9a792e19..0000000000 --- a/spring-security-anguar/client/angular6/src/app/app.module.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { NgModule } from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; -import { FormsModule } from '@angular/forms'; -import { HttpClientModule } from '@angular/common/http'; - -import { AppComponent } from './app.component'; -import { routing } from './app.routing'; - -import { HomeComponent } from './home/home.component'; -import { LoginComponent } from './login/login.component'; - -@NgModule({ - imports: [ - BrowserModule, - FormsModule, - HttpClientModule, - routing - ], - declarations: [ - AppComponent, - HomeComponent, - LoginComponent - ], - bootstrap: [AppComponent] -}) - -export class AppModule { } \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/app/app.routing.ts b/spring-security-anguar/client/angular6/src/app/app.routing.ts deleted file mode 100644 index e5f5e04aa8..0000000000 --- a/spring-security-anguar/client/angular6/src/app/app.routing.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { Routes, RouterModule } from '@angular/router'; -import { HomeComponent } from './home/home.component'; -import { LoginComponent } from './login/login.component'; - -const appRoutes: Routes = [ - { path: '', component: HomeComponent }, - { path: 'login', component: LoginComponent }, - { path: '**', redirectTo: '' } -]; - -export const routing = RouterModule.forRoot(appRoutes); \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/app/home/home.component.html b/spring-security-anguar/client/angular6/src/app/home/home.component.html deleted file mode 100644 index 7ccd2c2a3a..0000000000 --- a/spring-security-anguar/client/angular6/src/app/home/home.component.html +++ /dev/null @@ -1,4 +0,0 @@ -
-

Hi {{userName}}!

-

Logout

-
\ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/app/home/home.component.ts b/spring-security-anguar/client/angular6/src/app/home/home.component.ts deleted file mode 100644 index 6c9fcfd97f..0000000000 --- a/spring-security-anguar/client/angular6/src/app/home/home.component.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { Component, OnInit } from '@angular/core'; -import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http'; -import { Observable, throwError } from 'rxjs'; -import { catchError, map, tap} from 'rxjs/operators'; -@Component({ - selector: 'home', - templateUrl: './home.component.html' -}) - -export class HomeComponent implements OnInit { - - userName: string; - - constructor(private http: HttpClient) { } - - ngOnInit() { - let url = 'http://localhost:8082/user'; - - let headers: HttpHeaders = new HttpHeaders({ - 'Authorization': 'Basic ' + sessionStorage.getItem('token') - }); - - let options = { headers: headers }; - this.http.post>(url, {}, options). - subscribe(principal => { - this.userName = principal['name']; - }, - error => { - if(error.status == 401) - alert('Unauthorized'); - } - ); - } - - logout() { - sessionStorage.setItem('token', ''); - } - private handleError(error: HttpErrorResponse) { - if (error.error instanceof ErrorEvent) { - console.error('An error occurred:', error.error.message); - } else { - console.error( - `Backend returned code ${error.status}, ` + - `body was: ${error.error}`); - } - return throwError( - 'Something bad happened; please try again later.'); - }; -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/app/login/login.component.html b/spring-security-anguar/client/angular6/src/app/login/login.component.html deleted file mode 100644 index 4291206469..0000000000 --- a/spring-security-anguar/client/angular6/src/app/login/login.component.html +++ /dev/null @@ -1,15 +0,0 @@ -
-
- - -
Username is required
-
-
- - -
Password is required
-
-
- -
-
\ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/app/login/login.component.ts b/spring-security-anguar/client/angular6/src/app/login/login.component.ts deleted file mode 100644 index 27af9ebba5..0000000000 --- a/spring-security-anguar/client/angular6/src/app/login/login.component.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { Component, OnInit } from '@angular/core'; -import { Router, ActivatedRoute } from '@angular/router'; -import { HttpClient } from '@angular/common/http'; -import { Observable } from 'rxjs'; - -@Component({ - selector: 'login', - templateUrl: './login.component.html' -}) - -export class LoginComponent implements OnInit { - - model: any = {}; - - constructor( - private route: ActivatedRoute, - private router: Router, - private http: HttpClient - ) { } - - ngOnInit() { - sessionStorage.setItem('token', ''); - } - - login() { - let url = 'http://localhost:8082/login'; - this.http.post>(url, { - userName: this.model.username, - password: this.model.password - }).subscribe(isValid => { - if (isValid) { - sessionStorage.setItem('token', btoa(this.model.username + ':' + this.model.password)); - this.router.navigate(['']); - } else { - alert("Authentication failed.") - } - }); - } -} diff --git a/spring-security-anguar/client/angular6/src/index.html b/spring-security-anguar/client/angular6/src/index.html deleted file mode 100644 index c716820396..0000000000 --- a/spring-security-anguar/client/angular6/src/index.html +++ /dev/null @@ -1,14 +0,0 @@ - - - - - AngularCRUD - - - - - - - - - \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/main.ts b/spring-security-anguar/client/angular6/src/main.ts deleted file mode 100644 index 49db98ae89..0000000000 --- a/spring-security-anguar/client/angular6/src/main.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; - -import { AppModule } from './app/app.module'; - -platformBrowserDynamic().bootstrapModule(AppModule); \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/polyfills.ts b/spring-security-anguar/client/angular6/src/polyfills.ts deleted file mode 100644 index a4984ced57..0000000000 --- a/spring-security-anguar/client/angular6/src/polyfills.ts +++ /dev/null @@ -1,68 +0,0 @@ -/** - * This file includes polyfills needed by Angular and is loaded before the app. - * You can add your own extra polyfills to this file. - * - * This file is divided into 2 sections: - * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. - * 2. Application imports. Files imported after ZoneJS that should be loaded before your main - * file. - * - * The current setup is for so-called "evergreen" browsers; the last versions of browsers that - * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), - * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. - * - * Learn more in https://angular.io/docs/ts/latest/guide/browser-support.html - */ - -/*************************************************************************************************** - * BROWSER POLYFILLS - */ - -/** IE9, IE10 and IE11 requires all of the following polyfills. **/ -// import 'core-js/es6/symbol'; -// import 'core-js/es6/object'; -// import 'core-js/es6/function'; -// import 'core-js/es6/parse-int'; -// import 'core-js/es6/parse-float'; -// import 'core-js/es6/number'; -// import 'core-js/es6/math'; -// import 'core-js/es6/string'; -// import 'core-js/es6/date'; -// import 'core-js/es6/array'; -// import 'core-js/es6/regexp'; -// import 'core-js/es6/map'; -// import 'core-js/es6/weak-map'; -// import 'core-js/es6/set'; -/** IE10 and IE11 requires the following for NgClass support on SVG elements */ -// import 'classlist.js'; // Run `npm install --save classlist.js`. -/** Evergreen browsers require these. **/ -import 'core-js/es6/reflect'; -import 'core-js/es7/reflect'; - - -/** - * Required to support Web Animations `@angular/animation`. - * Needed for: All but Chrome, Firefox and Opera. http://caniuse.com/#feat=web-animation - **/ -// import 'web-animations-js'; // Run `npm install --save web-animations-js`. - - -/*************************************************************************************************** - * Zone JS is required by Angular itself. - */ -import 'zone.js/dist/zone'; // Included with Angular CLI. - - -/*************************************************************************************************** - * APPLICATION IMPORTS - */ - -/** - * Date, currency, decimal and percent pipes. - * Needed for: All but Chrome, Firefox, Edge, IE11 and Safari 10 - */ -// import 'intl'; // Run `npm install --save intl`. -/** - * Need to import at least one locale-data with intl. - */ -// import 'intl/locale-data/jsonp/en'; \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/styles.css b/spring-security-anguar/client/angular6/src/styles.css deleted file mode 100644 index cdd2d591d8..0000000000 --- a/spring-security-anguar/client/angular6/src/styles.css +++ /dev/null @@ -1,7 +0,0 @@ -a { - cursor: pointer; -} - -.help-block { - font-size: 12px; -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/tsconfig.app.json b/spring-security-anguar/client/angular6/src/tsconfig.app.json deleted file mode 100644 index 213ce42a1b..0000000000 --- a/spring-security-anguar/client/angular6/src/tsconfig.app.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "extends": "../tsconfig.json", - "compilerOptions": { - "outDir": "../out-tsc/app", - "baseUrl": "./", - "module": "es2015", - "types": [] - }, - "exclude": [ - "test.ts", - "**/*.spec.ts" - ] -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/tsconfig.json b/spring-security-anguar/client/angular6/tsconfig.json deleted file mode 100644 index ef44e2862b..0000000000 --- a/spring-security-anguar/client/angular6/tsconfig.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "compileOnSave": false, - "compilerOptions": { - "baseUrl": "./", - "outDir": "./dist/out-tsc", - "sourceMap": true, - "declaration": false, - "moduleResolution": "node", - "emitDecoratorMetadata": true, - "experimentalDecorators": true, - "target": "es5", - "typeRoots": [ - "node_modules/@types" - ], - "lib": [ - "es2017", - "dom" - ] - } -} diff --git a/spring-security-anguar/client/angular6/tslint.json b/spring-security-anguar/client/angular6/tslint.json deleted file mode 100644 index 3ea984c776..0000000000 --- a/spring-security-anguar/client/angular6/tslint.json +++ /dev/null @@ -1,130 +0,0 @@ -{ - "rulesDirectory": [ - "node_modules/codelyzer" - ], - "rules": { - "arrow-return-shorthand": true, - "callable-types": true, - "class-name": true, - "comment-format": [ - true, - "check-space" - ], - "curly": true, - "deprecation": { - "severity": "warn" - }, - "eofline": true, - "forin": true, - "import-blacklist": [ - true, - "rxjs/Rx" - ], - "import-spacing": true, - "indent": [ - true, - "spaces" - ], - "interface-over-type-literal": true, - "label-position": true, - "max-line-length": [ - true, - 140 - ], - "member-access": false, - "member-ordering": [ - true, - { - "order": [ - "static-field", - "instance-field", - "static-method", - "instance-method" - ] - } - ], - "no-arg": true, - "no-bitwise": true, - "no-console": [ - true, - "debug", - "info", - "time", - "timeEnd", - "trace" - ], - "no-construct": true, - "no-debugger": true, - "no-duplicate-super": true, - "no-empty": false, - "no-empty-interface": true, - "no-eval": true, - "no-inferrable-types": [ - true, - "ignore-params" - ], - "no-misused-new": true, - "no-non-null-assertion": true, - "no-shadowed-variable": true, - "no-string-literal": false, - "no-string-throw": true, - "no-switch-case-fall-through": true, - "no-trailing-whitespace": true, - "no-unnecessary-initializer": true, - "no-unused-expression": true, - "no-use-before-declare": true, - "no-var-keyword": true, - "object-literal-sort-keys": false, - "one-line": [ - true, - "check-open-brace", - "check-catch", - "check-else", - "check-whitespace" - ], - "prefer-const": true, - "quotemark": [ - true, - "single" - ], - "radix": true, - "semicolon": [ - true, - "always" - ], - "triple-equals": [ - true, - "allow-null-check" - ], - "typedef-whitespace": [ - true, - { - "call-signature": "nospace", - "index-signature": "nospace", - "parameter": "nospace", - "property-declaration": "nospace", - "variable-declaration": "nospace" - } - ], - "unified-signatures": true, - "variable-name": false, - "whitespace": [ - true, - "check-branch", - "check-decl", - "check-operator", - "check-separator", - "check-type" - ], - "no-output-on-prefix": true, - "use-input-property-decorator": true, - "use-output-property-decorator": true, - "use-host-property-decorator": true, - "no-input-rename": true, - "no-output-rename": true, - "use-life-cycle-interface": true, - "use-pipe-transform-interface": true, - "component-class-suffix": true, - "directive-class-suffix": true - } -} diff --git a/spring-security-anguar/server/pom.xml b/spring-security-anguar/server/pom.xml deleted file mode 100644 index 39de129c87..0000000000 --- a/spring-security-anguar/server/pom.xml +++ /dev/null @@ -1,71 +0,0 @@ - - - 4.0.0 - com.baeldung - spring-security-angular - 0.0.1-SNAPSHOT - jar - spring-boot-security-rest - Spring Boot Security REST - - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - ../../ - - - - - - org.springframework.boot - spring-boot-dependencies - 1.5.9.RELEASE - pom - import - - - - - - - - - - - org.springframework.boot - spring-boot-starter-security - - - org.springframework.boot - spring-boot-starter-web - - - - org.springframework.boot - spring-boot-starter-test - test - - - org.springframework.security - spring-security-test - test - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - - - UTF-8 - UTF-8 - - - diff --git a/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/basicauth/SpringBootSecurityApplication.java b/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/basicauth/SpringBootSecurityApplication.java deleted file mode 100644 index 681c7590a8..0000000000 --- a/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/basicauth/SpringBootSecurityApplication.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung.springbootsecurityrest.basicauth; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication(scanBasePackages = "com.baeldung.springbootsecurityrest") -@EnableAutoConfiguration -public class SpringBootSecurityApplication { - - public static void main(String[] args) { - SpringApplication.run(SpringBootSecurityApplication.class, args); - } -} diff --git a/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/basicauth/config/BasicAuthConfiguration.java b/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/basicauth/config/BasicAuthConfiguration.java deleted file mode 100644 index 3ed301439c..0000000000 --- a/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/basicauth/config/BasicAuthConfiguration.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.baeldung.springbootsecurityrest.basicauth.config; - -import org.springframework.context.annotation.Configuration; -import org.springframework.http.HttpMethod; -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 -public class BasicAuthConfiguration extends WebSecurityConfigurerAdapter { - - @Override - protected void configure(AuthenticationManagerBuilder auth) throws Exception { - auth - .inMemoryAuthentication() - .withUser("user") - .password("password") - .roles("USER"); - } - - @Override - protected void configure(HttpSecurity http) throws Exception { - http.csrf().disable() - .authorizeRequests() - .antMatchers(HttpMethod.OPTIONS, "/**").permitAll() - .antMatchers("/login").permitAll() - .anyRequest() - .authenticated() - .and() - .httpBasic(); - } -} diff --git a/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/controller/UserController.java b/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/controller/UserController.java deleted file mode 100644 index 825290ff2d..0000000000 --- a/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/controller/UserController.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.springbootsecurityrest.controller; - -import java.security.Principal; -import java.util.Base64; - -import javax.servlet.http.HttpServletRequest; - -import org.springframework.web.bind.annotation.CrossOrigin; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import com.baeldung.springbootsecurityrest.vo.User; - -@RestController -@CrossOrigin -public class UserController { - - @RequestMapping("/login") - public boolean login(@RequestBody User user) { - if(user.getUserName().equals("user") && user.getPassword().equals("password")) { - return true; - } - return false; - } - - @RequestMapping("/user") - public Principal user(HttpServletRequest request) { - String authToken = request.getHeader("Authorization").substring("Basic".length()).trim(); - return () -> new String(Base64.getDecoder().decode(authToken)).split(":")[0]; - } -} diff --git a/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/vo/User.java b/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/vo/User.java deleted file mode 100644 index 0eda5ce9ec..0000000000 --- a/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/vo/User.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.springbootsecurityrest.vo; - - -public class User { - - private String userName; - private String password; - - 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; - } -} diff --git a/spring-security-anguar/server/src/main/resources/application.properties b/spring-security-anguar/server/src/main/resources/application.properties deleted file mode 100644 index 565d97a7b0..0000000000 --- a/spring-security-anguar/server/src/main/resources/application.properties +++ /dev/null @@ -1,5 +0,0 @@ -#spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration -#security.user.password=password -#security.oauth2.client.client-id=client -#security.oauth2.client.client-secret=secret -server.port=8082 diff --git a/spring-security-anguar/server/src/main/resources/logback.xml b/spring-security-anguar/server/src/main/resources/logback.xml deleted file mode 100644 index 7d900d8ea8..0000000000 --- a/spring-security-anguar/server/src/main/resources/logback.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - - \ No newline at end of file diff --git a/spring-security-angular/README.md b/spring-security-angular/README.md new file mode 100644 index 0000000000..80312c4bab --- /dev/null +++ b/spring-security-angular/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Spring Security Login Page with Angular](https://www.baeldung.com/spring-security-login-angular) diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/ip/IpApplication.java b/spring-security-mvc-boot/src/main/java/org/baeldung/ip/IpApplication.java new file mode 100644 index 0000000000..fd270686a2 --- /dev/null +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/ip/IpApplication.java @@ -0,0 +1,12 @@ +package org.baeldung.ip; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; + +@SpringBootApplication +public class IpApplication extends SpringBootServletInitializer { + public static void main(String[] args) { + SpringApplication.run(IpApplication.class, args); + } +} diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/ip/config/CustomIpAuthenticationProvider.java b/spring-security-mvc-boot/src/main/java/org/baeldung/ip/config/CustomIpAuthenticationProvider.java new file mode 100644 index 0000000000..078dd81259 --- /dev/null +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/ip/config/CustomIpAuthenticationProvider.java @@ -0,0 +1,53 @@ +package org.baeldung.ip.config; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.springframework.security.authentication.AuthenticationProvider; +import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.web.authentication.WebAuthenticationDetails; +import org.springframework.stereotype.Component; + +@Component +public class CustomIpAuthenticationProvider implements AuthenticationProvider { + + Set whitelist = new HashSet(); + + public CustomIpAuthenticationProvider() { + super(); + whitelist.add("11.11.11.11"); + whitelist.add("127.0.0.1"); + } + + @Override + public Authentication authenticate(Authentication auth) throws AuthenticationException { + WebAuthenticationDetails details = (WebAuthenticationDetails) auth.getDetails(); + String userIp = details.getRemoteAddress(); + if(! whitelist.contains(userIp)){ + throw new BadCredentialsException("Invalid IP Address"); + } + final String name = auth.getName(); + final String password = auth.getCredentials().toString(); + + if (name.equals("john") && password.equals("123")) { + List authorities =new ArrayList(); + authorities.add(new SimpleGrantedAuthority("ROLE_USER")); + return new UsernamePasswordAuthenticationToken(name, password, authorities); + } + else{ + throw new BadCredentialsException("Invalid username or password"); + } + } + + @Override + public boolean supports(Class authentication) { + return authentication.equals(UsernamePasswordAuthenticationToken.class); + } +} diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/ip/config/SecurityConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/ip/config/SecurityConfig.java new file mode 100644 index 0000000000..3a8032a734 --- /dev/null +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/ip/config/SecurityConfig.java @@ -0,0 +1,36 @@ +package org.baeldung.ip.config; + +import org.springframework.beans.factory.annotation.Autowired; +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; + +@Configuration +@EnableWebSecurity//(debug = true) +public class SecurityConfig extends WebSecurityConfigurerAdapter { + + @Autowired + private CustomIpAuthenticationProvider authenticationProvider; + + @Override + protected void configure(final AuthenticationManagerBuilder auth) throws Exception { + auth.inMemoryAuthentication().withUser("john").password("{noop}123").authorities("ROLE_USER"); + // auth.authenticationProvider(authenticationProvider); + } + + @Override + protected void configure(final HttpSecurity http) throws Exception { + // @formatter:off + http.authorizeRequests() + .antMatchers("/login").permitAll() +// .antMatchers("/foos/**").hasIpAddress("11.11.11.11") + .antMatchers("/foos/**").access("isAuthenticated() and hasIpAddress('11.11.11.11')") + .anyRequest().authenticated() + .and().formLogin().permitAll() + .and().csrf().disable(); + // @formatter:on + } + +} \ No newline at end of file diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/ip/config/SecurityXmlConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/ip/config/SecurityXmlConfig.java new file mode 100644 index 0000000000..1d22ca4c67 --- /dev/null +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/ip/config/SecurityXmlConfig.java @@ -0,0 +1,9 @@ +package org.baeldung.ip.config; + + +//@Configuration +//@EnableWebSecurity +//@ImportResource({ "classpath:spring-security-ip.xml" }) +public class SecurityXmlConfig { + +} \ No newline at end of file diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/ip/web/MainController.java b/spring-security-mvc-boot/src/main/java/org/baeldung/ip/web/MainController.java new file mode 100644 index 0000000000..940194c421 --- /dev/null +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/ip/web/MainController.java @@ -0,0 +1,42 @@ +package org.baeldung.ip.web; + +import java.util.List; + +import javax.servlet.Filter; +import javax.servlet.http.HttpServletRequest; + +import org.baeldung.custom.persistence.model.Foo; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.security.web.FilterChainProxy; +import org.springframework.security.web.SecurityFilterChain; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class MainController { + + @Autowired + @Qualifier("springSecurityFilterChain") + private Filter springSecurityFilterChain; + + @RequestMapping(method = RequestMethod.GET, value = "/filters") + @ResponseBody + public void getFilters() { + FilterChainProxy filterChainProxy = (FilterChainProxy) springSecurityFilterChain; + List list = filterChainProxy.getFilterChains(); + list.stream() + .flatMap(chain -> chain.getFilters().stream()) + .forEach(filter -> System.out.println(filter.getClass())); + } + + @RequestMapping(method = RequestMethod.GET, value = "/foos/{id}") + @ResponseBody + public Foo findById(@PathVariable final long id, HttpServletRequest request) { + return new Foo("Sample"); + } + +} diff --git a/spring-security-mvc-boot/src/main/resources/application.properties b/spring-security-mvc-boot/src/main/resources/application.properties index f015086a4f..25eac743d1 100644 --- a/spring-security-mvc-boot/src/main/resources/application.properties +++ b/spring-security-mvc-boot/src/main/resources/application.properties @@ -6,4 +6,7 @@ spring.datasource.password= spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.database=H2 spring.jpa.show-sql=false -spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect \ No newline at end of file +spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect + + +#logging.level.org.springframework.security.web.FilterChainProxy=DEBUG \ No newline at end of file diff --git a/spring-security-mvc-boot/src/main/resources/spring-security-ip.xml b/spring-security-mvc-boot/src/main/resources/spring-security-ip.xml new file mode 100644 index 0000000000..31796ad134 --- /dev/null +++ b/spring-security-mvc-boot/src/main/resources/spring-security-ip.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + diff --git a/spring-security-mvc-boot/src/test/java/org/baeldung/web/IpLiveTest.java b/spring-security-mvc-boot/src/test/java/org/baeldung/web/IpLiveTest.java new file mode 100644 index 0000000000..e12e2f87b0 --- /dev/null +++ b/spring-security-mvc-boot/src/test/java/org/baeldung/web/IpLiveTest.java @@ -0,0 +1,27 @@ +package org.baeldung.web; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import io.restassured.RestAssured; +import io.restassured.response.Response; + +import org.junit.Test; + + +public class IpLiveTest { + + @Test + public void givenUser_whenGetHomePage_thenOK() { + final Response response = RestAssured.given().auth().form("john", "123").get("http://localhost:8082/"); + assertEquals(200, response.getStatusCode()); + assertTrue(response.asString().contains("Welcome")); + } + + @Test + public void givenUserWithWrongIP_whenGetFooById_thenForbidden() { + final Response response = RestAssured.given().auth().form("john", "123").get("http://localhost:8082/foos/1"); + assertEquals(403, response.getStatusCode()); + assertTrue(response.asString().contains("Forbidden")); + } + +} \ No newline at end of file diff --git a/spring-security-mvc-digest-auth/pom.xml b/spring-security-mvc-digest-auth/pom.xml index 2d3f1a3a00..33b3b5ff31 100644 --- a/spring-security-mvc-digest-auth/pom.xml +++ b/spring-security-mvc-digest-auth/pom.xml @@ -1,8 +1,6 @@ 4.0.0 - - com.baeldung spring-security-mvc-digest-auth 0.1-SNAPSHOT spring-security-mvc-digest-auth @@ -91,12 +89,6 @@ ${spring.version} - - org.springframework - spring-web - ${spring.version} - - diff --git a/spring-security-mvc-socket/README.md b/spring-security-mvc-socket/README.md index 5ae9d4f4cd..e99555e7fa 100644 --- a/spring-security-mvc-socket/README.md +++ b/spring-security-mvc-socket/README.md @@ -1,2 +1,10 @@ +To build the project, run the command: mvn clean install. This will build a war file in the target folder that you can deploye on a server like Tomcat. + +Alternatively, run the project from an IDE. + +To login, use credentials from the data.sql file in src/main/resource, eg: user/password. + + ### Relevant Articles: - [Intro to Security and WebSockets](http://www.baeldung.com/spring-security-websockets) +- [Spring WebSockets: Specific User Chat](http://www.baeldung.com/spring-websocket-specific-user-chat) \ No newline at end of file diff --git a/spring-security-mvc-socket/pom.xml b/spring-security-mvc-socket/pom.xml index 1f75becc98..ade24d8f7b 100644 --- a/spring-security-mvc-socket/pom.xml +++ b/spring-security-mvc-socket/pom.xml @@ -11,9 +11,9 @@ com.baeldung - parent-spring-5 + parent-spring-4 0.0.1-SNAPSHOT - ../parent-spring-5 + ../parent-spring-4 @@ -151,7 +151,7 @@ - + org.apache.tomcat.maven @@ -171,15 +171,16 @@ - spring-security-mvc-socket 5.2.10.Final + 4.2.3.RELEASE 1.11.3.RELEASE 1.4.196 1.2.3 + 2.8.7 \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/Constants.java b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/Constants.java new file mode 100644 index 0000000000..77bcf7f343 --- /dev/null +++ b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/Constants.java @@ -0,0 +1,8 @@ +package com.baeldung.springsecuredsockets; + +public class Constants { + public static final String SECURED_CHAT_HISTORY = "/secured/history"; + public static final String SECURED_CHAT = "/secured/chat"; + public static final String SECURED_CHAT_ROOM = "/secured/room"; + public static final String SECURED_CHAT_SPECIFIC_USER = "/secured/user/queue/specific-user"; +} diff --git a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/AppConfig.java b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/AppConfig.java index 49b7ab2ff8..afb1970b25 100644 --- a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/AppConfig.java +++ b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/AppConfig.java @@ -9,7 +9,7 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.resource.PathResourceResolver; import org.springframework.web.servlet.view.JstlView; import org.springframework.web.servlet.view.UrlBasedViewResolver; @@ -20,7 +20,7 @@ import java.sql.SQLException; @EnableJpaRepositories @ComponentScan("com.baeldung.springsecuredsockets") @Import({ SecurityConfig.class, DataStoreConfig.class, SocketBrokerConfig.class, SocketSecurityConfig.class }) -public class AppConfig implements WebMvcConfigurer { +public class AppConfig extends WebMvcConfigurerAdapter { public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index"); diff --git a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/SecurityConfig.java b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/SecurityConfig.java index d7b57d1829..be00662e3e 100644 --- a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/SecurityConfig.java +++ b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/SecurityConfig.java @@ -88,7 +88,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { .authorizeRequests() .antMatchers("/", "/index", "/authenticate") .permitAll() - .antMatchers("/secured/**/**", "/secured/socket", "/secured/success") + .antMatchers("/secured/**/**", "/secured/**/**/**", "/secured/socket", "/secured/success") .authenticated() .anyRequest().authenticated() .and() diff --git a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/SocketBrokerConfig.java b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/SocketBrokerConfig.java index 941cac8392..9b19de7b5a 100644 --- a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/SocketBrokerConfig.java +++ b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/SocketBrokerConfig.java @@ -1,27 +1,33 @@ package com.baeldung.springsecuredsockets.config; +import static com.baeldung.springsecuredsockets.Constants.SECURED_CHAT; +import static com.baeldung.springsecuredsockets.Constants.SECURED_CHAT_HISTORY; +import static com.baeldung.springsecuredsockets.Constants.SECURED_CHAT_ROOM; +import static com.baeldung.springsecuredsockets.Constants.SECURED_CHAT_SPECIFIC_USER; + import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.messaging.simp.config.MessageBrokerRegistry; +import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; -import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; - @Configuration @EnableWebSocketMessageBroker @ComponentScan("com.baeldung.springsecuredsockets.controllers") -public class SocketBrokerConfig implements WebSocketMessageBrokerConfigurer { +public class SocketBrokerConfig extends AbstractWebSocketMessageBrokerConfigurer { @Override public void configureMessageBroker(MessageBrokerRegistry config) { - config.enableSimpleBroker("/secured/history"); + config.enableSimpleBroker(SECURED_CHAT_HISTORY, SECURED_CHAT_SPECIFIC_USER); config.setApplicationDestinationPrefixes("/spring-security-mvc-socket"); + config.setUserDestinationPrefix("/secured/user"); } @Override public void registerStompEndpoints(StompEndpointRegistry registry) { - registry.addEndpoint("/secured/chat").withSockJS(); + registry.addEndpoint(SECURED_CHAT_ROOM).withSockJS(); + registry.addEndpoint(SECURED_CHAT).withSockJS(); } } diff --git a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/SocketSecurityConfig.java b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/SocketSecurityConfig.java index a37dfb7672..c833e70d47 100644 --- a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/SocketSecurityConfig.java +++ b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/SocketSecurityConfig.java @@ -15,7 +15,7 @@ public class SocketSecurityConfig extends AbstractSecurityWebSocketMessageBroker @Override protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) { messages - .simpDestMatchers("/secured/**").authenticated() + .simpDestMatchers("/secured/**", "/secured/**/**").authenticated() .anyMessage().authenticated(); } } \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/controllers/SocketController.java b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/controllers/SocketController.java index 68c5e306d8..6a74009c16 100644 --- a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/controllers/SocketController.java +++ b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/controllers/SocketController.java @@ -1,11 +1,19 @@ package com.baeldung.springsecuredsockets.controllers; +import static com.baeldung.springsecuredsockets.Constants.SECURED_CHAT; +import static com.baeldung.springsecuredsockets.Constants.SECURED_CHAT_HISTORY; +import static com.baeldung.springsecuredsockets.Constants.SECURED_CHAT_ROOM; +import static com.baeldung.springsecuredsockets.Constants.SECURED_CHAT_SPECIFIC_USER; + import com.baeldung.springsecuredsockets.transfer.socket.Message; import com.baeldung.springsecuredsockets.transfer.socket.OutputMessage; +import java.security.Principal; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.messaging.handler.annotation.Header; import org.springframework.messaging.handler.annotation.MessageMapping; +import org.springframework.messaging.handler.annotation.Payload; import org.springframework.messaging.handler.annotation.SendTo; import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.stereotype.Controller; @@ -19,10 +27,19 @@ public class SocketController { private SimpMessagingTemplate simpMessagingTemplate; private static final Logger log = LoggerFactory.getLogger(SocketController.class); - @MessageMapping("/secured/chat") - @SendTo("/secured/history") - public OutputMessage send(Message msg) throws Exception { + @MessageMapping(SECURED_CHAT) + @SendTo(SECURED_CHAT_HISTORY) + public OutputMessage sendAll(Message msg) throws Exception { OutputMessage out = new OutputMessage(msg.getFrom(), msg.getText(), new SimpleDateFormat("HH:mm").format(new Date())); return out; } + + /** + * Example of sending message to specific user using 'convertAndSendToUser()' and '/queue' + */ + @MessageMapping(SECURED_CHAT_ROOM) + public void sendSpecific(@Payload Message msg, Principal user, @Header("simpSessionId") String sessionId) throws Exception { + OutputMessage out = new OutputMessage(msg.getFrom(), msg.getText(), new SimpleDateFormat("HH:mm").format(new Date())); + simpMessagingTemplate.convertAndSendToUser(msg.getTo(), SECURED_CHAT_SPECIFIC_USER, out); + } } diff --git a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/transfer/socket/Message.java b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/transfer/socket/Message.java index 024b386164..fd5d632fce 100644 --- a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/transfer/socket/Message.java +++ b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/transfer/socket/Message.java @@ -3,6 +3,7 @@ package com.baeldung.springsecuredsockets.transfer.socket; public class Message { private String from; + private String to; private String text; public String getFrom() { @@ -11,6 +12,13 @@ public class Message { public void setFrom(String from) { this.from = from; } + + public String getTo() { + return to; + } + public void setTo(String to) { + this.to = to; + } public String getText() { return text; } diff --git a/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/denied.jsp b/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/denied.jsp index d61148b04f..e4c40da0a8 100644 --- a/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/denied.jsp +++ b/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/denied.jsp @@ -4,15 +4,20 @@ - - Spring Secured Sockets - " rel="stylesheet"> - - - -

ACCESS DENIED!

-
-
- Click to login. - + + + Spring Secured Sockets + " rel="stylesheet"> + " rel="stylesheet"> + + + + +
+
+

ACCESS DENIED!

+ Click to login. +
+
+ \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/index.jsp b/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/index.jsp index d83338680e..d1435e8c2b 100644 --- a/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/index.jsp +++ b/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/index.jsp @@ -4,27 +4,32 @@ - - Spring Secured Sockets - " rel="stylesheet"> - - - - - - - - - - - - -

Welcome!

-
- {{greeting}} -
-
- Click to login. - + + Spring Secured Sockets + " rel="stylesheet"> + " rel="stylesheet"> + + + + + + + + + + + + + + +
+
+

Welcome!

+ {{greeting}} + Click to login. +
+
+ + \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/login.jsp b/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/login.jsp index d50059c674..76e3334bbe 100644 --- a/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/login.jsp +++ b/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/login.jsp @@ -4,42 +4,50 @@ - - Spring Secured Sockets - " rel="stylesheet"> - - - - - - - - - - - - - - -

JSP Login Form


-
- - - - - - - - - - - - -
User:
Password:
-
+ + Spring Secured Sockets + " rel="stylesheet"> + " rel="stylesheet"> + + + + + + + + + + + + + + + +
+
+ +

JSP Login Form

+
+ + + + + + + + + + + + +
User:
Password:
+
+ +
+
+ + + - - - \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/socket.jsp b/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/socket.jsp index b5807d74a6..ccd48672a1 100644 --- a/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/socket.jsp +++ b/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/socket.jsp @@ -4,48 +4,56 @@ - - Spring Secured Sockets - " rel="stylesheet"> - - - - - - - - - - - - - - -

Socket Chat

- -
-
- -
-
-
- - - -
-
-
- - -

-
-
+ + Spring Secured Sockets + " rel="stylesheet"> + " rel="stylesheet"> -Click to go back! -Click to start over (you will still be authenticated)! + + + + + + + + + + + + + + + + +
+
+

Socket Chat

+
+

(Chat With Everyone)

+ + + +
+
+

(Chat With A Specific User)

+ + + +
+
+

(Send and See Messages)

+ + +
+ +
+ Click to go back! + Click to start over (you will still be authenticated)! +
+
+ + + - - - \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/success.jsp b/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/success.jsp index 27e8f7aa44..4cff3f7a25 100644 --- a/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/success.jsp +++ b/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/success.jsp @@ -4,28 +4,33 @@ - - Spring Secured Sockets - " rel="stylesheet"> - - - - - - - - - - - - -

Congrats! You've logged in.

+ + Spring Secured Sockets + " rel="stylesheet"> + " rel="stylesheet"> - Click to chat! - Click to start over (you will still be authenticated)! + + + - - - + + + + + + + + + +
+ +
+ + + \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/indexController.js b/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/indexController.js index 04c02e339b..5de3dcc78b 100644 --- a/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/indexController.js +++ b/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/indexController.js @@ -1,12 +1,13 @@ 'use strict'; angularApp - .controller('indexController', function ($scope) { - $scope.greeting = ''; +.controller('indexController', function ($scope) { - $scope.initialize = function () { - $scope.greeting = "Howdy!" - }; + $scope.greeting = ''; - $scope.initialize(); - }); \ No newline at end of file + $scope.initialize = function () { + $scope.greeting = "Howdy!" + }; + + $scope.initialize(); +}); \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/loginController.js b/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/loginController.js deleted file mode 100644 index 07187d5327..0000000000 --- a/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/loginController.js +++ /dev/null @@ -1,6 +0,0 @@ -'use strict'; - -angularApp - .controller('loginController', function ($scope) { - - }); diff --git a/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/socketController.js b/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/socketController.js index 395bf85c4a..ff000c251e 100644 --- a/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/socketController.js +++ b/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/socketController.js @@ -1,37 +1,52 @@ 'use strict'; angularApp - .controller('socketController', function ($scope, SocketService) { +.controller('socketController', function ($scope, SocketService) { - $scope.stompClient = null; - $scope.sendEndpoint = '/secured/chat'; - $scope.subscribeEndpoint = '/secured/history'; - $scope.elems = { - connect: 'connect', - from: 'from', - text: 'text', - disconnect: 'disconnect', - conversationDiv: 'conversationDiv', - response: 'response' - }; + /** + * URL mapping endpoints. + */ - $scope.connect = function (context) { - $scope.sendEndpoint = '/secured/chat'; - $scope.sendEndpoint = context + $scope.sendEndpoint ; - $scope.stompClient = SocketService.connect($scope.sendEndpoint, $scope.elems); - }; + var SECURED_CHAT = '/secured/chat'; + var SECURED_CHAT_HISTORY = '/secured/history'; + var SECURED_CHAT_ROOM = '/secured/room'; + var SECURED_CHAT_SPECIFIC_USER = '/secured/user/queue/specific-user'; - $scope.subscribe = function () { - $scope.stompClient.subscribe($scope.subscribeEndpoint, function (msgOut) { - SocketService.messageOut(JSON.parse(msgOut.body), $scope.elems); - }); - }; + var opts = { + from: 'from', + to: 'to', + text: 'text', + disconnect: 'disconnect', + conversationDiv: 'conversationDiv', + response: 'response' + }; - $scope.disconnect = function () { - SocketService.disconnect($scope.elems, $scope.stompClient); - }; + $scope.sendEndpoint = ''; + $scope.stompClient = null; - $scope.sendMessage = function () { - SocketService.sendMessage( $scope.elems, $scope.stompClient, $scope.sendEndpoint); - }; - }); \ No newline at end of file + /** + * Broadcast to All Users. + */ + + $scope.connectAll = function (context) { + $scope.sendEndpoint = context + SECURED_CHAT; + $scope.stompClient = SocketService.connect($scope.sendEndpoint , opts, true); + }; + + $scope.subscribeAll = function () { SocketService.subscribeToAll($scope.stompClient, SECURED_CHAT_HISTORY, opts); }; + + /** + * Broadcast to Specific User. + */ + + $scope.connectSpecific = function (context) { + $scope.sendEndpoint = context + SECURED_CHAT_ROOM; + $scope.stompClient = SocketService.connect(context + SECURED_CHAT_ROOM, opts, false); + }; + + $scope.subscribeSpecific = function () { SocketService.subscribeToSpecific($scope.stompClient, SECURED_CHAT_SPECIFIC_USER, opts); }; + + $scope.disconnect = function () { SocketService.disconnect(opts, $scope.stompClient); }; + + $scope.sendMessage = function () { SocketService.sendMessage(opts, $scope.stompClient, $scope.sendEndpoint); }; +}); \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/successController.js b/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/successController.js index ccb972202d..6926239202 100644 --- a/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/successController.js +++ b/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/successController.js @@ -1,13 +1,12 @@ 'use strict'; angularApp - .controller('successController', function ($scope) { +.controller('successController', function ($scope) { + $scope.successMsg = ''; - $scope.successMsg = ''; + $scope.initialize = function () { + $scope.successMsg = "You've logged in!"; + }; - $scope.initialize = function () { - $scope.successMsg = "You've logged in!"; - }; - - $scope.initialize(); - }); + $scope.initialize(); +}); diff --git a/spring-security-mvc-socket/src/main/webapp/resources/scripts/routes/router.js b/spring-security-mvc-socket/src/main/webapp/resources/scripts/routes/router.js index 70644f996d..004c7b331a 100644 --- a/spring-security-mvc-socket/src/main/webapp/resources/scripts/routes/router.js +++ b/spring-security-mvc-socket/src/main/webapp/resources/scripts/routes/router.js @@ -1,19 +1,16 @@ 'use strict'; angularApp - .config(function ($routeProvider) { - $routeProvider - .when('/index', { - controller: 'indexController' - }) - .when('/login', { - controller: 'loginController' - }) - .when('/sockets', { - controller: 'socketController' - }) - .when('/success', { - controller: 'successController' - }) - .otherwise('/index'); - }); \ No newline at end of file +.config(function ($routeProvider) { + $routeProvider + .when('/index', { + controller: 'indexController' + }) + .when('/sockets', { + controller: 'socketController' + }) + .when('/success', { + controller: 'successController' + }) + .otherwise('/index'); +}); \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/resources/scripts/services/SocketService.js b/spring-security-mvc-socket/src/main/webapp/resources/scripts/services/SocketService.js index 56930808a3..ba3f397f89 100644 --- a/spring-security-mvc-socket/src/main/webapp/resources/scripts/services/SocketService.js +++ b/spring-security-mvc-socket/src/main/webapp/resources/scripts/services/SocketService.js @@ -1,46 +1,99 @@ 'use strict'; +var idHelper = function (context) { + return document.getElementById(context); +}; + function SocketService() { - var that = this, - idHelper = function(context) { - return document.getElementById(context); - }; - that.setConnected = function (elems, connected) { - idHelper(elems.connect).disabled = connected; - idHelper(elems.disconnect).disabled = !connected; - idHelper(elems.conversationDiv).style.visibility = connected ? 'visible' : 'hidden'; - idHelper(elems.response).innerHTML = ''; + var that = this; + + that.sessionId = ''; + + /** + * Generic methods. + */ + + that.setConnected = function (opts, connected) { + idHelper('connectAll').disabled = connected; + idHelper('connectSpecific').disabled = connected; + idHelper(opts.disconnect).disabled = !connected; + idHelper(opts.response).innerHTML = ''; + idHelper('subscribeAll').disabled = !connected; + idHelper('subscribeSpecific').disabled = !connected; + }; + + that.connect = function (endpoint, opts, isBroadcastAll) { + var socket = new SockJS(endpoint), stompClient = Stomp.over(socket); + stompClient.connect({}, function (frame) { + that.setConnected(opts, true); + + if (!isBroadcastAll) { + var url = stompClient.ws._transport.url; + console.log(stompClient.ws._transport.url); + url = url.replace("ws://localhost:8080/spring-security-mvc-socket/secured/room/", ""); + url = url.replace("/websocket", ""); + url = url.replace(/^[0-9]+\//, ""); + console.log("Your current session is: " + url); + that.sessionId = url; + } + + }); + return stompClient; + }; + + that.disconnect = function (opts, stompClient) { + if (stompClient !== null && stompClient !== undefined) { stompClient.disconnect(); } + that.setConnected(opts, false); + }; + + that.sendMessage = function (opts, stompClient, endpoint) { + var to = idHelper(opts.to).value; + var from = idHelper(opts.from).value; + + var msg = { + 'from': (from === undefined || from === null ) ? to : from, + 'to': (to === undefined || to === null ) ? "ALL" : to, + 'text': idHelper(opts.text).value }; - that.connect = function (endpoint, elems) { - var socket = new SockJS(endpoint), stompClient = Stomp.over(socket); - stompClient.connect({}, function (frame) { - that.setConnected(elems, true); - }); - return stompClient; - }; + console.log(JSON.stringify(msg)); + stompClient.send(endpoint, {}, JSON.stringify(msg)); + }; - that.disconnect = function (elems, stompClient) { - if (stompClient !== null && stompClient !== undefined) stompClient.disconnect(); - that.setConnected(elems, false); - }; + that.messageOut = function (msg, opts) { + var r = idHelper(opts.response), p = document.createElement('p'); + p.style.wordWrap = 'break-word'; + p.appendChild(document.createTextNode(msg.from + ': ' + msg.text + ' (' + msg.time + ')')); + r.appendChild(p); + }; + + /** + * Broadcast to All Users. + */ + + that.subscribeToAll = function(client, url, opts) { + idHelper('subscribeAll').disabled = true; + idHelper('subscribeSpecific').disabled = true; + client.subscribe(url, function (msgOut) { + that.messageOut(JSON.parse(msgOut.body), opts); + }); + }; + + /** + * Broadcast to Specific User. + */ + + that.subscribeToSpecific = function(client, url, opts) { + idHelper('subscribeAll').disabled = true; + idHelper('subscribeSpecific').disabled = true; + client.subscribe(url + "-user" + that.sessionId, function (msgOut) { + that.messageOut(JSON.parse(msgOut.body), opts); + }); + }; - that.sendMessage = function (elems, stompClient, endpoint) { - stompClient.send(endpoint, {}, - JSON.stringify({ - 'from': idHelper(elems.from).value, - 'text': idHelper(elems.text).value - })); - }; - that.messageOut = function (msg, elems) { - var r = idHelper(elems.response), p = document.createElement('p'); - p.style.wordWrap = 'break-word'; - p.appendChild(document.createTextNode(msg.from + ': ' + msg.text + ' (' + msg.time + ')')); - r.appendChild(p); - }; } angularApp - .service('SocketService', SocketService); \ No newline at end of file +.service('SocketService', SocketService); \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/resources/styles/app.css b/spring-security-mvc-socket/src/main/webapp/resources/styles/app.css new file mode 100644 index 0000000000..ee2cb4d717 --- /dev/null +++ b/spring-security-mvc-socket/src/main/webapp/resources/styles/app.css @@ -0,0 +1,70 @@ +@import url('https://fonts.googleapis.com/css?family=Poiret+One'); + +html, body, main { + padding: 0; + margin: 0; + position: absolute; + height: 100%; + width: 100%; + color: white; + font-family: 'Poiret One', sans-serif; + opacity:.95; +} + +main { + background-size: cover; +} + +main > div.wrapper { + position: relative; + left: 15%; + top: 15%; + width: 40%; +} + +h1 { + text-transform: uppercase; + font-size: 48px; + letter-spacing: 2px; +} + +span, a { + font-size: 24px; + letter-spacing: 1px; +} + +a { + text-decoration: none; + color: white; +} + +a:hover { + color: lightgray; +} + +input { + width: 225px; + background: none !important; + border-radius: 25px; + background-color: #fff; + color: white; + outline: none; + margin-left: 2px; + padding-left: 25px; + padding-bottom: 9px; + padding-top: 9px; +} + +button { + width: 100px; + align-content: center; + text-align: center; + background: transparent; + border-radius: 25px; + padding: 6px; + color: white; +} + +button:hover { + opacity: .6; +} \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/resources/styles/denied.css b/spring-security-mvc-socket/src/main/webapp/resources/styles/denied.css new file mode 100644 index 0000000000..3956715e4a --- /dev/null +++ b/spring-security-mvc-socket/src/main/webapp/resources/styles/denied.css @@ -0,0 +1,3 @@ +main { + background-color: black; +} diff --git a/spring-security-mvc-socket/src/main/webapp/resources/styles/index.css b/spring-security-mvc-socket/src/main/webapp/resources/styles/index.css new file mode 100644 index 0000000000..b6da4814b7 --- /dev/null +++ b/spring-security-mvc-socket/src/main/webapp/resources/styles/index.css @@ -0,0 +1,7 @@ +main { + background-color: black; +} + +span, a { + top: 175px; +} \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/resources/styles/login.css b/spring-security-mvc-socket/src/main/webapp/resources/styles/login.css new file mode 100644 index 0000000000..197f3e41bb --- /dev/null +++ b/spring-security-mvc-socket/src/main/webapp/resources/styles/login.css @@ -0,0 +1,17 @@ +main { + background-color: black; +} + +input[type="submit"] { + width: 100px; + align-content: center; + text-align: center; + background: transparent; + border-radius: 25px; + padding: 6px; + color: white; +} + +input[type="submit"]:hover { + opacity:.6; +} \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/resources/styles/socket.css b/spring-security-mvc-socket/src/main/webapp/resources/styles/socket.css new file mode 100644 index 0000000000..aa483fd684 --- /dev/null +++ b/spring-security-mvc-socket/src/main/webapp/resources/styles/socket.css @@ -0,0 +1,19 @@ +main { + background-color: black; +} + +div#all, div#specific, div#conversationDiv { + border: 1px solid white; + border-radius: 25px; + padding: 15px; + margin: 15px +} + +button[disabled=true] { + color: lightgray; + border: 1px solid lightgray; +} + +button[disabled=true]:hover { + opacity: 1; +} \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/resources/styles/success.css b/spring-security-mvc-socket/src/main/webapp/resources/styles/success.css new file mode 100644 index 0000000000..3956715e4a --- /dev/null +++ b/spring-security-mvc-socket/src/main/webapp/resources/styles/success.css @@ -0,0 +1,3 @@ +main { + background-color: black; +} diff --git a/spring-security-openid/pom.xml b/spring-security-openid/pom.xml index a2c0b6b119..4343996e02 100644 --- a/spring-security-openid/pom.xml +++ b/spring-security-openid/pom.xml @@ -3,9 +3,7 @@ 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-security-openid - 0.0.1-SNAPSHOT war spring-security-openid Spring OpenID sample project diff --git a/spring-security-rest/pom.xml b/spring-security-rest/pom.xml index 5bde78db7e..57ce5ddb92 100644 --- a/spring-security-rest/pom.xml +++ b/spring-security-rest/pom.xml @@ -300,7 +300,7 @@ 2.9.0 - 2.7.0 + 2.9.2 2.6 diff --git a/spring-security-x509/spring-security-x509-basic-auth/pom.xml b/spring-security-x509/spring-security-x509-basic-auth/pom.xml index 9ae1ec65b2..67f2b4fbcd 100644 --- a/spring-security-x509/spring-security-x509-basic-auth/pom.xml +++ b/spring-security-x509/spring-security-x509-basic-auth/pom.xml @@ -3,10 +3,10 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - basic-secured-server + spring-security-x509-basic-auth 0.0.1-SNAPSHOT jar - basic-secured-server + Spring Security x.509 Basic Authentication Spring x.509 Authentication Demo diff --git a/spring-security-x509/spring-security-x509-client-auth/pom.xml b/spring-security-x509/spring-security-x509-client-auth/pom.xml index 4ade2aacd6..5cb4001a94 100644 --- a/spring-security-x509/spring-security-x509-client-auth/pom.xml +++ b/spring-security-x509/spring-security-x509-client-auth/pom.xml @@ -3,10 +3,10 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - client-auth-server + spring-security-x509-client-auth 0.0.1-SNAPSHOT jar - client-auth-server + Spring Security x.509 Client Authentication Spring x.509 Client Authentication Demo diff --git a/spring-spel/pom.xml b/spring-spel/pom.xml index da8be2ca8d..4af318fde2 100644 --- a/spring-spel/pom.xml +++ b/spring-spel/pom.xml @@ -3,8 +3,9 @@ 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 - spel + spring-spel 1.0-SNAPSHOT + Spring SpEL com.baeldung diff --git a/spring-swagger-codegen/pom.xml b/spring-swagger-codegen/pom.xml index d14dcc3176..8e551d850f 100644 --- a/spring-swagger-codegen/pom.xml +++ b/spring-swagger-codegen/pom.xml @@ -1,7 +1,6 @@ 4.0.0 - com.baeldung spring-swagger-codegen 0.0.1-SNAPSHOT pom diff --git a/spring-swagger-codegen/spring-swagger-codegen-api-client/pom.xml b/spring-swagger-codegen/spring-swagger-codegen-api-client/pom.xml index aac6fe9843..06a92ffae7 100644 --- a/spring-swagger-codegen/spring-swagger-codegen-api-client/pom.xml +++ b/spring-swagger-codegen/spring-swagger-codegen-api-client/pom.xml @@ -2,11 +2,9 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 - com.baeldung spring-swagger-codegen-api-client jar spring-swagger-codegen-api-client - 0.0.1-SNAPSHOT https://github.com/swagger-api/swagger-codegen Swagger Java @@ -14,9 +12,7 @@ scm:git:git@github.com:swagger-api/swagger-codegen.git https://github.com/swagger-api/swagger-codegen - - 2.2.0 - + Unlicense @@ -42,37 +38,6 @@ - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - - - - loggerPath - conf/log4j.properties - - - -Xms512m -Xmx1500m - methods - pertest - - - - maven-dependency-plugin - - - package - - copy-dependencies - - - ${project.build.directory}/lib - - - - - org.apache.maven.plugins diff --git a/spring-swagger-codegen/spring-swagger-codegen-app/pom.xml b/spring-swagger-codegen/spring-swagger-codegen-app/pom.xml index 281ed59857..0fb5cf8e75 100644 --- a/spring-swagger-codegen/spring-swagger-codegen-app/pom.xml +++ b/spring-swagger-codegen/spring-swagger-codegen-app/pom.xml @@ -1,11 +1,8 @@ 4.0.0 - - com.baeldung spring-swagger-codegen-app - 0.0.1-SNAPSHOT - + com.baeldung spring-swagger-codegen @@ -37,7 +34,6 @@ - 1.8 0.0.1-SNAPSHOT 1.5.10.RELEASE diff --git a/spring-thymeleaf/README.md b/spring-thymeleaf/README.md index 0dcb742c09..f9e7dd90a7 100644 --- a/spring-thymeleaf/README.md +++ b/spring-thymeleaf/README.md @@ -17,6 +17,7 @@ - [Working With Arrays in Thymeleaf](http://www.baeldung.com/thymeleaf-arrays) - [Spring with Thymeleaf Pagination for a List](http://www.baeldung.com/spring-thymeleaf-pagination) - [Working with Select and Option in Thymeleaf](http://www.baeldung.com/thymeleaf-select-option) +- [Working With Custom HTML Attributes in Thymeleaf](https://www.baeldung.com/thymeleaf-custom-html-attributes) ### Build the Project diff --git a/spring-vault/pom.xml b/spring-vault/pom.xml new file mode 100644 index 0000000000..31616cdb16 --- /dev/null +++ b/spring-vault/pom.xml @@ -0,0 +1,67 @@ + + + 4.0.0 + + com.baeldung + spring-vault + 0.0.1-SNAPSHOT + jar + spring-vault + Spring Vault sample project + + + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.vault + spring-vault-core + ${spring.vault.core.version} + + + com.fasterxml.jackson.core + jackson-databind + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + **/*IntegrationTest.java + + + + + + + + UTF-8 + 2.0.1.RELEASE + + diff --git a/spring-vault/src/main/java/org/baeldung/springvault/Credentials.java b/spring-vault/src/main/java/org/baeldung/springvault/Credentials.java new file mode 100644 index 0000000000..f90ab66a4e --- /dev/null +++ b/spring-vault/src/main/java/org/baeldung/springvault/Credentials.java @@ -0,0 +1,30 @@ +package org.baeldung.springvault; + +public class Credentials { + + private String username; + private String password; + + public Credentials() { + + } + + public Credentials(String username, String password) { + this.username = username; + this.password = password; + } + + public String getUsername() { + return username; + } + + public String getPassword() { + return password; + } + + @Override + public String toString() { + return "Credential [username=" + username + ", password=" + password + "]"; + } + +} diff --git a/spring-vault/src/main/java/org/baeldung/springvault/CredentialsService.java b/spring-vault/src/main/java/org/baeldung/springvault/CredentialsService.java new file mode 100644 index 0000000000..87c24bd947 --- /dev/null +++ b/spring-vault/src/main/java/org/baeldung/springvault/CredentialsService.java @@ -0,0 +1,50 @@ +package org.baeldung.springvault; + +import java.net.URI; +import java.net.URISyntaxException; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import org.springframework.vault.authentication.TokenAuthentication; +import org.springframework.vault.client.VaultEndpoint; +import org.springframework.vault.core.VaultTemplate; +import org.springframework.vault.core.env.VaultPropertySource; +import org.springframework.vault.support.VaultResponse; +import org.springframework.vault.support.VaultResponseSupport; + +/** + * Sample service to demonstrate storing and retrieval of secrets. + * + * NOTE: We need to configure Vault and provide the Vault uri in the properties file. + * + */ +@Service +public class CredentialsService { + + @Autowired + private VaultTemplate vaultTemplate; + + /** + * To Secure Credentials + * @param credentials + * @return VaultResponse + * @throws URISyntaxException + */ + public void secureCredentials(Credentials credentials) throws URISyntaxException { + + vaultTemplate.write("credentials/myapp", credentials); + } + + /** + * To Retrieve Credentials + * @return Credentials + * @throws URISyntaxException + */ + public Credentials accessCredentials() throws URISyntaxException { + + VaultResponseSupport response = vaultTemplate.read("credentials/myapp", Credentials.class); + return response.getData(); + } + +} diff --git a/spring-vault/src/main/java/org/baeldung/springvault/SpringVaultApplication.java b/spring-vault/src/main/java/org/baeldung/springvault/SpringVaultApplication.java new file mode 100644 index 0000000000..916a809be5 --- /dev/null +++ b/spring-vault/src/main/java/org/baeldung/springvault/SpringVaultApplication.java @@ -0,0 +1,17 @@ +package org.baeldung.springvault; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Sample application to demonstrate basics of Spring Vault + * + */ +@SpringBootApplication +public class SpringVaultApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringVaultApplication.class, args); + } + +} diff --git a/spring-vault/src/main/java/org/baeldung/springvault/VaultConfig.java b/spring-vault/src/main/java/org/baeldung/springvault/VaultConfig.java new file mode 100644 index 0000000000..106a1a3d42 --- /dev/null +++ b/spring-vault/src/main/java/org/baeldung/springvault/VaultConfig.java @@ -0,0 +1,26 @@ +package org.baeldung.springvault; + +import org.springframework.context.annotation.Configuration; +import org.springframework.vault.authentication.ClientAuthentication; +import org.springframework.vault.authentication.TokenAuthentication; +import org.springframework.vault.client.VaultEndpoint; +import org.springframework.vault.config.AbstractVaultConfiguration; + +/** + * Example class to configure Vault beans using AbstractVaultConfiguration + * + */ +@Configuration +public class VaultConfig extends AbstractVaultConfiguration { + + @Override + public ClientAuthentication clientAuthentication() { + return new TokenAuthentication("00000000-0000-0000-0000-000000000000"); + } + + @Override + public VaultEndpoint vaultEndpoint() { + return VaultEndpoint.create("host", 8020); + } + +} diff --git a/spring-vault/src/main/java/org/baeldung/springvault/VaultEnvironmentConfig.java b/spring-vault/src/main/java/org/baeldung/springvault/VaultEnvironmentConfig.java new file mode 100644 index 0000000000..6c796bc718 --- /dev/null +++ b/spring-vault/src/main/java/org/baeldung/springvault/VaultEnvironmentConfig.java @@ -0,0 +1,21 @@ +package org.baeldung.springvault; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.PropertySource; +import org.springframework.vault.authentication.ClientAuthentication; +import org.springframework.vault.authentication.TokenAuthentication; +import org.springframework.vault.client.VaultEndpoint; +import org.springframework.vault.config.AbstractVaultConfiguration; +import org.springframework.vault.config.EnvironmentVaultConfiguration; + +/** + * Example class to configure Vault beans using EnvironmentVaultConfiguration + * + */ +@Configuration +@PropertySource(value = { "vault-config.properties" }) +@Import(value = EnvironmentVaultConfiguration.class) +public class VaultEnvironmentConfig { + +} diff --git a/spring-vault/src/main/resources/vault-config.properties b/spring-vault/src/main/resources/vault-config.properties new file mode 100644 index 0000000000..68a080ebe8 --- /dev/null +++ b/spring-vault/src/main/resources/vault-config.properties @@ -0,0 +1,2 @@ +vault.uri=https://localhost:8200 +vault.token=00000000-0000-0000-0000-000000000000 \ No newline at end of file diff --git a/spring-vault/src/test/java/org/baeldung/springvault/VaultInitializer.java b/spring-vault/src/test/java/org/baeldung/springvault/VaultInitializer.java new file mode 100644 index 0000000000..c7db5eb199 --- /dev/null +++ b/spring-vault/src/test/java/org/baeldung/springvault/VaultInitializer.java @@ -0,0 +1,113 @@ +package org.baeldung.springvault; + +import java.io.BufferedReader; +import java.io.Closeable; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Map; + +/** + * + * This is a test class to initialize Vault. + */ +public class VaultInitializer implements Closeable { + + private static final String UNSEAL_KEY = "Unseal Key:"; + private static final String ROOT_TOKEN = "Root Token:"; + + private Process vaultProcess; + private String unSealKey; + private String rootToken; + + public String getRootToken() { + return rootToken; + } + + public String getUnSealKey() { + return unSealKey; + } + + public static final VaultInitializer initializeValut() { + VaultInitializer vaultProcess = new VaultInitializer(); + vaultProcess.start(); + // Secrets is by default enabled. + vaultProcess.enableSecrets(); + return vaultProcess; + } + + @SuppressWarnings("unused") + private void enableSecrets() { + System.out.println("Enabling Secrets at path credentials/myapp..."); + ProcessBuilder pb = new ProcessBuilder("vault", "secrets", "enable", "-path=credentials/myapp", "kv"); + Map map = pb.environment(); + map.put("VAULT_ADDR", "http://127.0.0.1:8200"); + try { + Process p = pb.inheritIO() + .start(); + p.waitFor(); + } catch (IOException e) { + System.out.println("unable to enableSecrets" + e); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + public void start() { + System.out.println("starting vault"); + // This starts the vault server. + ProcessBuilder pb = new ProcessBuilder("vault", "server", "-dev"); + + try { + vaultProcess = pb.start(); + // wait for initialization to complete. + Thread.sleep(5000); + } catch (IOException e) { + System.out.println("unable to start vault in new process" + e); + } catch (InterruptedException e) { + System.out.println("Thread interrupted " + e); + } + extractUnsealKeyAndToken(); + } + + /** + * To get the root token which is generated every time server is initialized. + */ + private void extractUnsealKeyAndToken() { + BufferedReader reader = new BufferedReader(new InputStreamReader(vaultProcess.getInputStream())); + StringBuilder builder = new StringBuilder(); + String line = null; + boolean tokenExtracted = false; + try { + while ((line = reader.readLine()) != null) { + builder.append(line); + builder.append(System.getProperty("line.separator")); + if (line.contains(UNSEAL_KEY)) { + String tmp = line.replace(UNSEAL_KEY, ""); + unSealKey = tmp.trim(); + } else if (line.contains(ROOT_TOKEN)) { + String tmp = line.replace(ROOT_TOKEN, ""); + rootToken = tmp.trim(); + tokenExtracted = true; + } + if (tokenExtracted) + break; + System.out.println(line); + } + } catch (IOException e) { + System.out.println("unable to read vault output" + e); + } + + String result = builder.toString(); + + System.out.println("Unseal Key {}" + unSealKey); + System.out.println("Root Token {}" + rootToken); + System.out.println(result); + } + + @Override + public void close() throws IOException { + + System.out.println("stoping vault"); + vaultProcess.destroy(); + } +} diff --git a/spring-vault/src/test/java/org/baeldung/springvault/VaultIntegrationTest.java b/spring-vault/src/test/java/org/baeldung/springvault/VaultIntegrationTest.java new file mode 100644 index 0000000000..75c700ee26 --- /dev/null +++ b/spring-vault/src/test/java/org/baeldung/springvault/VaultIntegrationTest.java @@ -0,0 +1,80 @@ +package org.baeldung.springvault; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; +import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.core.annotation.Order; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.ClassMode; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; +import org.springframework.vault.authentication.TokenAuthentication; +import org.springframework.vault.client.VaultEndpoint; +import org.springframework.vault.core.VaultTemplate; +import org.springframework.vault.support.VaultResponse; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = CredentialsService.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ContextConfiguration(classes = VaultTestConfiguration.class, loader = AnnotationConfigContextLoader.class) +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@DirtiesContext(classMode = ClassMode.AFTER_CLASS) +public class VaultIntegrationTest { + + @Autowired + private CredentialsService credentialsService; + + /** + * Test to secure credentials. + * + * @throws URISyntaxException + */ + @Test + public void givenCredentials_whenSecureCredentials_thenCredentialsSecured() throws URISyntaxException { + try { + // Given + Credentials credentials = new Credentials("username", "password"); + + // When + credentialsService.secureCredentials(credentials); + + } catch (Exception e) { + e.printStackTrace(); + } + + } + + /** + * Test to access credentials + * @throws URISyntaxException + */ + @Test + public void whenAccessCredentials_thenCredentialsRetrieved() throws URISyntaxException { + + // Given + Credentials credentials = credentialsService.accessCredentials(); + + // Then + assertNotNull(credentials); + assertEquals("username", credentials.getUsername()); + assertEquals("password", credentials.getPassword()); + } + +} diff --git a/spring-vault/src/test/java/org/baeldung/springvault/VaultTestConfiguration.java b/spring-vault/src/test/java/org/baeldung/springvault/VaultTestConfiguration.java new file mode 100644 index 0000000000..09a1445788 --- /dev/null +++ b/spring-vault/src/test/java/org/baeldung/springvault/VaultTestConfiguration.java @@ -0,0 +1,29 @@ +package org.baeldung.springvault; + +import java.net.URI; +import java.net.URISyntaxException; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.vault.authentication.TokenAuthentication; +import org.springframework.vault.client.VaultEndpoint; +import org.springframework.vault.core.VaultTemplate; + +@Configuration +public class VaultTestConfiguration { + + @Bean + public VaultInitializer vaultInitializer() { + VaultInitializer vaultInitializer = VaultInitializer.initializeValut(); + return vaultInitializer; + } + + @Bean + public VaultTemplate vaultTemplate() throws URISyntaxException { + + VaultInitializer vaultInitializer = vaultInitializer(); + VaultTemplate vaultTemplate = new VaultTemplate(VaultEndpoint.from(new URI("http://localhost:8200")), new TokenAuthentication(vaultInitializer.getRootToken())); + return vaultTemplate; + + } +} diff --git a/spring-vertx/pom.xml b/spring-vertx/pom.xml index 4ba90b841e..790eeff128 100644 --- a/spring-vertx/pom.xml +++ b/spring-vertx/pom.xml @@ -3,11 +3,9 @@ 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 - vertx-spring - 0.0.1-SNAPSHOT + spring-vertx jar - spring-vertx + Spring Vertx A demo project with vertx spring integration @@ -57,31 +55,10 @@ org.springframework.boot spring-boot-maven-plugin - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - - 3 - true - - **/*IntegrationTest.java - **/*IntTest.java - **/*LongRunningUnitTest.java - **/*ManualTest.java - **/JdbcTest.java - **/*LiveTest.java - - - - - UTF-8 - UTF-8 - 1.8 3.4.1 diff --git a/struts-2/pom.xml b/struts-2/pom.xml index 26998fa544..fee68c8303 100644 --- a/struts-2/pom.xml +++ b/struts-2/pom.xml @@ -2,10 +2,10 @@ 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 - struts + struts-2 0.0.1-SNAPSHOT pom - struts + Struts 2 com.baeldung diff --git a/testing-modules/README.md b/testing-modules/README.md index b269f547ec..b189b9819e 100644 --- a/testing-modules/README.md +++ b/testing-modules/README.md @@ -13,3 +13,4 @@ - [Headers, Cookies and Parameters with REST-assured](http://www.baeldung.com/rest-assured-header-cookie-parameter) - [JSON Schema Validation with REST-assured](http://www.baeldung.com/rest-assured-json-schema) - [Testing Callbacks with Mockito](http://www.baeldung.com/mockito-callbacks) +- [Running JUnit Tests in Parallel with Maven](https://www.baeldung.com/maven-junit-parallel-tests) diff --git a/testing-modules/gatling/README.md b/testing-modules/gatling/README.md index 5c81c4bd6a..941ab75f06 100644 --- a/testing-modules/gatling/README.md +++ b/testing-modules/gatling/README.md @@ -1,2 +1,5 @@ ### Relevant Articles: - [Intro to Gatling](http://www.baeldung.com/introduction-to-gatling) + +### Running a simualtion +- To run a simulation use "simulation" profile, command - `mvn install -Psimulation -Dgib.enabled=false` diff --git a/testing-modules/gatling/pom.xml b/testing-modules/gatling/pom.xml index 1afaefd47e..8d4c89ec62 100644 --- a/testing-modules/gatling/pom.xml +++ b/testing-modules/gatling/pom.xml @@ -7,13 +7,13 @@ gatling 1.0-SNAPSHOT - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - ../../ - - + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + + io.gatling.highcharts @@ -68,6 +68,14 @@ + + + + + + simulation + + io.gatling gatling-maven-plugin @@ -78,11 +86,16 @@ execute + + true + + + @@ -113,10 +126,10 @@ 1.8 1.8 UTF-8 - 2.11.12 - 2.2.5 + 2.12.6 + 2.3.1 3.2.2 - 2.2.1 + 2.2.4 diff --git a/testing-modules/junit-5/README.md b/testing-modules/junit-5/README.md index 5a73bca4d6..40c4e8cde9 100644 --- a/testing-modules/junit-5/README.md +++ b/testing-modules/junit-5/README.md @@ -14,4 +14,5 @@ - [Migrating from JUnit 4 to JUnit 5](http://www.baeldung.com/junit-5-migration) - [JUnit5 Programmatic Extension Registration with @RegisterExtension](http://www.baeldung.com/junit-5-registerextension-annotation) - [The Order of Tests in JUnit](http://www.baeldung.com/junit-5-test-order) - +- [Running JUnit Tests Programmatically, from a Java Application](https://www.baeldung.com/junit-tests-run-programmatically-from-java) +- [Testing an Abstract Class With JUnit](https://www.baeldung.com/junit-test-abstract-class) diff --git a/testing-modules/mockito/pom.xml b/testing-modules/mockito/pom.xml index fab2fcc795..acdd7eb2ac 100644 --- a/testing-modules/mockito/pom.xml +++ b/testing-modules/mockito/pom.xml @@ -14,6 +14,27 @@ + + + org.springframework + spring-core + ${spring-framework.version} + + + org.springframework + spring-context + ${spring-framework.version} + + + org.springframework.data + spring-data-jpa + ${spring-data.version} + + + org.eclipse.persistence + javax.persistence + 2.1.1 + @@ -46,35 +67,15 @@ org.springframework.boot spring-boot-starter - LATEST + ${spring-boot.version} test org.springframework.boot spring-boot-starter-test - LATEST + ${spring-boot.version} test - - org.springframework - spring-core - LATEST - - - org.springframework - spring-context - LATEST - - - org.eclipse.persistence - javax.persistence - 2.1.1 - - - org.springframework.data - spring-data-jpa - LATEST - @@ -88,6 +89,9 @@ + 2.0.4.RELEASE + 5.0.8.RELEASE + 2.0.9.RELEASE 19.0 3.5 diff --git a/testing-modules/spring-testing/src/main/java/com/baeldung/testpropertysource/ClassUsingProperty.java b/testing-modules/spring-testing/src/main/java/com/baeldung/testpropertysource/ClassUsingProperty.java new file mode 100644 index 0000000000..d545daf0df --- /dev/null +++ b/testing-modules/spring-testing/src/main/java/com/baeldung/testpropertysource/ClassUsingProperty.java @@ -0,0 +1,15 @@ +package com.baeldung.testpropertysource; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class ClassUsingProperty { + + @Value("${baeldung.testpropertysource.one}") + private String propertyOne; + + public String retrievePropertyOne() { + return propertyOne; + } +} diff --git a/testing-modules/spring-testing/src/test/java/com/baeldung/testpropertysource/DefaultTestPropertySourceIntegrationTest.java b/testing-modules/spring-testing/src/test/java/com/baeldung/testpropertysource/DefaultTestPropertySourceIntegrationTest.java new file mode 100644 index 0000000000..f983abdd54 --- /dev/null +++ b/testing-modules/spring-testing/src/test/java/com/baeldung/testpropertysource/DefaultTestPropertySourceIntegrationTest.java @@ -0,0 +1,26 @@ +package com.baeldung.testpropertysource; + +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.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = ClassUsingProperty.class) +@TestPropertySource +public class DefaultTestPropertySourceIntegrationTest { + + @Autowired + ClassUsingProperty classUsingProperty; + + @Test + public void givenDefaultTestPropertySource_whenVariableOneRetrieved_thenValueInDefaultFileReturned() { + String output = classUsingProperty.retrievePropertyOne(); + + assertThat(output).isEqualTo("default-value"); + } +} diff --git a/testing-modules/spring-testing/src/test/java/com/baeldung/testpropertysource/LocationTestPropertySourceIntegrationTest.java b/testing-modules/spring-testing/src/test/java/com/baeldung/testpropertysource/LocationTestPropertySourceIntegrationTest.java new file mode 100644 index 0000000000..93d4cc58da --- /dev/null +++ b/testing-modules/spring-testing/src/test/java/com/baeldung/testpropertysource/LocationTestPropertySourceIntegrationTest.java @@ -0,0 +1,26 @@ +package com.baeldung.testpropertysource; + +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.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = ClassUsingProperty.class) +@TestPropertySource(locations = "/other-location.properties") +public class LocationTestPropertySourceIntegrationTest { + + @Autowired + ClassUsingProperty classUsingProperty; + + @Test + public void givenDefaultTestPropertySource_whenVariableOneRetrieved_thenValueInDefaultFileReturned() { + String output = classUsingProperty.retrievePropertyOne(); + + assertThat(output).isEqualTo("other-location-value"); + } +} diff --git a/testing-modules/spring-testing/src/test/java/com/baeldung/testpropertysource/PropertiesTestPropertySourceIntegrationTest.java b/testing-modules/spring-testing/src/test/java/com/baeldung/testpropertysource/PropertiesTestPropertySourceIntegrationTest.java new file mode 100644 index 0000000000..d98e2b9f98 --- /dev/null +++ b/testing-modules/spring-testing/src/test/java/com/baeldung/testpropertysource/PropertiesTestPropertySourceIntegrationTest.java @@ -0,0 +1,26 @@ +package com.baeldung.testpropertysource; + +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.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = ClassUsingProperty.class) +@TestPropertySource(locations = "/other-location.properties", properties = "baeldung.testpropertysource.one=other-properties-value") +public class PropertiesTestPropertySourceIntegrationTest { + + @Autowired + ClassUsingProperty classUsingProperty; + + @Test + public void givenDefaultTestPropertySource_whenVariableOneRetrieved_thenValueInDefaultFileReturned() { + String output = classUsingProperty.retrievePropertyOne(); + + assertThat(output).isEqualTo("other-properties-value"); + } +} diff --git a/testing-modules/spring-testing/src/test/resources/com/baeldung/testpropertysource/DefaultTestPropertySourceIntegrationTest.properties b/testing-modules/spring-testing/src/test/resources/com/baeldung/testpropertysource/DefaultTestPropertySourceIntegrationTest.properties new file mode 100644 index 0000000000..a443a2b776 --- /dev/null +++ b/testing-modules/spring-testing/src/test/resources/com/baeldung/testpropertysource/DefaultTestPropertySourceIntegrationTest.properties @@ -0,0 +1 @@ +baeldung.testpropertysource.one=default-value \ No newline at end of file diff --git a/testing-modules/spring-testing/src/test/resources/other-location.properties b/testing-modules/spring-testing/src/test/resources/other-location.properties new file mode 100644 index 0000000000..6ef791ec75 --- /dev/null +++ b/testing-modules/spring-testing/src/test/resources/other-location.properties @@ -0,0 +1 @@ +baeldung.testpropertysource.one=other-location-value \ No newline at end of file diff --git a/testing-modules/testing/README.md b/testing-modules/testing/README.md index 6f13f45194..849b578a55 100644 --- a/testing-modules/testing/README.md +++ b/testing-modules/testing/README.md @@ -20,3 +20,4 @@ - [Custom Assertions with AssertJ](http://www.baeldung.com/assertj-custom-assertion) - [Using Conditions with AssertJ](http://www.baeldung.com/assertj-conditions) - [Guide to JavaFaker](https://www.baeldung.com/java-faker) +- [Running JUnit Tests Programmatically, from a Java Application](https://www.baeldung.com/junit-tests-run-programmatically-from-java) diff --git a/testing-modules/testng/pom.xml b/testing-modules/testng/pom.xml index 028db38727..a9f680aafb 100644 --- a/testing-modules/testng/pom.xml +++ b/testing-modules/testng/pom.xml @@ -38,24 +38,6 @@ true - - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - - diff --git a/twilio/pom.xml b/twilio/pom.xml index 8683192b5c..610cc04b60 100644 --- a/twilio/pom.xml +++ b/twilio/pom.xml @@ -2,11 +2,15 @@ 4.0.0 - - com.baeldung twilio 1.0-SNAPSHOT + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + com.twilio.sdk @@ -15,9 +19,4 @@ - - 1.8 - 1.8 - - diff --git a/vaadin-spring/README.md b/vaadin-spring/README.md new file mode 100644 index 0000000000..347c92d7b5 --- /dev/null +++ b/vaadin-spring/README.md @@ -0,0 +1,3 @@ +### Relevant articles + +- [Sample Application with Spring Boot and Vaadin](https://www.baeldung.com/spring-boot-vaadin) diff --git a/vaadin-spring/src/main/resources/logback.xml b/vaadin-spring/src/main/resources/logback.xml deleted file mode 100644 index 7d900d8ea8..0000000000 --- a/vaadin-spring/src/main/resources/logback.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - - \ No newline at end of file diff --git a/vaadin/README.md b/vaadin/README.md index 0dfeea8996..43d2cc8961 100644 --- a/vaadin/README.md +++ b/vaadin/README.md @@ -1,3 +1,4 @@ ## Relevant articles: - [Introduction to Vaadin](http://www.baeldung.com/vaadin) +- [Sample Application with Spring Boot and Vaadin](https://www.baeldung.com/spring-boot-vaadin) diff --git a/vaadin/pom.xml b/vaadin/pom.xml index 07f0ccbd1c..c34ffa3636 100644 --- a/vaadin/pom.xml +++ b/vaadin/pom.xml @@ -4,25 +4,34 @@ 4.0.0 org.test - vaadin-app + vaadin war 1.0-SNAPSHOT - vaadin-app - - 3 - - + Vaadin + + parent-boot-2 com.baeldung - parent-modules - 1.0.0-SNAPSHOT + 0.0.1-SNAPSHOT + ../parent-boot-2 + + + + com.vaadin + vaadin-bom + ${vaadin.version} + pom + import + + + + javax.servlet javax.servlet-api - ${servlet.version} provided @@ -41,6 +50,25 @@ com.vaadin vaadin-themes + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.vaadin + vaadin-spring-boot-starter + ${vaadin-spring-boot-starter.version} + + + com.h2database + h2 + + + org.springframework.boot + spring-boot-starter-test + test + @@ -97,21 +125,13 @@ 2 + + org.springframework.boot + spring-boot-maven-plugin + - - - - com.vaadin - vaadin-bom - ${vaadin.version} - pom - import - - - - vaadin-addons @@ -166,6 +186,7 @@ 3.0.1 7.7.10 8.0.6 + 10.0.1 9.3.9.v20160517 UTF-8 1.8 diff --git a/vaadin-spring/src/main/java/com/baeldung/Application.java b/vaadin/src/main/java/com/baeldung/Application.java similarity index 100% rename from vaadin-spring/src/main/java/com/baeldung/Application.java rename to vaadin/src/main/java/com/baeldung/Application.java diff --git a/vaadin-spring/src/main/java/com/baeldung/Employee.java b/vaadin/src/main/java/com/baeldung/Employee.java similarity index 100% rename from vaadin-spring/src/main/java/com/baeldung/Employee.java rename to vaadin/src/main/java/com/baeldung/Employee.java diff --git a/vaadin-spring/src/main/java/com/baeldung/EmployeeEditor.java b/vaadin/src/main/java/com/baeldung/EmployeeEditor.java similarity index 100% rename from vaadin-spring/src/main/java/com/baeldung/EmployeeEditor.java rename to vaadin/src/main/java/com/baeldung/EmployeeEditor.java diff --git a/vaadin-spring/src/main/java/com/baeldung/EmployeeRepository.java b/vaadin/src/main/java/com/baeldung/EmployeeRepository.java similarity index 100% rename from vaadin-spring/src/main/java/com/baeldung/EmployeeRepository.java rename to vaadin/src/main/java/com/baeldung/EmployeeRepository.java diff --git a/vaadin-spring/src/main/java/com/baeldung/MainView.java b/vaadin/src/main/java/com/baeldung/MainView.java similarity index 100% rename from vaadin-spring/src/main/java/com/baeldung/MainView.java rename to vaadin/src/main/java/com/baeldung/MainView.java diff --git a/xml/pom.xml b/xml/pom.xml index dba0308337..5490f3a89f 100644 --- a/xml/pom.xml +++ b/xml/pom.xml @@ -1,8 +1,6 @@ 4.0.0 - - com.baeldung xml 0.1-SNAPSHOT xml @@ -168,23 +166,7 @@ template-binding.xml - true - - - - process-classes - - bind - - - - - - org.jibx - maven-jibx-plugin - 1.3.1 - - src/main/resources + src/main/resources *-binding.xml @@ -192,6 +174,14 @@ + process-classes + process-classes + + bind + + + + process-test-classes process-test-classes test-bind