From cc1dcd9a4c177a28db4d8060748f59bf4e9b670e Mon Sep 17 00:00:00 2001 From: "Richard G. Curtis" Date: Wed, 26 Jan 2011 18:54:54 +0000 Subject: [PATCH] OPENJPA-1847: Fix NPE in SchemaGenerator and formatting. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1063827 13f79535-47bb-0310-9956-ffa450edef68 --- .../openjpa/jdbc/schema/SchemaGenerator.java | 78 ++++++++--------- openjpa-persistence-jdbc/pom.xml | 1 - .../persistence/jdbc/kernel/BaseJDBCTest.java | 26 ++---- .../jdbc/schema/TestSchemaGenerator.java | 83 ++++--------------- 4 files changed, 59 insertions(+), 129 deletions(-) diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaGenerator.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaGenerator.java index d8ec805a2..51bf3acbd 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaGenerator.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaGenerator.java @@ -267,59 +267,52 @@ public class SchemaGenerator { * {@link #generatePrimaryKeys}, and {@link #generateForeignKeys} * automatically. */ - public void generateSchemas(DBIdentifier[] schemasAndTables) - throws SQLException { + public void generateSchemas(DBIdentifier[] schemasAndTables) throws SQLException { fireGenerationEvent(_loc.get("generating-schemas")); - - Object[][] schemaMap; - if (schemasAndTables == null || schemasAndTables.length == 0) - schemaMap = _allowed; - else - schemaMap = parseSchemasList(schemasAndTables); - - if (schemaMap == null) { - generateSchema(DBIdentifier.NULL, (DBIdentifier[])null); - - // estimate the number of schema objects we will need to visit - // in order to estimate progress total for any listeners - int numTables = getTables(null).size(); - _schemaObjects += numTables - + (_pks ? numTables : 0) - + (_indexes ? numTables : 0) - + (_fks ? numTables : 0); - - if (_pks) - generatePrimaryKeys(DBIdentifier.NULL, null); - if (_indexes) - generateIndexes(DBIdentifier.NULL, null); - if (_fks) - generateForeignKeys(DBIdentifier.NULL, null); - return; - } - // generate all schemas and tables - try{ - getConn(); + try { + getConn(); + Object[][] schemaMap; + if (schemasAndTables == null || schemasAndTables.length == 0) + schemaMap = _allowed; + else + schemaMap = parseSchemasList(schemasAndTables); + + if (schemaMap == null) { + generateSchema(DBIdentifier.NULL, (DBIdentifier[]) null); + + // estimate the number of schema objects we will need to visit + // in order to estimate progress total for any listeners + int numTables = getTables(null).size(); + _schemaObjects += + numTables + (_pks ? numTables : 0) + (_indexes ? numTables : 0) + (_fks ? numTables : 0); + + if (_pks) + generatePrimaryKeys(DBIdentifier.NULL, null); + if (_indexes) + generateIndexes(DBIdentifier.NULL, null); + if (_fks) + generateForeignKeys(DBIdentifier.NULL, null); + return; + } + for (int i = 0; i < schemaMap.length; i++) { generateSchema((DBIdentifier) schemaMap[i][0], (DBIdentifier[]) schemaMap[i][1]); } - + // generate pks, indexes, fks DBIdentifier schemaName = DBIdentifier.NULL; DBIdentifier[] tableNames; for (int i = 0; i < schemaMap.length; i++) { schemaName = (DBIdentifier) schemaMap[i][0]; tableNames = (DBIdentifier[]) schemaMap[i][1]; - + // estimate the number of schema objects we will need to visit // in order to estimate progress total for any listeners - int numTables = (tableNames != null) ? tableNames.length - : getTables(schemaName).size(); - _schemaObjects += numTables - + (_pks ? numTables : 0) - + (_indexes ? numTables : 0) - + (_fks ? numTables : 0); - + int numTables = (tableNames != null) ? tableNames.length : getTables(schemaName).size(); + _schemaObjects += + numTables + (_pks ? numTables : 0) + (_indexes ? numTables : 0) + (_fks ? numTables : 0); + if (_pks) { generatePrimaryKeys(schemaName, tableNames); } @@ -330,9 +323,8 @@ public class SchemaGenerator { generateForeignKeys(schemaName, tableNames); } } - } - finally { - closeConn(); + } finally { + closeConn(); } } diff --git a/openjpa-persistence-jdbc/pom.xml b/openjpa-persistence-jdbc/pom.xml index 1001c1b2a..35fc1c398 100644 --- a/openjpa-persistence-jdbc/pom.xml +++ b/openjpa-persistence-jdbc/pom.xml @@ -926,7 +926,6 @@ org/apache/openjpa/persistence/jdbc/schema/TestPerClassSequenceFactory.java org/apache/openjpa/persistence/jdbc/schema/TestSchema.java org/apache/openjpa/persistence/jdbc/schema/TestSchemaClone.java - org/apache/openjpa/persistence/jdbc/schema/TestSchemaGenerator.java org/apache/openjpa/persistence/jdbc/schema/TestSequenceGeneratorEnsureCapacityCall.java org/apache/openjpa/persistence/jdbc/schema/TestXMLSchemaParser.java org/apache/openjpa/persistence/jdbc/schema/TestXMLSchemaSerializer.java diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/kernel/BaseJDBCTest.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/kernel/BaseJDBCTest.java index 10a689bd0..ec67df180 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/kernel/BaseJDBCTest.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/kernel/BaseJDBCTest.java @@ -27,31 +27,17 @@ */ package org.apache.openjpa.persistence.jdbc.kernel; -import java.beans.BeanInfo; -import java.beans.Introspector; -import java.beans.PropertyDescriptor; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.util.*; -import javax.management.IntrospectionException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + import org.apache.openjpa.kernel.Broker; -import org.apache.openjpa.kernel.OpenJPAStateManager; import org.apache.openjpa.kernel.jpql.JPQLParser; import org.apache.openjpa.lib.conf.ConfigurationProvider; import org.apache.openjpa.lib.conf.Configurations; -import org.apache.openjpa.persistence.OpenJPAPersistence; - - - -import java.lang.annotation.Annotation; -import junit.framework.*; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory; -import org.apache.openjpa.persistence.OpenJPAEntityManager; import org.apache.openjpa.persistence.JPAFacadeHelper; +import org.apache.openjpa.persistence.OpenJPAEntityManager; public class BaseJDBCTest diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestSchemaGenerator.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestSchemaGenerator.java index c62c0c0cf..2832d6c47 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestSchemaGenerator.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/schema/TestSchemaGenerator.java @@ -42,7 +42,6 @@ import org.apache.openjpa.jdbc.sql.DBDictionary; import org.apache.openjpa.persistence.jdbc.common.apps.*; - import java.lang.annotation.Annotation; import junit.framework.*; import javax.persistence.EntityManager; @@ -53,71 +52,26 @@ import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI; import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI; import org.apache.openjpa.persistence.OpenJPAPersistence; +public class TestSchemaGenerator extends org.apache.openjpa.persistence.jdbc.kernel.BaseJDBCTest { + + @Override + protected String getPersistenceUnitName() { + // TODO Auto-generated method stub + return "TestConv"; + } -public class TestSchemaGenerator - extends org.apache.openjpa.persistence.jdbc.kernel.BaseJDBCTest { - /** Creates a new instance of TestSchemaGenerator */ - public TestSchemaGenerator(String name) - { - super(name); + public TestSchemaGenerator(String name) { + super(name); } - - public void DBMetadataTest() - throws Exception { - OpenJPAEntityManagerFactory pmf = (OpenJPAEntityManagerFactory) - getEmf(); - //FIXME jthomas - - //ClassMapping cm = (ClassMapping) KodoJDOHelper.getMetaData - // (pmf, RuntimeTest1.class); - ClassMapping cm =null; - JDBCConfiguration conf = (JDBCConfiguration) getConfiguration(); - - DataSource ds = (DataSource) conf.getDataSource2(null); - Connection c = ds.getConnection(); - DatabaseMetaData meta = c.getMetaData(); - DBDictionary dict = conf.getDBDictionaryInstance(); - - String schema = cm.getTable().getSchema().getName(); - Table[] tables = dict.getTables(meta, c.getCatalog(), schema, - cm.getTable().getName(), c); - assertEquals(1, tables.length); - - Column[] columns = dict.getColumns(meta, c.getCatalog(), schema, - cm.getTable().getName(), null, c); - for (int i = 0; i < columns.length; i++) - System.out.println("### " + columns[i].getName()); - } - - public void testSchemaGen() - throws Exception { - OpenJPAEntityManagerFactory pmf = (OpenJPAEntityManagerFactory) - getEmf(); + + public void testSchemaGen() throws Exception { + OpenJPAEntityManagerFactory pmf = (OpenJPAEntityManagerFactory) getEmf(); OpenJPAEntityManager pm = pmf.createEntityManager(); - JDBCConfiguration con = - (JDBCConfiguration) ((OpenJPAEntityManagerSPI) pm) - .getConfiguration(); - DBDictionary dict = con.getDBDictionaryInstance(); - MappingRepository repos = con.getMappingRepositoryInstance(); - ClassMapping cm = repos.getMapping(RuntimeTest1.class, - pm.getClassLoader(), true); - String schemas = cm.getTable().getSchema().getName(); - if (schemas == null) - schemas = ""; - schemas += "." + cm.getTable().getName(); - - Map props=new HashMap(); - props.put("openjpa.jdbc.Schemas", schemas); - - OpenJPAEntityManagerFactory kpmf =(OpenJPAEntityManagerFactory) - getEmf(props); - JDBCConfiguration conf = - (JDBCConfiguration) ((OpenJPAEntityManagerFactorySPI) kpmf) - .getConfiguration(); - + JDBCConfiguration conf = (JDBCConfiguration) ((OpenJPAEntityManagerFactorySPI) pmf).getConfiguration(); + StringWriter sw = new StringWriter(); - + SchemaTool.Flags flags = new SchemaTool.Flags(); flags.writer = sw; flags.primaryKeys = true; @@ -125,10 +79,9 @@ public class TestSchemaGenerator flags.indexes = true; flags.openjpaTables = true; flags.action = SchemaTool.ACTION_REFLECT; - - SchemaTool.run(conf, new String[0], flags, - getClass().getClassLoader()); - + + SchemaTool.run(conf, new String[0], flags, getClass().getClassLoader()); + sw.flush(); String data = sw.toString(); assertTrue(data.length() > 0);