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 1606111 2014-06-27 14:46:47Z gk $
49   */
50  public class LoadingComponentsTest extends BaseTestCase
51  {
52      private static TurbineConfig tc = null;
53  
54  
55      /**
56       * Test to load a couple of Avalon services directly by the
57       * AvalonComponentService.
58       *
59       * @throws Exception loading failed
60       */
61      @Test public void testLoadingByAvalonComponentService() throws Exception
62      {
63          AvalonComponentService avalonComponentService =
64              (AvalonComponentService) TurbineServices.getInstance().getService(
65                      AvalonComponentService.SERVICE_NAME);
66  
67          assertNotNull(avalonComponentService);
68  
69          GlobalCacheService dgcs = (GlobalCacheService)avalonComponentService.lookup(GlobalCacheService.ROLE);
70          assertNotNull(dgcs);
71          CryptoService cs = (CryptoService)avalonComponentService.lookup(CryptoService.ROLE);
72          assertNotNull(cs);
73          LocalizationService ls = (LocalizationService)avalonComponentService.lookup(LocalizationService.ROLE);
74          assertNotNull(ls);
75          IntakeService intake = (IntakeService)avalonComponentService.lookup(IntakeService.ROLE);
76          assertNotNull(intake);
77          FactoryService fs = (FactoryService)avalonComponentService.lookup(FactoryService.ROLE);
78          assertNotNull(fs);
79          MimeTypeService mimetype = (MimeTypeService)avalonComponentService.lookup(MimeTypeService.ROLE);
80          assertNotNull(mimetype);
81          //avalonComponentService.shutdown();
82      }
83  
84      /**
85       * Test to load a couple of Avalon services by using the
86       * TurbineServices which delegate the service retrieval to
87       * the AvalonComponentService
88       *
89       * @throws Exception loading failed
90       */
91      @Test public void testLoadingByTurbineServices() throws Exception
92      {
93          ServiceManager serviceManager = TurbineServices.getInstance();
94  
95          GlobalCacheService gcs = (GlobalCacheService)serviceManager.getService(GlobalCacheService.ROLE);
96          assertNotNull(gcs);
97          CryptoService cs = (CryptoService)serviceManager.getService(CryptoService.ROLE);
98          assertNotNull(cs);
99          LocalizationService ls = (LocalizationService)serviceManager.getService(LocalizationService.ROLE);
100         assertNotNull(ls);
101         IntakeService intake = (IntakeService)serviceManager.getService(IntakeService.ROLE);
102         assertNotNull(intake);
103         FactoryService fs = (FactoryService)serviceManager.getService(FactoryService.ROLE);
104         assertNotNull(fs);
105         MimeTypeService mimetype = (MimeTypeService)serviceManager.getService(MimeTypeService.ROLE);
106         assertNotNull(mimetype);
107     }
108 
109     /**
110      * Lookup up an unknown servie
111      * @throws Exception
112      */
113     @Test public void testLookupUnknownService() throws Exception
114     {
115         ServiceManager serviceManager = TurbineServices.getInstance();
116 
117         try
118         {
119             serviceManager.getService("foo");
120             fail("We expect an InstantiationException");
121         }
122         catch (InstantiationException e)
123         {
124             // that'w what we expect
125             return;
126         }
127         catch (Throwable t)
128         {
129             fail("We expect an InstantiationException");
130         }
131     }
132 
133     /**
134      * Shutdown the AvalonComponentService where the MimeTypeService
135      * resides and lookup the MimeTypeService. This should trigger
136      * a late initialization of AvalonComponentService and returns
137      * a fully functional MimeTypeService.
138      */
139     @Test public void testAvalonComponentServiceShutdown() throws Exception
140     {
141         ServiceManager serviceManager = TurbineServices.getInstance();
142         serviceManager.shutdownService(AvalonComponentService.SERVICE_NAME);
143 
144         MimeTypeService mimeTypeService = (MimeTypeService) serviceManager.getService(MimeTypeService.class.getName());
145         assertNotNull(mimeTypeService);
146 
147         Locale locale = new Locale("en", "US");
148         String s = mimeTypeService.getCharSet(locale);
149         assertEquals("ISO-8859-1", s);
150     }
151 
152     @BeforeClass
153     public static void setUp() throws Exception
154     {
155         tc = new TurbineConfig(".", "/conf/test/TestFulcrumComponents.properties");
156         tc.initialize();
157     }
158     @AfterClass
159     public static void tearDown() throws Exception
160     {
161         if (tc != null)
162         {
163             tc.dispose();
164         }
165     }
166     
167 }