Skip to content

Commit 8d38e10

Browse files
committed
first test with conflict output
1 parent 8880ec5 commit 8d38e10

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

java-diff-utils/src/test/java/com/github/difflib/patch/PatchTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.junit.jupiter.api.Test;
1515

1616
import com.github.difflib.DiffUtils;
17+
import java.util.ArrayList;
1718

1819
public class PatchTest {
1920

@@ -78,4 +79,48 @@ public void testPatch_Serializable() throws IOException, ClassNotFoundException
7879
}
7980

8081
}
82+
83+
@Test
84+
public void testPatch_Change_withExceptionProcessor() {
85+
final List<String> changeTest_from = Arrays.asList("aaa", "bbb", "ccc", "ddd");
86+
final List<String> changeTest_to = Arrays.asList("aaa", "bxb", "cxc", "ddd");
87+
88+
final Patch<String> patch = DiffUtils.diff(changeTest_from, changeTest_to);
89+
90+
changeTest_from.set(2, "CDC");
91+
92+
patch.withConflictOutput(new ConflictOutput<String>() {
93+
@Override
94+
public void processConflict(VerifyChunk verifyChunk, AbstractDelta<String> delta, List<String> result) throws PatchFailedException {
95+
if (result.size() > delta.getSource().getPosition()) {
96+
List<String> orgData = new ArrayList<>();
97+
98+
for (int i = 0; i < delta.getSource().size(); i++) {
99+
orgData.add(result.get(delta.getSource().getPosition()));
100+
result.remove(delta.getSource().getPosition());
101+
}
102+
103+
orgData.add(0, "<<<<<< HEAD");
104+
orgData.add("======");
105+
orgData.addAll(delta.getSource().getLines());
106+
orgData.add(">>>>>>> PATCH");
107+
108+
result.addAll(delta.getSource().getPosition(), orgData);
109+
110+
} else {
111+
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
112+
}
113+
}
114+
});
115+
116+
try {
117+
List<String> data = DiffUtils.patch(changeTest_from, patch);
118+
assertEquals(9, data.size());
119+
120+
assertEquals(Arrays.asList("aaa", "<<<<<< HEAD", "bbb", "CDC", "======", "bbb", "ccc", ">>>>>>> PATCH", "ddd"), data);
121+
122+
} catch (PatchFailedException e) {
123+
fail(e.getMessage());
124+
}
125+
}
81126
}

0 commit comments

Comments
 (0)