001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.needle.coherence;
018:
019: import java.io.IOException;
020:
021: import com.tangosol.net.CacheFactory;
022: import com.tangosol.net.NamedCache;
023: import junit.framework.TestCase;
024: import org.apache.lucene.store.IndexInput;
025: import org.apache.lucene.store.IndexOutput;
026:
027: /**
028: * @author kimchy
029: */
030: public abstract class AbstractCoherenceDirectoryTests extends TestCase {
031:
032: private NamedCache cache;
033:
034: protected void setUp() throws Exception {
035: cache = CacheFactory.getCache("luceneDirectory");
036: cache.clear();
037: }
038:
039: protected NamedCache getCache() {
040: return this .cache;
041: }
042:
043: protected abstract CoherenceDirectory doCreateDirectory(
044: String name, int bucketSize);
045:
046: public void test1Buffer() throws Exception {
047: CoherenceDirectory dir = doCreateDirectory("test", 1);
048: insertData(dir);
049: verifyData(dir);
050: }
051:
052: public void test3Buffer() throws Exception {
053: CoherenceDirectory dir = doCreateDirectory("test", 3);
054: insertData(dir);
055: verifyData(dir);
056: }
057:
058: public void test10Buffer() throws Exception {
059: CoherenceDirectory dir = doCreateDirectory("test", 10);
060: insertData(dir);
061: verifyData(dir);
062: }
063:
064: public void test15Buffer() throws Exception {
065: CoherenceDirectory dir = doCreateDirectory("test", 15);
066: insertData(dir);
067: verifyData(dir);
068: }
069:
070: public void test40Buffer() throws Exception {
071: CoherenceDirectory dir = doCreateDirectory("test", 40);
072: insertData(dir);
073: verifyData(dir);
074: }
075:
076: private void insertData(CoherenceDirectory dir) throws IOException {
077: byte[] test = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
078: IndexOutput indexOutput = dir.createOutput("value1");
079: indexOutput.writeBytes(new byte[] { 2, 4, 6, 7, 8 }, 5);
080: indexOutput.writeInt(-1);
081: indexOutput.writeLong(10);
082: indexOutput.writeInt(0);
083: indexOutput.writeInt(0);
084: indexOutput.writeBytes(test, 8);
085: indexOutput.writeBytes(test, 5);
086:
087: indexOutput.seek(0);
088: indexOutput.writeByte((byte) 8);
089: if (dir.getBucketSize() > 4) {
090: indexOutput.seek(2);
091: indexOutput.writeBytes(new byte[] { 1, 2 }, 2);
092: }
093:
094: indexOutput.close();
095: }
096:
097: private void verifyData(CoherenceDirectory dir) throws IOException {
098: byte[] test = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
099: assertTrue(dir.fileExists("value1"));
100: assertEquals(38, dir.fileLength("value1"));
101:
102: IndexInput indexInput = dir.openInput("value1");
103: indexInput.readBytes(test, 0, 5);
104: assertEquals(8, test[0]);
105: assertEquals(-1, indexInput.readInt());
106: assertEquals(10, indexInput.readLong());
107: assertEquals(0, indexInput.readInt());
108: assertEquals(0, indexInput.readInt());
109: indexInput.readBytes(test, 0, 8);
110: assertEquals((byte) 1, test[0]);
111: assertEquals((byte) 8, test[7]);
112: indexInput.readBytes(test, 0, 5);
113: assertEquals((byte) 1, test[0]);
114: assertEquals((byte) 5, test[4]);
115:
116: indexInput.seek(28);
117: assertEquals((byte) 4, indexInput.readByte());
118: indexInput.seek(30);
119: assertEquals((byte) 6, indexInput.readByte());
120:
121: indexInput.close();
122: }
123: }
|