1 8 package org.codehaus.dna.tools.metaclass; 9 10 import com.thoughtworks.qdox.model.JavaMethod; 11 import java.util.Properties ; 12 import junit.framework.TestCase; 13 14 import org.codehaus.dna.tools.metaclass.DNAAttributeInterceptor; 15 import org.codehaus.metaclass.model.Attribute; 16 17 22 public class DNAAttributeInterceptorTestCase 23 extends TestCase 24 { 25 public void testProcessClassAttributeWithoutTransformations() 26 throws Exception  27 { 28 final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor(); 29 final Attribute attribute = new Attribute( "ignored" ); 30 final Attribute result = 31 interceptor.processClassAttribute( new MockJavaClass(), attribute ); 32 assertNotNull( "attribute", result ); 33 assertEquals( "attribute.name", "ignored", result.getName() ); 34 assertEquals( "attribute.value", null, result.getValue() ); 35 assertEquals( "attribute.parameterCount", 0, result.getParameterCount() ); 36 } 37 38 public void testProcessClassAttributeWithDNAService() 39 throws Exception  40 { 41 final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor(); 42 final Properties parameters = new Properties (); 43 parameters.setProperty( "type", "X" ); 44 final Attribute attribute = new Attribute( "dna.service", parameters ); 45 final Attribute result = 46 interceptor.processClassAttribute( new MockJavaClass(), attribute ); 47 assertNotNull( "attribute", result ); 48 assertEquals( "attribute.name", "dna.service", result.getName() ); 49 assertEquals( "attribute.value", null, result.getValue() ); 50 assertEquals( "attribute.parameterCount", 1, result.getParameterCount() ); 51 assertEquals( "attribute.parameter(type)", 52 MockJavaSource.PREFIX + "X", result.getParameter( "type" ) ); 53 } 54 55 public void testProcessClassAttributeWithDNAServiceMissingType() 56 throws Exception  57 { 58 final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor(); 59 final Attribute attribute = new Attribute( "dna.service" ); 60 try 61 { 62 final MockJavaClass clazz = new MockJavaClass(); 63 clazz.setName( "Blah" ); 64 interceptor.processClassAttribute( clazz, attribute ); 65 } 66 catch( final IllegalArgumentException iae ) 67 { 68 return; 69 } 70 fail( "Expected to fail to process dna.service " + 71 "attribute missing type parameter" ); 72 } 73 74 public void testProcessMethodAttributeWithDNAConfigurationWith() 75 throws Exception  76 { 77 final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor(); 78 final Attribute attribute = new Attribute( "ignored" ); 79 final Attribute result = 80 interceptor.processMethodAttribute( new JavaMethod(), attribute ); 81 assertNotNull( "attribute", result ); 82 assertEquals( "attribute.name", "ignored", result.getName() ); 83 assertEquals( "attribute.value", null, result.getValue() ); 84 assertEquals( "attribute.parameterCount", 0, result.getParameterCount() ); 85 } 86 87 public void testProcessMethodAttributeWithoutTransformations() 88 throws Exception  89 { 90 final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor(); 91 final Attribute attribute = new Attribute( "ignored" ); 92 final Attribute result = 93 interceptor.processMethodAttribute( new JavaMethod(), attribute ); 94 assertNotNull( "attribute", result ); 95 assertEquals( "attribute.name", "ignored", result.getName() ); 96 assertEquals( "attribute.value", null, result.getValue() ); 97 assertEquals( "attribute.parameterCount", 0, result.getParameterCount() ); 98 } 99 100 public void testProcessMethodForConfiguration() 101 throws Exception  102 { 103 final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor(); 104 105 final MockJavaClass clazz = new MockJavaClass(); 106 clazz.setName( "Blah" ); 107 108 final JavaMethod javaMethod = new JavaMethod(); 109 javaMethod.setParentClass( clazz ); 110 final Attribute attribute = new Attribute( "dna.configuration" ); 111 final Attribute result = 112 interceptor.processMethodAttribute( javaMethod, attribute ); 113 assertNotNull( "attribute", result ); 114 assertEquals( "attribute.name", "dna.configuration", result.getName() ); 115 assertEquals( "attribute.value", null, result.getValue() ); 116 assertEquals( "attribute.parameterCount", 1, result.getParameterCount() ); 117 assertEquals( "attribute.parameter(location)", 118 "Blah-schema.xml", result.getParameter( "location" ) ); 119 } 120 121 public void testProcessMethodForParameters() 122 throws Exception  123 { 124 final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor(); 125 126 final MockJavaClass clazz = new MockJavaClass(); 127 clazz.setName( "Blah" ); 128 129 final JavaMethod javaMethod = new JavaMethod(); 130 javaMethod.setParentClass( clazz ); 131 final Attribute attribute = new Attribute( "dna.parameters" ); 132 final Attribute result = 133 interceptor.processMethodAttribute( javaMethod, attribute ); 134 assertNotNull( "attribute", result ); 135 assertEquals( "attribute.name", "dna.parameters", result.getName() ); 136 assertEquals( "attribute.value", null, result.getValue() ); 137 assertEquals( "attribute.parameterCount", 1, result.getParameterCount() ); 138 assertEquals( "attribute.parameter(location)", 139 "Blah-schema.xml", result.getParameter( "location" ) ); 140 } 141 142 public void testProcessMethodForDependencySpecifyingOptionalAttribute() 143 throws Exception  144 { 145 final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor(); 146 final Properties parameters = new Properties (); 147 parameters.setProperty( "type", "Foo" ); 148 parameters.setProperty( "optional", "true" ); 149 final Attribute attribute = new Attribute( "dna.dependency", parameters ); 150 final JavaMethod method = new JavaMethod(); 151 method.setParentClass( new MockJavaClass() ); 152 final Attribute result = 153 interceptor.processMethodAttribute( method, attribute ); 154 assertNotNull( "attribute", result ); 155 assertEquals( "attribute.name", "dna.dependency", result.getName() ); 156 assertEquals( "attribute.value", null, result.getValue() ); 157 assertEquals( "attribute.parameterCount", 3, result.getParameterCount() ); 158 assertEquals( "attribute.parameter(type)", 159 MockJavaSource.PREFIX + "Foo", 160 result.getParameter( "type" ) ); 161 assertEquals( "attribute.parameter(key)", 162 MockJavaSource.PREFIX + "Foo", 163 result.getParameter( "key" ) ); 164 assertEquals( "attribute.parameter(optional)", 165 "true", 166 result.getParameter( "optional" ) ); 167 } 168 169 public void testProcessMethodForDependencyNotSpecifyingType() 170 throws Exception  171 { 172 final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor(); 173 final Properties parameters = new Properties (); 174 final Attribute attribute = new Attribute( "dna.dependency", parameters ); 175 final JavaMethod method = new JavaMethod(); 176 method.setParentClass( new MockJavaClass() ); 177 try 178 { 179 interceptor.processMethodAttribute( method, attribute ); 180 } 181 catch( final IllegalArgumentException iae ) 182 { 183 return; 184 } 185 fail( "Expected to fail as dna.dependency is missing typ[e parameter" ); 186 } 187 188 public void testProcessMethodForDependencySpecifyingJustType() 189 throws Exception  190 { 191 final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor(); 192 final Properties parameters = new Properties (); 193 parameters.setProperty( "type", "Foo" ); 194 final Attribute attribute = new Attribute( "dna.dependency", parameters ); 195 final JavaMethod method = new JavaMethod(); 196 method.setParentClass( new MockJavaClass() ); 197 final Attribute result = 198 interceptor.processMethodAttribute( method, attribute ); 199 assertNotNull( "attribute", result ); 200 assertEquals( "attribute.name", "dna.dependency", result.getName() ); 201 assertEquals( "attribute.value", null, result.getValue() ); 202 assertEquals( "attribute.parameterCount", 3, result.getParameterCount() ); 203 assertEquals( "attribute.parameter(type)", 204 MockJavaSource.PREFIX + "Foo", 205 result.getParameter( "type" ) ); 206 assertEquals( "attribute.parameter(key)", 207 MockJavaSource.PREFIX + "Foo", 208 result.getParameter( "key" ) ); 209 assertEquals( "attribute.parameter(optional)", 210 "false", 211 result.getParameter( "optional" ) ); 212 } 213 214 public void testProcessConfigurationAttributeWithoutLocationOrType() 215 throws Exception  216 { 217 final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor(); 218 219 final MockJavaClass clazz = new MockJavaClass(); 220 clazz.setName( "Blah" ); 221 222 final Attribute attribute = new Attribute( "dna.configuration" ); 223 final Attribute result = 224 interceptor.processConfigurationAttribute( clazz, attribute ); 225 assertNotNull( "attribute", result ); 226 assertEquals( "attribute.name", "dna.configuration", result.getName() ); 227 assertEquals( "attribute.value", null, result.getValue() ); 228 assertEquals( "attribute.parameterCount", 1, result.getParameterCount() ); 229 assertEquals( "attribute.parameter(location)", 230 "Blah-schema.xml", result.getParameter( "location" ) ); 231 } 232 233 public void testProcessConfigurationAttributeWithLocation() 234 throws Exception  235 { 236 final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor(); 237 238 final MockJavaClass clazz = new MockJavaClass(); 239 clazz.setName( "Blah" ); 240 241 final Properties parameters = new Properties (); 242 parameters.setProperty( "location", "Meep.xml" ); 243 final Attribute attribute = new Attribute( "dna.configuration", parameters ); 244 final Attribute result = 245 interceptor.processConfigurationAttribute( clazz, attribute ); 246 assertNotNull( "attribute", result ); 247 assertEquals( "attribute.name", "dna.configuration", result.getName() ); 248 assertEquals( "attribute.value", null, result.getValue() ); 249 assertEquals( "attribute.parameterCount", 1, result.getParameterCount() ); 250 assertEquals( "attribute.parameter(location)", 251 "Meep.xml", result.getParameter( "location" ) ); 252 } 253 254 public void testProcessConfigurationAttributeWithType() 255 throws Exception  256 { 257 final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor(); 258 259 final MockJavaClass clazz = new MockJavaClass(); 260 clazz.setName( "Blah" ); 261 262 final Properties parameters = new Properties (); 263 parameters.setProperty( "type", "MeepSchemaLanguage" ); 264 final Attribute attribute = new Attribute( "dna.configuration", parameters ); 265 final Attribute result = 266 interceptor.processConfigurationAttribute( clazz, attribute ); 267 assertNotNull( "attribute", result ); 268 assertEquals( "attribute.name", "dna.configuration", result.getName() ); 269 assertEquals( "attribute.value", null, result.getValue() ); 270 assertEquals( "attribute.parameterCount", 2, result.getParameterCount() ); 271 assertEquals( "attribute.parameter(location)", 272 "Blah-schema.xml", result.getParameter( "location" ) ); 273 assertEquals( "attribute.parameter(type)", 274 "MeepSchemaLanguage", result.getParameter( "type" ) ); 275 } 276 277 public void testDetermineKeyWhenNothingSpecified() 278 throws Exception  279 { 280 final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor(); 281 final Properties parameters = new Properties (); 282 final Attribute attribute = new Attribute( "dna.dependency", parameters ); 283 final String key = interceptor.determineKey( attribute, "com.biz.Foo" ); 284 assertEquals( "key", "com.biz.Foo", key ); 285 } 286 287 public void testDetermineKeyWhenQualifierSpecified() 288 throws Exception  289 { 290 final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor(); 291 final Properties parameters = new Properties (); 292 parameters.setProperty( "qualifier", "x" ); 293 final Attribute attribute = new Attribute( "dna.dependency", parameters ); 294 final String key = interceptor.determineKey( attribute, "com.biz.Foo" ); 295 assertEquals( "key", "com.biz.Foo/x", key ); 296 } 297 298 public void testDetermineKeyWhenKeySpecified() 299 throws Exception  300 { 301 final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor(); 302 final Properties parameters = new Properties (); 303 parameters.setProperty( "key", "x" ); 304 final Attribute attribute = new Attribute( "dna.dependency", parameters ); 305 final String key = interceptor.determineKey( attribute, "com.biz.Foo" ); 306 assertEquals( "key", "x", key ); 307 } 308 309 public void testGetSchemaLocationForClassInDefaultPackage() 310 throws Exception  311 { 312 final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor(); 313 final String location = interceptor.getSchemaLocationFor( "Foo" ); 314 assertEquals( "location", "Foo-schema.xml", location ); 315 } 316 317 public void testGetSchemaLocationForClassInNonDefaultPackage() 318 throws Exception  319 { 320 final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor(); 321 final String location = interceptor.getSchemaLocationFor( "com.biz.Foo" ); 322 assertEquals( "location", "Foo-schema.xml", location ); 323 } 324 325 public void testResolveType() 326 throws Exception  327 { 328 final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor(); 329 final String type = interceptor.resolveType( new MockJavaClass(), "X" ); 330 assertEquals( "type", MockJavaSource.PREFIX + "X", type ); 331 } 332 333 public void testResolveTypeOnArray() 334 throws Exception  335 { 336 final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor(); 337 final String type = interceptor.resolveType( new MockJavaClass(), "X[]" ); 338 assertEquals( "type", MockJavaSource.PREFIX + "X[]", type ); 339 } 340 341 public void testResolveTypeOnMap() 342 throws Exception  343 { 344 final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor(); 345 final String type = interceptor.resolveType( new MockJavaClass(), "X{}" ); 346 assertEquals( "type", MockJavaSource.PREFIX + "X{}", type ); 347 } 348 } 349 | Popular Tags |