1 package ru.indvdum.jpa.tests;
3 import java.util.ResourceBundle;
5 import javax.naming.InitialContext;
6 import javax.naming.NamingException;
8 import org.apache.commons.dbcp.BasicDataSource;
9 import org.junit.BeforeClass;
11 import ru.indvdum.jpa.dao.PropertySelector;
12 import ru.indvdum.jpa.props.Props;
15 * @author indvdum (gotoindvdum[at]gmail[dot]com)
16 * @since 22.12.2011 16:44:26
19 public abstract class AbstractJPATest {
22 public static void setUpClass() throws Exception {
23 if ("true".equalsIgnoreCase(System.getProperty("JPA.realDatabaseConnection"))) {
30 protected static String dataSource = null;
32 protected static String getDataSourceName() {
33 if (dataSource != null)
36 if (dataSource == null)
37 dataSource = System.getProperty(Props.DATA_SOURCE_PROPERTY);
38 if (dataSource == null)
39 dataSource = ResourceBundle.getBundle(Props.JPADAO_PROPERTY_FILE).getString(Props.DATA_SOURCE_PROPERTY);
40 if (dataSource == null)
41 dataSource = "jdbc/database";
45 private static void initDerby() throws NamingException {
46 System.setProperty("derby.stream.error.field", "java.lang.System.err");
48 InitialContext context = new InitialContext();
49 BasicDataSource datasource = (BasicDataSource) context.lookup(getDataSourceName());
51 if (datasource == null) {
52 datasource = new BasicDataSource();
53 datasource.setUrl("jdbc:derby:memory:myDb;create=true");
54 datasource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
55 datasource.setUsername("");
56 datasource.setPassword("");
57 datasource.setMaxActive(2);
59 context.bind(getDataSourceName(), datasource);
62 PropertySelector.setSystemProperty(PropertySelector.RUNTIMEENHANCEMENT, "supported");
63 PropertySelector.setSystemProperty(PropertySelector.SHOWSQL, "true");
64 PropertySelector.setSystemProperty(PropertySelector.SYNCHRONIZEDB, "true");
67 private static void initRealDatabase() throws NamingException {