1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.revwalk;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertNotSame;
16 import static org.junit.Assert.assertSame;
17 import static org.junit.Assert.assertTrue;
18
19 import org.eclipse.jgit.lib.AnyObjectId;
20 import org.eclipse.jgit.lib.Constants;
21 import org.junit.Test;
22
23 public class RevObjectTest extends RevWalkTestCase {
24 @Test
25 public void testId() throws Exception {
26 final RevCommit a = commit();
27 assertSame(a, a.getId());
28 }
29
30 @SuppressWarnings("unlikely-arg-type")
31 @Test
32 public void testEquals() throws Exception {
33 final RevCommit a1 = commit();
34 final RevCommit b1 = commit();
35
36 assertTrue(a1.equals(a1));
37 assertTrue(a1.equals((Object) a1));
38 assertFalse(a1.equals(b1));
39
40 assertTrue(a1.equals(a1));
41 assertTrue(a1.equals((Object) a1));
42 assertFalse(a1.equals(""));
43
44 final RevCommit a2;
45 final RevCommit b2;
46 try (RevWalk rw2 = new RevWalk(db)) {
47 a2 = rw2.parseCommit(a1);
48 b2 = rw2.parseCommit(b1);
49 }
50 assertNotSame(a1, a2);
51 assertNotSame(b1, b2);
52
53 assertTrue(a1.equals(a2));
54 assertTrue(b1.equals(b2));
55
56 assertEquals(a1.hashCode(), a2.hashCode());
57 assertEquals(b1.hashCode(), b2.hashCode());
58
59 assertTrue(AnyObjectId.isEqual(a1, a2));
60 assertTrue(AnyObjectId.isEqual(b1, b2));
61 }
62
63 @Test
64 public void testRevObjectTypes() throws Exception {
65 assertEquals(Constants.OBJ_TREE, tree().getType());
66 assertEquals(Constants.OBJ_COMMIT, commit().getType());
67 assertEquals(Constants.OBJ_BLOB, blob("").getType());
68 assertEquals(Constants.OBJ_TAG, tag("emptyTree", tree()).getType());
69 }
70
71 @Test
72 public void testHasRevFlag() throws Exception {
73 final RevCommit a = commit();
74 assertFalse(a.has(RevFlag.UNINTERESTING));
75 a.flags |= RevWalk.UNINTERESTING;
76 assertTrue(a.has(RevFlag.UNINTERESTING));
77 }
78
79 @Test
80 public void testHasAnyFlag() throws Exception {
81 final RevCommit a = commit();
82 final RevFlag flag1 = rw.newFlag("flag1");
83 final RevFlag flag2 = rw.newFlag("flag2");
84 final RevFlagSet s = new RevFlagSet();
85 s.add(flag1);
86 s.add(flag2);
87
88 assertFalse(a.hasAny(s));
89 a.flags |= flag1.mask;
90 assertTrue(a.hasAny(s));
91 }
92
93 @Test
94 public void testHasAllFlag() throws Exception {
95 final RevCommit a = commit();
96 final RevFlag flag1 = rw.newFlag("flag1");
97 final RevFlag flag2 = rw.newFlag("flag2");
98 final RevFlagSet s = new RevFlagSet();
99 s.add(flag1);
100 s.add(flag2);
101
102 assertFalse(a.hasAll(s));
103 a.flags |= flag1.mask;
104 assertFalse(a.hasAll(s));
105 a.flags |= flag2.mask;
106 assertTrue(a.hasAll(s));
107 }
108
109 @Test
110 public void testAddRevFlag() throws Exception {
111 final RevCommit a = commit();
112 final RevFlag flag1 = rw.newFlag("flag1");
113 final RevFlag flag2 = rw.newFlag("flag2");
114 assertEquals(RevWalk.PARSED, a.flags);
115
116 a.add(flag1);
117 assertEquals(RevWalk.PARSED | flag1.mask, a.flags);
118
119 a.add(flag2);
120 assertEquals(RevWalk.PARSED | flag1.mask | flag2.mask, a.flags);
121 }
122
123 @Test
124 public void testAddRevFlagSet() throws Exception {
125 final RevCommit a = commit();
126 final RevFlag flag1 = rw.newFlag("flag1");
127 final RevFlag flag2 = rw.newFlag("flag2");
128 final RevFlagSet s = new RevFlagSet();
129 s.add(flag1);
130 s.add(flag2);
131
132 assertEquals(RevWalk.PARSED, a.flags);
133
134 a.add(s);
135 assertEquals(RevWalk.PARSED | flag1.mask | flag2.mask, a.flags);
136 }
137
138 @Test
139 public void testRemoveRevFlag() throws Exception {
140 final RevCommit a = commit();
141 final RevFlag flag1 = rw.newFlag("flag1");
142 final RevFlag flag2 = rw.newFlag("flag2");
143 a.add(flag1);
144 a.add(flag2);
145 assertEquals(RevWalk.PARSED | flag1.mask | flag2.mask, a.flags);
146 a.remove(flag2);
147 assertEquals(RevWalk.PARSED | flag1.mask, a.flags);
148 }
149
150 @Test
151 public void testRemoveRevFlagSet() throws Exception {
152 final RevCommit a = commit();
153 final RevFlag flag1 = rw.newFlag("flag1");
154 final RevFlag flag2 = rw.newFlag("flag2");
155 final RevFlag flag3 = rw.newFlag("flag3");
156 final RevFlagSet s = new RevFlagSet();
157 s.add(flag1);
158 s.add(flag2);
159 a.add(flag3);
160 a.add(s);
161 assertEquals(RevWalk.PARSED | flag1.mask | flag2.mask | flag3.mask, a.flags);
162 a.remove(s);
163 assertEquals(RevWalk.PARSED | flag3.mask, a.flags);
164 }
165 }