-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mark Khazin
committed
Dec 22, 2019
1 parent
29499f6
commit 57d7171
Showing
2 changed files
with
95 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/test/java/com/intsights/elasticsearch/index/analysis/TestHomoglyphTokenFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.intsights.elasticsearch.index.analysis; | ||
|
||
import org.apache.lucene.analysis.BaseTokenStreamTestCase; | ||
import org.apache.lucene.analysis.TokenStream; | ||
import org.apache.lucene.analysis.MockTokenizer; | ||
|
||
import java.io.IOException; | ||
import java.io.StringReader; | ||
|
||
class NoUtf16ParsingTokenizer extends MockTokenizer { | ||
public NoUtf16ParsingTokenizer(){ | ||
super(MockTokenizer.WHITESPACE, false); | ||
} | ||
|
||
@Override | ||
protected int readCodePoint() throws IOException { | ||
int ch = readChar(); | ||
return ch; | ||
} | ||
} | ||
|
||
public class TestHomoglyphTokenFilter extends BaseTokenStreamTestCase { | ||
public void testEmptyInput() throws Exception { | ||
TokenStream stream = whitespaceMockTokenizer(""); | ||
HomoglyphTokenFilter filter = new HomoglyphTokenFilter(stream); | ||
assertTokenStreamContents(filter, new String[0]); | ||
} | ||
|
||
public void testSingleHomoglyph() throws Exception { | ||
TokenStream stream = whitespaceMockTokenizer("tℯst"); | ||
HomoglyphTokenFilter filter = new HomoglyphTokenFilter(stream); | ||
assertTokenStreamContents(filter, new String[] {"test"}); | ||
} | ||
|
||
public void testNoHomoglyphs() throws Exception { | ||
TokenStream stream = whitespaceMockTokenizer("test"); | ||
HomoglyphTokenFilter filter = new HomoglyphTokenFilter(stream); | ||
assertTokenStreamContents(filter, new String[0]); | ||
} | ||
|
||
public void testMultipleMappingHomoglyphs() throws Exception { | ||
TokenStream stream = whitespaceMockTokenizer("hellО"); | ||
HomoglyphTokenFilter filter = new HomoglyphTokenFilter(stream); | ||
assertTokenStreamContents(filter, new String[] { | ||
"hello", "hell0", "hel1o", "hel10", "he1lo", "he1l0", "he11o", "he110" | ||
}); | ||
} | ||
|
||
public void test4byteUtf16Homoglyphs() throws Exception { | ||
TokenStream stream = whitespaceMockTokenizer("𝒕𝒆𝒔𝒕"); | ||
HomoglyphTokenFilter filter = new HomoglyphTokenFilter(stream); | ||
assertTokenStreamContents(filter, new String[] {"test"}); | ||
} | ||
|
||
public void testMultipleLetterHomoglyphs() throws Exception { | ||
TokenStream stream = whitespaceMockTokenizer("㏔-㎰"); | ||
HomoglyphTokenFilter filter = new HomoglyphTokenFilter(stream); | ||
assertTokenStreamContents(filter, new String[] {"mb-ps"}); | ||
} | ||
|
||
public void testLowercasing() throws Exception { | ||
TokenStream stream = whitespaceMockTokenizer("TEST"); | ||
HomoglyphTokenFilter filter = new HomoglyphTokenFilter(stream); | ||
assertTokenStreamContents(filter, new String[] {"test"}); | ||
} | ||
|
||
public void testMultipleTokenInput() throws Exception { | ||
TokenStream stream = whitespaceMockTokenizer("onℯ two 𝒕hree fОur FIVE sⅸ"); | ||
HomoglyphTokenFilter filter = new HomoglyphTokenFilter(stream); | ||
assertTokenStreamContents(filter, new String[] { | ||
"one", "three", "four", "f0ur", "five", "six" | ||
}); | ||
} | ||
|
||
public void testUnicodeEdgeCases() throws Exception { | ||
NoUtf16ParsingTokenizer stream = new NoUtf16ParsingTokenizer(); | ||
stream.setReader(new StringReader("aa\ud802aaℯ bb\udc02bbℯ ℯcc\ud803")); | ||
|
||
HomoglyphTokenFilter filter = new HomoglyphTokenFilter(stream); | ||
assertTokenStreamContents(filter, new String[] { | ||
"aa\ud802aae", "bb\udc02bbe", "ecc\ud803" | ||
}); | ||
} | ||
} |