| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 149 | |
|
| 150 | |
|
| 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 | |
} |