Skip to content

Commit 3a54e08

Browse files
committed
OPENNLP-1532 Conduct cleanup in existing test code
1 parent 9fa715e commit 3a54e08

30 files changed

+205
-228
lines changed

opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkerFactoryTest.java

+10-12
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ void testDefaultFactory() throws IOException {
5757
ChunkerModel model = trainModel(ModelType.MAXENT, new ChunkerFactory());
5858

5959
ChunkerFactory factory = model.getFactory();
60-
Assertions.assertTrue(factory.getContextGenerator() instanceof DefaultChunkerContextGenerator);
61-
Assertions.assertTrue(factory.getSequenceValidator() instanceof DefaultChunkerSequenceValidator);
60+
Assertions.assertInstanceOf(DefaultChunkerContextGenerator.class, factory.getContextGenerator());
61+
Assertions.assertInstanceOf(DefaultChunkerSequenceValidator.class, factory.getSequenceValidator());
6262

6363
ByteArrayOutputStream out = new ByteArrayOutputStream();
6464
model.serialize(out);
@@ -67,8 +67,8 @@ void testDefaultFactory() throws IOException {
6767
ChunkerModel fromSerialized = new ChunkerModel(in);
6868

6969
factory = fromSerialized.getFactory();
70-
Assertions.assertTrue(factory.getContextGenerator() instanceof DefaultChunkerContextGenerator);
71-
Assertions.assertTrue(factory.getSequenceValidator() instanceof DefaultChunkerSequenceValidator);
70+
Assertions.assertInstanceOf(DefaultChunkerContextGenerator.class, factory.getContextGenerator());
71+
Assertions.assertInstanceOf(DefaultChunkerSequenceValidator.class, factory.getSequenceValidator());
7272
}
7373

7474

@@ -78,10 +78,10 @@ void testDummyFactory() throws IOException {
7878
ChunkerModel model = trainModel(ModelType.MAXENT, new DummyChunkerFactory());
7979

8080
DummyChunkerFactory factory = (DummyChunkerFactory) model.getFactory();
81-
Assertions.assertTrue(factory.getContextGenerator()
82-
instanceof DummyChunkerFactory.DummyContextGenerator);
83-
Assertions.assertTrue(factory.getSequenceValidator()
84-
instanceof DummyChunkerFactory.DummySequenceValidator);
81+
Assertions.assertInstanceOf(DummyChunkerFactory.DummyContextGenerator.class,
82+
factory.getContextGenerator());
83+
Assertions.assertInstanceOf(DummyChunkerFactory.DummySequenceValidator.class,
84+
factory.getSequenceValidator());
8585

8686

8787
ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -91,10 +91,8 @@ void testDummyFactory() throws IOException {
9191
ChunkerModel fromSerialized = new ChunkerModel(in);
9292

9393
factory = (DummyChunkerFactory) fromSerialized.getFactory();
94-
Assertions.assertTrue(factory.getContextGenerator()
95-
instanceof DefaultChunkerContextGenerator);
96-
Assertions.assertTrue(factory.getSequenceValidator()
97-
instanceof DefaultChunkerSequenceValidator);
94+
Assertions.assertInstanceOf(DefaultChunkerContextGenerator.class, factory.getContextGenerator());
95+
Assertions.assertInstanceOf(DefaultChunkerSequenceValidator.class, factory.getSequenceValidator());
9896

9997

10098
ChunkerME chunker = new ChunkerME(model);

opennlp-tools/src/test/java/opennlp/tools/chunker/DummyChunkSampleStream.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public ChunkSample read() throws IOException {
7474
count++;
7575
}
7676

77-
if (toks.size() > 0) {
77+
if (!toks.isEmpty()) {
7878
if (mIsPredicted) {
7979
return new ChunkSample(toks.toArray(new String[0]),
8080
posTags.toArray(new String[0]),

opennlp-tools/src/test/java/opennlp/tools/cmdline/TokenNameFinderToolTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ void run() throws IOException {
6969
TokenNameFinderTool tool = new TokenNameFinderTool();
7070
tool.run(args);
7171

72-
final String content = new String(baos.toByteArray(), StandardCharsets.UTF_8);
72+
final String content = baos.toString(StandardCharsets.UTF_8);
7373
Assertions.assertTrue(content.contains("It is <START:person> Stefanie Schmidt. <END>"));
7474

75-
model1.delete();
75+
Assertions.assertTrue(model1.delete());
7676
}
7777

7878
@Test
@@ -106,7 +106,7 @@ void usage() {
106106
TokenNameFinderTool tool = new TokenNameFinderTool();
107107
tool.run(args);
108108

109-
final String content = new String(baos.toByteArray(), StandardCharsets.UTF_8);
109+
final String content = baos.toString(StandardCharsets.UTF_8);
110110
Assertions.assertEquals(tool.getHelp(), content.trim());
111111

112112
}

opennlp-tools/src/test/java/opennlp/tools/cmdline/tokenizer/TokenizerTrainerToolTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void testTestRunHappyCase() throws IOException {
9696

9797
final String content = baos.toString(StandardCharsets.UTF_8);
9898
Assertions.assertTrue(content.contains("Number of Event Tokens: 171"));
99-
model.delete();
99+
Assertions.assertTrue(model.delete());
100100
}
101101

102102
//TODO OPENNLP-1447

opennlp-tools/src/test/java/opennlp/tools/eval/AbstractEvalTest.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.nio.file.Files;
2727
import java.nio.file.Path;
2828
import java.security.MessageDigest;
29-
import java.util.Collections;
3029
import java.util.List;
3130
import java.util.stream.Collectors;
3231

@@ -81,13 +80,11 @@ public static void verifyDirectoryChecksum(Path path, String extension, BigInteg
8180
MessageDigest digest = MessageDigest.getInstance(HASH_ALGORITHM);
8281

8382
final List<Path> paths = Files.walk(path)
84-
.filter(Files::isRegularFile)
85-
.filter(p -> p.toString().endsWith(extension))
86-
.collect(Collectors.toList());
83+
.filter(Files::isRegularFile)
84+
.filter(p -> p.toString().endsWith(extension)).sorted().collect(Collectors.toList());
8785

8886
// Ensure the paths are in a consistent order when
8987
// verifying the file checksums.
90-
Collections.sort(paths);
9188

9289
for (Path p : paths) {
9390
try (InputStream in = new BufferedInputStream(Files.newInputStream(p))) {

opennlp-tools/src/test/java/opennlp/tools/eval/SourceForgeModelEval.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.security.MessageDigest;
2525
import java.util.ArrayList;
2626
import java.util.Arrays;
27-
import java.util.Collections;
2827
import java.util.List;
2928
import java.util.Objects;
3029

@@ -92,7 +91,7 @@ private static class LeipzigTestSample {
9291

9392
private LeipzigTestSample(String[] text) {
9493
Objects.requireNonNull(text, "text must not be null");
95-
this.text = Collections.unmodifiableList(new ArrayList<>(Arrays.asList(text)));
94+
this.text = List.of(text);
9695
}
9796

9897
public String[] getText() {
@@ -110,7 +109,7 @@ public String toString() {
110109
sampleString.append(s).append(' ');
111110
}
112111

113-
if (sampleString.length() > 0) {
112+
if (!sampleString.isEmpty()) {
114113
// remove last space
115114
sampleString.setLength(sampleString.length() - 1);
116115
}
@@ -151,7 +150,7 @@ public LeipzigTestSample read() throws IOException {
151150
count++;
152151
}
153152

154-
if (tokensList.size() > 0) {
153+
if (!tokensList.isEmpty()) {
155154
return new LeipzigTestSample(tokensList.toArray(new String[0]));
156155
}
157156

opennlp-tools/src/test/java/opennlp/tools/formats/brat/BratDocumentTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void testDocumentWithEntitiesParsing() throws IOException {
5454
}
5555

5656
private void checkNote(BratAnnotation annotation, String expectedCoveredText, String expectedNote) {
57-
Assertions.assertTrue(annotation instanceof SpanAnnotation);
57+
Assertions.assertInstanceOf(SpanAnnotation.class, annotation);
5858
SpanAnnotation spanAnn = (SpanAnnotation) annotation;
5959
Assertions.assertEquals(expectedCoveredText, spanAnn.getCoveredText());
6060
Assertions.assertEquals(expectedNote, spanAnn.getNote());

opennlp-tools/src/test/java/opennlp/tools/formats/masc/MascPOSSampleStreamTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,10 @@ void train() {
109109
ObjectStream<POSSample> trainPOS = new MascPOSSampleStream(
110110
new MascDocumentStream(directory, true, fileFilter));
111111

112-
POSModel model = null;
113112
TrainingParameters trainingParameters = new TrainingParameters();
114113
trainingParameters.put(TrainingParameters.ITERATIONS_PARAM, 20);
115114

116-
model = POSTaggerME.train("en", trainPOS,
115+
POSModel model = POSTaggerME.train("en", trainPOS,
117116
trainingParameters, new POSTaggerFactory());
118117

119118
ObjectStream<POSSample> testPOS = new MascPOSSampleStream(

opennlp-tools/src/test/java/opennlp/tools/formats/masc/MascSentenceSampleStreamTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,10 @@ void train() {
126126
new MascDocumentStream(directory,
127127
true, fileFilter), 1);
128128

129-
SentenceModel model = null;
130129
TrainingParameters trainingParameters = new TrainingParameters();
131130
trainingParameters.put(TrainingParameters.ITERATIONS_PARAM, 20);
132131

133-
model = SentenceDetectorME.train("en", trainSentences,
132+
SentenceModel model = SentenceDetectorME.train("en", trainSentences,
134133
new SentenceDetectorFactory(), trainingParameters);
135134

136135
ObjectStream<SentenceSample> testPOS = new MascSentenceSampleStream(

opennlp-tools/src/test/java/opennlp/tools/formats/masc/MascTokenSampleStreamTest.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,11 @@ void train() {
129129
ObjectStream<TokenSample> trainTokens = new MascTokenSampleStream(
130130
new MascDocumentStream(directory, true, fileFilter));
131131

132-
TokenizerModel model = null;
133132
TrainingParameters trainingParameters = new TrainingParameters();
134133
trainingParameters.put(TrainingParameters.ITERATIONS_PARAM, 20);
135134

136-
model = TokenizerME.train(trainTokens, new TokenizerFactory("en", null, false, null),
137-
trainingParameters);
135+
TokenizerModel model = TokenizerME.train(trainTokens, new TokenizerFactory(
136+
"en", null, false, null), trainingParameters);
138137

139138
ObjectStream<TokenSample> testTokens = new MascTokenSampleStream(
140139
new MascDocumentStream(directory, true, fileFilter));

opennlp-tools/src/test/java/opennlp/tools/langdetect/DummyFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public String[] getContext(CharSequence document) {
8181

8282
for (StringList tokenList : tokenNgramModel) {
8383
if (tokenList.size() > 0) {
84-
context.add("tg=" + tokenList.toString());
84+
context.add("tg=" + tokenList);
8585
}
8686
}
8787
}

opennlp-tools/src/test/java/opennlp/tools/langdetect/LanguageDetectorFactoryTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ static void train() throws Exception {
6262
void testCorrectFactory() throws IOException {
6363
LanguageDetectorModel myModel = new LanguageDetectorModel(new ByteArrayInputStream(serialized));
6464
Assertions.assertNotNull(myModel.getFactory());
65-
Assertions.assertTrue(myModel.getFactory() instanceof LanguageDetectorFactory);
65+
Assertions.assertInstanceOf(LanguageDetectorFactory.class, myModel.getFactory());
6666
}
6767

6868
@Test
6969
void testDummyFactory() throws IOException {
7070
LanguageDetectorModel myModel = new LanguageDetectorModel(new ByteArrayInputStream(serialized));
7171
Assertions.assertNotNull(myModel.getFactory());
72-
Assertions.assertTrue(myModel.getFactory() instanceof DummyFactory);
72+
Assertions.assertInstanceOf(DummyFactory.class, myModel.getFactory());
7373
}
7474

7575
@Test

opennlp-tools/src/test/java/opennlp/tools/languagemodel/NgramLanguageModelTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.nio.charset.StandardCharsets;
2424
import java.util.Arrays;
2525
import java.util.List;
26-
import java.util.stream.Collectors;
2726

2827
import org.junit.jupiter.api.Assertions;
2928
import org.junit.jupiter.api.Test;
@@ -141,7 +140,7 @@ public void testTrigramLanguageModelCreationFromText() throws Exception {
141140

142141
try (InputStream is = getClass().getResourceAsStream("/opennlp/tools/languagemodel/sentences.txt");
143142
BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
144-
for (String line : reader.lines().collect(Collectors.toList())) {
143+
for (String line : reader.lines().toList()) {
145144
String[] array = line.split(" ");
146145
List<String> split = Arrays.asList(array);
147146
List<String> generatedStrings = NGramGenerator.generate(split, ngramSize, " ");

opennlp-tools/src/test/java/opennlp/tools/lemmatizer/DictionaryLemmatizerMultiTest.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,7 @@ void testForNullPointerException() {
4242
"down", "the", "street");
4343
List<String> sentencePOS = Arrays.asList("DT", "NNS", "VBD", "VBG", "CC", "VBG", "RP", "DT", "NN");
4444
List<List<String>> expectedLemmas = new ArrayList<>();
45-
expectedLemmas.add(Arrays.asList("the"));
46-
expectedLemmas.add(Arrays.asList("dog"));
47-
expectedLemmas.add(Arrays.asList("is"));
48-
expectedLemmas.add(Arrays.asList("run,run"));
49-
expectedLemmas.add(Arrays.asList("and"));
50-
expectedLemmas.add(Arrays.asList("bark,bark"));
51-
expectedLemmas.add(Arrays.asList("down"));
52-
expectedLemmas.add(Arrays.asList("the"));
53-
expectedLemmas.add(Arrays.asList("street"));
45+
expectedLemmas.add(List.of("the", "dog", "is", "run,run", "and", "bark, bark", "down", "the", "street"));
5446

5547
List<List<String>> actualLemmas = dictionaryLemmatizer.lemmatize(sentence, sentencePOS);
5648

opennlp-tools/src/test/java/opennlp/tools/lemmatizer/DummyLemmaSampleStream.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public LemmaSample read() throws IOException {
6868
count++;
6969
}
7070

71-
if (toks.size() > 0) {
71+
if (!toks.isEmpty()) {
7272
if (mIsPredicted) {
7373
return new LemmaSample(toks.toArray(new String[0]),
7474
posTags.toArray(new String[0]),

opennlp-tools/src/test/java/opennlp/tools/ml/naivebayes/NaiveBayesPrepAttachTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void testNaiveBayesOnPrepAttachData() throws IOException {
5959
testDataIndexer.index(trainingStream);
6060

6161
MaxentModel model = new NaiveBayesTrainer().trainModel(testDataIndexer);
62-
Assertions.assertTrue(model instanceof NaiveBayesModel);
62+
Assertions.assertInstanceOf(NaiveBayesModel.class, model);
6363
PrepAttachDataUtil.testModel(model, 0.7897994553107205);
6464
}
6565

@@ -71,7 +71,7 @@ void testNaiveBayesOnPrepAttachDataUsingTrainUtil() throws IOException {
7171

7272
EventTrainer trainer = TrainerFactory.getEventTrainer(trainParams, null);
7373
MaxentModel model = trainer.train(trainingStream);
74-
Assertions.assertTrue(model instanceof NaiveBayesModel);
74+
Assertions.assertInstanceOf(NaiveBayesModel.class, model);
7575
PrepAttachDataUtil.testModel(model, 0.7897994553107205);
7676
}
7777

@@ -83,7 +83,7 @@ void testNaiveBayesOnPrepAttachDataUsingTrainUtilWithCutoff5() throws IOExceptio
8383

8484
EventTrainer trainer = TrainerFactory.getEventTrainer(trainParams, null);
8585
MaxentModel model = trainer.train(trainingStream);
86-
Assertions.assertTrue(model instanceof NaiveBayesModel);
86+
Assertions.assertInstanceOf(NaiveBayesModel.class, model);
8787
PrepAttachDataUtil.testModel(model, 0.7945035899975241);
8888
}
8989
}

opennlp-tools/src/test/java/opennlp/tools/namefind/BilouCodecTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ void testEncodeAdjacentUnitSpans() {
104104

105105
@Test
106106
void testCreateSequenceValidator() {
107-
Assertions.assertTrue(codec.createSequenceValidator() instanceof BilouNameFinderSequenceValidator);
107+
Assertions.assertInstanceOf(BilouNameFinderSequenceValidator.class, codec.createSequenceValidator());
108108
}
109109

110110
@Test

opennlp-tools/src/test/java/opennlp/tools/namefind/BioCodecTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ void testEncodeAdjacentSpans() {
111111

112112
@Test
113113
void testCreateSequenceValidator() {
114-
Assertions.assertTrue(codec.createSequenceValidator() instanceof NameFinderSequenceValidator);
114+
Assertions.assertInstanceOf(NameFinderSequenceValidator.class, codec.createSequenceValidator());
115115
}
116116

117117

0 commit comments

Comments
 (0)