001: /*
002: * $Id: TestBinaryStream.java,v 1.3 2005/05/03 17:55:16 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2004 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: package org.axiondb.functional;
041:
042: import junit.framework.Test;
043: import junit.framework.TestCase;
044: import junit.framework.TestSuite;
045:
046: import java.io.ByteArrayInputStream;
047: import java.io.StringWriter;
048: import java.io.PrintWriter;
049: import java.sql.*;
050:
051: /**
052: * Test the usage of varbinary datatypes.
053: *
054: * @author Steven Harris
055: */
056: public class TestBinaryStream extends TestCase {
057:
058: // Change this to suite your specific needs
059: public static final String DB_URI = "jdbc:axiondb:memdb";
060:
061: // SQL text
062: public static final String CREATE_TABLE_SQL = "CREATE TABLE MyTable "
063: + "( pKey varchar(100) NOT NULL, uValue varchar(100) NOT NULL, vBin varbinary(100) NOT NULL, Primary key (pKey), Unique (uValue) )";
064:
065: public static final String INSERT_VALUES_SQL = "INSERT INTO MyTable (pKey, uValue, vBin) VALUES (?, ?, ?)";
066:
067: public static final String READ_BINVAL_SQL = "SELECT vBin FROM MyTable";
068:
069: public static final String DROP_TABLE_SQL = "DROP TABLE MyTable";
070:
071: // instances shared by all tests
072: private Connection con;
073: private Statement stmt;
074:
075: /**
076: * Constructor requiring the name of the test to be run.
077: *
078: * @param testName the name of the test to be run
079: */
080: public TestBinaryStream(String testName) {
081: super (testName);
082: }
083:
084: /**
085: * Default suite() method discovers all tests.
086: */
087: public static Test suite() {
088: return new TestSuite(TestBinaryStream.class);
089: }
090:
091: /**
092: * Fixture set up here.
093: *
094: * @throws Exception Some DB error
095: */
096: protected void setUp() throws Exception {
097: // grab a connection to the database
098: Class.forName("org.axiondb.jdbc.AxionDriver");
099: con = DriverManager.getConnection(DB_URI);
100: stmt = con.createStatement();
101: }
102:
103: /**
104: * Clean up resources.
105: *
106: * @throws Exception Some DB error
107: */
108: protected void tearDown() throws Exception {
109: String sqlText = DROP_TABLE_SQL;
110: stmt.executeUpdate(sqlText);
111: con.close();
112: }
113:
114: /**
115: * setBinaryStream succeeds but getting the same value as a binary stream causes an
116: * unexpected exception.
117: */
118: public void testGetBinaryStream() throws Exception {
119: String sqlText = CREATE_TABLE_SQL;
120: stmt.executeUpdate(sqlText);
121:
122: // insert a record
123: String text = "This is some text";
124: PreparedStatement ps = con.prepareStatement(INSERT_VALUES_SQL);
125: ps.setString(1, "Primary Key #1");
126: ps.setString(2, "Unique Value #1");
127: byte[] bArray = text.getBytes();
128: ByteArrayInputStream bais = new ByteArrayInputStream(bArray);
129: ps.setBinaryStream(3, bais, bArray.length);
130: ps.executeUpdate();
131: //con.commit();
132:
133: // read back the binary value
134: ResultSet rs = stmt.executeQuery(READ_BINVAL_SQL);
135: if (rs.next() == true) {
136: try {
137: rs.getBinaryStream("vBin");
138: rs.getObject("vBin");
139: } catch (SQLException e) {
140: String message;
141: message = "Unexpected exception when getting binary stream\n";
142: StringWriter sw = new StringWriter();
143: PrintWriter pw = new PrintWriter(sw);
144: e.printStackTrace(pw);
145: message += "Details : " + sw.toString();
146: fail(message);
147: }
148: }
149: }
150:
151: }
|