1 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 ; 31 import org.xml.sax.ContentHandler ; 32 import org.xml.sax.helpers.AttributesImpl ; 33 34 public class TestHiveMindBuilder extends HiveMindTestCase 35 { 36 public void testBasicScript() throws Exception 37 { 38 MockControl control = newControl(ContentHandler .class); 39 control.setDefaultMatcher(new SAXEqualsMatcher()); 40 41 ContentHandler mock = (ContentHandler ) control.getMock(); 42 43 mock.setDocumentLocator(HiveMindBuilder.GROOVY_LOCATOR); 44 45 AttributesImpl attrs = new AttributesImpl (); 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 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 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 expected, Object actual) 102 { 103 if ((expected instanceof Attributes ) && (actual instanceof Attributes )) 104 { 105 Attributes expectedAttributes = (Attributes ) expected; 106 Attributes actualAttributes = (Attributes ) 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 |