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;
018:
019: import java.io.IOException;
020: import java.sql.Connection;
021: import java.sql.PreparedStatement;
022: import java.sql.ResultSet;
023:
024: import org.apache.lucene.store.IndexOutput;
025: import org.apache.lucene.store.jdbc.datasource.DataSourceUtils;
026: import org.apache.lucene.store.jdbc.handler.ActualDeleteFileEntryHandler;
027: import org.apache.lucene.store.jdbc.support.JdbcTable;
028: import org.apache.lucene.store.jdbc.support.JdbcTemplate;
029:
030: /**
031: * @author kimchy
032: */
033: public class GeneralOperationsJdbcDirectoryTests extends
034: AbstractJdbcDirectoryTests {
035:
036: private JdbcDirectory jdbcDirectory;
037:
038: protected void setUp() throws Exception {
039: super .setUp();
040: JdbcDirectorySettings settings = new JdbcDirectorySettings();
041: settings.getDefaultFileEntrySettings().setClassSetting(
042: JdbcFileEntrySettings.FILE_ENTRY_HANDLER_TYPE,
043: ActualDeleteFileEntryHandler.class);
044:
045: jdbcDirectory = new JdbcDirectory(dataSource, new JdbcTable(
046: settings, createDialect(), "TEST"));
047: }
048:
049: protected void tearDown() throws Exception {
050: jdbcDirectory.close();
051: super .tearDown();
052: }
053:
054: public void testCreateDelteExists() throws IOException {
055: Connection con = DataSourceUtils.getConnection(dataSource);
056: jdbcDirectory.create();
057: DataSourceUtils.commitConnectionIfPossible(con);
058: DataSourceUtils.releaseConnection(con);
059:
060: if (jdbcDirectory.getDialect().supportsTableExists()) {
061: con = DataSourceUtils.getConnection(dataSource);
062: assertTrue(jdbcDirectory.tableExists());
063: DataSourceUtils.commitConnectionIfPossible(con);
064: DataSourceUtils.releaseConnection(con);
065: }
066:
067: con = DataSourceUtils.getConnection(dataSource);
068: jdbcDirectory.delete();
069: DataSourceUtils.commitConnectionIfPossible(con);
070: DataSourceUtils.releaseConnection(con);
071:
072: if (jdbcDirectory.getDialect().supportsTableExists()) {
073: con = DataSourceUtils.getConnection(dataSource);
074: assertFalse(jdbcDirectory.tableExists());
075: DataSourceUtils.commitConnectionIfPossible(con);
076: DataSourceUtils.releaseConnection(con);
077: }
078: }
079:
080: public void testCreateDelteExistsWitinTransaction()
081: throws IOException {
082: Connection con = DataSourceUtils.getConnection(dataSource);
083: jdbcDirectory.create();
084:
085: if (jdbcDirectory.getDialect().supportsTableExists()) {
086: assertTrue(jdbcDirectory.tableExists());
087: }
088:
089: jdbcTemplate.executeSelect("select * from test",
090: new JdbcTemplate.ExecuteSelectCallback() {
091:
092: public void fillPrepareStatement(
093: PreparedStatement ps) throws Exception {
094: }
095:
096: public Object execute(ResultSet rs)
097: throws Exception {
098: assertFalse(rs.next());
099: return null;
100: }
101: });
102:
103: jdbcDirectory.delete();
104:
105: if (jdbcDirectory.getDialect().supportsTableExists()) {
106: assertFalse(jdbcDirectory.tableExists());
107: }
108:
109: try {
110: jdbcTemplate.executeSelect("select * from test",
111: new JdbcTemplate.ExecuteSelectCallback() {
112:
113: public void fillPrepareStatement(
114: PreparedStatement ps) throws Exception {
115: }
116:
117: public Object execute(ResultSet rs)
118: throws Exception {
119: assertFalse(rs.next());
120: return null;
121: }
122: });
123: fail();
124: } catch (Exception e) {
125:
126: }
127: DataSourceUtils.rollbackConnectionIfPossible(con);
128: DataSourceUtils.releaseConnection(con);
129: }
130:
131: public void testList() throws IOException {
132: Connection con = DataSourceUtils.getConnection(dataSource);
133: jdbcDirectory.create();
134: DataSourceUtils.commitConnectionIfPossible(con);
135: DataSourceUtils.releaseConnection(con);
136:
137: con = DataSourceUtils.getConnection(dataSource);
138: String[] list = jdbcDirectory.list();
139: DataSourceUtils.commitConnectionIfPossible(con);
140: DataSourceUtils.releaseConnection(con);
141: assertEquals(0, list.length);
142:
143: con = DataSourceUtils.getConnection(dataSource);
144: IndexOutput indexOutput = jdbcDirectory.createOutput("test1");
145: indexOutput.writeString("TEST STRING");
146: indexOutput.close();
147: DataSourceUtils.commitConnectionIfPossible(con);
148: DataSourceUtils.releaseConnection(con);
149:
150: jdbcTemplate.executeSelect("select * from test",
151: new JdbcTemplate.ExecuteSelectCallback() {
152:
153: public void fillPrepareStatement(
154: PreparedStatement ps) throws Exception {
155: }
156:
157: public Object execute(ResultSet rs)
158: throws Exception {
159: assertTrue(rs.next());
160: return null;
161: }
162: });
163:
164: con = DataSourceUtils.getConnection(dataSource);
165: list = jdbcDirectory.list();
166: DataSourceUtils.commitConnectionIfPossible(con);
167: DataSourceUtils.releaseConnection(con);
168: assertEquals(1, list.length);
169:
170: con = DataSourceUtils.getConnection(dataSource);
171: jdbcDirectory.deleteFile("test1");
172: DataSourceUtils.commitConnectionIfPossible(con);
173: DataSourceUtils.releaseConnection(con);
174:
175: con = DataSourceUtils.getConnection(dataSource);
176: list = jdbcDirectory.list();
177: DataSourceUtils.commitConnectionIfPossible(con);
178: DataSourceUtils.releaseConnection(con);
179: assertEquals(0, list.length);
180: }
181:
182: public void testListWithinTransaction() throws IOException {
183: Connection con = DataSourceUtils.getConnection(dataSource);
184:
185: jdbcDirectory.create();
186:
187: String[] list = jdbcDirectory.list();
188: assertEquals(0, list.length);
189:
190: IndexOutput indexOutput = jdbcDirectory.createOutput("test1");
191: indexOutput.writeString("TEST STRING");
192: indexOutput.close();
193:
194: jdbcTemplate.executeSelect("select * from test",
195: new JdbcTemplate.ExecuteSelectCallback() {
196:
197: public void fillPrepareStatement(
198: PreparedStatement ps) throws Exception {
199: }
200:
201: public Object execute(ResultSet rs)
202: throws Exception {
203: assertTrue(rs.next());
204: return null;
205: }
206: });
207:
208: list = jdbcDirectory.list();
209: assertEquals(1, list.length);
210:
211: jdbcDirectory.deleteFile("test1");
212: list = jdbcDirectory.list();
213: assertEquals(0, list.length);
214:
215: DataSourceUtils.rollbackConnectionIfPossible(con);
216: DataSourceUtils.releaseConnection(con);
217: }
218:
219: public void testDeleteContent() throws IOException {
220: Connection con = DataSourceUtils.getConnection(dataSource);
221: jdbcDirectory.create();
222: DataSourceUtils.commitConnectionIfPossible(con);
223: DataSourceUtils.releaseConnection(con);
224:
225: con = DataSourceUtils.getConnection(dataSource);
226: String[] list = jdbcDirectory.list();
227: DataSourceUtils.commitConnectionIfPossible(con);
228: DataSourceUtils.releaseConnection(con);
229: assertEquals(0, list.length);
230:
231: con = DataSourceUtils.getConnection(dataSource);
232: IndexOutput indexOutput = jdbcDirectory.createOutput("test1");
233: indexOutput.writeString("TEST STRING");
234: indexOutput.close();
235: DataSourceUtils.commitConnectionIfPossible(con);
236: DataSourceUtils.releaseConnection(con);
237:
238: con = DataSourceUtils.getConnection(dataSource);
239: list = jdbcDirectory.list();
240: DataSourceUtils.commitConnectionIfPossible(con);
241: DataSourceUtils.releaseConnection(con);
242: assertEquals(1, list.length);
243:
244: con = DataSourceUtils.getConnection(dataSource);
245: jdbcDirectory.deleteContent();
246: DataSourceUtils.commitConnectionIfPossible(con);
247: DataSourceUtils.releaseConnection(con);
248:
249: con = DataSourceUtils.getConnection(dataSource);
250: list = jdbcDirectory.list();
251: DataSourceUtils.commitConnectionIfPossible(con);
252: DataSourceUtils.releaseConnection(con);
253: assertEquals(0, list.length);
254: }
255: }
|