1   package org.apache.fulcrum.yaafi.framework.container;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.Test;
23  import junit.framework.TestCase;
24  import junit.framework.TestSuite;
25  
26  import org.apache.avalon.framework.configuration.Configuration;
27  import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
28  import org.apache.fulcrum.yaafi.TestComponent;
29  import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerConfiguration;
30  import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerFactory;
31  import org.apache.fulcrum.yaafi.framework.role.RoleEntry;
32  import org.apache.fulcrum.yaafi.service.reconfiguration.ReconfigurationService;
33  
34  
35  /**
36   * Test suite for the ServiceLifecycleManager.
37   *
38   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
39   */
40  
41  public class ServiceLifecycleManagerTest extends TestCase
42  {
43      private ServiceLifecycleManager lifecycleManager;
44      private ServiceContainer container;
45  
46      /**
47       * Constructor
48       * @param name the name of the test case
49       */
50      public ServiceLifecycleManagerTest( String name )
51      {
52          super(name);
53      }
54  
55      /**
56       * @see junit.framework.TestCase#setUp()
57       */
58      protected void setUp() throws Exception
59      {
60          super.setUp();
61          ServiceContainerConfiguration config = new ServiceContainerConfiguration();
62          config.loadContainerConfiguration( "./src/test/TestYaafiContainerConfig.xml" );
63          this.container = ServiceContainerFactory.create( config );
64          this.lifecycleManager = this.container;
65      }
66  
67      /**
68       * @see junit.framework.TestCase#tearDown()
69       */
70      protected void tearDown() throws Exception
71      {
72          ServiceContainerFactory.dispose(this.container);
73          super.tearDown();
74      }
75  
76      public static Test suite()
77      {
78          TestSuite suite= new TestSuite();
79  
80          suite.addTest( new ServiceLifecycleManagerTest("testGetServiceComponents") );
81          suite.addTest( new ServiceLifecycleManagerTest("testGeneralReconfiguration") );
82          suite.addTest( new ServiceLifecycleManagerTest("testGeneralDecommision") );
83          suite.addTest( new ServiceLifecycleManagerTest("testGeneralReconfigurationAndDecommision") );
84          suite.addTest( new ServiceLifecycleManagerTest("testIndividualDecommission") );
85          suite.addTest( new ServiceLifecycleManagerTest("testException") );
86  
87          return suite;
88      }
89  
90      /**
91       * Check our TestComponent.
92       *
93       * @throws Exception
94       */
95      private void checkTestComponent()
96          throws Exception
97      {
98          TestComponent testComponent = (TestComponent) container.lookup(
99              TestComponent.ROLE
100             );
101 
102         testComponent.test();
103 
104         assertEquals( testComponent.getBar(), "BAR" );
105         assertEquals( testComponent.getFoo(), "FOO" );
106 
107         assertNotNull( testComponent.getUrnAvalonClassLoader() );
108         assertNotNull( testComponent.getUrnAvaloneHome() );
109         assertNotNull( testComponent.getUrnAvaloneTemp() );
110         assertNotNull( testComponent.getUrnAvalonName() );
111         assertNotNull( testComponent.getUrnAvalonPartition() );
112     }
113 
114     /**
115      * Gets a list of all available services and dumps them
116      * on System.out.
117      */
118     public void testGetServiceComponents() throws Exception
119     {
120         RoleEntry[] list = this.lifecycleManager.getRoleEntries();
121         assertNotNull( list );
122         assertTrue( list.length > 0  );
123 
124         for( int i=0; i<list.length; i++ )
125         {
126             System.out.println(list[i].toString());
127         }
128     }
129 
130     /**
131      * Reconfigure the all services
132      */
133     public void testGeneralReconfiguration() throws Exception
134     {
135         RoleEntry[] list = this.lifecycleManager.getRoleEntries();
136 
137         for( int i=0; i<list.length; i++ )
138         {
139             String serviceName = list[i].getName();
140             System.out.println("Reconfiguring " + serviceName + " ...");
141 
142             String[] serviceNames = {list[i].getName()};
143             this.lifecycleManager.reconfigure(serviceNames);
144             assertTrue(this.container.hasService(serviceName));
145             assertNotNull(this.container.lookup(serviceName));
146         }
147     }
148 
149     /**
150      * Decommission and resurrect all services
151      */
152     public void testGeneralDecommision() throws Exception
153     {
154         String serviceName = null;
155         RoleEntry[] list = this.lifecycleManager.getRoleEntries();
156 
157         for( int i=0; i<list.length; i++ )
158         {
159             serviceName = list[i].getName();
160             System.out.println("Decommissiong " + serviceName + " ...");
161 
162             assertTrue(this.container.hasService(serviceName));
163             this.lifecycleManager.decommision(serviceName);
164             assertTrue(this.container.hasService(serviceName));
165             this.container.lookup(serviceName);
166             assertTrue(this.container.hasService(serviceName));
167             this.lifecycleManager.decommision(serviceName);
168             assertTrue(this.container.hasService(serviceName));
169         }
170     }
171 
172     /**
173      * Decommission and resurrect all services
174      */
175     public void testGeneralReconfigurationAndDecommision() throws Exception
176     {
177         String serviceName = null;
178         RoleEntry[] list = this.lifecycleManager.getRoleEntries();
179         DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
180         Configuration configuration = builder.buildFromFile( "./src/test/TestReconfigurationConfig.xml" );
181 
182         for( int i=0; i<list.length; i++ )
183         {
184             serviceName = list[i].getName();
185             String[] serviceNames = {list[i].getName()};
186             System.out.println("Processing " + serviceName + " ...");
187 
188             // reconfigure/decommission/reconfigure the service
189             this.lifecycleManager.reconfigure(serviceNames);
190             this.lifecycleManager.decommision(serviceName);
191             this.lifecycleManager.reconfigure(serviceNames);
192 
193             // run a reconfiguration over all services
194             this.container.reconfigure(configuration);
195 
196             // reconfigure/decommission/reconfigure the service
197             this.container.lookup(serviceName);
198             this.lifecycleManager.reconfigure(serviceNames);
199             this.lifecycleManager.decommision(serviceName);
200             this.lifecycleManager.reconfigure(serviceNames);
201         }
202     }
203 
204 
205     /**
206      * Decommissions the TestComponent and ReconfigurationService
207      * to start them again.
208      */
209     public void testIndividualDecommission() throws Exception
210     {
211         String serviceName = null;
212 
213         // teminate the TestComponent and run it again
214 
215         serviceName = TestComponent.class.getName();
216 
217         this.checkTestComponent();
218         this.lifecycleManager.decommision( serviceName );
219         this.checkTestComponent();
220 
221         // terminate the ReconfigurationService which is currently
222         // not instantiated and resurrect it. The moment the
223         // ReconfigurationService is instantiated it is starting to
224         // work
225 
226         serviceName = ReconfigurationService.class.getName();
227 
228         this.lifecycleManager.decommision( ReconfigurationService.class.getName() );
229         this.container.lookup( ReconfigurationService.class.getName() );
230 
231         // now we should see that the service is starting up
232 
233         Thread.sleep( 5000 );
234 
235         // and terminate it again
236 
237         this.lifecycleManager.decommision( ReconfigurationService.class.getName() );
238     }
239 
240     /**
241      * Create an exception which should be handled by the JAMon interceptor.
242      */
243     public void testException() throws Exception
244     {
245       TestComponent testComponent = (TestComponent) container.lookup(
246           TestComponent.ROLE
247           );
248 
249       try
250       {
251           testComponent.createException("testException", this);
252       }
253       catch(Exception e)
254       {
255           return;
256       }
257 
258     }
259 }