001: /*
002: * $Id: TestConsole.java,v 1.13 2006/01/10 21:02:36 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:
041: package org.axiondb.tools;
042:
043: import java.io.ByteArrayInputStream;
044: import java.io.File;
045: import java.io.FileWriter;
046: import java.io.InputStream;
047: import java.io.PrintWriter;
048: import java.io.StringWriter;
049:
050: import junit.framework.Test;
051: import junit.framework.TestSuite;
052:
053: import org.axiondb.AbstractDbdirTest;
054:
055: /**
056: * @version $Revision: 1.13 $ $Date: 2006/01/10 21:02:36 $
057: * @author Chuck Burdick
058: */
059: public class TestConsole extends AbstractDbdirTest {
060:
061: //------------------------------------------------------------ Conventional
062:
063: public TestConsole(String testName) {
064: super (testName);
065: }
066:
067: public static void main(String args[]) {
068: String[] testCaseName = { TestConsole.class.getName() };
069: junit.textui.TestRunner.main(testCaseName);
070: }
071:
072: public static Test suite() {
073: return new TestSuite(TestConsole.class);
074: }
075:
076: //--------------------------------------------------------------- Lifecycle
077:
078: private static final String SEP = System
079: .getProperty("line.separator");
080: private Console _console = null;
081: private StringWriter _out = null;
082: private PrintWriter _writer = null;
083:
084: public void setUp() throws Exception {
085: super .setUp();
086: _out = new StringWriter();
087: _writer = new PrintWriter(_out, true);
088: _console = new Console("foo", _writer);
089: }
090:
091: public void tearDown() throws Exception {
092: _console.execute("drop table foo");
093: _console.cleanUp();
094: _writer.close();
095: _out.close();
096: _console = null;
097: super .tearDown();
098: }
099:
100: public void testDatabaseNameIsRequired() throws Exception {
101: try {
102: new Console(null, _writer);
103: fail("Expected NullPointerException");
104: } catch (NullPointerException e) {
105: // expected
106: }
107: }
108:
109: public void testWriterIsRequred() throws Exception {
110: try {
111: new Console("foo", null);
112: fail("Expected NullPointerException");
113: } catch (NullPointerException e) {
114: // expected
115: }
116: }
117:
118: public void testExecuteNullOrEmptyIsNoOp() throws Exception {
119: _console.execute(null);
120: _console.execute("");
121: _console.execute(" ");
122: }
123:
124: public void testCreateInMemory() throws Exception {
125: assertNotNull("Should have interface", _console);
126: assertNotNull("Should have connection", _console
127: .getConnection());
128: }
129:
130: public void testCreateOnDisk() throws Exception {
131: _console = new Console("foo", "./testdb", _writer);
132: assertNotNull("Should have interface", _console);
133: assertNotNull("Should have connection", _console
134: .getConnection());
135: }
136:
137: public void testPrematureSelect() throws Exception {
138: _console.execute("select * from foo");
139: String msg = _out.toString();
140: assertTrue("Should have error message but found: " + msg, msg
141: .indexOf("object does not exist") != -1);
142: }
143:
144: private void assertEmpty(String msg, StringBuffer buf) {
145: assertEquals(msg, 0, buf.length());
146: }
147:
148: private void assertNotEmpty(StringBuffer buf) {
149: assertTrue(buf.length() > 0);
150: }
151:
152: private void assertNotEmpty(String msg, StringBuffer buf) {
153: assertTrue(msg, buf.length() > 0);
154: }
155:
156: private void clearOutput(StringBuffer buf) {
157: buf.setLength(0);
158: }
159:
160: public void testCreateTableThenSelect() throws Exception {
161: assertEmpty("Should have no output yet", _out.getBuffer());
162: _console.execute("create table foo (id varchar2)");
163: assertNotEmpty("Should have some output", _out.getBuffer());
164: clearOutput(_out.getBuffer());
165: _console.execute("select id from foo");
166: StringBuffer expected = new StringBuffer();
167: expected.append("+====+");
168: expected.append(SEP);
169: expected.append("| ID |");
170: expected.append(SEP);
171: expected.append("+====+");
172: expected.append(SEP);
173: expected.append("Execution time: ");
174: assertNotEmpty("Should have some output", _out.getBuffer());
175: assertTrue(_out.toString().startsWith(expected.toString()));
176: }
177:
178: public void testCreateTableThenDescribe() throws Exception {
179: _console.execute("create table foo (id varchar2)");
180: _console.execute("describe table foo");
181: assertNotEmpty("Should have some output", _out.getBuffer());
182: }
183:
184: public void testCreateDBLinkThenShow() throws Exception {
185: _console
186: .execute("create dblink foo (driver='org.axiondb.jdbc.AxionDriver' url='jdbc:axiondb:testdb' username='ignored' password='ignored')");
187: _console.execute("show dblinks foo");
188: assertNotEmpty("Should have some output", _out.getBuffer());
189: }
190:
191: public void testCreateShowTableProperties() throws Exception {
192: _console
193: .execute("create external table foo (id varchar2) organization(loadtype='delimited'");
194: _console.execute("SHOW TABLE PROPERTIES foo");
195: assertNotEmpty("Should have some output", _out.getBuffer());
196: }
197:
198: public void testCreateTableThenList() throws Exception {
199: _console.execute("create table foo (id varchar2)");
200: _console.execute("create index fooidx on foo(id)");
201: _console.execute("list table");
202: assertNotEmpty("Should have some output", _out.getBuffer());
203: _out.getBuffer().setLength(0);
204: _console.execute("list tables");
205: assertNotEmpty("Should have some output", _out.getBuffer());
206: _out.getBuffer().setLength(0);
207: _console.execute("list table;");
208: assertNotEmpty("Should have some output", _out.getBuffer());
209: _out.getBuffer().setLength(0);
210: _console.execute("list tables;");
211: assertNotEmpty("Should have some output", _out.getBuffer());
212:
213: _out.getBuffer().setLength(0);
214: _console.execute("show INDICES;");
215: assertNotEmpty("Should have some output", _out.getBuffer());
216:
217: _out.getBuffer().setLength(0);
218: _console.execute("show INDEXES;");
219: assertNotEmpty("Should have some output", _out.getBuffer());
220:
221: }
222:
223: public void testUpdateCountMessages() throws Exception {
224: // create
225: _out.getBuffer().setLength(0);
226: _console.execute("create table foo (id varchar2)");
227: assertTrue(_out.getBuffer().indexOf("Executed.") != -1);
228:
229: // insert
230: _out.getBuffer().setLength(0);
231: _console.execute("insert into foo values ('X')");
232: assertTrue(_out.getBuffer().indexOf("1 row changed.") != -1);
233:
234: // insert
235: _out.getBuffer().setLength(0);
236: _console.execute("insert into foo values ('X')");
237: assertTrue(_out.getBuffer().indexOf("1 row changed.") != -1);
238:
239: // update
240: _out.getBuffer().setLength(0);
241: _console.execute("update foo set id = 'Y' where id = 'X'");
242: assertTrue(_out.getBuffer().indexOf("2 rows changed.") != -1);
243:
244: // delete
245: _out.getBuffer().setLength(0);
246: _console.execute("delete from foo");
247: assertTrue(_out.getBuffer().indexOf("2 rows changed.") != -1);
248:
249: // delete
250: _out.getBuffer().setLength(0);
251: _console.execute("delete from foo");
252: assertTrue(_out.getBuffer().indexOf("No rows changed.") != -1);
253: }
254:
255: public void testCreateTableWithDataThenSelect() throws Exception {
256: assertEmpty("Should have no output yet", _out.getBuffer());
257: _console.execute("create table foo (id varchar2(3))");
258: _console.execute("insert into foo (id) values ('a')");
259: _console.execute("insert into foo (id) values ('b')");
260: _console.execute("insert into foo (id) values ('ccc')");
261: _console.execute("insert into foo (id) values ( NULL )");
262: assertNotEmpty(_out.getBuffer());
263: clearOutput(_out.getBuffer());
264: _console.execute("select id from foo");
265: StringBuffer expected = new StringBuffer();
266: expected.append("+======+");
267: expected.append(SEP);
268: expected.append("| ID |");
269: expected.append(SEP);
270: expected.append("+======+");
271: expected.append(SEP);
272: expected.append("| a |");
273: expected.append(SEP);
274: expected.append("+------+");
275: expected.append(SEP);
276: expected.append("| b |");
277: expected.append(SEP);
278: expected.append("+------+");
279: expected.append(SEP);
280: expected.append("| ccc |");
281: expected.append(SEP);
282: expected.append("+------+");
283: expected.append(SEP);
284: expected.append("| NULL |");
285: expected.append(SEP);
286: expected.append("+------+");
287: expected.append(SEP);
288: expected.append("Execution time: ");
289: assertNotEmpty("Should have some output", _out.getBuffer());
290: assertTrue(_out.toString().startsWith(expected.toString()));
291: }
292:
293: public void testCreateTableWithMultiColsThenSelect()
294: throws Exception {
295: assertEmpty("Should have no output yet", _out.getBuffer());
296: _console
297: .execute("create table foo (id varchar2(3), val integer)");
298: _console.execute("insert into foo (id, val) values ('a', 1)");
299: _console
300: .execute("insert into foo (id, val) values ('b', 1234567)");
301: _console
302: .execute("insert into foo (id, val) values ('ccc', -400)");
303: assertNotEmpty(_out.getBuffer());
304: clearOutput(_out.getBuffer());
305: _console.execute("select id, val from foo");
306: StringBuffer expected = new StringBuffer();
307: expected.append("+=====+=========+");
308: expected.append(SEP);
309: expected.append("| ID | VAL |");
310: expected.append(SEP);
311: expected.append("+=====+=========+");
312: expected.append(SEP);
313: expected.append("| a | 1 |");
314: expected.append(SEP);
315: expected.append("+-----+---------+");
316: expected.append(SEP);
317: expected.append("| b | 1234567 |");
318: expected.append(SEP);
319: expected.append("+-----+---------+");
320: expected.append(SEP);
321: expected.append("| ccc | -400 |");
322: expected.append(SEP);
323: expected.append("+-----+---------+");
324: expected.append(SEP);
325: expected.append("Execution time: ");
326: assertNotEmpty("Should have some output", _out.getBuffer());
327: assertTrue(_out.toString().startsWith(expected.toString()));
328: }
329:
330: public void testBatchFile() throws Exception {
331: File batchFile = new File(getDbdir(), "test.batch");
332: PrintWriter writer = new PrintWriter(new FileWriter(batchFile));
333: writer
334: .println("CREATE TABLE foo (id VARCHAR2(3), val INTEGER);");
335: writer.println("INSERT INTO foo (id, val) VALUES ('a', 1);");
336: writer
337: .println("INSERT INTO foo (id, val) VALUES ('b', 1234567);");
338: writer
339: .println("INSERT INTO foo (id, val) VALUES ('ccc', -400);");
340: writer.flush();
341: writer.close();
342: writer = null;
343: batchFile = null;
344:
345: StringBuffer expected = new StringBuffer();
346: expected.append("+=====+=========+");
347: expected.append(SEP);
348: expected.append("| ID | VAL |");
349: expected.append(SEP);
350: expected.append("+=====+=========+");
351: expected.append(SEP);
352: expected.append("| a | 1 |");
353: expected.append(SEP);
354: expected.append("+-----+---------+");
355: expected.append(SEP);
356: expected.append("| b | 1234567 |");
357: expected.append(SEP);
358: expected.append("+-----+---------+");
359: expected.append(SEP);
360: expected.append("| ccc | -400 |");
361: expected.append(SEP);
362: expected.append("+-----+---------+");
363: expected.append(SEP);
364: expected.append("Execution time: ");
365:
366: _console.execute("@" + getDbdir() + "/test.batch");
367: assertNotEmpty("Should have some output", _out.getBuffer());
368: assertTrue("Should get output", _out.toString().indexOf(
369: "Successfully loaded") > 0);
370: _out.getBuffer().setLength(0);
371:
372: _console.execute("SELECT id, val FROM foo");
373: assertNotEmpty("Should have some output", _out.getBuffer());
374: assertTrue(_out.toString().startsWith(expected.toString()));
375: }
376:
377: public void testMissingBatchFile() throws Exception {
378: _console.execute("@" + getDbdir() + "/test.batch");
379: assertNotEmpty("Should have some output", _out.getBuffer());
380: assertTrue("Should get output", _out.toString().startsWith(
381: "Error reading file"));
382: }
383:
384: public void testErrorInBatchFile() throws Exception {
385: File batchFile = new File(getDbdir(), "test.batch");
386: PrintWriter writer = new PrintWriter(new FileWriter(batchFile));
387: writer.println("XYZZY xyzzy XYZZY;");
388: writer.flush();
389: writer.close();
390: writer = null;
391: batchFile = null;
392:
393: _console.execute("@" + getDbdir() + "/test.batch");
394: assertNotEmpty("Should have some output", _out.getBuffer());
395: assertTrue("Expected SQLException in |"
396: + _out.getBuffer().toString() + "|", _out.getBuffer()
397: .indexOf("SQLException") != -1);
398: }
399:
400: public void testPrintUsage() throws Exception {
401: Console.main(new String[0]);
402: }
403:
404: public void testOpenThenQuit() throws Exception {
405: InputStream oldin = System.in;
406: try {
407: System.setIn(new ByteArrayInputStream(
408: "list system tables;\nexit\n".getBytes()));
409: Console.main(new String[] { "foo" });
410: } finally {
411: System.setIn(oldin);
412: }
413: }
414:
415: public void testOpenThenExit() throws Exception {
416: InputStream oldin = System.in;
417: try {
418: System.setIn(new ByteArrayInputStream(
419: "list system tables;\nexit\n".getBytes()));
420: Console.main(new String[] { "foo" });
421: } finally {
422: System.setIn(oldin);
423: }
424: }
425: }
|