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
|
// Copyright 2017 Yahoo Holdings. 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;
}
}
}
|