replaced testing usage of properties config file with default embedded derby usage and the ability to override using system properties including maven profile support with an example of MySQL usage.
diff --git a/employee/employee.model/.classpath b/employee/employee.model/.classpath index f0a5f03..6b9ef37 100644 --- a/employee/employee.model/.classpath +++ b/employee/employee.model/.classpath
@@ -7,7 +7,6 @@ </attributes> </classpathentry> <classpathentry kind="src" output="target/classes" path="src/main/resources"/> - <classpathentry including="**/*.java" kind="src" output="target/test-classes" path="src/test/resources"/> <classpathentry kind="src" output="target/test-classes" path="src/test/java"> <attributes> <attribute name="optional" value="true"/>
diff --git a/employee/employee.model/eclipselink-example-employee.properties b/employee/employee.model/eclipselink-example-employee.properties deleted file mode 100644 index 10db7f8..0000000 --- a/employee/employee.model/eclipselink-example-employee.properties +++ /dev/null
@@ -1,21 +0,0 @@ -# Database login info -javax.persistence.jdbc.url=jdbc:mysql://localhost:3306/employee -javax.persistence.jdbc.user=root -javax.persistence.jdbc.password=password -javax.persistence.jdbc.driver=com.mysql.jdbc.Driver - -#Logging config -eclipselink.logging.level=FINE -eclipselink.logging.connection=false -eclipselink.logging.timestamp=false -eclipselink.logging.thread=false -eclipselink.logging.session=false -eclipselink.logging.exceptions=false -eclipselink.logging.level.sql=FINE -eclipselink.logging.level.metadata=WARNING -eclipselink.logging.parameters=true - -#Ensuring weaving is being used -eclipselink.weaving=true - -eclipselink.profiler=QueryMonitor \ No newline at end of file
diff --git a/employee/employee.model/pom.xml b/employee/employee.model/pom.xml index 8d6c7d1..216d980 100644 --- a/employee/employee.model/pom.xml +++ b/employee/employee.model/pom.xml
@@ -40,17 +40,11 @@ <version>10.9.1.0</version> <scope>test</scope> </dependency> - <dependency> - <groupId>mysql</groupId> - <artifactId>mysql-connector-java</artifactId> - <version>5.1.22</version> - <scope>test</scope> - </dependency> </dependencies> <build> <defaultGoal>test</defaultGoal> - + <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> @@ -76,30 +70,60 @@ <argLine>-javaagent:${user.home}/.m2/repository/org/eclipse/persistence/eclipselink/${eclipselink.version}/eclipselink-${eclipselink.version}.jar</argLine> </configuration> </plugin> -<!-- - <plugin> - <groupId>org.codehaus.mojo</groupId> - <artifactId>exec-maven-plugin</artifactId> - <version>1.2.1</version> - <executions> - <execution> - <goals> - <goal>exec</goal> - </goals> - </execution> - </executions> - <configuration> - <executable>java</executable> - <arguments> - <argument>-classpath</argument> - <classpath /> - <argument>-javaagent:${user.home}/.m2/repository/org/eclipse/persistence/eclipselink/${eclipselink.version}/eclipselink-${eclipselink.version}.jar</argument> - <argument>example.JavaSEExample</argument> - </arguments> - </configuration> - </plugin> ---> + </plugins> </build> + <!-- Profile to demonstrate how an additional database can be configured + for use in testing. Also useful in populating the target database your container's + data source will be using. USAGE: mvn -P mysql --> + <profiles> + <profile> + <id>mysql</id> + <activation> + <property> + <name>mysql</name> + </property> + </activation> + <dependencies> + <dependency> + <groupId>mysql</groupId> + <artifactId>mysql-connector-java</artifactId> + <version>5.1.22</version> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <version>2.12.4</version> + <configuration> + <systemProperties> + <property> + <name>javax.persistence.jdbc.url</name> + <value>jdbc:mysql://localhost:3306/employee</value> + </property> + <property> + <name>javax.persistence.jdbc.driver</name> + <value>com.mysql.jdbc.Driver</value> + </property> + <property> + <name>javax.persistence.jdbc.user</name> + <value>root</value> + </property> + <property> + <name>javax.persistence.jdbc.password</name> + <value>password</value> + </property> + </systemProperties> + </configuration> + </plugin> + </plugins> + </build> + </profile> + </profiles> + </project>
diff --git a/employee/employee.model/src/main/resources/META-INF/persistence.xml b/employee/employee.model/src/main/resources/META-INF/persistence.xml index e98022f..224a2fe 100644 --- a/employee/employee.model/src/main/resources/META-INF/persistence.xml +++ b/employee/employee.model/src/main/resources/META-INF/persistence.xml
@@ -1,7 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="employee" transaction-type="RESOURCE_LOCAL"> - <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <non-jta-data-source>java:global/employeeDS</non-jta-data-source> <class>model.Project</class> <class>model.Address</class>
diff --git a/employee/employee.model/src/test/java/example/ExamplePropertiesLoader.java b/employee/employee.model/src/test/java/example/ExamplePropertiesLoader.java deleted file mode 100644 index 2ac718c..0000000 --- a/employee/employee.model/src/test/java/example/ExamplePropertiesLoader.java +++ /dev/null
@@ -1,110 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010-2011 Oracle. All rights reserved. - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 - * which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Eclipse Distribution License is available at - * http://www.eclipse.org/org/documents/edl-v10.php. - * - * Contributors: - * dclarke - Employee Demo 2.3 - ******************************************************************************/ -package example; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.util.Map; -import java.util.Properties; - -import org.eclipse.persistence.config.PersistenceUnitProperties; - -/** - * Helper class that will load persistence unit overrides from a properties file - * in both the current running folder and the current user's home folder. The - * goal is to enable developers and users of the example to customize its - * behavior without having to modify the source of the example. - * - * @author dclarke - * @since EclipseLink 2.0.0 - */ -public class ExamplePropertiesLoader { - - public static final String DEFAULT_FILENAME = "eclipselink-example-employee.properties"; - - /** - * - * @param properties - */ - public static void loadProperties(Map<String, Object> properties) { - loadProperties(properties, DEFAULT_FILENAME); - } - - /** - * - * @param properties - */ - public static void loadProperties(Map<String, Object> properties, String filename) { - loadProperties(properties, Thread.currentThread().getContextClassLoader(), filename); - loadProperties(properties, new File(filename)); - - String home = System.getProperty("user.home"); - loadProperties(properties, new File(home + System.getProperty("file.separator") + filename)); - - for (Object key : System.getProperties().keySet()) { - String keyName = (String) key; - - if (keyName.startsWith("javax.persistence") || keyName.startsWith("eclipselink")) { - String value = System.getProperty(keyName); - properties.put(keyName, value); - } - } - - properties.put(PersistenceUnitProperties.NON_JTA_DATASOURCE, ""); - } - - public static void loadProperties(Map<String, Object> properties, File file) { - if (file.exists()) { - InputStream in; - try { - in = new FileInputStream(file); - } catch (FileNotFoundException e) { - return; - } - loadProperties(properties, in); - } - } - - public static void loadProperties(Map<String, Object> properties, ClassLoader cl, String resource) { - InputStream in = cl.getResourceAsStream(resource); - loadProperties(properties, in); - } - - public static void loadProperties(Map<String, Object> properties, InputStream in) { - try { - if (in != null) { - Properties exampleProps = new Properties(); - exampleProps.load(in); - in.close(); - - for (Map.Entry<Object, Object> entry : exampleProps.entrySet()) { - properties.put((String) entry.getKey(), entry.getValue()); - } - } - } catch (Exception e) { - // ignore - } finally { - try { - if (in != null) { - in.close(); - } - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - } -}
diff --git a/employee/employee.model/src/test/java/example/JavaSEExample.java b/employee/employee.model/src/test/java/example/JavaSEExample.java index e3797ae..8223771 100644 --- a/employee/employee.model/src/test/java/example/JavaSEExample.java +++ b/employee/employee.model/src/test/java/example/JavaSEExample.java
@@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010-2012 Oracle. All rights reserved. + * Copyright (c) 2010-2013 Oracle. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 * which accompanies this distribution. @@ -12,18 +12,13 @@ ******************************************************************************/ package example; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.Random; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; -import javax.persistence.Persistence; import javax.persistence.TypedQuery; -import org.eclipse.persistence.config.PersistenceUnitProperties; - import model.Employee; import model.Gender; @@ -38,7 +33,7 @@ public class JavaSEExample { public static void main(String[] args) throws Exception { - EntityManagerFactory emf = createEMF(true); + EntityManagerFactory emf = PersistenceTesting.createEMF(true); JavaSEExample example = new JavaSEExample(); try { @@ -64,21 +59,6 @@ } } - /** - * - */ - public static EntityManagerFactory createEMF(boolean replaceTables) { - Map<String, Object> props = new HashMap<String, Object>(); - - ExamplePropertiesLoader.loadProperties(props); - - if (replaceTables) { - props.put(PersistenceUnitProperties.DDL_GENERATION, PersistenceUnitProperties.DROP_AND_CREATE); - props.put(PersistenceUnitProperties.DDL_GENERATION_MODE, PersistenceUnitProperties.DDL_DATABASE_GENERATION); - } - - return Persistence.createEntityManagerFactory("employee", props); - } public void queryAllEmployees(EntityManager em) { List<Employee> results = em.createQuery("SELECT e FROM Employee e", Employee.class).getResultList();
diff --git a/employee/employee.model/src/test/java/example/PersistenceTesting.java b/employee/employee.model/src/test/java/example/PersistenceTesting.java new file mode 100644 index 0000000..e2185fe --- /dev/null +++ b/employee/employee.model/src/test/java/example/PersistenceTesting.java
@@ -0,0 +1,70 @@ +/******************************************************************************* + * Copyright (c) 2010-2013 Oracle. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 + * which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * dclarke - Employee Demo 2.4.2 + ******************************************************************************/ +package example; + +import java.util.HashMap; +import java.util.Map; + +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +import org.eclipse.persistence.config.PersistenceUnitProperties; + +/** + * Persistence testing helper which creates an EMF providing testing overrides + * to use direct JDBC instead of a data source + * + * @author dclarke + * @since EclipseLink 2.4.2 + */ +public class PersistenceTesting { + + public static EntityManagerFactory createEMF(boolean replaceTables) { + Map<String, Object> props = new HashMap<String, Object>(); + + // Ensure the persistence.xml provided data source are ignored for Java + // SE testing + props.put(PersistenceUnitProperties.NON_JTA_DATASOURCE, ""); + props.put(PersistenceUnitProperties.JTA_DATASOURCE, ""); + + // Configure the use of embedded derby for the tests allowing system + // properties of the same name to override + setProperty(props, PersistenceUnitProperties.JDBC_DRIVER, "org.apache.derby.jdbc.EmbeddedDriver"); + setProperty(props, PersistenceUnitProperties.JDBC_URL, "jdbc:derby:target/derby/mysports;create=true"); + setProperty(props, PersistenceUnitProperties.JDBC_USER, "app"); + setProperty(props, PersistenceUnitProperties.JDBC_PASSWORD, "app"); + + // Ensure weaving is used + props.put(PersistenceUnitProperties.WEAVING, "true"); + + if (replaceTables) { + props.put(PersistenceUnitProperties.DDL_GENERATION, PersistenceUnitProperties.DROP_AND_CREATE); + props.put(PersistenceUnitProperties.DDL_GENERATION_MODE, PersistenceUnitProperties.DDL_DATABASE_GENERATION); + } + + return Persistence.createEntityManagerFactory("employee", props); + } + + /** + * Add the system property value if it exists, otherwise use the default + * value. + */ + private static void setProperty(Map<String, Object> props, String key, String defaultValue) { + String value = defaultValue; + if (System.getProperties().containsKey(key)) { + value = System.getProperty(key); + } + props.put(key, value); + } + +}
diff --git a/employee/employee.model/src/test/java/test/ConfigTest.java b/employee/employee.model/src/test/java/test/ConfigTest.java index caf702a..f7d6a6c 100644 --- a/employee/employee.model/src/test/java/test/ConfigTest.java +++ b/employee/employee.model/src/test/java/test/ConfigTest.java
@@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010-2012 Oracle. All rights reserved. + * Copyright (c) 2010-2013 Oracle. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 * which accompanies this distribution. @@ -19,7 +19,7 @@ import org.junit.BeforeClass; import org.junit.Test; -import example.JavaSEExample; +import example.PersistenceTesting; public class ConfigTest { @@ -38,7 +38,7 @@ @BeforeClass public static void createEMF() { - emf = JavaSEExample.createEMF(true); + emf = PersistenceTesting.createEMF(true); } @AfterClass
diff --git a/employee/employee.model/src/test/resources/eclipselink-example-employee.properties b/employee/employee.model/src/test/resources/eclipselink-example-employee.properties deleted file mode 100644 index ef876f4..0000000 --- a/employee/employee.model/src/test/resources/eclipselink-example-employee.properties +++ /dev/null
@@ -1,21 +0,0 @@ -# Database login info -javax.persistence.jdbc.url=jdbc:derby:target/derby/mysports;create=true -javax.persistence.jdbc.user=app -javax.persistence.jdbc.password=app -javax.persistence.jdbc.driver=org.apache.derby.jdbc.EmbeddedDriver - -#Logging config -eclipselink.logging.level=FINE -eclipselink.logging.connection=false -eclipselink.logging.timestamp=false -eclipselink.logging.thread=false -eclipselink.logging.session=false -eclipselink.logging.exceptions=false -eclipselink.logging.level.sql=FINE -eclipselink.logging.level.metadata=WARNING -eclipselink.logging.parameters=true - -#Ensuring weaving is being used -eclipselink.weaving=true - -eclipselink.profiler=QueryMonitor \ No newline at end of file