001: /*
002: * $Id: AbstractBlobTest.java,v 1.4 2004/09/09 19:47:26 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002 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.sql.Blob;
046: import java.sql.SQLException;
047:
048: import junit.framework.TestCase;
049:
050: import org.axiondb.jdbc.AxionBlob;
051:
052: /**
053: * @version $Revision: 1.4 $ $Date: 2004/09/09 19:47:26 $
054: * @author Rodney Waldhoff
055: */
056: public abstract class AbstractBlobTest extends TestCase {
057:
058: public AbstractBlobTest(String testName) {
059: super (testName);
060: }
061:
062: protected byte[] _bytes = "The quick brown fox jumped over the lazy dogs."
063: .getBytes();
064:
065: protected abstract AxionBlob getBlob() throws Exception;
066:
067: public void testGetBinaryStream() throws Exception {
068: Blob blob = getBlob();
069: InputStream in = blob.getBinaryStream();
070: for (int i = 0; i < _bytes.length; i++) {
071: assertEquals(_bytes[i], in.read());
072: }
073: assertEquals(-1, in.read());
074: assertEquals(-1, in.read());
075: in.close();
076: }
077:
078: public void testGetBytes() throws Exception {
079: Blob blob = getBlob();
080: byte[] found = blob.getBytes(0L, _bytes.length);
081: assertNotNull(found);
082: for (int i = 0; i < _bytes.length; i++) {
083: assertEquals(_bytes[i], found[i]);
084: }
085: }
086:
087: public void testGetBytes2() throws Exception {
088: Blob blob = getBlob();
089: byte[] found = blob.getBytes(3, _bytes.length - 3);
090: assertNotNull(found);
091: for (int i = 3; i < _bytes.length - 3; i++) {
092: assertEquals(_bytes[i], found[i - 3]);
093: }
094: }
095:
096: public void testGetBytes3() throws Exception {
097: Blob blob = getBlob();
098: try {
099: blob.getBytes(-1L, 2);
100: fail("Expected SQLException");
101: } catch (SQLException e) {
102: // expected
103: }
104: try {
105: blob.getBytes(0, -1);
106: fail("Expected SQLException");
107: } catch (SQLException e) {
108: // expected
109: }
110: }
111:
112: public void testGetBytes4() throws Exception {
113: Blob blob = getBlob();
114: byte[] found = blob.getBytes(0, 0);
115: assertNotNull(found);
116: assertEquals(0, found.length);
117: }
118:
119: public void testTruncate() throws Exception {
120: AxionBlob blob = getBlob();
121: blob.truncate(("The quick".length()));
122: InputStream in = blob.getBinaryStream();
123: StringBuffer buf = new StringBuffer();
124: for (int c = in.read(); c != -1; c = in.read()) {
125: buf.append((char) c);
126: }
127: assertEquals("The quick", buf.toString());
128: in.close();
129: }
130:
131: public void testSetBinaryStream() throws Exception {
132: AxionBlob blob = getBlob();
133: OutputStream out = blob.setBinaryStream(0L);
134:
135: out
136: .write("The quick yellow dogs crept under the lazy foxes. Every good boy does fine."
137: .getBytes("US-ASCII"));
138: out.close();
139:
140: InputStream in = blob.getBinaryStream();
141: StringBuffer buf = new StringBuffer();
142: for (int c = in.read(); c != -1; c = in.read()) {
143: buf.append((char) c);
144: }
145: assertEquals(
146: "The quick yellow dogs crept under the lazy foxes. Every good boy does fine.",
147: buf.toString());
148: in.close();
149: }
150:
151: public void testSetBinaryStream2() throws Exception {
152: AxionBlob blob = getBlob();
153: {
154: OutputStream out = blob.setBinaryStream(0L);
155:
156: out
157: .write("The quick yellow dogs crept under the lazy foxes. Every good boy does fine."
158: .getBytes("US-ASCII"));
159: out.close();
160:
161: InputStream in = blob.getBinaryStream();
162: StringBuffer buf = new StringBuffer();
163: for (int c = in.read(); c != -1; c = in.read()) {
164: buf.append((char) c);
165: }
166: assertEquals(
167: "The quick yellow dogs crept under the lazy foxes. Every good boy does fine.",
168: buf.toString());
169: in.close();
170: }
171: blob.truncate(0);
172: {
173: OutputStream out = blob.setBinaryStream(0L);
174:
175: out.write("Every good boy does fine.".getBytes("US-ASCII"));
176: out.close();
177:
178: InputStream in = blob.getBinaryStream();
179: StringBuffer buf = new StringBuffer();
180: for (int c = in.read(); c != -1; c = in.read()) {
181: buf.append((char) c);
182: }
183: assertEquals("Every good boy does fine.", buf.toString());
184: in.close();
185: }
186: }
187: }
|