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.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.mockito.Mockito.mock;
23  import static org.mockito.Mockito.verify;
24  import static org.mockito.Mockito.verifyNoMoreInteractions;
25  import static org.mockito.Mockito.when;
26  
27  import java.util.ArrayList;
28  import java.util.Arrays;
29  import java.util.Collection;
30  import java.util.Iterator;
31  import java.util.List;
32  
33  import org.junit.jupiter.api.BeforeEach;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * Test class for {@code DisabledListDelimiterHandler}. Note that some functionality of the base class is tested, too.
38   */
39  public class TestDisabledListDelimiterHandler {
40  
41      /** An array with some test values. */
42      private static final Object[] VALUES = {20130630213801L, "A test value", 5};
43  
44      /** Constant for a test string value. */
45      private static final String STR_VALUE = "  A test, string; value! ";
46  
47      /**
48       * Checks whether the passed in container contains the expected values.
49       *
50       * @param container the iterator to test
51       */
52      private static void checkIterator(final Iterable<?> container) {
53          final Iterator<?> it = container.iterator();
54          for (final Object o : VALUES) {
55              assertEquals(o, it.next());
56          }
57          assertFalse(it.hasNext());
58      }
59  
60      /** The instance to be tested. */
61      private DisabledListDelimiterHandler handler;
62  
63      @BeforeEach
64      public void setUp() throws Exception {
65          handler = new DisabledListDelimiterHandler();
66      }
67  
68      /**
69       * Tests escapeList(). This operation is not supported.
70       */
71      @Test
72      void testEscapeList() {
73          final List<Object> values = Arrays.asList(VALUES);
74          assertThrows(UnsupportedOperationException.class, () -> handler.escapeList(values, ListDelimiterHandler.NOOP_TRANSFORMER));
75      }
76  
77      /**
78       * Tests whether a non-string value is correctly escaped. The object should not be modified.
79       */
80      @Test
81      void testEscapeNonStringValue() {
82          final Object value = 42;
83          assertEquals(value, handler.escape(value, ListDelimiterHandler.NOOP_TRANSFORMER));
84      }
85  
86      /**
87       * Tests whether the transformer is correctly called when escaping a non string value.
88       */
89      @Test
90      void testEscapeNonStringValueTransformer() {
91          final ValueTransformer trans = mock(ValueTransformer.class);
92          final Object value = 42;
93  
94          when(trans.transformValue(value)).thenReturn(STR_VALUE);
95  
96          assertEquals(STR_VALUE, handler.escape(value, trans));
97  
98          verify(trans).transformValue(value);
99          verifyNoMoreInteractions(trans);
100     }
101 
102     /**
103      * Tests whether a string value is correctly escaped. The string should not be modified.
104      */
105     @Test
106     void testEscapeStringValue() {
107         assertEquals(STR_VALUE, handler.escape(STR_VALUE, ListDelimiterHandler.NOOP_TRANSFORMER));
108     }
109 
110     /**
111      * Tests whether the transformer is correctly invoked when escaping a string.
112      */
113     @Test
114     void testEscapeStringValueTransformer() {
115         final ValueTransformer trans = mock(ValueTransformer.class);
116         final String testStr = "Some other string";
117 
118         when(trans.transformValue(testStr)).thenReturn(STR_VALUE);
119 
120         assertEquals(STR_VALUE, handler.escape(testStr, trans));
121 
122         verify(trans).transformValue(testStr);
123         verifyNoMoreInteractions(trans);
124     }
125 
126     /**
127      * Tests whether a limit is applied when extracting values from an array.
128      */
129     @Test
130     void testFlattenArrayWithLimit() {
131         final Collection<?> res = handler.flatten(VALUES, 1);
132         assertEquals(1, res.size());
133         assertEquals(VALUES[0], res.iterator().next());
134     }
135 
136     /**
137      * Tests whether elements can be extracted from a collection that contains an array if a limit is specified.
138      */
139     @Test
140     void testFlattenCollectionWithArrayWithLimit() {
141         final Collection<Object> src = new ArrayList<>(2);
142         src.add(STR_VALUE);
143         src.add(VALUES);
144         final Collection<?> res = handler.flatten(src, 2);
145         assertEquals(2, res.size());
146         final Iterator<?> it = res.iterator();
147         assertEquals(STR_VALUE, it.next());
148         assertEquals(VALUES[0], it.next());
149     }
150 
151     /**
152      * Tests whether a limit is applied when extracting elements from a collection.
153      */
154     @Test
155     void testFlattenCollectionWithLimit() {
156         final Collection<Object> src = Arrays.asList(VALUES);
157         final Collection<?> res = handler.flatten(src, 1);
158         assertEquals(1, res.size());
159         assertEquals(VALUES[0], res.iterator().next());
160     }
161 
162     /**
163      * Tests whether the values of an array can be extracted.
164      */
165     @Test
166     void testParseArray() {
167         checkIterator(handler.parse(VALUES));
168     }
169 
170     /**
171      * Tests whether the values of an Iterable object can be extracted.
172      */
173     @Test
174     void testParseIterable() {
175         checkIterator(handler.parse(Arrays.asList(VALUES)));
176     }
177 
178     /**
179      * Tests whether the values of an Iterator object can be extracted.
180      */
181     @Test
182     void testParseIterator() {
183         checkIterator(handler.parse(Arrays.asList(VALUES).iterator()));
184     }
185 
186     /**
187      * Tests whether a null value can be parsed.
188      */
189     @Test
190     void testParseNull() {
191         assertFalse(handler.parse(null).iterator().hasNext());
192     }
193 
194     /**
195      * Tests whether a simple string value can be parsed.
196      */
197     @Test
198     void testParseSimpleValue() {
199         final Iterator<?> it = handler.parse(STR_VALUE).iterator();
200         assertEquals(STR_VALUE, it.next());
201         assertFalse(it.hasNext());
202     }
203 }