KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/assertions/MD5HexAssertion.java,v 1.2.2.1 2004/10/13 00:30:31 sebb Exp $
2
/*
3  * Copyright 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 /**
20  * MD5HexAssertion class creates an MD5 checksum from the response <br/>
21  * and matches it with the MD5 hex provided.
22  * The assertion will fail when the expected hex is different from the <br/>
23  * one calculated from the response OR when the expected hex is left empty.
24  *
25  * @author <a HREF="mailto:jh@domek.be">Jorg Heymans</a>
26  * @version $Revision: 1.2.2.1 $ last updated $Date: 2004/10/13 00:30:31 $
27  */

28 package org.apache.jmeter.assertions;
29
30 import java.io.Serializable JavaDoc;
31 import java.security.MessageDigest JavaDoc;
32 import java.security.NoSuchAlgorithmException JavaDoc;
33 import java.text.MessageFormat JavaDoc;
34
35 import junit.framework.TestCase;
36
37 import org.apache.jmeter.samplers.SampleResult;
38 import org.apache.jmeter.testelement.AbstractTestElement;
39 import org.apache.jmeter.testelement.property.StringProperty;
40 import org.apache.jmeter.util.JMeterUtils;
41 import org.apache.jorphan.logging.LoggingManager;
42 import org.apache.log.Logger;
43
44 public class MD5HexAssertion
45     extends AbstractTestElement
46     implements Serializable JavaDoc, Assertion {
47
48     private static final Logger log = LoggingManager.getLoggerForClass();
49     
50     /** Key for storing assertion-informations in the jmx-file. */
51     private static final String JavaDoc MD5HEX_KEY = "MD5HexAssertion.size";
52
53     /*
54      * @param response
55      * @return
56      */

57     public AssertionResult getResult(SampleResult response) {
58
59         AssertionResult result = new AssertionResult();
60         result.setFailure(false);
61         byte[] resultData = response.getResponseData();
62
63         if (resultData == null) {
64             result.setError(false);
65             result.setFailure(true);
66             result.setFailureMessage("Response was null");
67             return result;
68         }
69
70         //no point in checking if we don't have anything to compare against
71
if (getAllowedMD5Hex().equals("")) {
72             result.setError(false);
73             result.setFailure(true);
74             result.setFailureMessage("MD5Hex to test against is empty");
75             return result;
76         }
77         
78         String JavaDoc md5Result=baMD5Hex(resultData);
79
80         //String md5Result = DigestUtils.md5Hex(resultData);
81

82         if (!md5Result.equalsIgnoreCase(getAllowedMD5Hex())) {
83             result.setFailure(true);
84
85             Object JavaDoc[] arguments = { md5Result, getAllowedMD5Hex()};
86             String JavaDoc message =
87                 MessageFormat.format(
88                     JMeterUtils.getResString("md5hex_assertion_failure"),
89                     arguments);
90             result.setFailureMessage(message);
91
92         }
93
94         return result;
95     }
96
97     public void setAllowedMD5Hex(String JavaDoc hex) {
98         setProperty(new StringProperty(MD5HexAssertion.MD5HEX_KEY, hex));
99     }
100
101     public String JavaDoc getAllowedMD5Hex() {
102         return getPropertyAsString(MD5HexAssertion.MD5HEX_KEY);
103     }
104     
105     private static String JavaDoc baToHex(byte ba [])
106     {
107         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(32);
108         for (int i = 0; i<ba.length; i++ )
109             {
110                 int j = ba[i]&0xff;
111                 if (j < 16) sb.append("0");
112                 sb.append(Integer.toHexString(j));
113             }
114         return sb.toString();
115     }
116     
117     private static String JavaDoc baMD5Hex(byte ba[])
118     {
119         byte [] md5Result={};
120         
121         try
122         {
123             MessageDigest JavaDoc md;
124             md = MessageDigest.getInstance("MD5");
125             md5Result = md.digest(ba);
126         }
127         catch (NoSuchAlgorithmException JavaDoc e)
128         {
129             log.error("",e);
130         }
131         return baToHex(md5Result);
132     }
133     
134     public static class Test extends TestCase
135     {
136         public void testHex() throws Exception JavaDoc
137         {
138             assertEquals("00010203",baToHex(new byte[]{0,1,2,3}));
139             assertEquals("03020100",baToHex(new byte[]{3,2,1,0}));
140             assertEquals("0f807fff",baToHex(new byte[]{0xF,-128,127,-1}));
141         }
142         public void testMD5() throws Exception JavaDoc
143         {
144             assertEquals("D41D8CD98F00B204E9800998ECF8427E",baMD5Hex(new byte[]{}).toUpperCase());
145         }
146     }
147 }
148
Popular Tags