1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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 }