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