001: /*
002: * $Id: TestBatchSqlCommandRunner.java,v 1.2 2003/03/27 19:14:07 rwald 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.tools;
042:
043: import java.io.BufferedReader;
044: import java.io.ByteArrayInputStream;
045: import java.io.InputStreamReader;
046: import java.sql.Connection;
047: import java.sql.DriverManager;
048: import java.sql.ResultSet;
049: import java.sql.Statement;
050:
051: import junit.framework.Test;
052: import junit.framework.TestCase;
053: import junit.framework.TestSuite;
054:
055: /**
056: * @author Chuck Burdick
057: */
058: public class TestBatchSqlCommandRunner extends TestCase {
059:
060: //------------------------------------------------------------- Constructor
061:
062: public TestBatchSqlCommandRunner(String testName) {
063: super (testName);
064: }
065:
066: public static Test suite() {
067: return new TestSuite(TestBatchSqlCommandRunner.class);
068: }
069:
070: //--------------------------------------------------------------- Utilities
071:
072: //--------------------------------------------------------------- Lifecycle
073:
074: public void setUp() throws Exception {
075: Class.forName("org.axiondb.jdbc.AxionDriver");
076:
077: _conn = DriverManager.getConnection("jdbc:axiondb:batchtest");
078: _stmt = _conn.createStatement();
079:
080: _runner = new BatchSqlCommandRunner(_conn);
081: _createdTables = false;
082: _reader = new BufferedReader(new InputStreamReader(
083: new ByteArrayInputStream(_input)));
084: }
085:
086: public void tearDown() throws Exception {
087: if (_createdTables) {
088: _stmt.execute("DROP TABLE products");
089: _stmt.execute("DROP TABLE preference_key_info");
090: }
091: _runner.close();
092: try {
093: _rset.close();
094: } catch (Exception e) {
095: }
096: try {
097: _stmt.close();
098: } catch (Exception e) {
099: }
100: try {
101: _conn.close();
102: } catch (Exception e) {
103: }
104: _reader.close();
105: }
106:
107: public void testReadLine() throws Exception {
108: String line = "/****************************************************************************/";
109: assertEquals("Should find first line", line, _runner
110: .readLine(_reader));
111: }
112:
113: public void testInQuotes() throws Exception {
114: assertTrue("done: no quotes", !_runner.isInQuotes("foo", false));
115: assertTrue("not done: one quote", _runner.isInQuotes("'foo",
116: false));
117: assertTrue("done: matched quotes", !_runner.isInQuotes("'foo'",
118: false));
119: assertTrue("done: apos literal", !_runner.isInQuotes(
120: "'foo '' bar'", false));
121:
122: boolean inQuote = _runner.isInQuotes("' foo", false);
123: assertTrue("not done: multi quote, multi line 1", inQuote);
124:
125: inQuote = _runner.isInQuotes(" ' , ';", inQuote);
126: assertTrue("not done: multi quote, multi line 2", inQuote);
127:
128: inQuote = _runner.isInQuotes(" bar');", inQuote);
129: assertTrue("not done: multi quote, multi line 3", !inQuote);
130: }
131:
132: public void testReadCommands() throws Exception {
133: String[] snippets = new String[] { "CREATE TABLE products ",
134: "CREATE TABLE preference_key_info " };
135:
136: for (int i = 0; i < snippets.length; i++) {
137: String cmd = _runner.readCommand(_reader);
138: assertNotNull("Should find command " + i, cmd);
139: assertTrue("Should have snippet " + snippets[i]
140: + " but found " + cmd, cmd.startsWith(snippets[i]));
141: }
142:
143: assertEquals("Should have no more commands", "", _runner
144: .readCommand(_reader));
145: }
146:
147: public void testRun() throws Exception {
148: _runner.runCommands(_reader);
149: _createdTables = true;
150: _rset = _stmt.executeQuery("SELECT product_id FROM products");
151: assertTrue("Should execute but find no results", !_rset.next());
152:
153: _rset.close();
154: _rset = null;
155:
156: _rset = _stmt
157: .executeQuery("SELECT description FROM preference_key_info");
158: assertTrue("Should execute but find no results", !_rset.next());
159: }
160:
161: //-------------------------------------------------------------- Attributes
162:
163: private boolean _createdTables = false;
164: private Connection _conn = null;
165: private Statement _stmt = null;
166: private ResultSet _rset = null;
167: private BatchSqlCommandRunner _runner = null;
168: private BufferedReader _reader = null;
169: private static final byte[] _input = "/****************************************************************************/\nCREATE TABLE products ( product_id INTEGER,\n description VARCHAR2,\n identifier_pattern VARCHAR2, \n created_date LONG, \n created_by VARCHAR2, \n modified_date LONG, \n modified_by VARCHAR2 ); \n\n/* some */\n/* multi-line */\n/* comment */\n/* block */\n\n/* some comment */\nCREATE TABLE preference_key_info ( preference_key_id INTEGER,\n pref_key INTEGER,\n description VARCHAR2,\n notes VARCHAR2,\n created_date LONG,\n created_by VARCHAR2,\n modified_date LONG,\n modified_by VARCHAR2);"
170: .getBytes();
171:
172: }
|