001: // Copyright 2004, 2005 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package hivemind.test.parse;
016:
017: import hivemind.test.FrameworkTestCase;
018:
019: import java.util.HashMap;
020: import java.util.List;
021: import java.util.Map;
022:
023: import junit.framework.AssertionFailedError;
024:
025: import org.apache.hivemind.ApplicationRuntimeException;
026: import org.apache.hivemind.Attribute;
027: import org.apache.hivemind.Element;
028: import org.apache.hivemind.Occurances;
029: import org.apache.hivemind.Resource;
030: import org.apache.hivemind.impl.DefaultErrorHandler;
031: import org.apache.hivemind.internal.Visibility;
032: import org.apache.hivemind.parse.ConfigurationPointDescriptor;
033: import org.apache.hivemind.parse.ContributionDescriptor;
034: import org.apache.hivemind.parse.CreateInstanceDescriptor;
035: import org.apache.hivemind.parse.DependencyDescriptor;
036: import org.apache.hivemind.parse.ImplementationDescriptor;
037: import org.apache.hivemind.parse.InterceptorDescriptor;
038: import org.apache.hivemind.parse.ModuleDescriptor;
039: import org.apache.hivemind.parse.ServicePointDescriptor;
040: import org.apache.hivemind.parse.XmlResourceProcessor;
041: import org.apache.hivemind.schema.AttributeModel;
042: import org.apache.hivemind.schema.ElementModel;
043: import org.apache.hivemind.schema.Schema;
044: import org.apache.hivemind.schema.impl.SchemaImpl;
045: import org.apache.hivemind.schema.rules.CreateObjectRule;
046: import org.apache.hivemind.schema.rules.InvokeParentRule;
047: import org.apache.hivemind.schema.rules.PushAttributeRule;
048: import org.apache.hivemind.schema.rules.PushContentRule;
049: import org.apache.hivemind.schema.rules.ReadAttributeRule;
050: import org.apache.hivemind.schema.rules.ReadContentRule;
051: import org.apache.hivemind.schema.rules.SetModuleRule;
052: import org.apache.hivemind.schema.rules.SetPropertyRule;
053:
054: /**
055: * Tests for parsing a HiveModule descriptor into a
056: * {@link org.apache.hivemind.parse.ModuleDescriptor} and related objects, using
057: * {@link org.apache.hivemind.parse.DescriptorParser}.
058: *
059: * @author Howard Lewis Ship
060: */
061: public class TestDescriptorParser extends FrameworkTestCase {
062: private void checkAttributes(Element e, String[] attributes) {
063: List l = e.getAttributes();
064: Map map = new HashMap();
065: int count = l.size();
066:
067: for (int i = 0; i < count; i++) {
068: Attribute a = (Attribute) l.get(i);
069: map.put(a.getName(), a.getValue());
070: }
071:
072: if (attributes == null)
073: count = 0;
074: else
075: count = attributes.length;
076:
077: for (int i = 0; i < count; i += 2) {
078: String name = attributes[i];
079: String value = attributes[i + 1];
080:
081: assertEquals("Attribute " + name + " of element "
082: + e.getElementName(), value, map.get(name));
083:
084: map.remove(name);
085: }
086:
087: if (map.isEmpty())
088: return;
089:
090: throw new AssertionFailedError("Unexpected attribute(s) " + map
091: + " in element " + e.getElementName() + ".");
092: }
093:
094: public void testModuleAttributes() throws Exception {
095: ModuleDescriptor md = parse("GenericModule.xml");
096:
097: assertEquals("hivemind.test.parse", md.getModuleId());
098: assertEquals("1.0.0", md.getVersion());
099:
100: // package attribute was added in 1.1. Defaultis same as
101: // module id.
102:
103: assertEquals("hivemind.test.parse", md.getPackageName());
104: }
105:
106: public void testConfigurationPointAttributes() throws Exception {
107: ModuleDescriptor md = parse("GenericModule.xml");
108:
109: List l = md.getConfigurationPoints();
110: assertEquals(1, l.size());
111:
112: ConfigurationPointDescriptor cpd = (ConfigurationPointDescriptor) l
113: .get(0);
114:
115: assertEquals("MyExtensionPoint", cpd.getId());
116: assertEquals(Occurances.ONE_PLUS, cpd.getCount());
117: }
118:
119: public void testContributionAttributes() throws Exception {
120: ModuleDescriptor md = parse("GenericModule.xml");
121:
122: List l = md.getContributions();
123: assertEquals(1, l.size());
124:
125: ContributionDescriptor cd = (ContributionDescriptor) l.get(0);
126:
127: assertEquals("MyExtensionPoint", cd.getConfigurationId());
128: }
129:
130: public void testContributionElements() throws Exception {
131: ModuleDescriptor md = parse("GenericModule.xml");
132:
133: List l = md.getContributions();
134: assertEquals(1, l.size());
135:
136: ContributionDescriptor cd = (ContributionDescriptor) l.get(0);
137:
138: l = cd.getElements();
139: assertEquals(2, l.size());
140:
141: Element e = (Element) l.get(0);
142:
143: assertEquals("foo1", e.getElementName());
144: assertEquals(0, e.getElements().size());
145: assertEquals("foo1 content", e.getContent());
146: checkAttributes(e, new String[] { "bar", "baz" });
147:
148: e = (Element) l.get(1);
149:
150: assertEquals("foo2", e.getElementName());
151: assertEquals(1, e.getElements().size());
152: assertEquals("", e.getContent());
153: checkAttributes(e, new String[] { "zip", "zap", "fred",
154: "barney" });
155:
156: l = e.getElements();
157: e = (Element) l.get(0);
158:
159: assertEquals("foo3", e.getElementName());
160: assertEquals(0, e.getElements().size());
161: assertEquals("", e.getContent());
162: checkAttributes(e, new String[] { "gnip", "gnop" });
163: }
164:
165: public void testServicePoint() throws Exception {
166: ModuleDescriptor md = parse("GenericModule.xml");
167:
168: List l = md.getServicePoints();
169: assertEquals(2, l.size());
170: ServicePointDescriptor spd = (ServicePointDescriptor) l.get(0);
171:
172: assertEquals("MyService1", spd.getId());
173: assertEquals("package.MyService", spd.getInterfaceClassName());
174:
175: CreateInstanceDescriptor cid = (CreateInstanceDescriptor) spd
176: .getInstanceBuilder();
177: assertEquals("package.impl.MyServiceImpl", cid
178: .getInstanceClassName());
179:
180: l = spd.getInterceptors();
181: assertEquals(2, l.size());
182:
183: InterceptorDescriptor id = (InterceptorDescriptor) l.get(0);
184: assertEquals("MyInterceptor", id.getFactoryServiceId());
185: assertEquals("OtherInterceptor", id.getBefore());
186: assertEquals("MyInterceptorName", id.getName());
187:
188: id = (InterceptorDescriptor) l.get(1);
189: assertEquals("OtherInterceptor", id.getFactoryServiceId());
190: assertEquals("MyInterceptorName", id.getAfter());
191: assertNull(id.getName());
192: }
193:
194: public void testImplementation() throws Exception {
195: ModuleDescriptor md = parse("GenericModule.xml");
196: List l = md.getImplementations();
197: assertEquals(1, l.size());
198:
199: ImplementationDescriptor id1 = (ImplementationDescriptor) l
200: .get(0);
201:
202: assertEquals("othermodule.OtherService", id1.getServiceId());
203:
204: l = id1.getInterceptors();
205: assertEquals(1, l.size());
206:
207: InterceptorDescriptor id2 = (InterceptorDescriptor) l.get(0);
208:
209: assertEquals("MyInterceptor", id2.getFactoryServiceId());
210: }
211:
212: public void testConfigurationPointSchema() throws Exception {
213: ModuleDescriptor md = parse("GenericModule.xml");
214:
215: List l = md.getConfigurationPoints();
216: assertEquals(1, l.size());
217: ConfigurationPointDescriptor cpd = (ConfigurationPointDescriptor) l
218: .get(0);
219:
220: String schemaId = cpd.getContributionsSchemaId();
221:
222: assertEquals("Fool", schemaId);
223:
224: Schema schema = md.getSchema(schemaId);
225:
226: assertNotNull(schema.getLocation());
227:
228: l = schema.getElementModel();
229: assertEquals(2, l.size());
230:
231: ElementModel em = (ElementModel) l.get(0);
232:
233: assertEquals("foo1", em.getElementName());
234: assertEquals(0, em.getElementModel().size());
235:
236: List al = em.getAttributeModels();
237: assertEquals(2, al.size());
238:
239: AttributeModel am = (AttributeModel) al.get(0);
240: assertEquals("bar", am.getName());
241: assertEquals(true, am.isRequired());
242:
243: am = (AttributeModel) al.get(1);
244: assertEquals("biff", am.getName());
245:
246: em = (ElementModel) l.get(1);
247:
248: assertEquals("foo2", em.getElementName());
249: assertEquals(2, em.getAttributeModels().size());
250:
251: l = em.getElementModel();
252:
253: assertEquals(1, l.size());
254:
255: em = (ElementModel) l.get(0);
256:
257: assertEquals(1, em.getAttributeModels().size());
258: }
259:
260: public void testBadElementAttributeKey() throws Exception {
261: interceptLogging();
262:
263: parse("BadElementAttributeKey.xml");
264:
265: assertLoggedMessage("Schema Bad is invalid: Key attribute 'bad' of element 'foo' never declared.");
266: }
267:
268: public void testRules() throws Exception {
269: ModuleDescriptor md = parse("GenericModule.xml");
270: List l = md.getConfigurationPoints();
271: assertEquals(1, l.size());
272: ConfigurationPointDescriptor cpd = (ConfigurationPointDescriptor) l
273: .get(0);
274: Schema schema = md.getSchema(cpd.getContributionsSchemaId());
275:
276: l = schema.getElementModel();
277:
278: ElementModel em = (ElementModel) l.get(0);
279:
280: List rl = em.getRules();
281:
282: assertEquals(5, rl.size());
283:
284: CreateObjectRule rule1 = (CreateObjectRule) rl.get(0);
285: assertEquals("package.Foo1", rule1.getClassName());
286:
287: ReadAttributeRule rule2 = (ReadAttributeRule) rl.get(1);
288:
289: assertEquals("bazomatic", rule2.getPropertyName());
290: assertEquals("bar", rule2.getAttributeName());
291: assertEquals(true, rule2.getSkipIfNull());
292:
293: ReadContentRule rule3 = (ReadContentRule) rl.get(2);
294:
295: assertEquals("description", rule3.getPropertyName());
296:
297: SetModuleRule rule4 = (SetModuleRule) rl.get(3);
298:
299: assertEquals("module", rule4.getPropertyName());
300:
301: InvokeParentRule rule5 = (InvokeParentRule) rl.get(4);
302:
303: assertEquals("addElement", rule5.getMethodName());
304: }
305:
306: public void testParametersSchema() throws Exception {
307: ModuleDescriptor md = parse("GenericModule.xml");
308: List l = md.getServicePoints();
309: assertEquals(2, l.size());
310: ServicePointDescriptor spd = (ServicePointDescriptor) l.get(1);
311:
312: String schemaId = spd.getParametersSchemaId();
313:
314: assertEquals("Parameters", schemaId);
315:
316: ElementModel em = (ElementModel) md.getSchema(schemaId)
317: .getElementModel().get(0);
318:
319: assertEquals("myParameter", em.getElementName());
320: }
321:
322: public void testDuplicateContributionsSchema() throws Exception {
323: interceptLogging();
324:
325: ModuleDescriptor md = parse("DuplicateSchemas.xml");
326:
327: assertLoggedMessagePattern("Multiple contributions schemas specified for configuration MyConfiguration. Using locally defined schema \\(at ");
328:
329: ConfigurationPointDescriptor cpd = (ConfigurationPointDescriptor) md
330: .getConfigurationPoints().get(0);
331: Schema nestedSchema = cpd.getContributionsSchema();
332:
333: assertNotNull(nestedSchema);
334:
335: ElementModel em = (ElementModel) nestedSchema.getElementModel()
336: .get(0);
337:
338: assertEquals("myParameter", em.getElementName());
339: }
340:
341: public void testDuplicateParametersSchema() throws Exception {
342: interceptLogging();
343:
344: ModuleDescriptor md = parse("DuplicateSchemas.xml");
345:
346: assertLoggedMessagePattern("Multiple parameters schemas specified for service MyServiceFactory. Using locally defined schema \\(at ");
347:
348: ServicePointDescriptor spd = (ServicePointDescriptor) md
349: .getServicePoints().get(0);
350: Schema nestedSchema = spd.getParametersSchema();
351:
352: assertNotNull(nestedSchema);
353:
354: ElementModel em = (ElementModel) nestedSchema.getElementModel()
355: .get(0);
356:
357: assertEquals("myParameter", em.getElementName());
358: }
359:
360: public void testDependency() throws Exception {
361: ModuleDescriptor md = parse("GenericModule.xml");
362: List l = md.getDependencies();
363: assertEquals(1, l.size());
364: DependencyDescriptor dd = (DependencyDescriptor) l.get(0);
365:
366: assertEquals("my.module", dd.getModuleId());
367: assertEquals("1.0.0", dd.getVersion());
368: }
369:
370: public void testBadElement() throws Exception {
371: try {
372: parse("BadElement.xml");
373:
374: unreachable();
375: } catch (ApplicationRuntimeException ex) {
376: assertExceptionSubstring(ex,
377: "Unexpected element bad-element within module/schema/element");
378: }
379: }
380:
381: public void testBadAttribute() throws Exception {
382: interceptLogging();
383:
384: parse("BadAttribute.xml");
385:
386: assertLoggedMessagePattern("Unknown attribute 'bad-attribute' in element module/schema\\.");
387: }
388:
389: public void testMissingAttribute() throws Exception {
390: try {
391: parse("MissingAttribute.xml");
392:
393: unreachable();
394: } catch (ApplicationRuntimeException ex) {
395: assertExceptionSubstring(ex,
396: "Missing required attribute 'name' in element module/schema/element");
397: }
398: }
399:
400: public void testBadConfigurationId() throws Exception {
401: interceptLogging();
402:
403: parse("BadConfigurationId.xml");
404:
405: assertLoggedMessagePattern("Attribute id \\(foo\\.bar\\) of element module/configuration-point is improperly "
406: + "formatted\\. Schema and extension point ids should be simple names with "
407: + "no punctuation\\.");
408: }
409:
410: public void testBadModuleId() throws Exception {
411: interceptLogging();
412:
413: parse("BadModuleId.xml");
414:
415: assertLoggedMessagePattern("Attribute id \\(big bad\\) of element module is improperly "
416: + "formatted\\. Module identifiers should consist of a period-seperated series "
417: + "of names, like a Java package\\.");
418: }
419:
420: public void testBadVersion() throws Exception {
421: interceptLogging();
422:
423: parse("BadVersion.xml");
424:
425: assertLoggedMessagePattern("Attribute version \\(1\\.0\\.alpha\\) of element module is improperly "
426: + "formatted\\. Version numbers should be a sequence of three numbers "
427: + "separated by periods\\.");
428: }
429:
430: public void testSchemaDescription() throws Exception {
431: ModuleDescriptor md = parse("SchemaDescription.xml");
432:
433: List points = md.getConfigurationPoints();
434:
435: ConfigurationPointDescriptor cpd = (ConfigurationPointDescriptor) points
436: .get(0);
437:
438: assertEquals("PointWithDescription", cpd.getId());
439: }
440:
441: public void testEmbeddedConfigSchema() throws Exception {
442: ModuleDescriptor md = parse("EmbeddedConfigSchema.xml");
443:
444: List points = md.getConfigurationPoints();
445: ConfigurationPointDescriptor cpd = (ConfigurationPointDescriptor) points
446: .get(0);
447: Schema s = cpd.getContributionsSchema();
448:
449: List l = s.getElementModel();
450:
451: assertEquals(1, l.size());
452:
453: ElementModel em = (ElementModel) l.get(0);
454:
455: assertEquals("foo", em.getElementName());
456: }
457:
458: public void testEmbeddedParametersSchema() throws Exception {
459: ModuleDescriptor md = parse("EmbeddedParametersSchema.xml");
460:
461: List points = md.getServicePoints();
462: ServicePointDescriptor spd = (ServicePointDescriptor) points
463: .get(0);
464: Schema s = spd.getParametersSchema();
465:
466: List l = s.getElementModel();
467:
468: assertEquals(1, l.size());
469:
470: ElementModel em = (ElementModel) l.get(0);
471:
472: assertEquals("foo", em.getElementName());
473: }
474:
475: public void testSetPropertyRule() throws Exception {
476: ModuleDescriptor md = parse("SetPropertyRule.xml");
477:
478: List points = md.getConfigurationPoints();
479: ConfigurationPointDescriptor cpd = (ConfigurationPointDescriptor) points
480: .get(0);
481: Schema s = cpd.getContributionsSchema();
482: List l = s.getElementModel();
483:
484: ElementModel em = (ElementModel) l.get(0);
485:
486: List rules = em.getRules();
487:
488: SetPropertyRule rule = (SetPropertyRule) rules.get(0);
489:
490: assertEquals("foo", rule.getPropertyName());
491: assertEquals("bar", rule.getValue());
492: }
493:
494: public void testPushAttributeRule() throws Exception {
495: ModuleDescriptor md = parse("PushAttributeRule.xml");
496:
497: List points = md.getConfigurationPoints();
498: ConfigurationPointDescriptor cpd = (ConfigurationPointDescriptor) points
499: .get(0);
500: Schema s = cpd.getContributionsSchema();
501: List l = s.getElementModel();
502:
503: ElementModel em = (ElementModel) l.get(0);
504:
505: List rules = em.getRules();
506:
507: PushAttributeRule rule = (PushAttributeRule) rules.get(0);
508:
509: assertEquals("foo", rule.getAttributeName());
510: }
511:
512: /** @since 1.1 */
513: public void testPushContentRule() throws Exception {
514: ModuleDescriptor md = parse("PushContentRule.xml");
515:
516: List points = md.getConfigurationPoints();
517: ConfigurationPointDescriptor cpd = (ConfigurationPointDescriptor) points
518: .get(0);
519: Schema s = cpd.getContributionsSchema();
520: List l = s.getElementModel();
521:
522: ElementModel em = (ElementModel) l.get(0);
523:
524: List rules = em.getRules();
525:
526: assertTrue(rules.get(0) instanceof PushContentRule);
527: }
528:
529: /** @since 1.1 */
530: public void testPrivateServicePoint() throws Exception {
531: ModuleDescriptor md = parse("PrivateServicePoint.xml");
532:
533: List points = md.getServicePoints();
534: ServicePointDescriptor spd = (ServicePointDescriptor) points
535: .get(0);
536:
537: assertEquals(Visibility.PRIVATE, spd.getVisibility());
538: }
539:
540: /** @since 1.1 */
541: public void testPrivateConfigurationPoint() throws Exception {
542: ModuleDescriptor md = parse("PrivateConfigurationPoint.xml");
543:
544: List points = md.getConfigurationPoints();
545: ConfigurationPointDescriptor cpd = (ConfigurationPointDescriptor) points
546: .get(0);
547:
548: assertEquals(Visibility.PRIVATE, cpd.getVisibility());
549: }
550:
551: public void testPrivateSchema() throws Exception {
552: Resource location = getResource("PrivateSchema.xml");
553: DefaultErrorHandler eh = new DefaultErrorHandler();
554:
555: XmlResourceProcessor p = new XmlResourceProcessor(_resolver, eh);
556:
557: ModuleDescriptor md = p.processResource(location);
558:
559: SchemaImpl s = (SchemaImpl) md.getSchema("PrivateSchema");
560:
561: assertEquals(Visibility.PRIVATE, s.getVisibility());
562: }
563:
564: /** @since 1.1 */
565:
566: public void testContributionIf() throws Exception {
567: ModuleDescriptor md = parse("ContributionIf.xml");
568:
569: List l = md.getContributions();
570: ContributionDescriptor cd = (ContributionDescriptor) l.get(0);
571:
572: assertEquals("class foo.bar.Blat", cd
573: .getConditionalExpression());
574: }
575:
576: /** @since 1.1 */
577:
578: public void testImplementationIf() throws Exception {
579: ModuleDescriptor md = parse("ImplementationIf.xml");
580:
581: List l = md.getImplementations();
582: ImplementationDescriptor id = (ImplementationDescriptor) l
583: .get(0);
584:
585: assertEquals("class foo.bar.Blat", id
586: .getConditionalExpression());
587: }
588:
589: /** @since 1.1 */
590:
591: public void testModuleWithPackage() throws Exception {
592: ModuleDescriptor md = parse("ModuleWithPackage.xml");
593:
594: assertEquals("my.package", md.getPackageName());
595: }
596:
597: /** @since 1.1 */
598:
599: public void testInterfaceNameQualifiedToModulePackage()
600: throws Exception {
601: ModuleDescriptor md = parse("InterfaceNameQualifiedToModulePackage.xml");
602:
603: ServicePointDescriptor spd = (ServicePointDescriptor) md
604: .getServicePoints().get(0);
605:
606: assertEquals("my.package.MyServiceInterface", spd
607: .getInterfaceClassName());
608: }
609:
610: /** @since 1.1 */
611:
612: public void testNoInterface() throws Exception {
613: ModuleDescriptor md = parse("NoInterface.xml");
614:
615: ServicePointDescriptor spd = (ServicePointDescriptor) md
616: .getServicePoints().get(0);
617:
618: assertEquals("hivemind.test.NoInterface", spd
619: .getInterfaceClassName());
620: }
621: }
|