001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.diff;
022:
023: import com.liferay.util.diff.DiffResult;
024: import com.liferay.util.diff.DiffUtil;
025:
026: import java.io.StringReader;
027:
028: import java.util.ArrayList;
029: import java.util.List;
030:
031: import junit.framework.TestCase;
032:
033: /**
034: * <a href="DiffTest.java.html"><b><i>View Source</i></b></a>
035: *
036: * @author Bruno Farache
037: *
038: */
039: public class DiffTest extends TestCase {
040:
041: public void testOne() {
042: StringReader reader1 = new StringReader("liferay");
043: StringReader reader2 = new StringReader("liferay");
044:
045: List[] results = DiffUtil.diff(reader1, reader2);
046:
047: List expectedSource = new ArrayList();
048: List expectedTarget = new ArrayList();
049:
050: assertEquals(results[0], expectedSource);
051: assertEquals(results[1], expectedTarget);
052: }
053:
054: public void testTwo() {
055: StringReader reader1 = new StringReader("liferay");
056: StringReader reader2 = new StringReader("LifeRay");
057:
058: List[] results = DiffUtil.diff(reader1, reader2);
059:
060: List expectedSource = new ArrayList();
061: List expectedTarget = new ArrayList();
062:
063: List changedLines = new ArrayList();
064:
065: changedLines.add(DiffUtil.OPEN_DEL + "l" + DiffUtil.CLOSE_DEL
066: + "ife" + DiffUtil.OPEN_DEL + "r" + DiffUtil.CLOSE_DEL
067: + "ay");
068:
069: expectedSource.add(new DiffResult(0, changedLines));
070:
071: changedLines = new ArrayList();
072:
073: changedLines.add(DiffUtil.OPEN_INS + "L" + DiffUtil.CLOSE_INS
074: + "ife" + DiffUtil.OPEN_INS + "R" + DiffUtil.CLOSE_INS
075: + "ay");
076:
077: expectedTarget.add(new DiffResult(0, changedLines));
078:
079: assertEquals(results[0], expectedSource);
080: assertEquals(results[1], expectedTarget);
081: }
082:
083: public void testThree() {
084: StringReader reader1 = new StringReader("aaa");
085: StringReader reader2 = new StringReader("bbb");
086:
087: List[] results = DiffUtil.diff(reader1, reader2);
088:
089: List expectedSource = new ArrayList();
090: List expectedTarget = new ArrayList();
091:
092: expectedSource.add(new DiffResult(0, DiffUtil.CONTEXT_LINE));
093:
094: expectedTarget.add(new DiffResult(0, DiffUtil.OPEN_INS + "bbb"
095: + DiffUtil.CLOSE_INS));
096:
097: expectedSource.add(new DiffResult(0, DiffUtil.OPEN_DEL + "aaa"
098: + DiffUtil.CLOSE_DEL));
099:
100: expectedTarget.add(new DiffResult(0, DiffUtil.CONTEXT_LINE));
101:
102: assertEquals(results[0], expectedSource);
103: assertEquals(results[1], expectedTarget);
104: }
105:
106: public void testFour() {
107: StringReader reader1 = new StringReader("rahab");
108: StringReader reader2 = new StringReader("boaz");
109:
110: List[] results = DiffUtil.diff(reader1, reader2);
111:
112: List expectedSource = new ArrayList();
113: List expectedTarget = new ArrayList();
114:
115: expectedSource.add(new DiffResult(0, DiffUtil.CONTEXT_LINE));
116:
117: expectedTarget.add(new DiffResult(0, DiffUtil.OPEN_INS + "boaz"
118: + DiffUtil.CLOSE_INS));
119:
120: expectedSource.add(new DiffResult(0, DiffUtil.OPEN_DEL
121: + "rahab" + DiffUtil.CLOSE_DEL));
122:
123: expectedTarget.add(new DiffResult(0, DiffUtil.CONTEXT_LINE));
124:
125: assertEquals(results[0], expectedSource);
126: assertEquals(results[1], expectedTarget);
127: }
128:
129: public void testFive() {
130: StringReader reader1 = new StringReader("aaa\nbbb");
131: StringReader reader2 = new StringReader("ccc\naaa");
132:
133: List[] results = DiffUtil.diff(reader1, reader2);
134:
135: List expectedSource = new ArrayList();
136: List expectedTarget = new ArrayList();
137:
138: expectedSource.add(new DiffResult(0, DiffUtil.CONTEXT_LINE));
139:
140: expectedTarget.add(new DiffResult(0, DiffUtil.OPEN_INS + "ccc"
141: + DiffUtil.CLOSE_INS));
142:
143: List changedLines = new ArrayList();
144:
145: changedLines.add("aaa");
146: changedLines
147: .add(DiffUtil.OPEN_DEL + "bbb" + DiffUtil.CLOSE_DEL);
148:
149: expectedSource.add(new DiffResult(1, changedLines));
150:
151: changedLines = new ArrayList();
152:
153: changedLines.add("aaa");
154: changedLines.add(DiffUtil.CONTEXT_LINE);
155:
156: expectedTarget.add(new DiffResult(1, changedLines));
157:
158: assertEquals(results[0], expectedSource);
159: assertEquals(results[1], expectedTarget);
160: }
161:
162: public void testSix() {
163: StringReader reader1 = new StringReader("ccc\naaa");
164: StringReader reader2 = new StringReader("aaa\nbbb");
165:
166: List[] results = DiffUtil.diff(reader1, reader2);
167:
168: List expectedSource = new ArrayList();
169: List expectedTarget = new ArrayList();
170:
171: expectedSource.add(new DiffResult(0, DiffUtil.OPEN_DEL + "ccc"
172: + DiffUtil.CLOSE_DEL));
173:
174: expectedTarget.add(new DiffResult(0, DiffUtil.CONTEXT_LINE));
175:
176: List changedLines = new ArrayList();
177:
178: changedLines.add("aaa");
179: changedLines.add(DiffUtil.CONTEXT_LINE);
180:
181: expectedSource.add(new DiffResult(1, changedLines));
182:
183: changedLines = new ArrayList();
184:
185: changedLines.add("aaa");
186: changedLines
187: .add(DiffUtil.OPEN_INS + "bbb" + DiffUtil.CLOSE_INS);
188:
189: expectedTarget.add(new DiffResult(1, changedLines));
190:
191: assertEquals(results[0], expectedSource);
192: assertEquals(results[1], expectedTarget);
193: }
194:
195: public void testSeven() {
196: StringReader reader1 = new StringReader("ccc\naaa\nbbe");
197: StringReader reader2 = new StringReader("aaa\nbbb");
198:
199: List[] results = DiffUtil.diff(reader1, reader2);
200:
201: List expectedSource = new ArrayList();
202: List expectedTarget = new ArrayList();
203:
204: List changedLines = new ArrayList();
205:
206: expectedSource.add(new DiffResult(0, DiffUtil.OPEN_DEL + "ccc"
207: + DiffUtil.CLOSE_DEL));
208:
209: expectedTarget.add(new DiffResult(0, DiffUtil.CONTEXT_LINE));
210:
211: changedLines = new ArrayList();
212:
213: changedLines.add("bb" + DiffUtil.OPEN_DEL + "e"
214: + DiffUtil.CLOSE_DEL);
215: expectedSource.add(new DiffResult(2, changedLines));
216:
217: changedLines = new ArrayList();
218:
219: changedLines.add("bb" + DiffUtil.OPEN_INS + "b"
220: + DiffUtil.CLOSE_INS);
221: expectedTarget.add(new DiffResult(1, changedLines));
222:
223: assertEquals(results[0], expectedSource);
224: assertEquals(results[1], expectedTarget);
225: }
226:
227: public void testEight() {
228: StringReader reader1 = new StringReader("add\nbbb\nccc");
229: StringReader reader2 = new StringReader("bbb\nccc\naee");
230:
231: List[] results = DiffUtil.diff(reader1, reader2);
232:
233: List expectedSource = new ArrayList();
234: List expectedTarget = new ArrayList();
235:
236: expectedSource.add(new DiffResult(0, DiffUtil.OPEN_DEL + "add"
237: + DiffUtil.CLOSE_DEL));
238:
239: expectedTarget.add(new DiffResult(0, DiffUtil.CONTEXT_LINE));
240:
241: List changedLines = new ArrayList();
242:
243: changedLines.add("bbb");
244: changedLines.add("ccc");
245: changedLines.add(DiffUtil.CONTEXT_LINE);
246: expectedSource.add(new DiffResult(2, changedLines));
247:
248: changedLines = new ArrayList();
249:
250: changedLines.add("bbb");
251: changedLines.add("ccc");
252: changedLines
253: .add(DiffUtil.OPEN_INS + "aee" + DiffUtil.CLOSE_INS);
254: expectedTarget.add(new DiffResult(2, changedLines));
255:
256: assertEquals(results[0], expectedSource);
257: assertEquals(results[1], expectedTarget);
258: }
259:
260: public void testNine() {
261: StringReader reader1 = new StringReader("abcd");
262: StringReader reader2 = new StringReader("abcdee");
263:
264: List[] results = DiffUtil.diff(reader1, reader2);
265:
266: List expectedSource = new ArrayList();
267: List expectedTarget = new ArrayList();
268:
269: expectedSource.add(new DiffResult(0, "abcd"));
270:
271: expectedTarget.add(new DiffResult(0, "abcd" + DiffUtil.OPEN_INS
272: + "ee" + DiffUtil.CLOSE_INS));
273:
274: assertEquals(results[0], expectedSource);
275: assertEquals(results[1], expectedTarget);
276: }
277:
278: public void testTen() {
279: StringReader reader1 = new StringReader("abcd");
280: StringReader reader2 = new StringReader("abcdeee");
281:
282: List[] results = DiffUtil.diff(reader1, reader2);
283:
284: List expectedSource = new ArrayList();
285: List expectedTarget = new ArrayList();
286:
287: expectedSource.add(new DiffResult(0, DiffUtil.CONTEXT_LINE));
288:
289: expectedTarget.add(new DiffResult(0, DiffUtil.OPEN_INS
290: + "abcdeee" + DiffUtil.CLOSE_INS));
291:
292: expectedSource.add(new DiffResult(0, DiffUtil.OPEN_DEL + "abcd"
293: + DiffUtil.CLOSE_DEL));
294:
295: expectedTarget.add(new DiffResult(0, DiffUtil.CONTEXT_LINE));
296:
297: assertEquals(results[0], expectedSource);
298: assertEquals(results[1], expectedTarget);
299: }
300:
301: public void testEleven() {
302: StringReader reader1 = new StringReader("aaa\nbbb\nfff");
303: StringReader reader2 = new StringReader("ccc\nada\nbeb");
304:
305: List[] results = DiffUtil.diff(reader1, reader2);
306:
307: List expectedSource = new ArrayList();
308: List expectedTarget = new ArrayList();
309:
310: expectedSource.add(new DiffResult(0, DiffUtil.CONTEXT_LINE));
311:
312: expectedTarget.add(new DiffResult(0, DiffUtil.OPEN_INS + "ccc"
313: + DiffUtil.CLOSE_INS));
314:
315: expectedSource.add(new DiffResult(0, "a" + DiffUtil.OPEN_DEL
316: + "a" + DiffUtil.CLOSE_DEL + "a"));
317:
318: expectedTarget.add(new DiffResult(1, "a" + DiffUtil.OPEN_INS
319: + "d" + DiffUtil.CLOSE_INS + "a"));
320:
321: expectedSource.add(new DiffResult(1, "b" + DiffUtil.OPEN_DEL
322: + "b" + DiffUtil.CLOSE_DEL + "b"));
323:
324: expectedTarget.add(new DiffResult(2, "b" + DiffUtil.OPEN_INS
325: + "e" + DiffUtil.CLOSE_INS + "b"));
326:
327: expectedSource.add(new DiffResult(2, DiffUtil.OPEN_DEL + "fff"
328: + DiffUtil.CLOSE_DEL));
329:
330: expectedTarget.add(new DiffResult(2, DiffUtil.CONTEXT_LINE));
331:
332: assertEquals(results[0], expectedSource);
333: assertEquals(results[1], expectedTarget);
334: }
335:
336: public void testTwelve() {
337: StringReader reader1 = new StringReader("ada");
338: StringReader reader2 = new StringReader("aaa\nccc");
339:
340: List[] results = DiffUtil.diff(reader1, reader2);
341:
342: List expectedSource = new ArrayList();
343: List expectedTarget = new ArrayList();
344:
345: expectedSource.add(new DiffResult(0, "a" + DiffUtil.OPEN_DEL
346: + "d" + DiffUtil.CLOSE_DEL + "a"));
347:
348: expectedTarget.add(new DiffResult(0, "a" + DiffUtil.OPEN_INS
349: + "a" + DiffUtil.CLOSE_INS + "a"));
350:
351: expectedSource.add(new DiffResult(1, DiffUtil.CONTEXT_LINE));
352:
353: expectedTarget.add(new DiffResult(1, DiffUtil.OPEN_INS + "ccc"
354: + DiffUtil.CLOSE_INS));
355:
356: assertEquals(results[0], expectedSource);
357: assertEquals(results[1], expectedTarget);
358: }
359:
360: }
|