Index: src/java/org/apache/jdo/tck/api/persistencemanagerfactory/Serialize.java =================================================================== --- src/java/org/apache/jdo/tck/api/persistencemanagerfactory/Serialize.java (revision 0) +++ src/java/org/apache/jdo/tck/api/persistencemanagerfactory/Serialize.java (revision 0) @@ -0,0 +1,142 @@ +/* + * 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.jdo.tck.api.persistencemanagerfactory; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.Date; + +import javax.jdo.PersistenceManager; +import javax.jdo.PersistenceManagerFactory; +import javax.jdo.Transaction; + +import org.apache.jdo.tck.JDO_Test; +import org.apache.jdo.tck.pc.company.Address; +import org.apache.jdo.tck.pc.company.Company; +import org.apache.jdo.tck.util.BatchTestRunner; +import org.apache.jdo.tck.util.EqualityHelper; + +/** + *Title:ReadOnly property of PersistenceManagerFactory + *
+ *Keywords: persistencemanagerfactory + *
+ *Assertion IDs:A11.10-1 + *
+ *Assertion Description: + * If the persistence manager factory was created + * via the getPersistenceManagerFactory(Map props) method, then + * the serializable properties in props must be written to the stream + * and upon restoration, used to construct or locate + * the persistence manager factory in the new context. + */ + + +public class Serialize extends JDO_Test { + + /** */ + private static final String ASSERTION_FAILED = + "Assertion Serializable failed: "; + + /** The company oid */ + Object oid; + + /** The company */ + Company referenceCompany; + + /** + * 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(FlushThrowsIfReadOnly.class); + } + + /** + * @see JDO_Test#localSetUp() + */ + protected void localSetUp() { + addTearDownClass(Company.class); + // make sure the PMF is initialized + getPM(); + pm.currentTransaction().begin(); + referenceCompany = new Company(1L, "Sun Microsystems", new Date(), + new Address(0,"","","","","")); + pm.makePersistent(referenceCompany); + oid = pm.getObjectId(referenceCompany); + pm.currentTransaction().commit(); + pm.close(); + } + + /** */ + public void testMaterializePMFinSameVM() { + /** Equality helper */ + EqualityHelper helper = new EqualityHelper(); + + // serialize the PMF to a stream + byte[] serializedPMF = serializeObject(pmf); + + // Materialize PMF from stream and read the object + PersistenceManagerFactory pmf2; + pmf2 = (PersistenceManagerFactory)materializeObject(serializedPMF); + + PersistenceManager pm2; + pm2 = pmf2.getPersistenceManager(); + + Transaction tx2 = pm2.currentTransaction(); + tx2.begin(); + // make sure Company and Company OID are known to this PMF + Company newCompany = new Company(100L, "Sun Microsystems", new Date(), + new Address(0,"","","","","")); + pm2.makePersistent(newCompany); + Company comp = (Company)pm2.getObjectById(oid); + if (!helper.deepEquals(referenceCompany, comp)) { + appendMessage(helper.getUnequalBuffer()); + } + tx2.rollback(); + pm2.close(); + pm2 = null; + closePMF(pmf2); + failOnError(); + } + + protected byte[] serializeObject(Object o) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try { + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.writeObject(o); + return baos.toByteArray(); + } catch (Throwable t) { + throw new RuntimeException("serializeObject threw " + t, t); + } + } + + protected Object materializeObject(byte[] ba) { + ObjectInputStream ois = null; + try { + ByteArrayInputStream bios = new ByteArrayInputStream(ba); + ois = new ObjectInputStream(bios); + return ois.readObject(); + } catch (Throwable t) { + throw new RuntimeException("materializeObject threw " + t, t); + } + } +} Property changes on: src/java/org/apache/jdo/tck/api/persistencemanagerfactory/Serialize.java ___________________________________________________________________ Name: svn:eol-style + LF