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.interpol;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNull;
21  import static org.junit.jupiter.api.Assertions.assertSame;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  import static org.mockito.Mockito.mock;
25  
26  import java.util.Arrays;
27  import java.util.Collection;
28  import java.util.HashMap;
29  import java.util.Map;
30  import java.util.Objects;
31  import java.util.function.Function;
32  
33  import org.junit.jupiter.api.BeforeEach;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * Test class for {@code InterpolatorSpecification}.
38   */
39  public class TestInterpolatorSpecification {
40  
41      /** Constant for a prefix for a prefix lookup. */
42      private static final String PREFIX1 = "p1";
43  
44      /** Constant for another prefix for a prefix lookup. */
45      private static final String PREFIX2 = "p2";
46  
47      /**
48       * Checks whether the given test object contains the expected default lookups.
49       *
50       * @param spec the object to be tested
51       * @param defLook1 default lookup 1
52       * @param defLook2 default lookup 2
53       */
54      private static void checkDefaultLookups(final InterpolatorSpecification spec, final Lookup defLook1, final Lookup defLook2) {
55          assertEquals(2, spec.getDefaultLookups().size());
56          assertTrue(spec.getDefaultLookups().containsAll(Arrays.asList(defLook1, defLook2)));
57      }
58  
59      /**
60       * Checks whether the given test object contains the expected prefix lookups.
61       *
62       * @param spec the object to be tested
63       * @param prefLook1 prefix lookup 1
64       * @param prefLook2 prefix lookup 2
65       */
66      private static void checkPrefixLookups(final InterpolatorSpecification spec, final Lookup prefLook1, final Lookup prefLook2) {
67          assertEquals(2, spec.getPrefixLookups().size());
68          assertSame(prefLook1, spec.getPrefixLookups().get(PREFIX1));
69          assertSame(prefLook2, spec.getPrefixLookups().get(PREFIX2));
70      }
71  
72      /**
73       * Convenience method for creating a mock lookup.
74       *
75       * @return the mock lookup
76       */
77      private static Lookup createLookup() {
78          return mock(Lookup.class);
79      }
80  
81      /** The builder for creating new instances. */
82      private InterpolatorSpecification.Builder builder;
83  
84      @BeforeEach
85      public void setUp() throws Exception {
86          builder = new InterpolatorSpecification.Builder();
87      }
88  
89      /**
90       * Tests whether a builder can be reused.
91       */
92      @Test
93      void testBuilderReuse() {
94          builder
95              .withDefaultLookup(createLookup())
96              .withInterpolator(mock(ConfigurationInterpolator.class))
97              .withPrefixLookup("test", createLookup())
98              .withParentInterpolator(mock(ConfigurationInterpolator.class))
99              .withStringConverter(obj -> "test")
100             .create();
101         final Lookup prefLook1 = createLookup();
102         final Lookup prefLook2 = createLookup();
103         final Lookup defLook1 = createLookup();
104         final Lookup defLook2 = createLookup();
105         final ConfigurationInterpolator parent = mock(ConfigurationInterpolator.class);
106         final Function<Object, String> stringConverter = Objects::toString;
107         final InterpolatorSpecification spec = builder
108             .withPrefixLookup(PREFIX1, prefLook1)
109             .withPrefixLookup(PREFIX2, prefLook2)
110             .withDefaultLookups(Arrays.asList(defLook1, defLook2))
111             .withParentInterpolator(parent)
112             .withStringConverter(stringConverter)
113             .create();
114         assertNull(spec.getInterpolator());
115         assertSame(parent, spec.getParentInterpolator());
116         assertSame(stringConverter, spec.getStringConverter());
117         checkPrefixLookups(spec, prefLook1, prefLook2);
118         checkDefaultLookups(spec, defLook1, defLook2);
119     }
120 
121     /**
122      * Tests whether an instance with all possible properties can be set.
123      */
124     @Test
125     void testCreateInstance() {
126         final Lookup prefLook1 = createLookup();
127         final Lookup prefLook2 = createLookup();
128         final Lookup defLook1 = createLookup();
129         final Lookup defLook2 = createLookup();
130         final ConfigurationInterpolator interpolator = mock(ConfigurationInterpolator.class);
131         final ConfigurationInterpolator parent = mock(ConfigurationInterpolator.class);
132         final Function<Object, String> stringConverter = Objects::toString;
133         final InterpolatorSpecification spec = builder
134             .withPrefixLookup(PREFIX1, prefLook1)
135             .withDefaultLookup(defLook1)
136             .withPrefixLookup(PREFIX2, prefLook2)
137             .withParentInterpolator(parent)
138             .withDefaultLookup(defLook2)
139             .withInterpolator(interpolator)
140             .withStringConverter(stringConverter)
141             .create();
142         assertSame(interpolator, spec.getInterpolator());
143         assertSame(parent, spec.getParentInterpolator());
144         assertSame(stringConverter, spec.getStringConverter());
145         checkPrefixLookups(spec, prefLook1, prefLook2);
146         checkDefaultLookups(spec, defLook1, defLook2);
147     }
148 
149     /**
150      * Tests whether lookups can be set passing in full collections.
151      */
152     @Test
153     void testCreateInstanceCollections() {
154         final Lookup prefLook1 = createLookup();
155         final Lookup prefLook2 = createLookup();
156         final Lookup defLook1 = createLookup();
157         final Lookup defLook2 = createLookup();
158         final Map<String, Lookup> prefixLookups = new HashMap<>();
159         prefixLookups.put(PREFIX1, prefLook1);
160         prefixLookups.put(PREFIX2, prefLook2);
161         final InterpolatorSpecification spec = builder
162             .withPrefixLookups(prefixLookups)
163             .withDefaultLookups(Arrays.asList(defLook1, defLook2))
164             .create();
165         checkPrefixLookups(spec, prefLook1, prefLook2);
166         checkDefaultLookups(spec, defLook1, defLook2);
167     }
168 
169     /**
170      * Tests that the collection with default lookups cannot be modified.
171      */
172     @Test
173     void testGetDefaultLookupsModify() {
174         final InterpolatorSpecification spec = builder.withDefaultLookup(createLookup()).create();
175         final Collection<Lookup> lookups = spec.getDefaultLookups();
176         final Lookup lookup = createLookup();
177         assertThrows(UnsupportedOperationException.class, () -> lookups.add(lookup));
178     }
179 
180     /**
181      * Tests that the map with prefix lookups cannot be modified.
182      */
183     @Test
184     void testGetPrefixLookupsModify() {
185         final InterpolatorSpecification spec = builder.withPrefixLookup(PREFIX1, createLookup()).create();
186         final Lookup lookup = createLookup();
187         assertThrows(UnsupportedOperationException.class, () -> spec.getPrefixLookups().put(PREFIX1, lookup));
188     }
189 
190     /**
191      * Tests whether a null default lookup causes an exception.
192      */
193     @Test
194     void testWithDefaultLookupNull() {
195         assertThrows(IllegalArgumentException.class, () -> builder.withDefaultLookup(null));
196     }
197 
198     /**
199      * Tests whether a null collection with default lookups is accepted.
200      */
201     @Test
202     void testWithDefaultLookupsNull() {
203         final InterpolatorSpecification spec = builder.withDefaultLookups(null).create();
204         assertTrue(spec.getDefaultLookups().isEmpty());
205     }
206 
207     /**
208      * Tests whether a null prefix lookup causes an exception.
209      */
210     @Test
211     void testWithPrefixLookupNoLookup() {
212         assertThrows(IllegalArgumentException.class, () -> builder.withPrefixLookup(PREFIX1, null));
213     }
214 
215     /**
216      * Tests whether a null prefix causes an exception.
217      */
218     @Test
219     void testWithPrefixLookupNoPrefix() {
220         final Lookup lookup = createLookup();
221         assertThrows(IllegalArgumentException.class, () -> builder.withPrefixLookup(null, lookup));
222     }
223 
224     /**
225      * Tests whether a null map with prefix lookups is accepted.
226      */
227     @Test
228     void testWithPrefixLookupsNull() {
229         final InterpolatorSpecification spec = builder.withPrefixLookups(null).create();
230         assertTrue(spec.getPrefixLookups().isEmpty());
231     }
232 }