001: /*
002: * Copyright (C) The Spice Group. All rights reserved.
003: *
004: * This software is published under the terms of the Spice
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.verifier;
009:
010: import junit.framework.TestCase;
011: import java.util.ArrayList;
012: import java.util.List;
013: import java.util.Properties;
014: import java.util.Arrays;
015: import java.awt.event.ActionListener;
016:
017: import org.codehaus.dna.Configurable;
018: import org.codehaus.dna.Configuration;
019: import org.codehaus.dna.tools.verifier.ComponentVerifier;
020: import org.codehaus.dna.tools.verifier.VerifyIssue;
021: import org.codehaus.metaclass.introspector.MetaClassIntrospector;
022: import org.codehaus.metaclass.model.Attribute;
023: import org.codehaus.metaclass.model.ClassDescriptor;
024: import org.codehaus.metaclass.model.FieldDescriptor;
025: import org.codehaus.metaclass.model.MethodDescriptor;
026: import org.codehaus.metaclass.model.ParameterDescriptor;
027: import org.codehaus.metaclass.Attributes;
028:
029: /**
030: *
031: * @author Peter Donald
032: * @version $Revision: 1.1 $ $Date: 2004/04/18 20:13:44 $
033: */
034: public class ComponentVerifierTestCase extends TestCase {
035: public void testVerifyNonArrayWithNonArray() throws Exception {
036: final ComponentVerifier verifier = new ComponentVerifier();
037: final List issues = new ArrayList();
038: verifier.verifyNonArray(Object.class, issues);
039: assertNoIssues(issues);
040: }
041:
042: public void testVerifyNonArrayWithArray() throws Exception {
043: final ComponentVerifier verifier = new ComponentVerifier();
044: final List issues = new ArrayList();
045: verifier.verifyNonArray(Object[].class, issues);
046: assertSingleIssue(issues, "The class is an array.", true, false);
047: }
048:
049: public void testVerifyNonInterfaceWithNonInterface()
050: throws Exception {
051: final ComponentVerifier verifier = new ComponentVerifier();
052: final List issues = new ArrayList();
053: verifier.verifyNonInterface(Object.class, issues);
054: assertNoIssues(issues);
055: }
056:
057: public void testVerifyNonInterfaceWithInterface() throws Exception {
058: final ComponentVerifier verifier = new ComponentVerifier();
059: final List issues = new ArrayList();
060: verifier.verifyNonInterface(ActionListener.class, issues);
061: assertSingleIssue(issues, "The class is an interface.", true,
062: false);
063: }
064:
065: public void testVerifyNonPrimitiveWithNonInterface()
066: throws Exception {
067: final ComponentVerifier verifier = new ComponentVerifier();
068: final List issues = new ArrayList();
069: verifier.verifyNonPrimitive(Object.class, issues);
070: assertNoIssues(issues);
071: }
072:
073: public void testVerifyNonPrimitiveWithPrimitive() throws Exception {
074: final ComponentVerifier verifier = new ComponentVerifier();
075: final List issues = new ArrayList();
076: verifier.verifyNonPrimitive(Integer.TYPE, issues);
077: assertSingleIssue(issues,
078: "The class represents a primitive type.", true, false);
079: }
080:
081: public void testVerifyPublicThatPasses() throws Exception {
082: final ComponentVerifier verifier = new ComponentVerifier();
083: final List issues = new ArrayList();
084: verifier.verifyPublic(Object.class, issues);
085: assertNoIssues(issues);
086: }
087:
088: public void testVerifyPublicThatNoPasses() throws Exception {
089: final ComponentVerifier verifier = new ComponentVerifier();
090: final List issues = new ArrayList();
091: verifier.verifyPublic(
092: AbstractNonPublicClassWithNonPublicCtor.class, issues);
093: assertSingleIssue(issues, "The class is not public.", true,
094: false);
095: }
096:
097: public void testVerifyNonAbstractThatPasses() throws Exception {
098: final ComponentVerifier verifier = new ComponentVerifier();
099: final List issues = new ArrayList();
100: verifier.verifyNonAbstract(Object.class, issues);
101: assertNoIssues(issues);
102: }
103:
104: public void testVerifyNonAbstractThatNoPasses() throws Exception {
105: final ComponentVerifier verifier = new ComponentVerifier();
106: final List issues = new ArrayList();
107: verifier.verifyNonAbstract(
108: AbstractNonPublicClassWithNonPublicCtor.class, issues);
109: assertSingleIssue(issues, "The class is abstract.", true, false);
110: }
111:
112: public void testVerifyNoArgConstructorThatPasses() throws Exception {
113: final ComponentVerifier verifier = new ComponentVerifier();
114: final List issues = new ArrayList();
115: verifier.verifyNoArgConstructor(Object.class, issues);
116: assertNoIssues(issues);
117: }
118:
119: public void testVerifyNoArgConstructorThatNoPasses()
120: throws Exception {
121: final ComponentVerifier verifier = new ComponentVerifier();
122: final List issues = new ArrayList();
123: verifier.verifyNoArgConstructor(
124: AbstractNonPublicClassWithNonPublicCtor.class, issues);
125: assertSingleIssue(
126: issues,
127: "The class does not have a public default constructor.",
128: true, false);
129: }
130:
131: public void testVerifyServiceNotALifecycleThatPasses()
132: throws Exception {
133: final ComponentVerifier verifier = new ComponentVerifier();
134: final List issues = new ArrayList();
135: verifier.verifyServiceNotALifecycle(Object.class, issues);
136: assertNoIssues(issues);
137: }
138:
139: public void testVerifyServiceNotALifecycleThatNoPasses()
140: throws Exception {
141: final ComponentVerifier verifier = new ComponentVerifier();
142: final List issues = new ArrayList();
143: verifier.verifyServiceNotALifecycle(
144: LifecycleExtendingService.class, issues);
145: assertSingleIssue(issues, "Service "
146: + LifecycleExtendingService.class.getName()
147: + " extends lifecycle interface "
148: + Configurable.class.getName() + ".", true, false);
149: }
150:
151: public void testVerifyServiceIsPublicThatPasses() throws Exception {
152: final ComponentVerifier verifier = new ComponentVerifier();
153: final List issues = new ArrayList();
154: verifier.verifyServiceIsPublic(Object.class, issues);
155: assertNoIssues(issues);
156: }
157:
158: public void testVerifyServiceIsPublicThatNoPasses()
159: throws Exception {
160: final ComponentVerifier verifier = new ComponentVerifier();
161: final List issues = new ArrayList();
162: verifier.verifyServiceIsPublic(LifecycleExtendingService.class,
163: issues);
164: assertSingleIssue(issues, "Service "
165: + LifecycleExtendingService.class.getName()
166: + " must be public.", true, false);
167: }
168:
169: public void testVerifyServiceIsaInterfaceThatPasses()
170: throws Exception {
171: final ComponentVerifier verifier = new ComponentVerifier();
172: final List issues = new ArrayList();
173: verifier
174: .verifyServiceIsaInterface(ActionListener.class, issues);
175: assertNoIssues(issues);
176: }
177:
178: public void testVerifyServiceIsaInterfaceThatNoPasses()
179: throws Exception {
180: final ComponentVerifier verifier = new ComponentVerifier();
181: final List issues = new ArrayList();
182: verifier.verifyServiceIsaInterface(Object.class, issues);
183: assertSingleIssue(issues, "Service " + Object.class.getName()
184: + " must be an interface.", true, false);
185: }
186:
187: public void testVerifyClass() throws Exception {
188: final ComponentVerifier verifier = new ComponentVerifier();
189: final List issues = new ArrayList();
190: verifier.verifyClass(Object.class, issues);
191: assertNoIssues(issues);
192: }
193:
194: public void testVerifyService() throws Exception {
195: final ComponentVerifier verifier = new ComponentVerifier();
196: final List issues = new ArrayList();
197: verifier.verifyService(ActionListener.class, issues);
198: assertNoIssues(issues);
199: }
200:
201: public void testVerifyImplementsServicesThatPasses()
202: throws Exception {
203: final ComponentVerifier verifier = new ComponentVerifier();
204: final List issues = new ArrayList();
205: final Class[] services = new Class[] { ActionListener.class };
206: verifier.verifyImplementsServices(
207: ActionListenerComponent.class, services, issues);
208: assertNoIssues(issues);
209: }
210:
211: public void testVerifyImplementsServicesThatNoPasses()
212: throws Exception {
213: final ComponentVerifier verifier = new ComponentVerifier();
214: final List issues = new ArrayList();
215: final Class[] services = new Class[] { ActionListener.class };
216: verifier.verifyImplementsServices(Object.class, services,
217: issues);
218: assertSingleIssue(issues,
219: "The metadata declares that the class "
220: + "supports the service "
221: + ActionListener.class.getName()
222: + " but the class does not implement the "
223: + "service interface.", true, false);
224: }
225:
226: public void testVerifyMetaDataThatPasses() throws Exception {
227: final ComponentVerifier verifier = new ComponentVerifier();
228: MetaClassIntrospector.setAccessor(new SimpleAccessor());
229: MetaClassIntrospector.clearCompleteCache();
230: final List issues = new ArrayList();
231: verifier
232: .verifyMetaData(ComponentVerifierTestCase.class, issues);
233: assertNoIssues(issues);
234: }
235:
236: public void testVerifyMetaDataThatNoPasses() throws Exception {
237: final ComponentVerifier verifier = new ComponentVerifier();
238: final List issues = new ArrayList();
239: MetaClassIntrospector.setAccessor(new NullAccessor());
240: MetaClassIntrospector.clearCompleteCache();
241: verifier.verifyMetaData(Object.class, issues);
242: assertSingleIssue(issues, "The class does not specify correct "
243: + "metadata. Missing expected dna.component "
244: + "attribute in the class attributes.", true, false);
245: }
246:
247: public void testGetServiceClasses() throws Exception {
248: final ComponentVerifier verifier = new ComponentVerifier();
249: MetaClassIntrospector.setAccessor(new BadServiceAccessor());
250: MetaClassIntrospector.clearCompleteCache();
251: final List issues = new ArrayList();
252: verifier.getServiceClasses(ComponentVerifier.class, issues);
253: assertSingleIssue(issues, "Unable to load service interface "
254: + BadServiceAccessor.BAD_SERVICE
255: + " for class. Reason: "
256: + "java.lang.ClassNotFoundException: I-No-Exist!.",
257: true, false);
258: }
259:
260: public void testVerifyType() throws Exception {
261: final Properties parameters = new Properties();
262: parameters.setProperty("type", ActionListener.class.getName());
263: final Attribute[] attributes = new Attribute[] {
264: new Attribute("dna.component"),
265: new Attribute("dna.service", parameters) };
266: final ClassDescriptor descriptor = new ClassDescriptor(
267: BasicComponent.class.getName(), attributes, attributes,
268: FieldDescriptor.EMPTY_SET, MethodDescriptor.EMPTY_SET);
269: final ComponentVerifier verifier = new ComponentVerifier();
270: final RegistrationMetaClassAccessor accessor = new RegistrationMetaClassAccessor();
271: accessor.registerDescriptor(descriptor);
272: MetaClassIntrospector.setAccessor(accessor);
273: MetaClassIntrospector.clearCompleteCache();
274: final VerifyIssue[] issues = verifier
275: .verifyType(BasicComponent.class);
276: assertEquals("issues.length", 0, issues.length);
277: }
278:
279: public void testVerifyDependencyOptionalValidThatPasses()
280: throws Exception {
281: final ComponentVerifier verifier = new ComponentVerifier();
282: final List issues = new ArrayList();
283: verifier.verifyDependencyOptionalValid("true", issues);
284: assertNoIssues(issues);
285: }
286:
287: public void testVerifyDependencyOptionalValidThatNoPasses()
288: throws Exception {
289: final ComponentVerifier verifier = new ComponentVerifier();
290: final List issues = new ArrayList();
291: verifier.verifyDependencyOptionalValid("blah", issues);
292: assertSingleIssue(
293: issues,
294: "The dna.dependency attribute specifies "
295: + "optional parameter as \"blah\" that is not one "
296: + "of true or false.", true, false);
297: }
298:
299: public void testVerifyDependencyKeyConformsThatPassesWithQualifier()
300: throws Exception {
301: final ComponentVerifier verifier = new ComponentVerifier();
302: final List issues = new ArrayList();
303: verifier.verifyDependencyKeyConforms("X", "X/X", issues);
304: assertNoIssues(issues);
305: }
306:
307: public void testVerifyDependencyKeyConformsThatPassesWithoutQualifier()
308: throws Exception {
309: final ComponentVerifier verifier = new ComponentVerifier();
310: final List issues = new ArrayList();
311: verifier.verifyDependencyKeyConforms("X", "X", issues);
312: assertNoIssues(issues);
313: }
314:
315: public void testVerifyDependencyKeyConformsThatNoPasses()
316: throws Exception {
317: final ComponentVerifier verifier = new ComponentVerifier();
318: final List issues = new ArrayList();
319: verifier.verifyDependencyKeyConforms("X", "blah", issues);
320: assertSingleIssue(
321: issues,
322: "The dna.dependency attribute specifies the key "
323: + "blah which does not conform to recomendation of "
324: + "(type)[/(qualifier)].", false, false);
325: }
326:
327: public void testVerifyDependencyTypeThatPasses() throws Exception {
328: final ComponentVerifier verifier = new ComponentVerifier();
329: final List issues = new ArrayList();
330: verifier.verifyDependencyType(LifecycleExtendingService.class,
331: ActionListener.class.getName(), issues);
332: assertNoIssues(issues);
333: }
334:
335: public void testVerifyDependencyTypeThatNoPasses() throws Exception {
336: final ComponentVerifier verifier = new ComponentVerifier();
337: final List issues = new ArrayList();
338: verifier.verifyDependencyType(LifecycleExtendingService.class,
339: "INoExist!", issues);
340: assertSingleIssue(
341: issues,
342: "Unable to load dependency with type INoExist! "
343: + "for class. Reason: "
344: + "java.lang.ClassNotFoundException: INoExist!.",
345: true, false);
346: }
347:
348: public void testVerifyOptionalParameterThatPasses()
349: throws Exception {
350: final ComponentVerifier verifier = new ComponentVerifier();
351: final List issues = new ArrayList();
352: verifier.verifyOptionalParameter("true", issues);
353: assertNoIssues(issues);
354: }
355:
356: public void testVerifyOptionalParameterThatNoPasses()
357: throws Exception {
358: final ComponentVerifier verifier = new ComponentVerifier();
359: final List issues = new ArrayList();
360: verifier.verifyOptionalParameter(null, issues);
361: assertSingleIssue(issues,
362: "The dna.dependency attribute does not "
363: + "specify the parameter optional.", true,
364: false);
365: }
366:
367: public void testVerifyDependencyMetaDataThatPasses()
368: throws Exception {
369: final ComponentVerifier verifier = new ComponentVerifier();
370: final List issues = new ArrayList();
371: final Properties parameters = new Properties();
372: parameters.setProperty("optional", "false");
373: parameters.setProperty("type", ActionListener.class.getName());
374: parameters.setProperty("key", ActionListener.class.getName());
375: verifier.verifyDependencyMetaData(
376: LifecycleExtendingService.class, new Attribute(
377: "dna.dependency", parameters), issues);
378: assertNoIssues(issues);
379: }
380:
381: public void testVerifyDependencyMetaDataThatNoPassesDueToMissingType()
382: throws Exception {
383: final ComponentVerifier verifier = new ComponentVerifier();
384: final List issues = new ArrayList();
385: final Properties parameters = new Properties();
386: parameters.setProperty("optional", "false");
387: verifier.verifyDependencyMetaData(
388: LifecycleExtendingService.class, new Attribute(
389: "dna.dependency", parameters), issues);
390: assertSingleIssue(
391: issues,
392: "The dna.dependency attribute does not specify the parameter type.",
393: true, false);
394: }
395:
396: public void testVerifyDependencyMetaDataThatNoPassesDueToMissingKey()
397: throws Exception {
398: final ComponentVerifier verifier = new ComponentVerifier();
399: final List issues = new ArrayList();
400: final Properties parameters = new Properties();
401: parameters.setProperty("optional", "false");
402: parameters.setProperty("type", ActionListener.class.getName());
403: final Attribute attribute = new Attribute("dna.dependency",
404: parameters);
405: verifier.verifyDependencyMetaData(
406: LifecycleExtendingService.class, attribute, issues);
407: assertSingleIssue(
408: issues,
409: "The dna.dependency attribute does not specify the parameter key.",
410: true, false);
411: }
412:
413: public void testVerifyDependencyMetaDataThatPassesDueToNotImplementingComposable()
414: throws Exception {
415: final ComponentVerifier verifier = new ComponentVerifier();
416: final List issues = new ArrayList();
417: verifier.verifyDependencyMetaData(Object.class, issues);
418: assertNoIssues(issues);
419: }
420:
421: public void testVerifyDependencyMetaDataThatPassesDueToNoMetaData()
422: throws Exception {
423: final ComponentVerifier verifier = new ComponentVerifier();
424: final List issues = new ArrayList();
425: MetaClassIntrospector.setAccessor(new SimpleAccessor());
426: MetaClassIntrospector.clearCompleteCache();
427: verifier.verifyDependencyMetaData(BasicComponent.class, issues);
428: assertNoIssues(issues);
429: }
430:
431: public void testVerifyDependencyMetaDataThatPassesWithMetaData()
432: throws Exception {
433: final ComponentVerifier verifier = new ComponentVerifier();
434: final List issues = new ArrayList();
435: MetaClassIntrospector
436: .setAccessor(new AccessorWithDependencyMetaData());
437: MetaClassIntrospector.clearCompleteCache();
438: verifier.verifyDependencyMetaData(BasicComponent.class, issues);
439: assertNoIssues(issues);
440: }
441:
442: public void testVerifyConfigurationMetaDataThatPassesAsNotConfigurable()
443: throws Exception {
444: final ComponentVerifier verifier = new ComponentVerifier();
445: final List issues = new ArrayList();
446: MetaClassIntrospector
447: .setAccessor(new AccessorWithDependencyMetaData());
448: MetaClassIntrospector.clearCompleteCache();
449: verifier.verifyConfigurationMetaData(Object.class, issues);
450: assertNoIssues(issues);
451: }
452:
453: public void testVerifyConfigurationMetaDataThatPassesAsNoMetaData()
454: throws Exception {
455: final ComponentVerifier verifier = new ComponentVerifier();
456: final List issues = new ArrayList();
457: MetaClassIntrospector.setAccessor(new SimpleAccessor());
458: MetaClassIntrospector.clearCompleteCache();
459: verifier.verifyConfigurationMetaData(BasicComponent.class,
460: issues);
461: assertNoIssues(issues);
462: }
463:
464: public void testVerifyConfigurationMetaDataThatPassesAsValidMetaData()
465: throws Exception {
466: final ComponentVerifier verifier = new ComponentVerifier();
467: final List issues = new ArrayList();
468:
469: final Properties parameters = new Properties();
470: parameters.setProperty("location", "BasicComponent-schema.xml");
471: final Attribute[] attributes = new Attribute[] { new Attribute(
472: "dna.configuration", parameters) };
473: final ParameterDescriptor param = new ParameterDescriptor("X",
474: Configuration.class.getName());
475: final ParameterDescriptor[] params = new ParameterDescriptor[] { param };
476: final MethodDescriptor method = new MethodDescriptor(
477: "configure", "", params, attributes, attributes);
478: final ClassDescriptor descriptor = new ClassDescriptor(
479: BasicComponent.class.getName(), Attribute.EMPTY_SET,
480: Attribute.EMPTY_SET, FieldDescriptor.EMPTY_SET,
481: new MethodDescriptor[] { method });
482: final RegistrationMetaClassAccessor accessor = new RegistrationMetaClassAccessor();
483: accessor.registerDescriptor(descriptor);
484: MetaClassIntrospector.setAccessor(accessor);
485: MetaClassIntrospector.clearCompleteCache();
486: verifier.verifyConfigurationMetaData(BasicComponent.class,
487: issues);
488: assertNoIssues(issues);
489: }
490:
491: public void testVerifyConfigurationMetaDataThatNoPassesAsInvalidMetaData()
492: throws Exception {
493: final ComponentVerifier verifier = new ComponentVerifier();
494: final List issues = new ArrayList();
495:
496: final Properties parameters = new Properties();
497: final Attribute[] attributes = new Attribute[] { new Attribute(
498: "dna.configuration", parameters) };
499: final ParameterDescriptor param = new ParameterDescriptor("X",
500: Configuration.class.getName());
501: final ParameterDescriptor[] params = new ParameterDescriptor[] { param };
502: final MethodDescriptor method = new MethodDescriptor(
503: "configure", "", params, attributes, attributes);
504: final ClassDescriptor descriptor = new ClassDescriptor(
505: BasicComponent.class.getName(), Attribute.EMPTY_SET,
506: Attribute.EMPTY_SET, FieldDescriptor.EMPTY_SET,
507: new MethodDescriptor[] { method });
508: final RegistrationMetaClassAccessor accessor = new RegistrationMetaClassAccessor();
509: accessor.registerDescriptor(descriptor);
510: MetaClassIntrospector.setAccessor(accessor);
511: MetaClassIntrospector.clearCompleteCache();
512: verifier.verifyConfigurationMetaData(BasicComponent.class,
513: issues);
514:
515: assertSingleIssue(issues,
516: "The dna.configuration attribute is missing the "
517: + "location parameter.", true, false);
518: }
519:
520: public void testverifyLocationThatPasses() throws Exception {
521: final ComponentVerifier verifier = new ComponentVerifier();
522: final List issues = new ArrayList();
523: MetaClassIntrospector
524: .setAccessor(new AccessorWithDependencyMetaData());
525: MetaClassIntrospector.clearCompleteCache();
526: verifier.verifyLocation(BasicComponent.class,
527: "BasicComponent-schema.xml", issues);
528: assertNoIssues(issues);
529: }
530:
531: public void testverifyLocationThatNoPasses() throws Exception {
532: final ComponentVerifier verifier = new ComponentVerifier();
533: final List issues = new ArrayList();
534: MetaClassIntrospector
535: .setAccessor(new AccessorWithDependencyMetaData());
536: MetaClassIntrospector.clearCompleteCache();
537: verifier
538: .verifyLocation(BasicComponent.class, "NoExist", issues);
539: assertSingleIssue(issues,
540: "Unable to load configuration schema from location "
541: + "NoExist.", true, false);
542: }
543:
544: private void assertNoIssues(final List issues) {
545: if (0 != issues.size()) {
546: final int count = issues.size();
547: for (int i = 0; i < count; i++) {
548: final VerifyIssue issue = (VerifyIssue) issues.get(i);
549: System.out.println("issue.getDescription() = "
550: + issue.getDescription());
551: }
552: }
553: assertEquals("issues.length", 0, issues.size());
554: }
555:
556: private void assertSingleIssue(final List issues,
557: final String message, final boolean error,
558: final boolean warning) {
559: assertEquals("issues.length", 1, issues.size());
560: final VerifyIssue issue = (VerifyIssue) issues.get(0);
561: assertEquals("issues[0].isWarning", warning, issue.isWarning());
562: assertEquals("issues[0].isError", error, issue.isError());
563: assertEquals("issues[0].description", message, issue
564: .getDescription());
565: }
566: }
|