BuildDbTest.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.sql;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class BuildDbTest {
private static final Logger logger = LoggerFactory.getLogger(BuildDbTest.class);
public static final String SCHEMA = "CREATE TABLE Test1 (Sender TEXT NOT NULL, MailID SERIAL PRIMARY KEY);" +
"CREATE TABLE Test2 (Name TEXT NOT NULL, pk SERIAL PRIMARY KEY);";
private static final SchemaGenerator schema = new SchemaGenerator() {
@Override public Iterator<String> openSchema() {
return SequenceExecutor.readerSequencer(new StringReader(SCHEMA));
}
};
/** Static interface method used by the {@link net.metanotion.sql.BuildDB} utility.
@return A schema generator that provides the schema needed by this application.
*/
public static SchemaGenerator schemaFactory() { return schema; }
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 String CLS_NAME = "net.metanotion.sql.BuildDbTest";
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_SCHEMA = "-schema:";
public static final String OPT_ECHO = "-echo";
public static final String OPT_CLASS = "-class:";
public static final String OPT_RESOURCE = "-resource:";
public static final String OPT_FILE = "-file:";
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[] create = new String[] {
OPT_URL + url,
OPT_USER + user,
OPT_PASS + pass,
OPT_DATABASE + dbName,
OPT_CREATE,
"-dropFirst",
};
BuildDb.main(create);
final String[] execResource = new String[] {
OPT_URL + url,
OPT_USER + user,
OPT_PASS + pass,
OPT_DATABASE + dbName,
OPT_RESOURCE + "buildDbFile.sql"
};
BuildDb.main(execResource);
final String[] execFile = new String[] {
OPT_URL + url,
OPT_USER + user,
OPT_PASS + pass,
OPT_DATABASE + dbName,
OPT_CREATE,
"-dropFirst",
OPT_FILE + file
};
BuildDb.main(execFile);
final String[] all = new String[] {
OPT_URL + url,
OPT_USER + user,
OPT_PASS + pass,
OPT_DATABASE + dbName,
OPT_CREATE,
"-dropFirst",
OPT_SCHEMA + "test",
OPT_CLASS + CLS_NAME,
OPT_ECHO
};
final PrintStream sysOut = System.out;
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final PrintStream echo = new PrintStream(baos);
System.setOut(echo);
BuildDb.main(all);
System.setOut(sysOut);
echo.close();
final String actual = new String(baos.toByteArray(), StandardCharsets.UTF_8);
final String expected = "CREATE SCHEMA test;\r\n"
+ "SET search_path TO test;\r\n"
+ "CREATE TABLE Test1 (Sender TEXT NOT NULL, MailID SERIAL PRIMARY KEY);"
+ "CREATE TABLE Test2 (Name TEXT NOT NULL, pk SERIAL PRIMARY KEY);\r\n";
if(!expected.equals(actual)) {
System.out.println(actual);
throw new AssertionError("Expected echo output does not match actual output");
}
}
}