forked from apache/commons-text
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathFileStringLookupPartitionTest.java
More file actions
82 lines (70 loc) · 2.97 KB
/
FileStringLookupPartitionTest.java
File metadata and controls
82 lines (70 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache license, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the license for the specific language governing permissions and
* limitations under the license.
*/
package org.apache.commons.text.lookup;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
public class FileStringLookupPartitionTest {
private final FileStringLookup lookup = new FileStringLookup();
@Test
void testNullKeyReturnsNull() {
assertNull(lookup.lookup(null));
}
@Test
void testKeyMissingColonThrows() {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () ->
lookup.lookup("UTF-8")
);
assertTrue(ex.getMessage().contains("Bad file key format"));
}
@Test
void testInvalidCharsetThrows() {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () ->
lookup.lookup("FAKECHARSET:/some/fake/file.txt")
);
assertTrue(ex.getMessage().contains("Error looking up file"));
}
@Test
void testNonexistentFileThrows() {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () ->
lookup.lookup("UTF-8:/definitely/does/not/exist.txt")
);
assertTrue(ex.getMessage().contains("Error looking up file"));
}
@Test
void testValidKeyReturnsFileContents() throws Exception {
Path tempFile = Files.createTempFile("test-file", ".txt");
Files.write(tempFile, "Hello, world!".getBytes(StandardCharsets.UTF_8));
String key = "UTF-8:" + tempFile.toString();
String result = lookup.lookup(key);
assertEquals("Hello, world!", result);
}
@Test
void testMultipleColonsStillWorksOrThrowsMeaningfully() {
// File doesn't exist, but structure is valid
String key = "UTF-8:/fake/path:ignored";
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () ->
lookup.lookup(key)
);
assertTrue(ex.getMessage().contains("Error looking up file"));
}
}