1 5 package xdoclet.modules.mockobjects; 6 7 import java.text.MessageFormat ; 9 import java.util.ArrayList ; 10 import java.util.Collection ; 11 import java.util.Comparator ; 12 import java.util.Iterator ; 13 import java.util.Properties ; 14 15 import org.apache.commons.logging.Log; 17 18 20 import xjavadoc.XClass; 21 import xjavadoc.XExecutableMember; 22 import xjavadoc.XMember; 23 import xjavadoc.XParameter; 24 25 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 43 public class MockObjectTagsHandler extends ParameterTagsHandler 44 { 45 final static Properties DUMMY = new Properties (); 46 47 50 final static Comparator methodComparator = 51 new Comparator () 52 { 53 public int compare(Object o1, Object 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 obj) 62 { 63 return obj == this; 65 } 66 }; 67 68 76 public static String getMockClassFor(XClass clazz) 77 throws XDocletException 78 { 79 Log log = 80 LogUtil.getLog(MockObjectTagsHandler.class, "getMockClassFor"); 81 82 String packageName = 84 PackageTagsHandler.getPackageNameFor(clazz.getContainingPackage(), 85 true); 86 String 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 packagePattern = null; 95 String mockClassPattern = getMockClassPattern(); 96 97 if (mockClassPattern.indexOf("{0}") != -1) { 98 String className = clazz.getName(); 99 100 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 []{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 136 protected static String 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 156 public String parameterTypeList(Properties attributes) 157 throws XDocletException 158 { 159 Collection parameters; 160 StringBuffer sbuf = new StringBuffer (); 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 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 result = sbuf.toString(); 190 191 if (result.indexOf("null") != -1) { 192 throw new XDocletException("FATAL: " + result); 193 } 194 195 return result; 196 } 197 198 204 public String wrap(Properties props) 205 { 206 String name = props.getProperty("name"); 207 String type = props.getProperty("type"); 208 209 return CodeUtils.wrapValue(name, type); 210 } 211 212 218 public String unwrap(Properties props) 219 { 220 String name = props.getProperty("name"); 221 String type = props.getProperty("type"); 222 223 return CodeUtils.unwrapValue(name, type); 224 } 225 226 233 public String mockClass() throws XDocletException 234 { 235 return getMockClassFor(getCurrentClass()); 236 } 237 238 246 public String uniqueMethodName(Properties attributes) 247 throws XDocletException 248 { 249 StringBuffer result = new StringBuffer (); 250 String template = attributes.getProperty("template"); 251 Object [] args; 252 253 if (null == template) { 254 template = "{0}{1}"; 256 } 257 258 args = 259 new Object [] 260 { 261 CodeUtils.capitalize(getCurrentMethod().getName()), 262 parameterTypeList(DUMMY) 263 }; 264 265 return MessageFormat.format(template, args); 266 } 267 268 277 public String uniqueMethodNameAndParam(Properties attributes) 278 throws XDocletException 279 { 280 StringBuffer result = new StringBuffer (); 281 String template = attributes.getProperty("template"); 282 Object [] args; 283 284 if (null == template) { 285 template = "{0}{1}{3}"; 287 } 288 289 args = 290 new Object [] 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 307 public void forAllExceptions(String template, Properties attributes) 308 throws XDocletException 309 { 310 Collection exceptions; 311 XExecutableMember executableMember = getCurrentMethod(); 312 313 if (executableMember == null) { 314 exceptions = new ArrayList (); 315 } 316 else { 317 exceptions = executableMember.getThrownExceptions(); 318 } 319 320 for (Iterator iter = exceptions.iterator(); iter.hasNext(); ) { 321 pushCurrentClass((XClass) iter.next()); 322 generate(template); 323 popCurrentClass(); 324 } 325 } 326 327 334 public String currentException(Properties attributes) 335 throws XDocletException 336 { 337 return getCurrentClass() == null ? "" : getCurrentClass().getQualifiedName(); 338 } 339 340 347 public void ifThrowsException(String template, Properties attributes) 348 throws XDocletException 349 { 350 Collection exceptions; 351 XExecutableMember executableMember = getCurrentMethod(); 352 353 if (executableMember == null) { 354 exceptions = new ArrayList (); 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 getTagParam(String tagName, String paramName, 366 String defaultValue) 367 throws XDocletException 368 { 369 Properties p = new Properties (); 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 |