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