KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > ojb > tests > ReferenceTagDatabaseForeignkeyAttributeTests


1 package xdoclet.modules.ojb.tests;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /**
19  * Tests for the ojb.reference tag with the database-foreignkey attribute
20  *
21  * @author <a HREF="mailto:tomdz@apache.org">Thomas Dudziak</a>
22  */

23 public class ReferenceTagDatabaseForeignkeyAttributeTests extends OjbTestBase
24 {
25     public ReferenceTagDatabaseForeignkeyAttributeTests(String JavaDoc name)
26     {
27         super(name);
28     }
29
30     // Test: foreignkey attribute specified, primary key of referenced type is anonymous
31
public void testForeignkey1()
32     {
33         addClass(
34             "test.A",
35             "package test;\n"+
36             "/** @ojb.class */\n"+
37             "public class A {\n"+
38             " /** @ojb.field */\n"+
39             " private int attrKey;\n"+
40             " /** @ojb.reference foreignkey=\"attrKey\"\n"+
41             " * database-foreignkey=\"false\"\n"+
42             " */\n"+
43             " private test.B attr;\n"+
44             "}\n");
45         addClass(
46             "test.B",
47             "package test;\n"+
48             "/** @ojb.class\n" +
49             " * @ojb.field name=\"id\"\n"+
50             " * jdbc-type=\"INTEGER\"\n"+
51             " * primarykey=\"true\"\n"+
52             " */\n"+
53             "public class B {}\n");
54
55         assertEqualsOjbDescriptorFile(
56             "<class-descriptor\n"+
57             " class=\"test.A\"\n"+
58             " table=\"A\"\n"+
59             ">\n"+
60             " <field-descriptor\n"+
61             " name=\"attrKey\"\n"+
62             " column=\"attrKey\"\n"+
63             " jdbc-type=\"INTEGER\"\n"+
64             " >\n"+
65             " </field-descriptor>\n"+
66             " <reference-descriptor\n"+
67             " name=\"attr\"\n"+
68             " class-ref=\"test.B\"\n"+
69             " >\n"+
70             " <foreignkey field-ref=\"attrKey\"/>\n"+
71             " </reference-descriptor>\n"+
72             "</class-descriptor>\n"+
73             "<class-descriptor\n"+
74             " class=\"test.B\"\n"+
75             " table=\"B\"\n"+
76             ">\n"+
77             " <field-descriptor\n"+
78             " name=\"id\"\n"+
79             " column=\"id\"\n"+
80             " jdbc-type=\"INTEGER\"\n"+
81             " primarykey=\"true\"\n"+
82             " access=\"anonymous\"\n"+
83             " >\n"+
84             " </field-descriptor>\n"+
85             "</class-descriptor>",
86             runOjbXDoclet(OJB_DEST_FILE));
87         assertEqualsTorqueSchemaFile(
88             "<database name=\"ojbtest\">\n"+
89             " <table name=\"A\">\n"+
90             " <column name=\"attrKey\"\n"+
91             " javaName=\"attrKey\"\n"+
92             " type=\"INTEGER\"\n"+
93             " />\n"+
94             " </table>\n"+
95             " <table name=\"B\">\n"+
96             " <column name=\"id\"\n"+
97             " javaName=\"id\"\n"+
98             " type=\"INTEGER\"\n"+
99             " primaryKey=\"true\"\n"+
100             " required=\"true\"\n"+
101             " />\n"+
102             " </table>\n"+
103             "</database>",
104             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
105     }
106
107     // Test: two foreignkey attributes specified whose types match the ordered primarykeys of the referenced type
108
public void testForeignkey2()
109     {
110         addClass(
111             "test.A",
112             "package test;\n"+
113             "/** @ojb.class */\n"+
114             "public class A {\n"+
115             " /** @ojb.field */\n"+
116             " private int attrKeyA;\n"+
117             " /** @ojb.field */\n"+
118             " private String attrKeyB;\n"+
119             " /** @ojb.reference foreignkey=\"attrKeyB,attrKeyA\"\n"+
120             " * database-foreignkey=\"false\"\n"+
121             " */\n"+
122             " private test.B attr;\n"+
123             "}\n");
124         addClass(
125             "test.B",
126             "package test;\n"+
127             "/** @ojb.class */\n"+
128             "public class B {\n"+
129             " /** @ojb.field primarykey=\"true\"\n"+
130             " * id=\"2\"\n"+
131             " */\n"+
132             " private int idA;\n"+
133             " /** @ojb.field primarykey=\"true\"\n"+
134             " * id=\"1\"\n"+
135             " */\n"+
136             " private String idB;\n"+
137             "}\n");
138
139         assertEqualsOjbDescriptorFile(
140             "<class-descriptor\n"+
141             " class=\"test.A\"\n"+
142             " table=\"A\"\n"+
143             ">\n"+
144             " <field-descriptor\n"+
145             " name=\"attrKeyA\"\n"+
146             " column=\"attrKeyA\"\n"+
147             " jdbc-type=\"INTEGER\"\n"+
148             " >\n"+
149             " </field-descriptor>\n"+
150             " <field-descriptor\n"+
151             " name=\"attrKeyB\"\n"+
152             " column=\"attrKeyB\"\n"+
153             " jdbc-type=\"VARCHAR\"\n"+
154             " length=\"254\"\n"+
155             " >\n"+
156             " </field-descriptor>\n"+
157             " <reference-descriptor\n"+
158             " name=\"attr\"\n"+
159             " class-ref=\"test.B\"\n"+
160             " >\n"+
161             " <foreignkey field-ref=\"attrKeyB\"/>\n"+
162             " <foreignkey field-ref=\"attrKeyA\"/>\n"+
163             " </reference-descriptor>\n"+
164             "</class-descriptor>\n"+
165             "<class-descriptor\n"+
166             " class=\"test.B\"\n"+
167             " table=\"B\"\n"+
168             ">\n"+
169             " <field-descriptor\n"+
170             " name=\"idB\"\n"+
171             " column=\"idB\"\n"+
172             " jdbc-type=\"VARCHAR\"\n"+
173             " primarykey=\"true\"\n"+
174             " length=\"254\"\n"+
175             " >\n"+
176             " </field-descriptor>\n"+
177             " <field-descriptor\n"+
178             " name=\"idA\"\n"+
179             " column=\"idA\"\n"+
180             " jdbc-type=\"INTEGER\"\n"+
181             " primarykey=\"true\"\n"+
182             " >\n"+
183             " </field-descriptor>\n"+
184             "</class-descriptor>",
185             runOjbXDoclet(OJB_DEST_FILE));
186         assertEqualsTorqueSchemaFile(
187             "<database name=\"ojbtest\">\n"+
188             " <table name=\"A\">\n"+
189             " <column name=\"attrKeyA\"\n"+
190             " javaName=\"attrKeyA\"\n"+
191             " type=\"INTEGER\"\n"+
192             " />\n"+
193             " <column name=\"attrKeyB\"\n"+
194             " javaName=\"attrKeyB\"\n"+
195             " type=\"VARCHAR\"\n"+
196             " size=\"254\"\n"+
197             " />\n"+
198             " </table>\n"+
199             " <table name=\"B\">\n"+
200             " <column name=\"idB\"\n"+
201             " javaName=\"idB\"\n"+
202             " type=\"VARCHAR\"\n"+
203             " primaryKey=\"true\"\n"+
204             " required=\"true\"\n"+
205             " size=\"254\"\n"+
206             " />\n"+
207             " <column name=\"idA\"\n"+
208             " javaName=\"idA\"\n"+
209             " type=\"INTEGER\"\n"+
210             " primaryKey=\"true\"\n"+
211             " required=\"true\"\n"+
212             " />\n"+
213             " </table>\n"+
214             "</database>",
215             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
216     }
217
218     // Test: two foreignkey attributes specified and referenced type is an interface with a subclass
219
public void testForeignkey3()
220     {
221         addClass(
222             "test.A",
223             "package test;\n"+
224             "/** @ojb.class */\n"+
225             "public class A {\n"+
226             " /** @ojb.field */\n"+
227             " private int attrKeyA;\n"+
228             " /** @ojb.field */\n"+
229             " private String attrKeyB;\n"+
230             " /** @ojb.reference foreignkey=\"attrKeyA,attrKeyB\"\n"+
231             " * database-foreignkey=\"true\"\n"+
232             " */\n"+
233             " private test.B attr;\n"+
234             "}\n");
235         addClass(
236             "test.B",
237             "package test;\n"+
238             "/** @ojb.class generate-table-info=\"false\" */\n"+
239             "public interface B {}\n");
240         addClass(
241             "test.C",
242             "package test;\n"+
243             "/** @ojb.class */\n"+
244             "public class C implements B {\n"+
245             " /** @ojb.field primarykey=\"true\" */\n"+
246             " private int idA;\n"+
247             " /** @ojb.field primarykey=\"true\" */\n"+
248             " private String idB;\n"+
249             "}\n");
250         
251         assertEqualsOjbDescriptorFile(
252             "<class-descriptor\n"+
253             " class=\"test.A\"\n"+
254             " table=\"A\"\n"+
255             ">\n"+
256             " <field-descriptor\n"+
257             " name=\"attrKeyA\"\n"+
258             " column=\"attrKeyA\"\n"+
259             " jdbc-type=\"INTEGER\"\n"+
260             " >\n"+
261             " </field-descriptor>\n"+
262             " <field-descriptor\n"+
263             " name=\"attrKeyB\"\n"+
264             " column=\"attrKeyB\"\n"+
265             " jdbc-type=\"VARCHAR\"\n"+
266             " length=\"254\"\n"+
267             " >\n"+
268             " </field-descriptor>\n"+
269             " <reference-descriptor\n"+
270             " name=\"attr\"\n"+
271             " class-ref=\"test.B\"\n"+
272             " >\n"+
273             " <foreignkey field-ref=\"attrKeyA\"/>\n"+
274             " <foreignkey field-ref=\"attrKeyB\"/>\n"+
275             " </reference-descriptor>\n"+
276             "</class-descriptor>\n"+
277             "<class-descriptor\n"+
278             " class=\"test.B\"\n"+
279             ">\n"+
280             " <extent-class class-ref=\"test.C\"/>\n"+
281             " <field-descriptor\n"+
282             " name=\"idA\"\n"+
283             " column=\"idA\"\n"+
284             " jdbc-type=\"INTEGER\"\n"+
285             " primarykey=\"true\"\n"+
286             " >\n"+
287             " </field-descriptor>\n"+
288             " <field-descriptor\n"+
289             " name=\"idB\"\n"+
290             " column=\"idB\"\n"+
291             " jdbc-type=\"VARCHAR\"\n"+
292             " primarykey=\"true\"\n"+
293             " length=\"254\"\n"+
294             " >\n"+
295             " </field-descriptor>\n"+
296             "</class-descriptor>\n"+
297             "<class-descriptor\n"+
298             " class=\"test.C\"\n"+
299             " table=\"C\"\n"+
300             ">\n"+
301             " <field-descriptor\n"+
302             " name=\"idA\"\n"+
303             " column=\"idA\"\n"+
304             " jdbc-type=\"INTEGER\"\n"+
305             " primarykey=\"true\"\n"+
306             " >\n"+
307             " </field-descriptor>\n"+
308             " <field-descriptor\n"+
309             " name=\"idB\"\n"+
310             " column=\"idB\"\n"+
311             " jdbc-type=\"VARCHAR\"\n"+
312             " primarykey=\"true\"\n"+
313             " length=\"254\"\n"+
314             " >\n"+
315             " </field-descriptor>\n"+
316             "</class-descriptor>",
317             runOjbXDoclet(OJB_DEST_FILE));
318         assertEqualsTorqueSchemaFile(
319             "<database name=\"ojbtest\">\n"+
320             " <table name=\"A\">\n"+
321             " <column name=\"attrKeyA\"\n"+
322             " javaName=\"attrKeyA\"\n"+
323             " type=\"INTEGER\"\n"+
324             " />\n"+
325             " <column name=\"attrKeyB\"\n"+
326             " javaName=\"attrKeyB\"\n"+
327             " type=\"VARCHAR\"\n"+
328             " size=\"254\"\n"+
329             " />\n"+
330             " <foreign-key foreignTable=\"C\">\n"+
331             " <reference local=\"attrKeyA\" foreign=\"idA\"/>\n"+
332             " <reference local=\"attrKeyB\" foreign=\"idB\"/>\n"+
333             " </foreign-key>\n"+
334             " </table>\n"+
335             " <table name=\"C\">\n"+
336             " <column name=\"idA\"\n"+
337             " javaName=\"idA\"\n"+
338             " type=\"INTEGER\"\n"+
339             " primaryKey=\"true\"\n"+
340             " required=\"true\"\n"+
341             " />\n"+
342             " <column name=\"idB\"\n"+
343             " javaName=\"idB\"\n"+
344             " type=\"VARCHAR\"\n"+
345             " primaryKey=\"true\"\n"+
346             " required=\"true\"\n"+
347             " size=\"254\"\n"+
348             " />\n"+
349             " </table>\n"+
350             "</database>",
351             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
352     }
353 }
354
Popular Tags