src/test/java/ru/indvdum/jpa/tests/AbstractJPATest.java
author indvdum (gotoindvdum[at]gmail[dot]com)
Fri, 09 Nov 2012 03:06:22 +0400
changeset 3 b400313e8aca
parent 2 f692598b3742
child 5 ccb029c396aa
permissions -rw-r--r--
Stable 0.2.0.0 version of commons-jpa module
     1 package ru.indvdum.jpa.tests;
     2 
     3 import java.util.ResourceBundle;
     4 
     5 import javax.naming.InitialContext;
     6 import javax.naming.NamingException;
     7 
     8 import org.apache.commons.dbcp.BasicDataSource;
     9 import org.junit.BeforeClass;
    10 
    11 import ru.indvdum.jpa.dao.PropertySelector;
    12 import ru.indvdum.jpa.props.Props;
    13 
    14 /**
    15  * @author indvdum (gotoindvdum[at]gmail[dot]com)
    16  * @since 22.12.2011 16:44:26
    17  * 
    18  */
    19 public abstract class AbstractJPATest {
    20 
    21 	@BeforeClass
    22 	public static void setUpClass() throws Exception {
    23 		if ("true".equalsIgnoreCase(System.getProperty("JPA.realDatabaseConnection"))) {
    24 			initRealDatabase();
    25 		} else {
    26 			initDerby();
    27 		}
    28 	}
    29 
    30 	protected static String dataSource = null;
    31 
    32 	protected static String getDataSourceName() {
    33 		if (dataSource != null)
    34 			return dataSource;
    35 
    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";
    42 		return dataSource;
    43 	}
    44 
    45 	private static void initDerby() throws NamingException {
    46 		System.setProperty("derby.stream.error.field", "java.lang.System.err");
    47 
    48 		InitialContext context = new InitialContext();
    49 		BasicDataSource datasource = (BasicDataSource) context.lookup(getDataSourceName());
    50 
    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);
    58 
    59 			context.bind(getDataSourceName(), datasource);
    60 		}
    61 
    62 		PropertySelector.setSystemProperty(PropertySelector.RUNTIMEENHANCEMENT, "supported");
    63 		PropertySelector.setSystemProperty(PropertySelector.SHOWSQL, "true");
    64 		PropertySelector.setSystemProperty(PropertySelector.SYNCHRONIZEDB, "true");
    65 	}
    66 
    67 	private static void initRealDatabase() throws NamingException {
    68 		// TODO: need realize
    69 	}
    70 
    71 }