001: /*
002: * BaseKernelTest.java
003: *
004: * Created on October 9, 2006, 12:56 PM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: /*
011: * Licensed to the Apache Software Foundation (ASF) under one
012: * or more contributor license agreements. See the NOTICE file
013: * distributed with this work for additional information
014: * regarding copyright ownership. The ASF licenses this file
015: * to you under the Apache License, Version 2.0 (the
016: * "License"); you may not use this file except in compliance
017: * with the License. You may obtain a copy of the License at
018: *
019: * http://www.apache.org/licenses/LICENSE-2.0
020: *
021: * Unless required by applicable law or agreed to in writing,
022: * software distributed under the License is distributed on an
023: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
024: * KIND, either express or implied. See the License for the
025: * specific language governing permissions and limitations
026: * under the License.
027: */
028: package org.apache.openjpa.persistence.kernel;
029:
030: import java.beans.BeanInfo;
031: import java.beans.Introspector;
032: import java.beans.PropertyDescriptor;
033: import java.lang.reflect.InvocationTargetException;
034: import java.lang.reflect.Method;
035: import java.math.BigDecimal;
036: import java.math.BigInteger;
037: import java.util.*;
038: import javax.management.IntrospectionException;
039: import org.apache.openjpa.kernel.Broker;
040: import org.apache.openjpa.kernel.OpenJPAStateManager;
041: import org.apache.openjpa.lib.conf.ConfigurationProvider;
042: import org.apache.openjpa.lib.conf.Configurations;
043: import org.apache.openjpa.persistence.OpenJPAPersistence;
044: import org.apache.openjpa.persistence.JPAFacadeHelper;
045: import org.apache.openjpa.persistence.kernel.common.apps.*;
046: import java.lang.annotation.Annotation;
047: import junit.framework.*;
048: import javax.persistence.EntityManager;
049: import javax.persistence.EntityManagerFactory;
050: import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
051: import org.apache.openjpa.persistence.OpenJPAEntityManager;
052:
053: public abstract class BaseKernelTest extends
054: org.apache.openjpa.persistence.common.utils.AbstractTestCase {
055: private static Map _sysprops = new HashMap();
056:
057: /** Creates a new instance of BaseKernelTest */
058: public BaseKernelTest() {
059: }
060:
061: public BaseKernelTest(String name) {
062: super (name, "kernelcactusapp");
063: }
064:
065: protected OpenJPAEntityManager getPM() {
066: return currentEntityManager();
067: }
068:
069: protected OpenJPAEntityManager getPM(boolean optimistic,
070: boolean retainValues) {
071: OpenJPAEntityManager em = currentEntityManager();
072: em.setNontransactionalRead(true);
073: em.setRetainState(retainValues);
074: if (em.getTransaction().isActive()) {
075: em.getTransaction().commit();
076: em.setOptimistic(optimistic);
077: }
078:
079: return em;
080: }
081:
082: protected Object persist(Object ob) {
083: //FIXME - this is just a workaround
084: // Broker broker = .getBrokerFactory().newBroker();
085: Broker broker = JPAFacadeHelper
086: .toBroker(currentEntityManager());
087: broker.begin();
088: broker.persist(ob, null);
089: Object id = broker.getObjectId(ob);
090: broker.commit();
091: broker.close();
092: return id;
093: }
094:
095: protected Properties getProperties() {
096: return getProperties(null);
097: }
098:
099: /**
100: * Return the runtime properties, optionally overriding or setting
101: * some via the given array, which should be in the form
102: * { key, val, key, val, ... }.
103: */
104: protected synchronized Properties getProperties(String[] props) {
105: Properties p = new Properties(System.getProperties());
106: String str = p.getProperty("openjpa.properties",
107: "kodo.properties");
108: if (str != null && str.length() > 0) {
109: // cache system properties to reduce load on file system
110: Properties loaded = (Properties) _sysprops.get(str);
111: if (loaded == null) {
112: loaded = new Properties();
113: ConfigurationProvider cp = (ConfigurationProvider) Configurations
114: .getProperty(str, null);
115:
116: if (cp != null)
117: loaded.putAll(cp.getProperties());
118: _sysprops.put(str, loaded);
119: }
120: p.putAll(loaded);
121: }
122:
123: for (int i = 0; props != null && i < props.length; i += 2) {
124: if (props[i + 1] != null) {
125: // remove any duplicate kodo/openjpa property so we don't clash
126: if (props[i].startsWith("openjpa."))
127: p.remove("openjpa." + props[i].substring(5));
128: else if (props[i].startsWith("openjpa."))
129: p.remove("openjpa." + props[i].substring(8));
130:
131: p.setProperty(props[i], props[i + 1]);
132: } else {
133: p.remove(props[i]);
134: }
135: }
136: return p;
137: }
138: //
139: //
140: // /**
141: // * Assert that the given List contain the exact same
142: // * elements. This is different than the normal List contract, which
143: // * states that list1.equals(list2) if each element e1.equals(e2).
144: // * This method asserts that e1 == n2.
145: // */
146: // public static void assertIdentical(List c1, List c2) {
147: // assertEquals(c1.size(), c2.size());
148: // for (Iterator i1 = c1.iterator(), i2 = c2.iterator();
149: // i1.hasNext() && i2.hasNext();)
150: // assertTrue(i1.next() == i2.next());
151: // }
152: //
153: // /**
154: // * Assert that the collection parameter is already ordered
155: // * according to the specified comparator.
156: // */
157: // public void assertOrdered(Collection c, Comparator comp) {
158: // List l1 = new LinkedList(c);
159: // List l2 = new LinkedList(c);
160: // assertEquals(l1, l2);
161: // Collections.sort(l2, comp);
162: // assertEquals(l1, l2);
163: // Collections.sort(l1, comp);
164: // assertEquals(l1, l2);
165: // }
166: //
167: // ////////////////////
168: // // Assertion Helpers
169: // ////////////////////
170: //
171: // public void assertNotEquals(Object a, Object b) {
172: // if (a == null && b != null)
173: // return;
174: // if (a != null && b == null)
175: // return;
176: // if (!(a.equals(b)))
177: // return;
178: // if (!(b.equals(a)))
179: // return;
180: //
181: // fail("expected !<" + a + ">.equals(<" + b + ">)");
182: // }
183: //
184: // public void assertSize(int size, Object ob) {
185: // if (ob == null) {
186: // assertEquals(size, 0);
187: // return;
188: // }
189: //
190: // if (ob instanceof Collection)
191: // ob = ((Collection) ob).iterator();
192: // if (ob instanceof Iterator) {
193: // Iterator i = (Iterator) ob;
194: // int count = 0;
195: // while (i.hasNext()) {
196: // count++;
197: // i.next();
198: // }
199: //
200: // assertEquals(size, count);
201: // } else
202: // fail("assertSize: expected Collection, Iterator, "
203: // + "Query, or Extent, but got "
204: // + ob.getClass().getName());
205: // }
206:
207: }
|