View Javadoc
1   package org.apache.commons.jcs.engine;
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 junit.framework.TestCase;
23  
24  import org.apache.commons.jcs.JCS;
25  import org.apache.commons.jcs.access.CacheAccess;
26  import org.apache.commons.jcs.engine.control.CompositeCacheManager;
27  import org.apache.commons.jcs.utils.props.PropertyLoader;
28  
29  import java.util.Properties;
30  
31  /**
32   * Verify that system properties can override.
33   */
34  public class SystemPropertyUsageUnitTest
35      extends TestCase
36  {
37      private static final String JCS_DEFAULT_CACHEATTRIBUTES_MAX_OBJECTS = "jcs.default.cacheattributes.MaxObjects";
38      private static final int testValue = 6789;
39  
40      private CompositeCacheManager manager = null;
41      
42      @Override
43      protected void setUp() throws Exception
44      {
45         super.setUp();
46         //First shut down any previously running manager.
47         manager = CompositeCacheManager.getInstance();
48         manager.shutDown();
49      }
50  
51  	/**
52  	 * @see junit.framework.TestCase#tearDown()
53  	 */
54  	@Override
55  	protected void tearDown() throws Exception
56  	{
57  		if (manager != null)
58  		{
59  			manager.shutDown();
60  		}
61  
62          System.clearProperty(JCS_DEFAULT_CACHEATTRIBUTES_MAX_OBJECTS);
63  		super.tearDown();
64  	}
65  
66  	/**
67       * Verify that the system properties are used.
68       * @throws Exception
69       *
70       */
71      public void testSystemPropertyUsage()
72          throws Exception
73      {
74          System.setProperty( JCS_DEFAULT_CACHEATTRIBUTES_MAX_OBJECTS, String.valueOf(testValue) );
75          
76          JCS.setConfigFilename( "/TestSystemPropertyUsage.ccf" );
77          
78          CacheAccess<String, String> jcs = JCS.getInstance( "someCacheNotInFile" );
79  
80          manager = CompositeCacheManager.getInstance();
81  
82          assertEquals( "System property value is not reflected.", testValue, jcs.getCacheAttributes().getMaxObjects());
83      }
84  
85      /**
86       * Verify that the system properties are not used is specified.
87       *
88       * @throws Exception
89       *
90       */
91      public void testSystemPropertyUsage_inactive()
92          throws Exception
93      {
94          System.setProperty( JCS_DEFAULT_CACHEATTRIBUTES_MAX_OBJECTS, String.valueOf(testValue) );
95  
96          manager = CompositeCacheManager.getUnconfiguredInstance();
97  
98          Properties props = PropertyLoader.loadProperties( "TestSystemPropertyUsage.ccf" );
99  
100         manager.configure( props, false );
101 
102         CacheAccess<String, String> jcs = JCS.getInstance( "someCacheNotInFile" );
103 
104         assertEquals( "System property value should not be reflected",
105                       Integer.parseInt( props.getProperty( JCS_DEFAULT_CACHEATTRIBUTES_MAX_OBJECTS ) ),
106                       jcs.getCacheAttributes().getMaxObjects());
107     }
108 }