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    *      http://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  
18  package org.apache.commons.lang3.text;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  import java.util.Properties;
26  
27  import org.apache.commons.lang3.AbstractLangTest;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Test class for StrLookup.
32   */
33  @Deprecated
34  public class StrLookupTest extends AbstractLangTest {
35  
36      @Test
37      public void testMapLookup() {
38          final Map<String, Object> map = new HashMap<>();
39          map.put("key", "value");
40          map.put("number", Integer.valueOf(2));
41          assertEquals("value", StrLookup.mapLookup(map).lookup("key"));
42          assertEquals("2", StrLookup.mapLookup(map).lookup("number"));
43          assertNull(StrLookup.mapLookup(map).lookup(null));
44          assertNull(StrLookup.mapLookup(map).lookup(""));
45          assertNull(StrLookup.mapLookup(map).lookup("other"));
46      }
47  
48      @Test
49      public void testMapLookup_nullMap() {
50          final Map<String, ?> map = null;
51          assertNull(StrLookup.mapLookup(map).lookup(null));
52          assertNull(StrLookup.mapLookup(map).lookup(""));
53          assertNull(StrLookup.mapLookup(map).lookup("any"));
54      }
55  
56      @Test
57      public void testNoneLookup() {
58          assertNull(StrLookup.noneLookup().lookup(null));
59          assertNull(StrLookup.noneLookup().lookup(""));
60          assertNull(StrLookup.noneLookup().lookup("any"));
61      }
62  
63      @Test
64      public void testSystemPropertiesLookup() {
65          assertEquals(System.getProperty("os.name"), StrLookup.systemPropertiesLookup().lookup("os.name"));
66          assertNull(StrLookup.systemPropertiesLookup().lookup(""));
67          assertNull(StrLookup.systemPropertiesLookup().lookup("other"));
68          assertNull(StrLookup.systemPropertiesLookup().lookup(null));
69      }
70  
71      /**
72       * Tests that a lookup object for system properties can deal with a full
73       * replacement of the system properties object. This test is related to
74       * LANG-1055.
75       */
76      @Test
77      public void testSystemPropertiesLookupReplacedProperties() {
78          final Properties oldProperties = System.getProperties();
79          final String osName = "os.name";
80          final String newOsName = oldProperties.getProperty(osName) + "_changed";
81  
82          final StrLookup<String> sysLookup = StrLookup.systemPropertiesLookup();
83          final Properties newProps = new Properties();
84          newProps.setProperty(osName, newOsName);
85          System.setProperties(newProps);
86          try {
87              assertEquals(newOsName, sysLookup.lookup(osName), "Changed properties not detected");
88          } finally {
89              System.setProperties(oldProperties);
90          }
91      }
92  
93      /**
94       * Tests that a lookup object for system properties sees changes on system
95       * properties. This test is related to LANG-1141.
96       */
97      @Test
98      public void testSystemPropertiesLookupUpdatedProperty() {
99          final String osName = "os.name";
100         final String oldOs = System.getProperty(osName);
101         final String newOsName = oldOs + "_changed";
102 
103         final StrLookup<String> sysLookup = StrLookup.systemPropertiesLookup();
104         System.setProperty(osName, newOsName);
105         try {
106             assertEquals(newOsName, sysLookup.lookup(osName), "Changed properties not detected");
107         } finally {
108             System.setProperty(osName, oldOs);
109         }
110     }
111 
112 }