001: package net.sourceforge.squirrel_sql.fw.sql;
002:
003: /*
004: * Copyright (C) 2007 Rob Manning
005: * manningr@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.io.File;
022: import java.io.FileWriter;
023: import java.io.IOException;
024: import java.io.PrintWriter;
025: import java.util.List;
026:
027: import junit.framework.Assert;
028:
029: /**
030: * A helper class for testing components that manipulate SQL.
031: *
032: * @author mannignr
033: */
034: public class SQLUtil {
035:
036: private static int genericSQLCount = 0;
037:
038: public static String getGenericSQLScript() {
039: StringBuffer result = new StringBuffer();
040: result.append(GenericSQL.CREATE_STUDENT);
041: result.append("\n\n");
042: result.append(GenericSQL.CREATE_COURSES);
043: result.append("\n\n");
044: result.append(GenericSQL.CREATE_PROFESSOR);
045: result.append("\n\n");
046: result.append(GenericSQL.CREATE_TAKE);
047: result.append("\n\n");
048: result.append(GenericSQL.CREATE_TEACH);
049: result.append("\n\n");
050: result.append(GenericSQL.STUDENTS_NOT_TAKING_CS112);
051: result.append("\n\n");
052: // Don't forget to set this to the number of statements in result
053: genericSQLCount = 6;
054: return result.toString();
055: }
056:
057: public static void checkQueryTokenizer(IQueryTokenizer qt,
058: int stmtCount) {
059: int count = 0;
060: while (qt.hasQuery()) {
061: count++;
062: System.out.println(" query: " + qt.nextQuery());
063: }
064: Assert.assertEquals(stmtCount, count);
065: }
066:
067: /**
068: * @param genericSQLCount the genericSQLCount to set
069: */
070: public static void setGenericSQLCount(int genericSQLCount) {
071: SQLUtil.genericSQLCount = genericSQLCount;
072: }
073:
074: /**
075: * @return the genericSQLCount
076: */
077: public static int getGenericSQLCount() {
078: return genericSQLCount;
079: }
080:
081: /**
082: * Creates a temporary file with the specified SQL statements in it.
083: * @param sqls
084: * @param deleteOnExit
085: * @return
086: * @throws IOException
087: */
088: public static String createSQLFile(List<String> sqls,
089: boolean deleteOnExit) throws IOException {
090: File f = File.createTempFile("test", ".sql");
091: if (deleteOnExit) {
092: f.deleteOnExit();
093: }
094: PrintWriter out = new PrintWriter(new FileWriter(f));
095: for (String sql : sqls) {
096: out.println(sql);
097: out.println();
098: }
099: out.close();
100: String tmpFilename = f.getAbsolutePath();
101: System.out.println("tmpFilename=" + tmpFilename);
102:
103: return tmpFilename;
104: }
105:
106: }
|