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