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.text.lookup;
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.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  
24  import java.text.SimpleDateFormat;
25  import java.util.Date;
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import org.junit.jupiter.api.AfterAll;
30  import org.junit.jupiter.api.Assertions;
31  import org.junit.jupiter.api.BeforeAll;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Tests {@link InterpolatorStringLookup}.
36   */
37  class InterpolatorStringLookupTest {
38  
39      private static final String TESTKEY = "TestKey";
40  
41      private static final String TESTKEY2 = "TestKey2";
42  
43      private static final String TESTVAL = "TestValue";
44  
45      @AfterAll
46      public static void afterAll() {
47          System.clearProperty(TESTKEY);
48          System.clearProperty(TESTKEY2);
49      }
50  
51      @BeforeAll
52      public static void beforeAll() {
53          System.setProperty(TESTKEY, TESTVAL);
54          System.setProperty(TESTKEY2, TESTVAL);
55      }
56  
57      private void assertLookupNotEmpty(final StringLookup lookup, final String key) {
58          final String value = lookup.apply(key);
59          assertNotNull(value);
60          assertFalse(value.isEmpty());
61          // System.out.println(key + " = " + value);
62      }
63  
64      private void check(final StringLookup lookup) {
65          String value = lookup.apply("sys:" + TESTKEY);
66          assertEquals(TESTVAL, value);
67          value = lookup.apply("env:PATH");
68          assertNotNull(value);
69          value = lookup.apply("date:yyyy-MM-dd");
70          assertNotNull(value, "No Date");
71          final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
72          final String today = format.format(new Date());
73          assertEquals(value, today);
74          assertLookupNotEmpty(lookup, "java:version");
75          assertLookupNotEmpty(lookup, "java:runtime");
76          assertLookupNotEmpty(lookup, "java:vm");
77          assertLookupNotEmpty(lookup, "java:os");
78          assertLookupNotEmpty(lookup, "java:locale");
79          assertLookupNotEmpty(lookup, "java:hardware");
80      }
81  
82      @Test
83      void testLookup() {
84          final Map<String, String> map = new HashMap<>();
85          map.put(TESTKEY, TESTVAL);
86          final StringLookup lookup = new InterpolatorStringLookup(StringLookupFactory.INSTANCE.mapStringLookup(map));
87          String value = lookup.apply(TESTKEY);
88          assertEquals(TESTVAL, value);
89          value = lookup.apply("ctx:" + TESTKEY);
90          assertEquals(TESTVAL, value);
91          value = lookup.apply("sys:" + TESTKEY);
92          assertEquals(TESTVAL, value);
93          value = lookup.apply("BadKey");
94          assertNull(value);
95          value = lookup.apply("ctx:" + TESTKEY);
96          assertEquals(TESTVAL, value);
97      }
98  
99      @Test
100     void testLookupKeys() {
101         final InterpolatorStringLookup lookup = new InterpolatorStringLookup((Map<String, Object>) null);
102         final Map<String, StringLookup> stringLookupMap = lookup.getStringLookupMap();
103         StringLookupFactoryTest.assertDefaultKeys(stringLookupMap);
104     }
105 
106     @Test
107     void testLookupWithDefaultInterpolator() {
108         check(new InterpolatorStringLookup());
109     }
110 
111     @Test
112     void testLookupWithNullDefaultInterpolator() {
113         check(new InterpolatorStringLookup((StringLookup) null));
114     }
115 
116     @Test
117     void testNull() {
118         Assertions.assertNull(InterpolatorStringLookup.INSTANCE.apply(null));
119     }
120 
121     @Test
122     void testToString() {
123         // does not blow up and gives some kind of string.
124         Assertions.assertFalse(new InterpolatorStringLookup().toString().isEmpty());
125     }
126 }