KickJava   Java API By Example, From Geeks To Geeks.

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


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

23 public class FieldTagConversionAttributeTests extends OjbTestBase
24 {
25     public FieldTagConversionAttributeTests(String JavaDoc name)
26     {
27         super(name);
28     }
29
30     // Test: conversion attribute with no value
31
public void testConversion1()
32     {
33         addClass(
34             "test.A",
35             "package test;\n"+
36             "/** @ojb.class */\n"+
37             "public class A {\n"+
38             "/** @ojb.field conversion=\"\" */\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: conversion attribute with name of unknown class
68
public void testConversion2()
69     {
70         addClass(
71             "test.A",
72             "package test;\n"+
73             "/** @ojb.class */\n"+
74             "public class A {\n"+
75             "/** @ojb.field conversion=\"SomeConversion\" */\n"+
76             " private int attr;\n"+
77             "}\n");
78
79         assertNull(runOjbXDoclet(OJB_DEST_FILE));
80         assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
81     }
82
83     // Test: conversion attribute with name of predefined conversion when no jdbc type is specified
84
public void testConversion3()
85     {
86         addClass(
87             "test.A",
88             "package test;\n"+
89             "/** @ojb.class */\n"+
90             "public class A {\n"+
91             "/** @ojb.field conversion=\"org.apache.ojb.broker.accesslayer.conversions.Object2Base64StringFieldConversion\" */\n"+
92             " private String attr;\n"+
93             "}\n");
94
95         assertEqualsOjbDescriptorFile(
96             "<class-descriptor\n"+
97             " class=\"test.A\"\n"+
98             " table=\"A\"\n"+
99             ">\n"+
100             " <field-descriptor\n"+
101             " name=\"attr\"\n"+
102             " column=\"attr\"\n"+
103             " jdbc-type=\"VARCHAR\"\n"+
104             " conversion=\"org.apache.ojb.broker.accesslayer.conversions.Object2Base64StringFieldConversion\"\n"+
105             " length=\"254\"\n"+
106             " >\n"+
107             " </field-descriptor>\n"+
108             "</class-descriptor>",
109             runOjbXDoclet(OJB_DEST_FILE));
110         assertEqualsTorqueSchemaFile(
111             "<database name=\"ojbtest\">\n"+
112             " <table name=\"A\">\n"+
113             " <column name=\"attr\"\n"+
114             " javaName=\"attr\"\n"+
115             " type=\"VARCHAR\"\n"+
116             " size=\"254\"\n"+
117             " />\n"+
118             " </table>\n"+
119             "</database>",
120             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
121     }
122
123     // Test: conversion attribute with name of predefined conversion when no jdbc type is specified and a predefined conversion
124
// would be used otherwise
125
public void testConversion4()
126     {
127         addClass(
128             "test.A",
129             "package test;\n"+
130             "/** @ojb.class */\n"+
131             "public class A {\n"+
132             "/** @ojb.field conversion=\"org.apache.ojb.broker.accesslayer.conversions.Object2Base64StringFieldConversion\" */\n"+
133             " private org.apache.ojb.broker.util.GUID attr;\n"+
134             "}\n");
135
136         assertEqualsOjbDescriptorFile(
137             "<class-descriptor\n"+
138             " class=\"test.A\"\n"+
139             " table=\"A\"\n"+
140             ">\n"+
141             " <field-descriptor\n"+
142             " name=\"attr\"\n"+
143             " column=\"attr\"\n"+
144             " jdbc-type=\"VARCHAR\"\n"+
145             " conversion=\"org.apache.ojb.broker.accesslayer.conversions.Object2Base64StringFieldConversion\"\n"+
146             " length=\"254\"\n"+
147             " >\n"+
148             " </field-descriptor>\n"+
149             "</class-descriptor>",
150             runOjbXDoclet(OJB_DEST_FILE));
151         assertEqualsTorqueSchemaFile(
152             "<database name=\"ojbtest\">\n"+
153             " <table name=\"A\">\n"+
154             " <column name=\"attr\"\n"+
155             " javaName=\"attr\"\n"+
156             " type=\"VARCHAR\"\n"+
157             " size=\"254\"\n"+
158             " />\n"+
159             " </table>\n"+
160             "</database>",
161             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
162     }
163
164     // Test: conversion attribute with name of predefined conversion when a jdbc type is specified
165
public void testConversion5()
166     {
167         addClass(
168             "test.A",
169             "package test;\n"+
170             "/** @ojb.class */\n"+
171             "public class A {\n"+
172             "/** @ojb.field jdbc-type=\"INTEGER\"\n"+
173             " conversion=\"org.apache.ojb.broker.accesslayer.conversions.Boolean2IntFieldConversion\" */\n"+
174             " private boolean attr;\n"+
175             "}\n");
176
177         assertEqualsOjbDescriptorFile(
178             "<class-descriptor\n"+
179             " class=\"test.A\"\n"+
180             " table=\"A\"\n"+
181             ">\n"+
182             " <field-descriptor\n"+
183             " name=\"attr\"\n"+
184             " column=\"attr\"\n"+
185             " jdbc-type=\"INTEGER\"\n"+
186             " conversion=\"org.apache.ojb.broker.accesslayer.conversions.Boolean2IntFieldConversion\"\n"+
187             " >\n"+
188             " </field-descriptor>\n"+
189             "</class-descriptor>",
190             runOjbXDoclet(OJB_DEST_FILE));
191         assertEqualsTorqueSchemaFile(
192             "<database name=\"ojbtest\">\n"+
193             " <table name=\"A\">\n"+
194             " <column name=\"attr\"\n"+
195             " javaName=\"attr\"\n"+
196             " type=\"INTEGER\"\n"+
197             " />\n"+
198             " </table>\n"+
199             "</database>",
200             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
201     }
202
203     // Test: conversion attribute with name of userdefined conversion when no jdbc type is specified
204
public void testConversion6()
205     {
206         addClass(
207             "test.A",
208             "package test;\n"+
209             "/** @ojb.class */\n"+
210             "public class A {\n"+
211             "/** @ojb.field conversion=\""+TestConversionClass.class.getName()+"\" */\n"+
212             " private String attr;\n"+
213             "}\n");
214
215         assertEqualsOjbDescriptorFile(
216             "<class-descriptor\n"+
217             " class=\"test.A\"\n"+
218             " table=\"A\"\n"+
219             ">\n"+
220             " <field-descriptor\n"+
221             " name=\"attr\"\n"+
222             " column=\"attr\"\n"+
223             " jdbc-type=\"VARCHAR\"\n"+
224             " conversion=\""+TestConversionClass.class.getName()+"\"\n"+
225             " length=\"254\"\n"+
226             " >\n"+
227             " </field-descriptor>\n"+
228             "</class-descriptor>",
229             runOjbXDoclet(OJB_DEST_FILE));
230         assertEqualsTorqueSchemaFile(
231             "<database name=\"ojbtest\">\n"+
232             " <table name=\"A\">\n"+
233             " <column name=\"attr\"\n"+
234             " javaName=\"attr\"\n"+
235             " type=\"VARCHAR\"\n"+
236             " size=\"254\"\n"+
237             " />\n"+
238             " </table>\n"+
239             "</database>",
240             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
241     }
242
243     // Test: conversion attribute with name of userdefined conversion when no jdbc type is specified and a predefined conversion
244
// would be used otherwise
245
public void testConversion7()
246     {
247         addClass(
248             "test.A",
249             "package test;\n"+
250             "/** @ojb.class */\n"+
251             "public class A {\n"+
252             "/** @ojb.field conversion=\""+TestConversionClass.class.getName()+"\" */\n"+
253             " private org.apache.ojb.broker.util.GUID attr;\n"+
254             "}\n");
255
256         assertEqualsOjbDescriptorFile(
257             "<class-descriptor\n"+
258             " class=\"test.A\"\n"+
259             " table=\"A\"\n"+
260             ">\n"+
261             " <field-descriptor\n"+
262             " name=\"attr\"\n"+
263             " column=\"attr\"\n"+
264             " jdbc-type=\"VARCHAR\"\n"+
265             " conversion=\""+TestConversionClass.class.getName()+"\"\n"+
266             " length=\"254\"\n"+
267             " >\n"+
268             " </field-descriptor>\n"+
269             "</class-descriptor>",
270             runOjbXDoclet(OJB_DEST_FILE));
271         assertEqualsTorqueSchemaFile(
272             "<database name=\"ojbtest\">\n"+
273             " <table name=\"A\">\n"+
274             " <column name=\"attr\"\n"+
275             " javaName=\"attr\"\n"+
276             " type=\"VARCHAR\"\n"+
277             " size=\"254\"\n"+
278             " />\n"+
279             " </table>\n"+
280             "</database>",
281             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
282     }
283
284     // Test: conversion attribute with name of user-defined conversion when a jdbc type is specified
285
public void testConversion8()
286     {
287         addClass(
288             "test.A",
289             "package test;\n"+
290             "/** @ojb.class */\n"+
291             "public class A {\n"+
292             "/** @ojb.field jdbc-type=\"INTEGER\"\n"+
293             " conversion=\""+TestConversionClass.class.getName()+"\" */\n"+
294             " private boolean attr;\n"+
295             "}\n");
296
297         assertEqualsOjbDescriptorFile(
298             "<class-descriptor\n"+
299             " class=\"test.A\"\n"+
300             " table=\"A\"\n"+
301             ">\n"+
302             " <field-descriptor\n"+
303             " name=\"attr\"\n"+
304             " column=\"attr\"\n"+
305             " jdbc-type=\"INTEGER\"\n"+
306             " conversion=\""+TestConversionClass.class.getName()+"\"\n"+
307             " >\n"+
308             " </field-descriptor>\n"+
309             "</class-descriptor>",
310             runOjbXDoclet(OJB_DEST_FILE));
311         assertEqualsTorqueSchemaFile(
312             "<database name=\"ojbtest\">\n"+
313             " <table name=\"A\">\n"+
314             " <column name=\"attr\"\n"+
315             " javaName=\"attr\"\n"+
316             " type=\"INTEGER\"\n"+
317             " />\n"+
318             " </table>\n"+
319             "</database>",
320             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
321     }
322
323     // Test: conversion attribute with name of invalid userdefined conversion when no jdbc type is specified
324
public void testConversion9()
325     {
326         addClass(
327             "test.Conversion",
328             "package test;\n"+
329             "public class Conversion {}\n");
330         addClass(
331             "test.A",
332             "package test;\n"+
333             "/** @ojb.class */\n"+
334             "public class A {\n"+
335             "/** @ojb.field conversion=\"java.lang.String\" */\n"+
336             " private String attr;\n"+
337             "}\n");
338
339         assertNull(runOjbXDoclet(OJB_DEST_FILE));
340         assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
341     }
342
343     // Test: inherited conversion attribute with name of predefined conversion when a jdbc type is specified
344
public void testConversion10()
345     {
346         addClass(
347             "test.A",
348             "package test;\n"+
349             "/** @ojb.class */\n"+
350             "public class A {\n"+
351             "/** @ojb.field jdbc-type=\"INTEGER\"\n"+
352             " * conversion=\"org.apache.ojb.broker.accesslayer.conversions.Boolean2IntFieldConversion\"\n"+
353             " */\n"+
354             " private boolean attr;\n"+
355             "}\n");
356         addClass(
357             "test.B",
358             "package test;\n"+
359             "public class B extends A {}\n");
360         addClass(
361             "test.C",
362             "package test;\n"+
363             "/** @ojb.class */\n"+
364             "public class C extends B {}\n");
365
366         assertEqualsOjbDescriptorFile(
367             "<class-descriptor\n"+
368             " class=\"test.A\"\n"+
369             " table=\"A\"\n"+
370             ">\n"+
371             " <extent-class class-ref=\"test.C\"/>\n"+
372             " <field-descriptor\n"+
373             " name=\"attr\"\n"+
374             " column=\"attr\"\n"+
375             " jdbc-type=\"INTEGER\"\n"+
376             " conversion=\"org.apache.ojb.broker.accesslayer.conversions.Boolean2IntFieldConversion\"\n"+
377             " >\n"+
378             " </field-descriptor>\n"+
379             "</class-descriptor>\n"+
380             "<class-descriptor\n"+
381             " class=\"test.C\"\n"+
382             " table=\"C\"\n"+
383             ">\n"+
384             " <field-descriptor\n"+
385             " name=\"attr\"\n"+
386             " column=\"attr\"\n"+
387             " jdbc-type=\"INTEGER\"\n"+
388             " conversion=\"org.apache.ojb.broker.accesslayer.conversions.Boolean2IntFieldConversion\"\n"+
389             " >\n"+
390             " </field-descriptor>\n"+
391             "</class-descriptor>",
392             runOjbXDoclet(OJB_DEST_FILE));
393         assertEqualsTorqueSchemaFile(
394             "<database name=\"ojbtest\">\n"+
395             " <table name=\"A\">\n"+
396             " <column name=\"attr\"\n"+
397             " javaName=\"attr\"\n"+
398             " type=\"INTEGER\"\n"+
399             " />\n"+
400             " </table>\n"+
401             " <table name=\"C\">\n"+
402             " <column name=\"attr\"\n"+
403             " javaName=\"attr\"\n"+
404             " type=\"INTEGER\"\n"+
405             " />\n"+
406             " </table>\n"+
407             "</database>",
408             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
409     }
410 }
411
Popular Tags