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.assertTrue;
21  
22  import java.io.StringReader;
23  
24  import org.apache.commons.configuration2.convert.DisabledListDelimiterHandler;
25  import org.apache.commons.configuration2.convert.LegacyListDelimiterHandler;
26  import org.apache.commons.configuration2.convert.ListDelimiterHandler;
27  import org.apache.commons.configuration2.ex.ConfigurationException;
28  import org.apache.commons.configuration2.io.FileHandler;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Tests for {@code XMLConfiguration} related to CONFIGURATION-605: XMLConfiguration drops configuration key immediately
33   * following one whose value contains a comma
34   */
35  public class TestXMLConfiguration_605 {
36      /**
37       * Checks whether the specified configuration contains all expected keys.
38       *
39       * @param config the configuration to be checked
40       */
41      private static void checkConfiguration(final Configuration config) {
42          assertTrue(config.containsKey("key0"));
43          assertTrue(config.containsKey("key1"));
44          assertTrue(config.containsKey("key3"));
45  
46          assertTrue(config.containsKey("key2"));
47      }
48  
49      /**
50       * Creates a configuration with the specified content and the legacy list delimiter handler.
51       *
52       * @param content the XML content
53       * @return the newly created configuration
54       */
55      private static Configuration create(final String content) throws ConfigurationException {
56          final XMLConfiguration config = new XMLConfiguration();
57          config.setListDelimiterHandler(new LegacyListDelimiterHandler(','));
58          final FileHandler handler = new FileHandler(config);
59          handler.load(new StringReader(content));
60          return config;
61      }
62  
63      /**
64       * Creates a new configuration with the specified content and the given list delimiter handler.
65       *
66       * @param content the XML content
67       * @param delimiterHandler the list delimiter handler
68       * @return the newly created configuration
69       */
70      private static Configuration create(final String content, final ListDelimiterHandler delimiterHandler) throws ConfigurationException {
71          final XMLConfiguration config = new XMLConfiguration();
72          config.setListDelimiterHandler(delimiterHandler);
73          final FileHandler handler = new FileHandler(config);
74          handler.load(new StringReader(content));
75          return config;
76      }
77  
78      @Test
79      public void testWithCommaSeparatedList() throws Exception {
80          final String source = "<configuration><key0></key0><key1>a,b</key1><key2></key2><key3></key3></configuration>";
81          checkConfiguration(create(source));
82      }
83  
84      @Test
85      public void testWithNoComma() throws Exception {
86          final String source = "<configuration><key0></key0><key1></key1><key2></key2><key3></key3></configuration>";
87          checkConfiguration(create(source));
88      }
89  
90      @Test
91      public void testWithOnlyComma() throws Exception {
92          final String source = "<configuration><key0></key0><key1>,</key1><key2></key2><key3></key3></configuration>";
93          checkConfiguration(create(source));
94      }
95  
96      @Test
97      public void testWithOnlyCommaWithoutDelimiterParsing() throws Exception {
98          final String source = "<configuration><key0></key0><key1>,</key1><key2></key2><key3></key3></configuration>";
99          checkConfiguration(create(source, DisabledListDelimiterHandler.INSTANCE));
100     }
101 
102     @Test
103     public void testWithOnlyCommaWithStringBuilder() throws Exception {
104         final StringBuilder sourceBuilder = new StringBuilder("<configuration>");
105         sourceBuilder.append("<key0></key0>");
106         sourceBuilder.append("<key1>,</key1>");
107         sourceBuilder.append("<key2></key2>");
108         sourceBuilder.append("<key3></key3>");
109         sourceBuilder.append("</configuration>");
110         checkConfiguration(create(sourceBuilder.toString()));
111     }
112 
113     @Test
114     public void testWithOnlyCommaWithStringBuilderWithoutDelimiterParsing() throws Exception {
115         final StringBuilder sourceBuilder = new StringBuilder("<configuration>");
116         sourceBuilder.append("<key0></key0>");
117         sourceBuilder.append("<key1>,</key1>");
118         sourceBuilder.append("<key2></key2>");
119         sourceBuilder.append("<key3></key3>");
120         sourceBuilder.append("</configuration>");
121         checkConfiguration(create(sourceBuilder.toString(), DisabledListDelimiterHandler.INSTANCE));
122     }
123 
124     @Test
125     public void testWithSeparatingNonWhitespace() throws Exception {
126         final String source = "<configuration><key0></key0><key1>,</key1>A<key2></key2><key3></key3></configuration>";
127         checkConfiguration(create(source));
128     }
129 
130     @Test
131     public void testWithSeparatingWhitespace() throws Exception {
132         final String source = "<configuration><key0></key0><key1>,</key1> <key2></key2><key3></key3></configuration>";
133         checkConfiguration(create(source));
134     }
135 }