001: /*
002: * Copyright (C) The DNA Group. All rights reserved.
003: *
004: * This software is published under the terms of the DNA
005: * Software License version 1.1, a copy of which has been included
006: * with this distribution in the LICENSE.txt file.
007: */
008: package org.codehaus.dna.tools.metaclass;
009:
010: import com.thoughtworks.qdox.model.JavaMethod;
011: import java.util.Properties;
012: import junit.framework.TestCase;
013:
014: import org.codehaus.dna.tools.metaclass.DNAAttributeInterceptor;
015: import org.codehaus.metaclass.model.Attribute;
016:
017: /**
018: *
019: * @author Peter Donald
020: * @version $Revision: 1.2 $ $Date: 2004/05/01 09:51:48 $
021: */
022: public class DNAAttributeInterceptorTestCase extends TestCase {
023: public void testProcessClassAttributeWithoutTransformations()
024: throws Exception {
025: final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor();
026: final Attribute attribute = new Attribute("ignored");
027: final Attribute result = interceptor.processClassAttribute(
028: new MockJavaClass(), attribute);
029: assertNotNull("attribute", result);
030: assertEquals("attribute.name", "ignored", result.getName());
031: assertEquals("attribute.value", null, result.getValue());
032: assertEquals("attribute.parameterCount", 0, result
033: .getParameterCount());
034: }
035:
036: public void testProcessClassAttributeWithDNAService()
037: throws Exception {
038: final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor();
039: final Properties parameters = new Properties();
040: parameters.setProperty("type", "X");
041: final Attribute attribute = new Attribute("dna.service",
042: parameters);
043: final Attribute result = interceptor.processClassAttribute(
044: new MockJavaClass(), attribute);
045: assertNotNull("attribute", result);
046: assertEquals("attribute.name", "dna.service", result.getName());
047: assertEquals("attribute.value", null, result.getValue());
048: assertEquals("attribute.parameterCount", 1, result
049: .getParameterCount());
050: assertEquals("attribute.parameter(type)", MockJavaSource.PREFIX
051: + "X", result.getParameter("type"));
052: }
053:
054: public void testProcessClassAttributeWithDNAServiceMissingType()
055: throws Exception {
056: final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor();
057: final Attribute attribute = new Attribute("dna.service");
058: try {
059: final MockJavaClass clazz = new MockJavaClass();
060: clazz.setName("Blah");
061: interceptor.processClassAttribute(clazz, attribute);
062: } catch (final IllegalArgumentException iae) {
063: return;
064: }
065: fail("Expected to fail to process dna.service "
066: + "attribute missing type parameter");
067: }
068:
069: public void testProcessMethodAttributeWithDNAConfigurationWith()
070: throws Exception {
071: final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor();
072: final Attribute attribute = new Attribute("ignored");
073: final Attribute result = interceptor.processMethodAttribute(
074: new JavaMethod(), attribute);
075: assertNotNull("attribute", result);
076: assertEquals("attribute.name", "ignored", result.getName());
077: assertEquals("attribute.value", null, result.getValue());
078: assertEquals("attribute.parameterCount", 0, result
079: .getParameterCount());
080: }
081:
082: public void testProcessMethodAttributeWithoutTransformations()
083: throws Exception {
084: final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor();
085: final Attribute attribute = new Attribute("ignored");
086: final Attribute result = interceptor.processMethodAttribute(
087: new JavaMethod(), attribute);
088: assertNotNull("attribute", result);
089: assertEquals("attribute.name", "ignored", result.getName());
090: assertEquals("attribute.value", null, result.getValue());
091: assertEquals("attribute.parameterCount", 0, result
092: .getParameterCount());
093: }
094:
095: public void testProcessMethodForConfiguration() throws Exception {
096: final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor();
097:
098: final MockJavaClass clazz = new MockJavaClass();
099: clazz.setName("Blah");
100:
101: final JavaMethod javaMethod = new JavaMethod();
102: javaMethod.setParentClass(clazz);
103: final Attribute attribute = new Attribute("dna.configuration");
104: final Attribute result = interceptor.processMethodAttribute(
105: javaMethod, attribute);
106: assertNotNull("attribute", result);
107: assertEquals("attribute.name", "dna.configuration", result
108: .getName());
109: assertEquals("attribute.value", null, result.getValue());
110: assertEquals("attribute.parameterCount", 1, result
111: .getParameterCount());
112: assertEquals("attribute.parameter(location)",
113: "Blah-schema.xml", result.getParameter("location"));
114: }
115:
116: public void testProcessMethodForParameters() throws Exception {
117: final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor();
118:
119: final MockJavaClass clazz = new MockJavaClass();
120: clazz.setName("Blah");
121:
122: final JavaMethod javaMethod = new JavaMethod();
123: javaMethod.setParentClass(clazz);
124: final Attribute attribute = new Attribute("dna.parameters");
125: final Attribute result = interceptor.processMethodAttribute(
126: javaMethod, attribute);
127: assertNotNull("attribute", result);
128: assertEquals("attribute.name", "dna.parameters", result
129: .getName());
130: assertEquals("attribute.value", null, result.getValue());
131: assertEquals("attribute.parameterCount", 1, result
132: .getParameterCount());
133: assertEquals("attribute.parameter(location)",
134: "Blah-schema.xml", result.getParameter("location"));
135: }
136:
137: public void testProcessMethodForDependencySpecifyingOptionalAttribute()
138: throws Exception {
139: final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor();
140: final Properties parameters = new Properties();
141: parameters.setProperty("type", "Foo");
142: parameters.setProperty("optional", "true");
143: final Attribute attribute = new Attribute("dna.dependency",
144: parameters);
145: final JavaMethod method = new JavaMethod();
146: method.setParentClass(new MockJavaClass());
147: final Attribute result = interceptor.processMethodAttribute(
148: method, attribute);
149: assertNotNull("attribute", result);
150: assertEquals("attribute.name", "dna.dependency", result
151: .getName());
152: assertEquals("attribute.value", null, result.getValue());
153: assertEquals("attribute.parameterCount", 3, result
154: .getParameterCount());
155: assertEquals("attribute.parameter(type)", MockJavaSource.PREFIX
156: + "Foo", result.getParameter("type"));
157: assertEquals("attribute.parameter(key)", MockJavaSource.PREFIX
158: + "Foo", result.getParameter("key"));
159: assertEquals("attribute.parameter(optional)", "true", result
160: .getParameter("optional"));
161: }
162:
163: public void testProcessMethodForDependencyNotSpecifyingType()
164: throws Exception {
165: final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor();
166: final Properties parameters = new Properties();
167: final Attribute attribute = new Attribute("dna.dependency",
168: parameters);
169: final JavaMethod method = new JavaMethod();
170: method.setParentClass(new MockJavaClass());
171: try {
172: interceptor.processMethodAttribute(method, attribute);
173: } catch (final IllegalArgumentException iae) {
174: return;
175: }
176: fail("Expected to fail as dna.dependency is missing typ[e parameter");
177: }
178:
179: public void testProcessMethodForDependencySpecifyingJustType()
180: throws Exception {
181: final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor();
182: final Properties parameters = new Properties();
183: parameters.setProperty("type", "Foo");
184: final Attribute attribute = new Attribute("dna.dependency",
185: parameters);
186: final JavaMethod method = new JavaMethod();
187: method.setParentClass(new MockJavaClass());
188: final Attribute result = interceptor.processMethodAttribute(
189: method, attribute);
190: assertNotNull("attribute", result);
191: assertEquals("attribute.name", "dna.dependency", result
192: .getName());
193: assertEquals("attribute.value", null, result.getValue());
194: assertEquals("attribute.parameterCount", 3, result
195: .getParameterCount());
196: assertEquals("attribute.parameter(type)", MockJavaSource.PREFIX
197: + "Foo", result.getParameter("type"));
198: assertEquals("attribute.parameter(key)", MockJavaSource.PREFIX
199: + "Foo", result.getParameter("key"));
200: assertEquals("attribute.parameter(optional)", "false", result
201: .getParameter("optional"));
202: }
203:
204: public void testProcessConfigurationAttributeWithoutLocationOrType()
205: throws Exception {
206: final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor();
207:
208: final MockJavaClass clazz = new MockJavaClass();
209: clazz.setName("Blah");
210:
211: final Attribute attribute = new Attribute("dna.configuration");
212: final Attribute result = interceptor
213: .processConfigurationAttribute(clazz, attribute);
214: assertNotNull("attribute", result);
215: assertEquals("attribute.name", "dna.configuration", result
216: .getName());
217: assertEquals("attribute.value", null, result.getValue());
218: assertEquals("attribute.parameterCount", 1, result
219: .getParameterCount());
220: assertEquals("attribute.parameter(location)",
221: "Blah-schema.xml", result.getParameter("location"));
222: }
223:
224: public void testProcessConfigurationAttributeWithLocation()
225: throws Exception {
226: final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor();
227:
228: final MockJavaClass clazz = new MockJavaClass();
229: clazz.setName("Blah");
230:
231: final Properties parameters = new Properties();
232: parameters.setProperty("location", "Meep.xml");
233: final Attribute attribute = new Attribute("dna.configuration",
234: parameters);
235: final Attribute result = interceptor
236: .processConfigurationAttribute(clazz, attribute);
237: assertNotNull("attribute", result);
238: assertEquals("attribute.name", "dna.configuration", result
239: .getName());
240: assertEquals("attribute.value", null, result.getValue());
241: assertEquals("attribute.parameterCount", 1, result
242: .getParameterCount());
243: assertEquals("attribute.parameter(location)", "Meep.xml",
244: result.getParameter("location"));
245: }
246:
247: public void testProcessConfigurationAttributeWithType()
248: throws Exception {
249: final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor();
250:
251: final MockJavaClass clazz = new MockJavaClass();
252: clazz.setName("Blah");
253:
254: final Properties parameters = new Properties();
255: parameters.setProperty("type", "MeepSchemaLanguage");
256: final Attribute attribute = new Attribute("dna.configuration",
257: parameters);
258: final Attribute result = interceptor
259: .processConfigurationAttribute(clazz, attribute);
260: assertNotNull("attribute", result);
261: assertEquals("attribute.name", "dna.configuration", result
262: .getName());
263: assertEquals("attribute.value", null, result.getValue());
264: assertEquals("attribute.parameterCount", 2, result
265: .getParameterCount());
266: assertEquals("attribute.parameter(location)",
267: "Blah-schema.xml", result.getParameter("location"));
268: assertEquals("attribute.parameter(type)", "MeepSchemaLanguage",
269: result.getParameter("type"));
270: }
271:
272: public void testDetermineKeyWhenNothingSpecified() throws Exception {
273: final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor();
274: final Properties parameters = new Properties();
275: final Attribute attribute = new Attribute("dna.dependency",
276: parameters);
277: final String key = interceptor.determineKey(attribute,
278: "com.biz.Foo");
279: assertEquals("key", "com.biz.Foo", key);
280: }
281:
282: public void testDetermineKeyWhenQualifierSpecified()
283: throws Exception {
284: final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor();
285: final Properties parameters = new Properties();
286: parameters.setProperty("qualifier", "x");
287: final Attribute attribute = new Attribute("dna.dependency",
288: parameters);
289: final String key = interceptor.determineKey(attribute,
290: "com.biz.Foo");
291: assertEquals("key", "com.biz.Foo/x", key);
292: }
293:
294: public void testDetermineKeyWhenKeySpecified() throws Exception {
295: final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor();
296: final Properties parameters = new Properties();
297: parameters.setProperty("key", "x");
298: final Attribute attribute = new Attribute("dna.dependency",
299: parameters);
300: final String key = interceptor.determineKey(attribute,
301: "com.biz.Foo");
302: assertEquals("key", "x", key);
303: }
304:
305: public void testGetSchemaLocationForClassInDefaultPackage()
306: throws Exception {
307: final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor();
308: final String location = interceptor.getSchemaLocationFor("Foo");
309: assertEquals("location", "Foo-schema.xml", location);
310: }
311:
312: public void testGetSchemaLocationForClassInNonDefaultPackage()
313: throws Exception {
314: final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor();
315: final String location = interceptor
316: .getSchemaLocationFor("com.biz.Foo");
317: assertEquals("location", "Foo-schema.xml", location);
318: }
319:
320: public void testResolveType() throws Exception {
321: final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor();
322: final String type = interceptor.resolveType(
323: new MockJavaClass(), "X");
324: assertEquals("type", MockJavaSource.PREFIX + "X", type);
325: }
326:
327: public void testResolveTypeOnArray() throws Exception {
328: final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor();
329: final String type = interceptor.resolveType(
330: new MockJavaClass(), "X[]");
331: assertEquals("type", MockJavaSource.PREFIX + "X[]", type);
332: }
333:
334: public void testResolveTypeOnMap() throws Exception {
335: final DNAAttributeInterceptor interceptor = new DNAAttributeInterceptor();
336: final String type = interceptor.resolveType(
337: new MockJavaClass(), "X{}");
338: assertEquals("type", MockJavaSource.PREFIX + "X{}", type);
339: }
340: }
|