Added:
/jcatapult-persistence/trunk/src/java/main/org/jcatapult/persistence/MySQLTools.java
/jcatapult-persistence/trunk/src/java/main/org/jcatapult/persistence/PostgreSQLTools.java
/jcatapult-persistence/trunk/src/java/main/org/jcatapult/persistence/ProjectTools.java
Deleted:
/jcatapult-persistence/trunk/src/java/main/org/jcatapult/persistence/DatabaseTools.java
Modified:
/jcatapult-persistence/trunk/.classpath
/jcatapult-persistence/trunk/jcatapult-persistence.eml
/jcatapult-persistence/trunk/jcatapult-persistence.iml
/jcatapult-persistence/trunk/jcatapult-persistence.ipr
/jcatapult-persistence/trunk/project.xml
/jcatapult-persistence/trunk/src/conf/test/unit/META-INF/persistence.xml
/jcatapult-persistence/trunk/src/conf/test/unit/logging.properties
/jcatapult-persistence/trunk/src/java/test/unit/org/jcatapult/persistence/service/jpa/JPAPersistenceServiceTest.java
/jcatapult-persistence/trunk/src/java/test/unit/org/jcatapult/persistence/service/jpa/User.java
/jcatapult-persistence/trunk/src/java/test/unit/org/jcatapult/persistence/test/JPABaseTest.java
/jcatapult-persistence/trunk/src/java/test/unit/org/jcatapult/persistence/test/JPATestHelper.java
/jcatapult-persistence/trunk/src/java/test/unit/org/jcatapult/persistence/txn/TransactionTest.java
/jcatapult-persistence/trunk/src/java/test/unit/org/jcatapult/servlet/JCatapultFilterTest.java
=======================================
--- /dev/null
+++
/jcatapult-persistence/trunk/src/java/main/org/jcatapult/persistence/MySQLTools.java
Mon Oct 19 11:27:57 2009
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2001-2007, JCatapult.org, All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
+ */
+package org.jcatapult.persistence;
+
+import java.util.logging.Logger;
+
+import net.java.naming.MockJNDI;
+
+import static org.jcatapult.persistence.ProjectTools.*;
+
+import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
+
+/**
+ * <p>
+ * This is a toolkit that provides helper methods for working with
+ * relational databases. Some of this is specific to MySQL and should
+ * eventually be refactored.
+ * </p>
+ *
+ * @author James Humphrey
+ * @author Brian Pontarelli
+ */
+public class MySQLTools {
+ private static final Logger logger =
Logger.getLogger(MySQLTools.class.getName());
+
+ /**
+ * Sets up the connection pool to MySQL and puts that into the JNDI
tree. This uses the
+ * project.xml file and the $HOME/build.properties file to grab the
project name and DB username
+ * and password.
+ *
+ * @param jndi {@link MockJNDI}
+ * @param dbName the db name
+ * @return The DataSource and never null.
+ */
+ public static MysqlDataSource setup(MockJNDI jndi, String dbName) {
+ String projectName = loadProjectName();
+
+ // if the dbName is empty then assume <projectName>_test
+ if (dbName == null || dbName.isEmpty()) {
+ dbName = projectName.replace('-', '_').replace('.', '_')
+ "_test";
+ }
+
+ String url = "jdbc:mysql://localhost:3306/" + dbName
+ "?user=dev&password=dev";
+ return setup(jndi, url, projectName);
+ }
+
+ /**
+ * Sets up the JDBC connection pool to MySQL and puts that into the
JNDI tree.
+ *
+ * @param jndi The {@link MockJNDI} instance.
+ * @param url The database url.
+ * @param projectName The name of the project that is used to build
the JNDI tree.
+ * @return The DataSource and never null.
+ */
+ public static MysqlDataSource setup(MockJNDI jndi, String url, String
projectName) {
+ MysqlDataSource dataSource = new MysqlDataSource();
+ dataSource.setURL(url);
+ dataSource.setAutoReconnect(true);
+
+ String jndiName = "java:comp/env/jdbc/" + projectName;
+ jndi.bind(jndiName, dataSource);
+
+ logger.info("DB Url [" + url + "]");
+ logger.info("JNDI name is [" + jndiName + "]");
+
+ return dataSource;
+ }
+}
=======================================
--- /dev/null
+++
/jcatapult-persistence/trunk/src/java/main/org/jcatapult/persistence/PostgreSQLTools.java
Mon Oct 19 11:27:57 2009
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2001-2007, JCatapult.org, All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
+ */
+package org.jcatapult.persistence;
+
+import java.util.logging.Logger;
+
+import net.java.naming.MockJNDI;
+import static org.jcatapult.persistence.ProjectTools.*;
+import org.postgresql.ds.PGSimpleDataSource;
+
+/**
+ * <p>
+ * This is a toolkit that provides helper methods for working with a
PostgreSQL
+ * relational databases.
+ * </p>
+ *
+ * @author James Humphrey
+ * @author Brian Pontarelli
+ */
+public class PostgreSQLTools {
+ private static final Logger logger =
Logger.getLogger(PostgreSQLTools.class.getName());
+
+ /**
+ * Sets up the connection pool to PostgreSQL and puts that into the
JNDI tree. This uses the
+ * project.xml file and the $HOME/build.properties file to grab the
project name and DB username
+ * and password.
+ *
+ * @param jndi {@link net.java.naming.MockJNDI}
+ * @param dbName the db name
+ * @return The DataSource and never null.
+ */
+ public static PGSimpleDataSource setup(MockJNDI jndi, String dbName) {
+ String projectName = loadProjectName();
+
+ // if the dbName is empty then assume <projectName>_test
+ if (dbName == null || dbName.isEmpty()) {
+ dbName = projectName.replace('-', '_').replace('.', '_')
+ "_test";
+ }
+
+ PGSimpleDataSource pds = new PGSimpleDataSource();
+ pds.setDatabaseName(dbName);
+ pds.setServerName("localhost");
+ pds.setUser("dev");
+ pds.setPassword("dev");
+
+ String jndiName = "java:comp/env/jdbc/" + projectName;
+ jndi.bind(jndiName, pds);
+
+ logger.info("JNDI name is [" + jndiName + "]");
+
+ return pds;
+ }
+}
=======================================
--- /dev/null
+++
/jcatapult-persistence/trunk/src/java/main/org/jcatapult/persistence/ProjectTools.java
Mon Oct 19 11:27:57 2009
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2009, JCatapult.org, All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
+ */
+package org.jcatapult.persistence;
+
+import java.io.File;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import org.w3c.dom.Document;
+
+/**
+ * <p>
+ * This class grabs the project name from the project.xml file.
+ * </p>
+ *
+ * @author Brian Pontarelli
+ */
+public class ProjectTools {
+
+ /**
+ * Loads the project name from the project.xml file.
+ *
+ * @return The project name.
+ */
+ public static String loadProjectName() {
+ // Open project.xml and grab the project name for the JDBC
connections
+ File f = new File("project.xml");
+ Document dom;
+ try {
+ DocumentBuilder builder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ dom = builder.parse(f);
+ } catch (Exception e) {
+ throw new AssertionError("Unable to locate the project.xml for
the project. Make sure " +
+ "you are running ant tests from inside the project and
that your project has this " +
+ "file.");
+ }
+
+ return dom.getDocumentElement().getAttribute("name").trim();
+ }
+}
=======================================
---
/jcatapult-persistence/trunk/src/java/main/org/jcatapult/persistence/DatabaseTools.java
Thu Oct 9 11:03:00 2008
+++ /dev/null
@@ -1,108 +0,0 @@
-/*
- * Copyright (c) 2001-2007, JCatapult.org, All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
- * either express or implied. See the License for the specific
- * language governing permissions and limitations under the License.
- */
-package org.jcatapult.persistence;
-
-import java.io.File;
-import java.util.logging.Logger;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-
-import org.w3c.dom.Document;
-
-import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
-import net.java.naming.MockJNDI;
-
-/**
- * <p>
- * This is a toolkit that provides helper methods for working with
- * relational databases. Some of this is specific to MySQL and should
- * eventually be refactored.
- * </p>
- *
- * @author James Humphrey
- * @author Brian Pontarelli
- */
-public class DatabaseTools {
- private static final Logger logger =
Logger.getLogger(DatabaseTools.class.getName());
-
- /**
- * Sets up the JDBC connection pool to MySQL and puts that into the
JNDI tree. This uses the
- * project.xml file and the $HOME/build.properties file to grab the
project name and DB username
- * and password.
- *
- * @param jndi {@link MockJNDI}
- * @param dbName the db name
- * @return The DataSource and never null.
- */
- public static MysqlDataSource setupJDBCandJNDI(MockJNDI jndi, String
dbName) {
- String projectName = loadProjectName();
-
- // if the dbName is empty then assume <projectName>_test
- if (dbName == null || dbName.isEmpty()) {
- dbName = projectName.replace('-', '_').replace('.', '_')
+ "_test";
- }
-
- String url = "jdbc:mysql://localhost:3306/" + dbName
+ "?user=dev&password=dev";
- return setupJDBCURLandJNDI(jndi, url, projectName);
- }
-
- /**
- * Sets up the JDBC connection pool to MySQL and puts that into the
JNDI tree.
- *
- * @param jndi The {@link MockJNDI} instance.
- * @param url The database url.
- * @param projectName The name of the project that is used to build
the JNDI tree.
- * @return The DataSource and never null.
- */
- public static MysqlDataSource setupJDBCURLandJNDI(MockJNDI jndi,
String url, String projectName) {
- MysqlDataSource dataSource = new MysqlDataSource();
- dataSource.setURL(url);
- dataSource.setAutoReconnect(true);
-
- String jndiName = "java:comp/env/jdbc/" + projectName;
- jndi.bind(jndiName, dataSource);
-
- logger.info("DB Url [" + url + "]");
- logger.info("JNDI name is [" + jndiName + "]");
-
- return dataSource;
- }
-
- public static MysqlDataSource setupJDBCandJNDI(MockJNDI jndi) {
- return setupJDBCandJNDI(jndi, null);
- }
-
- /**
- * Loads the project name from the project.xml file.
- *
- * @return The project name.
- */
- public static String loadProjectName() {
- // Open project.xml and grab the project name for the JDBC
connections
- File f = new File("project.xml");
- Document dom;
- try {
- DocumentBuilder builder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
- dom = builder.parse(f);
- } catch (Exception e) {
- throw new AssertionError("Unable to locate the project.xml for
the project. Make sure " +
- "you are running ant tests from inside the project and
that your project has this " +
- "file.");
- }
-
- return dom.getDocumentElement().getAttribute("name").trim();
- }
-}
=======================================
--- /jcatapult-persistence/trunk/.classpath Mon Sep 28 10:50:47 2009
+++ /jcatapult-persistence/trunk/.classpath Mon Oct 19 11:27:57 2009
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
- <classpathentry kind="src" path="src/conf/main"/>
- <classpathentry kind="src" output="target/classes/test/integration"
path="src/conf/test/integration"/>
- <classpathentry kind="src" output="target/classes/test/unit"
path="src/conf/test/unit"/>
<classpathentry kind="src" path="src/java/main"/>
+ <classpathentry kind="src" path="src/conf/main"/>
<classpathentry kind="src" output="target/classes/test/integration"
path="src/java/test/integration"/>
<classpathentry kind="src" output="target/classes/test/unit"
path="src/java/test/unit"/>
+ <classpathentry kind="src" output="target/classes/test/integration"
path="src/conf/test/integration"/>
+ <classpathentry kind="src" output="target/classes/test/unit"
path="src/conf/test/unit"/>
<classpathentry kind="con"
path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="var"
path="SAVANT_REPOSITORY/com/google/code/guice/guice/1.0/guice-1.0.jar"
sourcepath="/SAVANT_REPOSITORY/com/google/code/guice/guice/1.0/guice-1.0-src.jar"/>
<classpathentry kind="var"
path="SAVANT_REPOSITORY/com/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6.jar"
sourcepath="/SAVANT_REPOSITORY/com/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6-src.jar"/>
@@ -35,5 +35,6 @@
<classpathentry kind="var"
path="SAVANT_REPOSITORY/org/joda/joda-time/joda-time-hibernate/1.0/joda-time-hibernate-1.0.jar"
sourcepath="/SAVANT_REPOSITORY/org/joda/joda-time/joda-time-hibernate/1.0/joda-time-hibernate-1.0-src.jar"/>
<classpathentry kind="var"
path="SAVANT_REPOSITORY/org/junit/junit/4.4/junit-4.4.jar"
sourcepath="/SAVANT_REPOSITORY/org/junit/junit/4.4/junit-4.4-src.jar"/>
<classpathentry kind="var"
path="SAVANT_REPOSITORY/org/objectweb/asm/asm/3.1/asm-3.1.jar"
sourcepath="/SAVANT_REPOSITORY/org/objectweb/asm/asm/3.1/asm-3.1-src.jar"/>
+ <classpathentry kind="var"
path="SAVANT_REPOSITORY/org/postgresql/postgresql-jdbc3/8.4.701/postgresql-jdbc3-8.4.701.jar"
sourcepath="/SAVANT_REPOSITORY/org/postgresql/postgresql-jdbc3/8.4.701/postgresql-jdbc3-8.4.701-src.jar"/>
<classpathentry kind="output"
path="target/classes/production/jcatapult-persistence"/>
</classpath>
=======================================
--- /jcatapult-persistence/trunk/jcatapult-persistence.eml Mon Sep 28
10:50:47 2009
+++ /jcatapult-persistence/trunk/jcatapult-persistence.eml Mon Oct 19
11:27:57 2009
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<component inherit-compiler-output="true">
- <exclude-output/>
<contentEntry url="file://$MODULE_DIR$">
<testFolder url="file://$MODULE_DIR$/src/java/test/integration"/>
<testFolder url="file://$MODULE_DIR$/src/java/test/unit"/>
+ <testFolder url="file://$MODULE_DIR$/src/conf/test/integration"/>
+ <testFolder url="file://$MODULE_DIR$/src/conf/test/unit"/>
<excludeFolder url="file://$MODULE_DIR$/target"/>
<excludeFolder url="file://$MODULE_DIR$/target/classes"/>
</contentEntry>
=======================================
--- /jcatapult-persistence/trunk/jcatapult-persistence.iml Mon Sep 28
10:50:47 2009
+++ /jcatapult-persistence/trunk/jcatapult-persistence.iml Mon Oct 19
11:27:57 2009
@@ -6,7 +6,9 @@
<setting name="validation-enabled" value="true" />
<setting name="provider-name" value="" />
<setting name="targe-facet" value="" />
- <datasource-mapping />
+ <datasource-mapping>
+ <factory-entry name="punit" />
+ </datasource-mapping>
<deploymentDescriptor name="persistence.xml"
url="file://$MODULE_DIR$/src/conf/test/unit/META-INF/persistence.xml" />
</configuration>
</facet>
=======================================
--- /jcatapult-persistence/trunk/jcatapult-persistence.ipr Mon Sep 28
10:50:47 2009
+++ /jcatapult-persistence/trunk/jcatapult-persistence.ipr Mon Oct 19
11:27:57 2009
@@ -40,8 +40,28 @@
<entry name="?*.tld" />
</wildcardResourcePatterns>
</component>
- <component name="CopyrightManager" default="">
+ <component name="CopyrightManager" default="JCatapult">
+ <copyright>
+ <option name="notice" value="Copyright (c) &#36;today.year,
JCatapult.org, All Rights Reserved Licensed under the Apache
License, Version 2.0 (the "License"); you may not use this
file except in compliance with the License. You may obtain a copy of
the License at
http://www.apache.org/licenses/LICENSE-2.0 Unless required by
applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
the License for the specific language governing permissions and
limitations under the License." />
+ <option name="keyword" value="Copyright" />
+ <option name="myName" value="JCatapult" />
+ <option name="myLocal" value="true" />
+ </copyright>
<module2copyright />
+ <LanguageOptions name="__TEMPLATE__">
+ <option name="fileTypeOverride" value="2" />
+ <option name="relativeBefore" value="true" />
+ <option name="addBlankAfter" value="false" />
+ <option name="fileLocation" value="1" />
+ <option name="block" value="true" />
+ <option name="separateBefore" value="false" />
+ <option name="separateAfter" value="false" />
+ <option name="prefixLines" value="true" />
+ <option name="lenBefore" value="80" />
+ <option name="lenAfter" value="80" />
+ <option name="box" value="false" />
+ <option name="filler" value=" " />
+ </LanguageOptions>
</component>
<component name="DependenciesAnalyzeManager">
<option name="myForwardDirection" value="false" />
=======================================
--- /jcatapult-persistence/trunk/project.xml Mon Sep 28 10:50:47 2009
+++ /jcatapult-persistence/trunk/project.xml Mon Oct 19 11:27:57 2009
@@ -1,13 +1,13 @@
-<project xmlns="http://www.inversoft.org/schemas/savant-2.0/project"
+<project xmlns="http://www.inversoft.org/schemas/savant-1.5/project"
name="jcatapult-persistence"
group="jcatapult.org"
- version="1.0.3">
+ version="1.0.4">
<plugin group="plugins.savant.inversoft.org" name="ide" version="1.0"/>
<plugin group="plugins.savant.inversoft.org" name="clean" version="1.0"/>
<plugin group="plugins.savant.inversoft.org" name="java" version="1.0"/>
- <plugin group="plugins.savant.inversoft.org" name="junit" version="1.0"/>
- <plugin group="plugins.savant.inversoft.org" name="mysql" version="1.0"/>
+ <plugin group="plugins.savant.inversoft.org" name="junit"
version="1.0.1-{integration}" facet="database"/>
+ <plugin group="plugins.savant.inversoft.org" name="database"
version="1.0"/>
<plugin group="plugins.savant.inversoft.org" name="release-svn"
version="1.0"/>
<!-- Compile properties -->
@@ -15,6 +15,7 @@
<!-- This libary has hibernate entities and requires database creation
-->
<property name="project.requires.database" value="true"/>
+ <property name="project.database.types" value="mysql,postgresql"/>
<property name="version.jcatapult" value="1.0"/>
@@ -27,6 +28,7 @@
<artifact-group type="compile-only">
<artifact group="servlet.javax" name="servlet-api" version="2.4"/>
<artifact group="mysql.com" name="mysql-connector-java"
version="5.1.6"/>
+ <artifact group="postgresql.org" name="postgresql-jdbc3"
version="8.4.701"/>
</artifact-group>
<artifact-group type="compile">
<artifact group="aopalliance.sourceforge.net" name="aopalliance"
version="1.0"/>
=======================================
---
/jcatapult-persistence/trunk/src/conf/test/unit/META-INF/persistence.xml
Thu Oct 9 11:03:00 2008
+++
/jcatapult-persistence/trunk/src/conf/test/unit/META-INF/persistence.xml
Mon Oct 19 11:27:57 2009
@@ -9,9 +9,9 @@
<class>org.jcatapult.domain.commerce.MoneyHolder</class>
<class>org.jcatapult.domain.commerce.MoneyAmountHolder</class>
<properties>
- <property name="hibernate.dialect"
value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
+ <!--<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQL5InnoDBDialect"/>-->
<property name="hibernate.hbm2ddl.auto" value="create"/>
- <property name="hibernate.show_sql" value="true"/>
+ <!--<property name="hibernate.show_sql" value="true"/>-->
</properties>
</persistence-unit>
</persistence>
=======================================
--- /jcatapult-persistence/trunk/src/conf/test/unit/logging.properties Thu
Jul 3 11:52:22 2008
+++ /jcatapult-persistence/trunk/src/conf/test/unit/logging.properties Mon
Oct 19 11:27:57 2009
@@ -18,3 +18,5 @@
java.util.logging.ConsoleHandler.level=FINEST
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
+
+org.hibernate.level=WARNING
=======================================
---
/jcatapult-persistence/trunk/src/java/test/unit/org/jcatapult/persistence/service/jpa/JPAPersistenceServiceTest.java
Tue Jun 16 13:50:04 2009
+++
/jcatapult-persistence/trunk/src/java/test/unit/org/jcatapult/persistence/service/jpa/JPAPersistenceServiceTest.java
Mon Oct 19 11:27:57 2009
@@ -17,10 +17,9 @@
import java.sql.SQLException;
import java.util.List;
-import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
-import javax.persistence.RollbackException;
+import javax.persistence.PersistenceException;
import javax.sql.RowSet;
import static net.java.util.CollectionTools.*;
@@ -39,11 +38,11 @@
@Test
public void testReloadAttached() throws Exception {
- clearTable("User");
- executeSQL("insert into User (insert_date, update_date, name) " +
- "values (now(), now(), 'Fred')");
- executeSQL("insert into User (insert_date, update_date, name) " +
- "values (now(), now(), 'George')");
+ clearTable("users");
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
+ "values (1, now(), now(), 'Fred')");
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
+ "values (2, now(), now(), 'George')");
EntityManager em = EntityManagerContext.get();
User user = (User) em.createQuery("select u from User u where
u.name = 'Fred'").getSingleResult();
@@ -57,11 +56,11 @@
@Test
public void testReloadDetached() throws Exception {
- clearTable("User");
- executeSQL("insert into User (insert_date, update_date, name) " +
- "values (now(), now(), 'Fred')");
- executeSQL("insert into User (insert_date, update_date, name) " +
- "values (now(), now(), 'George')");
+ clearTable("users");
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
+ "values (1, now(), now(), 'Fred')");
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
+ "values (2, now(), now(), 'George')");
EntityManager em = EntityManagerContext.get();
User user = (User) em.createQuery("select u from User u where
u.name = 'Fred'").getSingleResult();
@@ -79,17 +78,17 @@
@Test
public void testFindAll() throws Exception {
- clearTable("User");
- executeSQL("insert into User (insert_date, update_date, name) " +
- "values (now(), now(), 'Fred')");
- executeSQL("insert into User (insert_date, update_date, name) " +
- "values (now(), now(), 'George')");
+ clearTable("users");
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
+ "values (1, now(), now(), 'Fred')");
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
+ "values (2, now(), now(), 'George')");
clearTable("SoftDeletableUser");
- executeSQL("insert into SoftDeletableUser (insert_date,
update_date, name, deleted) " +
- "values (now(), now(), 'Fred', false)");
- executeSQL("insert into SoftDeletableUser (insert_date,
update_date, name, deleted) " +
- "values (now(), now(), 'George', true)");
+ executeSQL("insert into SoftDeletableUser (id, insert_date,
update_date, name, deleted) " +
+ "values (1, now(), now(), 'Fred', false)");
+ executeSQL("insert into SoftDeletableUser (id, insert_date,
update_date, name, deleted) " +
+ "values (2, now(), now(), 'George', true)");
// This tests that non-soft delete find all works.
JPAPersistenceService service = new JPAPersistenceService(new
EntityManagerProxy());
@@ -122,16 +121,16 @@
@Test
public void testFind() throws Exception {
- clearTable("User");
+ clearTable("users");
for (int i = 0; i < 100; i++) {
- executeSQL("insert into User (insert_date, update_date,
name) " +
- "values (now(), now(), 'Fred" + i + "')");
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
+ "values (" + (i + 1) + ", now(), now(), 'Fred" + i + "')");
}
clearTable("SoftDeletableUser");
for (int i = 0; i < 100; i++) {
- executeSQL("insert into SoftDeletableUser (insert_date,
update_date, name, deleted) " +
- "values (now(), now(), 'Fred" + i + "', " + ((i % 2 ==
0) ? "false" : "true") + ")");
+ executeSQL("insert into SoftDeletableUser (id, insert_date,
update_date, name, deleted) " +
+ "values (" + (i + 1) + ", now(), now(), 'Fred" + i + "', "
+ ((i % 2 == 0) ? "false" : "true") + ")");
}
JPAPersistenceService service = new JPAPersistenceService(new
EntityManagerProxy());
@@ -178,12 +177,12 @@
@Test
public void testFindById() throws SQLException {
- clearTable("User");
- executeSQL("insert into User (id, insert_date, update_date,
name) " +
+ clearTable("users");
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
"values (1, now(), now(), 'Fred')");
- executeSQL("insert into User (id, insert_date, update_date,
name) " +
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
"values (2, now(), now(), 'George')");
- executeSQL("insert into User (id, insert_date, update_date,
name) " +
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
"values (3, now(), now(), 'Alan')");
// This tests that querying by id works
@@ -203,12 +202,12 @@
@Test
public void testFindByIdNoVerify() throws SQLException {
- clearTable("User");
- executeSQL("insert into User (id, insert_date, update_date,
name) " +
+ clearTable("users");
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
"values (1, now(), now(), 'Fred')");
- executeSQL("insert into User (id, insert_date, update_date,
name) " +
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
"values (2, now(), now(), 'George')");
- executeSQL("insert into User (id, insert_date, update_date,
name) " +
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
"values (3, now(), now(), 'Alan')");
// This tests that querying by id works
@@ -229,17 +228,17 @@
@Test
public void testQueryAll() throws Exception {
- clearTable("User");
- executeSQL("insert into User (insert_date, update_date, name) " +
- "values (now(), now(), 'Fred')");
- executeSQL("insert into User (insert_date, update_date, name) " +
- "values (now(), now(), 'George')");
- executeSQL("insert into User (insert_date, update_date, name) " +
- "values (now(), now(), 'Alan')");
+ clearTable("users");
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
+ "values (1, now(), now(), 'Fred')");
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
+ "values (2, now(), now(), 'George')");
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
+ "values (3, now(), now(), 'Alan')");
// This tests that querying with an orderBy clause works
JPAPersistenceService service = new JPAPersistenceService(new
EntityManagerProxy());
- List<User> users = service.queryAll(User.class, "select user from
User user order by user.name");
+ List<User> users = service.queryAll(User.class, "select u from
User u order by u.name");
assertEquals(3, users.size());
assertEquals("Alan", users.get(0).getName());
assertEquals("Fred", users.get(1).getName());
@@ -248,36 +247,36 @@
@Test
public void testQueryCount() throws Exception {
- clearTable("User");
- executeSQL("insert into User (insert_date, update_date, name) " +
- "values (now(), now(), 'Fred')");
- executeSQL("insert into User (insert_date, update_date, name) " +
- "values (now(), now(), 'George')");
- executeSQL("insert into User (insert_date, update_date, name) " +
- "values (now(), now(), 'Alan')");
+ clearTable("users");
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
+ "values (1, now(), now(), 'Fred')");
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
+ "values (2, now(), now(), 'George')");
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
+ "values (3, now(), now(), 'Alan')");
// This tests that querying with an orderBy clause works
JPAPersistenceService service = new JPAPersistenceService(new
EntityManagerProxy());
- long count = service.queryCount("select count(user) from User user
where user.name = ?1", "Alan");
+ long count = service.queryCount("select count(u) from User u where
u.name = ?1", "Alan");
assertEquals(1, count);
- count = service.queryCount("select count(user) from User user");
+ count = service.queryCount("select count(u) from User u");
assertEquals(3, count);
}
@Test
public void testQuery() throws Exception {
- clearTable("User");
+ clearTable("users");
char[] alphabet =
{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
for (int i = 25; i >= 0; i--) {
- executeSQL("insert into User (insert_date, update_date,
name) " +
- "values (now(), now(), 'Fred" + alphabet[i] + "')");
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
+ "values (" + (i + 1) + ", now(), now(), 'Fred" +
alphabet[i] + "')");
}
// This tests that querying with an orderBy clause works
JPAPersistenceService service = new JPAPersistenceService(new
EntityManagerProxy());
- List<User> users = service.query(User.class, "select user from
User user order by user.name", 3, 21);
+ List<User> users = service.query(User.class, "select u from User u
order by u.name", 3, 21);
// System.out.println("List is \n" + users);
assertEquals(21, users.size());
for (int i = 0; i < 21; i++) {
@@ -287,17 +286,17 @@
@Test
public void testQueryAllWithNamedParameters() throws Exception {
- clearTable("User");
- executeSQL("insert into User (insert_date, update_date, name) " +
- "values (now(), now(), 'Fred')");
- executeSQL("insert into User (insert_date, update_date, name) " +
- "values (now(), now(), 'George')");
- executeSQL("insert into User (insert_date, update_date, name) " +
- "values (now(), now(), 'Alan')");
+ clearTable("users");
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
+ "values (1, now(), now(), 'Fred')");
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
+ "values (2, now(), now(), 'George')");
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
+ "values (3, now(), now(), 'Alan')");
// This tests that querying with an orderBy clause works
JPAPersistenceService service = new JPAPersistenceService(new
EntityManagerProxy());
- List<User> users =
service.queryAllWithNamedParameters(User.class, "select user from User user
where name = :name order by user.name",
+ List<User> users =
service.queryAllWithNamedParameters(User.class, "select u from User u where
u.name = :name order by u.name",
mapNV("name", "Alan"));
assertEquals(1, users.size());
assertEquals("Alan", users.get(0).getName());
@@ -305,37 +304,37 @@
@Test
public void testQueryCountWithNamedParameters() throws Exception {
- clearTable("User");
- executeSQL("insert into User (insert_date, update_date, name) " +
- "values (now(), now(), 'Fred')");
- executeSQL("insert into User (insert_date, update_date, name) " +
- "values (now(), now(), 'George')");
- executeSQL("insert into User (insert_date, update_date, name) " +
- "values (now(), now(), 'Alan')");
+ clearTable("users");
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
+ "values (1, now(), now(), 'Fred')");
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
+ "values (2, now(), now(), 'George')");
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
+ "values (3, now(), now(), 'Alan')");
// This tests that querying with an orderBy clause works
JPAPersistenceService service = new JPAPersistenceService(new
EntityManagerProxy());
- long count = service.queryCountWithNamedParameters("select
count(user) from User user where user.name = :name",
+ long count = service.queryCountWithNamedParameters("select
count(u) from User u where u.name = :name",
mapNV("name", "Alan"));
assertEquals(1, count);
- count = service.queryCountWithNamedParameters("select count(user)
from User user", mapNV());
+ count = service.queryCountWithNamedParameters("select count(u)
from User u", mapNV());
assertEquals(3, count);
}
@Test
public void testQueryWithNamedParameters() throws Exception {
- clearTable("User");
+ clearTable("users");
char[] alphabet =
{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
for (int i = 25; i >= 0; i--) {
- executeSQL("insert into User (insert_date, update_date,
name) " +
- "values (now(), now(), 'Fred" + alphabet[i] + "')");
+ executeSQL("insert into users (id, insert_date, update_date,
name) " +
+ "values (" + (i + 1) + ", now(), now(), 'Fred" +
alphabet[i] + "')");
}
// This tests that querying with an orderBy clause works
JPAPersistenceService service = new JPAPersistenceService(new
EntityManagerProxy());
- List<User> users =
service.queryWithNamedParameters(User.class, "select user from User user
where name like :name order by user.name", 3, 21,
+ List<User> users =
service.queryWithNamedParameters(User.class, "select u from User u where
u.name like :name order by u.name", 3, 21,
mapNV("name", "%Fred%"));
assertEquals(21, users.size());
for (int i = 0; i < 21; i++) {
@@ -345,7 +344,7 @@
@Test
public void testInsert() throws Exception {
- clearTable("User");
+ clearTable("users");
JPAPersistenceService service = new JPAPersistenceService(new
EntityManagerProxy());
// Test that the persist works and that it correctly handles the
dates using the interceptor
@@ -363,7 +362,7 @@
try {
service.persist(user);
fail("Should have failed");
- } catch (EntityExistsException e) {
+ } catch (PersistenceException e) {
}
}
@@ -394,7 +393,7 @@
try {
service.persist(user);
fail("Should have failed");
- } catch (RollbackException e) {
+ } catch (PersistenceException e) {
}
}
@@ -418,7 +417,7 @@
@Test
public void testPersistOuterTransaction() throws Exception {
- clearTable("User");
+ clearTable("users");
JPAPersistenceService service = new JPAPersistenceService(new
EntityManagerProxy());
EntityManager em = EntityManagerContext.get();
@@ -431,7 +430,7 @@
service.persist(user);
// Check via JDBC that the data is NOT in the database yet
- RowSet rw = executeQuery("select * from User");
+ RowSet rw = executeQuery("select * from users");
assertFalse(rw.next());
rw.close();
@@ -439,7 +438,7 @@
et.commit();
// Now check again
- rw = executeQuery("select * from User");
+ rw = executeQuery("select * from users");
assertTrue(rw.next());
assertEquals("Fred", rw.getString("name"));
rw.close();
@@ -481,7 +480,7 @@
* @throws Exception If things get dicey.
*/
private void doRemove(boolean id, boolean verifyExists) throws
Exception {
- clearTable("User");
+ clearTable("users");
clearTable("SoftDeletableUser");
JPAPersistenceService service = new JPAPersistenceService(new
EntityManagerProxy());
@@ -505,7 +504,7 @@
if (verifyExists) {
// Verify the data is in there
- RowSet rw = executeQuery("select * from User");
+ RowSet rw = executeQuery("select * from users");
assertTrue(rw.next());
assertEquals("Fred", rw.getString("name"));
assertFalse(rw.next());
@@ -520,7 +519,7 @@
rw.close();
} else {
// Verify the data has not been committed yet because there is
an outer transaction
- RowSet rw = executeQuery("select * from User");
+ RowSet rw = executeQuery("select * from users");
assertFalse(rw.next());
rw.close();
@@ -548,7 +547,7 @@
*/
private void ensureDeleted() throws Exception {
// Ensure it was deleted
- RowSet rw = executeQuery("select * from User");
+ RowSet rw = executeQuery("select * from users");
assertFalse(rw.next());
rw.close();
=======================================
---
/jcatapult-persistence/trunk/src/java/test/unit/org/jcatapult/persistence/service/jpa/User.java
Mon Jun 30 09:09:57 2008
+++
/jcatapult-persistence/trunk/src/java/test/unit/org/jcatapult/persistence/service/jpa/User.java
Mon Oct 19 11:27:57 2009
@@ -17,6 +17,7 @@
import javax.persistence.Column;
import javax.persistence.Entity;
+import javax.persistence.Table;
import org.jcatapult.persistence.domain.TimeStampableImpl;
@@ -28,6 +29,7 @@
* @author Brian Pontarelli
*/
@Entity
+@Table(name = "users")
public class User extends TimeStampableImpl {
@Column(unique = true)
private String name;
=======================================
---
/jcatapult-persistence/trunk/src/java/test/unit/org/jcatapult/persistence/test/JPABaseTest.java
Thu Jul 3 11:52:22 2008
+++
/jcatapult-persistence/trunk/src/java/test/unit/org/jcatapult/persistence/test/JPABaseTest.java
Mon Oct 19 11:27:57 2009
@@ -18,6 +18,7 @@
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
+import javax.sql.DataSource;
import javax.sql.RowSet;
import org.jcatapult.test.JCatapultBaseTest;
@@ -56,13 +57,24 @@
/**
* Allows sub-classes to setup different databases. The default is to
setup a database connection
- * to the database that has the same name as the project (dashes are
replaced by underscores).
+ * to the database that has the same name as the project (dashes are
replaced by underscores) and
+ * using the MySQL connection.
*
* @param databaseName The database name.
*/
public static void setDatabaseName(String databaseName) {
JPATestHelper.setDatabaseName(databaseName);
}
+
+ /**
+ * Allows sub-classes to setup different databases. This is a data
source so that PostgreSQL or
+ * Oracle database can be setup.
+ *
+ * @param dataSource The data source to put into the JNDI tree.
+ */
+ public static void setDataSource(DataSource dataSource) {
+ JPATestHelper.setDataSource(dataSource);
+ }
/**
* Constructs the EntityManagerFactory.
=======================================
---
/jcatapult-persistence/trunk/src/java/test/unit/org/jcatapult/persistence/test/JPATestHelper.java
Wed Jul 2 14:54:08 2008
+++
/jcatapult-persistence/trunk/src/java/test/unit/org/jcatapult/persistence/test/JPATestHelper.java
Mon Oct 19 11:27:57 2009
@@ -21,6 +21,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
@@ -30,22 +31,23 @@
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Table;
+import javax.sql.DataSource;
import javax.sql.RowSet;
import javax.sql.rowset.CachedRowSet;
-import org.jcatapult.persistence.DatabaseTools;
-import org.jcatapult.persistence.service.jpa.EntityManagerContext;
-import org.jcatapult.test.Fixture;
-import org.junit.Ignore;
-
-import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
-import com.sun.rowset.CachedRowSetImpl;
import net.java.naming.MockJNDI;
import net.java.sql.ScriptExecutor;
import net.java.text.SimplePluralizer;
import static net.java.util.CollectionTools.*;
import net.java.xml.JavaBeanObjectCreator;
import net.java.xml.Unmarshaller;
+import org.jcatapult.persistence.MySQLTools;
+import org.jcatapult.persistence.PostgreSQLTools;
+import org.jcatapult.persistence.service.jpa.EntityManagerContext;
+import org.jcatapult.test.Fixture;
+import org.junit.Ignore;
+
+import com.sun.rowset.CachedRowSetImpl;
/**
* <p>
@@ -60,7 +62,7 @@
public static final Logger logger =
Logger.getLogger(JPABaseTest.class.getName());
public static String persistentUnit = "punit";
public static EntityManagerFactory emf;
- public static MysqlDataSource dataSource = new MysqlDataSource();
+ public static DataSource dataSource;
public static String databaseName;
/**
@@ -82,7 +84,7 @@
}
/**
- * Allows sub-classes to setup different databases. The default is to
setup a database connection
+ * Allows test classes to setup different databases. The default is to
setup a database connection
* to the database that has the same name as the project (dashes are
replaced by underscores).
*
* @param databaseName The database name.
@@ -90,6 +92,16 @@
public static void setDatabaseName(String databaseName) {
JPATestHelper.databaseName = databaseName;
}
+
+ /**
+ * Allows test classes to setup different databases. This is a data
source so that PostgreSQL or
+ * Oracle database can be setup.
+ *
+ * @param dataSource The data source to put into the JNDI tree.
+ */
+ public static void setDataSource(DataSource dataSource) {
+ JPATestHelper.dataSource = dataSource;
+ }
/**
* Constructs the JDBC connection pool, places it in the JNDI tree
given and then constructs an
@@ -98,10 +110,27 @@
* @param jndi The JNDI tree.
*/
public static void initializeJPA(MockJNDI jndi) {
- dataSource = DatabaseTools.setupJDBCandJNDI(jndi, databaseName);
+ Map<String, String> properties = new HashMap<String, String>();
+
+ if (dataSource == null) {
+ String dbType = System.getProperty("jcatapult.database.type");
+ if (dbType == null || dbType.equals("mysql")) {
+ logger.info("+++++++++++++++++++++++++++++++ Setting up
MySQL data source for testing +++++++++++++++++++++++++++++++");
+ dataSource = MySQLTools.setup(jndi, databaseName);
+
+ // This is required to tell Hibernate to use transactions
+
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
+ } else if (dbType.equals("postgresql")) {
+ logger.info("+++++++++++++++++++++++++++++++ Setting up
PostgreSQL data source for testing +++++++++++++++++++++++++++++++");
+ dataSource = PostgreSQLTools.setup(jndi, databaseName);
+
+ // This is required to tell Hibernate to use postgres to
create the tables
+
properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
+ }
+ }
// Create the JPA EMF
- emf = Persistence.createEntityManagerFactory(persistentUnit);
+ emf = Persistence.createEntityManagerFactory(persistentUnit,
properties);
}
/**
=======================================
---
/jcatapult-persistence/trunk/src/java/test/unit/org/jcatapult/persistence/txn/TransactionTest.java
Mon Jun 30 09:09:57 2008
+++
/jcatapult-persistence/trunk/src/java/test/unit/org/jcatapult/persistence/txn/TransactionTest.java
Mon Oct 19 11:27:57 2009
@@ -51,7 +51,7 @@
@Test
public void testMarco() throws SQLException {
service.success();
- RowSet rs = executeQuery("select name from User where name
= 'TransactionTest-success'");
+ RowSet rs = executeQuery("select name from users where name
= 'TransactionTest-success'");
assertTrue(rs.next());
rs.close();
@@ -60,17 +60,17 @@
} catch (Exception e) {
// Expected
}
- rs = executeQuery("select name from User where name
= 'TransactionTest-failure'");
+ rs = executeQuery("select name from users where name
= 'TransactionTest-failure'");
assertFalse(rs.next());
rs.close();
service.returnValueSuccess();
- rs = executeQuery("select name from User where name
= 'TransactionTest-returnValueSuccess'");
+ rs = executeQuery("select name from users where name
= 'TransactionTest-returnValueSuccess'");
assertTrue(rs.next());
rs.close();
service.returnValueFailure();
- rs = executeQuery("select name from User where name
= 'TransactionTest-returnValueFailure'");
+ rs = executeQuery("select name from users where name
= 'TransactionTest-returnValueFailure'");
assertFalse(rs.next());
rs.close();
}
=======================================
---
/jcatapult-persistence/trunk/src/java/test/unit/org/jcatapult/servlet/JCatapultFilterTest.java
Mon Sep 28 10:50:47 2009
+++
/jcatapult-persistence/trunk/src/java/test/unit/org/jcatapult/servlet/JCatapultFilterTest.java
Mon Oct 19 11:27:57 2009
@@ -30,7 +30,7 @@
import net.java.naming.MockJNDI;
import static org.easymock.EasyMock.*;
import org.jcatapult.environment.Environment;
-import org.jcatapult.persistence.DatabaseTools;
+import org.jcatapult.persistence.MySQLTools;
import org.jcatapult.persistence.service.jpa.EntityManagerContext;
import static org.junit.Assert.*;
import org.junit.Test;
@@ -46,7 +46,7 @@
@Test
public void testJPA() throws ServletException, IOException {
MockJNDI jndi = new MockJNDI();
- DatabaseTools.setupJDBCandJNDI(jndi, "jcatapult_persistence_test");
+ MySQLTools.setup(jndi, "jcatapult_persistence_test");
jndi.bind("java:comp/env/environment", new
Environment("unittesting"));
jndi.activate();