View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.configuration;
19  
20  import static org.junit.Assert.assertTrue;
21  
22  import java.io.File;
23  
24  import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
25  import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  /**
30   * Unit test for simple MultiConfigurationTest.
31   *
32   * @version $Id: TestPatternSubtreeConfiguration.java 1224818 2011-12-26 21:20:17Z oheger $
33   */
34  public class TestPatternSubtreeConfiguration
35  {
36      private static String CONFIG_FILE = "target/test-classes/testPatternSubtreeConfig.xml";
37      private static String PATTERN = "BusinessClient[@name='${sys:Id}']";
38      private XMLConfiguration conf;
39  
40      @Before
41      public void setUp() throws Exception
42      {
43          conf = new XMLConfiguration();
44          conf.setFile(new File(CONFIG_FILE));
45          conf.load();
46      }
47  
48      /**
49       * Rigourous Test :-)
50       */
51      @Test
52      public void testMultiConfiguration()
53      {
54          //set up a reloading strategy
55          FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
56          strategy.setRefreshDelay(10000);
57  
58          PatternSubtreeConfigurationWrapper config = new PatternSubtreeConfigurationWrapper(this.conf, PATTERN);
59          config.setReloadingStrategy(strategy);
60          config.setExpressionEngine(new XPathExpressionEngine());
61  
62          System.setProperty("Id", "1001");
63          assertTrue(config.getInt("rowsPerPage") == 15);
64  
65          System.setProperty("Id", "1002");
66          assertTrue(config.getInt("rowsPerPage") == 25);
67  
68          System.setProperty("Id", "1003");
69          assertTrue(config.getInt("rowsPerPage") == 35);
70      }
71  }