001: /*
002: * TestLazySchemaFactory.java
003: *
004: * Created on October 6, 2006, 1:47 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.util.*;
031: import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
032: import org.apache.openjpa.jdbc.schema.LazySchemaFactory;
033: import org.apache.openjpa.jdbc.schema.SchemaGroup;
034: import org.apache.openjpa.jdbc.schema.Table;
035:
036: import org.apache.openjpa.persistence.jdbc.common.apps.*;
037: import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
038:
039: import java.lang.annotation.Annotation;
040: import junit.framework.*;
041: import javax.persistence.EntityManager;
042: import javax.persistence.EntityManagerFactory;
043: import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
044: import org.apache.openjpa.persistence.OpenJPAEntityManager;
045:
046: public class TestLazySchemaFactory extends
047: org.apache.openjpa.persistence.jdbc.kernel.BaseJDBCTest {
048:
049: private SchemaGroup _group = null;
050: private boolean _fks = true;
051:
052: /** Creates a new instance of TestLazySchemaFactory */
053: public TestLazySchemaFactory() {
054: }
055:
056: public TestLazySchemaFactory(String test) {
057: super (test);
058: }
059:
060: public void setUp() throws Exception {
061: // see if the dict supports foreign keys; mysql can support them, but
062: // can't detect keys added through alter table commands, which is what
063: // kodo uses
064: JDBCConfiguration conf = (JDBCConfiguration) getConfiguration();
065: _fks = conf.getDBDictionaryInstance().supportsForeignKeys
066: && getCurrentPlatform() != AbstractTestCase.Platform.MYSQL;
067:
068: LazySchemaFactory factory = new LazySchemaFactory();
069: factory.setConfiguration(conf);
070: factory.setPrimaryKeys(true);
071: factory.setIndexes(true);
072: factory.setForeignKeys(true);
073: _group = factory;
074: }
075:
076: public void testLazySchemaFactory() {
077: // should read tables 1 and 2...
078: Table table = _group.findTable("T1");
079: assertNotNull(table);
080: Table table2 = _group.findTable("t1");
081: assertTrue(table == table2);
082:
083: int expectedColumns = 2;
084:
085: // Sybase has an extra "UNQ_INDEX" column.
086: if (getCurrentPlatform() == AbstractTestCase.Platform.SYBASE)
087: expectedColumns++;
088:
089: assertEquals(expectedColumns, table.getColumns().length);
090: if (_fks)
091: assertEquals(1, table.getForeignKeys().length);
092:
093: table2 = _group.findTable("T2");
094: assertNotNull(table2);
095: if (_fks)
096: assertTrue(table.getForeignKeys()[0].getPrimaryKeyColumns()[0]
097: .getTable() == table2);
098:
099: assertNull(table.getSchema().getTable("T3"));
100:
101: // should read table 3 only...
102: Table table3 = _group.findTable("T3");
103: assertNotNull(table3);
104: assertTrue(table.getSchema().getTable("T3") == table3);
105: if (_fks)
106: assertTrue(table3.getForeignKeys()[0]
107: .getPrimaryKeyColumns()[0].getTable() == table);
108:
109: assertNull(table3.getSchema().getTable("T4"));
110: }
111:
112: public static void main(String[] args) {
113: //main(TestLazySchemaFactory.class);
114: }
115:
116: }
|