pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / util / TestByteBlockPool.java
1 package org.apache.lucene.util;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.apache.lucene.store.IndexInput;
8 import org.apache.lucene.store.IndexOutput;
9 import org.apache.lucene.store.RAMDirectory;
10
11 /**
12  * Licensed to the Apache Software Foundation (ASF) under one or more
13  * contributor license agreements. See the NOTICE file distributed with this
14  * work for additional information regarding copyright ownership. The ASF
15  * licenses this file to You under the Apache License, Version 2.0 (the
16  * "License"); you may not use this file except in compliance with the License.
17  * You may obtain a copy of the License at
18  * 
19  * http://www.apache.org/licenses/LICENSE-2.0
20  * 
21  * Unless required by applicable law or agreed to in writing, software
22  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
23  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
24  * License for the specific language governing permissions and limitations under
25  * the License.
26  */
27 public class TestByteBlockPool extends LuceneTestCase {
28
29   public void testCopyRefAndWrite() throws IOException {
30     List<String> list = new ArrayList<String>();
31     int maxLength = atLeast(500);
32     ByteBlockPool pool = new ByteBlockPool(new ByteBlockPool.DirectAllocator());
33     pool.nextBuffer();
34     final int numValues = atLeast(100);
35     BytesRef ref = new BytesRef();
36     for (int i = 0; i < numValues; i++) {
37       final String value = _TestUtil.randomRealisticUnicodeString(random,
38           maxLength);
39       list.add(value);
40       ref.copy(value);
41       pool.copy(ref);
42     }
43     RAMDirectory dir = new RAMDirectory();
44     IndexOutput stream = dir.createOutput("foo.txt");
45     pool.writePool(stream);
46     stream.flush();
47     stream.close();
48     IndexInput input = dir.openInput("foo.txt");
49     assertEquals(pool.byteOffset + pool.byteUpto, stream.length());
50     BytesRef expected = new BytesRef();
51     BytesRef actual = new BytesRef();
52     for (String string : list) {
53       expected.copy(string);
54       actual.grow(expected.length);
55       actual.length = expected.length;
56       input.readBytes(actual.bytes, 0, actual.length);
57       assertEquals(expected, actual);
58     }
59     try {
60       input.readByte();
61       fail("must be EOF");
62     } catch (IOException e) {
63       // expected - read past EOF
64     }
65     dir.close();
66   }
67 }