Commit ab335a1a43007835225a824521818506ae42bed3

Authored by Thomas Ziemer
1 parent c77e2144

refactor: läuft jetzt

.gitignore
1 .classpath 1 .classpath
2 .project 2 .project
3 .settings/ 3 .settings/
  4 +.idea/
4 target/ 5 target/
  6 +*.iml
@@ -40,12 +40,12 @@ @@ -40,12 +40,12 @@
40 <groupId>org.slf4j</groupId> 40 <groupId>org.slf4j</groupId>
41 <artifactId>slf4j-api</artifactId> 41 <artifactId>slf4j-api</artifactId>
42 <version>${slf4j-version}</version> 42 <version>${slf4j-version}</version>
43 - <scope>provided</scope>  
44 </dependency> 43 </dependency>
45 <dependency> 44 <dependency>
46 <groupId>org.mockito</groupId> 45 <groupId>org.mockito</groupId>
47 <artifactId>mockito-core</artifactId> 46 <artifactId>mockito-core</artifactId>
48 <version>${mockito-version}</version> 47 <version>${mockito-version}</version>
  48 + <scope>test</scope>
49 </dependency> 49 </dependency>
50 <dependency> 50 <dependency>
51 <groupId>org.dbunit</groupId> 51 <groupId>org.dbunit</groupId>
@@ -62,7 +62,7 @@ @@ -62,7 +62,7 @@
62 <artifactId>mysql-connector-java</artifactId> 62 <artifactId>mysql-connector-java</artifactId>
63 <version>${mysql-version}</version> 63 <version>${mysql-version}</version>
64 </dependency> 64 </dependency>
65 - </dependencies> 65 + </dependencies>
66 <build> 66 <build>
67 <finalName>swXercise</finalName> 67 <finalName>swXercise</finalName>
68 68
src/main/java/net/ziemers/swxercise/db/BaseEntity.java
@@ -2,7 +2,9 @@ package net.ziemers.swxercise.db; @@ -2,7 +2,9 @@ package net.ziemers.swxercise.db;
2 2
3 import javax.persistence.GeneratedValue; 3 import javax.persistence.GeneratedValue;
4 import javax.persistence.Id; 4 import javax.persistence.Id;
  5 +import javax.persistence.MappedSuperclass;
5 6
  7 +@MappedSuperclass
6 public class BaseEntity { 8 public class BaseEntity {
7 9
8 @Id 10 @Id
src/main/java/net/ziemers/swxercise/db/dao/GenericDao.java
@@ -3,19 +3,19 @@ package net.ziemers.swxercise.db.dao; @@ -3,19 +3,19 @@ package net.ziemers.swxercise.db.dao;
3 import java.util.Collection; 3 import java.util.Collection;
4 4
5 import javax.ejb.Stateless; 5 import javax.ejb.Stateless;
6 -import javax.inject.Inject;  
7 import javax.persistence.EntityManager; 6 import javax.persistence.EntityManager;
  7 +import javax.persistence.PersistenceContext;
  8 +import javax.persistence.TypedQuery;
8 import javax.persistence.criteria.CriteriaBuilder; 9 import javax.persistence.criteria.CriteriaBuilder;
9 import javax.persistence.criteria.CriteriaQuery; 10 import javax.persistence.criteria.CriteriaQuery;
10 -// import javax.persistence.criteria.Predicate;  
11 -// import javax.persistence.criteria.Root; 11 +import javax.persistence.criteria.Root;
12 12
13 import net.ziemers.swxercise.db.BaseEntity; 13 import net.ziemers.swxercise.db.BaseEntity;
14 14
15 @Stateless 15 @Stateless
16 public class GenericDao { 16 public class GenericDao {
17 17
18 - @Inject 18 + @PersistenceContext
19 private EntityManager entityManager; 19 private EntityManager entityManager;
20 20
21 /** 21 /**
@@ -42,22 +42,22 @@ public class GenericDao { @@ -42,22 +42,22 @@ public class GenericDao {
42 * @return die Entität mit gegebener Id oder <code>null</code>, wenn keine gefunden 42 * @return die Entität mit gegebener Id oder <code>null</code>, wenn keine gefunden
43 */ 43 */
44 public <T extends BaseEntity> T findById(Class<T> entityType, Long primaryKey) { 44 public <T extends BaseEntity> T findById(Class<T> entityType, Long primaryKey) {
45 - T entity = entityManager.find(entityType, primaryKey); 45 + final T entity = entityManager.find(entityType, primaryKey);
46 return entity; 46 return entity;
47 } 47 }
48 48
49 /** 49 /**
50 * @param entityType Entity-Typ. 50 * @param entityType Entity-Typ.
51 * @param <T> Generic für den Entity-Typ. 51 * @param <T> Generic für den Entity-Typ.
52 - * @return alle (aktiven) Entitäten vom gegebenen Typ 52 + * @return alle Entitäten vom gegebenen Typ
53 */ 53 */
54 public <T extends BaseEntity> Collection<T> findAll(Class<T> entityType) { 54 public <T extends BaseEntity> Collection<T> findAll(Class<T> entityType) {
55 CriteriaBuilder cb = entityManager.getCriteriaBuilder(); 55 CriteriaBuilder cb = entityManager.getCriteriaBuilder();
56 - CriteriaQuery<T> query = cb.createQuery(entityType);  
57 - // Root<T> root = query.from(entityType);  
58 - // Predicate activeStatus = cb.equal(root.get(STATUS), Status.ACTIVE);  
59 - // query.where(activeStatus);  
60 - return entityManager.createQuery(query).getResultList(); 56 + CriteriaQuery<T> cq = cb.createQuery(entityType);
  57 + Root<T> root = cq.from(entityType);
  58 + CriteriaQuery<T> selectAll = cq.select(root);
  59 + TypedQuery<T> query = entityManager.createQuery(selectAll);
  60 + return query.getResultList();
61 } 61 }
62 62
63 } 63 }
src/main/java/net/ziemers/swxercise/db/utils/JpaTestUtils.java
@@ -27,7 +27,6 @@ import org.dbunit.ext.mysql.MySqlDataTypeFactory; @@ -27,7 +27,6 @@ import org.dbunit.ext.mysql.MySqlDataTypeFactory;
27 import org.dbunit.ext.mysql.MySqlMetadataHandler; 27 import org.dbunit.ext.mysql.MySqlMetadataHandler;
28 import org.dbunit.operation.DatabaseOperation; 28 import org.dbunit.operation.DatabaseOperation;
29 import org.hibernate.internal.SessionImpl; 29 import org.hibernate.internal.SessionImpl;
30 -import org.junit.After;  
31 import org.slf4j.Logger; 30 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory; 31 import org.slf4j.LoggerFactory;
33 32
@@ -254,15 +253,4 @@ public class JpaTestUtils { @@ -254,15 +253,4 @@ public class JpaTestUtils {
254 return String.format("DELETE FROM %s.%s;", schema, table); 253 return String.format("DELETE FROM %s.%s;", schema, table);
255 } 254 }
256 255
257 - /**  
258 - * Rollt eine jetzt noch aktive Transaktion zurück.  
259 - */  
260 - @After  
261 - public void tearDown() {  
262 - EntityTransaction transaction = getEm().getTransaction();  
263 - if (transaction.isActive()) {  
264 - transaction.rollback();  
265 - }  
266 - }  
267 -  
268 } 256 }
src/main/java/net/ziemers/swxercise/lg/user/service/UserService.java
@@ -5,7 +5,6 @@ import java.util.Collection; @@ -5,7 +5,6 @@ import java.util.Collection;
5 import javax.ejb.Stateless; 5 import javax.ejb.Stateless;
6 import javax.inject.Inject; 6 import javax.inject.Inject;
7 7
8 -import net.ziemers.swxercise.db.BaseEntity;  
9 import net.ziemers.swxercise.db.dao.GenericDao; 8 import net.ziemers.swxercise.db.dao.GenericDao;
10 import net.ziemers.swxercise.lg.user.User; 9 import net.ziemers.swxercise.lg.user.User;
11 10
@@ -15,15 +14,13 @@ public class UserService { @@ -15,15 +14,13 @@ public class UserService {
15 @Inject 14 @Inject
16 private GenericDao genericDao; 15 private GenericDao genericDao;
17 16
18 - public User findUser(final Long id) {  
19 - return genericDao.findById(User.class, id);  
20 - } 17 + public User findUser(final Long id) { return genericDao.findById(User.class, id); }
21 18
22 public Collection<User> findAllUsers() { 19 public Collection<User> findAllUsers() {
23 return genericDao.findAll(User.class); 20 return genericDao.findAll(User.class);
24 } 21 }
25 22
26 - public <T extends BaseEntity> Long saveUser(final User user) { 23 + public Long saveUser(final User user) {
27 return genericDao.save(user); 24 return genericDao.save(user);
28 } 25 }
29 26
src/main/java/net/ziemers/swxercise/ui/RestController.java
@@ -2,7 +2,6 @@ package net.ziemers.swxercise.ui; @@ -2,7 +2,6 @@ package net.ziemers.swxercise.ui;
2 2
3 import java.util.Collection; 3 import java.util.Collection;
4 4
5 -import javax.annotation.PostConstruct;  
6 import javax.enterprise.context.ApplicationScoped; 5 import javax.enterprise.context.ApplicationScoped;
7 import javax.inject.Inject; 6 import javax.inject.Inject;
8 import javax.ws.rs.GET; 7 import javax.ws.rs.GET;
@@ -13,8 +12,6 @@ import javax.ws.rs.core.MediaType; @@ -13,8 +12,6 @@ import javax.ws.rs.core.MediaType;
13 12
14 import net.ziemers.swxercise.lg.user.User; 13 import net.ziemers.swxercise.lg.user.User;
15 import net.ziemers.swxercise.lg.user.service.UserService; 14 import net.ziemers.swxercise.lg.user.service.UserService;
16 -import org.slf4j.Logger;  
17 -import org.slf4j.LoggerFactory;  
18 15
19 @ApplicationScoped 16 @ApplicationScoped
20 @Path(RestController.webContextPath) 17 @Path(RestController.webContextPath)
@@ -22,28 +19,19 @@ public class RestController { @@ -22,28 +19,19 @@ public class RestController {
22 19
23 static final String webContextPath = "/user"; 20 static final String webContextPath = "/user";
24 21
25 - private Logger logger;  
26 -  
27 @Inject 22 @Inject
28 private UserService userService; 23 private UserService userService;
29 24
30 - @PostConstruct  
31 - public void init() {  
32 - logger = LoggerFactory.getLogger(RestController.class);  
33 - }  
34 -  
35 /** 25 /**
36 * Schickt eine Hello-Nachricht zum Aufrufer zurück. 26 * Schickt eine Hello-Nachricht zum Aufrufer zurück.
37 - *  
38 - * @param name  
39 - * der Name, der in der Hallo-Nachricht angegeben wird 27 + *
  28 + * @param name der Name, der in der Hallo-Nachricht angegeben wird
40 * @return eine Hallo-Nachricht 29 * @return eine Hallo-Nachricht
41 */ 30 */
42 @GET 31 @GET
43 @Path("/hello/{name}") 32 @Path("/hello/{name}")
44 @Produces(MediaType.TEXT_PLAIN) 33 @Produces(MediaType.TEXT_PLAIN)
45 public String helloPath(@PathParam("name") String name) { 34 public String helloPath(@PathParam("name") String name) {
46 - logger.info("Hello {}", name);  
47 return String.format("Hello %s", name); 35 return String.format("Hello %s", name);
48 } 36 }
49 37
src/main/resources/META-INF/persistence.xml
@@ -8,40 +8,4 @@ @@ -8,40 +8,4 @@
8 <property name="hibernate.hbm2ddl.auto" value="update" /> 8 <property name="hibernate.hbm2ddl.auto" value="update" />
9 </properties> 9 </properties>
10 </persistence-unit> 10 </persistence-unit>
11 - <persistence-unit name="swXerciseTestPU"  
12 - transaction-type="RESOURCE_LOCAL">  
13 - <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>  
14 - <exclude-unlisted-classes>false</exclude-unlisted-classes>  
15 -  
16 - <properties>  
17 - <!-- Property for WildFly to ignore this -->  
18 - <property name="jboss.as.jpa.managed" value="false" />  
19 - <!-- Properties for Hibernate -->  
20 - <property name="javax.persistence.jdbc.url"  
21 - value="jdbc:mariadb://${abisnext.test.host}:${abisnext.test.port}/${abisnext.test.schema}" />  
22 - <property name="javax.persistence.jdbc.user" value="${abisnext.test.user}" />  
23 - <property name="javax.persistence.jdbc.password" value="${abisnext.test.password}" />  
24 - <property name="javax.persistence.jdbc.driver" value="org.mariadb.jdbc.Driver" />  
25 - <property name="hibernate.show_sql" value="false" />  
26 - <property name="hibernate.format_sql" value="false" />  
27 - <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />  
28 - <!--<property name="hibernate.hbm2ddl.auto" value="create-drop" /> -->  
29 - <property name="hibernate.connection.useUnicode" value="true" />  
30 - <property name="hibernate.connection.characterEncoding"  
31 - value="UTF-8" />  
32 - <property name="hibernate.connection.provider_class"  
33 - value="org.hibernate.connection.C3P0ConnectionProvider" />  
34 -  
35 - <property name="hibernate.c3p0.min_size" value="2" />  
36 - <property name="hibernate.c3p0.max_size" value="100" />  
37 - <property name="hibernate.c3p0.timeout" value="10" />  
38 - <property name="hibernate.c3p0.max_statements" value="100" />  
39 - <property name="hibernate.c3p0.idle_test_period" value="30" />  
40 -  
41 - <property name="hibernate.temp.use_jdbc_metadata_defaults"  
42 - value="false" />  
43 - <property name="hibernate.ejb.entitymanager_factory_name"  
44 - value="abisnext_test" />  
45 - </properties>  
46 - </persistence-unit>  
47 </persistence> 11 </persistence>
48 \ No newline at end of file 12 \ No newline at end of file
src/test/java/net/ziemers/swxercise/ui/RestControllerTest.java
1 package net.ziemers.swxercise.ui; 1 package net.ziemers.swxercise.ui;
2 2
3 -import net.ziemers.swxercise.ui.RestController; 3 +import org.junit.Assert;
4 import org.junit.Test; 4 import org.junit.Test;
5 import org.junit.runner.RunWith; 5 import org.junit.runner.RunWith;
6 import org.mockito.InjectMocks; 6 import org.mockito.InjectMocks;
@@ -14,7 +14,12 @@ public class RestControllerTest { @@ -14,7 +14,12 @@ public class RestControllerTest {
14 14
15 @Test 15 @Test
16 public void test() { 16 public void test() {
17 - 17 + final String name = "Tom";
  18 + final String expected = "Hello " + name;
  19 + String actual;
  20 +
  21 + actual = underTest.helloPath(name);
  22 + Assert.assertEquals(expected, actual);
18 } 23 }
19 24
20 } 25 }