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.configuration2;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import java.io.File;
24  import java.io.StringWriter;
25  
26  import org.apache.commons.configuration2.ex.ConfigurationException;
27  import org.apache.commons.configuration2.io.FileHandler;
28  import org.apache.commons.configuration2.tree.ImmutableNode;
29  import org.apache.commons.configuration2.tree.xpath.XPathExpressionEngine;
30  import org.junit.jupiter.api.BeforeEach;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Unit test for simple MultiConfigurationTest.
35   */
36  public class TestPatternSubtreeConfiguration {
37      private static final File CONFIG_FILE = ConfigurationAssert.getTestFile("testPatternSubtreeConfig.xml");
38      private static final String PATTERN = "BusinessClient[@name='${sys:Id}']";
39      private XMLConfiguration conf;
40  
41      @BeforeEach
42      public void setUp() throws Exception {
43          conf = new XMLConfiguration();
44          new FileHandler(conf).load(CONFIG_FILE);
45      }
46  
47      /**
48       * Rigorous Test :-)
49       */
50      @Test
51      public void testMultiConfiguration() {
52          final PatternSubtreeConfigurationWrapper config = new PatternSubtreeConfigurationWrapper(this.conf, PATTERN);
53          config.setExpressionEngine(new XPathExpressionEngine());
54  
55          System.setProperty("Id", "1001");
56          assertEquals(15, config.getInt("rowsPerPage"));
57  
58          System.setProperty("Id", "1002");
59          assertEquals(25, config.getInt("rowsPerPage"));
60  
61          System.setProperty("Id", "1003");
62          assertEquals(35, config.getInt("rowsPerPage"));
63      }
64  
65      /**
66       * Tests a read operation if the wrapped configuration does not implement FileBased.
67       */
68      @Test
69      public void testReadNotFileBased() {
70          final HierarchicalConfiguration<ImmutableNode> hc = new BaseHierarchicalConfiguration();
71          final PatternSubtreeConfigurationWrapper config = new PatternSubtreeConfigurationWrapper(hc, PATTERN);
72          final FileHandler fileHandler = new FileHandler(config);
73          assertThrows(ConfigurationException.class, () -> fileHandler.load(CONFIG_FILE));
74      }
75  
76      /**
77       * Tests a write operation if the wrapped configuration does not implement FileBased.
78       */
79      @Test
80      public void testSaveNotFileBased() {
81          final HierarchicalConfiguration<ImmutableNode> hc = new BaseHierarchicalConfiguration();
82          final PatternSubtreeConfigurationWrapper config = new PatternSubtreeConfigurationWrapper(hc, PATTERN);
83          final FileHandler fileHandler = new FileHandler(config);
84          final StringWriter writer = new StringWriter();
85          assertThrows(ConfigurationException.class, () -> fileHandler.save(writer));
86      }
87  }