MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PersistenceTestCase.java
1 /*
2  Copyright 2010 Sun Microsystems, Inc.
3  All rights reserved. Use is subject to license terms.
4 
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.
8 
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.
13 
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
17 */
18 
19 package com.mysql.clusterj.jpatest;
20 
21 import java.util.Arrays;
22 import java.util.HashMap;
23 import java.util.Map;
24 
25 import javax.persistence.EntityManagerFactory;
26 import javax.persistence.Persistence;
27 
28 import junit.framework.TestCase;
29 import junit.framework.TestResult;
30 
34 public abstract class PersistenceTestCase
35  extends TestCase {
36 
40  protected TestResult testResult;
41 
46  private StringBuffer errorMessages;
47 
48  private String NL = "\n";
49 
51  protected Map map;
52 
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));
62 // } else {
63 // System.out.println("Created EMF with properties PUName " + puName + " props " + Arrays.toString(props));
64 // }
65  return result;
66  }
67 
73  String puName = System.getProperty(
74  "com.mysql.clusterj.jpa.PersistenceUnit", "ndb");
75  if (puName.length() == 0 || puName.equals("${com.mysql.clusterj.jpa.PersistenceUnit}")) {
76  return "ndb";
77  } else {
78  return puName;
79  }
80  }
81 
87  protected EntityManagerFactory createNamedEMF(String pu,
88  Object... props) {
89  map = new HashMap();
90  boolean prop = false;
91  for (int i = 0; props != null && i < props.length; i++) {
92  if (prop) {
93  map.put(props[i - 1], props[i]);
94  prop = false;
95  } else if (props[i] != null)
96  prop = true;
97  }
98  return Persistence.createEntityManagerFactory(pu, map);
99  }
100 
101  @Override
102  public void run(TestResult testResult) {
103  this.testResult = testResult;
104  super.run(testResult);
105  }
106 
107  @Override
108  public void tearDown() throws Exception {
109  try {
110  super.tearDown();
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())
116  throw e;
117  }
118  }
119 
123  protected boolean closeEMF(EntityManagerFactory emf) {
124  if (emf == null || !emf.isOpen())
125  return false;
126  emf.close();
127  return !emf.isOpen();
128  }
129 
130  protected void initializeErrorMessages() {
131  if (errorMessages == null) {
132  errorMessages = new StringBuffer();
133  errorMessages.append(NL);
134  }
135  }
136 
137  protected void error(String message) {
138  initializeErrorMessages();
139  errorMessages.append(message + NL);
140  }
141 
142  protected void errorIfNotEqual(String message, Object expected, Object actual) {
143  if (expected == null && actual == null) {
144  return;
145  }
146  if (expected != null && expected.equals(actual)) {
147  return;
148  } else {
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);
154  }
155  }
156 
157  protected void errorIfNull(String message, Object actual) {
158  if (actual != null) {
159  return;
160  } else {
161  initializeErrorMessages();
162  errorMessages.append(message + NL);
163  }
164  }
165 
166  protected void failOnError() {
167  if (errorMessages != null) {
168  fail(errorMessages.toString());
169  }
170  }
171 
172 }