1 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 38 public class GoldenFileValidator extends BaseValidator 39 implements Validator { 40 41 protected File _goldFile = null; 42 protected boolean _ignoreWhitespace=false; 43 protected static String TRUE_MESSAGE = "EXPECTED TO MATCH GOLDEN FILE: "; 44 protected StringBuffer _matchLog = new StringBuffer (); 45 46 public GoldenFileValidator(File goldenFile) { 47 this(null,goldenFile); 48 } 49 50 public GoldenFileValidator(String label, File goldenFile) { 51 super(label); 52 _goldFile = goldenFile; 53 } 54 55 public void setIgnoreWhitespace(boolean ignoreWhitespace) { 56 _ignoreWhitespace = ignoreWhitespace; 57 } 58 59 public void validate(Response response) 60 throws ValidationException { 61 62 boolean cmp=true; 64 65 if (_ignoreWhitespace) { 66 String goldFileString = null; 67 68 try { 69 FileReader reader = new FileReader (_goldFile); 70 71 StringBuffer buf = new StringBuffer (); 72 73 while (true) { 74 int ch = reader.read(); 75 if (ch < 0) { 76 break; 77 } 78 buf.append((char) ch); 79 } 80 81 goldFileString = buf.toString(); 82 } catch (IOException e) { 83 fail(e.toString()); 84 } 85 86 cmp=compareWeak(response.getResource(), goldFileString ); 87 } else { 88 try { 89 InputStream responseStream = response.getStream(); 90 ByteArrayOutputStream responseBytes = new ByteArrayOutputStream (); 91 while (true) { 92 int ch = responseStream.read(); 93 if (ch < 0) { 94 break; 95 } 96 responseBytes.write(ch); 97 } 98 99 FileInputStream fileStream = new FileInputStream (_goldFile); 100 ByteArrayOutputStream fileBytes = new ByteArrayOutputStream (); 101 while (true) { 102 int ch = fileStream.read(); 103 if (ch < 0) { 104 break; 105 } 106 fileBytes.write(ch); 107 } 108 109 cmp=compare(new String (responseBytes.toByteArray()).getBytes(), 110 new String (fileBytes.toByteArray()).getBytes() ); 111 } catch (IOException e) { 112 fail(e.toString()); 113 } 114 } 115 116 if (cmp == false) { 117 StringBuffer buf = new StringBuffer (); 118 buf.append(TRUE_MESSAGE); 119 buf.append(_goldFile); 120 buf.append("\n"); 121 buf.append(_matchLog); 122 fail(buf.toString()); 123 } 124 125 126 } 127 128 protected boolean compare(byte[] array1, byte[] array2) { 130 if ( array1==null || array2==null) return false; 131 if ( array1.length != array2.length ) { 132 log("Wrong size " + array1.length + " " + array2.length ); 133 log("char 1:" + array1[array2.length - 1]); 134 log("char 2:" + array2[array2.length - 1]); 135 return false; 136 } 137 138 for (int i=0; i<array1.length ; i++ ) { 139 if (array1[i] != array2[i] ) { 140 log("Error at " + i + "comparing '" + array1[i] + 141 "' to '" + array2[i] + "'"); 142 return false; 143 } 144 } 145 return true; 146 } 147 148 protected boolean compareWeak(String str1, String str2) { 152 if ( str1==null || str2==null) return false; 153 154 StringTokenizer st1=new StringTokenizer (str1); 155 StringTokenizer st2=new StringTokenizer (str2); 156 157 while (st1.hasMoreTokens() && st2.hasMoreTokens()) { 158 String tok1 = st1.nextToken(); 159 String tok2 = st2.nextToken(); 160 if (!tok1.equals(tok2)) { 161 log("\tFAIL*** : Live token = " + tok1 162 + ", did not match golden file token = " + tok2); 163 return false; 164 } 165 } 166 167 if (st1.hasMoreTokens() || st2.hasMoreTokens()) { 168 return false; 169 } else { 170 return true; 171 } 172 } 173 174 protected void log(String message) { 175 _matchLog.append(message); 176 } 177 178 } 179 | Popular Tags |