001: /*
002: * $Id: AbstractClobTest.java,v 1.5 2004/09/09 19:47:26 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2003 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.types;
042:
043: import java.io.InputStream;
044: import java.io.OutputStream;
045: import java.io.Reader;
046: import java.io.Writer;
047: import java.sql.Clob;
048: import java.sql.SQLException;
049:
050: import junit.framework.TestCase;
051:
052: import org.axiondb.jdbc.AxionClob;
053:
054: /**
055: * @version $Revision: 1.5 $ $Date: 2004/09/09 19:47:26 $
056: * @author Rodney Waldhoff
057: */
058: public abstract class AbstractClobTest extends TestCase {
059:
060: public AbstractClobTest(String testName) {
061: super (testName);
062: }
063:
064: protected String _text = "The quick brown fox jumped over the lazy dogs.";
065:
066: protected abstract AxionClob getClob() throws Exception;
067:
068: public void testGetAsciiStream() throws Exception {
069: Clob clob = getClob();
070: InputStream in = clob.getAsciiStream();
071: StringBuffer buf = new StringBuffer();
072: for (int c = in.read(); c != -1; c = in.read()) {
073: buf.append((char) c);
074: }
075: assertEquals(_text, buf.toString());
076: in.close();
077: }
078:
079: public void testGetCharacterStream() throws Exception {
080: Clob clob = getClob();
081: Reader in = clob.getCharacterStream();
082: StringBuffer buf = new StringBuffer();
083: for (int c = in.read(); c != -1; c = in.read()) {
084: buf.append((char) c);
085: }
086: assertEquals(_text, buf.toString());
087: in.close();
088: }
089:
090: public void testTruncate() throws Exception {
091: AxionClob clob = getClob();
092: clob.truncate(("The quick".length()));
093: Reader in = clob.getCharacterStream();
094: StringBuffer buf = new StringBuffer();
095: for (int c = in.read(); c != -1; c = in.read()) {
096: buf.append((char) c);
097: }
098: assertEquals("The quick", buf.toString());
099: in.close();
100: }
101:
102: public void testGetSubstring() throws Exception {
103: AxionClob clob = getClob();
104: String substring = null;
105: try {
106: substring = clob.getSubString(("The quick".length()),
107: " brown fox jumped".length());
108: } catch (SQLException e) {
109: return; // exit on unsupported
110: }
111: assertEquals(" brown fox jumped", substring);
112: }
113:
114: public void testPositionOfString() throws Exception {
115: AxionClob clob = getClob();
116: String substring = " brown fox jumped";
117: long pos = -1;
118: try {
119: pos = clob.position(substring, 3L);
120: } catch (SQLException e) {
121: return; // exit on unsupported
122: }
123: assertEquals(_text.indexOf(substring, 3), (int) pos);
124: }
125:
126: public void testPositionOfClob() throws Exception {
127: AxionClob clob = getClob();
128: String substring = " brown fox jumped";
129: long pos = -1;
130: try {
131: pos = clob.position(new StringClob(substring), 3L);
132: } catch (SQLException e) {
133: return; // exit on unsupported
134: }
135: assertEquals(_text.indexOf(substring, 3), (int) pos);
136: }
137:
138: public void testPositionOfNullString() throws Exception {
139: AxionClob clob = getClob();
140: try {
141: clob.position((String) null, 0L);
142: fail("Expected SQLException");
143: } catch (SQLException e) {
144: // expected
145: }
146: }
147:
148: public void testPositionOfNullClob() throws Exception {
149: AxionClob clob = getClob();
150: try {
151: clob.position((Clob) null, 0L);
152: fail("Expected SQLException");
153: } catch (SQLException e) {
154: // expected
155: }
156: }
157:
158: public void testPositionOfStringNotFound() throws Exception {
159: AxionClob clob = getClob();
160: String substring = " bROWn fox jumped";
161: long pos = -1;
162: try {
163: pos = clob.position(substring, 3L);
164: } catch (SQLException e) {
165: return; // exit on unsupported
166: }
167: assertEquals((int) pos, _text.indexOf(substring, 3));
168: }
169:
170: public void testSetAsciiStream() throws Exception {
171: AxionClob clob = getClob();
172: OutputStream out = null;
173: try {
174: out = clob.setAsciiStream(0L);
175: } catch (SQLException e) {
176: return; // exit on unsupported
177: }
178: out
179: .write("The quick yellow dogs crept under the lazy foxes. Every good boy does fine."
180: .getBytes("US-ASCII"));
181: out.close();
182:
183: InputStream in = clob.getAsciiStream();
184: StringBuffer buf = new StringBuffer();
185: for (int c = in.read(); c != -1; c = in.read()) {
186: buf.append((char) c);
187: }
188: assertEquals(
189: "The quick yellow dogs crept under the lazy foxes. Every good boy does fine.",
190: buf.toString());
191: in.close();
192: }
193:
194: public void testSetAsciiStream2() throws Exception {
195: AxionClob clob = getClob();
196: {
197: OutputStream out = null;
198: try {
199: out = clob.setAsciiStream(0L);
200: } catch (SQLException e) {
201: return; // exit on unsupported
202: }
203: out
204: .write("The quick yellow dogs crept under the lazy foxes. Every good boy does fine."
205: .getBytes("US-ASCII"));
206: out.close();
207:
208: InputStream in = clob.getAsciiStream();
209: StringBuffer buf = new StringBuffer();
210: for (int c = in.read(); c != -1; c = in.read()) {
211: buf.append((char) c);
212: }
213: assertEquals(
214: "The quick yellow dogs crept under the lazy foxes. Every good boy does fine.",
215: buf.toString());
216: in.close();
217: }
218: clob.truncate(0);
219: {
220: OutputStream out = null;
221: try {
222: out = clob.setAsciiStream(0L);
223: } catch (SQLException e) {
224: return; // exit on unsupported
225: }
226: out.write("Every good boy does fine.".getBytes("US-ASCII"));
227: out.close();
228:
229: InputStream in = clob.getAsciiStream();
230: StringBuffer buf = new StringBuffer();
231: for (int c = in.read(); c != -1; c = in.read()) {
232: buf.append((char) c);
233: }
234: assertEquals("Every good boy does fine.", buf.toString());
235: in.close();
236: }
237: }
238:
239: public void testSetCharacterStream() throws Exception {
240: AxionClob clob = getClob();
241: Writer out = null;
242: try {
243: out = clob.setCharacterStream(0L);
244: } catch (SQLException e) {
245: return; // exit on unsupported
246: }
247: out
248: .write("The quick yellow dogs crept under the lazy foxes. Every good boy does fine.");
249: out.close();
250:
251: InputStream in = clob.getAsciiStream();
252: StringBuffer buf = new StringBuffer();
253: for (int c = in.read(); c != -1; c = in.read()) {
254: buf.append((char) c);
255: }
256: assertEquals(
257: "The quick yellow dogs crept under the lazy foxes. Every good boy does fine.",
258: buf.toString());
259: in.close();
260: }
261:
262: public void testSetCharacterStream2() throws Exception {
263: AxionClob clob = getClob();
264: {
265: Writer out = null;
266: try {
267: out = clob.setCharacterStream(0L);
268: } catch (SQLException e) {
269: return; // exit on unsupported
270: }
271: out
272: .write("The quick yellow dogs crept under the lazy foxes. Every good boy does fine.");
273: out.close();
274:
275: InputStream in = clob.getAsciiStream();
276: StringBuffer buf = new StringBuffer();
277: for (int c = in.read(); c != -1; c = in.read()) {
278: buf.append((char) c);
279: }
280: assertEquals(
281: "The quick yellow dogs crept under the lazy foxes. Every good boy does fine.",
282: buf.toString());
283: in.close();
284: }
285: clob.truncate(0);
286: {
287: Writer out = null;
288: try {
289: out = clob.setCharacterStream(0L);
290: } catch (SQLException e) {
291: return; // exit on unsupported
292: }
293: out.write("Every good boy does fine.");
294: out.close();
295:
296: InputStream in = clob.getAsciiStream();
297: StringBuffer buf = new StringBuffer();
298: for (int c = in.read(); c != -1; c = in.read()) {
299: buf.append((char) c);
300: }
301: assertEquals("Every good boy does fine.", buf.toString());
302: in.close();
303: }
304: }
305: }
|