1   package org.apache.fulcrum.yaafi.framework.factory;
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 java.io.File;
23  import java.io.IOException;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.avalon.framework.configuration.Configuration;
28  import org.apache.avalon.framework.configuration.ConfigurationUtil;
29  import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
30  import org.apache.avalon.framework.context.DefaultContext;
31  import org.apache.avalon.framework.service.ServiceException;
32  import org.apache.fulcrum.yaafi.TestComponent;
33  import org.apache.fulcrum.yaafi.framework.container.ServiceContainer;
34  
35  /**
36   * Test suite for the ServiceContainerFactory.
37   *
38   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
39   */
40  
41  public class ServiceContainerFactoryTest extends TestCase
42  {
43      private ServiceContainer container = null;
44  
45      /**
46       * Constructor
47       * @param name the name of the test case
48       */
49      public ServiceContainerFactoryTest( String name )
50      {
51          super(name);
52      }
53  
54      /**
55       * @see junit.framework.TestCase#tearDown()
56       */
57      protected void tearDown() throws Exception
58      {
59          ServiceContainerFactory.dispose(this.container);
60          super.tearDown();
61      }
62  
63      private void checkTestComponent()
64          throws Exception
65      {
66          TestComponent testComponent = this.getTestComponent();
67  
68          testComponent.test();
69  
70          assertEquals( testComponent.getBar(), "BAR" );
71          assertEquals( testComponent.getFoo(), "FOO" );
72  
73          assertNotNull( testComponent.getUrnAvalonClassLoader() );
74          assertNotNull( testComponent.getUrnAvaloneHome() );
75          assertNotNull( testComponent.getUrnAvaloneTemp() );
76          assertNotNull( testComponent.getUrnAvalonName() );
77          assertNotNull( testComponent.getUrnAvalonPartition() );
78  
79          try
80          {
81              testComponent.createException("enforce exception", this);
82          }
83          catch( Exception e )
84          {
85              // nothing to do
86          }
87      }
88  
89      /**
90       * @return get our simple test component
91       */
92      private TestComponent getTestComponent() throws ServiceException
93      {
94          return (TestComponent) container.lookup(
95              TestComponent.ROLE
96              );
97      }
98  
99      /**
100      * Creates a YAAFI container using a container configuration file
101      * which already contains most of the required settings
102      */
103     public void testCreationWithContainerConfiguration() throws Exception
104     {
105         ServiceContainerConfiguration config = new ServiceContainerConfiguration();
106         config.loadContainerConfiguration( "./src/test/TestYaafiContainerConfig.xml" );
107         this.container = ServiceContainerFactory.create( config );
108         this.checkTestComponent();
109         System.out.println(this.container.toString());
110         return;
111     }
112 
113     /**
114      * Creates a YAAFI container using a non-existent container
115      * configuration file. Therefore the creation should fail.
116      */
117     public void testCreationWithMissingContainerConfiguration() throws Exception
118     {
119         ServiceContainerConfiguration config = new ServiceContainerConfiguration();
120 
121         try
122         {
123             config.loadContainerConfiguration( "./src/test/MissingTestContainerConfig.xml" );
124             this.container = ServiceContainerFactory.create( config );
125             fail("The creation of the YAAFI container must fail");
126         }
127         catch (IOException e)
128         {
129             // nothing to do
130         }
131         catch (Exception e)
132         {
133             fail("We are expecting an IOException");
134         }
135     }
136 
137     /**
138      * Creates a YAAFI container providing all required settings
139      * manually
140      */
141     public void testCreationWithManualSettings() throws Exception
142     {
143         ServiceContainerConfiguration config = new ServiceContainerConfiguration();
144         config.setComponentRolesLocation( "./src/test/TestRoleConfig.xml" );
145         config.setComponentConfigurationLocation( "./src/test/TestComponentConfig.xml" );
146         config.setParametersLocation( "./src/test/TestParameters.properties" );
147         this.container = ServiceContainerFactory.create( config );
148         this.checkTestComponent();
149     }
150 
151     /**
152      * Creates a YAAFI container providing a Phoenix context
153      */
154     public void testCreationWithPhoenixContext() throws Exception
155     {
156         ServiceContainerConfiguration config = new ServiceContainerConfiguration();
157         DefaultContext context = new DefaultContext();
158 
159         // use an existing container configuration
160 
161         config.loadContainerConfiguration( "./src/test/TestPhoenixContainerConfig.xml" );
162 
163         // fill the context with Phoenix settings
164 
165         context.put( "app.name", "ServiceContainerFactoryTest" );
166         context.put( "block.name", "fulcrum-yaafi" );
167         context.put( "app.home", new File( new File("").getAbsolutePath() ) );
168 
169         // create an instance
170 
171         this.container = ServiceContainerFactory.create( config, context );
172 
173         // execute the test component
174 
175         this.checkTestComponent();
176     }
177 
178     /**
179      * Creates a YAAFI container providing a Fortress context
180      */
181     public void testCreationWithFortressContext() throws Exception
182     {
183         ServiceContainerConfiguration config = new ServiceContainerConfiguration();
184         DefaultContext context = new DefaultContext();
185 
186         // use an existing container configuration
187 
188         config.loadContainerConfiguration( "./src/test/TestFortressContainerConfig.xml" );
189 
190         // fill the context with Fortress settings
191 
192         context.put( "component.id", "ServiceContainerFactoryTest" );
193         context.put( "component.logger", "fulcrum-yaafi" );
194         context.put( "context-root", new File( new File("").getAbsolutePath() ) );
195         context.put( "impl.workDir", new File( new File("").getAbsolutePath() ) );
196 
197         // create an instance
198 
199         this.container = ServiceContainerFactory.create( config, context );
200 
201         // execute the test component
202 
203         this.checkTestComponent();
204     }
205 
206     /**
207      * Reconfigures the YAAFI container with the "TestReconfigurationConfig.xml".
208      */
209     public void testReconfiguration() throws Exception
210     {
211         // create a YAAFI instance
212 
213         ServiceContainerConfiguration config = new ServiceContainerConfiguration();
214         config.loadContainerConfiguration( "./src/test/TestYaafiContainerConfig.xml" );
215         this.container = ServiceContainerFactory.create( config );
216         this.checkTestComponent();
217 
218         // load a different configuration and reconfigure YAAFI
219 
220         DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
221         Configuration configuration = builder.buildFromFile( "./src/test/TestReconfigurationConfig.xml" );
222         System.out.println(ConfigurationUtil.toString(configuration));
223 
224         this.container.reconfigure( configuration );
225         TestComponent testComponent = this.getTestComponent();
226         testComponent.test();
227 
228         // the TestReconfigurationConfig.xml overwrites the
229         // TestComponentImpl.foo and the SystemProperty.FOO
230 
231         assertEquals( System.getProperty("FOO"), "YAAFI" );
232         assertEquals( testComponent.getFoo(), "YAAFI" );
233 
234     }
235 }