You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jmeter-dev@jakarta.apache.org by se...@apache.org on 2005/04/09 19:19:48 UTC
cvs commit: jakarta-jmeter/src/components/org/apache/jmeter/assertions/gui XMLSchemaAssertionGUI.java
sebb 2005/04/09 10:19:48
Added: src/components/org/apache/jmeter/assertions
XMLSchemaAssertion.java
src/components/org/apache/jmeter/assertions/gui
XMLSchemaAssertionGUI.java
Log:
Bug 34383 - add XMLSchemaAssertion
Revision Changes Path
1.1 jakarta-jmeter/src/components/org/apache/jmeter/assertions/XMLSchemaAssertion.java
Index: XMLSchemaAssertion.java
===================================================================
/*
* Copyright 2005 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.jmeter.assertions;
import java.io.IOException;
import java.io.Serializable;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.testelement.AbstractTestElement;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.jorphan.util.JOrphanUtils;
import org.apache.log.Logger;
//import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
// See Bug 34383
/**
* XMLSchemaAssertion.java
* Validate response against an XML Schema
* author <a href="mailto:d.maung@mdl.com">Dave Maung</a>
*
*/
public class XMLSchemaAssertion extends AbstractTestElement implements
Serializable, Assertion
{
public static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
public static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
public static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
private static final Logger log = LoggingManager.getLoggerForClass();
public static final String XSD_FILENAME_KEY = "xmlschema_assertion_filename";
//private StringBuffer failureMessage = new StringBuffer();
/**
* getResult
*
*/
public AssertionResult getResult(SampleResult response)
{
AssertionResult result = new AssertionResult();
if (response.getResponseData() == null) {
return result.setResultForNull();
}
result.setFailure(false);
String resultData = new String(
getResultBody(response.getResponseData()));
String xsdFileName = getXsdFileName();
if (log.isDebugEnabled()){
log.debug("xmlString: "+resultData);
log.debug("xsdFileName: "+xsdFileName);
}
if (xsdFileName == null || xsdFileName.length() == 0) {
result.setResultForFailure("FileName is required");
} else {
setSchemaResult(result, resultData, xsdFileName);
}
return result;
}
/*
* TODO move to SampleResult class?
* Return the body of the http return.
*/
private byte[] getResultBody(byte[] resultData)
{
for (int i = 0; i < (resultData.length - 1); i++) {
if (resultData[i] == '\n' && resultData[i + 1] == '\n') {
return JOrphanUtils.getByteArraySlice(resultData, (i + 2),
resultData.length - 1);
}
}
return resultData;
}
public void setXsdFileName(String xmlSchemaFileName)
throws IllegalArgumentException
{
setProperty(XSD_FILENAME_KEY, xmlSchemaFileName);
}
public String getXsdFileName()
{
return getPropertyAsString(XSD_FILENAME_KEY);
}
/**
* set Schema result
* @param result
* @param xmlStr
* @param xsdFileName
*/
private void setSchemaResult(AssertionResult result, String xmlStr,
String xsdFileName)
{
try
{
//boolean toReturn = true;
//Document doc = null;
DocumentBuilderFactory parserFactory = DocumentBuilderFactory
.newInstance();
parserFactory.setValidating(true);
parserFactory.setNamespaceAware(true);
parserFactory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
parserFactory.setAttribute(JAXP_SCHEMA_SOURCE, xsdFileName);
// create a parser:
DocumentBuilder parser = parserFactory.newDocumentBuilder();
parser.setErrorHandler(new SAXErrorHandler(result));
//doc =
parser.parse(new InputSource(new StringReader(xmlStr)));
//if everything went fine then xml schema validation is valid
} catch (SAXException e) {
result.setResultForFailure(e.getMessage());
} catch (IOException e) {
result.setResultForFailure(e.getMessage());
} catch (ParserConfigurationException e) {
result.setResultForFailure(e.getMessage());
}
}
/**
* SAXErrorHandler class
*/
private class SAXErrorHandler implements ErrorHandler
{
private AssertionResult result;
public SAXErrorHandler(AssertionResult result)
{
this.result = result;
}
public void error(SAXParseException exception)
throws SAXParseException
{
throw exception;
}
public void fatalError(SAXParseException exception)
throws SAXParseException
{
throw exception;
}
public void warning(SAXParseException exception)
throws SAXParseException
{
throw exception;
}
}
// TODO add some test cases
}
1.1 jakarta-jmeter/src/components/org/apache/jmeter/assertions/gui/XMLSchemaAssertionGUI.java
Index: XMLSchemaAssertionGUI.java
===================================================================
/*
* Copyright 2005 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.jmeter.assertions.gui;
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
//import javax.swing.event.ChangeEvent;
import org.apache.jmeter.assertions.XMLSchemaAssertion;
import org.apache.jmeter.gui.util.HorizontalPanel;
import org.apache.jmeter.gui.util.VerticalPanel;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
// See Bug 34383
/**
* XMLSchemaAssertionGUI.java
* author <a href="mailto:d.maung@mdl.com">Dave Maung</a>
*
*/
public class XMLSchemaAssertionGUI extends AbstractAssertionGui
{
//class attributes
transient private static Logger log = LoggingManager.getLoggerForClass();
private JTextField xmlSchema;
/**
* The constructor.
*/
public XMLSchemaAssertionGUI()
{
init();
}
/**
* Returns the label to be shown within the JTree-Component.
*/
public String getLabelResource()
{
return "xmlschema_assertion_title";
}
/**
* create Test Element
*/
public TestElement createTestElement()
{
log.debug("XMLSchemaAssertionGui.createTestElement() called");
XMLSchemaAssertion el = new XMLSchemaAssertion();
modifyTestElement(el);
return el;
}
/**
* Modifies a given TestElement to mirror the data in the gui components.
*
* @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
*/
public void modifyTestElement(TestElement inElement) {
log.debug("XMLSchemaAssertionGui.modifyTestElement() called");
configureTestElement(inElement);
((XMLSchemaAssertion) inElement).setXsdFileName(xmlSchema.getText());
}
/**
* Configures the GUI from the associated test element.
*
* @param el - the test element (should be XMLSchemaAssertion)
*/
public void configure(TestElement el)
{
super.configure(el);
XMLSchemaAssertion assertion = (XMLSchemaAssertion) el;
xmlSchema.setText(assertion.getXsdFileName());
}
/**
* Inits the GUI.
*/
private void init()
{
setLayout(new BorderLayout(0, 10));
setBorder(makeBorder());
add(makeTitlePanel(), BorderLayout.NORTH);
JPanel mainPanel = new JPanel(new BorderLayout());
// USER_INPUT
VerticalPanel assertionPanel = new VerticalPanel();
assertionPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "XML Schema"));
//doctype
HorizontalPanel xmlSchemaPanel = new HorizontalPanel();
xmlSchemaPanel.add(
new JLabel(JMeterUtils.getResString("xmlschema_assertion_label")));
xmlSchema = new JTextField(26);
xmlSchemaPanel.add(xmlSchema);
assertionPanel.add(xmlSchemaPanel);
mainPanel.add(assertionPanel, BorderLayout.NORTH);
add(mainPanel, BorderLayout.CENTER);
}
// public void stateChanged(ChangeEvent e) {
// log.debug("XMLSchemaAssertionGui.stateChanged() called");
// }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org