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