Merge branch 'master' into master
This commit is contained in:
commit
b403f5d776
|
@ -3,10 +3,11 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>JGitSnippets</artifactId>
|
||||
<artifactId>JGit</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://maven.apache.org</url>
|
||||
<name>JGit</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -1,13 +1,9 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.mabsisa</groupId>
|
||||
<artifactId>Twitter4J</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>Twitter4J</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
@ -23,27 +19,6 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>${project.artifactId}</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/ApplicationTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<twitter4j-stream.version>4.0.6</twitter4j-stream.version>
|
||||
</properties>
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>activejdbc</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
@ -79,55 +78,11 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<reportFormat>brief</reportFormat>
|
||||
<trimStackTrace>true</trimStackTrace>
|
||||
<useFile>false</useFile>
|
||||
<includes>
|
||||
<include>**/*Spec*.java</include>
|
||||
<include>**/*Test*.java</include>
|
||||
</includes>
|
||||
<excludes>
|
||||
<exclude>**/helpers/*</exclude>
|
||||
<exclude>**/*$*</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>snapshots1</id>
|
||||
<name>JavaLite Snapshots1</name>
|
||||
<url>http://repo.javalite.io/</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
<updatePolicy>always</updatePolicy>
|
||||
<checksumPolicy>warn</checksumPolicy>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>snapshots2</id>
|
||||
<name>JavaLite Snapshots2</name>
|
||||
<url>http://repo.javalite.io/</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
<updatePolicy>always</updatePolicy>
|
||||
<checksumPolicy>warn</checksumPolicy>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
<properties>
|
||||
<activejdbc.version>1.4.13</activejdbc.version>
|
||||
<activejdbc.version>2.0</activejdbc.version>
|
||||
<environments>development.test,development</environments>
|
||||
<mysql.connector.version>5.1.34</mysql.connector.version>
|
||||
</properties>
|
||||
|
|
|
@ -16,4 +16,8 @@ public class Employee extends Model {
|
|||
set("created_by",createdBy);
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return getString("last_name");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,4 +15,8 @@ public class Role extends Model {
|
|||
set("role_name",role);
|
||||
set("created_by",createdBy);
|
||||
}
|
||||
|
||||
public String getRoleName() {
|
||||
return getString("role_name");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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() {
|
|
@ -0,0 +1 @@
|
|||
/bin/
|
|
@ -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;
|
||||
};
|
||||
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
|
@ -4,8 +4,9 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>apache-avro-tutorial</artifactId>
|
||||
<artifactId>apache-avro</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>Apache Avro</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
</properties>
|
||||
|
||||
<build>
|
||||
<finalName>${artifactId}</finalName>
|
||||
<finalName>${project.artifactId}</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>net.wasdev.wlp.maven.plugins</groupId>
|
||||
|
|
|
@ -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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>apache-shiro</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
|
@ -36,13 +35,11 @@
|
|||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>jcl-over-slf4j</artifactId>
|
||||
<version>${slf4j-version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<version>${slf4j-version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -53,26 +50,9 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<apache-shiro-core-version>1.4.0</apache-shiro-core-version>
|
||||
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
|
||||
<java.version>1.8</java.version>
|
||||
<log4j-version>1.2.17</log4j-version>
|
||||
<slf4j-version>1.7.25</slf4j-version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -42,14 +42,6 @@
|
|||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<argLine>-javaagent:"C:\asm-1.0.jar"</argLine>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
|
|
@ -95,7 +95,6 @@
|
|||
<gson.version>2.8.2</gson.version>
|
||||
<aws-java-sdk.version>1.11.241</aws-java-sdk.version>
|
||||
<maven-shade-plugin.version>3.0.0</maven-shade-plugin.version>
|
||||
<maven-dependency-plugin.version>2.10</maven-dependency-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1 +1,2 @@
|
|||
/target/
|
||||
.idea/
|
21
aws/pom.xml
21
aws/pom.xml
|
@ -100,26 +100,6 @@
|
|||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<version>${maven-dependency-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>test-compile</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<includeScope>test</includeScope>
|
||||
<includeTypes>so,dll,dylib</includeTypes>
|
||||
<outputDirectory>${project.basedir}/native-libs</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
@ -144,7 +124,6 @@
|
|||
<commons-codec-version>1.10.L001</commons-codec-version>
|
||||
<jets3t-version>0.9.4.0006L</jets3t-version>
|
||||
<maven-shade-plugin.version>3.0.0</maven-shade-plugin.version>
|
||||
<maven-dependency-plugin.version>2.10</maven-dependency-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -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("<ACCESS_KEY>",
|
||||
"<SECRET_KEY>"));
|
||||
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<DBInstance> 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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
db_hostname=<RDS EndPoint>
|
||||
db_username=username
|
||||
db_password=password
|
||||
db_database=mydb
|
|
@ -2,12 +2,10 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>cas-secured-app</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>cas-secured-app</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
<description>Demo project for CAS</description>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-1</artifactId>
|
||||
|
@ -60,10 +58,4 @@
|
|||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -2,11 +2,17 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd ">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>cas-server</artifactId>
|
||||
<packaging>war</packaging>
|
||||
<version>1.0</version>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-1</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-1</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apereo.cas</groupId>
|
||||
|
@ -39,7 +45,6 @@
|
|||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${springboot.version}</version>
|
||||
<configuration>
|
||||
<mainClass>${mainClassName}</mainClass>
|
||||
<addResources>true</addResources>
|
||||
|
@ -74,42 +79,10 @@
|
|||
</overlays>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<finalName>cas</finalName>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>sonatype-releases</id>
|
||||
<url>http://oss.sonatype.org/content/repositories/releases/</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
</releases>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>sonatype-snapshots</id>
|
||||
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
<releases>
|
||||
<enabled>false</enabled>
|
||||
</releases>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>shibboleth-releases</id>
|
||||
<url>https://build.shibboleth.net/nexus/content/repositories/releases</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<activation>
|
||||
|
@ -214,8 +187,7 @@
|
|||
</profiles>
|
||||
|
||||
<properties>
|
||||
<cas.version>5.3.0-SNAPSHOT</cas.version>
|
||||
<springboot.version>1.5.13.RELEASE</springboot.version>
|
||||
<cas.version>5.3.3</cas.version>
|
||||
<!-- app.server could be -jetty, -undertow, -tomcat, or blank if you plan to provide appserver -->
|
||||
<app.server>-tomcat</app.server>
|
||||
|
||||
|
@ -223,12 +195,10 @@
|
|||
<isExecutable>false</isExecutable>
|
||||
<manifestFileToUse>${project.build.directory}/war/work/org.apereo.cas/cas-server-webapp${app.server}/META-INF/MANIFEST.MF</manifestFileToUse>
|
||||
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<wrapper-maven-plugin.version>0.0.4</wrapper-maven-plugin.version>
|
||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
||||
<maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
|
||||
|
||||
<echo-maven-plugin.version>0.3.0</echo-maven-plugin.version>
|
||||
<pgpverify-maven-plugin.version>1.1.0</pgpverify-maven-plugin.version>
|
||||
</properties>
|
||||
|
|
|
@ -41,27 +41,6 @@
|
|||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<!-- This plugin will set properties values using dependency information -->
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<!--
|
||||
Goal that sets a property pointing to the artifact file for each project dependency.
|
||||
For each dependency (direct and transitive) a project property will be set which
|
||||
follows the:
|
||||
|
||||
groupId:artifactId:type:[classifier]
|
||||
|
||||
form and contains the path to the resolved artifact. -->
|
||||
<goal>properties</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -116,23 +116,6 @@
|
|||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/libs</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
|
@ -0,0 +1,16 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||
<pattern>
|
||||
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} -- %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root>
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
|
||||
</configuration>
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
module com.baeldung.logging.app {
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
module com.baeldung.logging {
|
||||
provides java.lang.System.LoggerFinder
|
||||
with com.baeldung.logging.CustomLoggerFinder;
|
||||
exports com.baeldung.logging;
|
||||
}
|
|
@ -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);
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 <K, V> Stream<K> keys(Map<K, V> map, V value) {
|
||||
return map.entrySet()
|
||||
.stream()
|
||||
.filter(entry -> value.equals(entry.getValue()))
|
||||
.map(Map.Entry::getKey);
|
||||
}
|
||||
|
||||
public static <K, V> Set<K> getKeys(Map<K, V> map, V value) {
|
||||
Set<K> keys = new HashSet<>();
|
||||
for (Entry<K, V> entry : map.entrySet()) {
|
||||
if (entry.getValue().equals(value)) {
|
||||
keys.add(entry.getKey());
|
||||
}
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
public static <K, V> K getKey(Map<K, V> map, V value) {
|
||||
for (Entry<K, V> entry : map.entrySet()) {
|
||||
if (entry.getValue().equals(value)) {
|
||||
return entry.getKey();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Employee> employeeList = new ArrayList<>();
|
||||
//LinkedList<Employee> 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();
|
||||
}
|
||||
}
|
|
@ -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<Employee> 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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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<Long, Employee> employeeMap = new HashMap<>();
|
||||
//LinkedHashMap<Long, Employee> employeeMap = new LinkedHashMap<>();
|
||||
//IdentityHashMap<Long, Employee> employeeMap = new IdentityHashMap<>();
|
||||
//WeakHashMap<Long, Employee> employeeMap = new WeakHashMap<>();
|
||||
//ConcurrentHashMap<Long, Employee> employeeMap = new ConcurrentHashMap<>();
|
||||
//ConcurrentSkipListMap<Long, Employee> 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();
|
||||
}
|
||||
}
|
|
@ -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<Employee> employeeSet = new HashSet<>();
|
||||
LinkedHashSet<Employee> employeeSet = new LinkedHashSet<>();
|
||||
//ConcurrentSkipListSet<Employee> 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();
|
||||
}
|
||||
}
|
|
@ -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<Integer> collection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
|
||||
|
||||
collection.clear();
|
||||
|
||||
assertTrue(collection.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoArrayListsWithCommonElements_whenRemoveAll_thenFirstListMissElementsFromSecondList() {
|
||||
Collection<Integer> firstCollection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
|
||||
Collection<Integer> 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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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<String, String> 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<String, String> 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<String>(Arrays.asList(
|
||||
new String[] {"Cape Town", "Pretoria", "Bloemfontein"})),
|
||||
MapUtil.getKeys(capitalCountryMap, "South Africa"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingFunctionalWayForSingleKey_shouldReturnSingleKey() {
|
||||
Map<String, String> 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<String, String> 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<String>(Arrays.asList(
|
||||
new String[] {"Cape Town", "Pretoria", "Bloemfontein"})),
|
||||
MapUtil.keys(capitalCountryMap, "South Africa").collect(Collectors.toSet()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingBidiMap_shouldReturnKey() {
|
||||
BidiMap<String, String> capitalCountryMap = new DualHashBidiMap<String, String>();
|
||||
capitalCountryMap.put("Berlin", "Germany");
|
||||
capitalCountryMap.put("Cape Town", "South Africa");
|
||||
assertEquals("Berlin", capitalCountryMap.getKey("Germany"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingBidiMapAddDuplicateValue_shouldRemoveOldEntry() {
|
||||
BidiMap<String, String> capitalCountryMap = new DualHashBidiMap<String, String>();
|
||||
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<String, String> 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<String, String> capitalCountryMap = HashBiMap.create();
|
||||
capitalCountryMap.put("Berlin", "Germany");
|
||||
capitalCountryMap.put("Cape Town", "South Africa");
|
||||
capitalCountryMap.put("Pretoria", "South Africa");
|
||||
assertEquals("Berlin", capitalCountryMap.inverse().get("Germany"));
|
||||
}
|
||||
|
||||
}
|
|
@ -57,26 +57,6 @@
|
|||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/libs</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
|
|
|
@ -57,26 +57,6 @@
|
|||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/libs</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>core-java-io</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
@ -166,36 +165,6 @@
|
|||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
<exclude>**/*IntTest.java</exclude>
|
||||
<exclude>**/*LongRunningUnitTest.java</exclude>
|
||||
<exclude>**/*ManualTest.java</exclude>
|
||||
</excludes>
|
||||
<testFailureIgnore>true</testFailureIgnore>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring-boot-maven-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<classifier>spring-boot</classifier>
|
||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
|
@ -229,32 +198,6 @@
|
|||
<id>integration</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>integration-test</phase>
|
||||
<goals>
|
||||
<goal>test</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*ManualTest.java</exclude>
|
||||
</excludes>
|
||||
<includes>
|
||||
<include>**/*IntegrationTest.java</include>
|
||||
<include>**/*IntTest.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<test.mime>json</test.mime>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
|
@ -310,14 +253,11 @@
|
|||
<avaitility.version>1.7.0</avaitility.version>
|
||||
|
||||
<!-- maven plugins -->
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
|
||||
<hsqldb.version>2.4.0</hsqldb.version>
|
||||
<esapi.version>2.1.0.1</esapi.version>
|
||||
<jmh-generator-annprocess.version>1.19</jmh-generator-annprocess.version>
|
||||
<async-http-client.version>2.4.5</async-http-client.version>
|
||||
<spring-boot-maven-plugin.version>2.0.4.RELEASE</spring-boot-maven-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -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);
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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 {
|
|
@ -6,3 +6,4 @@
|
|||
- [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)
|
||||
- [A Simple Guide to Connection Pooling in Java](https://www.baeldung.com/java-connection-pooling)
|
||||
|
|
|
@ -166,22 +166,6 @@
|
|||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/libs</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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 <a href="http://stackoverflow.com/a/2861125/3474">StackOverflow</a>
|
||||
*/
|
||||
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}.
|
||||
*
|
||||
* <p>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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Integer> {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public int compare(Integer o1, Integer o2) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
|
@ -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() {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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 {
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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)));
|
||||
}
|
||||
}
|
|
@ -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)));
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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)
|
|
@ -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
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
<?xml version="1.0" encoding="ISO-8859-1" ?>
|
||||
|
||||
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
|
||||
version="3.0">
|
||||
<!-- path to application.conf file, required -->
|
||||
<!-- note that this file is always loaded as an absolute path from the classpath -->
|
||||
<context-param>
|
||||
<param-name>io.ktor.ktor.config</param-name>
|
||||
<param-value>application.conf</param-value>
|
||||
</context-param>
|
||||
|
||||
<servlet>
|
||||
<display-name>KtorServlet</display-name>
|
||||
<servlet-name>KtorServlet</servlet-name>
|
||||
<servlet-class>io.ktor.server.servlet.ServletApplicationEngine</servlet-class>
|
||||
|
||||
<!-- required! -->
|
||||
<async-supported>true</async-supported>
|
||||
|
||||
<!-- 100mb max file upload, optional -->
|
||||
<multipart-config>
|
||||
<max-file-size>304857600</max-file-size>
|
||||
<max-request-size>304857600</max-request-size>
|
||||
<file-size-threshold>0</file-size-threshold>
|
||||
</multipart-config>
|
||||
</servlet>
|
||||
|
||||
<servlet-mapping>
|
||||
<servlet-name>KtorServlet</servlet-name>
|
||||
<url-pattern>/</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
</web-app>
|
|
@ -12,33 +12,7 @@
|
|||
<relativePath>../parent-kotlin</relativePath>
|
||||
</parent>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>exposed</id>
|
||||
<name>exposed</name>
|
||||
<url>https://dl.bintray.com/kotlin/exposed</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.spek</groupId>
|
||||
<artifactId>spek-api</artifactId>
|
||||
<version>1.1.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.spek</groupId>
|
||||
<artifactId>spek-subject-extension</artifactId>
|
||||
<version>1.1.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.spek</groupId>
|
||||
<artifactId>spek-junit-platform-engine</artifactId>
|
||||
<version>1.1.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-math3</artifactId>
|
||||
|
@ -50,56 +24,52 @@
|
|||
<version>${junit.platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>khttp</groupId>
|
||||
<artifactId>khttp</artifactId>
|
||||
<version>${khttp.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.nhaarman</groupId>
|
||||
<artifactId>mockito-kotlin</artifactId>
|
||||
<version>${mockito-kotlin.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.salomonbrys.kodein</groupId>
|
||||
<artifactId>kodein</artifactId>
|
||||
<version>${kodein.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.beust</groupId>
|
||||
<artifactId>klaxon</artifactId>
|
||||
<version>${klaxon.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.exposed</groupId>
|
||||
<artifactId>exposed</artifactId>
|
||||
<version>${exposed.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2database.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.kittinunf.fuel</groupId>
|
||||
<artifactId>fuel</artifactId>
|
||||
<version>${fuel.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.kittinunf.fuel</groupId>
|
||||
<artifactId>fuel-gson</artifactId>
|
||||
<version>${fuel.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.kittinunf.fuel</groupId>
|
||||
<artifactId>fuel-rxjava</artifactId>
|
||||
<version>${fuel.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.kittinunf.fuel</groupId>
|
||||
<artifactId>fuel-coroutines</artifactId>
|
||||
<version>${fuel.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>nl.komponents.kovenant</groupId>
|
||||
<artifactId>kovenant</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
|
||||
<kodein.version>4.1.0</kodein.version>
|
||||
<klaxon.version>3.0.4</klaxon.version>
|
||||
<khttp.version>0.1.0</khttp.version>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<junit.platform.version>1.1.1</junit.platform.version>
|
||||
<junit.vintage.version>5.2.0</junit.vintage.version>
|
||||
<assertj.version>3.10.0</assertj.version>
|
||||
<h2database.version>1.4.197</h2database.version>
|
||||
<exposed.version>0.10.4</exposed.version>
|
||||
<fuel.version>1.15.0</fuel.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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<Array<Post>> {
|
||||
override fun deserialize(content: String): Array<Post> = Gson().fromJson(content, Array<Post>::class.java)
|
||||
}
|
||||
}
|
|
@ -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<Pair<String, Any?>>?
|
||||
get() {
|
||||
return when(this) {
|
||||
is PostRoutingAPI.posts -> listOf("id" to this.id)
|
||||
is PostRoutingAPI.comments -> listOf("postId" to this.postId)
|
||||
}
|
||||
}
|
||||
|
||||
override val headers: Map<String, String>?
|
||||
get() {
|
||||
return 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<String>) {
|
||||
|
||||
val toDoList = ArrayList<ToDo>();
|
||||
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>();
|
||||
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)
|
||||
}
|
|
@ -93,6 +93,7 @@ internal class BuilderPatternUnitTest {
|
|||
Assertions.assertNull(foodOrder.fish)
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun whenBuildingFoodOrderApplySettingValues_thenFieldsNotNull() {
|
||||
|
||||
|
|
|
@ -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<Post> { _,_, 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()
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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<Long, Exception>()
|
||||
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<Long, Exception>()
|
||||
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<Long, Exception>()
|
||||
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)
|
||||
}
|
||||
}
|
|
@ -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 <T> timedTask(millis: Long, body: () -> T) : Promise<T?, List<Exception>> {
|
||||
val timeoutTask = task {
|
||||
Thread.sleep(millis)
|
||||
null
|
||||
}
|
||||
val activeTask = task(body = body)
|
||||
return any(activeTask, timeoutTask)
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
|
@ -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
|
||||
)
|
||||
}
|
|
@ -3,11 +3,11 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>couchbase-sdk</artifactId>
|
||||
<artifactId>couchbase</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>couchbase</name>
|
||||
<description>Couchbase SDK Tutorials</description>
|
||||
<description>Couchbase Tutorials</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -36,22 +36,6 @@
|
|||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/libs</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue