You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by pc...@apache.org on 2008/02/15 10:20:40 UTC
svn commit: r627979 [23/39] - in /openjpa/trunk:
openjpa-lib/src/test/java/org/apache/openjpa/lib/test/
openjpa-persistence-jdbc/ openjpa-persistence-jdbc/src/test/java/
openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/
openjpa-per...
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestDBSequenceFactory.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestDBSequenceFactory.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestDBSequenceFactory.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestDBSequenceFactory.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,114 @@
+/*
+ * TestDBSequenceFactory.java
+ *
+ * Created on October 6, 2006, 1:24 PM
+ *
+ * To change this template, choose Tools | Template Manager
+ * and open the template in the editor.
+ */
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.jdbc.schema;
+
+import java.util.*;
+import javax.sql.DataSource;
+import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
+import org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl;
+import org.apache.openjpa.jdbc.kernel.TableJDBCSeq;
+import org.apache.openjpa.kernel.Broker;
+
+import org.apache.openjpa.persistence.jdbc.common.apps.*;
+
+
+import java.lang.annotation.Annotation;
+import junit.framework.*;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+
+
+public class TestDBSequenceFactory extends org.apache.openjpa.persistence.jdbc.kernel.BaseJDBCTest{
+
+ private Broker _broker;
+
+ /** Creates a new instance of TestDBSequenceFactory */
+ public TestDBSequenceFactory() {
+ }
+
+ public TestDBSequenceFactory(String test) {
+ super(test);
+ }
+
+ /**
+ * Tests that all sequence numbers are unique and in order.
+ * Will fail for dbs without pessimistic locking.
+ */
+ public void testSequence()
+ throws Exception {
+ _broker = getBrokerFactory().newBroker();
+
+ Set set = Collections.synchronizedSet(new HashSet());
+ Thread t1 = new UpdateThread(set);
+ Thread t2 = new UpdateThread(set);
+
+ t1.start();
+ t2.start();
+ t1.join(5 * 60 * 1000);
+ t2.join(5 * 60 * 1000);
+
+ assertFalse(t1.isAlive());
+ assertFalse(t2.isAlive());
+
+ assertEquals(102, set.size());
+ }
+
+ public static void main(String[] args) {
+ //main();
+ }
+
+ private class UpdateThread
+ extends Thread {
+
+ private Set _set = null;
+
+ public UpdateThread(Set set) {
+ _set = set;
+ }
+
+ public void run() {
+ try {
+ JDBCConfiguration conf = new JDBCConfigurationImpl();
+ TableJDBCSeq seq = new TableJDBCSeq();
+ seq.setConfiguration(conf);
+ seq.startConfiguration();
+ seq.endConfiguration();
+ DataSource ds = conf.getDataSource2(null);
+ for (int i = 0; i < 51; i++)
+ _set.add(seq.next(_broker,
+ conf.getMetaDataRepositoryInstance().
+ getMetaData(RuntimeTest1.class, null, true)));
+ } catch (Exception e) {
+ throw new RuntimeException(e.getMessage());
+ }
+ }
+ }
+
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestDynamicSchemaFactory.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestDynamicSchemaFactory.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestDynamicSchemaFactory.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestDynamicSchemaFactory.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,98 @@
+/*
+ * TestDynamicSchemaFactory.java
+ *
+ * Created on October 6, 2006, 1:34 PM
+ *
+ * To change this template, choose Tools | Template Manager
+ * and open the template in the editor.
+ */
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.jdbc.schema;
+
+import java.sql.Types;
+import java.util.*;
+import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
+import org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl;
+import org.apache.openjpa.jdbc.meta.ClassMapping;
+import org.apache.openjpa.jdbc.meta.Discriminator;
+import org.apache.openjpa.jdbc.meta.MappingRepository;
+import org.apache.openjpa.jdbc.meta.Version;
+import org.apache.openjpa.jdbc.schema.DynamicSchemaFactory;
+import org.apache.openjpa.jdbc.schema.Schemas;
+import org.apache.openjpa.jdbc.schema.Table;
+
+import org.apache.openjpa.persistence.jdbc.common.apps.*;
+
+
+import java.lang.annotation.Annotation;
+import junit.framework.*;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+
+
+public class TestDynamicSchemaFactory extends org.apache.openjpa.persistence.jdbc.kernel.BaseJDBCTest{
+
+ private ClassMapping _mapping = null;
+
+ /** Creates a new instance of TestDynamicSchemaFactory */
+ public TestDynamicSchemaFactory() {
+ }
+ public TestDynamicSchemaFactory(String test) {
+ super(test);
+ }
+
+ public void setUp()
+ throws Exception {
+ JDBCConfiguration conf = new JDBCConfigurationImpl();
+ conf.setSchemaFactory(DynamicSchemaFactory.class.getName());
+ MappingRepository repos = conf.newMappingRepositoryInstance();
+ _mapping = repos.getMapping(MappingTest1.class, null, true);
+ }
+
+ public void testClassMapping() {
+ Table table = _mapping.getTable();
+ assertEquals("MAPPINGTEST1", table.getName().toUpperCase());
+ assertEquals(1, table.getPrimaryKey().getColumns().length);
+ int type = table.getPrimaryKey().getColumns()[0].getType();
+ assertEquals(Schemas.getJDBCName(type), Types.INTEGER, type);
+ }
+
+ public void testIndicators() {
+ Version vers = _mapping.getVersion();
+ assertNotNull(vers);
+ assertEquals("MAPPINGTEST1", vers.getColumns()[0].getTable().
+ getName().toUpperCase());
+ assertEquals(Types.INTEGER, vers.getColumns()[0].getType());
+
+ Discriminator cls = _mapping.getDiscriminator();
+ assertNotNull(cls);
+ assertEquals("MAPPINGTEST1", cls.getColumns()[0].getTable().
+ getName().toUpperCase());
+ assertEquals(Types.VARCHAR, cls.getColumns()[0].getType());
+ }
+
+ public static void main(String[] args) {
+ // main(TestDynamicSchemaFactory.class);
+ }
+
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestJDBCListener.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestJDBCListener.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestJDBCListener.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestJDBCListener.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,83 @@
+/*
+ * TestJDBCListener.java
+ *
+ * Created on October 6, 2006, 1:38 PM
+ *
+ * To change this template, choose Tools | Template Manager
+ * and open the template in the editor.
+ */
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.jdbc.schema;
+
+import java.util.*;
+import org.apache.openjpa.lib.jdbc.AbstractJDBCListener;
+import org.apache.openjpa.lib.jdbc.JDBCEvent;
+
+import org.apache.openjpa.persistence.jdbc.common.apps.*;
+
+
+import java.lang.annotation.Annotation;
+import junit.framework.*;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+
+
+public class TestJDBCListener extends org.apache.openjpa.persistence.jdbc.kernel.BaseJDBCTest{
+
+ public static boolean commitOccurred;
+
+ /** Creates a new instance of TestJDBCListener */
+ public TestJDBCListener() {
+ }
+
+ public TestJDBCListener(String name) {
+ super(name);
+ }
+
+ public void testJDBCListener() {
+ Map props=new HashMap();
+ props.put("openjpa.jdbc.JDBCListeners", Listener.class.getName());
+
+ OpenJPAEntityManagerFactory factory =(OpenJPAEntityManagerFactory)
+ getEmf(props);
+
+ commitOccurred = false;
+ OpenJPAEntityManager pm = factory.createEntityManager();
+
+ pm.getTransaction().begin();
+ assertFalse(commitOccurred);
+ pm.persist(new RuntimeTest1("Listener test", 99));
+ pm.getTransaction().commit();
+ assertTrue("Commit event should have occurred, but did not",
+ commitOccurred);
+ pm.close();
+ }
+
+ public static class Listener
+ extends AbstractJDBCListener {
+
+ public void beforeCommit(JDBCEvent event) {
+ commitOccurred = true;
+ }
+ }
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestLazySchemaFactory.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestLazySchemaFactory.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestLazySchemaFactory.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestLazySchemaFactory.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,117 @@
+/*
+ * TestLazySchemaFactory.java
+ *
+ * Created on October 6, 2006, 1:47 PM
+ *
+ * To change this template, choose Tools | Template Manager
+ * and open the template in the editor.
+ */
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.jdbc.schema;
+
+import java.util.*;
+import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
+import org.apache.openjpa.jdbc.schema.LazySchemaFactory;
+import org.apache.openjpa.jdbc.schema.SchemaGroup;
+import org.apache.openjpa.jdbc.schema.Table;
+
+import org.apache.openjpa.persistence.jdbc.common.apps.*;
+import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
+
+import java.lang.annotation.Annotation;
+import junit.framework.*;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+
+
+public class TestLazySchemaFactory extends org.apache.openjpa.persistence.jdbc.kernel.BaseJDBCTest{
+
+ private SchemaGroup _group = null;
+ private boolean _fks = true;
+
+ /** Creates a new instance of TestLazySchemaFactory */
+ public TestLazySchemaFactory() {
+ }
+
+ public TestLazySchemaFactory(String test) {
+ super(test);
+ }
+
+ public void setUp()
+ throws Exception {
+ // see if the dict supports foreign keys; mysql can support them, but
+ // can't detect keys added through alter table commands, which is what
+ // kodo uses
+ JDBCConfiguration conf = (JDBCConfiguration) getConfiguration();
+ _fks = conf.getDBDictionaryInstance().supportsForeignKeys
+ && getCurrentPlatform() != AbstractTestCase.Platform.MYSQL;
+
+ LazySchemaFactory factory = new LazySchemaFactory();
+ factory.setConfiguration(conf);
+ factory.setPrimaryKeys(true);
+ factory.setIndexes(true);
+ factory.setForeignKeys(true);
+ _group = factory;
+ }
+
+ public void testLazySchemaFactory() {
+ // should read tables 1 and 2...
+ Table table = _group.findTable("T1");
+ assertNotNull(table);
+ Table table2 = _group.findTable("t1");
+ assertTrue(table == table2);
+
+ int expectedColumns = 2;
+
+ // Sybase has an extra "UNQ_INDEX" column.
+ if (getCurrentPlatform() == AbstractTestCase.Platform.SYBASE)
+ expectedColumns++;
+
+ assertEquals(expectedColumns, table.getColumns().length);
+ if (_fks)
+ assertEquals(1, table.getForeignKeys().length);
+
+ table2 = _group.findTable("T2");
+ assertNotNull(table2);
+ if (_fks)
+ assertTrue(table.getForeignKeys()[0].getPrimaryKeyColumns()[0].
+ getTable() == table2);
+
+ assertNull(table.getSchema().getTable("T3"));
+
+ // should read table 3 only...
+ Table table3 = _group.findTable("T3");
+ assertNotNull(table3);
+ assertTrue(table.getSchema().getTable("T3") == table3);
+ if (_fks)
+ assertTrue(table3.getForeignKeys()[0].getPrimaryKeyColumns()[0].
+ getTable() == table);
+
+ assertNull(table3.getSchema().getTable("T4"));
+ }
+
+ public static void main(String[] args) {
+ //main(TestLazySchemaFactory.class);
+ }
+
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestPerClassSequenceFactory.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestPerClassSequenceFactory.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestPerClassSequenceFactory.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestPerClassSequenceFactory.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,118 @@
+/*
+ * TestPerClassSequenceFactory.java
+ *
+ * Created on October 6, 2006, 2:25 PM
+ *
+ * To change this template, choose Tools | Template Manager
+ * and open the template in the editor.
+ */
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.jdbc.schema;
+
+import java.util.*;
+import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
+import org.apache.openjpa.jdbc.meta.ClassMapping;
+import org.apache.openjpa.jdbc.schema.Sequence;
+import org.apache.openjpa.kernel.Seq;
+import org.apache.openjpa.kernel.StoreContext;
+import org.apache.openjpa.meta.ClassMetaData;
+
+import org.apache.openjpa.persistence.jdbc.common.apps.*;
+
+
+import java.lang.annotation.Annotation;
+import junit.framework.*;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+
+
+public class TestPerClassSequenceFactory extends org.apache.openjpa.persistence.jdbc.kernel.BaseJDBCTest{
+
+
+ /** Creates a new instance of TestPerClassSequenceFactory */
+ public TestPerClassSequenceFactory() {
+ }
+ public TestPerClassSequenceFactory(String test) {
+ super(test);
+ }
+
+ public void testPerClassSequenceFactory()
+ throws Exception {
+ Map props=new HashMap();
+ props.put("openjpa.Sequence", "table");
+
+ OpenJPAEntityManagerFactory factory =(OpenJPAEntityManagerFactory)
+ getEmf(props);
+
+ JDBCConfiguration conf = (JDBCConfiguration)
+ factory.getConfiguration();
+ ClassMapping mapping1 = conf.getMappingRepositoryInstance().
+ getMapping(RuntimeTest1.class, null, true);
+ ClassMapping mapping2 = conf.getMappingRepositoryInstance().
+ getMapping(PerClassTestObject.class, null, true);
+ ClassMapping mapping3 = conf.getMappingRepositoryInstance().
+ getMapping(PerClassTestObject2.class, null, true);
+
+ OpenJPAEntityManager pm =(OpenJPAEntityManager)currentEntityManager();
+ //FIXME jthomas
+ /*
+ Sequence s1 = pm.getIdentitySequence(mapping1.getDescribedType());
+ Sequence s2 = pm.getIdentitySequence(mapping2.getDescribedType());
+ Sequence s3 = pm.getFieldSequence(mapping3.getDescribedType(), "age");
+ assertTrue(((SequenceImpl) s1).getDelegate()
+ instanceof TableJDBCSeq);
+ assertTrue(((SequenceImpl) s2).getDelegate().toString(),
+ ((SequenceImpl) s2).getDelegate()
+ instanceof DummySequenceFactory);
+ assertTrue(((SequenceImpl) s2).getDelegate().toString(),
+ ((SequenceImpl) s3).getDelegate()
+ instanceof DummySequenceFactory);
+ */
+ }
+
+ public static class DummySequenceFactory
+ implements Seq {
+
+ private long _val = 1;
+
+ public void setType(int type) {
+ }
+
+ public Object next(StoreContext ctx, ClassMetaData meta) {
+ _val++;
+ return current(ctx, meta);
+ }
+
+ public Object current(StoreContext ctx, ClassMetaData meta) {
+ return new Long(_val);
+ }
+
+ public void allocate(int num, StoreContext ctx, ClassMetaData meta) {
+ }
+
+ public void close() {
+ }
+ }
+
+
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestSchema.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestSchema.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestSchema.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestSchema.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,331 @@
+/*
+ * TestSchema.java
+ *
+ * Created on October 6, 2006, 2:36 PM
+ *
+ * To change this template, choose Tools | Template Manager
+ * and open the template in the editor.
+ */
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.jdbc.schema;
+
+import java.util.*;
+import org.apache.openjpa.jdbc.schema.Column;
+import org.apache.openjpa.jdbc.schema.ForeignKey;
+import org.apache.openjpa.jdbc.schema.Index;
+import org.apache.openjpa.jdbc.schema.PrimaryKey;
+import org.apache.openjpa.jdbc.schema.Schema;
+import org.apache.openjpa.jdbc.schema.SchemaGroup;
+import org.apache.openjpa.jdbc.schema.Table;
+
+import org.apache.openjpa.persistence.jdbc.common.apps.*;
+
+
+import java.lang.annotation.Annotation;
+import junit.framework.*;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+
+
+public class TestSchema extends org.apache.openjpa.persistence.jdbc.kernel.BaseJDBCTest{
+
+ private Schema _schema = new SchemaGroup().addSchema("schema");
+
+ /** Creates a new instance of TestSchema */
+ public TestSchema() {
+ }
+ public TestSchema(String test) {
+ super(test);
+ }
+
+ /**
+ * Test the {@link Schema} class.
+ */
+ public void testSchema() {
+ assertEquals("schema", _schema.getName());
+ assertTrue(_schema.getSchemaGroup().isNameTaken("schema"));
+
+ assertTrue(!_schema.getSchemaGroup().isNameTaken("table1"));
+ Table table1 = _schema.addTable("table1");
+ assertTrue(_schema.getSchemaGroup().isNameTaken("table1"));
+ Table table2 = _schema.addTable("table2");
+ assertTrue(_schema.getSchemaGroup().isNameTaken("table2"));
+
+ Table[] tables = _schema.getTables();
+ assertEquals(2, tables.length);
+ assertEquals(table1, tables[0]);
+ assertEquals(table2, tables[1]);
+ assertEquals(table1, _schema.getTable("table1"));
+ assertEquals(table2, _schema.getTable("table2"));
+ assertNull(_schema.getTable("table3"));
+
+ assertTrue(_schema.removeTable(table1));
+ assertTrue(!_schema.getSchemaGroup().isNameTaken("table1"));
+ assertNull(_schema.getTable("table1"));
+ assertEquals(table2, _schema.getTable("table2"));
+ assertTrue(_schema.removeTable(table2));
+ assertTrue(!_schema.getSchemaGroup().isNameTaken("table2"));
+ assertNull(_schema.getTable("table2"));
+ assertEquals(0, _schema.getTables().length);
+ }
+
+ /**
+ * Test the {@link Table} class.
+ */
+ public void testTable() {
+ Table table = _schema.addTable("table");
+ assertEquals(_schema, table.getSchema());
+ assertEquals("table", table.getName());
+
+ assertTrue(!table.isNameTaken("schema"));
+ assertTrue(table.isNameTaken("table"));
+
+ // pk testing
+ assertNull(table.getPrimaryKey());
+ PrimaryKey pk = table.addPrimaryKey("pk");
+ assertEquals(table, pk.getTable());
+ assertEquals(pk, table.getPrimaryKey());
+ assertTrue(!table.isNameTaken("pk"));
+ assertTrue(_schema.getSchemaGroup().isNameTaken("pk"));
+
+ table.removePrimaryKey();
+ assertNull(table.getPrimaryKey());
+ assertTrue(!_schema.getSchemaGroup().isNameTaken("pk"));
+
+ // column testing
+ Column c2 = table.addColumn("c2");
+ Column c1 = table.addColumn("c1");
+ assertTrue(table.isNameTaken("c1"));
+ assertTrue(!_schema.getSchemaGroup().isNameTaken("c1"));
+ assertEquals(table, c1.getTable());
+ Column[] cols = table.getColumns();
+ assertEquals(2, cols.length);
+ assertEquals(c2, cols[0]);
+ assertEquals(c1, cols[1]);
+
+ assertEquals(c1, table.getColumn("c1"));
+ assertEquals(c2, table.getColumn("c2"));
+
+ assertTrue(table.removeColumn(c1));
+ assertTrue(!table.isNameTaken("c1"));
+ assertNull(table.getColumn("c1"));
+
+ // foreign key testing
+ ForeignKey fk = table.addForeignKey("fk");
+ assertTrue(_schema.getSchemaGroup().isNameTaken("fk"));
+ assertTrue(!table.isNameTaken("fk"));
+ assertEquals(table, fk.getTable());
+ ForeignKey[] fks = table.getForeignKeys();
+ assertEquals(1, fks.length);
+ assertEquals(fk, fks[0]);
+
+ assertTrue(table.removeForeignKey(fk));
+ assertTrue(!_schema.getSchemaGroup().isNameTaken("fk"));
+ assertEquals(0, table.getForeignKeys().length);
+
+ // index testing
+ Index idx = table.addIndex("idx");
+ assertTrue(_schema.getSchemaGroup().isNameTaken("idx"));
+ assertTrue(!table.isNameTaken("idx"));
+ assertEquals(table, idx.getTable());
+ Index[] idxs = table.getIndexes();
+ assertEquals(1, idxs.length);
+ assertEquals(idx, idxs[0]);
+
+ assertEquals(idx, table.getIndex("idx"));
+ assertTrue(table.removeIndex(idx));
+ assertTrue(!table.isNameTaken("idx"));
+ assertNull(table.getIndex("idx"));
+ }
+
+ /**
+ * Test the {@link Index} class.
+ */
+ public void testIndex() {
+ Table table = _schema.addTable("table");
+ Column c1 = table.addColumn("c1");
+ Column c2 = table.addColumn("c2");
+ Table table2 = _schema.addTable("table2");
+ Column c3 = table2.addColumn("c3");
+
+ Index idx = table.addIndex("idx");
+ try {
+ idx.addColumn(c3);
+ fail("Allowed addition of column of another table.");
+ } catch (RuntimeException re) {
+ }
+
+ Column[] cols = idx.getColumns();
+ assertEquals(0, cols.length);
+
+ idx.addColumn(c1);
+ idx.addColumn(c2);
+ cols = idx.getColumns();
+ assertEquals(2, cols.length);
+ assertEquals(c1, cols[0]);
+ assertEquals(c2, cols[1]);
+
+ assertTrue(idx.removeColumn(c1));
+ cols = idx.getColumns();
+ assertEquals(1, cols.length);
+ assertEquals(c2, cols[0]);
+ assertTrue(idx.removeColumn(c2));
+ cols = idx.getColumns();
+ assertEquals(0, cols.length);
+
+ assertTrue(!idx.isUnique());
+ idx.setUnique(true);
+ assertTrue(idx.isUnique());
+ }
+
+ /**
+ * Test the {@link ForeignKey} class.
+ */
+ public void testForeignKey() {
+ Table table = _schema.addTable("table");
+ Column c1 = table.addColumn("c1");
+ Column c2 = table.addColumn("c2");
+ Table table2 = _schema.addTable("table2");
+ Column c3 = table2.addColumn("c3");
+ Column c4 = table2.addColumn("c4");
+
+ ForeignKey fk = table.addForeignKey("fk");
+ try {
+ fk.join(c4, c2);
+ fail("Allowed addition of column of another table.");
+ } catch (RuntimeException re) {
+ }
+
+ Column[] cols = fk.getColumns();
+ Column[] pkCols = fk.getPrimaryKeyColumns();
+ assertEquals(0, cols.length);
+ assertEquals(0, pkCols.length);
+ PrimaryKey pk = table2.addPrimaryKey("pk");
+ pk.addColumn(c3);
+ fk.join(c1, c3);
+ pk.addColumn(c4);
+ fk.join(c2, c4);
+ cols = fk.getColumns();
+ pkCols = fk.getPrimaryKeyColumns();
+ assertEquals(2, cols.length);
+ assertEquals(c1, cols[0]);
+ assertEquals(c2, cols[1]);
+ assertEquals(c3, pkCols[0]);
+ assertEquals(c4, pkCols[1]);
+
+ assertTrue(fk.removeJoin(c1));
+ cols = fk.getColumns();
+ pkCols = fk.getPrimaryKeyColumns();
+ assertEquals(1, cols.length);
+ assertEquals(1, pkCols.length);
+ assertEquals(c2, cols[0]);
+ assertEquals(c4, pkCols[0]);
+ assertTrue(fk.removeJoin(c2));
+ cols = fk.getColumns();
+ pkCols = fk.getPrimaryKeyColumns();
+ assertEquals(0, cols.length);
+ assertEquals(0, pkCols.length);
+
+ assertEquals(ForeignKey.ACTION_NONE, fk.getDeleteAction());
+ assertTrue(fk.isLogical());
+ fk.setDeleteAction(ForeignKey.ACTION_RESTRICT);
+ assertEquals(ForeignKey.ACTION_RESTRICT, fk.getDeleteAction());
+ assertFalse(fk.isLogical());
+ }
+
+ /**
+ * Tests the {@link SchemaGroup} class.
+ */
+ public void testSchemaGroup() {
+ SchemaGroup group = _schema.getSchemaGroup();
+ assertEquals(_schema, group.getSchema("schema"));
+ Table foo1 = _schema.addTable("foo");
+
+ Schema schema2 = group.addSchema("schema2");
+ assertNull(schema2.getTable("foo"));
+ Table foo2 = schema2.addTable("foo");
+ assertEquals(foo2, schema2.getTable("foo"));
+ assertEquals(foo1, _schema.getTable("foo"));
+
+ assertEquals(foo1, group.findTable("schema.foo"));
+ assertEquals(foo2, group.findTable("schema2.foo"));
+ }
+
+ /**
+ * Test primary key removal.
+ */
+ public void testPrimaryKeyRemoval() {
+ Table table = _schema.addTable("table");
+ Column c1 = table.addColumn("c1");
+ Column c2 = table.addColumn("c2");
+ Table table2 = _schema.addTable("table2");
+ Column c3 = table2.addColumn("c3");
+ Column c4 = table2.addColumn("c4");
+ PrimaryKey pk = table2.addPrimaryKey("pk");
+ pk.addColumn(c3);
+ pk.addColumn(c4);
+ ForeignKey fk = table.addForeignKey("fk");
+ fk.join(c1, c3);
+ fk.join(c2, c4);
+
+ table2.removePrimaryKey();
+ assertNull(pk.getTable());
+ assertNull(table2.getPrimaryKey());
+ assertEquals(0, table.getForeignKeys().length);
+ }
+
+ /**
+ * Test column removal.
+ */
+ public void testColumnRemoval() {
+ Table table = _schema.addTable("table");
+ Column c1 = table.addColumn("c1");
+ Column c2 = table.addColumn("c2");
+ PrimaryKey pk = table.addPrimaryKey("pk");
+ pk.addColumn(c1);
+ Index idx1 = table.addIndex("idx1");
+ idx1.addColumn(c1);
+ Index idx2 = table.addIndex("idx2");
+ idx2.addColumn(c1);
+ idx2.addColumn(c2);
+
+ Table table2 = _schema.addTable("table2");
+ Column c3 = table2.addColumn("c3");
+ Column c4 = table2.addColumn("c4");
+ pk = table2.addPrimaryKey("pk2");
+ pk.addColumn(c3);
+ ForeignKey fk = table.addForeignKey("fk");
+ fk.join(c1, c3);
+
+ table.removeColumn(c1);
+ assertNull(table.getPrimaryKey());
+ assertNull(table.getIndex("idx1"));
+ assertEquals(1, idx2.getColumns().length);
+ assertEquals(0, table.getForeignKeys().length);
+ }
+
+ public static void main(String[] args) {
+ //main(TestSchema.class);
+ }
+
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestSchemaClone.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestSchemaClone.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestSchemaClone.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestSchemaClone.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,65 @@
+/*
+ * TestSchemaClone.java
+ *
+ * Created on October 6, 2006, 2:40 PM
+ *
+ * To change this template, choose Tools | Template Manager
+ * and open the template in the editor.
+ */
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.jdbc.schema;
+
+import java.util.*;
+import org.apache.openjpa.jdbc.schema.SchemaGroup;
+
+import org.apache.openjpa.persistence.jdbc.common.apps.*;
+
+
+import java.lang.annotation.Annotation;
+import junit.framework.*;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+
+
+public class TestSchemaClone extends TestXMLSchemaParser {
+
+ /** Creates a new instance of TestSchemaClone */
+ public TestSchemaClone() {
+ }
+
+ public TestSchemaClone(String test) {
+ super(test);
+ }
+
+ protected SchemaGroup getSchemaGroup()
+ throws Exception {
+ // parse in the schema group, then clone it, and test against clone
+ SchemaGroup group = parseSchemaGroup();
+ return (SchemaGroup) group.clone();
+ }
+
+ public static void main(String[] args) {
+ //main(TestSchemaClone.class);
+ }
+
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestSchemaGenerator.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestSchemaGenerator.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestSchemaGenerator.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestSchemaGenerator.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,131 @@
+/*
+ * TestSchemaGenerator.java
+ *
+ * Created on October 6, 2006, 2:57 PM
+ *
+ * To change this template, choose Tools | Template Manager
+ * and open the template in the editor.
+ */
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.jdbc.schema;
+
+import java.io.StringWriter;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.util.*;
+import javax.sql.DataSource;
+import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
+import org.apache.openjpa.jdbc.meta.ClassMapping;
+import org.apache.openjpa.jdbc.meta.MappingRepository;
+import org.apache.openjpa.jdbc.schema.Column;
+import org.apache.openjpa.jdbc.schema.SchemaTool;
+import org.apache.openjpa.jdbc.schema.Table;
+import org.apache.openjpa.jdbc.sql.DBDictionary;
+
+import org.apache.openjpa.persistence.jdbc.common.apps.*;
+
+
+import java.lang.annotation.Annotation;
+import junit.framework.*;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
+import org.apache.openjpa.persistence.OpenJPAPersistence;
+
+
+public class TestSchemaGenerator extends org.apache.openjpa.persistence.jdbc.kernel.BaseJDBCTest{
+
+ /** Creates a new instance of TestSchemaGenerator */
+ public TestSchemaGenerator(String name)
+ {
+ super(name);
+ }
+
+ public void DBMetadataTest()
+ throws Exception {
+ OpenJPAEntityManagerFactory pmf = (OpenJPAEntityManagerFactory)
+ getEmf();
+ //FIXME jthomas
+
+ //ClassMapping cm = (ClassMapping) KodoJDOHelper.getMetaData
+ // (pmf, RuntimeTest1.class);
+ ClassMapping cm =null;
+ JDBCConfiguration conf = (JDBCConfiguration) getConfiguration();
+
+ DataSource ds = (DataSource) conf.getDataSource2(null);
+ Connection c = ds.getConnection();
+ DatabaseMetaData meta = c.getMetaData();
+ DBDictionary dict = conf.getDBDictionaryInstance();
+
+ String schema = cm.getTable().getSchema().getName();
+ Table[] tables = dict.getTables(meta, c.getCatalog(), schema,
+ cm.getTable().getName(), c);
+ assertEquals(1, tables.length);
+
+ Column[] columns = dict.getColumns(meta, c.getCatalog(), schema,
+ cm.getTable().getName(), null, c);
+ for (int i = 0; i < columns.length; i++)
+ System.out.println("### " + columns[i].getName());
+ }
+
+ public void testSchemaGen()
+ throws Exception {
+ OpenJPAEntityManagerFactory pmf = (OpenJPAEntityManagerFactory)
+ getEmf();
+ OpenJPAEntityManager pm = pmf.createEntityManager();
+ JDBCConfiguration con = (JDBCConfiguration) ((OpenJPAEntityManagerSPI) pm).getConfiguration();
+ DBDictionary dict = con.getDBDictionaryInstance();
+ MappingRepository repos = con.getMappingRepositoryInstance();
+ ClassMapping cm = repos.getMapping(RuntimeTest1.class,
+ pm.getClassLoader(), true);
+ String schemas = cm.getTable().getSchema().getName();
+ if (schemas == null)
+ schemas = "";
+ schemas += "." + cm.getTable().getName();
+
+ Map props=new HashMap();
+ props.put("openjpa.jdbc.Schemas", schemas);
+
+ OpenJPAEntityManagerFactory kpmf =(OpenJPAEntityManagerFactory)
+ getEmf(props);
+ JDBCConfiguration conf = (JDBCConfiguration) ((OpenJPAEntityManagerFactorySPI) kpmf).getConfiguration();
+
+ StringWriter sw = new StringWriter();
+
+ SchemaTool.Flags flags = new SchemaTool.Flags();
+ flags.writer = sw;
+ flags.primaryKeys = true;
+ flags.foreignKeys = true;
+ flags.indexes = true;
+ flags.openjpaTables = true;
+ flags.action = SchemaTool.ACTION_REFLECT;
+
+ SchemaTool.run(conf, new String[0], flags,
+ getClass().getClassLoader());
+
+ sw.flush();
+ String data = sw.toString();
+ assertTrue(data.length() > 0);
+ }
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestSequenceGeneratorEnsureCapacityCall.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestSequenceGeneratorEnsureCapacityCall.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestSequenceGeneratorEnsureCapacityCall.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestSequenceGeneratorEnsureCapacityCall.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.jdbc.schema;
+
+import java.util.*;
+import org.apache.openjpa.conf.OpenJPAConfiguration;
+import org.apache.openjpa.jdbc.kernel.ClassTableJDBCSeq;
+import org.apache.openjpa.jdbc.kernel.TableJDBCSeq;
+import org.apache.openjpa.jdbc.schema.Sequence;
+import org.apache.openjpa.kernel.Seq;
+import org.apache.openjpa.meta.ClassMetaData;
+import org.apache.openjpa.meta.SequenceMetaData;
+
+import org.apache.openjpa.persistence.jdbc.common.apps.*;
+
+
+import java.lang.annotation.Annotation;
+import junit.framework.*;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+
+
+public class TestSequenceGeneratorEnsureCapacityCall
+ extends org.apache.openjpa.persistence.jdbc.kernel.TestSQLListenerTestCase{
+
+
+
+ /** Creates a new instance of TestSequenceGeneratorEnsureCapacityCall */
+ public TestSequenceGeneratorEnsureCapacityCall(String name)
+ {
+ super(name);
+ }
+
+ // class SeqA uses the default sequence factory.
+ public void testDBSequenceFactory() {
+ TableJDBCSeq seq = (TableJDBCSeq) seqForClass(SeqA.class);
+ incrementTest(SeqA.class, seq.getAllocate());
+ }
+
+ // class SeqD uses the db-class sequence factory.
+ public void testClassDBSequenceFactory() {
+ ClassTableJDBCSeq seq = (ClassTableJDBCSeq) seqForClass(SeqD.class);
+ incrementTest(SeqD.class, seq.getAllocate());
+ }
+
+ private void incrementTest(Class cls, int generatorIncrement) {
+ // guaranteed to be more than a single batch, so ensureCapacity should
+ // do its thing.
+ int amountToIncrement = generatorIncrement * 2;
+
+ OpenJPAEntityManager pm =(OpenJPAEntityManager)currentEntityManager();
+ //FIXME jthomas
+ //Sequence s = pm.getIdentitySequence(cls);
+ Sequence s =null;
+ s.setAllocate(amountToIncrement);
+
+ sql.clear();
+ for (int i = 0; i < amountToIncrement; i++) {
+ s.getIncrement();
+ assertEquals("SQL list should have been empty on sequence"
+ + " fetch #" + i + " of #" + amountToIncrement
+ + ", but contained: " + sql, 0, sql.size());
+ }
+ }
+
+ private Seq seqForClass(Class cls) {
+ OpenJPAConfiguration conf = getConfiguration();
+ ClassMetaData meta = conf.getMetaDataRepositoryInstance().getMetaData
+ (cls, null, true);
+ SequenceMetaData smeta = meta.getIdentitySequenceMetaData();
+ return (smeta == null) ? conf.getSequenceInstance()
+ : smeta.getInstance(null);
+ }
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestXMLSchemaParser.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestXMLSchemaParser.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestXMLSchemaParser.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestXMLSchemaParser.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,239 @@
+/*
+ * TestXMLSchemaParser.java
+ *
+ * Created on October 6, 2006, 2:42 PM
+ *
+ * To change this template, choose Tools | Template Manager
+ * and open the template in the editor.
+ */
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.jdbc.schema;
+
+import java.io.IOException;
+import java.sql.Types;
+import java.util.*;
+import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
+import org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl;
+import org.apache.openjpa.jdbc.schema.Column;
+import org.apache.openjpa.jdbc.schema.ForeignKey;
+import org.apache.openjpa.jdbc.schema.Index;
+import org.apache.openjpa.jdbc.schema.PrimaryKey;
+import org.apache.openjpa.jdbc.schema.Schema;
+import org.apache.openjpa.jdbc.schema.SchemaGroup;
+import org.apache.openjpa.jdbc.schema.Sequence;
+import org.apache.openjpa.jdbc.schema.Table;
+import org.apache.openjpa.jdbc.schema.XMLSchemaParser;
+
+import org.apache.openjpa.persistence.jdbc.common.apps.*;
+
+
+import java.lang.annotation.Annotation;
+import junit.framework.*;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+
+
+public class TestXMLSchemaParser extends org.apache.openjpa.persistence.jdbc.kernel.BaseJDBCTest{
+
+ protected JDBCConfiguration conf = null;
+ private SchemaGroup _group = null;
+
+
+ /** Creates a new instance of TestXMLSchemaParser */
+ public TestXMLSchemaParser() {
+ }
+
+ public TestXMLSchemaParser(String test) {
+ super(test);
+ }
+
+ public void setUp()
+ throws Exception {
+ this.conf = new JDBCConfigurationImpl();
+ _group = getSchemaGroup();
+ }
+
+ /**
+ * Parses the schema group from the schema XML
+ * resources in this package.
+ */
+ protected SchemaGroup parseSchemaGroup()
+ throws IOException {
+ XMLSchemaParser parser = new SCMSchemaParser(this.conf);
+ parser.parse(TestXMLSchemaParser.class, false);
+ parser.parse(TestSchema.class, false); // will go up to package level
+ return parser.getSchemaGroup();
+ }
+
+ /**
+ * Return the schema group to use in testing. Returns
+ * {@link #parseSchemaGroup} by default.
+ */
+ protected SchemaGroup getSchemaGroup()
+ throws Exception {
+ return parseSchemaGroup();
+ }
+
+ /**
+ * Checks the generated schema group for accuracy.
+ */
+ public void testSchemaParsing() {
+ assertEquals(2, _group.getSchemas().length);
+ assertEquals("SCHEMA1", _group.getSchema("SCHEMA1").getName());
+ assertEquals("SCHEMA2", _group.getSchema("SCHEMA2").getName());
+ }
+
+ /**
+ * Checks the pased sequences.
+ */
+ public void testSequenceParsing() {
+ Schema schema = _group.getSchema("SCHEMA1");
+ assertEquals(2, schema.getSequences().length);
+ assertEquals(0, _group.getSchema("SCHEMA2").getSequences().length);
+
+ Sequence seq1 = schema.getSequence("SEQ1");
+ assertNotNull(seq1);
+ assertEquals("SEQ1", seq1.getName());
+ assertEquals(seq1, _group.findSequence("SEQ1"));
+ assertEquals(seq1, _group.findSequence("SCHEMA1.SEQ1"));
+ assertEquals(1, seq1.getInitialValue());
+ assertEquals(1, seq1.getIncrement());
+ assertEquals(0, seq1.getAllocate());
+
+ Sequence seq2 = schema.getSequence("SEQ2");
+ assertNotNull(seq2);
+ assertEquals(3, seq2.getInitialValue());
+ assertEquals(5, seq2.getIncrement());
+ assertEquals(50, seq2.getAllocate());
+ }
+
+ /**
+ * Checks table and column parsing.
+ */
+ public void testTableColumnParsing() {
+ Schema schema1 = _group.getSchema("SCHEMA1");
+ Table[] tables = schema1.getTables();
+ assertEquals(2, tables.length);
+ assertEquals("TABLE1", tables[0].getName());
+ assertEquals("TABLE3", tables[1].getName());
+
+ Column[] cols = tables[0].getColumns();
+ assertEquals(2, cols.length);
+ assertEquals("COL1", cols[0].getName());
+ assertEquals("COL2", cols[1].getName());
+ assertEquals(Types.VARCHAR, cols[0].getType());
+ assertEquals(Types.FLOAT, cols[1].getType());
+ assertTrue(cols[0].isNotNull());
+ assertTrue(!cols[1].isNotNull());
+ assertEquals("def", cols[0].getDefault());
+ assertNull(cols[1].getDefault());
+ }
+
+ /**
+ * Test that primary keys are resolved correctly.
+ */
+ public void testPrimaryKeyParsing() {
+ Table table = _group.getSchema("SCHEMA1").getTable("TABLE1");
+ PrimaryKey pk = table.getPrimaryKey();
+ assertNotNull(pk);
+ assertEquals("PK1", pk.getName());
+ assertTrue(pk.isLogical());
+ assertEquals(1, pk.getColumns().length);
+ assertEquals(table.getColumn("COL1"), pk.getColumns()[0]);
+
+ table = _group.getSchema("SCHEMA2").getTable("TABLE2");
+ pk = table.getPrimaryKey();
+ assertNotNull(pk);
+ assertEquals("PK2", pk.getName());
+ assertTrue(!pk.isLogical());
+ assertEquals(2, pk.getColumns().length);
+ assertEquals(table.getColumn("COL1"), pk.getColumns()[0]);
+ assertEquals(table.getColumn("COL2"), pk.getColumns()[1]);
+ }
+
+ /**
+ * Test that indexes are resolved correctly.
+ */
+ public void testIndexParsing() {
+ Table table = _group.getSchema("SCHEMA1").getTable("TABLE1");
+ Index idx = table.getIndex("IDX1");
+ assertNotNull(idx);
+ assertTrue(idx.isUnique());
+ assertEquals(1, idx.getColumns().length);
+ assertEquals(table.getColumn("COL2"), idx.getColumns()[0]);
+
+ table = _group.getSchema("SCHEMA2").getTable("TABLE2");
+ idx = table.getIndex("IDX2");
+ assertNotNull(idx);
+ assertTrue(!idx.isUnique());
+ assertEquals(2, idx.getColumns().length);
+ assertEquals(table.getColumn("COL1"), idx.getColumns()[0]);
+ assertEquals(table.getColumn("COL2"), idx.getColumns()[1]);
+ }
+
+ /**
+ * Test that foreign keys are resolved correctly.
+ */
+ public void testForeignKeyParsing() {
+ Table table1 = _group.getSchema("SCHEMA1").getTable("TABLE1");
+ Table table2 = _group.getSchema("SCHEMA2").getTable("TABLE2");
+
+ ForeignKey fk = table1.getForeignKeys()[0];
+ assertEquals("FK1", fk.getName());
+ assertNotNull(fk);
+ assertEquals(ForeignKey.ACTION_RESTRICT, fk.getDeleteAction());
+ Column[] cols = fk.getColumns();
+ Column[] pkCols = fk.getPrimaryKeyColumns();
+ assertEquals(2, cols.length);
+ assertEquals(2, pkCols.length);
+ assertEquals(table1.getColumn("COL1"), cols[0]);
+ assertEquals(table2.getColumn("COL1"), pkCols[0]);
+ assertEquals(table1.getColumn("COL2"), cols[1]);
+ assertEquals(table2.getColumn("COL2"), pkCols[1]);
+
+ fk = table2.getForeignKeys()[0];
+ assertEquals("FK2", fk.getName());
+ assertNotNull(fk);
+ assertEquals(ForeignKey.ACTION_NONE, fk.getDeleteAction());
+ cols = fk.getColumns();
+ pkCols = fk.getPrimaryKeyColumns();
+ assertEquals(1, cols.length);
+ assertEquals(1, pkCols.length);
+ assertEquals(table2.getColumn("COL2"), cols[0]);
+ assertEquals(table1.getColumn("COL1"), pkCols[0]);
+ }
+
+ public static void main(String[] args) {
+ //main(TestXMLSchemaParser.class);
+ }
+
+ public static class SCMSchemaParser
+ extends XMLSchemaParser {
+
+ public SCMSchemaParser(JDBCConfiguration conf) {
+ super(conf);
+ setSuffix(".scm");
+ }
+ }
+
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestXMLSchemaSerializer.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestXMLSchemaSerializer.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestXMLSchemaSerializer.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestXMLSchemaSerializer.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,86 @@
+/*
+ * TestXMLSchemaSerializer.java
+ *
+ * Created on October 6, 2006, 4:47 PM
+ *
+ * To change this template, choose Tools | Template Manager
+ * and open the template in the editor.
+ */
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.jdbc.schema;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.util.*;
+import org.apache.openjpa.jdbc.schema.SchemaGroup;
+import org.apache.openjpa.jdbc.schema.SchemaParser;
+import org.apache.openjpa.jdbc.schema.SchemaSerializer;
+import org.apache.openjpa.jdbc.schema.XMLSchemaParser;
+import org.apache.openjpa.jdbc.schema.XMLSchemaSerializer;
+
+import org.apache.openjpa.persistence.jdbc.common.apps.*;
+
+
+import java.lang.annotation.Annotation;
+import junit.framework.*;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+
+
+public class TestXMLSchemaSerializer extends TestXMLSchemaParser{
+
+
+ /** Creates a new instance of TestXMLSchemaSerializer */
+ public TestXMLSchemaSerializer() {
+ }
+
+
+ public TestXMLSchemaSerializer(String test) {
+ super(test);
+ }
+
+ protected SchemaGroup getSchemaGroup()
+ throws Exception {
+ // parse in the schema group, then serialize it to a buffer, then
+ // recreate it and test againt that to make sure it's the same as the
+ // original
+ SchemaGroup group = parseSchemaGroup();
+ SchemaSerializer ser = new XMLSchemaSerializer(this.conf);
+ ser.addAll(group);
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ ser.serialize(new OutputStreamWriter(out), ser.PRETTY);
+ byte[] bytes = out.toByteArray();
+
+ SchemaParser parser = new XMLSchemaParser(this.conf);
+ parser.parse(new InputStreamReader
+ (new ByteArrayInputStream(bytes)), "bytes");
+ return parser.getSchemaGroup();
+ }
+
+ public static void main(String[] args) {
+ // main(TestXMLSchemaSerializer.class);
+ }
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/clauses/TestBulkUpdate.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/clauses/TestBulkUpdate.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/clauses/TestBulkUpdate.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/clauses/TestBulkUpdate.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.jpql.clauses;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
+import org.apache.openjpa.persistence.common.apps.*;
+import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
+
+public class TestBulkUpdate extends AbstractTestCase {
+
+ public TestBulkUpdate(String name) {
+ super(name, "jpqlclausescactusapp");
+ }
+
+ public void setUp() {
+ deleteAll(Entity1.class);
+ deleteAll(Entity2.class);
+ }
+
+ public void testSimpleBulkUpdate() {
+ for (int i = 1; i < 5; i++)
+ testSimpleBulkUpdate(i);
+ }
+
+ public void testSimpleBulkUpdate(int num) {
+ deleteAll(Entity1.class);
+
+ EntityManager em = currentEntityManager();
+ startTx(em);
+ for (int i = 0; i < num; i++) {
+ Entity1 e = new Entity1(i, "value1", i);
+ em.persist(e);
+ }
+ endTx(em);
+
+ // test update
+ startTx(em);
+ Query updateQuery1 = em.createQuery("update Entity1 e"
+ + " set e.stringField = 'value2', e.intField = 2"
+ + " where e.stringField = :val").
+ setParameter("val", "value1");
+ assertEquals(num, updateQuery1.executeUpdate());
+ assertEquals(0, updateQuery1.executeUpdate()); // should be updated
+ endTx(em);
+
+ // test update with parameter
+ startTx(em);
+ Query updateQuery2 = em.createQuery("update Entity1 e"
+ + " set e.stringField = :newval where e.stringField = :val").
+ setParameter("val", "value2").
+ setParameter("newval", "value3");
+ assertEquals(num, updateQuery2.executeUpdate());
+ assertEquals(0, updateQuery2.executeUpdate()); // should be updated
+ endTx(em);
+
+ // test update with 2 parameters
+ startTx(em);
+ Query updateQuery3 = em.createQuery("update Entity1 e"
+ + " set e.stringField = :newval, e.intField = 999"
+ + " where e.stringField = :val").
+ setParameter("val", "value3").
+ setParameter("newval", "value4");
+ assertEquals(num, updateQuery3.executeUpdate());
+ assertEquals(0, updateQuery3.executeUpdate()); // should be updated
+ endTx(em);
+
+ // test update with null value
+ startTx(em);
+ Query updateQuery4 = em.createQuery("update Entity1 e"
+ + " set e.stringField = :nullval, e.intField = :intval"
+ + " where e.stringField = :val"
+ + " and e.intField = 999").
+ setParameter("val", "value4").
+ setParameter("intval", new Integer(987)).
+ setParameter("nullval", null);
+ assertEquals(num, updateQuery4.executeUpdate());
+ assertEquals(0, updateQuery4.executeUpdate()); // should be updated
+ endTx(em);
+
+ // test update with field refernece in update value
+ // ### this seems to not be working
+ startTx(em);
+ Query updateFieldValue = em.createQuery("update Entity1 e"
+ + " set e.intField = e.intField + 1");
+ assertEquals(num, updateFieldValue.executeUpdate());
+ endTx(em);
+
+ startTx(em);
+ Query deleteQuery = em.createQuery
+ ("delete from Entity1 e where e.stringField = :val").
+ setParameter("val", null);
+ assertEquals(num, deleteQuery.executeUpdate());
+ assertEquals(0, deleteQuery.executeUpdate());
+ endTx(em);
+
+ endEm(em);
+ }
+}
+
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/clauses/TestEJBClauses.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/clauses/TestEJBClauses.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/clauses/TestEJBClauses.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/clauses/TestEJBClauses.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,216 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.jpql.clauses;
+
+import java.util.ArrayList;
+import java.util.List;
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
+import org.apache.openjpa.persistence.common.apps.Course;
+import org.apache.openjpa.persistence.common.apps.ArtCourse;
+import org.apache.openjpa.persistence.common.apps.Department;
+import org.apache.openjpa.persistence.common.apps.Student;
+
+public class TestEJBClauses extends AbstractTestCase {
+
+ public TestEJBClauses(String name) {
+ super(name, "jpqlclausescactusapp");
+ }
+
+ public void setUp() {
+ deleteAll(Course.class);
+ deleteAll(Student.class);
+ deleteAll(Department.class);
+
+ EntityManager em = currentEntityManager();
+ startTx(em);
+
+ String name = "";
+ List<Course> clist = new ArrayList<Course>();
+ List<Department> dlist = new ArrayList<Department>();
+
+ for (int i = 0; i < 5; i++) {
+ Course curr = new Course("Math " + i, i * 2, i);
+ Course acurr = new ArtCourse(i + 20, "English" + (2 * i));
+ Department durr = new Department("CompSci" + i, null, i + 2);
+ clist.add(curr);
+ clist.add(acurr);
+ dlist.add(durr);
+ }
+
+ Student stud = new Student("Jonathan", clist, dlist);
+ Student stud2 = new Student("Stam", null, dlist);
+ Student stud3 = new Student("John", clist, null);
+ Student stud4 = new Student("Bill", null, null);
+
+ em.persist(stud);
+ em.persist(stud2);
+ em.persist(stud3);
+ em.persist(stud4);
+
+ endTx(em);
+ endEm(em);
+ }
+
+ public void testFromClause1() {
+ EntityManager em = currentEntityManager();
+ String query = "SELECT o.name FROM Student o";
+
+ List result = em.createQuery(query)
+ .getResultList();
+
+ assertNotNull(result);
+ assertEquals(4, result.size());
+
+ endEm(em);
+ }
+
+ public void testFromClause2() {
+ EntityManager em = currentEntityManager();
+
+ String query = "SELECT NEW apps.ArtCourse(e.name)" +
+ "FROM Student e";
+
+ List result = em.createQuery(query).getResultList();
+
+ assertNotNull(result);
+ assertEquals(4, result.size());
+
+ endEm(em);
+ }
+
+ public void testFromClause3() {
+ EntityManager em = currentEntityManager();
+ String query = "SELECT o.name " +
+ "FROM Student o, Course c " +
+ "WHERE o.course IS NULL AND o.department IS NULL";
+
+ List ls = (List) em.createQuery(query)
+ .getResultList();
+ String uno = (String) ls.get(0);
+
+ assertNotNull(ls);
+ assertEquals(1, ls.size());
+ assertEquals("Bill", uno);
+
+ endEm(em);
+ }
+
+ public void testWhereClause1() {
+ EntityManager em = currentEntityManager();
+ String query = "SELECT distinct s.name " +
+ "FROM Student s, Course d " +
+ "WHERE d.courseId >= 4 AND s.department IS NOT NULL";
+
+ List ls = em.createQuery(query).getResultList();
+
+ assertNotNull(ls);
+ assertEquals(2, ls.size());
+
+ endEm(em);
+ }
+
+ public void testWhereClause2() {
+ EntityManager em = currentEntityManager();
+ String query = "SELECT distinct s.name " +
+ "FROM Student s " +
+ "WHERE" +
+ " Exists(SELECT c FROM s.course c WHERE c.name LIKE 'Math%')";
+
+ List ls = em.createQuery(query).getResultList();
+
+ assertNotNull(ls);
+ assertEquals(2, ls.size());
+ assertTrue(ls.contains("Jonathan"));
+ assertTrue(ls.contains("John"));
+
+ endEm(em);
+ }
+
+ public void testClauseRangeVar() {
+ EntityManager em = currentEntityManager();
+ String query = "SELECT DISTINCT s FROM Student s, Student s2 " +
+ "WHERE s.name = 'John' AND s2.name = 'Jonathan'";
+
+ List ls = em.createQuery(query).getResultList();
+
+ assertNotNull(ls);
+ assertEquals(1, ls.size());
+
+ Student ret = (Student) ls.get(0);
+ assertEquals("John", ret.getName());
+
+ endEm(em);
+ }
+
+ public void testClausePathExpr() {
+ EntityManager em = currentEntityManager();
+ String failure = "SELECT DISTINCT s " +
+ "FROM Student s WHERE" +
+ " s.department.name = 'CompSci1'";
+
+ try {
+ List ls = em.createQuery(failure).getResultList();
+ fail(
+ "cannot compose path expressions from a path expression that evaluates to a collection.");
+ }
+ catch (Exception e) {
+ //
+ }
+
+ endEm(em);
+ }
+
+ public void testClausePathExpr2() {
+ EntityManager em = currentEntityManager();
+ String success =
+ "SELECT DISTINCT d.name FROM Student AS s, IN(s.department) d ";
+
+ List ls2 = em.createQuery(success).getResultList();
+
+ assertNotNull(ls2);
+ assertEquals(5, ls2.size());
+
+ endEm(em);
+ }
+
+ public void testCollMemberDecl() {
+ EntityManager em = currentEntityManager();
+
+ String colldec = "SELECT DISTINCT s.name " +
+ "FROM Student s," +
+ " IN(s.department) d" +
+ " WHERE d.name = 'CompSci2'";
+
+ List ls = em.createQuery(colldec).getResultList();
+
+ assertNotNull(ls);
+ assertEquals(2, ls.size());
+ assertTrue(ls.contains("Jonathan"));
+ assertTrue(ls.contains("Stam"));
+
+ endEm(em);
+ }
+
+ /**
+ * GroupBy , OrderBy clause is tested by testejbqlfunction under
+ * functional directory.
+ */
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/clauses/TestEJBDeleteUpdateImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/clauses/TestEJBDeleteUpdateImpl.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/clauses/TestEJBDeleteUpdateImpl.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/clauses/TestEJBDeleteUpdateImpl.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,163 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.jpql.clauses;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
+import org.apache.openjpa.persistence.common.apps.RuntimeTest1;
+import org.apache.openjpa.persistence.common.apps.RuntimeTest2;
+import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
+
+public class TestEJBDeleteUpdateImpl extends AbstractTestCase {
+
+ public TestEJBDeleteUpdateImpl() {
+ }
+
+ public TestEJBDeleteUpdateImpl(String name) {
+ super(name, "jpqlclausescactusapp");
+ }
+
+ public void setUp() {
+ deleteAll(RuntimeTest1.class);
+
+ EntityManager em = currentEntityManager();
+ startTx(em);
+
+ RuntimeTest1 runt1 = new RuntimeTest1(1);
+ runt1.setStringField("runt1");
+ runt1.setSelfOneOne(new RuntimeTest1(2));
+ RuntimeTest2 runt2 = new RuntimeTest2(3);
+ runt2.setStringField("runt2");
+
+ em.persist(runt1);
+ em.persist(runt2);
+
+ endTx(em);
+ endEm(em);
+ }
+
+ public void testUpdate1() {
+ EntityManager em = currentEntityManager();
+ startTx(em);
+
+ RuntimeTest1 ret = em.find(RuntimeTest1.class, 1);
+
+ assertNotNull(ret);
+ assertEquals("runt1", ret.getStringField());
+ assertNotNull(ret.getSelfOneOne());
+
+ String ejbqlUpdate =
+ "UPDATE RuntimeTest1 x SET x.stringField = :strngfld WHERE x.stringField = :stdfield";
+ int updatedEntities = em.createQuery(ejbqlUpdate)
+ .setParameter("strngfld", "runner13")
+ .setParameter("stdfield", "runt1")
+ .executeUpdate();
+
+ assertEquals(1, updatedEntities);
+
+ endTx(em);
+
+ RuntimeTest1 ret2 = em.find(RuntimeTest1.class, 1);
+ em.refresh(ret2);
+
+ assertNotNull(ret2);
+ assertEquals("runner13", ret2.getStringField());
+
+ em.close();
+ }
+
+ public void testUpdate2() {
+ EntityManager em = currentEntityManager();
+ startTx(em);
+
+ RuntimeTest1 run = em.find(RuntimeTest1.class, 1);
+ assertNotNull(run);
+ assertEquals("runt1", run.getStringField());
+ assertNotNull(run.getSelfOneOne());
+
+ String ejbqlUpdate = "UPDATE RuntimeTest1 x " +
+ "SET x.stringField = :strngfld " +
+ "WHERE x.stringField = :field " +
+ "AND x.selfOneOne " +
+ "IS NOT NULL";
+
+ int upEntities = em.createQuery(ejbqlUpdate)
+ .setParameter("strngfld", "upd")
+ .setParameter("field", "runt1")
+ .executeUpdate();
+
+ assertEquals(1, upEntities);
+
+ RuntimeTest1 inst = em.find(RuntimeTest1.class, 1);
+ em.refresh(inst);
+
+ assertNotNull(inst);
+// assertEquals("upd", inst.getStringField());
+
+ endTx(em);
+ endEm(em);
+ }
+
+ public void testDelete1() {
+ EntityManager em = currentEntityManager();
+ startTx(em);
+
+ RuntimeTest1 run = em.find(RuntimeTest1.class, 1);
+ assertNotNull(run);
+ assertEquals("runt1", run.getStringField());
+ assertNotNull(run.getSelfOneOne());
+
+ String ejbdelUpdate = "DELETE FROM RuntimeTest1 s " +
+ "WHERE s.stringField = :strngfld";
+ int delEntity = em.createQuery(ejbdelUpdate)
+ .setParameter("strngfld", "runt1")
+ .executeUpdate();
+
+ assertEquals(1, delEntity);
+
+ RuntimeTest1 del = em.find(RuntimeTest1.class, 1);
+ assertNull(del);
+
+ endTx(em);
+ endEm(em);
+ }
+
+ public void testDelete2() {
+ EntityManager em = currentEntityManager();
+ startTx(em);
+
+ RuntimeTest1 run = em.find(RuntimeTest1.class, 1);
+ assertNotNull(run);
+ assertEquals("runt1", run.getStringField());
+ assertNotNull(run.getSelfOneOne());
+
+ String ejbdelUpdate =
+ "DELETE FROM RuntimeTest1 r WHERE r.stringField = ?1";
+ Query query = em.createQuery(ejbdelUpdate);
+
+ query.setParameter(1, "runt1");
+ int ok = query.executeUpdate();
+
+ assertEquals(1, ok);
+
+ endTx(em);
+ endEm(em);
+ }
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/clauses/TestEJBPolymorphicQuery.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/clauses/TestEJBPolymorphicQuery.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/clauses/TestEJBPolymorphicQuery.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/clauses/TestEJBPolymorphicQuery.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.jpql.clauses;
+
+import java.util.List;
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.persistence.common.apps.Entity1;
+import org.apache.openjpa.persistence.common.apps.RuntimeTest1;
+import org.apache.openjpa.persistence.common.apps.RuntimeTest2;
+import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
+
+public class TestEJBPolymorphicQuery extends AbstractTestCase {
+
+ public TestEJBPolymorphicQuery(String name) {
+ super(name, "jpqlclausescactusapp");
+ }
+
+ public void setUp() {
+ deleteAll(RuntimeTest1.class);
+ deleteAll(RuntimeTest2.class);
+
+ EntityManager em = currentEntityManager();
+ startTx(em);
+
+ int run1 = 10;
+ int run2 = 15;
+
+ for (int i = 0; i < run1; i++) {
+ RuntimeTest1 rt = new RuntimeTest1(i);
+ rt.setStringField("foo " + i);
+ em.persist(rt);
+ }
+
+ for (int i = 10; i < run2; i++) {
+ em.persist(new RuntimeTest2(i));
+ }
+
+ endTx(em);
+ endEm(em);
+ }
+
+ /**
+ * Ensures that when a select query is ran against an entity at the top of the hierarchy
+ * that the result is its instances and that of all its subclass.
+ */
+ public void testPolymorphicSelect() {
+ EntityManager em = currentEntityManager();
+
+ List l = em.createQuery("Select object(o) from RuntimeTest1 o")
+ .getResultList();
+
+ assertNotNull(l);
+ assertEquals(15, l.size());
+
+ endEm(em);
+ }
+
+ public void testPolymorphicDelete() {
+ EntityManager em = currentEntityManager();
+ startTx(em);
+
+ int l = em.createQuery("Delete from RuntimeTest1")
+ .executeUpdate();
+
+ assertNotNull(l);
+ assertEquals(15, l);
+
+ endTx(em);
+ endEm(em);
+ }
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/clauses/TestEJBQueryInterface.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/clauses/TestEJBQueryInterface.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/clauses/TestEJBQueryInterface.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/clauses/TestEJBQueryInterface.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,152 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.jpql.clauses;
+
+import java.util.List;
+import javax.persistence.EntityManager;
+import javax.persistence.NamedQuery;
+
+import org.apache.openjpa.persistence.common.apps.Entity1;
+import org.apache.openjpa.persistence.common.apps.Entity2;
+import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
+
+@NamedQuery(name = "setParam1",
+ query = "SELECT o FROM Entity1 o WHERE o.pk LIKE :pk")
+public class TestEJBQueryInterface extends AbstractTestCase {
+
+ public TestEJBQueryInterface(String name) {
+ super(name, "jpqlclausescactusapp");
+ }
+
+ public void setUp() {
+ deleteAll(Entity1.class);
+
+ int instNum = 10;
+
+ EntityManager em = currentEntityManager();
+ startTx(em);
+
+ //create and persist multiple entity1 instances
+ for (int i = 0; i < instNum; i++) {
+ Entity1 ent = new Entity1(i, "string" + i, i + 2);
+ Entity2 ent2 = new Entity2(i * 2, "ent2" + i, i);
+ ent.setEntity2Field(ent2);
+ em.persist(ent);
+ }
+
+ endTx(em);
+ endEm(em);
+ }
+
+ public void testResultList() {
+ EntityManager em = currentEntityManager();
+ List list = em.createQuery("Select object(o) from Entity1 o")
+ .getResultList();
+
+ assertEquals(10, list.size());
+
+ endEm(em);
+ }
+
+ public void testGetSingleList() {
+ EntityManager em = currentEntityManager();
+ String curr = 2 + "";
+
+ Entity1 ret =
+ (Entity1) em.createQuery("SELECT o FROM Entity1 o WHERE o.pk = 2")
+ .getSingleResult();
+
+ assertNotNull(ret);
+ assertEquals("string2", ret.getStringField());
+ assertEquals(4, ret.getIntField());
+
+ endEm(em);
+ }
+
+ public void testExecuteUpdate() {
+
+ EntityManager em = currentEntityManager();
+ startTx(em);
+ int ret = em.createQuery("Delete FROM Entity1 o WHERE o.pk = 2")
+ .executeUpdate();
+
+ assertEquals(ret, 1);
+
+ int ret2 = em.createQuery("Delete FROM Entity1 o WHERE o.pk = 22")
+ .executeUpdate();
+
+ assertEquals(ret2, 0);
+
+ endTx(em);
+ endEm(em);
+ }
+
+ public void testSetMaxResults() {
+ EntityManager em = currentEntityManager();
+
+ List l = em.createQuery("Select object(o) from Entity1 o")
+ .setMaxResults(5)
+ .getResultList();
+
+ assertNotNull(l);
+ assertEquals(5, l.size());
+
+ endEm(em);
+ }
+
+ public void testSetFirstResults() {
+ EntityManager em = currentEntityManager();
+
+ List l = em.createQuery("Select object(o) from Entity1 o")
+ .setFirstResult(3)
+ .getResultList();
+
+ Entity1 ent = (Entity1) l.get(0);
+
+ assertNotNull(ent);
+ assertEquals("string3", ent.getStringField());
+ assertEquals(5, ent.getIntField());
+
+ endEm(em);
+ }
+
+ // Tests Binding an argument to a named parameter.
+ // pk, the named parameter --Not working yet--
+ public void xxxtestSetParameter1() {
+
+ EntityManager em = currentEntityManager();
+ String curr = 2 + "";
+
+ List ret = em.createQuery("SELECT o FROM Entity1 o WHERE o.pk LIKE :pk")
+ .setParameter("pk", curr)
+ .getResultList();
+
+ assertNotNull(ret);
+ assertEquals(1, ret.size());
+
+ ret = em.createNamedQuery("setParam1")
+ .setParameter("pk", curr)
+ .getResultList();
+
+ assertNotNull(ret);
+ assertEquals(1, ret.size());
+
+ endTx(em);
+ }
+}