01: package org.apache.lucene.index;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * the License. You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
13: * Unless required by applicable law or agreed to in writing, software
14: * distributed under the License is distributed on an "AS IS" BASIS,
15: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: * See the License for the specific language governing permissions and
17: * limitations under the License.
18: */
19:
20: import org.apache.lucene.util.LuceneTestCase;
21: import org.apache.lucene.store.IndexInput;
22:
23: import java.io.IOException;
24:
25: public class TestIndexInput extends LuceneTestCase {
26: public void testRead() throws IOException {
27: IndexInput is = new MockIndexInput(new byte[] { (byte) 0x80,
28: 0x01, (byte) 0xFF, 0x7F, (byte) 0x80, (byte) 0x80,
29: 0x01, (byte) 0x81, (byte) 0x80, 0x01, 0x06, 'L', 'u',
30: 'c', 'e', 'n', 'e' });
31: assertEquals(128, is.readVInt());
32: assertEquals(16383, is.readVInt());
33: assertEquals(16384, is.readVInt());
34: assertEquals(16385, is.readVInt());
35: assertEquals("Lucene", is.readString());
36: }
37:
38: /**
39: * Expert
40: *
41: * @throws IOException
42: */
43: public void testSkipChars() throws IOException {
44: byte[] bytes = new byte[] { (byte) 0x80, 0x01, (byte) 0xFF,
45: 0x7F, (byte) 0x80, (byte) 0x80, 0x01, (byte) 0x81,
46: (byte) 0x80, 0x01, 0x06, 'L', 'u', 'c', 'e', 'n', 'e', };
47: String utf8Str = "\u0634\u1ea1";
48: byte[] utf8Bytes = utf8Str.getBytes("UTF-8");
49: byte[] theBytes = new byte[bytes.length + 1 + utf8Bytes.length];
50: System.arraycopy(bytes, 0, theBytes, 0, bytes.length);
51: theBytes[bytes.length] = (byte) utf8Str.length();//Add in the number of chars we are storing, which should fit in a byte for this test
52: System.arraycopy(utf8Bytes, 0, theBytes, bytes.length + 1,
53: utf8Bytes.length);
54: IndexInput is = new MockIndexInput(theBytes);
55: assertEquals(128, is.readVInt());
56: assertEquals(16383, is.readVInt());
57: assertEquals(16384, is.readVInt());
58: assertEquals(16385, is.readVInt());
59: int charsToRead = is.readVInt();//number of chars in the Lucene string
60: assertTrue(0x06 + " does not equal: " + charsToRead,
61: 0x06 == charsToRead);
62: is.skipChars(3);
63: char[] chars = new char[3];//there should be 6 chars remaining
64: is.readChars(chars, 0, 3);
65: String tmpStr = new String(chars);
66: assertTrue(tmpStr + " is not equal to " + "ene", tmpStr
67: .equals("ene") == true);
68: //Now read the UTF8 stuff
69: charsToRead = is.readVInt() - 1;//since we are skipping one
70: is.skipChars(1);
71: assertTrue(utf8Str.length() - 1 + " does not equal: "
72: + charsToRead, utf8Str.length() - 1 == charsToRead);
73: chars = new char[charsToRead];
74: is.readChars(chars, 0, charsToRead);
75: tmpStr = new String(chars);
76: assertTrue(tmpStr + " is not equal to " + utf8Str.substring(1),
77: tmpStr.equals(utf8Str.substring(1)) == true);
78: }
79: }
|