KickJava   Java API By Example, From Geeks To Geeks.

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


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.ClassDescriptor;
8 import org.apache.ojb.broker.metadata.DescriptorRepository;
9 import org.apache.ojb.broker.metadata.FieldDescriptor;
10 import org.apache.ojb.broker.metadata.RepositoryPersistor;
11 import org.apache.ojb.broker.metadata.torque.TableDescriptor;
12 import org.apache.ojb.broker.metadata.torque.TorqueTableGenerator;
13 import org.apache.ojb.broker.TestHelper;
14
15 public class TorqueTableGeneratorTest extends TestCase {
16
17     private static final String JavaDoc EXAMPLE_FILE = TestHelper.DEF_REPOSITORY;
18     private static final String JavaDoc EMPTY_STANDARD_TABLE =
19             " <table name=\"null\" indexTablespace=\"JUnit indx\">\n" +
20             " </table>\n\n";
21     private static final String JavaDoc PERSON_PROJECT_TABLE =
22             " <table name=\"PERSON_PROJECT\" indexTablespace=\"JUnit indx\">\n" +
23             " <column name=\"Kategorie_Nr\" required=\"false\" autoIncrement=\"false\" primaryKey=\"true\" type=\"INTEGER\"/>\n" +
24             " <column name=\"KategorieName\" required=\"false\" autoIncrement=\"false\" primaryKey=\"false\" type=\"VARCHAR\"/>\n" +
25             " <column name=\"Beschreibung\" required=\"false\" autoIncrement=\"false\" primaryKey=\"false\" type=\"VARCHAR\"/>\n" +
26             " <foreign-key foreignTable=\"PROJECT\">\n" +
27             " <reference local=\"PROJECT_ID\" foreign=\"ID\"/>\n" +
28             " </foreign-key>\n" +
29             " <foreign-key foreignTable=\"PERSON\">\n" +
30             " <reference local=\"PERSON_ID\" foreign=\"ID\"/>\n" +
31             " </foreign-key>\n" +
32             " </table>\n\n";
33
34     private static final String JavaDoc MAPPING_TABLES =
35             " <table name=\"PageWords\" indexTablespace=\"JUnit indx\">\n" +
36             " <column name=\"ID\" required=\"true\" autoIncrement=\"false\" primaryKey=\"true\" type=\"INTEGER\"/>\n" +
37             " <foreign-key foreignTable=\"WORD\">\n" +
38             " <reference local=\"wordId\" foreign=\"ID\"/>\n" +
39             " </foreign-key>\n" +
40             " <foreign-key foreignTable=\"PAGE\">\n" +
41             " <reference local=\"pageId\" foreign=\"ID\"/>\n" +
42             " </foreign-key>\n" +
43             " </table>\n";
44
45     private TorqueTableGenerator torqueTableGenerator;
46     private DescriptorRepository repository;
47
48     public TorqueTableGeneratorTest(String JavaDoc name) {
49         super(name);
50     }
51
52     public void setUp() throws Exception JavaDoc {
53         RepositoryPersistor repositoryPersistor = new RepositoryPersistor();
54         this.repository = repositoryPersistor.readDescriptorRepository(EXAMPLE_FILE);
55         this.torqueTableGenerator = new TorqueTableGenerator(this.repository, false);
56     }
57
58     public void tearDown() throws Exception JavaDoc {
59         this.torqueTableGenerator = null;
60     }
61
62     public void testGenerateStandardTables() {
63         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
64         TableDescriptor tableDescriptor = new TableDescriptor();
65         this.torqueTableGenerator.generateStandardTable(tableDescriptor, buffer, "JUnit indx");
66         assertEquals(EMPTY_STANDARD_TABLE, buffer.toString());
67
68         buffer = new StringBuffer JavaDoc();
69         tableDescriptor.setName("PERSON_PROJECT");
70         ClassDescriptor classDescriptor = this.repository.getDescriptorFor(org.apache.ojb.odmg.shared.ProductGroup.class);
71         FieldDescriptor fieldDescriptors[] = classDescriptor.getFieldDescriptions();
72         for (int i = 0; i < fieldDescriptors.length; i++) {
73             tableDescriptor.addColumn(fieldDescriptors[i]);
74         }
75         tableDescriptor.setIndices(classDescriptor.getIndexes());
76         tableDescriptor.getReferences().addAll(classDescriptor.getObjectReferenceDescriptors());
77
78         this.torqueTableGenerator.generateStandardTable(tableDescriptor, buffer, "JUnit indx");
79         assertEquals(PERSON_PROJECT_TABLE, buffer.toString());
80     }
81
82     public void testGenerateMappingTables() {
83         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
84         TableDescriptor tableDescriptor = new TableDescriptor();
85         this.torqueTableGenerator.generateMappingTables(buffer, "JUnit indx");
86         assertEquals(MAPPING_TABLES, buffer.toString());
87     }
88
89     public static void main(String JavaDoc[] args) {
90         TestRunner.run(suite());
91     }
92
93     public static Test suite() {
94         return new TestSuite(TorqueTableGeneratorTest.class);
95     }
96
97 }
98
Popular Tags