You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jmeter-dev@jakarta.apache.org by se...@apache.org on 2005/04/11 00:53:47 UTC

cvs commit: jakarta-jmeter/test/src/org/apache/jmeter/assertions XMLSchemaAssertionTest.java

sebb        2005/04/10 15:53:47

  Added:       test/src/org/apache/jmeter/assertions
                        XMLSchemaAssertionTest.java
  Log:
  Test cases for XMLSchema
  
  Revision  Changes    Path
  1.1                  jakarta-jmeter/test/src/org/apache/jmeter/assertions/XMLSchemaAssertionTest.java
  
  Index: XMLSchemaAssertionTest.java
  ===================================================================
  package org.apache.jmeter.assertions;
  
  import java.io.BufferedInputStream;
  import java.io.ByteArrayOutputStream;
  import java.io.FileInputStream;
  import java.io.IOException;
  
  import org.apache.jmeter.junit.JMeterTestCase;
  import org.apache.jmeter.samplers.SampleResult;
  import org.apache.jmeter.threads.JMeterContext;
  import org.apache.jmeter.threads.JMeterContextService;
  import org.apache.jmeter.threads.JMeterVariables;
  //import org.apache.jorphan.logging.LoggingManager;
  
  public class XMLSchemaAssertionTest extends JMeterTestCase {
  
      private XMLSchemaAssertion assertion;
  	private SampleResult result;
  	private JMeterVariables vars;
  	private JMeterContext jmctx;
  	
  
  	public XMLSchemaAssertionTest(String arg0) {
  		super(arg0);
  	}
  
  	protected void setUp() throws Exception {
  		super.setUp();
      	jmctx = JMeterContextService.getContext();
          assertion = new XMLSchemaAssertion();
          assertion.setThreadContext(jmctx);// This would be done by the run command	        
          result = new SampleResult();
          vars = new JMeterVariables();
          jmctx.setVariables(vars);
          jmctx.setPreviousResult(result);
  		//LoggingManager.setPriority("DEBUG","jmeter");
  	}
  
  	protected void tearDown() throws Exception {
  		super.tearDown();
  	}
  	
  	private ByteArrayOutputStream readBA(String name) throws IOException{
  		BufferedInputStream bis = new BufferedInputStream(
  				new FileInputStream(findTestFile(name)));
  		ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
  		int len=0;
  		byte[] data=new byte[512];
  		while((len = bis.read(data)) >= 0){
  			baos.write(data,0,len);
  		}
  		bis.close();
  		return baos;
  	}
  	private byte[] readFile(String name) throws IOException  {
          return readBA(name).toByteArray();
      }
  
  	public void testAssertionOK() throws Exception {
          result.setResponseData(readFile("testfiles/XMLSchematest.xml"));
      	assertion.setXsdFileName("testfiles/XMLSchema-pass.xsd");
      	AssertionResult res = assertion.getResult(jmctx.getPreviousResult());
      	testLog.debug("isError() "+res.isError()+" isFailure() "+res.isFailure());
      	testLog.debug("failure "+res.getFailureMessage());
      	assertFalse("Should not be an error",res.isError());
      	assertFalse("Should not be a failure",res.isFailure());
      }
      
  	public void testAssertionFail() throws Exception {
          result.setResponseData(readFile("testfiles/XMLSchematest.xml"));
      	assertion.setXsdFileName("testfiles/XMLSchema-fail.xsd");
      	AssertionResult res =	assertion.getResult(jmctx.getPreviousResult());
      	testLog.debug("isError() "+res.isError()+" isFailure() "+res.isFailure());
      	testLog.debug("failure "+res.getFailureMessage());
      	assertTrue(res.isError());
      	assertFalse(res.isFailure());
      }
  	
  	public void testAssertionBadXSDFile() throws Exception {
          result.setResponseData(readFile("testfiles/XMLSchematest.xml"));
      	assertion.setXsdFileName("xtestfiles/XMLSchema-fail.xsd");
      	AssertionResult res =	assertion.getResult(jmctx.getPreviousResult());
      	testLog.debug("isError() "+res.isError()+" isFailure() "+res.isFailure());
      	testLog.debug("failure "+res.getFailureMessage());
  		assertTrue(res.getFailureMessage().indexOf("Failed to read schema document")>0);
      	assertTrue(res.isError());// TODO - should this be a failure?
      	assertFalse(res.isFailure());
      }
  	
  	public void testAssertionNoFile() throws Exception {
          result.setResponseData(readFile("testfiles/XMLSchematest.xml"));
      	assertion.setXsdFileName("");
      	AssertionResult res =	assertion.getResult(jmctx.getPreviousResult());
      	testLog.debug("isError() "+res.isError()+" isFailure() "+res.isFailure());
      	testLog.debug("failure "+res.getFailureMessage());
  		assertEquals(XMLSchemaAssertion.FILE_NAME_IS_REQUIRED,res.getFailureMessage());
      	assertFalse(res.isError());
      	assertTrue(res.isFailure());
      }
  	
  	public void testAssertionNoResult() throws Exception {
          //result.setResponseData - not set
      	assertion.setXsdFileName("testfiles/XMLSchema-fail.xsd");
      	AssertionResult res =	assertion.getResult(jmctx.getPreviousResult());
      	testLog.debug("isError() "+res.isError()+" isFailure() "+res.isFailure());
      	testLog.debug("failure "+res.getFailureMessage());
  		assertEquals(AssertionResult.RESPONSE_WAS_NULL,res.getFailureMessage());
      	assertFalse(res.isError());
      	assertTrue(res.isFailure());
      }
  	
  	public void testAssertionEmptyResult() throws Exception {
          result.setResponseData("".getBytes());
      	assertion.setXsdFileName("testfiles/XMLSchema-fail.xsd");
      	AssertionResult res =	assertion.getResult(jmctx.getPreviousResult());
      	testLog.debug("isError() "+res.isError()+" isFailure() "+res.isFailure());
      	testLog.debug("failure "+res.getFailureMessage());
  		assertEquals(AssertionResult.RESPONSE_WAS_NULL,res.getFailureMessage());
      	assertFalse(res.isError());
      	assertTrue(res.isFailure());
      }
  	
  	public void testAssertionBlankResult() throws Exception {
          result.setResponseData(" ".getBytes());
      	assertion.setXsdFileName("testfiles/XMLSchema-fail.xsd");
      	AssertionResult res =	assertion.getResult(jmctx.getPreviousResult());
      	testLog.debug("isError() "+res.isError()+" isFailure() "+res.isFailure());
      	testLog.debug("failure "+res.getFailureMessage());
  		assertTrue(res.getFailureMessage().indexOf("Premature end of file")>0);
      	assertTrue(res.isError());
      	assertFalse(res.isFailure());
      }
  	public void testXMLTrailingcontent() throws Exception {
  		ByteArrayOutputStream baos=readBA("testfiles/XMLSchematest.xml");
  		baos.write("extra".getBytes());
          result.setResponseData(baos.toByteArray());
      	assertion.setXsdFileName("testfiles/XMLSchema-pass.xsd");
      	AssertionResult res =	assertion.getResult(jmctx.getPreviousResult());
      	testLog.debug("isError() "+res.isError()+" isFailure() "+res.isFailure());
      	testLog.debug("failure "+res.getFailureMessage());
  		assertTrue(res.getFailureMessage().indexOf("Content is not allowed in trailing section")>0);
      	assertTrue(res.isError());
      	assertFalse(res.isFailure());
      }
  	public void testXMLTrailingwhitespace() throws Exception {
  		ByteArrayOutputStream baos=readBA("testfiles/XMLSchematest.xml");
  		baos.write(" \t\n".getBytes());
          result.setResponseData(baos.toByteArray());
      	assertion.setXsdFileName("testfiles/XMLSchema-pass.xsd");
      	AssertionResult res =	assertion.getResult(jmctx.getPreviousResult());
      	testLog.debug("xisError() "+res.isError()+" isFailure() "+res.isFailure());
      	testLog.debug("failure "+res.getFailureMessage());
      	assertFalse(res.isError());
      	assertFalse(res.isFailure());
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org