001: /*
002: * TestSchema.java
003: *
004: * Created on October 6, 2006, 2:36 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.schema.Column;
032: import org.apache.openjpa.jdbc.schema.ForeignKey;
033: import org.apache.openjpa.jdbc.schema.Index;
034: import org.apache.openjpa.jdbc.schema.PrimaryKey;
035: import org.apache.openjpa.jdbc.schema.Schema;
036: import org.apache.openjpa.jdbc.schema.SchemaGroup;
037: import org.apache.openjpa.jdbc.schema.Table;
038:
039: import org.apache.openjpa.persistence.jdbc.common.apps.*;
040:
041: import java.lang.annotation.Annotation;
042: import junit.framework.*;
043: import javax.persistence.EntityManager;
044: import javax.persistence.EntityManagerFactory;
045: import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
046: import org.apache.openjpa.persistence.OpenJPAEntityManager;
047:
048: public class TestSchema extends
049: org.apache.openjpa.persistence.jdbc.kernel.BaseJDBCTest {
050:
051: private Schema _schema = new SchemaGroup().addSchema("schema");
052:
053: /** Creates a new instance of TestSchema */
054: public TestSchema() {
055: }
056:
057: public TestSchema(String test) {
058: super (test);
059: }
060:
061: /**
062: * Test the {@link Schema} class.
063: */
064: public void testSchema() {
065: assertEquals("schema", _schema.getName());
066: assertTrue(_schema.getSchemaGroup().isNameTaken("schema"));
067:
068: assertTrue(!_schema.getSchemaGroup().isNameTaken("table1"));
069: Table table1 = _schema.addTable("table1");
070: assertTrue(_schema.getSchemaGroup().isNameTaken("table1"));
071: Table table2 = _schema.addTable("table2");
072: assertTrue(_schema.getSchemaGroup().isNameTaken("table2"));
073:
074: Table[] tables = _schema.getTables();
075: assertEquals(2, tables.length);
076: assertEquals(table1, tables[0]);
077: assertEquals(table2, tables[1]);
078: assertEquals(table1, _schema.getTable("table1"));
079: assertEquals(table2, _schema.getTable("table2"));
080: assertNull(_schema.getTable("table3"));
081:
082: assertTrue(_schema.removeTable(table1));
083: assertTrue(!_schema.getSchemaGroup().isNameTaken("table1"));
084: assertNull(_schema.getTable("table1"));
085: assertEquals(table2, _schema.getTable("table2"));
086: assertTrue(_schema.removeTable(table2));
087: assertTrue(!_schema.getSchemaGroup().isNameTaken("table2"));
088: assertNull(_schema.getTable("table2"));
089: assertEquals(0, _schema.getTables().length);
090: }
091:
092: /**
093: * Test the {@link Table} class.
094: */
095: public void testTable() {
096: Table table = _schema.addTable("table");
097: assertEquals(_schema, table.getSchema());
098: assertEquals("table", table.getName());
099:
100: assertTrue(!table.isNameTaken("schema"));
101: assertTrue(table.isNameTaken("table"));
102:
103: // pk testing
104: assertNull(table.getPrimaryKey());
105: PrimaryKey pk = table.addPrimaryKey("pk");
106: assertEquals(table, pk.getTable());
107: assertEquals(pk, table.getPrimaryKey());
108: assertTrue(!table.isNameTaken("pk"));
109: assertTrue(_schema.getSchemaGroup().isNameTaken("pk"));
110:
111: table.removePrimaryKey();
112: assertNull(table.getPrimaryKey());
113: assertTrue(!_schema.getSchemaGroup().isNameTaken("pk"));
114:
115: // column testing
116: Column c2 = table.addColumn("c2");
117: Column c1 = table.addColumn("c1");
118: assertTrue(table.isNameTaken("c1"));
119: assertTrue(!_schema.getSchemaGroup().isNameTaken("c1"));
120: assertEquals(table, c1.getTable());
121: Column[] cols = table.getColumns();
122: assertEquals(2, cols.length);
123: assertEquals(c2, cols[0]);
124: assertEquals(c1, cols[1]);
125:
126: assertEquals(c1, table.getColumn("c1"));
127: assertEquals(c2, table.getColumn("c2"));
128:
129: assertTrue(table.removeColumn(c1));
130: assertTrue(!table.isNameTaken("c1"));
131: assertNull(table.getColumn("c1"));
132:
133: // foreign key testing
134: ForeignKey fk = table.addForeignKey("fk");
135: assertTrue(_schema.getSchemaGroup().isNameTaken("fk"));
136: assertTrue(!table.isNameTaken("fk"));
137: assertEquals(table, fk.getTable());
138: ForeignKey[] fks = table.getForeignKeys();
139: assertEquals(1, fks.length);
140: assertEquals(fk, fks[0]);
141:
142: assertTrue(table.removeForeignKey(fk));
143: assertTrue(!_schema.getSchemaGroup().isNameTaken("fk"));
144: assertEquals(0, table.getForeignKeys().length);
145:
146: // index testing
147: Index idx = table.addIndex("idx");
148: assertTrue(_schema.getSchemaGroup().isNameTaken("idx"));
149: assertTrue(!table.isNameTaken("idx"));
150: assertEquals(table, idx.getTable());
151: Index[] idxs = table.getIndexes();
152: assertEquals(1, idxs.length);
153: assertEquals(idx, idxs[0]);
154:
155: assertEquals(idx, table.getIndex("idx"));
156: assertTrue(table.removeIndex(idx));
157: assertTrue(!table.isNameTaken("idx"));
158: assertNull(table.getIndex("idx"));
159: }
160:
161: /**
162: * Test the {@link Index} class.
163: */
164: public void testIndex() {
165: Table table = _schema.addTable("table");
166: Column c1 = table.addColumn("c1");
167: Column c2 = table.addColumn("c2");
168: Table table2 = _schema.addTable("table2");
169: Column c3 = table2.addColumn("c3");
170:
171: Index idx = table.addIndex("idx");
172: try {
173: idx.addColumn(c3);
174: fail("Allowed addition of column of another table.");
175: } catch (RuntimeException re) {
176: }
177:
178: Column[] cols = idx.getColumns();
179: assertEquals(0, cols.length);
180:
181: idx.addColumn(c1);
182: idx.addColumn(c2);
183: cols = idx.getColumns();
184: assertEquals(2, cols.length);
185: assertEquals(c1, cols[0]);
186: assertEquals(c2, cols[1]);
187:
188: assertTrue(idx.removeColumn(c1));
189: cols = idx.getColumns();
190: assertEquals(1, cols.length);
191: assertEquals(c2, cols[0]);
192: assertTrue(idx.removeColumn(c2));
193: cols = idx.getColumns();
194: assertEquals(0, cols.length);
195:
196: assertTrue(!idx.isUnique());
197: idx.setUnique(true);
198: assertTrue(idx.isUnique());
199: }
200:
201: /**
202: * Test the {@link ForeignKey} class.
203: */
204: public void testForeignKey() {
205: Table table = _schema.addTable("table");
206: Column c1 = table.addColumn("c1");
207: Column c2 = table.addColumn("c2");
208: Table table2 = _schema.addTable("table2");
209: Column c3 = table2.addColumn("c3");
210: Column c4 = table2.addColumn("c4");
211:
212: ForeignKey fk = table.addForeignKey("fk");
213: try {
214: fk.join(c4, c2);
215: fail("Allowed addition of column of another table.");
216: } catch (RuntimeException re) {
217: }
218:
219: Column[] cols = fk.getColumns();
220: Column[] pkCols = fk.getPrimaryKeyColumns();
221: assertEquals(0, cols.length);
222: assertEquals(0, pkCols.length);
223: PrimaryKey pk = table2.addPrimaryKey("pk");
224: pk.addColumn(c3);
225: fk.join(c1, c3);
226: pk.addColumn(c4);
227: fk.join(c2, c4);
228: cols = fk.getColumns();
229: pkCols = fk.getPrimaryKeyColumns();
230: assertEquals(2, cols.length);
231: assertEquals(c1, cols[0]);
232: assertEquals(c2, cols[1]);
233: assertEquals(c3, pkCols[0]);
234: assertEquals(c4, pkCols[1]);
235:
236: assertTrue(fk.removeJoin(c1));
237: cols = fk.getColumns();
238: pkCols = fk.getPrimaryKeyColumns();
239: assertEquals(1, cols.length);
240: assertEquals(1, pkCols.length);
241: assertEquals(c2, cols[0]);
242: assertEquals(c4, pkCols[0]);
243: assertTrue(fk.removeJoin(c2));
244: cols = fk.getColumns();
245: pkCols = fk.getPrimaryKeyColumns();
246: assertEquals(0, cols.length);
247: assertEquals(0, pkCols.length);
248:
249: assertEquals(ForeignKey.ACTION_NONE, fk.getDeleteAction());
250: assertTrue(fk.isLogical());
251: fk.setDeleteAction(ForeignKey.ACTION_RESTRICT);
252: assertEquals(ForeignKey.ACTION_RESTRICT, fk.getDeleteAction());
253: assertFalse(fk.isLogical());
254: }
255:
256: /**
257: * Tests the {@link SchemaGroup} class.
258: */
259: public void testSchemaGroup() {
260: SchemaGroup group = _schema.getSchemaGroup();
261: assertEquals(_schema, group.getSchema("schema"));
262: Table foo1 = _schema.addTable("foo");
263:
264: Schema schema2 = group.addSchema("schema2");
265: assertNull(schema2.getTable("foo"));
266: Table foo2 = schema2.addTable("foo");
267: assertEquals(foo2, schema2.getTable("foo"));
268: assertEquals(foo1, _schema.getTable("foo"));
269:
270: assertEquals(foo1, group.findTable("schema.foo"));
271: assertEquals(foo2, group.findTable("schema2.foo"));
272: }
273:
274: /**
275: * Test primary key removal.
276: */
277: public void testPrimaryKeyRemoval() {
278: Table table = _schema.addTable("table");
279: Column c1 = table.addColumn("c1");
280: Column c2 = table.addColumn("c2");
281: Table table2 = _schema.addTable("table2");
282: Column c3 = table2.addColumn("c3");
283: Column c4 = table2.addColumn("c4");
284: PrimaryKey pk = table2.addPrimaryKey("pk");
285: pk.addColumn(c3);
286: pk.addColumn(c4);
287: ForeignKey fk = table.addForeignKey("fk");
288: fk.join(c1, c3);
289: fk.join(c2, c4);
290:
291: table2.removePrimaryKey();
292: assertNull(pk.getTable());
293: assertNull(table2.getPrimaryKey());
294: assertEquals(0, table.getForeignKeys().length);
295: }
296:
297: /**
298: * Test column removal.
299: */
300: public void testColumnRemoval() {
301: Table table = _schema.addTable("table");
302: Column c1 = table.addColumn("c1");
303: Column c2 = table.addColumn("c2");
304: PrimaryKey pk = table.addPrimaryKey("pk");
305: pk.addColumn(c1);
306: Index idx1 = table.addIndex("idx1");
307: idx1.addColumn(c1);
308: Index idx2 = table.addIndex("idx2");
309: idx2.addColumn(c1);
310: idx2.addColumn(c2);
311:
312: Table table2 = _schema.addTable("table2");
313: Column c3 = table2.addColumn("c3");
314: Column c4 = table2.addColumn("c4");
315: pk = table2.addPrimaryKey("pk2");
316: pk.addColumn(c3);
317: ForeignKey fk = table.addForeignKey("fk");
318: fk.join(c1, c3);
319:
320: table.removeColumn(c1);
321: assertNull(table.getPrimaryKey());
322: assertNull(table.getIndex("idx1"));
323: assertEquals(1, idx2.getColumns().length);
324: assertEquals(0, table.getForeignKeys().length);
325: }
326:
327: public static void main(String[] args) {
328: //main(TestSchema.class);
329: }
330:
331: }
|