View Javadoc
1   /*
2    * Copyright (C) 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.notes;
45  
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertNull;
48  import static org.junit.Assert.assertSame;
49  
50  import java.io.IOException;
51  
52  import org.eclipse.jgit.lib.AnyObjectId;
53  import org.eclipse.jgit.lib.MutableObjectId;
54  import org.junit.Test;
55  
56  public class LeafBucketTest {
57  	@Test
58  	public void testEmpty() {
59  		LeafBucket b = new LeafBucket(0);
60  		assertNull(b.getNote(id(0x00), null));
61  		assertNull(b.getNote(id(0x01), null));
62  		assertNull(b.getNote(id(0xfe), null));
63  	}
64  
65  	@Test
66  	public void testParseFive() {
67  		LeafBucket b = new LeafBucket(0);
68  
69  		b.parseOneEntry(id(0x11), id(0x81));
70  		b.parseOneEntry(id(0x22), id(0x82));
71  		b.parseOneEntry(id(0x33), id(0x83));
72  		b.parseOneEntry(id(0x44), id(0x84));
73  		b.parseOneEntry(id(0x55), id(0x85));
74  
75  		assertNull(b.getNote(id(0x01), null));
76  		assertEquals(id(0x81), b.getNote(id(0x11), null).getData());
77  		assertEquals(id(0x82), b.getNote(id(0x22), null).getData());
78  		assertEquals(id(0x83), b.getNote(id(0x33), null).getData());
79  		assertEquals(id(0x84), b.getNote(id(0x44), null).getData());
80  		assertEquals(id(0x85), b.getNote(id(0x55), null).getData());
81  		assertNull(b.getNote(id(0x66), null));
82  	}
83  
84  	@Test
85  	public void testSetFive_InOrder() throws IOException {
86  		LeafBucket b = new LeafBucket(0);
87  
88  		assertSame(b, b.set(id(0x11), id(0x81), null));
89  		assertSame(b, b.set(id(0x22), id(0x82), null));
90  		assertSame(b, b.set(id(0x33), id(0x83), null));
91  		assertSame(b, b.set(id(0x44), id(0x84), null));
92  		assertSame(b, b.set(id(0x55), id(0x85), null));
93  
94  		assertNull(b.getNote(id(0x01), null));
95  		assertEquals(id(0x81), b.getNote(id(0x11), null).getData());
96  		assertEquals(id(0x82), b.getNote(id(0x22), null).getData());
97  		assertEquals(id(0x83), b.getNote(id(0x33), null).getData());
98  		assertEquals(id(0x84), b.getNote(id(0x44), null).getData());
99  		assertEquals(id(0x85), b.getNote(id(0x55), null).getData());
100 		assertNull(b.getNote(id(0x66), null));
101 	}
102 
103 	@Test
104 	public void testSetFive_ReverseOrder() throws IOException {
105 		LeafBucket b = new LeafBucket(0);
106 
107 		assertSame(b, b.set(id(0x55), id(0x85), null));
108 		assertSame(b, b.set(id(0x44), id(0x84), null));
109 		assertSame(b, b.set(id(0x33), id(0x83), null));
110 		assertSame(b, b.set(id(0x22), id(0x82), null));
111 		assertSame(b, b.set(id(0x11), id(0x81), null));
112 
113 		assertNull(b.getNote(id(0x01), null));
114 		assertEquals(id(0x81), b.getNote(id(0x11), null).getData());
115 		assertEquals(id(0x82), b.getNote(id(0x22), null).getData());
116 		assertEquals(id(0x83), b.getNote(id(0x33), null).getData());
117 		assertEquals(id(0x84), b.getNote(id(0x44), null).getData());
118 		assertEquals(id(0x85), b.getNote(id(0x55), null).getData());
119 		assertNull(b.getNote(id(0x66), null));
120 	}
121 
122 	@Test
123 	public void testSetFive_MixedOrder() throws IOException {
124 		LeafBucket b = new LeafBucket(0);
125 
126 		assertSame(b, b.set(id(0x11), id(0x81), null));
127 		assertSame(b, b.set(id(0x33), id(0x83), null));
128 		assertSame(b, b.set(id(0x55), id(0x85), null));
129 
130 		assertSame(b, b.set(id(0x22), id(0x82), null));
131 		assertSame(b, b.set(id(0x44), id(0x84), null));
132 
133 		assertNull(b.getNote(id(0x01), null));
134 		assertEquals(id(0x81), b.getNote(id(0x11), null).getData());
135 		assertEquals(id(0x82), b.getNote(id(0x22), null).getData());
136 		assertEquals(id(0x83), b.getNote(id(0x33), null).getData());
137 		assertEquals(id(0x84), b.getNote(id(0x44), null).getData());
138 		assertEquals(id(0x85), b.getNote(id(0x55), null).getData());
139 		assertNull(b.getNote(id(0x66), null));
140 	}
141 
142 	@Test
143 	public void testSet_Replace() throws IOException {
144 		LeafBucket b = new LeafBucket(0);
145 
146 		assertSame(b, b.set(id(0x11), id(0x81), null));
147 		assertEquals(id(0x81), b.getNote(id(0x11), null).getData());
148 
149 		assertSame(b, b.set(id(0x11), id(0x01), null));
150 		assertEquals(id(0x01), b.getNote(id(0x11), null).getData());
151 	}
152 
153 	@Test
154 	public void testRemoveMissingNote() throws IOException {
155 		LeafBucket b = new LeafBucket(0);
156 		assertNull(b.getNote(id(0x11), null));
157 		assertSame(b, b.set(id(0x11), null, null));
158 		assertNull(b.getNote(id(0x11), null));
159 	}
160 
161 	@Test
162 	public void testRemoveFirst() throws IOException {
163 		LeafBucket b = new LeafBucket(0);
164 
165 		assertSame(b, b.set(id(0x11), id(0x81), null));
166 		assertSame(b, b.set(id(0x22), id(0x82), null));
167 		assertSame(b, b.set(id(0x33), id(0x83), null));
168 		assertSame(b, b.set(id(0x44), id(0x84), null));
169 		assertSame(b, b.set(id(0x55), id(0x85), null));
170 
171 		assertSame(b, b.set(id(0x11), null, null));
172 
173 		assertNull(b.getNote(id(0x01), null));
174 		assertNull(b.getNote(id(0x11), null));
175 		assertEquals(id(0x82), b.getNote(id(0x22), null).getData());
176 		assertEquals(id(0x83), b.getNote(id(0x33), null).getData());
177 		assertEquals(id(0x84), b.getNote(id(0x44), null).getData());
178 		assertEquals(id(0x85), b.getNote(id(0x55), null).getData());
179 		assertNull(b.getNote(id(0x66), null));
180 	}
181 
182 	@Test
183 	public void testRemoveMiddle() throws IOException {
184 		LeafBucket b = new LeafBucket(0);
185 
186 		assertSame(b, b.set(id(0x11), id(0x81), null));
187 		assertSame(b, b.set(id(0x22), id(0x82), null));
188 		assertSame(b, b.set(id(0x33), id(0x83), null));
189 		assertSame(b, b.set(id(0x44), id(0x84), null));
190 		assertSame(b, b.set(id(0x55), id(0x85), null));
191 
192 		assertSame(b, b.set(id(0x33), null, null));
193 
194 		assertNull(b.getNote(id(0x01), null));
195 		assertEquals(id(0x81), b.getNote(id(0x11), null).getData());
196 		assertEquals(id(0x82), b.getNote(id(0x22), null).getData());
197 		assertNull(b.getNote(id(0x33), null));
198 		assertEquals(id(0x84), b.getNote(id(0x44), null).getData());
199 		assertEquals(id(0x85), b.getNote(id(0x55), null).getData());
200 		assertNull(b.getNote(id(0x66), null));
201 	}
202 
203 	@Test
204 	public void testRemoveLast() throws IOException {
205 		LeafBucket b = new LeafBucket(0);
206 
207 		assertSame(b, b.set(id(0x11), id(0x81), null));
208 		assertSame(b, b.set(id(0x22), id(0x82), null));
209 		assertSame(b, b.set(id(0x33), id(0x83), null));
210 		assertSame(b, b.set(id(0x44), id(0x84), null));
211 		assertSame(b, b.set(id(0x55), id(0x85), null));
212 
213 		assertSame(b, b.set(id(0x55), null, null));
214 
215 		assertNull(b.getNote(id(0x01), null));
216 		assertEquals(id(0x81), b.getNote(id(0x11), null).getData());
217 		assertEquals(id(0x82), b.getNote(id(0x22), null).getData());
218 		assertEquals(id(0x83), b.getNote(id(0x33), null).getData());
219 		assertEquals(id(0x84), b.getNote(id(0x44), null).getData());
220 		assertNull(b.getNote(id(0x55), null));
221 		assertNull(b.getNote(id(0x66), null));
222 	}
223 
224 	@Test
225 	public void testRemoveMakesEmpty() throws IOException {
226 		LeafBucket b = new LeafBucket(0);
227 
228 		assertSame(b, b.set(id(0x11), id(0x81), null));
229 		assertEquals(id(0x81), b.getNote(id(0x11), null).getData());
230 
231 		assertNull(b.set(id(0x11), null, null));
232 		assertNull(b.getNote(id(0x11), null));
233 	}
234 
235 	private static AnyObjectId id(int first) {
236 		MutableObjectId id = new MutableObjectId();
237 		id.setByte(1, first);
238 		return id;
239 	}
240 }