01: /*
02: * Copyright 2004 Outerthought bvba and Schaubroeck nv
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: package org.outerj.daisy.diff.test;
17:
18: import org.outerj.daisy.diff.*;
19: import junit.framework.TestCase;
20:
21: import java.io.*;
22:
23: public class CompareTest extends TestCase {
24: public void testDiff() throws Exception {
25: String result;
26:
27: result = doDiff(readResource("text1.txt"),
28: readResource("text2.txt"), true, -1);
29: assertEquals(readResource("output1.txt"), result);
30:
31: result = doDiff(readResource("text1.txt"),
32: readResource("text2.txt"), false, 1);
33: assertEquals(readResource("output2.txt"), result);
34:
35: result = doDiff(readResource("text3.txt"),
36: readResource("text4.txt"), false, 1);
37: assertEquals(readResource("output3.txt"), result);
38:
39: result = doDiff(readResource("text3.txt"),
40: readResource("text4.txt"), false, 2);
41: assertEquals(readResource("output4.txt"), result);
42:
43: result = doDiff(readResource("text5.txt"),
44: readResource("text6.txt"), true, -1);
45: assertEquals(readResource("output5.txt"), result);
46: }
47:
48: String doDiff(String text1, String text2, boolean markLines,
49: int contextLines) throws Exception {
50: StringWriter writer = new StringWriter();
51: Diff.diff(text1, text2, new TextDiffOutput(writer, markLines),
52: contextLines);
53: return writer.toString();
54: }
55:
56: String readResource(String name) throws Exception {
57: InputStream is = getClass().getClassLoader()
58: .getResourceAsStream(
59: "org/outerj/daisy/diff/test/" + name);
60: Reader reader = new InputStreamReader(is, "UTF-8");
61: BufferedReader bufferedReader = new BufferedReader(reader);
62:
63: StringBuilder buffer = new StringBuilder();
64: int c = bufferedReader.read();
65: while (c != -1) {
66: buffer.append((char) c);
67: c = bufferedReader.read();
68: }
69:
70: return buffer.toString();
71: }
72: }
|