2 Copyright 2010 Sun Microsystems, Inc.
3 All rights reserved. Use is subject to license terms.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 package com.mysql.clusterj.jpatest;
21 import java.util.Arrays;
22 import java.util.HashMap;
25 import javax.persistence.EntityManagerFactory;
26 import javax.persistence.Persistence;
28 import junit.framework.TestCase;
29 import junit.framework.TestResult;
32 * Base class for all ClusterJPA test cases.
34 public abstract class PersistenceTestCase
38 * The {@link TestResult} instance for the current test run.
40 protected TestResult testResult;
44 * Error messages collected during a test.
46 private StringBuffer errorMessages;
48 private String NL = "\n";
50 /** The properties used to create the entity manager factory */
54 * Create an entity manager factory from properties.
55 * @param props configuration values in the form key, value, key, value...
57 protected EntityManagerFactory createEMF(Object... props) {
58 String puName = getPersistenceUnitName();
59 EntityManagerFactory result = createNamedEMF(puName, props);
60 // if (result == null) {
61 // System.out.println("Unable to create EMF with properties PUName " + puName + " props " + Arrays.toString(props));
63 // System.out.println("Created EMF with properties PUName " + puName + " props " + Arrays.toString(props));
69 * The name of the persistence unit that this test class should use
70 * by default. This defaults to "ndb".
72 protected String getPersistenceUnitName() {
73 String puName = System.getProperty(
74 "com.mysql.clusterj.jpa.PersistenceUnit", "ndb");
75 if (puName.length() == 0 || puName.equals("${com.mysql.clusterj.jpa.PersistenceUnit}")) {
83 * Create an entity manager factory for persistence unit <code>pu</code>.
85 * @param props configuration values in the form key, value, key, value...
87 protected EntityManagerFactory createNamedEMF(String pu,
91 for (int i = 0; props != null && i < props.length; i++) {
93 map.put(props[i - 1], props[i]);
95 } else if (props[i] != null)
98 return Persistence.createEntityManagerFactory(pu, map);
102 public void run(TestResult testResult) {
103 this.testResult = testResult;
104 super.run(testResult);
108 public void tearDown() throws Exception {
111 } catch (Exception e) {
112 // if the test failed, swallow any exceptions that happen
113 // during tear-down, as these just mask the original problem.
114 // if the test succeeded, this is a real problem.
115 if (testResult.wasSuccessful())
123 protected boolean closeEMF(EntityManagerFactory emf) {
124 if (emf == null || !emf.isOpen())
127 return !emf.isOpen();
130 protected void initializeErrorMessages() {
131 if (errorMessages == null) {
132 errorMessages = new StringBuffer();
133 errorMessages.append(NL);
137 protected void error(String message) {
138 initializeErrorMessages();
139 errorMessages.append(message + NL);
142 protected void errorIfNotEqual(String message, Object expected, Object actual) {
143 if (expected == null && actual == null) {
146 if (expected != null && expected.equals(actual)) {
149 initializeErrorMessages();
150 errorMessages.append(message + NL);
151 errorMessages.append(
152 "Expected: " + ((expected==null)?"null":expected.toString())
153 + " actual: " + ((actual==null)?"null":actual.toString()) + NL);
157 protected void errorIfNull(String message, Object actual) {
158 if (actual != null) {
161 initializeErrorMessages();
162 errorMessages.append(message + NL);
166 protected void failOnError() {
167 if (errorMessages != null) {
168 fail(errorMessages.toString());