1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.notes;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertSame;
16
17 import org.eclipse.jgit.junit.RepositoryTestCase;
18 import org.eclipse.jgit.junit.TestRepository;
19 import org.eclipse.jgit.lib.ObjectInserter;
20 import org.eclipse.jgit.lib.ObjectReader;
21 import org.eclipse.jgit.lib.Repository;
22 import org.eclipse.jgit.revwalk.RevBlob;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26
27 public class DefaultNoteMergerTest extends RepositoryTestCase {
28
29 private TestRepository<Repository> tr;
30
31 private ObjectReader reader;
32
33 private ObjectInserter inserter;
34
35 private DefaultNoteMerger merger;
36
37 private Note baseNote;
38
39 private RevBlob noteOn;
40
41 @Override
42 @Before
43 public void setUp() throws Exception {
44 super.setUp();
45 tr = new TestRepository<>(db);
46 reader = db.newObjectReader();
47 inserter = db.newObjectInserter();
48 merger = new DefaultNoteMerger();
49 noteOn = tr.blob("a");
50 baseNote = newNote("data");
51 }
52
53 @Override
54 @After
55 public void tearDown() throws Exception {
56 reader.close();
57 inserter.close();
58 super.tearDown();
59 }
60
61 @Test
62 public void testDeleteDelete() throws Exception {
63 assertNull(merger.merge(baseNote, null, null, null, null));
64 }
65
66 @Test
67 public void testEditDelete() throws Exception {
68 Note edit = newNote("edit");
69 assertSame(merger.merge(baseNote, edit, null, null, null), edit);
70 assertSame(merger.merge(baseNote, null, edit, null, null), edit);
71 }
72
73 @Test
74 public void testIdenticalEdit() throws Exception {
75 Note edit = newNote("edit");
76 assertSame(merger.merge(baseNote, edit, edit, null, null), edit);
77 }
78
79 @Test
80 public void testEditEdit() throws Exception {
81 Note edit1 = newNote("edit1");
82 Note edit2 = newNote("edit2");
83
84 Note result = merger.merge(baseNote, edit1, edit2, reader, inserter);
85 assertEquals(result, noteOn);
86 assertEquals(result.getData(), tr.blob("edit1edit2"));
87
88 result = merger.merge(baseNote, edit2, edit1, reader, inserter);
89 assertEquals(result, noteOn);
90 assertEquals(result.getData(), tr.blob("edit2edit1"));
91 }
92
93 @Test
94 public void testIdenticalAdd() throws Exception {
95 Note add = newNote("add");
96 assertSame(merger.merge(null, add, add, null, null), add);
97 }
98
99 @Test
100 public void testAddAdd() throws Exception {
101 Note add1 = newNote("add1");
102 Note add2 = newNote("add2");
103
104 Note result = merger.merge(null, add1, add2, reader, inserter);
105 assertEquals(result, noteOn);
106 assertEquals(result.getData(), tr.blob("add1add2"));
107
108 result = merger.merge(null, add2, add1, reader, inserter);
109 assertEquals(result, noteOn);
110 assertEquals(result.getData(), tr.blob("add2add1"));
111 }
112
113 private Note newNote(String data) throws Exception {
114 return new Note(noteOn, tr.blob(data));
115 }
116 }