BAEL-16653 (#7691)
* added module 'design-patterns-behavioral' * added module 'design-patterns-structural' * added module 'design-patterns-creational' * added module 'design-patterns-architectural' * added module 'design-patterns-functional' * moved facade code examples from design-patterns to design-patterns-structural * moved singleton examples from design-patterns to design-patterns-creational * moved bridge examples from design-patterns to design-patterns-structural * moved creational examples from design-patterns to design-patterns-creational * moved observer examples from design-patterns to design-patterns-behavioral * moved flyweight examples from design-patterns to design-patterns-creational * moved service locator examples from design-patterns to design-patterns-architectural * moved double checked locking singleton examples from design-patterns to design-patterns-creational * moved composite examples from design-patterns to design-patterns-structural * moved visitor examples from design-patterns to design-patterns-behavioral * moved dao examples from design-patterns to design-patterns-architectural * moved interpreter examples from design-patterns to design-patterns-behavioral * moved state examples from design-patterns to design-patterns-behavioral * moved decorator examples from design-patterns to design-patterns-structural * renamed LogerUtil to LoggerUtil * moved template method examples from design-patterns to design-patterns-behavioral * moved chain of responsibility examples from design-patterns to design-patterns-behavioral * moved command examples from design-patterns to design-patterns-behavioral * moved constructor vs static factory method examples from design-patterns to design-patterns-creational * moved adapter examples from design-patterns to design-patterns-structural * moved currying examples from design-patterns to design-patterns-functional * moved proxy examples from design-patterns to design-patterns-structural * moved persistence.xml from design-patterns to design-patterns-architectural * deleted empty module: design-patterns * moved mediator examples from design-patterns-2 to design-patterns-behavioral * moved null object examples from design-patterns-2 to design-patterns-behavioral * moved null check examples from design-patterns to design-patterns-behavioral * moved freebuilder examples from design-patterns-2 to design-patterns-creational * added module design-patterns-behavioral-2 * moved memento examples from design-patterns-2 to design-patterns-behavioral-2 * removed empty module design-patterns-2 * changed http to https in readmes in modules design-patterns-*
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
### Relevant Articles:
|
||||
- [Service Locator Pattern](https://www.baeldung.com/java-service-locator-pattern)
|
||||
- [The DAO Pattern in Java](https://www.baeldung.com/java-dao-pattern)
|
||||
@@ -0,0 +1,54 @@
|
||||
<?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>
|
||||
<artifactId>design-patterns-architectural</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>design-patterns-architectural</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>patterns</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>..</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax</groupId>
|
||||
<artifactId>javaee-api</artifactId>
|
||||
<version>${javaee.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>${hibernate-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>${mysql-connector.version}</version>
|
||||
<type>jar</type>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
|
||||
<assertj-core.version>3.9.1</assertj-core.version>
|
||||
|
||||
<javaee.version>8.0</javaee.version>
|
||||
<hibernate-core.version>5.2.16.Final</hibernate-core.version>
|
||||
<mysql-connector.version>6.0.6</mysql-connector.version>
|
||||
</properties>
|
||||
</project>
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
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;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
public class UserApplication {
|
||||
|
||||
private static JpaUserDao jpaUserDao;
|
||||
|
||||
public static void main(String[] args) {
|
||||
User user1 = getUser(1);
|
||||
System.out.println(user1);
|
||||
updateUser(user1, new String[]{"John", "john@domain.com"});
|
||||
saveUser(new User("Monica", "monica@domain.com"));
|
||||
deleteUser(getUser(2));
|
||||
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() {
|
||||
return JpaUserDaoHolder.jpaUserDao;
|
||||
}
|
||||
|
||||
public static User getUser(long id) {
|
||||
Optional<User> user = getJpaUserDao().get(id);
|
||||
return user.orElseGet(()-> {return new User("non-existing user", "no-email");});
|
||||
}
|
||||
|
||||
public static List<User> getAllUsers() {
|
||||
return getJpaUserDao().getAll();
|
||||
}
|
||||
|
||||
public static void updateUser(User user, String[] params){
|
||||
getJpaUserDao().update(user, params);
|
||||
}
|
||||
|
||||
public static void saveUser(User user) {
|
||||
getJpaUserDao().save(user);
|
||||
}
|
||||
|
||||
public static void deleteUser(User user) {
|
||||
getJpaUserDao().delete(user);
|
||||
}
|
||||
}
|
||||
+65
@@ -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;
|
||||
}
|
||||
}
|
||||
+130
@@ -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;
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.daopattern.daos;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface Dao<T> {
|
||||
|
||||
Optional<T> get(long id);
|
||||
|
||||
List<T> getAll();
|
||||
|
||||
void save(T t);
|
||||
|
||||
void update(T t, String[] params);
|
||||
|
||||
void delete(T t);
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.daopattern.daos;
|
||||
|
||||
import com.baeldung.daopattern.entities.User;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityTransaction;
|
||||
import javax.persistence.Query;
|
||||
|
||||
public class JpaUserDao implements Dao<User> {
|
||||
|
||||
private final EntityManager entityManager;
|
||||
|
||||
public JpaUserDao(EntityManager entityManager) {
|
||||
this.entityManager = entityManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<User> get(long id) {
|
||||
return Optional.ofNullable(entityManager.find(User.class, id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> getAll() {
|
||||
Query query = entityManager.createQuery("SELECT e FROM User e");
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(User user) {
|
||||
executeInsideTransaction(entityManager -> entityManager.persist(user));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(User user, String[] params) {
|
||||
user.setName(Objects.requireNonNull(params[0], "Name cannot be null"));
|
||||
user.setEmail(Objects.requireNonNull(params[1], "Email cannot be null"));
|
||||
executeInsideTransaction(entityManager -> entityManager.merge(user));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(User user) {
|
||||
executeInsideTransaction(entityManager -> entityManager.remove(user));
|
||||
}
|
||||
|
||||
private void executeInsideTransaction(Consumer<EntityManager> action) {
|
||||
final EntityTransaction tx = entityManager.getTransaction();
|
||||
try {
|
||||
tx.begin();
|
||||
action.accept(entityManager);
|
||||
tx.commit();
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.daopattern.daos;
|
||||
|
||||
import com.baeldung.daopattern.entities.User;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
public class UserDao implements Dao<User> {
|
||||
|
||||
private List<User> users = new ArrayList<>();
|
||||
|
||||
public UserDao() {
|
||||
users.add(new User("John", "john@domain.com"));
|
||||
users.add(new User("Susan", "susan@domain.com"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<User> get(long id) {
|
||||
return Optional.ofNullable(users.get((int) id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> getAll() {
|
||||
return users;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(User user) {
|
||||
users.add(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(User user, String[] params) {
|
||||
user.setName(Objects.requireNonNull(params[0], "Name cannot be null"));
|
||||
user.setEmail(Objects.requireNonNull(params[1], "Email cannot be null"));
|
||||
users.add(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(User user) {
|
||||
users.remove(user);
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.daopattern.entities;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private long id;
|
||||
private String name;
|
||||
private String email;
|
||||
|
||||
public User(){}
|
||||
|
||||
public User(String name, String email) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.service.locator;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 4/20/2018.
|
||||
*/
|
||||
public class Cache {
|
||||
private List<MessagingService> services;
|
||||
|
||||
public Cache(){
|
||||
services = new ArrayList<MessagingService>();
|
||||
}
|
||||
|
||||
public MessagingService getService(String serviceName){
|
||||
|
||||
for (MessagingService service : services) {
|
||||
if(service.getServiceName().equalsIgnoreCase(serviceName)){
|
||||
System.out.println("Returning cached " + serviceName + " object");
|
||||
return service;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addService(MessagingService newService){
|
||||
boolean exists = false;
|
||||
|
||||
for (MessagingService service : services) {
|
||||
if(service.getServiceName().equalsIgnoreCase(newService.getServiceName())){
|
||||
exists = true;
|
||||
}
|
||||
}
|
||||
if(!exists){
|
||||
services.add(newService);
|
||||
}
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.service.locator;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 4/20/2018.
|
||||
*/
|
||||
public class EmailService implements MessagingService {
|
||||
|
||||
public String getMessageBody() {
|
||||
return "email message";
|
||||
}
|
||||
|
||||
public String getServiceName() {
|
||||
return "EmailService";
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.service.locator;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 4/20/2018.
|
||||
*/
|
||||
public class InitialContext {
|
||||
|
||||
public Object lookup(String serviceName) {
|
||||
|
||||
if (serviceName.equalsIgnoreCase("EmailService")) {
|
||||
return new EmailService();
|
||||
} else if (serviceName.equalsIgnoreCase("SMSService")) {
|
||||
return new SMSService();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.service.locator;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 4/20/2018.
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
MessagingService service = ServiceLocator.getService("EmailService");
|
||||
String email = service.getMessageBody();
|
||||
System.out.println(email);
|
||||
|
||||
service = ServiceLocator.getService("SMSService");
|
||||
String sms = service.getMessageBody();
|
||||
System.out.println(sms);
|
||||
|
||||
service = ServiceLocator.getService("EmailService");
|
||||
String newEmail = service.getMessageBody();
|
||||
System.out.println(newEmail);
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.service.locator;
|
||||
|
||||
public interface MessagingService {
|
||||
|
||||
String getMessageBody();
|
||||
|
||||
String getServiceName();
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.service.locator;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 4/20/2018.
|
||||
*/
|
||||
public class SMSService implements MessagingService {
|
||||
|
||||
public String getMessageBody() {
|
||||
return "sms message";
|
||||
}
|
||||
|
||||
public String getServiceName() {
|
||||
return "SMSService";
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.service.locator;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 4/20/2018.
|
||||
*/
|
||||
public class ServiceLocator {
|
||||
|
||||
private static Cache cache;
|
||||
|
||||
static {
|
||||
cache = new Cache();
|
||||
}
|
||||
|
||||
public static MessagingService getService(String serviceName){
|
||||
|
||||
MessagingService service = cache.getService(serviceName);
|
||||
|
||||
if(service != null){
|
||||
return service;
|
||||
}
|
||||
|
||||
InitialContext context = new InitialContext();
|
||||
MessagingService service1 = (MessagingService)context.lookup(serviceName);
|
||||
cache.addService(service1);
|
||||
return service1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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.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://databaseurl"/>
|
||||
<property name="javax.persistence.jdbc.user" value="username"/>
|
||||
<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"/>
|
||||
<property name="hibernate.format_sql" value="true"/>
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
</persistence>
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.daopattern.test;
|
||||
|
||||
import com.baeldung.daopattern.daos.UserDao;
|
||||
import com.baeldung.daopattern.entities.User;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UserDaoUnitTest {
|
||||
|
||||
private static UserDao userDao;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpUserDaoInstance() {
|
||||
userDao = new UserDao();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserDaoInstance_whenCalledget_thenOneAssertion() {
|
||||
assertThat(userDao.get(0)).isInstanceOf(Optional.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserDaoInstance_whenCalledgetAll_thenOneAssertion() {
|
||||
assertThat(userDao.getAll()).isInstanceOf(List.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserDaoInstance_whenCalledupdate_thenTwoAssertions() {
|
||||
User user = new User("Julie", "julie@domain.com");
|
||||
userDao.update(user, new String[] {"Julie", "julie@domain.com"});
|
||||
assertThat(userDao.get(2).get().getName()).isEqualTo("Julie");
|
||||
assertThat(userDao.get(2).get().getEmail()).isEqualTo("julie@domain.com");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserDaoInstance_whenCalledsave_thenTwoAssertions() {
|
||||
User user = new User("Julie", "julie@domain.com");
|
||||
userDao.save(user);
|
||||
assertThat(userDao.get(2).get().getName()).isEqualTo("Julie");
|
||||
assertThat(userDao.get(2).get().getEmail()).isEqualTo("julie@domain.com");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserDaoInstance_whenCalleddelete_thenOneAssertion() {
|
||||
User user = new User("Julie", "julie@domain.com");
|
||||
userDao.delete(user);
|
||||
assertThat(userDao.getAll().size()).isEqualTo(2);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user