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.gigaspaces.store;
018:
019: import java.io.IOException;
020:
021: import com.j_spaces.core.IJSpace;
022: import com.j_spaces.core.client.SpaceFinder;
023: import junit.framework.TestCase;
024: import org.apache.lucene.store.IndexInput;
025: import org.apache.lucene.store.IndexOutput;
026: import org.apache.lucene.store.Lock;
027: import org.apache.lucene.store.LockObtainFailedException;
028:
029: /**
030: * @author kimchy
031: */
032: public class GigaSpaceDirectoryTests extends TestCase {
033:
034: private IJSpace space;
035:
036: protected void setUp() throws Exception {
037: space = (IJSpace) SpaceFinder.find("/./lucene");
038: space.clear(null, null);
039: }
040:
041: public void test1Buffer() throws Exception {
042: GigaSpaceDirectory dir = new GigaSpaceDirectory(space, "test",
043: 1);
044: insertData(dir);
045: verifyData(dir);
046: }
047:
048: public void test3Buffer() throws Exception {
049: GigaSpaceDirectory dir = new GigaSpaceDirectory(space, "test",
050: 3);
051: insertData(dir);
052: verifyData(dir);
053: }
054:
055: public void test10Buffer() throws Exception {
056: GigaSpaceDirectory dir = new GigaSpaceDirectory(space, "test",
057: 10);
058: insertData(dir);
059: verifyData(dir);
060: }
061:
062: public void test15Buffer() throws Exception {
063: GigaSpaceDirectory dir = new GigaSpaceDirectory(space, "test",
064: 15);
065: insertData(dir);
066: verifyData(dir);
067: }
068:
069: public void test40Buffer() throws Exception {
070: GigaSpaceDirectory dir = new GigaSpaceDirectory(space, "test",
071: 40);
072: insertData(dir);
073: verifyData(dir);
074: }
075:
076: public void testSimpeLocking() throws Exception {
077: GigaSpaceDirectory dir = new GigaSpaceDirectory(space, "test",
078: 40);
079: Lock lock = dir.makeLock("testlock");
080: assertFalse(lock.isLocked());
081: assertTrue(lock.obtain(2000));
082: assertTrue(lock.isLocked());
083: try {
084: assertFalse(lock.obtain(2000));
085: fail();
086: } catch (LockObtainFailedException e) {
087: // all is well
088: }
089: lock.release();
090: assertFalse(lock.isLocked());
091: }
092:
093: private void insertData(GigaSpaceDirectory dir) throws IOException {
094: byte[] test = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
095: IndexOutput indexOutput = dir.createOutput("value1");
096: indexOutput.writeBytes(new byte[] { 2, 4, 6, 7, 8 }, 5);
097: indexOutput.writeInt(-1);
098: indexOutput.writeLong(10);
099: indexOutput.writeInt(0);
100: indexOutput.writeInt(0);
101: indexOutput.writeBytes(test, 8);
102: indexOutput.writeBytes(test, 5);
103:
104: indexOutput.seek(0);
105: indexOutput.writeByte((byte) 8);
106: if (dir.getBucketSize() > 4) {
107: indexOutput.seek(2);
108: indexOutput.writeBytes(new byte[] { 1, 2 }, 2);
109: }
110:
111: indexOutput.close();
112: }
113:
114: private void verifyData(GigaSpaceDirectory dir) throws IOException {
115: byte[] test = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
116: assertTrue(dir.fileExists("value1"));
117: assertEquals(38, dir.fileLength("value1"));
118:
119: IndexInput indexInput = dir.openInput("value1");
120: indexInput.readBytes(test, 0, 5);
121: assertEquals(8, test[0]);
122: assertEquals(-1, indexInput.readInt());
123: assertEquals(10, indexInput.readLong());
124: assertEquals(0, indexInput.readInt());
125: assertEquals(0, indexInput.readInt());
126: indexInput.readBytes(test, 0, 8);
127: assertEquals((byte) 1, test[0]);
128: assertEquals((byte) 8, test[7]);
129: indexInput.readBytes(test, 0, 5);
130: assertEquals((byte) 1, test[0]);
131: assertEquals((byte) 5, test[4]);
132:
133: indexInput.seek(28);
134: assertEquals((byte) 4, indexInput.readByte());
135: indexInput.seek(30);
136: assertEquals((byte) 6, indexInput.readByte());
137:
138: indexInput.close();
139: }
140: }
|