001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.assertions;
020:
021: import java.io.IOException;
022: import java.io.Serializable;
023: import java.io.StringReader;
024:
025: import org.apache.jmeter.samplers.SampleResult;
026: import org.apache.jmeter.testelement.AbstractTestElement;
027: import org.apache.jorphan.logging.LoggingManager;
028: import org.apache.log.Logger;
029: import org.jdom.JDOMException;
030: import org.jdom.input.SAXBuilder;
031:
032: /**
033: * Checks if the result is a well-formed XML content using jdom
034: *
035: * @author <a href="mailto:gottfried@szing.at">Gottfried Szing</a>
036: */
037: public class XMLAssertion extends AbstractTestElement implements
038: Serializable, Assertion {
039: private static final Logger log = LoggingManager
040: .getLoggerForClass();
041:
042: private static final char NEW_LINE = '\n'; // $NON-NLS-1$
043:
044: // one builder for all requests in a thread
045: private static ThreadLocal myBuilder = new ThreadLocal() {
046: protected Object initialValue() {
047: return new SAXBuilder();
048: }
049: };
050:
051: /**
052: * Returns the result of the Assertion. Here it checks wether the Sample
053: * took to long to be considered successful. If so an AssertionResult
054: * containing a FailureMessage will be returned. Otherwise the returned
055: * AssertionResult will reflect the success of the Sample.
056: */
057: public AssertionResult getResult(SampleResult response) {
058: // no error as default
059: AssertionResult result = new AssertionResult(getName());
060: byte[] responseData = response.getResponseData();
061: if (responseData.length == 0) {
062: return result.setResultForNull();
063: }
064: result.setFailure(false);
065:
066: // the result data
067: String resultData = new String(getResultBody(responseData));
068:
069: SAXBuilder builder = (SAXBuilder) myBuilder.get();
070:
071: try {
072: builder.build(new StringReader(resultData));
073: } catch (JDOMException e) {
074: log.debug("Cannot parse result content", e); // may well happen
075: result.setFailure(true);
076: result.setFailureMessage(e.getMessage());
077: } catch (IOException e) {
078: log.error("Cannot read result content", e); // should never happen
079: result.setError(true);
080: result.setFailureMessage(e.getMessage());
081: }
082:
083: return result;
084: }
085:
086: /**
087: * Return the body of the http return.
088: */
089: private byte[] getResultBody(byte[] resultData) {
090: for (int i = 0; i < (resultData.length - 1); i++) {
091: if (resultData[i] == NEW_LINE
092: && resultData[i + 1] == NEW_LINE) {
093: return getByteArraySlice(resultData, (i + 2),
094: resultData.length - 1);
095: }
096: }
097: return resultData;
098: }
099:
100: /**
101: * Return a slice of a byte array
102: */
103: private byte[] getByteArraySlice(byte[] array, int begin, int end) {
104: byte[] slice = new byte[(end - begin + 1)];
105: int count = 0;
106: for (int i = begin; i <= end; i++) {
107: slice[count] = array[i];
108: count++;
109: }
110:
111: return slice;
112: }
113: }
|