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