Skip to content

Commit 5d7315e

Browse files
committed
OPENNLP-1932: Add DirectoryModelFinderTest
DirectoryModelFinder had no test. The new test copies the opennlp-models jars from the test class path into a temporary directory one level below the scanned root, runs the shared finder assertions against it, and checks the non-recursive mode, a narrowing jar prefix, an unknown prefix, and the null directory rejection.
1 parent 8fbb780 commit 5d7315e

1 file changed

Lines changed: 126 additions & 0 deletions

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
* http://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+
package opennlp.tools.models.dir;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.net.URISyntaxException;
22+
import java.net.URL;
23+
import java.net.URLClassLoader;
24+
import java.nio.file.Files;
25+
import java.nio.file.Path;
26+
import java.util.ArrayList;
27+
import java.util.List;
28+
import java.util.Set;
29+
30+
import org.junit.jupiter.api.Assertions;
31+
import org.junit.jupiter.api.BeforeAll;
32+
import org.junit.jupiter.api.Test;
33+
import org.junit.jupiter.api.io.TempDir;
34+
35+
import opennlp.tools.models.AbstractClassPathFinderTest;
36+
import opennlp.tools.models.ClassPathModelEntry;
37+
import opennlp.tools.models.ClassPathModelFinder;
38+
39+
/**
40+
* Runs the shared finder tests against a directory holding copies of the model jars
41+
* from the test classpath, placed one level below the scanned root.
42+
*/
43+
public class DirectoryModelFinderTest extends AbstractClassPathFinderTest {
44+
45+
private static final String MODEL_JAR_PREFIX = "opennlp-models-";
46+
private static final String JAR_SUFFIX = ".jar";
47+
48+
@TempDir
49+
private static Path root;
50+
private static Path modelDir;
51+
52+
@BeforeAll
53+
static void copyModelJars() throws IOException, URISyntaxException {
54+
modelDir = Files.createDirectory(root.resolve("models"));
55+
final List<Path> jars = modelJarsOnClassPath();
56+
Assertions.assertFalse(jars.isEmpty(), "no model jars found on the test classpath");
57+
for (Path jar : jars) {
58+
Files.copy(jar, modelDir.resolve(jar.getFileName()));
59+
}
60+
}
61+
62+
private static List<Path> modelJarsOnClassPath() throws URISyntaxException {
63+
final List<Path> jars = new ArrayList<>();
64+
final ClassLoader cl = Thread.currentThread().getContextClassLoader();
65+
if (cl instanceof URLClassLoader ucl) {
66+
for (URL url : ucl.getURLs()) {
67+
if ("file".equals(url.getProtocol())) {
68+
addIfModelJar(Path.of(url.toURI()), jars);
69+
}
70+
}
71+
}
72+
if (jars.isEmpty()) {
73+
for (String element : System.getProperty("java.class.path", "").split(File.pathSeparator)) {
74+
addIfModelJar(Path.of(element), jars);
75+
}
76+
}
77+
return jars;
78+
}
79+
80+
private static void addIfModelJar(Path candidate, List<Path> jars) {
81+
final String name = String.valueOf(candidate.getFileName());
82+
if (name.startsWith(MODEL_JAR_PREFIX) && name.endsWith(JAR_SUFFIX) && Files.isRegularFile(candidate)) {
83+
jars.add(candidate);
84+
}
85+
}
86+
87+
@Override
88+
protected ClassPathModelFinder getModelFinder() {
89+
return new DirectoryModelFinder(null, root, true);
90+
}
91+
92+
@Override
93+
protected ClassPathModelFinder getModelFinder(String pattern) {
94+
return new DirectoryModelFinder(pattern, root, true);
95+
}
96+
97+
@Test
98+
void testNonRecursiveSkipsSubdirectories() {
99+
final Set<ClassPathModelEntry> models = new DirectoryModelFinder(null, root, false).findModels(false);
100+
Assertions.assertNotNull(models);
101+
Assertions.assertTrue(models.isEmpty());
102+
}
103+
104+
@Test
105+
void testNonRecursiveFindsDirectChildren() {
106+
final Set<ClassPathModelEntry> models = new DirectoryModelFinder(null, modelDir, false).findModels(false);
107+
Assertions.assertEquals(4, models.size());
108+
}
109+
110+
@Test
111+
void testJarPrefixNarrowsTheResult() {
112+
final Set<ClassPathModelEntry> pos =
113+
new DirectoryModelFinder("opennlp-models-pos-*", root, true).findModels(false);
114+
Assertions.assertEquals(1, pos.size());
115+
Assertions.assertTrue(pos.iterator().next().model().toString().contains("opennlp-models-pos-"));
116+
117+
final Set<ClassPathModelEntry> none =
118+
new DirectoryModelFinder("opennlp-models-unknown-*", root, true).findModels(false);
119+
Assertions.assertTrue(none.isEmpty());
120+
}
121+
122+
@Test
123+
void testNullDirectoryIsRejected() {
124+
Assertions.assertThrows(IllegalArgumentException.class, () -> new DirectoryModelFinder(null, null, true));
125+
}
126+
}

0 commit comments

Comments
 (0)