001: /*
002: * $Id: DiffUtil.java 6044 2006-06-04 20:00:30 +0000 (Sun, 04 Jun 2006) jannehietamaki $ $Revision: 6044 $
003: * $Date: 2006-06-04 20:00:30 +0000 (Sun, 04 Jun 2006) $
004: *
005: * ==================================================================== Licensed
006: * under the Apache License, Version 2.0 (the "License"); you may not use this
007: * file except in compliance with the License. You may obtain a copy of the
008: * License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package wicket.util.diff;
019:
020: import java.io.FileOutputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.PrintWriter;
024: import java.net.URL;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: import wicket.util.io.Streams;
030: import wicket.util.string.StringList;
031:
032: /**
033: * This is a utility class. It serves two purposes.
034: * <p>
035: * First: compare a string output generated by wicket with a file's content
036: * (expected result).
037: * <p>
038: * Second: Create/replace the expected result file with the new content, if a
039: * system property has be made available like
040: * -Dwicket.replace.expected.results=true
041: *
042: * @author Juergen Donnerstag
043: */
044: public final class DiffUtil {
045: private static final Log log = LogFactory.getLog(DiffUtil.class);
046:
047: /**
048: * Compare the output generated by Wicket ("document") with the a previously
049: * generated file which contains the expected result.
050: *
051: * @param document
052: * Current output
053: * @param file
054: * Expected ouput
055: * @param clazz
056: * Used to load the file (relativ to clazz package)
057: * @return true, if equal
058: * @throws IOException
059: */
060: public static final boolean validatePage(String document,
061: final Class clazz, final String file) throws IOException {
062: String filename = clazz.getPackage().getName();
063: filename = filename.replace('.', '/');
064: filename += "/" + file;
065:
066: InputStream in = clazz.getClassLoader().getResourceAsStream(
067: filename);
068: if (in == null) {
069: throw new IOException("File not found: " + filename);
070: }
071:
072: String reference = Streams.readString(in);
073:
074: // replace all line endings with unix style line ending
075: reference = reference.replaceAll("\n\r", "\n");
076: reference = reference.replaceAll("\r\n", "\n");
077:
078: // replace all line endings with unix style line ending
079: document = document.replaceAll("\n\r", "\n");
080: document = document.replaceAll("\r\n", "\n");
081:
082: boolean equals = document.equals(reference);
083: if (equals == false) {
084: // Change the condition to true, if you want to make the new output
085: // the reference output for future tests. That is, it is regarded as
086: // correct. It'll replace the current reference files. Thus change
087: // it only for one test-run.
088: // -Dwicket.replace.expected.results=true
089: final String prop = System
090: .getProperty("wicket.replace.expected.results");
091: if (prop != null) {
092: in.close();
093: in = null;
094:
095: replaceExpectedResultFile(document, clazz, file);
096: return true;
097: }
098:
099: log.error("File name: " + file);
100: /* */
101: log.error("===================");
102: log.error(reference);
103: log.error("===================");
104:
105: log.error(document);
106: log.error("===================");
107: /* */
108:
109: String[] test1 = StringList.tokenize(reference, "\n")
110: .toArray();
111: String[] test2 = StringList.tokenize(document, "\n")
112: .toArray();
113: Diff df = new Diff(test1);
114: Revision r;
115: try {
116: r = df.diff(test2);
117: } catch (DifferentiationFailedException e) {
118: throw new RuntimeException(e);
119: }
120:
121: //Diff diff = new Diff(test1, test2);
122: //Diff.change script = diff.diff_2(false);
123: //DiffPrint.Base p = new DiffPrint.UnifiedPrint(test1, test2);
124: //p.setOutput(new PrintWriter(System.err));
125: //p.print_script(script);
126: System.out.println(r.toString());
127: }
128:
129: return equals;
130: }
131:
132: /**
133: * Replace the expected result file with the current output.
134: *
135: * @param document
136: * How the expected result should look like
137: * @param clazz
138: * Used to load the file (relativ to clazz package)
139: * @param file
140: * The name of the expected result file to be created
141: * @throws IOException
142: */
143: public final static void replaceExpectedResultFile(
144: final String document, final Class clazz, final String file)
145: throws IOException {
146: String filename = clazz.getPackage().getName();
147: filename = filename.replace('.', '/');
148: filename += "/" + file;
149:
150: final URL url = clazz.getClassLoader().getResource(filename);
151: filename = url.getFile();
152: filename = filename.replaceAll("/target/test-classes/",
153: "/src/test/java/");
154: PrintWriter out = new PrintWriter(
155: new FileOutputStream(filename));
156: out.print(document);
157: out.close();
158: }
159:
160: }
|