Merge branch 'master' into avro-error
This commit is contained in:
@@ -48,3 +48,12 @@ dependency-reduced-pom.xml
|
||||
*.so
|
||||
*.dylib
|
||||
*.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
|
||||
====================
|
||||
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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
+67
@@ -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()));
|
||||
}
|
||||
|
||||
}
|
||||
+68
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+3
-14
@@ -50,22 +50,9 @@ public class HashSetInitalizingUnitTest {
|
||||
|
||||
@Test
|
||||
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());
|
||||
}
|
||||
@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
|
||||
public void whenUsingGoogleGuava_createMutableSet_thenCorrectSize() {
|
||||
@@ -78,4 +65,6 @@ public class HashSetInitalizingUnitTest {
|
||||
Set<String> set = ImmutableSet.of("a", "b", "c");
|
||||
assertEquals(3, set.size());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
+45
-5
@@ -1,5 +1,5 @@
|
||||
<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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>core-java</artifactId>
|
||||
@@ -73,9 +73,10 @@
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
@@ -146,6 +147,32 @@
|
||||
<artifactId>icu4j</artifactId>
|
||||
<version>${icu4j.version}</version>
|
||||
</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>
|
||||
|
||||
<build>
|
||||
@@ -301,7 +328,7 @@
|
||||
</arguments>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
@@ -348,6 +375,7 @@
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<version>${exec-maven-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>run-benchmarks</id>
|
||||
@@ -375,6 +403,7 @@
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
|
||||
<!-- marshalling -->
|
||||
<jackson.version>2.8.5</jackson.version>
|
||||
<gson.version>2.8.2</gson.version>
|
||||
@@ -395,16 +424,23 @@
|
||||
<vavr.version>0.9.0</vavr.version>
|
||||
|
||||
<!-- 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-surefire-plugin.version>2.19.1</maven-surefire-plugin.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>
|
||||
|
||||
<javamoney.moneta.version>1.1</javamoney.moneta.version>
|
||||
<h2database.version>1.4.197</h2database.version>
|
||||
<esapi.version>2.1.0.1</esapi.version>
|
||||
<jmh-core.version>1.19</jmh-core.version>
|
||||
|
||||
<jmh-generator-annprocess.version>1.19</jmh-generator-annprocess.version>
|
||||
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.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>
|
||||
<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>
|
||||
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.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>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
||||
+87
@@ -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();
|
||||
}
|
||||
}
|
||||
+28
@@ -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(){}
|
||||
}
|
||||
+18
@@ -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();
|
||||
}
|
||||
+25
@@ -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(){}
|
||||
}
|
||||
+28
@@ -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(){}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Binary file not shown.
|
After Width: | Height: | Size: 54 KiB |
+5
-5
@@ -60,7 +60,6 @@
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-reflect</artifactId>
|
||||
<version>${kotlin-reflect.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlinx</groupId>
|
||||
@@ -224,10 +223,11 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<kotlin-maven-plugin.version>1.2.41</kotlin-maven-plugin.version>
|
||||
<kotlin-test-junit.version>1.2.41</kotlin-test-junit.version>
|
||||
<kotlin-stdlib.version>1.2.41</kotlin-stdlib.version>
|
||||
<kotlin-reflect.version>1.2.41</kotlin-reflect.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<kotlin-maven-plugin.version>1.2.51</kotlin-maven-plugin.version>
|
||||
<kotlin-test-junit.version>1.2.51</kotlin-test-junit.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>
|
||||
<ktor.io.version>0.9.2</ktor.io.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"
|
||||
}
|
||||
}
|
||||
+30
@@ -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
|
||||
}
|
||||
+57
-56
@@ -1,61 +1,62 @@
|
||||
<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>geotools</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>geotools</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
<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>
|
||||
<artifactId>geotools</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>geotools</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.geotools</groupId>
|
||||
<artifactId>gt-shapefile</artifactId>
|
||||
<version>${geotools-shapefile.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.geotools</groupId>
|
||||
<artifactId>gt-epsg-hsql</artifactId>
|
||||
<version>${geotools.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.geotools</groupId>
|
||||
<artifactId>gt-swing</artifactId>
|
||||
<version>${geotools-swing.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.geotools</groupId>
|
||||
<artifactId>gt-shapefile</artifactId>
|
||||
<version>${geotools-shapefile.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.geotools</groupId>
|
||||
<artifactId>gt-epsg-hsql</artifactId>
|
||||
<version>${geotools.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.geotools</groupId>
|
||||
<artifactId>gt-swing</artifactId>
|
||||
<version>${geotools-swing.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>maven2-repository.dev.java.net</id>
|
||||
<name>Java.net repository</name>
|
||||
<url>http://download.java.net/maven/2</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>osgeo</id>
|
||||
<name>Open Source Geospatial Foundation Repository</name>
|
||||
<url>http://download.osgeo.org/webdav/geotools/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
<id>opengeo</id>
|
||||
<name>OpenGeo Maven Repository</name>
|
||||
<url>http://repo.opengeo.org</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>maven2-repository.dev.java.net</id>
|
||||
<name>Java.net repository</name>
|
||||
<url>http://download.java.net/maven/2</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>osgeo</id>
|
||||
<name>Open Source Geospatial Foundation Repository</name>
|
||||
<url>http://download.osgeo.org/webdav/geotools/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
<id>opengeo</id>
|
||||
<name>OpenGeo Maven Repository</name>
|
||||
<url>http://repo.opengeo.org</url>
|
||||
</repository>
|
||||
</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>
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>hibernate5</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>hibernate5</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
@@ -59,9 +57,6 @@
|
||||
</build>
|
||||
|
||||
<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>
|
||||
<mysql.version>6.0.6</mysql.version>
|
||||
<mariaDB4j.version>2.2.3</mariaDB4j.version>
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user