1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/test/java/ru/indvdum/jpa/tests/AbstractJPATest.java Fri Nov 09 03:00:49 2012 +0400
1.3 @@ -0,0 +1,71 @@
1.4 +package ru.indvdum.jpa.tests;
1.5 +
1.6 +import java.util.ResourceBundle;
1.7 +
1.8 +import javax.naming.InitialContext;
1.9 +import javax.naming.NamingException;
1.10 +
1.11 +import org.apache.commons.dbcp.BasicDataSource;
1.12 +import org.junit.BeforeClass;
1.13 +
1.14 +import ru.indvdum.jpa.dao.PropertySelector;
1.15 +import ru.indvdum.jpa.props.Props;
1.16 +
1.17 +/**
1.18 + * @author indvdum (gotoindvdum[at]gmail[dot]com)
1.19 + * @since 22.12.2011 16:44:26
1.20 + *
1.21 + */
1.22 +public abstract class AbstractJPATest {
1.23 +
1.24 + @BeforeClass
1.25 + public static void setUpClass() throws Exception {
1.26 + if ("true".equalsIgnoreCase(System.getProperty("JPA.realDatabaseConnection"))) {
1.27 + initRealDatabase();
1.28 + } else {
1.29 + initDerby();
1.30 + }
1.31 + }
1.32 +
1.33 + protected static String dataSource = null;
1.34 +
1.35 + protected static String getDataSourceName() {
1.36 + if (dataSource != null)
1.37 + return dataSource;
1.38 +
1.39 + if (dataSource == null)
1.40 + dataSource = System.getProperty(Props.DATA_SOURCE_PROPERTY);
1.41 + if (dataSource == null)
1.42 + dataSource = ResourceBundle.getBundle(Props.JPADAO_PROPERTY_FILE).getString(Props.DATA_SOURCE_PROPERTY);
1.43 + if (dataSource == null)
1.44 + dataSource = "jdbc/database";
1.45 + return dataSource;
1.46 + }
1.47 +
1.48 + private static void initDerby() throws NamingException {
1.49 + System.setProperty("derby.stream.error.field", "java.lang.System.err");
1.50 +
1.51 + InitialContext context = new InitialContext();
1.52 + BasicDataSource datasource = (BasicDataSource) context.lookup(getDataSourceName());
1.53 +
1.54 + if (datasource == null) {
1.55 + datasource = new BasicDataSource();
1.56 + datasource.setUrl("jdbc:derby:memory:myDb;create=true");
1.57 + datasource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
1.58 + datasource.setUsername("");
1.59 + datasource.setPassword("");
1.60 + datasource.setMaxActive(2);
1.61 +
1.62 + context.bind(getDataSourceName(), datasource);
1.63 + }
1.64 +
1.65 + PropertySelector.setSystemProperty(PropertySelector.RUNTIMEENHANCEMENT, "supported");
1.66 + PropertySelector.setSystemProperty(PropertySelector.SHOWSQL, "true");
1.67 + PropertySelector.setSystemProperty(PropertySelector.SYNCHRONIZEDB, "true");
1.68 + }
1.69 +
1.70 + private static void initRealDatabase() throws NamingException {
1.71 + // TODO: need realize
1.72 + }
1.73 +
1.74 +}