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  
18  package org.apache.commons.text.lookup;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  
24  import org.junit.jupiter.api.AfterEach;
25  import org.junit.jupiter.api.BeforeEach;
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Tests {@link ConstantStringLookup}.
30   */
31  class ConstantStringLookupBasicTest {
32  
33      /**
34       * Test fixture.
35       */
36      public static final String NULL_STRING_FIXTURE = null;
37  
38      /**
39       * Test fixture.
40       */
41      public static final String STRING_FIXTURE = "Hello World!";
42  
43      /**
44       * Clears the test environment. Here the static cache of the constant lookup class is wiped out.
45       */
46      @AfterEach
47      public void afterEach() {
48          ConstantStringLookup.clear();
49      }
50  
51      /**
52       * Clears the test environment. Here the static cache of the constant lookup class is wiped out.
53       */
54      @BeforeEach
55      public void beforeEach() {
56          ConstantStringLookup.clear();
57      }
58  
59      @Test
60      void testNull() {
61          assertNull(ConstantStringLookup.INSTANCE.apply(null));
62      }
63  
64      @Test
65      void testNullClassFetch() {
66          assertNull(new ConstantStringLookup() {
67              @Override
68              protected Class<?> fetchClass(final String className) throws ClassNotFoundException {
69                  return null;
70              }
71          }.apply(ConstantStringLookupBasicTest.class.getName() + ".STRING_FIXTURE"));
72      }
73  
74      @Test
75      void testNullValue() {
76          assertEquals(NULL_STRING_FIXTURE, ConstantStringLookup.INSTANCE
77              .apply(ConstantStringLookupBasicTest.class.getName() + ".NULL_STRING_FIXTURE"));
78      }
79  
80      @Test
81      void testOne() {
82          assertEquals(STRING_FIXTURE,
83              ConstantStringLookup.INSTANCE.apply(ConstantStringLookupBasicTest.class.getName() + ".STRING_FIXTURE"));
84      }
85  
86      @Test
87      void testToString() {
88          // does not blow up and gives some kind of string.
89          assertFalse(ConstantStringLookup.INSTANCE.toString().isEmpty());
90      }
91  
92  }