KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > deployment > test > DeploymentServiceTest


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

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 /**
36  * Template for junit test harness. Change this description to reflect what this class is testing.
37  * @since carbon 1.0
38  * @author Douglas Voet, April 2002
39  * @version $Revision: 1.8 $($Author: dvoet $ / $Date: 2003/09/24 20:31:11 $)
40  * <br>Copyright 2002 Sapient
41  */

42 public class DeploymentServiceTest extends TestCase {
43     private static final String JavaDoc DEPLOYMENT_COMPONENT_NAME =
44         "/deployment/test/DeploymentService";
45     private static final String JavaDoc PROD_NAME_LOOKUP =
46         "/deployment/test/ProdLookup";
47     private static final String JavaDoc DEV_NAME_LOOKUP =
48         "/deployment/test/DevLookup";
49     private static final String JavaDoc INST_NAME_LOOKUP =
50         "/deployment/test/InstLookup";
51     public static final String JavaDoc DEV_ENV_NAME = "dev";
52     public static final String JavaDoc INST_NAME = "inst";
53     public static final String JavaDoc PROD_ENV_NAME = "prod";
54     private static final String JavaDoc CURRENT_DEPLOYMENT_NODE =
55         "/deployment/test/deployments/CurrentDeployment";
56
57     public DeploymentServiceTest(String JavaDoc 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             // expected
123
}
124
125         try {
126             Lookup.getInstance().fetchComponent("/deployment/test/InvalidDeploymentService2");
127             fail("InvalidDeploymentService2 did not throw expected exception");
128         } catch (StateTransitionException ste) {
129             // expected
130
}
131
132         try {
133             Lookup.getInstance().fetchComponent("/deployment/test/InvalidDeploymentService3");
134             fail("InvalidDeploymentService3 did not throw expected exception");
135         } catch (StateTransitionException ste) {
136             // expected
137
}
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     /**
206      * Method called by jUnit to get all the tests in this test case.
207      * @return Test the suite of tests in this test case
208      */

209     public static Test suite() {
210         TestSuite masterSuite = new TestSuite();
211         // add single threaded tests
212
Test singleThreadedTests = getSingleThreadedTests();
213         if (singleThreadedTests != null) {
214             masterSuite.addTest(singleThreadedTests);
215         }
216         // add multi threaded tests
217
Test multiThreadedTests = getMultiThreadedTests();
218         if (multiThreadedTests != null) {
219             masterSuite.addTest(multiThreadedTests);
220         }
221         return masterSuite;
222     }
223
224     /**
225      * This method is used within the suite method to get all of the single threaded tests.
226      * Add all your single threaded tests in this method with a line like:
227      * suite.addTest(new DeploymentServiceTest("testFunction1"));
228      * @return Test the suite of single threaded tests in this test case
229      */

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     /**
243      * This method is used within the suite method to get all of the multi threaded tests.
244      * Add all your multi threaded tests in this method with a line like: addTest(suite, "testFunction1", 5);
245      * @return Test the suite of multi-threaded tests in this test case
246      */

247     private static Test getMultiThreadedTests() {
248         TestSuite suite = new ActiveTestSuite();
249
250         /*
251          * add your tests here following these examples:
252          *
253          * addTest(suite, "testFunction1", 5);
254          * addTest(suite, "testFunction2", 10);
255          */

256
257         return suite;
258     }
259
260     /**
261      * This method will add the give test to the give suite the specified
262      * number of times. This is best used for multi-threaded tests where
263      * suite is an instance of ActiveTestSuite and you want to run the same test in multiple threads.
264      * @param suite the suite to add the test to.
265      * @param testName the name of the test to add.
266      * @param number the number of times to add the test to the suite
267      */

268     private static void addTest(TestSuite suite, String JavaDoc testName, int number) {
269         for (int count = 0; count < number; count++) {
270             suite.addTest(new DeploymentServiceTest(testName));
271         }
272     }
273 }
274
Popular Tags