📄 dblook_test.java
字号:
/* Derby - Class org.apache.derbyTesting.functionTests.tests.tools.dblook_test Copyright 2004 The Apache Software Foundation or its licensors, as applicable. 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 org.apache.derbyTesting.functionTests.tests.tools;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Connection;import java.sql.Statement;import java.sql.PreparedStatement;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.SQLWarning;import java.sql.Timestamp;import java.io.PrintWriter;import java.io.FileOutputStream;import java.io.FileNotFoundException;import java.io.BufferedReader;import java.io.FileReader;import java.io.File;import org.apache.derby.tools.dblook;import org.apache.derby.tools.ij;import org.apache.derby.catalog.DependableFinder;import org.apache.derbyTesting.functionTests.util.TestUtil;import java.util.HashMap;import java.util.TreeMap;import java.util.Set;import java.util.Iterator;import java.util.ArrayList;import java.util.StringTokenizer;public class dblook_test { private static final int SERVER_PORT = 1527; private static final int FRONT = -1; private static final int REAR = 1; protected static final String dbCreationScript_1 = "dblook_makeDB.sql"; protected static final String dbCreationScript_2 = "dblook_makeDB_2.sql"; private static final char TEST_DELIMITER='#'; protected static String testDirectory = "dblook_test/"; protected static final String testDBName = "wombat"; protected static String separator; private static String dbPath; private static int duplicateCounter = 0; private static int sysNameCount = 0; private static String jdbcProtocol; /* ********************************************** * main: ****/ public static void main (String[] args) { separator = System.getProperty("file.separator"); new dblook_test().doTest(); System.out.println("\n[ Done. ]\n"); } /* ********************************************** * doTest * Run a full test of the dblook utility. ****/ protected void doTest() { try { // Test full dblook functionality. System.out.println("\n-= Start dblook Functional Tests. =-"); createTestDatabase(dbCreationScript_1); runDBLook(testDBName); // Test dblook messages. System.out.println("\n-= Start dblook Message Tests =-"); createTestDatabase(dbCreationScript_2); runMessageCheckTest(testDBName); } catch (SQLException se) { System.out.println("FAILED: to complete the test:"); se.printStackTrace(System.out); for (se = se.getNextException(); se != null; se = se.getNextException()) { se.printStackTrace(System.out); } } catch (Exception e) { System.out.println("FAILED: to complete the test:"); e.printStackTrace(System.out); } } /* ********************************************** * createTestDatabase: * Using the creation script created as part of * the test package, create the database that * will be used as the basis for all dblook * tests. * @param scriptName The name of the sql script * to use for creating the test database. * @return The test database has been created * in the current test directory, which is * "./dblook/" (as created by the harness). ****/ protected void createTestDatabase(String scriptName) throws Exception { // Delete existing database, if it exists. try { deleteDB(testDBName); } catch (Exception e) { System.out.println("** Warning: failed to delete " + "old test db before creating a new one..."); } Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); jdbcProtocol = "jdbc:derby:"; createDBFromDDL(testDBName, scriptName); // Figure out where our database directory is (abs path). dbPath = (new File(testDirectory)).getAbsolutePath(); return; } /* ********************************************** * runDBLook: * Runs a series of tests using dblook on * the received database. * @param dbName The name of the database on which to * run the tests. * @return A series of tests intended to verify * the full functionality of the dblook utility * has been run. ****/ private void runDBLook(String dbName) throws Exception { // Close the error stream, so that messages // printed to System.err aren't intermixed // with our output (otherwise, the order // of the System.out vs System.err is // arbitrary (because of the way the harness // works), and so we will get diffs with // the master. System.err.close(); // First, we dump all system catalogs for // the original source database to file. dumpSysCatalogs(dbName); // Then, we run dblook on the source database // with no limitations (i.e. we generate the // DDL for the FULL database). lookOne(dbName); dumpFileToSysOut("dblook.log"); // Now, create new db from the DDL that // was generated by dblook. String newDBName = dbName + "_new"; createDBFromDDL(newDBName, dbName + ".sql"); deleteFile(new File(dbName + ".sql")); // Dump all system catalogs for the database // that was created from the DDL generated // by dblook. dumpSysCatalogs(newDBName); // Delete the new database. deleteDB(newDBName); deleteFile(new File(newDBName + ".sql")); // Run dblook on the source database // with various parameter configurations, // to make sure they are all working as // planned. runAllTests(dbName, newDBName); } /* ********************************************** * runAllTests: * Makes the call to execute each of the desired * tests. * @param dbName The name of the database on which to * run the tests. * @param newDBName The name of the database to be * created from the DDL that is generated (by * dblook) for the source database. ****/ protected void runAllTests(String dbName, String newDBName) throws Exception { runTest(2, dbName, newDBName); // Test 3 is run as part of derbynet suite; // see derbynet/dblook_test_net.java. runTest(4, dbName, newDBName); runTest(5, dbName, newDBName); runTest(7, dbName, newDBName); runTest(6, dbName, newDBName); return; } /* ********************************************** * runTest: * Runs dblook on the source database with a * specific set of parameters, then uses the * resultant DDL to create a new database, and * dumps the system catalogs for that database * to file. Finally, the new database is deleted * in preparation for subsequent calls to this * method. * @param whichTest An indication of which test to run; * each test number has a different set of * parameters. * @param dbName The name of the source database. * @param newDBName The name of the database to be * created from the DDL that is generated (by * dblook) for the source database. * @return dblook has been executed using the * parameters associated with the given test, * and that DDL has been written to a ".sql" * file named after the source database; * a new database has been created from the * ".sql" generated by dblook; the system * catalogs for that new database have been * dumped to output; and the new database has * been deleted. ****/ protected void runTest(int whichTest, String dbName, String newDBName) { try { switch(whichTest) { case 2: lookTwo(dbName); break; case 3: lookThree(dbName); break; case 4: lookFour(dbName); break; case 5: lookFive(dbName); break; case 6: lookSix(dbName); break; case 7: lookSeven(dbName); break; default: break; } dumpFileToSysOut("dblook.log"); createDBFromDDL(newDBName, dbName + ".sql"); dumpSysCatalogs(newDBName); deleteDB(newDBName); deleteFile(new File(dbName + ".sql")); } catch (SQLException e) { System.out.println("FAILED: Test # : " + whichTest); e.printStackTrace(System.out); for (e = e.getNextException(); e != null; e = e.getNextException()) { e.printStackTrace(System.out); } } catch (Exception e) { System.out.println("FAILED: Test # : " + whichTest); e.printStackTrace(System.out); } return; } /* ********************************************** * lookOne: * Use dblook to generate FULL DDL for a given * database. * @param dbName The name of the source database (i.e. * the database for which the DDL is generated). * @return The full DDL for the source database * has been generated and written to a file * called <dbName + ".sql">. ****/ private void lookOne(String dbName) throws Exception { printAsHeader("\nDumping full schema for '" + dbName + "'\nto file '" + dbName + ".sql':\n"); String [] args = new String[] { "-o", dbName + ".sql", "-td", "" }; go(dbName, args); return; } /* ********************************************** * lookTwo: * Use dblook to generate DDL for all objects * in the source database with schema 'BAR', * excluding views: * -z bar -noview * @param dbName The name of the source database (i.e. * the database for which the DDL is generated). * @return The appropriate DDL has been generated * and written to a file called <dbName + ".sql">. ****/ private void lookTwo(String dbName) throws Exception { printAsHeader("\nDumping DDL for all objects " + "with schema\n'BAR', excluding views:\n"); String [] args = new String[] { "-o", dbName + ".sql", "-td", "", "-z", "bar", "-noview" }; go(dbName, args); return; } /* ********************************************** * lookThree: * Use dblook to generate DDL for all objects * in the source database, using Network * Server. * @param dbName The name of the source database (i.e. * the database for which the DDL is generated). * @return The appropriate DDL has been generated * and written to a file called <dbName + ".sql">. ****/ private void lookThree(String dbName) throws Exception { printAsHeader("\nDumping DDL for all objects, " + "using\nNetwork Server:\n"); jdbcProtocol = TestUtil.getJdbcUrlPrefix("localhost",SERVER_PORT); String sourceDBUrl; if (TestUtil.isJCCFramework()) sourceDBUrl = jdbcProtocol + "\"" + dbPath + separator + dbName + "\":user=someusr;password=somepwd;"; else sourceDBUrl = jdbcProtocol + dbPath + separator + dbName + ";user=someusr;password=somepwd"; // Make sure we're not connected to the database // (we connected to it in embedded mode when we // created it, so we have to shut it down). try { DriverManager.getConnection( "jdbc:derby:" + dbName + ";shutdown=true"); } catch (SQLException e) {} // Run the test. try { new dblook(new String[] { "-d", sourceDBUrl, "-o", dbName + ".sql", "-td", "" }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -