    001package org.apache.turbine;
    002
    003
    004/*
    005 * Licensed to the Apache Software Foundation (ASF) under one
    006 * or more contributor license agreements.  See the NOTICE file
    007 * distributed with this work for additional information
    008 * regarding copyright ownership.  The ASF licenses this file
    009 * to you under the Apache License, Version 2.0 (the
    010 * "License"); you may not use this file except in compliance
    011 * with the License.  You may obtain a copy of the License at
    012 *
    013 *   http://www.apache.org/licenses/LICENSE-2.0
    014 *
    015 * Unless required by applicable law or agreed to in writing,
    016 * software distributed under the License is distributed on an
    017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    018 * KIND, either express or implied.  See the License for the
    019 * specific language governing permissions and limitations
    020 * under the License.
    021 */
    022
    023
    024import static org.junit.Assert.assertEquals;
    025import static org.junit.Assert.assertFalse;
    026import static org.junit.Assert.assertNotNull;
    027
    028import java.io.File;
    029import java.net.URL;
    030
    031import org.apache.commons.configuration.Configuration;
    032import org.apache.commons.configuration.FileSystem;
    033import org.apache.commons.configuration.PropertiesConfiguration;
    034import org.apache.turbine.test.BaseTestCase;
    035import org.apache.turbine.util.TurbineConfig;
    036import org.apache.turbine.util.TurbineXmlConfig;
    037import org.junit.Test;
    038
    039/**
    040 * Tests that the ConfigurationFactory and regular old properties methods both work.
    041 * Verify the overriding of properties.
    042 *
    043 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
    044 * @version $Id: ConfigurationTest.java 1695636 2015-08-13 00:45:19Z tv $
    045 */
    046public class ConfigurationTest extends BaseTestCase
    047{
    048    public static final String SERVICE_PREFIX = "services.";
    049
    050    /**
    051     * A <code>Service</code> property determining its implementing
    052     * class name .
    053     */
    054    public static final String CLASSNAME_SUFFIX = ".classname";
    055
    056    private TurbineConfig tc = null;
    057    private TurbineXmlConfig txc = null;
    058
    059    @Test
    060    public void testCreateTurbineWithConfigurationXML() throws Exception
    061    {
    062        txc = new TurbineXmlConfig(".", "conf/test/TurbineConfiguration.xml");
    063
    064        try
    065        {
    066            txc.initialize();
    067
    068            Configuration configuration = Turbine.getConfiguration();
    069            assertNotNull("No Configuration Object found!", configuration);
    070            assertFalse("Make sure we have values", configuration.isEmpty());
    071
    072            // overridden value
    073            String key = "module.cache";
    074            assertEquals("Read a config value " + key + ", received:" + configuration.getString(key), "true", configuration.getString(key));
    075
    076            // non overridden value
    077            key = "scheduledjob.cache.size";
    078            assertEquals("Read a config value " + key + ", received:" + configuration.getString(key), "10", configuration.getString(key));
    079        }
    080        catch (Exception e)
    081        {
    082            throw e;
    083        }
    084        finally
    085        {
    086            txc.dispose();
    087        }
    088    }
    089
    090    @Test
    091    public void testCreateTurbineWithConfiguration() throws Exception
    092    {
    093        tc = new TurbineConfig(".", "/conf/test/TemplateService.properties");
    094
    095        try
    096        {
    097            tc.initialize();
    098
    099            Configuration configuration = Turbine.getConfiguration();
    100            assertNotNull("No Configuration Object found!", configuration);
    101            assertFalse("Make sure we have values", configuration.isEmpty());
    102
    103            String key = "scheduledjob.cache.size";
    104            assertEquals("Read a config value " + key + ", received:" + configuration.getString(key), "10", configuration.getString(key));
    105        }
    106        catch (Exception e)
    107        {
    108            throw e;
    109        }
    110        finally
    111        {
    112            tc.dispose();
    113        }
    114    }
    115
    116    @Test
    117    public void testCreateTurbineWithIncludedConfiguration() throws Exception
    118    {
    119        String confPath = Turbine.getRealPath( "/conf/test/usersettings.properties" );
    120        try
    121        {
    122            Configuration configuration = new PropertiesConfiguration(confPath);
    123            assertNotNull("No Configuration Object found!", configuration);
    124            assertFalse("Make sure we have values", configuration.isEmpty());
    125
    126            String key = "scheduledjob.cache.size";
    127            assertEquals("Read a config value " + key + ", received:" + configuration.getString(key), "100", configuration.getString(key));
    128            String key2 ="module.cache";
    129            assertEquals("Read a config value " + key2 + ", received:" + configuration.getString(key2), "false", configuration.getString(key2));
    130        }
    131        catch (Exception e)
    132        {
    133            throw e;
    134        }
    135    }
    136
    137    @SuppressWarnings("boxing")
    138    @Test
    139    public void testCreateTurbineWithXMLBuilderConfiguration() throws Exception
    140    {
    141        String configurationRessourcePath ="conf/test/ConfigurationBuilder.xml";
    142        tc = new TurbineXmlConfig(".",configurationRessourcePath );
    143
    144        try
    145        {
    146            tc.initialize();
    147
    148            Configuration configuration = Turbine.getConfiguration();
    149            assertNotNull("No Configuration Object found!", configuration);
    150            assertFalse("Make sure we have values", configuration.isEmpty());
    151
    152            //assertTrue("Test  combined configuration is"+ configuration, configuration instanceof CombinedConfiguration);
    153
    154            // overridden value
    155            String key = "scheduledjob.cache.size";
    156            assertEquals("Read a config value " + key + ", received:" + configuration.getInt(key), 100, configuration.getInt(key));
    157
    158            // double overridden value
    159            key = "module.cache";
    160            assertEquals("Read a config value " + key + ", received:" + configuration.getBoolean(key), false, configuration.getBoolean(key));
    161            // new property
    162            key = "tests.test";
    163            configuration.addProperty( key, 123 );
    164            assertEquals("Read a config value " + key + ", received:" + configuration.getInt(key), 123, configuration.getInt(key));
    165            // not set
    166            key="test.nulltest3";
    167            assertEquals("Read a included config value " + key + ", received:" + configuration.getString(key), null, configuration.getString(key));
    168            // overridden value
    169            key="services.PullService.earlyInit";
    170            assertEquals("Read a config value " + key + ", received:" + configuration.getBoolean(key), true, configuration.getBoolean(key));
    171            configuration.setProperty( key, false );
    172            assertEquals("Read a config value " + key + ", received:" + configuration.getBoolean(key), false, configuration.getBoolean(key));
    173
    174            // converts to URL, cft. RFC2396
    175            URL testURL = FileSystem.getDefaultFileSystem().locateFromURL(new File( Turbine.getApplicationRoot()).toURI().toString() , configurationRessourcePath);
    176            assertNotNull( "Should be a valid URL",testURL);
    177        }
    178        finally
    179        {
    180            tc.dispose();
    181        }
    182    }
    183
    184}