/* * Copyright 2005 The Apache Software Foundation. * * Licensed 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.jdo.tck.mapping; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.List; import javax.jdo.Extent; import javax.jdo.Query; import javax.jdo.identity.SingleFieldIdentity; import org.apache.jdo.tck.JDO_Test; import org.apache.jdo.tck.pc.company.CompanyFactoryRegistry; import org.apache.jdo.tck.pc.company.CompanyModelReader; import org.apache.jdo.tck.util.BatchTestRunner; import org.apache.jdo.tck.util.DeepEquality; import org.apache.jdo.tck.util.EqualityHelper; //import org.springframework.beans.factory.xml.XmlBeanFactory; /** *Title:Completeness Test *
*Keywords: mapping *
*Assertion ID: A18.[not identified] *
*Assertion Description: */ public class LifecycleCompleteness extends JDO_Test { /** */ private static final String ASSERTION_FAILED = "Assertion A18-[not identified] failed: "; /** */ private final boolean isTestToBePerformed = isTestToBePerformed(); /** Reader for transient instances */ private CompanyModelReader reader; /** Reader for persistent instances */ private CompanyModelReader reader2; /** */ protected List allOids; /** */ protected List allTransientInstances; /** */ protected List persistentClasses; /** */ protected final String inputFilename = System.getProperty("jdo.tck.testdata"); /** * The main is called when the class * is directly executed from the command line. * @param args The arguments passed to the program. */ public static void main(String[] args) { BatchTestRunner.run(LifecycleCompleteness.class); } /** * @see JDO_Test#localSetUp() */ protected void localSetUp() { if (isTestToBePerformed) { // register the default factory CompanyFactoryRegistry.registerFactory(); // get new obj graph to compare persistent graph with reader = new CompanyModelReader(inputFilename); reader.getRootList(); allTransientInstances = CompanyFactoryRegistry.getInstance().getAllInstances(); getPM(); CompanyFactoryRegistry.registerFactory(pm); reader2 = new CompanyModelReader(inputFilename); // persist test data pm.currentTransaction().begin(); List rootObjects2 = reader2.getRootList(); Class[] tearDownClasses = reader2.getTearDownClassesFromFactory(); addTearDownClass(tearDownClasses); persistentClasses = Arrays.asList(tearDownClasses); pm.makePersistentAll(rootObjects2); Collection allPersistentInstances = CompanyFactoryRegistry.getInstance().getAllInstances(); allOids = new ArrayList(); for (Iterator i = allTransientInstances.iterator(); i.hasNext();) { // get the object in the transient list Object transientInstance = i.next(); // find the corresponding persistent instance Object persistentInstance; boolean found = false; Iterator persistentIterator = allPersistentInstances.iterator(); while (persistentIterator.hasNext()) { persistentInstance = persistentIterator.next(); if (transientInstance.equals(persistentInstance)) { found = true; allOids.add(pm.getObjectId(persistentInstance)); break; } } if (!found) { appendMessage("localSetUp could not find persistent instance for" + transientInstance); } } pm.currentTransaction().commit(); cleanupPM(); } } /** */ public void test() { if (isTestToBePerformed) { verifyOids(); runGetObjectById(); runGetObjectsById(); runExtentIteration(); runQueryIterationFromClass(); runQueryIterationFromExtent(); failOnError(); } } /** */ protected void verifyOids() { // for each oid, verify that the class is one of the persistent classes Iterator oidIterator = allOids.iterator(); while (oidIterator.hasNext()) { Object oid = oidIterator.next(); if (oid instanceof SingleFieldIdentity) { SingleFieldIdentity id = (SingleFieldIdentity)(oid); Class clazz = id.getTargetClass(); if (!persistentClasses.contains(clazz)) { appendMessage("verifyOids found wrong class " + clazz + " in oid " + id); } } } } /** */ public void runGetObjectById() { List rootList = reader.getRootList(); getPM(); pm.currentTransaction().begin(); int size = allOids.size(); List actual = new ArrayList(size); for (int i = 0; i < size; i++) { Object oid = allOids.get(i); Object persisted = pm.getObjectById(oid); actual.add(persisted); } compareLists("runGetObjectById", allTransientInstances, actual); pm.currentTransaction().commit(); cleanupPM(); } /** */ public void runGetObjectsById() { List rootList = reader.getRootList(); getPM(); pm.currentTransaction().begin(); int size = allOids.size(); Collection actual = pm.getObjectsById(allOids); compareLists("runGetObjectsById", allTransientInstances, actual); pm.currentTransaction().commit(); cleanupPM(); } protected void compareLists(String where, Collection expectedCollection, Collection actualCollection) { int expectedSize = expectedCollection.size(); int actualSize = actualCollection.size(); if (expectedSize != actualSize) { appendMessage(where + " expected size " + expectedSize + " does not match actual size " + actualSize); } Iterator expectedIterator = expectedCollection.iterator(); Iterator actualIterator = actualCollection.iterator(); for (int i = 0; i < expectedSize; ++i) { Object expected = expectedIterator.next(); if (actualIterator.hasNext()) { Object actual = actualIterator.next(); if (!expected.equals(actual)) { appendMessage(where + " comparison failed; position " + i + NL + "expected: " + expected + " actual: " + actual + NL); } } else { appendMessage(where + " actual collection exhausted after " + i + " elements"); return; } } } /** */ protected void runExtentIteration() { List expectedList = new ArrayList(allTransientInstances); Collection actualCollection = new ArrayList(); getPM(); pm.currentTransaction().begin(); Class[] classes = reader2.getTearDownClassesFromFactory(); // get the objects for each class from the iterator over the Extent for (int i = 0; i < classes.length; ++i) { Class clazz = classes[i]; Extent extent = pm.getExtent(clazz, false); int count = 0; for (Iterator it = extent.iterator(); it.hasNext();) { ++count; Object persistent = it.next(); actualCollection.add(persistent); } } // check list of persistent instances against expected compareCollections("runExtentIteration", expectedList, actualCollection); pm.currentTransaction().commit(); cleanupPM(); } /** */ protected void runQueryIterationFromClass() { List expectedList = new ArrayList(allTransientInstances); Collection actualCollection = new ArrayList(); getPM(); pm.currentTransaction().begin(); Class[] classes = reader2.getTearDownClassesFromFactory(); // query for instances of each class in the list for (int i = 0; i < classes.length; ++i) { Query query = pm.newQuery(classes[i]); Collection resultCollection = (Collection)query.execute(); Iterator resultIterator = resultCollection.iterator(); while (resultIterator.hasNext()) { actualCollection.add(resultIterator.next()); } } compareCollections("runQueryIterationFromClass", expectedList, actualCollection); pm.currentTransaction().commit(); cleanupPM(); } /** */ protected void runQueryIterationFromExtent() { List expectedList = new ArrayList(allTransientInstances); Collection actualCollection = new ArrayList(); getPM(); pm.currentTransaction().begin(); Class[] classes = reader2.getTearDownClassesFromFactory(); // query for instances of each class in the list for (int i = 0; i < classes.length; ++i) { Query query = pm.newQuery(); query.setCandidates(pm.getExtent(classes[i])); Collection resultCollection = (Collection)query.execute(); Iterator resultIterator = resultCollection.iterator(); while (resultIterator.hasNext()) { actualCollection.add(resultIterator.next()); } } compareCollections("runQueryIterationFromExtent", expectedList, actualCollection); pm.currentTransaction().commit(); cleanupPM(); } /** */ protected void compareCollections(String where, List expectedList, Collection actualCollection) { // first compare size int expectedSize = expectedList.size(); int actualSize = actualCollection.size(); if (expectedSize != actualSize) { appendMessage(where + " expected size " + expectedSize + " does not match actual size " + actualSize); } // iterate over the actual collection and remove corresponding elements Iterator actualIterator = actualCollection.iterator(); while (actualIterator.hasNext()) { Object actualInstance = actualIterator.next(); if (!expectedList.remove(actualInstance)) { appendMessage(where + " unmatched instance in actual collection" + actualInstance); } } // make sure each element in the expectedList was retrieved int leftovers = expectedList.size(); if (leftovers != 0) { appendMessage(where + " had " + leftovers + " leftovers"); Iterator leftoverIterator = expectedList.iterator(); while (leftoverIterator.hasNext()) { appendMessage(where + " resulted in leftover instance " + leftoverIterator.next()); } } } /** public void test() { if (isTestToBePerformed) { /** * Get by object id getObjectById() * Get by object id getObjectsById() * Get by Extent iteration * Get by Query without predicate * Get by Query with predicate * Delete by object id deletePersistent() */ /* List rootList = reader.getRootList(); getPM(); pm.currentTransaction().begin(); // compare persisted and new int size = rootList.size(); StringBuffer msg = new StringBuffer(); for (int i = 0; i < size; i++) { DeepEquality expected = (DeepEquality) rootList.get(i); Object oid = rootOids.get(i); Object persisted = pm.getObjectById(oid); EqualityHelper equalityHelper = new EqualityHelper(); if (!expected.deepCompareFields(persisted, equalityHelper)) { if (msg.length() > 0) { msg.append("\n"); } msg.append("Expected this instance:\n " + expected + "\n" + "Got persistent instance:" + "\n " + persisted + "\n" + "Detailed list of differences follows...\n"); msg.append(equalityHelper.getUnequalBuffer()); } } pm.currentTransaction().commit(); // fail test if at least one of the instances is not the expected one if (msg.length() > 0) { fail("CompletenessTest failed; see list of failures below:", msg.toString()); } } } */ }