KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > assertions > XMLAssertion


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/assertions/XMLAssertion.java,v 1.4.2.1 2004/10/13 00:30:31 sebb Exp $
2
/*
3  * Copyright 2003-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.assertions;
20
21 import java.io.Serializable JavaDoc;
22 import java.io.StringReader JavaDoc;
23
24 import org.apache.jmeter.samplers.SampleResult;
25 import org.apache.jmeter.testelement.AbstractTestElement;
26 import org.apache.jorphan.logging.LoggingManager;
27 import org.apache.log.Logger;
28 import org.jdom.input.SAXBuilder;
29
30 /**
31  * Checks if the result is a well-formed XML content.
32  *
33  * @author <a HREF="mailto:gottfried@szing.at">Gottfried Szing</a>
34  * @version $Revision: 1.4.2.1 $, $Date: 2004/10/13 00:30:31 $
35  */

36 public class XMLAssertion
37     extends AbstractTestElement
38     implements Serializable JavaDoc, Assertion
39 {
40     private static final Logger log = LoggingManager.getLoggerForClass();
41     
42     // one builder for all requests
43
private static SAXBuilder builder = null;
44
45     /**
46      * Returns the result of the Assertion. Here it checks wether the
47      * Sample took to long to be considered successful. If so an AssertionResult
48      * containing a FailureMessage will be returned. Otherwise the returned
49      * AssertionResult will reflect the success of the Sample.
50      */

51     public AssertionResult getResult(SampleResult response)
52     {
53         // no error as default
54
AssertionResult result = new AssertionResult();
55         if(response.getResponseData() == null)
56              {
57                  return setResultForNull(result);
58              }
59         result.setFailure(false);
60
61         // the result data
62
String JavaDoc resultData =
63             new String JavaDoc(getResultBody(response.getResponseData()));
64
65         // create parser like (!) a singleton
66
if (builder == null)
67         {
68             try
69             {
70                 // This builds a document of whatever's in the given resource
71
builder = new SAXBuilder();
72             }
73             catch (Exception JavaDoc e)
74             {
75                 log.error("Unable to instantiate DOM Builder", e);
76
77                 result.setFailure(true);
78                 result.setFailureMessage("Unable to instantiate DOM Builder");
79
80                 // return with an error
81
return result;
82             }
83         }
84
85         try
86         {
87             builder.build(new StringReader JavaDoc(resultData));
88         }
89         catch (Exception JavaDoc e)
90         {
91             log.debug("Cannot parse result content", e);
92
93             result.setFailure(true);
94             result.setFailureMessage(e.getMessage());
95         }
96
97         return result;
98     }
99     
100     protected AssertionResult setResultForNull(AssertionResult result)
101         {
102             result.setError(false);
103             result.setFailure(true);
104             result.setFailureMessage("Response was null");
105             return result;
106         }
107
108     /**
109      * Return the body of the http return.
110      */

111     private byte[] getResultBody(byte[] resultData)
112     {
113         for (int i = 0; i < (resultData.length - 1); i++)
114         {
115             if (resultData[i] == '\n' && resultData[i + 1] == '\n')
116             {
117                 return getByteArraySlice(
118                     resultData,
119                     (i + 2),
120                     resultData.length - 1);
121             }
122         }
123         return resultData;
124     }
125
126     /**
127      * Return a slice of a byte array
128      */

129     private byte[] getByteArraySlice(byte[] array, int begin, int end)
130     {
131         byte[] slice = new byte[(end - begin + 1)];
132         int count = 0;
133         for (int i = begin; i <= end; i++)
134         {
135             slice[count] = array[i];
136             count++;
137         }
138
139         return slice;
140     }
141 }
142
Popular Tags