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