Commit ab335a1a43007835225a824521818506ae42bed3
1 parent
c77e2144
refactor: läuft jetzt
Showing
9 changed files
with
28 additions
and
82 deletions
.gitignore
pom.xml
... | ... | @@ -40,12 +40,12 @@ |
40 | 40 | <groupId>org.slf4j</groupId> |
41 | 41 | <artifactId>slf4j-api</artifactId> |
42 | 42 | <version>${slf4j-version}</version> |
43 | - <scope>provided</scope> | |
44 | 43 | </dependency> |
45 | 44 | <dependency> |
46 | 45 | <groupId>org.mockito</groupId> |
47 | 46 | <artifactId>mockito-core</artifactId> |
48 | 47 | <version>${mockito-version}</version> |
48 | + <scope>test</scope> | |
49 | 49 | </dependency> |
50 | 50 | <dependency> |
51 | 51 | <groupId>org.dbunit</groupId> |
... | ... | @@ -62,7 +62,7 @@ |
62 | 62 | <artifactId>mysql-connector-java</artifactId> |
63 | 63 | <version>${mysql-version}</version> |
64 | 64 | </dependency> |
65 | - </dependencies> | |
65 | + </dependencies> | |
66 | 66 | <build> |
67 | 67 | <finalName>swXercise</finalName> |
68 | 68 | ... | ... |
src/main/java/net/ziemers/swxercise/db/BaseEntity.java
src/main/java/net/ziemers/swxercise/db/dao/GenericDao.java
... | ... | @@ -3,19 +3,19 @@ package net.ziemers.swxercise.db.dao; |
3 | 3 | import java.util.Collection; |
4 | 4 | |
5 | 5 | import javax.ejb.Stateless; |
6 | -import javax.inject.Inject; | |
7 | 6 | import javax.persistence.EntityManager; |
7 | +import javax.persistence.PersistenceContext; | |
8 | +import javax.persistence.TypedQuery; | |
8 | 9 | import javax.persistence.criteria.CriteriaBuilder; |
9 | 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 | 13 | import net.ziemers.swxercise.db.BaseEntity; |
14 | 14 | |
15 | 15 | @Stateless |
16 | 16 | public class GenericDao { |
17 | 17 | |
18 | - @Inject | |
18 | + @PersistenceContext | |
19 | 19 | private EntityManager entityManager; |
20 | 20 | |
21 | 21 | /** |
... | ... | @@ -42,22 +42,22 @@ public class GenericDao { |
42 | 42 | * @return die Entität mit gegebener Id oder <code>null</code>, wenn keine gefunden |
43 | 43 | */ |
44 | 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 | 46 | return entity; |
47 | 47 | } |
48 | 48 | |
49 | 49 | /** |
50 | 50 | * @param entityType Entity-Typ. |
51 | 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 | 54 | public <T extends BaseEntity> Collection<T> findAll(Class<T> entityType) { |
55 | 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 | 27 | import org.dbunit.ext.mysql.MySqlMetadataHandler; |
28 | 28 | import org.dbunit.operation.DatabaseOperation; |
29 | 29 | import org.hibernate.internal.SessionImpl; |
30 | -import org.junit.After; | |
31 | 30 | import org.slf4j.Logger; |
32 | 31 | import org.slf4j.LoggerFactory; |
33 | 32 | |
... | ... | @@ -254,15 +253,4 @@ public class JpaTestUtils { |
254 | 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 | 5 | import javax.ejb.Stateless; |
6 | 6 | import javax.inject.Inject; |
7 | 7 | |
8 | -import net.ziemers.swxercise.db.BaseEntity; | |
9 | 8 | import net.ziemers.swxercise.db.dao.GenericDao; |
10 | 9 | import net.ziemers.swxercise.lg.user.User; |
11 | 10 | |
... | ... | @@ -15,15 +14,13 @@ public class UserService { |
15 | 14 | @Inject |
16 | 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 | 19 | public Collection<User> findAllUsers() { |
23 | 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 | 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 | 2 | |
3 | 3 | import java.util.Collection; |
4 | 4 | |
5 | -import javax.annotation.PostConstruct; | |
6 | 5 | import javax.enterprise.context.ApplicationScoped; |
7 | 6 | import javax.inject.Inject; |
8 | 7 | import javax.ws.rs.GET; |
... | ... | @@ -13,8 +12,6 @@ import javax.ws.rs.core.MediaType; |
13 | 12 | |
14 | 13 | import net.ziemers.swxercise.lg.user.User; |
15 | 14 | import net.ziemers.swxercise.lg.user.service.UserService; |
16 | -import org.slf4j.Logger; | |
17 | -import org.slf4j.LoggerFactory; | |
18 | 15 | |
19 | 16 | @ApplicationScoped |
20 | 17 | @Path(RestController.webContextPath) |
... | ... | @@ -22,28 +19,19 @@ public class RestController { |
22 | 19 | |
23 | 20 | static final String webContextPath = "/user"; |
24 | 21 | |
25 | - private Logger logger; | |
26 | - | |
27 | 22 | @Inject |
28 | 23 | private UserService userService; |
29 | 24 | |
30 | - @PostConstruct | |
31 | - public void init() { | |
32 | - logger = LoggerFactory.getLogger(RestController.class); | |
33 | - } | |
34 | - | |
35 | 25 | /** |
36 | 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 | 29 | * @return eine Hallo-Nachricht |
41 | 30 | */ |
42 | 31 | @GET |
43 | 32 | @Path("/hello/{name}") |
44 | 33 | @Produces(MediaType.TEXT_PLAIN) |
45 | 34 | public String helloPath(@PathParam("name") String name) { |
46 | - logger.info("Hello {}", name); | |
47 | 35 | return String.format("Hello %s", name); |
48 | 36 | } |
49 | 37 | ... | ... |
src/main/resources/META-INF/persistence.xml
... | ... | @@ -8,40 +8,4 @@ |
8 | 8 | <property name="hibernate.hbm2ddl.auto" value="update" /> |
9 | 9 | </properties> |
10 | 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 | 11 | </persistence> |
48 | 12 | \ No newline at end of file | ... | ... |
src/test/java/net/ziemers/swxercise/ui/RestControllerTest.java
1 | 1 | package net.ziemers.swxercise.ui; |
2 | 2 | |
3 | -import net.ziemers.swxercise.ui.RestController; | |
3 | +import org.junit.Assert; | |
4 | 4 | import org.junit.Test; |
5 | 5 | import org.junit.runner.RunWith; |
6 | 6 | import org.mockito.InjectMocks; |
... | ... | @@ -14,7 +14,12 @@ public class RestControllerTest { |
14 | 14 | |
15 | 15 | @Test |
16 | 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 | } | ... | ... |