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;
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.assertThrows;
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  import java.util.Properties;
26  import java.util.ResourceBundle;
27  
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Test class for {@link StrLookup}.
32   *
33   * @deprecated This class will be removed in 2.0.
34   */
35  class StrLookupTest  {
36  
37      @Test
38      void testMapLookup() {
39          final Map<String, Object> map = new HashMap<>();
40          map.put("key", "value");
41          map.put("number", 2);
42          assertEquals("value", StrLookup.mapLookup(map).apply("key"));
43          assertEquals("2", StrLookup.mapLookup(map).apply("number"));
44          assertNull(StrLookup.mapLookup(map).apply(null));
45          assertNull(StrLookup.mapLookup(map).apply(""));
46          assertNull(StrLookup.mapLookup(map).apply("other"));
47      }
48  
49      @Test
50      void testMapLookup_nullMap() {
51          final Map<String, ?> map = null;
52          assertNull(StrLookup.mapLookup(map).apply(null));
53          assertNull(StrLookup.mapLookup(map).apply(""));
54          assertNull(StrLookup.mapLookup(map).apply("any"));
55      }
56  
57      @Test
58      void testNoneLookup() {
59          assertNull(StrLookup.noneLookup().apply(null));
60          assertNull(StrLookup.noneLookup().apply(""));
61          assertNull(StrLookup.noneLookup().apply("any"));
62      }
63  
64      @Test
65      void testResourceBundleLookup() {
66          final ResourceBundle map = ResourceBundle.getBundle("org.apache.commons.text.example.testResourceBundleLookup");
67          assertEquals("value", StrLookup.resourceBundleLookup(map).apply("key"));
68          assertEquals("2", StrLookup.resourceBundleLookup(map).lookup("number"));
69          assertNull(StrLookup.resourceBundleLookup(map).apply(null));
70          assertNull(StrLookup.resourceBundleLookup(map).apply(""));
71          assertNull(StrLookup.resourceBundleLookup(map).apply("other"));
72      }
73  
74      @Test
75      void testResourceBundleLookup_nullMap() {
76          final ResourceBundle resourceBundle = null;
77          assertNull(StrLookup.resourceBundleLookup(resourceBundle).apply(null));
78          assertNull(StrLookup.resourceBundleLookup(resourceBundle).apply(""));
79          assertNull(StrLookup.resourceBundleLookup(resourceBundle).apply("any"));
80      }
81  
82      @Test
83      void testSystemPropertiesLookup() {
84          assertEquals(System.getProperty("os.name"), StrLookup.systemPropertiesLookup().apply("os.name"));
85          assertNull(StrLookup.systemPropertiesLookup().apply(""));
86          assertNull(StrLookup.systemPropertiesLookup().apply("other"));
87          assertThrows(NullPointerException.class, () -> StrLookup.systemPropertiesLookup().apply(null));
88      }
89  
90      /**
91       * Tests that a lookup object for system properties can deal with a full
92       * replacement of the system properties object. This test is related to
93       * LANG-1055.
94       */
95      @Test
96      void testSystemPropertiesLookupReplacedProperties() {
97          final Properties oldProperties = System.getProperties();
98          final String osName = "os.name";
99          final String newOsName = oldProperties.getProperty(osName) + "_changed";
100 
101         final StrLookup<String> sysLookup = StrLookup.systemPropertiesLookup();
102         final Properties newProps = new Properties();
103         newProps.setProperty(osName, newOsName);
104         System.setProperties(newProps);
105         try {
106             assertEquals(newOsName, sysLookup.apply(osName), "Changed properties not detected");
107         } finally {
108             System.setProperties(oldProperties);
109         }
110     }
111 
112     /**
113      * Tests that a lookup object for system properties sees changes on system
114      * properties. This test is related to LANG-1141.
115      */
116     @Test
117     void testSystemPropertiesLookupUpdatedProperty() {
118         final String osName = "os.name";
119         final String oldOs = System.getProperty(osName);
120         final String newOsName = oldOs + "_changed";
121 
122         final StrLookup<String> sysLookup = StrLookup.systemPropertiesLookup();
123         System.setProperty(osName, newOsName);
124         try {
125             assertEquals(newOsName, sysLookup.apply(osName), "Changed properties not detected");
126         } finally {
127             System.setProperty(osName, oldOs);
128         }
129     }
130 
131 }