Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
@@ -8,4 +8,4 @@
|
||||
- [Service Locator Pattern](http://www.baeldung.com/java-service-locator-pattern)
|
||||
- [Double-Checked Locking with Singleton](http://www.baeldung.com/java-singleton-double-checked-locking)
|
||||
- [Composite Design Pattern in Java](http://www.baeldung.com/java-composite-pattern)
|
||||
|
||||
- [Visitor Design Pattern in Java](http://www.baeldung.com/java-visitor-pattern)
|
||||
|
||||
@@ -50,18 +50,18 @@
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.11</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<version>6.0.6</version>
|
||||
<type>jar</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.grep4j</groupId>
|
||||
<artifactId>grep4j</artifactId>
|
||||
<version>${grep4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.grep4j</groupId>
|
||||
<artifactId>grep4j</artifactId>
|
||||
<version>${grep4j.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<properties>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.baeldung.daopattern.application;
|
||||
|
||||
import com.baeldung.daopattern.config.JpaEntityManagerFactory;
|
||||
import com.baeldung.daopattern.daos.Dao;
|
||||
import com.baeldung.daopattern.daos.JpaUserDao;
|
||||
import com.baeldung.daopattern.entities.User;
|
||||
@@ -21,17 +22,17 @@ public class UserApplication {
|
||||
getAllUsers().forEach(user -> System.out.println(user.getName()));
|
||||
}
|
||||
|
||||
private static class JpaUserDaoHolder {
|
||||
private static final JpaUserDao jpaUserDao = new JpaUserDao(new JpaEntityManagerFactory().getEntityManager());
|
||||
}
|
||||
|
||||
public static Dao getJpaUserDao() {
|
||||
if (jpaUserDao == null) {
|
||||
EntityManager entityManager = Persistence.createEntityManagerFactory("user-unit").createEntityManager();
|
||||
jpaUserDao = new JpaUserDao(entityManager);
|
||||
}
|
||||
return jpaUserDao;
|
||||
return JpaUserDaoHolder.jpaUserDao;
|
||||
}
|
||||
|
||||
public static User getUser(long id) {
|
||||
Optional<User> user = getJpaUserDao().get(id);
|
||||
return user.orElse(new User("Non-existing user", "no-email"));
|
||||
return user.orElseGet(()-> {return new User("non-existing user", "no-email");});
|
||||
}
|
||||
|
||||
public static List<User> getAllUsers() {
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.baeldung.daopattern.config;
|
||||
|
||||
import com.baeldung.daopattern.entities.User;
|
||||
import com.mysql.cj.jdbc.MysqlDataSource;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.sql.DataSource;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.spi.PersistenceUnitInfo;
|
||||
import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl;
|
||||
import org.hibernate.jpa.boot.internal.PersistenceUnitInfoDescriptor;
|
||||
|
||||
public class JpaEntityManagerFactory {
|
||||
|
||||
private final String DB_URL = "jdbc:mysql://databaseurl";
|
||||
private final String DB_USER_NAME = "username";
|
||||
private final String DB_PASSWORD = "password";
|
||||
|
||||
public EntityManager getEntityManager() {
|
||||
return getEntityManagerFactory().createEntityManager();
|
||||
}
|
||||
|
||||
protected EntityManagerFactory getEntityManagerFactory() {
|
||||
PersistenceUnitInfo persistenceUnitInfo = getPersistenceUnitInfo(getClass().getSimpleName());
|
||||
Map<String, Object> configuration = new HashMap<>();
|
||||
return new EntityManagerFactoryBuilderImpl(new PersistenceUnitInfoDescriptor(persistenceUnitInfo), configuration)
|
||||
.build();
|
||||
}
|
||||
|
||||
protected PersistenceUnitInfoImpl getPersistenceUnitInfo(String name) {
|
||||
return new PersistenceUnitInfoImpl(name, getEntityClassNames(), getProperties());
|
||||
}
|
||||
|
||||
protected List<String> getEntityClassNames() {
|
||||
return Arrays.asList(getEntities())
|
||||
.stream()
|
||||
.map(Class::getName)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
protected Properties getProperties() {
|
||||
Properties properties = new Properties();
|
||||
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
|
||||
properties.put("hibernate.id.new_generator_mappings", false);
|
||||
properties.put("hibernate.connection.datasource", getMysqlDataSource());
|
||||
return properties;
|
||||
}
|
||||
|
||||
protected Class[] getEntities() {
|
||||
return new Class[]{User.class};
|
||||
}
|
||||
|
||||
protected DataSource getMysqlDataSource() {
|
||||
MysqlDataSource mysqlDataSource = new MysqlDataSource();
|
||||
mysqlDataSource.setURL(DB_URL);
|
||||
mysqlDataSource.setUser(DB_USER_NAME);
|
||||
mysqlDataSource.setPassword(DB_PASSWORD);
|
||||
return mysqlDataSource;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
package com.baeldung.daopattern.config;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import javax.sql.DataSource;
|
||||
import javax.persistence.SharedCacheMode;
|
||||
import javax.persistence.ValidationMode;
|
||||
import javax.persistence.spi.ClassTransformer;
|
||||
import javax.persistence.spi.PersistenceUnitInfo;
|
||||
import javax.persistence.spi.PersistenceUnitTransactionType;
|
||||
import org.hibernate.jpa.HibernatePersistenceProvider;
|
||||
|
||||
public class PersistenceUnitInfoImpl implements PersistenceUnitInfo {
|
||||
|
||||
public static final String JPA_VERSION = "2.1";
|
||||
private final String persistenceUnitName;
|
||||
private PersistenceUnitTransactionType transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;
|
||||
private final List<String> managedClassNames;
|
||||
private final List<String> mappingFileNames = new ArrayList<>();
|
||||
private final Properties properties;
|
||||
private DataSource jtaDataSource;
|
||||
private DataSource nonJtaDataSource;
|
||||
|
||||
public PersistenceUnitInfoImpl(String persistenceUnitName, List<String> managedClassNames, Properties properties) {
|
||||
this.persistenceUnitName = persistenceUnitName;
|
||||
this.managedClassNames = managedClassNames;
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPersistenceUnitName() {
|
||||
return persistenceUnitName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPersistenceProviderClassName() {
|
||||
return HibernatePersistenceProvider.class.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PersistenceUnitTransactionType getTransactionType() {
|
||||
return transactionType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSource getJtaDataSource() {
|
||||
return jtaDataSource;
|
||||
}
|
||||
|
||||
public PersistenceUnitInfoImpl setJtaDataSource(DataSource jtaDataSource) {
|
||||
this.jtaDataSource = jtaDataSource;
|
||||
this.nonJtaDataSource = null;
|
||||
transactionType = PersistenceUnitTransactionType.JTA;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSource getNonJtaDataSource() {
|
||||
return nonJtaDataSource;
|
||||
}
|
||||
|
||||
public PersistenceUnitInfoImpl setNonJtaDataSource(DataSource nonJtaDataSource) {
|
||||
this.nonJtaDataSource = nonJtaDataSource;
|
||||
this.jtaDataSource = null;
|
||||
transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getMappingFileNames() {
|
||||
return mappingFileNames;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<URL> getJarFileUrls() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public URL getPersistenceUnitRootUrl() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getManagedClassNames() {
|
||||
return managedClassNames;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean excludeUnlistedClasses() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SharedCacheMode getSharedCacheMode() {
|
||||
return SharedCacheMode.UNSPECIFIED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ValidationMode getValidationMode() {
|
||||
return ValidationMode.AUTO;
|
||||
}
|
||||
|
||||
public Properties getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPersistenceXMLSchemaVersion() {
|
||||
return JPA_VERSION;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassLoader getClassLoader() {
|
||||
return Thread.currentThread().getContextClassLoader();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addTransformer(ClassTransformer transformer) {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassLoader getNewTempClassLoader() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,12 @@
|
||||
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
|
||||
<persistence-unit name="user-unit">
|
||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||
<class>com.baeldung.daopattern.entities.User</class>
|
||||
<class>com.baeldung.pattern.daopattern.entities.User</class>
|
||||
<properties>
|
||||
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
|
||||
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://url/to/database"/>
|
||||
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://databaseurl"/>
|
||||
<property name="javax.persistence.jdbc.user" value="username"/>
|
||||
<property name="javax.persistence.jdbc.password" value="passsword"/>
|
||||
<property name="javax.persistence.jdbc.password" value="password"/>
|
||||
<!-- Hibernate Properties -->
|
||||
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
|
||||
<property name="hibernate.show_sql" value="true"/>
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
|
||||
# Root logger
|
||||
log4j.rootLogger=INFO, file, stdout
|
||||
|
||||
# Write to console
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.Target=System.out
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
|
||||
@@ -1,10 +1,15 @@
|
||||
package com.baeldung.pattern.chainofresponsibility;
|
||||
package com.baeldung.chainofresponsibility;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.pattern.chainofresponsibility.AuthenticationProcessor;
|
||||
import com.baeldung.pattern.chainofresponsibility.OAuthAuthenticationProcessor;
|
||||
import com.baeldung.pattern.chainofresponsibility.OAuthTokenProvider;
|
||||
import com.baeldung.pattern.chainofresponsibility.UsernamePasswordProvider;
|
||||
import com.baeldung.pattern.chainofresponsibility.SamlAuthenticationProvider;
|
||||
import com.baeldung.pattern.chainofresponsibility.UsernamePasswordAuthenticationProcessor;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ChainOfResponsibilityTest {
|
||||
public class ChainOfResponsibilityIntegrationTest {
|
||||
|
||||
private static AuthenticationProcessor getChainOfAuthProcessor() {
|
||||
|
||||
@@ -13,7 +13,7 @@ import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class CarEngineFacadeTest {
|
||||
public class CarEngineFacadeIntegrationTest {
|
||||
|
||||
|
||||
private InMemoryCustomTestAppender appender;
|
||||
@@ -15,7 +15,7 @@ import org.junit.Test;
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
public class SingletonSynchronizationUnitTest {
|
||||
public class SingletonSynchronizationIntegrationTest {
|
||||
|
||||
/**
|
||||
* Size of the thread pools used.
|
||||
@@ -33,7 +33,7 @@ public class SingletonSynchronizationUnitTest {
|
||||
@Test
|
||||
public void givenDraconianSingleton_whenMultithreadInstancesEquals_thenTrue() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
|
||||
Set<DraconianSingleton> resultSet = Collections.synchronizedSet(new HashSet<DraconianSingleton>());
|
||||
Set<DraconianSingleton> resultSet = Collections.synchronizedSet(new HashSet<>());
|
||||
|
||||
// Submits the instantiation tasks.
|
||||
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
|
||||
@@ -51,7 +51,7 @@ public class SingletonSynchronizationUnitTest {
|
||||
@Test
|
||||
public void givenDclSingleton_whenMultithreadInstancesEquals_thenTrue() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
|
||||
Set<DclSingleton> resultSet = Collections.synchronizedSet(new HashSet<DclSingleton>());
|
||||
Set<DclSingleton> resultSet = Collections.synchronizedSet(new HashSet<>());
|
||||
|
||||
// Submits the instantiation tasks.
|
||||
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
|
||||
@@ -69,7 +69,7 @@ public class SingletonSynchronizationUnitTest {
|
||||
@Test
|
||||
public void givenEarlyInitSingleton_whenMultithreadInstancesEquals_thenTrue() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
|
||||
Set<EarlyInitSingleton> resultSet = Collections.synchronizedSet(new HashSet<EarlyInitSingleton>());
|
||||
Set<EarlyInitSingleton> resultSet = Collections.synchronizedSet(new HashSet<>());
|
||||
|
||||
// Submits the instantiation tasks.
|
||||
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
|
||||
@@ -87,7 +87,7 @@ public class SingletonSynchronizationUnitTest {
|
||||
@Test
|
||||
public void givenInitOnDemandSingleton_whenMultithreadInstancesEquals_thenTrue() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
|
||||
Set<InitOnDemandSingleton> resultSet = Collections.synchronizedSet(new HashSet<InitOnDemandSingleton>());
|
||||
Set<InitOnDemandSingleton> resultSet = Collections.synchronizedSet(new HashSet<>());
|
||||
|
||||
// Submits the instantiation tasks.
|
||||
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
|
||||
@@ -105,7 +105,7 @@ public class SingletonSynchronizationUnitTest {
|
||||
@Test
|
||||
public void givenEnumSingleton_whenMultithreadInstancesEquals_thenTrue() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
|
||||
Set<EnumSingleton> resultSet = Collections.synchronizedSet(new HashSet<EnumSingleton>());
|
||||
Set<EnumSingleton> resultSet = Collections.synchronizedSet(new HashSet<>());
|
||||
|
||||
// Submits the instantiation tasks.
|
||||
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.pattern.templatemethod.test;
|
||||
package com.baeldung.templatemethod.test;
|
||||
|
||||
import com.baeldung.pattern.templatemethod.model.Computer;
|
||||
import com.baeldung.pattern.templatemethod.model.HighEndComputerBuilder;
|
||||
@@ -10,7 +10,7 @@ import org.junit.Test;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class TemplateMethodPatternTest {
|
||||
public class TemplateMethodPatternIntegrationTest {
|
||||
|
||||
private static StandardComputerBuilder standardComputerBuilder;
|
||||
private static HighEndComputerBuilder highEndComputerBuilder;
|
||||
Reference in New Issue
Block a user