KickJava   Java API By Example, From Geeks To Geeks.

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


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.class tag with the factory-class and factory-method attributes.
22  *
23  * @author <a HREF="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
24  */

25 public class ClassTagFactoryClassAndMethodAttributeTests extends OjbTestBase
26 {
27     public ClassTagFactoryClassAndMethodAttributeTests(String JavaDoc name)
28     {
29         super(name);
30     }
31     
32     // Test: factory-class with empty value
33
public void testFactoryClassAndMethod1()
34     {
35         addClass(
36             "test.A",
37             "package test;\n"+
38             "/** @ojb.class factory-class=\"\" */\n"+
39             "public class A {}\n");
40
41         assertEqualsOjbDescriptorFile(
42             "<class-descriptor\n"+
43             " class=\"test.A\"\n"+
44             " table=\"A\"\n"+
45             ">\n"+
46             "</class-descriptor>",
47             runOjbXDoclet(OJB_DEST_FILE));
48         assertEqualsTorqueSchemaFile(
49             "<database name=\"ojbtest\">\n"+
50             " <table name=\"A\">\n"+
51             " </table>\n"+
52             "</database>",
53             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
54     }
55
56     // Test: factory-class with empty value
57
public void testFactoryClassAndMethod2()
58     {
59         // note that we only recreate parts of the test class here
60
// (only the actual class is used in the method check)
61
addClass(
62             TestClass.class.getName(),
63             "package "+TestClass.class.getPackage().getName()+";\n"+
64             "/** @ojb.class factory-class=\"\"\n"+
65             " * factory-method=\"create"+TestClass.getShortName()+"\"\n"+
66             " */\n"+
67             "public class "+TestClass.getShortName()+" {}\n");
68
69         assertNull(runOjbXDoclet(OJB_DEST_FILE));
70         assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
71     }
72
73     // Test: factory-method with empty value
74
public void testFactoryClassAndMethod3()
75     {
76         // note that we only recreate parts of the test class here
77
// (only the actual class is used in the method check)
78
addClass(
79             TestClass.class.getName(),
80             "package "+TestClass.class.getPackage().getName()+";\n"+
81             "/** @ojb.class factory-class=\""+TestFactoryClass.class.getName()+"\"\n"+
82             " * factory-method=\"\"\n"+
83             " */\n"+
84             "public class "+TestClass.getShortName()+" {}\n");
85
86         assertNull(runOjbXDoclet(OJB_DEST_FILE));
87         assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
88     }
89
90     // Test: both with value
91
public void testFactoryClassAndMethod4()
92     {
93         // note that we only recreate parts of the test class here
94
// (only the actual class is used in the method check)
95
addClass(
96             TestClass.class.getName(),
97             "package "+TestClass.class.getPackage().getName()+";\n"+
98             "/** @ojb.class factory-class=\""+TestFactoryClass.class.getName()+"\"\n"+
99             " * factory-method=\"create"+TestClass.getShortName()+"\"\n"+
100             " */\n"+
101             "public class "+TestClass.getShortName()+" {}\n");
102
103         assertEqualsOjbDescriptorFile(
104             "<class-descriptor\n"+
105             " class=\""+TestClass.class.getName()+"\"\n"+
106             " table=\""+TestClass.getShortName()+"\"\n"+
107             " factory-class=\""+TestFactoryClass.class.getName()+"\"\n"+
108             " factory-method=\"create"+TestClass.getShortName()+"\"\n"+
109             ">\n"+
110             "</class-descriptor>",
111             runOjbXDoclet(OJB_DEST_FILE));
112         assertEqualsTorqueSchemaFile(
113             "<database name=\"ojbtest\">\n"+
114             " <table name=\""+TestClass.getShortName()+"\">\n"+
115             " </table>\n"+
116             "</database>",
117             runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
118     }
119
120     // Test: unknown class
121
public void testFactoryClassAndMethod5()
122     {
123         addClass(
124             "test.A",
125             "package test;\n"+
126             "/** @ojb.class factory-class=\"test.SomeClass\"\n"+
127             " * factory-method=\"someMethod\"\n"+
128             " */\n"+
129             "public class A {}\n");
130
131         assertNull(runOjbXDoclet(OJB_DEST_FILE));
132         assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
133     }
134
135     // Test: unknown method
136
public void testFactoryClassAndMethod6()
137     {
138         // note that we only recreate parts of the test class here
139
// (only the actual class is used in the method check)
140
addClass(
141             TestClass.class.getName(),
142             "package "+TestClass.class.getPackage().getName()+";\n"+
143             "/** @ojb.class factory-class=\""+TestFactoryClass.class.getName()+"\"\n"+
144             " * factory-method=\"make"+TestClass.getShortName()+"\"\n"+
145             " */\n"+
146             "public class "+TestClass.getShortName()+" {}\n");
147
148         assertNull(runOjbXDoclet(OJB_DEST_FILE));
149         assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
150     }
151
152     // Test: method exists but has parameters
153
public void testFactoryClassAndMethod7()
154     {
155         // note that we only recreate parts of the test class here
156
// (only the actual class is used in the method check)
157
addClass(
158             TestClass.class.getName(),
159             "package "+TestClass.class.getPackage().getName()+";\n"+
160             "/** @ojb.class factory-class=\""+TestFactoryClass.class.getName()+"\"\n"+
161             " * factory-method=\"createNew"+TestClass.getShortName()+"\"\n"+
162             " */\n"+
163             "public class "+TestClass.getShortName()+" {}\n");
164
165         assertNull(runOjbXDoclet(OJB_DEST_FILE));
166         assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
167     }
168
169     // Test: method exists but is not static
170
public void testFactoryClassAndMethod8()
171     {
172         // note that we only recreate parts of the test class here
173
// (only the actual class is used in the method check)
174
addClass(
175             TestClass.class.getName(),
176             "package "+TestClass.class.getPackage().getName()+";\n"+
177             "/** @ojb.class factory-class=\""+TestFactoryClass.class.getName()+"\"\n"+
178             " * factory-method=\"makeNew"+TestClass.getShortName()+"\"\n"+
179             " */\n"+
180             "public class "+TestClass.getShortName()+" {}\n");
181
182         assertNull(runOjbXDoclet(OJB_DEST_FILE));
183         assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
184     }
185
186     // Test: unknown factory-class but strict is set to false
187
public void testFactoryClassAndMethod9()
188     {
189         addClass(
190             "test.A",
191             "package test;\n"+
192             "/** @ojb.class factory-class=\"SomeClass\"\n"+
193             " * factory-method=\"someMethod\"\n"+
194             " */\n"+
195             "public class A {}\n");
196
197         HashMap JavaDoc taskProps = new HashMap JavaDoc();
198         HashMap JavaDoc torqueSubTaskProps = new HashMap JavaDoc();
199         
200         taskProps.put(PROPERTY_CHECKS, "basic");
201         torqueSubTaskProps.put(PROPERTY_DATABASENAME, "ojbtest");
202
203         assertEqualsOjbDescriptorFile(
204             "<class-descriptor\n"+
205             " class=\"test.A\"\n"+
206             " table=\"A\"\n"+
207             " factory-class=\"SomeClass\"\n"+
208             " factory-method=\"someMethod\"\n"+
209             ">\n"+
210             "</class-descriptor>",
211             runOjbXDoclet(OJB_DEST_FILE, taskProps, null));
212         assertEqualsTorqueSchemaFile(
213             "<database name=\"ojbtest\">\n"+
214             " <table name=\"A\">\n"+
215             " </table>\n"+
216             "</database>",
217             runTorqueXDoclet(TORQUE_DEST_FILE, taskProps, torqueSubTaskProps));
218     }
219 }
220
Popular Tags