KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > latka > validators > GoldenFileValidator


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 JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileReader JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 import java.util.StringTokenizer JavaDoc;
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     protected File JavaDoc _goldFile = null;
42     protected boolean _ignoreWhitespace=false;
43     protected static String JavaDoc TRUE_MESSAGE = "EXPECTED TO MATCH GOLDEN FILE: ";
44     protected StringBuffer JavaDoc _matchLog = new StringBuffer JavaDoc();
45
46     public GoldenFileValidator(File JavaDoc goldenFile) {
47         this(null,goldenFile);
48     }
49
50     public GoldenFileValidator(String JavaDoc label, File JavaDoc 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         // Compare the results and set the status
63
boolean cmp=true;
64
65         if (_ignoreWhitespace) {
66             String JavaDoc goldFileString = null;
67
68             try {
69                 FileReader JavaDoc reader = new FileReader JavaDoc(_goldFile);
70
71                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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 JavaDoc e) {
83                 fail(e.toString());
84             }
85
86             cmp=compareWeak(response.getResource(), goldFileString );
87         } else {
88             try {
89                 InputStream JavaDoc responseStream = response.getStream();
90                 ByteArrayOutputStream JavaDoc responseBytes = new ByteArrayOutputStream JavaDoc();
91                 while (true) {
92                     int ch = responseStream.read();
93                     if (ch < 0) {
94                         break;
95                     }
96                     responseBytes.write(ch);
97                 }
98
99                 FileInputStream JavaDoc fileStream = new FileInputStream JavaDoc(_goldFile);
100                 ByteArrayOutputStream JavaDoc fileBytes = new ByteArrayOutputStream JavaDoc();
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 JavaDoc(responseBytes.toByteArray()).getBytes(),
110                             new String JavaDoc(fileBytes.toByteArray()).getBytes() );
111             } catch (IOException JavaDoc e) {
112                 fail(e.toString());
113             }
114         }
115
116         if (cmp == false) {
117             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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     // Compare the actual result and the expected result.
129
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     // 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 JavaDoc str1, String JavaDoc str2) {
152         if ( str1==null || str2==null) return false;
153
154         StringTokenizer JavaDoc st1=new StringTokenizer JavaDoc(str1);
155         StringTokenizer JavaDoc st2=new StringTokenizer JavaDoc(str2);
156
157         while (st1.hasMoreTokens() && st2.hasMoreTokens()) {
158             String JavaDoc tok1 = st1.nextToken();
159             String JavaDoc 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 JavaDoc message) {
175         _matchLog.append(message);
176     }
177
178 }
179
Popular Tags