KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > metadata > torque > TorqueForeignKeyGeneratorTest


1 package org.apache.ojb.broker.metadata.torque;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6 import junit.textui.TestRunner;
7 import org.apache.ojb.broker.metadata.DescriptorRepository;
8 import org.apache.ojb.broker.metadata.RepositoryPersistor;
9 import org.apache.ojb.broker.metadata.torque.TableDescriptor;
10 import org.apache.ojb.broker.metadata.torque.TorqueForeignKeyGenerator;
11 import org.apache.ojb.broker.TestHelper;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Vector JavaDoc;
15
16 public class TorqueForeignKeyGeneratorTest extends TestCase {
17
18     private static final String JavaDoc EXAMPLE_FILE = TestHelper.DEF_REPOSITORY;
19     private static final String JavaDoc OJB_DLIST_ENTRY_FOREIGN_KEY =
20             " <foreign-key foreignTable=\"OJB_DLIST\">\n" +
21             " <reference local=\"DLIST_ID\" foreign=\"ID\"/>\n" +
22             " </foreign-key>\n";
23     private static final String JavaDoc PERSON_PROJECT_FK_1 =
24             " <foreign-key foreignTable=\"PERSON\">\n" +
25             " <reference local=\"PERSON_ID\" foreign=\"ID\"/>\n" +
26             " </foreign-key>\n";
27
28     private static final String JavaDoc PERSON_PROJECT_FK_2 =
29             " <foreign-key foreignTable=\"PROJECT\">\n" +
30             " <reference local=\"PROJECT_ID\" foreign=\"ID\"/>\n" +
31             " </foreign-key>\n";
32     private static final String JavaDoc ARTIKEL_FK =
33             " <foreign-key foreignTable=\"Kategorien\">\n" +
34             " <reference local=\"Kategorie_Nr\" foreign=\"Kategorie_Nr\"/>\n" +
35             " </foreign-key>\n";
36     private static final String JavaDoc PAGEWORD_FK_1 =
37             " <foreign-key foreignTable=\"WORD\">\n" +
38             " <reference local=\"wordId\" foreign=\"ID\"/>\n" +
39             " </foreign-key>\n";
40     private static final String JavaDoc PAGEWORD_FK_2 =
41             " <foreign-key foreignTable=\"PAGE\">\n" +
42             " <reference local=\"pageId\" foreign=\"ID\"/>\n" +
43             " </foreign-key>\n";
44
45     private TorqueForeignKeyGenerator foreignKeyGenerator;
46
47     public TorqueForeignKeyGeneratorTest(String JavaDoc name) {
48         super(name);
49     }
50
51     public void setUp() throws Exception JavaDoc {
52         RepositoryPersistor repositoryPersistor = new RepositoryPersistor();
53         DescriptorRepository descriptorRepository = repositoryPersistor.readDescriptorRepository(EXAMPLE_FILE);
54         this.foreignKeyGenerator = new TorqueForeignKeyGenerator(descriptorRepository);
55     }
56
57     public void tearDown() throws Exception JavaDoc {
58         this.foreignKeyGenerator = null;
59     }
60
61     public void testBuildConstraintsMap() {
62         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
63         this.foreignKeyGenerator.buildConstraintsMap();
64         Vector JavaDoc foreignKeys = this.foreignKeyGenerator.getForeignKeysForTable("OJB_DLIST_ENTRIES");
65         assertNotNull(foreignKeys);
66         assertEquals(1, foreignKeys.size());
67         assertEquals(OJB_DLIST_ENTRY_FOREIGN_KEY, foreignKeys.get(0));
68
69         foreignKeys = this.foreignKeyGenerator.getForeignKeysForTable("PERSON_PROJECT");
70         assertEquals(2, foreignKeys.size());
71         assertTrue(foreignKeys.contains(PERSON_PROJECT_FK_1));
72         assertTrue(foreignKeys.contains(PERSON_PROJECT_FK_2));
73
74         foreignKeys = this.foreignKeyGenerator.getForeignKeysForTable("Artikel");
75         assertEquals(1, foreignKeys.size());
76         assertEquals(ARTIKEL_FK, foreignKeys.get(0));
77
78         HashMap JavaDoc mappingTables = this.foreignKeyGenerator.getMappingTables();
79         assertEquals(1, mappingTables.size());
80         TableDescriptor tableDescriptor = (TableDescriptor) mappingTables.values().iterator().next();
81         assertEquals("PageWords", tableDescriptor.getName());
82         foreignKeys = this.foreignKeyGenerator.getForeignKeysForTable("PageWords");
83         assertNotNull(foreignKeys);
84         assertEquals(2, foreignKeys.size());
85         assertTrue(foreignKeys.contains(PAGEWORD_FK_1));
86         assertTrue(foreignKeys.contains(PAGEWORD_FK_2));
87     }
88
89
90     public static void main(String JavaDoc[] args) {
91         TestRunner.run(suite());
92     }
93
94     public static Test suite() {
95         return new TestSuite(TorqueForeignKeyGeneratorTest.class);
96     }
97
98 }
99
Popular Tags