mirror of https://github.com/apache/openjpa.git
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
This commit is contained in:
parent
d91475d506
commit
cc1dcd9a4c
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -926,7 +926,6 @@
|
|||
<exclude>org/apache/openjpa/persistence/jdbc/schema/TestPerClassSequenceFactory.java</exclude>
|
||||
<exclude>org/apache/openjpa/persistence/jdbc/schema/TestSchema.java</exclude>
|
||||
<exclude>org/apache/openjpa/persistence/jdbc/schema/TestSchemaClone.java</exclude>
|
||||
<exclude>org/apache/openjpa/persistence/jdbc/schema/TestSchemaGenerator.java</exclude>
|
||||
<exclude>org/apache/openjpa/persistence/jdbc/schema/TestSequenceGeneratorEnsureCapacityCall.java</exclude>
|
||||
<exclude>org/apache/openjpa/persistence/jdbc/schema/TestXMLSchemaParser.java</exclude>
|
||||
<exclude>org/apache/openjpa/persistence/jdbc/schema/TestXMLSchemaSerializer.java</exclude>
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue