1 17 18 package org.sape.carbon.core.config.cache.test; 19 20 import org.sape.carbon.core.bootstrap.BootStrapper; 21 import org.sape.carbon.core.config.Config; 22 import org.sape.carbon.core.config.ConfigurationException; 23 import org.sape.carbon.core.config.ConfigurationNotFoundException; 24 import org.sape.carbon.core.config.PropertyConfiguration; 25 import org.sape.carbon.core.config.cache.ConfigurationCache; 26 import org.sape.carbon.core.config.cache.ConfigurationCacheFactory; 27 import org.sape.carbon.core.config.cache.DoubleCheckConfigurationCache; 28 import org.sape.carbon.core.config.cache.SynchronizedConfigurationCache; 29 30 import junit.extensions.ActiveTestSuite; 31 import junit.framework.Test; 32 import junit.framework.TestCase; 33 import junit.framework.TestSuite; 34 35 43 public class ConfigurationCacheTest extends TestCase { 44 45 private String actualCacheClassName; 46 47 public ConfigurationCacheTest(String name) { 48 super(name); 49 } 50 51 public void testSynchronizedCache() throws ConfigurationException { 52 setCacheClass(SynchronizedConfigurationCache.class); 53 testCache(); 54 restoreCacheClass(); 55 } 56 57 public void testDoubleCheckCache() throws ConfigurationException { 58 setCacheClass(DoubleCheckConfigurationCache.class); 59 testCache(); 60 restoreCacheClass(); 61 } 62 63 private void setCacheClass(Class cacheClass) { 64 this.actualCacheClassName = 65 BootStrapper.getInstance().getDeploymentProperty( 66 ConfigurationCacheFactory.CACHE_TYPE_PROPERTY_NAME); 67 68 BootStrapper.getInstance().setDeploymentProperty( 69 ConfigurationCacheFactory.CACHE_TYPE_PROPERTY_NAME, 70 cacheClass.getName()); 71 } 72 73 private void restoreCacheClass() { 74 BootStrapper.getInstance().setDeploymentProperty( 75 ConfigurationCacheFactory.CACHE_TYPE_PROPERTY_NAME, 76 this.actualCacheClassName); 77 } 78 79 private void testCache() throws ConfigurationException { 80 final String CONFIG_NAME = "/core/test/ConfigurationCacheTest"; 81 final String ATTRIBUTE_NAME = "foo"; 82 83 PropertyConfiguration origConfig = 84 (PropertyConfiguration) Config.getInstance().createConfiguration( 85 PropertyConfiguration.class); 86 87 origConfig.setProperty(ATTRIBUTE_NAME, "bar"); 88 Config.getInstance().storeConfiguration(CONFIG_NAME, origConfig); 89 90 ConfigurationCache cache = 91 ConfigurationCacheFactory.getInstance( 92 Config.getInstance().getConfigurationService()); 93 94 PropertyConfiguration cachedConfig = 95 (PropertyConfiguration) cache.getConfiguration(CONFIG_NAME); 96 97 origConfig.setProperty(ATTRIBUTE_NAME, "splat"); 98 Config.getInstance().storeConfiguration(CONFIG_NAME, origConfig); 99 100 PropertyConfiguration changedConfig = 101 (PropertyConfiguration) cache.getConfiguration(CONFIG_NAME); 102 103 TestCase.assertTrue( 104 "Cached configuration did not change", 105 !cachedConfig.getProperty(ATTRIBUTE_NAME).equals( 106 changedConfig.getProperty(ATTRIBUTE_NAME))); 107 108 Config.getInstance().fetchNode(CONFIG_NAME).remove(); 109 110 try { 111 cache.getConfiguration(CONFIG_NAME); 112 TestCase.fail("Cached configuration was not removed"); 113 } catch (ConfigurationNotFoundException cnfe) { 114 } 116 } 117 118 122 public static Test suite() { 123 TestSuite masterSuite = new TestSuite(); 124 Test singleThreadedTests = getSingleThreadedTests(); 126 if (singleThreadedTests != null) { 127 masterSuite.addTest(singleThreadedTests); 128 } 129 Test multiThreadedTests = getMultiThreadedTests(); 131 if (multiThreadedTests != null) { 132 masterSuite.addTest(multiThreadedTests); 133 } 134 return masterSuite; 135 } 136 137 143 private static Test getSingleThreadedTests() { 144 TestSuite suite = new TestSuite(); 145 146 suite.addTest(new ConfigurationCacheTest("testSynchronizedCache")); 147 suite.addTest(new ConfigurationCacheTest("testDoubleCheckCache")); 148 149 return suite; 150 } 151 152 157 private static Test getMultiThreadedTests() { 158 TestSuite suite = new ActiveTestSuite(); 159 160 166 167 return suite; 168 } 169 170 178 private static void addTest(TestSuite suite, String testName, int number) { 179 for (int count = 0; count < number; count++) { 180 suite.addTest(new ConfigurationCacheTest(testName)); 181 } 182 } 183 } 184 | Popular Tags |