Author: tfischer
Date: Wed Jul 6 08:41:27 2011
New Revision: 1143291
URL: http://svn.apache.org/viewvc?rev=1143291&view=rev
Log:
TORQUE-163
- moved the shutdown test into an extra test class
- checked that the database map entries survive a Torque.shutdown() and
subsequent Torque.init()
Added:
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/InitShutdownTest.java
Modified:
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/DataTest.java
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DeleteTest.java
Modified:
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/DataTest.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/DataTest.java?rev=1143291&r1=1143290&r2=1143291&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/DataTest.java
(original)
+++
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/DataTest.java
Wed Jul 6 08:41:27 2011
@@ -2577,39 +2577,6 @@ public class DataTest extends BaseRuntim
}
/**
- * Tests whether shutdown works correctly and whether reinitialisation
- * is possible after shutdown.
- * @throws TorqueException if shutdown does not exit cleanly.
- */
- public void testShutdown() throws Exception
- {
- Torque.shutdown();
-
- try
- {
- Torque.getDatabase(Torque.getDefaultDB());
- fail("database access should not be possible "
- + "when Torque is shutdown()");
- }
- catch (TorqueException e)
- {
- }
-
- Torque.init(System.getProperty(CONFIG_FILE_SYSTEM_PROPERTY));
-
- cleanBookstore();
- Author author = new Author();
- author.setName("shutdownName");
- author.save();
- List<Author> authors = AuthorPeer.doSelect(new Criteria());
- assertEquals("List should contain one author", 1, authors.size());
- author = authors.get(0);
- assertEquals("Author's name should be shutdownName",
- "shutdownName",
- author.getName());
- }
-
- /**
* Deletes all As, Bs, Cs and RAs
* @throws Exception if the cleanup fails
*/
Added:
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/InitShutdownTest.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/InitShutdownTest.java?rev=1143291&view=auto
==============================================================================
---
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/InitShutdownTest.java
(added)
+++
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/InitShutdownTest.java
Wed Jul 6 08:41:27 2011
@@ -0,0 +1,180 @@
+package org.apache.torque;
+
+/*
+ * 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.
+ */
+
+import java.util.List;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.torque.map.MapBuilder;
+import org.apache.torque.map.TableMap;
+import org.apache.torque.test.NonPkOIntegerFkPeer;
+import org.apache.torque.test.NonPkPIntegerFkPeer;
+import org.apache.torque.test.NullableOIntegerFkPeer;
+import org.apache.torque.test.NullablePIntegerFkPeer;
+import org.apache.torque.test.OIntegerPk;
+import org.apache.torque.test.OIntegerPkPeer;
+import org.apache.torque.test.PIntegerPk;
+import org.apache.torque.test.PIntegerPkPeer;
+import org.apache.torque.test.RequiredOIntegerFkPeer;
+import org.apache.torque.test.RequiredPIntegerFkPeer;
+import org.apache.torque.test.map.PIntegerPkMapBuilder;
+import org.apache.torque.util.Criteria;
+
+/**
+ * Tests initialisation and shutdown of Torque.
+ *
+ * @version $Id: DataTest.java 1139590 2011-06-25 18:43:58Z tfischer $
+ */
+public class InitShutdownTest extends TestCase
+{
+ private static Log log = LogFactory.getLog(DataTest.class);
+
+ /**
+ * Creates a new instance.
+ */
+ public InitShutdownTest(String name)
+ {
+ super(name);
+ }
+
+ public void testInitFromScratch() throws TorqueException
+ {
+ // Torque can already be inititialized if the test is run in eclipse
+ // and other tests have run before. This is not the ideal case
+ // but we consider it anyway
+ if (Torque.isInit())
+ {
+ Torque.shutdown();
+ }
+
+ // make sure PIntegerPkPeer class is loaded
+ String dummy = PIntegerPkPeer.ID;
+
+ initTorque();
+
+ assertNotNull(
+ Torque.getDatabaseMap().getTable(PIntegerPkPeer.TABLE_NAME));
+
+ checkTorqueInitialisationWithPIntegerPk();
+ checkTorqueInitialisationWithOIntegerPk();
+ }
+
+ /**
+ * Tests whether shutdown works correctly and whether reinitialisation
+ * is possible after shutdown.
+ *
+ * @throws TorqueException if shutdown does not exit cleanly.
+ */
+ public void testShutdown() throws Exception
+ {
+ // Torque should be initialized from previous test so this is not
+ // strictly needed
+ if (!Torque.isInit())
+ {
+ initTorque();
+ }
+
+ checkTorqueInitialisationWithOIntegerPk();
+ assertNotNull(
+ Torque.getDatabaseMap().getTable(OIntegerPkPeer.TABLE_NAME));
+ Torque.shutdown();
+
+ try
+ {
+ Torque.getDatabase(Torque.getDefaultDB());
+ fail("database access should not be possible "
+ + "when Torque is shutdown()");
+ }
+ catch (TorqueException e)
+ {
+ }
+ assertNotNull(
+ Torque.getDatabaseMap().getTable(OIntegerPkPeer.TABLE_NAME));
+
+
+ initTorque();
+ checkTorqueInitialisationWithOIntegerPk();
+ assertNotNull(
+ Torque.getDatabaseMap().getTable(OIntegerPkPeer.TABLE_NAME));
+ }
+
+ private void initTorque() throws TorqueException
+ {
+ Torque.init(System.getProperty(
+ BaseRuntimeTestCase.CONFIG_FILE_SYSTEM_PROPERTY));
+ }
+
+ private void checkTorqueInitialisationWithOIntegerPk() throws
TorqueException
+ {
+ Criteria criteria = new Criteria();
+ NullableOIntegerFkPeer.doDelete(criteria);
+ criteria.clear();
+ RequiredOIntegerFkPeer.doDelete(criteria);
+ criteria.clear();
+ NonPkOIntegerFkPeer.doDelete(criteria);
+ criteria.clear();
+ OIntegerPkPeer.doDelete(criteria);
+
+ OIntegerPk oIntegerPk = new OIntegerPk();
+ oIntegerPk.setName("shutdownName");
+ oIntegerPk.save();
+ List<OIntegerPk> oIntegerPkList
+ = OIntegerPkPeer.doSelect(new Criteria());
+ assertEquals(
+ "List should contain one OIntegerPk",
+ 1,
+ oIntegerPkList.size());
+ oIntegerPk = oIntegerPkList.get(0);
+ assertEquals("OIntegerPk name should be shutdownName",
+ "shutdownName",
+ oIntegerPk.getName());
+ }
+
+ private void checkTorqueInitialisationWithPIntegerPk() throws
TorqueException
+ {
+ Criteria criteria = new Criteria();
+ NullablePIntegerFkPeer.doDelete(criteria);
+ criteria.clear();
+ RequiredPIntegerFkPeer.doDelete(criteria);
+ criteria.clear();
+ NonPkPIntegerFkPeer.doDelete(criteria);
+ criteria.clear();
+ PIntegerPkPeer.doDelete(criteria);
+
+ ForeignKeySchemaData.clearTablesInDatabase();
+ PIntegerPk pIntegerPk = new PIntegerPk();
+ pIntegerPk.setName("shutdownName");
+ pIntegerPk.save();
+ List<PIntegerPk> pIntegerPkList
+ = PIntegerPkPeer.doSelect(new Criteria());
+ assertEquals(
+ "List should contain one PIntegerPk",
+ 1,
+ pIntegerPkList.size());
+ pIntegerPk = pIntegerPkList.get(0);
+ assertEquals("PIntegerPk name should be shutdownName",
+ "shutdownName",
+ pIntegerPk.getName());
+ }
+}
Modified:
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DeleteTest.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DeleteTest.java?rev=1143291&r1=1143290&r2=1143291&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DeleteTest.java
(original)
+++
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DeleteTest.java
Wed Jul 6 08:41:27 2011
@@ -20,15 +20,19 @@ package org.apache.torque.generated.peer
*/
import java.math.BigDecimal;
+import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.torque.BaseRuntimeTestCase;
import org.apache.torque.ForeignKeySchemaData;
+import org.apache.torque.PkSchemaData;
import org.apache.torque.TorqueException;
import org.apache.torque.om.NumberKey;
import org.apache.torque.om.ObjectKey;
+import org.apache.torque.test.Nopk;
+import org.apache.torque.test.NopkPeer;
import org.apache.torque.test.NullableOIntegerFk;
import org.apache.torque.test.NullableOIntegerFkPeer;
import org.apache.torque.test.OIntegerPk;
@@ -54,7 +58,8 @@ public class DeleteTest extends BaseRunt
}
/**
- * Tests the delete(DatabaseObject) method.
+ * Tests that the delete(DatabaseObject) method deletes the correct
+ * Object and no related objects.
*
* @throws Exception if a database error occurs.
*/
@@ -90,7 +95,6 @@ public class DeleteTest extends BaseRunt
List<OIntegerPk> oIntegerPkList
= OIntegerPkPeer.doSelect(new Criteria());
assertEquals(3, oIntegerPkList.size());
-
}
/**
@@ -128,6 +132,197 @@ public class DeleteTest extends BaseRunt
}
/**
+ * Tests that the delete(DatabaseObject) method still deletes an object
+ * if the pk matches but other column values are changed.
+ *
+ * @throws Exception if a database error occurs.
+ */
+ public void testDeleteByObjectChangedNopkColumn() throws Exception
+ {
+ ForeignKeySchemaData.clearTablesInDatabase();
+ ForeignKeySchemaData testData
+ = ForeignKeySchemaData.getDefaultTestData();
+ testData.save();
+
+ NullableOIntegerFk toDelete
+ = testData.getNullableOIntegerFkList().get(0);
+ toDelete.setName("nullableOIntegerFk2Changed");
+ int preDeleteId = toDelete.getId();
+
+ // check that three entries are in the NullableOIntegerFk table
+ List<NullableOIntegerFk> nullableOIntegerFkList
+ = getNullableOIntegerFkList();
+ assertEquals(3, nullableOIntegerFkList.size());
+ assertTrue(nullableOIntegerFkList.contains(toDelete));
+
+ // call delete method and check result.
+ int deleted = NullableOIntegerFkPeer.doDelete(toDelete);
+ assertEquals(1, deleted);
+ assertEquals(preDeleteId, toDelete.getId().intValue());
+
+ // check that there are two entries remaining in the database
+ // and the toDelete object was deleted
+ nullableOIntegerFkList = getNullableOIntegerFkList();
+ assertEquals(2, nullableOIntegerFkList.size());
+ assertFalse(nullableOIntegerFkList.contains(toDelete));
+ }
+
+ /**
+ * Tests that the delete(DatabaseObject) method also works for objects
+ * without primary key (pk).
+ *
+ * @throws Exception if a database error occurs.
+ */
+ public void testDeleteByObjectWithoutPk() throws Exception
+ {
+ PkSchemaData.clearTablesInDatabase();
+ PkSchemaData testData = PkSchemaData.getDefaultTestData();
+ testData.save();
+
+ Nopk toDelete = testData.getNopkList().get(1);
+
+ // check that three entries are in the Nopk table
+ List<Nopk> nopkList = getNopkList();
+ assertEquals(3, nopkList.size());
+ // check toDelete object is in database
+ // equals does not work without pk so check intcol
+ assertEquals(2, nopkList.get(1).getIntcol());
+
+ // call delete method and check result.
+ int deleted = NopkPeer.doDelete(toDelete);
+ assertEquals(1, deleted);
+
+ // check that there are two entries remaining in the database
+ // and the toDelete object was deleted
+ // (use intcol for latter as equals does not work)
+ nopkList = getNopkList();
+ assertEquals(2, nopkList.size());
+ assertFalse(2 == nopkList.get(0).getIntcol());
+ assertFalse(2 == nopkList.get(1).getIntcol());
+ }
+
+ /**
+ * Tests that the delete(DatabaseObject) does not delete objects
+ * without primary key where a not-binary column has been changed.
+ *
+ * @throws Exception if a database error occurs.
+ */
+ public void testDeleteByObjectWithoutPkChangedColumn() throws Exception
+ {
+ PkSchemaData.clearTablesInDatabase();
+ PkSchemaData testData = PkSchemaData.getDefaultTestData();
+ testData.save();
+
+ Nopk toDelete = testData.getNopkList().get(1);
+ toDelete.setName("nopk1Changed");
+
+ // check that three entries are in the Nopk table
+ List<Nopk> nopkList = getNopkList();
+ assertEquals(3, nopkList.size());
+ // check toDelete object is in database
+ // equals does not work without pk so check intcol
+ assertEquals(2, nopkList.get(1).getIntcol());
+
+ // call delete method and check result.
+ int deleted = NopkPeer.doDelete(toDelete);
+ assertEquals(0, deleted);
+
+ // check that there are all entries remaining in the database
+ // and the toDelete object is still there
+ // (use intcol for latter as equals does not work)
+ nopkList = getNopkList();
+ assertEquals(3, nopkList.size());
+ assertEquals(2, nopkList.get(1).getIntcol());
+ }
+
+ /**
+ * Tests that the delete(Collection<DatabaseObject>) method deletes
+ * the correct Objects and no related objects.
+ *
+ * @throws Exception if a database error occurs.
+ */
+ public void testDeleteByObjectCollection() throws Exception
+ {
+ ForeignKeySchemaData.clearTablesInDatabase();
+ ForeignKeySchemaData testData
+ = ForeignKeySchemaData.getDefaultTestData();
+ testData.save();
+
+ List<NullableOIntegerFk> toDelete
+ = new ArrayList<NullableOIntegerFk>();
+ toDelete.add(testData.getNullableOIntegerFkList().get(0));
+ toDelete.add(testData.getNullableOIntegerFkList().get(2));
+ List<Integer> preDeleteIds = new ArrayList<Integer>();
+ preDeleteIds.add(toDelete.get(0).getId());
+ preDeleteIds.add(toDelete.get(1).getId());
+
+ // check that three entries are in the NullableOIntegerFk table
+ List<NullableOIntegerFk> nullableOIntegerFkList
+ = getNullableOIntegerFkList();
+ assertEquals(3, nullableOIntegerFkList.size());
+ assertTrue(nullableOIntegerFkList.contains(toDelete.get(0)));
+ assertTrue(nullableOIntegerFkList.contains(toDelete.get(1)));
+
+ // call delete method and check that two objects have been deleted.
+ // and that the ids of the delete objects did not change
+ int deleted = NullableOIntegerFkPeer.doDelete(toDelete);
+ assertEquals(2, deleted);
+ assertEquals(preDeleteIds.get(0), toDelete.get(0).getId());
+ assertEquals(preDeleteIds.get(1), toDelete.get(1).getId());
+
+ // check that there is one entries remaining in the database
+ // and the objects contained in toDelete object were deleted
+ nullableOIntegerFkList = getNullableOIntegerFkList();
+ assertEquals(1, nullableOIntegerFkList.size());
+ assertFalse(nullableOIntegerFkList.contains(toDelete.get(0)));
+ assertFalse(nullableOIntegerFkList.contains(toDelete.get(1)));
+
+ // check that no associated object has been deleted
+ List<OIntegerPk> oIntegerPkList
+ = OIntegerPkPeer.doSelect(new Criteria());
+ assertEquals(3, oIntegerPkList.size());
+ }
+
+ /**
+ * Tests that the delete(Collection<DatabaseObject>) method deletes
+ * no object if a object to delete does not exist in the database.
+ *
+ * @throws Exception if a database error occurs.
+ */
+ public void testDeleteByObjectCollectionNoMatch() throws Exception
+ {
+ ForeignKeySchemaData.clearTablesInDatabase();
+ ForeignKeySchemaData testData
+ = ForeignKeySchemaData.getDefaultTestData();
+ testData.save();
+
+ List<NullableOIntegerFk> toDelete
+ = new ArrayList<NullableOIntegerFk>();
+ toDelete.add(testData.getNullableOIntegerFkList().get(0));
+ toDelete.add(testData.getNullableOIntegerFkList().get(2));
+ toDelete.get(0).setId(toDelete.get(0).getId() - 1);
+ int preDeleteId = toDelete.get(0).getId();
+
+ // check that three entries are in the NullableOIntegerFk table
+ // prior to deletion
+ List<NullableOIntegerFk> nullableOIntegerFkList
+ = getNullableOIntegerFkList();
+ assertEquals(3, nullableOIntegerFkList.size());
+ assertFalse(nullableOIntegerFkList.contains(toDelete.get(0)));
+ assertTrue(nullableOIntegerFkList.contains(toDelete.get(1)));
+
+ // call delete method and check result.
+ int deleted = NullableOIntegerFkPeer.doDelete(toDelete);
+ assertEquals(1, deleted);
+ assertEquals(preDeleteId, toDelete.get(0).getId().intValue());
+
+ // check that two entries remain in the database
+ nullableOIntegerFkList = getNullableOIntegerFkList();
+ assertEquals(2, nullableOIntegerFkList.size());
+ assertFalse(nullableOIntegerFkList.contains(toDelete.get(1)));
+ }
+
+ /**
* Tests the delete(ObjectKey) method.
*
* @throws Exception if a database error occurs.
@@ -378,7 +573,7 @@ public class DeleteTest extends BaseRunt
*
* @return the NullableOIntegerFk rows
*
- * @throws TorqueException if deleting fails.
+ * @throws TorqueException if reading fails.
*/
private List<NullableOIntegerFk> getNullableOIntegerFkList()
throws TorqueException
@@ -389,4 +584,19 @@ public class DeleteTest extends BaseRunt
= NullableOIntegerFkPeer.doSelect(criteria);
return result;
}
+
+ /**
+ * Reads all Nopk rows from the database.
+ *
+ * @return the Nopk rows
+ *
+ * @throws TorqueException if reading fails.
+ */
+ private List<Nopk> getNopkList() throws TorqueException
+ {
+ Criteria criteria = new Criteria();
+ criteria.addAscendingOrderByColumn(NopkPeer.INTCOL);
+ List<Nopk> result = NopkPeer.doSelect(criteria);
+ return result;
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]