001: /*
002: * TestDBSequenceFactory.java
003: *
004: * Created on October 6, 2006, 1:24 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.util.*;
031: import javax.sql.DataSource;
032: import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
033: import org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl;
034: import org.apache.openjpa.jdbc.kernel.TableJDBCSeq;
035: import org.apache.openjpa.kernel.Broker;
036:
037: import org.apache.openjpa.persistence.jdbc.common.apps.*;
038:
039: import java.lang.annotation.Annotation;
040: import junit.framework.*;
041: import javax.persistence.EntityManager;
042: import javax.persistence.EntityManagerFactory;
043: import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
044: import org.apache.openjpa.persistence.OpenJPAEntityManager;
045:
046: public class TestDBSequenceFactory extends
047: org.apache.openjpa.persistence.jdbc.kernel.BaseJDBCTest {
048:
049: private Broker _broker;
050:
051: /** Creates a new instance of TestDBSequenceFactory */
052: public TestDBSequenceFactory() {
053: }
054:
055: public TestDBSequenceFactory(String test) {
056: super (test);
057: }
058:
059: /**
060: * Tests that all sequence numbers are unique and in order.
061: * Will fail for dbs without pessimistic locking.
062: */
063: public void testSequence() throws Exception {
064: _broker = getBrokerFactory().newBroker();
065:
066: Set set = Collections.synchronizedSet(new HashSet());
067: Thread t1 = new UpdateThread(set);
068: Thread t2 = new UpdateThread(set);
069:
070: t1.start();
071: t2.start();
072: t1.join(5 * 60 * 1000);
073: t2.join(5 * 60 * 1000);
074:
075: assertFalse(t1.isAlive());
076: assertFalse(t2.isAlive());
077:
078: assertEquals(102, set.size());
079: }
080:
081: public static void main(String[] args) {
082: //main();
083: }
084:
085: private class UpdateThread extends Thread {
086:
087: private Set _set = null;
088:
089: public UpdateThread(Set set) {
090: _set = set;
091: }
092:
093: public void run() {
094: try {
095: JDBCConfiguration conf = new JDBCConfigurationImpl();
096: TableJDBCSeq seq = new TableJDBCSeq();
097: seq.setConfiguration(conf);
098: seq.startConfiguration();
099: seq.endConfiguration();
100: DataSource ds = conf.getDataSource2(null);
101: for (int i = 0; i < 51; i++)
102: _set.add(seq.next(_broker,
103: conf.getMetaDataRepositoryInstance()
104: .getMetaData(RuntimeTest1.class,
105: null, true)));
106: } catch (Exception e) {
107: throw new RuntimeException(e.getMessage());
108: }
109: }
110: }
111:
112: }
|