001: /*
002: * BaseJDBCTest.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.jdbc.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.kernel.jpql.JPQLParser;
042: import org.apache.openjpa.lib.conf.ConfigurationProvider;
043: import org.apache.openjpa.lib.conf.Configurations;
044: import org.apache.openjpa.persistence.OpenJPAPersistence;
045:
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: import org.apache.openjpa.persistence.JPAFacadeHelper;
053:
054: public class BaseJDBCTest extends
055: org.apache.openjpa.persistence.common.utils.AbstractTestCase {
056:
057: private static Map _sysprops = new HashMap();
058:
059: /** Creates a new instance of BaseJDBCTest */
060: public BaseJDBCTest() {
061: }
062:
063: public BaseJDBCTest(String name) {
064: super (name, "jdbccactusapp");
065: }
066:
067: @Override
068: protected String defaultPersistenceXmlResource() {
069: return "org/apache/openjpa/persistence/jdbc/common"
070: + "/apps/META-INF/persistence.xml";
071: }
072:
073: protected OpenJPAEntityManager getPM() {
074: return (OpenJPAEntityManager) currentEntityManager();
075: }
076:
077: protected OpenJPAEntityManager getPM(boolean optimistic,
078: boolean retainValues) {
079: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
080: em.setNontransactionalRead(true);
081: em.setRetainState(retainValues);
082: em.setOptimistic(optimistic);
083: return em;
084: }
085:
086: /**
087: * Delete all instances of the given class.
088: */
089: protected int deleteAll(Broker broker, Class clazz) {
090: return deleteAll(broker, clazz, true);
091: }
092:
093: /**
094: * Delete all instances of the given class.
095: */
096: protected int deleteAll(Broker broker, Class clazz,
097: boolean subclasses) {
098: final boolean useDeleteByQuery = false;
099:
100: if (useDeleteByQuery) {
101: org.apache.openjpa.kernel.Query query = broker.newQuery(
102: JPQLParser.LANG_JPQL, clazz, "");
103: query.setCandidateType(clazz, subclasses);
104: return (int) query.deleteAll();
105: } else {
106: org.apache.openjpa.kernel.Extent extent = broker.newExtent(
107: clazz, subclasses);
108: List list = extent.list();
109: int size = list.size();
110: broker.deleteAll(list, null);
111: return size;
112: }
113: }
114:
115: /**
116: * Delete all instances of the given class.
117: */
118: protected int deleteAll(OpenJPAEntityManager pm, Class clazz) {
119: return deleteAll(JPAFacadeHelper.toBroker(pm), clazz);
120: }
121:
122: /**
123: * Delete all instances of the given class.
124: */
125: protected int deleteAll(OpenJPAEntityManager pm, Class clazz,
126: boolean subclasses) {
127: return deleteAll(JPAFacadeHelper.toBroker(pm), clazz,
128: subclasses);
129: }
130:
131: protected Object persist(Object ob) {
132: Broker broker = getBrokerFactory().newBroker();
133: broker.begin();
134: broker.persist(ob, null);
135: Object id = broker.getObjectId(ob);
136: broker.commit();
137: broker.close();
138: return id;
139: }
140:
141: protected Properties getProperties() {
142: return getProperties(null);
143: }
144:
145: /**
146: * Return the runtime properties, optionally overriding or setting
147: * some via the given array, which should be in the form
148: * { key, val, key, val, ... }.
149: */
150: protected synchronized Properties getProperties(String[] props) {
151: Properties p = new Properties(System.getProperties());
152: String str = p.getProperty("openjpa.properties",
153: "kodo.properties");
154: if (str != null && str.length() > 0) {
155: // cache system properties to reduce load on file system
156: Properties loaded = (Properties) _sysprops.get(str);
157: if (loaded == null) {
158: loaded = new Properties();
159: ConfigurationProvider cp = (ConfigurationProvider) Configurations
160: .getProperty(str, null);
161:
162: if (cp != null)
163: loaded.putAll(cp.getProperties());
164: _sysprops.put(str, loaded);
165: }
166: p.putAll(loaded);
167: }
168:
169: for (int i = 0; props != null && i < props.length; i += 2) {
170: if (props[i + 1] != null) {
171: // remove any duplicate kodo/openjpa property so we don't clash
172: if (props[i].startsWith("openjpa."))
173: p.remove("openjpa." + props[i].substring(5));
174: else if (props[i].startsWith("openjpa."))
175: p.remove("openjpa." + props[i].substring(8));
176:
177: p.setProperty(props[i], props[i + 1]);
178: } else {
179: p.remove(props[i]);
180: }
181: }
182: return p;
183: }
184:
185: }
|