KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hivemind > test > parse > TestDescriptorParser


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package hivemind.test.parse;
16
17 import java.util.HashMap JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import junit.framework.AssertionFailedError;
22
23 import org.apache.hivemind.ApplicationRuntimeException;
24 import org.apache.hivemind.Attribute;
25 import org.apache.hivemind.Element;
26 import org.apache.hivemind.Resource;
27 import org.apache.hivemind.definition.Occurances;
28 import org.apache.hivemind.definition.Visibility;
29 import org.apache.hivemind.impl.DefaultErrorHandler;
30 import org.apache.hivemind.parse.ConfigurationPointDescriptor;
31 import org.apache.hivemind.parse.ContributionDescriptor;
32 import org.apache.hivemind.parse.CreateInstanceDescriptor;
33 import org.apache.hivemind.parse.DependencyDescriptor;
34 import org.apache.hivemind.parse.ImplementationDescriptor;
35 import org.apache.hivemind.parse.InterceptorDescriptor;
36 import org.apache.hivemind.parse.ModuleDescriptor;
37 import org.apache.hivemind.parse.ServicePointDescriptor;
38 import org.apache.hivemind.parse.XmlResourceProcessor;
39 import org.apache.hivemind.schema.AttributeModel;
40 import org.apache.hivemind.schema.ElementModel;
41 import org.apache.hivemind.schema.Schema;
42 import org.apache.hivemind.schema.impl.SchemaImpl;
43 import org.apache.hivemind.schema.rules.CreateObjectRule;
44 import org.apache.hivemind.schema.rules.InvokeParentRule;
45 import org.apache.hivemind.schema.rules.PushAttributeRule;
46 import org.apache.hivemind.schema.rules.PushContentRule;
47 import org.apache.hivemind.schema.rules.ReadAttributeRule;
48 import org.apache.hivemind.schema.rules.ReadContentRule;
49 import org.apache.hivemind.schema.rules.SetModuleRule;
50 import org.apache.hivemind.schema.rules.SetPropertyRule;
51 import org.apache.hivemind.xml.XmlTestCase;
52
53 /**
54  * Tests for parsing a HiveModule descriptor into a
55  * {@link org.apache.hivemind.parse.ModuleDescriptor} and related objects, using
56  * {@link org.apache.hivemind.parse.DescriptorParser}.
57  *
58  * @author Howard Lewis Ship
59  */

60 public class TestDescriptorParser extends XmlTestCase
61 {
62     private void checkAttributes(Element e, String JavaDoc[] attributes)
63     {
64         List JavaDoc l = e.getAttributes();
65         Map JavaDoc map = new HashMap JavaDoc();
66         int count = l.size();
67
68         for (int i = 0; i < count; i++)
69         {
70             Attribute a = (Attribute) l.get(i);
71             map.put(a.getName(), a.getValue());
72         }
73
74         if (attributes == null)
75             count = 0;
76         else
77             count = attributes.length;
78
79         for (int i = 0; i < count; i += 2)
80         {
81             String JavaDoc name = attributes[i];
82             String JavaDoc value = attributes[i + 1];
83
84             assertEquals("Attribute " + name + " of element " + e.getElementName(), value, map
85                     .get(name));
86
87             map.remove(name);
88         }
89
90         if (map.isEmpty())
91             return;
92
93         throw new AssertionFailedError("Unexpected attribute(s) " + map + " in element "
94                 + e.getElementName() + ".");
95     }
96
97     public void testModuleAttributes() throws Exception JavaDoc
98     {
99         ModuleDescriptor md = parse("GenericModule.xml");
100
101         assertEquals("hivemind.test.parse", md.getModuleId());
102         assertEquals("1.0.0", md.getVersion());
103
104         // package attribute was added in 1.1. Defaultis same as
105
// module id.
106

107         assertEquals("hivemind.test.parse", md.getPackageName());
108     }
109
110     public void testConfigurationPointAttributes() throws Exception JavaDoc
111     {
112         ModuleDescriptor md = parse("GenericModule.xml");
113
114         List JavaDoc l = md.getConfigurationPoints();
115         assertEquals(1, l.size());
116
117         ConfigurationPointDescriptor cpd = (ConfigurationPointDescriptor) l.get(0);
118
119         assertEquals("MyExtensionPoint", cpd.getId());
120         assertEquals(Occurances.ONE_PLUS, cpd.getCount());
121     }
122
123     public void testContributionAttributes() throws Exception JavaDoc
124     {
125         ModuleDescriptor md = parse("GenericModule.xml");
126
127         List JavaDoc l = md.getContributions();
128         assertEquals(1, l.size());
129
130         ContributionDescriptor cd = (ContributionDescriptor) l.get(0);
131
132         assertEquals("MyExtensionPoint", cd.getConfigurationId());
133     }
134
135     public void testContributionElements() throws Exception JavaDoc
136     {
137         ModuleDescriptor md = parse("GenericModule.xml");
138
139         List JavaDoc l = md.getContributions();
140         assertEquals(1, l.size());
141
142         ContributionDescriptor cd = (ContributionDescriptor) l.get(0);
143
144         l = cd.getElements();
145         assertEquals(2, l.size());
146
147         Element e = (Element) l.get(0);
148
149         assertEquals("foo1", e.getElementName());
150         assertEquals(0, e.getElements().size());
151         assertEquals("foo1 content", e.getContent());
152         checkAttributes(e, new String JavaDoc[]
153         { "bar", "baz" });
154
155         e = (Element) l.get(1);
156
157         assertEquals("foo2", e.getElementName());
158         assertEquals(1, e.getElements().size());
159         assertEquals("", e.getContent());
160         checkAttributes(e, new String JavaDoc[]
161         { "zip", "zap", "fred", "barney" });
162
163         l = e.getElements();
164         e = (Element) l.get(0);
165
166         assertEquals("foo3", e.getElementName());
167         assertEquals(0, e.getElements().size());
168         assertEquals("", e.getContent());
169         checkAttributes(e, new String JavaDoc[]
170         { "gnip", "gnop" });
171     }
172
173     public void testServicePoint() throws Exception JavaDoc
174     {
175         ModuleDescriptor md = parse("GenericModule.xml");
176
177         List JavaDoc l = md.getServicePoints();
178         assertEquals(2, l.size());
179         ServicePointDescriptor spd = (ServicePointDescriptor) l.get(0);
180
181         assertEquals("MyService1", spd.getId());
182         assertEquals("package.MyService", spd.getInterfaceClassName());
183
184         CreateInstanceDescriptor cid = (CreateInstanceDescriptor) spd.getInstanceBuilder();
185         assertEquals("package.impl.MyServiceImpl", cid.getInstanceClassName());
186
187         l = spd.getInterceptors();
188         assertEquals(2, l.size());
189
190         InterceptorDescriptor id = (InterceptorDescriptor) l.get(0);
191         assertEquals("MyInterceptor", id.getFactoryServiceId());
192         assertEquals("OtherInterceptor", id.getBefore());
193         assertEquals("MyInterceptorName", id.getName());
194
195         id = (InterceptorDescriptor) l.get(1);
196         assertEquals("OtherInterceptor", id.getFactoryServiceId());
197         assertEquals("MyInterceptorName", id.getAfter());
198         assertNull(id.getName());
199     }
200
201     public void testImplementation() throws Exception JavaDoc
202     {
203         ModuleDescriptor md = parse("GenericModule.xml");
204         List JavaDoc l = md.getImplementations();
205         assertEquals(1, l.size());
206
207         ImplementationDescriptor id1 = (ImplementationDescriptor) l.get(0);
208
209         assertEquals("othermodule.OtherService", id1.getServiceId());
210
211         l = id1.getInterceptors();
212         assertEquals(1, l.size());
213
214         InterceptorDescriptor id2 = (InterceptorDescriptor) l.get(0);
215
216         assertEquals("MyInterceptor", id2.getFactoryServiceId());
217     }
218
219     public void testConfigurationPointSchema() throws Exception JavaDoc
220     {
221         ModuleDescriptor md = parse("GenericModule.xml");
222
223         List JavaDoc l = md.getConfigurationPoints();
224         assertEquals(1, l.size());
225         ConfigurationPointDescriptor cpd = (ConfigurationPointDescriptor) l.get(0);
226
227         String JavaDoc schemaId = cpd.getContributionsSchemaId();
228
229         assertEquals("Fool", schemaId);
230
231         Schema schema = md.getSchema(schemaId);
232
233         assertNotNull(schema.getLocation());
234
235         l = schema.getElementModel();
236         assertEquals(2, l.size());
237
238         ElementModel em = (ElementModel) l.get(0);
239
240         assertEquals("foo1", em.getElementName());
241         assertEquals(0, em.getElementModel().size());
242
243         List JavaDoc al = em.getAttributeModels();
244         assertEquals(2, al.size());
245
246         AttributeModel am = (AttributeModel) al.get(0);
247         assertEquals("bar", am.getName());
248         assertEquals(true, am.isRequired());
249
250         am = (AttributeModel) al.get(1);
251         assertEquals("biff", am.getName());
252         assertEquals("glob", am.getDefault());
253
254         em = (ElementModel) l.get(1);
255
256         assertEquals("foo2", em.getElementName());
257         assertEquals(2, em.getAttributeModels().size());
258
259         l = em.getElementModel();
260
261         assertEquals(1, l.size());
262
263         em = (ElementModel) l.get(0);
264
265         assertEquals(1, em.getAttributeModels().size());
266     }
267
268     public void testBadElementAttributeKey() throws Exception JavaDoc
269     {
270         interceptLogging();
271
272         parse("BadElementAttributeKey.xml");
273
274         assertLoggedMessage("Schema Bad is invalid: Key attribute 'bad' of element 'foo' never declared.");
275     }
276
277     public void testRules() throws Exception JavaDoc
278     {
279         ModuleDescriptor md = parse("GenericModule.xml");
280         List JavaDoc l = md.getConfigurationPoints();
281         assertEquals(1, l.size());
282         ConfigurationPointDescriptor cpd = (ConfigurationPointDescriptor) l.get(0);
283         Schema schema = md.getSchema(cpd.getContributionsSchemaId());
284
285         l = schema.getElementModel();
286
287         ElementModel em = (ElementModel) l.get(0);
288
289         List JavaDoc rl = em.getRules();
290
291         assertEquals(5, rl.size());
292
293         CreateObjectRule rule1 = (CreateObjectRule) rl.get(0);
294         assertEquals("package.Foo1", rule1.getClassName());
295
296         ReadAttributeRule rule2 = (ReadAttributeRule) rl.get(1);
297
298         assertEquals("bazomatic", rule2.getPropertyName());
299         assertEquals("bar", rule2.getAttributeName());
300         assertEquals(true, rule2.getSkipIfNull());
301
302         ReadContentRule rule3 = (ReadContentRule) rl.get(2);
303
304         assertEquals("description", rule3.getPropertyName());
305
306         SetModuleRule rule4 = (SetModuleRule) rl.get(3);
307
308         assertEquals("module", rule4.getPropertyName());
309
310         InvokeParentRule rule5 = (InvokeParentRule) rl.get(4);
311
312         assertEquals("addElement", rule5.getMethodName());
313     }
314
315     public void testParametersSchema() throws Exception JavaDoc
316     {
317         ModuleDescriptor md = parse("GenericModule.xml");
318         List JavaDoc l = md.getServicePoints();
319         assertEquals(2, l.size());
320         ServicePointDescriptor spd = (ServicePointDescriptor) l.get(1);
321
322         String JavaDoc schemaId = spd.getParametersSchemaId();
323
324         assertEquals("Parameters", schemaId);
325
326         ElementModel em = (ElementModel) md.getSchema(schemaId).getElementModel().get(0);
327
328         assertEquals("myParameter", em.getElementName());
329     }
330
331     public void testDuplicateContributionsSchema() throws Exception JavaDoc
332     {
333         interceptLogging();
334
335         ModuleDescriptor md = parse("DuplicateSchemas.xml");
336
337         assertLoggedMessagePattern("Multiple contributions schemas specified for configuration MyConfiguration. Using locally defined schema \\(at ");
338
339         ConfigurationPointDescriptor cpd = (ConfigurationPointDescriptor) md
340                 .getConfigurationPoints().get(0);
341         Schema nestedSchema = cpd.getContributionsSchema();
342
343         assertNotNull(nestedSchema);
344
345         ElementModel em = (ElementModel) nestedSchema.getElementModel().get(0);
346
347         assertEquals("myParameter", em.getElementName());
348     }
349
350     public void testDuplicateParametersSchema() throws Exception JavaDoc
351     {
352         interceptLogging();
353
354         ModuleDescriptor md = parse("DuplicateSchemas.xml");
355
356         assertLoggedMessagePattern("Multiple parameters schemas specified for service MyServiceFactory. Using locally defined schema \\(at ");
357
358         ServicePointDescriptor spd = (ServicePointDescriptor) md.getServicePoints().get(0);
359         Schema nestedSchema = spd.getParametersSchema();
360
361         assertNotNull(nestedSchema);
362
363         ElementModel em = (ElementModel) nestedSchema.getElementModel().get(0);
364
365         assertEquals("myParameter", em.getElementName());
366     }
367
368     public void testDependency() throws Exception JavaDoc
369     {
370         ModuleDescriptor md = parse("GenericModule.xml");
371         List JavaDoc l = md.getDependencies();
372         assertEquals(1, l.size());
373         DependencyDescriptor dd = (DependencyDescriptor) l.get(0);
374
375         assertEquals("my.module", dd.getModuleId());
376         assertEquals("1.0.0", dd.getVersion());
377     }
378
379     public void testBadElement() throws Exception JavaDoc
380     {
381         try
382         {
383             parse("BadElement.xml");
384
385             unreachable();
386         }
387         catch (ApplicationRuntimeException ex)
388         {
389             assertExceptionSubstring(
390                     ex,
391                     "Unexpected element bad-element within module/schema/element");
392         }
393     }
394
395     public void testBadAttribute() throws Exception JavaDoc
396     {
397         interceptLogging();
398
399         parse("BadAttribute.xml");
400
401         assertLoggedMessagePattern("Unknown attribute 'bad-attribute' in element module/schema\\.");
402     }
403
404     public void testMissingAttribute() throws Exception JavaDoc
405     {
406         try
407         {
408             parse("MissingAttribute.xml");
409
410             unreachable();
411         }
412         catch (ApplicationRuntimeException ex)
413         {
414             assertExceptionSubstring(
415                     ex,
416                     "Missing required attribute 'name' in element module/schema/element");
417         }
418     }
419
420     public void testBadConfigurationId() throws Exception JavaDoc
421     {
422         interceptLogging();
423
424         parse("BadConfigurationId.xml");
425
426         assertLoggedMessagePattern("Attribute id \\(foo\\.bar\\) of element module/configuration-point is improperly "
427                 + "formatted\\. Schema and extension point ids should be simple names with "
428                 + "no punctuation\\.");
429     }
430
431     public void testBadModuleId() throws Exception JavaDoc
432     {
433         interceptLogging();
434
435         parse("BadModuleId.xml");
436
437         assertLoggedMessagePattern("Attribute id \\(big bad\\) of element module is improperly "
438                 + "formatted\\. Module identifiers should consist of a period-seperated series "
439                 + "of names, like a Java package\\.");
440     }
441
442     public void testBadVersion() throws Exception JavaDoc
443     {
444         interceptLogging();
445
446         parse("BadVersion.xml");
447
448         assertLoggedMessagePattern("Attribute version \\(1\\.0\\.alpha\\) of element module is improperly "
449                 + "formatted\\. Version numbers should be a sequence of three numbers "
450                 + "separated by periods\\.");
451     }
452
453     public void testSchemaDescription() throws Exception JavaDoc
454     {
455         ModuleDescriptor md = parse("SchemaDescription.xml");
456
457         List JavaDoc points = md.getConfigurationPoints();
458
459         ConfigurationPointDescriptor cpd = (ConfigurationPointDescriptor) points.get(0);
460
461         assertEquals("PointWithDescription", cpd.getId());
462     }
463
464     public void testEmbeddedConfigSchema() throws Exception JavaDoc
465     {
466         ModuleDescriptor md = parse("EmbeddedConfigSchema.xml");
467
468         List JavaDoc points = md.getConfigurationPoints();
469         ConfigurationPointDescriptor cpd = (ConfigurationPointDescriptor) points.get(0);
470         Schema s = cpd.getContributionsSchema();
471
472         List JavaDoc l = s.getElementModel();
473
474         assertEquals(1, l.size());
475
476         ElementModel em = (ElementModel) l.get(0);
477
478         assertEquals("foo", em.getElementName());
479     }
480
481     public void testEmbeddedParametersSchema() throws Exception JavaDoc
482     {
483         ModuleDescriptor md = parse("EmbeddedParametersSchema.xml");
484
485         List JavaDoc points = md.getServicePoints();
486         ServicePointDescriptor spd = (ServicePointDescriptor) points.get(0);
487         Schema s = spd.getParametersSchema();
488
489         List JavaDoc l = s.getElementModel();
490
491         assertEquals(1, l.size());
492
493         ElementModel em = (ElementModel) l.get(0);
494
495         assertEquals("foo", em.getElementName());
496     }
497
498     public void testSetPropertyRule() throws Exception JavaDoc
499     {
500         ModuleDescriptor md = parse("SetPropertyRule.xml");
501
502         List JavaDoc points = md.getConfigurationPoints();
503         ConfigurationPointDescriptor cpd = (ConfigurationPointDescriptor) points.get(0);
504         Schema s = cpd.getContributionsSchema();
505         List JavaDoc l = s.getElementModel();
506
507         ElementModel em = (ElementModel) l.get(0);
508
509         List JavaDoc rules = em.getRules();
510
511         SetPropertyRule rule = (SetPropertyRule) rules.get(0);
512
513         assertEquals("foo", rule.getPropertyName());
514         assertEquals("bar", rule.getValue());
515     }
516
517     public void testPushAttributeRule() throws Exception JavaDoc
518     {
519         ModuleDescriptor md = parse("PushAttributeRule.xml");
520
521         List JavaDoc points = md.getConfigurationPoints();
522         ConfigurationPointDescriptor cpd = (ConfigurationPointDescriptor) points.get(0);
523         Schema s = cpd.getContributionsSchema();
524         List JavaDoc l = s.getElementModel();
525
526         ElementModel em = (ElementModel) l.get(0);
527
528         List JavaDoc rules = em.getRules();
529
530         PushAttributeRule rule = (PushAttributeRule) rules.get(0);
531
532         assertEquals("foo", rule.getAttributeName());
533     }
534
535     /** @since 1.1 */
536     public void testPushContentRule() throws Exception JavaDoc
537     {
538         ModuleDescriptor md = parse("PushContentRule.xml");
539
540         List JavaDoc points = md.getConfigurationPoints();
541         ConfigurationPointDescriptor cpd = (ConfigurationPointDescriptor) points.get(0);
542         Schema s = cpd.getContributionsSchema();
543         List JavaDoc l = s.getElementModel();
544
545         ElementModel em = (ElementModel) l.get(0);
546
547         List JavaDoc rules = em.getRules();
548
549         assertTrue(rules.get(0) instanceof PushContentRule);
550     }
551
552     /** @since 1.1 */
553     public void testPrivateServicePoint() throws Exception JavaDoc
554     {
555         ModuleDescriptor md = parse("PrivateServicePoint.xml");
556
557         List JavaDoc points = md.getServicePoints();
558         ServicePointDescriptor spd = (ServicePointDescriptor) points.get(0);
559
560         assertEquals(Visibility.PRIVATE, spd.getVisibility());
561     }
562
563     /** @since 1.1 */
564     public void testPrivateConfigurationPoint() throws Exception JavaDoc
565     {
566         ModuleDescriptor md = parse("PrivateConfigurationPoint.xml");
567
568         List JavaDoc points = md.getConfigurationPoints();
569         ConfigurationPointDescriptor cpd = (ConfigurationPointDescriptor) points.get(0);
570
571         assertEquals(Visibility.PRIVATE, cpd.getVisibility());
572     }
573
574     public void testPrivateSchema() throws Exception JavaDoc
575     {
576         Resource location = getResource("PrivateSchema.xml");
577         DefaultErrorHandler eh = new DefaultErrorHandler();
578
579         XmlResourceProcessor p = new XmlResourceProcessor(_resolver, eh);
580
581         ModuleDescriptor md = p.processResource(location);
582
583         SchemaImpl s = (SchemaImpl) md.getSchema("PrivateSchema");
584
585         assertEquals(Visibility.PRIVATE, s.getVisibility());
586     }
587
588     /** @since 1.1 */
589
590     public void testContributionIf() throws Exception JavaDoc
591     {
592         ModuleDescriptor md = parse("ContributionIf.xml");
593
594         List JavaDoc l = md.getContributions();
595         ContributionDescriptor cd = (ContributionDescriptor) l.get(0);
596
597         assertEquals("class foo.bar.Blat", cd.getConditionalExpression());
598     }
599
600     /** @since 1.1 */
601
602     public void testImplementationIf() throws Exception JavaDoc
603     {
604         ModuleDescriptor md = parse("ImplementationIf.xml");
605
606         List JavaDoc l = md.getImplementations();
607         ImplementationDescriptor id = (ImplementationDescriptor) l.get(0);
608
609         assertEquals("class foo.bar.Blat", id.getConditionalExpression());
610     }
611
612     /** @since 1.1 */
613
614     public void testModuleWithPackage() throws Exception JavaDoc
615     {
616         ModuleDescriptor md = parse("ModuleWithPackage.xml");
617
618         assertEquals("my.package", md.getPackageName());
619     }
620
621     /** @since 1.1 */
622
623     public void testInterfaceNameQualifiedToModulePackage() throws Exception JavaDoc
624     {
625         ModuleDescriptor md = parse("InterfaceNameQualifiedToModulePackage.xml");
626
627         ServicePointDescriptor spd = (ServicePointDescriptor) md.getServicePoints().get(0);
628
629         assertEquals("my.package.MyServiceInterface", spd.getInterfaceClassName());
630     }
631
632     /** @since 1.1 */
633
634     public void testNoInterface() throws Exception JavaDoc
635     {
636         ModuleDescriptor md = parse("NoInterface.xml");
637
638         ServicePointDescriptor spd = (ServicePointDescriptor) md.getServicePoints().get(0);
639
640         assertEquals("hivemind.test.NoInterface", spd.getInterfaceClassName());
641     }
642 }
Popular Tags