SqlTestTest.java

/***************************************************************************
   Copyright 2015 Emily Estes

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
***************************************************************************/
package net.metanotion.sqltest;


import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;

import net.metanotion.util.RecordInputStream;

public final class SqlTestTest {
	public static final int ARGS_URL = 0;
	public static final int ARGS_DBNAME = 1;
	public static final int ARGS_USER = 2;
	public static final int ARGS_PASS = 3;
	public static final int ARGS_FILE = 4;
	public static final int ARGS_DDL = 5;
	public static final int ARGS_SQLC = 6;
	public static final int ARGS_FAIL = 7;

	public static final String OPT_URL = "-url:";
	public static final String OPT_USER = "-user:";
	public static final String OPT_PASS = "-pass:";
	public static final String OPT_DATABASE = "-database:";
	public static final String OPT_CREATE = "-create";
	public static final String OPT_FILE = "-file:";
	public static final String OPT_TRANSCRIPT = "-transcript:";
	public static final String OPT_SQLC = "-sqlc:";

	public static void main(final String[] args) throws Exception {
		final String url = args[ARGS_URL];
		final String dbName = args[ARGS_DBNAME];
		final String user = args[ARGS_USER];
		final String pass = args[ARGS_PASS];
		final String file = args[ARGS_FILE];
		final String ddl = args[ARGS_DDL];
		final String path = args[ARGS_SQLC];
		final String fail = args[ARGS_FAIL];

		final String[] run = new String[] {
			OPT_URL + url,
			OPT_USER + user,
			OPT_PASS + pass,
			OPT_DATABASE + dbName,
			OPT_CREATE,
			"-dropFirst",
			OPT_FILE + ddl,
			OPT_TRANSCRIPT + file,
			OPT_SQLC + path
		};
		SqlTest.main(run);

		try (final Connection conn =  DriverManager.getConnection(url + dbName, user, pass)) {
			try (final InputStream in = new FileInputStream(fail)) {
				final RecordInputStream records = new RecordInputStream(in);
				do {
					boolean success = false;
					try {
						SqlTest.runTests(conn, records, null);
						success = true;
					} catch (final Throwable ex) {
						// Expected.
					}
					if(success) { throw new AssertionError("Sql Test was supposed to fail, succeeded instead."); }
				} while(records.nextRecord());
			}
		}
	}
}