001: /*
002: * TestClassSequenceFactory.java
003: *
004: * Created on October 6, 2006, 12:21 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.schema;
029:
030: import java.sql.Connection;
031: import java.sql.SQLException;
032: import java.sql.Statement;
033: import java.util.*;
034: import javax.sql.DataSource;
035: import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
036: import org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl;
037: import org.apache.openjpa.jdbc.kernel.ClassTableJDBCSeq;
038: import org.apache.openjpa.jdbc.meta.ClassMapping;
039: import org.apache.openjpa.kernel.Broker;
040: import org.apache.openjpa.kernel.Seq;
041: import org.apache.openjpa.lib.conf.ConfigurationProvider;
042: import org.apache.openjpa.lib.conf.Configurations;
043:
044: import org.apache.openjpa.persistence.jdbc.common.apps.*;
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.jdbc.kernel.BaseJDBCTest;
053:
054: public class TestClassSequenceFactory extends BaseJDBCTest {
055:
056: private static Map _sysprops = new HashMap();
057:
058: Broker _broker;
059:
060: /** Creates a new instance of TestClassSequenceFactory */
061: public TestClassSequenceFactory() {
062: }
063:
064: public TestClassSequenceFactory(String test) {
065: super (test);
066: }
067:
068: public void setUp() throws Exception {
069: JDBCConfiguration conf = new JDBCConfigurationImpl();
070: conf.fromProperties(getProperties());
071: if (!adjustConfiguration(conf))
072: return;
073:
074: String driver = conf.getConnectionDriverName().toLowerCase();
075: String[] sql = null;
076:
077: if (driver.indexOf("oracle") >= 0) {
078: sql = new String[] { "create sequence seqa_seq",
079: "create sequence seqb_seq" };
080: }
081:
082: if (sql == null)
083: return;
084:
085: DataSource ds = conf.getDataSource2(null);
086: Connection c = ds.getConnection();
087: Statement s = null;
088: try {
089: s = c.createStatement();
090: for (int i = 0; i < sql.length; i++) {
091: try {
092: s.execute(sql[i]);
093: } catch (SQLException sqe) {
094: }
095: }
096: } finally {
097: if (s != null)
098: try {
099: s.close();
100: } catch (Exception e) {
101: }
102: }
103:
104: _broker = getBrokerFactory().newBroker();
105: }
106:
107: /**
108: * Tests that all sequence numbers are unique and in order.
109: */
110: public void testSequence() throws Exception {
111: Set set = new HashSet();
112:
113: JDBCConfiguration conf = new JDBCConfigurationImpl();
114: conf.fromProperties(getProperties());
115: if (!adjustConfiguration(conf))
116: return;
117: Thread t1 = new UpdateThread(set, conf);
118: Thread t2 = new UpdateThread(set, conf);
119:
120: t1.start();
121: t2.start();
122: t1.join();
123: t2.join();
124:
125: assertEquals(102, set.size());
126: }
127:
128: public void testExtensions() throws Exception {
129: JDBCConfiguration conf = new JDBCConfigurationImpl();
130: conf.fromProperties(getProperties());
131: if (!adjustConfiguration(conf))
132: return;
133:
134: ClassMapping aMapping = conf.getMappingRepositoryInstance()
135: .getMapping(SeqA.class, null, true);
136: ClassMapping bMapping = conf.getMappingRepositoryInstance()
137: .getMapping(SeqB.class, null, true);
138: ClassMapping cMapping = conf.getMappingRepositoryInstance()
139: .getMapping(SeqC.class, null, true);
140: DataSource ds = conf.getDataSource2(null);
141:
142: // hold a and c and start b
143:
144: Seq seq = conf.getSequenceInstance();
145: long aid = ((Long) seq.next(_broker, aMapping)).longValue();
146: for (int i = 0; i < 5; i++)
147: seq.next(_broker, bMapping);
148:
149: assertEquals(new Long(aid + 1), seq.next(_broker, aMapping));
150: assertEquals(new Long(aid + 2), seq.next(_broker, cMapping));
151: }
152:
153: /**
154: * Pass in a mutable configuration
155: * <p/>
156: * return true if useable.
157: */
158: private boolean adjustConfiguration(JDBCConfiguration conf) {
159: String driver = conf.getConnectionDriverName();
160: if (driver == null)
161: return false;
162: driver = driver.toLowerCase();
163: if (driver.indexOf("oracle") >= 0) {
164: conf.setSequence(ClassTableJDBCSeq.class.getName());
165: return true;
166: }
167:
168: return false;
169: }
170:
171: public static void main(String[] args) {
172: // main();
173: }
174:
175: private class UpdateThread extends Thread {
176:
177: private JDBCConfiguration _conf;
178: private Set _set = null;
179:
180: public UpdateThread(Set set, JDBCConfiguration conf) {
181: _set = set;
182: _conf = conf;
183: }
184:
185: public void run() {
186: DataSource ds = _conf.getDataSource2(null);
187: try {
188: Seq seq = _conf.getSequenceInstance();
189: ClassMapping mapping = _conf
190: .getMappingRepositoryInstance().getMapping(
191: SeqA.class, null, true);
192: for (int i = 0; i < 51; i++)
193: _set.add(seq.next(_broker, mapping));
194: } catch (Exception e) {
195: throw new RuntimeException(e.getMessage());
196: }
197: }
198: }
199:
200: }
|