KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/assertions/SizeAssertion.java,v 1.14 2004/02/13 01:27:26 sebb Exp $
2
/*
3  * Copyright 2001-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.text.MessageFormat JavaDoc;
23
24 import org.apache.jmeter.samplers.SampleResult;
25 import org.apache.jmeter.testelement.AbstractTestElement;
26 import org.apache.jmeter.testelement.property.IntegerProperty;
27 import org.apache.jmeter.testelement.property.LongProperty;
28 import org.apache.jmeter.util.JMeterUtils;
29
30 /**
31  * Checks if the results of a Sample matches a particular size.
32  *
33  * @author <a HREF="mailto:wolfram.rittmeyer@web.de">Wolfram Rittmeyer</a>
34  * @version $Revision: 1.14 $, $Date: 2004/02/13 01:27:26 $
35  */

36 public class SizeAssertion
37     extends AbstractTestElement
38     implements Serializable JavaDoc, Assertion
39 {
40
41     private String JavaDoc comparatorErrorMessage = "ERROR!";
42     //* Static int to signify the type of logical comparitor to assert
43
public final static int EQUAL = 1;
44     public final static int NOTEQUAL = 2;
45     public final static int GREATERTHAN = 3;
46     public final static int LESSTHAN = 4;
47     public final static int GREATERTHANEQUAL = 5;
48     public final static int LESSTHANEQUAL = 6;
49     /** Key for storing assertion-informations in the jmx-file. */
50     private static final String JavaDoc SIZE_KEY = "SizeAssertion.size";
51     private static final String JavaDoc OPERATOR_KEY = "SizeAssertion.operator";
52     byte[] resultData;
53
54     /**
55      * Returns the result of the Assertion. Here it checks wether the
56      * Sample took to long to be considered successful. If so an AssertionResult
57      * containing a FailureMessage will be returned. Otherwise the returned
58      * AssertionResult will reflect the success of the Sample.
59      */

60     public AssertionResult getResult(SampleResult response)
61     {
62         AssertionResult result = new AssertionResult();
63         result.setFailure(false);
64         if (response.getResponseData() == null)
65         {
66             return setResultForNull(result);
67         }
68         // is the Sample the correct size?
69
resultData = response.getResponseData();
70         long resultSize = resultData.length;
71         if ((!(compareSize(resultSize)) && (getAllowedSize() > 0)))
72         {
73             result.setFailure(true);
74             Object JavaDoc[] arguments =
75                 {
76                     new Long JavaDoc(resultSize),
77                     comparatorErrorMessage,
78                     new Long JavaDoc(getAllowedSize())};
79             String JavaDoc message =
80                 MessageFormat.format(
81                     JMeterUtils.getResString("size_assertion_failure"),
82                     arguments);
83             result.setFailureMessage(message);
84         }
85         return result;
86     }
87
88     /**
89      * Returns the size in bytes to be asserted. A duration of 0 indicates this
90      * assertion is to be ignored.
91      */

92     public long getAllowedSize()
93     {
94         return getPropertyAsLong(SIZE_KEY);
95     }
96
97     /******
98      * set the Operator
99      ******/

100     public void setCompOper(int operator)
101     {
102         setProperty(new IntegerProperty(OPERATOR_KEY, operator));
103
104     }
105
106     protected AssertionResult setResultForNull(AssertionResult result)
107     {
108         result.setError(false);
109         result.setFailure(true);
110         result.setFailureMessage("Response was null");
111         return result;
112     }
113
114     /**
115      * Returns the operator to be asserted. EQUAL = 1, NOTEQUAL = 2
116      * GREATERTHAN = 3,LESSTHAN = 4,GREATERTHANEQUAL = 5,LESSTHANEQUAL = 6
117      */

118
119     public int getCompOper()
120     {
121         return getPropertyAsInt(OPERATOR_KEY);
122     }
123
124     /**
125      * Set the size that shall be asserted.
126      *
127      * @param size - a number of bytes. Is not allowed to be negative. Use
128      * Long.MAX_VALUE to indicate illegal or empty inputs.
129      * This will result in not checking the assertion.
130      *
131      * @throws IllegalArgumentException If <code>size</code> is negative.
132      */

133     public void setAllowedSize(long size) throws IllegalArgumentException JavaDoc
134     {
135         if (size < 0L)
136         {
137             throw new IllegalArgumentException JavaDoc(
138                 JMeterUtils.getResString("argument_must_not_be_negative"));
139         }
140         if (size == Long.MAX_VALUE)
141         {
142             setProperty(new LongProperty(SIZE_KEY, 0));
143         }
144         else
145         {
146             setProperty(new LongProperty(SIZE_KEY, size));
147         }
148     }
149
150     /**
151      * Compares the the size of a return result to the set allowed size
152      *using a logical comparator set in setLogicalComparator().
153      *
154      * Possible values are:
155      * equal, not equal,
156      * greater than, less than,
157      * greater than eqaul, less than equal, .
158      *
159      */

160     private boolean compareSize(long resultSize)
161     {
162         boolean result = false;
163         int comp = getCompOper();
164         switch (comp)
165         {
166             case EQUAL :
167                 result = (resultSize == getAllowedSize());
168                 comparatorErrorMessage =
169                     JMeterUtils.getResString(
170                         "size_assertion_comparator_error_equal");
171                 break;
172             case NOTEQUAL :
173                 result = (resultSize != getAllowedSize());
174                 comparatorErrorMessage =
175                     JMeterUtils.getResString(
176                         "size_assertion_comparator_error_notequal");
177                 break;
178             case GREATERTHAN :
179                 result = (resultSize > getAllowedSize());
180                 comparatorErrorMessage =
181                     JMeterUtils.getResString(
182                         "size_assertion_comparator_error_greater");
183                 break;
184             case LESSTHAN :
185                 result = (resultSize < getAllowedSize());
186                 comparatorErrorMessage =
187                     JMeterUtils.getResString(
188                         "size_assertion_comparator_error_less");
189                 break;
190             case GREATERTHANEQUAL :
191                 result = (resultSize >= getAllowedSize());
192                 comparatorErrorMessage =
193                     JMeterUtils.getResString(
194                         "size_assertion_comparator_error_greaterequal");
195                 break;
196             case LESSTHANEQUAL :
197                 result = (resultSize <= getAllowedSize());
198                 comparatorErrorMessage =
199                     JMeterUtils.getResString(
200                         "size_assertion_comparator_error_lessequal");
201                 break;
202         }
203         return result;
204     }
205 }
206
Popular Tags