KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.HashMap JavaDoc;
19
20 /**
21  * Tests for the ojb.reference tag with the foreignkey attribute
22  * Note: For normal use tests see the tests for class-ref
23  *
24  * @author <a HREF="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
25  */

26 public class ReferenceTagForeignkeyAttributeTests extends OjbTestBase
27 {
28     public ReferenceTagForeignkeyAttributeTests(String JavaDoc name)
29     {
30         super(name);
31     }
32
33     // Test: no foreignkey attribute specified
34
public void testForeignkey1()
35     {
36         addClass(
37             "test.A",
38             "package test;\n"+
39             "/** @ojb.class */\n"+
40             "public class A {\n"+
41             " /** @ojb.field */\n"+
42             " private int attrKey;\n"+
43             " /** @ojb.reference */\n"+
44             " private test.B attr;\n"+
45             "}\n");
46         addClass(
47             "test.B",
48             "package test;\n"+
49             "/** @ojb.class */\n"+
50             "public class B {\n"+
51             " /** @ojb.field primarykey=\"true\" */\n"+
52             " private int id;\n"+
53             "}\n");
54
55         assertNull(runOjbXDoclet(OJB_DEST_FILE));
56         assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
57     }
58
59     // Test: foreignkey attribute is empty
60
public void testForeignkey2()
61     {
62         addClass(
63             "test.A",
64             "package test;\n"+
65             "/** @ojb.class */\n"+
66             "public class A {\n"+
67             " /** @ojb.field */\n"+
68             " private int attrKey;\n"+
69             " /** @ojb.reference foreignkey=\"\" */\n"+
70             " private test.B attr;\n"+
71             "}\n");
72         addClass(
73             "test.B",
74             "package test;\n"+
75             "/** @ojb.class */\n"+
76             "public class B {\n"+
77             " /** @ojb.field primarykey=\"true\" */\n"+
78             " private int id;\n"+
79             "}\n");
80
81         assertNull(runOjbXDoclet(OJB_DEST_FILE));
82         assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
83     }
84
85     // Test: foreignkey attribute specifies unknown field
86
public void testForeignkey3()
87     {
88         addClass(
89             "test.A",
90             "package test;\n"+
91             "/** @ojb.class */\n"+
92             "public class A {\n"+
93             " /** @ojb.field */\n"+
94             " private int attrKey;\n"+
95             " /** @ojb.reference foreignkey=\"bid\" */\n"+
96             " private test.B attr;\n"+
97             "}\n");
98         addClass(
99             "test.B",
100             "package test;\n"+
101             "/** @ojb.class */\n"+
102             "public class B {\n"+
103             " /** @ojb.field primarykey=\"true\" */\n"+
104             " private int id;\n"+
105             "}\n");
106
107         assertNull(runOjbXDoclet(OJB_DEST_FILE));
108         assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
109     }
110
111     // Test: foreignkey attribute specifies field without ojb.field tag
112
public void testForeignkey4()
113     {
114         addClass(
115             "test.A",
116             "package test;\n"+
117             "/** @ojb.class */\n"+
118             "public class A {\n"+
119             " private int attrKey;\n"+
120             " /** @ojb.reference foreignkey=\"attrKey\" */\n"+
121             " private test.B attr;\n"+
122             "}\n");
123         addClass(
124             "test.B",
125             "package test;\n"+
126             "/** @ojb.class */\n"+
127             "public class B {\n"+
128             " /** @ojb.field primarykey=\"true\" */\n"+
129             " private int id;\n"+
130             "}\n");
131
132         assertNull(runOjbXDoclet(OJB_DEST_FILE));
133         assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
134     }
135
136     // Test: foreignkey attribute specifies field only defined in subtype
137
public void testForeignkey5()
138     {
139         addClass(
140             "test.A",
141             "package test;\n"+
142             "/** @ojb.class */\n"+
143             "public class A {\n"+
144             " /** @ojb.reference foreignkey=\"attrKey\" */\n"+
145             " private test.B attr;\n"+
146             "}\n");
147         addClass(
148             "test.B",
149             "package test;\n"+
150             "/** @ojb.class */\n"+
151             "public class B {\n"+
152             " /** @ojb.field primarykey=\"true\" */\n"+
153             " private int id;\n"+
154             "}\n");
155         addClass(
156             "test.C",
157             "package test;\n"+
158             "/** @ojb.class */\n"+
159             "public class C extends A {\n"+
160             " /** @ojb.field */\n"+
161             " private int attrKey;\n"+
162             "}\n");
163
164         assertNull(runOjbXDoclet(OJB_DEST_FILE));
165         assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
166     }
167
168     // Test: foreignkey attribute specified, primary key of referenced type is anonymous
169
public void testForeignkey6()
170     {
171         addClass(
172             "test.A",
173             "package test;\n"+
174             "/** @ojb.class */\n"+
175             "public class A {\n"+
176             " /** @ojb.field */\n"+
177             " private int attrKey;\n"+
178             " /** @ojb.reference foreignkey=\"attrKey\" */\n"+
179             " private test.B attr;\n"+
180             "}\n");
181         addClass(
182             "test.B",
183             "package test;\n"+
184             "/** @ojb.class\n" +
185             " * @ojb.field name=\"id\"\n"+
186             " * jdbc-type=\"INTEGER\"\n"+
187             " * primarykey=\"true\"\n"+
188             " */\n"+
189             "public class B {}\n");
190
191         assertEqualsOjbDescriptorFile(
192             "<class-descriptor\n"+
193             " class=\"test.A\"\n"+
194             " table=\"A\"\n"+
195             ">\n"+
196             " <field-descriptor\n"+
197             " name=\"attrKey\"\n"+
198             " column=\"attrKey\"\n"+
199             " jdbc-type=\"INTEGER\"\n"+
200             " >\n"+
201             " </field-descriptor>\n"+
202             " <reference-descriptor\n"+
203             " name=\"attr\"\n"+
204             " class-ref=\"test.B\"\n"+
205             " >\n"+
206             " <foreignkey field-ref=\"attrKey\"/>\n"+
207             " </reference-descriptor>\n"+
208             "</class-descriptor>\n"+
209             "<class-descriptor\n"+
210             " class=\"test.B\"\n"+
211             " table=\"B\"\n"+
212             ">\n"+
213             " <field-descriptor\n"+
214             " name=\"id\"\n"+
215             " column=\"id\"\n"+
216             " jdbc-type=\"INTEGER\"\n"+
217             " primarykey=\"true\"\n"+
218             " access=\"anonymous\"\n"+
219             " >\n"+
220             " </field-descriptor>\n"+
221             "</class-descriptor>",
222             runOjbXDoclet(OJB_DEST_FILE));
223         assertEqualsTorqueSchemaFile(
224             "<database name=\"ojbtest\">\n"+
225             " <table name=\"A\">\n"+
226             " <column name=\"attrKey\"\n"+
227             " javaName=\"attrKey\"\n"+
228             " type=\"INTEGER\"\n"+
229             " />\n"+
230             " <foreign-key foreignTable=\"B\">\n"+
231             " <reference local=\"attrKey\" foreign=\"id\"/>\n"+
232             " </foreign-key>\n"+
233             " </table>\n"+
234             " <table name=\"B\">\n"+
235             " <column name=\"id\"\n"+
236             " javaName=\"id\"\n"+
237             " type=\"INTEGER\"\n"+
238             " primaryKey=\"true\"\n"+
239             " required=\"true\"\n"+
240             " />\n"+
241             " </table>\n"+
242             "</database>",
243             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
244     }
245
246     // Test: foreignkey attribute specified but primary key of referenced type has different type
247
public void testForeignkey7()
248     {
249         addClass(
250             "test.A",
251             "package test;\n"+
252             "/** @ojb.class */\n"+
253             "public class A {\n"+
254             " /** @ojb.field */\n"+
255             " private int attrKey;\n"+
256             " /** @ojb.reference foreignkey=\"attrKey\" */\n"+
257             " private test.B attr;\n"+
258             "}\n");
259         addClass(
260             "test.B",
261             "package test;\n"+
262             "/** @ojb.class */\n"+
263             "public class B {\n"+
264             " /** @ojb.field primarykey=\"true\" */\n"+
265             " private String id;\n"+
266             "}\n");
267
268         assertNull(runOjbXDoclet(OJB_DEST_FILE));
269         assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
270     }
271
272     // Test: two foreignkey attributes specified whose types match the ordered primarykeys of the referenced type
273
public void testForeignkey8()
274     {
275         addClass(
276             "test.A",
277             "package test;\n"+
278             "/** @ojb.class */\n"+
279             "public class A {\n"+
280             " /** @ojb.field */\n"+
281             " private int attrKeyA;\n"+
282             " /** @ojb.field */\n"+
283             " private String attrKeyB;\n"+
284             " /** @ojb.reference foreignkey=\"attrKeyB,attrKeyA\" */\n"+
285             " private test.B attr;\n"+
286             "}\n");
287         addClass(
288             "test.B",
289             "package test;\n"+
290             "/** @ojb.class */\n"+
291             "public class B {\n"+
292             " /** @ojb.field primarykey=\"true\"\n"+
293             " * id=\"2\"\n"+
294             " */\n"+
295             " private int idA;\n"+
296             " /** @ojb.field primarykey=\"true\"\n"+
297             " * id=\"1\"\n"+
298             " */\n"+
299             " private String idB;\n"+
300             "}\n");
301
302         assertEqualsOjbDescriptorFile(
303             "<class-descriptor\n"+
304             " class=\"test.A\"\n"+
305             " table=\"A\"\n"+
306             ">\n"+
307             " <field-descriptor\n"+
308             " name=\"attrKeyA\"\n"+
309             " column=\"attrKeyA\"\n"+
310             " jdbc-type=\"INTEGER\"\n"+
311             " >\n"+
312             " </field-descriptor>\n"+
313             " <field-descriptor\n"+
314             " name=\"attrKeyB\"\n"+
315             " column=\"attrKeyB\"\n"+
316             " jdbc-type=\"VARCHAR\"\n"+
317             " length=\"254\"\n"+
318             " >\n"+
319             " </field-descriptor>\n"+
320             " <reference-descriptor\n"+
321             " name=\"attr\"\n"+
322             " class-ref=\"test.B\"\n"+
323             " >\n"+
324             " <foreignkey field-ref=\"attrKeyB\"/>\n"+
325             " <foreignkey field-ref=\"attrKeyA\"/>\n"+
326             " </reference-descriptor>\n"+
327             "</class-descriptor>\n"+
328             "<class-descriptor\n"+
329             " class=\"test.B\"\n"+
330             " table=\"B\"\n"+
331             ">\n"+
332             " <field-descriptor\n"+
333             " name=\"idB\"\n"+
334             " column=\"idB\"\n"+
335             " jdbc-type=\"VARCHAR\"\n"+
336             " primarykey=\"true\"\n"+
337             " length=\"254\"\n"+
338             " >\n"+
339             " </field-descriptor>\n"+
340             " <field-descriptor\n"+
341             " name=\"idA\"\n"+
342             " column=\"idA\"\n"+
343             " jdbc-type=\"INTEGER\"\n"+
344             " primarykey=\"true\"\n"+
345             " >\n"+
346             " </field-descriptor>\n"+
347             "</class-descriptor>",
348             runOjbXDoclet(OJB_DEST_FILE));
349         assertEqualsTorqueSchemaFile(
350             "<database name=\"ojbtest\">\n"+
351             " <table name=\"A\">\n"+
352             " <column name=\"attrKeyA\"\n"+
353             " javaName=\"attrKeyA\"\n"+
354             " type=\"INTEGER\"\n"+
355             " />\n"+
356             " <column name=\"attrKeyB\"\n"+
357             " javaName=\"attrKeyB\"\n"+
358             " type=\"VARCHAR\"\n"+
359             " size=\"254\"\n"+
360             " />\n"+
361             " <foreign-key foreignTable=\"B\">\n"+
362             " <reference local=\"attrKeyB\" foreign=\"idB\"/>\n"+
363             " <reference local=\"attrKeyA\" foreign=\"idA\"/>\n"+
364             " </foreign-key>\n"+
365             " </table>\n"+
366             " <table name=\"B\">\n"+
367             " <column name=\"idB\"\n"+
368             " javaName=\"idB\"\n"+
369             " type=\"VARCHAR\"\n"+
370             " primaryKey=\"true\"\n"+
371             " required=\"true\"\n"+
372             " size=\"254\"\n"+
373             " />\n"+
374             " <column name=\"idA\"\n"+
375             " javaName=\"idA\"\n"+
376             " type=\"INTEGER\"\n"+
377             " primaryKey=\"true\"\n"+
378             " required=\"true\"\n"+
379             " />\n"+
380             " </table>\n"+
381             "</database>",
382             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
383     }
384
385     // Test: two foreignkey attributes specified whose types don't match the ordered primarykeys of the referenced type
386
public void testForeignkey9()
387     {
388         addClass(
389             "test.A",
390             "package test;\n"+
391             "/** @ojb.class */\n"+
392             "public class A {\n"+
393             " /** @ojb.field */\n"+
394             " private int attrKeyA;\n"+
395             " /** @ojb.field */\n"+
396             " private String attrKeyB;\n"+
397             " /** @ojb.reference foreignkey=\"attrKeyA,attrKeyB\" */\n"+
398             " private test.B attr;\n"+
399             "}\n");
400         addClass(
401             "test.B",
402             "package test;\n"+
403             "/** @ojb.class */\n"+
404             "public class B {\n"+
405             " /** @ojb.field primarykey=\"true\" */\n"+
406             " private String idA;\n"+
407             " /** @ojb.field primarykey=\"true\" */\n"+
408             " private int idB;\n"+
409             "}\n");
410
411         assertNull(runOjbXDoclet(OJB_DEST_FILE));
412         assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
413     }
414
415     // Test: two foreignkey attributes specified but only one primary key in the referenced type
416
public void testForeignkey10()
417     {
418         addClass(
419             "test.A",
420             "package test;\n"+
421             "/** @ojb.class */\n"+
422             "public class A {\n"+
423             " /** @ojb.field */\n"+
424             " private int attrKeyA;\n"+
425             " /** @ojb.field */\n"+
426             " private int attrKeyB;\n"+
427             " /** @ojb.reference foreignkey=\"attrKeyA,attrKeyB\" */\n"+
428             " private test.B attr;\n"+
429             "}\n");
430         addClass(
431             "test.B",
432             "package test;\n"+
433             "/** @ojb.class */\n"+
434             "public class B {\n"+
435             " /** @ojb.field primarykey=\"true\" */\n"+
436             " private int id;\n"+
437             "}\n");
438
439         assertNull(runOjbXDoclet(OJB_DEST_FILE));
440         assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
441     }
442
443     // Test: two foreignkey attributes specified and referenced type is an interface with a subclass
444
public void testForeignkey11()
445     {
446         addClass(
447             "test.A",
448             "package test;\n"+
449             "/** @ojb.class */\n"+
450             "public class A {\n"+
451             " /** @ojb.field */\n"+
452             " private int attrKeyA;\n"+
453             " /** @ojb.field */\n"+
454             " private String attrKeyB;\n"+
455             " /** @ojb.reference foreignkey=\"attrKeyA,attrKeyB\" */\n"+
456             " private test.B attr;\n"+
457             "}\n");
458         addClass(
459             "test.B",
460             "package test;\n"+
461             "/** @ojb.class generate-table-info=\"false\" */\n"+
462             "public interface B {}\n");
463         addClass(
464             "test.C",
465             "package test;\n"+
466             "/** @ojb.class */\n"+
467             "public class C implements B {\n"+
468             " /** @ojb.field primarykey=\"true\" */\n"+
469             " private int idA;\n"+
470             " /** @ojb.field primarykey=\"true\" */\n"+
471             " private String idB;\n"+
472             "}\n");
473         
474         assertEqualsOjbDescriptorFile(
475             "<class-descriptor\n"+
476             " class=\"test.A\"\n"+
477             " table=\"A\"\n"+
478             ">\n"+
479             " <field-descriptor\n"+
480             " name=\"attrKeyA\"\n"+
481             " column=\"attrKeyA\"\n"+
482             " jdbc-type=\"INTEGER\"\n"+
483             " >\n"+
484             " </field-descriptor>\n"+
485             " <field-descriptor\n"+
486             " name=\"attrKeyB\"\n"+
487             " column=\"attrKeyB\"\n"+
488             " jdbc-type=\"VARCHAR\"\n"+
489             " length=\"254\"\n"+
490             " >\n"+
491             " </field-descriptor>\n"+
492             " <reference-descriptor\n"+
493             " name=\"attr\"\n"+
494             " class-ref=\"test.B\"\n"+
495             " >\n"+
496             " <foreignkey field-ref=\"attrKeyA\"/>\n"+
497             " <foreignkey field-ref=\"attrKeyB\"/>\n"+
498             " </reference-descriptor>\n"+
499             "</class-descriptor>\n"+
500             "<class-descriptor\n"+
501             " class=\"test.B\"\n"+
502             ">\n"+
503             " <extent-class class-ref=\"test.C\"/>\n"+
504             " <field-descriptor\n"+
505             " name=\"idA\"\n"+
506             " column=\"idA\"\n"+
507             " jdbc-type=\"INTEGER\"\n"+
508             " primarykey=\"true\"\n"+
509             " >\n"+
510             " </field-descriptor>\n"+
511             " <field-descriptor\n"+
512             " name=\"idB\"\n"+
513             " column=\"idB\"\n"+
514             " jdbc-type=\"VARCHAR\"\n"+
515             " primarykey=\"true\"\n"+
516             " length=\"254\"\n"+
517             " >\n"+
518             " </field-descriptor>\n"+
519             "</class-descriptor>\n"+
520             "<class-descriptor\n"+
521             " class=\"test.C\"\n"+
522             " table=\"C\"\n"+
523             ">\n"+
524             " <field-descriptor\n"+
525             " name=\"idA\"\n"+
526             " column=\"idA\"\n"+
527             " jdbc-type=\"INTEGER\"\n"+
528             " primarykey=\"true\"\n"+
529             " >\n"+
530             " </field-descriptor>\n"+
531             " <field-descriptor\n"+
532             " name=\"idB\"\n"+
533             " column=\"idB\"\n"+
534             " jdbc-type=\"VARCHAR\"\n"+
535             " primarykey=\"true\"\n"+
536             " length=\"254\"\n"+
537             " >\n"+
538             " </field-descriptor>\n"+
539             "</class-descriptor>",
540             runOjbXDoclet(OJB_DEST_FILE));
541         assertEqualsTorqueSchemaFile(
542             "<database name=\"ojbtest\">\n"+
543             " <table name=\"A\">\n"+
544             " <column name=\"attrKeyA\"\n"+
545             " javaName=\"attrKeyA\"\n"+
546             " type=\"INTEGER\"\n"+
547             " />\n"+
548             " <column name=\"attrKeyB\"\n"+
549             " javaName=\"attrKeyB\"\n"+
550             " type=\"VARCHAR\"\n"+
551             " size=\"254\"\n"+
552             " />\n"+
553             " <foreign-key foreignTable=\"C\">\n"+
554             " <reference local=\"attrKeyA\" foreign=\"idA\"/>\n"+
555             " <reference local=\"attrKeyB\" foreign=\"idB\"/>\n"+
556             " </foreign-key>\n"+
557             " </table>\n"+
558             " <table name=\"C\">\n"+
559             " <column name=\"idA\"\n"+
560             " javaName=\"idA\"\n"+
561             " type=\"INTEGER\"\n"+
562             " primaryKey=\"true\"\n"+
563             " required=\"true\"\n"+
564             " />\n"+
565             " <column name=\"idB\"\n"+
566             " javaName=\"idB\"\n"+
567             " type=\"VARCHAR\"\n"+
568             " primaryKey=\"true\"\n"+
569             " required=\"true\"\n"+
570             " size=\"254\"\n"+
571             " />\n"+
572             " </table>\n"+
573             "</database>",
574             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
575     }
576
577     // Test: one foreignkey attribute specified, no foreignkey generation for torque
578
public void testForeignkey12()
579     {
580         addClass(
581             "test.A",
582             "package test;\n"+
583             "/** @ojb.class */\n"+
584             "public class A {\n"+
585             " /** @ojb.field */\n"+
586             " private int attrKey;\n"+
587             " /** @ojb.reference foreignkey=\"attrKey\" */\n"+
588             " private test.B attr;\n"+
589             "}\n");
590         addClass(
591             "test.B",
592             "package test;\n"+
593             "/** @ojb.class */\n"+
594             "public class B {\n"+
595             " /** @ojb.field primarykey=\"true\" */\n"+
596             " private int id;\n"+
597             "}\n");
598
599         HashMap JavaDoc torqueSubTaskProps = new HashMap JavaDoc();
600         
601         torqueSubTaskProps.put(PROPERTY_DATABASENAME, "ojbtest");
602         torqueSubTaskProps.put(PROPERTY_GENERATE_FOREIGNKEYS, Boolean.FALSE);
603
604         assertEqualsOjbDescriptorFile(
605             "<class-descriptor\n"+
606             " class=\"test.A\"\n"+
607             " table=\"A\"\n"+
608             ">\n"+
609             " <field-descriptor\n"+
610             " name=\"attrKey\"\n"+
611             " column=\"attrKey\"\n"+
612             " jdbc-type=\"INTEGER\"\n"+
613             " >\n"+
614             " </field-descriptor>\n"+
615             " <reference-descriptor\n"+
616             " name=\"attr\"\n"+
617             " class-ref=\"test.B\"\n"+
618             " >\n"+
619             " <foreignkey field-ref=\"attrKey\"/>\n"+
620             " </reference-descriptor>\n"+
621             "</class-descriptor>\n"+
622             "<class-descriptor\n"+
623             " class=\"test.B\"\n"+
624             " table=\"B\"\n"+
625             ">\n"+
626             " <field-descriptor\n"+
627             " name=\"id\"\n"+
628             " column=\"id\"\n"+
629             " jdbc-type=\"INTEGER\"\n"+
630             " primarykey=\"true\"\n"+
631             " >\n"+
632             " </field-descriptor>\n"+
633             "</class-descriptor>",
634             runOjbXDoclet(OJB_DEST_FILE));
635         assertEqualsTorqueSchemaFile(
636             "<database name=\"ojbtest\">\n"+
637             " <table name=\"A\">\n"+
638             " <column name=\"attrKey\"\n"+
639             " javaName=\"attrKey\"\n"+
640             " type=\"INTEGER\"\n"+
641             " />\n"+
642             " </table>\n"+
643             " <table name=\"B\">\n"+
644             " <column name=\"id\"\n"+
645             " javaName=\"id\"\n"+
646             " type=\"INTEGER\"\n"+
647             " primaryKey=\"true\"\n"+
648             " required=\"true\"\n"+
649             " />\n"+
650             " </table>\n"+
651             "</database>",
652             runTorqueXDoclet(TORQUE_DEST_FILE, null, torqueSubTaskProps));
653     }
654
655     // Test: referencing type has a subclass
656
public void testForeignkey13()
657     {
658         addClass(
659             "test.A",
660             "package test;\n"+
661             "/** @ojb.class */\n"+
662             "public class A {\n"+
663             " /** @ojb.field */\n"+
664             " private int attrKey;\n"+
665             " /** @ojb.reference foreignkey=\"attrKey\" */\n"+
666             " private test.C attr;\n"+
667             "}\n");
668         addClass(
669             "test.B",
670             "package test;\n"+
671             "/** @ojb.class */\n"+
672             "public class B extends A {}\n");
673         addClass(
674             "test.C",
675             "package test;\n"+
676             "/** @ojb.class generate-table-info=\"false\" */\n"+
677             "public interface C {}\n");
678         addClass(
679             "test.D",
680             "package test;\n"+
681             "/** @ojb.class */\n"+
682             "public class D implements C {\n"+
683             " /** @ojb.field primarykey=\"true\" */\n"+
684             " private int id;\n"+
685             "}\n");
686         
687         assertEqualsOjbDescriptorFile(
688             "<class-descriptor\n"+
689             " class=\"test.A\"\n"+
690             " table=\"A\"\n"+
691             ">\n"+
692             " <extent-class class-ref=\"test.B\"/>\n"+
693             " <field-descriptor\n"+
694             " name=\"attrKey\"\n"+
695             " column=\"attrKey\"\n"+
696             " jdbc-type=\"INTEGER\"\n"+
697             " >\n"+
698             " </field-descriptor>\n"+
699             " <reference-descriptor\n"+
700             " name=\"attr\"\n"+
701             " class-ref=\"test.C\"\n"+
702             " >\n"+
703             " <foreignkey field-ref=\"attrKey\"/>\n"+
704             " </reference-descriptor>\n"+
705             "</class-descriptor>\n"+
706             "<class-descriptor\n"+
707             " class=\"test.B\"\n"+
708             " table=\"B\"\n"+
709             ">\n"+
710             " <field-descriptor\n"+
711             " name=\"attrKey\"\n"+
712             " column=\"attrKey\"\n"+
713             " jdbc-type=\"INTEGER\"\n"+
714             " >\n"+
715             " </field-descriptor>\n"+
716             " <reference-descriptor\n"+
717             " name=\"attr\"\n"+
718             " class-ref=\"test.C\"\n"+
719             " >\n"+
720             " <foreignkey field-ref=\"attrKey\"/>\n"+
721             " </reference-descriptor>\n"+
722             "</class-descriptor>\n"+
723             "<class-descriptor\n"+
724             " class=\"test.C\"\n"+
725             ">\n"+
726             " <extent-class class-ref=\"test.D\"/>\n"+
727             " <field-descriptor\n"+
728             " name=\"id\"\n"+
729             " column=\"id\"\n"+
730             " jdbc-type=\"INTEGER\"\n"+
731             " primarykey=\"true\"\n"+
732             " >\n"+
733             " </field-descriptor>\n"+
734             "</class-descriptor>\n"+
735             "<class-descriptor\n"+
736             " class=\"test.D\"\n"+
737             " table=\"D\"\n"+
738             ">\n"+
739             " <field-descriptor\n"+
740             " name=\"id\"\n"+
741             " column=\"id\"\n"+
742             " jdbc-type=\"INTEGER\"\n"+
743             " primarykey=\"true\"\n"+
744             " >\n"+
745             " </field-descriptor>\n"+
746             "</class-descriptor>",
747             runOjbXDoclet(OJB_DEST_FILE));
748         assertEqualsTorqueSchemaFile(
749             "<database name=\"ojbtest\">\n"+
750             " <table name=\"A\">\n"+
751             " <column name=\"attrKey\"\n"+
752             " javaName=\"attrKey\"\n"+
753             " type=\"INTEGER\"\n"+
754             " />\n"+
755             " <foreign-key foreignTable=\"D\">\n"+
756             " <reference local=\"attrKey\" foreign=\"id\"/>\n"+
757             " </foreign-key>\n"+
758             " </table>\n"+
759             " <table name=\"B\">\n"+
760             " <column name=\"attrKey\"\n"+
761             " javaName=\"attrKey\"\n"+
762             " type=\"INTEGER\"\n"+
763             " />\n"+
764             " <foreign-key foreignTable=\"D\">\n"+
765             " <reference local=\"attrKey\" foreign=\"id\"/>\n"+
766             " </foreign-key>\n"+
767             " </table>\n"+
768             " <table name=\"D\">\n"+
769             " <column name=\"id\"\n"+
770             " javaName=\"id\"\n"+
771             " type=\"INTEGER\"\n"+
772             " primaryKey=\"true\"\n"+
773             " required=\"true\"\n"+
774             " />\n"+
775             " </table>\n"+
776             "</database>",
777             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
778     }
779
780     // Test: referenced type has a multiple subtypes that map to different tables
781
public void testForeignkey14()
782     {
783         addClass(
784             "test.A",
785             "package test;\n"+
786             "/** @ojb.class */\n"+
787             "public class A {\n"+
788             " /** @ojb.field */\n"+
789             " private int attrKey;\n"+
790             " /** @ojb.reference foreignkey=\"attrKey\" */\n"+
791             " private test.B attr;\n"+
792             "}\n");
793         addClass(
794             "test.B",
795             "package test;\n"+
796             "/** @ojb.class generate-repository-info=\"false\" */\n"+
797             "public interface B {}\n");
798         addClass(
799             "test.C",
800             "package test;\n"+
801             "/** @ojb.class */\n"+
802             "public class C implements B {\n"+
803             " /** @ojb.field primarykey=\"true\" */\n"+
804             " private int id;\n"+
805             "}\n");
806         addClass(
807             "test.D",
808             "package test;\n"+
809             "/** @ojb.class */\n"+
810             "public class D implements B {\n"+
811             " /** @ojb.field primarykey=\"true\" */\n"+
812             " private int id;\n"+
813             "}\n");
814         
815         assertEqualsOjbDescriptorFile(
816             "<class-descriptor\n"+
817             " class=\"test.A\"\n"+
818             " table=\"A\"\n"+
819             ">\n"+
820             " <field-descriptor\n"+
821             " name=\"attrKey\"\n"+
822             " column=\"attrKey\"\n"+
823             " jdbc-type=\"INTEGER\"\n"+
824             " >\n"+
825             " </field-descriptor>\n"+
826             " <reference-descriptor\n"+
827             " name=\"attr\"\n"+
828             " class-ref=\"test.B\"\n"+
829             " >\n"+
830             " <foreignkey field-ref=\"attrKey\"/>\n"+
831             " </reference-descriptor>\n"+
832             "</class-descriptor>\n"+
833             "<class-descriptor\n"+
834             " class=\"test.B\"\n"+
835             ">\n"+
836             " <extent-class class-ref=\"test.C\"/>\n"+
837             " <extent-class class-ref=\"test.D\"/>\n"+
838             " <field-descriptor\n"+
839             " name=\"id\"\n"+
840             " column=\"id\"\n"+
841             " jdbc-type=\"INTEGER\"\n"+
842             " primarykey=\"true\"\n"+
843             " >\n"+
844             " </field-descriptor>\n"+
845             "</class-descriptor>\n"+
846             "<class-descriptor\n"+
847             " class=\"test.C\"\n"+
848             " table=\"C\"\n"+
849             ">\n"+
850             " <field-descriptor\n"+
851             " name=\"id\"\n"+
852             " column=\"id\"\n"+
853             " jdbc-type=\"INTEGER\"\n"+
854             " primarykey=\"true\"\n"+
855             " >\n"+
856             " </field-descriptor>\n"+
857             "</class-descriptor>\n"+
858             "<class-descriptor\n"+
859             " class=\"test.D\"\n"+
860             " table=\"D\"\n"+
861             ">\n"+
862             " <field-descriptor\n"+
863             " name=\"id\"\n"+
864             " column=\"id\"\n"+
865             " jdbc-type=\"INTEGER\"\n"+
866             " primarykey=\"true\"\n"+
867             " >\n"+
868             " </field-descriptor>\n"+
869             "</class-descriptor>",
870             runOjbXDoclet(OJB_DEST_FILE));
871         assertEqualsTorqueSchemaFile(
872             "<database name=\"ojbtest\">\n"+
873             " <table name=\"A\">\n"+
874             " <column name=\"attrKey\"\n"+
875             " javaName=\"attrKey\"\n"+
876             " type=\"INTEGER\"\n"+
877             " />\n"+
878             " </table>\n"+
879             " <table name=\"C\">\n"+
880             " <column name=\"id\"\n"+
881             " javaName=\"id\"\n"+
882             " type=\"INTEGER\"\n"+
883             " primaryKey=\"true\"\n"+
884             " required=\"true\"\n"+
885             " />\n"+
886             " </table>\n"+
887             " <table name=\"D\">\n"+
888             " <column name=\"id\"\n"+
889             " javaName=\"id\"\n"+
890             " type=\"INTEGER\"\n"+
891             " primaryKey=\"true\"\n"+
892             " required=\"true\"\n"+
893             " />\n"+
894             " </table>\n"+
895             "</database>",
896             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
897     }
898 }
899
Popular Tags