001    package org.apache.turbine.services;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.util.Locale;
023    
024    import org.apache.fulcrum.cache.GlobalCacheService;
025    import org.apache.fulcrum.crypto.CryptoService;
026    import org.apache.fulcrum.factory.FactoryService;
027    import org.apache.fulcrum.intake.IntakeService;
028    import org.apache.fulcrum.localization.LocalizationService;
029    import org.apache.fulcrum.mimetype.MimeTypeService;
030    import org.apache.turbine.services.avaloncomponent.AvalonComponentService;
031    import org.apache.turbine.test.BaseTestCase;
032    import org.apache.turbine.util.TurbineConfig;
033    
034    /**
035     * Unit test for verifing that we can load all the appropriate components from the
036     * appropriate Container.  For now that is just ECM (AvalonComponentService)
037     * but in the future with mixed containers there could be multiple.
038     *
039     * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
040     * @author <a href="mailto:sgoeschl@apache.org">Siegfried Goeschl</a>
041     * @version $Id: LoadingComponentsTest.java 947330 2010-05-22 19:38:55Z tv $
042     */
043    public class LoadingComponentsTest extends BaseTestCase
044    {
045        private static TurbineConfig tc = null;
046        public LoadingComponentsTest(String name) throws Exception
047        {
048            super(name);
049        }
050    
051        /**
052         * Test to load a couple of Avalon services directly by the
053         * AvalonComponentService.
054         *
055         * @throws Exception loading failed
056         */
057        public void testLoadingByAvalonComponentService() throws Exception
058        {
059            AvalonComponentService avalonComponentService =
060                (AvalonComponentService) TurbineServices.getInstance().getService(
061                        AvalonComponentService.SERVICE_NAME);
062    
063            assertNotNull(avalonComponentService);
064    
065            GlobalCacheService dgcs = (GlobalCacheService)avalonComponentService.lookup(GlobalCacheService.ROLE);
066            assertNotNull(dgcs);
067            CryptoService cs = (CryptoService)avalonComponentService.lookup(CryptoService.ROLE);
068            assertNotNull(cs);
069            LocalizationService ls = (LocalizationService)avalonComponentService.lookup(LocalizationService.ROLE);
070            assertNotNull(ls);
071            IntakeService intake = (IntakeService)avalonComponentService.lookup(IntakeService.ROLE);
072            assertNotNull(intake);
073            FactoryService fs = (FactoryService)avalonComponentService.lookup(FactoryService.ROLE);
074            assertNotNull(fs);
075            MimeTypeService mimetype = (MimeTypeService)avalonComponentService.lookup(MimeTypeService.ROLE);
076            assertNotNull(mimetype);
077        }
078    
079        /**
080         * Test to load a couple of Avalon services by using the
081         * TurbineServices which delegate the service retrieval to
082         * the AvalonComponentService
083         *
084         * @throws Exception loading failed
085         */
086        public void testLoadingByTurbineServices() throws Exception
087        {
088            ServiceManager serviceManager = TurbineServices.getInstance();
089    
090            GlobalCacheService gcs = (GlobalCacheService)serviceManager.getService(GlobalCacheService.ROLE);
091            assertNotNull(gcs);
092            CryptoService cs = (CryptoService)serviceManager.getService(CryptoService.ROLE);
093            assertNotNull(cs);
094            LocalizationService ls = (LocalizationService)serviceManager.getService(LocalizationService.ROLE);
095            assertNotNull(ls);
096            IntakeService intake = (IntakeService)serviceManager.getService(IntakeService.ROLE);
097            assertNotNull(intake);
098            FactoryService fs = (FactoryService)serviceManager.getService(FactoryService.ROLE);
099            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    }