1 18 19 package org.apache.jmeter.assertions; 20 21 import java.io.Serializable ; 22 import java.io.StringReader ; 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 36 public class XMLAssertion 37 extends AbstractTestElement 38 implements Serializable , Assertion 39 { 40 private static final Logger log = LoggingManager.getLoggerForClass(); 41 42 private static SAXBuilder builder = null; 44 45 51 public AssertionResult getResult(SampleResult response) 52 { 53 AssertionResult result = new AssertionResult(); 55 if(response.getResponseData() == null) 56 { 57 return setResultForNull(result); 58 } 59 result.setFailure(false); 60 61 String resultData = 63 new String (getResultBody(response.getResponseData())); 64 65 if (builder == null) 67 { 68 try 69 { 70 builder = new SAXBuilder(); 72 } 73 catch (Exception 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 result; 82 } 83 } 84 85 try 86 { 87 builder.build(new StringReader (resultData)); 88 } 89 catch (Exception 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 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 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 |