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