Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDeptSerialization.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDeptSerialization.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDeptSerialization.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDeptSerialization.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,257 @@ +/* + * 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. + */ + +/* + * Test_EmpDeptSerialization.java + * + * Created on June 29, 2001, 3:37 PM + */ + +package org.apache.jdo.test; + +import java.util.*; +import java.io.*; + +import javax.jdo.*; + +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; + +import org.apache.jdo.impl.fostore.FOStorePMF; +import org.apache.jdo.pc.empdept.PCDepartment; +import org.apache.jdo.pc.empdept.PCEmployee; +import org.apache.jdo.pc.empdept.PCFullTimeEmployee; +import org.apache.jdo.pc.empdept.PCInsurance; +import org.apache.jdo.pc.empdept.PCPartTimeEmployee; +import org.apache.jdo.pc.empdept.PCProject; + +/** This class measures the difference between Serialization and JDO as a + * persistence strategy. + * Some number of instances are created, and then the test starts timing. + * First the objects are serialized; then a transaction is started, the + * objects are made persistent in JDO, and the transaction is committed. + * + * @author Craig Russell + * @version 1.0 + */ +public class Test_EmpDeptSerialization extends AbstractTest { + + ArrayList allObjects = new ArrayList(); + PCEmployee scott; + PCEmployee ed; + PCInsurance scottIns; + PCInsurance edIns; + PCDepartment board; + PCDepartment emg; + PCProject solaris; + PCProject sparc; + + /** + * @param args the command line arguments + */ + public static void main (String args[]) { + switch (args.length) { + case 0: + JDORITestRunner.run(Test_EmpDeptSerialization.class); + break; + case 2: + runLocal(args[0], args[1]); + break; + default: + System.err.println ("usage: \nTest_EmpDeptSerialization <url> <file>\n\t<url>: the url for fostore\n\t<file>: the file name for serialization"); + break; + } + } + + /** */ + public void test() throws Exception { + doTest("serialize.tmp"); + } + + /** Run the test. */ + void doTest(String fileName) throws Exception { + createObjects(); + long serializeTime = serializeObjects(fileName); + long fostoreTime = persistObjects(); + } + + /** */ + void createObjects() { + HashSet h; + + // Create and set up employees. Scott is Ed's manager. Ed is + // Scott's sole employee. + // + GregorianCalendar born = + new GregorianCalendar(TimeZone.getTimeZone("America/New_York")); + GregorianCalendar hired = + new GregorianCalendar(TimeZone.getTimeZone("America/New_York")); + + born.set(1969, 7, 20); + hired.set(1982, 5, 5); + scott = + new PCFullTimeEmployee( + "Scott", "McNealy", born.getTime(), + 1L, hired.getTime(), + //null, null, null, null, null, + 200000.0); + born.set(1960, 4, 8); + hired.set(1985, 2, 3); + ed = + new PCPartTimeEmployee( + "Ed", "Zander", born.getTime(), + 100L, hired.getTime(), + //null, null, null, null, null, + 400.0); + allObjects.add(ed); + allObjects.add(scott); + ed.setManager(scott); + + h = new HashSet(); + h.add(ed); + scott.setEmployees(h); + + // Set up their departments. + board = + new PCDepartment(100L, "board"); + h = new HashSet(); + h.add(scott); + board.setEmployees(h); + scott.setDepartment(board); + + emg = + new PCDepartment(200L, "emg"); + h = new HashSet(); + h.add(ed); + emg.setEmployees(h); + ed.setDepartment(emg); + + // Insure these guys + scottIns = new PCInsurance(1000, "Aetna", scott); + edIns = new PCInsurance(1001, "BlueCross", ed); + scott.setInsurance(scottIns); + ed.setInsurance(edIns); + + // Give them some projects to work on. Scott works on both; Ed only + // on one. + solaris = new PCProject(1L, "Solaris"); + sparc = new PCProject(2L, "Sparc"); + h = new HashSet(); + h.add(scott); + h.add(ed); + solaris.setEmployees(h); // Solaris is worked on by Scott and Ed + + h = new HashSet(); + h.add(scott); + sparc.setEmployees(h); // Sparc is worked on by Scott + + h = new HashSet(); + h.add(solaris); + h.add(sparc); + scott.setProjects(h); // Scott works on Solaris and Sparc + + h = new HashSet(); + h.add(solaris); + ed.setProjects(h); // Ed works on Solaris + + // Show what we've got + if (debug) { + logger.debug("Before insert: "); + logger.debug(scott.toString()); + logger.debug(ed.toString()); + logger.debug(board.toString()); + logger.debug(emg.toString()); + logger.debug(scottIns.toString()); + logger.debug(edIns.toString()); + logger.debug(solaris.toString()); + logger.debug(sparc.toString()); + } + } + + /** Insert objects into the database. If reachability were implemented, we'd + * only have to make scott and ed persistent, as everything else is + * reachable from them. + */ + long persistObjects() { + Timer timer = new Timer(); + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + tx.begin(); + pm.makePersistentAll(allObjects); + tx.commit(); + pm.close(); + long elapsed = timer.stop(); + if (debug) logger.debug ("FOStore results: \t" + elapsed); + return elapsed; + } + + /** Write the objects to a file using serialization. + */ + long serializeObjects(String fileName) throws IOException { + Timer timer = new Timer(); + ObjectOutputStream oos = getObjectOutputStream(fileName); + oos.writeObject(allObjects); + oos.flush(); + oos.close(); + long elapsed = timer.stop(); + if (debug) logger.debug ("Serialization results:\t" + elapsed); + return elapsed; + } + + /** Timer utility class. Measures wall clock time. + */ + static class Timer { + long startTime; + long stopTime; + Timer() { + startTime = new Date().getTime(); + } + + long stop() { + stopTime = new Date().getTime(); + return (stopTime - startTime); + } + + void start() { + startTime = new Date().getTime(); + } + } + + /** */ + static void runLocal(String url, String file) { + try { + // create PMF + FOStorePMF pmf = new FOStorePMF(); + pmf.setConnectionURL(url); + pmf.setConnectionUserName("craig"); + pmf.setConnectionPassword("secret"); + pmf.setConnectionCreate(true); + + // create and setup test + Test_EmpDeptSerialization sm = new Test_EmpDeptSerialization(); + sm.logger.debug("Test_EmpDeptSerialization using URL: " + url + + " FileName: " + file); + sm.pmf = pmf; + sm.debug = true; + sm.doTest(file); + sm.closePMF(); + } + catch(Exception ex) { + ex.printStackTrace(); + } + } + +}
Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Extent.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Extent.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Extent.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Extent.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,224 @@ +/* + * 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.test; + +import java.util.*; + +import javax.jdo.*; + +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; + +import org.apache.jdo.test.util.Factory; + +/** +* This test retrieves an extent-full of instances. +* +* @author Dave Bristor +*/ +public class Test_Extent extends AbstractTest { + /** If true, get subclass instances too. */ + private final boolean subclasses; + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_Extent.class); + } + + /** */ + public Test_Extent() { + super(); + this.subclasses = Boolean.getBoolean("subclasses"); + } + + /** */ + public void testOutsideTx() throws Exception { + insertObjects(); + runOutsideTx(); + } + + /** */ + public void testDatastoreTx() throws Exception { + insertObjects(); + runInsideTx(false); + } + + /** */ + public void testOptimisticTx() throws Exception { + insertObjects(); + runInsideTx(true); + } + + /** */ + public void testNewInstance() throws Exception { + insertObjects(); + runNewInstance(false); + } + + /** */ + public void testNewInstanceIgnoreCache() throws Exception { + insertObjects(); + runNewInstance(true); + } + + /** */ + public void testDeleteInstance() throws Exception { + insertObjects(); + runDeleteInstance(false); + } + + /** */ + public void testDeleteInstanceIgnoreCache() throws Exception { + insertObjects(); + runDeleteInstance(true); + } + + /** */ + protected void runOutsideTx() { + PersistenceManager pm = null; + try { + if (debug) logger.debug("getExtent outside of a transaction"); + pm = pmf.getPersistenceManager(); + getExtent(pm, factory.getPCClass(), numInsert); + } + finally { + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + /** */ + protected void runInsideTx(boolean optimistic) { + PersistenceManager pm = null; + Transaction tx = null; + try { + if (debug) + logger.debug("getExtent in " + (optimistic?"OPTIMISTIC":"DATASTORE") + + " Transaction"); + pm = pmf.getPersistenceManager(); + tx = pm.currentTransaction(); + tx.setOptimistic(optimistic); + tx.begin(); + getExtent(pm, factory.getPCClass(), numInsert); + tx.commit(); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + /** */ + protected void runNewInstance(boolean ignoreCache) { + PersistenceManager pm = null; + Transaction tx = null; + try { + if (debug) + logger.debug("getExtent in with persistence-new instance and ignoreCache " + ignoreCache); + pm = pmf.getPersistenceManager(); + pm.setIgnoreCache(ignoreCache); + tx = pm.currentTransaction(); + tx.begin(); + pm.makePersistent(factory.create(numInsert)); + getExtent(pm, factory.getPCClass(), numInsert + 1); + tx.commit(); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + /** */ + protected void runDeleteInstance(boolean ignoreCache) { + PersistenceManager pm = null; + Transaction tx = null; + try { + if (debug) + logger.debug("getExtent in with persistence-deleted instance and ignoreCache " + ignoreCache); + pm = pmf.getPersistenceManager(); + pm.setIgnoreCache(ignoreCache); + tx = pm.currentTransaction(); + tx.begin(); + Object toBeDeleted = pm.getObjectById(oids.get(oids.size()-1), false); + if (debug) logger.debug("Now delete " + toBeDeleted); + pm.deletePersistent(toBeDeleted); + getExtent(pm, factory.getPCClass(), numInsert - 1); + tx.commit(); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + /** */ + protected void getExtent(PersistenceManager pm, + Class clazz, int expectedCount) { + String className = clazz.getName(); + if (debug) logger.debug("\nEXTENT of " + className); + Extent e = pm.getExtent(clazz, subclasses); + + Map elements = new TreeMap(); + for (Iterator i = e.iterator(); i.hasNext();) { + Object pc = i.next(); + elements.put(JDOHelper.getObjectId(pc), pc); + } + int objCount = 0; + for (Iterator k = elements.values().iterator(); k.hasNext();) { + Object pc = k.next(); + verify(objCount, pc); + if (debug) logger.debug(pc.toString()); + objCount++; + } + if (debug) + logger.debug("extent of " + className + " has " + objCount + + " objects\n"); + assertEquals("extent of " + className + " has wrong number of instances", + expectedCount, objCount); + } + + /** */ + protected Factory getFactory(int verify) { + return getFactoryByClassProperty(verify, "org.apache.jdo.pc.PCPoint"); + } + + /** */ + protected int getDefaultInsert() + { + return 5; + } + + /** */ + protected int getDefaultVerify() + { + return 1; + } + + /** */ + protected void verify(int i, Object pc) { + assertEquals("Wrong instance type", factory.getPCClass(), pc.getClass()); + super.verify(i, pc); + } + +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_FSUID2.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_FSUID2.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_FSUID2.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_FSUID2.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,335 @@ +/* + * 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.test; + +import java.io.EOFException; +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; +import java.lang.reflect.Constructor; +import java.security.AccessController; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; +import javax.jdo.spi.PersistenceCapable; + +import org.apache.jdo.pc.PCPoint; +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Tests that we can insert, fetch, and get extent of instances of 2 distinct +* classes which have the same name. For this test, they are both +* org.apache.jdo.pc.PCPoint, but one of them has 2 coordinates and the +* other has 3. We use our own ClassLoader to be able to load them both in +* the same JVM. +* +* @author Dave Brigstor +*/ +public class Test_FSUID2 extends AbstractTest { + /** Maps class names to classes. */ + // This is only for use byt FSUIDLoader, but inner classes cannot have + // static members, so here it is. + private static final HashMap cache = new HashMap(); + + /** Name of the jarfile property. */ + private static final String JARFILE_PROPERTY = "fsuidjar"; + + /** Default jarfile name, used if property is undefined. */ + private static final String DEFAULT_JARFILE = "fsuid2.jar"; + + /** Name of the jar file for class loading. */ + private String jarfile; + + /** Reflection constructor instance for 3D point class. */ + private Constructor p3Constructor; + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_FSUID2.class); + } + + /** */ + public Test_FSUID2() { + super(); + jarfile = System.getProperty(JARFILE_PROPERTY, DEFAULT_JARFILE); + } + + /** */ + public void test() throws Exception { + insertObjects(); + writeOIDs(); + readObjects(); + } + + /** + * Create and store instances of PCPoint whose classes are loaded from + * different class loaders and have different structure (and hence + * different FOStoreSchemaUID's). + */ + protected void insertObjects() throws Exception { + PersistenceManager pm = null; + Transaction tx = null; + + try { + if (debug) logger.debug("\nINSERT"); + + pm = pmf.getPersistenceManager(); + tx = pm.currentTransaction(); + tx.begin(); + + // Get loader for and load class of 3-D point. + p3Constructor = get3DPointConstructor(); + + Object inserted1[] = new Object[numInsert]; + Object inserted2[] = new Object[numInsert]; + for (int i = 0; i < numInsert; i++) { + // create 2D point + Object pc = new PCPoint(i, new Integer(i)); + if (debug) + logger.debug("cl " + i + " " + pc.getClass().getClassLoader()); + pm.makePersistent(pc); + inserted1[i] = pc; + + // create 3D point + pc = (PersistenceCapable)p3Constructor.newInstance( + new Object[] { + new Integer(i), new Integer(i), new Float(1.0 * i)}); + if (debug) + logger.debug("cl " + i + " " + pc.getClass().getClassLoader()); + pm.makePersistent(pc); + inserted2[i] = pc; + } + + tx.commit(); + + for (int i = 0; i < numInsert; i++) { + announce("inserted ", inserted1[i]); + announce("inserted ", inserted2[i]); + } + + if (debug) logger.debug("inserted " + insertedCount + " objects"); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + /** + * Loads and verifies the pc instances specified by the stored oids. + */ + protected void readObjects() throws Exception { + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + ObjectInputStream in = getOIDFileInputStream(); + tx.begin(); + try { + int count = 0; + while (true) { + Object oid = in.readObject(); + Object pc = pm.getObjectById(oid, true); + if (debug) logger.debug("fetching: " + oid + " -> " + pc); + // The first numInsert instances are supposed to be + // 2D points and the next are supposed to be 3D points. + if (count < numInsert) + verify2DPoint(count, pc); + else + verify3DPoint(count - numInsert, pc); + count++; + } + } + catch (EOFException ex) { + // OK + } + tx.commit(); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + /** */ + private void verify2DPoint(int i, Object pc) { + Object expected = new PCPoint(i, new Integer(i)); + assertEquals("Wrong 2D point instance", expected, pc); + } + + /** */ + private void verify3DPoint(int i, Object pc) throws Exception { + Object expected = p3Constructor.newInstance(new Object[] { + new Integer(i), new Integer(i), new Float(1.0 * i)}); + assertEquals("Wrong 2D point instance", expected, pc); + } + + /** */ + protected int getDefaultInsert() + { + return 4; + } + + private Constructor get3DPointConstructor() throws Exception { + try { + return (Constructor)AccessController.doPrivileged( + new PrivilegedExceptionAction () { + public Object run () throws Exception { + FSUIDLoader l = new FSUIDLoader(jarfile); + Class p3Class = l.loadClass("org.apache.jdo.pc.PCPoint", true); + return p3Class.getDeclaredConstructor( + new Class[] { int.class, Integer.class, float.class }); + }}); + } + catch (PrivilegedActionException ex) { + // unwrap FileNotFoundException + throw ex.getException(); + } + } + + /** + * Expects to load classes from within a specified jar file. + */ + class FSUIDLoader extends ClassLoader { + /** Name of the jar file for class loading. */ + private final String filename; + + FSUIDLoader(String filename) { + this.filename = filename; + } + + public InputStream getResourceAsStream(String name) { + if (!name.startsWith("org/apache/jdo/pc")) { + return getSystemResourceAsStream(name); + } + + try { + JarFile jarFile = getJarFile(); + JarEntry entry = jarFile.getJarEntry(name); + if (entry != null) { + return jarFile.getInputStream(entry); + } + } + catch (IOException ex) { + ex.printStackTrace(); + } + return null; + } + + public synchronized Class loadClass(String name, boolean resolve) + throws ClassNotFoundException { + + Class c = (Class)cache.get(name); + + try { + if (null == c && ! name.startsWith("org.apache.jdo.pc")) { + try { + c = findSystemClass(name); + } catch (ClassNotFoundException ex) { + ; + } catch (NoClassDefFoundError ex) { + ; + } + } + + if (null == c) { + byte b[] = loadClassData(name); + if (null == b) { + throw new NoClassDefFoundError(name); + } + c = defineClass(name, b, 0, b.length); + cache.put(name, c); + } + if (resolve) { + resolveClass(c); + } + } catch (NoClassDefFoundError ex) { + throw new ClassNotFoundException("loadClass " + name, ex); + } + return c; + } + + private byte[] loadClassData(String name) + throws ClassNotFoundException { + byte rc[] = null; + + // Class names in jar files have file separators...I hope they're + // the same on Un*x and Windows. + name = name.replace('.', '/') + ".class"; + try { + JarFile jf = getJarFile(); + for (Enumeration e = jf.entries(); e.hasMoreElements();) { + JarEntry je = (JarEntry)e.nextElement(); + if (name.equals(je.getName())) { + InputStream is = jf.getInputStream(je); + int avail = is.available(); + if (-1 != avail) { + int size = avail; + rc = new byte[size]; + int off = 0; + int count = 0; + while (size > 0) { + count = is.read(rc, off, size); + if (count <= 0) { + is.close(); + break; + } + off += count; + size -= count; + } + if (off != avail) { + throw new IOException( + "failed to read complete class " + name); + } + } + break; + } + } + } catch (IOException ex) { + ex.printStackTrace(); + throw new ClassNotFoundException("loadClassData " + name, ex); + } + return rc; + } + + private JarFile getJarFile() throws IOException { + try { + return (JarFile)AccessController.doPrivileged( + new PrivilegedExceptionAction () { + public Object run () throws IOException { + return new JarFile(filename); + }}); + } + catch (PrivilegedActionException ex) { + // unwrap FileNotFoundException + throw (IOException)ex.getException(); + } + } + + } + +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Fetch.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Fetch.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Fetch.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Fetch.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,56 @@ +/* + * 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.test; + +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; + +import org.apache.jdo.test.util.Factory; +import org.apache.jdo.pc.PointFactory; + +/** +* This test is similar to Test_ActivateClass, but it adds extra steps of +* getting OIDs for objects and later retrieving those objects. +* +* @author Dave Bristor +*/ +public class Test_Fetch extends AbstractTest { + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_Fetch.class); + } + + /** */ + public void test() throws Exception { + insertObjects(); + readObjects(); + } + + /** */ + protected Factory getFactory(int verify) { + PointFactory rc = new PointFactory(); + // verify in any case + rc.setVerify(verify); + return rc; + } + + /** */ + protected int getDefaultVerify() { + return 1; + } + +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Fetch2.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Fetch2.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Fetch2.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Fetch2.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,44 @@ +/* + * 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.test; + +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* This test is similar to Test_ActivateClass, but it adds extra steps of +* getting OIDs for objects and later retrieving those objects. Unlike +* TestFetch, this test inserts more objects. +* +* @author Dave Bristor +*/ +public class Test_Fetch2 extends Test_Fetch { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_Fetch2.class); + } + + // The idea is that we're going to write a bunch of stuff to a data + // output stream, then read it back in; we should get the same data + // back. + public void test() throws Exception { + super.test(); + + insertObjects(); // Do another insertion. + readObjects(); + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_FetchInserted.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_FetchInserted.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_FetchInserted.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_FetchInserted.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,113 @@ +/* + * 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.test; + +import java.io.EOFException; +import java.io.ObjectInputStream; + +import javax.jdo.JDOObjectNotFoundException; +import javax.jdo.PersistenceManager; + +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.Factory; +import org.apache.jdo.test.util.JDORITestRunner; + + +/** +* This test is similar to Test_ActivateClass, but it adds extra steps of +* getting OIDs for objects and later retrieving those objects. Unlike +* Test_Fetch and Test_Fetch2, this should be run in a separate JVM +* <i>after</i> a first JVM has inserted some objects. +* +* Note that by default, this fetches inserted PCPoints...to test other kinds of +* objects, use -Dclass=XXX on the java command line. +* +* @author Dave Bristor +*/ +public class Test_FetchInserted extends AbstractTest { + /** By default, we expect that the instances for which we have oids should + * exist in the database. But if shouldExist is false, then we expect + * that they should not exist. + */ + private final boolean shouldExist; + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_FetchInserted.class); + } + + /** */ + public Test_FetchInserted() { + super(); + existing = true; // Must use existing database! + shouldExist = Boolean.getBoolean("shouldExist"); + } + + // The idea is that we're going to write a bunch of stuff to a data + // output stream, then read it back in; we should get the same data + // back. + public void test() throws Exception { + readObjects(); + } + + protected void readObjects() throws Exception { + PersistenceManager pm = pmf.getPersistenceManager(); + try { + ObjectInputStream in = getOIDFileInputStream(); + if (debug) logger.debug("\nFETCH"); + + int count = 0; + while (true) { + Object oid = null; + try { + oid = in.readObject(); + if (debug) logger.debug("fetching: " + oid); + Object pc = pm.getObjectById(oid, true); + if (!shouldExist) { + fail("fetched " + oid + + " but it should not have been in database"); + } else { + if (debug) logger.debug("After fetch: " + pc); + verify(count++, pc); + } + } catch (JDOObjectNotFoundException ex) { + if (shouldExist) { + fail("should find object with ObjectId " + oid + ": " + ex); + } else { + if (debug) + logger.debug(oid.toString() + + " does not exist in database, as expected"); + } + } + } + } catch (EOFException ex) { + // OK + } + finally { + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + /** + * Determines the kind of objects that are inserted. Override this if + * you want to insert some other kind of object. + */ + protected Factory getFactory(int verify) { + return getFactoryByClassProperty(verify, "org.apache.jdo.pc.PCPoint"); + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Freezer.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Freezer.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Freezer.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Freezer.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,221 @@ +/* + * 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. + */ + +/* + * Test_Freezer.java + * + * Created on April 12, 2003, 4:15 PM + */ + +package org.apache.jdo.test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.jdo.impl.sco.Freezer; + +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; + +/** + * + * @author Craig Russell + * @version 1.0.1 + */ +public class Test_Freezer extends AbstractTest { + + /** */ + static private final short s33 = 33; + + /** */ + private boolean verbose; + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_Freezer.class); + } + + /** */ + protected void setUp() { } + + /** */ + protected void tearDown() { } + + /** */ + public void testSet() { + Set ts = new HashSet(); + ts.add(new Integer(12)); + ts.add(new Integer(12)); + ts.add(new Integer(12)); + ts.add(new Integer(12)); + ts.add(new java.util.Date(2000)); + ts.add(new Float(2000)); + ts.add(new Float(2020)); + ts.add(new Float(2000.1)); + ts.add(new Integer(112)); + ts.add(new Integer(212)); + ts.add(new Float(2000.001)); + ts.add(new Float(2000.00001)); + ts.add(new Short(s33)); + ts.add(new Integer(2)); + ts.add(new Long(3456789L)); + ts.add(new java.util.Date(1000)); + List expected = new ArrayList(); + expected.add(new Float(2000)); + expected.add(new Float(2000.001)); + expected.add(new Float(2000.1)); + expected.add(new Float(2020)); + expected.add(new Integer(2)); + expected.add(new Integer(12)); + expected.add(new Integer(112)); + expected.add(new Integer(212)); + expected.add(new Long(3456789L)); + expected.add(new Short(s33)); + expected.add(new java.util.Date(1000)); + expected.add(new java.util.Date(2000)); + + // now freeze the set + Object[] ordered = Freezer.freeze(ts, ts.size()); + List al = Arrays.asList(ordered); + if (verbose) + for (Iterator it=al.iterator(); it.hasNext();) { + Object o = it.next(); + System.out.println(o.getClass().getName() + ":" + o.toString()); + } + assertEquals("freezing a Set returned unexpected result", expected, al); + } + + /** */ + public void testMap() { + Map hm = new HashMap(); + hm.put(new Integer(22), new Integer(1000)); + hm.put(new java.sql.Time(2004), new Integer(6)); + hm.put(new Integer(22), new Integer(1000)); + hm.put(new Integer(22), new Integer(1000)); + hm.put(new Integer(22), new Integer(16)); + hm.put(new java.sql.Date(2001), new Integer(2)); + hm.put(new java.util.Date(2000), new Integer(1)); + hm.put(new Float(2000.), new Integer(10)); + hm.put(new Float(2020), new Integer(14)); + hm.put(new org.apache.jdo.impl.sco.SqlTime(3000), new Integer(7)); + hm.put(new java.sql.Date(2002), new Integer(3)); + hm.put(new Float(2000.1), new Integer(13)); + hm.put(new Integer(112), new Integer(17)); + hm.put(new org.apache.jdo.impl.sco.SqlDate(3004), new Integer(5)); + hm.put(new Integer(212), new Integer(18)); + hm.put(new java.sql.Date(2003), new Integer(4)); + hm.put(new Float(2000.001), new Integer(11)); + hm.put(new Float(2000.01), new Integer(12)); + hm.put(new java.sql.Timestamp(2004), new Integer(8)); + hm.put(new org.apache.jdo.impl.sco.SqlTimestamp(3002), new Integer(9)); + hm.put(new Short(s33), new Integer(20)); + hm.put(new Integer(2), new Integer(15)); + hm.put(new Long(3456789L), new Integer(19)); + hm.put(new java.util.Date(1000), new Integer(0)); + + List expected = new ArrayList(); + expected.add(new SimpleEntry(new Float(2000.), new Integer(10))); + expected.add(new SimpleEntry(new Float(2000.001), new Integer(11))); + expected.add(new SimpleEntry(new Float(2000.01), new Integer(12))); + expected.add(new SimpleEntry(new Float(2000.1), new Integer(13))); + expected.add(new SimpleEntry(new Float(2020), new Integer(14))); + expected.add(new SimpleEntry(new Integer(2), new Integer(15))); + expected.add(new SimpleEntry(new Integer(22), new Integer(16))); + expected.add(new SimpleEntry(new Integer(112), new Integer(17))); + expected.add(new SimpleEntry(new Integer(212), new Integer(18))); + expected.add(new SimpleEntry(new Long(3456789L), new Integer(19))); + expected.add(new SimpleEntry(new Short(s33), new Integer(20))); + expected.add(new SimpleEntry(new java.util.Date(1000), new Integer(0))); + expected.add(new SimpleEntry(new java.util.Date(2000), new Integer(1))); + expected.add(new SimpleEntry(new java.sql.Date(2001), new Integer(2))); + expected.add(new SimpleEntry(new java.sql.Date(2002), new Integer(3))); + expected.add(new SimpleEntry(new java.sql.Date(2003), new Integer(4))); + expected.add(new SimpleEntry(new org.apache.jdo.impl.sco.SqlDate(3004), new Integer(5))); + expected.add(new SimpleEntry(new java.sql.Time(2004), new Integer(6))); + expected.add(new SimpleEntry(new org.apache.jdo.impl.sco.SqlTime(3000), new Integer(7))); + expected.add(new SimpleEntry(new java.sql.Timestamp(2004), new Integer(8))); + expected.add(new SimpleEntry(new org.apache.jdo.impl.sco.SqlTimestamp(3002), new Integer(9))); + + Object[] ordered = Freezer.freeze(hm, hm.size()); + List al = Arrays.asList(ordered); + if (verbose) + for (Iterator it=al.iterator(); it.hasNext();) { + Map.Entry o = (Map.Entry)it.next(); + Object key = o.getKey(); + Object value = o.getValue(); + System.out.println(" " + value + " " + key.getClass().getName() + ":" + key.toString()); + } + assertEquals("freezing a Map returned unexpected result", expected, al); + } + + /** */ + static class SimpleEntry implements Map.Entry { + Object key; + Object value; + + public SimpleEntry(Object key, Object value) { + this.key = key; + this.value = value; + } + + public SimpleEntry(Map.Entry e) { + this.key = e.getKey(); + this.value = e.getValue(); + } + + public Object getKey() { + return key; + } + + public Object getValue() { + return value; + } + + public Object setValue(Object value) { + Object oldValue = this.value; + this.value = value; + return oldValue; + } + + public boolean equals(Object o) { + if (!(o instanceof Map.Entry)) + return false; + Map.Entry e = (Map.Entry)o; + return eq(key, e.getKey()) && eq(value, e.getValue()); + } + + public int hashCode() { + Object v; + return ((key == null) ? 0 : key.hashCode()) ^ + ((value == null) ? 0 : value.hashCode()); + } + + public String toString() { + return key + "=" + value; + } + + private static boolean eq(Object o1, Object o2) { + return (o1 == null ? o2 == null : o1.equals(o2)); + } + } + +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_GetObjectById.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_GetObjectById.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_GetObjectById.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_GetObjectById.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,88 @@ +/* + * 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.test; + +import javax.jdo.JDOUserException; +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.pc.PointFactory; +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.Factory; +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* This test is similar to Test_ActivateClass, but it adds extra steps of +* getting OIDs for objects and later retrieving those objects. +* +* @author Dave Bristor +*/ +public class Test_GetObjectById extends AbstractTest { + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_GetObjectById.class); + } + + // The idea is that we're going to write a bunch of stuff to a data + // output stream, then read it back in; we should get the same data + // back. + public void test() throws Exception { + PersistenceManager pm = pmf.getPersistenceManager(); + try { + Object o = new Object(); + Object o2 = pm.getObjectById(o, false); + throw new RuntimeException("Expected exception not caught from getObjectById()"); + } catch (JDOUserException ex) { + // this is expected + } + finally { + pm.close(); + } + + pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + tx.begin(); + + Object inserted[] = new Object[numInsert]; + for (int i = 0; i < numInsert; i++) { + Object pc = factory.create(i); + pm.makePersistent(pc); + inserted[i] = pc; + + Object oid = pm.getObjectId(pc); + Object obj = pm.getObjectById(oid, false); + announce("GetObjectId for " + oid + ", obj is " + obj + ", pc is ", pc); + assertSame(("object mismatch at " + i), pc, obj); + } + + tx.commit(); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + protected Factory getFactory(int verify) { + PointFactory rc = new PointFactory(); + rc.setVerify(verify); + return rc; + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_GetObjectById2.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_GetObjectById2.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_GetObjectById2.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_GetObjectById2.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,88 @@ +/* + * 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.test; + +import java.io.EOFException; +import java.io.File; +import java.io.FileInputStream; +import java.io.ObjectInputStream; + +import javax.jdo.PersistenceManager; + +import org.apache.jdo.impl.fostore.FOStorePMF; +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; + +/** + * This is similar to Test_GetObjectById except that it is much simpler. It + * explicitly does not use any features inherited from AbstractTest other than + * creating an url and accessing system properties. It ensures that we can run + * a user application which does not explicitly load a PersistenceCapable class + * but can nonetheless fetch instances of that class by OID. + * + * @author Dave Bristor + */ +public class Test_GetObjectById2 extends AbstractTest { + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_GetObjectById2.class); + } + + /** */ + protected void setUp() { } + + /** */ + protected void tearDown() { } + + /** */ + public void test () throws Exception { + File f = new File(dbName + ".oid"); + assertTrue("Test_GetObjectById2: " + dbName + ".oid does not exist", f.exists()); + + FOStorePMF pmf = new FOStorePMF(); + pmf.setConnectionCreate(false); + pmf.setConnectionUserName(System.getProperty ("user", "fred")); + pmf.setConnectionPassword(System.getProperty ("password", "wombat")); + + String url = createURL(); + pmf.setConnectionURL(url); + + PersistenceManager pm = pmf.getPersistenceManager(); + + try { + ObjectInputStream in = new ObjectInputStream( + new FileInputStream(f)); + if (debug) System.out.println("\nFETCH"); + + int count = 0; + while (true) { + Object oid = in.readObject(); + if (debug) + System.out.println("Test_GetObjectById2: getting object by id: " + oid); + Object pc = pm.getObjectById(oid, true); + if (debug) + System.out.println("After getObjectById: " + pc); + } + } catch (EOFException ex) { + // OK + } + finally { + if (pm != null && !pm.isClosed()) + pm.close(); + } + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_HollowUpdate.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_HollowUpdate.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_HollowUpdate.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_HollowUpdate.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,151 @@ +/* + * 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.test; + +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.TimeZone; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.pc.PCPoint; +import org.apache.jdo.pc.empdept.PCPerson; +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Tests that we can do update of a hollow instance in Optimistic transaction. +* +* @author Marina Vatkina +*/ +public class Test_HollowUpdate extends AbstractTest { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_HollowUpdate.class); + } + + /** */ + private static Date date; + + /** */ + static { + GregorianCalendar calendarDate = + new GregorianCalendar(TimeZone.getTimeZone("America/New_York")); + calendarDate.set(1969, 7, 20); + date = calendarDate.getTime(); + } + + /** */ + public void testPCPoint() { + insertPCPoint(); + checkExtent(PCPoint.class, 1); + } + + /** */ + public void testPCPerson() { + insertPCPerson(); + checkExtent(PCPerson.class, 1); + } + + /** */ + private void insertPCPoint() { + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + if (debug) logger.debug("\nUPDATE HOLLOW"); + + tx.setRetainValues(false); + tx.setOptimistic(true); + PCPoint p = new PCPoint(0xaa, 0xcc); + + tx.begin(); + if (debug) logger.debug("Transaction begin OK"); + pm.makePersistent(p); + if (debug) logger.debug("Make Persistent OK, p is " + p); + tx.commit(); + if (debug) logger.debug("Transaction commit OK"); + + tx.begin(); + if (debug) logger.debug("Transaction begin OK"); + p.setY(new Integer(0xee)); + Object oid = pm.getObjectId(p); + if (debug) logger.debug("Updated to " + p); + tx.commit(); tx = null; + if (debug) logger.debug("Transaction commit OK, p is " + p); + pm.close(); pm =null; + + // check state of object in with pm + pm = pmf.getPersistenceManager(); + tx = pm.currentTransaction(); + tx.begin(); + PCPoint p2 = (PCPoint)pm.getObjectById(oid, true); + assertEquals("Field y has wrong value", new Integer(0xee), p2.getY()); + tx.commit(); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + /** */ + public void insertPCPerson() { + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + if (debug) logger.debug("\nUPDATE HOLLOW"); + + tx.setRetainValues(false); + tx.setOptimistic(true); + PCPerson p = new PCPerson("FFFFFF", "LLLL", date); + + tx.begin(); + if (debug) logger.debug("Transaction begin OK"); + pm.makePersistent(p); + if (debug) logger.debug("Make Persistent OK, p is " + p); + tx.commit(); + if (debug) logger.debug("Transaction commit OK"); + + tx.begin(); + if (debug) logger.debug("Transaction begin OK"); + p.setLastname("xxxxxx"); + Object oid = pm.getObjectId(p); + if (debug) logger.debug("Updated to " + p); + tx.commit(); tx = null; + if (debug) logger.debug("Transaction commit OK, p is " + p); + pm.close(); pm = null; + + // check state of object in with pm + pm = pmf.getPersistenceManager(); + tx = pm.currentTransaction(); + tx.begin(); + PCPerson p2 = (PCPerson)pm.getObjectById(oid, true); + assertEquals("Field lastname has wrong value", "xxxxxx", p2.getLastname()); + tx.commit(); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Inheritance.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Inheritance.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Inheritance.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Inheritance.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,166 @@ +/* + * 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.test; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.TreeSet; + +import javax.jdo.Extent; +import javax.jdo.PersistenceManager; + +import org.apache.jdo.pc.empdept.PCDepartment; +import org.apache.jdo.pc.empdept.PCEmployee; +import org.apache.jdo.pc.empdept.PCFullTimeEmployee; +import org.apache.jdo.pc.empdept.PCInsurance; +import org.apache.jdo.pc.empdept.PCPartTimeEmployee; +import org.apache.jdo.pc.empdept.PCProject; +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Test a PC that has some fields which are PC's. +*/ +public class Test_Inheritance extends EmpDeptSupport { + + Map emps = new HashMap(); + Map depts = new HashMap(); + Map inss = new HashMap(); + Map projs = new HashMap(); + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_Inheritance.class); + } + + // The idea is that we're going to write a bunch of stuff to a data + // output stream, then read it back in; we should get the same data + // back. + public void test() throws Exception + { + insertObjects(); + getEmpExtent(); + } + + // The above test inserted a bunch of instances and committed them. We + // now try to load the extent of Employee and navigate to all associated + // instances. + protected void getEmpExtent() { + PersistenceManager pm = null; + try { + if (debug) logger.debug("\ngetEmpExtent"); + pm = pmf.getPersistenceManager(); + Extent extent; + Iterator it; + extent = pm.getExtent (PCEmployee.class, true); + it = extent.iterator(); + + // Put the printed version of each emp in the extent into a SortedSet + TreeSet sortedEmps = new TreeSet(); + + while (it.hasNext()) { + PCEmployee e = (PCEmployee)it.next(); + sortedEmps.add("from extent of PCEmployee: " + e); + checkEmployee(e); + } + + // Print out the extent in sorted order + if (debug) + for (Iterator i = sortedEmps.iterator(); i.hasNext();) { + logger.debug((String)i.next()); + } + + PCFullTimeEmployee scott = (PCFullTimeEmployee) emps.get (new Long (1L)); + PCPartTimeEmployee ed = (PCPartTimeEmployee) emps.get (new Long (100L)); + PCDepartment board = (PCDepartment) depts.get (new Long (100L)); + PCDepartment emg = (PCDepartment) depts.get (new Long (200L)); + PCInsurance scottIns = (PCInsurance) inss.get (new Long (1000L)); + PCInsurance edIns = (PCInsurance) inss.get (new Long (1001L)); + PCProject solaris = (PCProject) projs.get (new Long (1L)); + PCProject sparc = (PCProject) projs.get (new Long (2L)); + + String repr = null; + repr = scott.toString(); + if (debug) logger.debug(repr); + assertEquals("Wrong string representation of scott", + "PCFullTimeEmployee: PCEmployee: PCPerson: McNealy, Scott, born 20/Aug/1969, id=1, hired 5/Jun/1982 manager: none dept: board emps: 1 insurance: Aetna $200000.0", + repr); + repr = ed.toString(); + if (debug) logger.debug(repr); + assertEquals("Wrong string representation of ed", + "PCPartTimeEmployee: PCEmployee: PCPerson: Zander, Ed, born 8/May/1960, id=100, hired 3/Mar/1985 manager: McNealy dept: emg emps: 0 insurance: BlueCross $400000.0", + repr); + repr = board.toString(); + if (debug) logger.debug(repr); + assertEquals("Wrong string representation of board", + "Dept: board, id=100, emps: 1", + repr); + repr = emg.toString(); + if (debug) logger.debug(repr); + assertEquals("Wrong string representation of emg", + "Dept: emg, id=200, emps: 1", + repr); + repr = scottIns.toString(); + if (debug) logger.debug(repr); + assertEquals("Wrong string representation of scotts insurance", + "Ins: Aetna, id=1000, emp McNealy", + repr); + repr = edIns.toString(); + if (debug) logger.debug(repr); + assertEquals("Wrong string representation of eds insurance", + "Ins: BlueCross, id=1001, emp Zander", + repr); + repr = solaris.toString(); + if (debug) logger.debug(repr); + assertEquals("Wrong string representation of project solaris", + "PCProject: Solaris, id=1, emps: 2", + repr); + repr = sparc.toString(); + if (debug) logger.debug(repr); + assertEquals("Wrong string representation of project sparc", + "PCProject: Sparc, id=2, emps: 1", + repr); + } + finally { + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + protected void checkEmployee (PCEmployee e) { + emps.put (new Long(e.getEmpid()), e); + for (Iterator it = e.getProjects().iterator(); it.hasNext();) { + PCProject proj = (PCProject)it.next(); + checkProject (proj); + } + checkDepartment ((PCDepartment) e.getDepartment()); + checkInsurance ((PCInsurance) e.getInsurance()); + } + + protected void checkDepartment (PCDepartment d) { + depts.put (new Long(d.getDeptid()), d); + } + + protected void checkProject (PCProject p) { + projs.put (new Long(p.getProjid()), p); + } + + protected void checkInsurance (PCInsurance i) { + inss.put (new Long(i.getInsid()), i); + } + +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Insert.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Insert.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Insert.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Insert.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,85 @@ +/* + * 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.test; + +import org.apache.jdo.pc.PointFactory; +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.Factory; +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Tests that we can insert objects into a database. Creates objects as per +* Test_ActivateClass, and writes their OIDs into a file. +* Test_FetchInserted reads that file, and fetches the objects by OID. +* +* @author Dave Bristor +*/ +public class Test_Insert extends AbstractTest { + + /** */ + private static boolean silent = false; + + /** */ + public static void main(String args[]) { + handleArgs(args); + if (silent) + runSilentInsert(); + else + JDORITestRunner.run(Test_Insert.class); + } + + /** Write the OIDs of the objects that were inserted to a file dbName.oid. + */ + public void test() throws Exception + { + insertObjects(); + checkExtent(factory.getPCClass(), numInsert); + writeOIDs(); + } + + /** */ + protected Factory getFactory(int verify) { + PointFactory rc = new PointFactory(); + rc.setVerify(verify); + return rc; + } + + /** */ + private static void runSilentInsert() { + try { + Test_Insert insert = new Test_Insert(); + insert.setupPMF(); + insert.test(); + insert.closePMF(); + } + catch (Exception ex) { + System.out.println("Excetion during insert"); + ex.printStackTrace(); + } + } + + /** */ + private static void handleArgs(String[] args) { + for (int i = 0; i < args.length; i++) { + if ("-silent".equals(args[i])) + silent = true; + else + System.err.println("Test_Insert: ignoring unknon option" + args[i]); + } + } + +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Insert2.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Insert2.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Insert2.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Insert2.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,102 @@ +/* + * 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.test; + +import javax.jdo.JDOException; +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.pc.PCPoint; +import org.apache.jdo.pc.PointFactory; +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.Factory; +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Tests that we can rollback an insert and commit the same insert in the +* next transaction. +* +* @author Marina Vatkina +*/ +public class Test_Insert2 extends AbstractTest { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_Insert2.class); + } + + /** */ + public void test() throws Exception { + insertObjects(); + checkExtent(factory.getPCClass(), 1); + readObjects(); + } + + /** Tries to insert an object rolled back in a previous tx. */ + protected void insertObjects() throws Exception { + PersistenceManager pm = null; + Transaction tx = null; + try { + if (debug) logger.debug("\nINSERT-ROLLBACK-INSERT-COMMIT"); + pm = pmf.getPersistenceManager(); + tx = pm.currentTransaction(); + PCPoint p = new PCPoint(11, 111); + tx.begin(); + if (debug) logger.debug("Transaction begin OK"); + pm.makePersistent(p); + if (debug) logger.debug("Make Persistent OK"); + tx.rollback(); + if (debug) logger.debug("Transaction rollback OK"); + + tx.begin(); + if (debug) logger.debug("Transaction begin OK"); + pm.makePersistent(p); + if (debug) logger.debug("Make Persistent OK"); + + // This try-catch block is usefull for internal testing only. + // It should not affect this test in a regular test runs. + try { + tx.commit(); + if (debug) logger.debug("Transaction commit OK"); + } catch (JDOException e) { + if (debug) logger.debug("Transaction active: " + tx.isActive()); + throw e; + } + announce("inserted ", p); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + /** */ + protected Factory getFactory(int verify) { + PointFactory rc = new PointFactory(); + rc.setVerify(verify); + return rc; + } + + /** */ + protected void verify(int i, Object pc) { + assertEquals("wrong PCPoint instance", new PCPoint(11, 111), pc); + } + +} + Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Interfaces.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Interfaces.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Interfaces.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Interfaces.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,80 @@ +/* + * 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.test; + +import javax.jdo.JDOHelper; +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.pc.PCInterfaces; +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Tests that arrays can be stored in the datastore. +* +* @author Dave Bristor +*/ +public class Test_Interfaces extends Test_Fetch { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_Interfaces.class); + } + + /** */ + public void test() throws Exception { + insertObjects(); + readObjects(); + checkExtent(PCInterfaces.class, 1); + } + + // We override this from Test_Fetch and insert our own objects + protected void insertObjects() { + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + tx.begin(); + PCInterfaces pcInterfaces = new PCInterfaces(); + pcInterfaces.init(); + if (debug) logger.debug("Before insert: " + pcInterfaces); + pm.makePersistent(pcInterfaces); + tx.commit(); + + Object oid1 = JDOHelper.getObjectId(pcInterfaces); + if (debug) logger.debug("inserted pcInterfaces: " + oid1); + oids.add(oid1); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + /** + * redefine verify called by readObjects to check whether the read + * instance is correct. + */ + protected void verify(int i, Object pc) { + if (i > 0) + fail("Wrong number of inserted objects, expected only one"); + PCInterfaces expected = new PCInterfaces(); + expected.init(); + assertEquals("Wrong instance returned from datastore", expected, pc); + } +}