View Javadoc
1   /*
2    * Copyright (C) 2009-2010, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.revwalk;
45  
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertFalse;
48  import static org.junit.Assert.assertNotSame;
49  import static org.junit.Assert.assertSame;
50  import static org.junit.Assert.assertTrue;
51  
52  import org.eclipse.jgit.lib.AnyObjectId;
53  import org.eclipse.jgit.lib.Constants;
54  import org.junit.Test;
55  
56  public class RevObjectTest extends RevWalkTestCase {
57  	@Test
58  	public void testId() throws Exception {
59  		final RevCommit a = commit();
60  		assertSame(a, a.getId());
61  	}
62  
63  	@SuppressWarnings("unlikely-arg-type")
64  	@Test
65  	public void testEquals() throws Exception {
66  		final RevCommit a1 = commit();
67  		final RevCommit b1 = commit();
68  
69  		assertTrue(a1.equals(a1));
70  		assertTrue(a1.equals((Object) a1));
71  		assertFalse(a1.equals(b1));
72  
73  		assertTrue(a1.equals(a1));
74  		assertTrue(a1.equals((Object) a1));
75  		assertFalse(a1.equals(""));
76  
77  		final RevCommit a2;
78  		final RevCommit b2;
79  		try (RevWalk rw2 = new RevWalk(db)) {
80  			a2 = rw2.parseCommit(a1);
81  			b2 = rw2.parseCommit(b1);
82  		}
83  		assertNotSame(a1, a2);
84  		assertNotSame(b1, b2);
85  
86  		assertTrue(a1.equals(a2));
87  		assertTrue(b1.equals(b2));
88  
89  		assertEquals(a1.hashCode(), a2.hashCode());
90  		assertEquals(b1.hashCode(), b2.hashCode());
91  
92  		assertTrue(AnyObjectId.equals(a1, a2));
93  		assertTrue(AnyObjectId.equals(b1, b2));
94  	}
95  
96  	@Test
97  	public void testRevObjectTypes() throws Exception {
98  		assertEquals(Constants.OBJ_TREE, tree().getType());
99  		assertEquals(Constants.OBJ_COMMIT, commit().getType());
100 		assertEquals(Constants.OBJ_BLOB, blob("").getType());
101 		assertEquals(Constants.OBJ_TAG, tag("emptyTree", tree()).getType());
102 	}
103 
104 	@Test
105 	public void testHasRevFlag() throws Exception {
106 		final RevCommit a = commit();
107 		assertFalse(a.has(RevFlag.UNINTERESTING));
108 		a.flags |= RevWalk.UNINTERESTING;
109 		assertTrue(a.has(RevFlag.UNINTERESTING));
110 	}
111 
112 	@Test
113 	public void testHasAnyFlag() throws Exception {
114 		final RevCommit a = commit();
115 		final RevFlag flag1 = rw.newFlag("flag1");
116 		final RevFlag flag2 = rw.newFlag("flag2");
117 		final RevFlagSet s = new RevFlagSet();
118 		s.add(flag1);
119 		s.add(flag2);
120 
121 		assertFalse(a.hasAny(s));
122 		a.flags |= flag1.mask;
123 		assertTrue(a.hasAny(s));
124 	}
125 
126 	@Test
127 	public void testHasAllFlag() throws Exception {
128 		final RevCommit a = commit();
129 		final RevFlag flag1 = rw.newFlag("flag1");
130 		final RevFlag flag2 = rw.newFlag("flag2");
131 		final RevFlagSet s = new RevFlagSet();
132 		s.add(flag1);
133 		s.add(flag2);
134 
135 		assertFalse(a.hasAll(s));
136 		a.flags |= flag1.mask;
137 		assertFalse(a.hasAll(s));
138 		a.flags |= flag2.mask;
139 		assertTrue(a.hasAll(s));
140 	}
141 
142 	@Test
143 	public void testAddRevFlag() throws Exception {
144 		final RevCommit a = commit();
145 		final RevFlag flag1 = rw.newFlag("flag1");
146 		final RevFlag flag2 = rw.newFlag("flag2");
147 		assertEquals(0, a.flags);
148 
149 		a.add(flag1);
150 		assertEquals(flag1.mask, a.flags);
151 
152 		a.add(flag2);
153 		assertEquals(flag1.mask | flag2.mask, a.flags);
154 	}
155 
156 	@Test
157 	public void testAddRevFlagSet() throws Exception {
158 		final RevCommit a = commit();
159 		final RevFlag flag1 = rw.newFlag("flag1");
160 		final RevFlag flag2 = rw.newFlag("flag2");
161 		final RevFlagSet s = new RevFlagSet();
162 		s.add(flag1);
163 		s.add(flag2);
164 
165 		assertEquals(0, a.flags);
166 
167 		a.add(s);
168 		assertEquals(flag1.mask | flag2.mask, a.flags);
169 	}
170 
171 	@Test
172 	public void testRemoveRevFlag() throws Exception {
173 		final RevCommit a = commit();
174 		final RevFlag flag1 = rw.newFlag("flag1");
175 		final RevFlag flag2 = rw.newFlag("flag2");
176 		a.add(flag1);
177 		a.add(flag2);
178 		assertEquals(flag1.mask | flag2.mask, a.flags);
179 		a.remove(flag2);
180 		assertEquals(flag1.mask, a.flags);
181 	}
182 
183 	@Test
184 	public void testRemoveRevFlagSet() throws Exception {
185 		final RevCommit a = commit();
186 		final RevFlag flag1 = rw.newFlag("flag1");
187 		final RevFlag flag2 = rw.newFlag("flag2");
188 		final RevFlag flag3 = rw.newFlag("flag3");
189 		final RevFlagSet s = new RevFlagSet();
190 		s.add(flag1);
191 		s.add(flag2);
192 		a.add(flag3);
193 		a.add(s);
194 		assertEquals(flag1.mask | flag2.mask | flag3.mask, a.flags);
195 		a.remove(s);
196 		assertEquals(flag3.mask, a.flags);
197 	}
198 }