KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > test > TestSqlTool


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.test;
33
34 import java.io.BufferedReader JavaDoc;
35 import java.io.File JavaDoc;
36 import java.io.FileReader JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.StringTokenizer JavaDoc;
40
41 public class TestSqlTool extends junit.framework.TestCase {
42
43     /**
44      * Trivial utility class (for use like x.a dna x.b.
45      * Does not have getters/setters. No purpose would be served by
46      * getters and setters, other than over-engineering.
47      */

48     private class TestSqlFile {
49
50         public File JavaDoc file;
51         public String JavaDoc description;
52
53         public TestSqlFile(String JavaDoc filename,
54                            String JavaDoc inDescript) throws IOException JavaDoc {
55
56             file = new File JavaDoc(filename);
57
58             if (!file.isFile()) {
59                 throw new IOException JavaDoc("'" + file + "' is not a file");
60             }
61
62             description = inDescript;
63         }
64     }
65
66     /**
67      * List of SQL files, with a description of the purpose.
68      */

69     private class SqlFileList extends ArrayList JavaDoc {
70
71         /**
72          * Loads a list of SQL files and descriptions for the specified
73          * test * method.
74          */

75         public SqlFileList(String JavaDoc filename) throws IOException JavaDoc {
76
77             BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(filename));
78             String JavaDoc s, trimmed;
79             StringTokenizer JavaDoc st;
80             int ctr = 0;
81
82             while ((s = br.readLine()) != null) {
83                 ctr++;
84
85                 trimmed = s.replaceFirst("#.*", "").trim(); // Remove comments.
86

87                 if (trimmed.length() < 1) {
88                     continue; // Skip blank and comment lines
89
}
90
91                 st = new StringTokenizer JavaDoc(trimmed);
92
93                 if (st.countTokens() < 2) {
94                     throw new IOException JavaDoc("Bad line no. " + ctr
95                                           + " in list file '" + filename
96                                           + "'");
97                 }
98
99                 add(new TestSqlFile(st.nextToken(), st.nextToken("")));
100             }
101
102             br.close();
103         }
104
105         public TestSqlFile getSqlFile(int i) {
106             return (TestSqlFile) get(i);
107         }
108     }
109
110     SqlToolHarness harness = new SqlToolHarness();
111
112     private void runTestsInList(String JavaDoc testList) throws Exception JavaDoc {
113
114         SqlFileList fileList = new SqlFileList(testList);
115         TestSqlFile sqlFile;
116
117         for (int i = 0; i < fileList.size(); i++) {
118             sqlFile = fileList.getSqlFile(i);
119
120             assertTrue(sqlFile.description + " (" + sqlFile.file + ')',
121                        harness.execute(sqlFile.file));
122         }
123     }
124
125     public void testHistory() throws Exception JavaDoc {
126         runTestsInList("testHistory.list");
127     }
128
129     public void testEditing() throws Exception JavaDoc {
130         runTestsInList("testEditing.list");
131     }
132
133     public void testArgs() throws Exception JavaDoc {
134         runTestsInList("testArgs.list");
135     }
136
137     public void testComments() throws Exception JavaDoc {
138         runTestsInList("testComments.list");
139     }
140
141     public void testPL() throws Exception JavaDoc {
142         runTestsInList("testPL.list");
143     }
144
145     public void testSpecials() throws Exception JavaDoc {
146         runTestsInList("testSpecials.list");
147     }
148
149     public void testSQL() throws Exception JavaDoc {
150         runTestsInList("testSQL.list");
151     }
152
153     // public TestSqlTool() { super(); } necessary?
154
public TestSqlTool(String JavaDoc s) {
155         super(s);
156     }
157
158     public static void main(String JavaDoc[] sa) {
159
160         if (sa.length > 0 && sa[0].startsWith("--gui")) {
161             junit.swingui.TestRunner.run(TestSqlTool.class);
162         } else {
163             junit.textui.TestRunner runner = new junit.textui.TestRunner();
164
165             System.exit(
166                 runner.run(
167                     runner.getTest(
168                         TestSqlTool.class.getName())).wasSuccessful() ? 0
169                                                                       : 1);
170         }
171     }
172
173     public static junit.framework.Test suite() {
174
175         junit.framework.TestSuite newSuite = new junit.framework.TestSuite();
176
177         newSuite.addTest(new TestSqlTool("testHistory"));
178         newSuite.addTest(new TestSqlTool("testEditing"));
179         newSuite.addTest(new TestSqlTool("testArgs"));
180         newSuite.addTest(new TestSqlTool("testComments"));
181         newSuite.addTest(new TestSqlTool("testPL"));
182         newSuite.addTest(new TestSqlTool("testSpecials"));
183         newSuite.addTest(new TestSqlTool("testSQL"));
184
185         return newSuite;
186     }
187     ;
188 }
189
Popular Tags