1 17 18 package org.sape.carbon.core.bootstrap.test; 19 20 import java.util.Properties ; 21 22 import org.sape.carbon.core.bootstrap.BootStrapper; 23 import org.sape.carbon.core.bootstrap.BootStrapperStateEnum; 24 import org.sape.carbon.core.component.ComponentKeeper; 25 import org.sape.carbon.core.config.ConfigurationService; 26 import org.sape.carbon.core.exception.ExceptionUtility; 27 28 import junit.extensions.ActiveTestSuite; 29 import junit.framework.Test; 30 import junit.framework.TestCase; 31 import junit.framework.TestSuite; 32 33 34 46 public class BootStrapperTest extends TestCase { 47 public BootStrapperTest(String name) { 48 super(name); 49 } 50 51 55 public void testLoad() { 56 TestCase.assertTrue( 57 "System state was loaded before call to load", 58 BootStrapper.getInstance().getState() == BootStrapperStateEnum.NOT_LOADED); 59 60 BootStrapper.getInstance().load(); 61 62 TestCase.assertTrue( 63 "System was not loaded after call to load", 64 BootStrapper.getInstance().getState() == BootStrapperStateEnum.LOADED); 65 } 66 67 72 public void testFetchComponentKeeper() { 73 ComponentKeeper keeper = 74 BootStrapper.getInstance().fetchComponentKeeper(); 75 TestCase.assertTrue("Keeper reference changed when it should not have", 76 BootStrapperTest.verifyKeeper(keeper)); 77 } 78 79 84 public void testFetchConfigurationService() { 85 ConfigurationService config = 86 BootStrapper.getInstance().fetchConfigurationService(); 87 TestCase.assertTrue("Config reference changed when it should not have", 88 BootStrapperTest.verifyConfig(config)); 89 } 90 91 96 public void testFetchComponentKeeperN() { 97 for(int n=0; n<BootStrapperTest.numberRepetitions; n++) { 98 testFetchComponentKeeper(); 99 } 100 } 101 102 107 public void testFetchConfigurationServiceN() { 108 for(int n=0; n<BootStrapperTest.numberRepetitions; n++) { 109 testFetchConfigurationService(); 110 } 111 } 112 113 118 public void testLoadN() { 119 for(int n=0; n<BootStrapperTest.numberRepetitions; n++) { 120 BootStrapper.getInstance().load(); 121 } 122 } 123 124 126 private static ComponentKeeper keeper; 127 private static Object keeperLock = new Object (); 128 private static ConfigurationService config; 129 private static Object configLock = new Object (); 130 private static final int numberRepetitions = 100; 131 132 133 public static Test suite() { 134 TestSuite masterSuite = new TestSuite(); 135 Test singleThreadedTests = getSingleThreadedTests(); 137 if (singleThreadedTests != null) { 138 masterSuite.addTest(singleThreadedTests); 139 } 140 Test multiThreadedTests = getMultiThreadedTests(); 142 if (multiThreadedTests != null) { 143 masterSuite.addTest(multiThreadedTests); 144 } 145 return masterSuite; 146 } 147 148 161 private static Test getSingleThreadedTests() { 162 TestSuite suite = new TestSuite(); 163 164 suite.addTest(new BootStrapperTest("testLoad")); 165 suite.addTest(new BootStrapperTest("testFetchComponentKeeper")); 166 suite.addTest(new BootStrapperTest("testFetchConfigurationService")); 167 suite.addTest(new BootStrapperTest("testFetchComponentKeeper")); 169 suite.addTest(new BootStrapperTest("testFetchConfigurationService")); 170 suite.addTest(new BootStrapperTest("testSetDeploymentProperty")); 171 172 return suite; 173 } 174 175 184 private static Test getMultiThreadedTests() { 185 TestSuite suite = new ActiveTestSuite(); 186 187 addTest(suite, "testFetchComponentKeeperN", 3); 188 addTest(suite, "testFetchConfigurationServiceN", 3); 189 addTest(suite, "testLoadN", 3); 190 191 return suite; 192 } 193 194 200 private static void addTest(TestSuite suite, String testName, int number) { 201 for (int count = 0; count < number; count++) { 202 suite.addTest(new BootStrapperTest(testName)); 203 } 204 } 205 206 214 private static boolean verifyKeeper(ComponentKeeper keeper) { 215 if (keeper == null) { 216 return false; 217 } 218 219 synchronized(BootStrapperTest.keeperLock) { 220 if (BootStrapperTest.keeper == null) { 221 BootStrapperTest.keeper = keeper; 222 } 223 return(BootStrapperTest.keeper == keeper); 224 } 225 } 226 227 235 private static boolean verifyConfig(ConfigurationService config) { 236 if (config == null) { 237 return false; 238 } 239 240 synchronized(BootStrapperTest.configLock) { 241 if (BootStrapperTest.config == null) { 242 BootStrapperTest.config = config; 243 } 244 return(BootStrapperTest.config == config); 245 } 246 } 247 248 private static final String KEY = "test.property"; 249 private static final String DEPLOYMENT_VALUE = "testDeployment"; 250 private static final String SYSTEM_VALUE = "testSystem"; 251 252 258 public void testSetDeploymentProperty() { 259 try { 260 Properties systemProperties = System.getProperties(); 261 String valueFromSystem = System.getProperty(KEY); 262 if ((valueFromSystem == null) || (valueFromSystem.equals(""))) { 263 Object settingProperty = BootStrapper.getInstance(). 264 setDeploymentProperty(KEY,DEPLOYMENT_VALUE); 265 String valueFromDeployment = BootStrapper.getInstance(). 266 getDeploymentProperty(KEY); 267 TestCase.assertTrue( 268 "Deployment property not changed where it should have been", valueFromDeployment.equals(DEPLOYMENT_VALUE)); 269 } 270 271 String oldValueFromSystem = System.setProperty(KEY,""); 272 Object settingValue = BootStrapper.getInstance().setDeploymentProperty(KEY,SYSTEM_VALUE); 273 String valueReturned = BootStrapper.getInstance().getDeploymentProperty(KEY); 274 TestCase.assertTrue("SystemPropertyChanged",valueReturned.equals(SYSTEM_VALUE)); 275 276 System.setProperties(systemProperties); 277 } catch(Exception e) { 278 TestCase.fail("Encountered unexpected " + 279 "error while executing BootStrapper.setDeploymentProperty: " + 280 ExceptionUtility.printStackTracesToString(e)); 281 } 282 } 283 } 284 | Popular Tags |