morgand 01/08/17 13:11:43 Added: latka/src/java/org/apache/commons/latka/validators GoldenFileHandler.java GoldenFileValidator.java ResponseHeaderHandler.java ResponseHeaderValidator.java Log: new validators Revision Changes Path 1.1 jakarta-commons-sandbox/latka/src/java/org/apache/commons/latka/validators/GoldenFileHandler.java Index: GoldenFileHandler.java =================================================================== package org.apache.commons.latka.validators; import java.io.File; import org.apache.commons.latka.xml.ValidationHandler; import org.xml.sax.Attributes; import org.xml.sax.SAXException; public class GoldenFileHandler extends ValidationHandler { public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { GoldenFileValidator validator = new GoldenFileValidator(atts.getValue("label"), new File(atts.getValue("file_name"))); String ignoreWhitespace = atts.getValue("ignore_whitespace"); if (ignoreWhitespace != null) { validator.setIgnoreWhitespace(Boolean.valueOf(ignoreWhitespace).booleanValue()); } validate(validator); } } 1.1 jakarta-commons-sandbox/latka/src/java/org/apache/commons/latka/validators/GoldenFileValidator.java Index: GoldenFileValidator.java =================================================================== /* * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * [Additional notices, if required by prior licensing conditions] * */ package org.apache.commons.latka.validators; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.StringTokenizer; import org.apache.commons.latka.Validator; import org.apache.commons.latka.ValidationException; import org.apache.commons.latka.http.Response; /** Compare the response with a golden file. Right now this is a proof of concept class; we should extend it to support more than just files, I think. Also, maybe golden files should be more than just text. */ public class GoldenFileValidator extends BaseValidator implements Validator { protected File _goldFile = null; protected boolean _ignoreWhitespace=false; protected static String TRUE_MESSAGE = "EXPECTED TO MATCH GOLDEN FILE: "; protected StringBuffer _matchLog = new StringBuffer(); public GoldenFileValidator(File goldenFile) { this(null,goldenFile); } public GoldenFileValidator(String label, File goldenFile) { super(label); _goldFile = goldenFile; } public void setIgnoreWhitespace(boolean ignoreWhitespace) { _ignoreWhitespace = ignoreWhitespace; } public void validate(Response response) throws ValidationException { String goldFileString = null; try { FileReader reader = new FileReader(_goldFile); StringBuffer buf = new StringBuffer(); while (true) { int ch = reader.read(); if (ch < 0) { break; } buf.append((char) ch); } goldFileString = buf.toString(); } catch (IOException e) { fail(e.toString()); } // Compare the results and set the status boolean cmp=true; if(_ignoreWhitespace) { cmp=compareWeak(response.getResource(), goldFileString ); } else { cmp=compare(response.getResource(), goldFileString ); } if (cmp == false) { StringBuffer buf = new StringBuffer(); buf.append(TRUE_MESSAGE); buf.append(_goldFile); buf.append("\n"); buf.append(_matchLog); fail(buf.toString()); } } // Compare the actual result and the expected result. protected boolean compare(String str1, String str2) { if ( str1==null || str2==null) return false; if ( str1.length() != str2.length() ) { log("Wrong size " + str1.length() +" " + str2.length() ); return false; } for (int i=0; i<str1.length() ; i++ ) { if (str1.charAt( i ) != str2.charAt( i ) ) { log("Error at " + i + " " + str1.charAt(1) + str2.charAt(i)); return false; } } return true; } // Compare the actual result and the expected result. // Original compare - ignores whitespace ( because most // golden files are wrong !) protected boolean compareWeak(String str1, String str2) { if ( str1==null || str2==null) return false; StringTokenizer st1=new StringTokenizer(str1); StringTokenizer st2=new StringTokenizer(str2); while (st1.hasMoreTokens() && st2.hasMoreTokens()) { String tok1 = st1.nextToken(); String tok2 = st2.nextToken(); if (!tok1.equals(tok2)) { log("\tFAIL*** : Rtok1 = " + tok1 + ", Etok2 = " + tok2); return false; } } if (st1.hasMoreTokens() || st2.hasMoreTokens()) { return false; } else { return true; } } protected void log(String message) { _matchLog.append(message); } } 1.1 jakarta-commons-sandbox/latka/src/java/org/apache/commons/latka/validators/ResponseHeaderHandler.java Index: ResponseHeaderHandler.java =================================================================== package org.apache.commons.latka.validators; import org.apache.commons.latka.xml.ValidationHandler; import org.xml.sax.Attributes; import org.xml.sax.SAXException; public class ResponseHeaderHandler extends ValidationHandler { public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { ResponseHeaderValidator validator = new ResponseHeaderValidator(atts.getValue("label"),atts.getValue("header")); validate(validator); } } 1.1 jakarta-commons-sandbox/latka/src/java/org/apache/commons/latka/validators/ResponseHeaderValidator.java Index: ResponseHeaderValidator.java =================================================================== package org.apache.commons.latka.validators; import java.util.StringTokenizer; import org.apache.commons.latka.Validator; import org.apache.commons.latka.ValidationException; import org.apache.commons.latka.http.Response; /** * ResponseHeaderValidator validates response headers in an HTTP session. * * @author Morgan Delagrange */ public class ResponseHeaderValidator extends BaseValidator implements Validator { // --------------------------------------------------------------- Attributes protected String _header = null; protected static final String MESSAGE_NONEXISTENT_HEADER = "HEADER NOT IN RESPONSE"; protected static final String MESSAGE_UNEQUAL_VALUES = "HEADER VALUES UNEQUAL:"; // ------------------------------------------------------------- Constructors public ResponseHeaderValidator(String header) { this(null,header); } public ResponseHeaderValidator(String label, String header) { super(label); _header = header; } public void validate(Response response) throws ValidationException { StringTokenizer tokenizer = new StringTokenizer(_header,":"); String name = tokenizer.nextToken(); String expectedValue = tokenizer.nextToken(); String actualValue = response.getHeader(name); if (actualValue == null) { if (!expectedValue.equals("null")) { fail(MESSAGE_NONEXISTENT_HEADER); } } else { if (!actualValue.equals(expectedValue)) { StringBuffer buffer = new StringBuffer(MESSAGE_UNEQUAL_VALUES); buffer.append(" EXPECTED: "); buffer.append(expectedValue); buffer.append(" RECEIVED: "); buffer.append(actualValue); fail(buffer.toString()); } } } }