[jboss-svn-commits] JBL Code SVN: r27010 - labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/internal/soa/esb/services/registry.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Jun 18 09:58:13 EDT 2009
Author: kevin.conner at jboss.com
Date: 2009-06-18 09:58:12 -0400 (Thu, 18 Jun 2009)
New Revision: 27010
Added:
labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/internal/soa/esb/services/registry/CachingRegistryInterceptorUnitTest.java
Log:
Add initial unit test for caching interceptor: JBESB-2626
Copied: labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/internal/soa/esb/services/registry/CachingRegistryInterceptorUnitTest.java (from rev 26215, labs/jbossesb/branches/JBESB_4_4_GA_CP/product/rosetta/tests/src/org/jboss/internal/soa/esb/services/registry/CachingRegistryInterceptorUnitTest.java)
===================================================================
--- labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/internal/soa/esb/services/registry/CachingRegistryInterceptorUnitTest.java (rev 0)
+++ labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/internal/soa/esb/services/registry/CachingRegistryInterceptorUnitTest.java 2009-06-18 13:58:12 UTC (rev 27010)
@@ -0,0 +1,238 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * 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,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2006, JBoss Inc.
+ */
+package org.jboss.internal.soa.esb.services.registry;
+
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.jboss.soa.esb.addressing.EPR;
+import org.jboss.soa.esb.services.registry.Registry;
+import org.jboss.soa.esb.services.registry.RegistryException;
+import org.jboss.soa.esb.services.registry.RegistryInterceptor;
+import org.jboss.soa.esb.services.registry.ServiceNotFoundException;
+
+import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * Unit tests for the caching registry interceptor
+ *
+ * @author <a href="mailto:Kevin.Conner at jboss.com">Kevin Conner</a>
+ */
+public class CachingRegistryInterceptorUnitTest extends TestCase
+{
+ /**
+ * Tests of caching, intended to run within the current cache validity period
+ */
+ public void testCaching()
+ throws Exception
+ {
+ final String category = "category" ;
+ final String name = "name" ;
+ final EPR epr1 = new EPR() ;
+ final EPR epr2 = new EPR() ;
+
+ final CachingRegistryInterceptor interceptor = new CachingRegistryInterceptor() ;
+ final CacheRegistryStatsInterceptor stats = new CacheRegistryStatsInterceptor() ;
+ final MockRegistry registry = new MockRegistry() ;
+ stats.setRegistry(registry) ;
+ interceptor.setRegistry(stats) ;
+
+ registry.registerEPR(category, name, "Description", epr1, "EPR description") ;
+
+ // findAllServices passes through to registry
+ assertEquals("findAllServices count", 0, stats.getFindAllServicesCount()) ;
+ interceptor.findAllServices() ;
+ assertEquals("findAllServices count", 1, stats.getFindAllServicesCount()) ;
+ interceptor.findAllServices() ;
+ assertEquals("findAllServices count", 2, stats.getFindAllServicesCount()) ;
+
+ // findServices passes through to registry
+ assertEquals("findServices count", 0, stats.getFindServicesCount()) ;
+ interceptor.findServices(category) ;
+ assertEquals("findServices count", 1, stats.getFindServicesCount()) ;
+ interceptor.findServices(category) ;
+ assertEquals("findServices count", 2, stats.getFindServicesCount()) ;
+
+ // findEPR/findEPRs go to registry on first call, then cache.
+ assertEquals("findEPRs count", 0, stats.getFindEPRsCount()) ;
+ interceptor.findEPRs(category, name) ;
+ assertEquals("findEPRs count", 1, stats.getFindEPRsCount()) ;
+ interceptor.findEPRs(category, name) ;
+ assertEquals("findEPRs count", 1, stats.getFindEPRsCount()) ;
+ interceptor.findEPR(category, name) ;
+ assertEquals("findEPRs count", 1, stats.getFindEPRsCount()) ;
+ assertEquals("findEPR count", 0, stats.getFindEPRCount()) ;
+
+ // registerEPR goes to registry and adds to cache
+ assertEquals("findEPRs count", 1, stats.getFindEPRsCount()) ;
+ assertEquals("registerEPR count", 0, stats.getRegisterEPRCount()) ;
+ final List<EPR> registerOrigEPRs = interceptor.findEPRs(category, name) ;
+ assertEquals("findEPRs count", 1, stats.getFindEPRsCount()) ;
+ assertNotNull("Original EPRs", registerOrigEPRs) ;
+ assertEquals("Original EPR count", 1, registerOrigEPRs.size()) ;
+ interceptor.registerEPR(category, name, "Description", epr2, "EPR description") ;
+ assertEquals("registerEPR count", 1, stats.getRegisterEPRCount()) ;
+ final List<EPR> registerNewEPRs = interceptor.findEPRs(category, name) ;
+ assertEquals("findEPRs count", 1, stats.getFindEPRsCount()) ;
+ assertNotNull("New EPRs", registerNewEPRs) ;
+ assertEquals("New EPR count", 2, registerNewEPRs.size()) ;
+
+ // unRegister goes to registry and removes from cache
+ // If the EPR list is empty after unRegister then the
+ // service details expire and the next access will defer
+ // to the registry
+ assertEquals("findEPRs count", 1, stats.getFindEPRsCount()) ;
+ assertEquals("unRegisterEPR count", 0, stats.getUnRegisterEPRCount()) ;
+ final List<EPR> unRegisterOrigEPRs = interceptor.findEPRs(category, name) ;
+ assertEquals("findEPRs count", 1, stats.getFindEPRsCount()) ;
+ assertNotNull("Original EPRs", unRegisterOrigEPRs) ;
+ assertEquals("Original EPR count", 2, unRegisterOrigEPRs.size()) ;
+ interceptor.unRegisterEPR(category, name, epr2) ;
+ assertEquals("unRegisterEPR count", 1, stats.getUnRegisterEPRCount()) ;
+ final List<EPR> unRegisterNewEPRs = interceptor.findEPRs(category, name) ;
+ assertEquals("findEPRs count", 1, stats.getFindEPRsCount()) ;
+ assertNotNull("New EPRs", unRegisterNewEPRs) ;
+ assertEquals("New EPR count", 1, unRegisterNewEPRs.size()) ;
+ interceptor.unRegisterEPR(category, name, epr1) ;
+ assertEquals("unRegisterEPR count", 2, stats.getUnRegisterEPRCount()) ;
+ final List<EPR> finalEPRs = interceptor.findEPRs(category, name) ;
+ assertEquals("findEPRs count", 2, stats.getFindEPRsCount()) ;
+ assertNotNull("Final EPRs", finalEPRs) ;
+ assertEquals("Final EPR count", 0, finalEPRs.size()) ;
+ final EPR finalEPR = interceptor.findEPR(category, name) ;
+ assertNull("Final EPR", finalEPR) ;
+ assertEquals("findEPR count", 0, stats.getFindEPRCount()) ;
+ assertEquals("findEPRs count", 2, stats.getFindEPRsCount()) ;
+
+ // Need to fix MockRegistry as it does not correctly handle
+ // service information. We cannot use unRegisterService unless
+ // there is an EPR present so we re-register the first one.
+ // Also findEPR and findEPRs should throw ServiceNotFoundException
+ // after unRegister but MockRegistry does not support it.
+ registry.registerEPR(category, name, "Description", epr1, "EPR description") ;
+ assertEquals("unRegisterService count", 0, stats.getUnRegisterServiceCount()) ;
+ interceptor.unRegisterService(category, name) ;
+ assertEquals("unRegisterService count", 1, stats.getUnRegisterServiceCount()) ;
+ }
+
+ private static final class CacheRegistryStatsInterceptor implements RegistryInterceptor
+ {
+ private Registry registry ;
+ final AtomicInteger findAllServicesCount = new AtomicInteger() ;
+ final AtomicInteger findEPRCount = new AtomicInteger() ;
+ final AtomicInteger findEPRsCount = new AtomicInteger() ;
+ final AtomicInteger findServicesCount = new AtomicInteger() ;
+ final AtomicInteger registerEPRCount = new AtomicInteger() ;
+ final AtomicInteger unRegisterEPRCount = new AtomicInteger() ;
+ final AtomicInteger unRegisterServiceCount = new AtomicInteger() ;
+
+ public List<String> findAllServices()
+ throws RegistryException
+ {
+ findAllServicesCount.incrementAndGet() ;
+ return registry.findAllServices() ;
+ }
+
+ public int getFindAllServicesCount()
+ {
+ return findAllServicesCount.get() ;
+ }
+
+ public EPR findEPR(final String serviceCategoryName, final String serviceName)
+ throws RegistryException, ServiceNotFoundException
+ {
+ findEPRCount.incrementAndGet() ;
+ return registry.findEPR(serviceCategoryName, serviceName) ;
+ }
+
+ public int getFindEPRCount()
+ {
+ return findEPRCount.get() ;
+ }
+
+ public List<EPR> findEPRs(final String serviceCategoryName, final String serviceName)
+ throws RegistryException, ServiceNotFoundException
+ {
+ findEPRsCount.incrementAndGet() ;
+ return registry.findEPRs(serviceCategoryName, serviceName) ;
+ }
+
+ public int getFindEPRsCount()
+ {
+ return findEPRsCount.get() ;
+ }
+
+ public List<String> findServices(final String serviceCategoryName)
+ throws RegistryException
+ {
+ findServicesCount.incrementAndGet() ;
+ return registry.findServices(serviceCategoryName) ;
+ }
+
+ public int getFindServicesCount()
+ {
+ return findServicesCount.get();
+ }
+
+ public void registerEPR(final String serviceCategoryName, final String serviceName,
+ final String serviceDescription, final EPR epr, final String eprDescription)
+ throws RegistryException
+ {
+ registerEPRCount.incrementAndGet() ;
+ registry.registerEPR(serviceCategoryName, serviceName, serviceDescription, epr, eprDescription) ;
+ }
+
+ public int getRegisterEPRCount()
+ {
+ return registerEPRCount.get();
+ }
+
+ public void unRegisterEPR(final String serviceCategoryName,
+ final String serviceName, final EPR epr)
+ throws RegistryException, ServiceNotFoundException
+ {
+ unRegisterEPRCount.incrementAndGet() ;
+ registry.unRegisterEPR(serviceCategoryName, serviceName, epr) ;
+ }
+
+ public int getUnRegisterEPRCount()
+ {
+ return unRegisterEPRCount.get();
+ }
+
+ public void unRegisterService(final String category, final String serviceName)
+ throws RegistryException, ServiceNotFoundException
+ {
+ unRegisterServiceCount.incrementAndGet() ;
+ registry.unRegisterService(category, serviceName) ;
+ }
+
+ public int getUnRegisterServiceCount()
+ {
+ return unRegisterServiceCount.get();
+ }
+
+ public void setRegistry(final Registry registry)
+ {
+ this.registry = registry ;
+ }
+ }
+}
More information about the jboss-svn-commits
mailing list