Skip to content

Commit e8af4a6

Browse files
author
Juraj Veverka
committed
implemented unit tests using apache derby
1 parent feccfd7 commit e8af4a6

File tree

7 files changed

+88
-5
lines changed

7 files changed

+88
-5
lines changed

hibernate-demo/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ target
88
.project
99
.idea
1010
*.iml
11+
derby.log
12+
1113

hibernate-demo/README.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
# Hibernate Postgresql demo
22
Inspired by [this](https://dzone.com/articles/hibernate-5-java-configuration-example) tutorial.
3+
This is simple demo of Hibernate ORM in Java standalone environment.
4+
5+
## Example application
6+
Example application ``itx.examples.hibernate.App`` is designed to run against real postgresql server.
7+
Please install and start postgresql server before running the App, following guide below.
8+
9+
## Unit tests
10+
Unit tests are designed to run with in-memory apache derby database acting like real SQL persistence database.
11+
12+
## Build and run
13+
```gradle clean build test```
314

415
### Install Postresql
516
Download [binary distribution](https://www.enterprisedb.com/download-postgresql-binaries)
617
of [postresql](https://www.postgresql.org/) and unzip it into ``/opt/postresql``
18+
19+
#### Setup server
720
```
821
cd /opt/postgresql
922
./bin/initdb /opt/postgresql/data
1023
# start server
1124
./bin/pg_ctl -D /opt/postgresql/data -l /opt/postgresql/logfile start
12-
# stop server
13-
./bin/pg_ctl -D /opt/postgresql/data -l /opt/postgresql/logfile stop
1425
```
1526
Configure database server access. In ``data/postgresql.conf``, set
1627
```
@@ -38,3 +49,6 @@ GRANT ALL PRIVILEGES ON DATABASE userdata TO username;
3849
\c userdata;
3950
select * from userdata;
4051
```
52+
53+
#### Stop server
54+
```./bin/pg_ctl -D /opt/postgresql/data -l /opt/postgresql/logfile stop```

hibernate-demo/build.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ dependencies {
1616
compile 'org.slf4j:slf4j-simple:1.8.0-beta2'
1717
compile 'org.hibernate:hibernate-core:5.4.3.Final'
1818
compile 'org.postgresql:postgresql:42.2.6'
19+
20+
testCompile 'org.apache.derby:derby:10.15.1.3'
21+
testCompile 'org.apache.derby:derbytools:10.15.1.3'
22+
testCompile 'org.apache.derby:derbyshared:10.15.1.3'
1923
testCompile 'org.testng:testng:6.14.3'
2024
}
2125

hibernate-demo/src/main/java/itx/examples/hibernate/App.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
public class App {
1313

14-
final private static Logger LOG = LoggerFactory.getLogger(App.class);
14+
private static final Logger LOG = LoggerFactory.getLogger(App.class);
1515

1616
public static void main(String[] args) throws Exception {
1717
DataAccessService dataAccessService = new DataAccessServiceImpl(HibernateUtil.getPostgresql10Properties());

hibernate-demo/src/main/java/itx/examples/hibernate/entities/UserData.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class UserData {
99
@Id
1010
@GeneratedValue(strategy = GenerationType.IDENTITY)
1111
@Column(name = "id")
12-
long id;
12+
Long id;
1313

1414
@Column(name = "first_name")
1515
private String firstName;

hibernate-demo/src/main/java/itx/examples/hibernate/services/DataAccessServiceImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
public class DataAccessServiceImpl implements DataAccessService {
1515

16-
final private static Logger LOG = LoggerFactory.getLogger(DataAccessServiceImpl.class);
16+
private static final Logger LOG = LoggerFactory.getLogger(DataAccessServiceImpl.class);
1717

1818
private final SessionFactory sessionFactory;
1919

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,67 @@
11
package itx.examples.hibernate.tests;
22

3+
import itx.examples.hibernate.entities.UserData;
4+
import itx.examples.hibernate.services.DataAccessService;
5+
import itx.examples.hibernate.services.DataAccessServiceImpl;
6+
import org.hibernate.cfg.Environment;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
import org.testng.Assert;
10+
import org.testng.annotations.AfterClass;
11+
import org.testng.annotations.BeforeClass;
12+
import org.testng.annotations.Test;
13+
14+
import java.util.Collection;
15+
import java.util.Properties;
16+
317
public class HibernateTest {
18+
19+
private static final Logger LOG = LoggerFactory.getLogger(HibernateTest.class);
20+
21+
public static Properties getDerbyProperties() {
22+
Properties properties = new Properties();
23+
properties.put(Environment.DRIVER, "org.apache.derby.jdbc.EmbeddedDriver");
24+
properties.put(Environment.URL, "jdbc:derby:memory:userdata;create=true");
25+
properties.put(Environment.DIALECT, "org.hibernate.dialect.DerbyDialect");
26+
properties.put(Environment.SHOW_SQL, "true");
27+
properties.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
28+
properties.put(Environment.HBM2DDL_AUTO, "update");
29+
return properties;
30+
}
31+
32+
private DataAccessService dataAccessService;
33+
34+
@BeforeClass
35+
public void init() {
36+
LOG.info("init ...");
37+
dataAccessService = new DataAccessServiceImpl(getDerbyProperties());
38+
}
39+
40+
@Test
41+
public void testDataAccessService() {
42+
UserData userData = new UserData("Juraj", "Veverka", "[email protected]");
43+
dataAccessService.saveUserData(userData);
44+
45+
Collection<UserData> userDataCollection = dataAccessService.getUserData();
46+
Assert.assertNotNull(userDataCollection);
47+
Assert.assertFalse(userDataCollection.isEmpty());
48+
UserData userDataFromDb = userDataCollection.iterator().next();
49+
Assert.assertNotNull(userDataFromDb);
50+
Assert.assertNotNull(userDataFromDb.getId());
51+
Assert.assertEquals(userDataFromDb.getFirstName(), "Juraj");
52+
Assert.assertEquals(userDataFromDb.getLastName(), "Veverka");
53+
Assert.assertEquals(userDataFromDb.getEmail(), "[email protected]");
54+
55+
dataAccessService.deleteUserData(userDataFromDb.getId());
56+
userDataCollection = dataAccessService.getUserData();
57+
Assert.assertNotNull(userDataCollection);
58+
Assert.assertTrue(userDataCollection.isEmpty());
59+
}
60+
61+
@AfterClass
62+
public void shutdown() throws Exception {
63+
LOG.info("shutdown.");
64+
dataAccessService.close();
65+
}
66+
467
}

0 commit comments

Comments
 (0)