KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > mockobjects > MockObjectTagsHandler


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet.modules.mockobjects;
6
7 // Java import
8
import java.text.MessageFormat JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.Comparator JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Properties JavaDoc;
14
15 // Div import
16
import org.apache.commons.logging.Log;
17
18 // XJavadoc import
19

20 import xjavadoc.XClass;
21 import xjavadoc.XExecutableMember;
22 import xjavadoc.XMember;
23 import xjavadoc.XParameter;
24
25 // XDoclet import
26
import xdoclet.DocletContext;
27 import xdoclet.DocletTask;
28 import xdoclet.XDocletException;
29 import xdoclet.modules.mockobjects.util.*;
30 import xdoclet.tagshandler.PackageTagsHandler;
31 import xdoclet.tagshandler.ParameterTagsHandler;
32 import xdoclet.util.LogUtil;
33 import xdoclet.util.TypeConversionUtil;
34
35 /**
36  * Tagshandler for mockobject.
37  *
38  * @author Joe Walnes
39  * @author Stig Jørgensen
40  * @created 5. februar 2003
41  * @xdoclet.taghandler namespace="MockObject"
42  */

43 public class MockObjectTagsHandler extends ParameterTagsHandler
44 {
45     final static Properties JavaDoc DUMMY = new Properties JavaDoc();
46
47     /**
48      * Comparator for comparing two XMembers.
49      */

50     final static Comparator JavaDoc methodComparator =
51         new Comparator JavaDoc()
52         {
53             public int compare(Object JavaDoc o1, Object JavaDoc o2)
54             {
55                 XMember m1 = (XMember) o1;
56                 XMember m2 = (XMember) o2;
57
58                 return m1.getName().compareTo(m2.getName());
59             }
60
61             public boolean equals(Object JavaDoc obj)
62             {
63                 // dumb
64
return obj == this;
65             }
66         };
67
68     /**
69      * Returns the fully classified name of the mock class from the specified class. It will do package substition (if
70      * it is specified) and use the class pattern specified to generate the class name.
71      *
72      * @param clazz the class to generate a mock classname for.
73      * @return String the fully classified name of the mock class.
74      * @throws XDocletException
75      */

76     public static String JavaDoc getMockClassFor(XClass clazz)
77          throws XDocletException
78     {
79         Log log =
80             LogUtil.getLog(MockObjectTagsHandler.class, "getMockClassFor");
81
82         // This will do package substition as specified in ant's build.xml on the subtask
83
String JavaDoc packageName =
84             PackageTagsHandler.getPackageNameFor(clazz.getContainingPackage(),
85             true);
86         String JavaDoc mockClassName =
87             clazz.getDoc().getTagAttributeValue("mock.generate", "class", false);
88
89         if (log.isDebugEnabled()) {
90             log.debug("MockObject for " + clazz.getQualifiedName());
91         }
92
93         if (mockClassName == null) {
94             String JavaDoc packagePattern = null;
95             String JavaDoc mockClassPattern = getMockClassPattern();
96
97             if (mockClassPattern.indexOf("{0}") != -1) {
98                 String JavaDoc className = clazz.getName();
99
100                 // Remove the 'I' (for interface) from the class name if it is present
101
// TODO: Come up with a better method for doing this; maybe use regexp?
102
if ((className.length() >= 2) &&
103                     (className.charAt(0) == 'I') &&
104                     Character.isUpperCase(className.charAt(1))) {
105                     className = className.substring(1);
106                 }
107
108                 mockClassName =
109                     MessageFormat.format(mockClassPattern, new Object JavaDoc[]{className});
110             }
111             else {
112                 mockClassName = mockClassPattern;
113             }
114         }
115
116         if ((mockClassName.indexOf('.') == -1) &&
117             (packageName.length() > 0)) {
118             mockClassName = packageName + "." + mockClassName;
119         }
120
121         if (log.isDebugEnabled()) {
122             log.debug("clazz.getName()=" + clazz.getName());
123             log.debug("clazz.getQualifiedName()=" + clazz.getQualifiedName());
124             log.debug("mockClassName=" + mockClassName);
125         }
126
127         return mockClassName;
128     }
129
130     /**
131      * Returns the pattern to be used for deciding the name of the class to be generated. This is retrieved from the
132      * MockObjectSubTask instance.
133      *
134      * @return String The pattern to be used for deciding the name of the class to be generated
135      */

136     protected static String JavaDoc getMockClassPattern()
137     {
138         MockObjectSubTask mockST =
139             ((MockObjectSubTask) DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(MockObjectSubTask.class)));
140
141         if (mockST != null) {
142             return mockST.getMockClassPattern();
143         }
144         else {
145             return MockObjectSubTask.DEFAULT_MOCKCLASS_PATTERN;
146         }
147     }
148
149     /**
150      * Iterates over all parameters in current method and returns a string containing the types of all those parameters.
151      *
152      * @param attributes The attributes of the template tag
153      * @return a string containing the types of all the parameters for the current method.
154      * @exception XDocletException
155      */

156     public String JavaDoc parameterTypeList(Properties JavaDoc attributes)
157          throws XDocletException
158     {
159         Collection JavaDoc parameters;
160         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
161
162         boolean constr =
163             TypeConversionUtil.stringToBoolean(attributes.getProperty("forConstructor"),
164             false);
165
166         if (constr == true) {
167             parameters = getCurrentConstructor().getParameters();
168         }
169         else {
170             parameters = getCurrentMethod().getParameters();
171         }
172
173         for (Iterator JavaDoc iter =
174             parameters.iterator(); iter.hasNext(); ) {
175             XParameter parameter = (XParameter)iter.next();
176             XClass type = parameter.getType();
177
178             if (type == null) {
179                 throw new XDocletException("FATAL: " + parameter);
180             }
181
182             sbuf.append(CodeUtils.capitalize(type.getName()));
183
184             for (int cnt = parameter.getDimension(); cnt > 0; cnt--) {
185                 sbuf.append("Array");
186             }
187         }
188
189         String JavaDoc result = sbuf.toString();
190
191         if (result.indexOf("null") != -1) {
192             throw new XDocletException("FATAL: " + result);
193         }
194
195         return result;
196     }
197
198     /**
199      * Tag for wrapping a simple type in its object counterpart.
200      *
201      * @param props holds the parameters for the tag: name & type
202      * @return code for wrapping a simple type in its object counterpart.
203      */

204     public String JavaDoc wrap(Properties JavaDoc props)
205     {
206         String JavaDoc name = props.getProperty("name");
207         String JavaDoc type = props.getProperty("type");
208
209         return CodeUtils.wrapValue(name, type);
210     }
211
212     /**
213      * Tag for unwrapping a simple type out of its object counterpart.
214      *
215      * @param props holds the parameters for the tag: name & type
216      * @return code for unwrapping a simple type out of its object counterpart.
217      */

218     public String JavaDoc unwrap(Properties JavaDoc props)
219     {
220         String JavaDoc name = props.getProperty("name");
221         String JavaDoc type = props.getProperty("type");
222
223         return CodeUtils.unwrapValue(name, type);
224     }
225
226     /**
227      * Returns the mock classname for the current class.
228      *
229      * @return String the fully classified name of the mock class.
230      * @throws XDocletException
231      * @see #getMockClassFor(XClass)
232      */

233     public String JavaDoc mockClass() throws XDocletException
234     {
235         return getMockClassFor(getCurrentClass());
236     }
237
238     /**
239      * Returns a String with the current method using the supplied template as a boilerplate.
240      *
241      * @param attributes holds the parameters for the tag: template
242      * @return String the current method concat with the param types using the supplied template as a
243      * boilerplate.
244      * @throws XDocletException not thrown.
245      */

246     public String JavaDoc uniqueMethodName(Properties JavaDoc attributes)
247          throws XDocletException
248     {
249         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
250         String JavaDoc template = attributes.getProperty("template");
251         Object JavaDoc[] args;
252
253         if (null == template) {
254             // Use a default template
255
template = "{0}{1}";
256         }
257
258         args =
259             new Object JavaDoc[]
260             {
261             CodeUtils.capitalize(getCurrentMethod().getName()),
262             parameterTypeList(DUMMY)
263             };
264
265         return MessageFormat.format(template, args);
266     }
267
268     /**
269      * Returns a String with the current method concat with the param types using the supplied template as a
270      * boilerplate.
271      *
272      * @param attributes holds the parameters for the tag: template
273      * @return String the current method concat with the param types using the supplied template as a
274      * boilerplate.
275      * @throws XDocletException not thrown.
276      */

277     public String JavaDoc uniqueMethodNameAndParam(Properties JavaDoc attributes)
278          throws XDocletException
279     {
280         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
281         String JavaDoc template = attributes.getProperty("template");
282         Object JavaDoc[] args;
283
284         if (null == template) {
285             // Use a default template
286
template = "{0}{1}{3}";
287         }
288
289         args =
290             new Object JavaDoc[]
291             {
292             CodeUtils.capitalize(getCurrentMethod().getName()),
293             parameterTypeList(DUMMY),
294             CodeUtils.capitalize(currentMethodParameter.getName())
295             };
296
297         return MessageFormat.format(template, args);
298     }
299
300     /**
301      * Iterates over all the exceptions for the current method.
302      *
303      * @param template the text inside the tag that should be processed.
304      * @param attributes the parameters for the tag; not used.
305      * @exception XDocletException not thrown by our code; might be thrown from <code>generate</code>.
306      */

307     public void forAllExceptions(String JavaDoc template, Properties JavaDoc attributes)
308          throws XDocletException
309     {
310         Collection JavaDoc exceptions;
311         XExecutableMember executableMember = getCurrentMethod();
312
313         if (executableMember == null) {
314             exceptions = new ArrayList JavaDoc();
315         }
316         else {
317             exceptions = executableMember.getThrownExceptions();
318         }
319
320         for (Iterator JavaDoc iter = exceptions.iterator(); iter.hasNext(); ) {
321             pushCurrentClass((XClass) iter.next());
322             generate(template);
323             popCurrentClass();
324         }
325     }
326
327     /**
328      * Returns the current exception.
329      *
330      * @param attributes the parameters for the tag; not used.
331      * @return
332      * @exception XDocletException not thrown by our code; might be thrown from <code>getCurrentClass</code>.
333      */

334     public String JavaDoc currentException(Properties JavaDoc attributes)
335          throws XDocletException
336     {
337         return getCurrentClass() == null ? "" : getCurrentClass().getQualifiedName();
338     }
339
340     /**
341      * Processes the text inside the tag if an exception is thrown by the current exception.
342      *
343      * @param template the text inside the tag that should be processed.
344      * @param attributes the parameters for the tag; not used.
345      * @exception XDocletException not thrown by our code; might be thrown from <code>generate</code>.
346      */

347     public void ifThrowsException(String JavaDoc template, Properties JavaDoc attributes)
348          throws XDocletException
349     {
350         Collection JavaDoc exceptions;
351         XExecutableMember executableMember = getCurrentMethod();
352
353         if (executableMember == null) {
354             exceptions = new ArrayList JavaDoc();
355         }
356         else {
357             exceptions = executableMember.getThrownExceptions();
358         }
359
360         if ((exceptions != null) && (exceptions.size() > 0)) {
361             generate(template);
362         }
363     }
364
365     protected String JavaDoc getTagParam(String JavaDoc tagName, String JavaDoc paramName,
366         String JavaDoc defaultValue)
367          throws XDocletException
368     {
369         Properties JavaDoc p = new Properties JavaDoc();
370
371         p.setProperty("tagName", tagName);
372         p.setProperty("paramName", paramName);
373         p.setProperty("default", defaultValue);
374
375         return getTagValue(p, FOR_CLASS);
376     }
377 }
378
Popular Tags