001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.configuration;
019    
020    import static org.junit.Assert.assertTrue;
021    
022    import java.io.File;
023    
024    import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
025    import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
026    import org.junit.Before;
027    import org.junit.Test;
028    
029    /**
030     * Unit test for simple MultiConfigurationTest.
031     *
032     * @version $Id: TestPatternSubtreeConfiguration.java 1224818 2011-12-26 21:20:17Z oheger $
033     */
034    public class TestPatternSubtreeConfiguration
035    {
036        private static String CONFIG_FILE = "target/test-classes/testPatternSubtreeConfig.xml";
037        private static String PATTERN = "BusinessClient[@name='${sys:Id}']";
038        private XMLConfiguration conf;
039    
040        @Before
041        public void setUp() throws Exception
042        {
043            conf = new XMLConfiguration();
044            conf.setFile(new File(CONFIG_FILE));
045            conf.load();
046        }
047    
048        /**
049         * Rigourous Test :-)
050         */
051        @Test
052        public void testMultiConfiguration()
053        {
054            //set up a reloading strategy
055            FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
056            strategy.setRefreshDelay(10000);
057    
058            PatternSubtreeConfigurationWrapper config = new PatternSubtreeConfigurationWrapper(this.conf, PATTERN);
059            config.setReloadingStrategy(strategy);
060            config.setExpressionEngine(new XPathExpressionEngine());
061    
062            System.setProperty("Id", "1001");
063            assertTrue(config.getInt("rowsPerPage") == 15);
064    
065            System.setProperty("Id", "1002");
066            assertTrue(config.getInt("rowsPerPage") == 25);
067    
068            System.setProperty("Id", "1003");
069            assertTrue(config.getInt("rowsPerPage") == 35);
070        }
071    }