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    *     https://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  package org.apache.commons.configuration2.convert;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertIterableEquals;
21  import static org.mockito.Mockito.mock;
22  import static org.mockito.Mockito.verify;
23  import static org.mockito.Mockito.verifyNoMoreInteractions;
24  import static org.mockito.Mockito.when;
25  
26  import java.util.Arrays;
27  import java.util.Collection;
28  import java.util.List;
29  
30  import org.junit.jupiter.api.BeforeEach;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Test class for {@code DefaultListDelimiterHandler}.
35   */
36  public class TestDefaultListDelimiterHandler {
37  
38      /** The handler to be tested. */
39      private DefaultListDelimiterHandler handler;
40  
41      /**
42       * Helper methods for testing a split operation. A split is executed with the passed in parameters. Then the results are
43       * compared to the expected elements.
44       *
45       * @param value the value to be split
46       * @param trim the trim flag
47       * @param expectedElements the expected results
48       */
49      private void checkSplit(final String value, final boolean trim, final String... expectedElements) {
50          final Collection<String> elems = handler.split(value, trim);
51          assertIterableEquals(Arrays.asList(expectedElements), elems);
52      }
53  
54      @BeforeEach
55      public void setUp() throws Exception {
56          handler = new DefaultListDelimiterHandler(',');
57      }
58  
59      @Test
60      void testEscapeIntegerList() {
61          final ValueTransformer trans = ListDelimiterHandler.NOOP_TRANSFORMER;
62          final List<Integer> data = Arrays.asList(1, 2, 3, 4);
63          assertEquals("1,2,3,4", handler.escapeList(data, trans));
64      }
65  
66      /**
67       * Tests whether a list is correctly escaped.
68       */
69      @Test
70      void testEscapeList() {
71          final ValueTransformer trans = value -> String.valueOf(value) + "_trans";
72          final List<String> data = Arrays.asList("simple", "Hello,world!", "\\,\\", "end");
73          assertEquals("simple_trans,Hello\\,world!_trans,\\\\\\,\\\\_trans,end_trans", handler.escapeList(data, trans));
74      }
75  
76      /**
77       * Tests whether a backslash is correctly escaped.
78       */
79      @Test
80      void testEscapeStringBackslash() {
81          assertEquals("C:\\\\Temp\\\\", handler.escapeString("C:\\Temp\\"));
82      }
83  
84      /**
85       * Tests whether the list delimiter character is correctly escaped in a string.
86       */
87      @Test
88      void testEscapeStringListDelimiter() {
89          assertEquals("3\\,1415", handler.escapeString("3,1415"));
90      }
91  
92      /**
93       * Tests whether combinations of list delimiters and backslashes are correctly escaped.
94       */
95      @Test
96      void testEscapeStringListDelimiterAndBackslash() {
97          assertEquals("C:\\\\Temp\\\\\\,\\\\\\\\Share\\,/root", handler.escapeString("C:\\Temp\\,\\\\Share,/root"));
98      }
99  
100     /**
101      * Tests whether a string is correctly escaped which does not contain any special character.
102      */
103     @Test
104     void testEscapeStringNoSpecialCharacter() {
105         assertEquals("test", handler.escapeString("test"));
106     }
107 
108     /**
109      * Tests whether a value transformer is correctly called when escaping a single value.
110      */
111     @Test
112     void testEscapeWithTransformer() {
113         final ValueTransformer trans = mock(ValueTransformer.class);
114 
115         when(trans.transformValue("a\\,b")).thenReturn("ok");
116 
117         assertEquals("ok", handler.escape("a,b", trans));
118 
119         verify(trans).transformValue("a\\,b");
120         verifyNoMoreInteractions(trans);
121     }
122 
123     /**
124      * Tests whether split() deals correctly with escaped backslashes.
125      */
126     @Test
127     void testSplitEscapeBackslash() {
128         checkSplit("C:\\\\Temp\\\\", true, "C:\\Temp\\");
129     }
130 
131     /**
132      * Tests whether a line delimiter can be escaped when splitting a list.
133      */
134     @Test
135     void testSplitEscapeLineDelimiter() {
136         checkSplit("3\\,1415", true, "3,1415");
137     }
138 
139     /**
140      * Tests a split operation with a complex combination of list delimiters and backslashes.
141      */
142     @Test
143     void testSplitEscapeListDelimiterAndBackslashes() {
144         checkSplit("C:\\\\Temp\\\\\\,\\\\\\\\Share\\\\,/root", false, "C:\\Temp\\,\\\\Share\\", "/root");
145     }
146 
147     /**
148      * Tests whether a string list is split correctly.
149      */
150     @Test
151     void testSplitList() {
152         checkSplit("a, b,c   ,   d", true, "a", "b", "c", "d");
153     }
154 
155     /**
156      * Tests whether trimming can be disabled when splitting a list.
157      */
158     @Test
159     void testSplitNoTrim() {
160         checkSplit("a , b,  c  ,d", false, "a ", " b", "  c  ", "d");
161     }
162 
163     /**
164      * Tests split() if there is only a single element.
165      */
166     @Test
167     void testSplitSingleElement() {
168         checkSplit("test", true, "test");
169     }
170 
171     /**
172      * Tests whether an unexpected escape character is handled properly.
173      */
174     @Test
175     void testSplitUnexpectedEscape() {
176         checkSplit("\\x, \\,y, \\", true, "\\x", ",y", "\\");
177     }
178 }