Index: api20/src/java/javax/jdo/JDOHelper.java =================================================================== --- api20/src/java/javax/jdo/JDOHelper.java (revision 328583) +++ api20/src/java/javax/jdo/JDOHelper.java (working copy) @@ -30,6 +30,9 @@ import java.lang.reflect.Method; import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; import java.util.Map; import java.util.Properties; @@ -257,7 +260,47 @@ return implHelper.nonBinaryCompatibleGet(pc, getObjectId); } } - + + /** Get object ids for a collection of instances. For each instance + * in the parameter, the getObjectId method is called. This method + * returns one identity instance for each element + * in the parameter. The order of iteration of the returned + * Collection exactly matches the order of iteration of the + * parameter Collection. + * @param pcs the persistence-capable instances + * @return the object ids of the parameters + * @see #getObjectId(Object pc) + * @see #getObjectIds(Object[] pcs) + * @since 2.0 + */ + public static Collection getObjectIds(Collection pcs) { + ArrayList result = new ArrayList(); + for (Iterator it = pcs.iterator(); it.hasNext();) { + result.add(getObjectId(it.next())); + } + return result; + } + + /** Get object ids for an array of instances. For each instance + * in the parameter, the getObjectId method is called. This method + * returns one identity instance for each element + * in the parameter. The order of instances of the returned + * array exactly matches the order of instances of the + * parameter array. + * @param pcs the persistence-capable instances + * @return the object ids of the parameters + * @see #getObjectId(Object pc) + * @see #getObjectIds(Collection pcs) + * @since 2.0 + */ + public static Object[] getObjectIds(Object[] pcs) { + Object[] result = (Object[])pcs.clone(); + for (int i = 0; i < pcs.length; ++i) { + result[i] = getObjectId(pcs[i]); + } + return result; + } + /** Return a copy of the JDO identity associated with the parameter instance. * * @see PersistenceCapable#jdoGetTransactionalObjectId() Index: tck20/test/java/org/apache/jdo/tck/api/jdohelper/GetObjectIds.java =================================================================== --- tck20/test/java/org/apache/jdo/tck/api/jdohelper/GetObjectIds.java (revision 0) +++ tck20/test/java/org/apache/jdo/tck/api/jdohelper/GetObjectIds.java (revision 0) @@ -0,0 +1,165 @@ +/* + * 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.api.jdohelper; + +import java.util.List; +import java.util.Arrays; +import java.util.Collection; +import java.util.Date; +import java.util.Iterator; + +import javax.jdo.JDOHelper; +import javax.jdo.PersistenceManager; + +import org.apache.jdo.tck.JDO_Test; + +import org.apache.jdo.tck.pc.mylib.PCPoint; + +import org.apache.jdo.tck.util.BatchTestRunner; + +/** + *Title: Test GetObjectIds + *
+ *Keywords: getObjectIds getObjectId + *
+ *Assertion IDs: A8.3-11 + *
+ *Assertion Description: + * These methods return the JDO identities of the parameter instances. + * For each instance in the parameter, the getObjectId method is called. + * They return one identity instance for each persistence-capable instance + * in the parameter. The order of iteration of the returned Collection + * exactly matches the order of iteration of the parameter Collection.] + */ + +public class GetObjectIds extends JDO_Test { + + /** */ + private static final String ASSERTION_FAILED = + "Assertion A8.3-11 (GetObjectIds) failed: "; + + /** + * 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(GetObjectIds.class); + } + + /** The parameter objects + */ + Object obj0; + Object obj1; + Object obj2; + Object obj3; + Object obj4; + + Object[] objs; + + /** The object ids + */ + Object oid0; + Object oid1; + Object oid2; + Object oid3; + Object oid4; + + Object[] oids; + + /** + * @see JDO_Test#localSetUp() + */ + protected void localSetUp() { + addTearDownClass(PCPoint.class); + objs = new Object[] { + obj0 = null, + obj1 = new Date(), + obj2 = new PCPoint(23,45), // persistent in pm1 + obj3 = new PCPoint(26,47), // persistent in pm2 + obj4 = new PCPoint(23,45) // not persistent + }; + + getPM(); + pm.currentTransaction().begin(); + pm.makePersistent(obj2); + pm.currentTransaction().commit(); + + PersistenceManager pm2 = getPMF().getPersistenceManager(); + pm2.currentTransaction().begin(); + pm2.makePersistent(obj3); + pm2.currentTransaction().commit(); + + oids = new Object[] { + oid0 = JDOHelper.getObjectId(obj0), + oid1 = JDOHelper.getObjectId(obj1), + oid2 = JDOHelper.getObjectId(obj2), + oid3 = JDOHelper.getObjectId(obj3), + oid4 = JDOHelper.getObjectId(obj4), + }; + + } + + /** */ + public void testGetObjectIdsArray() { + StringBuffer messages = new StringBuffer(); + Object[] expectedArray = oids; + Object[] actualArray = JDOHelper.getObjectIds(objs); + for (int i = 0; i < objs.length; ++i) { + Object expected = expectedArray[i]; + Object actual = actualArray[i]; + if (expected == null? + actual != null: + !expected.equals(actual)) { + messages.append( + "\nComparison failed for object ids at position " + i + + "\nexpected: " + expected + + "\nactual: " + actual); + } + } + if (messages.length() != 0) { + fail(ASSERTION_FAILED + "getObjectIds(Object[] pcs) " + + messages.toString()); + } + } + + /** */ + public void testGetObjectIdsCollection() { + StringBuffer messages = new StringBuffer(); + List paramList = Arrays.asList(objs); + List expectedList = Arrays.asList(oids); + Collection actualCollection = JDOHelper.getObjectIds(paramList); + Iterator expectedIterator = expectedList.iterator(); + Iterator actualIterator = actualCollection.iterator(); + for (int i = 0; i < objs.length; ++i) { + Object expected = expectedIterator.next(); + Object actual = actualIterator.next(); + if (expected == null? + actual != null: + !expected.equals(actual)) { + messages.append( + "\nComparison failed for object ids at position " + i + + "\nexpected: " + expected + + "\nactual: " + actual); + } + } + if (messages.length() != 0) { + fail(ASSERTION_FAILED + "getObjectIds(Collection pcs) " + + messages.toString()); + } + } +}