Merge remote-tracking branch 'eugenp/master'
This commit is contained in:
commit
c87617470e
|
@ -26,14 +26,6 @@
|
|||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>animal-sniffer-maven-plugin</artifactId>
|
||||
|
|
|
@ -19,32 +19,11 @@
|
|||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.auto.service</groupId>
|
||||
<artifactId>auto-service</artifactId>
|
||||
<version>${auto-service.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
<plugins>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -25,15 +25,6 @@
|
|||
<mainClass>com.baeldung.cxf.introduction.Server</mainClass>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
|
|
@ -27,15 +27,6 @@
|
|||
<mainClass>com.baeldung.cxf.jaxrs.implementation.RestfulServer</mainClass>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
|
|
@ -52,15 +52,6 @@
|
|||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
@ -102,24 +93,6 @@
|
|||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>integration-test</phase>
|
||||
<goals>
|
||||
<goal>test</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>none</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
|
|
|
@ -16,6 +16,7 @@ import opennlp.tools.util.ObjectStream;
|
|||
import opennlp.tools.util.PlainTextByLineStream;
|
||||
import opennlp.tools.util.TrainingParameters;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.tuple;
|
||||
import org.junit.Test;
|
||||
|
||||
public class LanguageDetectorAndTrainingDataTest {
|
||||
|
@ -35,7 +36,9 @@ public class LanguageDetectorAndTrainingDataTest {
|
|||
|
||||
LanguageDetector ld = new LanguageDetectorME(model);
|
||||
Language[] languages = ld.predictLanguages("estava em uma marcenaria na Rua Bruno");
|
||||
assertThat(Arrays.asList(languages).toString()).contains("pob (0.9999999950605625)", "ita (4.939427661577956E-9)", "spa (9.665954064665144E-15)",
|
||||
"fra (8.250349924885834E-25)");
|
||||
|
||||
assertThat(Arrays.asList(languages)).extracting("lang", "confidence").contains(tuple("pob", 0.9999999950605625),
|
||||
tuple("ita", 4.939427661577956E-9), tuple("spa", 9.665954064665144E-15),
|
||||
tuple("fra", 8.250349924885834E-25));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,17 +32,4 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
|
|
|
@ -29,18 +29,4 @@
|
|||
<version>1.2.17</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
|
|
15
aws/pom.xml
15
aws/pom.xml
|
@ -24,6 +24,8 @@
|
|||
<assertj-core.version>3.8.0</assertj-core.version>
|
||||
<dynamodblocal.version>1.11.86</dynamodblocal.version>
|
||||
<dynamodblocal.repository.url>https://s3-us-west-2.amazonaws.com/dynamodb-local/release</dynamodblocal.repository.url>
|
||||
<commons-codec-version>1.10.L001</commons-codec-version>
|
||||
<jets3t-version>0.9.4.0006L</jets3t-version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
@ -97,6 +99,19 @@
|
|||
<version>${dynamodblocal.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- JetS3t -->
|
||||
<dependency>
|
||||
<groupId>org.lucee</groupId>
|
||||
<artifactId>jets3t</artifactId>
|
||||
<version>${jets3t-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lucee</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>${commons-codec-version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -0,0 +1,351 @@
|
|||
package com.baeldung.jets3t;
|
||||
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.jets3t.service.S3Service;
|
||||
import org.jets3t.service.ServiceException;
|
||||
import org.jets3t.service.impl.rest.httpclient.RestS3Service;
|
||||
import org.jets3t.service.model.S3Bucket;
|
||||
import org.jets3t.service.model.S3Object;
|
||||
import org.jets3t.service.model.StorageObject;
|
||||
import org.jets3t.service.security.AWSCredentials;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class JetS3tLiveTest {
|
||||
|
||||
private Log log = LogFactory.getLog(JetS3tLiveTest.class);
|
||||
|
||||
private static final String BucketName = "baeldung-barfoo";
|
||||
private static final String TestString = "test string";
|
||||
private static final String TestStringName = "string object";
|
||||
private static final String TgtBucket = "baeldung-tgtbucket";
|
||||
|
||||
private static S3Service s3Service;
|
||||
|
||||
@BeforeClass
|
||||
public static void connectS3() throws Exception {
|
||||
|
||||
// Replace with your keys
|
||||
String awsAccessKey = "your access key";
|
||||
String awsSecretKey = "your secret key";
|
||||
|
||||
// Create credentials
|
||||
AWSCredentials awsCredentials = new AWSCredentials(awsAccessKey, awsSecretKey);
|
||||
|
||||
// Create service
|
||||
s3Service = new RestS3Service(awsCredentials);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCreate_AndDeleteBucket_CountGoesUpThenDown() throws Exception {
|
||||
|
||||
// List buckets, get a count
|
||||
S3Bucket[] myBuckets = s3Service.listAllBuckets();
|
||||
int count = Arrays.stream(myBuckets).map(S3Bucket::getName).collect(Collectors.toList()).size();
|
||||
|
||||
// Create a bucket
|
||||
S3Bucket bucket = createBucket();
|
||||
assertNotNull(bucket);
|
||||
|
||||
// List again
|
||||
myBuckets = s3Service.listAllBuckets();
|
||||
int newCount = Arrays.stream(myBuckets).map(S3Bucket::getName).collect(Collectors.toList()).size();
|
||||
|
||||
// We should have one more
|
||||
assertEquals((count + 1), newCount);
|
||||
|
||||
// Delete so next test doesn't fail
|
||||
deleteBucket();
|
||||
|
||||
// Check the count again, just for laughs
|
||||
myBuckets = s3Service.listAllBuckets();
|
||||
newCount = Arrays.stream(myBuckets).map(S3Bucket::getName).collect(Collectors.toList()).size();
|
||||
assertEquals(count, newCount);
|
||||
|
||||
}
|
||||
|
||||
private S3Bucket createBucket() throws Exception {
|
||||
S3Bucket bucket = s3Service.createBucket(BucketName);
|
||||
log.info(bucket);
|
||||
return bucket;
|
||||
}
|
||||
|
||||
|
||||
private void deleteBucket() throws ServiceException {
|
||||
s3Service.deleteBucket(BucketName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_Uploaded_StringInfoIsAvailable() throws Exception {
|
||||
|
||||
// Create a bucket
|
||||
S3Bucket bucket = createBucket();
|
||||
assertNotNull(bucket);
|
||||
|
||||
// Upload a string
|
||||
uploadStringData();
|
||||
|
||||
// Get the details
|
||||
StorageObject objectDetailsOnly = s3Service.getObjectDetails(BucketName, TestStringName);
|
||||
log.info("Content type: " + objectDetailsOnly.getContentType() + " length: " + objectDetailsOnly.getContentLength());
|
||||
|
||||
// Delete it
|
||||
deleteObject(TestStringName);
|
||||
|
||||
// For next test
|
||||
deleteBucket();
|
||||
}
|
||||
|
||||
private void uploadStringData() throws Exception {
|
||||
S3Object stringObject = new S3Object(TestStringName, TestString);
|
||||
s3Service.putObject(BucketName, stringObject);
|
||||
log.info("Content type:" + stringObject.getContentType());
|
||||
}
|
||||
|
||||
private void deleteObject(String objectName) throws ServiceException {
|
||||
s3Service.deleteObject(BucketName, objectName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringUploaded_StringIsDownloaded() throws Exception {
|
||||
|
||||
// Get a bucket
|
||||
S3Bucket bucket = createBucket();
|
||||
assertNotNull(bucket);
|
||||
|
||||
uploadStringData();
|
||||
|
||||
// Download
|
||||
S3Object stringObject = s3Service.getObject(BucketName, TestStringName);
|
||||
|
||||
// Process stream into a string
|
||||
String downloadedString = new BufferedReader(new InputStreamReader(stringObject.getDataInputStream())).lines().collect(Collectors.joining("\n"));
|
||||
|
||||
// Verify
|
||||
assertTrue(TestString.equals(downloadedString));
|
||||
|
||||
|
||||
// Clean up for next test
|
||||
deleteObject(TestStringName);
|
||||
deleteBucket();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBinaryFileUploaded_FileIsDownloaded() throws Exception {
|
||||
|
||||
// get a bucket
|
||||
S3Bucket bucket = createBucket();
|
||||
assertNotNull(bucket);
|
||||
|
||||
// Put a binary file
|
||||
S3Object fileObject = new S3Object(new File("src/test/resources/test.jpg"));
|
||||
s3Service.putObject(BucketName, fileObject);
|
||||
|
||||
// Print info about type and name
|
||||
log.info("Content type:" + fileObject.getContentType());
|
||||
log.info("File object name is " + fileObject.getName());
|
||||
|
||||
// Download
|
||||
S3Object newFileObject = s3Service.getObject(BucketName, "test.jpg");
|
||||
|
||||
// Save to a different name
|
||||
File newFile = new File("src/test/resources/newtest.jpg");
|
||||
Files.copy(newFileObject.getDataInputStream(), newFile.toPath(), REPLACE_EXISTING);
|
||||
|
||||
|
||||
// Get hashes and compare
|
||||
String origMD5 = getFileMD5("src/test/resources/test.jpg");
|
||||
String newMD5 = getFileMD5("src/test/resources/newtest.jpg");
|
||||
assertTrue(origMD5.equals(newMD5));
|
||||
|
||||
// Clean up
|
||||
deleteObject("test.jpg");
|
||||
deleteBucket();
|
||||
}
|
||||
|
||||
// Get MD5 hash for a file
|
||||
private String getFileMD5(String filename) throws IOException {
|
||||
try (FileInputStream fis = new FileInputStream(new File(filename))) {
|
||||
return DigestUtils.md5Hex(fis);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void givenStreamDataUploaded_StreamDataIsDownloaded() throws Exception {
|
||||
|
||||
// get a bucket
|
||||
S3Bucket bucket = createBucket();
|
||||
assertNotNull(bucket);
|
||||
|
||||
ArrayList<Integer> numbers = new ArrayList<>();
|
||||
numbers.add(2);
|
||||
numbers.add(3);
|
||||
numbers.add(5);
|
||||
numbers.add(7);
|
||||
|
||||
// Serialize ArrayList
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(bytes);
|
||||
objectOutputStream.writeObject(numbers);
|
||||
|
||||
// Wrap bytes
|
||||
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes.toByteArray());
|
||||
|
||||
// Create and populate object
|
||||
S3Object streamObject = new S3Object("stream");
|
||||
streamObject.setDataInputStream(byteArrayInputStream);
|
||||
streamObject.setContentLength(byteArrayInputStream.available());
|
||||
streamObject.setContentType("binary/octet-stream");
|
||||
|
||||
// Put it
|
||||
s3Service.putObject(BucketName, streamObject);
|
||||
|
||||
// Get it
|
||||
S3Object newStreamObject = s3Service.getObject(BucketName, "stream");
|
||||
|
||||
// Convert back to ArrayList
|
||||
ObjectInputStream objectInputStream = new ObjectInputStream(newStreamObject.getDataInputStream());
|
||||
ArrayList<Integer> newNumbers = (ArrayList<Integer>)objectInputStream.readObject();
|
||||
|
||||
assertEquals(2, (int)newNumbers.get(0));
|
||||
assertEquals(3, (int)newNumbers.get(1));
|
||||
assertEquals(5, (int)newNumbers.get(2));
|
||||
assertEquals(7, (int)newNumbers.get(3));
|
||||
|
||||
// Clean up
|
||||
deleteObject("stream");
|
||||
deleteBucket();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFileCopied_CopyIsSame() throws Exception {
|
||||
|
||||
// get a bucket
|
||||
S3Bucket bucket = createBucket();
|
||||
assertNotNull(bucket);
|
||||
|
||||
// Put a binary file
|
||||
S3Object fileObject = new S3Object(new File("src/test/resources/test.jpg"));
|
||||
s3Service.putObject(BucketName, fileObject);
|
||||
|
||||
|
||||
// Copy it
|
||||
S3Object targetObject = new S3Object("testcopy.jpg");
|
||||
s3Service.copyObject(BucketName, "test.jpg", BucketName, targetObject, false);
|
||||
|
||||
|
||||
// Download
|
||||
S3Object newFileObject = s3Service.getObject(BucketName, "testcopy.jpg");
|
||||
|
||||
// Save to a different name
|
||||
File newFile = new File("src/test/resources/testcopy.jpg");
|
||||
Files.copy(newFileObject.getDataInputStream(), newFile.toPath(), REPLACE_EXISTING);
|
||||
|
||||
|
||||
// Get hashes and compare
|
||||
String origMD5 = getFileMD5("src/test/resources/test.jpg");
|
||||
String newMD5 = getFileMD5("src/test/resources/testcopy.jpg");
|
||||
assertTrue(origMD5.equals(newMD5));
|
||||
|
||||
// Clean up
|
||||
deleteObject("test.jpg");
|
||||
deleteObject("testcopy.jpg");
|
||||
deleteBucket();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenFileRenamed_NewNameIsSame() throws Exception {
|
||||
|
||||
// get a bucket
|
||||
S3Bucket bucket = createBucket();
|
||||
assertNotNull(bucket);
|
||||
|
||||
// Put a binary file
|
||||
S3Object fileObject = new S3Object(new File("src/test/resources/test.jpg"));
|
||||
s3Service.putObject(BucketName, fileObject);
|
||||
|
||||
|
||||
// Copy it
|
||||
s3Service.renameObject(BucketName, "test.jpg", new S3Object("spidey.jpg"));
|
||||
|
||||
|
||||
// Download
|
||||
S3Object newFileObject = s3Service.getObject(BucketName, "spidey.jpg");
|
||||
|
||||
// Save to a different name
|
||||
File newFile = new File("src/test/resources/spidey.jpg");
|
||||
Files.copy(newFileObject.getDataInputStream(), newFile.toPath(), REPLACE_EXISTING);
|
||||
|
||||
|
||||
// Get hashes and compare
|
||||
String origMD5 = getFileMD5("src/test/resources/test.jpg");
|
||||
String newMD5 = getFileMD5("src/test/resources/spidey.jpg");
|
||||
assertTrue(origMD5.equals(newMD5));
|
||||
|
||||
// Clean up
|
||||
deleteObject("test.jpg");
|
||||
deleteObject("spidey.jpg");
|
||||
deleteBucket();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFileMoved_NewInstanceIsSame() throws Exception {
|
||||
|
||||
// get a bucket
|
||||
S3Bucket bucket = createBucket();
|
||||
assertNotNull(bucket);
|
||||
|
||||
// create another bucket
|
||||
S3Bucket tgtBucket = s3Service.createBucket(TgtBucket);
|
||||
|
||||
|
||||
// Put a binary file
|
||||
S3Object fileObject = new S3Object(new File("src/test/resources/test.jpg"));
|
||||
s3Service.putObject(BucketName, fileObject);
|
||||
|
||||
|
||||
// Copy it
|
||||
s3Service.moveObject(BucketName, "test.jpg", TgtBucket,
|
||||
new S3Object("spidey.jpg"), false);
|
||||
|
||||
|
||||
// Download
|
||||
S3Object newFileObject = s3Service.getObject(TgtBucket, "spidey.jpg");
|
||||
|
||||
// Save to a different name
|
||||
File newFile = new File("src/test/resources/spidey.jpg");
|
||||
Files.copy(newFileObject.getDataInputStream(), newFile.toPath(), REPLACE_EXISTING);
|
||||
|
||||
|
||||
// Get hashes and compare
|
||||
String origMD5 = getFileMD5("src/test/resources/test.jpg");
|
||||
String newMD5 = getFileMD5("src/test/resources/spidey.jpg");
|
||||
assertTrue(origMD5.equals(newMD5));
|
||||
|
||||
// Clean up
|
||||
deleteBucket();
|
||||
|
||||
s3Service.deleteObject(TgtBucket, "spidey.jpg");
|
||||
s3Service.deleteBucket(TgtBucket);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<parent>
|
||||
<artifactId>bootique-parent</artifactId>
|
||||
<groupId>io.bootique.parent</groupId>
|
||||
<version>0.12</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.bootique</groupId>
|
||||
<artifactId>bootique</artifactId>
|
||||
<name>bootique</name>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<url>http://maven.apache.org</url>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.bootique</groupId>
|
||||
<artifactId>bootique-test</artifactId>
|
||||
<version>0.23</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>hamcrest-core</artifactId>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.bootique.bom</groupId>
|
||||
<artifactId>bootique-bom</artifactId>
|
||||
<version>0.23</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
<properties>
|
||||
<main.class>com.baeldung.bootique.App</main.class>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
@ -45,14 +45,6 @@
|
|||
<defaultGoal>spring-boot:run</defaultGoal>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
|
@ -71,7 +63,6 @@
|
|||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
|
||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||
<camel.version>2.19.1</camel.version>
|
||||
<spring-boot-starter.version>1.5.4.RELEASE</spring-boot-starter.version>
|
||||
</properties>
|
||||
|
|
|
@ -65,24 +65,6 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<forkCount>3</forkCount>
|
||||
<reuseForks>true</reuseForks>
|
||||
<excludes>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
<exclude>**/*LongRunningUnitTest.java</exclude>
|
||||
<exclude>**/*ManualTest.java</exclude>
|
||||
<exclude>**/JdbcTest.java</exclude>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</excludes>
|
||||
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
|
|
@ -78,10 +78,6 @@
|
|||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-failsafe-plugin</artifactId>
|
||||
<version>2.19.1</version>
|
||||
|
|
|
@ -75,13 +75,6 @@
|
|||
<target>1.9</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
|
|
|
@ -1,55 +1,36 @@
|
|||
package com.baeldung.concurrent.volatilekeyword;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class SharedObjectManualTest {
|
||||
|
||||
private SharedObject sharedObject;
|
||||
private int valueReadByThread2;
|
||||
private int valueReadByThread3;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
sharedObject = new SharedObject();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOneThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException {
|
||||
SharedObject sharedObject = new SharedObject();
|
||||
|
||||
Thread writer = new Thread(() -> sharedObject.increamentCount());
|
||||
writer.start();
|
||||
|
||||
Thread.sleep(100);
|
||||
|
||||
Thread readerOne = new Thread(() -> {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
valueReadByThread2 = sharedObject.getCount();
|
||||
int valueReadByThread2 = sharedObject.getCount();
|
||||
assertEquals(1, valueReadByThread2);
|
||||
});
|
||||
readerOne.start();
|
||||
|
||||
Thread readerTwo = new Thread(() -> {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
valueReadByThread3 = sharedObject.getCount();
|
||||
int valueReadByThread3 = sharedObject.getCount();
|
||||
assertEquals(1, valueReadByThread3);
|
||||
});
|
||||
readerTwo.start();
|
||||
|
||||
assertEquals(1, valueReadByThread2);
|
||||
assertEquals(1, valueReadByThread3);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTwoThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException {
|
||||
SharedObject sharedObject = new SharedObject();
|
||||
Thread writerOne = new Thread(() -> sharedObject.increamentCount());
|
||||
writerOne.start();
|
||||
Thread.sleep(100);
|
||||
|
@ -58,14 +39,17 @@ public class SharedObjectManualTest {
|
|||
writerTwo.start();
|
||||
Thread.sleep(100);
|
||||
|
||||
Thread readerOne = new Thread(() -> valueReadByThread2 = sharedObject.getCount());
|
||||
Thread readerOne = new Thread(() -> {
|
||||
int valueReadByThread2 = sharedObject.getCount();
|
||||
assertEquals(2, valueReadByThread2);
|
||||
});
|
||||
readerOne.start();
|
||||
|
||||
Thread readerTwo = new Thread(() -> valueReadByThread3 = sharedObject.getCount());
|
||||
Thread readerTwo = new Thread(() -> {
|
||||
int valueReadByThread3 = sharedObject.getCount();
|
||||
assertEquals(2, valueReadByThread3);
|
||||
});
|
||||
readerTwo.start();
|
||||
|
||||
assertEquals(2, valueReadByThread2);
|
||||
assertEquals(2, valueReadByThread3);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -232,18 +232,7 @@
|
|||
</resources>
|
||||
|
||||
<plugins>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
|
@ -256,7 +245,6 @@
|
|||
<testFailureIgnore>true</testFailureIgnore>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
|
|
|
@ -238,31 +238,6 @@
|
|||
</resources>
|
||||
|
||||
<plugins>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
<exclude>**/*LongRunningUnitTest.java</exclude>
|
||||
<exclude>**/*ManualTest.java</exclude>
|
||||
</excludes>
|
||||
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
*.ear
|
||||
|
||||
# Files generated by integration tests
|
||||
*.txt
|
||||
backup-pom.xml
|
||||
/bin/
|
||||
/temp
|
||||
|
|
Binary file not shown.
|
@ -231,10 +231,9 @@
|
|||
<version>1.5.8.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hsqldb</groupId>
|
||||
<artifactId>hsqldb</artifactId>
|
||||
<version>2.4.0</version>
|
||||
<scope>runtime</scope>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.4.197</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
@ -248,17 +247,6 @@
|
|||
</resources>
|
||||
|
||||
<plugins>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
|
|
|
@ -13,8 +13,8 @@ public class BatchProcessing {
|
|||
|
||||
public void getConnection(){
|
||||
try {
|
||||
Class.forName("org.hsqldb.jdbcDriver");
|
||||
connection = DriverManager.getConnection("jdbc:hsqldb:file:C:\\EMPLOYEEDB", "SA", "");
|
||||
Class.forName("org.h2.Driver");
|
||||
connection = DriverManager.getConnection("jdbc:h2:mem:db", "SA", "");
|
||||
connection.setAutoCommit(false);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace(System.out);
|
||||
|
|
|
@ -0,0 +1,92 @@
|
|||
package com.baeldung.keystore;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.KeyStore;
|
||||
import java.security.KeyStoreException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.UnrecoverableEntryException;
|
||||
import java.security.cert.Certificate;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.util.Enumeration;
|
||||
|
||||
/**
|
||||
* Created by adi on 3/7/18.
|
||||
*/
|
||||
public class JavaKeyStore {
|
||||
|
||||
private KeyStore keyStore;
|
||||
|
||||
private String keyStoreName;
|
||||
private String keyStoreType;
|
||||
private String keyStorePassword;
|
||||
|
||||
JavaKeyStore(String keyStoreType, String keyStorePassword, String keyStoreName) throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException {
|
||||
this.keyStoreName = keyStoreName;
|
||||
this.keyStoreType = keyStoreType;
|
||||
this.keyStorePassword = keyStorePassword;
|
||||
}
|
||||
|
||||
void createEmptyKeyStore() throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException {
|
||||
if(keyStoreType ==null || keyStoreType.isEmpty()){
|
||||
keyStoreType = KeyStore.getDefaultType();
|
||||
}
|
||||
keyStore = KeyStore.getInstance(keyStoreType);
|
||||
//load
|
||||
char[] pwdArray = keyStorePassword.toCharArray();
|
||||
keyStore.load(null, pwdArray);
|
||||
|
||||
// Save the keyStore
|
||||
FileOutputStream fos = new FileOutputStream(keyStoreName);
|
||||
keyStore.store(fos, pwdArray);
|
||||
fos.close();
|
||||
}
|
||||
|
||||
void loadKeyStore() throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException {
|
||||
char[] pwdArray = keyStorePassword.toCharArray();
|
||||
keyStore.load(new FileInputStream(keyStoreName), pwdArray);
|
||||
}
|
||||
|
||||
void setEntry(String alias, KeyStore.SecretKeyEntry secretKeyEntry, KeyStore.ProtectionParameter protectionParameter) throws KeyStoreException {
|
||||
keyStore.setEntry(alias, secretKeyEntry, protectionParameter);
|
||||
}
|
||||
|
||||
KeyStore.Entry getEntry(String alias) throws UnrecoverableEntryException, NoSuchAlgorithmException, KeyStoreException {
|
||||
KeyStore.ProtectionParameter protParam = new KeyStore.PasswordProtection(keyStorePassword.toCharArray());
|
||||
return keyStore.getEntry(alias, protParam);
|
||||
}
|
||||
|
||||
void setKeyEntry(String alias, PrivateKey privateKey, String keyPassword, Certificate[] certificateChain) throws KeyStoreException {
|
||||
keyStore.setKeyEntry(alias, privateKey, keyPassword.toCharArray(), certificateChain);
|
||||
}
|
||||
|
||||
void setCertificateEntry(String alias, Certificate certificate) throws KeyStoreException {
|
||||
keyStore.setCertificateEntry(alias, certificate);
|
||||
}
|
||||
|
||||
Certificate getCertificate(String alias) throws KeyStoreException {
|
||||
return keyStore.getCertificate(alias);
|
||||
}
|
||||
|
||||
void deleteEntry(String alias) throws KeyStoreException {
|
||||
keyStore.deleteEntry(alias);
|
||||
}
|
||||
|
||||
void deleteKeyStore() throws KeyStoreException, IOException {
|
||||
Enumeration<String> aliases = keyStore.aliases();
|
||||
while (aliases.hasMoreElements()) {
|
||||
String alias = aliases.nextElement();
|
||||
keyStore.deleteEntry(alias);
|
||||
}
|
||||
keyStore = null;
|
||||
Files.delete(Paths.get(keyStoreName));
|
||||
}
|
||||
|
||||
KeyStore getKeyStore() {
|
||||
return this.keyStore;
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
baeldung.com
|
|
@ -0,0 +1,205 @@
|
|||
package com.baeldung.keystore;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import sun.security.x509.AlgorithmId;
|
||||
import sun.security.x509.CertificateAlgorithmId;
|
||||
import sun.security.x509.CertificateSerialNumber;
|
||||
import sun.security.x509.CertificateValidity;
|
||||
import sun.security.x509.CertificateVersion;
|
||||
import sun.security.x509.CertificateX509Key;
|
||||
import sun.security.x509.X500Name;
|
||||
import sun.security.x509.X509CertImpl;
|
||||
import sun.security.x509.X509CertInfo;
|
||||
|
||||
import javax.crypto.KeyGenerator;
|
||||
import javax.crypto.SecretKey;
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.KeyStore;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.NoSuchProviderException;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.SignatureException;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Created by adi on 4/14/18.
|
||||
*/
|
||||
public class JavaKeyStoreTest {
|
||||
|
||||
private JavaKeyStore keyStore;
|
||||
|
||||
private static final String KEYSTORE_PWD = "abc123";
|
||||
private static final String KEYSTORE_NAME = "myKeyStore";
|
||||
private static final String KEY_STORE_TYPE = "JCEKS";
|
||||
|
||||
private static final String MY_SECRET_ENTRY = "mySecretEntry";
|
||||
private static final String DN_NAME = "CN=test, OU=test, O=test, L=test, ST=test, C=CY";
|
||||
private static final String SHA1WITHRSA = "SHA1withRSA";
|
||||
private static final String MY_PRIVATE_KEY = "myPrivateKey";
|
||||
private static final String MY_CERTIFICATE = "myCertificate";
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
//using java cryptography extension keyStore instead of Keystore.getDefaultType
|
||||
keyStore = new JavaKeyStore(KEY_STORE_TYPE, KEYSTORE_PWD, KEYSTORE_NAME);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
if (keyStore.getKeyStore() != null) {
|
||||
keyStore.deleteKeyStore();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNoKeyStore_whenCreateEmptyKeyStore_thenGetKeyStoreNotNull() throws Exception {
|
||||
keyStore.createEmptyKeyStore();
|
||||
KeyStore result = keyStore.getKeyStore();
|
||||
Assert.assertNotNull(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyKeystore_whenLoadKeyStore_thenKeyStoreLoadedAndSizeZero() throws Exception {
|
||||
keyStore.createEmptyKeyStore();
|
||||
keyStore.loadKeyStore();
|
||||
KeyStore result = keyStore.getKeyStore();
|
||||
Assert.assertNotNull(result);
|
||||
Assert.assertTrue(result.size() == 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLoadedKeyStore_whenSetEntry_thenSizeIsOneAndGetKeyNotNull() throws Exception {
|
||||
keyStore.createEmptyKeyStore();
|
||||
keyStore.loadKeyStore();
|
||||
|
||||
KeyGenerator keygen = KeyGenerator.getInstance("HmacSHA256");
|
||||
SecretKey secretKey = keygen.generateKey();
|
||||
//ideally, password should be different for every key
|
||||
KeyStore.ProtectionParameter protParam = new KeyStore.PasswordProtection(KEYSTORE_PWD.toCharArray());
|
||||
KeyStore.SecretKeyEntry secretKeyEntry = new KeyStore.SecretKeyEntry(secretKey);
|
||||
keyStore.setEntry(MY_SECRET_ENTRY, secretKeyEntry, protParam);
|
||||
|
||||
KeyStore result = keyStore.getKeyStore();
|
||||
Assert.assertTrue(result.size() == 1);
|
||||
KeyStore.Entry entry = keyStore.getEntry(MY_SECRET_ENTRY);
|
||||
Assert.assertTrue(entry != null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLoadedKeyStore_whenSetKeyEntry_thenSizeIsOneAndGetEntryNotNull() throws Exception {
|
||||
keyStore.createEmptyKeyStore();
|
||||
keyStore.loadKeyStore();
|
||||
|
||||
// Generate the key pair
|
||||
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
|
||||
keyPairGenerator.initialize(1024);
|
||||
KeyPair keyPair = keyPairGenerator.generateKeyPair();
|
||||
|
||||
// Generate a self signed certificate
|
||||
X509Certificate certificate = generateSelfSignedCertificate(keyPair);
|
||||
|
||||
X509Certificate[] certificateChain = new X509Certificate[1];
|
||||
certificateChain[0] = certificate;
|
||||
keyStore.setKeyEntry(MY_PRIVATE_KEY, keyPair.getPrivate(), KEYSTORE_PWD, certificateChain);
|
||||
|
||||
KeyStore result = keyStore.getKeyStore();
|
||||
Assert.assertTrue(result.size() == 1);
|
||||
KeyStore.Entry entry = keyStore.getEntry(MY_PRIVATE_KEY);
|
||||
Assert.assertTrue(entry != null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLoadedKeyStore_whenSetCertificateEntry_thenSizeIsOneAndGetCertificateEntryNotNull() throws Exception {
|
||||
keyStore.createEmptyKeyStore();
|
||||
keyStore.loadKeyStore();
|
||||
|
||||
// Generate the key pair
|
||||
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
|
||||
keyPairGenerator.initialize(1024);
|
||||
KeyPair keyPair = keyPairGenerator.generateKeyPair();
|
||||
|
||||
// Generate a self signed certificate
|
||||
X509Certificate certificate = generateSelfSignedCertificate(keyPair);
|
||||
|
||||
keyStore.setCertificateEntry(MY_CERTIFICATE, certificate);
|
||||
|
||||
KeyStore result = this.keyStore.getKeyStore();
|
||||
Assert.assertTrue(result.size() == 1);
|
||||
java.security.cert.Certificate resultCertificate = keyStore.getCertificate(MY_CERTIFICATE);
|
||||
Assert.assertNotNull(resultCertificate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLoadedKeyStoreWithOneEntry_whenDeleteEntry_thenKeyStoreSizeIsZero() throws Exception {
|
||||
keyStore.createEmptyKeyStore();
|
||||
keyStore.loadKeyStore();
|
||||
|
||||
KeyGenerator keygen = KeyGenerator.getInstance("HmacSHA256");
|
||||
SecretKey secretKey = keygen.generateKey();
|
||||
//ideally, password should be different for every key
|
||||
KeyStore.ProtectionParameter protParam = new KeyStore.PasswordProtection(KEYSTORE_PWD.toCharArray());
|
||||
KeyStore.SecretKeyEntry secretKeyEntry = new KeyStore.SecretKeyEntry(secretKey);
|
||||
keyStore.setEntry(MY_SECRET_ENTRY, secretKeyEntry, protParam);
|
||||
|
||||
keyStore.deleteEntry(MY_SECRET_ENTRY);
|
||||
|
||||
KeyStore result = this.keyStore.getKeyStore();
|
||||
Assert.assertTrue(result.size() == 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLoadedKeystore_whenDeleteKeyStore_thenKeyStoreIsNull() throws Exception {
|
||||
keyStore.createEmptyKeyStore();
|
||||
keyStore.loadKeyStore();
|
||||
|
||||
keyStore.deleteKeyStore();
|
||||
|
||||
KeyStore result = this.keyStore.getKeyStore();
|
||||
Assert.assertTrue(result == null);
|
||||
}
|
||||
|
||||
private X509Certificate generateSelfSignedCertificate(KeyPair keyPair) throws CertificateException, IOException, NoSuchProviderException, NoSuchAlgorithmException, InvalidKeyException, SignatureException {
|
||||
X509CertInfo certInfo = new X509CertInfo();
|
||||
// Serial number and version
|
||||
certInfo.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(new BigInteger(64, new SecureRandom())));
|
||||
certInfo.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3));
|
||||
|
||||
// Subject & Issuer
|
||||
X500Name owner = new X500Name(DN_NAME);
|
||||
certInfo.set(X509CertInfo.SUBJECT, owner);
|
||||
certInfo.set(X509CertInfo.ISSUER, owner);
|
||||
|
||||
// Key and algorithm
|
||||
certInfo.set(X509CertInfo.KEY, new CertificateX509Key(keyPair.getPublic()));
|
||||
AlgorithmId algorithm = new AlgorithmId(AlgorithmId.sha1WithRSAEncryption_oid);
|
||||
certInfo.set(X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId(algorithm));
|
||||
|
||||
// Validity
|
||||
Date validFrom = new Date();
|
||||
Date validTo = new Date(validFrom.getTime() + 50L * 365L * 24L * 60L * 60L * 1000L); //50 years
|
||||
CertificateValidity validity = new CertificateValidity(validFrom, validTo);
|
||||
certInfo.set(X509CertInfo.VALIDITY, validity);
|
||||
|
||||
// Create certificate and sign it
|
||||
X509CertImpl cert = new X509CertImpl(certInfo);
|
||||
cert.sign(keyPair.getPrivate(), SHA1WITHRSA);
|
||||
|
||||
// Since the SHA1withRSA provider may have a different algorithm ID to what we think it should be,
|
||||
// we need to reset the algorithm ID, and resign the certificate
|
||||
AlgorithmId actualAlgorithm = (AlgorithmId) cert.get(X509CertImpl.SIG_ALG);
|
||||
certInfo.set(CertificateAlgorithmId.NAME + "." + CertificateAlgorithmId.ALGORITHM, actualAlgorithm);
|
||||
X509CertImpl newCert = new X509CertImpl(certInfo);
|
||||
newCert.sign(keyPair.getPrivate(), SHA1WITHRSA);
|
||||
|
||||
return newCert;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package com.baeldung.system;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Properties;
|
||||
|
@ -44,6 +45,7 @@ public class SystemPropertiesTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void givenSystem_whenCalledClearProperties_thenDeleteAllPropertiesasResult() {
|
||||
|
||||
// Clears all system properties. Use with care!
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1 @@
|
|||
/bin/
|
|
@ -23,3 +23,4 @@
|
|||
- [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)
|
||||
|
|
|
@ -21,6 +21,11 @@
|
|||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-math3</artifactId>
|
||||
<version>${commons-math3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
|
@ -33,12 +38,6 @@
|
|||
<version>${junit.platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit4.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib</artifactId>
|
||||
|
@ -46,7 +45,7 @@
|
|||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib-jre8</artifactId>
|
||||
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
||||
<version>${kotlin-stdlib.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -160,10 +159,6 @@
|
|||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-failsafe-plugin</artifactId>
|
||||
<version>2.19.1</version>
|
||||
|
@ -194,14 +189,16 @@
|
|||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<kotlin-maven-plugin.version>1.1.2</kotlin-maven-plugin.version>
|
||||
<kotlin-test-junit.version>1.1.2</kotlin-test-junit.version>
|
||||
<kotlin-stdlib.version>1.1.2</kotlin-stdlib.version>
|
||||
<kotlin-reflect.version>1.1.2</kotlin-reflect.version>
|
||||
<kotlin-maven-plugin.version>1.2.31</kotlin-maven-plugin.version>
|
||||
<kotlin-test-junit.version>1.2.31</kotlin-test-junit.version>
|
||||
<kotlin-stdlib.version>1.2.31</kotlin-stdlib.version>
|
||||
<kotlin-reflect.version>1.2.31</kotlin-reflect.version>
|
||||
<kotlinx.version>0.15</kotlinx.version>
|
||||
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
|
||||
<kodein.version>4.1.0</kodein.version>
|
||||
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
|
||||
<junit.jupiter.version>5.0.0</junit.jupiter.version>
|
||||
<junit.platform.version>1.0.0</junit.platform.version>
|
||||
<junit.vintage.version>4.12.0</junit.vintage.version>
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.filter
|
||||
|
||||
import org.junit.jupiter.api.Assertions.assertIterableEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
internal class ChunkedTest {
|
||||
|
||||
@Test
|
||||
fun givenDNAFragmentString_whenChunking_thenProduceListOfChunks() {
|
||||
val dnaFragment = "ATTCGCGGCCGCCAA"
|
||||
|
||||
val fragments = dnaFragment.chunked(3)
|
||||
|
||||
assertIterableEquals(listOf("ATT", "CGC", "GGC", "CGC", "CAA"), fragments)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenDNAString_whenChunkingWithTransformer_thenProduceTransformedList() {
|
||||
val codonTable = mapOf("ATT" to "Isoleucine", "CAA" to "Glutamine", "CGC" to "Arginine", "GGC" to "Glycine")
|
||||
val dnaFragment = "ATTCGCGGCCGCCAA"
|
||||
|
||||
val proteins = dnaFragment.chunked(3) { codon ->
|
||||
codonTable[codon.toString()] ?: error("Unknown codon")
|
||||
}
|
||||
|
||||
assertIterableEquals(listOf("Isoleucine", "Arginine", "Glycine", "Arginine", "Glutamine"), proteins)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenListOfValues_whenChunking_thenProduceListOfArrays() {
|
||||
val whole = listOf(1, 4, 7, 4753, 2, 34, 62, 76, 5868, 0)
|
||||
val chunks = whole.chunked(6)
|
||||
|
||||
val expected = listOf(listOf(1, 4, 7, 4753, 2, 34), listOf(62, 76, 5868, 0))
|
||||
|
||||
assertIterableEquals(expected, chunks)
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
package com.baeldung.filter
|
||||
|
||||
import org.junit.jupiter.api.Assertions.assertIterableEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
internal class DistinctTest {
|
||||
data class SmallClass(val key: String, val num: Int)
|
||||
|
||||
@Test
|
||||
fun whenApplyingDistinct_thenReturnListOfNoDuplicateValues() {
|
||||
val array = arrayOf(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 5, 6, 7, 8, 9)
|
||||
val result = array.distinct()
|
||||
val expected = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9)
|
||||
|
||||
assertIterableEquals(expected, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenArrayOfClassObjects_whenApplyingDistinctOnClassProperty_thenReturnListDistinctOnThatValue() {
|
||||
|
||||
val original = arrayOf(
|
||||
SmallClass("key1", 1),
|
||||
SmallClass("key2", 2),
|
||||
SmallClass("key3", 3),
|
||||
SmallClass("key4", 3),
|
||||
SmallClass("er", 9),
|
||||
SmallClass("er", 10),
|
||||
SmallClass("er", 11))
|
||||
|
||||
val actual = original.distinctBy { it.key }
|
||||
|
||||
val expected = listOf(
|
||||
SmallClass("key1", 1),
|
||||
SmallClass("key2", 2),
|
||||
SmallClass("key3", 3),
|
||||
SmallClass("key4", 3),
|
||||
SmallClass("er", 9))
|
||||
|
||||
|
||||
assertIterableEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenArrayOfClassObjects_whenApplyingComplicatedSelector_thenReturnFirstElementToMatchEachSelectorValue() {
|
||||
val array = arrayOf(
|
||||
SmallClass("key1", 1),
|
||||
SmallClass("key2", 2),
|
||||
SmallClass("key3", 3),
|
||||
SmallClass("key4", 3),
|
||||
SmallClass("er", 9),
|
||||
SmallClass("er", 10),
|
||||
SmallClass("er", 11),
|
||||
SmallClass("er", 11),
|
||||
SmallClass("er", 91),
|
||||
SmallClass("blob", 22),
|
||||
SmallClass("dob", 27),
|
||||
SmallClass("high", 201_434_314))
|
||||
|
||||
val actual = array.distinctBy { Math.floor(it.num / 10.0) }
|
||||
|
||||
val expected = listOf(
|
||||
SmallClass("key1", 1),
|
||||
SmallClass("er", 10),
|
||||
SmallClass("er", 91),
|
||||
SmallClass("blob", 22),
|
||||
SmallClass("high", 201_434_314))
|
||||
|
||||
assertIterableEquals(expected, actual)
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package com.baeldung.filter
|
||||
|
||||
import org.junit.jupiter.api.Assertions.assertIterableEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
internal class DropTest {
|
||||
|
||||
@Test
|
||||
fun whenDroppingFirstTwoItemsOfArray_thenTwoLess() {
|
||||
val array = arrayOf(1, 2, 3, 4)
|
||||
val result = array.drop(2)
|
||||
val expected = listOf(3, 4)
|
||||
|
||||
assertIterableEquals(expected, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenDroppingMoreItemsOfArray_thenEmptyList() {
|
||||
val array = arrayOf(1, 2, 3, 4)
|
||||
val result = array.drop(5)
|
||||
val expected = listOf<Int>()
|
||||
|
||||
assertIterableEquals(expected, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenArray_whenDroppingLastElement_thenReturnListWithoutLastElement() {
|
||||
val array = arrayOf("1", "2", "3", "4")
|
||||
val result = array.dropLast(1)
|
||||
val expected = listOf("1", "2", "3")
|
||||
|
||||
assertIterableEquals(expected, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenArrayOfFloats_whenDroppingLastUntilPredicateIsFalse_thenReturnSubsetListOfFloats() {
|
||||
val array = arrayOf(1f, 1f, 1f, 1f, 1f, 2f, 1f, 1f, 1f)
|
||||
val result = array.dropLastWhile { it == 1f }
|
||||
val expected = listOf(1f, 1f, 1f, 1f, 1f, 2f)
|
||||
|
||||
assertIterableEquals(expected, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenList_whenDroppingMoreThanAvailable_thenThrowException() {
|
||||
val list = listOf('a', 'e', 'i', 'o', 'u')
|
||||
val result = list.drop(6)
|
||||
val expected: List<String> = listOf()
|
||||
|
||||
assertIterableEquals(expected, result)
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.filter
|
||||
|
||||
import org.apache.commons.math3.primes.Primes
|
||||
import org.junit.jupiter.api.Assertions.assertIterableEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
internal class FilterTest {
|
||||
|
||||
@Test
|
||||
fun givenAscendingValueMap_whenFilteringOnValue_ThenReturnSubsetOfMap() {
|
||||
val originalMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3)
|
||||
val filteredMap = originalMap.filter { it.value < 2 }
|
||||
val expectedMap = mapOf("key1" to 1)
|
||||
|
||||
assertTrue { expectedMap == filteredMap }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenSeveralCollections_whenFilteringToAccumulativeList_thenListContainsAllContents() {
|
||||
val array1 = arrayOf(90, 92, 93, 94, 92, 95, 93)
|
||||
val array2 = sequenceOf(51, 31, 83, 674_506_111, 256_203_161, 15_485_863)
|
||||
val list1 = listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
|
||||
val primes = mutableListOf<Int>()
|
||||
|
||||
val expected = listOf(2, 3, 5, 7, 31, 83, 15_485_863, 256_203_161, 674_506_111)
|
||||
|
||||
val primeCheck = { num: Int -> Primes.isPrime(num) }
|
||||
|
||||
array1.filterTo(primes, primeCheck)
|
||||
list1.filterTo(primes, primeCheck)
|
||||
array2.filterTo(primes, primeCheck)
|
||||
|
||||
primes.sort()
|
||||
|
||||
assertIterableEquals(expected, primes)
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.baeldung.filter
|
||||
|
||||
import org.junit.jupiter.api.Assertions.assertIterableEquals
|
||||
import org.junit.jupiter.api.Assertions.assertThrows
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
internal class SliceTest {
|
||||
|
||||
@Test
|
||||
fun whenSlicingAnArrayWithDotRange_ThenListEqualsTheSlice() {
|
||||
val original = arrayOf(1, 2, 3, 2, 1)
|
||||
val actual = original.slice(1..3)
|
||||
val expected = listOf(2, 3, 2)
|
||||
|
||||
assertIterableEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenSlicingAnArrayWithDownToRange_thenListMadeUpOfReverseSlice() {
|
||||
val original = arrayOf(1, 2, 3, 2, 1)
|
||||
val actual = original.slice(3 downTo 0)
|
||||
val expected = listOf(2, 3, 2, 1)
|
||||
|
||||
assertIterableEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenSlicingBeyondTheRangeOfTheArray_thenContainManyNulls() {
|
||||
val original = arrayOf(12, 3, 34, 4)
|
||||
val actual = original.slice(3..8)
|
||||
val expected = listOf(4, null, null, null, null, null)
|
||||
|
||||
assertIterableEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenSlicingBeyondRangeOfArrayWithStep_thenOutOfBoundsException() {
|
||||
assertThrows(ArrayIndexOutOfBoundsException::class.java) {
|
||||
val original = arrayOf(12, 3, 34, 4)
|
||||
original.slice(3..8 step 2)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.filter
|
||||
|
||||
import org.junit.jupiter.api.Assertions.assertIterableEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
internal class TakeTest {
|
||||
|
||||
@Test
|
||||
fun `given array of alternating types, when predicating on 'is String', then produce list of array up until predicate is false`() {
|
||||
val originalArray = arrayOf("val1", 2, "val3", 4, "val5", 6)
|
||||
val actualList = originalArray.takeWhile { it is String }
|
||||
val expectedList = listOf("val1")
|
||||
|
||||
assertIterableEquals(expectedList, actualList)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given array of alternating types, when taking 4 items, then produce list of first 4 items`() {
|
||||
val originalArray = arrayOf("val1", 2, "val3", 4, "val5", 6)
|
||||
val actualList = originalArray.take(4)
|
||||
val expectedList = listOf("val1", 2, "val3", 4)
|
||||
|
||||
println(originalArray.drop(4))
|
||||
println(actualList)
|
||||
|
||||
assertIterableEquals(expectedList, actualList)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when taking more items than available, then return all elements`() {
|
||||
val originalArray = arrayOf(1, 2)
|
||||
val actual = originalArray.take(10)
|
||||
val expected = listOf(1, 2)
|
||||
|
||||
assertIterableEquals(expected, actual)
|
||||
}
|
||||
|
||||
}
|
|
@ -60,28 +60,5 @@
|
|||
<artifactId>spring-core</artifactId>
|
||||
<version>4.3.6.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<forkCount>3</forkCount>
|
||||
<reuseForks>true</reuseForks>
|
||||
<excludes>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
<exclude>**/*LongRunningUnitTest.java</exclude>
|
||||
<exclude>**/*ManualTest.java</exclude>
|
||||
<exclude>**/JdbcTest.java</exclude>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -47,27 +47,4 @@
|
|||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -80,14 +80,6 @@
|
|||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>2.4</version>
|
||||
|
@ -95,10 +87,6 @@
|
|||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.17</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
|
|
@ -57,20 +57,6 @@
|
|||
<url>http://repo.opengeo.org</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<inherited>true</inherited>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<properties>
|
||||
<geotools.version>15.2</geotools.version>
|
||||
<geotools-swing.version>15.2</geotools-swing.version>
|
||||
|
|
|
@ -40,19 +40,6 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<properties>
|
||||
<log4j2.version>2.8.2</log4j2.version>
|
||||
<org.hamcrest.version>1.3</org.hamcrest.version>
|
||||
|
|
|
@ -29,17 +29,6 @@
|
|||
|
||||
<build>
|
||||
<finalName>deep-jsf</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -44,14 +44,6 @@
|
|||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.19.1</version>
|
||||
|
|
|
@ -41,16 +41,6 @@
|
|||
</excludes>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
|
|
|
@ -28,19 +28,5 @@
|
|||
<artifactId>janino</artifactId>
|
||||
<version>3.0.7</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -30,15 +30,5 @@
|
|||
<directory>src/test/resources/</directory>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -17,17 +17,4 @@
|
|||
<version>1.2.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -55,14 +55,6 @@
|
|||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
|
|
|
@ -12,14 +12,6 @@
|
|||
</parent>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>2.6</version>
|
||||
|
|
|
@ -36,14 +36,6 @@
|
|||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
|
|
|
@ -67,33 +67,5 @@
|
|||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<forkCount>3</forkCount>
|
||||
<reuseForks>true</reuseForks>
|
||||
<excludes>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -36,22 +36,7 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<target>1.8</target>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
<joda-time.version>2.9.9</joda-time.version>
|
||||
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
### Relevant Articles:
|
||||
-
|
||||
-[Introduction to JavaFX](http://www.baeldung.com/javafx)
|
||||
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
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>javax-servlets</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
|
@ -13,8 +12,6 @@
|
|||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
|
@ -35,6 +32,12 @@
|
|||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>5.0.5.RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
|
|
@ -6,7 +6,11 @@
|
|||
<packaging>war</packaging>
|
||||
<name>JavaEEAnnotationsSample</name>
|
||||
<description>JavaEEAnnotationsSample</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
|
@ -31,15 +35,6 @@
|
|||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
<hibernate.version>5.2.5.Final</hibernate.version>
|
||||
<mysql.version>6.0.5</mysql.version>
|
||||
<commons-io.version>2.5</commons-io.version>
|
||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -99,7 +99,6 @@
|
|||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<junit.platform.version>1.0.0</junit.platform.version>
|
||||
<junit.jupiter.version>5.0.0</junit.jupiter.version>
|
||||
<maven-surefire-plugin.version>2.20</maven-surefire-plugin.version>
|
||||
<jsonb-api.version>1.0</jsonb-api.version>
|
||||
<johnzon.version>1.1.3</johnzon.version>
|
||||
<geronimo-json_1.1_spec.version>1.0</geronimo-json_1.1_spec.version>
|
||||
|
|
|
@ -65,19 +65,30 @@
|
|||
<classifier>test</classifier>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.ignite</groupId>
|
||||
<artifactId>ignite-core</artifactId>
|
||||
<version>${ignite.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.ignite</groupId>
|
||||
<artifactId>ignite-spring</artifactId>
|
||||
<version>${ignite.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.ignite</groupId>
|
||||
<artifactId>ignite-indexing</artifactId>
|
||||
<version>${ignite.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>${gson.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<!-- Reladomo -->
|
||||
<plugin>
|
||||
|
@ -181,5 +192,7 @@
|
|||
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
|
||||
<ormlite.version>5.0</ormlite.version>
|
||||
<kafka.version>1.0.0</kafka.version>
|
||||
<ignite.version>2.3.0</ignite.version>
|
||||
<gson.version>2.8.2</gson.version>
|
||||
</properties>
|
||||
</project>
|
14
libraries-data/src/main/java/com/baeldung/ignite/cache/CustomLifecycleBean.java
vendored
Normal file
14
libraries-data/src/main/java/com/baeldung/ignite/cache/CustomLifecycleBean.java
vendored
Normal file
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.ignite.cache;
|
||||
|
||||
import org.apache.ignite.IgniteException;
|
||||
import org.apache.ignite.lifecycle.LifecycleBean;
|
||||
import org.apache.ignite.lifecycle.LifecycleEventType;
|
||||
|
||||
public class CustomLifecycleBean implements LifecycleBean {
|
||||
@Override
|
||||
public void onLifecycleEvent(LifecycleEventType lifecycleEventType) throws IgniteException {
|
||||
if (lifecycleEventType == LifecycleEventType.AFTER_NODE_START) {
|
||||
//do something right after the Ignite node starts
|
||||
}
|
||||
}
|
||||
}
|
58
libraries-data/src/main/java/com/baeldung/ignite/cache/IgniteCacheExample.java
vendored
Normal file
58
libraries-data/src/main/java/com/baeldung/ignite/cache/IgniteCacheExample.java
vendored
Normal file
|
@ -0,0 +1,58 @@
|
|||
package com.baeldung.ignite.cache;
|
||||
|
||||
import com.baeldung.ignite.model.Employee;
|
||||
import org.apache.ignite.Ignite;
|
||||
import org.apache.ignite.IgniteCache;
|
||||
import org.apache.ignite.Ignition;
|
||||
import org.apache.ignite.cache.query.QueryCursor;
|
||||
import org.apache.ignite.cache.query.SqlFieldsQuery;
|
||||
import org.apache.ignite.configuration.IgniteConfiguration;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class IgniteCacheExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Ignite ignite = Ignition.ignite();
|
||||
|
||||
IgniteCache<Integer, String> cache = ignite.cache("baeldungCache");
|
||||
|
||||
cache.put(1, "baeldung cache value");
|
||||
|
||||
String message = cache.get(1);
|
||||
}
|
||||
|
||||
private static void getObjectFromCache(Ignite ignite) {
|
||||
|
||||
IgniteCache<Integer, Employee> cache = ignite.getOrCreateCache("baeldungCache");
|
||||
|
||||
cache.put(1, new Employee(1, "John", true));
|
||||
cache.put(2, new Employee(2, "Anna", false));
|
||||
cache.put(3, new Employee(3, "George", true));
|
||||
|
||||
Employee employee = cache.get(1);
|
||||
}
|
||||
|
||||
private static void getFromCacheWithSQl(Ignite ignite) {
|
||||
|
||||
IgniteCache<Integer, Employee> cache = ignite.cache("baeldungCache");
|
||||
|
||||
SqlFieldsQuery sql = new SqlFieldsQuery(
|
||||
"select name from Employee where isEmployed = 'true'");
|
||||
|
||||
QueryCursor<List<?>> cursor = cache.query(sql);
|
||||
|
||||
for (List<?> row : cursor) {
|
||||
System.out.println(row.get(0));
|
||||
}
|
||||
}
|
||||
|
||||
private static void customInitialization() {
|
||||
|
||||
IgniteConfiguration configuration = new IgniteConfiguration();
|
||||
configuration.setLifecycleBeans(new CustomLifecycleBean());
|
||||
Ignite ignite = Ignition.start(configuration);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
package com.baeldung.ignite.jdbc;
|
||||
|
||||
import java.sql.*;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 3/14/2018.
|
||||
*/
|
||||
public class IgniteJDBC {
|
||||
|
||||
public static void main(String[] args) throws ClassNotFoundException, SQLException {
|
||||
|
||||
Class.forName("org.apache.ignite.IgniteJdbcThinDriver");
|
||||
|
||||
Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1/");
|
||||
|
||||
createDatabaseTables(conn);
|
||||
|
||||
insertData(conn);
|
||||
|
||||
getData(conn);
|
||||
}
|
||||
|
||||
private static void createDatabaseTables(Connection conn) throws SQLException {
|
||||
|
||||
Statement sql = conn.createStatement();
|
||||
sql.executeUpdate("CREATE TABLE Employee (" +
|
||||
" id INTEGER PRIMARY KEY, name VARCHAR, isEmployed timyint(1)) " +
|
||||
" WITH \"template=replicated\"");
|
||||
|
||||
sql.executeUpdate("CREATE INDEX idx_employee_name ON Employee (name)");
|
||||
}
|
||||
|
||||
private static void insertData(Connection conn) throws SQLException {
|
||||
|
||||
PreparedStatement sql =
|
||||
conn.prepareStatement("INSERT INTO Employee (id, name, isEmployed) VALUES (?, ?, ?)");
|
||||
sql.setLong(1, 1);
|
||||
sql.setString(2, "James");
|
||||
sql.setBoolean(3, true);
|
||||
sql.executeUpdate();
|
||||
|
||||
sql.setLong(1, 2);
|
||||
sql.setString(2, "Monica");
|
||||
sql.setBoolean(3, false);
|
||||
sql.executeUpdate();
|
||||
}
|
||||
|
||||
private static void getData(Connection conn) throws SQLException {
|
||||
|
||||
Statement sql = conn.createStatement();
|
||||
ResultSet rs = sql.executeQuery("SELECT e.name, e.isEmployed " +
|
||||
" FROM Employee e " +
|
||||
" WHERE e.isEmployed = TRUE ");
|
||||
|
||||
while (rs.next())
|
||||
System.out.println(rs.getString(1) + ", " + rs.getString(2));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package com.baeldung.ignite.model;
|
||||
|
||||
|
||||
public class Employee {
|
||||
|
||||
private Integer id;
|
||||
private String name;
|
||||
private boolean isEmployed;
|
||||
|
||||
public Employee(Integer id, String name, boolean isEmployed) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.isEmployed = isEmployed;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public boolean isEmployed() {
|
||||
return isEmployed;
|
||||
}
|
||||
|
||||
public void setEmployed(boolean employed) {
|
||||
isEmployed = employed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Employee{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
", isEmployed=" + isEmployed +
|
||||
'}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.ignite.stream;
|
||||
|
||||
import com.baeldung.ignite.model.Employee;
|
||||
import org.apache.ignite.configuration.CacheConfiguration;
|
||||
|
||||
import javax.cache.configuration.FactoryBuilder;
|
||||
import javax.cache.expiry.CreatedExpiryPolicy;
|
||||
import javax.cache.expiry.Duration;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
||||
public class CacheConfig {
|
||||
|
||||
public static CacheConfiguration<Integer, Employee> employeeCache() {
|
||||
|
||||
CacheConfiguration<Integer, Employee> config = new CacheConfiguration<>("baeldungEmployees");
|
||||
|
||||
config.setIndexedTypes(Integer.class, Employee.class);
|
||||
config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(
|
||||
new CreatedExpiryPolicy(new Duration(TimeUnit.SECONDS, 5))));
|
||||
|
||||
return config;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.baeldung.ignite.stream;
|
||||
|
||||
import com.baeldung.ignite.model.Employee;
|
||||
import com.google.gson.Gson;
|
||||
import org.apache.ignite.Ignite;
|
||||
import org.apache.ignite.IgniteCache;
|
||||
import org.apache.ignite.IgniteDataStreamer;
|
||||
import org.apache.ignite.Ignition;
|
||||
import org.apache.ignite.stream.StreamTransformer;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class IgniteStream {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
Ignition.setClientMode(true);
|
||||
Ignite ignite = Ignition.start();
|
||||
|
||||
IgniteCache<Integer, Employee> cache = ignite.getOrCreateCache(CacheConfig.employeeCache());
|
||||
IgniteDataStreamer<Integer, Employee> streamer = ignite.dataStreamer(cache.getName());
|
||||
streamer.allowOverwrite(true);
|
||||
|
||||
streamer.receiver(StreamTransformer.from((e, arg) -> {
|
||||
|
||||
Employee employee = e.getValue();
|
||||
employee.setEmployed(true);
|
||||
e.setValue(employee);
|
||||
|
||||
return null;
|
||||
}));
|
||||
|
||||
Path path = Paths.get(IgniteStream.class.getResource("employees.txt").toURI());
|
||||
|
||||
Files.lines(path)
|
||||
.forEach(line -> {
|
||||
Employee employee = new Gson().fromJson(line, Employee.class);
|
||||
streamer.addData(employee.getId(), employee);
|
||||
});
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
{id:"1", name="John", isEmployed: "true"}
|
||||
{id:"1", name="Anna", isEmployed: "false"}
|
||||
{id:"1", name="George", isEmployed: "true"}
|
|
@ -785,16 +785,6 @@
|
|||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<!-- /Neuroph -->
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@ import org.junit.Test;
|
|||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
@ -191,14 +192,25 @@ public class JetS3tLiveTest {
|
|||
S3Bucket bucket = createBucket();
|
||||
assertNotNull(bucket);
|
||||
|
||||
// Create a stream
|
||||
ByteArrayInputStream testStream = new ByteArrayInputStream("test stream".getBytes());
|
||||
ArrayList<Integer> numbers = new ArrayList<>();
|
||||
numbers.add(2);
|
||||
numbers.add(3);
|
||||
numbers.add(5);
|
||||
numbers.add(7);
|
||||
|
||||
// Serialize ArrayList
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(bytes);
|
||||
objectOutputStream.writeObject(numbers);
|
||||
|
||||
// Wrap bytes
|
||||
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes.toByteArray());
|
||||
|
||||
// Create and populate object
|
||||
S3Object streamObject = new S3Object("stream");
|
||||
streamObject.setDataInputStream(testStream);
|
||||
streamObject.setContentLength(testStream.available());
|
||||
streamObject.setContentType("text/plain");
|
||||
streamObject.setDataInputStream(byteArrayInputStream);
|
||||
streamObject.setContentLength(byteArrayInputStream.available());
|
||||
streamObject.setContentType("binary/octet-stream");
|
||||
|
||||
// Put it
|
||||
s3Service.putObject(BucketName, streamObject);
|
||||
|
@ -206,11 +218,14 @@ public class JetS3tLiveTest {
|
|||
// Get it
|
||||
S3Object newStreamObject = s3Service.getObject(BucketName, "stream");
|
||||
|
||||
// Convert back to string
|
||||
String testString = new BufferedReader(new InputStreamReader(newStreamObject.getDataInputStream()))
|
||||
.lines().collect(Collectors.joining("\n"));
|
||||
// Convert back to ArrayList
|
||||
ObjectInputStream objectInputStream = new ObjectInputStream(newStreamObject.getDataInputStream());
|
||||
ArrayList<Integer> newNumbers = (ArrayList<Integer>)objectInputStream.readObject();
|
||||
|
||||
assertTrue("test stream".equals(testString));
|
||||
assertEquals(2, (int)newNumbers.get(0));
|
||||
assertEquals(3, (int)newNumbers.get(1));
|
||||
assertEquals(5, (int)newNumbers.get(2));
|
||||
assertEquals(7, (int)newNumbers.get(3));
|
||||
|
||||
// Clean up
|
||||
deleteObject("stream");
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
package com.baeldung.netty;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.junit.Assert;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Test;
|
||||
|
||||
import io.netty.channel.embedded.EmbeddedChannel;
|
||||
|
@ -17,91 +16,82 @@ import io.netty.handler.codec.http.HttpResponseStatus;
|
|||
import io.netty.handler.codec.http.HttpVersion;
|
||||
|
||||
public class EmbeddedChannelUnitTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void givenTwoChannelHandlers_testPipeline() {
|
||||
|
||||
final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/calculate?a=10&b=5");
|
||||
httpRequest.headers().add("Operator", "Add");
|
||||
|
||||
EmbeddedChannel channel = new EmbeddedChannel(
|
||||
new HttpMessageHandler(), new CalculatorOperationHandler());
|
||||
|
||||
channel.pipeline()
|
||||
.addFirst(new HttpMessageHandler())
|
||||
.addLast(new CalculatorOperationHandler());
|
||||
final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
|
||||
"/calculate?a=10&b=5");
|
||||
httpRequest.headers().add("Operator", "Add");
|
||||
|
||||
EmbeddedChannel channel = new EmbeddedChannel(new HttpMessageHandler(), new CalculatorOperationHandler());
|
||||
|
||||
// send HTTP request to server and check that the message is on the inbound pipeline
|
||||
assertTrue(channel.writeInbound(httpRequest));
|
||||
channel.pipeline().addFirst(new HttpMessageHandler()).addLast(new CalculatorOperationHandler());
|
||||
|
||||
long inboundChannelResponse = channel.readInbound();
|
||||
assertEquals(15, inboundChannelResponse);
|
||||
// send HTTP request to server and check that the message is on the inbound pipeline
|
||||
assertThat(channel.writeInbound(httpRequest)).isTrue();
|
||||
|
||||
// we should have an outbound message in the form of a HTTP response
|
||||
assertEquals(1, channel.outboundMessages().size());
|
||||
// Object response = channel.readOutbound();
|
||||
long inboundChannelResponse = channel.readInbound();
|
||||
assertThat(inboundChannelResponse).isEqualTo(15);
|
||||
|
||||
FullHttpResponse httpResponse = channel.readOutbound();
|
||||
String httpResponseContent = httpResponse.content().toString(Charset.defaultCharset());
|
||||
assertTrue("15".equalsIgnoreCase(httpResponseContent));
|
||||
// we should have an outbound message in the form of a HTTP response
|
||||
assertThat(channel.outboundMessages().size()).isEqualTo(1);
|
||||
// Object response = channel.readOutbound();
|
||||
|
||||
FullHttpResponse httpResponse = channel.readOutbound();
|
||||
String httpResponseContent = httpResponse.content().toString(Charset.defaultCharset());
|
||||
assertThat(httpResponseContent).isEqualTo("15");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenTwoChannelHandlers_testExceptionHandlingInHttpMessageHandler() {
|
||||
|
||||
EmbeddedChannel channel = new EmbeddedChannel(
|
||||
new HttpMessageHandler(), new CalculatorOperationHandler());
|
||||
|
||||
final FullHttpRequest wrongHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/calculate?a=10&b=5");
|
||||
wrongHttpRequest.headers().add("Operator", "Add");
|
||||
EmbeddedChannel channel = new EmbeddedChannel(new HttpMessageHandler(), new CalculatorOperationHandler());
|
||||
|
||||
try {
|
||||
// send invalid HTTP request to server and expect and error
|
||||
channel.pipeline().fireChannelRead(wrongHttpRequest);
|
||||
channel.checkException();
|
||||
// channel.writeInbound(wrongHttpRequest);
|
||||
Assert.fail();
|
||||
final FullHttpRequest wrongHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
|
||||
"/calculate?a=10&b=5");
|
||||
wrongHttpRequest.headers().add("Operator", "Add");
|
||||
|
||||
} catch (Exception ex) {
|
||||
Throwable thrownException = catchThrowable(() -> {
|
||||
// send invalid HTTP request to server and expect and error
|
||||
channel.pipeline().fireChannelRead(wrongHttpRequest);
|
||||
channel.checkException();
|
||||
Assertions.failBecauseExceptionWasNotThrown(UnsupportedOperationException.class);
|
||||
});
|
||||
|
||||
// the HttpMessageHandler does not handle the exception and throws it down the pipeline
|
||||
assertTrue(ex instanceof UnsupportedOperationException);
|
||||
assertTrue(ex.getMessage().equalsIgnoreCase("HTTP method not supported"));
|
||||
assertThat(thrownException)
|
||||
.isInstanceOf(UnsupportedOperationException.class)
|
||||
.hasMessage("HTTP method not supported");
|
||||
|
||||
FullHttpResponse errorHttpResponse = channel.readOutbound();
|
||||
String errorHttpResponseContent = errorHttpResponse.content().toString(Charset.defaultCharset());
|
||||
assertTrue("Operation not defined".equalsIgnoreCase(errorHttpResponseContent));
|
||||
assertEquals(HttpResponseStatus.INTERNAL_SERVER_ERROR, errorHttpResponse.status());
|
||||
}
|
||||
FullHttpResponse errorHttpResponse = channel.readOutbound();
|
||||
String errorHttpResponseContent = errorHttpResponse.content().toString(Charset.defaultCharset());
|
||||
assertThat(errorHttpResponseContent).isEqualToIgnoringCase("Operation not defined");
|
||||
assertThat(errorHttpResponse.status()).isEqualTo(HttpResponseStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void givenTwoChannelHandlers_testExceptionHandlingInCalculatorOperationHandler() {
|
||||
EmbeddedChannel channel = new EmbeddedChannel(
|
||||
new HttpMessageHandler(), new CalculatorOperationHandler());
|
||||
EmbeddedChannel channel = new EmbeddedChannel(new HttpMessageHandler(), new CalculatorOperationHandler());
|
||||
|
||||
final FullHttpRequest wrongHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/calculate?a=10&b=5");
|
||||
wrongHttpRequest.headers().add("Operator", "Invalid_operation");
|
||||
final FullHttpRequest wrongHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
|
||||
"/calculate?a=10&b=5");
|
||||
wrongHttpRequest.headers().add("Operator", "Invalid_operation");
|
||||
|
||||
try {
|
||||
// send invalid HTTP request to server and expect and error
|
||||
channel.writeInbound(wrongHttpRequest);
|
||||
Assert.fail();
|
||||
Throwable thrownException = catchThrowable(() -> {
|
||||
// send invalid HTTP request to server and expect and error
|
||||
channel.writeInbound(wrongHttpRequest);
|
||||
Assertions.failBecauseExceptionWasNotThrown(IllegalArgumentException.class);
|
||||
});
|
||||
|
||||
} catch (Exception ex) {
|
||||
// the HttpMessageHandler does not handle the exception and throws it down the
|
||||
// pipeline
|
||||
assertThat(thrownException).isInstanceOf(IllegalArgumentException.class).hasMessage("Operation not defined");
|
||||
|
||||
// the HttpMessageHandler does not handle the exception and throws it down the pipeline
|
||||
assertTrue(ex instanceof IllegalArgumentException);
|
||||
assertTrue(ex.getMessage().equalsIgnoreCase("Operation not defined"));
|
||||
|
||||
// the outbound message is a HTTP response with the status code 500
|
||||
FullHttpResponse errorHttpResponse = channel.readOutbound();
|
||||
String errorHttpResponseContent = errorHttpResponse.content().toString(Charset.defaultCharset());
|
||||
assertTrue("Operation not defined".equalsIgnoreCase(errorHttpResponseContent));
|
||||
assertEquals(HttpResponseStatus.INTERNAL_SERVER_ERROR, errorHttpResponse.status());
|
||||
}
|
||||
// the outbound message is a HTTP response with the status code 500
|
||||
FullHttpResponse errorHttpResponse = channel.readOutbound();
|
||||
String errorHttpResponseContent = errorHttpResponse.content().toString(Charset.defaultCharset());
|
||||
assertThat(errorHttpResponseContent).isEqualToIgnoringCase("Operation not defined");
|
||||
assertThat(errorHttpResponse.status()).isEqualTo(HttpResponseStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -35,14 +35,6 @@
|
|||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>2.6</version>
|
||||
|
|
|
@ -71,24 +71,7 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<forkCount>3</forkCount>
|
||||
<reuseForks>true</reuseForks>
|
||||
<excludes>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
<exclude>**/*LongRunningUnitTest.java</exclude>
|
||||
<exclude>**/*ManualTest.java</exclude>
|
||||
<exclude>**/JdbcTest.java</exclude>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
|
|
|
@ -31,15 +31,6 @@
|
|||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
|
|
|
@ -20,15 +20,6 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
### Relevant Articles:
|
||||
- [Facade Design Pattern in Java](http://www.baeldung.com/java-facade-pattern)
|
|
@ -20,15 +20,6 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
|
|
|
@ -22,15 +22,6 @@
|
|||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
|
|
|
@ -23,16 +23,6 @@
|
|||
<build>
|
||||
<finalName>baeldung-liquibase-demo</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.liquibase</groupId>
|
||||
<artifactId>liquibase-maven-plugin</artifactId>
|
||||
|
|
|
@ -149,32 +149,6 @@
|
|||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.jdbc.autogenkey.config;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
@Configuration
|
||||
public class PersistenceConfig {
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource(Environment env) {
|
||||
return new EmbeddedDatabaseBuilder()
|
||||
.setType(EmbeddedDatabaseType.H2)
|
||||
.addScript("autogenkey-schema.sql")
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
|
||||
return new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.jdbc.autogenkey.repository;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.support.GeneratedKeyHolder;
|
||||
import org.springframework.jdbc.support.KeyHolder;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class MessageRepositoryJDBCTemplate {
|
||||
|
||||
@Autowired
|
||||
JdbcTemplate jdbcTemplate;
|
||||
|
||||
final String INSERT_MESSAGE_SQL = "insert into sys_message (message) values(?) ";
|
||||
|
||||
public long insert(final String message) {
|
||||
|
||||
KeyHolder keyHolder = new GeneratedKeyHolder();
|
||||
|
||||
jdbcTemplate.update(connection -> {
|
||||
PreparedStatement ps = connection.prepareStatement(INSERT_MESSAGE_SQL);
|
||||
ps.setString(1, message);
|
||||
return ps;
|
||||
}, keyHolder);
|
||||
|
||||
return (long) keyHolder.getKey();
|
||||
}
|
||||
|
||||
final String SELECT_BY_ID = "select message from sys_message where id = ?";
|
||||
|
||||
public String getMessageById(long id) {
|
||||
return this.jdbcTemplate.queryForObject(SELECT_BY_ID, String.class, new Object[] { id });
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.jdbc.autogenkey.repository;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class MessageRepositorySimpleJDBCInsert {
|
||||
|
||||
SimpleJdbcInsert messageInsert;
|
||||
|
||||
@Autowired
|
||||
public MessageRepositorySimpleJDBCInsert(DataSource dataSource) {
|
||||
messageInsert = new SimpleJdbcInsert(dataSource).withTableName("sys_message").usingGeneratedKeyColumns("id");
|
||||
}
|
||||
|
||||
public long insert(String message) {
|
||||
Map<String, Object> parameters = new HashMap<String, Object>(1);
|
||||
parameters.put("message", message);
|
||||
Number newId = messageInsert.executeAndReturnKey(parameters);
|
||||
return (long) newId;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
CREATE TABLE IF NOT EXISTS sys_message (
|
||||
id bigint(20) NOT NULL AUTO_INCREMENT,
|
||||
message varchar(100) NOT NULL,
|
||||
PRIMARY KEY (id)
|
||||
);
|
|
@ -0,0 +1,49 @@
|
|||
package com.baeldung.jdbc.autogenkey;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.jdbc.autogenkey.repository.MessageRepositoryJDBCTemplate;
|
||||
import com.baeldung.jdbc.autogenkey.repository.MessageRepositorySimpleJDBCInsert;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
public class GetAutoGenKeyByJDBC {
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = { "com.baeldung.jdbc.autogenkey" })
|
||||
public static class SpringConfig {
|
||||
|
||||
}
|
||||
|
||||
@Autowired
|
||||
MessageRepositorySimpleJDBCInsert messageRepositorySimpleJDBCInsert;
|
||||
|
||||
@Autowired
|
||||
MessageRepositoryJDBCTemplate messageRepositoryJDBCTemplate;
|
||||
|
||||
final String MESSAGE_CONTENT = "Test";
|
||||
|
||||
@Test
|
||||
public void insertJDBC_whenLoadMessageByKey_thenGetTheSameMessage() {
|
||||
long key = messageRepositoryJDBCTemplate.insert(MESSAGE_CONTENT);
|
||||
String loadedMessage = messageRepositoryJDBCTemplate.getMessageById(key);
|
||||
|
||||
assertEquals(MESSAGE_CONTENT, loadedMessage);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void insertSimpleInsert_whenLoadMessageKey_thenGetTheSameMessage() {
|
||||
long key = messageRepositorySimpleJDBCInsert.insert(MESSAGE_CONTENT);
|
||||
String loadedMessage = messageRepositoryJDBCTemplate.getMessageById(key);
|
||||
|
||||
assertEquals(MESSAGE_CONTENT, loadedMessage);
|
||||
}
|
||||
|
||||
}
|
1
pom.xml
1
pom.xml
|
@ -64,6 +64,7 @@
|
|||
<module>core-java</module>
|
||||
<module>core-java-io</module>
|
||||
<module>core-java-8</module>
|
||||
<!--<module>core-kotlin</module>-->
|
||||
<module>core-groovy</module>
|
||||
<module>core-java-concurrency</module>
|
||||
|
||||
|
|
|
@ -13,6 +13,12 @@
|
|||
<commons-io.version>2.5</commons-io.version>
|
||||
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
||||
</properties>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>resteasy-tutorial</artifactId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<finalName>RestEasyTutorial</finalName>
|
||||
|
@ -73,66 +79,5 @@
|
|||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons-io.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>live</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>**/*IntegrationTest.java</exclude>
|
||||
</excludes>
|
||||
<includes>
|
||||
<include>**/*LiveTest.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<test.mime>json</test.mime>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.cargo</groupId>
|
||||
<artifactId>cargo-maven2-plugin</artifactId>
|
||||
<version>${cargo-maven2-plugin.version}</version>
|
||||
<configuration>
|
||||
<wait>false</wait>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>start-server</id>
|
||||
<phase>pre-integration-test</phase>
|
||||
<goals>
|
||||
<goal>start</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>stop-server</id>
|
||||
<phase>post-integration-test</phase>
|
||||
<goals>
|
||||
<goal>stop</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
</project>
|
|
@ -39,8 +39,8 @@ public class Car {
|
|||
// ...
|
||||
}
|
||||
|
||||
public void сhangeOilWithIsNull(String oil) {
|
||||
Assert.isNull(oil, "oil must be null");
|
||||
public void replaceBattery(CarBattery carBattery) {
|
||||
Assert.isNull(carBattery.getCharge(), "to replace battery the charge must be null");
|
||||
// ...
|
||||
}
|
||||
|
||||
|
@ -99,7 +99,9 @@ public class Car {
|
|||
car.fuel();
|
||||
|
||||
car.сhangeOil("oil");
|
||||
car.сhangeOilWithIsNull(null);
|
||||
|
||||
CarBattery carBattery = new CarBattery();
|
||||
car.replaceBattery(carBattery);
|
||||
|
||||
car.сhangeEngine(new ToyotaEngine());
|
||||
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.assertions;
|
||||
|
||||
public class CarBattery {
|
||||
private String charge;
|
||||
|
||||
public String getCharge() {
|
||||
return charge;
|
||||
}
|
||||
|
||||
public void setCharge(String charge) {
|
||||
this.charge = charge;
|
||||
}
|
||||
}
|
|
@ -5,7 +5,6 @@
|
|||
|
||||
<groupId>org.baeldung</groupId>
|
||||
<artifactId>spring-boot-bootstrap</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>spring-boot-bootstrap</name>
|
||||
|
@ -87,15 +86,6 @@
|
|||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>integration</id>
|
||||
|
|
|
@ -32,3 +32,5 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|||
- [Spring Data Java 8 Support](http://www.baeldung.com/spring-data-java-8)
|
||||
- [A Quick Guide to Maven Wrapper](http://www.baeldung.com/maven-wrapper)
|
||||
- [An Introduction to Kong](http://www.baeldung.com/kong)
|
||||
- [Spring Boot Customize Whitelabel Error Page](http://www.baeldung.com/spring-boot-custom-error-page)
|
||||
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.bootcustomfilters;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* Boot application
|
||||
* @author hemant
|
||||
*
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class SpringBootFiltersApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootFiltersApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.bootcustomfilters.controller;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.bootcustomfilters.model.User;
|
||||
|
||||
/**
|
||||
* Rest controller for User
|
||||
* @author hemant
|
||||
*
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/users")
|
||||
public class UserController {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(UserController.class);
|
||||
|
||||
@RequestMapping("")
|
||||
public List<User> getAllUsers() {
|
||||
LOG.info("Fetching all the users");
|
||||
return Arrays.asList(
|
||||
new User(UUID.randomUUID().toString(), "User1", "user1@test.com"),
|
||||
new User(UUID.randomUUID().toString(), "User1", "user1@test.com"),
|
||||
new User(UUID.randomUUID().toString(), "User1", "user1@test.com"));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung.bootcustomfilters.filters;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* A servlet filter to log request and response
|
||||
* The logging implementation is pretty native and for demonstration only
|
||||
* @author hemant
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
@Order(2)
|
||||
public class RequestResponseLoggingFilter implements Filter {
|
||||
|
||||
private final static Logger LOG = LoggerFactory.getLogger(RequestResponseLoggingFilter.class);
|
||||
|
||||
@Override
|
||||
public void init(final FilterConfig filterConfig) throws ServletException {
|
||||
LOG.info("Initializing filter :{}", this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
|
||||
throws IOException, ServletException {
|
||||
HttpServletRequest req = (HttpServletRequest) request;
|
||||
HttpServletResponse res = (HttpServletResponse) response;
|
||||
LOG.info("Logging Request {} : {}", req.getMethod(), req.getRequestURI());
|
||||
chain.doFilter(request, response);
|
||||
LOG.info("Logging Response :{}", res.getContentType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
LOG.warn("Destructing filter :{}", this);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package com.baeldung.bootcustomfilters.filters;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* A filter to create transaction before and commit it once request completes
|
||||
* The current implemenatation is just for demo
|
||||
* @author hemant
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
@Order(1)
|
||||
public class TransactionFilter implements Filter {
|
||||
|
||||
private final static Logger LOG = LoggerFactory.getLogger(TransactionFilter.class);
|
||||
|
||||
@Override
|
||||
public void init(final FilterConfig filterConfig) throws ServletException {
|
||||
LOG.info("Initializing filter :{}", this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
|
||||
HttpServletRequest req = (HttpServletRequest) request;
|
||||
LOG.info("Starting Transaction for req :{}", req.getRequestURI());
|
||||
chain.doFilter(request, response);
|
||||
LOG.info("Committing Transaction for req :{}", req.getRequestURI());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
LOG.warn("Destructing filter :{}", this);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package com.baeldung.bootcustomfilters.model;
|
||||
|
||||
/**
|
||||
* User model
|
||||
* @author hemant
|
||||
*
|
||||
*/
|
||||
public class User {
|
||||
|
||||
private String id;
|
||||
private String name;
|
||||
private String email;
|
||||
|
||||
public User(String id, String name, String email) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
}
|
|
@ -15,19 +15,21 @@ public class MyErrorController implements ErrorController {
|
|||
|
||||
@RequestMapping(value = "/error")
|
||||
public String handleError(HttpServletRequest request) {
|
||||
|
||||
Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
|
||||
|
||||
Integer statusCode =
|
||||
Integer.valueOf(request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE).toString());
|
||||
if (status != null) {
|
||||
|
||||
Integer statusCode = Integer.valueOf(status.toString());
|
||||
|
||||
if(statusCode == HttpStatus.NOT_FOUND.value()) {
|
||||
return "error-404";
|
||||
}
|
||||
else if(statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
|
||||
return "error-500";
|
||||
}
|
||||
else {
|
||||
return "error";
|
||||
if(statusCode == HttpStatus.NOT_FOUND.value()) {
|
||||
return "error-404";
|
||||
}
|
||||
else if(statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
|
||||
return "error-500";
|
||||
}
|
||||
}
|
||||
return "error";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,7 +1,14 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous" />
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
|
||||
</head>
|
||||
<body>
|
||||
<h1>Sorry we couldn't find the resource you are looking for</h1>
|
||||
<a href="/">Go Home</a>
|
||||
<div class="container">
|
||||
<div class="jumbotron" >
|
||||
<h1 class="text-center"><i class="fa fa-frown-o"> </i> Sorry, we couldn't find the page you were looking for. </h1>
|
||||
<p class="text-center"><a class="btn btn-primary" href="/"><i class="fa fa-home"></i>Go Home</a></p>
|
||||
</div></div>
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
|
@ -1,7 +1,16 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous" />
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
|
||||
</head>
|
||||
<body>
|
||||
<h1>Oops! We spilled something but we're fixing it</h1>
|
||||
<a href="/">Go Home</a>
|
||||
<div class="container">
|
||||
<div class="jumbotron" >
|
||||
<h1 class="text-center"><i class="fa fa-frown-o"> </i> Sorry, something went wrong! </h1>
|
||||
|
||||
<h2 class="text-center">We're fixing it.</h2>
|
||||
<p class="text-center"><a class="btn btn-primary" href="/"><i class="fa fa-home"></i>Go Home</a></p>
|
||||
</div></div>
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
|
@ -1,7 +1,16 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous" />
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
|
||||
</head>
|
||||
<body>
|
||||
<h1>Something went wrong! Our Engineers are on it temp</h1>
|
||||
<a href="/">Go Home</a>
|
||||
<div class="container">
|
||||
<div class="jumbotron" >
|
||||
<h1 class="text-center"><i class="fa fa-frown-o"> </i> Something went wrong! </h1>
|
||||
|
||||
<h2 class="text-center">Our Engineers are on it.</h2>
|
||||
<p class="text-center"><a class="btn btn-primary" href="/"><i class="fa fa-home"></i>Go Home</a></p>
|
||||
</div></div>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<body>
|
||||
<h1>Welcome Home</h1>
|
||||
</body>
|
||||
</html>
|
|
@ -70,12 +70,7 @@
|
|||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue