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  	@Test
64  	public void testEquals() throws Exception {
65  		final RevCommit a1 = commit();
66  		final RevCommit b1 = commit();
67  
68  		assertTrue(a1.equals(a1));
69  		assertTrue(a1.equals((Object) a1));
70  		assertFalse(a1.equals(b1));
71  
72  		assertTrue(a1.equals(a1));
73  		assertTrue(a1.equals((Object) a1));
74  		assertFalse(a1.equals(""));
75  
76  		final RevCommit a2;
77  		final RevCommit b2;
78  		try (final RevWalk rw2 = new RevWalk(db)) {
79  			a2 = rw2.parseCommit(a1);
80  			b2 = rw2.parseCommit(b1);
81  		}
82  		assertNotSame(a1, a2);
83  		assertNotSame(b1, b2);
84  
85  		assertTrue(a1.equals(a2));
86  		assertTrue(b1.equals(b2));
87  
88  		assertEquals(a1.hashCode(), a2.hashCode());
89  		assertEquals(b1.hashCode(), b2.hashCode());
90  
91  		assertTrue(AnyObjectId.equals(a1, a2));
92  		assertTrue(AnyObjectId.equals(b1, b2));
93  	}
94  
95  	@Test
96  	public void testRevObjectTypes() throws Exception {
97  		assertEquals(Constants.OBJ_TREE, tree().getType());
98  		assertEquals(Constants.OBJ_COMMIT, commit().getType());
99  		assertEquals(Constants.OBJ_BLOB, blob("").getType());
100 		assertEquals(Constants.OBJ_TAG, tag("emptyTree", tree()).getType());
101 	}
102 
103 	@Test
104 	public void testHasRevFlag() throws Exception {
105 		final RevCommit a = commit();
106 		assertFalse(a.has(RevFlag.UNINTERESTING));
107 		a.flags |= RevWalk.UNINTERESTING;
108 		assertTrue(a.has(RevFlag.UNINTERESTING));
109 	}
110 
111 	@Test
112 	public void testHasAnyFlag() throws Exception {
113 		final RevCommit a = commit();
114 		final RevFlag flag1 = rw.newFlag("flag1");
115 		final RevFlag flag2 = rw.newFlag("flag2");
116 		final RevFlagSet s = new RevFlagSet();
117 		s.add(flag1);
118 		s.add(flag2);
119 
120 		assertFalse(a.hasAny(s));
121 		a.flags |= flag1.mask;
122 		assertTrue(a.hasAny(s));
123 	}
124 
125 	@Test
126 	public void testHasAllFlag() throws Exception {
127 		final RevCommit a = commit();
128 		final RevFlag flag1 = rw.newFlag("flag1");
129 		final RevFlag flag2 = rw.newFlag("flag2");
130 		final RevFlagSet s = new RevFlagSet();
131 		s.add(flag1);
132 		s.add(flag2);
133 
134 		assertFalse(a.hasAll(s));
135 		a.flags |= flag1.mask;
136 		assertFalse(a.hasAll(s));
137 		a.flags |= flag2.mask;
138 		assertTrue(a.hasAll(s));
139 	}
140 
141 	@Test
142 	public void testAddRevFlag() throws Exception {
143 		final RevCommit a = commit();
144 		final RevFlag flag1 = rw.newFlag("flag1");
145 		final RevFlag flag2 = rw.newFlag("flag2");
146 		assertEquals(0, a.flags);
147 
148 		a.add(flag1);
149 		assertEquals(flag1.mask, a.flags);
150 
151 		a.add(flag2);
152 		assertEquals(flag1.mask | flag2.mask, a.flags);
153 	}
154 
155 	@Test
156 	public void testAddRevFlagSet() throws Exception {
157 		final RevCommit a = commit();
158 		final RevFlag flag1 = rw.newFlag("flag1");
159 		final RevFlag flag2 = rw.newFlag("flag2");
160 		final RevFlagSet s = new RevFlagSet();
161 		s.add(flag1);
162 		s.add(flag2);
163 
164 		assertEquals(0, a.flags);
165 
166 		a.add(s);
167 		assertEquals(flag1.mask | flag2.mask, a.flags);
168 	}
169 
170 	@Test
171 	public void testRemoveRevFlag() throws Exception {
172 		final RevCommit a = commit();
173 		final RevFlag flag1 = rw.newFlag("flag1");
174 		final RevFlag flag2 = rw.newFlag("flag2");
175 		a.add(flag1);
176 		a.add(flag2);
177 		assertEquals(flag1.mask | flag2.mask, a.flags);
178 		a.remove(flag2);
179 		assertEquals(flag1.mask, a.flags);
180 	}
181 
182 	@Test
183 	public void testRemoveRevFlagSet() throws Exception {
184 		final RevCommit a = commit();
185 		final RevFlag flag1 = rw.newFlag("flag1");
186 		final RevFlag flag2 = rw.newFlag("flag2");
187 		final RevFlag flag3 = rw.newFlag("flag3");
188 		final RevFlagSet s = new RevFlagSet();
189 		s.add(flag1);
190 		s.add(flag2);
191 		a.add(flag3);
192 		a.add(s);
193 		assertEquals(flag1.mask | flag2.mask | flag3.mask, a.flags);
194 		a.remove(s);
195 		assertEquals(flag3.mask, a.flags);
196 	}
197 }