001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.test;
032:
033: import java.io.BufferedReader;
034: import java.io.File;
035: import java.io.FileReader;
036: import java.io.IOException;
037: import java.util.ArrayList;
038: import java.util.StringTokenizer;
039:
040: public class TestSqlTool extends junit.framework.TestCase {
041:
042: /**
043: * Trivial utility class (for use like x.a dna x.b.
044: * Does not have getters/setters. No purpose would be served by
045: * getters and setters, other than over-engineering.
046: */
047: private class TestSqlFile {
048:
049: public File file;
050: public String description;
051:
052: public TestSqlFile(String filename, String inDescript)
053: throws IOException {
054:
055: file = new File(filename);
056:
057: if (!file.isFile()) {
058: throw new IOException("'" + file + "' is not a file");
059: }
060:
061: description = inDescript;
062: }
063: }
064:
065: /**
066: * List of SQL files, with a description of the purpose.
067: */
068: private class SqlFileList extends ArrayList {
069:
070: /**
071: * Loads a list of SQL files and descriptions for the specified
072: * test * method.
073: */
074: public SqlFileList(String filename) throws IOException {
075:
076: BufferedReader br = new BufferedReader(new FileReader(
077: filename));
078: String s, trimmed;
079: StringTokenizer st;
080: int ctr = 0;
081:
082: while ((s = br.readLine()) != null) {
083: ctr++;
084:
085: trimmed = s.replaceFirst("#.*", "").trim(); // Remove comments.
086:
087: if (trimmed.length() < 1) {
088: continue; // Skip blank and comment lines
089: }
090:
091: st = new StringTokenizer(trimmed);
092:
093: if (st.countTokens() < 2) {
094: throw new IOException("Bad line no. " + ctr
095: + " in list file '" + filename + "'");
096: }
097:
098: add(new TestSqlFile(st.nextToken(), st.nextToken("")));
099: }
100:
101: br.close();
102: }
103:
104: public TestSqlFile getSqlFile(int i) {
105: return (TestSqlFile) get(i);
106: }
107: }
108:
109: SqlToolHarness harness = new SqlToolHarness();
110:
111: private void runTestsInList(String testList) throws Exception {
112:
113: SqlFileList fileList = new SqlFileList(testList);
114: TestSqlFile sqlFile;
115:
116: for (int i = 0; i < fileList.size(); i++) {
117: sqlFile = fileList.getSqlFile(i);
118:
119: assertTrue(sqlFile.description + " (" + sqlFile.file + ')',
120: harness.execute(sqlFile.file));
121: }
122: }
123:
124: public void testHistory() throws Exception {
125: runTestsInList("testHistory.list");
126: }
127:
128: public void testEditing() throws Exception {
129: runTestsInList("testEditing.list");
130: }
131:
132: public void testArgs() throws Exception {
133: runTestsInList("testArgs.list");
134: }
135:
136: public void testComments() throws Exception {
137: runTestsInList("testComments.list");
138: }
139:
140: public void testPL() throws Exception {
141: runTestsInList("testPL.list");
142: }
143:
144: public void testSpecials() throws Exception {
145: runTestsInList("testSpecials.list");
146: }
147:
148: public void testSQL() throws Exception {
149: runTestsInList("testSQL.list");
150: }
151:
152: // public TestSqlTool() { super(); } necessary?
153: public TestSqlTool(String s) {
154: super (s);
155: }
156:
157: public static void main(String[] sa) {
158:
159: if (sa.length > 0 && sa[0].startsWith("--gui")) {
160: junit.swingui.TestRunner.run(TestSqlTool.class);
161: } else {
162: junit.textui.TestRunner runner = new junit.textui.TestRunner();
163:
164: System.exit(runner.run(
165: runner.getTest(TestSqlTool.class.getName()))
166: .wasSuccessful() ? 0 : 1);
167: }
168: }
169:
170: public static junit.framework.Test suite() {
171:
172: junit.framework.TestSuite newSuite = new junit.framework.TestSuite();
173:
174: newSuite.addTest(new TestSqlTool("testHistory"));
175: newSuite.addTest(new TestSqlTool("testEditing"));
176: newSuite.addTest(new TestSqlTool("testArgs"));
177: newSuite.addTest(new TestSqlTool("testComments"));
178: newSuite.addTest(new TestSqlTool("testPL"));
179: newSuite.addTest(new TestSqlTool("testSpecials"));
180: newSuite.addTest(new TestSqlTool("testSQL"));
181:
182: return newSuite;
183: };
184: }
|