diff options
Diffstat (limited to 'linguistics/src/test/java/com/yahoo/language/detect/AbstractDetectorTestCase.java')
-rw-r--r-- | linguistics/src/test/java/com/yahoo/language/detect/AbstractDetectorTestCase.java | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/linguistics/src/test/java/com/yahoo/language/detect/AbstractDetectorTestCase.java b/linguistics/src/test/java/com/yahoo/language/detect/AbstractDetectorTestCase.java new file mode 100644 index 00000000000..aa8102fe9f2 --- /dev/null +++ b/linguistics/src/test/java/com/yahoo/language/detect/AbstractDetectorTestCase.java @@ -0,0 +1,61 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.language.detect; + +import com.yahoo.language.Language; +import org.junit.Test; + +import java.nio.ByteBuffer; +import java.nio.charset.Charset; + +import static org.junit.Assert.*; + +/** + * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen Hult</a> + */ +public class AbstractDetectorTestCase { + + private static final Detection DETECTION = new Detection(Language.ARABIC, "encoding", true); + private static final Charset UTF8 = Charset.forName("UTF-8"); + + @Test + public void requireThatDetectStringForwardsUtf8Bytes() { + Hint hint = Hint.newCountryHint("no"); + MyDetector detector = new MyDetector(); + Detection detection = detector.detect("69", hint); + assertSame(DETECTION, detection); + assertArrayEquals("69".getBytes(UTF8), detector.input); + assertEquals(0, detector.offset); + assertEquals(2, detector.length); + assertSame(hint, detector.hint); + } + + @Test + public void requireThatDetectByteBufferForwardsUtf8Bytes() { + byte[] buf = new byte[] { 6, 9 }; + Hint hint = Hint.newCountryHint("no"); + MyDetector detector = new MyDetector(); + Detection detection = detector.detect(ByteBuffer.wrap(buf), hint); + assertSame(DETECTION, detection); + assertArrayEquals(buf, detector.input); + assertEquals(0, detector.offset); + assertEquals(2, detector.length); + assertSame(hint, detector.hint); + } + + private static class MyDetector extends AbstractDetector { + + byte[] input; + int offset; + int length; + Hint hint; + + @Override + public Detection detect(byte[] input, int offset, int length, Hint hint) { + this.input = input; + this.offset = offset; + this.length = length; + this.hint = hint; + return DETECTION; + } + } +} |