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