| author | indvdum (gotoindvdum[at]gmail[dot]com) |
| Fri, 09 Nov 2012 03:22:29 +0400 | |
| changeset 4 | eca51f09ed0a |
| parent 2 | f692598b3742 |
| child 5 | ccb029c396aa |
| permissions | -rw-r--r-- |
| indvdum@2 | 1 |
package ru.indvdum.jpa.tests; |
| indvdum@2 | 2 |
|
| indvdum@2 | 3 |
import java.util.ResourceBundle; |
| indvdum@2 | 4 |
|
| indvdum@2 | 5 |
import javax.naming.InitialContext; |
| indvdum@2 | 6 |
import javax.naming.NamingException; |
| indvdum@2 | 7 |
|
| indvdum@2 | 8 |
import org.apache.commons.dbcp.BasicDataSource; |
| indvdum@2 | 9 |
import org.junit.BeforeClass; |
| indvdum@2 | 10 |
|
| indvdum@2 | 11 |
import ru.indvdum.jpa.dao.PropertySelector; |
| indvdum@2 | 12 |
import ru.indvdum.jpa.props.Props; |
| indvdum@2 | 13 |
|
| indvdum@2 | 14 |
/** |
| indvdum@2 | 15 |
* @author indvdum (gotoindvdum[at]gmail[dot]com) |
| indvdum@2 | 16 |
* @since 22.12.2011 16:44:26 |
| indvdum@2 | 17 |
* |
| indvdum@2 | 18 |
*/ |
| indvdum@2 | 19 |
public abstract class AbstractJPATest {
|
| indvdum@2 | 20 |
|
| indvdum@2 | 21 |
@BeforeClass |
| indvdum@2 | 22 |
public static void setUpClass() throws Exception {
|
| indvdum@2 | 23 |
if ("true".equalsIgnoreCase(System.getProperty("JPA.realDatabaseConnection"))) {
|
| indvdum@2 | 24 |
initRealDatabase(); |
| indvdum@2 | 25 |
} else {
|
| indvdum@2 | 26 |
initDerby(); |
| indvdum@2 | 27 |
} |
| indvdum@2 | 28 |
} |
| indvdum@2 | 29 |
|
| indvdum@2 | 30 |
protected static String dataSource = null; |
| indvdum@2 | 31 |
|
| indvdum@2 | 32 |
protected static String getDataSourceName() {
|
| indvdum@2 | 33 |
if (dataSource != null) |
| indvdum@2 | 34 |
return dataSource; |
| indvdum@2 | 35 |
|
| indvdum@2 | 36 |
if (dataSource == null) |
| indvdum@2 | 37 |
dataSource = System.getProperty(Props.DATA_SOURCE_PROPERTY); |
| indvdum@2 | 38 |
if (dataSource == null) |
| indvdum@2 | 39 |
dataSource = ResourceBundle.getBundle(Props.JPADAO_PROPERTY_FILE).getString(Props.DATA_SOURCE_PROPERTY); |
| indvdum@2 | 40 |
if (dataSource == null) |
| indvdum@2 | 41 |
dataSource = "jdbc/database"; |
| indvdum@2 | 42 |
return dataSource; |
| indvdum@2 | 43 |
} |
| indvdum@2 | 44 |
|
| indvdum@2 | 45 |
private static void initDerby() throws NamingException {
|
| indvdum@2 | 46 |
System.setProperty("derby.stream.error.field", "java.lang.System.err");
|
| indvdum@2 | 47 |
|
| indvdum@2 | 48 |
InitialContext context = new InitialContext(); |
| indvdum@2 | 49 |
BasicDataSource datasource = (BasicDataSource) context.lookup(getDataSourceName()); |
| indvdum@2 | 50 |
|
| indvdum@2 | 51 |
if (datasource == null) {
|
| indvdum@2 | 52 |
datasource = new BasicDataSource(); |
| indvdum@2 | 53 |
datasource.setUrl("jdbc:derby:memory:myDb;create=true");
|
| indvdum@2 | 54 |
datasource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
|
| indvdum@2 | 55 |
datasource.setUsername("");
|
| indvdum@2 | 56 |
datasource.setPassword("");
|
| indvdum@2 | 57 |
datasource.setMaxActive(2); |
| indvdum@2 | 58 |
|
| indvdum@2 | 59 |
context.bind(getDataSourceName(), datasource); |
| indvdum@2 | 60 |
} |
| indvdum@2 | 61 |
|
| indvdum@2 | 62 |
PropertySelector.setSystemProperty(PropertySelector.RUNTIMEENHANCEMENT, "supported"); |
| indvdum@2 | 63 |
PropertySelector.setSystemProperty(PropertySelector.SHOWSQL, "true"); |
| indvdum@2 | 64 |
PropertySelector.setSystemProperty(PropertySelector.SYNCHRONIZEDB, "true"); |
| indvdum@2 | 65 |
} |
| indvdum@2 | 66 |
|
| indvdum@2 | 67 |
private static void initRealDatabase() throws NamingException {
|
| indvdum@2 | 68 |
// TODO: need realize |
| indvdum@2 | 69 |
} |
| indvdum@2 | 70 |
|
| indvdum@2 | 71 |
} |