01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.openjpa.persistence.jdbc.schema;
20:
21: import java.util.*;
22: import org.apache.openjpa.conf.OpenJPAConfiguration;
23: import org.apache.openjpa.jdbc.kernel.ClassTableJDBCSeq;
24: import org.apache.openjpa.jdbc.kernel.TableJDBCSeq;
25: import org.apache.openjpa.jdbc.schema.Sequence;
26: import org.apache.openjpa.kernel.Seq;
27: import org.apache.openjpa.meta.ClassMetaData;
28: import org.apache.openjpa.meta.SequenceMetaData;
29:
30: import org.apache.openjpa.persistence.jdbc.common.apps.*;
31:
32: import java.lang.annotation.Annotation;
33: import junit.framework.*;
34: import javax.persistence.EntityManager;
35: import javax.persistence.EntityManagerFactory;
36: import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
37: import org.apache.openjpa.persistence.OpenJPAEntityManager;
38:
39: public class TestSequenceGeneratorEnsureCapacityCall
40: extends
41: org.apache.openjpa.persistence.jdbc.kernel.TestSQLListenerTestCase {
42:
43: /** Creates a new instance of TestSequenceGeneratorEnsureCapacityCall */
44: public TestSequenceGeneratorEnsureCapacityCall(String name) {
45: super (name);
46: }
47:
48: // class SeqA uses the default sequence factory.
49: public void testDBSequenceFactory() {
50: TableJDBCSeq seq = (TableJDBCSeq) seqForClass(SeqA.class);
51: incrementTest(SeqA.class, seq.getAllocate());
52: }
53:
54: // class SeqD uses the db-class sequence factory.
55: public void testClassDBSequenceFactory() {
56: ClassTableJDBCSeq seq = (ClassTableJDBCSeq) seqForClass(SeqD.class);
57: incrementTest(SeqD.class, seq.getAllocate());
58: }
59:
60: private void incrementTest(Class cls, int generatorIncrement) {
61: // guaranteed to be more than a single batch, so ensureCapacity should
62: // do its thing.
63: int amountToIncrement = generatorIncrement * 2;
64:
65: OpenJPAEntityManager pm = (OpenJPAEntityManager) currentEntityManager();
66: //FIXME jthomas
67: //Sequence s = pm.getIdentitySequence(cls);
68: Sequence s = null;
69: s.setAllocate(amountToIncrement);
70:
71: sql.clear();
72: for (int i = 0; i < amountToIncrement; i++) {
73: s.getIncrement();
74: assertEquals("SQL list should have been empty on sequence"
75: + " fetch #" + i + " of #" + amountToIncrement
76: + ", but contained: " + sql, 0, sql.size());
77: }
78: }
79:
80: private Seq seqForClass(Class cls) {
81: OpenJPAConfiguration conf = getConfiguration();
82: ClassMetaData meta = conf.getMetaDataRepositoryInstance()
83: .getMetaData(cls, null, true);
84: SequenceMetaData smeta = meta.getIdentitySequenceMetaData();
85: return (smeta == null) ? conf.getSequenceInstance() : smeta
86: .getInstance(null);
87: }
88: }
|