    001package 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
    022import static org.junit.Assert.assertEquals;
    023import static org.junit.Assert.assertNotNull;
    024import static org.junit.Assert.fail;
    025
    026import java.util.Locale;
    027
    028import org.apache.fulcrum.cache.GlobalCacheService;
    029import org.apache.fulcrum.crypto.CryptoService;
    030import org.apache.fulcrum.factory.FactoryService;
    031import org.apache.fulcrum.intake.IntakeService;
    032import org.apache.fulcrum.localization.LocalizationService;
    033import org.apache.fulcrum.mimetype.MimeTypeService;
    034import org.apache.turbine.services.avaloncomponent.AvalonComponentService;
    035import org.apache.turbine.test.BaseTestCase;
    036import org.apache.turbine.util.TurbineConfig;
    037import org.junit.AfterClass;
    038import org.junit.BeforeClass;
    039import org.junit.Test;
    040
    041/**
    042 * Unit test for verifing that we can load all the appropriate components from the
    043 * appropriate Container.  For now that is just ECM (AvalonComponentService)
    044 * but in the future with mixed containers there could be multiple.
    045 *
    046 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
    047 * @author <a href="mailto:sgoeschl@apache.org">Siegfried Goeschl</a>
    048 * @version $Id: LoadingComponentsTest.java 1812628 2017-10-19 12:34:25Z gk $
    049 */
    050public class LoadingComponentsTest extends BaseTestCase
    051{
    052    private static TurbineConfig tc = null;
    053
    054
    055    /**
    056     * Test to load a couple of Avalon services directly by the
    057     * AvalonComponentService.
    058     *
    059     * @throws Exception loading failed
    060     */
    061    @Test public void testLoadingByAvalonComponentService() throws Exception
    062    {
    063        AvalonComponentService avalonComponentService =
    064            (AvalonComponentService) TurbineServices.getInstance().getService(
    065                    AvalonComponentService.SERVICE_NAME);
    066
    067        assertNotNull(avalonComponentService);
    068
    069        GlobalCacheService dgcs = (GlobalCacheService)avalonComponentService.lookup(GlobalCacheService.ROLE);
    070        assertNotNull(dgcs);
    071        CryptoService cs = (CryptoService)avalonComponentService.lookup(CryptoService.ROLE);
    072        assertNotNull(cs);
    073        LocalizationService ls = (LocalizationService)avalonComponentService.lookup(LocalizationService.ROLE);
    074        assertNotNull(ls);
    075        IntakeService intake = (IntakeService)avalonComponentService.lookup(IntakeService.ROLE);
    076        assertNotNull(intake);
    077        FactoryService fs = (FactoryService)avalonComponentService.lookup(FactoryService.ROLE);
    078        assertNotNull(fs);
    079        MimeTypeService mimetype = (MimeTypeService)avalonComponentService.lookup(MimeTypeService.ROLE);
    080        assertNotNull(mimetype);
    081        //avalonComponentService.shutdown();
    082    }
    083
    084    /**
    085     * Test to load a couple of Avalon services by using the
    086     * TurbineServices which delegate the service retrieval to
    087     * the AvalonComponentService
    088     *
    089     * @throws Exception loading failed
    090     */
    091    @Test public void testLoadingByTurbineServices() throws Exception
    092    {
    093        ServiceManager serviceManager = TurbineServices.getInstance();
    094
    095        GlobalCacheService gcs = (GlobalCacheService)serviceManager.getService(GlobalCacheService.ROLE);
    096        assertNotNull(gcs);
    097        CryptoService cs = (CryptoService)serviceManager.getService(CryptoService.ROLE);
    098        assertNotNull(cs);
    099        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}