Skip to content

Commit 4316665

Browse files
add test for model and dao
1 parent a9fa211 commit 4316665

File tree

11 files changed

+490
-59
lines changed

11 files changed

+490
-59
lines changed

pom.xml

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,56 @@
66
<version>0.0.1-SNAPSHOT</version>
77
<packaging>war</packaging>
88

9+
<properties>
10+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11+
<jacoco.report.path>${project.basedir}/target/jacoco</jacoco.report.path>
12+
</properties>
13+
914
<dependencies>
1015
<dependency>
1116
<groupId>javax</groupId>
1217
<artifactId>javaee-api</artifactId>
1318
<version>7.0</version>
1419
<scope>provided</scope>
1520
</dependency>
16-
21+
1722
<dependency>
1823
<groupId>org.apache.commons</groupId>
1924
<artifactId>commons-lang3</artifactId>
2025
<version>3.5</version>
2126
</dependency>
27+
28+
<!-- TEST DEPENDENCIES -->
29+
<dependency>
30+
<groupId>junit</groupId>
31+
<artifactId>junit</artifactId>
32+
<version>4.11</version>
33+
<scope>test</scope>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>org.hibernate</groupId>
38+
<artifactId>hibernate-core</artifactId>
39+
<version>5.2.6.Final</version>
40+
<scope>test</scope>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.mockito</groupId>
45+
<artifactId>mockito-all</artifactId>
46+
<version>1.8.5</version>
47+
<scope>test</scope>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>org.hsqldb</groupId>
52+
<artifactId>hsqldb</artifactId>
53+
<version>2.3.2</version>
54+
<scope>test</scope>
55+
</dependency>
56+
2257
</dependencies>
23-
58+
2459
<build>
2560
<pluginManagement>
2661
<plugins>
@@ -39,4 +74,43 @@
3974
</plugins>
4075
</pluginManagement>
4176
</build>
77+
78+
<profiles>
79+
<profile>
80+
<id>jacoco</id>
81+
<activation>
82+
<activeByDefault>false</activeByDefault>
83+
</activation>
84+
<build>
85+
<plugins>
86+
<plugin>
87+
<groupId>org.jacoco</groupId>
88+
<artifactId>jacoco-maven-plugin</artifactId>
89+
<version>0.7.7.201606060606</version>
90+
<configuration>
91+
<destFile>${jacoco.report.path}/jacoco.exec</destFile>
92+
</configuration>
93+
<executions>
94+
<execution>
95+
<goals>
96+
<goal>prepare-agent</goal>
97+
</goals>
98+
</execution>
99+
<execution>
100+
<id>post-unit-test</id>
101+
<phase>test</phase>
102+
<goals>
103+
<goal>report</goal>
104+
</goals>
105+
<configuration>
106+
<dataFile>${jacoco.report.path}/jacoco.exec</dataFile>
107+
<outputDirectory>${jacoco.report.path}</outputDirectory>
108+
</configuration>
109+
</execution>
110+
</executions>
111+
</plugin>
112+
</plugins>
113+
</build>
114+
</profile>
115+
</profiles>
42116
</project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package it.unifi.ing.stlab.swa.model;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
import javax.persistence.MappedSuperclass;
8+
9+
@MappedSuperclass
10+
public abstract class BaseEntity {
11+
12+
@Id
13+
@GeneratedValue(strategy = GenerationType.IDENTITY)
14+
private Long id;
15+
16+
@Column(unique = true)
17+
private String uuid;
18+
19+
protected BaseEntity() {
20+
}
21+
22+
public BaseEntity(String uuid) {
23+
if(uuid == null) {
24+
throw new IllegalArgumentException("uuid cannot be null");
25+
}
26+
this.uuid = uuid;
27+
}
28+
29+
public Long getId() {
30+
return id;
31+
}
32+
33+
public String getUuid() {
34+
return uuid;
35+
}
36+
37+
@Override
38+
public int hashCode() {
39+
final int prime = 31;
40+
int result = 1;
41+
result = prime * result + uuid.hashCode();
42+
return result;
43+
}
44+
45+
@Override
46+
public boolean equals(Object obj) {
47+
if (this == obj) {
48+
return true;
49+
}
50+
if (obj == null) {
51+
return false;
52+
}
53+
if (!(obj instanceof BaseEntity)) {
54+
return false;
55+
}
56+
BaseEntity other = (BaseEntity) obj;
57+
return uuid.equals(other.getUuid());
58+
}
59+
}

src/main/java/it/unifi/ing/stlab/swa/model/Note.java

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,79 +3,62 @@
33
import java.util.Date;
44

55
import javax.persistence.Entity;
6-
import javax.persistence.GeneratedValue;
7-
import javax.persistence.Id;
86
import javax.persistence.Lob;
97
import javax.persistence.ManyToOne;
108
import javax.persistence.Table;
119
import javax.persistence.Temporal;
1210
import javax.persistence.TemporalType;
1311

1412
@Entity
15-
@Table(name="notes")
16-
public class Note {
13+
@Table(name = "notes")
14+
public class Note extends BaseEntity {
1715

18-
private Long id;
19-
private String uuid;
20-
2116
private String title;
17+
@Lob
2218
private String body;
19+
@Temporal(TemporalType.TIMESTAMP)
2320
private Date creationDate;
24-
21+
22+
@ManyToOne
2523
private User user;
26-
24+
2725
Note() {
2826
}
29-
27+
3028
public Note(String uuid) {
31-
this.uuid = uuid;
29+
super(uuid);
3230
}
33-
34-
@Id
35-
@GeneratedValue
36-
public Long getId() {
37-
return id;
38-
}
39-
void setId(Long id) {
40-
this.id = id;
41-
}
42-
43-
public String getUuid() {
44-
return uuid;
45-
}
46-
void setUuid(String uuid) {
47-
this.uuid = uuid;
48-
}
49-
31+
5032
public String getTitle() {
5133
return title;
5234
}
35+
5336
public void setTitle(String title) {
5437
this.title = title;
5538
}
56-
57-
@Lob
39+
5840
public String getBody() {
5941
return body;
6042
}
43+
6144
public void setBody(String body) {
6245
this.body = body;
6346
}
64-
65-
@Temporal(TemporalType.TIMESTAMP)
47+
6648
public Date getCreationDate() {
6749
return creationDate;
6850
}
51+
6952
public void setCreationDate(Date creationDate) {
7053
this.creationDate = creationDate;
7154
}
72-
73-
@ManyToOne
55+
7456
public User getUser() {
7557
return user;
7658
}
59+
7760
public void setUser(User user) {
7861
this.user = user;
7962
}
80-
63+
8164
}

src/main/java/it/unifi/ing/stlab/swa/model/User.java

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,20 @@
11
package it.unifi.ing.stlab.swa.model;
22

33
import javax.persistence.Entity;
4-
import javax.persistence.GeneratedValue;
5-
import javax.persistence.Id;
64
import javax.persistence.Table;
75

86
@Entity
97
@Table(name="users")
10-
public class User {
8+
public class User extends BaseEntity {
119

12-
private Long id;
13-
private String uuid;
1410
private String email;
1511
private String password;
1612

1713
User(){
1814
}
1915

2016
public User(String uuid) {
21-
this.uuid = uuid;
22-
}
23-
24-
@Id
25-
@GeneratedValue
26-
public Long getId() {
27-
return id;
28-
}
29-
void setId(Long id) {
30-
this.id = id;
31-
}
32-
33-
public String getUuid() {
34-
return uuid;
35-
}
36-
void setUuid(String uuid) {
37-
this.uuid = uuid;
17+
super(uuid);
3818
}
3919

4020
public String getEmail() {

src/test/java/empty

Whitespace-only changes.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package it.unifi.ing.stlab.swa.dao;
2+
3+
import javax.persistence.EntityManager;
4+
import javax.persistence.EntityManagerFactory;
5+
import javax.persistence.Persistence;
6+
7+
import org.junit.After;
8+
import org.junit.AfterClass;
9+
import org.junit.Before;
10+
import org.junit.BeforeClass;
11+
import org.junit.runners.model.InitializationError;
12+
13+
public abstract class JpaTest {
14+
15+
private static EntityManagerFactory entityManagerFactory;
16+
protected EntityManager entityManager;
17+
18+
@BeforeClass
19+
public static void setUpClass() {
20+
entityManagerFactory = Persistence.createEntityManagerFactory("test");
21+
}
22+
23+
@Before
24+
public void setUp() throws InitializationError {
25+
entityManager = entityManagerFactory.createEntityManager();
26+
entityManager.getTransaction().begin();
27+
entityManager.createNativeQuery("TRUNCATE SCHEMA public AND COMMIT").executeUpdate();
28+
entityManager.getTransaction().commit();
29+
30+
entityManager.getTransaction().begin();
31+
init();
32+
entityManager.getTransaction().commit();
33+
entityManager.clear();
34+
35+
entityManager.getTransaction().begin();
36+
}
37+
38+
@After
39+
public void tearDown() {
40+
if ( entityManager.getTransaction().isActive() ) {
41+
entityManager.getTransaction().rollback();
42+
}
43+
entityManager.close();
44+
}
45+
46+
@AfterClass
47+
public static void tearDownClass() {
48+
entityManagerFactory.close();
49+
}
50+
51+
protected abstract void init() throws InitializationError;
52+
53+
}

0 commit comments

Comments
 (0)