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.BufferedInputStream;
022: import java.io.ByteArrayOutputStream;
023: import java.io.FileInputStream;
024: import java.io.IOException;
025:
026: import org.apache.jmeter.junit.JMeterTestCase;
027: import org.apache.jmeter.samplers.SampleResult;
028: import org.apache.jmeter.threads.JMeterContext;
029: import org.apache.jmeter.threads.JMeterContextService;
030: import org.apache.jmeter.threads.JMeterVariables;
031:
032: public class XPathAssertionTest extends JMeterTestCase {
033:
034: private XPathAssertion assertion;
035:
036: private SampleResult result;
037:
038: private JMeterVariables vars;
039:
040: private JMeterContext jmctx;
041:
042: public XPathAssertionTest(String arg0) {
043: super (arg0);
044: }
045:
046: protected void setUp() throws Exception {
047: super .setUp();
048: jmctx = JMeterContextService.getContext();
049: assertion = new XPathAssertion();
050: assertion.setThreadContext(jmctx);// This would be done by the run
051: // command
052: result = new SampleResult();
053: result
054: .setResponseData(readFile("testfiles/XPathAssertionTest.xml"));
055: vars = new JMeterVariables();
056: jmctx.setVariables(vars);
057: //jmctx.setPreviousResult(result);
058: //testLog.setPriority(org.apache.log.Priority.DEBUG);
059: }
060:
061: private ByteArrayOutputStream readBA(String name)
062: throws IOException {
063: BufferedInputStream bis = new BufferedInputStream(
064: new FileInputStream(findTestFile(name)));
065: ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
066: int len = 0;
067: byte[] data = new byte[512];
068: while ((len = bis.read(data)) >= 0) {
069: baos.write(data, 0, len);
070: }
071: bis.close();
072: return baos;
073: }
074:
075: private byte[] readFile(String name) throws IOException {
076: return readBA(name).toByteArray();
077: }
078:
079: public void testAssertionOK() throws Exception {
080: assertion.setXPathString("/");
081: AssertionResult res = assertion.getResult(result);
082: testLog.debug("isError() " + res.isError() + " isFailure() "
083: + res.isFailure());
084: testLog.debug("failure message: " + res.getFailureMessage());
085: assertFalse("Should not be an error", res.isError());
086: assertFalse("Should not be a failure", res.isFailure());
087: }
088:
089: public void testAssertionFail() throws Exception {
090: assertion.setXPathString("//x");
091: AssertionResult res = assertion.getResult(result);
092: testLog.debug("isError() " + res.isError() + " isFailure() "
093: + res.isFailure());
094: testLog.debug("failure message: " + res.getFailureMessage());
095: assertFalse("Should not be an error", res.isError());
096: assertTrue("Should be a failure", res.isFailure());
097: }
098:
099: public void testAssertionPath1() throws Exception {
100: assertion.setXPathString("//*[code=1]");
101: AssertionResult res = assertion.getResult(result);
102: testLog.debug("isError() " + res.isError() + " isFailure() "
103: + res.isFailure());
104: testLog.debug("failure message: " + res.getFailureMessage());
105: assertFalse("Should not be an error", res.isError());
106: assertFalse("Should not be a failure", res.isFailure());
107: }
108:
109: public void testAssertionPath2() throws Exception {
110: assertion.setXPathString("//*[code=2]"); // Not present
111: AssertionResult res = assertion.getResult(result);
112: testLog.debug("isError() " + res.isError() + " isFailure() "
113: + res.isFailure());
114: testLog.debug("failure message: " + res.getFailureMessage());
115: assertFalse("Should not be an error", res.isError());
116: assertTrue("Should be a failure", res.isFailure());
117: }
118:
119: public void testAssertionBool1() throws Exception {
120: assertion.setXPathString("count(//error)=2");
121: AssertionResult res = assertion.getResult(result);
122: testLog.debug("isError() " + res.isError() + " isFailure() "
123: + res.isFailure());
124: testLog.debug("failure message: " + res.getFailureMessage());
125: assertFalse("Should not be an error", res.isError());
126: assertFalse("Should not be a failure", res.isFailure());
127: }
128:
129: public void testAssertionBool2() throws Exception {
130: assertion.setXPathString("count(//*[code=1])=1");
131: AssertionResult res = assertion.getResult(result);
132: testLog.debug("isError() " + res.isError() + " isFailure() "
133: + res.isFailure());
134: testLog.debug("failure message: " + res.getFailureMessage());
135: assertFalse("Should not be an error", res.isError());
136: assertFalse("Should not be a failure", res.isFailure());
137: }
138:
139: public void testAssertionBool3() throws Exception {
140: assertion.setXPathString("count(//error)=1"); // wrong
141: AssertionResult res = assertion.getResult(result);
142: testLog.debug("isError() " + res.isError() + " isFailure() "
143: + res.isFailure());
144: testLog.debug("failure message: " + res.getFailureMessage());
145: assertFalse("Should not be an error", res.isError());
146: assertTrue("Should be a failure", res.isFailure());
147: }
148:
149: public void testAssertionBool4() throws Exception {
150: assertion.setXPathString("count(//*[code=2])=1"); //Wrong
151: AssertionResult res = assertion.getResult(result);
152: testLog.debug("isError() " + res.isError() + " isFailure() "
153: + res.isFailure());
154: testLog.debug("failure message: " + res.getFailureMessage());
155: assertFalse("Should not be an error", res.isError());
156: assertTrue("Should be a failure", res.isFailure());
157: }
158:
159: public void testAssertionNumber() throws Exception {
160: assertion.setXPathString("count(//error)");// not yet handled
161: AssertionResult res = assertion.getResult(result);
162: testLog.debug("isError() " + res.isError() + " isFailure() "
163: + res.isFailure());
164: testLog.debug("failure message: " + res.getFailureMessage());
165: assertFalse("Should not be an error", res.isError());
166: assertTrue("Should be a failure", res.isFailure());
167: }
168:
169: public void testAssertionNoResult() throws Exception {
170: // result.setResponseData - not set
171: result = new SampleResult();
172: AssertionResult res = assertion.getResult(result);
173: testLog.debug("isError() " + res.isError() + " isFailure() "
174: + res.isFailure());
175: testLog.debug("failure message: " + res.getFailureMessage());
176: assertEquals(AssertionResult.RESPONSE_WAS_NULL, res
177: .getFailureMessage());
178: assertFalse("Should not be an error", res.isError());
179: assertTrue("Should be a failure", res.isFailure());
180: }
181:
182: public void testAssertionEmptyResult() throws Exception {
183: result.setResponseData("".getBytes());
184: AssertionResult res = assertion.getResult(result);
185: testLog.debug("isError() " + res.isError() + " isFailure() "
186: + res.isFailure());
187: testLog.debug("failure message: " + res.getFailureMessage());
188: assertEquals(AssertionResult.RESPONSE_WAS_NULL, res
189: .getFailureMessage());
190: assertFalse("Should not be an error", res.isError());
191: assertTrue("Should be a failure", res.isFailure());
192: }
193:
194: public void testAssertionBlankResult() throws Exception {
195: result.setResponseData(" ".getBytes());
196: AssertionResult res = assertion.getResult(result);
197: testLog.debug("isError() " + res.isError() + " isFailure() "
198: + res.isFailure());
199: testLog.debug("failure message: " + res.getFailureMessage());
200: assertTrue(res.getFailureMessage().indexOf(
201: "Premature end of file") > 0);
202: assertTrue("Should be an error", res.isError());
203: assertFalse("Should not be a failure", res.isFailure());
204: }
205:
206: }
|