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 bu...@apache.org on 2005/04/09 07:16:34 UTC
DO NOT REPLY [Bug 34383] New: -
Request to add XML Schema Assertion
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=34383>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.
http://issues.apache.org/bugzilla/show_bug.cgi?id=34383
Summary: Request to add XML Schema Assertion
Product: JMeter
Version: unspecified
Platform: All
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P4
Component: HTTP
AssignedTo: jmeter-dev@jakarta.apache.org
ReportedBy: dmaung1@yahoo.com
I would like to request to add XML Schema assertion. Below is my code that I
have written. Please review.
Dave
/************************
#message.properties
*/
xmlschema_assertion_title = XML Schema Assertion
xmlschema_assertion_label = File Name:
//********************************************************************
//XMLSchemaAssertionGUI.java
/*
* Copyright 2001-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.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;
/**
* 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()
{
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 associated test element.
* @param el
*/
public void configure(TestElement el)
{
super.configure(el);
}
/**
* 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");
}
}
//**************************************************************************
//XMLSchemaAssertion.java
/*
* Copyright 2003-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.jmeter.assertions;
import java.io.Serializable;
import java.io.StringReader;
import javax.xml.parsers.*;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.testelement.AbstractTestElement;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;
/**
* XMLSchemaAssertion.java
* Validate 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 setResultForNull(result);
}
result.setFailure(false);
String resultData = new String(
getResultBody(response.getResponseData()));
String xsdFileName = getXsdFileName();
setSchemaResult(result, resultData, xsdFileName);
return result;
}
protected AssertionResult setResultForNull(AssertionResult result)
{
result.setError(false);
result.setFailure(true);
result.setFailureMessage("Response was null");
return result;
}
/**
* 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 getByteArraySlice(resultData, (i + 2),
resultData.length - 1);
}
}
return resultData;
}
/**
* Return a slice of a byte array
*/
private byte[] getByteArraySlice(byte[] array, int begin, int end)
{
byte[] slice = new byte[(end - begin + 1)];
int count = 0;
for (int i = begin; i <= end; i++)
{
slice[count] = array[i];
count++;
}
return slice;
}
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
*/
public 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);
if (xsdFileName != null && xsdFileName.length() > 0) {
parserFactory.setAttribute(JAXP_SCHEMA_SOURCE, xsdFileName);
} else
{
throw new Exception(xsdFileName + "is invalid");
}
// 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 (Exception e)
{
result.setError(false);
result.setFailureMessage(e.getMessage());
result.setFailure(true);
}
}
/**
* 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;
}
}
}
--
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.
---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org