KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > lib > groovy > TestHiveMindBuilder


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 org.apache.hivemind.lib.groovy;
16
17 import groovy.lang.Binding;
18 import groovy.lang.GroovyCodeSource;
19 import groovy.lang.GroovyShell;
20 import groovy.lang.Script;
21
22 import org.apache.hivemind.ApplicationRuntimeException;
23 import org.apache.hivemind.ErrorHandler;
24 import org.apache.hivemind.Resource;
25 import org.apache.hivemind.impl.DefaultErrorHandler;
26 import org.apache.hivemind.parse.DescriptorParser;
27 import org.apache.hivemind.test.HiveMindTestCase;
28 import org.easymock.MockControl;
29 import org.easymock.internal.EqualsMatcher;
30 import org.xml.sax.Attributes JavaDoc;
31 import org.xml.sax.ContentHandler JavaDoc;
32 import org.xml.sax.helpers.AttributesImpl JavaDoc;
33
34 public class TestHiveMindBuilder extends HiveMindTestCase
35 {
36     public void testBasicScript() throws Exception JavaDoc
37     {
38         MockControl control = newControl(ContentHandler JavaDoc.class);
39         control.setDefaultMatcher(new SAXEqualsMatcher());
40
41         ContentHandler JavaDoc mock = (ContentHandler JavaDoc) control.getMock();
42
43         mock.setDocumentLocator(HiveMindBuilder.GROOVY_LOCATOR);
44
45         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
46
47         attrs.addAttribute("", "id", "id", "", "basic");
48         attrs.addAttribute("", "version", "version", "", "1.0.0");
49
50         mock.startElement("", "module", "module", attrs);
51         mock.endElement("", "module", "module");
52
53         replayControls();
54
55         Script script = new GroovyShell().parse("processor.module(id:'basic', version:'1.0.0')");
56
57         runScript(script, mock);
58     }
59
60     public void testLinePreciseErrorReporting() throws Exception JavaDoc
61     {
62         Resource resource = getResource("missingModuleId.groovy");
63
64         ErrorHandler handler = new DefaultErrorHandler();
65         DescriptorParser parser = new DescriptorParser(handler);
66
67         parser.initialize(resource, getClassResolver());
68
69         GroovyCodeSource source = new GroovyCodeSource(resource.getResourceURL());
70
71         Script script = new GroovyShell().parse(source);
72
73         try
74         {
75             runScript(script, parser);
76
77             unreachable();
78         }
79         catch (ApplicationRuntimeException e)
80         {
81             assertExceptionRegexp(
82                     e,
83                     "Missing required attribute .+missingModuleId\\.groovy, line 15\\)\\.");
84         }
85     }
86
87     private void runScript(Script script, ContentHandler JavaDoc handler)
88     {
89         HiveMindBuilder builder = new HiveMindBuilder(handler);
90
91         Binding processorBinding = new Binding();
92         processorBinding.setVariable("processor", builder);
93
94         script.setBinding(processorBinding);
95
96         script.run();
97     }
98
99     private static class SAXEqualsMatcher extends EqualsMatcher
100     {
101         protected boolean argumentMatches(Object JavaDoc expected, Object JavaDoc actual)
102         {
103             if ((expected instanceof Attributes JavaDoc) && (actual instanceof Attributes JavaDoc))
104             {
105                 Attributes JavaDoc expectedAttributes = (Attributes JavaDoc) expected;
106                 Attributes JavaDoc actualAttributes = (Attributes JavaDoc) actual;
107
108                 if (expectedAttributes.getLength() != actualAttributes.getLength())
109                     return false;
110
111                 for (int i = 0; i < expectedAttributes.getLength(); i++)
112                 {
113                     if (!expectedAttributes.getLocalName(i)
114                             .equals(actualAttributes.getLocalName(i))
115                             || !expectedAttributes.getValue(i).equals(actualAttributes.getValue(i)))
116                         return false;
117                 }
118                 return true;
119             }
120
121             return super.argumentMatches(expected, actual);
122         }
123     }
124 }
Popular Tags