1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.lib;
11
12 import static java.nio.charset.StandardCharsets.UTF_8;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15
16 import java.io.File;
17 import java.io.FileOutputStream;
18 import java.io.IOException;
19 import java.util.Arrays;
20 import java.util.Collections;
21
22 import org.eclipse.jgit.junit.RepositoryTestCase;
23 import org.junit.Test;
24
25 public class MergeHeadMsgTest extends RepositoryTestCase {
26 private static final String mergeMsg = "merge a and b";
27
28 private static final String sampleId = "1c6db447abdbb291b25f07be38ea0b1bf94947c5";
29
30 @Test
31 public void testReadWriteMergeHeads() throws IOException {
32 assertEquals(db.readMergeHeads(), null);
33 db.writeMergeHeads(Arrays.asList(ObjectId.zeroId(),
34 ObjectId.fromString(sampleId)));
35 assertEquals(read(new File(db.getDirectory(), "MERGE_HEAD")), "0000000000000000000000000000000000000000\n1c6db447abdbb291b25f07be38ea0b1bf94947c5\n");
36 assertEquals(db.readMergeHeads().size(), 2);
37 assertEquals(db.readMergeHeads().get(0), ObjectId.zeroId());
38 assertEquals(db.readMergeHeads().get(1), ObjectId.fromString(sampleId));
39
40
41 try (FileOutputStream fos = new FileOutputStream(
42 new File(db.getDirectory(), "MERGE_HEAD"));) {
43 fos.write(
44 "0000000000000000000000000000000000000000\n1c6db447abdbb291b25f07be38ea0b1bf94947c5\n"
45 .getBytes(UTF_8));
46 }
47 assertEquals(db.readMergeHeads().size(), 2);
48 assertEquals(db.readMergeHeads().get(0), ObjectId.zeroId());
49 assertEquals(db.readMergeHeads().get(1), ObjectId.fromString(sampleId));
50 db.writeMergeHeads(Collections.<ObjectId> emptyList());
51 assertEquals(read(new File(db.getDirectory(), "MERGE_HEAD")), "");
52 assertEquals(db.readMergeHeads(), null);
53 try (FileOutputStream fos = new FileOutputStream(
54 new File(db.getDirectory(), "MERGE_HEAD"))) {
55 fos.write(sampleId.getBytes(UTF_8));
56 }
57 assertEquals(db.readMergeHeads().size(), 1);
58 assertEquals(db.readMergeHeads().get(0), ObjectId.fromString(sampleId));
59 }
60
61 @Test
62 public void testReadWriteMergeMsg() throws IOException {
63 assertEquals(db.readMergeCommitMsg(), null);
64 assertFalse(new File(db.getDirectory(), "MERGE_MSG").exists());
65 db.writeMergeCommitMsg(mergeMsg);
66 assertEquals(db.readMergeCommitMsg(), mergeMsg);
67 assertEquals(read(new File(db.getDirectory(), "MERGE_MSG")), mergeMsg);
68 db.writeMergeCommitMsg(null);
69 assertEquals(db.readMergeCommitMsg(), null);
70 assertFalse(new File(db.getDirectory(), "MERGE_MSG").exists());
71 try (FileOutputStream fos = new FileOutputStream(
72 new File(db.getDirectory(), Constants.MERGE_MSG))) {
73 fos.write(mergeMsg.getBytes(UTF_8));
74 }
75 assertEquals(db.readMergeCommitMsg(), mergeMsg);
76 }
77
78 }