openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From p..@apache.org
Subject svn commit: r627979 [8/39] - in /openjpa/trunk: openjpa-lib/src/test/java/org/apache/openjpa/lib/test/ openjpa-persistence-jdbc/ openjpa-persistence-jdbc/src/test/java/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/ openjpa-pers...
Date Fri, 15 Feb 2008 09:20:40 GMT
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestDataCacheScheduler.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestDataCacheScheduler.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestDataCacheScheduler.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestDataCacheScheduler.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,228 @@
+/*
+ * 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.openjpa.persistence.datacache;
+
+import java.util.Calendar;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+
+import org.apache.openjpa.persistence.datacache.common.apps.ScheduledEviction;
+import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
+import org.apache.openjpa.conf.OpenJPAConfiguration;
+import org.apache.openjpa.datacache.DataCache;
+import org.apache.openjpa.datacache.DataCacheScheduler;
+import org.apache.openjpa.datacache.ConcurrentDataCache;
+import org.apache.openjpa.persistence.JPAFacadeHelper;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
+import org.apache.openjpa.persistence.OpenJPAPersistence;
+import org.apache.openjpa.util.Id;
+
+public class TestDataCacheScheduler
+    extends AbstractTestCase {
+
+    private static final String MINUTES = getMinutes();
+
+    public TestDataCacheScheduler(String str) {
+        super(str, "datacachecactusapp");
+    }
+
+    private static String getMinutes() {
+        StringBuffer buf = new StringBuffer();
+        for (int i = 0; i < 60; i++) {
+            if (i % 2 == 0)
+                buf.append(i).append(',');
+        }
+        return buf.toString();
+    }
+
+    public void setUp() {
+        deleteAll(ScheduledEviction.class);
+    }
+
+    public void testRuntime()
+        throws Exception {
+        String sched = MINUTES + " * * * *";
+        Map propsMap = new HashMap();
+        propsMap
+            .put("openjpa.DataCache", "true(EvictionSchedule=\"" + sched + "\")");
+        propsMap.put("openjpa.RemoteCommitProvider", "sjvm");
+        propsMap.put("openjpa.FlushBeforeQueries", "true");
+        propsMap.put("openjpa.BrokerImpl", CacheTestBroker.class.getName());
+        OpenJPAEntityManagerFactory emf =
+            (OpenJPAEntityManagerFactory) getEmf(propsMap);
+
+        ((OpenJPAEntityManagerFactorySPI) OpenJPAPersistence.cast(emf))
+            .getConfiguration().getDataCacheManagerInstance()
+            .getDataCacheScheduler().setInterval(1);
+        DataCache cache = JPAFacadeHelper.getMetaData(emf,
+            ScheduledEviction.class).getDataCache();
+
+        OpenJPAEntityManager em = (OpenJPAEntityManager) emf
+            .createEntityManager();
+        startTx(em);
+        ScheduledEviction pc = new ScheduledEviction("Foo");
+        em.persist(pc);
+        Object oid = em.getObjectId(pc);
+        Object oidwithclass = new Id(ScheduledEviction.class, oid.toString());
+        endTx(em);
+        endEm(em);
+
+        cache.clear();// clear and wait until next run.
+        Calendar cal = Calendar.getInstance();
+        cal.setTime(new Date());
+        if (cal.get(Calendar.MINUTE) % 2 == 0)
+            Thread.currentThread().sleep
+                ((60 - cal.get(Calendar.SECOND)) * 1000);
+        cal.setTime(new Date());
+        assertTrue(cal.get(Calendar.MINUTE) % 2 == 1);
+        em = (OpenJPAEntityManager) emf.createEntityManager();
+        em.find(ScheduledEviction.class, oid);
+        endEm(em);
+        assertTrue(cache.contains(oidwithclass));
+
+        Thread.currentThread().sleep(130 * 1000);
+        assertFalse(cache.contains(oidwithclass));
+        emf.close();
+    }
+
+    /**
+     * too slow ! *
+     */
+    //FIXME Seetha Sep 26,2006
+    /*public void XXXtestRunnable()
+        throws Exception {
+        KodoPersistenceManager pm = getPM();
+        OpenJPAConfiguration conf = pm.getConfiguration();
+        DataCacheScheduler scheduler = new DataCacheScheduler(conf);
+        scheduler.setInterval(1);
+
+        Calendar cal = Calendar.getInstance();
+        cal.setTime(new Date());
+        int minute = (cal.get(Calendar.MINUTE) + 2) % 60;
+        StringBuffer sched = new StringBuffer();
+        sched.append(minute).append(' ');
+        sched.append("* ");
+        sched.append("* ");
+        sched.append("* ");
+        sched.append("* ");
+        DummyCache cache = new DummyCache();
+        scheduler.scheduleEviction(cache, sched.toString());
+
+        Thread thread = new Thread(scheduler);
+        thread.setDaemon(true);
+        thread.start();
+        // test that it did not run yet...
+        Thread.currentThread().sleep(90 * 1000); // 90 seconds
+        assertEquals(0, cache.clearCount);
+        // test that it ran...
+        Thread.currentThread().sleep(45 * 1000); // 45 seconds
+        assertEquals(1, cache.clearCount);
+        // test that it wasn't too eager
+        Thread.currentThread().sleep(50 * 1000); // 90 seconds
+        assertEquals(1, cache.clearCount);
+        scheduler.stop();
+    }*/
+
+    /**
+     * too slow *
+     */
+    /* public void XXXtestMonth()
+        throws Exception {
+        Calendar cal = Calendar.getInstance();
+        cal.setTime(new Date());
+        int month = cal.get(Calendar.MONTH);
+        int month2 = month + 1;
+        if (month2 > 12)
+            month2 = 1;
+        doTest("* * " + month + " *", "* * " + month2 + " *");
+    }*/
+
+    /**
+     * too slow *
+     */
+    /* public void XXXtestDayOfMonth()
+        throws Exception {
+        Calendar cal = Calendar.getInstance();
+        cal.setTime(new Date());
+        int dom = cal.get(Calendar.DAY_OF_MONTH);
+        doTest("* " + dom + " * *", "* " + (dom % 12 + 1) + " * *");
+    }*/
+    public void testDayOfWeek()
+        throws Exception {
+        Calendar cal = Calendar.getInstance();
+        cal.setTime(new Date());
+        int day = cal.get(Calendar.DAY_OF_WEEK);
+        doTest("* * * " + day, "* * * " + (day % 7 + 1));
+    }
+
+    public void testHour()
+        throws Exception {
+        Calendar cal = Calendar.getInstance();
+        cal.setTime(new Date());
+        int hour = cal.get(Calendar.HOUR_OF_DAY);
+        doTest(hour + " * * *", ((hour + 1) % 24) + " * * *");
+    }
+
+    /**
+     * Pass in 4 out of 5 tokens.
+     */
+    private void doTest(String valid, String invalid)
+        throws Exception {
+
+        OpenJPAEntityManagerFactory emf =
+            (OpenJPAEntityManagerFactory) getEmf();
+        OpenJPAConfiguration conf =
+            ((OpenJPAEntityManagerFactorySPI) OpenJPAPersistence.cast(emf))
+                .getConfiguration();
+
+        DataCacheScheduler scheduler = new DataCacheScheduler(conf);
+        scheduler.setInterval(1);
+
+        Calendar cal = Calendar.getInstance();
+        cal.setTime(new Date());
+        String sched = ((cal.get(Calendar.MINUTE) + 1) % 60) + " ";
+        DummyCache validCache = new DummyCache();
+        scheduler.scheduleEviction(validCache, sched + valid);
+        DummyCache invalidCache = new DummyCache();
+        scheduler.scheduleEviction(invalidCache, sched + invalid);
+        Thread thread = new Thread(scheduler);
+        thread.setDaemon(true);
+        thread.start();
+        // test that it did not run yet...
+        Thread.currentThread().sleep(70 * 1000); // 70 seconds
+        scheduler.stop();
+//        assertEquals(2, validCache.clearCount);
+        assertTrue("Wrong invocation count: " + validCache.clearCount,
+            validCache.clearCount == 1 || validCache.clearCount == 2);
+        assertEquals(0, invalidCache.clearCount);
+    }
+
+    private class DummyCache extends ConcurrentDataCache {
+
+        int clearCount = 0;
+
+        public void clear() {
+            clearCount++;
+        }
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestDistributedKodoDataCache.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestDistributedKodoDataCache.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestDistributedKodoDataCache.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestDistributedKodoDataCache.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,357 @@
+/*
+ * 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.openjpa.persistence.datacache;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+
+import org.apache.openjpa.persistence.datacache.common.apps.RuntimeTest1;
+import org.apache.openjpa.persistence.datacache.common.apps.RuntimeTest2;
+import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
+import org.apache.openjpa.datacache.DataCache;
+import org.apache.openjpa.event.TCPRemoteCommitProvider;
+import org.apache.openjpa.lib.conf.Configurations;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
+import org.apache.openjpa.util.Id;
+
+public class TestDistributedKodoDataCache extends AbstractTestCase {
+
+    // We want more than 1 instance of each type of class.
+    private static final int NUM_OBJECTS = 4;
+
+    private Object[] _runtime1sOids;
+
+    private Object[] _runtime2sOids;
+
+    private Object spec_oid;
+
+    public TestDistributedKodoDataCache(String test) {
+        super(test, "datacachecactusapp");
+    }
+
+    public void setUp() {
+        deleteAll(RuntimeTest1.class);
+        deleteAll(RuntimeTest2.class);
+    }
+
+    private interface ChangeOperation {
+
+        public String getName();
+
+        public void operation(OpenJPAEntityManagerFactory kpmf,
+            boolean asLarge);
+    }
+
+    private class performAsModify implements ChangeOperation {
+
+        public String getName() {
+            return "Modify SpecialRuntimeTest1";
+        }
+
+        public void operation(OpenJPAEntityManagerFactory kpmf,
+            boolean asLarge) {
+            OpenJPAEntityManager pm;
+            pm = (OpenJPAEntityManager) kpmf.createEntityManager();
+            if (asLarge)
+                //pm.setLargeTransaction(true);
+                pm.setTrackChangesByType(true);
+            startTx(pm);
+            RuntimeTest1 special = pm.find(RuntimeTest1.class, spec_oid);
+            assertNotNull(special);
+            special.setStringField("SpeicalRuntimeTest1_MODIFIED");
+
+            endTx(pm);
+            endEm(pm);
+        }
+    }
+
+    private class performAsDelete implements ChangeOperation {
+
+        public String getName() {
+            return "Delete SpecialRuntimeTest1";
+        }
+
+        public void operation(OpenJPAEntityManagerFactory kpmf,
+            boolean asLarge) {
+            OpenJPAEntityManager pm;
+            pm = (OpenJPAEntityManager) kpmf.createEntityManager();
+            if (asLarge)
+                //pm.setLargeTransaction(true);
+                pm.setTrackChangesByType(true);
+            startTx(pm);
+            RuntimeTest1 specialObj = pm.find(RuntimeTest1.class, spec_oid);
+            assertNotNull(specialObj);
+
+            pm.remove(specialObj);
+
+            endTx(pm);
+            endEm(pm);
+        }
+    }
+
+    public void testNormalTransAndDataCacheDelete() {
+        coreTestTransAndChange(new performAsDelete(), false, true);
+    }
+
+    public void testNormalTransAndDataCacheModify() {
+        coreTestTransAndChange(new performAsModify(), false, false);
+    }
+
+    public void testLargeTransAndDataCacheDelete() {
+        coreTestTransAndChange(new performAsDelete(), true, true);
+    }
+
+    public void testLargeTransAndDataCacheModify() {
+        coreTestTransAndChange(new performAsModify(), true, false);
+    }
+
+    public void coreTestTransAndChange(ChangeOperation changeOperation,
+        boolean asLargeTransaction, boolean isDelete) {
+        // Create two pmfs in a cluster that are using RCPTCP.
+        OpenJPAEntityManagerFactory pmfSender = createDistinctFactory(
+            TCPRemoteCommitProvider.class,
+            "Port=5636, Addresses=127.0.0.1:6636");
+        OpenJPAEntityManagerFactory pmfReceiver = createDistinctFactory(
+            TCPRemoteCommitProvider.class,
+            "Port=6636, Addresses=127.0.0.1:5636");
+        // Get the datacaches from each pmf
+        DataCache dcSender =
+            ((OpenJPAEntityManagerFactorySPI) pmfSender).getConfiguration()
+                .getDataCacheManagerInstance().getSystemDataCache();
+        DataCache dcReceiver =
+            ((OpenJPAEntityManagerFactorySPI) pmfReceiver).getConfiguration()
+                .getDataCacheManagerInstance().getSystemDataCache();
+
+        deleteAll(RuntimeTest1.class);
+        deleteAll(RuntimeTest2.class);
+
+        _runtime1sOids = null;
+        _runtime2sOids = null;
+
+        String transType = "normal";
+        String rcpType = "OIDs";
+        if (asLargeTransaction) {
+            transType = "large";
+            rcpType = "classes";
+        }
+        System.out.println("-------------------");
+        System.out
+            .println(
+                "2 PMFs created, acting as a cluster using ports 5636 and 6636");
+        System.out.println("Testing scenario:");
+        System.out
+            .println("  1 Seed datastore with instances of RuntimeTest1 AND "
+                + "RuntimeTest2 objs.\n"
+                + "  2 pmf2 fills its cache with both sets of objs.\n"
+                + "  3 pmf1 does a "
+                + transType
+                + " tx that invokes an operation of "
+                + changeOperation.getName()
+                + " to affect a single Runtime1 \n"
+                + "    assert that both pmf1 and pmf2's data caches dropped all"
+                + "Runtime1s");
+        System.out.println("Remote commit event is transmiting " + rcpType);
+
+        // Create runtimes1s and 2s.
+        // Noraml transaction
+        OpenJPAEntityManager pmSender = (OpenJPAEntityManager) pmfSender
+            .createEntityManager();
+        seedDataStore(pmSender, NUM_OBJECTS);
+        endEm(pmSender);
+
+        // wait a bit so they get stored
+        pause(1);
+
+        OpenJPAEntityManager pm2;
+        pm2 = (OpenJPAEntityManager) pmfReceiver.createEntityManager();
+        performLoadAll(pm2);
+        endEm(pm2);
+
+        pmSender = (OpenJPAEntityManager) pmfSender.createEntityManager();
+        performLoadAll(pmSender);
+        endEm(pmSender);
+
+        // assert that pmf2's data cache now has all the Runtime1 and 2s.
+        for (int i = 0; i < _runtime1sOids.length; i++) {
+            assertTrue(dcReceiver.contains(
+                Id.newInstance(RuntimeTest1.class, _runtime1sOids[i])));
+        }
+        for (int i = 0; i < _runtime2sOids.length; i++) {
+            assertTrue(dcReceiver.contains(
+                Id.newInstance(RuntimeTest2.class, _runtime2sOids[i])));
+        }
+
+        // Modify or delete exactly 1 RuntimeTest1 object during a
+        // largeTransaction
+        changeOperation.operation(pmfSender, asLargeTransaction);
+
+        // assert that pmf1's data cache now only has Runtime2 objects
+        if (asLargeTransaction) {
+            for (int i = 0; i < _runtime1sOids.length; i++) {
+                assertFalse(dcSender.contains(
+                    Id.newInstance(RuntimeTest1.class, _runtime1sOids[i])));
+            }
+        } else {
+            // Normal transaction
+            for (int i = 0; i < _runtime1sOids.length; i++) {
+                if (isDelete && i == 0) {
+                    assertFalse(dcSender.contains(
+                        Id.newInstance(RuntimeTest1.class, _runtime1sOids[i])));
+                } else {
+                    // modified the first elemnt, which just updated it.
+                    // (for Kodo's data cache).
+                    assertTrue(dcSender.contains(
+                        Id.newInstance(RuntimeTest1.class, _runtime1sOids[i])));
+                }
+            }
+        }
+        for (int i = 0; i < _runtime2sOids.length; i++) {
+            assertTrue(dcSender.contains(
+                Id.newInstance(RuntimeTest2.class, _runtime2sOids[i])));
+        }
+        // wait a tiny bit so the rce propagates
+        pause(2);
+        // assert the pmf2's data cache also now only has Runtime2 objects
+        if (asLargeTransaction) {
+            for (int i = 0; i < _runtime1sOids.length; i++) {
+                assertFalse(dcReceiver.contains(Id.newInstance(
+                    RuntimeTest1.class, _runtime1sOids[i]))); //failing here
+            }
+        } else {
+            for (int i = 0; i < _runtime1sOids.length; i++) {
+                if (i == 0) {
+                    assertFalse(dcReceiver.contains(Id.newInstance(
+                        RuntimeTest1.class,
+                        _runtime1sOids[i])));  //failing here
+                } else {
+                    assertTrue(dcReceiver.contains(
+                        Id.newInstance(RuntimeTest1.class, _runtime1sOids[i])));
+                }
+            }
+        }
+        for (int i = 0; i < _runtime2sOids.length; i++) {
+            assertTrue(dcReceiver.contains(
+                Id.newInstance(RuntimeTest2.class, _runtime2sOids[i])));
+        }
+
+        // shutdown
+        pmfSender.close();
+        pmfReceiver.close();
+    }
+
+    protected void performLoadAll(OpenJPAEntityManager pm)
+    // load in (and thus cache) all the 1s and 2s
+    {
+        startTx(pm);
+        RuntimeTest1 temp1;
+        Collection runtime1s = (Collection) pm
+            .createQuery("SELECT a FROM RuntimeTest1 a").getResultList();
+
+        for (Iterator itr = runtime1s.iterator(); itr.hasNext();)
+            temp1 = (RuntimeTest1) itr.next();
+        RuntimeTest2 temp2;
+        Collection runtime2s = (Collection) pm
+            .createQuery("SELECT a FROM RuntimeTest2 a").getResultList();
+
+        for (Iterator itr = runtime2s.iterator(); itr.hasNext();)
+            temp2 = (RuntimeTest2) itr.next();
+        endTx(pm);
+    }
+
+    protected void seedDataStore(OpenJPAEntityManager pm, int numObjects) {
+        startTx(pm);
+        // create objects
+        RuntimeTest1[] persistables = new RuntimeTest1[numObjects];
+        _runtime1sOids = new Object[numObjects];
+        for (int i = 0; i < persistables.length; i++) {
+            persistables[i] = new RuntimeTest1("foo #" + i, i);
+            pm.persist(persistables[i]);
+            _runtime1sOids[i] = pm.getObjectId(persistables[i]);
+            if (i == 0) {
+                persistables[i].setStringField("SpecialRuntimeTest1");
+                spec_oid = pm.getObjectId(persistables[i]);
+            }
+        }
+        RuntimeTest2[] persistables2 = new RuntimeTest2[numObjects];
+        _runtime2sOids = new Object[numObjects];
+        for (int i = 0; i < persistables2.length; i++) {
+            persistables2[i] = new RuntimeTest2("bar #" + i, i);
+            pm.persist(persistables2[i]);
+            _runtime2sOids[i] = pm.getObjectId(persistables2[i]);
+        }
+        endTx(pm);
+    }
+
+    static int _fetchGroupSerial = 0;
+
+    protected OpenJPAEntityManagerFactory createDistinctFactory(
+        Class providerClass, String classProps1) {
+        Map propsMap;
+
+        if (providerClass != null) {
+            // This test is for the combination of RCP, largeTrans,
+            // and Kodo's builtin DataCache.
+            // use this property to differentiate the factory
+
+            propsMap = new HashMap();
+            propsMap.put("openjpa.DataCache", "lru");
+            propsMap.put("openjpa.RemoteCommitProvider", Configurations.getPlugin(
+                providerClass.getName(), classProps1));
+            propsMap.put("openjpa.FetchGroups", "differentiatingFetchGroup"
+                + _fetchGroupSerial);
+        } else {
+            // No RCP
+            propsMap = new HashMap();
+            propsMap.put("openjpa.RemoteCommitProvider", "sjvm");
+            propsMap.put("openjpa.FetchGroups", "differentiatingFetchGroup"
+                + _fetchGroupSerial);
+        }
+        _fetchGroupSerial += 1;
+
+        return (OpenJPAEntityManagerFactory) getEmf(propsMap);
+    }
+
+    private void pause(double seconds) {
+        try {
+            Thread.currentThread().yield();
+            Thread.currentThread().sleep((int) seconds * 1000);
+        }
+        catch (Exception e) {
+        }
+    }
+}
+
+/*
+ remove all Runteim1, and 2
+ create a few 1 and 2s
+ get them into the cache, assert
+ large transaction, and modify a runtime2, assert cache only has runtime1s
+
+
+ remove all Runteim1, and 2
+ create a few 1 and 2s
+ get them into the cache, assert
+ large transaction, delete a runtime2, assert cache only has runteime1s
+ */
+

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestFlushDataCache.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestFlushDataCache.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestFlushDataCache.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestFlushDataCache.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,102 @@
+/*
+ * 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.openjpa.persistence.datacache;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+
+
+import org.apache.openjpa.persistence.datacache.common.apps.FlushDataCacheObject;
+import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
+
+public class TestFlushDataCache
+    extends AbstractTestCase {
+
+    public TestFlushDataCache(String str) {
+        super(str, "datacachecactusapp");
+    }
+
+    public void setUp() {
+        deleteAll(FlushDataCacheObject.class);
+    }
+
+    public void testQueryFlushPlusDataCache() {
+        Map propsMap = new HashMap();
+        propsMap.put("openjpa.DataCache", "true");
+        propsMap.put("openjpa.RemoteCommitProvider", "sjvm");
+        propsMap.put("openjpa.FlushBeforeQueries", "true");
+        //propsMap.put("javax.jdo.option.IgnoreCache", "false");
+        //propsMap.put("openjpa.BrokerImpl", "kodo.datacache.CacheTestBroker");//CacheTestBroker.class.getName ());
+        EntityManagerFactory emf = getEmf(propsMap);
+
+        try {
+
+            //assertEquals(Class.forName("openjpa.datacache.CacheTestBroker",true,emf.getClass().getClassLoader()).getClassLoader(),emf.getClass().getClassLoader());
+            //Thread.currentThread().setContextClassLoader(emf.getClass().getClassLoader());
+            Class.forName("org.apache.openjpa.persistence.datacache.CacheTestBroker", true,
+                Thread.currentThread().getContextClassLoader());
+        } catch (Exception e) {
+
+        }
+
+        EntityManager em = emf.createEntityManager();
+        startTx(em);
+        FlushDataCacheObject o = new FlushDataCacheObject();
+        o.setString("foo");
+        em.persist(o);
+        endTx(em);
+        //Object oid = JDOHelper.getObjectId (o);
+        endEm(em);
+
+        em = emf.createEntityManager();
+        startTx(em);
+
+        Collection c = (Collection) em.createQuery(
+            "select a FROM " + FlushDataCacheObject.class.getSimpleName() +
+                " a where a.string = 'foo'").getResultList();
+
+        assertEquals(1, c.size());
+        em.remove(c.iterator().next());
+
+        c = (Collection) em.createQuery(
+            "select a FROM " + FlushDataCacheObject.class.getSimpleName() +
+                " a where a.string = 'foo'").getResultList();
+        assertEquals(0, c.size());
+
+        endTx(em);
+        endEm(em);
+    }
+
+    public void testEmptyCommit() {
+        Map propsMap = new HashMap();
+        propsMap.put("openjpa.DataCache", "true");
+        propsMap.put("openjpa.RemoteCommitProvider", "sjvm");
+        propsMap.put("openjpa.FlushBeforeQueries", "true");
+        //propsMap.put("javax.jdo.option.IgnoreCache", "false");
+        propsMap.put("openjpa.BrokerImpl", CacheTestBroker.class.getName());
+        EntityManagerFactory emf = getEmf(propsMap);
+        EntityManager em = emf.createEntityManager();
+        startTx(em);
+        endTx(em);
+        endEm(em);
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestJPQL2Queries.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestJPQL2Queries.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestJPQL2Queries.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestJPQL2Queries.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,134 @@
+/*
+ * 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.openjpa.persistence.datacache;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import javax.persistence.EntityManagerFactory;
+
+
+import org.apache.openjpa.persistence.datacache.common.apps.CacheObjectA;
+import org.apache.openjpa.persistence.datacache.common.apps.CacheObjectAChild1;
+import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
+
+import org.apache.openjpa.kernel.Broker;
+import org.apache.openjpa.kernel.BrokerFactory;
+import org.apache.openjpa.kernel.Query;
+import org.apache.openjpa.kernel.jpql.JPQLParser;
+import org.apache.openjpa.persistence.JPAFacadeHelper;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
+
+public class TestJPQL2Queries
+    extends AbstractTestCase {
+
+    public TestJPQL2Queries(String test) {
+        super(test, "datacachecactusapp");
+    }
+
+    private BrokerFactory _factory;
+
+    public void setUp() {
+        deleteAll(CacheObjectA.class);
+
+        Map propsMap = new HashMap();
+        propsMap.put("openjpa.DataCache", "true");
+        propsMap.put("openjpa.RemoteCommitProvider", "sjvm");
+        propsMap.put("openjpa.BrokerImpl", CacheTestBroker.class.getName());
+        EntityManagerFactory emf = getEmf(propsMap);
+        _factory = JPAFacadeHelper.toBrokerFactory(emf);
+        Broker broker = _factory.newBroker();
+        broker.begin();
+        for (int i = 0; i < 50; i++)
+            broker.persist(new CacheObjectAChild1("", "JPQL2Queries", i),
+                null);
+        broker.commit();
+        broker.close();
+
+        CacheTestHelper.cacheManager(_factory).getSystemQueryCache().clear();
+    }
+
+    public void tearDown()
+        throws Exception {
+        try {
+            _factory.close();
+        } catch (Exception e) {
+        }
+
+        super.tearDown();
+    }
+
+    public void testUpperRange() {
+        rangeTestHelper(0, 10);
+    }
+
+    public void testLowerRange() {
+        rangeTestHelper(5, 50);
+    }
+
+    public void testBothRanges() {
+        rangeTestHelper(20, 28);
+    }
+
+    private void rangeTestHelper(final int start, final int end) {
+        Broker broker = _factory.newBroker();
+        Query q = broker.newQuery(JPQLParser.LANG_JPQL, "Select a FROM " +
+            CacheObjectAChild1.class.getSimpleName() + " a");
+        q.setRange(start, end);
+
+        // should not yet be in the cache
+        CacheTestHelper.assertInCache(this, q, Boolean.FALSE);
+        Collection c = (Collection) q.execute();
+
+        // iterate the results. This will cause the query to be
+        // enlisted in the cache.
+        CacheTestHelper.iterate(c);
+        assertEquals(end - start, c.size());
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+
+        broker.close();
+
+        broker = _factory.newBroker();
+        q = broker.newQuery(JPQLParser.LANG_JPQL, "Select a FROM " +
+            CacheObjectAChild1.class.getSimpleName() + " a");
+        q.setRange(start, end);
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+        c = (Collection) q.execute();
+        assertEquals(end - start, c.size());
+
+        // now check if a smaller range is in cache
+        q = broker.newQuery(JPQLParser.LANG_JPQL, "Select a FROM " +
+            CacheObjectAChild1.class.getSimpleName() + " a");
+        q.setRange(start, end - 1);
+        CacheTestHelper.assertInCache(this, q, Boolean.FALSE);
+        c = (Collection) q.execute();
+        assertEquals(end - start - 1, c.size());
+        broker.close();
+    }
+
+    public void testResultClassIsCached() {
+        Broker broker = _factory.newBroker();
+        Query q = broker.newQuery(JPQLParser.LANG_JPQL, "Select a FROM " +
+            CacheObjectAChild1.class.getSimpleName() + " a");
+        q.setResultType(Object.class);
+        Collection c = (Collection) q.execute();
+        CacheTestHelper.iterate(c);
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestJPQL2ResultsAndResultClasses.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestJPQL2ResultsAndResultClasses.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestJPQL2ResultsAndResultClasses.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestJPQL2ResultsAndResultClasses.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,472 @@
+/*
+ * 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.openjpa.persistence.datacache;
+
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import javax.persistence.EntityManagerFactory;
+
+
+import org.apache.openjpa.persistence.datacache.common.apps.CacheObjectA;
+import org.apache.openjpa.persistence.datacache.common.apps.CacheObjectAChild1;
+import org.apache.openjpa.persistence.datacache.common.apps.CacheObjectB;
+import org.apache.openjpa.persistence.datacache.common.apps.CacheObjectWithExternalizedFields;
+import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
+
+import org.apache.openjpa.kernel.Broker;
+import org.apache.openjpa.kernel.BrokerFactory;
+import org.apache.openjpa.kernel.Query;
+import org.apache.openjpa.kernel.jpql.JPQLParser;
+import org.apache.openjpa.persistence.JPAFacadeHelper;
+
+public class TestJPQL2ResultsAndResultClasses
+    extends AbstractTestCase {
+
+    public TestJPQL2ResultsAndResultClasses(String test) {
+        super(test, "datacachecactusapp");
+    }
+
+    private BrokerFactory _factory;
+
+    public void setUp() {
+        deleteAll(CacheObjectA.class);
+        deleteAll(CacheObjectWithExternalizedFields.class);
+
+        Map propsMap = new HashMap();
+        propsMap.put("openjpa.DataCache", "true");
+        propsMap.put("openjpa.RemoteCommitProvider", "sjvm");
+        propsMap.put("openjpa.BrokerImpl", CacheTestBroker.class.getName());
+        EntityManagerFactory emf =
+            getEmf(propsMap);
+        _factory = JPAFacadeHelper.toBrokerFactory(emf);
+        Broker broker = _factory.newBroker();
+        try {
+            broker.begin();
+        } catch (Exception e) {
+            System.out.println(
+                "Exception in TestJPQL2ResultsAndResultClasses setup : \n" +
+                    getStackTrace(e));
+        }
+
+        int j = 0;
+        for (int i = 0; i < 10; i++) {
+            // make some common names so that GROUP BY is useful.
+            if (i % 2 == 0)
+                j++;
+            CacheObjectA o;
+            broker.persist(o = new CacheObjectAChild1("", "results-" + j, i),
+                null);
+
+            o.setDate(new Date());
+            o.setDateArray(new Date[]{ new Date(10), new Date(20) });
+
+            if (i < 5)
+                o.setRelatedB(new CacheObjectB());
+        }
+
+        //Seetha Oct 30,2006
+        //deleteAll closes the TX.  So use the local
+        //deleteAll fn.
+        //deleteAll(CacheObjectWithExternalizedFields.class,());
+        //deleteAll(broker,CacheObjectWithExternalizedFields.class,true);
+
+        CacheObjectWithExternalizedFields o =
+            new CacheObjectWithExternalizedFields();
+        broker.persist(o, null);
+        o.setCls(Broker.class);
+
+        broker.commit();
+        broker.close();
+
+        CacheTestHelper.cacheManager(_factory).getSystemQueryCache().clear();
+    }
+
+    public void tearDown()
+        throws Exception {
+        _factory.close();
+
+        super.tearDown();
+    }
+
+    public void testAggregateResultIsCached() {
+        Broker broker = _factory.newBroker();
+        Query q =
+            broker.newQuery(JPQLParser.LANG_JPQL, "select max(a.age) FROM " +
+                CacheObjectAChild1.class.getSimpleName() + " a");
+
+        Object o = q.execute();
+        assertEquals(Long.class, o.getClass());
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+
+        o = q.execute();
+        assertEquals(Long.class, o.getClass());
+    }
+
+    public void testAggregateNonUniqueResultIsCached() {
+        Broker broker = _factory.newBroker();
+        Query q =
+            broker.newQuery(JPQLParser.LANG_JPQL, "select max(a.age) FROM " +
+                CacheObjectAChild1.class.getSimpleName() + " a");
+        q.setUnique(false);
+        List res = (List) q.execute();
+        assertEquals(1, res.size());
+        Object o = res.get(0);
+        assertEquals(Long.class, o.getClass());
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+
+        res = (List) q.execute();
+        assertEquals(1, res.size());
+        o = res.get(0);
+        assertEquals(Long.class, o.getClass());
+    }
+
+    public void testProjectionResultIsCached() {
+        Broker broker = _factory.newBroker();
+        Query q = broker.newQuery(JPQLParser.LANG_JPQL, "select a.age FROM " +
+            CacheObjectAChild1.class.getSimpleName() + " a");
+        Collection c = (Collection) q.execute();
+        CacheTestHelper.iterate(c);
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+
+        c = (Collection) q.execute();
+        Object o = c.iterator().next();
+        assertEquals(Long.class, o.getClass());
+    }
+
+    public void testProjectionOfThisIsCached() {
+        // ##### need to test single projection
+        Broker broker = _factory.newBroker();
+        Query q = broker.newQuery(JPQLParser.LANG_JPQL, "select a FROM " +
+            CacheObjectAChild1.class.getSimpleName() + " a");
+        //CacheObjectAChild1.class, "select this");
+        Collection c = (Collection) q.execute();
+        CacheTestHelper.iterate(c);
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+
+        c = (Collection) q.execute();
+        Object o = c.iterator().next();
+        assertEquals(CacheObjectAChild1.class, o.getClass());
+        assertNotNull(broker.getObjectId(o));
+    }
+
+    public void testProjectionResultWithThisIsCached() {
+        Broker broker = _factory.newBroker();
+        Query q =
+            broker.newQuery(JPQLParser.LANG_JPQL, "select a.name,a FROM " +
+                CacheObjectAChild1.class.getSimpleName() + " a");
+        Collection c = (Collection) q.execute();
+        CacheTestHelper.iterate(c);
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+
+        c = (Collection) q.execute();
+        Object[] result = (Object[]) c.iterator().next();
+        assertEquals(2, result.length);
+        assertEquals(String.class, result[0].getClass());
+        assertEquals(CacheObjectAChild1.class, result[1].getClass());
+        assertNotNull(broker.getObjectId(result[1]));
+    }
+
+    public void testNullProjectionValues() {
+        Broker broker = _factory.newBroker();
+        Query q =
+            broker.newQuery(JPQLParser.LANG_JPQL, "select a.locale FROM " +
+                CacheObjectAChild1.class.getSimpleName() + " a");
+        Collection c = (Collection) q.execute();
+        CacheTestHelper.iterate(c);
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+
+        c = (Collection) q.execute();
+        assertNull(c.iterator().next());
+    }
+
+    public void testNullAndNotNullProjectionValues() {
+        Broker broker = _factory.newBroker();
+        Query q =
+            broker
+                .newQuery(JPQLParser.LANG_JPQL, "select a.name,a.locale FROM " +
+                    CacheObjectAChild1.class.getSimpleName() + " a");
+
+        Collection c = (Collection) q.execute();
+        CacheTestHelper.iterate(c);
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+
+        c = (Collection) q.execute();
+        Object[] result = (Object[]) c.iterator().next();
+        assertEquals(2, result.length);
+        assertEquals(String.class, result[0].getClass());
+        assertNull(result[1]);
+    }
+
+    public void XXXtestNullAggregateValues() {
+        // ???
+    }
+
+    public void testMultipleAggregateResultIsCached() {
+        Broker broker = _factory.newBroker();
+        Query q =
+            broker.newQuery(JPQLParser.LANG_JPQL,
+                "select max(a.age), avg(a.age), count(a.age) FROM " +
+                    CacheObjectAChild1.class.getSimpleName() + " a");
+
+        Object o = q.execute();
+        assertEquals(Object[].class, o.getClass());
+        assertEquals(3, ((Object[]) o).length);
+        assertEquals(Long.class, ((Object[]) o)[0].getClass());
+
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+
+        o = q.execute();
+        assertEquals(Object[].class, o.getClass());
+        assertEquals(3, ((Object[]) o).length);
+        assertEquals(Long.class, ((Object[]) o)[0].getClass());
+    }
+
+    public void testFieldUsedTwice() {
+        // Postgres bug
+        Broker broker = _factory.newBroker();
+        // group avoids postgres bug
+        Query q = broker
+            .newQuery(JPQLParser.LANG_JPQL, "select a.age, avg(a.age) FROM " +
+                CacheObjectAChild1.class.getSimpleName() +
+                " a  group by a.age");
+        Collection c = (Collection) q.execute();
+        CacheTestHelper.iterate(c);
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+    }
+
+    public void testAggregateAndProjection() {
+        // Postgres bug
+        Broker broker = _factory.newBroker();
+        // group avoids postgres bug
+        Query q = broker
+            .newQuery(JPQLParser.LANG_JPQL, "select a.name, avg(a.age) FROM " +
+                CacheObjectAChild1.class.getSimpleName() +
+                " a  group by a.name");
+
+        List l = (List) q.execute();
+        CacheTestHelper.iterate(l);
+        assertEquals(Object[].class, l.get(0).getClass());
+        assertEquals(2, ((Object[]) l.get(0)).length);
+        assertEquals(String.class, ((Object[]) l.get(0))[0].getClass());
+
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+
+        l = (List) q.execute();
+        assertEquals(Object[].class, l.get(0).getClass());
+        assertEquals(2, ((Object[]) l.get(0)).length);
+        assertEquals(String.class, ((Object[]) l.get(0))[0].getClass());
+    }
+
+    //FIXME Seetha Dec 19,2006
+    /*public void testMath() {
+        Broker broker = _factory.newBroker();
+        Query q =
+            broker.newQuery(JPQLParser.LANG_JPQL,"select avg(a.age) FROM "+
+                    CacheObjectAChild1.class.getSimpleName()+" a");
+        Number n = (Number) q.execute();
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+        n = (Number) q.execute();
+    }*/
+
+    public void testResultClassIsCached() {
+        Broker broker = _factory.newBroker();
+        Query q = broker.newQuery(JPQLParser.LANG_JPQL, "Select a FROM " +
+            CacheObjectAChild1.class.getSimpleName() + " a");
+        q.setResultType(Object.class);
+        Collection c = (Collection) q.execute();
+        CacheTestHelper.iterate(c);
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+    }
+
+    public void testGroupingIsCached() {
+        Broker broker = _factory.newBroker();
+        Query q =
+            broker.newQuery(JPQLParser.LANG_JPQL, "select max(a.age) FROM " +
+                CacheObjectAChild1.class.getSimpleName() +
+                " a  group by a.name");
+
+        Collection c = (Collection) q.execute();
+        CacheTestHelper.iterate(c);
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+
+        c = (Collection) q.execute();
+        Object o = c.iterator().next();
+        assertEquals(Long.class, o.getClass());
+    }
+
+    public void testAggregateProjectionGroupingIsCached() {
+        Broker broker = _factory.newBroker();
+        Query q =
+            broker.newQuery(JPQLParser.LANG_JPQL,
+                "select a.name, max(a.age) FROM " +
+                    CacheObjectAChild1.class.getSimpleName() +
+                    " a  group by a.name");
+
+        Collection c = (Collection) q.execute();
+        CacheTestHelper.iterate(c);
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+
+        c = (Collection) q.execute();
+        Object[] result = (Object[]) c.iterator().next();
+        assertEquals(2, result.length);
+        assertEquals(String.class, result[0].getClass());
+        assertEquals(Long.class, result[1].getClass());
+    }
+
+    public void testUniqueResultsAreCachedAndConsistent() {
+        Broker broker = _factory.newBroker();
+        Query q =
+            broker.newQuery(JPQLParser.LANG_JPQL, "select a FROM " +
+                CacheObjectAChild1.class.getSimpleName() +
+                " a  where a.age =1");
+
+        q.setUnique(true);
+        CacheObjectAChild1 a = (CacheObjectAChild1) q.execute();
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+
+        CacheObjectAChild1 a2 = (CacheObjectAChild1) q.execute();
+        assertTrue(a == a2);
+    }
+
+    public void testMutableProjectionFieldsAreCopied() {
+        Broker broker = _factory.newBroker();
+        Query q = broker.newQuery(JPQLParser.LANG_JPQL, "select a.date FROM " +
+            CacheObjectAChild1.class.getSimpleName() + " a  where a.age=1");
+
+        q.setUnique(true);
+        Date d0 = (Date) q.execute(); // get it in cache
+        Date d1 = (Date) q.execute();
+        assertNotSame(d0, d1);
+
+        Date d2 = (Date) q.execute();
+        assertNotSame(d1, d2);
+    }
+
+    public void testArrayProjectionFieldsAreNotCached() {
+        Broker broker = _factory.newBroker();
+        Query q =
+            broker.newQuery(JPQLParser.LANG_JPQL, "select a.dateArray FROM " +
+                CacheObjectAChild1.class.getSimpleName() + " a");
+
+        try {
+            q.execute();
+            fail("Allowed array projection query.");
+        } catch (Exception e) {
+        }
+    }
+
+    public void testCollectionProjectionFieldsAreNotCached() {
+        Broker broker = _factory.newBroker();
+        Query q =
+            broker.newQuery(JPQLParser.LANG_JPQL, "select a.stringColl FROM " +
+                CacheObjectAChild1.class.getSimpleName() + " a");
+
+        try {
+            q.execute();
+            fail("Allowed array projection query.");
+        } catch (Exception e) {
+        }
+    }
+
+    public void testExternalizedSingleValueFieldIsNotCached() {
+        Broker broker = _factory.newBroker();
+        Query q = broker.newQuery(JPQLParser.LANG_JPQL, "select a.cls FROM " +
+            CacheObjectWithExternalizedFields.class.getSimpleName() + " a");
+
+        q.setUnique(true);
+        Object o = q.execute(); // get it in cache
+        // ##### assertEquals (Class.class, o);
+        CacheTestHelper.assertInCache(this, q, Boolean.FALSE);
+    }
+
+    public void testMutatedQueryReturnsNewResults() {
+        Broker broker = _factory.newBroker();
+        Query q =
+            broker.newQuery(JPQLParser.LANG_JPQL,
+                "select a.name, max(a.age) FROM " +
+                    CacheObjectAChild1.class.getSimpleName() +
+                    " a group by a.name");
+
+        List l = (List) q.execute();
+        CacheTestHelper.iterate(l);
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+
+        l = (List) q.execute();
+        Object[] result = (Object[]) l.get(0);
+        assertEquals(2, result.length);
+        assertEquals(String.class, result[0].getClass());
+        assertEquals(Long.class, result[1].getClass());
+
+        // now, mutate the query and see what happens
+        q.setQuery("select max(a.age),a.name FROM " +
+            CacheObjectAChild1.class.getSimpleName() + " a group by a.name");
+        CacheTestHelper.assertInCache(this, q, Boolean.FALSE);
+        l = (List) q.execute();
+        result = (Object[]) l.get(0);
+        assertEquals(2, result.length);
+        assertEquals(Long.class, result[0].getClass());
+        assertEquals(String.class, result[1].getClass());
+        CacheTestHelper.iterate(l);
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+
+        l = (List) q.execute();
+        result = (Object[]) l.get(0);
+        assertEquals(2, result.length);
+        assertEquals(Long.class, result[0].getClass());
+        assertEquals(String.class, result[1].getClass());
+    }
+
+    public void XXXtestExternalizedContainerFieldIsExternalized() {
+    }
+
+    public void XXXtestSerializedSingleValueFieldIsSerialized() {
+    }
+
+    public void XXXtestSerializedContainerFieldIsSerialized() {
+    }
+
+    public void XXXtestCustomMappedSingleValueFieldIsHandled() {
+    }
+
+    public void XXXtestCustomMappedContainerFieldIsHandled() {
+    }
+
+    private static int deleteAll(Broker broker, Class clazz,
+        boolean subclasses) {
+        final boolean useDeleteByQuery = false;
+
+        if (useDeleteByQuery) {
+            org.apache.openjpa.kernel.Query query =
+                broker.newQuery(JPQLParser.LANG_JPQL, "Select a FROM " +
+                    clazz.getSimpleName() + " a");
+            query.setCandidateType(clazz, subclasses);
+            return (int) query.deleteAll();
+        } else {
+            org.apache.openjpa.kernel.Extent extent =
+                broker.newExtent(clazz, subclasses);
+            List list = extent.list();
+            int size = list.size();
+            broker.deleteAll(list, null);
+            return size;
+        }
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestJPQLRelationProjections.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestJPQLRelationProjections.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestJPQLRelationProjections.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestJPQLRelationProjections.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,219 @@
+/*
+ * 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.openjpa.persistence.datacache;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import javax.persistence.EntityManagerFactory;
+
+
+import org.apache.openjpa.persistence.datacache.common.apps.CacheObjectAChild1;
+import org.apache.openjpa.persistence.datacache.common.apps.CacheObjectE;
+import org.apache.openjpa.persistence.datacache.common.apps.CacheObjectJ;
+import org.apache.openjpa.persistence.datacache.common.apps.SelfReferencingCacheTestObject;
+import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
+
+import org.apache.openjpa.kernel.Broker;
+import org.apache.openjpa.kernel.BrokerFactory;
+import org.apache.openjpa.kernel.Query;
+import org.apache.openjpa.kernel.jpql.JPQLParser;
+import org.apache.openjpa.persistence.JPAFacadeHelper;
+import org.apache.openjpa.util.UserException;
+
+public class TestJPQLRelationProjections
+    extends AbstractTestCase {
+
+    public TestJPQLRelationProjections(String test) {
+        super(test, "datacachecactusapp");
+    }
+
+    private BrokerFactory _factory;
+
+    public void setUp() {
+        deleteAll(CacheObjectJ.class);
+        deleteAll(CacheObjectE.class);
+        deleteAll(SelfReferencingCacheTestObject.class);
+
+        Map propsMap = new HashMap();
+        propsMap.put("openjpa.DataCache", "true");
+        propsMap.put("openjpa.RemoteCommitProvider", "sjvm");
+        propsMap.put("openjpa.BrokerImpl", CacheTestBroker.class.getName());
+
+        EntityManagerFactory emf = getEmf(propsMap);
+        _factory = JPAFacadeHelper.toBrokerFactory(emf);
+        Broker broker = _factory.newBroker();
+        try {
+            broker.begin();
+        } catch (Exception e) {
+            fail("Set up failed due to exception : \n" +
+                getStackTrace(e));
+        }
+        int j = 0;
+        for (int i = 0; i < 6; i++) {
+            CacheObjectE e;
+            if (i < 3)
+                e = new CacheObjectE(i + "");
+            else
+                e = null;
+
+            // make some common names so that GROUP BY is useful.
+            if (i % 2 == 0)
+                j++;
+            broker.persist(new CacheObjectJ("projections-" + j, e), null);
+        }
+
+        broker.persist(new SelfReferencingCacheTestObject("foo",
+            new SelfReferencingCacheTestObject("bar", null)), null);
+
+        broker.commit();
+        broker.close();
+
+        CacheTestHelper.cacheManager(_factory).getSystemQueryCache().clear();
+    }
+
+    public void tearDown()
+        throws Exception {
+        _factory.close();
+
+        super.tearDown();
+    }
+
+    public void testNullRelatedPCIsCached() {
+        Broker broker = _factory.newBroker();
+        Query q = broker.newQuery(JPQLParser.LANG_JPQL, "select a.e FROM " +
+            CacheObjectJ.class.getSimpleName() + " a where a.e is null");
+
+        Collection c = (Collection) q.execute();
+        CacheTestHelper.iterate(c);
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+
+        c = (Collection) q.execute();
+        assertNull(c.iterator().next());
+    }
+
+    public void testNullRelatedPCAndProjectionIsCached() {
+        Broker broker = _factory.newBroker();
+        Query q =
+            broker.newQuery(JPQLParser.LANG_JPQL, "select a.str,a.e FROM " +
+                CacheObjectJ.class.getSimpleName() + " a where a.e is null");
+
+        Collection c = (Collection) q.execute();
+        CacheTestHelper.iterate(c);
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+
+        c = (Collection) q.execute();
+        Object[] result = (Object[]) c.iterator().next();
+        assertEquals(2, result.length);
+        assertEquals(String.class, result[0].getClass());
+        assertNull(result[1]);
+    }
+
+    public void testNonNullRelatedPCIsCached() {
+        Broker broker = _factory.newBroker();
+        Query q = broker.newQuery(JPQLParser.LANG_JPQL, "select a.e FROM " +
+            CacheObjectJ.class.getSimpleName() + " a where a.e  is not null");
+
+        Collection c = (Collection) q.execute();
+        CacheTestHelper.iterate(c);
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+
+        c = (Collection) q.execute();
+        assertEquals(CacheObjectE.class, c.iterator().next().getClass());
+    }
+
+    public void testNonNullRelatedPCAndProjectionIsCached() {
+        Broker broker = _factory.newBroker();
+        Query q =
+            broker.newQuery(JPQLParser.LANG_JPQL, "select a.str,a.e FROM " +
+                CacheObjectJ.class.getSimpleName() +
+                " a where a.e is not null");
+
+        Collection c = (Collection) q.execute();
+        CacheTestHelper.iterate(c);
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+
+        c = (Collection) q.execute();
+        Object[] result = (Object[]) c.iterator().next();
+        assertEquals(2, result.length);
+        assertEquals(String.class, result[0].getClass());
+        assertEquals(CacheObjectE.class, result[1].getClass());
+    }
+
+    public void testEmbeddedFields() {
+        Broker broker = _factory.newBroker();
+        Query q =
+            broker.newQuery(JPQLParser.LANG_JPQL, "select embedded FROM " +
+                SelfReferencingCacheTestObject.class.getSimpleName() +
+                " a where a.str='foo'");
+
+        List l = null;
+        try {
+            l = (List) q.execute();
+            assertEquals(CacheObjectAChild1.class, l.get(0).getClass());
+        } catch (UserException ue) {
+            //bug(1150, "embedded-field projections cause exception");
+            ue.printStackTrace();
+            return;
+        }
+
+        CacheTestHelper.iterate(l);
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+
+        l = (List) q.execute();
+        assertEquals(CacheObjectAChild1.class, l.get(0).getClass());
+    }
+
+    public void testNonNullRelationOfSameTypeAsCandidate() {
+        Broker broker = _factory.newBroker();
+        Query q = broker.newQuery(JPQLParser.LANG_JPQL, "select a.o FROM " +
+            SelfReferencingCacheTestObject.class.getSimpleName() +
+            " a where a.o IS NOT NULL");
+
+        List l = (List) q.execute();
+        assertEquals(SelfReferencingCacheTestObject.class,
+            l.get(0).getClass());
+        assertEquals("bar",
+            ((SelfReferencingCacheTestObject) l.get(0)).getStr());
+        CacheTestHelper.iterate(l);
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+
+        l = (List) q.execute();
+        assertEquals(SelfReferencingCacheTestObject.class,
+            l.get(0).getClass());
+        assertEquals("bar",
+            ((SelfReferencingCacheTestObject) l.get(0)).getStr());
+    }
+
+    public void testNullRelationOfSameTypeAsCandidate() {
+        Broker broker = _factory.newBroker();
+        Query q = broker.newQuery(JPQLParser.LANG_JPQL, "select a.o FROM " +
+            SelfReferencingCacheTestObject.class.getSimpleName() +
+            " a where a.o IS NULL");
+
+        List l = (List) q.execute();
+        assertNull(l.get(0));
+        CacheTestHelper.iterate(l);
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE);
+
+        l = (List) q.execute();
+        assertNull(l.get(0));
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestLocalCache.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestLocalCache.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestLocalCache.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestLocalCache.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,48 @@
+/*
+ * 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.openjpa.persistence.datacache;
+
+/**
+ * Test the local cache. Tests only a single PMF.
+ */
+public class TestLocalCache
+    extends CacheTest {
+
+    public TestLocalCache(String test) {
+        super(test);
+    }
+
+    protected String[] getConfs() {
+        return new String[]{
+            "openjpa.DataCache", "true",
+            "openjpa.QueryCache", "true",
+            "openjpa.RemoteCommitProvider", "sjvm",
+            "openjpa.BrokerImpl", CacheTestBroker.class.getName(),
+        };
+    }
+
+    protected String[] getConfs2() {
+        return new String[]{
+            "openjpa.DataCache", "true",
+            "openjpa.QueryCache", "true",
+            "openjpa.RemoteCommitProvider", "sjvm",
+            "openjpa.BrokerImpl", CacheTestBroker.class.getName(),
+        };
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestMutableParameters.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestMutableParameters.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestMutableParameters.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestMutableParameters.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,172 @@
+/*
+ * 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.openjpa.persistence.datacache;
+
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+import javax.persistence.EntityManagerFactory;
+
+
+import org.apache.openjpa.persistence.datacache.common.apps.CacheObjectA;
+import org.apache.openjpa.persistence.datacache.common.apps.CacheObjectAChild1;
+import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
+
+import org.apache.openjpa.kernel.Broker;
+import org.apache.openjpa.kernel.BrokerFactory;
+import org.apache.openjpa.kernel.Query;
+import org.apache.openjpa.kernel.jpql.JPQLParser;
+import org.apache.openjpa.persistence.JPAFacadeHelper;
+
+public class TestMutableParameters
+    extends AbstractTestCase {
+
+    public TestMutableParameters(String test) {
+        super(test, "datacachecactusapp");
+    }
+
+    private BrokerFactory _factory;
+    private Date _startDate;
+
+    public void setUp()
+        throws Exception {
+        deleteAll(CacheObjectA.class);
+        Map propsMap = new HashMap();
+        propsMap.put("openjpa.DataCache", "true");
+        propsMap.put("openjpa.RemoteCommitProvider", "sjvm");
+        propsMap.put("openjpa.BrokerImpl", CacheTestBroker.class.getName());
+
+        EntityManagerFactory emf = getEmf(propsMap);
+        _factory = JPAFacadeHelper.toBrokerFactory(emf);
+
+        // create a very early date so that when we mutate it, we
+        // won't need to worry about precision issues.
+        _startDate = new java.text.SimpleDateFormat("dd/MM/yyyy").
+            parse("01/01/1990");
+        Broker broker = _factory.newBroker();
+        broker.begin();
+        for (int i = 0; i < 50; i++) {
+            CacheObjectAChild1 o = new CacheObjectAChild1
+                ("", "JPQL2Queries", i);
+            o.setDate(_startDate);
+            broker.persist(o, null);
+        }
+        broker.commit();
+        broker.close();
+
+        CacheTestHelper.cacheManager(_factory).getSystemQueryCache().clear();
+    }
+
+    public void tearDown()
+        throws Exception {
+        try {
+            _factory.close();
+        } catch (Exception e) {
+        }
+
+        super.tearDown();
+    }
+
+    public void testMutatedDateParameter() {
+        Broker broker = _factory.newBroker();
+        Query q = broker.newQuery(JPQLParser.LANG_JPQL,
+            "select a from " +
+                CacheObjectAChild1.class.getSimpleName() +
+                " a where a.date < :p_date");
+        Date d = new Date();
+        Collection c = (Collection) q.execute(new Object[]{ d });
+        CacheTestHelper.iterate(c);
+        int initialSize = c.size();
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE,
+            new Object[]{ d });
+        d.setTime(_startDate.getTime());
+        CacheTestHelper.assertInCache(this, q, Boolean.FALSE,
+            new Object[]{ d });
+        c = (Collection) q.execute(new Object[]{ d });
+
+        assertFalse(new Integer(initialSize).equals(new Integer(c.size())));
+    }
+
+    //FIXME Seetha Nov 10,2006
+    //need to find the JPQL query for :p_age contains
+    /*
+    public void testMutatedSetParameter() {
+        mutatedCollectionParameterHelper(true);
+    }
+
+    public void testMutatedListParameter() {
+        mutatedCollectionParameterHelper(false);
+    }
+
+    public void mutatedCollectionParameterHelper(boolean set) {
+        Broker broker = _factory.newBroker();
+        Query q =
+            broker.newQuery(JPQLParser.LANG_JPQL,
+                    "select a from "+
+                    CacheObjectAChild1.class.getSimpleName()+ "a :p_ages.contains (age)");
+        Collection c_param;
+        if (set)
+            c_param = new HashSet();
+        else
+            c_param = new LinkedList();
+        c_param.add(new Long(0));
+        c_param.add(new Long(1));
+        Collection c = (Collection) q.execute(new Object[]{ c_param });
+        CacheTestHelper.iterate(c);
+        int initialSize = c.size();
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE,
+            new Object[]{ c_param });
+        c_param.add(new Long(2));
+        CacheTestHelper.assertInCache(this, q, Boolean.FALSE,
+            new Object[]{ c_param });
+        c = (Collection) q.execute(new Object[]{ c_param });
+        assertFalse(new Integer(initialSize).equals(new Integer(c.size())));
+    }
+
+    public void testMutatedSetParameterDates() {
+        mutatedCollectionParameterDatesHelper(true);
+    }
+
+    public void testMutatedListParameterDates() {
+        mutatedCollectionParameterDatesHelper(false);
+    }
+
+    public void mutatedCollectionParameterDatesHelper(boolean set) {
+        Broker broker = _factory.newBroker();
+        Query q =
+            broker.newQuery(JPQLParser.LANG_JPQL, CacheObjectAChild1.class,
+                ":p_dates.contains (date)");
+        Collection c_param;
+        if (set)
+            c_param = new HashSet();
+        else
+            c_param = new LinkedList();
+        c_param.add(new Date());
+        c_param.add(new Date(System.currentTimeMillis() - 1000));
+        Collection c = (Collection) q.execute(new Object[]{ c_param });
+        CacheTestHelper.iterate(c);
+        int initialSize = c.size();
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE,
+            new Object[]{ c_param });
+        c_param.add(new Date(System.currentTimeMillis() - 500));
+        CacheTestHelper.assertInCache(this, q, Boolean.FALSE,
+            new Object[]{ c_param });
+    }*/
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestPCParametersInQueries.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestPCParametersInQueries.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestPCParametersInQueries.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestPCParametersInQueries.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,97 @@
+/*
+ * 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.openjpa.persistence.datacache;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import javax.persistence.EntityManagerFactory;
+
+
+import org.apache.openjpa.persistence.datacache.common.apps.CacheObjectA;
+import org.apache.openjpa.persistence.datacache.common.apps.CacheObjectE;
+import org.apache.openjpa.persistence.datacache.common.apps.CacheObjectJ;
+import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
+
+import org.apache.openjpa.kernel.Broker;
+import org.apache.openjpa.kernel.BrokerFactory;
+import org.apache.openjpa.kernel.Query;
+import org.apache.openjpa.kernel.jpql.JPQLParser;
+import org.apache.openjpa.persistence.JPAFacadeHelper;
+
+/**
+ * New test case.
+ */
+public class TestPCParametersInQueries extends AbstractTestCase {
+
+    public TestPCParametersInQueries(String test) {
+        super(test, "datacachecactusapp");
+    }
+
+    private BrokerFactory _factory;
+    private Object _eId;
+
+    public void setUp() {
+        deleteAll(CacheObjectA.class);
+
+        Map propsMap = new HashMap();
+        propsMap.put("openjpa.DataCache", "true");
+        propsMap.put("openjpa.RemoteCommitProvider", "sjvm");
+        propsMap.put("openjpa.BrokerImpl", CacheTestBroker.class.getName());
+        EntityManagerFactory emf = getEmf(propsMap);
+        _factory = JPAFacadeHelper.toBrokerFactory(emf);
+
+        Broker broker = _factory.newBroker();
+        broker.begin();
+        CacheObjectE e = new CacheObjectE("bar");
+        CacheObjectJ j = new CacheObjectJ("foo", e);
+        broker.persist(j, null);
+        broker.persist(e, null);
+        broker.commit();
+        _eId = broker.getObjectId(e);
+        broker.close();
+    }
+
+    public void testPCParameter() {
+        Broker broker = _factory.newBroker();
+        broker.begin();
+        CacheObjectE e = (CacheObjectE) broker.find(_eId, true, null);
+        Query q = broker.newQuery(JPQLParser.LANG_JPQL,
+            "select a from " +
+                CacheObjectJ.class.getSimpleName() + " a where a.e = :param");
+
+        Collection c = (Collection) q.execute(new Object[]{ e });
+        CacheTestHelper.iterate(c);
+        Object o = c.iterator().next();
+        assertTrue(o instanceof CacheObjectJ);
+        CacheTestHelper.assertInCache(this, q, Boolean.TRUE, new Object[]{ e });
+
+        q = broker.newQuery(JPQLParser.LANG_JPQL,
+            "select a from " +
+                CacheObjectJ.class.getSimpleName() + " a where a.e = :param");
+
+        c = (Collection) q.execute(new Object[]{ e });
+        Object o2 = c.iterator().next();
+        assertTrue(o2 instanceof CacheObjectJ);
+        assertTrue(o == o2);
+
+        broker.commit();
+        broker.close();
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestQueryResultTypes.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestQueryResultTypes.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestQueryResultTypes.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestQueryResultTypes.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,98 @@
+/*
+ * 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.openjpa.persistence.datacache;
+
+
+import org.apache.openjpa.persistence.datacache.common.apps.CacheObjectA;
+import org.apache.openjpa.persistence.datacache.common.apps.CacheObjectB;
+import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
+
+import org.apache.openjpa.kernel.Broker;
+import org.apache.openjpa.kernel.Query;
+import org.apache.openjpa.kernel.jpql.JPQLParser;
+import org.apache.openjpa.persistence.JPAFacadeHelper;
+
+/**
+ * Test some assumptions about query result metadata that the query
+ * cache relies on.
+ */
+public class TestQueryResultTypes extends AbstractTestCase {
+
+    public TestQueryResultTypes(String test) {
+        super(test, "datacachecactusapp");
+    }
+
+    public void testQueryResultTypesWithThis() {
+        Broker broker = getBroker();
+        Query q =
+            broker.newQuery(JPQLParser.LANG_JPQL, "select a.name,a FROM " +
+                CacheObjectA.class.getSimpleName() + " a");
+
+        Class[] types = q.getProjectionTypes();
+        assertEquals(2, types.length);
+        assertEquals(String.class, types[0]);
+        assertEquals(CacheObjectA.class, types[1]);
+    }
+
+    public void testQueryResultTypesWithRelated() {
+        Broker broker = getBroker();
+        Query q = broker
+            .newQuery(JPQLParser.LANG_JPQL, "select a.name,a.relatedB FROM " +
+                CacheObjectA.class.getSimpleName() + " a");
+
+        //CacheObjectA.class,           "select name, relatedB");
+        Class[] types = q.getProjectionTypes();
+        assertEquals(2, types.length);
+        assertEquals(String.class, types[0]);
+        assertEquals(CacheObjectB.class, types[1]);
+    }
+
+    public void testSingleProjectionOfNonThisRelation() {
+        Broker broker = getBroker();
+        Query q =
+            broker.newQuery(JPQLParser.LANG_JPQL, "select a.relatedB FROM " +
+                CacheObjectA.class.getSimpleName() + " a");
+
+        Class[] types = q.getProjectionTypes();
+        assertEquals(1, types.length);
+        assertEquals(CacheObjectB.class, types[0]);
+    }
+
+    public void testSingleProjectionOfThisRelation() {
+        Broker broker = getBroker();
+        Query q = broker.newQuery(JPQLParser.LANG_JPQL, "select a FROM " +
+            CacheObjectA.class.getSimpleName() + " a");
+        Class[] types = q.getProjectionTypes();
+        assertEquals(0, types.length);
+    }
+
+    public void testNoResultClause() {
+        Broker broker = getBroker();
+        Query q =
+            broker.newQuery(JPQLParser.LANG_JPQL, "select a FROM " +
+                CacheObjectA.class.getSimpleName() + " a");
+        Class[] types = q.getProjectionTypes();
+        assertEquals(0, types.length);
+    }
+
+    private Broker getBroker() {
+        return JPAFacadeHelper
+            .toBroker(currentEntityManager());
+    }
+}



Mime
View raw message