1   package org.apache.turbine.services;
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.util.Locale;
23  
24  import org.apache.fulcrum.cache.GlobalCacheService;
25  import org.apache.fulcrum.crypto.CryptoService;
26  import org.apache.fulcrum.factory.FactoryService;
27  import org.apache.fulcrum.intake.IntakeService;
28  import org.apache.fulcrum.localization.LocalizationService;
29  import org.apache.fulcrum.mimetype.MimeTypeService;
30  import org.apache.turbine.services.avaloncomponent.AvalonComponentService;
31  import org.apache.turbine.test.BaseTestCase;
32  import org.apache.turbine.util.TurbineConfig;
33  
34  /**
35   * Unit test for verifing that we can load all the appropriate components from the
36   * appropriate Container.  For now that is just ECM (AvalonComponentService)
37   * but in the future with mixed containers there could be multiple.
38   *
39   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
40   * @author <a href="mailto:sgoeschl@apache.org">Siegfried Goeschl</a>
41   * @version $Id: LoadingComponentsTest.java 947330 2010-05-22 19:38:55Z tv $
42   */
43  public class LoadingComponentsTest extends BaseTestCase
44  {
45      private static TurbineConfig tc = null;
46      public LoadingComponentsTest(String name) throws Exception
47      {
48          super(name);
49      }
50  
51      /**
52       * Test to load a couple of Avalon services directly by the
53       * AvalonComponentService.
54       *
55       * @throws Exception loading failed
56       */
57      public void testLoadingByAvalonComponentService() throws Exception
58      {
59          AvalonComponentService avalonComponentService =
60              (AvalonComponentService) TurbineServices.getInstance().getService(
61                      AvalonComponentService.SERVICE_NAME);
62  
63          assertNotNull(avalonComponentService);
64  
65          GlobalCacheService dgcs = (GlobalCacheService)avalonComponentService.lookup(GlobalCacheService.ROLE);
66          assertNotNull(dgcs);
67          CryptoService cs = (CryptoService)avalonComponentService.lookup(CryptoService.ROLE);
68          assertNotNull(cs);
69          LocalizationService ls = (LocalizationService)avalonComponentService.lookup(LocalizationService.ROLE);
70          assertNotNull(ls);
71          IntakeService intake = (IntakeService)avalonComponentService.lookup(IntakeService.ROLE);
72          assertNotNull(intake);
73          FactoryService fs = (FactoryService)avalonComponentService.lookup(FactoryService.ROLE);
74          assertNotNull(fs);
75          MimeTypeService mimetype = (MimeTypeService)avalonComponentService.lookup(MimeTypeService.ROLE);
76          assertNotNull(mimetype);
77      }
78  
79      /**
80       * Test to load a couple of Avalon services by using the
81       * TurbineServices which delegate the service retrieval to
82       * the AvalonComponentService
83       *
84       * @throws Exception loading failed
85       */
86      public void testLoadingByTurbineServices() throws Exception
87      {
88          ServiceManager serviceManager = TurbineServices.getInstance();
89  
90          GlobalCacheService gcs = (GlobalCacheService)serviceManager.getService(GlobalCacheService.ROLE);
91          assertNotNull(gcs);
92          CryptoService cs = (CryptoService)serviceManager.getService(CryptoService.ROLE);
93          assertNotNull(cs);
94          LocalizationService ls = (LocalizationService)serviceManager.getService(LocalizationService.ROLE);
95          assertNotNull(ls);
96          IntakeService intake = (IntakeService)serviceManager.getService(IntakeService.ROLE);
97          assertNotNull(intake);
98          FactoryService fs = (FactoryService)serviceManager.getService(FactoryService.ROLE);
99          assertNotNull(fs);
100         MimeTypeService mimetype = (MimeTypeService)serviceManager.getService(MimeTypeService.ROLE);
101         assertNotNull(mimetype);
102     }
103 
104     /**
105      * Lookup up an unknown servie
106      * @throws Exception
107      */
108     public void testLookupUnknownService() throws Exception
109     {
110         ServiceManager serviceManager = TurbineServices.getInstance();
111 
112         try
113         {
114             serviceManager.getService("foo");
115             fail("We expect an InstantiationException");
116         }
117         catch (InstantiationException e)
118         {
119             // that'w what we expect
120             return;
121         }
122         catch (Throwable t)
123         {
124             fail("We expect an InstantiationException");
125         }
126     }
127 
128     /**
129      * Shutdown the AvalonComponentService where the MimeTypeService
130      * resides and lookup the MimeTypeService. This should trigger
131      * a late initialization of AvalonComponentService and returns
132      * a fully functional MimeTypeService.
133      */
134     public void testAvalonComponentServiceShutdown() throws Exception
135     {
136         ServiceManager serviceManager = TurbineServices.getInstance();
137         serviceManager.shutdownService(AvalonComponentService.SERVICE_NAME);
138 
139         MimeTypeService mimeTypeService = (MimeTypeService) serviceManager.getService(MimeTypeService.class.getName());
140         assertNotNull(mimeTypeService);
141 
142         Locale locale = new Locale("en", "US");
143         String s = mimeTypeService.getCharSet(locale);
144         assertEquals("ISO-8859-1", s);
145     }
146 
147     public void setUp() throws Exception
148     {
149         tc = new TurbineConfig(".", "/conf/test/TestFulcrumComponents.properties");
150         tc.initialize();
151     }
152     public void tearDown() throws Exception
153     {
154         if (tc != null)
155         {
156             tc.dispose();
157         }
158     }
159 }