1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.lib;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertNull;
16 import static org.junit.Assert.assertSame;
17 import static org.junit.Assert.assertTrue;
18 import static org.junit.Assert.fail;
19
20 import java.util.Arrays;
21 import java.util.List;
22
23 import org.junit.Test;
24
25 public class ObjectIdRefTest {
26 private static final ObjectId ID_A = ObjectId
27 .fromString("41eb0d88f833b558bddeb269b7ab77399cdf98ed");
28
29 private static final ObjectId ID_B = ObjectId
30 .fromString("698dd0b8d0c299f080559a1cffc7fe029479a408");
31
32 private static final String name = "refs/heads/a.test.ref";
33
34 @Test
35 public void testConstructor_PeeledStatusNotKnown() {
36 ObjectIdRef r;
37
38 r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, name, ID_A);
39 assertSame(Ref.Storage.LOOSE, r.getStorage());
40 assertSame(name, r.getName());
41 assertSame(ID_A, r.getObjectId());
42 assertFalse("not peeled", r.isPeeled());
43 assertNull("no peel id", r.getPeeledObjectId());
44 assertSame("leaf is this", r, r.getLeaf());
45 assertSame("target is this", r, r.getTarget());
46 assertFalse("not symbolic", r.isSymbolic());
47
48 r = new ObjectIdRef.Unpeeled(Ref.Storage.PACKED, name, ID_A);
49 assertSame(Ref.Storage.PACKED, r.getStorage());
50
51 r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE_PACKED, name, ID_A);
52 assertSame(Ref.Storage.LOOSE_PACKED, r.getStorage());
53
54 r = new ObjectIdRef.Unpeeled(Ref.Storage.NEW, name, null);
55 assertSame(Ref.Storage.NEW, r.getStorage());
56 assertSame(name, r.getName());
57 assertNull("no id on new ref", r.getObjectId());
58 assertFalse("not peeled", r.isPeeled());
59 assertNull("no peel id", r.getPeeledObjectId());
60 assertSame("leaf is this", r, r.getLeaf());
61 assertSame("target is this", r, r.getTarget());
62 assertFalse("not symbolic", r.isSymbolic());
63 }
64
65 @Test
66 public void testConstructor_Peeled() {
67 ObjectIdRef r;
68
69 r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, name, ID_A);
70 assertSame(Ref.Storage.LOOSE, r.getStorage());
71 assertSame(name, r.getName());
72 assertSame(ID_A, r.getObjectId());
73 assertFalse("not peeled", r.isPeeled());
74 assertNull("no peel id", r.getPeeledObjectId());
75 assertSame("leaf is this", r, r.getLeaf());
76 assertSame("target is this", r, r.getTarget());
77 assertFalse("not symbolic", r.isSymbolic());
78
79 r = new ObjectIdRef.PeeledNonTag(Ref.Storage.LOOSE, name, ID_A);
80 assertTrue("is peeled", r.isPeeled());
81 assertNull("no peel id", r.getPeeledObjectId());
82
83 r = new ObjectIdRef.PeeledTag(Ref.Storage.LOOSE, name, ID_A, ID_B);
84 assertTrue("is peeled", r.isPeeled());
85 assertSame(ID_B, r.getPeeledObjectId());
86 }
87
88 @Test
89 public void testUpdateIndex() {
90 ObjectIdRef r;
91
92 r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, name, ID_A, 3);
93 assertTrue(r.getUpdateIndex() == 3);
94
95 r = new ObjectIdRef.PeeledTag(Ref.Storage.LOOSE, name, ID_A, ID_B, 4);
96 assertTrue(r.getUpdateIndex() == 4);
97
98 r = new ObjectIdRef.PeeledNonTag(Ref.Storage.LOOSE, name, ID_A, 5);
99 assertTrue(r.getUpdateIndex() == 5);
100 }
101
102 @Test
103 public void testUpdateIndexNotSet() {
104 List<ObjectIdRef> r = Arrays.asList(
105 new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, name, ID_A),
106 new ObjectIdRef.PeeledTag(Ref.Storage.LOOSE, name, ID_A, ID_B),
107 new ObjectIdRef.PeeledNonTag(Ref.Storage.LOOSE, name, ID_A));
108
109 for (ObjectIdRef ref : r) {
110 try {
111 ref.getUpdateIndex();
112 fail("Update index wasn't set. It must throw");
113 } catch (UnsupportedOperationException u) {
114
115 }
116 }
117 }
118
119
120 @Test
121 public void testToString() {
122 ObjectIdRef r;
123
124 r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, name, ID_A);
125 assertEquals("Ref[" + name + "=" + ID_A.name() + "(-1)]",
126 r.toString());
127 }
128 }