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.assertThrows;
22  
23  import java.io.IOException;
24  import java.nio.charset.StandardCharsets;
25  import java.nio.file.Files;
26  import java.nio.file.Path;
27  import java.nio.file.Paths;
28  
29  import org.apache.commons.lang3.StringUtils;
30  import org.apache.commons.text.StringSubstitutor;
31  import org.junit.jupiter.api.Assertions;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Tests {@link FileStringLookup}.
36   */
37  public class FileStringLookupTest {
38  
39      private static final Path DOCUEMENT_PATH = Paths.get("src/test/resources/org/apache/commons/text/document.properties");
40      private static final Path CURRENT_PATH = Paths.get(StringUtils.EMPTY);
41  
42      public static String readDocumentFixtureString() throws IOException {
43          return new String(Files.readAllBytes(DOCUEMENT_PATH), StandardCharsets.UTF_8);
44      }
45  
46      public static void testFence(final String expectedString, final FileStringLookup fileStringLookup) {
47          Assertions.assertEquals(expectedString, fileStringLookup.apply("UTF-8:src/test/resources/org/apache/commons/text/document.properties"));
48          assertThrows(IllegalArgumentException.class, () -> fileStringLookup.apply("UTF-8:/src/test/resources/org/apache/commons/text/document.properties"));
49          assertThrows(IllegalArgumentException.class, () -> fileStringLookup.apply("UTF-8:../src/test/resources/org/apache/commons/text/document.properties"));
50      }
51  
52      public static void testFence(final StringSubstitutor stringSubstitutor) throws IOException {
53          assertEquals(readDocumentFixtureString(), stringSubstitutor.replace("${file:UTF-8:" + DOCUEMENT_PATH + "}"));
54          assertThrows(IllegalArgumentException.class, () -> stringSubstitutor.replace("${file:UTF-8:/foo.txt}"));
55          assertThrows(IllegalArgumentException.class, () -> stringSubstitutor.replace("${file:UTF-8:../foo.txt}"));
56      }
57  
58      @Test
59      void testDefaultInstanceBadCharsetName() {
60          assertThrows(IllegalArgumentException.class,
61                  () -> FileStringLookup.INSTANCE.apply("BAD_CHARSET_NAME:src/test/resources/org/apache/commons/text/document.properties"));
62      }
63  
64      @Test
65      void testDefaultInstanceBadDocumentPath() {
66          assertThrows(IllegalArgumentException.class, () -> FileStringLookup.INSTANCE.apply("BAD_CHARSET_NAME:src/test/resources/DOCUMENT_NOT_FOUND.TXT"));
67      }
68  
69      @Test
70      void testDefaultInstanceMissingFilePart() {
71          assertThrows(IllegalArgumentException.class, () -> FileStringLookup.INSTANCE.apply(StandardCharsets.UTF_8.name()));
72      }
73  
74      @Test
75      void testDefaultInstanceNull() {
76          Assertions.assertNull(FileStringLookup.INSTANCE.apply(null));
77      }
78  
79      @Test
80      void testDefaultInstanceOne() throws Exception {
81          final String expectedString = readDocumentFixtureString();
82          Assertions.assertEquals(expectedString, FileStringLookup.INSTANCE.apply("UTF-8:src/test/resources/org/apache/commons/text/document.properties"));
83      }
84  
85      @Test
86      void testDefaultInstanceToString() {
87          // does not blow up and gives some kind of string.
88          Assertions.assertFalse(FileStringLookup.INSTANCE.toString().isEmpty());
89      }
90  
91      @Test
92      void testFenceBadDirOne() throws Exception {
93          final FileStringLookup fileStringLookup = new FileStringLookup(Paths.get("dir does not exist at all"));
94          assertThrows(IllegalArgumentException.class, () -> fileStringLookup.apply("UTF-8:src/test/resources/org/apache/commons/text/document.properties"));
95          assertThrows(IllegalArgumentException.class, () -> fileStringLookup.apply("UTF-8:/src/test/resources/org/apache/commons/text/document.properties"));
96          assertThrows(IllegalArgumentException.class, () -> fileStringLookup.apply("UTF-8:../src/test/resources/org/apache/commons/text/document.properties"));
97      }
98  
99      @Test
100     void testFenceBadDirPlusGoodOne() throws Exception {
101         final String expectedString = readDocumentFixtureString();
102         final FileStringLookup fileStringLookup = new FileStringLookup(Paths.get("dir does not exist at all"), CURRENT_PATH);
103         testFence(expectedString, fileStringLookup);
104     }
105 
106     @Test
107     void testFenceCurrentDirOne() throws Exception {
108         final String expectedString = readDocumentFixtureString();
109         final FileStringLookup fileStringLookup = new FileStringLookup(CURRENT_PATH);
110         testFence(expectedString, fileStringLookup);
111     }
112 
113     @Test
114     void testFenceCurrentDirPlusOne() throws Exception {
115         final String expectedString = readDocumentFixtureString();
116         final FileStringLookup fileStringLookup = new FileStringLookup(Paths.get("target"), CURRENT_PATH);
117         testFence(expectedString, fileStringLookup);
118     }
119 
120     @Test
121     void testFenceEmptyOne() throws Exception {
122         final String expectedString = readDocumentFixtureString();
123         Assertions.assertEquals(expectedString, new FileStringLookup().apply("UTF-8:src/test/resources/org/apache/commons/text/document.properties"));
124     }
125 
126     @Test
127     void testFenceNullOne() throws Exception {
128         final String expectedString = readDocumentFixtureString();
129         Assertions.assertEquals(expectedString,
130                 new FileStringLookup((Path[]) null).apply("UTF-8:src/test/resources/org/apache/commons/text/document.properties"));
131     }
132 
133     @Test
134     void testInterpolatorReplace() throws IOException {
135         final StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator();
136         assertEquals(readDocumentFixtureString(), stringSubstitutor.replace("${file:UTF-8:" + DOCUEMENT_PATH + "}"));
137         final InterpolatorStringLookup stringLookup = (InterpolatorStringLookup) stringSubstitutor.getStringLookup();
138         stringLookup.getStringLookupMap().replace(StringLookupFactory.KEY_FILE, StringLookupFactory.INSTANCE.fileStringLookup(CURRENT_PATH));
139         testFence(stringSubstitutor);
140     }
141 }