Skip to content

Commit 174a83f

Browse files
committed
added unit tests
1 parent 4553ec0 commit 174a83f

10 files changed

+317
-86
lines changed

src/main/java/org/audit4j/handler/db/ConnectionFactory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ Connection getSingleConnection() {
118118
* @return the pooled connection
119119
*/
120120
Connection getPooledConnection() {
121+
if (cpds == null) {
122+
throw new InitializationException("Could not obtain the db connection: Connection pool is null.");
123+
}
121124
try {
122125
return cpds.getConnection();
123126
} catch (SQLException e) {

src/main/java/org/audit4j/handler/db/DatabaseAuditHandler.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ public class DatabaseAuditHandler extends Handler {
6161
/** The Constant JNDI_CONNECTION. */
6262
private static final String JNDI_CONNECTION = "jndi";
6363

64-
EmbededDBServer server;
64+
/** The server. */
65+
private EmbededDBServer server;
6566

66-
ConnectionFactory factory;
67+
/** The factory. */
68+
private ConnectionFactory factory;
6769

6870
/**
6971
* Initialize database handler.
@@ -93,12 +95,12 @@ public void init() throws InitializationException {
9395
factory.setUrl(getDb_url());
9496
factory.setUser(getDb_user());
9597
factory.setPassword(getDb_password());
96-
factory.setJndiDataSource(getDb_jndi_datasource());
9798

9899
if (getDb_connection_type() != null && getDb_connection_type().equals(POOLED_CONNECTION)) {
99100
factory.setConnectionType(ConnectionType.POOLED);
100101
} else if (getDb_connection_type() != null && getDb_connection_type().equals(JNDI_CONNECTION)) {
101102
factory.setConnectionType(ConnectionType.JNDI);
103+
factory.setJndiDataSource(getDb_jndi_datasource());
102104
} else {
103105
factory.setConnectionType(ConnectionType.SINGLE);
104106
}

src/main/java/org/audit4j/handler/db/EmbededDBServer.java

Lines changed: 78 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -23,83 +23,86 @@
2323

2424
/**
2525
* The Class EmbededDBServer.
26-
*
26+
*
2727
* @author <a href="mailto:[email protected]">Janith Bandara</a>
2828
*/
2929
public abstract class EmbededDBServer {
3030

31-
/** The Constant EMBEDED_DB_NAME. */
32-
static final String EMBEDED_DB_NAME = "audit4j";
33-
34-
/** The Constant EMBEDED_DB_FILE_NAME. */
35-
static final String EMBEDED_DB_FILE_NAME = "audit4jdb";
36-
37-
/** The uname. */
38-
private String uname;
39-
40-
/** The password. */
41-
private String password;
42-
43-
/**
44-
* Gets the uname.
45-
*
46-
* @return the uname
47-
*/
48-
public String getUname() {
49-
return uname;
50-
}
51-
52-
/**
53-
* Sets the uname.
54-
*
55-
* @param uname the new uname
56-
*/
57-
public void setUname(String uname) {
58-
this.uname = uname;
59-
}
60-
61-
/**
62-
* Gets the password.
63-
*
64-
* @return the password
65-
*/
66-
public String getPassword() {
67-
return password;
68-
}
69-
70-
/**
71-
* Sets the password.
72-
*
73-
* @param password the new password
74-
*/
75-
public void setPassword(String password) {
76-
this.password = password;
77-
}
78-
79-
/**
80-
* Start.
81-
*
82-
* @throws InitializationException the initialization exception
83-
*/
84-
abstract void start() throws InitializationException;
85-
86-
/**
87-
* Shutdown.
88-
*/
89-
abstract void shutdown();
90-
91-
/**
92-
* Gets the driver.
93-
*
94-
* @return the driver
95-
*/
96-
abstract String getDriver();
97-
98-
/**
99-
* Gets the network protocol.
100-
*
101-
* @return the network protocol
102-
*/
103-
abstract String getNetworkProtocol();
104-
31+
/** The Constant EMBEDED_DB_NAME. */
32+
static final String EMBEDED_DB_NAME = "audit4j";
33+
34+
/** The Constant EMBEDED_DB_FILE_NAME. */
35+
static final String EMBEDED_DB_FILE_NAME = "audit4jdb";
36+
37+
/** The uname. */
38+
private String uname;
39+
40+
/** The password. */
41+
private String password;
42+
43+
/**
44+
* Gets the uname.
45+
*
46+
* @return the uname
47+
*/
48+
String getUname() {
49+
return uname;
50+
}
51+
52+
/**
53+
* Sets the uname.
54+
*
55+
* @param uname
56+
* the new uname
57+
*/
58+
void setUname(String uname) {
59+
this.uname = uname;
60+
}
61+
62+
/**
63+
* Gets the password.
64+
*
65+
* @return the password
66+
*/
67+
String getPassword() {
68+
return password;
69+
}
70+
71+
/**
72+
* Sets the password.
73+
*
74+
* @param password
75+
* the new password
76+
*/
77+
void setPassword(String password) {
78+
this.password = password;
79+
}
80+
81+
/**
82+
* Start.
83+
*
84+
* @throws InitializationException
85+
* the initialization exception
86+
*/
87+
abstract void start() throws InitializationException;
88+
89+
/**
90+
* Shutdown.
91+
*/
92+
abstract void shutdown();
93+
94+
/**
95+
* Gets the driver.
96+
*
97+
* @return the driver
98+
*/
99+
abstract String getDriver();
100+
101+
/**
102+
* Gets the network protocol.
103+
*
104+
* @return the network protocol
105+
*/
106+
abstract String getNetworkProtocol();
107+
105108
}

src/main/java/org/audit4j/handler/db/HSQLEmbededDBServer.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/*
22
* Copyright 2014 Janith Bandara, This source is a part of Audit4j -
33
* An open-source audit platform for Enterprise java platform.
4-
* http://mechanizedspace.com/audit4j
54
* http://audit4j.org
65
*
76
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -27,7 +26,7 @@
2726
*
2827
* @author <a href="mailto:[email protected]">Janith Bandara</a>
2928
*/
30-
class HSQLEmbededDBServer extends EmbededDBServer {
29+
final class HSQLEmbededDBServer extends EmbededDBServer {
3130

3231
/** The Constant driver. */
3332
private static final String driver = "org.hsqldb.jdbcDriver";
@@ -38,6 +37,10 @@ class HSQLEmbededDBServer extends EmbededDBServer {
3837

3938
/** The hsql server. */
4039
private Server hsqlServer = null;
40+
41+
private HSQLEmbededDBServer(){
42+
// Singalton private constructor.
43+
}
4144

4245
/*
4346
* (non-Javadoc)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.audit4j.handler.db;
2+
3+
import static org.junit.Assert.assertNotNull;
4+
5+
import java.sql.Connection;
6+
7+
import org.junit.After;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
public class AuditBaseDaoTest {
12+
13+
EmbededDBServer server;
14+
ConnectionFactory connectionFactory;
15+
16+
@Before
17+
public void before() {
18+
server = HSQLEmbededDBServer.getInstance();
19+
server.setUname("audit4jdbuser");
20+
server.setPassword("audit4jdbpassword");
21+
server.start();
22+
23+
connectionFactory = ConnectionFactory.getInstance();
24+
connectionFactory.setDriver("org.hsqldb.jdbcDriver");
25+
connectionFactory.setUrl("jdbc:hsqldb:hsql://localhost/audit4j");
26+
connectionFactory.setUser("audit4jdbuser");
27+
connectionFactory.setPassword("audit4jdbpassword");
28+
connectionFactory.setConnectionType(ConnectionType.SINGLE);
29+
connectionFactory.init();
30+
}
31+
32+
@Test
33+
public void testgetConnection() {
34+
AuditBaseDao base = new AuditBaseDao();
35+
Connection conn = base.getConnection();
36+
assertNotNull(conn);
37+
}
38+
39+
@After
40+
public void after() {
41+
server.shutdown();
42+
connectionFactory.stop();
43+
}
44+
45+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.audit4j.handler.db;
2+
3+
import java.sql.SQLException;
4+
5+
import org.audit4j.core.dto.EventBuilder;
6+
import org.audit4j.core.exception.HandlerException;
7+
import org.junit.After;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
public class AuditLogDaoImplTest {
12+
13+
EmbededDBServer server;
14+
ConnectionFactory connectionFactory;
15+
16+
@Before
17+
public void before() {
18+
server = HSQLEmbededDBServer.getInstance();
19+
server.setUname("audit4jdbuser");
20+
server.setPassword("audit4jdbpassword");
21+
server.start();
22+
23+
connectionFactory = ConnectionFactory.getInstance();
24+
connectionFactory.setDriver("org.hsqldb.jdbcDriver");
25+
connectionFactory.setUrl("jdbc:hsqldb:hsql://localhost/audit4j");
26+
connectionFactory.setUser("audit4jdbuser");
27+
connectionFactory.setPassword("audit4jdbpassword");
28+
connectionFactory.setConnectionType(ConnectionType.SINGLE);
29+
connectionFactory.init();
30+
}
31+
32+
@Test
33+
public void testcreateAuditTableIFNotExist() {
34+
AuditLogDao logDao = AuditLogDaoImpl.getInstance();
35+
try {
36+
logDao.createAuditTableIFNotExist();
37+
} catch (SQLException e) {
38+
// TODO Auto-generated catch block
39+
e.printStackTrace();
40+
}
41+
}
42+
43+
@Test
44+
public void testwriteEvent() {
45+
String actor = "Dummy Actor";
46+
EventBuilder builder = new EventBuilder();
47+
builder.addActor(actor).addAction("myMethod").addOrigin("Origin1").addField("myParam1Name", "param1")
48+
.addField("myParam2Name", new Integer(2));
49+
50+
AuditLogDao logDao = AuditLogDaoImpl.getInstance();
51+
try {
52+
logDao.writeEvent(builder.build());
53+
} catch (SQLException e) {
54+
// TODO Auto-generated catch block
55+
e.printStackTrace();
56+
} catch (HandlerException e) {
57+
// TODO Auto-generated catch block
58+
e.printStackTrace();
59+
}
60+
61+
}
62+
63+
@After
64+
public void after() {
65+
server.shutdown();
66+
connectionFactory.stop();
67+
}
68+
}

0 commit comments

Comments
 (0)