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;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertIterableEquals;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.List;
27  
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Test class for {@code ConfigurationLookup}.
32   */
33  public class TestConfigurationLookup {
34      /** Constant for a test variable name. */
35      private static final String VAR = "testVariable";
36  
37      /** Constant for the value of the test variable. */
38      private static final Object VALUE = "SomeTestValue";
39  
40      /**
41       * Tries to create an instance without a configuration.
42       */
43      @Test
44      public void testInitNoConfig() {
45          assertThrows(IllegalArgumentException.class, () -> new ConfigurationLookup(null));
46      }
47  
48      /**
49       * Tests lookup() for a complex property value.
50       */
51      @Test
52      public void testLookupComplex() {
53          final int count = 5;
54          final Configuration conf = new BaseConfiguration();
55          for (int i = 0; i < count; i++) {
56              conf.addProperty(VAR, String.valueOf(VALUE) + i);
57          }
58          final ConfigurationLookup lookup = new ConfigurationLookup(conf);
59          final Collection<?> col = (Collection<?>) lookup.lookup(VAR);
60  
61          final List<String> expected = new ArrayList<>();
62          for (int i = 0; i < count; i++) {
63              expected.add(String.valueOf(VALUE) + i);
64          }
65          assertIterableEquals(expected, col);
66      }
67  
68      /**
69       * Tests lookup() if the variable cannot be resolved.
70       */
71      @Test
72      public void testLookupNotFound() {
73          final Configuration conf = new BaseConfiguration();
74          final ConfigurationLookup lookup = new ConfigurationLookup(conf);
75          assertNull(lookup.lookup(VAR));
76      }
77  
78      /**
79       * Tests lookup() if the variable cannot be resolved, and the configuration throws an exception.
80       */
81      @Test
82      public void testLookupNotFoundEx() {
83          final BaseConfiguration conf = new BaseConfiguration();
84          conf.setThrowExceptionOnMissing(true);
85          final ConfigurationLookup lookup = new ConfigurationLookup(conf);
86          assertNull(lookup.lookup(VAR));
87      }
88  
89      /**
90       * Tests whether an existing variable can be resolved.
91       */
92      @Test
93      public void testLookupSuccess() {
94          final Configuration conf = new BaseConfiguration();
95          conf.addProperty(VAR, VALUE);
96          final ConfigurationLookup lookup = new ConfigurationLookup(conf);
97          assertEquals(VALUE, lookup.lookup(VAR));
98      }
99  }