Coverage Report - org.apache.commons.latka.validators.GoldenFileValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
GoldenFileValidator
0%
0/79
0%
0/34
4.429
 
 1  
 /*
 2  
  * Copyright 1999-2002,2004 The Apache Software Foundation.
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package org.apache.commons.latka.validators;
 18  
 
 19  
 import java.io.ByteArrayOutputStream;
 20  
 import java.io.File;
 21  
 import java.io.FileInputStream;
 22  
 import java.io.FileReader;
 23  
 import java.io.InputStream;
 24  
 import java.io.IOException;
 25  
 
 26  
 import java.util.StringTokenizer;
 27  
 
 28  
 import org.apache.commons.latka.Validator;
 29  
 import org.apache.commons.latka.ValidationException;
 30  
 import org.apache.commons.latka.http.Response;
 31  
 
 32  
 /** Compare the response with a golden
 33  
     file.  Right now this is a proof
 34  
     of concept class; we should extend it to support
 35  
     more than just files, I think.  Also, maybe
 36  
     golden files should be more than just text.
 37  
 */
 38  
 public class GoldenFileValidator extends BaseValidator 
 39  
 implements Validator {
 40  
 
 41  0
     protected File _goldFile = null;
 42  0
     protected boolean _ignoreWhitespace=false;
 43  0
     protected static String TRUE_MESSAGE = "EXPECTED TO MATCH GOLDEN FILE: ";
 44  0
     protected StringBuffer _matchLog = new StringBuffer();
 45  
 
 46  
     public GoldenFileValidator(File goldenFile) {
 47  0
         this(null,goldenFile);
 48  0
     }
 49  
 
 50  
     public GoldenFileValidator(String label, File goldenFile) {
 51  0
         super(label);
 52  0
         _goldFile = goldenFile;
 53  0
     }
 54  
 
 55  
     public void setIgnoreWhitespace(boolean ignoreWhitespace) {
 56  0
         _ignoreWhitespace = ignoreWhitespace;
 57  0
     }
 58  
 
 59  
     public void validate(Response response)
 60  
     throws ValidationException {
 61  
 
 62  
         // Compare the results and set the status
 63  0
         boolean cmp=true;
 64  
 
 65  0
         if (_ignoreWhitespace) {
 66  0
             String goldFileString = null;
 67  
 
 68  
             try {
 69  0
                 FileReader reader = new FileReader(_goldFile);
 70  
 
 71  0
                 StringBuffer buf = new StringBuffer();
 72  
 
 73  
                 while (true) {
 74  0
                     int ch = reader.read();
 75  0
                     if (ch < 0) {
 76  0
                         break;
 77  
                     }
 78  0
                     buf.append((char) ch);
 79  0
                 }
 80  
 
 81  0
                 goldFileString = buf.toString();
 82  0
             } catch (IOException e) {
 83  0
                 fail(e.toString());
 84  0
             }
 85  
 
 86  0
             cmp=compareWeak(response.getResource(), goldFileString );
 87  0
         } else {
 88  
             try {
 89  0
                 InputStream responseStream = response.getStream();
 90  0
                 ByteArrayOutputStream responseBytes = new ByteArrayOutputStream();
 91  
                 while (true) {
 92  0
                     int ch = responseStream.read();
 93  0
                     if (ch < 0) {
 94  0
                         break;
 95  
                     }
 96  0
                     responseBytes.write(ch);
 97  0
                 }
 98  
 
 99  0
                 FileInputStream fileStream = new FileInputStream(_goldFile);
 100  0
                 ByteArrayOutputStream fileBytes = new ByteArrayOutputStream();
 101  
                 while (true) {
 102  0
                     int ch = fileStream.read();
 103  0
                     if (ch < 0) {
 104  0
                         break;
 105  
                     }
 106  0
                     fileBytes.write(ch);
 107  0
                 }
 108  
 
 109  0
                 cmp=compare(new String(responseBytes.toByteArray()).getBytes(), 
 110  
                             new String(fileBytes.toByteArray()).getBytes() );
 111  0
             } catch (IOException e) {
 112  0
                 fail(e.toString());
 113  0
             }
 114  
         }
 115  
 
 116  0
         if (cmp == false) {
 117  0
             StringBuffer buf = new StringBuffer();
 118  0
             buf.append(TRUE_MESSAGE);
 119  0
             buf.append(_goldFile);
 120  0
             buf.append("\n");
 121  0
             buf.append(_matchLog);
 122  0
             fail(buf.toString());
 123  
         }
 124  
 
 125  
 
 126  0
     }
 127  
 
 128  
     // Compare the actual result and the expected result.
 129  
     protected boolean compare(byte[] array1, byte[] array2) {
 130  0
         if ( array1==null || array2==null) return false;
 131  0
         if ( array1.length != array2.length ) {
 132  0
             log("Wrong size " + array1.length + " " + array2.length );
 133  0
             log("char 1:" + array1[array2.length - 1]);
 134  0
             log("char 2:" + array2[array2.length - 1]);
 135  0
             return false;
 136  
         }
 137  
 
 138  0
         for (int i=0; i<array1.length ; i++ ) {
 139  0
             if (array1[i] != array2[i] ) {
 140  0
                 log("Error at " + i  + "comparing '" + array1[i] +
 141  
                     "' to '" + array2[i] + "'");
 142  0
                 return false;
 143  
             }
 144  
         }
 145  0
         return true;
 146  
     }
 147  
 
 148  
     // Compare the actual result and the expected result.
 149  
     // Original compare - ignores whitespace ( because most
 150  
     // golden files are wrong !)
 151  
     protected boolean compareWeak(String str1, String str2) {
 152  0
         if ( str1==null || str2==null) return false;
 153  
 
 154  0
         StringTokenizer st1=new StringTokenizer(str1);
 155  0
         StringTokenizer st2=new StringTokenizer(str2);
 156  
 
 157  0
         while (st1.hasMoreTokens() && st2.hasMoreTokens()) {
 158  0
             String tok1 = st1.nextToken();
 159  0
             String tok2 = st2.nextToken();
 160  0
             if (!tok1.equals(tok2)) {
 161  0
                 log("\tFAIL*** : Live token = " + tok1 
 162  
                     + ", did not match golden file token = " + tok2);
 163  0
                 return false;
 164  
             }
 165  0
         }
 166  
 
 167  0
         if (st1.hasMoreTokens() || st2.hasMoreTokens()) {
 168  0
             return false;
 169  
         } else {
 170  0
             return true;
 171  
         }
 172  
     }
 173  
 
 174  
     protected void log(String message) {
 175  0
         _matchLog.append(message);
 176  0
     }
 177  
 
 178  
 }