001: package com.ecyrd.jspwiki.diff;
002:
003: import java.io.IOException;
004: import java.util.Properties;
005:
006: import junit.framework.Test;
007: import junit.framework.TestCase;
008: import junit.framework.TestSuite;
009:
010: import org.apache.log4j.PropertyConfigurator;
011:
012: import com.ecyrd.jspwiki.*;
013:
014: public class ContextualDiffProviderTest extends TestCase {
015: /**
016: * Sets up some shorthand notation for writing test cases.
017: * <p>
018: * The quick |^Brown Fox^-Blue Monster-| jumped over |^the^| moon.
019: * <p>
020: * Get it?
021: */
022: private void specializedNotation(ContextualDiffProvider diff) {
023: diff.m_changeEndHtml = "|";
024: diff.m_changeStartHtml = "|";
025:
026: diff.m_deletionEndHtml = "-";
027: diff.m_deletionStartHtml = "-";
028:
029: diff.m_diffEnd = "";
030: diff.m_diffStart = "";
031:
032: diff.m_elidedHeadIndicatorHtml = "...";
033: diff.m_elidedTailIndicatorHtml = "...";
034:
035: diff.m_emitChangeNextPreviousHyperlinks = false;
036:
037: diff.m_insertionEndHtml = "^";
038: diff.m_insertionStartHtml = "^";
039:
040: diff.m_lineBreakHtml = "";
041: diff.m_alternatingSpaceHtml = "_";
042: }
043:
044: public void testNoChanges() throws IOException, WikiException {
045: diffTest(null, "", "", "");
046: diffTest(null, "A", "A", "A");
047: diffTest(null, "A B", "A B", "A B");
048:
049: diffTest(null, " ", " ", " _ _ _");
050: diffTest(null, "A B C", "A B C", "A B _C");
051: diffTest(null, "A B C", "A B C", "A B _ C");
052: }
053:
054: public void testSimpleInsertions() throws IOException,
055: WikiException {
056: // Ah, the white space trailing an insertion is tacked onto the insertion, this is fair, the
057: // alternative would be to greedily take the leading whitespace before the insertion as part
058: // of it instead, and that doesn't make any more or less sense. just remember this behaviour
059: // when writing tests.
060:
061: // Simple inserts...
062: diffTest(null, "A C", "A B C", "A |^B ^|C");
063: diffTest(null, "A D", "A B C D", "A |^B C ^|D");
064:
065: // Simple inserts with spaces...
066: diffTest(null, "A C", "A B C", "A |^B _^|C");
067: diffTest(null, "A C", "A B C", "A |^B _ ^|C");
068: diffTest(null, "A C", "A B C", "A |^B _ _^|C");
069:
070: // Just inserted spaces...
071: diffTest(null, "A B", "A B", "A |^_^|B");
072: diffTest(null, "A B", "A B", "A |^_ ^|B");
073: diffTest(null, "A B", "A B", "A |^_ _^|B");
074: diffTest(null, "A B", "A B", "A |^_ _ ^|B");
075: }
076:
077: public void testSimpleDeletions() throws IOException, WikiException {
078: // Simple deletes...
079: diffTest(null, "A B C", "A C", "A |-B -|C");
080: diffTest(null, "A B C D", "A D", "A |-B C -|D");
081:
082: // Simple deletes with spaces...
083: diffTest(null, "A B C", "A C", "A |-B _-|C");
084: diffTest(null, "A B C", "A C", "A |-B _ -|C");
085:
086: // Just deleted spaces...
087: diffTest(null, "A B", "A B", "A |-_-|B");
088: diffTest(null, "A B", "A B", "A |-_ -|B");
089: diffTest(null, "A B", "A B", "A |-_ _-|B");
090: }
091:
092: public void testContextLimits() throws IOException, WikiException {
093: // No change
094: diffTest("1", "A B C D E F G H I", "A B C D E F G H I", "A...");
095: //TODO Hmm, should the diff provider instead return the string, "No Changes"?
096:
097: // Bad property value, should default to huge context limit and return entire string.
098: diffTest("foobar", "A B C D E F G H I", "A B C D F G H I",
099: "A B C D |-E -|F G H I");
100:
101: // One simple deletion, limit context to 2...
102: diffTest("2", "A B C D E F G H I", "A B C D F G H I",
103: "...D |-E -|F ...");
104:
105: // Deletion of first element, limit context to 2...
106: diffTest("2", "A B C D E", "B C D E", "|-A -|B ...");
107:
108: // Deletion of last element, limit context to 2...
109: diffTest("2", "A B C D E", "A B C D ", "...D |-E-|");
110:
111: // Two simple deletions, limit context to 2...
112: diffTest("2", "A B C D E F G H I J K L M N O P",
113: "A B C E F G H I J K M N O P",
114: "...C |-D -|E ......K |-L -|M ...");
115:
116: }
117:
118: public void testMultiples() throws IOException, WikiException {
119: diffTest(null, "A F", "A B C D E F", "A |^B C D E ^|F");
120: diffTest(null, "A B C D E F", "A F", "A |-B C D E -|F");
121:
122: }
123:
124: public void testSimpleChanges() throws IOException, WikiException {
125: // *changes* are actually an insert and a delete in the output...
126:
127: //single change
128: diffTest(null, "A B C", "A b C", "A |^b^-B-| C");
129:
130: //non-consequtive changes...
131: diffTest(null, "A B C D E", "A b C d E",
132: "A |^b^-B-| C |^d^-D-| E");
133:
134: }
135:
136: // FIXME: This test fails; must be enabled again asap.
137: /*
138: public void testKnownProblemCases() throws NoRequiredPropertyException, IOException
139: {
140: //These all fail...
141:
142: //make two consequtive changes
143: diffTest(null, "A B C D", "A b c D", "A |^b c^-B C-| D");
144: //acually returns -> "A |^b^-B-| |^c^-C-| D"
145:
146: //collapse adjacent elements...
147: diffTest(null, "A B C D", "A BC D", "A |^BC^-B C-| D");
148: //acually returns -> "A |^BC^-B-| |-C -|D"
149:
150:
151: //These failures are all due to how we process the diff results, we need to collapse
152: //adjacent edits into one...
153:
154: }
155: */
156:
157: private void diffTest(String contextLimit, String oldText,
158: String newText, String expectedDiff) throws IOException,
159: WikiException {
160: ContextualDiffProvider diff = new ContextualDiffProvider();
161:
162: specializedNotation(diff);
163:
164: Properties props = new Properties();
165: if (null != contextLimit)
166: props
167: .put(
168: ContextualDiffProvider.PROP_UNCHANGED_CONTEXT_LIMIT,
169: contextLimit);
170:
171: diff.initialize(null, props);
172:
173: props.load(TestEngine.findTestProperties());
174: PropertyConfigurator.configure(props);
175: TestEngine engine = new TestEngine(props);
176:
177: WikiContext ctx = new WikiContext(engine, new WikiPage(engine,
178: "Dummy"));
179: String actualDiff = diff.makeDiffHtml(ctx, oldText, newText);
180:
181: assertEquals(expectedDiff, actualDiff);
182: }
183:
184: public static Test suite() {
185: return new TestSuite(ContextualDiffProviderTest.class);
186: }
187:
188: }
|