001: /*
002: * TestOpenJPAEntityManagerFactoryImpl.java
003: *
004: * Created on October 13, 2006, 10:54 AM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009: /*
010: * Licensed to the Apache Software Foundation (ASF) under one
011: * or more contributor license agreements. See the NOTICE file
012: * distributed with this work for additional information
013: * regarding copyright ownership. The ASF licenses this file
014: * to you under the Apache License, Version 2.0 (the
015: * "License"); you may not use this file except in compliance
016: * with the License. You may obtain a copy of the License at
017: *
018: * http://www.apache.org/licenses/LICENSE-2.0
019: *
020: * Unless required by applicable law or agreed to in writing,
021: * software distributed under the License is distributed on an
022: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
023: * KIND, either express or implied. See the License for the
024: * specific language governing permissions and limitations
025: * under the License.
026: */
027: package org.apache.openjpa.persistence.kernel;
028:
029: import java.util.Map;
030:
031: import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest1;
032: import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest4;
033: import org.apache.openjpa.conf.OpenJPAConfiguration;
034: import org.apache.openjpa.conf.OpenJPAConfigurationImpl;
035: import org.apache.openjpa.event.LifecycleEvent;
036: import org.apache.openjpa.event.LoadListener;
037: import org.apache.openjpa.kernel.AbstractBrokerFactory;
038: import org.apache.openjpa.kernel.BrokerImpl;
039: import org.apache.openjpa.kernel.StoreManager;
040: import org.apache.openjpa.lib.conf.ConfigurationProvider;
041: import org.apache.openjpa.meta.MetaDataRepository;
042: import org.apache.openjpa.persistence.JPAFacadeHelper;
043: import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
044: import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
045: import org.apache.openjpa.persistence.OpenJPAPersistence;
046:
047: public class TestPersistenceManagerFactoryImpl extends BaseKernelTest {
048:
049: private OpenJPAConfiguration _conf = null;
050:
051: /**
052: * Creates a new instance of TestOpenJPAEntityManagerFactoryImpl
053: */
054: public TestPersistenceManagerFactoryImpl() {
055: }
056:
057: public TestPersistenceManagerFactoryImpl(String test) {
058: super (test);
059: }
060:
061: public void setUp() {
062: _conf = new OpenJPAConfigurationImpl();
063: _conf.setConnection2UserName("user");
064: _conf.setConnection2Password("pass");
065: _conf.setConnection2URL("url");
066: }
067:
068: /**
069: * Test that configuration is frozen after retrieving a factory.
070: *
071: * This test case is for kodo persistencemanagerfactories and not openjpaentitymanagers.
072: * therefore its been commented out.
073: * FIX ME: aokeke
074: */
075: /* public void testConfigurationFreeze()
076: {
077: OpenJPAEntityManagerFactory pmf = getOpenJPAEntityManagerFactory(_conf.toProperties(false));
078: assertEquals("user", pmf.getConfiguration().getConnection2UserName());
079: assertEquals("url", pmf.getConfiguration().getConnection2URL());
080: try
081: {
082: pmf.getConfiguration().setConnection2URL("url2");
083: fail("Allowed configuration change.");
084: }
085: catch (Exception e)
086: {
087: }
088: }*/
089:
090: /**
091: * Test that persistence manager factories are being pooled.
092: *
093: * This test case is for kodo persistencemanagerfactories. It doesnt apply to
094: * openjpaentitymanagerfactories therefore it will be commented out.
095: * FIX ME: aokeke
096: */
097: /*public void testFactoryPooling() {
098: Properties props = new Properties();
099: props.putAll(_conf.toProperties(false));
100: OpenJPAEntityManagerFactory pmf1 = getOpenJPAEntityManagerFactory(props);
101:
102: props = new Properties();
103: props.putAll(_conf.toProperties(false));
104: OpenJPAEntityManagerFactory pmf2 = getOpenJPAEntityManagerFactory(props);
105:
106: props = new Properties();
107: props.putAll(_conf.toProperties(false));
108: OpenJPAEntityManagerFactory pmf3 = getOpenJPAEntityManagerFactory(props);
109:
110: _conf.setConnectionURL("url2");
111: props = new Properties();
112: props.putAll(_conf.toProperties(false));
113: OpenJPAEntityManagerFactory pmf4 = getOpenJPAEntityManagerFactory
114: (_conf.toProperties(false));
115:
116: props = new Properties();
117: props.putAll(_conf.toProperties(false));
118: OpenJPAEntityManagerFactory pmf5 = getOpenJPAEntityManagerFactory(_conf.toProperties(false));
119:
120: assertTrue(JPAFacadeHelper.toBrokerFactory(pmf1) == JPAFacadeHelper.toBrokerFactory(pmf2));
121: assertTrue(JPAFacadeHelper.toBrokerFactory(pmf1) == JPAFacadeHelper.toBrokerFactory(pmf3));
122: assertTrue(JPAFacadeHelper.toBrokerFactory(pmf1) != JPAFacadeHelper.toBrokerFactory(pmf4));
123: assertTrue(JPAFacadeHelper.toBrokerFactory(pmf4) == JPAFacadeHelper.toBrokerFactory(pmf5));
124: }*/
125:
126: /**
127: * Tests that lifecycle listeners are transferred from factory to
128: * persistence managers.
129: */
130: public void testFactoryLifecycleListeners() {
131: OpenJPAEntityManagerFactory pmf = (OpenJPAEntityManagerFactory) getEmf();
132: OpenJPAEntityManagerFactorySPI pmfSPI = ((OpenJPAEntityManagerFactorySPI) OpenJPAPersistence
133: .cast(pmf));
134:
135: //FIXME jthomas
136: LoadListener listener = new LoadListener() {
137: public void afterLoad(LifecycleEvent ev) {
138: }
139:
140: public void afterRefresh(LifecycleEvent ev) {
141: }
142: };
143:
144: pmfSPI.addLifecycleListener(listener,
145: new Class[] { RuntimeTest4.class });
146:
147: try {
148: BrokerImpl broker = (BrokerImpl) JPAFacadeHelper
149: .toBroker(pmf.createEntityManager());
150: MetaDataRepository repos = broker.getConfiguration()
151: .getMetaDataRepositoryInstance();
152: assertTrue("no listeners defined added to Runtimetest4",
153: broker.getLifecycleEventManager().hasLoadListeners(
154: new RuntimeTest4("foo"),
155: repos.getMetaData(RuntimeTest4.class, null,
156: true)));
157: assertFalse(
158: "there should be listeners def for runtimetest1",
159: broker.getLifecycleEventManager().hasLoadListeners(
160: new RuntimeTest1(),
161: repos.getMetaData(RuntimeTest1.class, null,
162: true)));
163: broker.close();
164: } finally {
165: pmfSPI.removeLifecycleListener(listener);
166: }
167: }
168:
169: /**
170: * Tests that pooling is maintained on deserialization.
171: * This test case is for kodo persistencemanagerfactories. It doesnt apply to
172: * openjpaentitymanagerfactories therefore it will be commented out.
173: */
174: /*
175: public void testFactorySerialization()
176: throws Exception {
177: OpenJPAEntityManagerFactory pmf1 = getOpenJPAEntityManagerFactory(_conf.toProperties(false));
178: Object pmf2 = roundtrip(pmf1, true);
179: assertEquals(pmf1, pmf2);
180: assertTrue(JPAFacadeHelper.toBrokerFactory(pmf1) ==
181: JPAFacadeHelper.toBrokerFactory((OpenJPAEntityManagerFactory) pmf2));
182: }
183: */
184:
185: /**
186: * Tests that the <code>Platform</code> property is set by the
187: * concrete PMF implementation.
188: */
189: public void testPlatform() {
190: OpenJPAEntityManagerFactory pmf = (OpenJPAEntityManagerFactory) getEmf();
191: assertNotNull(pmf.getProperties().getProperty("Platform"));
192: }
193:
194: protected OpenJPAEntityManagerFactory getEmf(Map props) {
195: props.put("openjpa.BrokerFactory", BrokerFactoryTest.class
196: .getName());
197: return (OpenJPAEntityManagerFactory) super .getEmf(props);
198: }
199:
200: public static class BrokerFactoryTest extends AbstractBrokerFactory {
201:
202: // standard brokerfactory getter implemented by subclasses
203: public static synchronized BrokerFactoryTest getInstance(
204: ConfigurationProvider cp) {
205: Object key = toPoolKey(cp.getProperties());
206: BrokerFactoryTest factory = (BrokerFactoryTest) getPooledFactoryForKey(key);
207: if (factory != null)
208: return factory;
209:
210: factory = newInstance(cp);
211: pool(key, factory);
212: return factory;
213: }
214:
215: // standard brokerfactory getter implemented by subclasses
216: public static BrokerFactoryTest newInstance(
217: ConfigurationProvider cp) {
218: OpenJPAConfigurationImpl conf = new OpenJPAConfigurationImpl();
219: cp.setInto(conf);
220: return new BrokerFactoryTest(conf);
221: }
222:
223: protected BrokerFactoryTest(OpenJPAConfiguration conf) {
224: super (conf);
225: }
226:
227: protected StoreManager newStoreManager() {
228: return null;
229: }
230: }
231: }
|