Merge branch 'master' into avro-error
This commit is contained in:
commit
b205db170e
9
.gitignore
vendored
9
.gitignore
vendored
@ -48,3 +48,12 @@ dependency-reduced-pom.xml
|
|||||||
*.so
|
*.so
|
||||||
*.dylib
|
*.dylib
|
||||||
*.dll
|
*.dll
|
||||||
|
|
||||||
|
xml/src/test/resources/example_dom4j_new.xml
|
||||||
|
xml/src/test/resources/example_dom4j_updated.xml
|
||||||
|
xml/src/test/resources/example_jaxb_new.xml
|
||||||
|
core-java-io/hard_link.txt
|
||||||
|
core-java-io/target_link.txt
|
||||||
|
core-java/src/main/java/com/baeldung/manifest/MANIFEST.MF
|
||||||
|
ethereum/logs/
|
||||||
|
jmeter/src/main/resources/*-JMeter.csv
|
@ -19,7 +19,7 @@ In additional to Spring, the following technologies are in focus: `core Java`, `
|
|||||||
|
|
||||||
Building the project
|
Building the project
|
||||||
====================
|
====================
|
||||||
To do the full build, do: `mvn install -Dgib.enabled=false`
|
To do the full build, do: `mvn install -Pdefault -Dgib.enabled=false`
|
||||||
|
|
||||||
|
|
||||||
Working with the code in Eclipse
|
Working with the code in Eclipse
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.convertlisttomap;
|
||||||
|
|
||||||
|
public class Animal {
|
||||||
|
private int id;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Animal(int id, String name) {
|
||||||
|
this.id = id;
|
||||||
|
this.setName(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
package com.baeldung.convertlisttomap;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.apache.commons.collections4.MapUtils;
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
|
|
||||||
|
public class ConvertListToMapService {
|
||||||
|
|
||||||
|
public Map<Integer, Animal> convertListBeforeJava8(List<Animal> list) {
|
||||||
|
|
||||||
|
Map<Integer, Animal> map = new HashMap<>();
|
||||||
|
|
||||||
|
for (Animal animal : list) {
|
||||||
|
map.put(animal.getId(), animal);
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<Integer, Animal> convertListAfterJava8(List<Animal> list) {
|
||||||
|
Map<Integer, Animal> map = list.stream().collect(Collectors.toMap(Animal::getId, animal -> animal));
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<Integer, Animal> convertListWithGuava(List<Animal> list) {
|
||||||
|
|
||||||
|
Map<Integer, Animal> map = Maps.uniqueIndex(list, Animal::getId);
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<Integer, Animal> convertListWithApacheCommons(List<Animal> list) {
|
||||||
|
|
||||||
|
Map<Integer, Animal> map = new HashMap<>();
|
||||||
|
|
||||||
|
MapUtils.populateMap(map, list, Animal::getId);
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,67 @@
|
|||||||
|
package com.baeldung.convertlisttomap;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class ConvertListToMapServiceUnitTest {
|
||||||
|
List<Animal> list;
|
||||||
|
|
||||||
|
private ConvertListToMapService convertListService;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() {
|
||||||
|
this.convertListService = new ConvertListToMapService();
|
||||||
|
this.list = new ArrayList<>();
|
||||||
|
|
||||||
|
Animal cat = new Animal(1, "Cat");
|
||||||
|
list.add(cat);
|
||||||
|
Animal dog = new Animal(2, "Dog");
|
||||||
|
list.add(dog);
|
||||||
|
Animal pig = new Animal(3, "Pig");
|
||||||
|
list.add(pig);
|
||||||
|
Animal cow = new Animal(4, "Cow");
|
||||||
|
list.add(cow);
|
||||||
|
Animal goat = new Animal(5, "Goat");
|
||||||
|
list.add(goat);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAList_whenConvertBeforeJava8_thenReturnMapWithTheSameElements() {
|
||||||
|
|
||||||
|
Map<Integer, Animal> map = convertListService.convertListBeforeJava8(list);
|
||||||
|
|
||||||
|
assertThat(map.values(), containsInAnyOrder(list.toArray()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAList_whenConvertAfterJava8_thenReturnMapWithTheSameElements() {
|
||||||
|
|
||||||
|
Map<Integer, Animal> map = convertListService.convertListAfterJava8(list);
|
||||||
|
|
||||||
|
assertThat(map.values(), containsInAnyOrder(list.toArray()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAList_whenConvertWithGuava_thenReturnMapWithTheSameElements() {
|
||||||
|
|
||||||
|
Map<Integer, Animal> map = convertListService.convertListWithGuava(list);
|
||||||
|
|
||||||
|
assertThat(map.values(), containsInAnyOrder(list.toArray()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAList_whenConvertWithApacheCommons_thenReturnMapWithTheSameElements() {
|
||||||
|
|
||||||
|
Map<Integer, Animal> map = convertListService.convertListWithApacheCommons(list);
|
||||||
|
|
||||||
|
assertThat(map.values(), containsInAnyOrder(list.toArray()));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,68 @@
|
|||||||
|
package com.baeldung.convertlisttomap;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.hasItem;
|
||||||
|
import static org.hamcrest.Matchers.hasSize;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class ConvertListWithDiplicatedIdToMapServiceUnitTest {
|
||||||
|
List<Animal> duplicatedIdList;
|
||||||
|
|
||||||
|
private ConvertListToMapService convertListService = new ConvertListToMapService();
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() {
|
||||||
|
|
||||||
|
this.duplicatedIdList = new ArrayList<>();
|
||||||
|
|
||||||
|
Animal cat = new Animal(1, "Cat");
|
||||||
|
duplicatedIdList.add(cat);
|
||||||
|
Animal dog = new Animal(2, "Dog");
|
||||||
|
duplicatedIdList.add(dog);
|
||||||
|
Animal pig = new Animal(3, "Pig");
|
||||||
|
duplicatedIdList.add(pig);
|
||||||
|
Animal cow = new Animal(4, "Cow");
|
||||||
|
duplicatedIdList.add(cow);
|
||||||
|
Animal goat = new Animal(4, "Goat");
|
||||||
|
duplicatedIdList.add(goat);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenADupIdList_whenConvertBeforeJava8_thenReturnMapWithRewrittenElement() {
|
||||||
|
|
||||||
|
Map<Integer, Animal> map = convertListService.convertListBeforeJava8(duplicatedIdList);
|
||||||
|
|
||||||
|
assertThat(map.values(), hasSize(4));
|
||||||
|
assertThat(map.values(), hasItem(duplicatedIdList.get(4)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenADupIdList_whenConvertWithApacheCommons_thenReturnMapWithRewrittenElement() {
|
||||||
|
|
||||||
|
Map<Integer, Animal> map = convertListService.convertListWithApacheCommons(duplicatedIdList);
|
||||||
|
|
||||||
|
assertThat(map.values(), hasSize(4));
|
||||||
|
assertThat(map.values(), hasItem(duplicatedIdList.get(4)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalStateException.class)
|
||||||
|
public void givenADupIdList_whenConvertAfterJava8_thenException() {
|
||||||
|
|
||||||
|
convertListService.convertListAfterJava8(duplicatedIdList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void givenADupIdList_whenConvertWithGuava_thenException() {
|
||||||
|
|
||||||
|
convertListService.convertListWithGuava(duplicatedIdList);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -50,22 +50,9 @@ public class HashSetInitalizingUnitTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingJava8_usingCollectOnStream_thenCorrectSize() {
|
public void whenUsingJava8_usingCollectOnStream_thenCorrectSize() {
|
||||||
Set<String> set = Stream.of("a", "b", "c").collect(Collectors.toSet());
|
Set<String> set = Stream.of("a", "b", "c").collect(Collectors.toCollection(HashSet::new));
|
||||||
assertEquals(3, set.size());
|
assertEquals(3, set.size());
|
||||||
}
|
}
|
||||||
@Test
|
|
||||||
public void whenUsingJava8_fromStringArray_thenCorrectSize() {
|
|
||||||
String[] stringArray = {"a","b","c"};
|
|
||||||
Set<String> set = Arrays.stream(stringArray).collect(Collectors.toCollection(HashSet::new));
|
|
||||||
assertEquals(3, set.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Requires Java9 - uncomment if you are using Java 9 or higher
|
|
||||||
/*@Test
|
|
||||||
public void whenUsingJava9_usingCollectOnStream_thenCorrectSize() {
|
|
||||||
Set set = Set.of("a", "b", "c");
|
|
||||||
assertEquals(3, set.size());
|
|
||||||
}*/
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingGoogleGuava_createMutableSet_thenCorrectSize() {
|
public void whenUsingGoogleGuava_createMutableSet_thenCorrectSize() {
|
||||||
@ -78,4 +65,6 @@ public class HashSetInitalizingUnitTest {
|
|||||||
Set<String> set = ImmutableSet.of("a", "b", "c");
|
Set<String> set = ImmutableSet.of("a", "b", "c");
|
||||||
assertEquals(3, set.size());
|
assertEquals(3, set.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>core-java</artifactId>
|
<artifactId>core-java</artifactId>
|
||||||
@ -73,9 +73,10 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.assertj</groupId>
|
<groupId>org.assertj</groupId>
|
||||||
<artifactId>assertj-core</artifactId>
|
<artifactId>assertj-core</artifactId>
|
||||||
<version>${assertj.version}</version>
|
<version>${assertj-core.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>commons-codec</groupId>
|
<groupId>commons-codec</groupId>
|
||||||
<artifactId>commons-codec</artifactId>
|
<artifactId>commons-codec</artifactId>
|
||||||
@ -146,6 +147,32 @@
|
|||||||
<artifactId>icu4j</artifactId>
|
<artifactId>icu4j</artifactId>
|
||||||
<version>${icu4j.version}</version>
|
<version>${icu4j.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- Mime Type Resolution Libraries -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.tika</groupId>
|
||||||
|
<artifactId>tika-core</artifactId>
|
||||||
|
<version>${tika.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.sf.jmimemagic</groupId>
|
||||||
|
<artifactId>jmimemagic</artifactId>
|
||||||
|
<version>${jmime-magic.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-dbcp2</artifactId>
|
||||||
|
<version>${commons-dbcp2.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.zaxxer</groupId>
|
||||||
|
<artifactId>HikariCP</artifactId>
|
||||||
|
<version>${HikariCP.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.mchange</groupId>
|
||||||
|
<artifactId>c3p0</artifactId>
|
||||||
|
<version>${c3p0.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@ -301,7 +328,7 @@
|
|||||||
</arguments>
|
</arguments>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-javadoc-plugin</artifactId>
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
@ -348,6 +375,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.codehaus.mojo</groupId>
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
<artifactId>exec-maven-plugin</artifactId>
|
<artifactId>exec-maven-plugin</artifactId>
|
||||||
|
<version>${exec-maven-plugin.version}</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<id>run-benchmarks</id>
|
<id>run-benchmarks</id>
|
||||||
@ -375,6 +403,7 @@
|
|||||||
</profiles>
|
</profiles>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
|
||||||
<!-- marshalling -->
|
<!-- marshalling -->
|
||||||
<jackson.version>2.8.5</jackson.version>
|
<jackson.version>2.8.5</jackson.version>
|
||||||
<gson.version>2.8.2</gson.version>
|
<gson.version>2.8.2</gson.version>
|
||||||
@ -395,16 +424,23 @@
|
|||||||
<vavr.version>0.9.0</vavr.version>
|
<vavr.version>0.9.0</vavr.version>
|
||||||
|
|
||||||
<!-- testing -->
|
<!-- testing -->
|
||||||
<assertj.version>3.6.1</assertj.version>
|
<assertj-core.version>3.10.0</assertj-core.version>
|
||||||
|
|
||||||
|
<!-- JDBC pooling frameworks -->
|
||||||
|
<commons-dbcp2.version>2.4.0</commons-dbcp2.version>
|
||||||
|
<HikariCP.version>3.2.0</HikariCP.version>
|
||||||
|
<c3p0.version>0.9.5.2</c3p0.version>
|
||||||
|
|
||||||
<!-- maven plugins -->
|
<!-- maven plugins -->
|
||||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||||
<springframework.spring-web.version>4.3.4.RELEASE</springframework.spring-web.version>
|
<springframework.spring-web.version>4.3.4.RELEASE</springframework.spring-web.version>
|
||||||
<springframework.boot.spring-boot-starter.version>1.5.8.RELEASE</springframework.boot.spring-boot-starter.version>
|
<springframework.boot.spring-boot-starter.version>1.5.8.RELEASE</springframework.boot.spring-boot-starter.version>
|
||||||
|
|
||||||
<javamoney.moneta.version>1.1</javamoney.moneta.version>
|
<javamoney.moneta.version>1.1</javamoney.moneta.version>
|
||||||
<h2database.version>1.4.197</h2database.version>
|
<h2database.version>1.4.197</h2database.version>
|
||||||
<esapi.version>2.1.0.1</esapi.version>
|
<esapi.version>2.1.0.1</esapi.version>
|
||||||
<jmh-core.version>1.19</jmh-core.version>
|
<jmh-core.version>1.19</jmh-core.version>
|
||||||
|
|
||||||
<jmh-generator-annprocess.version>1.19</jmh-generator-annprocess.version>
|
<jmh-generator-annprocess.version>1.19</jmh-generator-annprocess.version>
|
||||||
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
|
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
|
||||||
<javax.mail.version>1.5.0-b01</javax.mail.version>
|
<javax.mail.version>1.5.0-b01</javax.mail.version>
|
||||||
@ -412,7 +448,11 @@
|
|||||||
<onejar-maven-plugin.version>1.4.4</onejar-maven-plugin.version>
|
<onejar-maven-plugin.version>1.4.4</onejar-maven-plugin.version>
|
||||||
<maven-shade-plugin.version>3.1.1</maven-shade-plugin.version>
|
<maven-shade-plugin.version>3.1.1</maven-shade-plugin.version>
|
||||||
<spring-boot-maven-plugin.version>2.0.3.RELEASE</spring-boot-maven-plugin.version>
|
<spring-boot-maven-plugin.version>2.0.3.RELEASE</spring-boot-maven-plugin.version>
|
||||||
|
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
|
||||||
<icu4j.version>61.1</icu4j.version>
|
<icu4j.version>61.1</icu4j.version>
|
||||||
|
<!-- Mime Type Libraries -->
|
||||||
|
<tika.version>1.18</tika.version>
|
||||||
|
<jmime-magic.version>0.1.5</jmime-magic.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -0,0 +1,87 @@
|
|||||||
|
package com.baeldung.connectionpool.connectionpools;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class BasicConnectionPool implements ConnectionPool {
|
||||||
|
|
||||||
|
private final String url;
|
||||||
|
private final String user;
|
||||||
|
private final String password;
|
||||||
|
private final List<Connection> connectionPool;
|
||||||
|
private final List<Connection> usedConnections = new ArrayList<>();
|
||||||
|
private static final int INITIAL_POOL_SIZE = 10;
|
||||||
|
private final int MAX_POOL_SIZE = 20;
|
||||||
|
|
||||||
|
public static BasicConnectionPool create(String url, String user, String password) throws SQLException {
|
||||||
|
List<Connection> pool = new ArrayList<>(INITIAL_POOL_SIZE);
|
||||||
|
for (int i = 0; i < INITIAL_POOL_SIZE; i++) {
|
||||||
|
pool.add(createConnection(url, user, password));
|
||||||
|
}
|
||||||
|
return new BasicConnectionPool(url, user, password, pool);
|
||||||
|
}
|
||||||
|
|
||||||
|
private BasicConnectionPool(String url, String user, String password, List<Connection> connectionPool) {
|
||||||
|
this.url = url;
|
||||||
|
this.user = user;
|
||||||
|
this.password = password;
|
||||||
|
this.connectionPool = connectionPool;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Connection getConnection() throws SQLException {
|
||||||
|
if (connectionPool.size() == 0) {
|
||||||
|
if (usedConnections.size() < MAX_POOL_SIZE) {
|
||||||
|
connectionPool.add(createConnection(url, user, password));
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("Maximum pool size reached, no available connections!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Connection connection = connectionPool.remove(connectionPool.size() - 1);
|
||||||
|
usedConnections.add(connection);
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean releaseConnection(Connection connection) {
|
||||||
|
connectionPool.add(connection);
|
||||||
|
return usedConnections.remove(connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Connection createConnection(String url, String user, String password) throws SQLException {
|
||||||
|
return DriverManager.getConnection(url, user, password);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSize() {
|
||||||
|
return connectionPool.size() + usedConnections.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getUrl() {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getUser() {
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void shutdown() throws SQLException {
|
||||||
|
for (Connection c : usedConnections) {
|
||||||
|
this.releaseConnection(c);
|
||||||
|
}
|
||||||
|
for (Connection c : connectionPool) {
|
||||||
|
c.close();
|
||||||
|
}
|
||||||
|
connectionPool.clear();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.baeldung.connectionpool.connectionpools;
|
||||||
|
|
||||||
|
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
||||||
|
import java.beans.PropertyVetoException;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class C3poDataSource {
|
||||||
|
|
||||||
|
private static final ComboPooledDataSource cpds = new ComboPooledDataSource();
|
||||||
|
|
||||||
|
static {
|
||||||
|
try {
|
||||||
|
cpds.setDriverClass("org.h2.Driver");
|
||||||
|
cpds.setJdbcUrl("jdbc:h2:mem:test");
|
||||||
|
cpds.setUser("user");
|
||||||
|
cpds.setPassword("password");
|
||||||
|
} catch (PropertyVetoException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Connection getConnection() throws SQLException {
|
||||||
|
return cpds.getConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
private C3poDataSource(){}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.baeldung.connectionpool.connectionpools;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface ConnectionPool {
|
||||||
|
|
||||||
|
Connection getConnection() throws SQLException;
|
||||||
|
|
||||||
|
boolean releaseConnection(Connection connection);
|
||||||
|
|
||||||
|
String getUrl();
|
||||||
|
|
||||||
|
String getUser();
|
||||||
|
|
||||||
|
String getPassword();
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.baeldung.connectionpool.connectionpools;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import org.apache.commons.dbcp2.BasicDataSource;
|
||||||
|
|
||||||
|
public class DBCPDataSource {
|
||||||
|
|
||||||
|
private static final BasicDataSource ds = new BasicDataSource();
|
||||||
|
|
||||||
|
static {
|
||||||
|
ds.setUrl("jdbc:h2:mem:test");
|
||||||
|
ds.setUsername("user");
|
||||||
|
ds.setPassword("password");
|
||||||
|
ds.setMinIdle(5);
|
||||||
|
ds.setMaxIdle(10);
|
||||||
|
ds.setMaxOpenPreparedStatements(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Connection getConnection() throws SQLException {
|
||||||
|
return ds.getConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
private DBCPDataSource(){}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.baeldung.connectionpool.connectionpools;
|
||||||
|
|
||||||
|
import com.zaxxer.hikari.HikariConfig;
|
||||||
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class HikariCPDataSource {
|
||||||
|
|
||||||
|
private static final HikariConfig config = new HikariConfig();
|
||||||
|
private static final HikariDataSource ds;
|
||||||
|
|
||||||
|
static {
|
||||||
|
config.setJdbcUrl("jdbc:h2:mem:test");
|
||||||
|
config.setUsername("user");
|
||||||
|
config.setPassword("password");
|
||||||
|
config.addDataSourceProperty("cachePrepStmts", "true");
|
||||||
|
config.addDataSourceProperty("prepStmtCacheSize", "250");
|
||||||
|
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
|
||||||
|
ds = new HikariDataSource(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Connection getConnection() throws SQLException {
|
||||||
|
return ds.getConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
private HikariCPDataSource(){}
|
||||||
|
}
|
BIN
core-java/src/main/resources/product.png
Normal file
BIN
core-java/src/main/resources/product.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 54 KiB |
@ -0,0 +1,69 @@
|
|||||||
|
package com.baeldung.connectionpool;
|
||||||
|
|
||||||
|
import com.baeldung.connectionpool.connectionpools.BasicConnectionPool;
|
||||||
|
import com.baeldung.connectionpool.connectionpools.ConnectionPool;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class BasicConnectionPoolUnitTest {
|
||||||
|
|
||||||
|
private static ConnectionPool connectionPool;
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setUpBasicConnectionPoolInstance() throws SQLException {
|
||||||
|
connectionPool = BasicConnectionPool.create("jdbc:h2:mem:test", "user", "password");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBasicConnectionPoolInstance_whenCalledgetConnection_thenCorrect() throws Exception {
|
||||||
|
assertTrue(connectionPool.getConnection().isValid(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBasicConnectionPoolInstance_whenCalledreleaseConnection_thenCorrect() throws Exception {
|
||||||
|
Connection connection = connectionPool.getConnection();
|
||||||
|
assertThat(connectionPool.releaseConnection(connection)).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBasicConnectionPoolInstance_whenCalledgetUrl_thenCorrect() {
|
||||||
|
assertThat(connectionPool.getUrl()).isEqualTo("jdbc:h2:mem:test");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBasicConnectionPoolInstance_whenCalledgetUser_thenCorrect() {
|
||||||
|
assertThat(connectionPool.getUser()).isEqualTo("user");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBasicConnectionPoolInstance_whenCalledgetPassword_thenCorrect() {
|
||||||
|
assertThat(connectionPool.getPassword()).isEqualTo("password");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = RuntimeException.class)
|
||||||
|
public void givenBasicConnectionPoolInstance_whenAskedForMoreThanMax_thenError() throws Exception {
|
||||||
|
// this test needs to be independent so it doesn't share the same connection pool as other tests
|
||||||
|
ConnectionPool cp = BasicConnectionPool.create("jdbc:h2:mem:test", "user", "password");
|
||||||
|
final int MAX_POOL_SIZE = 20;
|
||||||
|
for (int i = 0; i < MAX_POOL_SIZE + 1; i++) {
|
||||||
|
cp.getConnection();
|
||||||
|
}
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBasicConnectionPoolInstance_whenSutdown_thenEmpty() throws Exception {
|
||||||
|
ConnectionPool cp = BasicConnectionPool.create("jdbc:h2:mem:test", "user", "password");
|
||||||
|
assertThat(((BasicConnectionPool)cp).getSize()).isEqualTo(10);
|
||||||
|
|
||||||
|
((BasicConnectionPool) cp).shutdown();
|
||||||
|
assertThat(((BasicConnectionPool)cp).getSize()).isEqualTo(0);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.connectionpool;
|
||||||
|
|
||||||
|
import com.baeldung.connectionpool.connectionpools.C3poDataSource;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class C3poDataSourceUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenC3poDataSourceClass_whenCalledgetConnection_thenCorrect() throws SQLException {
|
||||||
|
assertTrue(C3poDataSource.getConnection().isValid(1));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.connectionpool;
|
||||||
|
|
||||||
|
import com.baeldung.connectionpool.connectionpools.DBCPDataSource;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class DBCPDataSourceUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenDBCPDataSourceClass_whenCalledgetConnection_thenCorrect() throws SQLException {
|
||||||
|
assertTrue(DBCPDataSource.getConnection().isValid(1));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.connectionpool;
|
||||||
|
|
||||||
|
import com.baeldung.connectionpool.connectionpools.HikariCPDataSource;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class HikariCPDataSourceUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenHikariDataSourceClass_whenCalledgetConnection_thenCorrect() throws SQLException {
|
||||||
|
assertTrue(HikariCPDataSource.getConnection().isValid(1));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,131 @@
|
|||||||
|
package com.baeldung.java.mimetype;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.FileNameMap;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URLConnection;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
import javax.activation.MimetypesFileTypeMap;
|
||||||
|
|
||||||
|
import org.apache.tika.Tika;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import net.sf.jmimemagic.Magic;
|
||||||
|
import net.sf.jmimemagic.MagicException;
|
||||||
|
import net.sf.jmimemagic.MagicMatch;
|
||||||
|
import net.sf.jmimemagic.MagicMatchNotFoundException;
|
||||||
|
import net.sf.jmimemagic.MagicParseException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test class demonstrating various strategies to resolve MIME type of a file.
|
||||||
|
* @author tritty
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class MimeTypeUnitTest {
|
||||||
|
/**
|
||||||
|
* Expected Ouput.
|
||||||
|
*/
|
||||||
|
public static final String PNG_EXT = "image/png";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The location of the file.
|
||||||
|
*/
|
||||||
|
public static final String FILE_LOC = "src/test/resources/product.png";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method, demonstrating usage in Java 7.
|
||||||
|
*
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void whenUsingJava7_thenSuccess() throws IOException {
|
||||||
|
final Path path = new File(FILE_LOC).toPath();
|
||||||
|
final String mimeType = Files.probeContentType(path);
|
||||||
|
assertEquals(mimeType, PNG_EXT);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method demonstrating the usage of URLConnection to resolve MIME type.
|
||||||
|
*
|
||||||
|
* @throws MalformedURLException
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void whenUsingGetContentType_thenSuccess() throws MalformedURLException, IOException {
|
||||||
|
final File file = new File(FILE_LOC);
|
||||||
|
final URLConnection connection = file.toURL()
|
||||||
|
.openConnection();
|
||||||
|
final String mimeType = connection.getContentType();
|
||||||
|
assertEquals(mimeType, PNG_EXT);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method demonstrating the usage of URLConnection to resolve MIME type.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void whenUsingGuessContentTypeFromName_thenSuccess() {
|
||||||
|
final File file = new File(FILE_LOC);
|
||||||
|
final String mimeType = URLConnection.guessContentTypeFromName(file.getName());
|
||||||
|
assertEquals(mimeType, PNG_EXT);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method demonstrating the usage of FileNameMap from URLConnection
|
||||||
|
* to resolve MIME type of a file.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void whenUsingGetFileNameMap_thenSuccess() {
|
||||||
|
final File file = new File(FILE_LOC);
|
||||||
|
final FileNameMap fileNameMap = URLConnection.getFileNameMap();
|
||||||
|
final String mimeType = fileNameMap.getContentTypeFor(file.getName());
|
||||||
|
assertEquals(mimeType, PNG_EXT);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method demonstrating the usage of MimeTypesFileTypeMap for resolution of
|
||||||
|
* MIME type.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void whenUsingMimeTypesFileTypeMap_thenSuccess() {
|
||||||
|
final File file = new File(FILE_LOC);
|
||||||
|
final MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap();
|
||||||
|
final String mimeType = fileTypeMap.getContentType(file.getName());
|
||||||
|
assertEquals(mimeType, PNG_EXT);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method demonstrating usage of jMimeMagic.
|
||||||
|
*
|
||||||
|
* @throws MagicParseException
|
||||||
|
* @throws MagicMatchNotFoundException
|
||||||
|
* @throws MagicException
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void whenUsingJmimeMagic_thenSuccess() throws MagicParseException, MagicMatchNotFoundException, MagicException {
|
||||||
|
final File file = new File(FILE_LOC);
|
||||||
|
final Magic magic = new Magic();
|
||||||
|
final MagicMatch match = magic.getMagicMatch(file, false);
|
||||||
|
assertEquals(match.getMimeType(), PNG_EXT);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method demonstrating usage of Apache Tika.
|
||||||
|
*
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void whenUsingTika_thenSuccess() throws IOException {
|
||||||
|
final File file = new File(FILE_LOC);
|
||||||
|
final Tika tika = new Tika();
|
||||||
|
final String mimeType = tika.detect(file);
|
||||||
|
assertEquals(mimeType, PNG_EXT);
|
||||||
|
}
|
||||||
|
}
|
1588
core-java/src/test/resources/META-INF/mime.types
Normal file
1588
core-java/src/test/resources/META-INF/mime.types
Normal file
File diff suppressed because it is too large
Load Diff
BIN
core-java/src/test/resources/product.png
Normal file
BIN
core-java/src/test/resources/product.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 54 KiB |
@ -60,7 +60,6 @@
|
|||||||
<groupId>org.jetbrains.kotlin</groupId>
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
<artifactId>kotlin-reflect</artifactId>
|
<artifactId>kotlin-reflect</artifactId>
|
||||||
<version>${kotlin-reflect.version}</version>
|
<version>${kotlin-reflect.version}</version>
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jetbrains.kotlinx</groupId>
|
<groupId>org.jetbrains.kotlinx</groupId>
|
||||||
@ -224,10 +223,11 @@
|
|||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<kotlin-maven-plugin.version>1.2.41</kotlin-maven-plugin.version>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<kotlin-test-junit.version>1.2.41</kotlin-test-junit.version>
|
<kotlin-maven-plugin.version>1.2.51</kotlin-maven-plugin.version>
|
||||||
<kotlin-stdlib.version>1.2.41</kotlin-stdlib.version>
|
<kotlin-test-junit.version>1.2.51</kotlin-test-junit.version>
|
||||||
<kotlin-reflect.version>1.2.41</kotlin-reflect.version>
|
<kotlin-stdlib.version>1.2.51</kotlin-stdlib.version>
|
||||||
|
<kotlin-reflect.version>1.2.51</kotlin-reflect.version>
|
||||||
<kotlinx.version>0.22.5</kotlinx.version>
|
<kotlinx.version>0.22.5</kotlinx.version>
|
||||||
<ktor.io.version>0.9.2</ktor.io.version>
|
<ktor.io.version>0.9.2</ktor.io.version>
|
||||||
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
|
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
|
||||||
|
@ -0,0 +1,30 @@
|
|||||||
|
package com.baeldung.kotlin.logging
|
||||||
|
|
||||||
|
import org.slf4j.Logger
|
||||||
|
|
||||||
|
open class LoggerAsExtensionOnAny {
|
||||||
|
val logger = logger()
|
||||||
|
|
||||||
|
fun log(s: String) {
|
||||||
|
logger().info(s)
|
||||||
|
logger.info(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ExtensionSubclass : LoggerAsExtensionOnAny()
|
||||||
|
|
||||||
|
fun <T : Any> T.logger(): Logger = getLogger(getClassForLogging(javaClass))
|
||||||
|
|
||||||
|
fun main(args: Array<String>) {
|
||||||
|
LoggerAsExtensionOnAny().log("test")
|
||||||
|
ExtensionSubclass().log("sub")
|
||||||
|
"foo".logger().info("foo")
|
||||||
|
1.logger().info("uh-oh!")
|
||||||
|
SomeOtherClass().logger()
|
||||||
|
}
|
||||||
|
|
||||||
|
class SomeOtherClass {
|
||||||
|
fun logger(): String {
|
||||||
|
return "foo"
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
package com.baeldung.kotlin.logging
|
||||||
|
|
||||||
|
import org.slf4j.Logger
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
|
||||||
|
interface Logging
|
||||||
|
|
||||||
|
inline fun <reified T : Logging> T.logger(): Logger =
|
||||||
|
//Wrong logger name!
|
||||||
|
//LoggerFactory.getLogger(javaClass.name + " w/interface")
|
||||||
|
LoggerFactory.getLogger(getClassForLogging(T::class.java).name + " w/interface")
|
||||||
|
|
||||||
|
open class LoggerAsExtensionOnMarkerInterface : Logging {
|
||||||
|
companion object : Logging {
|
||||||
|
val logger = logger()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun log(s: String) {
|
||||||
|
logger().info(s)
|
||||||
|
logger.info(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class MarkerExtensionSubclass : LoggerAsExtensionOnMarkerInterface()
|
||||||
|
|
||||||
|
fun main(args: Array<String>) {
|
||||||
|
LoggerAsExtensionOnMarkerInterface().log("test")
|
||||||
|
MarkerExtensionSubclass().log("sub")
|
||||||
|
"foo".logger().info("foo")
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.baeldung.kotlin.logging
|
||||||
|
|
||||||
|
open class LoggerAsProperty {
|
||||||
|
private val logger = getLogger(javaClass)
|
||||||
|
|
||||||
|
fun log(s: String) {
|
||||||
|
logger.info(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class PropertySubclass : LoggerAsProperty()
|
||||||
|
|
||||||
|
fun main(args: Array<String>) {
|
||||||
|
LoggerAsProperty().log("test")
|
||||||
|
PropertySubclass().log("sub")
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
package com.baeldung.kotlin.logging
|
||||||
|
|
||||||
|
import org.slf4j.Logger
|
||||||
|
import kotlin.properties.ReadOnlyProperty
|
||||||
|
import kotlin.reflect.KProperty
|
||||||
|
|
||||||
|
open class LoggerAsPropertyDelegate {
|
||||||
|
private val lazyLogger by lazyLogger()
|
||||||
|
protected val logger by LoggerDelegate()
|
||||||
|
private val logger2 = logger
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val lazyLoggerComp by lazyLogger()
|
||||||
|
private val loggerComp by LoggerDelegate()
|
||||||
|
}
|
||||||
|
|
||||||
|
open fun log(s: String) {
|
||||||
|
logger.info(s)
|
||||||
|
logger2.info(s)
|
||||||
|
lazyLogger.info(s)
|
||||||
|
loggerComp.info(s)
|
||||||
|
lazyLoggerComp.info(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class DelegateSubclass : LoggerAsPropertyDelegate() {
|
||||||
|
override fun log(s: String) {
|
||||||
|
logger.info("-- in sub")
|
||||||
|
super.log(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun lazyLogger(forClass: Class<*>): Lazy<Logger> =
|
||||||
|
lazy { getLogger(getClassForLogging(forClass)) }
|
||||||
|
|
||||||
|
fun <T : Any> T.lazyLogger(): Lazy<Logger> = lazyLogger(javaClass)
|
||||||
|
|
||||||
|
fun main(args: Array<String>) {
|
||||||
|
LoggerAsPropertyDelegate().log("test")
|
||||||
|
DelegateSubclass().log("sub")
|
||||||
|
}
|
||||||
|
|
||||||
|
class LoggerDelegate<in R : Any> : ReadOnlyProperty<R, Logger> {
|
||||||
|
override fun getValue(thisRef: R, property: KProperty<*>) =
|
||||||
|
getLogger(getClassForLogging(thisRef.javaClass))
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.baeldung.kotlin.logging
|
||||||
|
|
||||||
|
open class LoggerInCompanionObject {
|
||||||
|
companion object {
|
||||||
|
private val loggerWithExplicitClass = getLogger(LoggerInCompanionObject::class.java)
|
||||||
|
@Suppress("JAVA_CLASS_ON_COMPANION")
|
||||||
|
private val loggerWithWrongClass = getLogger(javaClass)
|
||||||
|
@Suppress("JAVA_CLASS_ON_COMPANION")
|
||||||
|
private val logger = getLogger(javaClass.enclosingClass)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun log(s: String) {
|
||||||
|
loggerWithExplicitClass.info(s)
|
||||||
|
loggerWithWrongClass.info(s)
|
||||||
|
logger.info(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
class Inner {
|
||||||
|
companion object {
|
||||||
|
private val loggerWithExplicitClass = getLogger(Inner::class.java)
|
||||||
|
@Suppress("JAVA_CLASS_ON_COMPANION")
|
||||||
|
@JvmStatic
|
||||||
|
private val loggerWithWrongClass = getLogger(javaClass)
|
||||||
|
@Suppress("JAVA_CLASS_ON_COMPANION")
|
||||||
|
@JvmStatic
|
||||||
|
private val logger = getLogger(javaClass.enclosingClass)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun log(s: String) {
|
||||||
|
loggerWithExplicitClass.info(s)
|
||||||
|
loggerWithWrongClass.info(s)
|
||||||
|
logger.info(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class CompanionSubclass : LoggerInCompanionObject()
|
||||||
|
|
||||||
|
fun main(args: Array<String>) {
|
||||||
|
LoggerInCompanionObject().log("test")
|
||||||
|
LoggerInCompanionObject.Inner().log("test")
|
||||||
|
CompanionSubclass().log("sub")
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.baeldung.kotlin.logging
|
||||||
|
|
||||||
|
import org.slf4j.Logger
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
import kotlin.reflect.full.companionObject
|
||||||
|
|
||||||
|
fun getLogger(forClass: Class<*>): Logger = LoggerFactory.getLogger(forClass)
|
||||||
|
|
||||||
|
fun <T : Any> getClassForLogging(javaClass: Class<T>): Class<*> {
|
||||||
|
return javaClass.enclosingClass?.takeIf {
|
||||||
|
it.kotlin.companionObject?.java == javaClass
|
||||||
|
} ?: javaClass
|
||||||
|
}
|
113
geotools/pom.xml
113
geotools/pom.xml
@ -1,61 +1,62 @@
|
|||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
<modelVersion>4.0.0</modelVersion>
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<groupId>com.baeldung</groupId>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>geotools</artifactId>
|
<artifactId>geotools</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<name>geotools</name>
|
<name>geotools</name>
|
||||||
<url>http://maven.apache.org</url>
|
<url>http://maven.apache.org</url>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-modules</artifactId>
|
<artifactId>parent-modules</artifactId>
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.geotools</groupId>
|
<groupId>org.geotools</groupId>
|
||||||
<artifactId>gt-shapefile</artifactId>
|
<artifactId>gt-shapefile</artifactId>
|
||||||
<version>${geotools-shapefile.version}</version>
|
<version>${geotools-shapefile.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.geotools</groupId>
|
<groupId>org.geotools</groupId>
|
||||||
<artifactId>gt-epsg-hsql</artifactId>
|
<artifactId>gt-epsg-hsql</artifactId>
|
||||||
<version>${geotools.version}</version>
|
<version>${geotools.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.geotools</groupId>
|
<groupId>org.geotools</groupId>
|
||||||
<artifactId>gt-swing</artifactId>
|
<artifactId>gt-swing</artifactId>
|
||||||
<version>${geotools-swing.version}</version>
|
<version>${geotools-swing.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<repositories>
|
<repositories>
|
||||||
<repository>
|
<repository>
|
||||||
<id>maven2-repository.dev.java.net</id>
|
<id>maven2-repository.dev.java.net</id>
|
||||||
<name>Java.net repository</name>
|
<name>Java.net repository</name>
|
||||||
<url>http://download.java.net/maven/2</url>
|
<url>http://download.java.net/maven/2</url>
|
||||||
</repository>
|
</repository>
|
||||||
<repository>
|
<repository>
|
||||||
<id>osgeo</id>
|
<id>osgeo</id>
|
||||||
<name>Open Source Geospatial Foundation Repository</name>
|
<name>Open Source Geospatial Foundation Repository</name>
|
||||||
<url>http://download.osgeo.org/webdav/geotools/</url>
|
<url>http://download.osgeo.org/webdav/geotools/</url>
|
||||||
</repository>
|
</repository>
|
||||||
<repository>
|
<repository>
|
||||||
<snapshots>
|
<snapshots>
|
||||||
<enabled>true</enabled>
|
<enabled>true</enabled>
|
||||||
</snapshots>
|
</snapshots>
|
||||||
<id>opengeo</id>
|
<id>opengeo</id>
|
||||||
<name>OpenGeo Maven Repository</name>
|
<name>OpenGeo Maven Repository</name>
|
||||||
<url>http://repo.opengeo.org</url>
|
<url>http://repo.opengeo.org</url>
|
||||||
</repository>
|
</repository>
|
||||||
</repositories>
|
</repositories>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<geotools.version>15.2</geotools.version>
|
||||||
|
<geotools-swing.version>15.2</geotools-swing.version>
|
||||||
|
<geotools-shapefile.version>15.2</geotools-shapefile.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
<properties>
|
|
||||||
<geotools.version>15.2</geotools.version>
|
|
||||||
<geotools-swing.version>15.2</geotools-swing.version>
|
|
||||||
<geotools-shapefile.version>15.2</geotools-shapefile.version>
|
|
||||||
</properties>
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -5,8 +5,6 @@
|
|||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>hibernate5</artifactId>
|
<artifactId>hibernate5</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<name>hibernate5</name>
|
|
||||||
<url>http://maven.apache.org</url>
|
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
@ -59,9 +57,6 @@
|
|||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
|
||||||
<!-- Maven plugins -->
|
|
||||||
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
|
|
||||||
<hibernate.version>5.3.2.Final</hibernate.version>
|
<hibernate.version>5.3.2.Final</hibernate.version>
|
||||||
<mysql.version>6.0.6</mysql.version>
|
<mysql.version>6.0.6</mysql.version>
|
||||||
<mariaDB4j.version>2.2.3</mariaDB4j.version>
|
<mariaDB4j.version>2.2.3</mariaDB4j.version>
|
||||||
|
13
hibernate5/src/main/resources/logback.xml
Normal file
13
hibernate5/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern> %date [%thread] %-5level %logger{36} - %message%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
@ -347,7 +347,7 @@
|
|||||||
</plugins>
|
</plugins>
|
||||||
</pluginManagement>
|
</pluginManagement>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<!-- <plugin>
|
||||||
<groupId>com.github.ekryd.sortpom</groupId>
|
<groupId>com.github.ekryd.sortpom</groupId>
|
||||||
<artifactId>sortpom-maven-plugin</artifactId>
|
<artifactId>sortpom-maven-plugin</artifactId>
|
||||||
<version>${sortpom-maven-plugin.version}</version>
|
<version>${sortpom-maven-plugin.version}</version>
|
||||||
@ -367,7 +367,7 @@
|
|||||||
<keepBlankLines>true</keepBlankLines>
|
<keepBlankLines>true</keepBlankLines>
|
||||||
<expandEmptyElements>false</expandEmptyElements>
|
<expandEmptyElements>false</expandEmptyElements>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin> -->
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>com.spotify</groupId>
|
<groupId>com.spotify</groupId>
|
||||||
<artifactId>docker-maven-plugin</artifactId>
|
<artifactId>docker-maven-plugin</artifactId>
|
||||||
|
88
jnosql/jnosql-artemis/pom.xml
Normal file
88
jnosql/jnosql-artemis/pom.xml
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung.jnosql</groupId>
|
||||||
|
<artifactId>jnosql</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>jnosql-artemis</artifactId>
|
||||||
|
<packaging>war</packaging>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<liberty-maven-plugin.version>2.4.2</liberty-maven-plugin.version>
|
||||||
|
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>${artifactId}</finalName>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>net.wasdev.wlp.maven.plugins</groupId>
|
||||||
|
<artifactId>liberty-maven-plugin</artifactId>
|
||||||
|
<version>${liberty-maven-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<assemblyArtifact>
|
||||||
|
<groupId>io.openliberty</groupId>
|
||||||
|
<artifactId>openliberty-webProfile8</artifactId>
|
||||||
|
<version>RELEASE</version>
|
||||||
|
<type>zip</type>
|
||||||
|
</assemblyArtifact>
|
||||||
|
<installAppPackages>project</installAppPackages>
|
||||||
|
<looseApplication>true</looseApplication>
|
||||||
|
<configFile>src/main/liberty/config/server.xml</configFile>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>install-server</id>
|
||||||
|
<phase>prepare-package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>install-server</goal>
|
||||||
|
<goal>create-server</goal>
|
||||||
|
<goal>install-feature</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
<execution>
|
||||||
|
<id>install-apps</id>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>install-apps</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax</groupId>
|
||||||
|
<artifactId>javaee-web-api</artifactId>
|
||||||
|
<version>8.0</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jnosql.artemis</groupId>
|
||||||
|
<artifactId>artemis-configuration</artifactId>
|
||||||
|
<version>${jnosql.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jnosql.artemis</groupId>
|
||||||
|
<artifactId>artemis-document</artifactId>
|
||||||
|
<version>${jnosql.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jnosql.diana</groupId>
|
||||||
|
<artifactId>mongodb-driver</artifactId>
|
||||||
|
<version>${jnosql.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.baeldung.jnosql.artemis;
|
||||||
|
|
||||||
|
import javax.enterprise.context.Initialized;
|
||||||
|
import javax.enterprise.event.Observes;
|
||||||
|
import javax.ws.rs.ApplicationPath;
|
||||||
|
import javax.ws.rs.core.Application;
|
||||||
|
|
||||||
|
@ApplicationPath("")
|
||||||
|
public class AppConfig extends Application {
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
package com.baeldung.jnosql.artemis;
|
||||||
|
|
||||||
|
import de.flapdoodle.embed.mongo.MongodExecutable;
|
||||||
|
import de.flapdoodle.embed.mongo.MongodProcess;
|
||||||
|
import de.flapdoodle.embed.mongo.MongodStarter;
|
||||||
|
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
|
||||||
|
import de.flapdoodle.embed.mongo.config.Net;
|
||||||
|
import de.flapdoodle.embed.mongo.distribution.Version;
|
||||||
|
import de.flapdoodle.embed.process.runtime.Network;
|
||||||
|
|
||||||
|
import javax.enterprise.context.ApplicationScoped;
|
||||||
|
import javax.enterprise.context.Destroyed;
|
||||||
|
import javax.enterprise.context.Initialized;
|
||||||
|
import javax.enterprise.event.Observes;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@ApplicationScoped
|
||||||
|
public class EmbeddedMongoDBSetup {
|
||||||
|
|
||||||
|
private static final String MONGODB_HOST = "localhost";
|
||||||
|
private static final int MONGODB_PORT = 27019;
|
||||||
|
|
||||||
|
private static final MongodStarter starter = MongodStarter.getDefaultInstance();
|
||||||
|
private static MongodExecutable _mongodExe;
|
||||||
|
private static MongodProcess _mongod;
|
||||||
|
|
||||||
|
public void init(@Observes @Initialized(ApplicationScoped.class) Object init) {
|
||||||
|
try {
|
||||||
|
System.out.println("Starting Embedded MongoDB");
|
||||||
|
initdb();
|
||||||
|
System.out.println("Embedded MongoDB started");
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Embedded MongoDB starting error !!");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initdb() throws IOException {
|
||||||
|
_mongodExe = starter.prepare(new MongodConfigBuilder()
|
||||||
|
.version(Version.Main.DEVELOPMENT)
|
||||||
|
.net(new Net(MONGODB_HOST, MONGODB_PORT, Network.localhostIsIPv6()))
|
||||||
|
.build());
|
||||||
|
_mongod = _mongodExe.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void destroy(@Observes @Destroyed(ApplicationScoped.class) Object init) {
|
||||||
|
System.out.println("Stopping Embedded MongoDB");
|
||||||
|
_mongod.stop();
|
||||||
|
_mongodExe.stop();
|
||||||
|
System.out.println("Embedded MongoDB stopped !");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.baeldung.jnosql.artemis;
|
||||||
|
|
||||||
|
import org.jnosql.artemis.ConfigurationUnit;
|
||||||
|
import org.jnosql.diana.api.document.DocumentCollectionManager;
|
||||||
|
import org.jnosql.diana.api.document.DocumentCollectionManagerFactory;
|
||||||
|
import org.jnosql.diana.mongodb.document.MongoDBDocumentCollectionManager;
|
||||||
|
import org.jnosql.diana.mongodb.document.MongoDBDocumentConfiguration;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import javax.enterprise.context.ApplicationScoped;
|
||||||
|
import javax.enterprise.inject.Disposes;
|
||||||
|
import javax.enterprise.inject.Produces;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
@ApplicationScoped
|
||||||
|
public class EntityManagerProducer {
|
||||||
|
|
||||||
|
private static final String DATABASE = "todos";
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@ConfigurationUnit(name = "document")
|
||||||
|
private DocumentCollectionManagerFactory<MongoDBDocumentCollectionManager> managerFactory;
|
||||||
|
|
||||||
|
@Produces
|
||||||
|
public DocumentCollectionManager getEntityManager() {
|
||||||
|
return managerFactory.get(DATABASE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close(@Disposes DocumentCollectionManager entityManager) {
|
||||||
|
entityManager.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
package com.baeldung.jnosql.artemis;
|
||||||
|
|
||||||
|
import com.baeldung.jnosql.artemis.qualifier.Repo;
|
||||||
|
import org.jnosql.artemis.Database;
|
||||||
|
import org.jnosql.artemis.DatabaseType;
|
||||||
|
|
||||||
|
import javax.enterprise.context.ApplicationScoped;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@ApplicationScoped
|
||||||
|
@Repo
|
||||||
|
public class RepositoryTodoManager implements TodoManager {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@Database(DatabaseType.DOCUMENT)
|
||||||
|
private TodoRepository repository;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Todo add(Todo todo) {
|
||||||
|
return repository.save(todo);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Todo get(String id) {
|
||||||
|
Optional<Todo> todo = repository.findById(id);
|
||||||
|
return todo.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Todo> getAll() {
|
||||||
|
return repository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delete(String id) {
|
||||||
|
repository.deleteById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
package com.baeldung.jnosql.artemis;
|
||||||
|
|
||||||
|
import com.baeldung.jnosql.artemis.qualifier.Template;
|
||||||
|
import org.jnosql.artemis.document.DocumentTemplate;
|
||||||
|
import org.jnosql.diana.api.document.DocumentQuery;
|
||||||
|
|
||||||
|
import javax.enterprise.context.ApplicationScoped;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.jnosql.diana.api.document.query.DocumentQueryBuilder.select;
|
||||||
|
|
||||||
|
@ApplicationScoped
|
||||||
|
@Template
|
||||||
|
public class TemplateTodoManager implements TodoManager {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private DocumentTemplate documentTemplate;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Todo add(Todo todo) {
|
||||||
|
return documentTemplate.insert(todo);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Todo get(String id) {
|
||||||
|
Optional<Todo> todo = documentTemplate.find(Todo.class, id);
|
||||||
|
return todo.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Todo> getAll() {
|
||||||
|
DocumentQuery query = select().from("Todo").build();
|
||||||
|
return documentTemplate.select(query);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delete(String id) {
|
||||||
|
documentTemplate.delete(Todo.class, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
package com.baeldung.jnosql.artemis;
|
||||||
|
|
||||||
|
import org.jnosql.artemis.Column;
|
||||||
|
import org.jnosql.artemis.Entity;
|
||||||
|
import org.jnosql.artemis.Id;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Todo implements Serializable {
|
||||||
|
|
||||||
|
@Id("id")
|
||||||
|
public String id;
|
||||||
|
@Column
|
||||||
|
public String name;
|
||||||
|
@Column
|
||||||
|
public String description;
|
||||||
|
|
||||||
|
public Todo() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Todo(String id, String name, String description) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDescription(String description) {
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.jnosql.artemis;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface TodoManager {
|
||||||
|
|
||||||
|
Todo add(Todo todo);
|
||||||
|
|
||||||
|
Todo get(String id);
|
||||||
|
|
||||||
|
List<Todo> getAll();
|
||||||
|
|
||||||
|
void delete(String id);
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.baeldung.jnosql.artemis;
|
||||||
|
|
||||||
|
import org.jnosql.artemis.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface TodoRepository extends Repository<Todo, String> {
|
||||||
|
List<Todo> findByName(String name);
|
||||||
|
List<Todo> findAll();
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
package com.baeldung.jnosql.artemis;
|
||||||
|
|
||||||
|
import com.baeldung.jnosql.artemis.qualifier.Repo;
|
||||||
|
import com.baeldung.jnosql.artemis.qualifier.Template;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.ws.rs.*;
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
import javax.ws.rs.core.Response;
|
||||||
|
import javax.ws.rs.core.UriBuilder;
|
||||||
|
|
||||||
|
@Path("todos")
|
||||||
|
public class TodoResource {
|
||||||
|
|
||||||
|
/*
|
||||||
|
Use eiher @Template or @Repo
|
||||||
|
*/
|
||||||
|
@Inject
|
||||||
|
@Template
|
||||||
|
//@Repo
|
||||||
|
private TodoManager todoManager;
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Path("")
|
||||||
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
public Response all() {
|
||||||
|
return Response.ok(todoManager.getAll()).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Path("{id}")
|
||||||
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
public Response get(@PathParam("id") String id) {
|
||||||
|
Todo todo = todoManager.get(id);
|
||||||
|
return Response.ok(todo).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@POST
|
||||||
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
|
public Response add(Todo todo) {
|
||||||
|
Todo savedTodo = todoManager.add(todo);
|
||||||
|
System.out.println(savedTodo.id);
|
||||||
|
return Response.created(
|
||||||
|
UriBuilder.fromResource(this.getClass()).path(String.valueOf(savedTodo.id)).build())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.baeldung.jnosql.artemis.qualifier;
|
||||||
|
|
||||||
|
import javax.inject.Qualifier;
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target({ElementType.FIELD, ElementType.TYPE})
|
||||||
|
@Qualifier
|
||||||
|
public @interface Repo {
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.baeldung.jnosql.artemis.qualifier;
|
||||||
|
|
||||||
|
import javax.inject.Qualifier;
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target({ElementType.FIELD, ElementType.TYPE})
|
||||||
|
@Qualifier
|
||||||
|
public @interface Template {
|
||||||
|
}
|
6
jnosql/jnosql-artemis/src/main/liberty/config/server.xml
Normal file
6
jnosql/jnosql-artemis/src/main/liberty/config/server.xml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<server description="OpenLiberty Server">
|
||||||
|
<featureManager>
|
||||||
|
<feature>webProfile-8.0</feature>
|
||||||
|
</featureManager>
|
||||||
|
<httpEndpoint httpPort="9080" httpsPort="9443" id="defaultHttpEndpoint" host="*"/>
|
||||||
|
</server>
|
@ -0,0 +1,6 @@
|
|||||||
|
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
|
||||||
|
http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
|
||||||
|
bean-discovery-mode="all">
|
||||||
|
</beans>
|
@ -0,0 +1,10 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"description": "The mongodb document configuration",
|
||||||
|
"name": "document",
|
||||||
|
"provider": "org.jnosql.diana.mongodb.document.MongoDBDocumentConfiguration",
|
||||||
|
"settings": {
|
||||||
|
"mongodb-server-host-1":"localhost:27019"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
10
jnosql/jnosql-artemis/src/main/webapp/WEB-INF/jnosql.json
Normal file
10
jnosql/jnosql-artemis/src/main/webapp/WEB-INF/jnosql.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"description": "The mongodb document configuration",
|
||||||
|
"name": "document",
|
||||||
|
"provider": "org.jnosql.diana.mongodb.document.MongoDBDocumentConfiguration",
|
||||||
|
"settings": {
|
||||||
|
"mongodb-server-host-1":"localhost:27019"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
93
jnosql/jnosql-diana/pom.xml
Normal file
93
jnosql/jnosql-diana/pom.xml
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung.jnosql</groupId>
|
||||||
|
<artifactId>jnosql</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>jnosql-diana</artifactId>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>exec-maven-plugin</artifactId>
|
||||||
|
<version>1.6.0</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>document</id>
|
||||||
|
<goals>
|
||||||
|
<goal>java</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<mainClass>com.baeldung.jnosql.diana.document.DocumentApp</mainClass>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
<execution>
|
||||||
|
<id>column</id>
|
||||||
|
<goals>
|
||||||
|
<goal>java</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<mainClass>com.baeldung.jnosql.diana.column.ColumnFamilyApp</mainClass>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
<execution>
|
||||||
|
<id>key</id>
|
||||||
|
<goals>
|
||||||
|
<goal>java</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<mainClass>com.baeldung.jnosql.diana.key.KeyValueApp</mainClass>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<!--NoSQL Document oriented-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jnosql.diana</groupId>
|
||||||
|
<artifactId>diana-document</artifactId>
|
||||||
|
<version>0.0.5</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jnosql.diana</groupId>
|
||||||
|
<artifactId>mongodb-driver</artifactId>
|
||||||
|
<version>0.0.5</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!--NoSQL Column oriented-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jnosql.diana</groupId>
|
||||||
|
<artifactId>diana-column</artifactId>
|
||||||
|
<version>0.0.5</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jnosql.diana</groupId>
|
||||||
|
<artifactId>cassandra-driver</artifactId>
|
||||||
|
<version>0.0.5</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!--NoSQL Key Value oriented-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jnosql.diana</groupId>
|
||||||
|
<artifactId>diana-key-value</artifactId>
|
||||||
|
<version>0.0.5</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jnosql.diana</groupId>
|
||||||
|
<artifactId>hazelcast-driver</artifactId>
|
||||||
|
<version>0.0.5</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,32 @@
|
|||||||
|
package com.baeldung.jnosql.diana.column;
|
||||||
|
|
||||||
|
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
|
||||||
|
import org.jnosql.diana.api.column.*;
|
||||||
|
import org.jnosql.diana.cassandra.column.CassandraConfiguration;
|
||||||
|
|
||||||
|
public class ColumnFamilyApp {
|
||||||
|
|
||||||
|
private static final String KEY_SPACE = "myKeySpace";
|
||||||
|
private static final String COLUMN_FAMILY = "books";
|
||||||
|
|
||||||
|
public static void main(String... args) throws Exception {
|
||||||
|
|
||||||
|
EmbeddedCassandraServerHelper.startEmbeddedCassandra();
|
||||||
|
|
||||||
|
ColumnConfiguration configuration = new CassandraConfiguration();
|
||||||
|
try(ColumnFamilyManagerFactory entityManagerFactory = configuration.get()) {
|
||||||
|
ColumnFamilyManager entityManager = entityManagerFactory.get(KEY_SPACE);
|
||||||
|
ColumnEntity columnEntity = ColumnEntity.of(COLUMN_FAMILY);
|
||||||
|
Column key = Columns.of("id", 10L);
|
||||||
|
Column name = Columns.of("name", "JNoSQL in Acion");
|
||||||
|
columnEntity.add(key);
|
||||||
|
columnEntity.add(name);
|
||||||
|
ColumnEntity saved = entityManager.insert(columnEntity);
|
||||||
|
System.out.println(saved);
|
||||||
|
}
|
||||||
|
|
||||||
|
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
|
||||||
|
EmbeddedCassandraServerHelper.stopEmbeddedCassandra();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,67 @@
|
|||||||
|
package com.baeldung.jnosql.diana.document;
|
||||||
|
|
||||||
|
import org.jnosql.diana.api.Settings;
|
||||||
|
import org.jnosql.diana.api.document.*;
|
||||||
|
import org.jnosql.diana.mongodb.document.MongoDBDocumentConfiguration;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static org.jnosql.diana.api.document.query.DocumentQueryBuilder.delete;
|
||||||
|
import static org.jnosql.diana.api.document.query.DocumentQueryBuilder.select;
|
||||||
|
|
||||||
|
public class DocumentApp {
|
||||||
|
|
||||||
|
private static final String DB_NAME = "my-db";
|
||||||
|
private static final String DOCUMENT_COLLECTION = "books";
|
||||||
|
|
||||||
|
public static final String KEY_NAME = "_id";
|
||||||
|
|
||||||
|
DocumentConfiguration configuration = new MongoDBDocumentConfiguration();
|
||||||
|
|
||||||
|
public static void main(String... args) throws Exception {
|
||||||
|
MongoDbInit.startMongoDb();
|
||||||
|
|
||||||
|
DocumentApp app = new DocumentApp();
|
||||||
|
app.process();
|
||||||
|
|
||||||
|
MongoDbInit.stopMongoDb();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void process() {
|
||||||
|
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
map.put("mongodb-server-host-1", "localhost:27017");
|
||||||
|
|
||||||
|
try (DocumentCollectionManagerFactory managerFactory = configuration.get(Settings.of(map));
|
||||||
|
DocumentCollectionManager manager = managerFactory.get(DB_NAME);) {
|
||||||
|
|
||||||
|
DocumentEntity documentEntity = DocumentEntity.of(DOCUMENT_COLLECTION);
|
||||||
|
documentEntity.add(Document.of(KEY_NAME, "100"));
|
||||||
|
documentEntity.add(Document.of("name", "JNoSQL in Action"));
|
||||||
|
documentEntity.add(Document.of("pages", 620));
|
||||||
|
|
||||||
|
//CREATE
|
||||||
|
DocumentEntity saved = manager.insert(documentEntity);
|
||||||
|
|
||||||
|
//READ
|
||||||
|
DocumentQuery query = select().from(DOCUMENT_COLLECTION).where(KEY_NAME).eq("100").build();
|
||||||
|
List<DocumentEntity> entities = manager.select(query);
|
||||||
|
System.out.println(entities.get(0));
|
||||||
|
|
||||||
|
//UPDATE
|
||||||
|
saved.add(Document.of("author", "baeldung"));
|
||||||
|
DocumentEntity updated = manager.update(saved);
|
||||||
|
System.out.println(updated);
|
||||||
|
|
||||||
|
//DELETE
|
||||||
|
DocumentDeleteQuery deleteQuery = delete().from(DOCUMENT_COLLECTION).where(KEY_NAME).eq("100").build();
|
||||||
|
manager.delete(deleteQuery);
|
||||||
|
|
||||||
|
List<DocumentEntity> documentEntityList = manager.select(select().from(DOCUMENT_COLLECTION).build());
|
||||||
|
System.out.println(documentEntityList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package com.baeldung.jnosql.diana.document;
|
||||||
|
|
||||||
|
import de.flapdoodle.embed.mongo.MongodExecutable;
|
||||||
|
import de.flapdoodle.embed.mongo.MongodProcess;
|
||||||
|
import de.flapdoodle.embed.mongo.MongodStarter;
|
||||||
|
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
|
||||||
|
import de.flapdoodle.embed.mongo.config.Net;
|
||||||
|
import de.flapdoodle.embed.mongo.distribution.Version;
|
||||||
|
import de.flapdoodle.embed.process.runtime.Network;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public abstract class MongoDbInit {
|
||||||
|
|
||||||
|
private static final MongodStarter starter = MongodStarter.getDefaultInstance();
|
||||||
|
private static MongodExecutable _mongodExe;
|
||||||
|
private static MongodProcess _mongod;
|
||||||
|
|
||||||
|
public static void startMongoDb() throws IOException {
|
||||||
|
_mongodExe = starter.prepare(new MongodConfigBuilder()
|
||||||
|
.version(Version.Main.DEVELOPMENT)
|
||||||
|
.net(new Net("localhost", 27017, Network.localhostIsIPv6()))
|
||||||
|
.build());
|
||||||
|
_mongod = _mongodExe.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void stopMongoDb(){
|
||||||
|
_mongod.stop();
|
||||||
|
_mongodExe.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,63 @@
|
|||||||
|
package com.baeldung.jnosql.diana.key;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class Book implements Serializable {
|
||||||
|
|
||||||
|
private String isbn;
|
||||||
|
private String name;
|
||||||
|
private String author;
|
||||||
|
private int pages;
|
||||||
|
|
||||||
|
public Book() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Book(String isbn, String name, String author, int pages) {
|
||||||
|
this.isbn = isbn;
|
||||||
|
this.name = name;
|
||||||
|
this.author = author;
|
||||||
|
this.pages = pages;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getIsbn() {
|
||||||
|
return isbn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIsbn(String isbn) {
|
||||||
|
this.isbn = isbn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAuthor() {
|
||||||
|
return author;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAuthor(String author) {
|
||||||
|
this.author = author;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPages() {
|
||||||
|
return pages;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPages(int pages) {
|
||||||
|
this.pages = pages;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Book{" +
|
||||||
|
"isbn='" + isbn + '\'' +
|
||||||
|
", name='" + name + '\'' +
|
||||||
|
", author='" + author + '\'' +
|
||||||
|
", pages=" + pages +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.baeldung.jnosql.diana.key;
|
||||||
|
|
||||||
|
import com.hazelcast.core.Hazelcast;
|
||||||
|
import org.jnosql.diana.api.Value;
|
||||||
|
import org.jnosql.diana.api.key.BucketManager;
|
||||||
|
import org.jnosql.diana.api.key.BucketManagerFactory;
|
||||||
|
import org.jnosql.diana.api.key.KeyValueConfiguration;
|
||||||
|
import org.jnosql.diana.api.key.KeyValueEntity;
|
||||||
|
import org.jnosql.diana.hazelcast.key.HazelcastKeyValueConfiguration;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public class KeyValueApp {
|
||||||
|
|
||||||
|
private static final String BUCKET_NAME = "books";
|
||||||
|
|
||||||
|
public static void main(String... args) throws Exception {
|
||||||
|
KeyValueConfiguration configuration = new HazelcastKeyValueConfiguration();
|
||||||
|
try (BucketManagerFactory managerFactory = configuration.get()) {
|
||||||
|
BucketManager manager = managerFactory.getBucketManager(BUCKET_NAME);
|
||||||
|
|
||||||
|
Book book = new Book("12345", "JNoSQL in Action", "baeldung", 420);
|
||||||
|
KeyValueEntity keyValueEntity = KeyValueEntity.of(book.getIsbn(), book);
|
||||||
|
manager.put(keyValueEntity);
|
||||||
|
|
||||||
|
Optional<Value> optionalValue = manager.get("12345");
|
||||||
|
Value value = optionalValue.get();
|
||||||
|
Book savedBook = value.get(Book.class);
|
||||||
|
System.out.println(savedBook);
|
||||||
|
}
|
||||||
|
Hazelcast.shutdownAll();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
cassandra-host-1=localhost
|
||||||
|
cassandra-port=9142
|
||||||
|
#cassandra-threads-number=2
|
||||||
|
cassandra-query-1=CREATE KEYSPACE IF NOT EXISTS myKeySpace WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3};
|
||||||
|
cassandra-query-2=CREATE COLUMNFAMILY IF NOT EXISTS myKeySpace.books (id bigint PRIMARY KEY, name text);
|
@ -0,0 +1 @@
|
|||||||
|
hazelcast-instanceName=hazelcast
|
@ -0,0 +1,2 @@
|
|||||||
|
#Define Host and Port
|
||||||
|
mongodb-server-host-1=localhost:27017
|
23
jnosql/pom.xml
Normal file
23
jnosql/pom.xml
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.baeldung.jnosql</groupId>
|
||||||
|
<artifactId>jnosql</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
<jnosql.version>0.0.5</jnosql.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<modules>
|
||||||
|
<module>jnosql-diana</module>
|
||||||
|
<module>jnosql-artemis</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
</project>
|
@ -1,2 +0,0 @@
|
|||||||
## Relevant articles:
|
|
||||||
- [The Order of Tests in JUnit](http://www.baeldung.com/junit-5-test-order)
|
|
@ -141,6 +141,13 @@
|
|||||||
<artifactId>hazelcast</artifactId>
|
<artifactId>hazelcast</artifactId>
|
||||||
<version>${hazelcast.version}</version>
|
<version>${hazelcast.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.googlecode.jmapper-framework</groupId>
|
||||||
|
<artifactId>jmapper-core</artifactId>
|
||||||
|
<version>${jmapper.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@ -275,6 +282,7 @@
|
|||||||
<datanucleus-maven-plugin.version>5.0.2</datanucleus-maven-plugin.version>
|
<datanucleus-maven-plugin.version>5.0.2</datanucleus-maven-plugin.version>
|
||||||
<datanucleus-xml.version>5.0.0-release</datanucleus-xml.version>
|
<datanucleus-xml.version>5.0.0-release</datanucleus-xml.version>
|
||||||
<datanucleus-jdo-query.version>5.0.4</datanucleus-jdo-query.version>
|
<datanucleus-jdo-query.version>5.0.4</datanucleus-jdo-query.version>
|
||||||
|
<jmapper.version>1.6.0.1</jmapper.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
56
libraries-data/src/main/java/com/baeldung/jmapper/User.java
Normal file
56
libraries-data/src/main/java/com/baeldung/jmapper/User.java
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package com.baeldung.jmapper;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
|
||||||
|
public class User {
|
||||||
|
|
||||||
|
private long id;
|
||||||
|
private String email;
|
||||||
|
private LocalDate birthDate;
|
||||||
|
|
||||||
|
// constructors
|
||||||
|
|
||||||
|
public User() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public User(long id, String email, LocalDate birthDate) {
|
||||||
|
super();
|
||||||
|
this.id = id;
|
||||||
|
this.email = email;
|
||||||
|
this.birthDate = birthDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
// getters and setters
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmail(String email) {
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDate getBirthDate() {
|
||||||
|
return birthDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBirthDate(LocalDate birthDate) {
|
||||||
|
this.birthDate = birthDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "User [id=" + id + ", email=" + email + ", birthDate=" + birthDate + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,69 @@
|
|||||||
|
package com.baeldung.jmapper;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.Period;
|
||||||
|
|
||||||
|
import com.googlecode.jmapper.annotations.JMap;
|
||||||
|
import com.googlecode.jmapper.annotations.JMapConversion;
|
||||||
|
|
||||||
|
public class UserDto {
|
||||||
|
|
||||||
|
@JMap
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
@JMap("email")
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
@JMap("birthDate")
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
@JMapConversion(from={"birthDate"}, to={"age"})
|
||||||
|
public int conversion(LocalDate birthDate){
|
||||||
|
return Period.between(birthDate, LocalDate.now()).getYears();
|
||||||
|
}
|
||||||
|
|
||||||
|
// constructors
|
||||||
|
|
||||||
|
public UserDto() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserDto(long id, String username, int age) {
|
||||||
|
super();
|
||||||
|
this.id = id;
|
||||||
|
this.username = username;
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
// getters and setters
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(int age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "UserDto [id=" + id + ", username=" + username + ", age=" + age + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
package com.baeldung.jmapper;
|
||||||
|
|
||||||
|
import com.googlecode.jmapper.annotations.JGlobalMap;
|
||||||
|
|
||||||
|
@JGlobalMap
|
||||||
|
public class UserDto1 {
|
||||||
|
|
||||||
|
private long id;
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
|
||||||
|
// constructors
|
||||||
|
|
||||||
|
public UserDto1() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserDto1(long id, String email) {
|
||||||
|
super();
|
||||||
|
this.id = id;
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
// getters and setters
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmail(String email) {
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "UserDto [id=" + id + ", email=" + email + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package com.baeldung.jmapper.relational;
|
||||||
|
|
||||||
|
import com.googlecode.jmapper.annotations.JMap;
|
||||||
|
|
||||||
|
|
||||||
|
public class User {
|
||||||
|
|
||||||
|
@JMap(classes = {UserDto1.class, UserDto2.class})
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
@JMap(attributes = {"username", "email"}, classes = {UserDto1.class, UserDto2.class})
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
// constructors
|
||||||
|
|
||||||
|
public User() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public User(long id, String email) {
|
||||||
|
super();
|
||||||
|
this.id = id;
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
// getters and setters
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmail(String email) {
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "User [id=" + id + ", email=" + email + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.baeldung.jmapper.relational;
|
||||||
|
|
||||||
|
|
||||||
|
public class UserDto1 {
|
||||||
|
|
||||||
|
private long id;
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
// constructors
|
||||||
|
|
||||||
|
public UserDto1() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserDto1(long id, String username) {
|
||||||
|
super();
|
||||||
|
this.id = id;
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
// getters and setters
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "UserDto [id=" + id + ", username=" + username + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.baeldung.jmapper.relational;
|
||||||
|
|
||||||
|
|
||||||
|
public class UserDto2 {
|
||||||
|
|
||||||
|
private long id;
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
// constructors
|
||||||
|
|
||||||
|
public UserDto2() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserDto2(long id, String email) {
|
||||||
|
super();
|
||||||
|
this.id = id;
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
// getters and setters
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmail(String email) {
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "UserDto2 [id=" + id + ", email=" + email + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
10
libraries-data/src/main/resources/user_jmapper.xml
Normal file
10
libraries-data/src/main/resources/user_jmapper.xml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<jmapper>
|
||||||
|
<class name="com.baeldung.jmapper.UserDto">
|
||||||
|
<attribute name="id">
|
||||||
|
<value name="id"/>
|
||||||
|
</attribute>
|
||||||
|
<attribute name="username">
|
||||||
|
<value name="email"/>
|
||||||
|
</attribute>
|
||||||
|
</class>
|
||||||
|
</jmapper>
|
5
libraries-data/src/main/resources/user_jmapper1.xml
Normal file
5
libraries-data/src/main/resources/user_jmapper1.xml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<jmapper>
|
||||||
|
<class name="com.baeldung.jmapper.UserDto1">
|
||||||
|
<global/>
|
||||||
|
</class>
|
||||||
|
</jmapper>
|
21
libraries-data/src/main/resources/user_jmapper2.xml
Normal file
21
libraries-data/src/main/resources/user_jmapper2.xml
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<jmapper>
|
||||||
|
<class name="com.baeldung.jmapper.relational.User">
|
||||||
|
<attribute name="id">
|
||||||
|
<value name="id"/>
|
||||||
|
<classes>
|
||||||
|
<class name="com.baeldung.jmapper.relational.UserDto1"/>
|
||||||
|
<class name="com.baeldung.jmapper.relational.UserDto2"/>
|
||||||
|
</classes>
|
||||||
|
</attribute>
|
||||||
|
<attribute name="email">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="username"/>
|
||||||
|
<attribute name="email"/>
|
||||||
|
</attributes>
|
||||||
|
<classes>
|
||||||
|
<class name="com.baeldung.jmapper.relational.UserDto1"/>
|
||||||
|
<class name="com.baeldung.jmapper.relational.UserDto2"/>
|
||||||
|
</classes>
|
||||||
|
</attribute>
|
||||||
|
</class>
|
||||||
|
</jmapper>
|
@ -0,0 +1,114 @@
|
|||||||
|
package com.baeldung.jmapper;
|
||||||
|
|
||||||
|
import static com.googlecode.jmapper.api.JMapperAPI.attribute;
|
||||||
|
import static com.googlecode.jmapper.api.JMapperAPI.global;
|
||||||
|
import static com.googlecode.jmapper.api.JMapperAPI.mappedClass;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.googlecode.jmapper.JMapper;
|
||||||
|
import com.googlecode.jmapper.api.JMapperAPI;
|
||||||
|
|
||||||
|
public class JMapperIntegrationTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUser_whenUseAnnotation_thenConverted(){
|
||||||
|
JMapper<UserDto, User> userMapper = new JMapper<>(UserDto.class, User.class);
|
||||||
|
|
||||||
|
User user = new User(1L,"john@test.com", LocalDate.of(1980,8,20));
|
||||||
|
UserDto result = userMapper.getDestination(user);
|
||||||
|
|
||||||
|
System.out.println(result);
|
||||||
|
assertEquals(user.getId(), result.getId());
|
||||||
|
assertEquals(user.getEmail(), result.getUsername());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUser_whenUseGlobalMapAnnotation_thenConverted(){
|
||||||
|
JMapper<UserDto1, User> userMapper= new JMapper<>(UserDto1.class, User.class);
|
||||||
|
|
||||||
|
User user = new User(1L,"john@test.com", LocalDate.of(1980,8,20));
|
||||||
|
UserDto1 result = userMapper.getDestination(user);
|
||||||
|
|
||||||
|
System.out.println(result);
|
||||||
|
assertEquals(user.getId(), result.getId());
|
||||||
|
assertEquals(user.getEmail(), result.getEmail());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUser_whenUseAnnotationExplicitConversion_thenConverted(){
|
||||||
|
JMapper<UserDto, User> userMapper = new JMapper<>(UserDto.class, User.class);
|
||||||
|
|
||||||
|
User user = new User(1L,"john@test.com", LocalDate.of(1980,8,20));
|
||||||
|
UserDto result = userMapper.getDestination(user);
|
||||||
|
|
||||||
|
System.out.println(result);
|
||||||
|
assertEquals(user.getId(), result.getId());
|
||||||
|
assertEquals(user.getEmail(), result.getUsername());
|
||||||
|
assertTrue(result.getAge() > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
//======================= XML
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUser_whenUseXml_thenConverted(){
|
||||||
|
JMapper<UserDto, User> userMapper = new JMapper<>(UserDto.class, User.class,"user_jmapper.xml");
|
||||||
|
|
||||||
|
User user = new User(1L,"john@test.com", LocalDate.of(1980,8,20));
|
||||||
|
UserDto result = userMapper.getDestination(user);
|
||||||
|
|
||||||
|
System.out.println(result);
|
||||||
|
assertEquals(user.getId(), result.getId());
|
||||||
|
assertEquals(user.getEmail(), result.getUsername());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUser_whenUseXmlGlobal_thenConverted(){
|
||||||
|
JMapper<UserDto1, User> userMapper = new JMapper<>(UserDto1.class, User.class,"user_jmapper1.xml");
|
||||||
|
|
||||||
|
User user = new User(1L,"john@test.com", LocalDate.of(1980,8,20));
|
||||||
|
UserDto1 result = userMapper.getDestination(user);
|
||||||
|
|
||||||
|
System.out.println(result);
|
||||||
|
assertEquals(user.getId(), result.getId());
|
||||||
|
assertEquals(user.getEmail(), result.getEmail());
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===== API
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUser_whenUseApi_thenConverted(){
|
||||||
|
JMapperAPI jmapperApi = new JMapperAPI() .add(mappedClass(UserDto.class)
|
||||||
|
.add(attribute("id").value("id"))
|
||||||
|
.add(attribute("username").value("email"))
|
||||||
|
) ;
|
||||||
|
JMapper<UserDto, User> userMapper = new JMapper<>(UserDto.class, User.class, jmapperApi);
|
||||||
|
|
||||||
|
User user = new User(1L,"john@test.com", LocalDate.of(1980,8,20));
|
||||||
|
UserDto result = userMapper.getDestination(user);
|
||||||
|
|
||||||
|
System.out.println(result);
|
||||||
|
assertEquals(user.getId(), result.getId());
|
||||||
|
assertEquals(user.getEmail(), result.getUsername());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUser_whenUseApiGlobal_thenConverted(){
|
||||||
|
JMapperAPI jmapperApi = new JMapperAPI() .add(mappedClass(UserDto.class)
|
||||||
|
.add(global())
|
||||||
|
) ;
|
||||||
|
JMapper<UserDto1, User> userMapper1 = new JMapper<>(UserDto1.class, User.class,jmapperApi);
|
||||||
|
|
||||||
|
User user = new User(1L,"john@test.com", LocalDate.of(1980,8,20));
|
||||||
|
UserDto1 result = userMapper1.getDestination(user);
|
||||||
|
|
||||||
|
System.out.println(result);
|
||||||
|
assertEquals(user.getId(), result.getId());
|
||||||
|
assertEquals(user.getEmail(), result.getEmail());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,76 @@
|
|||||||
|
package com.baeldung.jmapper;
|
||||||
|
|
||||||
|
import static com.googlecode.jmapper.api.JMapperAPI.attribute;
|
||||||
|
import static com.googlecode.jmapper.api.JMapperAPI.mappedClass;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.jmapper.relational.User;
|
||||||
|
import com.baeldung.jmapper.relational.UserDto1;
|
||||||
|
import com.baeldung.jmapper.relational.UserDto2;
|
||||||
|
import com.googlecode.jmapper.RelationalJMapper;
|
||||||
|
import com.googlecode.jmapper.api.JMapperAPI;
|
||||||
|
|
||||||
|
public class JMapperRelationalIntegrationTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUser_whenUseAnnotation_thenConverted(){
|
||||||
|
RelationalJMapper<User> relationalMapper = new RelationalJMapper<>(User.class);
|
||||||
|
|
||||||
|
User user = new User(1L,"john@test.com");
|
||||||
|
UserDto1 result1 = relationalMapper.oneToMany(UserDto1.class, user);
|
||||||
|
UserDto2 result2= relationalMapper.oneToMany(UserDto2.class, user);
|
||||||
|
|
||||||
|
System.out.println(result1);
|
||||||
|
System.out.println(result2);
|
||||||
|
assertEquals(user.getId(), result1.getId());
|
||||||
|
assertEquals(user.getEmail(), result1.getUsername());
|
||||||
|
assertEquals(user.getId(), result2.getId());
|
||||||
|
assertEquals(user.getEmail(), result2.getEmail());
|
||||||
|
}
|
||||||
|
|
||||||
|
//======================= XML
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUser_whenUseXml_thenConverted(){
|
||||||
|
RelationalJMapper<User> relationalMapper = new RelationalJMapper<>(User.class,"user_jmapper2.xml");
|
||||||
|
|
||||||
|
User user = new User(1L,"john@test.com");
|
||||||
|
UserDto1 result1 = relationalMapper.oneToMany(UserDto1.class, user);
|
||||||
|
UserDto2 result2 = relationalMapper.oneToMany(UserDto2.class, user);
|
||||||
|
|
||||||
|
System.out.println(result1);
|
||||||
|
System.out.println(result2);
|
||||||
|
assertEquals(user.getId(), result1.getId());
|
||||||
|
assertEquals(user.getEmail(), result1.getUsername());
|
||||||
|
assertEquals(user.getId(), result2.getId());
|
||||||
|
assertEquals(user.getEmail(), result2.getEmail());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ===== API
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUser_whenUseApi_thenConverted(){
|
||||||
|
JMapperAPI jmapperApi = new JMapperAPI()
|
||||||
|
.add(mappedClass(User.class)
|
||||||
|
.add(attribute("id").value("id").targetClasses(UserDto1.class,UserDto2.class))
|
||||||
|
.add(attribute("email").targetAttributes("username","email").targetClasses(UserDto1.class,UserDto2.class)) )
|
||||||
|
;
|
||||||
|
RelationalJMapper<User> relationalMapper = new RelationalJMapper<>(User.class,jmapperApi);
|
||||||
|
|
||||||
|
User user = new User(1L,"john@test.com");
|
||||||
|
UserDto1 result1 = relationalMapper.oneToMany(UserDto1.class, user);
|
||||||
|
UserDto2 result2 = relationalMapper.oneToMany(UserDto2.class, user);
|
||||||
|
|
||||||
|
System.out.println(result1);
|
||||||
|
System.out.println(result2);
|
||||||
|
assertEquals(user.getId(), result1.getId());
|
||||||
|
assertEquals(user.getEmail(), result1.getUsername());
|
||||||
|
assertEquals(user.getId(), result2.getId());
|
||||||
|
assertEquals(user.getEmail(), result2.getEmail());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -48,8 +48,10 @@
|
|||||||
<exclude>DataTest.java</exclude>
|
<exclude>DataTest.java</exclude>
|
||||||
</excludes>
|
</excludes>
|
||||||
<includes>
|
<includes>
|
||||||
|
<include>TestFail.java</include>
|
||||||
<include>DataCheck.java</include>
|
<include>DataCheck.java</include>
|
||||||
</includes>
|
</includes>
|
||||||
|
<testFailureIgnore>true</testFailureIgnore>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
15
maven/src/test/java/testfail/TestFail.java
Normal file
15
maven/src/test/java/testfail/TestFail.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package testfail;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
|
public class TestFail {
|
||||||
|
@Test
|
||||||
|
public void whenMessageAssigned_thenItIsNotNull() {
|
||||||
|
String message = "hello there";
|
||||||
|
message = null;
|
||||||
|
assertNotNull(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -2,9 +2,8 @@
|
|||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>cassandra-java-client</artifactId>
|
<artifactId>java-cassandra</artifactId>
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
<name>cassandra-java-client</name>
|
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
|
473
pom.xml
473
pom.xml
@ -195,6 +195,61 @@
|
|||||||
<version>${gitflow-incremental-builder.version}</version>
|
<version>${gitflow-incremental-builder.version}</version>
|
||||||
</extension>
|
</extension>
|
||||||
</extensions>
|
</extensions>
|
||||||
|
<pluginManagement>
|
||||||
|
<plugins>
|
||||||
|
<!--This plugin's configuration is used to store Eclipse m2e settings
|
||||||
|
only. It has no influence on the Maven build itself. -->
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.eclipse.m2e</groupId>
|
||||||
|
<artifactId>lifecycle-mapping</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
<configuration>
|
||||||
|
<lifecycleMappingMetadata>
|
||||||
|
<pluginExecutions>
|
||||||
|
<pluginExecution>
|
||||||
|
<pluginExecutionFilter>
|
||||||
|
<groupId>
|
||||||
|
org.commonjava.maven.plugins
|
||||||
|
</groupId>
|
||||||
|
<artifactId>
|
||||||
|
directory-maven-plugin
|
||||||
|
</artifactId>
|
||||||
|
<versionRange>
|
||||||
|
[0.3.1,)
|
||||||
|
</versionRange>
|
||||||
|
<goals>
|
||||||
|
<goal>directory-of</goal>
|
||||||
|
</goals>
|
||||||
|
</pluginExecutionFilter>
|
||||||
|
<action>
|
||||||
|
<ignore></ignore>
|
||||||
|
</action>
|
||||||
|
</pluginExecution>
|
||||||
|
<pluginExecution>
|
||||||
|
<pluginExecutionFilter>
|
||||||
|
<groupId>
|
||||||
|
org.apache.maven.plugins
|
||||||
|
</groupId>
|
||||||
|
<artifactId>
|
||||||
|
maven-install-plugin
|
||||||
|
</artifactId>
|
||||||
|
<versionRange>
|
||||||
|
[2.5.1,)
|
||||||
|
</versionRange>
|
||||||
|
<goals>
|
||||||
|
<goal>install-file</goal>
|
||||||
|
</goals>
|
||||||
|
</pluginExecutionFilter>
|
||||||
|
<action>
|
||||||
|
<ignore></ignore>
|
||||||
|
</action>
|
||||||
|
</pluginExecution>
|
||||||
|
</pluginExecutions>
|
||||||
|
</lifecycleMappingMetadata>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</pluginManagement>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<profiles>
|
<profiles>
|
||||||
@ -203,33 +258,25 @@
|
|||||||
<id>default</id>
|
<id>default</id>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
<executions>
|
<version>${maven-surefire-plugin.version}</version>
|
||||||
<execution>
|
|
||||||
<phase>integration-test</phase>
|
|
||||||
<goals>
|
|
||||||
<goal>test</goal>
|
|
||||||
</goals>
|
|
||||||
<configuration>
|
|
||||||
<excludes>
|
|
||||||
<exclude>**/*ManualTest.java</exclude>
|
|
||||||
<exclude>**/*LiveTest.java</exclude>
|
|
||||||
</excludes>
|
|
||||||
<includes>
|
|
||||||
<include>**/*IntegrationTest.java</include>
|
|
||||||
<include>**/*IntTest.java</include>
|
|
||||||
</includes>
|
|
||||||
</configuration>
|
|
||||||
</execution>
|
|
||||||
</executions>
|
|
||||||
<configuration>
|
<configuration>
|
||||||
<systemPropertyVariables>
|
<forkCount>3</forkCount>
|
||||||
<test.mime>json</test.mime>
|
<reuseForks>true</reuseForks>
|
||||||
</systemPropertyVariables>
|
<excludes>
|
||||||
|
<exclude>**/*IntegrationTest.java</exclude>
|
||||||
|
<exclude>**/*IntTest.java</exclude>
|
||||||
|
<exclude>**/*LongRunningUnitTest.java</exclude>
|
||||||
|
<exclude>**/*ManualTest.java</exclude>
|
||||||
|
<exclude>**/JdbcTest.java</exclude>
|
||||||
|
<exclude>**/*LiveTest.java</exclude>
|
||||||
|
</excludes>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
@ -498,6 +545,9 @@
|
|||||||
<module>antlr</module>
|
<module>antlr</module>
|
||||||
<module>maven-archetype</module>
|
<module>maven-archetype</module>
|
||||||
<module>apache-meecrowave</module>
|
<module>apache-meecrowave</module>
|
||||||
|
<module>spring-reactive-kotlin</module>
|
||||||
|
<module>jnosql</module>
|
||||||
|
<module>testing-modules/junit-abstract</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
</profile>
|
</profile>
|
||||||
@ -580,8 +630,9 @@
|
|||||||
<!-- group 1 - OK, 27min, 7911Kb log, 8 failusres -->
|
<!-- group 1 - OK, 27min, 7911Kb log, 8 failusres -->
|
||||||
|
|
||||||
<!-- group 2 -->
|
<!-- group 2 -->
|
||||||
|
|
||||||
<!-- <module>testing-modules/mockito</module>
|
<!-- -->
|
||||||
|
<module>testing-modules/mockito</module>
|
||||||
<module>testing-modules/mockito-2</module>
|
<module>testing-modules/mockito-2</module>
|
||||||
<module>testing-modules/mocks</module>
|
<module>testing-modules/mocks</module>
|
||||||
<module>mustache</module>
|
<module>mustache</module>
|
||||||
@ -616,6 +667,13 @@
|
|||||||
<module>spring-amqp-simple</module>
|
<module>spring-amqp-simple</module>
|
||||||
<module>spring-apache-camel</module>
|
<module>spring-apache-camel</module>
|
||||||
<module>spring-batch</module>
|
<module>spring-batch</module>
|
||||||
|
<module>testing-modules/junit-abstract</module>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- group 2 - Pass, 11-16 min, 42 test failures, 4,020 KB -->
|
||||||
|
|
||||||
|
<!-- group 3.1 -->
|
||||||
|
|
||||||
<module>spring-bom</module>
|
<module>spring-bom</module>
|
||||||
<module>spring-boot</module>
|
<module>spring-boot</module>
|
||||||
<module>spring-boot-keycloak</module>
|
<module>spring-boot-keycloak</module>
|
||||||
@ -641,6 +699,274 @@
|
|||||||
<module>persistence-modules/spring-data-neo4j</module>
|
<module>persistence-modules/spring-data-neo4j</module>
|
||||||
<module>persistence-modules/spring-data-redis</module>
|
<module>persistence-modules/spring-data-redis</module>
|
||||||
<module>spring-data-rest</module>
|
<module>spring-data-rest</module>
|
||||||
|
|
||||||
|
<!-- group 3.1 - Pass, 23 min, 35 failed tests, 7,942 KB -->
|
||||||
|
|
||||||
|
<!-- group 3.2 -->
|
||||||
|
|
||||||
|
<!-- -->
|
||||||
|
<module>persistence-modules/spring-data-solr</module>
|
||||||
|
<module>spring-dispatcher-servlet</module>
|
||||||
|
<module>spring-exceptions</module>
|
||||||
|
<module>spring-freemarker</module>
|
||||||
|
<module>persistence-modules/spring-hibernate-3</module>
|
||||||
|
<module>spring-hibernate4</module>
|
||||||
|
<module>persistence-modules/spring-hibernate-5</module>
|
||||||
|
<module>persistence-modules/spring-data-eclipselink</module>
|
||||||
|
<module>spring-integration</module>
|
||||||
|
<module>spring-jenkins-pipeline</module>
|
||||||
|
<module>spring-jersey</module>
|
||||||
|
<module>spring-jms</module>
|
||||||
|
<module>spring-jooq</module>
|
||||||
|
<module>persistence-modules/spring-jpa</module>
|
||||||
|
<module>spring-kafka</module>
|
||||||
|
<module>spring-katharsis</module>
|
||||||
|
<module>spring-ldap</module>
|
||||||
|
<module>spring-mockito</module>
|
||||||
|
<module>spring-mvc-forms-jsp</module>
|
||||||
|
<module>spring-mvc-forms-thymeleaf</module>
|
||||||
|
<module>spring-mvc-java</module>
|
||||||
|
<module>spring-mvc-velocity</module>
|
||||||
|
<module>spring-mvc-webflow</module>
|
||||||
|
<module>spring-mvc-xml</module>
|
||||||
|
<module>spring-mvc-kotlin</module>
|
||||||
|
<module>spring-protobuf</module>
|
||||||
|
<module>spring-quartz</module>
|
||||||
|
<module>spring-rest-angular</module>
|
||||||
|
<module>spring-rest-full</module>
|
||||||
|
<module>spring-rest-query-language</module>
|
||||||
|
<module>spring-rest</module>
|
||||||
|
<module>spring-rest-simple</module>
|
||||||
|
<module>spring-reactive-kotlin</module>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- group 3.2 - Pass, 8 minutes, 1 failed test, 2,294 KB log -->
|
||||||
|
<!-- group 3 - Pass, 25 minutes, 36 failed tests, 10,163 KB log -->
|
||||||
|
<!-- group 3 - Pass, 23 minutes, 31 failed tests, 8,693 KB log -->
|
||||||
|
<!-- group 2+3 - Fail, 12 minutes, 44 failed tests -->
|
||||||
|
|
||||||
|
<!-- group 4 -->
|
||||||
|
|
||||||
|
<!-- <module>spring-security-acl</module> <module>spring-security-cache-control</module>
|
||||||
|
<module>spring-security-client/spring-security-jsp-authentication</module>
|
||||||
|
<module>spring-security-client/spring-security-jsp-authorize</module> <module>spring-security-client/spring-security-jsp-config</module>
|
||||||
|
<module>spring-security-client/spring-security-mvc</module> <module>spring-security-client/spring-security-thymeleaf-authentication</module>
|
||||||
|
<module>spring-security-client/spring-security-thymeleaf-authorize</module>
|
||||||
|
<module>spring-security-client/spring-security-thymeleaf-config</module>
|
||||||
|
<module>spring-security-core</module> <module>spring-security-mvc-boot</module>
|
||||||
|
<module>spring-security-mvc-custom</module> <module>spring-security-mvc-digest-auth</module>
|
||||||
|
<module>spring-security-mvc-ldap</module> <module>spring-security-mvc-login</module>
|
||||||
|
<module>spring-security-mvc-persisted-remember-me</module> <module>spring-security-mvc-session</module>
|
||||||
|
<module>spring-security-mvc-socket</module> <module>spring-security-openid</module>
|
||||||
|
<module>spring-security-react</module> <module>spring-security-rest-basic-auth</module>
|
||||||
|
<module>spring-security-rest-custom</module> <module>spring-security-rest</module>
|
||||||
|
<module>spring-security-sso</module> <module>spring-security-x509</module>
|
||||||
|
<module>spring-session</module> <module>spring-sleuth</module> <module>spring-social-login</module>
|
||||||
|
<module>spring-spel</module> <module>spring-state-machine</module> <module>spring-thymeleaf</module>
|
||||||
|
<module>spring-userservice</module> <module>spring-zuul</module> <module>spring-reactor</module>
|
||||||
|
<module>spring-vertx</module> <module>spring-jinq</module> <module>spring-rest-embedded-tomcat</module>
|
||||||
|
<module>testing-modules/testing</module> <module>testing-modules/testng</module>
|
||||||
|
<module>video-tutorials</module> <module>xml</module> <module>xmlunit-2</module>
|
||||||
|
<module>struts-2</module> <module>apache-velocity</module> <module>apache-solrj</module>
|
||||||
|
<module>rabbitmq</module> <module>vertx</module> <module>persistence-modules/spring-data-gemfire</module>
|
||||||
|
<module>mybatis</module> <module>spring-drools</module> <module>drools</module>
|
||||||
|
<module>persistence-modules/liquibase</module> <module>spring-boot-property-exp</module>
|
||||||
|
<module>testing-modules/mockserver</module> <module>testing-modules/test-containers</module>
|
||||||
|
<module>undertow</module> <module>vertx-and-rxjava</module> <module>saas</module>
|
||||||
|
<module>deeplearning4j</module> <module>lucene</module> <module>vraptor</module>
|
||||||
|
<module>persistence-modules/java-cockroachdb</module> <module>spring-security-thymeleaf</module>
|
||||||
|
<module>persistence-modules/java-jdbi</module> <module>jersey</module> <module>java-spi</module>
|
||||||
|
<module>performance-tests</module> <module>twilio</module> <module>spring-boot-ctx-fluent</module>
|
||||||
|
<module>java-ee-8-security-api</module> <module>spring-webflux-amqp</module>
|
||||||
|
<module>antlr</module> <module>maven-archetype</module> <module>apache-meecrowave</module> -->
|
||||||
|
|
||||||
|
<!-- group 4 - OK, 12 min, 3,961 KB log, 12 failed tests -->
|
||||||
|
|
||||||
|
<!-- <module>libraries</module> <module>jmeter</module> -->
|
||||||
|
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
</profile>
|
||||||
|
|
||||||
|
<profile>
|
||||||
|
<id>integration-lite</id>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>integration-test</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>test</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/*ManualTest.java</exclude>
|
||||||
|
<exclude>**/*LiveTest.java</exclude>
|
||||||
|
</excludes>
|
||||||
|
<includes>
|
||||||
|
<include>**/*IntegrationTest.java</include>
|
||||||
|
<include>**/*IntTest.java</include>
|
||||||
|
</includes>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<systemPropertyVariables>
|
||||||
|
<test.mime>json</test.mime>
|
||||||
|
</systemPropertyVariables>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<modules>
|
||||||
|
<module>parent-boot-1</module>
|
||||||
|
<module>parent-boot-2</module>
|
||||||
|
<module>parent-spring-4</module>
|
||||||
|
<module>parent-spring-5</module>
|
||||||
|
<module>parent-java</module>
|
||||||
|
|
||||||
|
<module>asm</module>
|
||||||
|
<module>atomix</module>
|
||||||
|
<module>apache-cayenne</module>
|
||||||
|
<module>aws</module>
|
||||||
|
<module>aws-lambda</module>
|
||||||
|
<module>akka-streams</module>
|
||||||
|
<module>algorithms</module>
|
||||||
|
<module>annotations</module>
|
||||||
|
<module>apache-cxf</module>
|
||||||
|
<module>apache-fop</module>
|
||||||
|
<module>apache-poi</module>
|
||||||
|
<module>apache-tika</module>
|
||||||
|
<module>apache-thrift</module>
|
||||||
|
<module>apache-curator</module>
|
||||||
|
<module>apache-zookeeper</module>
|
||||||
|
<module>apache-opennlp</module>
|
||||||
|
<module>autovalue</module>
|
||||||
|
<module>axon</module>
|
||||||
|
<module>azure</module>
|
||||||
|
<module>bootique</module>
|
||||||
|
<module>cdi</module>
|
||||||
|
<!--<module>core-java-9</module> --> <!-- Commented because we have still not upgraded to java 9 -->
|
||||||
|
<module>core-java-collections</module>
|
||||||
|
<module>core-java-io</module>
|
||||||
|
<module>core-java-8</module>
|
||||||
|
<module>core-groovy</module>
|
||||||
|
<module>core-java-concurrency</module>
|
||||||
|
<module>couchbase</module>
|
||||||
|
<module>deltaspike</module>
|
||||||
|
<module>dozer</module>
|
||||||
|
<module>ethereum</module>
|
||||||
|
<module>feign</module>
|
||||||
|
<module>flips</module>
|
||||||
|
<module>testing-modules/groovy-spock</module>
|
||||||
|
<module>google-cloud</module>
|
||||||
|
<module>gson</module>
|
||||||
|
<module>guava</module>
|
||||||
|
<module>guava-modules/guava-18</module>
|
||||||
|
<module>guava-modules/guava-19</module>
|
||||||
|
<module>guava-modules/guava-21</module>
|
||||||
|
<module>guice</module>
|
||||||
|
<module>disruptor</module>
|
||||||
|
<module>spring-static-resources</module>
|
||||||
|
<module>hazelcast</module>
|
||||||
|
<module>hbase</module>
|
||||||
|
<module>httpclient</module>
|
||||||
|
<module>hystrix</module>
|
||||||
|
<module>image-processing</module>
|
||||||
|
<module>immutables</module>
|
||||||
|
<module>influxdb</module>
|
||||||
|
<module>jackson</module>
|
||||||
|
<module>vavr</module>
|
||||||
|
<module>java-lite</module>
|
||||||
|
<module>java-numbers</module>
|
||||||
|
<module>java-rmi</module>
|
||||||
|
<module>java-vavr-stream</module>
|
||||||
|
<module>javax-servlets</module>
|
||||||
|
<module>javaxval</module>
|
||||||
|
<module>jaxb</module>
|
||||||
|
<module>javafx</module>
|
||||||
|
<module>jgroups</module>
|
||||||
|
<module>jee-7</module>
|
||||||
|
<module>jjwt</module>
|
||||||
|
<module>jpa-storedprocedure</module>
|
||||||
|
<module>jsf</module>
|
||||||
|
<module>json-path</module>
|
||||||
|
<module>json</module>
|
||||||
|
<module>jsoup</module>
|
||||||
|
<module>testing-modules/junit-5</module>
|
||||||
|
<module>jws</module>
|
||||||
|
<module>libraries-data</module>
|
||||||
|
<module>linkrest</module>
|
||||||
|
<module>logging-modules/log-mdc</module>
|
||||||
|
<module>logging-modules/log4j</module>
|
||||||
|
<module>logging-modules/log4j2</module>
|
||||||
|
<module>logging-modules/logback</module>
|
||||||
|
<module>lombok</module>
|
||||||
|
<module>mapstruct</module>
|
||||||
|
<module>metrics</module>
|
||||||
|
<module>maven</module>
|
||||||
|
<module>mesos-marathon</module>
|
||||||
|
<module>msf4j</module>
|
||||||
|
<module>testing-modules/mockito</module>
|
||||||
|
<module>testing-modules/mockito-2</module>
|
||||||
|
<module>testing-modules/mocks</module>
|
||||||
|
<module>mustache</module>
|
||||||
|
<module>mvn-wrapper</module>
|
||||||
|
<module>noexception</module>
|
||||||
|
<module>orientdb</module>
|
||||||
|
<module>osgi</module>
|
||||||
|
<module>orika</module>
|
||||||
|
<module>patterns</module>
|
||||||
|
<module>pdf</module>
|
||||||
|
<module>protobuffer</module>
|
||||||
|
<module>persistence-modules/querydsl</module>
|
||||||
|
<module>reactor-core</module>
|
||||||
|
<module>persistence-modules/redis</module>
|
||||||
|
<module>testing-modules/rest-assured</module>
|
||||||
|
<module>testing-modules/rest-testing</module>
|
||||||
|
<module>resteasy</module>
|
||||||
|
<module>rxjava</module>
|
||||||
|
<module>spring-swagger-codegen</module>
|
||||||
|
<module>testing-modules/selenium-junit-testng</module>
|
||||||
|
<module>persistence-modules/solr</module>
|
||||||
|
<module>spark-java</module>
|
||||||
|
<module>spring-4</module>
|
||||||
|
<module>spring-5-reactive</module>
|
||||||
|
<module>spring-5-mvc</module>
|
||||||
|
<module>spring-5-security</module>
|
||||||
|
<module>spring-activiti</module>
|
||||||
|
<module>spring-akka</module>
|
||||||
|
<module>spring-amqp</module>
|
||||||
|
<module>spring-all</module>
|
||||||
|
<module>spring-amqp-simple</module>
|
||||||
|
<module>spring-apache-camel</module>
|
||||||
|
<module>spring-batch</module>
|
||||||
|
<module>spring-bom</module>
|
||||||
|
<module>spring-boot-keycloak</module>
|
||||||
|
<module>spring-boot-bootstrap</module>
|
||||||
|
<module>spring-boot-admin</module>
|
||||||
|
<module>spring-boot-persistence</module>
|
||||||
|
<module>spring-boot-security</module>
|
||||||
|
<module>spring-boot-mvc</module>
|
||||||
|
<module>spring-boot-logging-log4j2</module>
|
||||||
|
<module>spring-cloud-data-flow</module>
|
||||||
|
<module>spring-cloud</module>
|
||||||
|
<module>spring-core</module>
|
||||||
|
<module>spring-cucumber</module>
|
||||||
|
<module>spring-ejb</module>
|
||||||
|
<module>spring-aop</module>
|
||||||
|
<module>spring-data-couchbase-2</module>
|
||||||
|
<module>persistence-modules/spring-data-dynamodb</module>
|
||||||
|
<module>spring-data-keyvalue</module>
|
||||||
|
<module>spring-data-mongodb</module>
|
||||||
|
<module>persistence-modules/spring-data-neo4j</module>
|
||||||
|
<module>persistence-modules/spring-data-redis</module>
|
||||||
|
<module>spring-data-rest</module>
|
||||||
<module>persistence-modules/spring-data-solr</module>
|
<module>persistence-modules/spring-data-solr</module>
|
||||||
<module>spring-dispatcher-servlet</module>
|
<module>spring-dispatcher-servlet</module>
|
||||||
<module>spring-exceptions</module>
|
<module>spring-exceptions</module>
|
||||||
@ -673,12 +999,7 @@
|
|||||||
<module>spring-rest-full</module>
|
<module>spring-rest-full</module>
|
||||||
<module>spring-rest-query-language</module>
|
<module>spring-rest-query-language</module>
|
||||||
<module>spring-rest</module>
|
<module>spring-rest</module>
|
||||||
<module>spring-rest-simple</module> -->
|
<module>spring-rest-simple</module>
|
||||||
|
|
||||||
<!-- group 2 - Fail, 19 min, 5,794 KB log, 45 failed tests -->
|
|
||||||
|
|
||||||
<!-- group 3 -->
|
|
||||||
|
|
||||||
<module>spring-security-acl</module>
|
<module>spring-security-acl</module>
|
||||||
<module>spring-security-cache-control</module>
|
<module>spring-security-cache-control</module>
|
||||||
<module>spring-security-client/spring-security-jsp-authentication</module>
|
<module>spring-security-client/spring-security-jsp-authentication</module>
|
||||||
@ -690,7 +1011,6 @@
|
|||||||
<module>spring-security-client/spring-security-thymeleaf-config</module>
|
<module>spring-security-client/spring-security-thymeleaf-config</module>
|
||||||
<module>spring-security-core</module>
|
<module>spring-security-core</module>
|
||||||
<module>spring-security-mvc-boot</module>
|
<module>spring-security-mvc-boot</module>
|
||||||
<module>spring-security-mvc-custom</module>
|
|
||||||
<module>spring-security-mvc-digest-auth</module>
|
<module>spring-security-mvc-digest-auth</module>
|
||||||
<module>spring-security-mvc-ldap</module>
|
<module>spring-security-mvc-ldap</module>
|
||||||
<module>spring-security-mvc-login</module>
|
<module>spring-security-mvc-login</module>
|
||||||
@ -698,7 +1018,7 @@
|
|||||||
<module>spring-security-mvc-session</module>
|
<module>spring-security-mvc-session</module>
|
||||||
<module>spring-security-mvc-socket</module>
|
<module>spring-security-mvc-socket</module>
|
||||||
<module>spring-security-openid</module>
|
<module>spring-security-openid</module>
|
||||||
<module>spring-security-react</module>
|
<!--<module>spring-security-react</module> -->
|
||||||
<module>spring-security-rest-basic-auth</module>
|
<module>spring-security-rest-basic-auth</module>
|
||||||
<module>spring-security-rest-custom</module>
|
<module>spring-security-rest-custom</module>
|
||||||
<module>spring-security-rest</module>
|
<module>spring-security-rest</module>
|
||||||
@ -753,13 +1073,96 @@
|
|||||||
<module>antlr</module>
|
<module>antlr</module>
|
||||||
<module>maven-archetype</module>
|
<module>maven-archetype</module>
|
||||||
<module>apache-meecrowave</module>
|
<module>apache-meecrowave</module>
|
||||||
|
<module>testing-modules/junit-abstract</module>
|
||||||
<!-- <module>libraries</module> -->
|
|
||||||
|
<!-- problematic -->
|
||||||
|
<!--
|
||||||
|
<module>ejb</module>
|
||||||
|
<module>persistence-modules/java-cassandra</module>
|
||||||
|
<module>persistence-modules/spring-data-cassandra</module>
|
||||||
|
-->
|
||||||
|
|
||||||
|
<!-- heavy -->
|
||||||
|
<!--
|
||||||
|
<module>libraries</module>
|
||||||
|
<module>geotools</module>
|
||||||
|
<module>jhipster/jhipster-monolithic</module>
|
||||||
|
<module>testing-modules/gatling</module>
|
||||||
|
<module>spring-boot</module>
|
||||||
|
<module>spring-boot-ops</module>
|
||||||
|
<module>spring-5</module>
|
||||||
|
<module>core-kotlin</module>
|
||||||
|
<module>core-java</module>
|
||||||
|
<module>google-web-toolkit</module>
|
||||||
|
<module>spring-security-mvc-custom</module>
|
||||||
|
-->
|
||||||
|
|
||||||
|
<!-- 30:32 min -->
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
</profile>
|
</profile>
|
||||||
|
|
||||||
|
<profile>
|
||||||
|
<id>integration-heavy</id>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>integration-test</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>test</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/*ManualTest.java</exclude>
|
||||||
|
<exclude>**/*LiveTest.java</exclude>
|
||||||
|
</excludes>
|
||||||
|
<includes>
|
||||||
|
<include>**/*IntegrationTest.java</include>
|
||||||
|
<include>**/*IntTest.java</include>
|
||||||
|
</includes>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<systemPropertyVariables>
|
||||||
|
<test.mime>json</test.mime>
|
||||||
|
</systemPropertyVariables>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<modules>
|
||||||
|
<module>parent-boot-1</module>
|
||||||
|
<module>parent-boot-2</module>
|
||||||
|
<module>parent-spring-4</module>
|
||||||
|
<module>parent-spring-5</module>
|
||||||
|
<module>parent-java</module>
|
||||||
|
|
||||||
|
<module>libraries</module>
|
||||||
|
<module>geotools</module>
|
||||||
|
<module>jhipster/jhipster-monolithic</module>
|
||||||
|
<module>testing-modules/gatling</module>
|
||||||
|
<module>spring-boot</module>
|
||||||
|
<module>spring-boot-ops</module>
|
||||||
|
<module>spring-5</module>
|
||||||
|
<module>core-kotlin</module>
|
||||||
|
<module>core-java</module>
|
||||||
|
<module>google-web-toolkit</module>
|
||||||
|
<module>spring-security-mvc-custom</module>
|
||||||
|
<module>hibernate5</module>
|
||||||
|
<module>spring-data-elasticsearch</module>
|
||||||
|
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
</profile>
|
||||||
|
|
||||||
|
|
||||||
</profiles>
|
</profiles>
|
||||||
|
|
||||||
<reporting>
|
<reporting>
|
||||||
@ -819,4 +1222,4 @@
|
|||||||
<maven-pmd-plugin.version>3.8</maven-pmd-plugin.version>
|
<maven-pmd-plugin.version>3.8</maven-pmd-plugin.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -1,48 +0,0 @@
|
|||||||
package broadcast;
|
|
||||||
|
|
||||||
import com.baeldung.springamqpsimple.broadcast.BroadcastConfig;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
|
||||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.test.context.ActiveProfiles;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
import static org.mockito.Mockito.*;
|
|
||||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@ActiveProfiles("test")
|
|
||||||
@SpringBootTest(webEnvironment = RANDOM_PORT)
|
|
||||||
public class BroadcastMessageControllerIntegrationTest {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private TestRestTemplate restTemplate;
|
|
||||||
|
|
||||||
@MockBean
|
|
||||||
private RabbitTemplate rabbitTemplate;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenPostingMessage_thenMessageIsCreated() {
|
|
||||||
final String message = "Hello World!";
|
|
||||||
ResponseEntity<Void> responseEntity = restTemplate.postForEntity("/broadcast", message, Void.class);
|
|
||||||
|
|
||||||
assertEquals(HttpStatus.CREATED, responseEntity.getStatusCode());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenPostingMessage_thenMessageIsSentToBroker() {
|
|
||||||
final String message = "Hello World!";
|
|
||||||
restTemplate.postForEntity("/broadcast", message, Void.class);
|
|
||||||
|
|
||||||
verify(rabbitTemplate).convertAndSend(BroadcastConfig.fanoutExchangeName, "", message);
|
|
||||||
verify(rabbitTemplate).convertAndSend(BroadcastConfig.topicExchangeName, "user.not-important.info", message);
|
|
||||||
verify(rabbitTemplate).convertAndSend(BroadcastConfig.topicExchangeName, "user.important.error", message);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,46 +0,0 @@
|
|||||||
package com.baeldung.springamqpsimple;
|
|
||||||
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
|
||||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.test.context.ActiveProfiles;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.mockito.Mockito.verify;
|
|
||||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@ActiveProfiles("test")
|
|
||||||
@SpringBootTest(webEnvironment = RANDOM_PORT)
|
|
||||||
public class MessageControllerIntegrationTest {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private TestRestTemplate restTemplate;
|
|
||||||
|
|
||||||
@MockBean
|
|
||||||
private RabbitTemplate rabbitTemplate;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenPostingMessage_thenMessageIsCreated() {
|
|
||||||
final String message = "Hello World!";
|
|
||||||
ResponseEntity<Void> responseEntity = restTemplate.postForEntity("/messages", message, Void.class);
|
|
||||||
|
|
||||||
assertEquals(HttpStatus.CREATED, responseEntity.getStatusCode());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenPostingMessage_thenMessageIsSentToBroker() {
|
|
||||||
final String message = "Hello World!";
|
|
||||||
restTemplate.postForEntity("/messages", message, Void.class);
|
|
||||||
|
|
||||||
verify(rabbitTemplate).convertAndSend(SpringAmqpConfig.queueName, message);
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.baeldung.domain;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Article {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private Integer id;
|
||||||
|
@Temporal(TemporalType.DATE)
|
||||||
|
private Date publicationDate;
|
||||||
|
@Temporal(TemporalType.TIME)
|
||||||
|
private Date publicationTime;
|
||||||
|
@Temporal(TemporalType.TIMESTAMP)
|
||||||
|
private Date creationDateTime;
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.repository;
|
||||||
|
|
||||||
|
import com.baeldung.domain.Article;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface ArticleRepository extends JpaRepository<Article, Integer> {
|
||||||
|
|
||||||
|
List<Article> findAllByPublicationDate(Date publicationDate);
|
||||||
|
|
||||||
|
List<Article> findAllByPublicationTimeBetween(Date publicationTimeStart,
|
||||||
|
Date publicationTimeEnd);
|
||||||
|
|
||||||
|
@Query("select a from Article a where a.creationDateTime <= :creationDateTime")
|
||||||
|
List<Article> findAllWithCreationDateTimeBefore(
|
||||||
|
@Param("creationDateTime") Date creationDateTime);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,66 @@
|
|||||||
|
package com.baeldung.repository;
|
||||||
|
|
||||||
|
import com.baeldung.domain.Article;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@DataJpaTest
|
||||||
|
public class ArticleRepositoryIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ArticleRepository repository;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenImportedArticlesWhenFindAllByPublicationDateThenArticles1And2Returned()
|
||||||
|
throws Exception {
|
||||||
|
List<Article> result = repository.findAllByPublicationDate(
|
||||||
|
new SimpleDateFormat("yyyy-MM-dd").parse("2018-01-01")
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(2, result.size());
|
||||||
|
assertTrue(result.stream()
|
||||||
|
.map(Article::getId)
|
||||||
|
.allMatch(id -> Arrays.asList(1, 2).contains(id))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenImportedArticlesWhenFindAllByPublicationTimeBetweenThenArticles2And3Returned()
|
||||||
|
throws Exception {
|
||||||
|
List<Article> result = repository.findAllByPublicationTimeBetween(
|
||||||
|
new SimpleDateFormat("HH:mm").parse("15:15"),
|
||||||
|
new SimpleDateFormat("HH:mm").parse("16:30")
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(2, result.size());
|
||||||
|
assertTrue(result.stream()
|
||||||
|
.map(Article::getId)
|
||||||
|
.allMatch(id -> Arrays.asList(2, 3).contains(id))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenImportedArticlesWhenFindAllWithCreationDateTimeBeforeThenArticles2And3Returned() throws Exception {
|
||||||
|
List<Article> result = repository.findAllWithCreationDateTimeBefore(
|
||||||
|
new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2017-12-15 10:00")
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(2, result.size());
|
||||||
|
assertTrue(result.stream()
|
||||||
|
.map(Article::getId)
|
||||||
|
.allMatch(id -> Arrays.asList(2, 3).contains(id))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -13,4 +13,4 @@ hibernate.cache.use_query_cache=true
|
|||||||
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
|
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
|
||||||
|
|
||||||
spring.jpa.properties.hibernate.hbm2ddl.import_files=migrated_users.sql
|
spring.jpa.properties.hibernate.hbm2ddl.import_files=migrated_users.sql
|
||||||
spring.datasource.data=import_*_users.sql
|
spring.datasource.data=import_*_users.sql,import_articles.sql
|
@ -0,0 +1,3 @@
|
|||||||
|
insert into Article(id, publication_date, publication_time, creation_date_time) values(1, TO_DATE('01/01/2018', 'DD/MM/YYYY'), TO_DATE('15:00', 'HH24:MI'), TO_DATE('31/12/2017 07:30', 'DD/MM/YYYY HH24:MI'));
|
||||||
|
insert into Article(id, publication_date, publication_time, creation_date_time) values(2, TO_DATE('01/01/2018', 'DD/MM/YYYY'), TO_DATE('15:30', 'HH24:MI'), TO_DATE('15/12/2017 08:00', 'DD/MM/YYYY HH24:MI'));
|
||||||
|
insert into Article(id, publication_date, publication_time, creation_date_time) values(3, TO_DATE('15/12/2017', 'DD/MM/YYYY'), TO_DATE('16:00', 'HH24:MI'), TO_DATE('01/12/2017 13:45', 'DD/MM/YYYY HH24:MI'));
|
@ -1,107 +0,0 @@
|
|||||||
package com.baeldung.relationships;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
import org.json.JSONArray;
|
|
||||||
import org.json.JSONException;
|
|
||||||
import org.json.JSONObject;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
|
||||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
|
||||||
import org.springframework.http.HttpEntity;
|
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
import org.springframework.http.HttpMethod;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
import com.baeldung.SpringDataRestApplication;
|
|
||||||
import com.baeldung.models.Address;
|
|
||||||
import com.baeldung.models.Author;
|
|
||||||
import com.baeldung.models.Book;
|
|
||||||
import com.baeldung.models.Library;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest(classes = SpringDataRestApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
|
||||||
public class SpringDataRelationshipsIntegrationTest {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private TestRestTemplate template;
|
|
||||||
|
|
||||||
@Value("${local.server.port}")
|
|
||||||
private int port;
|
|
||||||
|
|
||||||
private static final String BOOK_ENDPOINT = "http://localhost:%s/books/";
|
|
||||||
private static final String AUTHOR_ENDPOINT = "http://localhost:%s/authors/";
|
|
||||||
private static final String ADDRESS_ENDPOINT = "http://localhost:%s/addresses/";
|
|
||||||
private static final String LIBRARY_ENDPOINT = "http://localhost:%s/libraries/";
|
|
||||||
|
|
||||||
private static final String LIBRARY_NAME = "My Library";
|
|
||||||
private static final String AUTHOR_NAME = "George Orwell";
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenSaveOneToOneRelationship_thenCorrect() throws JSONException {
|
|
||||||
Library library = new Library(LIBRARY_NAME);
|
|
||||||
template.postForEntity(String.format(LIBRARY_ENDPOINT, port), library, Library.class);
|
|
||||||
|
|
||||||
Address address = new Address("Main street, nr 1");
|
|
||||||
template.postForEntity(String.format(ADDRESS_ENDPOINT, port), address, Address.class);
|
|
||||||
|
|
||||||
HttpHeaders requestHeaders = new HttpHeaders();
|
|
||||||
requestHeaders.add("Content-type", "text/uri-list");
|
|
||||||
HttpEntity<String> httpEntity = new HttpEntity<>(String.format(ADDRESS_ENDPOINT, port) + "/1", requestHeaders);
|
|
||||||
template.exchange(String.format(LIBRARY_ENDPOINT, port) + "/1/libraryAddress", HttpMethod.PUT, httpEntity, String.class);
|
|
||||||
|
|
||||||
ResponseEntity<Library> libraryGetResponse = template.getForEntity(String.format(ADDRESS_ENDPOINT, port) + "/1/library", Library.class);
|
|
||||||
assertEquals("library is incorrect", libraryGetResponse.getBody()
|
|
||||||
.getName(), LIBRARY_NAME);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenSaveOneToManyRelationship_thenCorrect() throws JSONException{
|
|
||||||
Library library = new Library(LIBRARY_NAME);
|
|
||||||
template.postForEntity(String.format(LIBRARY_ENDPOINT, port), library, Library.class);
|
|
||||||
|
|
||||||
Book book1 = new Book("Dune");
|
|
||||||
template.postForEntity(String.format(BOOK_ENDPOINT, port), book1, Book.class);
|
|
||||||
|
|
||||||
Book book2 = new Book("1984");
|
|
||||||
template.postForEntity(String.format(BOOK_ENDPOINT, port), book2, Book.class);
|
|
||||||
|
|
||||||
HttpHeaders requestHeaders = new HttpHeaders();
|
|
||||||
requestHeaders.add("Content-type", "text/uri-list");
|
|
||||||
HttpEntity<String> bookHttpEntity = new HttpEntity<>(String.format(LIBRARY_ENDPOINT, port) + "/1", requestHeaders);
|
|
||||||
template.exchange(String.format(BOOK_ENDPOINT, port) + "/1/library", HttpMethod.PUT, bookHttpEntity, String.class);
|
|
||||||
template.exchange(String.format(BOOK_ENDPOINT, port) + "/2/library", HttpMethod.PUT, bookHttpEntity, String.class);
|
|
||||||
|
|
||||||
ResponseEntity<Library> libraryGetResponse = template.getForEntity(String.format(BOOK_ENDPOINT, port) + "/1/library", Library.class);
|
|
||||||
assertEquals("library is incorrect", libraryGetResponse.getBody()
|
|
||||||
.getName(), LIBRARY_NAME);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenSaveManyToManyRelationship_thenCorrect() throws JSONException{
|
|
||||||
Author author1 = new Author(AUTHOR_NAME);
|
|
||||||
template.postForEntity(String.format(AUTHOR_ENDPOINT, port), author1, Author.class);
|
|
||||||
|
|
||||||
Book book1 = new Book("Animal Farm");
|
|
||||||
template.postForEntity(String.format(BOOK_ENDPOINT, port), book1, Book.class);
|
|
||||||
|
|
||||||
Book book2 = new Book("1984");
|
|
||||||
template.postForEntity(String.format(BOOK_ENDPOINT, port), book2, Book.class);
|
|
||||||
|
|
||||||
HttpHeaders requestHeaders = new HttpHeaders();
|
|
||||||
requestHeaders.add("Content-type", "text/uri-list");
|
|
||||||
HttpEntity<String> httpEntity = new HttpEntity<>(String.format(BOOK_ENDPOINT, port) + "/1\n" + String.format(BOOK_ENDPOINT, port) + "/2", requestHeaders);
|
|
||||||
template.exchange(String.format(AUTHOR_ENDPOINT, port) + "/1/books", HttpMethod.PUT, httpEntity, String.class);
|
|
||||||
|
|
||||||
String jsonResponse = template.getForObject(String.format(BOOK_ENDPOINT, port) + "/1/authors", String.class);
|
|
||||||
JSONObject jsonObj = new JSONObject(jsonResponse).getJSONObject("_embedded");
|
|
||||||
JSONArray jsonArray = jsonObj.getJSONArray("authors");
|
|
||||||
assertEquals("author is incorrect", jsonArray.getJSONObject(0)
|
|
||||||
.getString("name"), AUTHOR_NAME);
|
|
||||||
}
|
|
||||||
}
|
|
93
spring-reactive-kotlin/pom.xml
Normal file
93
spring-reactive-kotlin/pom.xml
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>springreactivekotlin</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>springreactivekotlin</name>
|
||||||
|
<description>Demo project for Spring Boot</description>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.0.3.RELEASE</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
<kotlin.version>1.2.41</kotlin.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.module</groupId>
|
||||||
|
<artifactId>jackson-module-kotlin</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-reflect</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.projectreactor</groupId>
|
||||||
|
<artifactId>reactor-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
|
||||||
|
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>kotlin-maven-plugin</artifactId>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<configuration>
|
||||||
|
<args>
|
||||||
|
<arg>-Xjsr305=strict</arg>
|
||||||
|
</args>
|
||||||
|
<compilerPlugins>
|
||||||
|
<plugin>spring</plugin>
|
||||||
|
</compilerPlugins>
|
||||||
|
</configuration>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-maven-allopen</artifactId>
|
||||||
|
<version>${kotlin.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.baeldung.springreactivekotlin
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
class Application
|
||||||
|
|
||||||
|
fun main(args: Array<String>) {
|
||||||
|
SpringApplication.run(Application::class.java, *args)
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.baeldung.springreactivekotlin
|
||||||
|
|
||||||
|
import org.springframework.http.MediaType
|
||||||
|
import org.springframework.stereotype.Controller
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody
|
||||||
|
import reactor.core.publisher.Flux
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
class Controller {
|
||||||
|
|
||||||
|
@GetMapping(path = ["/numbers"], produces = [MediaType.APPLICATION_STREAM_JSON_VALUE])
|
||||||
|
@ResponseBody
|
||||||
|
fun getNumbers(): Flux<Int> {
|
||||||
|
return Flux.range(1, 100)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.baeldung.springreactivekotlin
|
||||||
|
|
||||||
|
class Device(val name: String, val reading: Double) {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.baeldung.springreactivekotlin
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component
|
||||||
|
import org.springframework.web.reactive.function.BodyInserters.fromObject
|
||||||
|
import org.springframework.web.reactive.function.server.ServerRequest
|
||||||
|
import org.springframework.web.reactive.function.server.ServerResponse
|
||||||
|
import reactor.core.publisher.Mono
|
||||||
|
|
||||||
|
@Component
|
||||||
|
class HomeSensorsHandler {
|
||||||
|
|
||||||
|
var data = mapOf("lamp" to arrayOf(0.7, 0.65, 0.67), "fridge" to arrayOf(12.0, 11.9, 12.5))
|
||||||
|
|
||||||
|
fun setLight(request: ServerRequest): Mono<ServerResponse> = ServerResponse.ok().build()
|
||||||
|
|
||||||
|
fun getLightReading(request: ServerRequest): Mono<ServerResponse> =
|
||||||
|
ServerResponse.ok().body(fromObject(data["lamp"]!!))
|
||||||
|
|
||||||
|
fun getDeviceReadings(request: ServerRequest): Mono<ServerResponse> {
|
||||||
|
val id = request.pathVariable("id")
|
||||||
|
return ServerResponse.ok().body(fromObject(Device(id, 1.0)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getAllDevices(request: ServerRequest): Mono<ServerResponse> =
|
||||||
|
ServerResponse.ok().body(fromObject(arrayOf("lamp", "tv")))
|
||||||
|
|
||||||
|
fun getAllDeviceApi(request: ServerRequest): Mono<ServerResponse> =
|
||||||
|
ServerResponse.ok().body(fromObject(arrayListOf("kettle", "fridge")))
|
||||||
|
|
||||||
|
fun setDeviceReadingApi(request: ServerRequest): Mono<ServerResponse> {
|
||||||
|
return request.bodyToMono(Device::class.java).flatMap { it ->
|
||||||
|
ServerResponse.ok().body(fromObject(Device(it.name.toUpperCase(), it.reading)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package com.baeldung.springreactivekotlin
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean
|
||||||
|
import org.springframework.context.annotation.Configuration
|
||||||
|
import org.springframework.http.MediaType.APPLICATION_JSON
|
||||||
|
import org.springframework.http.MediaType.TEXT_HTML
|
||||||
|
import org.springframework.web.reactive.function.server.router
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
class HomeSensorsRouters(private val handler: HomeSensorsHandler) {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
fun roomsRouter() = router {
|
||||||
|
(accept(TEXT_HTML) and "/room").nest {
|
||||||
|
GET("/light", handler::getLightReading)
|
||||||
|
POST("/light", handler::setLight)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
fun deviceRouter() = router {
|
||||||
|
accept(TEXT_HTML).nest {
|
||||||
|
(GET("/device/") or GET("/devices/")).invoke(handler::getAllDevices)
|
||||||
|
GET("/device/{id}", handler::getDeviceReadings)
|
||||||
|
}
|
||||||
|
(accept(APPLICATION_JSON) and "/api").nest {
|
||||||
|
(GET("/device/") or GET("/devices/")).invoke(handler::getAllDeviceApi)
|
||||||
|
POST("/device/", handler::setDeviceReadingApi)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.baeldung.springreactivekotlin
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean
|
||||||
|
import org.springframework.context.annotation.Configuration
|
||||||
|
import org.springframework.web.reactive.function.server.ServerResponse
|
||||||
|
import org.springframework.web.reactive.function.server.router
|
||||||
|
|
||||||
|
import org.springframework.web.reactive.function.BodyInserters.fromObject
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
class SimpleRoute {
|
||||||
|
@Bean
|
||||||
|
fun route() = router {
|
||||||
|
GET("/route") { _ -> ServerResponse.ok().body(fromObject(arrayOf(1, 2, 3))) }
|
||||||
|
}
|
||||||
|
}
|
35
spring-reactive-kotlin/src/test/kotlin/RoutesTest.kt
Normal file
35
spring-reactive-kotlin/src/test/kotlin/RoutesTest.kt
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package veontomo
|
||||||
|
|
||||||
|
import com.baeldung.springreactivekotlin.SimpleRoute
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Test
|
||||||
|
import org.springframework.test.web.reactive.server.WebTestClient
|
||||||
|
|
||||||
|
class RoutesTest {
|
||||||
|
|
||||||
|
lateinit var client: WebTestClient
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun init() {
|
||||||
|
this.client = WebTestClient.bindToRouterFunction(SimpleRoute().route()).build()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenRequestToRoute_thenStatusShouldBeOk() {
|
||||||
|
client.get()
|
||||||
|
.uri("/route")
|
||||||
|
.exchange()
|
||||||
|
.expectStatus().isOk
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenRequestToRoute_thenBodyShouldContainArray123() {
|
||||||
|
client.get()
|
||||||
|
.uri("/route")
|
||||||
|
.exchange()
|
||||||
|
.expectBody()
|
||||||
|
.json("[1, 2, 3]")
|
||||||
|
}
|
||||||
|
}
|
@ -45,4 +45,19 @@ public class FooController {
|
|||||||
.setName("Foo Name")
|
.setName("Foo Name")
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequestMapping(method = RequestMethod.POST, value = "/foos/new")
|
||||||
|
@ResponseStatus(HttpStatus.OK)
|
||||||
|
@ResponseBody
|
||||||
|
public Foo createFoo(@RequestBody final Foo foo) {
|
||||||
|
return foo;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(method = RequestMethod.DELETE, value = "/foos/{id}")
|
||||||
|
@ResponseStatus(HttpStatus.OK)
|
||||||
|
@ResponseBody
|
||||||
|
public long deleteFoo(@PathVariable final long id) {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
14
spring-rest-template/.gitignore
vendored
Normal file
14
spring-rest-template/.gitignore
vendored
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
*.class
|
||||||
|
|
||||||
|
#folders#
|
||||||
|
/target
|
||||||
|
/neoDb*
|
||||||
|
/data
|
||||||
|
/src/main/webapp/WEB-INF/classes
|
||||||
|
*/META-INF/*
|
||||||
|
*/.idea/*
|
||||||
|
|
||||||
|
# Packaged files #
|
||||||
|
*.jar
|
||||||
|
*.war
|
||||||
|
*.ear
|
4
spring-rest-template/README.md
Normal file
4
spring-rest-template/README.md
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
## Spring REST Template Example Project
|
||||||
|
|
||||||
|
### The Course
|
||||||
|
The "REST With Spring" Classes: http://bit.ly/restwithspring
|
11
spring-rest-template/checkstyle.xml
Normal file
11
spring-rest-template/checkstyle.xml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE module PUBLIC
|
||||||
|
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
|
||||||
|
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
|
||||||
|
<module name="Checker">
|
||||||
|
<module name="TreeWalker">
|
||||||
|
<module name="AvoidStarImport">
|
||||||
|
<property name="severity" value="warning" />
|
||||||
|
</module>
|
||||||
|
</module>
|
||||||
|
</module>
|
83
spring-rest-template/pom.xml
Normal file
83
spring-rest-template/pom.xml
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>spring-rest-template</artifactId>
|
||||||
|
<version>0.1-SNAPSHOT</version>
|
||||||
|
<name>spring-rest-template</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<artifactId>parent-boot-2</artifactId>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../parent-boot-2</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<!-- Spring -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-web</artifactId>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<artifactId>commons-logging</artifactId>
|
||||||
|
<groupId>commons-logging</groupId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>spring-rest-template</finalName>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-checkstyle-plugin</artifactId>
|
||||||
|
<version>${checkstyle-maven-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<configLocation>checkstyle.xml</configLocation>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>check</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</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>**/*LongRunningUnitTest.java</exclude>
|
||||||
|
<exclude>**/*ManualTest.java</exclude>
|
||||||
|
<exclude>**/*LiveTest.java</exclude>
|
||||||
|
</excludes>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
<reporting>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-checkstyle-plugin</artifactId>
|
||||||
|
<version>${checkstyle-maven-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<configLocation>checkstyle.xml</configLocation>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</reporting>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<!-- Maven plugins -->
|
||||||
|
<checkstyle-maven-plugin.version>3.0.0</checkstyle-maven-plugin.version>
|
||||||
|
</properties>
|
||||||
|
</project>
|
@ -0,0 +1,62 @@
|
|||||||
|
package com.baeldung.web.upload.client;
|
||||||
|
|
||||||
|
import org.springframework.core.io.FileSystemResource;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
|
import org.springframework.http.HttpEntity;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
|
import org.springframework.util.MultiValueMap;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
public class MultipartFileUploadClient {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
uploadSingleFile();
|
||||||
|
uploadMultipleFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void uploadSingleFile() throws IOException {
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
||||||
|
|
||||||
|
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
||||||
|
body.add("file", getTestFile());
|
||||||
|
|
||||||
|
|
||||||
|
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
|
||||||
|
String serverUrl = "http://localhost:8082/spring-rest/fileserver/singlefileupload/";
|
||||||
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
|
ResponseEntity<String> response = restTemplate.postForEntity(serverUrl, requestEntity, String.class);
|
||||||
|
System.out.println("Response code: " + response.getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void uploadMultipleFile() throws IOException {
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
||||||
|
|
||||||
|
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
||||||
|
body.add("files", getTestFile());
|
||||||
|
body.add("files", getTestFile());
|
||||||
|
body.add("files", getTestFile());
|
||||||
|
|
||||||
|
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
|
||||||
|
String serverUrl = "http://localhost:8082/spring-rest/fileserver/multiplefileupload/";
|
||||||
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
|
ResponseEntity<String> response = restTemplate.postForEntity(serverUrl, requestEntity, String.class);
|
||||||
|
System.out.println("Response code: " + response.getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Resource getTestFile() throws IOException {
|
||||||
|
Path testFile = Files.createTempFile("test-file", ".txt");
|
||||||
|
System.out.println("Creating and Uploading Test File: " + testFile);
|
||||||
|
Files.write(testFile, "Hello World !!, This is a test file.".getBytes());
|
||||||
|
return new FileSystemResource(testFile.toFile());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,87 +0,0 @@
|
|||||||
package com.baeldung.springbootsecurityrest;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.MalformedURLException;
|
|
||||||
import java.net.URL;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.boot.context.embedded.LocalServerPort;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
import com.baeldung.springbootsecurityrest.basicauth.SpringBootSecurityApplication;
|
|
||||||
import com.baeldung.springbootsecurityrest.vo.User;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = SpringBootSecurityApplication.class)
|
|
||||||
public class BasicAuthConfigurationIntegrationTest {
|
|
||||||
|
|
||||||
TestRestTemplate restTemplate;
|
|
||||||
URL base;
|
|
||||||
|
|
||||||
@LocalServerPort int port;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() throws MalformedURLException {
|
|
||||||
restTemplate = new TestRestTemplate("user", "password");
|
|
||||||
base = new URL("http://localhost:" + port);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenCorrectCredentials_whenLogin_ThenSuccess() throws IllegalStateException, IOException {
|
|
||||||
restTemplate = new TestRestTemplate();
|
|
||||||
User user = new User();
|
|
||||||
user.setUserName("user");
|
|
||||||
user.setPassword("password");
|
|
||||||
ResponseEntity<String> response = restTemplate.postForEntity(base.toString()+"/login",user, String.class);
|
|
||||||
|
|
||||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
|
||||||
assertTrue(response
|
|
||||||
.getBody()
|
|
||||||
.contains("true"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenWrongCredentials_whenLogin_ThenReturnFalse() throws IllegalStateException, IOException {
|
|
||||||
restTemplate = new TestRestTemplate();
|
|
||||||
User user = new User();
|
|
||||||
user.setUserName("user");
|
|
||||||
user.setPassword("wrongpassword");
|
|
||||||
ResponseEntity<String> response = restTemplate.postForEntity(base.toString()+"/login",user, String.class);
|
|
||||||
|
|
||||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
|
||||||
assertTrue(response
|
|
||||||
.getBody()
|
|
||||||
.contains("false"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenLoggedInUser_whenRequestsHomePage_ThenSuccess() throws IllegalStateException, IOException {
|
|
||||||
ResponseEntity<String> response = restTemplate.getForEntity(base.toString()+"/user", String.class);
|
|
||||||
|
|
||||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
|
||||||
assertTrue(response
|
|
||||||
.getBody()
|
|
||||||
.contains("user"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenWrongCredentials_whenRequestsHomePage_ThenUnauthorized() throws IllegalStateException, IOException {
|
|
||||||
restTemplate = new TestRestTemplate("user", "wrongpassword");
|
|
||||||
ResponseEntity<String> response = restTemplate.getForEntity(base.toString()+"/user", String.class);
|
|
||||||
|
|
||||||
assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
|
|
||||||
assertTrue(response
|
|
||||||
.getBody()
|
|
||||||
.contains("Unauthorized"));
|
|
||||||
}
|
|
||||||
}
|
|
@ -10,9 +10,9 @@
|
|||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-spring-4</artifactId>
|
<artifactId>parent-spring-5</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../parent-spring-4</relativePath>
|
<relativePath>../parent-spring-5</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@ -22,12 +22,12 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.security</groupId>
|
<groupId>org.springframework.security</groupId>
|
||||||
<artifactId>spring-security-web</artifactId>
|
<artifactId>spring-security-web</artifactId>
|
||||||
<version>${org.springframework.security.version}</version>
|
<version>${spring.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.security</groupId>
|
<groupId>org.springframework.security</groupId>
|
||||||
<artifactId>spring-security-config</artifactId>
|
<artifactId>spring-security-config</artifactId>
|
||||||
<version>${org.springframework.security.version}</version>
|
<version>${spring.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- Spring -->
|
<!-- Spring -->
|
||||||
@ -96,7 +96,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
<artifactId>jackson-databind</artifactId>
|
<artifactId>jackson-databind</artifactId>
|
||||||
<version>${jackson.version}</version>
|
<version>${jackson-databind.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- http -->
|
<!-- http -->
|
||||||
@ -270,8 +270,6 @@
|
|||||||
</profiles>
|
</profiles>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<!-- Spring -->
|
|
||||||
<org.springframework.security.version>4.2.6.RELEASE</org.springframework.security.version>
|
|
||||||
|
|
||||||
<!-- http -->
|
<!-- http -->
|
||||||
<httpcore.version>4.4.5</httpcore.version>
|
<httpcore.version>4.4.5</httpcore.version>
|
||||||
@ -280,7 +278,6 @@
|
|||||||
<!-- various -->
|
<!-- various -->
|
||||||
<jstl.version>1.2</jstl.version>
|
<jstl.version>1.2</jstl.version>
|
||||||
<javax.servlet.version>3.1.0</javax.servlet.version>
|
<javax.servlet.version>3.1.0</javax.servlet.version>
|
||||||
<jackson.version>2.8.5</jackson.version>
|
|
||||||
|
|
||||||
<!-- util -->
|
<!-- util -->
|
||||||
<guava.version>19.0</guava.version>
|
<guava.version>19.0</guava.version>
|
||||||
|
@ -2,11 +2,14 @@ package org.baeldung.filter;
|
|||||||
|
|
||||||
import org.baeldung.security.RestAuthenticationEntryPoint;
|
import org.baeldung.security.RestAuthenticationEntryPoint;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
|
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ -20,7 +23,7 @@ public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAda
|
|||||||
auth
|
auth
|
||||||
.inMemoryAuthentication()
|
.inMemoryAuthentication()
|
||||||
.withUser("user1")
|
.withUser("user1")
|
||||||
.password("user1Pass")
|
.password(passwordEncoder().encode("user1Pass"))
|
||||||
.authorities("ROLE_USER");
|
.authorities("ROLE_USER");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,4 +41,9 @@ public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAda
|
|||||||
|
|
||||||
http.addFilterAfter(new CustomFilter(), BasicAuthenticationFilter.class);
|
http.addFilterAfter(new CustomFilter(), BasicAuthenticationFilter.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public PasswordEncoder passwordEncoder() {
|
||||||
|
return new BCryptPasswordEncoder();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user