KickJava   Java API By Example, From Geeks To Geeks.

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


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.field tag with the length attribute
20  *
21  * @author <a HREF="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
22  */

23 public class FieldTagLengthAttributeTests extends OjbTestBase
24 {
25     public FieldTagLengthAttributeTests(String JavaDoc name)
26     {
27         super(name);
28     }
29
30     // Test: length attribute with no value for jdbc type without default length value
31
public void testLength1()
32     {
33         addClass(
34             "test.A",
35             "package test;\n"+
36             "/** @ojb.class */\n"+
37             "public class A {\n"+
38             "/** @ojb.field length=\"\" */\n"+
39             " private int attr;\n"+
40             "}\n");
41
42         assertEqualsOjbDescriptorFile(
43             "<class-descriptor\n"+
44             " class=\"test.A\"\n"+
45             " table=\"A\"\n"+
46             ">\n"+
47             " <field-descriptor\n"+
48             " name=\"attr\"\n"+
49             " column=\"attr\"\n"+
50             " jdbc-type=\"INTEGER\"\n"+
51             " >\n"+
52             " </field-descriptor>\n"+
53             "</class-descriptor>",
54             runOjbXDoclet(OJB_DEST_FILE));
55         assertEqualsTorqueSchemaFile(
56             "<database name=\"ojbtest\">\n"+
57             " <table name=\"A\">\n"+
58             " <column name=\"attr\"\n"+
59             " javaName=\"attr\"\n"+
60             " type=\"INTEGER\"\n"+
61             " />\n"+
62             " </table>\n"+
63             "</database>",
64             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
65     }
66
67     // Test: length attribute with no value for jdbc type with default length value
68
public void testLength2()
69     {
70         addClass(
71             "test.A",
72             "package test;\n"+
73             "/** @ojb.class */\n"+
74             "public class A {\n"+
75             "/** @ojb.field length=\"\" */\n"+
76             " private String attr;\n"+
77             "}\n");
78
79         assertEqualsOjbDescriptorFile(
80             "<class-descriptor\n"+
81             " class=\"test.A\"\n"+
82             " table=\"A\"\n"+
83             ">\n"+
84             " <field-descriptor\n"+
85             " name=\"attr\"\n"+
86             " column=\"attr\"\n"+
87             " jdbc-type=\"VARCHAR\"\n"+
88             " length=\"254\"\n"+
89             " >\n"+
90             " </field-descriptor>\n"+
91             "</class-descriptor>",
92             runOjbXDoclet(OJB_DEST_FILE));
93         assertEqualsTorqueSchemaFile(
94             "<database name=\"ojbtest\">\n"+
95             " <table name=\"A\">\n"+
96             " <column name=\"attr\"\n"+
97             " javaName=\"attr\"\n"+
98             " type=\"VARCHAR\"\n"+
99             " size=\"254\"\n"+
100             " />\n"+
101             " </table>\n"+
102             "</database>",
103             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
104     }
105
106     // Test: length attribute with no value for non-default jdbc type with default length value
107
public void testLength3()
108     {
109         addClass(
110             "test.A",
111             "package test;\n"+
112             "/** @ojb.class */\n"+
113             "public class A {\n"+
114             "/** @ojb.field jdbc-type=\"VARCHAR\"\n"+
115             " * conversion=\"org.apache.ojb.broker.accesslayer.conversions.Object2Base64StringFieldConversion\"\n"+
116             " * length=\"\"\n"+
117             " */\n"+
118             " private Object attr;\n"+
119             "}\n");
120
121         assertEqualsOjbDescriptorFile(
122             "<class-descriptor\n"+
123             " class=\"test.A\"\n"+
124             " table=\"A\"\n"+
125             ">\n"+
126             " <field-descriptor\n"+
127             " name=\"attr\"\n"+
128             " column=\"attr\"\n"+
129             " jdbc-type=\"VARCHAR\"\n"+
130             " conversion=\"org.apache.ojb.broker.accesslayer.conversions.Object2Base64StringFieldConversion\"\n"+
131             " length=\"254\"\n"+
132             " >\n"+
133             " </field-descriptor>\n"+
134             "</class-descriptor>",
135             runOjbXDoclet(OJB_DEST_FILE));
136         assertEqualsTorqueSchemaFile(
137             "<database name=\"ojbtest\">\n"+
138             " <table name=\"A\">\n"+
139             " <column name=\"attr\"\n"+
140             " javaName=\"attr\"\n"+
141             " type=\"VARCHAR\"\n"+
142             " size=\"254\"\n"+
143             " />\n"+
144             " </table>\n"+
145             "</database>",
146             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
147     }
148
149     // Test: length attribute overriding value default length value for default jdbc type
150
public void testLength4()
151     {
152         addClass(
153             "test.A",
154             "package test;\n"+
155             "/** @ojb.class */\n"+
156             "public class A {\n"+
157             "/** @ojb.field length=\"128\" */\n"+
158             " private char attr;\n"+
159             "}\n");
160
161         assertEqualsOjbDescriptorFile(
162             "<class-descriptor\n"+
163             " class=\"test.A\"\n"+
164             " table=\"A\"\n"+
165             ">\n"+
166             " <field-descriptor\n"+
167             " name=\"attr\"\n"+
168             " column=\"attr\"\n"+
169             " jdbc-type=\"CHAR\"\n"+
170             " length=\"128\"\n"+
171             " >\n"+
172             " </field-descriptor>\n"+
173             "</class-descriptor>",
174             runOjbXDoclet(OJB_DEST_FILE));
175         assertEqualsTorqueSchemaFile(
176             "<database name=\"ojbtest\">\n"+
177             " <table name=\"A\">\n"+
178             " <column name=\"attr\"\n"+
179             " javaName=\"attr\"\n"+
180             " type=\"CHAR\"\n"+
181             " size=\"128\"\n"+
182             " />\n"+
183             " </table>\n"+
184             "</database>",
185             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
186     }
187
188     // Test: length attribute overriding value default length value for default jdbc type
189
public void testLength5()
190     {
191         addClass(
192             "test.A",
193             "package test;\n"+
194             "/** @ojb.class */\n"+
195             "public class A {\n"+
196             "/** @ojb.field length=\"128\" */\n"+
197             " private String attr;\n"+
198             "}\n");
199
200         assertEqualsOjbDescriptorFile(
201             "<class-descriptor\n"+
202             " class=\"test.A\"\n"+
203             " table=\"A\"\n"+
204             ">\n"+
205             " <field-descriptor\n"+
206             " name=\"attr\"\n"+
207             " column=\"attr\"\n"+
208             " jdbc-type=\"VARCHAR\"\n"+
209             " length=\"128\"\n"+
210             " >\n"+
211             " </field-descriptor>\n"+
212             "</class-descriptor>",
213             runOjbXDoclet(OJB_DEST_FILE));
214         assertEqualsTorqueSchemaFile(
215             "<database name=\"ojbtest\">\n"+
216             " <table name=\"A\">\n"+
217             " <column name=\"attr\"\n"+
218             " javaName=\"attr\"\n"+
219             " type=\"VARCHAR\"\n"+
220             " size=\"128\"\n"+
221             " />\n"+
222             " </table>\n"+
223             "</database>",
224             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
225     }
226
227     // Test: length attribute with value for specified jdbc type
228
public void testLength6()
229     {
230         addClass(
231             "test.A",
232             "package test;\n"+
233             "/** @ojb.class */\n"+
234             "public class A {\n"+
235             "/** @ojb.field jdbc-type=\"CHAR\"\n"+
236             " * length=\"4\"\n"+
237             " */\n"+
238             " private int attr;\n"+
239             "}\n");
240
241         assertEqualsOjbDescriptorFile(
242             "<class-descriptor\n"+
243             " class=\"test.A\"\n"+
244             " table=\"A\"\n"+
245             ">\n"+
246             " <field-descriptor\n"+
247             " name=\"attr\"\n"+
248             " column=\"attr\"\n"+
249             " jdbc-type=\"CHAR\"\n"+
250             " length=\"4\"\n"+
251             " >\n"+
252             " </field-descriptor>\n"+
253             "</class-descriptor>",
254             runOjbXDoclet(OJB_DEST_FILE));
255         assertEqualsTorqueSchemaFile(
256             "<database name=\"ojbtest\">\n"+
257             " <table name=\"A\">\n"+
258             " <column name=\"attr\"\n"+
259             " javaName=\"attr\"\n"+
260             " type=\"CHAR\"\n"+
261             " size=\"4\"\n"+
262             " />\n"+
263             " </table>\n"+
264             "</database>",
265             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
266     }
267
268     // Test: length attribute with value for specified jdbc type
269
public void testLength7()
270     {
271         addClass(
272             "test.A",
273             "package test;\n"+
274             "/** @ojb.class */\n"+
275             "public class A {\n"+
276             "/** @ojb.field jdbc-type=\"LONGVARCHAR\"\n"+
277             " * length=\"1024\"\n"+
278             " */\n"+
279             " private String attr;\n"+
280             "}\n");
281
282         assertEqualsOjbDescriptorFile(
283             "<class-descriptor\n"+
284             " class=\"test.A\"\n"+
285             " table=\"A\"\n"+
286             ">\n"+
287             " <field-descriptor\n"+
288             " name=\"attr\"\n"+
289             " column=\"attr\"\n"+
290             " jdbc-type=\"LONGVARCHAR\"\n"+
291             " length=\"1024\"\n"+
292             " >\n"+
293             " </field-descriptor>\n"+
294             "</class-descriptor>",
295             runOjbXDoclet(OJB_DEST_FILE));
296         assertEqualsTorqueSchemaFile(
297             "<database name=\"ojbtest\">\n"+
298             " <table name=\"A\">\n"+
299             " <column name=\"attr\"\n"+
300             " javaName=\"attr\"\n"+
301             " type=\"LONGVARCHAR\"\n"+
302             " size=\"1024\"\n"+
303             " />\n"+
304             " </table>\n"+
305             "</database>",
306             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
307     }
308
309     // Test: length attribute with value for specified jdbc type
310
// Note that the length setting does not really make sense here, but the check should be
311
// performed by the JDBC driver, not the xdoclet ojb module
312
public void testLength10()
313     {
314         addClass(
315             "test.A",
316             "package test;\n"+
317             "/** @ojb.class */\n"+
318             "public class A {\n"+
319             "/** @ojb.field jdbc-type=\"DOUBLE\"\n"+
320             " * length=\"10\"\n"+
321             " */\n"+
322             " private float attr;\n"+
323             "}\n");
324
325         assertEqualsOjbDescriptorFile(
326             "<class-descriptor\n"+
327             " class=\"test.A\"\n"+
328             " table=\"A\"\n"+
329             ">\n"+
330             " <field-descriptor\n"+
331             " name=\"attr\"\n"+
332             " column=\"attr\"\n"+
333             " jdbc-type=\"DOUBLE\"\n"+
334             " length=\"10\"\n"+
335             " >\n"+
336             " </field-descriptor>\n"+
337             "</class-descriptor>",
338             runOjbXDoclet(OJB_DEST_FILE));
339         assertEqualsTorqueSchemaFile(
340             "<database name=\"ojbtest\">\n"+
341             " <table name=\"A\">\n"+
342             " <column name=\"attr\"\n"+
343             " javaName=\"attr\"\n"+
344             " type=\"DOUBLE\"\n"+
345             " size=\"10\"\n"+
346             " />\n"+
347             " </table>\n"+
348             "</database>",
349             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
350     }
351
352     // Test: inherited length attribute
353
public void testLength11()
354     {
355         addClass(
356             "test.A",
357             "package test;\n"+
358             "/** @ojb.class */\n"+
359             "public class A {\n"+
360             "/** @ojb.field length=\"128\" */\n"+
361             " private String attr;\n"+
362             "}\n");
363         addClass(
364             "test.B",
365             "package test;\n"+
366             "public class B extends A {}\n");
367         addClass(
368             "test.C",
369             "package test;\n"+
370             "/** @ojb.class */\n"+
371             "public class C extends B {}\n");
372
373         assertEqualsOjbDescriptorFile(
374             "<class-descriptor\n"+
375             " class=\"test.A\"\n"+
376             " table=\"A\"\n"+
377             ">\n"+
378             " <extent-class class-ref=\"test.C\"/>\n"+
379             " <field-descriptor\n"+
380             " name=\"attr\"\n"+
381             " column=\"attr\"\n"+
382             " jdbc-type=\"VARCHAR\"\n"+
383             " length=\"128\"\n"+
384             " >\n"+
385             " </field-descriptor>\n"+
386             "</class-descriptor>\n"+
387             "<class-descriptor\n"+
388             " class=\"test.C\"\n"+
389             " table=\"C\"\n"+
390             ">\n"+
391             " <field-descriptor\n"+
392             " name=\"attr\"\n"+
393             " column=\"attr\"\n"+
394             " jdbc-type=\"VARCHAR\"\n"+
395             " length=\"128\"\n"+
396             " >\n"+
397             " </field-descriptor>\n"+
398             "</class-descriptor>",
399             runOjbXDoclet(OJB_DEST_FILE));
400         assertEqualsTorqueSchemaFile(
401             "<database name=\"ojbtest\">\n"+
402             " <table name=\"A\">\n"+
403             " <column name=\"attr\"\n"+
404             " javaName=\"attr\"\n"+
405             " type=\"VARCHAR\"\n"+
406             " size=\"128\"\n"+
407             " />\n"+
408             " </table>\n"+
409             " <table name=\"C\">\n"+
410             " <column name=\"attr\"\n"+
411             " javaName=\"attr\"\n"+
412             " type=\"VARCHAR\"\n"+
413             " size=\"128\"\n"+
414             " />\n"+
415             " </table>\n"+
416             "</database>",
417             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
418     }
419
420     // Test: default type for field would have length attribute, but specified type has not
421
public void testLength12()
422     {
423         addClass(
424             "test.A",
425             "package test;\n"+
426             "/** @ojb.class */\n"+
427             "public class A {\n"+
428             "/** @ojb.field jdbc-type=\"LONGVARCHAR\" */\n"+
429             " private String attr;\n"+
430             "}\n");
431
432         assertEqualsOjbDescriptorFile(
433             "<class-descriptor\n"+
434             " class=\"test.A\"\n"+
435             " table=\"A\"\n"+
436             ">\n"+
437             " <field-descriptor\n"+
438             " name=\"attr\"\n"+
439             " column=\"attr\"\n"+
440             " jdbc-type=\"LONGVARCHAR\"\n"+
441             " >\n"+
442             " </field-descriptor>\n"+
443             "</class-descriptor>",
444             runOjbXDoclet(OJB_DEST_FILE));
445         assertEqualsTorqueSchemaFile(
446             "<database name=\"ojbtest\">\n"+
447             " <table name=\"A\">\n"+
448             " <column name=\"attr\"\n"+
449             " javaName=\"attr\"\n"+
450             " type=\"LONGVARCHAR\"\n"+
451             " />\n"+
452             " </table>\n"+
453             "</database>",
454             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
455     }
456 }
457
Popular Tags