|
14 | 14 | import org.junit.jupiter.api.Test;
|
15 | 15 |
|
16 | 16 | import com.github.difflib.DiffUtils;
|
| 17 | +import java.util.ArrayList; |
17 | 18 |
|
18 | 19 | public class PatchTest {
|
19 | 20 |
|
@@ -78,4 +79,48 @@ public void testPatch_Serializable() throws IOException, ClassNotFoundException
|
78 | 79 | }
|
79 | 80 |
|
80 | 81 | }
|
| 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 | + } |
81 | 126 | }
|
0 commit comments