001: /*
002: * TestXMLSchemaParser.java
003: *
004: * Created on October 6, 2006, 2:42 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.IOException;
031: import java.sql.Types;
032: import java.util.*;
033: import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
034: import org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl;
035: import org.apache.openjpa.jdbc.schema.Column;
036: import org.apache.openjpa.jdbc.schema.ForeignKey;
037: import org.apache.openjpa.jdbc.schema.Index;
038: import org.apache.openjpa.jdbc.schema.PrimaryKey;
039: import org.apache.openjpa.jdbc.schema.Schema;
040: import org.apache.openjpa.jdbc.schema.SchemaGroup;
041: import org.apache.openjpa.jdbc.schema.Sequence;
042: import org.apache.openjpa.jdbc.schema.Table;
043: import org.apache.openjpa.jdbc.schema.XMLSchemaParser;
044:
045: import org.apache.openjpa.persistence.jdbc.common.apps.*;
046:
047: import java.lang.annotation.Annotation;
048: import junit.framework.*;
049: import javax.persistence.EntityManager;
050: import javax.persistence.EntityManagerFactory;
051: import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
052: import org.apache.openjpa.persistence.OpenJPAEntityManager;
053:
054: public class TestXMLSchemaParser extends
055: org.apache.openjpa.persistence.jdbc.kernel.BaseJDBCTest {
056:
057: protected JDBCConfiguration conf = null;
058: private SchemaGroup _group = null;
059:
060: /** Creates a new instance of TestXMLSchemaParser */
061: public TestXMLSchemaParser() {
062: }
063:
064: public TestXMLSchemaParser(String test) {
065: super (test);
066: }
067:
068: public void setUp() throws Exception {
069: this .conf = new JDBCConfigurationImpl();
070: _group = getSchemaGroup();
071: }
072:
073: /**
074: * Parses the schema group from the schema XML
075: * resources in this package.
076: */
077: protected SchemaGroup parseSchemaGroup() throws IOException {
078: XMLSchemaParser parser = new SCMSchemaParser(this .conf);
079: parser.parse(TestXMLSchemaParser.class, false);
080: parser.parse(TestSchema.class, false); // will go up to package level
081: return parser.getSchemaGroup();
082: }
083:
084: /**
085: * Return the schema group to use in testing. Returns
086: * {@link #parseSchemaGroup} by default.
087: */
088: protected SchemaGroup getSchemaGroup() throws Exception {
089: return parseSchemaGroup();
090: }
091:
092: /**
093: * Checks the generated schema group for accuracy.
094: */
095: public void testSchemaParsing() {
096: assertEquals(2, _group.getSchemas().length);
097: assertEquals("SCHEMA1", _group.getSchema("SCHEMA1").getName());
098: assertEquals("SCHEMA2", _group.getSchema("SCHEMA2").getName());
099: }
100:
101: /**
102: * Checks the pased sequences.
103: */
104: public void testSequenceParsing() {
105: Schema schema = _group.getSchema("SCHEMA1");
106: assertEquals(2, schema.getSequences().length);
107: assertEquals(0,
108: _group.getSchema("SCHEMA2").getSequences().length);
109:
110: Sequence seq1 = schema.getSequence("SEQ1");
111: assertNotNull(seq1);
112: assertEquals("SEQ1", seq1.getName());
113: assertEquals(seq1, _group.findSequence("SEQ1"));
114: assertEquals(seq1, _group.findSequence("SCHEMA1.SEQ1"));
115: assertEquals(1, seq1.getInitialValue());
116: assertEquals(1, seq1.getIncrement());
117: assertEquals(0, seq1.getAllocate());
118:
119: Sequence seq2 = schema.getSequence("SEQ2");
120: assertNotNull(seq2);
121: assertEquals(3, seq2.getInitialValue());
122: assertEquals(5, seq2.getIncrement());
123: assertEquals(50, seq2.getAllocate());
124: }
125:
126: /**
127: * Checks table and column parsing.
128: */
129: public void testTableColumnParsing() {
130: Schema schema1 = _group.getSchema("SCHEMA1");
131: Table[] tables = schema1.getTables();
132: assertEquals(2, tables.length);
133: assertEquals("TABLE1", tables[0].getName());
134: assertEquals("TABLE3", tables[1].getName());
135:
136: Column[] cols = tables[0].getColumns();
137: assertEquals(2, cols.length);
138: assertEquals("COL1", cols[0].getName());
139: assertEquals("COL2", cols[1].getName());
140: assertEquals(Types.VARCHAR, cols[0].getType());
141: assertEquals(Types.FLOAT, cols[1].getType());
142: assertTrue(cols[0].isNotNull());
143: assertTrue(!cols[1].isNotNull());
144: assertEquals("def", cols[0].getDefault());
145: assertNull(cols[1].getDefault());
146: }
147:
148: /**
149: * Test that primary keys are resolved correctly.
150: */
151: public void testPrimaryKeyParsing() {
152: Table table = _group.getSchema("SCHEMA1").getTable("TABLE1");
153: PrimaryKey pk = table.getPrimaryKey();
154: assertNotNull(pk);
155: assertEquals("PK1", pk.getName());
156: assertTrue(pk.isLogical());
157: assertEquals(1, pk.getColumns().length);
158: assertEquals(table.getColumn("COL1"), pk.getColumns()[0]);
159:
160: table = _group.getSchema("SCHEMA2").getTable("TABLE2");
161: pk = table.getPrimaryKey();
162: assertNotNull(pk);
163: assertEquals("PK2", pk.getName());
164: assertTrue(!pk.isLogical());
165: assertEquals(2, pk.getColumns().length);
166: assertEquals(table.getColumn("COL1"), pk.getColumns()[0]);
167: assertEquals(table.getColumn("COL2"), pk.getColumns()[1]);
168: }
169:
170: /**
171: * Test that indexes are resolved correctly.
172: */
173: public void testIndexParsing() {
174: Table table = _group.getSchema("SCHEMA1").getTable("TABLE1");
175: Index idx = table.getIndex("IDX1");
176: assertNotNull(idx);
177: assertTrue(idx.isUnique());
178: assertEquals(1, idx.getColumns().length);
179: assertEquals(table.getColumn("COL2"), idx.getColumns()[0]);
180:
181: table = _group.getSchema("SCHEMA2").getTable("TABLE2");
182: idx = table.getIndex("IDX2");
183: assertNotNull(idx);
184: assertTrue(!idx.isUnique());
185: assertEquals(2, idx.getColumns().length);
186: assertEquals(table.getColumn("COL1"), idx.getColumns()[0]);
187: assertEquals(table.getColumn("COL2"), idx.getColumns()[1]);
188: }
189:
190: /**
191: * Test that foreign keys are resolved correctly.
192: */
193: public void testForeignKeyParsing() {
194: Table table1 = _group.getSchema("SCHEMA1").getTable("TABLE1");
195: Table table2 = _group.getSchema("SCHEMA2").getTable("TABLE2");
196:
197: ForeignKey fk = table1.getForeignKeys()[0];
198: assertEquals("FK1", fk.getName());
199: assertNotNull(fk);
200: assertEquals(ForeignKey.ACTION_RESTRICT, fk.getDeleteAction());
201: Column[] cols = fk.getColumns();
202: Column[] pkCols = fk.getPrimaryKeyColumns();
203: assertEquals(2, cols.length);
204: assertEquals(2, pkCols.length);
205: assertEquals(table1.getColumn("COL1"), cols[0]);
206: assertEquals(table2.getColumn("COL1"), pkCols[0]);
207: assertEquals(table1.getColumn("COL2"), cols[1]);
208: assertEquals(table2.getColumn("COL2"), pkCols[1]);
209:
210: fk = table2.getForeignKeys()[0];
211: assertEquals("FK2", fk.getName());
212: assertNotNull(fk);
213: assertEquals(ForeignKey.ACTION_NONE, fk.getDeleteAction());
214: cols = fk.getColumns();
215: pkCols = fk.getPrimaryKeyColumns();
216: assertEquals(1, cols.length);
217: assertEquals(1, pkCols.length);
218: assertEquals(table2.getColumn("COL2"), cols[0]);
219: assertEquals(table1.getColumn("COL1"), pkCols[0]);
220: }
221:
222: public static void main(String[] args) {
223: //main(TestXMLSchemaParser.class);
224: }
225:
226: public static class SCMSchemaParser extends XMLSchemaParser {
227:
228: public SCMSchemaParser(JDBCConfiguration conf) {
229: super (conf);
230: setSuffix(".scm");
231: }
232: }
233:
234: }
|