aboutsummaryrefslogtreecommitdiffstats
path: root/linguistics/src/test/java/com/yahoo/language/detect/AbstractDetectorTestCase.java
blob: b167722a5538e7ba52b5ab390a48e0c6cee14ff3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright Vespa.ai. 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 java.nio.charset.StandardCharsets;

import static org.junit.Assert.*;

/**
 * @author Simon Thoresen Hult
 */
public class AbstractDetectorTestCase {

    private static final Detection DETECTION = new Detection(Language.ARABIC, "encoding", true);
    private static final Charset UTF8 = StandardCharsets.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;
        }
    }
}