1 17 18 package org.sape.carbon.services.deployment.test; 19 20 import org.sape.carbon.core.component.Component; 21 import org.sape.carbon.core.component.Lookup; 22 import org.sape.carbon.core.component.lifecycle.StateTransitionException; 23 import org.sape.carbon.core.config.Config; 24 import org.sape.carbon.core.config.InvalidConfigurationException; 25 import org.sape.carbon.core.config.PropertyConfiguration; 26 import org.sape.carbon.core.config.interceptor.ConfigurationInterceptor; 27 import org.sape.carbon.services.deployment.DeploymentServiceConfiguration; 28 import org.sape.carbon.services.deployment.namelookup.NameLookup; 29 30 import junit.extensions.ActiveTestSuite; 31 import junit.framework.Test; 32 import junit.framework.TestCase; 33 import junit.framework.TestSuite; 34 35 42 public class DeploymentServiceTest extends TestCase { 43 private static final String DEPLOYMENT_COMPONENT_NAME = 44 "/deployment/test/DeploymentService"; 45 private static final String PROD_NAME_LOOKUP = 46 "/deployment/test/ProdLookup"; 47 private static final String DEV_NAME_LOOKUP = 48 "/deployment/test/DevLookup"; 49 private static final String INST_NAME_LOOKUP = 50 "/deployment/test/InstLookup"; 51 public static final String DEV_ENV_NAME = "dev"; 52 public static final String INST_NAME = "inst"; 53 public static final String PROD_ENV_NAME = "prod"; 54 private static final String CURRENT_DEPLOYMENT_NODE = 55 "/deployment/test/deployments/CurrentDeployment"; 56 57 public DeploymentServiceTest(String name) { 58 super(name); 59 } 60 61 public void testEnvironmentOnly() { 62 Component deploymentService = 63 Lookup.getInstance().fetchComponent(DEPLOYMENT_COMPONENT_NAME); 64 65 ((DeploymentServiceConfiguration) deploymentService). 66 setEnvironmentName(PROD_ENV_NAME); 67 ((DeploymentServiceConfiguration) deploymentService). 68 setInstanceName(null); 69 70 ((ConfigurationInterceptor) deploymentService).applyConfiguration(); 71 72 PropertyConfiguration config = (PropertyConfiguration) 73 Config.getInstance().fetchConfiguration(CURRENT_DEPLOYMENT_NODE); 74 75 try { 76 TestCase.assertTrue( 77 "Unexpected value from config: Expected [prod] found [" + 78 config.getProperty("Prop1") + "]", 79 "prod".equals(config.getProperty("Prop1"))); 80 TestCase.assertTrue( 81 "Unexpected value from config: Expected [prod] found [" + 82 config.getProperty("Prop2") + "]", 83 "prod".equals(config.getProperty("Prop2"))); 84 } catch(InvalidConfigurationException ice) { 85 TestCase.fail("Property not found: " + ice); 86 } 87 } 88 89 public void testSeparateInstances() { 90 Component deploymentService = 91 Lookup.getInstance().fetchComponent(DEPLOYMENT_COMPONENT_NAME); 92 93 ((DeploymentServiceConfiguration) deploymentService). 94 setEnvironmentName(DEV_ENV_NAME); 95 ((DeploymentServiceConfiguration) deploymentService). 96 setInstanceName(INST_NAME); 97 98 ((ConfigurationInterceptor) deploymentService).applyConfiguration(); 99 100 PropertyConfiguration config = (PropertyConfiguration) 101 Config.getInstance().fetchConfiguration(CURRENT_DEPLOYMENT_NODE); 102 103 try { 104 TestCase.assertTrue( 105 "Unexpected value from config: Expected [dev] found [" + 106 config.getProperty("Prop1") + "]", 107 "dev".equals(config.getProperty("Prop1"))); 108 TestCase.assertTrue( 109 "Unexpected value from config: Expected [inst] found [" + 110 config.getProperty("Prop2") + "]", 111 "inst".equals(config.getProperty("Prop2"))); 112 } catch(InvalidConfigurationException ice) { 113 TestCase.fail("Property not found: " + ice); 114 } 115 } 116 117 public void testInvalidConfigurations() { 118 try { 119 Lookup.getInstance().fetchComponent("/deployment/test/InvalidDeploymentService1"); 120 fail("InvalidDeploymentService1 did not throw expected exception"); 121 } catch (StateTransitionException ste) { 122 } 124 125 try { 126 Lookup.getInstance().fetchComponent("/deployment/test/InvalidDeploymentService2"); 127 fail("InvalidDeploymentService2 did not throw expected exception"); 128 } catch (StateTransitionException ste) { 129 } 131 132 try { 133 Lookup.getInstance().fetchComponent("/deployment/test/InvalidDeploymentService3"); 134 fail("InvalidDeploymentService3 did not throw expected exception"); 135 } catch (StateTransitionException ste) { 136 } 138 } 139 140 public void testEnvironmentOnlyWithLookup() { 141 Component deploymentService = 142 Lookup.getInstance().fetchComponent(DEPLOYMENT_COMPONENT_NAME); 143 144 ((DeploymentServiceConfiguration) deploymentService). 145 setEnvironmentName(null); 146 ((DeploymentServiceConfiguration) deploymentService). 147 setInstanceName(null); 148 ((DeploymentServiceConfiguration) deploymentService). 149 setEnvironmentNameLookup((NameLookup) 150 Lookup.getInstance().fetchComponent(PROD_NAME_LOOKUP)); 151 152 ((ConfigurationInterceptor) deploymentService).applyConfiguration(); 153 154 PropertyConfiguration config = (PropertyConfiguration) 155 Config.getInstance().fetchConfiguration(CURRENT_DEPLOYMENT_NODE); 156 157 try { 158 TestCase.assertTrue( 159 "Unexpected value from config: Expected [prod] found [" + 160 config.getProperty("Prop1") + "]", 161 "prod".equals(config.getProperty("Prop1"))); 162 TestCase.assertTrue( 163 "Unexpected value from config: Expected [prod] found [" + 164 config.getProperty("Prop2") + "]", 165 "prod".equals(config.getProperty("Prop2"))); 166 } catch(InvalidConfigurationException ice) { 167 TestCase.fail("Property not found: " + ice); 168 } 169 } 170 171 public void testSeparateInstancesWithLookup() { 172 Component deploymentService = 173 Lookup.getInstance().fetchComponent(DEPLOYMENT_COMPONENT_NAME); 174 175 ((DeploymentServiceConfiguration) deploymentService). 176 setEnvironmentName(null); 177 ((DeploymentServiceConfiguration) deploymentService). 178 setInstanceName(null); 179 ((DeploymentServiceConfiguration) deploymentService). 180 setEnvironmentNameLookup((NameLookup) 181 Lookup.getInstance().fetchComponent(DEV_NAME_LOOKUP)); 182 ((DeploymentServiceConfiguration) deploymentService). 183 setInstanceNameLookup((NameLookup) 184 Lookup.getInstance().fetchComponent(INST_NAME_LOOKUP)); 185 186 ((ConfigurationInterceptor) deploymentService).applyConfiguration(); 187 188 PropertyConfiguration config = (PropertyConfiguration) 189 Config.getInstance().fetchConfiguration(CURRENT_DEPLOYMENT_NODE); 190 191 try { 192 TestCase.assertTrue( 193 "Unexpected value from config: Expected [dev] found [" + 194 config.getProperty("Prop1") + "]", 195 "dev".equals(config.getProperty("Prop1"))); 196 TestCase.assertTrue( 197 "Unexpected value from config: Expected [inst] found [" + 198 config.getProperty("Prop2") + "]", 199 "inst".equals(config.getProperty("Prop2"))); 200 } catch(InvalidConfigurationException ice) { 201 TestCase.fail("Property not found: " + ice); 202 } 203 } 204 205 209 public static Test suite() { 210 TestSuite masterSuite = new TestSuite(); 211 Test singleThreadedTests = getSingleThreadedTests(); 213 if (singleThreadedTests != null) { 214 masterSuite.addTest(singleThreadedTests); 215 } 216 Test multiThreadedTests = getMultiThreadedTests(); 218 if (multiThreadedTests != null) { 219 masterSuite.addTest(multiThreadedTests); 220 } 221 return masterSuite; 222 } 223 224 230 private static Test getSingleThreadedTests() { 231 TestSuite suite = new TestSuite(); 232 233 suite.addTest(new DeploymentServiceTest("testEnvironmentOnly")); 234 suite.addTest(new DeploymentServiceTest("testSeparateInstances")); 235 suite.addTest(new DeploymentServiceTest("testEnvironmentOnlyWithLookup")); 236 suite.addTest(new DeploymentServiceTest("testSeparateInstancesWithLookup")); 237 suite.addTest(new DeploymentServiceTest("testInvalidConfigurations")); 238 239 return suite; 240 } 241 242 247 private static Test getMultiThreadedTests() { 248 TestSuite suite = new ActiveTestSuite(); 249 250 256 257 return suite; 258 } 259 260 268 private static void addTest(TestSuite suite, String testName, int number) { 269 for (int count = 0; count < number; count++) { 270 suite.addTest(new DeploymentServiceTest(testName)); 271 } 272 } 273 } 274 | Popular Tags |