001: /*
002: * TestMultiThreaded.java
003: *
004: * Created on October 12, 2006, 2:21 PM
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.HashMap;
030: import java.util.Iterator;
031: import java.util.Map;
032:
033: import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest1;
034: import org.apache.openjpa.persistence.OpenJPAEntityManager;
035: import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
036: import org.apache.openjpa.persistence.OpenJPAQuery;
037:
038: public class TestMultiThreaded extends BaseKernelTest {
039:
040: static int serial = 5;
041: static int threads = 5;
042: static int iterations = 5;
043:
044: private OpenJPAEntityManager pm;
045: private Object id;
046: private String name;
047:
048: /**
049: * Creates a new instance of TestMultiThreaded
050: */
051: public TestMultiThreaded() {
052: }
053:
054: public TestMultiThreaded(String name) {
055: super (name);
056: }
057:
058: public void setUp() {
059: deleteAll(RuntimeTest1.class);
060:
061: OpenJPAEntityManager pm2 = getPM();
062: startTx(pm2);
063: name = "testMultiThreaded" + Math.random();
064: RuntimeTest1 a = new RuntimeTest1(name,
065: (int) (Math.random() * Integer.MAX_VALUE));
066: pm2.persist(a);
067: id = pm2.getObjectId(a);
068: endTx(pm2);
069:
070: Map props = new HashMap();
071: props.put("openjpa.Multithreaded", "true");
072: OpenJPAEntityManagerFactory pmf = (OpenJPAEntityManagerFactory) getEmf(props);
073: pm = pmf.createEntityManager();
074: startTx(pm);
075: }
076:
077: public void tearDown() throws Exception {
078: try {
079: rollbackTx(pm);
080: endEm(pm);
081: } catch (Exception e) {
082: // this is not what we are testing
083: }
084: super .tearDown();
085: }
086:
087: public void testgetTransaction() {
088: mttest(serial, threads, iterations);
089: pm.getTransaction();
090: }
091:
092: public void testGetObjectById() {
093: mttest(serial, threads, iterations);
094: assertNotNull(pm.find(RuntimeTest1.class, id));
095: }
096:
097: public void testQueryExecution() {
098: mttest(serial, threads, iterations);
099: OpenJPAQuery q = pm.createQuery("select o from RuntimeTest1 o "
100: + "where o.stringField = '" + name + "'");
101: assertEquals(1, q.getResultList().size());
102: }
103:
104: public void testDeletePersistent() {
105: mttest(serial, threads, iterations);
106: pm.removeAll(pm.createQuery(
107: "select o from RuntimeTest1 o "
108: + "where o.stringField = '" + name + "'")
109: .getResultList());
110: }
111:
112: public void testRefreshAll() {
113: mttest(serial, threads, iterations);
114: pm.refreshAll();
115: }
116:
117: public void testEvictAll() {
118: mttest(serial, threads, iterations);
119: pm.evictAll();
120: }
121:
122: public void testIterateExtent() throws Throwable {
123: if (timeout(120 * 1000))
124: return;
125:
126: mttest(serial, threads, iterations);
127:
128: assertTrue("Transaction should have been active", pm
129: .getTransaction().isActive());
130:
131: for (int i = 0; i < 3; i++) {
132: pm.persist(new RuntimeTest1("testIterateExtent"
133: + Math.random(),
134: (int) (Math.random() * Integer.MAX_VALUE)));
135: }
136:
137: assertTrue("Transaction should have been active", pm
138: .getTransaction().isActive());
139:
140: for (Iterator i = pm.createExtent(RuntimeTest1.class, true)
141: .iterator(); i.hasNext(); i.next())
142: ;
143:
144: assertTrue("Transaction should have been active", pm
145: .getTransaction().isActive());
146: }
147: }
|