Skip to content

Commit 286f7ac

Browse files
committed
Extract assertSymbolStream() method to CustomAssertions
1 parent 8f3d658 commit 286f7ac

File tree

4 files changed

+48
-94
lines changed

4 files changed

+48
-94
lines changed

test/org/opensolaris/opengrok/analysis/ada/AdaSymbolTokenizerTest.java

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,11 @@
2626
import java.io.BufferedReader;
2727
import java.io.InputStream;
2828
import java.io.InputStreamReader;
29-
import java.io.Reader;
3029
import java.util.ArrayList;
3130
import java.util.List;
32-
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
33-
import static org.junit.Assert.assertEquals;
3431
import static org.junit.Assert.assertNotNull;
35-
import static org.junit.Assert.assertTrue;
3632
import org.junit.Test;
37-
import org.opensolaris.opengrok.analysis.JFlexTokenizer;
33+
import static org.opensolaris.opengrok.util.CustomAssertions.assertSymbolStream;
3834

3935
/**
4036
* Tests the {@link AdaSymbolTokenizer} class.
@@ -65,32 +61,6 @@ public void testPerlSymbolStream() throws Exception {
6561
}
6662
}
6763

68-
testSymbolStream(AdaSymbolTokenizer.class, adbres, expectedSymbols);
69-
}
70-
71-
/**
72-
* Runs the test on one single implementation class with the specified
73-
* input text and expected tokens.
74-
*/
75-
private void testSymbolStream(Class<? extends JFlexTokenizer> klass,
76-
InputStream iss, List<String> expectedTokens)
77-
throws Exception {
78-
JFlexTokenizer tokenizer = klass.getConstructor(Reader.class).
79-
newInstance(new InputStreamReader(iss, "UTF-8"));
80-
81-
CharTermAttribute term = tokenizer.addAttribute(
82-
CharTermAttribute.class);
83-
84-
int count = 0;
85-
while (tokenizer.incrementToken()) {
86-
assertTrue("too many tokens at term" + (1 + count) + ": " +
87-
term.toString(), count < expectedTokens.size());
88-
String expected = expectedTokens.get(count);
89-
// 1-based offset to accord with line #
90-
assertEquals("term" + (1 + count), expected, term.toString());
91-
count++;
92-
}
93-
94-
assertEquals("wrong number of tokens", expectedTokens.size(), count);
64+
assertSymbolStream(AdaSymbolTokenizer.class, adbres, expectedSymbols);
9565
}
9666
}

test/org/opensolaris/opengrok/analysis/perl/PerlSymbolTokenizerTest.java

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@
3232
import java.util.List;
3333
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
3434
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
35+
import static org.junit.Assert.assertEquals;
36+
import static org.junit.Assert.assertTrue;
3537
import org.junit.Test;
3638
import org.opensolaris.opengrok.analysis.JFlexTokenizer;
37-
import static org.junit.Assert.*;
39+
import static org.opensolaris.opengrok.util.CustomAssertions.assertSymbolStream;
3840

3941
/**
40-
* Unit tests for PerlSymbolTokenizerTest.
42+
* Unit tests for {@link PerlSymbolTokenizer}.
4143
*/
4244
public class PerlSymbolTokenizerTest {
4345

@@ -110,31 +112,6 @@ public void testPerlSymbolStream() throws Exception {
110112
}
111113
}
112114

113-
testSymbolStream(PerlSymbolTokenizer.class, plres, expectedSymbols);
114-
}
115-
116-
/**
117-
* Runs the test on one single implementation class with the specified
118-
* input text and expected tokens.
119-
*/
120-
private void testSymbolStream(Class<? extends JFlexTokenizer> klass,
121-
InputStream iss, List<String> expectedTokens)
122-
throws Exception {
123-
JFlexTokenizer tokenizer = klass.getConstructor(Reader.class)
124-
.newInstance(new InputStreamReader(iss, "UTF-8"));
125-
126-
CharTermAttribute term = tokenizer.addAttribute(CharTermAttribute.class);
127-
128-
int count = 0;
129-
while (tokenizer.incrementToken()) {
130-
assertTrue("too many tokens at term" + (1 + count) + ": " +
131-
term.toString(), count < expectedTokens.size());
132-
String expected = expectedTokens.get(count);
133-
// 1-based offset to accord with line #
134-
assertEquals("term" + (1 + count), expected, term.toString());
135-
count++;
136-
}
137-
138-
assertEquals("wrong number of tokens", expectedTokens.size(), count);
115+
assertSymbolStream(PerlSymbolTokenizer.class, plres, expectedSymbols);
139116
}
140117
}

test/org/opensolaris/opengrok/analysis/ruby/RubySymbolTokenizerTest.java

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,11 @@
2727
import java.io.BufferedReader;
2828
import java.io.InputStream;
2929
import java.io.InputStreamReader;
30-
import java.io.Reader;
3130
import java.util.ArrayList;
3231
import java.util.List;
33-
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
34-
import static org.junit.Assert.assertEquals;
3532
import static org.junit.Assert.assertNotNull;
36-
import static org.junit.Assert.assertTrue;
3733
import org.junit.Test;
38-
import org.opensolaris.opengrok.analysis.JFlexTokenizer;
34+
import static org.opensolaris.opengrok.util.CustomAssertions.assertSymbolStream;
3935

4036
/**
4137
* Tests the {@link RubySymbolTokenizer} class.
@@ -66,33 +62,6 @@ public void testRubySymbolStream() throws Exception {
6662
}
6763
}
6864

69-
testSymbolStream(RubySymbolTokenizer.class, rbres, expectedSymbols);
70-
}
71-
72-
/**
73-
* Runs the test on one single implementation class with the specified
74-
* input text and expected tokens.
75-
*/
76-
private void testSymbolStream(Class<? extends JFlexTokenizer> klass,
77-
InputStream iss, List<String> expectedTokens)
78-
throws Exception {
79-
80-
JFlexTokenizer tokenizer = klass.getConstructor(Reader.class).
81-
newInstance(new InputStreamReader(iss, "UTF-8"));
82-
83-
CharTermAttribute term = tokenizer.addAttribute(
84-
CharTermAttribute.class);
85-
86-
int count = 0;
87-
while (tokenizer.incrementToken()) {
88-
assertTrue("too many tokens at term" + (1 + count) + ": " +
89-
term.toString(), count < expectedTokens.size());
90-
String expected = expectedTokens.get(count);
91-
// 1-based offset to accord with line #
92-
assertEquals("term" + (1 + count), expected, term.toString());
93-
count++;
94-
}
95-
96-
assertEquals("wrong number of tokens", expectedTokens.size(), count);
65+
assertSymbolStream(RubySymbolTokenizer.class, rbres, expectedSymbols);
9766
}
9867
}

test/org/opensolaris/opengrok/util/CustomAssertions.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,19 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2017, [email protected].
21+
* Copyright (c) 2017, Chris Fraire <[email protected]>.
2222
*/
2323

2424
package org.opensolaris.opengrok.util;
2525

26+
import java.io.InputStream;
27+
import java.io.InputStreamReader;
28+
import java.io.Reader;
29+
import java.util.List;
30+
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
2631
import static org.junit.Assert.assertEquals;
32+
import static org.junit.Assert.assertTrue;
33+
import org.opensolaris.opengrok.analysis.JFlexTokenizer;
2734

2835
/**
2936
* Represents a container for custom test assertion methods
@@ -59,4 +66,35 @@ public static void assertLinesEqual(String messagePrefix,
5966
assertEquals(messagePrefix + ":number of lines", expecteds.length,
6067
actuals.length);
6168
}
69+
70+
/**
71+
* Asserts the specified tokenizer class produces an expected stream of
72+
* symbols from the specified input.
73+
* @param klass the test class
74+
* @param iss the input stream
75+
* @param expectedTokens the expected, ordered token list
76+
* @throws java.lang.Exception if an error occurs constructing a
77+
* {@code klass} instance or testing the stream
78+
*/
79+
public static void assertSymbolStream(Class<? extends JFlexTokenizer> klass,
80+
InputStream iss, List<String> expectedTokens) throws Exception {
81+
82+
JFlexTokenizer tokenizer = klass.getConstructor(Reader.class).
83+
newInstance(new InputStreamReader(iss, "UTF-8"));
84+
85+
CharTermAttribute term = tokenizer.addAttribute(
86+
CharTermAttribute.class);
87+
88+
int count = 0;
89+
while (tokenizer.incrementToken()) {
90+
assertTrue("too many tokens at term" + (1 + count) + ": " +
91+
term.toString(), count < expectedTokens.size());
92+
String expected = expectedTokens.get(count);
93+
// 1-based offset to accord with line #
94+
assertEquals("term" + (1 + count), expected, term.toString());
95+
count++;
96+
}
97+
98+
assertEquals("wrong number of tokens", expectedTokens.size(), count);
99+
}
62100
}

0 commit comments

Comments
 (0)