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 junit.framework.TestCase;
022:
023: import org.apache.jmeter.junit.JMeterTestCase;
024: import org.apache.jmeter.samplers.SampleResult;
025: import org.apache.jmeter.threads.JMeterContext;
026: import org.apache.jmeter.threads.JMeterContextService;
027: import org.apache.jmeter.threads.JMeterVariables;
028: import org.apache.jorphan.logging.LoggingManager;
029: import org.apache.log.Logger;
030:
031: public class PackageTest extends TestCase {
032:
033: public PackageTest() {
034: super ();
035: }
036:
037: public PackageTest(String arg0) {
038: super (arg0);
039: }
040:
041: public void testHex() throws Exception {
042: assertEquals("00010203", MD5HexAssertion.baToHex(new byte[] {
043: 0, 1, 2, 3 }));
044: assertEquals("03020100", MD5HexAssertion.baToHex(new byte[] {
045: 3, 2, 1, 0 }));
046: assertEquals("0f807fff", MD5HexAssertion.baToHex(new byte[] {
047: 0xF, -128, 127, -1 }));
048: }
049:
050: public void testMD5() throws Exception {
051: assertEquals("D41D8CD98F00B204E9800998ECF8427E",
052: MD5HexAssertion.baMD5Hex(new byte[] {}).toUpperCase());
053: }
054:
055: int threadsRunning;
056:
057: int failed;
058:
059: public void testThreadSafety() throws Exception {
060: Thread[] threads = new Thread[100];
061: for (int i = 0; i < threads.length; i++) {
062: threads[i] = new TestThread();
063: }
064: failed = 0;
065: for (int i = 0; i < threads.length; i++) {
066: threads[i].start();
067: threadsRunning++;
068: }
069: synchronized (this ) {
070: while (threadsRunning > 0) {
071: wait();
072: }
073: }
074: assertEquals(failed, 0);
075: }
076:
077: class TestThread extends Thread {
078: static final String TEST_STRING = "DAbale arroz a la zorra el abad.";
079:
080: // Used to be 'dábale', but caused trouble on Gump. Reasons
081: // unknown.
082: static final String TEST_PATTERN = ".*A.*\\.";
083:
084: public void run() {
085: ResponseAssertion assertion = new ResponseAssertion();
086: assertion.setTestFieldResponseData();
087: assertion.setToContainsType();
088: assertion.addTestString(TEST_PATTERN);
089: SampleResult response = new SampleResult();
090: response.setResponseData(TEST_STRING.getBytes());
091: for (int i = 0; i < 100; i++) {
092: AssertionResult result;
093: result = assertion.getResult(response);
094: if (result.isFailure() || result.isError()) {
095: failed++;
096: }
097: }
098: synchronized (PackageTest.this ) {
099: threadsRunning--;
100: PackageTest.this .notifyAll();
101: }
102: }
103: }
104:
105: public static class XPathAssertionTest extends TestCase {
106: private static final Logger log = LoggingManager
107: .getLoggerForClass();
108:
109: XPathAssertion assertion;
110:
111: SampleResult result;
112:
113: JMeterVariables vars;
114:
115: JMeterContext jmctx;
116:
117: public XPathAssertionTest() {
118: super ();
119: }
120:
121: public XPathAssertionTest(String name) {
122: super (name);
123: }
124:
125: public void setUp() {
126: jmctx = JMeterContextService.getContext();
127: assertion = new XPathAssertion();
128: assertion.setThreadContext(jmctx);// This would be done by the run
129: // command
130: // assertion.setRefName("regVal");
131:
132: result = new SampleResult();
133: String data = "<company-xmlext-query-ret>" + "<row>"
134: + "<value field=\"RetCode\">LIS_OK</value>"
135: + "<value field=\"RetCodeExtension\"></value>"
136: + "<value field=\"alias\"></value>"
137: + "<value field=\"positioncount\"></value>"
138: + "<value field=\"invalidpincount\">0</value>"
139: + "<value field=\"pinposition1\">1</value>"
140: + "<value field=\"pinpositionvalue1\"></value>"
141: + "<value field=\"pinposition2\">5</value>"
142: + "<value field=\"pinpositionvalue2\"></value>"
143: + "<value field=\"pinposition3\">6</value>"
144: + "<value field=\"pinpositionvalue3\"></value>"
145: + "</row>" + "</company-xmlext-query-ret>";
146: result.setResponseData(data.getBytes());
147: vars = new JMeterVariables();
148: jmctx.setVariables(vars);
149: jmctx.setPreviousResult(result);
150: }
151:
152: public void testAssertion() throws Exception {
153: assertion.setXPathString("//row/value[@field = 'alias']");
154: AssertionResult res = assertion.getResult(jmctx
155: .getPreviousResult());
156: log.debug(" res " + res.isError());
157: log.debug(" failure " + res.getFailureMessage());
158: assertFalse(res.isError());
159: assertFalse(res.isFailure());
160: }
161:
162: public void testNegateAssertion() throws Exception {
163: assertion.setXPathString("//row/value[@field = 'noalias']");
164: assertion.setNegated(true);
165:
166: AssertionResult res = assertion.getResult(jmctx
167: .getPreviousResult());
168: log.debug(" res " + res.isError());
169: log.debug(" failure " + res.getFailureMessage());
170: assertFalse(res.isError());
171: assertFalse(res.isFailure());
172: }
173:
174: public void testValidationFailure() throws Exception {
175: assertion.setXPathString("//row/value[@field = 'alias']");
176: assertion.setNegated(false);
177: assertion.setValidating(true);
178: AssertionResult res = assertion.getResult(jmctx
179: .getPreviousResult());
180: log.debug(res.getFailureMessage() + " error: "
181: + res.isError() + " failure: " + res.isFailure());
182: assertTrue(res.isError());
183: assertFalse(res.isFailure());
184:
185: }
186:
187: public void testValidationSuccess() throws Exception {
188: String data = "<?xml version=\"1.0\"?>"
189: + "<!DOCTYPE BOOK ["
190: + "<!ELEMENT p (#PCDATA)>"
191: + "<!ELEMENT BOOK (OPENER,SUBTITLE?,INTRODUCTION?,(SECTION | PART)+)>"
192: + "<!ELEMENT OPENER (TITLE_TEXT)*>"
193: + "<!ELEMENT TITLE_TEXT (#PCDATA)>"
194: + "<!ELEMENT SUBTITLE (#PCDATA)>"
195: + "<!ELEMENT INTRODUCTION (HEADER, p+)+>"
196: + "<!ELEMENT PART (HEADER, CHAPTER+)>"
197: + "<!ELEMENT SECTION (HEADER, p+)>"
198: + "<!ELEMENT HEADER (#PCDATA)>"
199: + "<!ELEMENT CHAPTER (CHAPTER_NUMBER, CHAPTER_TEXT)>"
200: + "<!ELEMENT CHAPTER_NUMBER (#PCDATA)>"
201: + "<!ELEMENT CHAPTER_TEXT (p)+>" + "]>" + "<BOOK>"
202: + "<OPENER>"
203: + "<TITLE_TEXT>All About Me</TITLE_TEXT>"
204: + "</OPENER>" + "<PART>"
205: + "<HEADER>Welcome To My Book</HEADER>"
206: + "<CHAPTER>"
207: + "<CHAPTER_NUMBER>CHAPTER 1</CHAPTER_NUMBER>"
208: + "<CHAPTER_TEXT>"
209: + "<p>Glad you want to hear about me.</p>"
210: + "<p>There's so much to say!</p>"
211: + "<p>Where should we start?</p>"
212: + "<p>How about more about me?</p>"
213: + "</CHAPTER_TEXT>" + "</CHAPTER>" + "</PART>"
214: + "</BOOK>";
215:
216: result.setResponseData(data.getBytes());
217: vars = new JMeterVariables();
218: jmctx.setVariables(vars);
219: jmctx.setPreviousResult(result);
220: assertion.setXPathString("/");
221: assertion.setValidating(true);
222: AssertionResult res = assertion.getResult(result);
223: assertFalse(res.isError());
224: assertFalse(res.isFailure());
225: }
226:
227: public void testValidationFailureWithDTD() throws Exception {
228: String data = "<?xml version=\"1.0\"?>"
229: + "<!DOCTYPE BOOK ["
230: + "<!ELEMENT p (#PCDATA)>"
231: + "<!ELEMENT BOOK (OPENER,SUBTITLE?,INTRODUCTION?,(SECTION | PART)+)>"
232: + "<!ELEMENT OPENER (TITLE_TEXT)*>"
233: + "<!ELEMENT TITLE_TEXT (#PCDATA)>"
234: + "<!ELEMENT SUBTITLE (#PCDATA)>"
235: + "<!ELEMENT INTRODUCTION (HEADER, p+)+>"
236: + "<!ELEMENT PART (HEADER, CHAPTER+)>"
237: + "<!ELEMENT SECTION (HEADER, p+)>"
238: + "<!ELEMENT HEADER (#PCDATA)>"
239: + "<!ELEMENT CHAPTER (CHAPTER_NUMBER, CHAPTER_TEXT)>"
240: + "<!ELEMENT CHAPTER_NUMBER (#PCDATA)>"
241: + "<!ELEMENT CHAPTER_TEXT (p)+>" + "]>" + "<BOOK>"
242: + "<OPENER>"
243: + "<TITLE_TEXT>All About Me</TITLE_TEXT>"
244: + "</OPENER>" + "<PART>"
245: + "<HEADER>Welcome To My Book</HEADER>"
246: + "<CHAPTER>"
247: + "<CHAPTER_NUMBER>CHAPTER 1</CHAPTER_NUMBER>"
248: + "<CHAPTER_TEXT>"
249: + "<p>Glad you want to hear about me.</p>"
250: + "<p>There's so much to say!</p>"
251: + "<p>Where should we start?</p>"
252: + "<p>How about more about me?</p>"
253: + "</CHAPTER_TEXT>" + "</CHAPTER>"
254: + "<illegal>not defined in dtd</illegal>"
255: + "</PART>" + "</BOOK>";
256:
257: result.setResponseData(data.getBytes());
258: vars = new JMeterVariables();
259: jmctx.setVariables(vars);
260: jmctx.setPreviousResult(result);
261: assertion.setXPathString("/");
262: assertion.setValidating(true);
263: AssertionResult res = assertion.getResult(result);
264: log.debug("failureMessage: " + res.getFailureMessage());
265: assertTrue(res.isError());
266: assertFalse(res.isFailure());
267: }
268:
269: public void testTolerance() throws Exception {
270: String data = "<html><head><title>testtitle</title></head>"
271: + "<body>"
272: + "<p><i><b>invalid tag nesting</i></b><hr>"
273: + "</body></html>";
274:
275: result.setResponseData(data.getBytes());
276: vars = new JMeterVariables();
277: jmctx.setVariables(vars);
278: jmctx.setPreviousResult(result);
279: assertion.setXPathString("/html/head/title");
280: assertion.setValidating(true);
281: assertion.setTolerant(true);
282: AssertionResult res = assertion.getResult(result);
283: log.debug("failureMessage: " + res.getFailureMessage());
284: assertFalse(res.isFailure());
285: assertFalse(res.isError());
286: }
287:
288: public void testNoTolerance() throws Exception {
289: String data = "<html><head><title>testtitle</title></head>"
290: + "<body>"
291: + "<p><i><b>invalid tag nesting</i></b><hr>"
292: + "</body></html>";
293:
294: result.setResponseData(data.getBytes());
295: vars = new JMeterVariables();
296: jmctx.setVariables(vars);
297: jmctx.setPreviousResult(result);
298: assertion.setXPathString("/html/head/title");
299: assertion.setValidating(false);
300: assertion.setTolerant(false);
301: AssertionResult res = assertion.getResult(result);
302: log.debug("failureMessage: " + res.getFailureMessage());
303: assertTrue(res.isError());
304: assertFalse(res.isFailure());
305: }
306:
307: }
308:
309: public static class ResponseAssertionTest extends TestCase {
310:
311: private JMeterContext jmctx;
312: private ResponseAssertion assertion;
313: private SampleResult sample;
314: private JMeterVariables vars;
315: private AssertionResult result;
316:
317: public void setUp() {
318: jmctx = JMeterContextService.getContext();
319: assertion = new ResponseAssertion();
320: assertion.setThreadContext(jmctx);
321: sample = new SampleResult();
322: vars = new JMeterVariables();
323: jmctx.setVariables(vars);
324: jmctx.setPreviousResult(sample);
325: sample
326: .setResponseData(("response Data\n"
327: + "line 2\n\nEOF").getBytes());
328: sample.setSamplerData("Sampler Label");// This is where RA checks the URL!
329: sample.setResponseCode("401");
330: sample.setResponseHeaders("X-Header: abcd");
331: }
332:
333: public void testResponseAssertionEquals() throws Exception {
334: assertion.unsetNotType();
335: assertion.setToEqualsType();
336: assertion.setTestFieldURL();
337: assertion.addTestString("Sampler Label");
338: assertion.addTestString("Sampler labelx");
339: result = assertion.getResult(sample);
340: assertFailed();
341:
342: assertion.setToNotType();
343: assertion.clearTestStrings();
344: assertion.addTestString("Sampler LabeL");
345: assertion.addTestString("Sampler Labelx");
346: result = assertion.getResult(sample);
347: assertPassed();
348: }
349:
350: public void testResponseAssertionHeaders() throws Exception {
351: assertion.unsetNotType();
352: assertion.setToEqualsType();
353: assertion.setTestFieldResponseHeaders();
354: assertion.addTestString("X-Header: abcd");
355: assertion.addTestString("X-Header: abcdx");
356: result = assertion.getResult(sample);
357: assertFailed();
358:
359: assertion.clearTestStrings();
360: assertion.addTestString("X-Header: abcd");
361: result = assertion.getResult(sample);
362: assertPassed();
363: }
364:
365: public void testResponseAssertionContains() throws Exception {
366: assertion.unsetNotType();
367: assertion.setToContainsType();
368: assertion.setTestFieldURL();
369: assertion.addTestString("Sampler");
370: assertion.addTestString("Label");
371: assertion.addTestString(" x");
372:
373: result = assertion.getResult(sample);
374: assertFailed();
375:
376: assertion.setToNotType();
377:
378: result = assertion.getResult(sample);
379: assertFailed();
380:
381: assertion.clearTestStrings();
382: assertion.addTestString("r l");
383: result = assertion.getResult(sample);
384: assertPassed();
385:
386: assertion.unsetNotType();
387: assertion.setTestFieldResponseData();
388:
389: assertion.clearTestStrings();
390: assertion.addTestString("line 2");
391: result = assertion.getResult(sample);
392: assertPassed();
393:
394: assertion.clearTestStrings();
395: assertion.addTestString("(?s)line \\d+.*EOF");
396: result = assertion.getResult(sample);
397: assertPassed();
398:
399: assertion.setTestFieldResponseCode();
400:
401: assertion.clearTestStrings();
402: assertion.addTestString("401");
403: result = assertion.getResult(sample);
404: assertPassed();
405:
406: }
407:
408: // TODO - need a lot more tests
409:
410: private void assertPassed() throws Exception {
411: if (null != result.getFailureMessage()) {
412: //System.out.println(result.getFailureMessage());// debug
413: }
414: assertNull(result.getFailureMessage(), result
415: .getFailureMessage());
416: assertFalse(result.isError());
417: assertFalse(result.isFailure());
418: }
419:
420: private void assertFailed() throws Exception {
421: assertNotNull(result.getFailureMessage());
422: //System.out.println(result.getFailureMessage());
423: assertFalse("Should not be: Response was null",
424: "Response was null".equals(result
425: .getFailureMessage()));
426: assertFalse(result.isError());
427: assertTrue(result.isFailure());
428:
429: }
430: }
431:
432: public static class SizeAssertionTest extends JMeterTestCase {
433:
434: private JMeterContext jmctx;
435: private SizeAssertion assertion;
436: private SampleResult sample1, sample0;
437: private JMeterVariables vars;
438: private AssertionResult result;
439: private String data1 = "response Data\n" + "line 2\n\nEOF";
440: private int data1Len = data1.length();
441:
442: public void setUp() {
443: jmctx = JMeterContextService.getContext();
444: assertion = new SizeAssertion();
445: assertion.setThreadContext(jmctx);
446: vars = new JMeterVariables();
447: jmctx.setVariables(vars);
448: sample0 = new SampleResult();
449: sample1 = new SampleResult();
450: sample1.setResponseData(data1.getBytes());
451: }
452:
453: public void testSizeAssertionEquals() throws Exception {
454: assertion.setCompOper(SizeAssertion.EQUAL);
455: assertion.setAllowedSize(0);
456: result = assertion.getResult(sample1);
457: assertFailed();
458:
459: result = assertion.getResult(sample0);
460: assertPassed();
461:
462: assertion.setAllowedSize(data1Len);
463: result = assertion.getResult(sample1);
464: assertPassed();
465:
466: result = assertion.getResult(sample0);
467: assertFailed();
468: }
469:
470: public void testSizeAssertionNotEquals() throws Exception {
471: assertion.setCompOper(SizeAssertion.NOTEQUAL);
472: assertion.setAllowedSize(0);
473: result = assertion.getResult(sample1);
474: assertPassed();
475:
476: result = assertion.getResult(sample0);
477: assertFailed();
478:
479: assertion.setAllowedSize(data1Len);
480: result = assertion.getResult(sample1);
481: assertFailed();
482:
483: result = assertion.getResult(sample0);
484: assertPassed();
485: }
486:
487: public void testSizeAssertionGreaterThan() throws Exception {
488: assertion.setCompOper(SizeAssertion.GREATERTHAN);
489: assertion.setAllowedSize(0);
490: result = assertion.getResult(sample1);
491: assertPassed();
492:
493: result = assertion.getResult(sample0);
494: assertFailed();
495:
496: assertion.setAllowedSize(data1Len);
497: result = assertion.getResult(sample1);
498: assertFailed();
499:
500: result = assertion.getResult(sample0);
501: assertFailed();
502: }
503:
504: public void testSizeAssertionGreaterThanEqual()
505: throws Exception {
506: assertion.setCompOper(SizeAssertion.GREATERTHANEQUAL);
507: assertion.setAllowedSize(0);
508: result = assertion.getResult(sample1);
509: assertPassed();
510:
511: result = assertion.getResult(sample0);
512: assertPassed();
513:
514: assertion.setAllowedSize(data1Len);
515: result = assertion.getResult(sample1);
516: assertPassed();
517:
518: result = assertion.getResult(sample0);
519: assertFailed();
520: }
521:
522: public void testSizeAssertionLessThan() throws Exception {
523: assertion.setCompOper(SizeAssertion.LESSTHAN);
524: assertion.setAllowedSize(0);
525: result = assertion.getResult(sample1);
526: assertFailed();
527:
528: result = assertion.getResult(sample0);
529: assertFailed();
530:
531: assertion.setAllowedSize(data1Len + 1);
532: result = assertion.getResult(sample1);
533: assertPassed();
534:
535: result = assertion.getResult(sample0);
536: assertPassed();
537: }
538:
539: public void testSizeAssertionLessThanEqual() throws Exception {
540: assertion.setCompOper(SizeAssertion.LESSTHANEQUAL);
541: assertion.setAllowedSize(0);
542: result = assertion.getResult(sample1);
543: assertFailed();
544:
545: result = assertion.getResult(sample0);
546: assertPassed();
547:
548: assertion.setAllowedSize(data1Len + 1);
549: result = assertion.getResult(sample1);
550: assertPassed();
551:
552: result = assertion.getResult(sample0);
553: assertPassed();
554: }
555:
556: // TODO - need a lot more tests
557:
558: private void assertPassed() throws Exception {
559: if (null != result.getFailureMessage()) {
560: //System.out.println(result.getFailureMessage());// debug
561: }
562: assertNull("Failure message should be null", result
563: .getFailureMessage());
564: assertFalse(result.isError());
565: assertFalse(result.isFailure());
566: }
567:
568: private void assertFailed() throws Exception {
569: assertNotNull("Failure nessage should not be null", result
570: .getFailureMessage());
571: //System.out.println(result.getFailureMessage());
572: assertFalse("Should not be: Response was null",
573: "Response was null".equals(result
574: .getFailureMessage()));
575: assertFalse(result.isError());
576: assertTrue(result.isFailure());
577:
578: }
579: }
580:
581: }
|