forked from jeffkole/jpile
/
AbstractIntTestForJPile.java
85 lines (76 loc) · 3.16 KB
/
AbstractIntTestForJPile.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.opower.persistence.jpile;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.opower.persistence.jpile.loader.HierarchicalInfileObjectLoader;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.springframework.core.io.InputStreamResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.jdbc.JdbcTestUtils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.List;
/**
* Abstract test case for all int tests. Loads MySQL drivers and creates a new MySQL {@link Connection}
*
* @author amir.raminfar
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public abstract class AbstractIntTestForJPile {
private static final String JDBC_URL = "jdbc:mysql://localhost/jpile?useUnicode=true&characterEncoding=utf-8";
private static final List<String> TABLES =
ImmutableList.of("customer", "product", "contact", "contact_phone", "binary_data", "supplier");
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "";
static {
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e) {
throw Throwables.propagate(e);
}
}
protected Connection connection;
protected HierarchicalInfileObjectLoader hierarchicalInfileObjectLoader = new HierarchicalInfileObjectLoader();
protected JdbcTemplate jdbcTemplate;
@BeforeClass
public static void createTables() throws Exception {
Connection connection = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PASSWORD);
JdbcTestUtils.executeSqlScript(
new JdbcTemplate(new SingleConnectionDataSource(connection, true)),
new InputStreamResource(AbstractIntTestForJPile.class.getResourceAsStream("/jpile.sql")),
false
);
connection.close();
}
@Before
public void setUp() throws Exception {
this.connection = DriverManager.getConnection(JDBC_URL, DB_USER, "");
this.hierarchicalInfileObjectLoader.setConnection(this.connection);
this.jdbcTemplate = new JdbcTemplate(new SingleConnectionDataSource(this.connection, true));
}
@After
public void tearDown() throws Exception {
this.hierarchicalInfileObjectLoader.close();
for (String table : TABLES) {
this.jdbcTemplate.update("truncate " + table);
}
this.connection.close();
}
@AfterClass
public static void dropTables() throws Exception {
Connection connection = DriverManager.getConnection(JDBC_URL, DB_USER, "");
JdbcTemplate template = new JdbcTemplate(new SingleConnectionDataSource(connection, true));
for (String table : TABLES) {
template.update("drop table " + table);
}
connection.close();
}
}