001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.junit.diff;
043:
044: import java.io.BufferedReader;
045: import java.io.File;
046: import java.io.FileReader;
047: import java.io.FileWriter;
048: import java.io.IOException;
049: import java.io.Writer;
050: import org.netbeans.junit.NbTestCase;
051:
052: /**
053: *
054: * @author Jan Lahoda, ehucka
055: */
056: public class LineDiffTest extends NbTestCase {
057:
058: public LineDiffTest(String testName) {
059: super (testName);
060: }
061:
062: protected void setUp() throws Exception {
063: }
064:
065: protected void tearDown() throws Exception {
066: }
067:
068: private void doOutputToFile(File f, String content)
069: throws IOException {
070: content = content.replaceAll("\n", System
071: .getProperty("line.separator"));
072: Writer w = new FileWriter(f);
073: try {
074: w.write(content);
075: } finally {
076: w.close();
077: }
078: }
079:
080: private String getFileContent(File f) throws IOException {
081: BufferedReader br = new BufferedReader(new FileReader(f));
082: char[] buffer = new char[1024];
083: int read = br.read(buffer);
084: String t = new String(buffer, 0, read);
085: String ls = System.getProperty("line.separator");
086: return t.replace(ls, "\n");
087: }
088:
089: public void testSimple() throws Exception {
090: File test1 = new File(getWorkDir(), "test1");
091: File test2 = new File(getWorkDir(), "test2");
092:
093: doOutputToFile(test1, "a\nb\nc\nd\n");
094: doOutputToFile(test2, "a\nb\nc\nd\n");
095:
096: LineDiff diff = new LineDiff();
097:
098: assertFalse(diff.diff(test1, test2, null));
099: }
100:
101: public void testEmpty1() throws Exception {
102: File test1 = new File(getWorkDir(), "test1");
103: File test2 = new File(getWorkDir(), "test2");
104:
105: doOutputToFile(test1, "a\nb\nc\nd\n");
106: doOutputToFile(test2, "");
107:
108: LineDiff diff = new LineDiff();
109: assertTrue(diff.diff(test1, test2, null));
110: }
111:
112: public void testEmpty2() throws Exception {
113: File test1 = new File(getWorkDir(), "test1");
114: File test2 = new File(getWorkDir(), "test2");
115:
116: doOutputToFile(test1, "");
117: doOutputToFile(test2, "a\nb\nc\nd\n");
118:
119: LineDiff diff = new LineDiff();
120: assertTrue(diff.diff(test1, test2, null));
121: }
122:
123: public void testIgnoreCase() throws Exception {
124: File test1 = new File(getWorkDir(), "test1");
125: File test2 = new File(getWorkDir(), "test2");
126:
127: doOutputToFile(test1, "A\nb\nC\nd\n");
128: doOutputToFile(test2, "a\nB\nc\nD\n");
129:
130: LineDiff diff = new LineDiff(true);
131: assertFalse(diff.diff(test1, test2, null));
132: }
133:
134: public void testIgnoreEmptyLines() throws Exception {
135: File test1 = new File(getWorkDir(), "test1");
136: File test2 = new File(getWorkDir(), "test2");
137:
138: doOutputToFile(test1, "a\n\nb\n\nc\n\n\nd\n\n");
139: doOutputToFile(test2, "a\nb\nc\nd\n");
140:
141: LineDiff diff = new LineDiff(false, true);
142: assertFalse(diff.diff(test1, test2, null));
143: }
144:
145: public void testDiff1() throws Exception {
146: File test1 = new File(getWorkDir(), "test1");
147: File test2 = new File(getWorkDir(), "test2");
148: File test3 = new File(getWorkDir(), "test3");
149:
150: doOutputToFile(test1, "a\nb\nc\n");
151: doOutputToFile(test2, "a\nb\nc\nd\n");
152:
153: LineDiff diff = new LineDiff();
154: assertTrue(diff.diff(test1, test2, test3));
155: assertEquals(" 1 a\n 2 b\n 3 c\n 4 - d\n",
156: getFileContent(test3));
157: }
158:
159: public void testDiff2() throws Exception {
160: File test1 = new File(getWorkDir(), "test1");
161: File test2 = new File(getWorkDir(), "test2");
162: File test3 = new File(getWorkDir(), "test3");
163:
164: doOutputToFile(test1, "b\nc\nd\n");
165: doOutputToFile(test2, "a\nb\nc\nd\n");
166:
167: LineDiff diff = new LineDiff();
168: assertTrue(diff.diff(test1, test2, test3));
169: assertEquals(" 1 - a\n 2 b\n 3 c\n 4 d\n",
170: getFileContent(test3));
171: }
172:
173: public void testDiff3() throws Exception {
174: File test1 = new File(getWorkDir(), "test1");
175: File test2 = new File(getWorkDir(), "test2");
176: File test3 = new File(getWorkDir(), "test3");
177:
178: doOutputToFile(test1, "a\nb\nb\nc\nc\nc\nb\nd\n");
179: doOutputToFile(test2, "a\nb\nc\nd\n");
180:
181: LineDiff diff = new LineDiff();
182: assertTrue(diff.diff(test1, test2, test3));
183: assertEquals(
184: " 1 a\n 2 b\n + b\n 3 c\n + c\n + c\n + b\n 4 d\n",
185: getFileContent(test3));
186: }
187:
188: public void testDiff4() throws Exception {
189: File test1 = new File(getWorkDir(), "test1");
190: File test2 = new File(getWorkDir(), "test2");
191: File test3 = new File(getWorkDir(), "test3");
192:
193: doOutputToFile(test1, "a\nb\nb\nd\na\nb\nd\n");
194: doOutputToFile(test2, "a\nb\nc\nd\n");
195:
196: LineDiff diff = new LineDiff();
197: assertTrue(diff.diff(test1, test2, test3));
198: assertEquals(
199: " 1 a\n 2 b\n 3 - c\n + b\n 4 d\n + a\n + b\n + d\n",
200: getFileContent(test3));
201: }
202:
203: public void testDiff5() throws Exception {
204: File test1 = new File(getWorkDir(), "test1");
205: File test2 = new File(getWorkDir(), "test2");
206: File test3 = new File(getWorkDir(), "test3");
207:
208: doOutputToFile(test1, "0\na\nb\nc\nd\n");
209: doOutputToFile(test2, "a\nb\nc\nd\n");
210:
211: LineDiff diff = new LineDiff();
212: assertTrue(diff.diff(test1, test2, test3));
213: assertEquals(" + 0\n 1 a\n 2 b\n 3 c\n",
214: getFileContent(test3));
215: }
216:
217: public void testDiff6() throws Exception {
218: File test1 = new File(getWorkDir(), "test1");
219: File test2 = new File(getWorkDir(), "test2");
220: File test3 = new File(getWorkDir(), "test3");
221:
222: doOutputToFile(test1, "a\nb\nc\nd\ne\n");
223: doOutputToFile(test2, "a\nb\nc\nd\n");
224:
225: LineDiff diff = new LineDiff();
226: assertTrue(diff.diff(test1, test2, test3));
227: assertEquals(" 2 b\n 3 c\n 4 d\n + e\n",
228: getFileContent(test3));
229: }
230:
231: public void testDiff7() throws Exception {
232: File test1 = new File(getWorkDir(), "test1");
233: File test2 = new File(getWorkDir(), "test2");
234: File test3 = new File(getWorkDir(), "test3");
235:
236: doOutputToFile(test1, "e\nf\ng\nh\n");
237: doOutputToFile(test2, "a\nb\nc\nd\n");
238:
239: LineDiff diff = new LineDiff();
240: assertTrue(diff.diff(test1, test2, test3));
241: assertEquals(
242: " 1 - a\n + e\n 2 - b\n + f\n 3 - c\n + g\n 4 - d\n + h\n",
243: getFileContent(test3));
244: }
245:
246: public void testDiff8() throws Exception {
247: File test1 = new File(getWorkDir(), "test1");
248: File test2 = new File(getWorkDir(), "test2");
249: File test3 = new File(getWorkDir(), "test3");
250:
251: doOutputToFile(test1, "a\ne\nc\nd\n\nf\n");
252: doOutputToFile(test2, "a\nb\n\nc\nd\nf\n");
253:
254: LineDiff diff = new LineDiff();
255: assertTrue(diff.diff(test1, test2, test3));
256: assertEquals(
257: " 1 a\n 2 - b\n + e\n 3 - \n 4 c\n 5 d\n + \n 6 f\n",
258: getFileContent(test3));
259: }
260:
261: public void testDiff9() throws Exception {
262: File test1 = new File(getWorkDir(), "test1");
263: File test2 = new File(getWorkDir(), "test2");
264: File test3 = new File(getWorkDir(), "test3");
265:
266: doOutputToFile(test1, "a\nx\nc\nd\nb\nf\n");
267: doOutputToFile(test2, "a\nb\nc\nd\nb\nf\n");
268:
269: LineDiff diff = new LineDiff();
270: assertTrue(diff.diff(test1, test2, test3));
271: assertEquals(
272: " 1 a\n + x\n 2 - b\n 3 c\n 4 d\n 5 b\n",
273: getFileContent(test3));
274: }
275:
276: public void testDiff10() throws Exception {
277: File test1 = new File(getWorkDir(), "test1");
278: File test2 = new File(getWorkDir(), "test2");
279: File test3 = new File(getWorkDir(), "test3");
280:
281: doOutputToFile(test1, "a\nx\nc\nd\nx\ny\n");
282: doOutputToFile(test2, "a\nb\nc\nd\nx\ny\n");
283:
284: LineDiff diff = new LineDiff();
285: assertTrue(diff.diff(test1, test2, test3));
286: assertEquals(
287: " 1 a\n 2 - b\n + x\n 3 c\n 4 d\n 5 x\n",
288: getFileContent(test3));
289: }
290:
291: public void testDiff11() throws Exception {
292: File test1 = new File(getWorkDir(), "test1");
293: File test2 = new File(getWorkDir(), "test2");
294: File test3 = new File(getWorkDir(), "test3");
295: try {
296: doOutputToFile(test1, "a\nx\nc\nd\nx\ny\n");
297: doOutputToFile(test2, "a\nb\nc\nd\nx\ny\n");
298:
299: System.setProperty("nbjunit.linediff.context", "0");
300: LineDiff diff = new LineDiff();
301: assertTrue(diff.diff(test1, test2, test3));
302: assertEquals(" 2 - b\n + x\n", getFileContent(test3));
303: } finally {
304: System.setProperty("nbjunit.linediff.context", "");
305: }
306: }
307: }
|