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