KickJava   Java API By Example, From Geeks To Geeks.

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


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.collection tag without attributes other than element-class-ref and foreignkey.
20  *
21  * @author <a HREF="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
22  */

23 public class CollectionTagSimpleTests extends OjbTestBase
24 {
25     public CollectionTagSimpleTests(String JavaDoc name)
26     {
27         super(name);
28     }
29
30     // Test: simple usage
31
public void testSimple1()
32     {
33         addClass(
34             "test.A",
35             "package test;\n"+
36             "/** @ojb.class */\n" +
37             "public class A {\n"+
38             " /** @ojb.field primarykey=\"true\" */\n"+
39             " private int id;\n"+
40             " /** @ojb.collection element-class-ref=\"test.B\"\n"+
41             " * foreignkey=\"aid\"\n"+
42             " */\n"+
43             " private java.util.List attr;\n"+
44             "}");
45         addClass(
46             "test.B",
47             "package test;\n"+
48             "/** @ojb.class */\n"+
49             "public class B {\n"+
50             " /** @ojb.field */\n"+
51             " private int aid;\n"+
52             "}\n");
53
54         assertEqualsOjbDescriptorFile(
55             "<class-descriptor\n"+
56             " class=\"test.A\"\n"+
57             " table=\"A\"\n"+
58             ">\n"+
59             " <field-descriptor\n"+
60             " name=\"id\"\n"+
61             " column=\"id\"\n"+
62             " jdbc-type=\"INTEGER\"\n"+
63             " primarykey=\"true\"\n"+
64             " >\n"+
65             " </field-descriptor>\n"+
66             " <collection-descriptor\n"+
67             " name=\"attr\"\n"+
68             " element-class-ref=\"test.B\"\n"+
69             " >\n"+
70             " <inverse-foreignkey field-ref=\"aid\"/>\n"+
71             " </collection-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=\"aid\"\n"+
79             " column=\"aid\"\n"+
80             " jdbc-type=\"INTEGER\"\n"+
81             " >\n"+
82             " </field-descriptor>\n"+
83             "</class-descriptor>",
84             runOjbXDoclet(OJB_DEST_FILE));
85         assertEqualsTorqueSchemaFile(
86             "<database name=\"ojbtest\">\n"+
87             " <table name=\"A\">\n"+
88             " <column name=\"id\"\n"+
89             " javaName=\"id\"\n"+
90             " type=\"INTEGER\"\n"+
91             " primaryKey=\"true\"\n"+
92             " required=\"true\"\n"+
93             " />\n"+
94             " </table>\n"+
95             " <table name=\"B\">\n"+
96             " <column name=\"aid\"\n"+
97             " javaName=\"aid\"\n"+
98             " type=\"INTEGER\"\n"+
99             " />\n"+
100             " <foreign-key foreignTable=\"A\">\n"+
101             " <reference local=\"aid\" foreign=\"id\"/>\n"+
102             " </foreign-key>\n"+
103             " </table>\n"+
104             "</database>",
105             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
106     }
107
108     // Test: collection is of type DListImpl (an ojb class)
109
public void testSimple2()
110     {
111         addClass(
112             "test.A",
113             "package test;\n"+
114             "/** @ojb.class */\n" +
115             "public class A {\n"+
116             " /** @ojb.field primarykey=\"true\" */\n"+
117             " private int id;\n"+
118             " /** @ojb.collection element-class-ref=\"test.B\"\n"+
119             " * foreignkey=\"aid\"\n"+
120             " */\n"+
121             " private org.apache.ojb.odmg.collections.DListImpl attr;\n"+
122             "}");
123         addClass(
124             "test.B",
125             "package test;\n"+
126             "/** @ojb.class */\n"+
127             "public class B {\n"+
128             " /** @ojb.field */\n"+
129             " private int aid;\n"+
130             "}\n");
131
132         assertEqualsOjbDescriptorFile(
133             "<class-descriptor\n"+
134             " class=\"test.A\"\n"+
135             " table=\"A\"\n"+
136             ">\n"+
137             " <field-descriptor\n"+
138             " name=\"id\"\n"+
139             " column=\"id\"\n"+
140             " jdbc-type=\"INTEGER\"\n"+
141             " primarykey=\"true\"\n"+
142             " >\n"+
143             " </field-descriptor>\n"+
144             " <collection-descriptor\n"+
145             " name=\"attr\"\n"+
146             " collection-class=\"org.apache.ojb.odmg.collections.DListImpl\"\n"+
147             " element-class-ref=\"test.B\"\n"+
148             " >\n"+
149             " <inverse-foreignkey field-ref=\"aid\"/>\n"+
150             " </collection-descriptor>\n"+
151             "</class-descriptor>\n"+
152             "<class-descriptor\n"+
153             " class=\"test.B\"\n"+
154             " table=\"B\"\n"+
155             ">\n"+
156             " <field-descriptor\n"+
157             " name=\"aid\"\n"+
158             " column=\"aid\"\n"+
159             " jdbc-type=\"INTEGER\"\n"+
160             " >\n"+
161             " </field-descriptor>\n"+
162             "</class-descriptor>",
163             runOjbXDoclet(OJB_DEST_FILE));
164         assertEqualsTorqueSchemaFile(
165             "<database name=\"ojbtest\">\n"+
166             " <table name=\"A\">\n"+
167             " <column name=\"id\"\n"+
168             " javaName=\"id\"\n"+
169             " type=\"INTEGER\"\n"+
170             " primaryKey=\"true\"\n"+
171             " required=\"true\"\n"+
172             " />\n"+
173             " </table>\n"+
174             " <table name=\"B\">\n"+
175             " <column name=\"aid\"\n"+
176             " javaName=\"aid\"\n"+
177             " type=\"INTEGER\"\n"+
178             " />\n"+
179             " <foreign-key foreignTable=\"A\">\n"+
180             " <reference local=\"aid\" foreign=\"id\"/>\n"+
181             " </foreign-key>\n"+
182             " </table>\n"+
183             "</database>",
184             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
185     }
186
187     // Test: collection is user-defined type
188
public void testSimple3()
189     {
190         addClass(
191             "test.A",
192             "package test;\n"+
193             "/** @ojb.class */\n" +
194             "public class A {\n"+
195             " /** @ojb.field primarykey=\"true\" */\n"+
196             " private int id;\n"+
197             " /** @ojb.collection element-class-ref=\"test.B\"\n"+
198             " * foreignkey=\"aid\"\n"+
199             " */\n"+
200             " private "+TestCollectionClass.class.getName()+" attr;\n"+
201             "}");
202         addClass(
203             "test.B",
204             "package test;\n"+
205             "/** @ojb.class */\n"+
206             "public class B {\n"+
207             " /** @ojb.field */\n"+
208             " private int aid;\n"+
209             "}\n");
210
211         assertEqualsOjbDescriptorFile(
212             "<class-descriptor\n"+
213             " class=\"test.A\"\n"+
214             " table=\"A\"\n"+
215             ">\n"+
216             " <field-descriptor\n"+
217             " name=\"id\"\n"+
218             " column=\"id\"\n"+
219             " jdbc-type=\"INTEGER\"\n"+
220             " primarykey=\"true\"\n"+
221             " >\n"+
222             " </field-descriptor>\n"+
223             " <collection-descriptor\n"+
224             " name=\"attr\"\n"+
225             " collection-class=\""+TestCollectionClass.class.getName()+"\"\n"+
226             " element-class-ref=\"test.B\"\n"+
227             " >\n"+
228             " <inverse-foreignkey field-ref=\"aid\"/>\n"+
229             " </collection-descriptor>\n"+
230             "</class-descriptor>\n"+
231             "<class-descriptor\n"+
232             " class=\"test.B\"\n"+
233             " table=\"B\"\n"+
234             ">\n"+
235             " <field-descriptor\n"+
236             " name=\"aid\"\n"+
237             " column=\"aid\"\n"+
238             " jdbc-type=\"INTEGER\"\n"+
239             " >\n"+
240             " </field-descriptor>\n"+
241             "</class-descriptor>",
242             runOjbXDoclet(OJB_DEST_FILE));
243         assertEqualsTorqueSchemaFile(
244             "<database name=\"ojbtest\">\n"+
245             " <table name=\"A\">\n"+
246             " <column name=\"id\"\n"+
247             " javaName=\"id\"\n"+
248             " type=\"INTEGER\"\n"+
249             " primaryKey=\"true\"\n"+
250             " required=\"true\"\n"+
251             " />\n"+
252             " </table>\n"+
253             " <table name=\"B\">\n"+
254             " <column name=\"aid\"\n"+
255             " javaName=\"aid\"\n"+
256             " type=\"INTEGER\"\n"+
257             " />\n"+
258             " <foreign-key foreignTable=\"A\">\n"+
259             " <reference local=\"aid\" foreign=\"id\"/>\n"+
260             " </foreign-key>\n"+
261             " </table>\n"+
262             "</database>",
263             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
264     }
265
266     // Test: collection is of a type not implementing Collection
267
public void testSimple4()
268     {
269         addClass(
270             "test.List",
271             "package test;\n"+
272             "public class List {}");
273         addClass(
274             "test.A",
275             "package test;\n"+
276             "/** @ojb.class */\n" +
277             "public class A {\n"+
278             " /** @ojb.field primarykey=\"true\" */\n"+
279             " private int id;\n"+
280             " /** @ojb.collection element-class-ref=\"test.B\"\n"+
281             " * foreignkey=\"aid\"\n"+
282             " */\n"+
283             " private List attr;\n"+
284             "}");
285         addClass(
286             "test.B",
287             "package test;\n"+
288             "/** @ojb.class */\n"+
289             "public class B {\n"+
290             " /** @ojb.field */\n"+
291             " private int aid;\n"+
292             "}\n");
293
294         assertNull(runOjbXDoclet(OJB_DEST_FILE));
295         assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
296     }
297
298     // Test: array of persistent class
299
public void testSimple5()
300     {
301         addClass(
302             "test.A",
303             "package test;\n"+
304             "/** @ojb.class */\n" +
305             "public class A {\n"+
306             " /** @ojb.field primarykey=\"true\" */\n"+
307             " private int id;\n"+
308             " /** @ojb.collection foreignkey=\"aid\"\n"+
309             " */\n"+
310             " private B[] attr;\n"+
311             "}");
312         addClass(
313             "test.B",
314             "package test;\n"+
315             "/** @ojb.class */\n"+
316             "public class B {\n"+
317             " /** @ojb.field */\n"+
318             " private int aid;\n"+
319             "}\n");
320
321         assertEqualsOjbDescriptorFile(
322             "<class-descriptor\n"+
323             " class=\"test.A\"\n"+
324             " table=\"A\"\n"+
325             ">\n"+
326             " <field-descriptor\n"+
327             " name=\"id\"\n"+
328             " column=\"id\"\n"+
329             " jdbc-type=\"INTEGER\"\n"+
330             " primarykey=\"true\"\n"+
331             " >\n"+
332             " </field-descriptor>\n"+
333             " <collection-descriptor\n"+
334             " name=\"attr\"\n"+
335             " element-class-ref=\"test.B\"\n"+
336             " >\n"+
337             " <inverse-foreignkey field-ref=\"aid\"/>\n"+
338             " </collection-descriptor>\n"+
339             "</class-descriptor>\n"+
340             "<class-descriptor\n"+
341             " class=\"test.B\"\n"+
342             " table=\"B\"\n"+
343             ">\n"+
344             " <field-descriptor\n"+
345             " name=\"aid\"\n"+
346             " column=\"aid\"\n"+
347             " jdbc-type=\"INTEGER\"\n"+
348             " >\n"+
349             " </field-descriptor>\n"+
350             "</class-descriptor>",
351             runOjbXDoclet(OJB_DEST_FILE));
352         assertEqualsTorqueSchemaFile(
353             "<database name=\"ojbtest\">\n"+
354             " <table name=\"A\">\n"+
355             " <column name=\"id\"\n"+
356             " javaName=\"id\"\n"+
357             " type=\"INTEGER\"\n"+
358             " primaryKey=\"true\"\n"+
359             " required=\"true\"\n"+
360             " />\n"+
361             " </table>\n"+
362             " <table name=\"B\">\n"+
363             " <column name=\"aid\"\n"+
364             " javaName=\"aid\"\n"+
365             " type=\"INTEGER\"\n"+
366             " />\n"+
367             " <foreign-key foreignTable=\"A\">\n"+
368             " <reference local=\"aid\" foreign=\"id\"/>\n"+
369             " </foreign-key>\n"+
370             " </table>\n"+
371             "</database>",
372             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
373     }
374
375     // Test: array of non-persistent class
376
public void testSimple6()
377     {
378         addClass(
379             "test.A",
380             "package test;\n"+
381             "/** @ojb.class */\n" +
382             "public class A {\n"+
383             " /** @ojb.field primarykey=\"true\" */\n"+
384             " private int id;\n"+
385             " /** @ojb.collection foreignkey=\"aid\"\n"+
386             " */\n"+
387             " private B[] attr;\n"+
388             "}");
389         addClass(
390             "test.B",
391             "package test;\n"+
392             "public class B {\n"+
393             " private int aid;\n"+
394             "}\n");
395
396         assertNull(runOjbXDoclet(OJB_DEST_FILE));
397         assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
398     }
399
400     // Test: array of non-persistent class that has a persistent sub-type
401
public void testSimple7()
402     {
403         addClass(
404             "test.A",
405             "package test;\n"+
406             "/** @ojb.class */\n" +
407             "public class A {\n"+
408             " /** @ojb.field primarykey=\"true\" */\n"+
409             " private int id;\n"+
410             " /** @ojb.collection foreignkey=\"aid\"\n"+
411             " */\n"+
412             " private B[] attr;\n"+
413             "}");
414         addClass(
415             "test.B",
416             "package test;\n"+
417             "public class B {\n"+
418             " /** @ojb.field */\n"+
419             " private int aid;\n"+
420             "}\n");
421         addClass(
422             "test.C",
423             "package test;\n"+
424             "/** @ojb.class */\n"+
425             "public class C {}\n");
426
427         assertNull(runOjbXDoclet(OJB_DEST_FILE));
428         assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
429     }
430
431     // Test: inherited
432
public void testSimple8()
433     {
434         addClass(
435             "test.A",
436             "package test;\n"+
437             "/** @ojb.class */\n" +
438             "public class A {\n"+
439             " /** @ojb.field primarykey=\"true\" */\n"+
440             " private int id;\n"+
441             " /** @ojb.field length=\"256\" */\n"+
442             " private String ojbConcreteClass;\n"+
443             " /** @ojb.collection element-class-ref=\"test.B\"\n"+
444             " * foreignkey=\"aid\"\n"+
445             " */\n"+
446             " private java.util.List attr;\n"+
447             "}");
448         addClass(
449             "test.B",
450             "package test;\n"+
451             "/** @ojb.class */\n"+
452             "public class B {\n"+
453             " /** @ojb.field */\n"+
454             " private int aid;\n"+
455             "}\n");
456         addClass(
457             "test.C",
458             "package test;\n"+
459             "/** @ojb.class table=\"A\" */\n"+
460             "public class C extends A {}\n");
461
462         assertEqualsOjbDescriptorFile(
463             "<class-descriptor\n"+
464             " class=\"test.A\"\n"+
465             " table=\"A\"\n"+
466             ">\n"+
467             " <extent-class class-ref=\"test.C\"/>\n"+
468             " <field-descriptor\n"+
469             " name=\"id\"\n"+
470             " column=\"id\"\n"+
471             " jdbc-type=\"INTEGER\"\n"+
472             " primarykey=\"true\"\n"+
473             " >\n"+
474             " </field-descriptor>\n"+
475             " <field-descriptor\n"+
476             " name=\"ojbConcreteClass\"\n"+
477             " column=\"ojbConcreteClass\"\n"+
478             " jdbc-type=\"VARCHAR\"\n"+
479             " length=\"256\"\n"+
480             " >\n"+
481             " </field-descriptor>\n"+
482             " <collection-descriptor\n"+
483             " name=\"attr\"\n"+
484             " element-class-ref=\"test.B\"\n"+
485             " >\n"+
486             " <inverse-foreignkey field-ref=\"aid\"/>\n"+
487             " </collection-descriptor>\n"+
488             "</class-descriptor>\n"+
489             "<class-descriptor\n"+
490             " class=\"test.B\"\n"+
491             " table=\"B\"\n"+
492             ">\n"+
493             " <field-descriptor\n"+
494             " name=\"aid\"\n"+
495             " column=\"aid\"\n"+
496             " jdbc-type=\"INTEGER\"\n"+
497             " >\n"+
498             " </field-descriptor>\n"+
499             "</class-descriptor>\n"+
500             "<class-descriptor\n"+
501             " class=\"test.C\"\n"+
502             " table=\"A\"\n"+
503             ">\n"+
504             " <field-descriptor\n"+
505             " name=\"id\"\n"+
506             " column=\"id\"\n"+
507             " jdbc-type=\"INTEGER\"\n"+
508             " primarykey=\"true\"\n"+
509             " >\n"+
510             " </field-descriptor>\n"+
511             " <field-descriptor\n"+
512             " name=\"ojbConcreteClass\"\n"+
513             " column=\"ojbConcreteClass\"\n"+
514             " jdbc-type=\"VARCHAR\"\n"+
515             " length=\"256\"\n"+
516             " >\n"+
517             " </field-descriptor>\n"+
518             " <collection-descriptor\n"+
519             " name=\"attr\"\n"+
520             " element-class-ref=\"test.B\"\n"+
521             " >\n"+
522             " <inverse-foreignkey field-ref=\"aid\"/>\n"+
523             " </collection-descriptor>\n"+
524             "</class-descriptor>",
525             runOjbXDoclet(OJB_DEST_FILE));
526         assertEqualsTorqueSchemaFile(
527             "<database name=\"ojbtest\">\n"+
528             " <table name=\"A\">\n"+
529             " <column name=\"id\"\n"+
530             " javaName=\"id\"\n"+
531             " type=\"INTEGER\"\n"+
532             " primaryKey=\"true\"\n"+
533             " required=\"true\"\n"+
534             " />\n"+
535             " <column name=\"ojbConcreteClass\"\n"+
536             " javaName=\"ojbConcreteClass\"\n"+
537             " type=\"VARCHAR\"\n"+
538             " size=\"256\"\n"+
539             " />\n"+
540             " </table>\n"+
541             " <table name=\"B\">\n"+
542             " <column name=\"aid\"\n"+
543             " javaName=\"aid\"\n"+
544             " type=\"INTEGER\"\n"+
545             " />\n"+
546             " <foreign-key foreignTable=\"A\">\n"+
547             " <reference local=\"aid\" foreign=\"id\"/>\n"+
548             " </foreign-key>\n"+
549             " </table>\n"+
550             "</database>",
551             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
552     }
553 }
554
Popular Tags