public void testCaching()
throws Exception
{
final String category = "category" ;
final String name = "name" ;
final EPR epr1 = new EPR() ;
final EPR epr2 = new EPR() ;
final EPR epr3 = new EPR(URI.create("urn:test")) ;
final CachingRegistryInterceptor interceptor = new CachingRegistryInterceptor() ;
final RegistryStatsInterceptor stats = new RegistryStatsInterceptor() ;
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> duplicateEPRs = interceptor.findEPRs(category, name) ;
assertEquals("findEPRs count", 1, stats.getFindEPRsCount()) ;
assertNotNull("Duplicate EPRs", duplicateEPRs) ;
assertEquals("Duplicate EPR count", 1, duplicateEPRs.size()) ;
interceptor.registerEPR(category, name, "Description", epr3, "EPR description") ;
assertEquals("registerEPR count", 2, 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
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, epr3) ;
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, epr2) ;
assertEquals("unRegisterEPR count", 2, stats.getUnRegisterEPRCount()) ;
final List<EPR> unRegisterDuplicateEPRs = interceptor.findEPRs(category, name) ;
assertEquals("findEPRs count", 1, stats.getFindEPRsCount()) ;
assertNotNull("Duplicate EPRs", unRegisterDuplicateEPRs) ;
assertEquals("Duplicate EPR count", 1, unRegisterDuplicateEPRs.size()) ;
interceptor.unRegisterEPR(category, name, epr1) ;
assertEquals("unRegisterEPR count", 3, stats.getUnRegisterEPRCount()) ;
final List<EPR> finalEPRs = interceptor.findEPRs(category, name) ;
assertEquals("findEPRs count", 1, 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", 1, stats.getFindEPRsCount()) ;
// Test unregistering of service