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.apache.lucene.store.jdbc.index;
018:
019: import java.io.IOException;
020: import java.sql.Connection;
021:
022: import org.apache.lucene.store.IndexInput;
023: import org.apache.lucene.store.IndexOutput;
024: import org.apache.lucene.store.jdbc.AbstractJdbcDirectoryTests;
025: import org.apache.lucene.store.jdbc.JdbcDirectory;
026: import org.apache.lucene.store.jdbc.JdbcDirectorySettings;
027: import org.apache.lucene.store.jdbc.JdbcFileEntrySettings;
028: import org.apache.lucene.store.jdbc.datasource.DataSourceUtils;
029: import org.apache.lucene.store.jdbc.support.JdbcTable;
030:
031: /**
032: * @author kimchy
033: */
034: public abstract class AbstractIndexInputOutputTests extends
035: AbstractJdbcDirectoryTests {
036:
037: protected JdbcDirectory jdbcDirectory;
038:
039: protected boolean disable;
040:
041: protected void setUp() throws Exception {
042: super .setUp();
043: JdbcDirectorySettings settings = new JdbcDirectorySettings();
044: settings.getDefaultFileEntrySettings().setClassSetting(
045: JdbcFileEntrySettings.INDEX_INPUT_TYPE_SETTING,
046: indexInputClass());
047: settings.getDefaultFileEntrySettings().setClassSetting(
048: JdbcFileEntrySettings.INDEX_OUTPUT_TYPE_SETTING,
049: indexOutputClass());
050:
051: jdbcDirectory = new JdbcDirectory(dataSource, new JdbcTable(
052: settings, createDialect(), "TEST"));
053: }
054:
055: protected void tearDown() throws Exception {
056: jdbcDirectory.close();
057: super .tearDown();
058: }
059:
060: protected abstract Class indexInputClass();
061:
062: protected abstract Class indexOutputClass();
063:
064: public void testSize5() throws IOException {
065: innerTestSize(5);
066: }
067:
068: public void testSize5WithinTransaction() throws IOException {
069: innertTestSizeWithinTransaction(5);
070: }
071:
072: public void testSize15() throws IOException {
073: innerTestSize(15);
074: }
075:
076: public void testSize15WithinTransaction() throws IOException {
077: innertTestSizeWithinTransaction(15);
078: }
079:
080: public void testSize2() throws IOException {
081: innerTestSize(2);
082: }
083:
084: public void testSize2WithinTransaction() throws IOException {
085: innertTestSizeWithinTransaction(2);
086: }
087:
088: public void testSize1() throws IOException {
089: innerTestSize(1);
090: }
091:
092: public void testSize1WithinTransaction() throws IOException {
093: innertTestSizeWithinTransaction(1);
094: }
095:
096: public void testSize50() throws IOException {
097: innerTestSize(50);
098: }
099:
100: public void testSize50WithinTransaction() throws IOException {
101: innertTestSizeWithinTransaction(50);
102: }
103:
104: protected void innerTestSize(int bufferSize) throws IOException {
105: if (disable) {
106: return;
107: }
108: Connection con = DataSourceUtils.getConnection(dataSource);
109: jdbcDirectory.create();
110: DataSourceUtils.commitConnectionIfPossible(con);
111: DataSourceUtils.releaseConnection(con);
112:
113: jdbcDirectory.getSettings().getDefaultFileEntrySettings()
114: .setIntSetting(
115: JdbcBufferedIndexInput.BUFFER_SIZE_SETTING,
116: bufferSize);
117: jdbcDirectory.getSettings().getDefaultFileEntrySettings()
118: .setIntSetting(
119: JdbcBufferedIndexOutput.BUFFER_SIZE_SETTING,
120: bufferSize);
121:
122: con = DataSourceUtils.getConnection(dataSource);
123: insertData();
124: DataSourceUtils.commitConnectionIfPossible(con);
125: DataSourceUtils.releaseConnection(con);
126:
127: con = DataSourceUtils.getConnection(dataSource);
128: verifyData();
129: DataSourceUtils.commitConnectionIfPossible(con);
130: DataSourceUtils.releaseConnection(con);
131: }
132:
133: protected void innertTestSizeWithinTransaction(int bufferSize)
134: throws IOException {
135: if (disable) {
136: return;
137: }
138: Connection con = DataSourceUtils.getConnection(dataSource);
139: jdbcDirectory.create();
140: jdbcDirectory.getSettings().getDefaultFileEntrySettings()
141: .setIntSetting(
142: JdbcBufferedIndexInput.BUFFER_SIZE_SETTING,
143: bufferSize);
144: jdbcDirectory.getSettings().getDefaultFileEntrySettings()
145: .setIntSetting(
146: JdbcBufferedIndexOutput.BUFFER_SIZE_SETTING,
147: bufferSize);
148:
149: insertData();
150: verifyData();
151:
152: DataSourceUtils.rollbackConnectionIfPossible(con);
153: DataSourceUtils.releaseConnection(con);
154: }
155:
156: private void insertData() throws IOException {
157: byte[] test = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
158: IndexOutput indexOutput = jdbcDirectory.createOutput("value1");
159: indexOutput.writeInt(-1);
160: indexOutput.writeLong(10);
161: indexOutput.writeInt(0);
162: indexOutput.writeInt(0);
163: indexOutput.writeBytes(test, 8);
164: indexOutput.writeBytes(test, 5);
165:
166: indexOutput.seek(28);
167: indexOutput.writeByte((byte) 8);
168: indexOutput.seek(30);
169: indexOutput.writeBytes(new byte[] { 1, 2 }, 2);
170:
171: indexOutput.close();
172: }
173:
174: private void verifyData() throws IOException {
175: byte[] test = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
176: assertTrue(jdbcDirectory.fileExists("value1"));
177: assertEquals(33, jdbcDirectory.fileLength("value1"));
178:
179: IndexInput indexInput = jdbcDirectory.openInput("value1");
180: assertEquals(-1, indexInput.readInt());
181: assertEquals(10, indexInput.readLong());
182: assertEquals(0, indexInput.readInt());
183: assertEquals(0, indexInput.readInt());
184: indexInput.readBytes(test, 0, 8);
185: assertEquals((byte) 1, test[0]);
186: assertEquals((byte) 8, test[7]);
187: indexInput.readBytes(test, 0, 5);
188: assertEquals((byte) 8, test[0]);
189: assertEquals((byte) 5, test[4]);
190:
191: indexInput.seek(28);
192: assertEquals((byte) 8, indexInput.readByte());
193: indexInput.seek(30);
194: assertEquals((byte) 1, indexInput.readByte());
195:
196: indexInput.close();
197: }
198: }
|