Author: mikedd
Date: Tue Jan 20 08:50:01 2009
New Revision: 736036

URL: http://svn.apache.org/viewvc?rev=736036&view=rev
Log:
OPENJPA-809 support JPA 2.0 cache interface.

Added:
    
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/CachedEmployee.java
    
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/CachedManager.java
    
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/CachedPerson.java
    
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestJPACache.java
Modified:
    
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/AbstractDataCache.java
    
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ConcurrentDataCache.java
    
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCache.java
    
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/CacheMap.java
    
openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java
    
openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/StoreCache.java
    
openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/StoreCacheImpl.java

Modified: 
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/AbstractDataCache.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/AbstractDataCache.java?rev=736036&r1=736035&r2=736036&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/AbstractDataCache.java
 (original)
+++ 
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/AbstractDataCache.java
 Tue Jan 20 08:50:01 2009
@@ -244,7 +244,7 @@
         return set;
     }
 
-    public void pinAll(Class cls, boolean subs) {
+    public void pinAll(Class<?> cls, boolean subs) {
         if (log.isWarnEnabled())
             log.warn(s_loc.get("cache-class-pin", getName()));
     }
@@ -272,7 +272,7 @@
         return set;
     }
 
-    public void unpinAll(Class cls, boolean subs) {
+    public void unpinAll(Class<?> cls, boolean subs) {
         if (log.isWarnEnabled())
             log.warn(s_loc.get("cache-class-unpin", getName()));
     }
@@ -388,7 +388,7 @@
     /**
      * Evict objects in cache by class.
      */
-    protected abstract void removeAllInternal(Class cls, boolean subclasses);
+    protected abstract void removeAllInternal(Class<?> cls, boolean 
subclasses);
 
     /**
      * Remove all objects under the given oids from the cache.

Modified: 
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ConcurrentDataCache.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ConcurrentDataCache.java?rev=736036&r1=736035&r2=736036&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ConcurrentDataCache.java
 (original)
+++ 
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ConcurrentDataCache.java
 Tue Jan 20 08:50:01 2009
@@ -35,10 +35,12 @@
     extends AbstractDataCache
     implements RemoteCommitListener {
 
+    private static final long serialVersionUID = 7331996968322793473L;
+
     private static final Localizer _loc = Localizer.forPackage
         (ConcurrentDataCache.class);
 
-    private final CacheMap _cache = newCacheMap();
+    private CacheMap _cache = newCacheMap();
 
     /**
      * Returns the underlying {...@link CacheMap} that this cache is using.
@@ -92,7 +94,7 @@
         conf.getRemoteCommitEventManager().addInternalListener(this);
     }
 
-    public void unpinAll(Class cls, boolean subs) {
+    public void unpinAll(Class<?> cls, boolean subs) {
         if (log.isWarnEnabled())
             log.warn(_loc.get("cache-class-unpin-all", getName()));
         unpinAll(_cache.getPinnedKeys());
@@ -131,12 +133,21 @@
         return (DataCachePCData) _cache.remove(key);
     }
 
-    protected void removeAllInternal(Class cls, boolean subs) {
-        // we could keep a histogram of the counts of contained classes and
-        // only clear if we have the class, but that still wouldn't support 
subs
-        // well, would involve synching, and won't yield much benefit when 
we're
-        // used as a primary cache
-        _cache.clear();
+    protected void removeAllInternal(Class<?> cls, boolean subs) {
+        // The performance in this area can be improved upon, however it seems
+        // unlikely that this method will be called in a performance intensive
+        // environment. In any event applications can revert to the old 
behavior
+        // by simply calling removeAll().
+        CacheMap orig = _cache;
+        _cache = newCacheMap(); 
+        for (Object o : orig.values()) {
+            Class<?> curClass = ((DataCachePCData) o).getType();
+            if (cls == curClass
+                || (curClass != null && curClass.isAssignableFrom(cls))) {
+                orig.remove(((DataCachePCData) o).getId());
+            }
+        }
+        _cache.putAll(orig, false);
     }
 
     protected void clearInternal() {

Modified: 
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCache.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCache.java?rev=736036&r1=736035&r2=736036&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCache.java
 (original)
+++ 
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCache.java
 Tue Jan 20 08:50:01 2009
@@ -170,7 +170,7 @@
     /**
      * Evict all values of a specified type.
      */
-    public void removeAll(Class cls, boolean subclasses);
+    public void removeAll(Class<?> cls, boolean subclasses);
 
     /**
      * Remove all data from this cache. This does <em>not</em> result
@@ -203,7 +203,7 @@
      * Pin all oids for the given type.
      * @param subs Whether to include subclasses.
      */
-    public void pinAll(Class cls, boolean subs);
+    public void pinAll(Class<?> cls, boolean subs);
 
     /**
      * Unpin the value stored under <code>oid</code> from the cache.
@@ -229,7 +229,7 @@
      * Unpin all oids associaed with the given type from the cache.
      * @param subs Whether to include subclasses.
      */
-    public void unpinAll(Class cls, boolean subs);
+    public void unpinAll(Class<?> cls, boolean subs);
 
     /**
      * Obtain a write lock on the cache.

Modified: 
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/CacheMap.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/CacheMap.java?rev=736036&r1=736035&r2=736036&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/CacheMap.java
 (original)
+++ 
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/CacheMap.java
 Tue Jan 20 08:50:01 2009
@@ -401,11 +401,17 @@
         }
     }
 
-    public void putAll(Map map) {
+    public void putAll(Map map) { 
+        putAll(map, true);
+    }
+    
+    public void putAll(Map map, boolean replaceExisting) {
         Map.Entry entry;
         for (Iterator itr = map.entrySet().iterator(); itr.hasNext();) {
             entry = (Map.Entry) itr.next();
-            put(entry.getKey(), entry.getValue());
+            if(replaceExisting || !containsKey(entry.getKey())) { 
+                put(entry.getKey(), entry.getValue());
+            }
         }
     }
 

Added: 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/CachedEmployee.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/CachedEmployee.java?rev=736036&view=auto
==============================================================================
--- 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/CachedEmployee.java
 (added)
+++ 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/CachedEmployee.java
 Tue Jan 20 08:50:01 2009
@@ -0,0 +1,37 @@
+/*
+ * 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 javax.persistence.Entity;
+import javax.persistence.ManyToOne;
+
+...@entity
+public class CachedEmployee extends CachedPerson {
+
+    @ManyToOne
+    private CachedManager manager;
+
+    public CachedManager getManager() {
+        return manager;
+    }
+
+    public void setManager(CachedManager manager) {
+        this.manager = manager;
+    }
+}

Added: 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/CachedManager.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/CachedManager.java?rev=736036&view=auto
==============================================================================
--- 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/CachedManager.java
 (added)
+++ 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/CachedManager.java
 Tue Jan 20 08:50:01 2009
@@ -0,0 +1,39 @@
+/*
+ * 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 javax.persistence.Entity;
+import javax.persistence.OneToMany;
+
+...@entity
+public class CachedManager extends CachedPerson {
+
+    @OneToMany(mappedBy="manager")
+    Collection<CachedEmployee> employees;
+
+    public Collection<CachedEmployee> getEmployees() {
+        return employees;
+    }
+
+    public void setEmployees(Collection<CachedEmployee> employees) {
+        this.employees = employees;
+    }
+}

Added: 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/CachedPerson.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/CachedPerson.java?rev=736036&view=auto
==============================================================================
--- 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/CachedPerson.java
 (added)
+++ 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/CachedPerson.java
 Tue Jan 20 08:50:01 2009
@@ -0,0 +1,66 @@
+/*
+ * 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 javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Inheritance;
+import javax.persistence.InheritanceType;
+import javax.persistence.Version;
+
+...@entity
+...@inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
+public class CachedPerson {
+
+    @Id
+    private int id;
+
+    @Version
+    private int version;
+
+    private String firstName, lastName;
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public String getFirstName() {
+        return firstName;
+    }
+
+    public void setFirstName(String firstName) {
+        this.firstName = firstName;
+    }
+
+    public String getLastName() {
+        return lastName;
+    }
+
+    public void setLastName(String lastName) {
+        this.lastName = lastName;
+    }
+
+    public int getVersion() {
+        return version;
+    }
+}

Added: 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestJPACache.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestJPACache.java?rev=736036&view=auto
==============================================================================
--- 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestJPACache.java
 (added)
+++ 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestJPACache.java
 Tue Jan 20 08:50:01 2009
@@ -0,0 +1,323 @@
+/*
+ * 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.ArrayList;
+
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.persistence.ArgumentException;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+public class TestJPACache extends SingleEMFTestCase {
+
+    public void setUp() {
+        super.setUp(CachedPerson.class, CachedManager.class,
+            CachedEmployee.class, "openjpa.DataCache", "true",
+            "openjpa.RemoteCommitProvider", "sjvm");
+    }
+
+    private void populate() {
+        EntityManager em = emf.createEntityManager();
+
+        em.getTransaction().begin();
+
+        CachedPerson p = new CachedPerson();
+        p.setFirstName("John");
+        p.setLastName("Doe");
+        p.setId(10);
+        em.persist(p);
+
+        p = new CachedPerson();
+        p.setFirstName("Jane");
+        p.setLastName("Doe");
+        p.setId(11);
+        em.persist(p);
+
+        CachedManager m = new CachedManager();
+        m.setFirstName("Joan");
+        m.setLastName("Baker");
+        m.setId(12);
+        m.setEmployees(new ArrayList<CachedEmployee>());
+        em.persist(m);
+
+        CachedEmployee e = new CachedEmployee();
+        e.setFirstName("Jim");
+        e.setFirstName("Smith");
+        e.setManager(m);
+        e.setId(13);
+        m.getEmployees().add(e);
+        em.persist(e);
+
+        e = new CachedEmployee();
+        e.setFirstName("Jeff");
+        e.setFirstName("Parker");
+        e.setId(14);
+        e.setManager(m);
+        m.getEmployees().add(e);
+        em.persist(e);
+
+        em.getTransaction().commit();
+
+        em.close();
+    }
+
+    /**
+     * Ensure the cached returned by emf.getCache supports the JPA and OpenJPA
+     * interfaces. Expected interfaces are
+     * <ul>
+     * <li>javax.persistence.Cache</li>
+     * <li>org.apache.openjpa.persistence.StoreCache</li>
+     * </ul>
+     */
+    public void testInterfacesReturned() {
+        Object cache = emf.getCache();
+        assertNotNull("Cache is not enabled", cache);
+        assertTrue(cache instanceof javax.persistence.Cache);
+        assertTrue(cache instanceof org.apache.openjpa.persistence.StoreCache);
+    }
+
+    /**
+     * Ensure that an Entity is not inserted in the cache until the transaction
+     * commits.
+     */
+    public void testContains() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+
+        CachedPerson p = new CachedPerson();
+        p.setFirstName("John");
+        p.setLastName("Doe");
+        p.setId(1);
+
+        assertNotInCache(CachedPerson.class, 1);
+        em.persist(p);
+        assertNotInCache(CachedPerson.class, 1);
+
+        em.flush();
+        assertNotInCache(CachedPerson.class, 1);
+
+        em.getTransaction().commit();
+        assertInCache(CachedPerson.class, 1);
+
+        em.close();
+    }
+
+    /**
+     * Evict a single entity instance of type CachedPerson from the cache.
+     * Testcase will fail if
+     * <ul>
+     * <li>CachedPerson id:10 and 11 are not in the cache when the test starts.
+     * </li>
+     * <li>CachedPerson id:1 <b>is</b> found in the cache when the test starts.
+     * </li>
+     * <li>CachedPerson id:10 is not found in the cache after evicting
+     * CachedPerson id:11</li>
+     * <li>CachedPerson id:11 is found in the cache after evicting CachedPerson
+     * id:11</li>
+     * <li>CachedPerson id:1 is found in the cache after evicting CachedPerson
+     * id:11</li>
+     * </ul>
+     */
+    public void testEvictInstance() {
+        populate();
+        assertInCache(CachedPerson.class, 10);
+        assertInCache(CachedPerson.class, 11);
+        assertNotInCache(CachedPerson.class, 1);
+
+        emf.getCache().evict(CachedPerson.class, 11);
+
+        assertInCache(CachedPerson.class, 10);
+        assertNotInCache(CachedPerson.class, 11);
+        assertNotInCache(CachedPerson.class, 1);
+    }
+
+    /**
+     * Ensure that evict(Class cls) evicts only the specified class. This test
+     * will fail if
+     * <ul>
+     * <li>Any of the entites created in populate() are not found in the cache
+     * before calling evict()</li>
+     * <li>Any instance of CachedPerson from populate() is found in the cache
+     * after calling evict(CachedPerson.class)</li>
+     * <li>Any instance of CachedManager or CachedEmployee which previously
+     * existed in the cache was evicted after calling 
evict(CachedPerson.class)</li>
+     * </ul>
+     * 
+     */
+    public void testEvictClass() {
+        populate();
+        assertInCache(CachedPerson.class, 10);
+        assertInCache(CachedPerson.class, 11);
+        assertInCache(CachedEmployee.class, 13);
+        assertInCache(CachedEmployee.class, 14);
+        assertInCache(CachedManager.class, 12);
+
+        emf.getCache().evict(CachedPerson.class);
+
+        assertNotInCache(CachedPerson.class, 10);
+        assertNotInCache(CachedPerson.class, 11);
+        assertInCache(CachedEmployee.class, 13);
+        assertInCache(CachedEmployee.class, 14);
+        assertInCache(CachedManager.class, 12);
+
+    }
+
+    /**
+     * Ensure the cache is cleared after calling evictAll. This test will fail
+     * if :
+     * <ul>
+     * <li>Any of the entities created in populate() are not found in the cache
+     * </li>
+     * <li>Any of the entities which were in the cache before calling 
evictAll()
+     * are still in the cache after calling evictAll()</li>
+     * </ul>
+     * 
+     */
+    public void testEvictAll() {
+        populate();
+
+        assertInCache(CachedPerson.class, 10);
+        assertInCache(CachedPerson.class, 11);
+        assertInCache(CachedEmployee.class, 13);
+        assertInCache(CachedEmployee.class, 14);
+        assertInCache(CachedManager.class, 12);
+
+        emf.getCache().evictAll();
+
+        assertNotInCache(CachedPerson.class, 10);
+        assertNotInCache(CachedPerson.class, 11);
+        assertNotInCache(CachedEmployee.class, 13);
+        assertNotInCache(CachedEmployee.class, 14);
+        assertNotInCache(CachedManager.class, 12);
+    }
+
+    // test methods for bad input.
+    public void testContainsNullEntity() {
+        try {
+            emf.getCache().contains(null, 1);
+            fail("Expected ArgumentException when calling  "
+                + "contains(<null>, <nonNull>)");
+        } catch (ArgumentException ae) {
+            // normal
+        }
+    }
+
+    public void testContainsNonEntityClass() {
+        try {
+            emf.getCache().contains(Object.class, 1);
+            fail("Expected ArgumentException when calling "
+                + "contains(<nonEntityClass>, <nonNull>");
+        } catch (ArgumentException ae) {
+            // expected exception
+        }
+    }
+
+    public void testContainsNullPrimaryKey() {
+        assertFalse(emf.getCache().contains(CachedPerson.class, null));
+    }
+
+    public void testContainsNegativePrimaryKey() {
+        assertFalse(emf.getCache().contains(CachedPerson.class, -1));
+    }
+
+    public void testContainsInvalidPrimaryKeyType() {
+        assertFalse(emf.getCache().contains(CachedPerson.class, "abcd"));
+    }
+
+    public void testEvictNullInstance() {
+        try {
+            emf.getCache().evict(null, 1);
+            fail("Expected ArgumentException when calling "
+                + "evict(<null>, <id");
+        } catch (ArgumentException ae) {
+            // expected exception
+        }
+    }
+
+    public void testEvictNonEntityInstance() {
+        try {
+            emf.getCache().evict(Object.class, 1);
+            fail("Expected ArgumentException when calling "
+                + "evict(<null>, <id");
+        } catch (ArgumentException ae) {
+            // expected exception
+        }
+    }
+
+    public void testEvictNullPrimaryKey() {
+        emf.getCache().evict(CachedPerson.class, null);
+    }
+
+    public void testEvictNegativePrimaryKey() {
+        emf.getCache().evict(CachedPerson.class, -1);
+    }
+
+    public void testEvictInvalidPrimaryKeyType() {
+        emf.getCache().evict(CachedPerson.class, "abcd");
+    }
+
+    public void testEvictNullClass() {
+        try {
+            emf.getCache().evict(null);
+            fail("Expected ArgumentException when calling " + "evict(<null>");
+        } catch (ArgumentException ae) {
+            // expected exception
+        }
+    }
+
+    public void testEvictNonEntity() {
+        try {
+            emf.getCache().evict(Object.class);
+            fail("Expected ArgumentException when calling "
+                + "evict(<nonEntity>");
+        } catch (ArgumentException ae) {
+            // expected exception
+        }
+    }
+
+    /**
+     * Convenience method. Asserts that the class & primary key do exist in the
+     * cache
+     * 
+     * @param cls
+     *            Entity class.
+     * @param primaryKey
+     *            PrimaryKey of the entity.
+     */
+    private void assertInCache(Class<?> cls, int primaryKey) {
+        assertTrue(
+            "Expected " + cls + ":" + primaryKey + " to exist in cache ", emf
+                .getCache().contains(cls, primaryKey));
+    }
+
+    /**
+     * Convenience method. Assert that the class and primary key do not exist 
in
+     * the cache
+     * 
+     * @param cls
+     *            Entity class.
+     * @param primaryKey
+     *            PrimaryKey of the entity.
+     */
+    private void assertNotInCache(Class<?> cls, Object primaryKey) {
+        assertFalse(emf.getCache().contains(cls, primaryKey));
+    }
+
+}

Modified: 
openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java?rev=736036&r1=736035&r2=736036&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java
 (original)
+++ 
openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java
 Tue Jan 20 08:50:01 2009
@@ -342,8 +342,7 @@
        }
 
     public Cache getCache() {
-        throw new UnsupportedOperationException(
-            "JPA 2.0 - Method not yet implemented");
+        return getStoreCache();
     }
 
     public OpenJPAQueryBuilder getQueryBuilder() {

Modified: 
openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/StoreCache.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/StoreCache.java?rev=736036&r1=736035&r2=736036&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/StoreCache.java
 (original)
+++ 
openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/StoreCache.java
 Tue Jan 20 08:50:01 2009
@@ -20,6 +20,8 @@
 
 import java.util.Collection;
 
+import javax.persistence.Cache;
+
 import org.apache.openjpa.datacache.CacheStatistics;
 import org.apache.openjpa.datacache.DataCache;
 
@@ -30,7 +32,7 @@
  * @since 0.4.1
  * @published
  */
-public interface StoreCache {
+public interface StoreCache extends Cache {
 
     public static final String NAME_DEFAULT = DataCache.NAME_DEFAULT;
 

Modified: 
openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/StoreCacheImpl.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/StoreCacheImpl.java?rev=736036&r1=736035&r2=736036&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/StoreCacheImpl.java
 (original)
+++ 
openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/StoreCacheImpl.java
 Tue Jan 20 08:50:01 2009
@@ -158,4 +158,13 @@
             return false;
         return _cache.equals (((StoreCacheImpl) other)._cache);
        }
+
+    @SuppressWarnings("unchecked")
+    public void evict(Class cls) {
+        // Check MetaData throws a consistent exception with evict(Class,
+        // Object)
+        if(getMetaData(cls) != null) {
+            _cache.removeAll(cls, false);
+        }
+    }
 }


Reply via email to