001: /*
002: * TestSchemaGenerator.java
003: *
004: * Created on October 6, 2006, 2:57 PM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: /*
011: * Licensed to the Apache Software Foundation (ASF) under one
012: * or more contributor license agreements. See the NOTICE file
013: * distributed with this work for additional information
014: * regarding copyright ownership. The ASF licenses this file
015: * to you under the Apache License, Version 2.0 (the
016: * "License"); you may not use this file except in compliance
017: * with the License. You may obtain a copy of the License at
018: *
019: * http://www.apache.org/licenses/LICENSE-2.0
020: *
021: * Unless required by applicable law or agreed to in writing,
022: * software distributed under the License is distributed on an
023: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
024: * KIND, either express or implied. See the License for the
025: * specific language governing permissions and limitations
026: * under the License.
027: */
028: package org.apache.openjpa.persistence.jdbc.schema;
029:
030: import java.io.StringWriter;
031: import java.sql.Connection;
032: import java.sql.DatabaseMetaData;
033: import java.util.*;
034: import javax.sql.DataSource;
035: import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
036: import org.apache.openjpa.jdbc.meta.ClassMapping;
037: import org.apache.openjpa.jdbc.meta.MappingRepository;
038: import org.apache.openjpa.jdbc.schema.Column;
039: import org.apache.openjpa.jdbc.schema.SchemaTool;
040: import org.apache.openjpa.jdbc.schema.Table;
041: import org.apache.openjpa.jdbc.sql.DBDictionary;
042:
043: import org.apache.openjpa.persistence.jdbc.common.apps.*;
044:
045: import java.lang.annotation.Annotation;
046: import junit.framework.*;
047: import javax.persistence.EntityManager;
048: import javax.persistence.EntityManagerFactory;
049: import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
050: import org.apache.openjpa.persistence.OpenJPAEntityManager;
051: import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
052: import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
053: import org.apache.openjpa.persistence.OpenJPAPersistence;
054:
055: public class TestSchemaGenerator extends
056: org.apache.openjpa.persistence.jdbc.kernel.BaseJDBCTest {
057:
058: /** Creates a new instance of TestSchemaGenerator */
059: public TestSchemaGenerator(String name) {
060: super (name);
061: }
062:
063: public void DBMetadataTest() throws Exception {
064: OpenJPAEntityManagerFactory pmf = (OpenJPAEntityManagerFactory) getEmf();
065: //FIXME jthomas
066:
067: //ClassMapping cm = (ClassMapping) KodoJDOHelper.getMetaData
068: // (pmf, RuntimeTest1.class);
069: ClassMapping cm = null;
070: JDBCConfiguration conf = (JDBCConfiguration) getConfiguration();
071:
072: DataSource ds = (DataSource) conf.getDataSource2(null);
073: Connection c = ds.getConnection();
074: DatabaseMetaData meta = c.getMetaData();
075: DBDictionary dict = conf.getDBDictionaryInstance();
076:
077: String schema = cm.getTable().getSchema().getName();
078: Table[] tables = dict.getTables(meta, c.getCatalog(), schema,
079: cm.getTable().getName(), c);
080: assertEquals(1, tables.length);
081:
082: Column[] columns = dict.getColumns(meta, c.getCatalog(),
083: schema, cm.getTable().getName(), null, c);
084: for (int i = 0; i < columns.length; i++)
085: System.out.println("### " + columns[i].getName());
086: }
087:
088: public void testSchemaGen() throws Exception {
089: OpenJPAEntityManagerFactory pmf = (OpenJPAEntityManagerFactory) getEmf();
090: OpenJPAEntityManager pm = pmf.createEntityManager();
091: JDBCConfiguration con = (JDBCConfiguration) ((OpenJPAEntityManagerSPI) pm)
092: .getConfiguration();
093: DBDictionary dict = con.getDBDictionaryInstance();
094: MappingRepository repos = con.getMappingRepositoryInstance();
095: ClassMapping cm = repos.getMapping(RuntimeTest1.class, pm
096: .getClassLoader(), true);
097: String schemas = cm.getTable().getSchema().getName();
098: if (schemas == null)
099: schemas = "";
100: schemas += "." + cm.getTable().getName();
101:
102: Map props = new HashMap();
103: props.put("openjpa.jdbc.Schemas", schemas);
104:
105: OpenJPAEntityManagerFactory kpmf = (OpenJPAEntityManagerFactory) getEmf(props);
106: JDBCConfiguration conf = (JDBCConfiguration) ((OpenJPAEntityManagerFactorySPI) kpmf)
107: .getConfiguration();
108:
109: StringWriter sw = new StringWriter();
110:
111: SchemaTool.Flags flags = new SchemaTool.Flags();
112: flags.writer = sw;
113: flags.primaryKeys = true;
114: flags.foreignKeys = true;
115: flags.indexes = true;
116: flags.openjpaTables = true;
117: flags.action = SchemaTool.ACTION_REFLECT;
118:
119: SchemaTool.run(conf, new String[0], flags, getClass()
120: .getClassLoader());
121:
122: sw.flush();
123: String data = sw.toString();
124: assertTrue(data.length() > 0);
125: }
126: }
|