KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > diff > test > CompareTest


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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 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 JavaDoc {
25         String JavaDoc result;
26
27         result = doDiff(readResource("text1.txt"), readResource("text2.txt"), true, -1);
28         assertEquals(readResource("output1.txt"), result);
29
30         result = doDiff(readResource("text1.txt"), readResource("text2.txt"), false, 1);
31         assertEquals(readResource("output2.txt"), result);
32
33         result = doDiff(readResource("text3.txt"), readResource("text4.txt"), false, 1);
34         assertEquals(readResource("output3.txt"), result);
35
36         result = doDiff(readResource("text3.txt"), readResource("text4.txt"), false, 2);
37         assertEquals(readResource("output4.txt"), result);
38
39         result = doDiff(readResource("text5.txt"), readResource("text6.txt"), true, -1);
40         assertEquals(readResource("output5.txt"), result);
41     }
42
43     String JavaDoc doDiff(String JavaDoc text1, String JavaDoc text2, boolean markLines, int contextLines) throws Exception JavaDoc {
44         StringWriter writer = new StringWriter();
45         Diff.diff(text1, text2, new TextDiffOutput(writer, markLines), contextLines);
46         return writer.toString();
47     }
48
49     String JavaDoc readResource(String JavaDoc name) throws Exception JavaDoc {
50         InputStream is = getClass().getClassLoader().getResourceAsStream("org/outerj/daisy/diff/test/" + name);
51         Reader reader = new InputStreamReader(is, "UTF-8");
52         BufferedReader bufferedReader = new BufferedReader(reader);
53
54         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
55         int c = bufferedReader.read();
56         while (c != -1) {
57             buffer.append((char)c);
58             c = bufferedReader.read();
59         }
60
61         return buffer.toString();
62     }
63 }
64
Popular Tags