[jboss-cvs] JBossAS SVN: r77547 - in projects/cluster/hibernate-jbc-cacheprovider/trunk: src/test/java/org/jboss/hibernate/jbc/cacheprovider/functional and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Aug 27 15:09:51 EDT 2008


Author: bstansberry at jboss.com
Date: 2008-08-27 15:09:50 -0400 (Wed, 27 Aug 2008)
New Revision: 77547

Added:
   projects/cluster/hibernate-jbc-cacheprovider/trunk/src/test/java/org/jboss/hibernate/jbc/cacheprovider/functional/CustomerContactDAO.java
   projects/cluster/hibernate-jbc-cacheprovider/trunk/src/test/java/org/jboss/hibernate/jbc/cacheprovider/functional/OptimisticQueryCacheInvalidationTestCase.java
   projects/cluster/hibernate-jbc-cacheprovider/trunk/src/test/java/org/jboss/hibernate/jbc/cacheprovider/functional/PessimisticQueryCacheInvalidationTestCase.java
Modified:
   projects/cluster/hibernate-jbc-cacheprovider/trunk/pom.xml
   projects/cluster/hibernate-jbc-cacheprovider/trunk/src/test/java/org/jboss/hibernate/jbc/cacheprovider/functional/PessimisticEntityReplicationNoLocalPutsOnlyTestCase.java
   projects/cluster/hibernate-jbc-cacheprovider/trunk/src/test/java/org/jboss/hibernate/jbc/cacheprovider/functional/PessimisticEntityReplicationTestCase.java
Log:
[JBCLUSTER-206] Add test of query invalidation

Modified: projects/cluster/hibernate-jbc-cacheprovider/trunk/pom.xml
===================================================================
--- projects/cluster/hibernate-jbc-cacheprovider/trunk/pom.xml	2008-08-27 19:06:44 UTC (rev 77546)
+++ projects/cluster/hibernate-jbc-cacheprovider/trunk/pom.xml	2008-08-27 19:09:50 UTC (rev 77547)
@@ -48,6 +48,7 @@
     <version.jboss.javaee.persistence.api>3.0.0.CR1</version.jboss.javaee.persistence.api>
     <version.jboss.serialization>1.0.3.GA</version.jboss.serialization>
     <version.trove>1.0.2</version.trove>    
+    <version.antlr>2.7.6</version.antlr>    
     <!-- 
          Following is the default jgroups mcast address.  If you find the testsuite runs very slowly, there
          may be problems with multicast on the interface JGroups uses by default on your machine. You can
@@ -247,7 +248,13 @@
        <artifactId>trove</artifactId>
        <version>${version.trove}</version>
        <scope>test</scope>
-    </dependency>        
+    </dependency>
+    <dependency>
+       <groupId>antlr</groupId>
+       <artifactId>antlr</artifactId>
+       <version>${version.antlr}</version>
+       <scope>test</scope>
+    </dependency>
   </dependencies> 
 
 </project>
\ No newline at end of file

Added: projects/cluster/hibernate-jbc-cacheprovider/trunk/src/test/java/org/jboss/hibernate/jbc/cacheprovider/functional/CustomerContactDAO.java
===================================================================
--- projects/cluster/hibernate-jbc-cacheprovider/trunk/src/test/java/org/jboss/hibernate/jbc/cacheprovider/functional/CustomerContactDAO.java	                        (rev 0)
+++ projects/cluster/hibernate-jbc-cacheprovider/trunk/src/test/java/org/jboss/hibernate/jbc/cacheprovider/functional/CustomerContactDAO.java	2008-08-27 19:09:50 UTC (rev 77547)
@@ -0,0 +1,256 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.hibernate.jbc.cacheprovider.functional;
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import javax.transaction.TransactionManager;
+
+import org.hibernate.FlushMode;
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+import org.jboss.logging.Logger;
+
+/**
+ * DAO to allow code reuse between functional tests that manipulate Customers
+ * and Contacts as part of the test.
+ * 
+ * @author Brian Stansberry
+ */
+public class CustomerContactDAO
+{
+   private static final Logger log = Logger.getLogger(CustomerContactDAO.class);
+   
+   public IdContainer createCustomer(SessionFactory sessionFactory, TransactionManager tm)
+      throws Exception
+   {
+      System.out.println("CREATE CUSTOMER");
+      
+      tm.begin(); 
+
+      try
+      {
+         Session session = sessionFactory.getCurrentSession();
+         
+         Customer customer = new Customer();
+         customer.setName("JBoss");
+         Set<Contact> contacts = new HashSet<Contact>();
+         
+         Contact kabir = new Contact();
+         kabir.setCustomer(customer);
+         kabir.setName("Kabir");
+         kabir.setTlf("1111");
+         contacts.add(kabir);
+         
+         Contact bill = new Contact();
+         bill.setCustomer(customer);
+         bill.setName("Bill");
+         bill.setTlf("2222");
+         contacts.add(bill);
+
+         customer.setContacts(contacts);
+
+         session.save(customer);
+         tm.commit();
+         
+         IdContainer ids = new IdContainer();
+         ids.customerId = customer.getId();
+         Set<Integer> contactIds = new HashSet<Integer>();
+         contactIds.add(kabir.getId());
+         contactIds.add(bill.getId());
+         ids.contactIds = contactIds;
+         
+         return ids;
+      }
+      catch (Exception e)
+      {
+         log.error("Caught exception creating customer", e);
+         try {
+            tm.rollback();
+         }
+         catch (Exception e1) {
+            log.error("Exception rolling back txn", e1);
+         }
+         throw e;
+      }
+      finally
+      {
+         System.out.println("CREATE CUSTOMER -  END");         
+      }
+   }
+
+   public Customer getCustomer(Integer id, SessionFactory sessionFactory, TransactionManager tm)
+       throws Exception
+   {      
+      System.out.println("FIND CUSTOMER");
+      
+      tm.begin();
+      try
+      {
+         Session session = sessionFactory.getCurrentSession();
+         Customer customer = (Customer) session.get(Customer.class, id);
+         // Access all the contacts
+         for (Iterator<Contact> it = customer.getContacts().iterator(); it.hasNext();) {
+            ((Contact) it.next()).getName();
+         }
+         tm.commit();
+         return customer;
+      }
+      catch (Exception e)
+      {
+         try {
+            tm.rollback();
+         }
+         catch (Exception e1) {
+            log.error("Exception rolling back txn", e1);
+         }
+         throw e;
+      }
+      finally
+      {
+         System.out.println("FIND CUSTOMER -  END");         
+      }
+   }
+   
+   public void updateCustomerForContact(Contact contact, Customer newCustomer, SessionFactory sessionFactory, TransactionManager tm) throws Exception
+   {
+      System.out.println("UPDATE CONTACT");
+      
+      tm.begin(); 
+
+      try
+      {
+         Session session = sessionFactory.getCurrentSession();
+         Customer oldCustomer = contact.getCustomer();
+         oldCustomer.getContacts().remove(contact);
+         contact.setCustomer(newCustomer);
+         newCustomer.getContacts().add(contact);
+
+         session.update(oldCustomer);
+         session.update(newCustomer);
+         
+         session.update(contact);
+         
+         tm.commit();
+      }
+      catch (Exception e)
+      {
+         log.error("Caught exception updating contact", e);
+         try {
+            tm.rollback();
+         }
+         catch (Exception e1) {
+            log.error("Exception rolling back txn", e1);
+         }
+         throw e;
+      }
+      finally
+      {
+         System.out.println("CREATE CUSTOMER -  END");         
+      }
+      
+   }
+   
+   @SuppressWarnings("unchecked")
+   public List<Integer> getContactsForCustomer(Integer customerId, SessionFactory sessionFactory, TransactionManager tm) throws Exception
+   {
+      System.out.println("GET CONTACTS FOR CUSTOMER");
+      String selectHQL = "select contact.id from Contact contact";
+      selectHQL += " where contact.customer.id = :cId";
+      
+      tm.begin(); 
+
+      try
+      {
+         Session session = sessionFactory.getCurrentSession();
+         
+         List<Integer> results = (List<Integer>) session.createQuery(selectHQL)
+                                                        .setFlushMode(FlushMode.AUTO)
+                                                        .setParameter("cId", customerId)
+                                                        .setCacheable(true).list();
+         
+         tm.commit();
+         
+         return results;
+      }
+      catch (Exception e)
+      {
+         log.error("Caught exception querying contacts", e);
+         try {
+            tm.rollback();
+         }
+         catch (Exception e1) {
+            log.error("Exception rolling back txn", e1);
+         }
+         throw e;
+      }
+      finally
+      {
+         System.out.println("CREATE CUSTOMER -  END");         
+      }
+      
+      
+   }
+   
+   public void cleanup(Set<Integer> customerIds, SessionFactory sessionFactory, TransactionManager tm) throws Exception
+   {
+      for (Integer customerId : customerIds)
+      {
+         tm.begin();
+         try
+         {
+            Session session = sessionFactory.getCurrentSession();
+            Customer c = (Customer) session.get(Customer.class, customerId);
+            if (c != null)
+            {
+               for (Contact contact :  c.getContacts())
+                  session.delete(contact);
+               c.setContacts(null);
+               session.delete(c);
+            }
+            
+            tm.commit();
+         }
+         catch (Exception e)
+         {
+            try {
+               tm.rollback();
+            }
+            catch (Exception e1) {
+               log.error("Exception rolling back txn", e1);
+            }
+            log.error("Caught exception in cleanup", e);
+         }
+      }
+   }
+
+   
+   public class IdContainer 
+   {
+      Integer customerId;
+      Set<Integer> contactIds;
+   }
+}

Added: projects/cluster/hibernate-jbc-cacheprovider/trunk/src/test/java/org/jboss/hibernate/jbc/cacheprovider/functional/OptimisticQueryCacheInvalidationTestCase.java
===================================================================
--- projects/cluster/hibernate-jbc-cacheprovider/trunk/src/test/java/org/jboss/hibernate/jbc/cacheprovider/functional/OptimisticQueryCacheInvalidationTestCase.java	                        (rev 0)
+++ projects/cluster/hibernate-jbc-cacheprovider/trunk/src/test/java/org/jboss/hibernate/jbc/cacheprovider/functional/OptimisticQueryCacheInvalidationTestCase.java	2008-08-27 19:09:50 UTC (rev 77547)
@@ -0,0 +1,52 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.hibernate.jbc.cacheprovider.functional;
+
+import org.hibernate.cfg.Configuration;
+import org.hibernate.cfg.Environment;
+
+/**
+ * @author Brian Stansberry
+ *
+ */
+public class OptimisticQueryCacheInvalidationTestCase extends PessimisticQueryCacheInvalidationTestCase
+{
+
+   /**
+    * Create a new OptimisticQueryCacheInvalidationTestCase.
+    * 
+    * @param x
+    * @param cacheMode
+    */
+   public OptimisticQueryCacheInvalidationTestCase(String name)
+   {
+      super(name);
+   }
+
+   @Override
+   protected void configureCacheFactory(Configuration cfg)
+   {
+      cfg.setProperty(Environment.CACHE_PROVIDER_CONFIG, OptimisticJBossCacheTestCase.JBC_CONFIG);
+      cfg.setProperty(Environment.CACHE_PROVIDER, OptimisticJBossCacheTestCase.JBC_CACHE_PROVIDER);      
+   }
+}

Modified: projects/cluster/hibernate-jbc-cacheprovider/trunk/src/test/java/org/jboss/hibernate/jbc/cacheprovider/functional/PessimisticEntityReplicationNoLocalPutsOnlyTestCase.java
===================================================================
--- projects/cluster/hibernate-jbc-cacheprovider/trunk/src/test/java/org/jboss/hibernate/jbc/cacheprovider/functional/PessimisticEntityReplicationNoLocalPutsOnlyTestCase.java	2008-08-27 19:06:44 UTC (rev 77546)
+++ projects/cluster/hibernate-jbc-cacheprovider/trunk/src/test/java/org/jboss/hibernate/jbc/cacheprovider/functional/PessimisticEntityReplicationNoLocalPutsOnlyTestCase.java	2008-08-27 19:09:50 UTC (rev 77547)
@@ -21,6 +21,9 @@
  */
 package org.jboss.hibernate.jbc.cacheprovider.functional;
 
+import java.util.HashSet;
+import java.util.Set;
+
 import javax.transaction.TransactionManager;
 
 import org.hibernate.SessionFactory;
@@ -28,6 +31,7 @@
 import org.hibernate.cfg.Environment;
 import org.jboss.cache.TreeCache;
 import org.jboss.hibernate.jbc.cacheprovider.CacheProperties;
+import org.jboss.hibernate.jbc.cacheprovider.functional.CustomerContactDAO.IdContainer;
 import org.jboss.hibernate.jbc.cacheprovider.functional.util.DualNodeTestUtil;
 import org.jboss.hibernate.jbc.cacheprovider.functional.util.TestCacheInstanceManager;
 
@@ -72,10 +76,14 @@
       SessionFactory localFactory = getEnvironment().getSessionFactory();
       SessionFactory remoteFactory = getSecondNodeEnvironment().getSessionFactory();
       
+      CustomerContactDAO dao = new CustomerContactDAO();
+      Set<Integer> createdCustomerIds = new HashSet<Integer>();
+      
       try
       {
          System.out.println("Create node 0");
-         IdContainer ids = createCustomer(localFactory, localTM);
+         IdContainer ids = dao.createCustomer(localFactory, localTM);
+         createdCustomerIds.add(ids.customerId);
          
          // Sleep a bit to let async commit propagate. Really just to
          // help keep the logs organized for debugging any issues
@@ -83,7 +91,7 @@
          
          System.out.println("Find node 0");
          // This actually brings the collection into the cache
-         getCustomer(ids.customerId, localFactory, localTM);
+         dao.getCustomer(ids.customerId, localFactory, localTM);
          
          sleep(SLEEP_TIME);
          
@@ -91,14 +99,14 @@
          // should read everything from the cache
          System.out.println("Find(2) node 0");         
          localListener.clear();
-         getCustomer(ids.customerId, localFactory, localTM);
+         dao.getCustomer(ids.customerId, localFactory, localTM);
          
          //Check the read came from the cache
          System.out.println("Check cache 0");
          assertLoadedFromCache(localListener, ids.customerId, ids.contactIds);
          
          System.out.println("Find node 1");
-         getCustomer(ids.customerId, remoteFactory, remoteTM);
+         dao.getCustomer(ids.customerId, remoteFactory, remoteTM);
    
          //Check everything was in cache
          System.out.println("Check cache 1");
@@ -108,7 +116,7 @@
       {
          // cleanup the db
          System.out.println("Cleaning up");
-         cleanup(localFactory, localTM);
+         dao.cleanup(createdCustomerIds, localFactory, localTM);
       }
    }
 }

Modified: projects/cluster/hibernate-jbc-cacheprovider/trunk/src/test/java/org/jboss/hibernate/jbc/cacheprovider/functional/PessimisticEntityReplicationTestCase.java
===================================================================
--- projects/cluster/hibernate-jbc-cacheprovider/trunk/src/test/java/org/jboss/hibernate/jbc/cacheprovider/functional/PessimisticEntityReplicationTestCase.java	2008-08-27 19:06:44 UTC (rev 77546)
+++ projects/cluster/hibernate-jbc-cacheprovider/trunk/src/test/java/org/jboss/hibernate/jbc/cacheprovider/functional/PessimisticEntityReplicationTestCase.java	2008-08-27 19:09:50 UTC (rev 77547)
@@ -27,21 +27,15 @@
 import java.util.Iterator;
 import java.util.Set;
 
-import javax.management.ObjectName;
 import javax.transaction.TransactionManager;
 
-import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.hibernate.cfg.Configuration;
 import org.hibernate.cfg.Environment;
-import org.hibernate.transaction.CMTTransactionFactory;
-import org.hibernate.transaction.TransactionManagerLookup;
-import org.hibernate.transaction.TransactionManagerLookupFactory;
 import org.jboss.cache.AbstractTreeCacheListener;
 import org.jboss.cache.Fqn;
-import org.jboss.cache.PropertyConfigurator;
 import org.jboss.cache.TreeCache;
-import org.jboss.hibernate.jbc.cacheprovider.TreeCacheProvider;
+import org.jboss.hibernate.jbc.cacheprovider.functional.CustomerContactDAO.IdContainer;
 import org.jboss.hibernate.jbc.cacheprovider.functional.util.DualNodeTestUtil;
 import org.jboss.hibernate.jbc.cacheprovider.functional.util.TestCacheInstanceManager;
 import org.jboss.logging.Logger;
@@ -60,8 +54,6 @@
 
    protected static final long SLEEP_TIME = 50l;
    
-   private static final Integer CUSTOMER_ID = new Integer(1);
-   
    static int test = 0;
    
    public PessimisticEntityReplicationTestCase(String name)
@@ -103,10 +95,13 @@
       SessionFactory localFactory = getEnvironment().getSessionFactory();
       SessionFactory remoteFactory = getSecondNodeEnvironment().getSessionFactory();
       
+      CustomerContactDAO dao = new CustomerContactDAO();
+      Set<Integer> createdCustomerIds = new HashSet<Integer>();
       try
       {
          System.out.println("Create node 0");
-         IdContainer ids = createCustomer(localFactory, localTM);
+         IdContainer ids = dao.createCustomer(localFactory, localTM);
+         createdCustomerIds.add(ids.customerId);
          
          // Sleep a bit to let async commit propagate. Really just to
          // help keep the logs organized for debugging any issues
@@ -114,7 +109,7 @@
          
          System.out.println("Find node 0");
          // This actually brings the collection into the cache
-         getCustomer(ids.customerId, localFactory, localTM);
+         dao.getCustomer(ids.customerId, localFactory, localTM);
          
          sleep(SLEEP_TIME);
          
@@ -122,7 +117,7 @@
          // should read everything from the cache
          System.out.println("Find(2) node 0");         
          localListener.clear();
-         getCustomer(ids.customerId, localFactory, localTM);
+         dao.getCustomer(ids.customerId, localFactory, localTM);
          
          //Check the read came from the cache
          System.out.println("Check cache 0");
@@ -133,13 +128,13 @@
          // cache. IOW, putting the customer into the cache in node 0 does not 
          // result on the customer being already available in node 1, we need to
          // get it.
-         getCustomer(ids.customerId, remoteFactory, remoteTM);
+         dao.getCustomer(ids.customerId, remoteFactory, remoteTM);
          
          sleep(SLEEP_TIME);
          
          System.out.println("Find(2) node 1");
          remoteListener.clear();
-         getCustomer(ids.customerId, remoteFactory, remoteTM);
+         dao.getCustomer(ids.customerId, remoteFactory, remoteTM);
    
          //Check everything was in cache
          System.out.println("Check cache 1");
@@ -149,131 +144,10 @@
       {
          // cleanup the db
          System.out.println("Cleaning up");
-         cleanup(localFactory, localTM);
+         dao.cleanup(createdCustomerIds, localFactory, localTM);
       }
    }
    
-   protected IdContainer createCustomer(SessionFactory sessionFactory, TransactionManager tm)
-      throws Exception
-   {
-      System.out.println("CREATE CUSTOMER");
-      
-      tm.begin(); 
-
-      try
-      {
-         Session session = sessionFactory.getCurrentSession();
-         
-         Customer customer = new Customer();
-         customer.setName("JBoss");
-         Set<Contact> contacts = new HashSet<Contact>();
-         
-         Contact kabir = new Contact();
-         kabir.setCustomer(customer);
-         kabir.setName("Kabir");
-         kabir.setTlf("1111");
-         contacts.add(kabir);
-         
-         Contact bill = new Contact();
-         bill.setCustomer(customer);
-         bill.setName("Bill");
-         bill.setTlf("2222");
-         contacts.add(bill);
-
-         customer.setContacts(contacts);
-
-         session.save(customer);
-         tm.commit();
-         
-         IdContainer ids = new IdContainer();
-         ids.customerId = customer.getId();
-         Set contactIds = new HashSet();
-         contactIds.add(kabir.getId());
-         contactIds.add(bill.getId());
-         ids.contactIds = contactIds;
-         
-         return ids;
-      }
-      catch (Exception e)
-      {
-         log.error("Caught exception creating customer", e);
-         try {
-            tm.rollback();
-         }
-         catch (Exception e1) {
-            log.error("Exception rolling back txn", e1);
-         }
-         throw e;
-      }
-      finally
-      {
-         System.out.println("CREATE CUSTOMER -  END");         
-      }
-   }
-
-   protected Customer getCustomer(Integer id, SessionFactory sessionFactory, TransactionManager tm)
-       throws Exception
-   {      
-      System.out.println("FIND CUSTOMER");
-      
-      tm.begin();
-      try
-      {
-         Session session = sessionFactory.getCurrentSession();
-         Customer customer = (Customer) session.get(Customer.class, id);
-         // Access all the contacts
-         for (Iterator it = customer.getContacts().iterator(); it.hasNext();) {
-            ((Contact) it.next()).getName();
-         }
-         tm.commit();
-         return customer;
-      }
-      catch (Exception e)
-      {
-         try {
-            tm.rollback();
-         }
-         catch (Exception e1) {
-            log.error("Exception rolling back txn", e1);
-         }
-         throw e;
-      }
-      finally
-      {
-         System.out.println("FIND CUSTOMER -  END");         
-      }
-   }
-   
-   protected void cleanup(SessionFactory sessionFactory, TransactionManager tm) throws Exception
-   {
-      tm.begin();
-      try
-      {
-         Session session = sessionFactory.getCurrentSession();
-         Customer c = (Customer) session.get(Customer.class, CUSTOMER_ID);
-         if (c != null)
-         {
-            Set contacts = c.getContacts();
-            for (Iterator it = contacts.iterator(); it.hasNext();)
-               session.delete(it.next());
-            c.setContacts(null);
-            session.delete(c);
-         }
-         
-         tm.commit();
-      }
-      catch (Exception e)
-      {
-         try {
-            tm.rollback();
-         }
-         catch (Exception e1) {
-            log.error("Exception rolling back txn", e1);
-         }
-         log.error("Caught exception in cleanup", e);
-      }
-   }
-   
    protected void assertLoadedFromCache(MyListener listener, Integer custId, Set contactIds)
    {
       assertTrue("Customer#" + custId + " was in cache", listener.visited.contains("Customer#" + custId));
@@ -314,10 +188,4 @@
          }
       }
    }
-   
-   class IdContainer 
-   {
-      Integer customerId;
-      Set contactIds;
-   }
 }

Added: projects/cluster/hibernate-jbc-cacheprovider/trunk/src/test/java/org/jboss/hibernate/jbc/cacheprovider/functional/PessimisticQueryCacheInvalidationTestCase.java
===================================================================
--- projects/cluster/hibernate-jbc-cacheprovider/trunk/src/test/java/org/jboss/hibernate/jbc/cacheprovider/functional/PessimisticQueryCacheInvalidationTestCase.java	                        (rev 0)
+++ projects/cluster/hibernate-jbc-cacheprovider/trunk/src/test/java/org/jboss/hibernate/jbc/cacheprovider/functional/PessimisticQueryCacheInvalidationTestCase.java	2008-08-27 19:09:50 UTC (rev 77547)
@@ -0,0 +1,189 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.hibernate.jbc.cacheprovider.functional;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import javax.transaction.TransactionManager;
+
+import org.hibernate.SessionFactory;
+import org.hibernate.cache.StandardQueryCache;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.cfg.Environment;
+import org.jboss.cache.AbstractTreeCacheListener;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.TreeCache;
+import org.jboss.hibernate.jbc.cacheprovider.functional.CustomerContactDAO.IdContainer;
+import org.jboss.hibernate.jbc.cacheprovider.functional.PessimisticEntityReplicationTestCase.MyListener;
+import org.jboss.hibernate.jbc.cacheprovider.functional.util.DualNodeTestUtil;
+import org.jboss.hibernate.jbc.cacheprovider.functional.util.TestCacheInstanceManager;
+import org.jboss.logging.Logger;
+
+/**
+ * @author Brian Stansberry
+ *
+ */
+public class PessimisticQueryCacheInvalidationTestCase extends DualNodeTestCaseBase
+{
+   private static final Logger log = Logger.getLogger(PessimisticQueryCacheInvalidationTestCase.class);
+
+   protected static final long SLEEP_TIME = 50l;
+   
+   private static final Integer CUSTOMER_ID = new Integer(1);
+   
+   static int test = 0;
+
+   /**
+    * Create a new PessimisticQueryCacheInvalidationTestCase.
+    * 
+    * @param x
+    * @param cacheMode
+    */
+   public PessimisticQueryCacheInvalidationTestCase(String name)
+   {
+      super(name, TreeCache.REPL_SYNC);
+   }
+
+   @Override
+   protected boolean getUseQueryCache()
+   {
+      return true;
+   }
+
+   @Override
+   protected void configureCacheFactory(Configuration cfg)
+   {
+      cfg.setProperty(Environment.CACHE_PROVIDER_CONFIG, PessimisticJBossCacheTestCase.JBC_CONFIG);
+      cfg.setProperty(Environment.CACHE_PROVIDER, PessimisticJBossCacheTestCase.JBC_CACHE_PROVIDER);      
+   }
+   
+   public void testRemoteQueryCacheInvalidation() throws Exception
+   {
+      System.out.println("*** " + getName());
+      
+      // Our region factory makes its CacheManager available to us
+      TreeCache localCache = TestCacheInstanceManager.getTreeCache(DualNodeTestUtil.LOCAL);
+      TransactionManager localTM = localCache.getTransactionManager();
+      
+      // Bind a listener to the "remote" cache
+      TreeCache remoteCache = TestCacheInstanceManager.getTreeCache(DualNodeTestUtil.REMOTE);
+      MyListener remoteListener = new MyListener();
+      remoteCache.addTreeCacheListener(remoteListener);      
+      TransactionManager remoteTM = remoteCache.getTransactionManager();
+      
+      SessionFactory localFactory = getEnvironment().getSessionFactory();
+      SessionFactory remoteFactory = getSecondNodeEnvironment().getSessionFactory();
+      
+      CustomerContactDAO dao = new CustomerContactDAO();
+      Set<Integer> createdCustomerIds = new HashSet<Integer>();
+      
+      try
+      {
+         // Set up entities
+         IdContainer ids0 = dao.createCustomer(localFactory, localTM);
+         createdCustomerIds.add(ids0.customerId);
+         
+         assertTrue(ids0.contactIds.size() > 0);
+         
+         IdContainer ids1 = dao.createCustomer(localFactory, localTM);
+         createdCustomerIds.add(ids0.customerId);
+         
+         Customer customer0 = dao.getCustomer(ids0.customerId, localFactory, localTM);
+         Customer customer1 = dao.getCustomer(ids1.customerId, localFactory, localTM);
+         
+         // Execute and cache a query on remote node
+         List<Integer> contactIdList = dao.getContactsForCustomer(ids0.customerId, remoteFactory, remoteTM);
+         assertEquals("No query cache visit", 0, remoteListener.visited.size());
+         
+         Set<Integer> contactIds0 = new HashSet<Integer>(contactIdList);
+         assertEquals("Query correct", ids0.contactIds, contactIds0);
+         
+         // Change a contact
+         Contact contact0A = customer0.getContacts().iterator().next();         
+         dao.updateCustomerForContact(contact0A, customer1, localFactory, localTM);
+         
+         // Sleep a bit to let async timestamp update propagate.
+         sleep(SLEEP_TIME);
+         
+         // Clear out any old state from listener
+         remoteListener.clear();
+         
+         // Re-execute query on remote node
+         contactIdList = dao.getContactsForCustomer(ids0.customerId, remoteFactory, remoteTM);
+         assertEquals("Had a query cache visit", 1, remoteListener.visited.size());
+         
+         contactIds0 = new HashSet<Integer>(contactIdList);
+         assertFalse("contact removed from customer 0", contactIds0.contains(contact0A.getId()));
+         
+         // Validate the query result
+         ids0.contactIds.remove(contact0A.getId());         
+         assertEquals(ids0.contactIds, contactIds0);
+         
+         // Validate a query for customer 1s contacts
+         contactIdList = dao.getContactsForCustomer(ids1.customerId, remoteFactory, remoteTM);
+         Set<Integer> contactIds1 = new HashSet<Integer>(contactIdList);
+         ids1.contactIds.add(contact0A.getId());
+         assertEquals(ids1.contactIds, contactIds1);
+      }
+      finally
+      {
+         // cleanup the db
+         System.out.println("Cleaning up");
+         dao.cleanup(createdCustomerIds, localFactory, localTM);         
+      }
+      
+   }
+   public class MyListener extends AbstractTreeCacheListener
+   {
+      HashSet<String> visited = new HashSet<String>(); 
+      
+      public void clear()
+      {
+         visited.clear();
+      }
+
+      @Override
+      public void nodeVisited(Fqn fqn)
+      {
+         super.nodeVisited(fqn);
+         
+         log.info("MyListener - Visiting node " + fqn.toString());
+         boolean foundStdQuery = false;
+         for (Object element : fqn.peekElements())
+         {
+            if (element.toString().contains(StandardQueryCache.class.getSimpleName()))
+            {
+               foundStdQuery = true;
+            }
+            else if (foundStdQuery)
+            {
+               visited.add(element.toString());
+               break;
+            }
+         }
+      }
+   }
+
+}




More information about the jboss-cvs-commits mailing list