1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.addam.ddlgen.generic;
20
21 import com.mockrunner.mock.jdbc.MockDatabaseMetaData;
22 import com.mockrunner.mock.jdbc.MockResultSet;
23 import junit.framework.Assert;
24 import junit.framework.TestCase;
25 import net.sourceforge.addam.ddlgen.generic.ForeignKey;
26 import net.sourceforge.addam.ddlgen.generic.ForeignKeyGenerator;
27
28 import org.apache.velocity.VelocityContext;
29
30 import java.util.Collection;
31
32 /**
33 * User: mkrishna
34 * Date: Nov 17, 2004
35 */
36
37 public class ForeignKeyGeneratorUTEST extends TestCase {
38
39 public void testForeignKeyDDLGeneratorConstructsForeignKeyObjectsProperly() throws Exception {
40 MockResultSet resultset = new MockResultSet("getImportedKeys");
41 Object[] foreignKeyInfo1 = new Object[]{"PKTABLE_CAT1 ", "PKTABLE_SCHEM1 ", "PKTABLE_NAME1 ", "PKCOLUMN_NAME1 ",
42 "FKTABLE_CAT1 ", "FKTABLE_SCHEM1 ", "FKTABLE_NAME1 ", "FKCOLUMN_NAME1 "
43 , "KEY_SEQ1 ", "UPDATE_RULE1 ", "DELETE_RULE1 ", "FK_NAME1 ", "PK_NAME1 ", "DEFERRABILITY1 "};
44 Object[] foreignKeyInfo2 = new Object[]{"PKTABLE_CAT2 ", "PKTABLE_SCHEM2 ", "PKTABLE_NAME2 ", "PKCOLUMN_NAME2 ",
45 "FKTABLE_CAT2 ", "FKTABLE_SCHEM2 ", "FKTABLE_NAME2 ", "FKCOLUMN_NAME2 "
46 , "KEY_SEQ2 ", "UPDATE_RULE2 ", "DELETE_RULE2 ", "FK_NAME2 ", "PK_NAME2 ", "DEFERRABILITY2 "};
47 Object[] foreignKeyInfo3 = new Object[]{"PKTABLE_CAT3 ", "PKTABLE_SCHEM3 ", "PKTABLE_NAME3 ", "PKCOLUMN_NAME3 ",
48 "FKTABLE_CAT3 ", "FKTABLE_SCHEM3 ", "FKTABLE_NAME3 ", "FKCOLUMN_NAME3 "
49 , "KEY_SEQ3 ", "UPDATE_RULE3 ", "DELETE_RULE3 ", "FK_NAME3 ", "PK_NAME3 ", "DEFERRABILITY3 "};
50
51 resultset.addRow(foreignKeyInfo1);
52 resultset.addRow(foreignKeyInfo2);
53
54 MockDatabaseMetaData metadata = new MockDatabaseMetaData();
55 metadata.setImportedKeys(resultset);
56 net.sourceforge.addam.ddlgen.generic.ForeignKeyGenerator generator = new ForeignKeyGenerator();
57 VelocityContext context = generator.getContext(null, null, "testforeignKey", null, metadata);
58 Collection<ForeignKey> foreignKeys = (Collection<ForeignKey>)context.get("foreignKeys");
59 Assert.assertEquals(2, foreignKeys.size());
60
61 assertForeignKeyDDL(foreignKeys, "FKCOLUMN_NAME1 ", "FK_NAME1 ", "FKTABLE_NAME1 ", "PKCOLUMN_NAME1 ", "PKTABLE_NAME1 ");
62 assertForeignKeyDDL(foreignKeys, "FKCOLUMN_NAME2 ", "FK_NAME2 ", "FKTABLE_NAME2 ", "PKCOLUMN_NAME2 ", "PKTABLE_NAME2 ");
63
64
65 resultset = new MockResultSet("test");
66 resultset.addRow(foreignKeyInfo1);
67 resultset.addRow(foreignKeyInfo2);
68 resultset.addRow(foreignKeyInfo3);
69 metadata.setImportedKeys(resultset);
70 context = generator.getContext(null, null, "testforeignKey", null, metadata);
71 foreignKeys = (Collection<ForeignKey>)context.get("foreignKeys");
72 Assert.assertEquals(3, foreignKeys.size());
73 assertForeignKeyDDL(foreignKeys, "FKCOLUMN_NAME3 ", "FK_NAME3 ", "FKTABLE_NAME3 ", "PKCOLUMN_NAME3 ", "PKTABLE_NAME3 ");
74 }
75
76 public void testForeignKeyDDLGeneratorConstructsForeignKeyObjectsProperlyForCompositeKeys() throws Exception {
77 MockResultSet resultset = new MockResultSet("getImportedKeys");
78 Object[] foreignKeyInfo1 = new Object[]{"PKTABLE_CAT1 ", "PKTABLE_SCHEM1 ", "PKTABLE_NAME1 ", "PKCOLUMN_NAME1 ",
79 "FKTABLE_CAT1 ", "FKTABLE_SCHEM1 ", "FKTABLE_NAME1 ", "FKCOLUMN_NAME1 "
80 , "KEY_SEQ1 ", "UPDATE_RULE1 ", "DELETE_RULE1 ", "FK_NAME1 ", "PK_NAME1 ", "DEFERRABILITY1 "};
81 Object[] foreignKeyInfo2 = new Object[]{"PKTABLE_CAT1 ", "PKTABLE_SCHEM1 ", "PKTABLE_NAME1 ", "PKCOLUMN_NAME2 ",
82 "FKTABLE_CAT1 ", "FKTABLE_SCHEM1 ", "FKTABLE_NAME1 ", "FKCOLUMN_NAME2 "
83 , "KEY_SEQ1 ", "UPDATE_RULE1 ", "DELETE_RULE1 ", "FK_NAME1 ", "PK_NAME1 ", "DEFERRABILITY1 "};
84 resultset.addRow(foreignKeyInfo1);
85 resultset.addRow(foreignKeyInfo2);
86
87 MockDatabaseMetaData metadata = new MockDatabaseMetaData();
88 metadata.setImportedKeys(resultset);
89 net.sourceforge.addam.ddlgen.generic.ForeignKeyGenerator generator = new ForeignKeyGenerator();
90 VelocityContext context = generator.getContext(null, null, "testforeignKey", null, metadata);
91 Collection foreignKeys = (Collection<ForeignKey>)context.get("foreignKeys");
92 Assert.assertEquals(1, foreignKeys.size());
93
94 ForeignKey foreignKey = (ForeignKey) foreignKeys.iterator().next();
95 assertEquals("FK_NAME1 ", foreignKey.getForeignKeyName());
96 assertEquals("FKTABLE_NAME1 ", foreignKey.getForeignKeyTableName());
97 assertEquals("PKTABLE_NAME1 ", foreignKey.getPrimaryKeyTable());
98 assertEquals(1, foreignKey.getForeignKeyColumns().size());
99 assertEquals("FKCOLUMN_NAME1 ", foreignKey.getForeignKeyColumns().get(0));
100 assertEquals("FKCOLUMN_NAME2 ", foreignKey.getForeignKeyColumns().get(1));
101 assertEquals(1, foreignKey.getPrimaryKeyColumns().size());
102 assertEquals("PKCOLUMN_NAME1 ", foreignKey.getPrimaryKeyColumns().get(0));
103 assertEquals("PKCOLUMN_NAME2 ", foreignKey.getPrimaryKeyColumns().get(1));
104 }
105
106
107 private void assertForeignKeyDDL(Collection<ForeignKey> foreignKeys, String fkColName, String fkName, String fkTabName, String pkColName, String pkTabName) {
108 boolean contains = false;
109 for (ForeignKey foreignKey : foreignKeys) {
110 if (verifyForeignKeyDDL(fkColName, fkName, fkTabName, pkColName, pkTabName, foreignKey)) {
111 contains = true;
112 break;
113 }
114 }
115 assertTrue(contains);
116 }
117
118 private boolean verifyForeignKeyDDL(String fkColName, String fkName, String fkTabName, String pkColName, String pkTabName, ForeignKey foreignKey) {
119 return fkColName.equals(foreignKey.getForeignKeyColumns().get(0)) &&
120 fkName.equals(foreignKey.getForeignKeyName()) &&
121 fkTabName.equals(foreignKey.getForeignKeyTableName()) &&
122 pkColName.equals(foreignKey.getPrimaryKeyColumns().get(0)) &&
123 pkTabName.equals(foreignKey.getPrimaryKeyTable());
124
125 }
126 }