/* $$ Clover has instrumented this file $$ */// Copyright 2004 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.tapestry.test;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.Resource;
import org.apache.hivemind.parse.AbstractParser;
import org.apache.hivemind.parse.ElementParseInfo;
import org.apache.tapestry.test.assertions.AssertOutput;
import org.apache.tapestry.test.assertions.AssertRegexp;
import org.apache.tapestry.test.assertions.RegexpMatch;
import org.apache.tapestry.util.xml.DocumentParseException;
/**
* Parses Tapestry test scripts; XML files that define an execution environment and
* a sequence of operations and assertions.
*
* @author Howard Lewis Ship
* @since 3.1
*/
public class IntegrationTestScriptParser extends AbstractParser
{public static com.cortexeb.tools.clover.d __CLOVER_368_0 = com.cortexeb.tools.clover.aq.getRecorder(new char[] {67,58,92,119,111,114,107,115,112,97,99,101,92,106,97,107,97,114,116,97,45,116,97,112,101,115,116,114,121,92,102,114,97,109,101,119,111,114,107,92,116,97,114,103,101,116,92,99,108,111,118,101,114,45,100,98},1096998272901L);
private ScriptDescriptor _scriptDescriptor;
private Map _attributes;
private String _elementName;
/**
* Map from element name to a ElementParseInfo
*/
private final Map _elementParseInfo = new HashMap();
public IntegrationTestScriptParser()
{try { __CLOVER_368_0.M[1841]++;
__CLOVER_368_0.S[8059]++;initializeFromPropertiesFile();
} finally { }}
private void initializeFromPropertiesFile()
{try { __CLOVER_368_0.M[1842]++;
__CLOVER_368_0.S[8060]++;Properties p = new Properties();
__CLOVER_368_0.S[8061]++;InputStream stream = null;
__CLOVER_368_0.S[8062]++;try
{
__CLOVER_368_0.S[8063]++;InputStream rawStream = getClass().getResourceAsStream("ScriptParser.properties");
__CLOVER_368_0.S[8064]++;stream = new BufferedInputStream(rawStream);
__CLOVER_368_0.S[8065]++;p.load(stream);
__CLOVER_368_0.S[8066]++;stream.close();
__CLOVER_368_0.S[8067]++;stream = null;
}
catch (IOException ex)
{
__CLOVER_368_0.S[8068]++;throw new ApplicationRuntimeException(ex);
}
finally
{
__CLOVER_368_0.S[8069]++;close(stream);
}
__CLOVER_368_0.S[8070]++;initializeFromProperties(p);
} finally { }}
private void initializeFromProperties(Properties p)
{try { __CLOVER_368_0.M[1843]++;
__CLOVER_368_0.S[8071]++;Iterator i = p.keySet().iterator();
__CLOVER_368_0.S[8072]++;while ((((i.hasNext()) && (++__CLOVER_368_0.CT[1383] != 0)) || (++__CLOVER_368_0.CF[1383] == 0))){
{
__CLOVER_368_0.S[8073]++;String key = (String) i.next();
__CLOVER_368_0.S[8074]++;String value = p.getProperty(key);
__CLOVER_368_0.S[8075]++;initializeFromProperty(key, value);
}}
} finally { }}
private void initializeFromProperty(String key, String value)
{try { __CLOVER_368_0.M[1844]++;
// Ignore keys that don't start with "required."
__CLOVER_368_0.S[8076]++;if ((((!key.startsWith("required.")) && (++__CLOVER_368_0.CT[1384] != 0)) || (++__CLOVER_368_0.CF[1384] == 0))){
__CLOVER_368_0.S[8077]++;return;}
__CLOVER_368_0.S[8078]++;int lastDotx = key.lastIndexOf('.');
__CLOVER_368_0.S[8079]++;String elementName = key.substring(9, lastDotx);
__CLOVER_368_0.S[8080]++;String attributeName = key.substring(lastDotx + 1);
__CLOVER_368_0.S[8081]++;boolean required = value.equalsIgnoreCase("true");
__CLOVER_368_0.S[8082]++;ElementParseInfo epi = getElementParseInfo(elementName);
__CLOVER_368_0.S[8083]++;epi.addAttribute(attributeName, required);
} finally { }}
private void close(InputStream stream)
{try { __CLOVER_368_0.M[1845]++;
__CLOVER_368_0.S[8084]++;try
{
__CLOVER_368_0.S[8085]++;if ((((stream != null) && (++__CLOVER_368_0.CT[1385] != 0)) || (++__CLOVER_368_0.CF[1385] == 0))){
__CLOVER_368_0.S[8086]++;stream.close();}
}
catch (IOException ex)
{
// Ingore.
}
} finally { }}
public ScriptDescriptor parse(Resource script)
{try { __CLOVER_368_0.M[1846]++;
__CLOVER_368_0.S[8087]++;initializeParser(script, STATE_INITIAL);
__CLOVER_368_0.S[8088]++;try
{
__CLOVER_368_0.S[8089]++;startParse();
__CLOVER_368_0.S[8090]++;return _scriptDescriptor;
}
finally
{
__CLOVER_368_0.S[8091]++;resetParser();
}
} finally { }}
private void startParse()
{try { __CLOVER_368_0.M[1847]++;
__CLOVER_368_0.S[8092]++;try
{
__CLOVER_368_0.S[8093]++;SAXParserFactory factory = SAXParserFactory.newInstance();
__CLOVER_368_0.S[8094]++;SAXParser parser = factory.newSAXParser();
__CLOVER_368_0.S[8095]++;Resource resource = getResource();
__CLOVER_368_0.S[8096]++;URL url = resource.getResourceURL();
__CLOVER_368_0.S[8097]++;InputStream is = url.openStream();
__CLOVER_368_0.S[8098]++;parser.parse(is, this);
}
catch (Exception ex)
{
__CLOVER_368_0.S[8099]++;throw new DocumentParseException(ex.getMessage(), getResource(), ex);
}
} finally { }}
protected void initializeParser(Resource resource, int startState)
{try { __CLOVER_368_0.M[1848]++;
__CLOVER_368_0.S[8100]++;super.initializeParser(resource, startState);
__CLOVER_368_0.S[8101]++;_attributes = new HashMap();
} finally { }}
protected void resetParser()
{try { __CLOVER_368_0.M[1849]++;
__CLOVER_368_0.S[8102]++;_attributes = null;
__CLOVER_368_0.S[8103]++;_elementName = null;
__CLOVER_368_0.S[8104]++;_scriptDescriptor = null;
__CLOVER_368_0.S[8105]++;super.resetParser();
} finally { }}
private static final int STATE_INITIAL = 0;
private static final int STATE_TEST_SCRIPT = 1;
private static final int STATE_SERVLET = 2;
private static final int STATE_INIT_PARAMETER = 3;
private static final int STATE_REQUEST = 4;
private static final int STATE_ASSERT_OUTPUT = 5;
private static final int STATE_ASSERT_REGEXP = 6;
private static final int STATE_MATCH = 7;
private static final int STATE_NO_CONTENT = 1000;
protected void begin(String elementName, Map attributes)
{try { __CLOVER_368_0.M[1850]++;
__CLOVER_368_0.S[8106]++;_elementName = elementName;
__CLOVER_368_0.S[8107]++;_attributes = attributes;
__CLOVER_368_0.S[8108]++;switch (getState())
{
case STATE_INITIAL :
__CLOVER_368_0.S[8109]++;beginInitial();
__CLOVER_368_0.S[8110]++;break;
case STATE_TEST_SCRIPT :
__CLOVER_368_0.S[8111]++;beginTestScript();
__CLOVER_368_0.S[8112]++;break;
case STATE_SERVLET :
__CLOVER_368_0.S[8113]++;beginServlet();
__CLOVER_368_0.S[8114]++;break;
case STATE_REQUEST :
__CLOVER_368_0.S[8115]++;beginRequest();
__CLOVER_368_0.S[8116]++;break;
case STATE_ASSERT_REGEXP :
__CLOVER_368_0.S[8117]++;beginAssertRegexp();
__CLOVER_368_0.S[8118]++;break;
default :
__CLOVER_368_0.S[8119]++;unexpectedElement(_elementName);
}
} finally { }}
protected void end(String elementName)
{try { __CLOVER_368_0.M[1851]++;
__CLOVER_368_0.S[8120]++;_elementName = elementName;
__CLOVER_368_0.S[8121]++;switch (getState())
{
case STATE_ASSERT_OUTPUT :
__CLOVER_368_0.S[8122]++;endAssertOutput();
__CLOVER_368_0.S[8123]++;break;
case STATE_ASSERT_REGEXP :
__CLOVER_368_0.S[8124]++;endAssertRegexp();
__CLOVER_368_0.S[8125]++;break;
case STATE_MATCH :
__CLOVER_368_0.S[8126]++;endMatch();
__CLOVER_368_0.S[8127]++;break;
default :
__CLOVER_368_0.S[8128]++;break;
}
__CLOVER_368_0.S[8129]++;pop();
} finally { }}
private void beginInitial()
{try { __CLOVER_368_0.M[1852]++;
__CLOVER_368_0.S[8130]++;if ((((_elementName.equals("test-script")) && (++__CLOVER_368_0.CT[1386] != 0)) || (++__CLOVER_368_0.CF[1386] == 0))){
{
__CLOVER_368_0.S[8131]++;enterTestScript();
__CLOVER_368_0.S[8132]++;return;
}}
__CLOVER_368_0.S[8133]++;unexpectedElement(_elementName);
} finally { }}
private void beginTestScript()
{try { __CLOVER_368_0.M[1853]++;
__CLOVER_368_0.S[8134]++;if ((((_elementName.equals("servlet")) && (++__CLOVER_368_0.CT[1387] != 0)) || (++__CLOVER_368_0.CF[1387] == 0))){
{
__CLOVER_368_0.S[8135]++;enterServlet();
__CLOVER_368_0.S[8136]++;return;
}}
__CLOVER_368_0.S[8137]++;if ((((_elementName.equals("request")) && (++__CLOVER_368_0.CT[1388] != 0)) || (++__CLOVER_368_0.CF[1388] == 0))){
{
__CLOVER_368_0.S[8138]++;enterRequest();
__CLOVER_368_0.S[8139]++;return;
}}
__CLOVER_368_0.S[8140]++;unexpectedElement(_elementName);
} finally { }}
private void beginServlet()
{try { __CLOVER_368_0.M[1854]++;
__CLOVER_368_0.S[8141]++;if ((((_elementName.equals("init-parameter")) && (++__CLOVER_368_0.CT[1389] != 0)) || (++__CLOVER_368_0.CF[1389] == 0))){
{
__CLOVER_368_0.S[8142]++;enterInitParameter();
__CLOVER_368_0.S[8143]++;return;
}}
__CLOVER_368_0.S[8144]++;unexpectedElement(_elementName);
} finally { }}
private void enterRequest()
{try { __CLOVER_368_0.M[1855]++;
__CLOVER_368_0.S[8145]++;validateAttributes();
__CLOVER_368_0.S[8146]++;String servletName = getAttribute("servlet");
__CLOVER_368_0.S[8147]++;String servletPath = getAttribute("servlet-path", "/app");
__CLOVER_368_0.S[8148]++;RequestDescriptor rd = new RequestDescriptor();
__CLOVER_368_0.S[8149]++;rd.setServletName(servletName);
__CLOVER_368_0.S[8150]++;rd.setServletPath(servletPath);
__CLOVER_368_0.S[8151]++;ScriptDescriptor sd = (ScriptDescriptor) peekObject();
__CLOVER_368_0.S[8152]++;sd.addRequestDescriptor(rd);
__CLOVER_368_0.S[8153]++;push(_elementName, rd, STATE_REQUEST);
} finally { }}
public void beginRequest()
{try { __CLOVER_368_0.M[1856]++;
__CLOVER_368_0.S[8154]++;if ((((_elementName.equals("parameter")) && (++__CLOVER_368_0.CT[1390] != 0)) || (++__CLOVER_368_0.CF[1390] == 0))){
{
__CLOVER_368_0.S[8155]++;enterParameter();
__CLOVER_368_0.S[8156]++;return;
}}
__CLOVER_368_0.S[8157]++;if ((((_elementName.equals("assert-output")) && (++__CLOVER_368_0.CT[1391] != 0)) || (++__CLOVER_368_0.CF[1391] == 0))){
{
__CLOVER_368_0.S[8158]++;enterAssertOutput();
__CLOVER_368_0.S[8159]++;return;
}}
__CLOVER_368_0.S[8160]++;if ((((_elementName.equals("assert-regexp")) && (++__CLOVER_368_0.CT[1392] != 0)) || (++__CLOVER_368_0.CF[1392] == 0))){
{
__CLOVER_368_0.S[8161]++;enterAssertRegexp();
__CLOVER_368_0.S[8162]++;return;
}}
__CLOVER_368_0.S[8163]++;unexpectedElement(_elementName);
} finally { }}
private void enterAssertOutput()
{try { __CLOVER_368_0.M[1857]++;
__CLOVER_368_0.S[8164]++;validateAttributes();
__CLOVER_368_0.S[8165]++;AssertOutput ao = new AssertOutput();
__CLOVER_368_0.S[8166]++;RequestDescriptor rd = (RequestDescriptor) peekObject();
__CLOVER_368_0.S[8167]++;rd.addAssertion(ao);
__CLOVER_368_0.S[8168]++;push(_elementName, ao, STATE_ASSERT_OUTPUT, false);
} finally { }}
private void enterAssertRegexp()
{try { __CLOVER_368_0.M[1858]++;
__CLOVER_368_0.S[8169]++;validateAttributes();
__CLOVER_368_0.S[8170]++;int subgroup = getIntAttribute("subgroup", 0);
__CLOVER_368_0.S[8171]++;AssertRegexp ar = new AssertRegexp();
__CLOVER_368_0.S[8172]++;ar.setSubgroup(subgroup);
__CLOVER_368_0.S[8173]++;RequestDescriptor rd = (RequestDescriptor) peekObject();
__CLOVER_368_0.S[8174]++;rd.addAssertion(ar);
__CLOVER_368_0.S[8175]++;push(_elementName, ar, STATE_ASSERT_REGEXP, false);
} finally { }}
private void beginAssertRegexp()
{try { __CLOVER_368_0.M[1859]++;
__CLOVER_368_0.S[8176]++;if ((((_elementName.equals("match")) && (++__CLOVER_368_0.CT[1393] != 0)) || (++__CLOVER_368_0.CF[1393] == 0))){
{
__CLOVER_368_0.S[8177]++;enterMatch();
__CLOVER_368_0.S[8178]++;return;
}}
__CLOVER_368_0.S[8179]++;unexpectedElement(_elementName);
} finally { }}
private void enterMatch()
{try { __CLOVER_368_0.M[1860]++;
__CLOVER_368_0.S[8180]++;validateAttributes();
__CLOVER_368_0.S[8181]++;RegexpMatch m = new RegexpMatch();
__CLOVER_368_0.S[8182]++;AssertRegexp ar = (AssertRegexp) peekObject();
__CLOVER_368_0.S[8183]++;ar.addMatch(m);
__CLOVER_368_0.S[8184]++;push(_elementName, m, STATE_MATCH, false);
} finally { }}
private void endAssertOutput()
{try { __CLOVER_368_0.M[1861]++;
__CLOVER_368_0.S[8185]++;String content = peekContent();
__CLOVER_368_0.S[8186]++;AssertOutput ao = (AssertOutput) peekObject();
__CLOVER_368_0.S[8187]++;ao.setExpectedSubstring(content);
} finally { }}
private void endAssertRegexp()
{try { __CLOVER_368_0.M[1862]++;
__CLOVER_368_0.S[8188]++;String content = peekContent();
__CLOVER_368_0.S[8189]++;AssertRegexp ar = (AssertRegexp) peekObject();
__CLOVER_368_0.S[8190]++;ar.setRegexp(content);
} finally { }}
private void endMatch()
{try { __CLOVER_368_0.M[1863]++;
__CLOVER_368_0.S[8191]++;String content = peekContent();
__CLOVER_368_0.S[8192]++;RegexpMatch m = (RegexpMatch) peekObject();
__CLOVER_368_0.S[8193]++;m.setExpectedString(content);
} finally { }}
protected String peekContent()
{try { __CLOVER_368_0.M[1864]++;
__CLOVER_368_0.S[8194]++;String rawContent = super.peekContent();
__CLOVER_368_0.S[8195]++;if ((((rawContent == null) && (++__CLOVER_368_0.CT[1394] != 0)) || (++__CLOVER_368_0.CF[1394] == 0))){
__CLOVER_368_0.S[8196]++;return null;}
__CLOVER_368_0.S[8197]++;return rawContent.trim();
} finally { }}
private void enterParameter()
{try { __CLOVER_368_0.M[1865]++;
__CLOVER_368_0.S[8198]++;validateAttributes();
__CLOVER_368_0.S[8199]++;String name = getAttribute("name");
__CLOVER_368_0.S[8200]++;String value = getAttribute("value");
__CLOVER_368_0.S[8201]++;RequestDescriptor rd = (RequestDescriptor) peekObject();
__CLOVER_368_0.S[8202]++;rd.addParameter(name, value);
__CLOVER_368_0.S[8203]++;push(_elementName, null, STATE_NO_CONTENT);
} finally { }}
private void enterInitParameter()
{try { __CLOVER_368_0.M[1866]++;
__CLOVER_368_0.S[8204]++;validateAttributes();
__CLOVER_368_0.S[8205]++;String name = getAttribute("name");
__CLOVER_368_0.S[8206]++;String value = getAttribute("value");
__CLOVER_368_0.S[8207]++;ServletDescriptor sd = (ServletDescriptor) peekObject();
__CLOVER_368_0.S[8208]++;sd.addInitParameter(name, value);
__CLOVER_368_0.S[8209]++;push(_elementName, null, STATE_NO_CONTENT);
} finally { }}
private void enterTestScript()
{try { __CLOVER_368_0.M[1867]++;
__CLOVER_368_0.S[8210]++;validateAttributes();
__CLOVER_368_0.S[8211]++;String contextName = getAttribute("context");
__CLOVER_368_0.S[8212]++;String directory = getAttribute("directory");
__CLOVER_368_0.S[8213]++;ScriptDescriptor sd = new ScriptDescriptor();
__CLOVER_368_0.S[8214]++;sd.setContextName(contextName);
__CLOVER_368_0.S[8215]++;sd.setRootDirectory(directory);
__CLOVER_368_0.S[8216]++;_scriptDescriptor = sd;
__CLOVER_368_0.S[8217]++;push(_elementName, sd, STATE_TEST_SCRIPT);
} finally { }}
private void enterServlet()
{try { __CLOVER_368_0.M[1868]++;
__CLOVER_368_0.S[8218]++;validateAttributes();
__CLOVER_368_0.S[8219]++;String name = getAttribute("name");
__CLOVER_368_0.S[8220]++;String className = getAttribute("class", "org.apache.tapestry.ApplicationServlet");
__CLOVER_368_0.S[8221]++;ServletDescriptor sd = new ServletDescriptor();
__CLOVER_368_0.S[8222]++;sd.setName(name);
__CLOVER_368_0.S[8223]++;sd.setClassName(className);
// Can't wait for push() to do this, because of checks inside
// addServletDescriptor().
__CLOVER_368_0.S[8224]++;sd.setLocation(getLocation());
__CLOVER_368_0.S[8225]++;ScriptDescriptor scriptDescriptor = (ScriptDescriptor) peekObject();
__CLOVER_368_0.S[8226]++;scriptDescriptor.addServletDescriptor(sd);
__CLOVER_368_0.S[8227]++;push(_elementName, sd, STATE_SERVLET);
} finally { }}
private String getAttribute(String name)
{try { __CLOVER_368_0.M[1869]++;
__CLOVER_368_0.S[8228]++;return (String) _attributes.get(name);
} finally { }}
private String getAttribute(String name, String defaultValue)
{try { __CLOVER_368_0.M[1870]++;
__CLOVER_368_0.S[8229]++;if ((((!_attributes.containsKey(name)) && (++__CLOVER_368_0.CT[1395] != 0)) || (++__CLOVER_368_0.CF[1395] == 0))){
__CLOVER_368_0.S[8230]++;return defaultValue;}
__CLOVER_368_0.S[8231]++;return (String) _attributes.get(name);
} finally { }}
private void validateAttributes()
{try { __CLOVER_368_0.M[1871]++;
__CLOVER_368_0.S[8232]++;Iterator i = _attributes.keySet().iterator();
__CLOVER_368_0.S[8233]++;ElementParseInfo epi = getElementParseInfo(_elementName);
// First, check that each attribute is in the set of expected attributes.
__CLOVER_368_0.S[8234]++;while ((((i.hasNext()) && (++__CLOVER_368_0.CT[1396] != 0)) || (++__CLOVER_368_0.CF[1396] == 0))){
{
__CLOVER_368_0.S[8235]++;String name = (String) i.next();
__CLOVER_368_0.S[8236]++;if ((((!epi.isKnown(name)) && (++__CLOVER_368_0.CT[1397] != 0)) || (++__CLOVER_368_0.CF[1397] == 0))){
__CLOVER_368_0.S[8237]++;throw new DocumentParseException(
ScriptMessages.unexpectedAttributeInElement(name, _elementName),
getLocation(),
null);}
}}
// Now check that all required attributes have been specified.
__CLOVER_368_0.S[8238]++;i = epi.getRequiredNames();
__CLOVER_368_0.S[8239]++;while ((((i.hasNext()) && (++__CLOVER_368_0.CT[1398] != 0)) || (++__CLOVER_368_0.CF[1398] == 0))){
{
__CLOVER_368_0.S[8240]++;String name = (String) i.next();
__CLOVER_368_0.S[8241]++;if ((((!_attributes.containsKey(name)) && (++__CLOVER_368_0.CT[1399] != 0)) || (++__CLOVER_368_0.CF[1399] == 0))){
__CLOVER_368_0.S[8242]++;throw new DocumentParseException(
ScriptMessages.missingRequiredAttribute(name, _elementName),
getLocation(),
null);}
}}
} finally { }}
private ElementParseInfo getElementParseInfo(String elementName)
{try { __CLOVER_368_0.M[1872]++;
__CLOVER_368_0.S[8243]++;ElementParseInfo result = (ElementParseInfo) _elementParseInfo.get(elementName);
__CLOVER_368_0.S[8244]++;if ((((result == null) && (++__CLOVER_368_0.CT[1400] != 0)) || (++__CLOVER_368_0.CF[1400] == 0))){
{
__CLOVER_368_0.S[8245]++;result = new ElementParseInfo();
__CLOVER_368_0.S[8246]++;_elementParseInfo.put(elementName, result);
}}
__CLOVER_368_0.S[8247]++;return result;
} finally { }}
private int getIntAttribute(String name, int defaultValue)
{try { __CLOVER_368_0.M[1873]++;
__CLOVER_368_0.S[8248]++;String attributeValue = getAttribute(name);
__CLOVER_368_0.S[8249]++;if ((((attributeValue == null) && (++__CLOVER_368_0.CT[1401] != 0)) || (++__CLOVER_368_0.CF[1401] == 0))){
__CLOVER_368_0.S[8250]++;return defaultValue;}
__CLOVER_368_0.S[8251]++;try
{
__CLOVER_368_0.S[8252]++;return Integer.parseInt(attributeValue);
}
catch (NumberFormatException ex)
{
__CLOVER_368_0.S[8253]++;throw new ApplicationRuntimeException(
ScriptMessages.invalidIntAttribute(
name,
_elementName,
getLocation(),
attributeValue),
getLocation(),
ex);
}
} finally { }}
}