View Javadoc
1   package org.apache.turbine;
2   
3   
4   /*
5    * Licensed to the Apache Software Foundation (ASF) under one
6    * or more contributor license agreements.  See the NOTICE file
7    * distributed with this work for additional information
8    * regarding copyright ownership.  The ASF licenses this file
9    * to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License.  You may obtain a copy of the License at
12   *
13   *   http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on an
17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertFalse;
26  import static org.junit.Assert.assertNotNull;
27  
28  import java.io.File;
29  import java.net.URL;
30  
31  import org.apache.commons.configuration.Configuration;
32  import org.apache.commons.configuration.FileSystem;
33  import org.apache.commons.configuration.PropertiesConfiguration;
34  import org.apache.turbine.test.BaseTestCase;
35  import org.apache.turbine.util.TurbineConfig;
36  import org.apache.turbine.util.TurbineXmlConfig;
37  import org.junit.Test;
38  
39  /**
40   * Tests that the ConfigurationFactory and regular old properties methods both work.
41   * Verify the overriding of properties.
42   *
43   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
44   * @version $Id: ConfigurationTest.java 1695636 2015-08-13 00:45:19Z tv $
45   */
46  public class ConfigurationTest extends BaseTestCase
47  {
48      public static final String SERVICE_PREFIX = "services.";
49  
50      /**
51       * A <code>Service</code> property determining its implementing
52       * class name .
53       */
54      public static final String CLASSNAME_SUFFIX = ".classname";
55  
56      private TurbineConfig tc = null;
57      private TurbineXmlConfig txc = null;
58  
59      @Test
60      public void testCreateTurbineWithConfigurationXML() throws Exception
61      {
62          txc = new TurbineXmlConfig(".", "conf/test/TurbineConfiguration.xml");
63  
64          try
65          {
66              txc.initialize();
67  
68              Configuration configuration = Turbine.getConfiguration();
69              assertNotNull("No Configuration Object found!", configuration);
70              assertFalse("Make sure we have values", configuration.isEmpty());
71  
72              // overridden value
73              String key = "module.cache";
74              assertEquals("Read a config value " + key + ", received:" + configuration.getString(key), "true", configuration.getString(key));
75  
76              // non overridden value
77              key = "scheduledjob.cache.size";
78              assertEquals("Read a config value " + key + ", received:" + configuration.getString(key), "10", configuration.getString(key));
79          }
80          catch (Exception e)
81          {
82              throw e;
83          }
84          finally
85          {
86              txc.dispose();
87          }
88      }
89  
90      @Test
91      public void testCreateTurbineWithConfiguration() throws Exception
92      {
93          tc = new TurbineConfig(".", "/conf/test/TemplateService.properties");
94  
95          try
96          {
97              tc.initialize();
98  
99              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 }