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: //import org.apache.jorphan.logging.LoggingManager;
033:
034: public class XMLSchemaAssertionTest extends JMeterTestCase {
035:
036: private XMLSchemaAssertion assertion;
037:
038: private SampleResult result;
039:
040: private JMeterVariables vars;
041:
042: private JMeterContext jmctx;
043:
044: public XMLSchemaAssertionTest(String arg0) {
045: super (arg0);
046: }
047:
048: protected void setUp() throws Exception {
049: super .setUp();
050: jmctx = JMeterContextService.getContext();
051: assertion = new XMLSchemaAssertion();
052: assertion.setThreadContext(jmctx);// This would be done by the run
053: // command
054: result = new SampleResult();
055: vars = new JMeterVariables();
056: jmctx.setVariables(vars);
057: jmctx.setPreviousResult(result);
058: // LoggingManager.setPriority("DEBUG","jmeter");
059: }
060:
061: protected void tearDown() throws Exception {
062: super .tearDown();
063: }
064:
065: private ByteArrayOutputStream readBA(String name)
066: throws IOException {
067: BufferedInputStream bis = new BufferedInputStream(
068: new FileInputStream(findTestFile(name)));
069: ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
070: int len = 0;
071: byte[] data = new byte[512];
072: while ((len = bis.read(data)) >= 0) {
073: baos.write(data, 0, len);
074: }
075: bis.close();
076: return baos;
077: }
078:
079: private byte[] readFile(String name) throws IOException {
080: return readBA(name).toByteArray();
081: }
082:
083: public void testAssertionOK() throws Exception {
084: result.setResponseData(readFile("testfiles/XMLSchematest.xml"));
085: assertion.setXsdFileName("testfiles/XMLSchema-pass.xsd");
086: AssertionResult res = assertion.getResult(jmctx
087: .getPreviousResult());
088: testLog.debug("isError() " + res.isError() + " isFailure() "
089: + res.isFailure());
090: testLog.debug("failure " + res.getFailureMessage());
091: assertFalse("Should not be an error", res.isError());
092: assertFalse("Should not be a failure", res.isFailure());
093: }
094:
095: public void testAssertionFail() throws Exception {
096: result.setResponseData(readFile("testfiles/XMLSchematest.xml"));
097: assertion.setXsdFileName("testfiles/XMLSchema-fail.xsd");
098: AssertionResult res = assertion.getResult(jmctx
099: .getPreviousResult());
100: testLog.debug("isError() " + res.isError() + " isFailure() "
101: + res.isFailure());
102: testLog.debug("failure " + res.getFailureMessage());
103: assertTrue(res.isError());
104: assertFalse(res.isFailure());
105: }
106:
107: public void testAssertionBadXSDFile() throws Exception {
108: result.setResponseData(readFile("testfiles/XMLSchematest.xml"));
109: assertion.setXsdFileName("xtestfiles/XMLSchema-fail.xsd");
110: AssertionResult res = assertion.getResult(jmctx
111: .getPreviousResult());
112: testLog.debug("isError() " + res.isError() + " isFailure() "
113: + res.isFailure());
114: testLog.debug("failure " + res.getFailureMessage());
115: assertTrue(res.getFailureMessage().indexOf(
116: "Failed to read schema document") > 0);
117: assertTrue(res.isError());// TODO - should this be a failure?
118: assertFalse(res.isFailure());
119: }
120:
121: public void testAssertionNoFile() throws Exception {
122: result.setResponseData(readFile("testfiles/XMLSchematest.xml"));
123: assertion.setXsdFileName("");
124: AssertionResult res = assertion.getResult(jmctx
125: .getPreviousResult());
126: testLog.debug("isError() " + res.isError() + " isFailure() "
127: + res.isFailure());
128: testLog.debug("failure " + res.getFailureMessage());
129: assertEquals(XMLSchemaAssertion.FILE_NAME_IS_REQUIRED, res
130: .getFailureMessage());
131: assertFalse(res.isError());
132: assertTrue(res.isFailure());
133: }
134:
135: public void testAssertionNoResult() throws Exception {
136: // result.setResponseData - not set
137: assertion.setXsdFileName("testfiles/XMLSchema-fail.xsd");
138: AssertionResult res = assertion.getResult(jmctx
139: .getPreviousResult());
140: testLog.debug("isError() " + res.isError() + " isFailure() "
141: + res.isFailure());
142: testLog.debug("failure " + res.getFailureMessage());
143: assertEquals(AssertionResult.RESPONSE_WAS_NULL, res
144: .getFailureMessage());
145: assertFalse(res.isError());
146: assertTrue(res.isFailure());
147: }
148:
149: public void testAssertionEmptyResult() throws Exception {
150: result.setResponseData("".getBytes());
151: assertion.setXsdFileName("testfiles/XMLSchema-fail.xsd");
152: AssertionResult res = assertion.getResult(jmctx
153: .getPreviousResult());
154: testLog.debug("isError() " + res.isError() + " isFailure() "
155: + res.isFailure());
156: testLog.debug("failure " + res.getFailureMessage());
157: assertEquals(AssertionResult.RESPONSE_WAS_NULL, res
158: .getFailureMessage());
159: assertFalse(res.isError());
160: assertTrue(res.isFailure());
161: }
162:
163: public void testAssertionBlankResult() throws Exception {
164: result.setResponseData(" ".getBytes());
165: assertion.setXsdFileName("testfiles/XMLSchema-fail.xsd");
166: AssertionResult res = assertion.getResult(jmctx
167: .getPreviousResult());
168: testLog.debug("isError() " + res.isError() + " isFailure() "
169: + res.isFailure());
170: testLog.debug("failure " + res.getFailureMessage());
171: assertTrue(res.getFailureMessage().indexOf(
172: "Premature end of file") > 0);
173: assertTrue(res.isError());
174: assertFalse(res.isFailure());
175: }
176:
177: public void testXMLTrailingcontent() throws Exception {
178: ByteArrayOutputStream baos = readBA("testfiles/XMLSchematest.xml");
179: baos.write("extra".getBytes());
180: result.setResponseData(baos.toByteArray());
181: assertion.setXsdFileName("testfiles/XMLSchema-pass.xsd");
182: AssertionResult res = assertion.getResult(jmctx
183: .getPreviousResult());
184: testLog.debug("isError() " + res.isError() + " isFailure() "
185: + res.isFailure());
186: testLog.debug("failure " + res.getFailureMessage());
187: assertTrue(res.getFailureMessage().indexOf(
188: "Content is not allowed in trailing section") > 0);
189: assertTrue(res.isError());
190: assertFalse(res.isFailure());
191: }
192:
193: public void testXMLTrailingwhitespace() throws Exception {
194: ByteArrayOutputStream baos = readBA("testfiles/XMLSchematest.xml");
195: baos.write(" \t\n".getBytes());
196: result.setResponseData(baos.toByteArray());
197: assertion.setXsdFileName("testfiles/XMLSchema-pass.xsd");
198: AssertionResult res = assertion.getResult(jmctx
199: .getPreviousResult());
200: testLog.debug("xisError() " + res.isError() + " isFailure() "
201: + res.isFailure());
202: testLog.debug("failure " + res.getFailureMessage());
203: assertFalse(res.isError());
204: assertFalse(res.isFailure());
205: }
206: }
|