001: /*
002: * WbSchemaReportTest.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.sql.wbcommands;
013:
014: import java.io.File;
015: import java.io.FileInputStream;
016: import java.io.InputStreamReader;
017: import java.sql.SQLException;
018: import java.sql.Statement;
019: import junit.framework.TestCase;
020: import workbench.TestUtil;
021: import workbench.db.WbConnection;
022: import workbench.sql.StatementRunnerResult;
023: import workbench.util.FileUtil;
024: import workbench.util.SqlUtil;
025:
026: /**
027: *
028: * @author support@sql-workbench.net
029: */
030: public class WbSchemaReportTest extends TestCase {
031:
032: private WbConnection source;
033: private TestUtil util;
034:
035: public WbSchemaReportTest(String testName) {
036: super (testName);
037: }
038:
039: public void testExecute() throws Exception {
040: try {
041: setupDatabase();
042: WbSchemaReport report = new WbSchemaReport();
043: report.setConnection(source);
044:
045: File output = new File(util.getBaseDir(), "report.xml");
046: output.delete();
047: StatementRunnerResult result = report
048: .execute("WbReport -file='"
049: + output.getAbsolutePath()
050: + "' -includeSequences=true -includeTableGrants=true");
051: assertTrue(result.isSuccess());
052: assertTrue("File not created", output.exists());
053:
054: InputStreamReader r = new InputStreamReader(
055: new FileInputStream(output), "UTF-8");
056: String xml = FileUtil.readCharacters(r);
057: r.close();
058:
059: String count = TestUtil.getXPathValue(xml,
060: "count(/schema-report/table-def)");
061: assertEquals("Incorrect table count", "3", count);
062:
063: count = TestUtil.getXPathValue(xml,
064: "count(/schema-report/view-def[@name='V_PERSON'])");
065: assertEquals("Incorrect view count", "1", count);
066:
067: count = TestUtil.getXPathValue(xml,
068: "count(/schema-report/sequence-def)");
069: assertEquals("Incorrect sequence count", "3", count);
070:
071: String value = TestUtil
072: .getXPathValue(xml,
073: "/schema-report/table-def[@name='PERSON']/grant/privilege");
074: assertEquals("Wrong privilege", "SELECT", value);
075:
076: count = TestUtil
077: .getXPathValue(xml,
078: "count(/schema-report/table-def[@name='ADDRESS']/grant)");
079: assertEquals("Incorrect grant count", "4", count);
080:
081: if (!output.delete()) {
082: fail("could not delete output file");
083: }
084: } catch (Exception e) {
085: e.printStackTrace();
086: fail(e.getMessage());
087: }
088: }
089:
090: private void setupDatabase() throws SQLException,
091: ClassNotFoundException {
092: util = new TestUtil("schemaReportTest");
093: this .source = util.getConnection();
094:
095: Statement stmt = null;
096:
097: try {
098: stmt = source.createStatement();
099: stmt
100: .executeUpdate("create table person (person_id integer primary key, firstname varchar(100), lastname varchar(100))");
101: stmt
102: .executeUpdate("create table address (address_id integer primary key, street varchar(50), city varchar(100), phone varchar(50), email varchar(50))");
103: stmt
104: .executeUpdate("create table person_address (person_id integer, address_id integer, primary key (person_id, address_id))");
105: stmt
106: .executeUpdate("alter table person_address add constraint fk_pa_person foreign key (person_id) references person(person_id)");
107: stmt
108: .executeUpdate("alter table person_address add constraint fk_pa_address foreign key (address_id) references address(address_id)");
109:
110: stmt
111: .executeUpdate("CREATE VIEW v_person AS SELECT * FROM person");
112: stmt.executeUpdate("CREATE sequence seq_one");
113: stmt
114: .executeUpdate("CREATE sequence seq_two increment by 5");
115: stmt.executeUpdate("CREATE sequence seq_three");
116:
117: stmt.executeUpdate("create user arthur password '42'");
118: stmt.executeUpdate("GRANT SELECT ON person TO arthur");
119: stmt
120: .executeUpdate("GRANT SELECT,INSERT,UPDATE,DELETE ON address TO arthur");
121: } finally {
122: SqlUtil.closeStatement(stmt);
123: }
124: }
125:
126: }
|