File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\test\java\jenkins\model\lazy\AbstractLazyLoadRunMapTest.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2012, CloudBees, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package jenkins.model.lazy;
25
26 import jenkins.model.lazy.AbstractLazyLoadRunMap.Direction;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31
32 import java.io.IOException;
33 import java.util.Map;
34 import java.util.NoSuchElementException;
35 import java.util.SortedMap;
36 import java.util.logging.Level;
37 import org.junit.BeforeClass;
38 import org.jvnet.hudson.test.Bug;
39
40 /**
41 * @author Kohsuke Kawaguchi
42 */
43 public class AbstractLazyLoadRunMapTest extends Assert {
44 // A=1, B=3, C=5
45 @Rule
46 public FakeMapBuilder aBuilder = new FakeMapBuilder();
47 private FakeMap a;
48
49 // empty map
50 @Rule
51 public FakeMapBuilder bBuilder = new FakeMapBuilder();
52 private FakeMap b;
53
54 @Rule
55 public FakeMapBuilder localBuilder = new FakeMapBuilder();
56
57 @BeforeClass
58 public static void setUpClass() {
59 AbstractLazyLoadRunMap.LOGGER.setLevel(Level.OFF);
60 }
61
62 @Before
63 public void setUp() throws Exception {
64 a = aBuilder.add(1, "A").add(3, "B").add(5, "C").make();
65
66 b = bBuilder.make();
67 }
68
69 @Test
70 public void lookup() {
71 assertNull(a.get(0));
72 a.get(1).asserts(1, "A");
73 assertNull(a.get(2));
74 a.get(3).asserts(3, "B");
75 assertNull(a.get(4));
76 a.get(5).asserts(5, "C");
77 assertNull(a.get(6));
78
79 assertNull(b.get(1));
80 assertNull(b.get(3));
81 assertNull(b.get(5));
82 }
83
84 @Test
85 public void lookup2() {
86 assertNull(a.get(6));
87 }
88
89 @Test
90 public void idempotentLookup() {
91 for (int i=0; i<5; i++) {
92 a.get(1).asserts(1,"A");
93 a.get((Object)1).asserts(1, "A");
94 }
95 }
96
97 @Test
98 public void lookupWithBogusKeyType() {
99 assertNull(a.get(null));
100 assertNull(a.get("foo"));
101 assertNull(a.get(this));
102 }
103
104 @Test
105 public void firstKey() {
106 assertEquals(5, a.firstKey().intValue());
107
108 try {
109 b.firstKey();
110 fail();
111 } catch (NoSuchElementException e) {
112 // as expected
113 }
114 }
115
116 @Test
117 public void lastKey() {
118 assertEquals(1, a.lastKey().intValue());
119 try {
120 b.lastKey();
121 fail();
122 } catch (NoSuchElementException e) {
123 // as expected
124 }
125 }
126
127 @Test
128 public void search() {
129 // searching toward non-existent direction
130 assertNull(a.search(99, Direction.ASC));
131 assertNull(a.search(-99, Direction.DESC));
132 }
133
134 /**
135 * If load fails, search needs to gracefully handle it
136 */
137 @Test
138 public void unloadableData() throws IOException {
139 FakeMap m = localBuilder.add(1, "A").addUnloadable("B").add(5, "C").make();
140
141 assertNull(m.search(3, Direction.EXACT));
142 m.search(3,Direction.DESC).asserts(1, "A");
143 m.search(3, Direction.ASC ).asserts(5, "C");
144 }
145
146 @Test
147 public void eagerLoading() throws IOException {
148 Map.Entry[] b = a.entrySet().toArray(new Map.Entry[3]);
149 ((Build)b[0].getValue()).asserts(5, "C");
150 ((Build)b[1].getValue()).asserts(3, "B");
151 ((Build)b[2].getValue()).asserts(1, "A");
152 }
153
154 @Test
155 public void fastLookup() throws IOException {
156 FakeMap a = localBuilder.addBoth(1, "A").addBoth(3, "B").addBoth(5, "C"). make();
157
158 a.get(1).asserts(1,"A");
159 assertNull(a.get(2));
160 a.get(3).asserts(3,"B");
161 assertNull(a.get(4));
162 a.get(5).asserts(5,"C");
163 }
164
165 @Test
166 public void fastSearch() throws IOException {
167 FakeMap a = localBuilder.addBoth(1, "A").addBoth(3, "B").addBoth(5, "C").addBoth(7, "D").make();
168
169 // we should be using the cache to find the entry efficiently
170 a.search(6, Direction.ASC).asserts(7,"D");
171 a.search(2, Direction.DESC).asserts(1, "A");
172 }
173
174 @Test
175 public void bogusCache() throws IOException {
176 FakeMap a = localBuilder.addUnloadableCache(1).make();
177 assertNull(a.get(1));
178 }
179
180 @Test
181 public void bogusCacheAndHiddenRealData() throws IOException {
182 FakeMap a = localBuilder.addUnloadableCache(1).add(1, "A").make();
183 a.get(1).asserts(1, "A");
184 }
185
186 @Test
187 public void bogusCache2() throws IOException {
188 FakeMap a = localBuilder.addBogusCache(1,3,"A").make();
189 assertNull(a.get(1));
190 a.get(3).asserts(3,"A");
191 }
192
193 @Test
194 public void incompleteCache() throws IOException {
195 FakeMapBuilder setup = localBuilder.addBoth(1, "A").add(3, "B").addBoth(5, "C");
196
197 // each test uses a fresh map since cache lookup causes additional loads
198 // to verify the results
199
200 // if we just rely on cache,
201 // it'll pick up 5:C as the first ascending value,
202 // but we should be then verifying this by loading B, so in the end we should
203 // find the correct value
204 setup.make().search(2, Direction.ASC).asserts(3,"B");
205 setup.make().search(4, Direction.DESC).asserts(3,"B");
206
207 // variation of the cache based search where we find the outer-most value via cache
208 setup.make().search(0, Direction.ASC).asserts(1,"A");
209 setup.make().search(6, Direction.DESC).asserts(5,"C");
210
211 // variation of the cache search where the cache tells us that we are searching
212 // in the direction that doesn't have any records
213 assertNull(setup.make().search(0, Direction.DESC));
214 assertNull(setup.make().search(6, Direction.ASC));
215 }
216
217 @Test
218 public void fastSubMap() throws Exception {
219 SortedMap<Integer,Build> m = a.subMap(99, 2);
220 assertEquals(2, m.size());
221
222 Build[] b = m.values().toArray(new Build[2]);
223 assertEquals(2, b.length);
224 b[0].asserts(5, "C");
225 b[1].asserts(3, "B");
226 }
227
228 @Test
229 public void identity() {
230 assertTrue(a.equals(a));
231 assertTrue(!a.equals(b));
232 a.hashCode();
233 b.hashCode();
234 }
235
236 @Bug(15439)
237 @Test
238 public void indexOutOfBounds() throws Exception {
239 FakeMapBuilder f = localBuilder;
240 f.add(100,"A")
241 .addUnloadable("B")
242 .addUnloadable("C")
243 .addUnloadable("D")
244 .addUnloadable("E")
245 .addUnloadable("F")
246 .addUnloadable("G")
247 .add(200,"H")
248 .add(201,"I");
249 FakeMap map = f.make();
250
251 Build x = map.search(Integer.MAX_VALUE, Direction.DESC);
252 assert x.n==201;
253 }
254 }
255