adding license headers to SQL and the code needed to parse it
Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/5734d017 Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/5734d017 Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/5734d017 Branch: refs/heads/master Commit: 5734d01789c4313f197edef3cfadc452250dea7d Parents: c450a1c Author: aadamchik <aadamc...@apache.org> Authored: Sun Nov 30 15:45:56 2014 +0300 Committer: aadamchik <aadamc...@apache.org> Committed: Sun Nov 30 15:45:56 2014 +0300 ---------------------------------------------------------------------- .../cayenne/test/resource/ResourceUtil.java | 154 +++---- .../cayenne/tools/DbImporterTaskTest.java | 2 +- plugins/maven-cayenne-plugin/pom.xml | 140 +++---- .../cayenne/tools/DbImporterMojoTest.java | 404 +++++++++---------- 4 files changed, 355 insertions(+), 345 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cayenne/blob/5734d017/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/resource/ResourceUtil.java ---------------------------------------------------------------------- diff --git a/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/resource/ResourceUtil.java b/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/resource/ResourceUtil.java index e5f8c05..f989273 100644 --- a/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/resource/ResourceUtil.java +++ b/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/resource/ResourceUtil.java @@ -18,6 +18,8 @@ ****************************************************************/ package org.apache.cayenne.test.resource; +import static org.junit.Assert.assertNotNull; + import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; @@ -30,83 +32,91 @@ import java.net.URL; public class ResourceUtil { - /** - * Copies resources to a file, thus making it available to the caller as File. - */ - public static void copyResourceToFile(String resourceName, File file) { - URL in = getResource(resourceName); + /** + * Copies resources to a file, thus making it available to the caller as + * File. + */ + public static void copyResourceToFile(String resourceName, File file) { + URL in = getResource(resourceName); + + if (!copyResourceToFile(in, file)) { + throw new RuntimeException("Error copying resource to file : " + file); + } + } + + /** + * Returns a guaranteed non-null resource for a given name. + */ + public static URL getResource(Class<?> relativeTo, String name) { + URL in = relativeTo.getResource(name); + assertNotNull("Resource not found: " + name, in); + return getResource(in); + } - if (!copyResourceToFile(in, file)) { - throw new RuntimeException("Error copying resource to file : " + file); - } - } + /** + * Returns a guaranteed non-null resource for a given name. + */ + public static URL getResource(String name) { + URL in = Thread.currentThread().getContextClassLoader().getResource(name); + assertNotNull("Resource not found: " + name, in); + return getResource(in); + } - /** - * Returns a guaranteed non-null resource for a given name. - */ - public static URL getResource(String name) { - URL in = Thread.currentThread().getContextClassLoader().getResource(name); - - if (in == null) { - throw new RuntimeException("Resource not found: " + name); - } + /** + * Returns a guaranteed non-null resource for a given name. + */ + private static URL getResource(URL classloaderUrl) { - // Fix for the issue described at https://issues.apache.org/struts/browse/SB-35 - // Basically, spaces in filenames make maven cry. - try { - in = new URL(in.toExternalForm().replaceAll(" ", "%20")); - } - catch (MalformedURLException e) { - throw new RuntimeException("Error constructing URL.", e); - } + if (classloaderUrl == null) { + throw new NullPointerException("null URL"); + } - return in; - } + // Fix for the issue described at + // https://issues.apache.org/struts/browse/SB-35 + // Basically, spaces in filenames make maven cry. + try { + return new URL(classloaderUrl.toExternalForm().replaceAll(" ", "%20")); + } catch (MalformedURLException e) { + throw new RuntimeException("Error constructing URL.", e); + } + } - public static boolean copyResourceToFile(URL from, File to) { - BufferedInputStream urlin = null; - BufferedOutputStream fout = null; - try { - int bufSize = 8 * 1024; - urlin = new BufferedInputStream( - from.openConnection().getInputStream(), - bufSize); - fout = new BufferedOutputStream(new FileOutputStream(to), bufSize); - copyPipe(urlin, fout, bufSize); - } - catch (IOException ioex) { - return false; - } - catch (SecurityException sx) { - return false; - } - finally { - if (urlin != null) { - try { - urlin.close(); - } - catch (IOException cioex) { - } - } - if (fout != null) { - try { - fout.close(); - } - catch (IOException cioex) { - } - } - } - return true; - } + public static boolean copyResourceToFile(URL from, File to) { + BufferedInputStream urlin = null; + BufferedOutputStream fout = null; + try { + int bufSize = 8 * 1024; + urlin = new BufferedInputStream(from.openConnection().getInputStream(), bufSize); + fout = new BufferedOutputStream(new FileOutputStream(to), bufSize); + copyPipe(urlin, fout, bufSize); + } catch (IOException ioex) { + return false; + } catch (SecurityException sx) { + return false; + } finally { + if (urlin != null) { + try { + urlin.close(); + } catch (IOException cioex) { + } + } + if (fout != null) { + try { + fout.close(); + } catch (IOException cioex) { + } + } + } + return true; + } - private static void copyPipe(InputStream in, OutputStream out, int bufSizeHint) - throws IOException { - int read = -1; - byte[] buf = new byte[bufSizeHint]; - while ((read = in.read(buf, 0, bufSizeHint)) >= 0) { - out.write(buf, 0, read); - } - out.flush(); - } + private static void copyPipe(InputStream in, OutputStream out, int bufSizeHint) throws IOException { + int read = -1; + byte[] buf = new byte[bufSizeHint]; + while ((read = in.read(buf, 0, bufSizeHint)) >= 0) { + out.write(buf, 0, read); + } + out.flush(); + } } http://git-wip-us.apache.org/repos/asf/cayenne/blob/5734d017/cayenne-tools/src/test/java/org/apache/cayenne/tools/DbImporterTaskTest.java ---------------------------------------------------------------------- diff --git a/cayenne-tools/src/test/java/org/apache/cayenne/tools/DbImporterTaskTest.java b/cayenne-tools/src/test/java/org/apache/cayenne/tools/DbImporterTaskTest.java index 7a0f7d1..1fdde49 100644 --- a/cayenne-tools/src/test/java/org/apache/cayenne/tools/DbImporterTaskTest.java +++ b/cayenne-tools/src/test/java/org/apache/cayenne/tools/DbImporterTaskTest.java @@ -189,7 +189,7 @@ public class DbImporterTaskTest { private void prepareDatabase(String sqlFile, DbImportConfiguration dbImportConfiguration) throws Exception { - URL sqlUrl = getClass().getResource("dbimport/" + sqlFile + ".sql"); + URL sqlUrl = ResourceUtil.getResource(getClass(), "dbimport/" + sqlFile + ".sql"); assertNotNull(sqlUrl); Class.forName(dbImportConfiguration.getDriver()).newInstance(); http://git-wip-us.apache.org/repos/asf/cayenne/blob/5734d017/plugins/maven-cayenne-plugin/pom.xml ---------------------------------------------------------------------- diff --git a/plugins/maven-cayenne-plugin/pom.xml b/plugins/maven-cayenne-plugin/pom.xml index 93f56c2..394ef7d 100644 --- a/plugins/maven-cayenne-plugin/pom.xml +++ b/plugins/maven-cayenne-plugin/pom.xml @@ -1,23 +1,16 @@ <?xml version="1.0" encoding="UTF-8"?> -<!-- - Licensed to the Apache Software Foundation (ASF) under one - or more contributor license agreements. See the NOTICE file - distributed with this work for additional information - regarding copyright ownership. The ASF licenses this file - to you 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. ---> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> +<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor + license agreements. See the NOTICE file distributed with this work for additional + information regarding copyright ownership. The ASF licenses this file to + you 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. --> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <parent> <artifactId>cayenne-plugins-parent</artifactId> @@ -50,7 +43,7 @@ <dependency> <groupId>org.apache.maven.plugin-tools</groupId> <artifactId>maven-plugin-tools-api</artifactId> - </dependency> + </dependency> <dependency> <groupId>org.apache.maven</groupId> @@ -61,7 +54,7 @@ <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-interpolation</artifactId> </dependency> - + <dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-container-default</artifactId> @@ -110,60 +103,67 @@ <scope>test</scope> </dependency> - <dependency> - <groupId>org.apache.derby</groupId> - <artifactId>derby</artifactId> - <scope>test</scope> - </dependency> - - <dependency> - <groupId>xmlunit</groupId> - <artifactId>xmlunit</artifactId> - <scope>test</scope> - </dependency> + <dependency> + <groupId>org.apache.derby</groupId> + <artifactId>derby</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>xmlunit</groupId> + <artifactId>xmlunit</artifactId> + <scope>test</scope> + </dependency> - <dependency> + <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-core</artifactId> <scope>test</scope> </dependency> - <dependency> - <groupId>org.apache.cayenne</groupId> - <artifactId>cayenne-server</artifactId> - <version>${project.version}</version> - <scope>test</scope> - <type>test-jar</type> - </dependency> - - <dependency> - <groupId>org.apache.cayenne</groupId> - <artifactId>cayenne-tools</artifactId> - <version>${project.version}</version> - <scope>test</scope> - <type>test-jar</type> - </dependency> - </dependencies> - - <reporting> - <plugins> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-plugin-plugin</artifactId> - <version>2.5.1</version> - </plugin> - </plugins> - </reporting> - - <build> - <plugins> - <plugin> - <artifactId>maven-checkstyle-plugin</artifactId> - </plugin> - <plugin> - <artifactId>maven-pmd-plugin</artifactId> - </plugin> - </plugins> - </build> + <dependency> + <groupId>org.apache.cayenne</groupId> + <artifactId>cayenne-server</artifactId> + <version>${project.version}</version> + <scope>test</scope> + <type>test-jar</type> + </dependency> + + <dependency> + <groupId>org.apache.cayenne</groupId> + <artifactId>cayenne-tools</artifactId> + <version>${project.version}</version> + <scope>test</scope> + <type>test-jar</type> + </dependency> + + <dependency> + <groupId>org.apache.cayenne.build-tools</groupId> + <artifactId>cayenne-test-utilities</artifactId> + <version>${project.version}</version> + <scope>test</scope> + </dependency> + </dependencies> + + <reporting> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-plugin-plugin</artifactId> + <version>2.5.1</version> + </plugin> + </plugins> + </reporting> + + <build> + <plugins> + <plugin> + <artifactId>maven-checkstyle-plugin</artifactId> + </plugin> + <plugin> + <artifactId>maven-pmd-plugin</artifactId> + </plugin> + </plugins> + </build> </project> http://git-wip-us.apache.org/repos/asf/cayenne/blob/5734d017/plugins/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java ---------------------------------------------------------------------- diff --git a/plugins/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java b/plugins/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java index dcaa026..9a36e60 100644 --- a/plugins/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java +++ b/plugins/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java @@ -23,7 +23,6 @@ import static org.apache.commons.lang.StringUtils.isBlank; import java.io.File; import java.io.FileReader; import java.io.IOException; -import java.net.URISyntaxException; import java.net.URL; import java.sql.Connection; import java.sql.DriverManager; @@ -32,6 +31,8 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.Iterator; +import org.apache.cayenne.test.jdbc.SQLReader; +import org.apache.cayenne.test.resource.ResourceUtil; import org.apache.cayenne.tools.dbimport.DbImportConfiguration; import org.apache.cayenne.tools.dbimport.config.Catalog; import org.apache.cayenne.tools.dbimport.config.IncludeTable; @@ -45,206 +46,205 @@ import org.xml.sax.SAXException; public class DbImporterMojoTest extends AbstractMojoTestCase { - static { - XMLUnit.setIgnoreWhitespace(true); - } - - public void testToParameters_MeaningfulPk() throws Exception { - - DbImportConfiguration parameters1 = getCdbImport("dbimporter-pom1.xml").toParameters(); - assertNull(parameters1.getMeaningfulPkTables()); - assertPathEquals("target/test/org/apache/cayenne/tools/dbimporter-map1.map.xml", - parameters1.getDataMapFile().getPath()); - - assertEquals("x,b*", getCdbImport("dbimporter-pom2.xml").toParameters().getMeaningfulPkTables()); - assertEquals("*", getCdbImport("dbimporter-pom3.xml").toParameters().getMeaningfulPkTables()); - } - - public void testToParameters_Map() throws Exception { - - DbImportConfiguration parameters1 = getCdbImport("dbimporter-pom1.xml").toParameters(); - assertNotNull(parameters1.getDataMapFile()); - assertPathEquals("target/test/org/apache/cayenne/tools/dbimporter-map1.map.xml", - parameters1.getDataMapFile().getPath()); - - assertNull(getCdbImport("dbimporter-pom2.xml").toParameters().getDataMapFile()); - } - - private DbImporterMojo getCdbImport(String pomFileName) throws Exception { - return (DbImporterMojo) lookupMojo("cdbimport", - getTestFile("src/test/resources/org/apache/cayenne/tools/" + pomFileName)); - } - - private void assertPathEquals(String expectedPath, String actualPath) { - assertEquals(new File(expectedPath), new File(actualPath)); - } - - public void testImportNewDataMap() throws Exception { - test("testImportNewDataMap"); - } - - public void testImportWithoutChanges() throws Exception { - test("testImportWithoutChanges"); - } - - public void testImportAddTableAndColumn() throws Exception { - test("testImportAddTableAndColumn"); - } - - public void testSimpleFiltering() throws Exception { - test("testSimpleFiltering"); - } - - public void testFilteringWithSchema() throws Exception { - test("testFilteringWithSchema"); - } - - public void testSchemasAndTableExclude() throws Exception { - test("testSchemasAndTableExclude"); - } - - public void testViewsExclude() throws Exception { - test("testViewsExclude"); - } - - private void test(String name) throws Exception { - DbImporterMojo cdbImport = getCdbImport("dbimport/" + name + "-pom.xml"); - File mapFile = cdbImport.getMap(); - File mapFileCopy = new File(mapFile.getParentFile(), "copy-" + mapFile.getName()); - if (mapFile.exists()) { - FileUtils.copyFile(mapFile, mapFileCopy); - cdbImport.setMap(mapFileCopy); - } else { - mapFileCopy = mapFile; - } - - prepareDatabase(name, cdbImport.toParameters()); - - try { - cdbImport.execute(); - verifyResult(mapFile, mapFileCopy); - } finally { - cleanDb(cdbImport.toParameters()); - } - } - - private void cleanDb(DbImportConfiguration dbImportConfiguration) throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException { - Class.forName(dbImportConfiguration.getDriver()).newInstance(); - // Get a connection - Connection connection = DriverManager.getConnection(dbImportConfiguration.getUrl()); - Statement stmt = connection.createStatement(); - - ResultSet views = connection.getMetaData().getTables(null, null, null, new String[]{"VIEW"}); - while (views.next()) { - String schema = views.getString("TABLE_SCHEM"); - System.out.println("DROP VIEW " + (isBlank(schema) ? "" : schema + ".") + views.getString("TABLE_NAME")); - stmt.execute("DROP VIEW " + (isBlank(schema) ? "" : schema + ".") + views.getString("TABLE_NAME")); - } - - ResultSet tables = connection.getMetaData().getTables(null, null, null, new String[]{"TABLE"}); - while (tables.next()) { - String schema = tables.getString("TABLE_SCHEM"); - System.out.println("DROP TABLE " + (isBlank(schema) ? "" : schema + ".") + tables.getString("TABLE_NAME")); - stmt.execute("DROP TABLE " + (isBlank(schema) ? "" : schema + ".") + tables.getString("TABLE_NAME")); - } - - ResultSet schemas = connection.getMetaData().getSchemas(); - while (schemas.next()) { - String schem = schemas.getString("TABLE_SCHEM"); - if (schem.startsWith("SCHEMA")) { - System.out.println("DROP SCHEMA " + schem); - stmt.execute("DROP SCHEMA " + schem + " RESTRICT"); - } - } - } - - private void verifyResult(File map, File mapFileCopy) { - try { - FileReader control = new FileReader(map.getAbsolutePath() + "-result"); - FileReader test = new FileReader(mapFileCopy); - - DetailedDiff diff = new DetailedDiff(new Diff(control, test)); - if (!diff.similar()) { - System.out.println(" >>>> " + map.getAbsolutePath() + "-result"); - System.out.println(" >>>> " + mapFileCopy); - fail(diff.toString()); - } - - } catch (SAXException e) { - e.printStackTrace(); - fail(); - } catch (IOException e) { - e.printStackTrace(); - fail(); - } - } - - public void testFilteringConfig() throws Exception { - DbImporterMojo cdbImport = getCdbImport("config/pom-01.xml"); - - assertEquals(2, cdbImport.getReverseEngineering().getCatalogs().size()); - Iterator<Catalog> iterator = cdbImport.getReverseEngineering().getCatalogs().iterator(); - assertEquals("catalog-name-01", iterator.next().getName()); - - Catalog catalog = iterator.next(); - assertEquals("catalog-name-02", catalog.getName()); - Iterator<Schema> schemaIterator = catalog.getSchemas().iterator(); - - assertEquals("schema-name-01", schemaIterator.next().getName()); - - Schema schema = schemaIterator.next(); - assertEquals("schema-name-02", schema.getName()); - - Iterator<IncludeTable> includeTableIterator = schema.getIncludeTables().iterator(); - assertEquals("incTable-01", includeTableIterator.next().getPattern()); - - IncludeTable includeTable = includeTableIterator.next(); - assertEquals("incTable-02", includeTable.getPattern()); - assertEquals("includeColumn-01", includeTable.getIncludeColumns().iterator().next().getPattern()); - assertEquals("excludeColumn-01", includeTable.getExcludeColumns().iterator().next().getPattern()); - - assertEquals("includeColumn-02", schema.getIncludeColumns().iterator().next().getPattern()); - assertEquals("excludeColumn-02", schema.getExcludeColumns().iterator().next().getPattern()); - - assertEquals("includeColumn-03", catalog.getIncludeColumns().iterator().next().getPattern()); - assertEquals("excludeColumn-03", catalog.getExcludeColumns().iterator().next().getPattern()); - - schemaIterator = cdbImport.getReverseEngineering().getSchemas().iterator(); - schema = schemaIterator.next(); - assertEquals("schema-name-03", schema.getName()); - - schema = schemaIterator.next(); - assertEquals("schema-name-04", schema.getName()); - - includeTableIterator = schema.getIncludeTables().iterator(); - assertEquals("incTable-04", includeTableIterator.next().getPattern()); - assertEquals("excTable-04", schema.getExcludeTables().iterator().next().getPattern()); - - includeTable = includeTableIterator.next(); - assertEquals("incTable-05", includeTable.getPattern()); - assertEquals("includeColumn-04", includeTable.getIncludeColumns().iterator().next().getPattern()); - assertEquals("excludeColumn-04", includeTable.getExcludeColumns().iterator().next().getPattern()); - - assertEquals("includeColumn-04", schema.getIncludeColumns().iterator().next().getPattern()); - assertEquals("excludeColumn-04", schema.getExcludeColumns().iterator().next().getPattern()); - - assertEquals("includeColumn-03", catalog.getIncludeColumns().iterator().next().getPattern()); - assertEquals("excludeColumn-03", catalog.getExcludeColumns().iterator().next().getPattern()); - } - - private void prepareDatabase(String sqlFile, DbImportConfiguration dbImportConfiguration) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, IOException, URISyntaxException { - Class.forName(dbImportConfiguration.getDriver()).newInstance(); - // Get a connection - Statement stmt = DriverManager.getConnection(dbImportConfiguration.getUrl()).createStatement(); - - for (String sql : FileUtils.fileRead(sqlFile(sqlFile + ".sql")).split(";")) { - stmt.execute(sql); - } - } - - private File sqlFile(String name) throws URISyntaxException { - URL url = DbImporterMojoTest.class.getResource("dbimport/" + name); - assertNotNull("Can't find resource: " + name); - return new File(url.toURI()); + static { + XMLUnit.setIgnoreWhitespace(true); + } + + public void testToParameters_MeaningfulPk() throws Exception { + + DbImportConfiguration parameters1 = getCdbImport("dbimporter-pom1.xml").toParameters(); + assertNull(parameters1.getMeaningfulPkTables()); + assertPathEquals("target/test/org/apache/cayenne/tools/dbimporter-map1.map.xml", parameters1.getDataMapFile() + .getPath()); + + assertEquals("x,b*", getCdbImport("dbimporter-pom2.xml").toParameters().getMeaningfulPkTables()); + assertEquals("*", getCdbImport("dbimporter-pom3.xml").toParameters().getMeaningfulPkTables()); + } + + public void testToParameters_Map() throws Exception { + + DbImportConfiguration parameters1 = getCdbImport("dbimporter-pom1.xml").toParameters(); + assertNotNull(parameters1.getDataMapFile()); + assertPathEquals("target/test/org/apache/cayenne/tools/dbimporter-map1.map.xml", parameters1.getDataMapFile() + .getPath()); + + assertNull(getCdbImport("dbimporter-pom2.xml").toParameters().getDataMapFile()); + } + + private DbImporterMojo getCdbImport(String pomFileName) throws Exception { + return (DbImporterMojo) lookupMojo("cdbimport", getTestFile("src/test/resources/org/apache/cayenne/tools/" + + pomFileName)); + } + + private void assertPathEquals(String expectedPath, String actualPath) { + assertEquals(new File(expectedPath), new File(actualPath)); + } + + public void testImportNewDataMap() throws Exception { + test("testImportNewDataMap"); + } + + public void testImportWithoutChanges() throws Exception { + test("testImportWithoutChanges"); + } + + public void testImportAddTableAndColumn() throws Exception { + test("testImportAddTableAndColumn"); + } + + public void testSimpleFiltering() throws Exception { + test("testSimpleFiltering"); + } + + public void testFilteringWithSchema() throws Exception { + test("testFilteringWithSchema"); + } + + public void testSchemasAndTableExclude() throws Exception { + test("testSchemasAndTableExclude"); + } + + public void testViewsExclude() throws Exception { + test("testViewsExclude"); + } + + private void test(String name) throws Exception { + DbImporterMojo cdbImport = getCdbImport("dbimport/" + name + "-pom.xml"); + File mapFile = cdbImport.getMap(); + File mapFileCopy = new File(mapFile.getParentFile(), "copy-" + mapFile.getName()); + if (mapFile.exists()) { + FileUtils.copyFile(mapFile, mapFileCopy); + cdbImport.setMap(mapFileCopy); + } else { + mapFileCopy = mapFile; + } + + prepareDatabase(name, cdbImport.toParameters()); + + try { + cdbImport.execute(); + verifyResult(mapFile, mapFileCopy); + } finally { + cleanDb(cdbImport.toParameters()); + } + } + + private void cleanDb(DbImportConfiguration dbImportConfiguration) throws ClassNotFoundException, + IllegalAccessException, InstantiationException, SQLException { + Class.forName(dbImportConfiguration.getDriver()).newInstance(); + // Get a connection + Connection connection = DriverManager.getConnection(dbImportConfiguration.getUrl()); + Statement stmt = connection.createStatement(); + + ResultSet views = connection.getMetaData().getTables(null, null, null, new String[] { "VIEW" }); + while (views.next()) { + String schema = views.getString("TABLE_SCHEM"); + System.out.println("DROP VIEW " + (isBlank(schema) ? "" : schema + ".") + views.getString("TABLE_NAME")); + stmt.execute("DROP VIEW " + (isBlank(schema) ? "" : schema + ".") + views.getString("TABLE_NAME")); + } + + ResultSet tables = connection.getMetaData().getTables(null, null, null, new String[] { "TABLE" }); + while (tables.next()) { + String schema = tables.getString("TABLE_SCHEM"); + System.out.println("DROP TABLE " + (isBlank(schema) ? "" : schema + ".") + tables.getString("TABLE_NAME")); + stmt.execute("DROP TABLE " + (isBlank(schema) ? "" : schema + ".") + tables.getString("TABLE_NAME")); + } + + ResultSet schemas = connection.getMetaData().getSchemas(); + while (schemas.next()) { + String schem = schemas.getString("TABLE_SCHEM"); + if (schem.startsWith("SCHEMA")) { + System.out.println("DROP SCHEMA " + schem); + stmt.execute("DROP SCHEMA " + schem + " RESTRICT"); + } + } + } + + private void verifyResult(File map, File mapFileCopy) { + try { + FileReader control = new FileReader(map.getAbsolutePath() + "-result"); + FileReader test = new FileReader(mapFileCopy); + + DetailedDiff diff = new DetailedDiff(new Diff(control, test)); + if (!diff.similar()) { + System.out.println(" >>>> " + map.getAbsolutePath() + "-result"); + System.out.println(" >>>> " + mapFileCopy); + fail(diff.toString()); + } + + } catch (SAXException e) { + e.printStackTrace(); + fail(); + } catch (IOException e) { + e.printStackTrace(); + fail(); + } + } + + public void testFilteringConfig() throws Exception { + DbImporterMojo cdbImport = getCdbImport("config/pom-01.xml"); + + assertEquals(2, cdbImport.getReverseEngineering().getCatalogs().size()); + Iterator<Catalog> iterator = cdbImport.getReverseEngineering().getCatalogs().iterator(); + assertEquals("catalog-name-01", iterator.next().getName()); + + Catalog catalog = iterator.next(); + assertEquals("catalog-name-02", catalog.getName()); + Iterator<Schema> schemaIterator = catalog.getSchemas().iterator(); + + assertEquals("schema-name-01", schemaIterator.next().getName()); + + Schema schema = schemaIterator.next(); + assertEquals("schema-name-02", schema.getName()); + + Iterator<IncludeTable> includeTableIterator = schema.getIncludeTables().iterator(); + assertEquals("incTable-01", includeTableIterator.next().getPattern()); + + IncludeTable includeTable = includeTableIterator.next(); + assertEquals("incTable-02", includeTable.getPattern()); + assertEquals("includeColumn-01", includeTable.getIncludeColumns().iterator().next().getPattern()); + assertEquals("excludeColumn-01", includeTable.getExcludeColumns().iterator().next().getPattern()); + + assertEquals("includeColumn-02", schema.getIncludeColumns().iterator().next().getPattern()); + assertEquals("excludeColumn-02", schema.getExcludeColumns().iterator().next().getPattern()); + + assertEquals("includeColumn-03", catalog.getIncludeColumns().iterator().next().getPattern()); + assertEquals("excludeColumn-03", catalog.getExcludeColumns().iterator().next().getPattern()); + + schemaIterator = cdbImport.getReverseEngineering().getSchemas().iterator(); + schema = schemaIterator.next(); + assertEquals("schema-name-03", schema.getName()); + + schema = schemaIterator.next(); + assertEquals("schema-name-04", schema.getName()); + + includeTableIterator = schema.getIncludeTables().iterator(); + assertEquals("incTable-04", includeTableIterator.next().getPattern()); + assertEquals("excTable-04", schema.getExcludeTables().iterator().next().getPattern()); + + includeTable = includeTableIterator.next(); + assertEquals("incTable-05", includeTable.getPattern()); + assertEquals("includeColumn-04", includeTable.getIncludeColumns().iterator().next().getPattern()); + assertEquals("excludeColumn-04", includeTable.getExcludeColumns().iterator().next().getPattern()); + + assertEquals("includeColumn-04", schema.getIncludeColumns().iterator().next().getPattern()); + assertEquals("excludeColumn-04", schema.getExcludeColumns().iterator().next().getPattern()); + + assertEquals("includeColumn-03", catalog.getIncludeColumns().iterator().next().getPattern()); + assertEquals("excludeColumn-03", catalog.getExcludeColumns().iterator().next().getPattern()); + } + + private void prepareDatabase(String sqlFile, DbImportConfiguration dbImportConfiguration) throws Exception { + + URL sqlUrl = ResourceUtil.getResource(getClass(), "dbimport/" + sqlFile + ".sql"); + assertNotNull(sqlUrl); + + Class.forName(dbImportConfiguration.getDriver()).newInstance(); + // Get a connection + Statement stmt = DriverManager.getConnection(dbImportConfiguration.getUrl()).createStatement(); + + for (String sql : SQLReader.statements(sqlUrl, ";")) { + stmt.execute(sql); + } } }