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