forked from bndtools/bnd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RunResolutionTest.java
377 lines (304 loc) · 13.2 KB
/
RunResolutionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package biz.aQute.resolve;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.osgi.resource.Resource;
import org.osgi.resource.Wire;
import aQute.bnd.build.Container;
import aQute.bnd.build.ProjectLauncher;
import aQute.bnd.build.Workspace;
import aQute.bnd.build.model.clauses.VersionedClause;
import aQute.bnd.help.instructions.ResolutionInstructions.Runorder;
import aQute.bnd.osgi.Constants;
import aQute.bnd.result.Result;
import aQute.bnd.test.jupiter.InjectTemporaryDirectory;
import aQute.lib.io.IO;
import biz.aQute.resolve.Bndrun.CacheReason;
public class RunResolutionTest {
Workspace workspace;
@InjectTemporaryDirectory
Path tmp;
Path ws;
@BeforeEach
public void before() throws Exception {
IO.copy(IO.getPath("testdata/enroute"), tmp);
ws = IO.copy(IO.getPath("testdata/pre-buildworkspace"), tmp.resolve("workspace"));
workspace = new Workspace(ws.toFile());
assertThat(workspace).isNotNull();
}
@AfterEach
public void after() {
IO.close(workspace);
}
@Test
public void testSimple() throws Exception {
Bndrun bndrun = Bndrun.createBndrun(workspace, IO.getFile(ws.toFile(), "test.simple/resolve.bndrun"));
String resolve = bndrun.resolve(false, false);
assertThat(bndrun.check()).isTrue();
}
@Test
public void testExcludeSystemResource() throws Exception {
Bndrun bndrun = Bndrun.createBndrun(workspace, IO.getFile(ws.toFile(), "test.simple/resolve.bndrun"));
RunResolution resolve = RunResolution.resolve(bndrun, null);
Set<Resource> noFramework = resolve.getRequired()
.keySet();
bndrun.setProperty(Constants.RESOLVE_EXCLUDESYSTEM, "false");
resolve = RunResolution.resolve(bndrun, null);
Set<Resource> withFramework = new HashSet<>(resolve.getRequired()
.keySet());
withFramework.removeAll(noFramework);
assertThat(withFramework).hasSize(1);
}
@Test
public void testOrdering() throws Exception {
Bndrun bndrun = Bndrun.createBndrun(workspace, IO.getFile("testdata/ordering.bndrun"));
RunResolution resolution = bndrun.resolve();
assertThat(bndrun.check()).isTrue();
RunResolution resolution2 = bndrun.resolve();
assertThat(bndrun.check()).isTrue();
List<Resource> l1 = resolution.getOrderedResources(resolution.getRequired(), Runorder.LEASTDEPENDENCIESFIRST);
List<Resource> l2 = resolution2.getOrderedResources(permutate(resolution.getRequired()),
Runorder.LEASTDEPENDENCIESFIRST);
assertThat(l1).isEqualTo(l2);
}
@Test
public void testCachingOfResult() throws Exception {
Bndrun bndrun = Bndrun.createBndrun(workspace, IO.getFile(tmp.toFile(), "resolver.bndrun"));
bndrun.setProperty("-resolve", "beforelaunch");
bndrun.unsetProperty("-runbundles");
RunResolution.clearCache(bndrun.getWorkspace());
Result<String> nonExistent = RunResolution.getRunBundles(bndrun, false);
assertThat(nonExistent.unwrap()).isEmpty();
Result<String> force = RunResolution.getRunBundles(bndrun, true);
assertThat(force.unwrap()).isNotEmpty();
Result<String> existent = RunResolution.getRunBundles(bndrun, false);
assertThat(existent.unwrap()).isNotEmpty();
System.out.println("Runbundles " + existent.unwrap());
bndrun.setProperty("foo", "bar");
nonExistent = RunResolution.getRunBundles(bndrun, false);
assertThat(nonExistent.unwrap()).isEmpty();
ProjectLauncher pl = bndrun.getProjectLauncher();
assertThat(pl).isNotNull();
Collection<Container> runbundles = pl.getProject()
.getRunbundles();
runbundles.forEach(c -> System.out.println(c.getFile()));
// assertThat(runbundles).hasSize(22);
}
@Test
public void testResolveCachedWithStandalone() throws Exception {
Bndrun bndrun = Bndrun.createBndrun(workspace, IO.getFile(tmp.toFile(), "resolver.bndrun"));
bndrun.setProperty("-resolve", "cache");
Collection<Container> runbundles = bndrun.getRunbundles();
assertThat(bndrun.testReason).isEqualTo(CacheReason.NOT_A_BND_LAYOUT);
}
@Test
public void testResolveCached() throws Exception {
Bndrun bndrun = Bndrun.createBndrun(workspace, IO.getFile(ws.toFile(), "test.simple/resolve.bndrun"));
bndrun.setTrace(true);
File file = bndrun.getPropertiesFile();
assertTrue(bndrun.check());
File cache = bndrun.getCacheFile(file);
System.out.println("get the embedded list of runbundles, this is out benchmark");
bndrun.setProperty("-resolve", "manual");
Collection<Container> manual = bndrun.getRunbundles();
assertThat(manual).hasSize(2);
System.out.println("remove the embedded list and set mode to 'cache'");
bndrun.setProperty("-resolve", "cache");
bndrun.unsetProperty("-runbundles");
assertThat(cache).doesNotExist();
System.out.println("First time we should resolve & create a cache file");
Collection<Container> cached = bndrun.getRunbundles();
assertTrue(bndrun.check());
assertThat(cache).isFile();
assertThat(cached).containsExactlyElementsOf(manual);
assertThat(cache.lastModified()).isGreaterThan(bndrun.lastModified());
assertThat(bndrun.testReason).isEqualTo(CacheReason.NO_CACHE_FILE);
System.out
.println("Second time, the cache file should used, so make it valid but empty ");
long lastModified = cache.lastModified();
IO.store("-runbundles ", cache);
cached = bndrun.getRunbundles();
assertTrue(bndrun.check());
assertThat(cached).isEmpty();
assertThat(bndrun.testReason).isEqualTo(CacheReason.USE_CACHE);
System.out.println("Now make cache invalid, should be ignored");
IO.store("-runbundles is not a valid file", cache);
cached = bndrun.getRunbundles();
assertTrue(bndrun.check());
assertThat(cached).containsExactlyElementsOf(manual);
assertThat(bndrun.testReason).isEqualTo(CacheReason.INVALID_CACHE);
System.out.println("Now empty cache, but still use it");
IO.store("-runbundles ", cache);
cached = bndrun.getRunbundles();
assertTrue(bndrun.check());
assertThat(cached).isEmpty();
assertThat(bndrun.testReason).isEqualTo(CacheReason.USE_CACHE);
System.out.println("Refresh and check we still use the cache");
bndrun.refresh();
bndrun.setProperty("-resolve", "cache");
bndrun.unsetProperty("-runbundles");
cached = bndrun.getRunbundles();
assertThat(cached).isEmpty();
assertThat(bndrun.testReason).isEqualTo(CacheReason.USE_CACHE);
System.out.println("Update an include file, refresh and check we still sue the cache");
File empty = IO.getFile(ws.toFile(), "test.simple/empty-included-in-resolve.bnd");
long now = System.currentTimeMillis();
empty.setLastModified(now);
assertThat(bndrun.lastModified()).isLessThan(now);
bndrun.refresh();
bndrun.setProperty("-resolve", "cache");
bndrun.unsetProperty("-runbundles");
cached = bndrun.getRunbundles();
assertTrue(bndrun.check());
assertThat(cached).containsExactlyElementsOf(manual);
assertThat(bndrun.testReason).isEqualTo(CacheReason.CACHE_STALE_PROJECT);
System.out.println("Next we use the cache");
cached = bndrun.getRunbundles();
assertThat(cached).containsExactlyElementsOf(manual);
assertThat(bndrun.testReason).isEqualTo(CacheReason.USE_CACHE);
Thread.sleep(100);
System.out.println("Update the cnf/build file");
File build = IO.getFile(ws.toFile(), "cnf/build.bnd");
now = System.currentTimeMillis();
build.setLastModified(now);
workspace.refresh();
cached = bndrun.getRunbundles();
assertTrue(bndrun.check());
assertThat(cached).containsExactlyElementsOf(manual);
assertThat(bndrun.testReason).isEqualTo(CacheReason.CACHE_STALE_WORKSPACE);
}
@Test
public void testNotCachingOfResultForOtherResolveOption() throws Exception {
Bndrun bndrun = Bndrun.createBndrun(workspace, IO.getFile(tmp.toFile(), "resolver.bndrun"));
bndrun.setProperty("-resolve", "manual");
bndrun.unsetProperty("-runbundles");
RunResolution.clearCache(bndrun.getWorkspace());
ProjectLauncher pl = bndrun.getProjectLauncher();
assertThat(pl).isNotNull();
Collection<Container> runbundles = pl.getProject()
.getRunbundles();
assertThat(runbundles).isEmpty();
// The repo used is an XML and the URLs are not found when downloaded in
// the background. Sometimes they're in,
// sometimes not. This is valid since the Container will be error.
if (!bndrun.isPerfect()) {
assertThat(bndrun.check("Download java.io.FileNotFoundException:")).isTrue();
}
}
@Test
public void testLaunchWithBeforeLaunchResolve() throws Exception {
Bndrun bndrun = Bndrun.createBndrun(workspace, IO.getFile(tmp.toFile(), "resolver.bndrun"));
bndrun.setProperty("-resolve", "beforelaunch");
bndrun.unsetProperty("-runbundles");
RunResolution.clearCache(bndrun.getWorkspace());
ProjectLauncher pl = bndrun.getProjectLauncher();
pl.setCwd(tmp.toFile());
assertThat(pl).isNotNull();
Collection<Container> runbundles = pl.getProject()
.getRunbundles();
}
private Map<Resource, List<Wire>> permutate(Map<Resource, List<Wire>> required) {
TreeMap<Resource, List<Wire>> map = new TreeMap<>(required);
map.entrySet()
.forEach(e -> Collections.shuffle(e.getValue()));
return map;
}
@Test
public void testUpdateBundles() throws Exception {
Bndrun bndrun = Bndrun.createBndrun(workspace, IO.getFile(ws.toFile(), "test.simple/resolve.bndrun"));
RunResolution resolution = bndrun.resolve();
assertThat(bndrun.check()).isTrue();
assertThat(resolution.updateBundles(bndrun.getModel())).isFalse();
bndrun.getModel()
.setRunBundles(Collections.emptyList());
assertThat(resolution.updateBundles(bndrun.getModel())).isTrue();
}
@Test
public void testStartLevelsLeastDependenciesFirst() throws Exception {
Bndrun bndrun = Bndrun.createBndrun(workspace, IO.getFile(ws.toFile(), "test.simple/resolve.bndrun"));
bndrun.setProperty("-runstartlevel", "order=leastdependenciesfirst,begin=100,step=10");
RunResolution resolution = bndrun.resolve();
assertThat(bndrun.check()).isTrue();
List<VersionedClause> runBundles = resolution.getRunBundles();
assertThat(runBundles).hasSize(2);
assertThat(runBundles.get(0)
.getName()).isEqualTo("osgi.enroute.junit.wrapper");
assertThat(runBundles.get(0)
.getAttribs()).containsEntry(Constants.RUNBUNDLES_STARTLEVEL_ATTRIBUTE, "100");
assertThat(runBundles.get(1)
.getName()).isEqualTo("test.simple");
assertThat(runBundles.get(1)
.getAttribs()).containsEntry(Constants.RUNBUNDLES_STARTLEVEL_ATTRIBUTE, "110");
}
@Test
public void testStartLevelsLeastDependenciesLast() throws Exception {
Bndrun bndrun = Bndrun.createBndrun(workspace, IO.getFile(ws.toFile(), "test.simple/resolve.bndrun"));
bndrun.setProperty("-runstartlevel", "order=leastdependencieslast,begin=100,step=10");
RunResolution resolution = bndrun.resolve();
assertThat(bndrun.check()).isTrue();
List<VersionedClause> runBundles = resolution.getRunBundles();
assertThat(runBundles).hasSize(2);
assertThat(runBundles.get(0)
.getName()).isEqualTo("test.simple");
assertThat(runBundles.get(0)
.getAttribs()).containsEntry(Constants.RUNBUNDLES_STARTLEVEL_ATTRIBUTE, "100");
assertThat(runBundles.get(1)
.getName()).isEqualTo("osgi.enroute.junit.wrapper");
assertThat(runBundles.get(1)
.getAttribs()).containsEntry(Constants.RUNBUNDLES_STARTLEVEL_ATTRIBUTE, "110");
}
@Test
public void testStartLevelsStep() throws Exception {
Bndrun bndrun = Bndrun.createBndrun(workspace, IO.getFile(ws.toFile(), "test.simple/resolve.bndrun"));
bndrun.setProperty("-runstartlevel", "order=random,begin=10,step=1");
RunResolution resolution = bndrun.resolve();
assertThat(bndrun.check()).isTrue();
List<VersionedClause> runBundles = resolution.getRunBundles();
assertThat(runBundles).hasSize(2);
assertThat(runBundles.get(0)
.getAttribs()).containsEntry(Constants.RUNBUNDLES_STARTLEVEL_ATTRIBUTE, "10");
assertThat(runBundles.get(1)
.getAttribs()).containsEntry(Constants.RUNBUNDLES_STARTLEVEL_ATTRIBUTE, "11");
}
@Test
public void testNoStartLevels() throws Exception {
Bndrun bndrun = Bndrun.createBndrun(workspace, IO.getFile(ws.toFile(), "test.simple/resolve.bndrun"));
RunResolution resolution = bndrun.resolve();
assertThat(bndrun.check()).isTrue();
List<VersionedClause> runBundles = resolution.getRunBundles();
assertThat(runBundles).hasSize(2);
assertThat(runBundles.get(0)
.getAttribs()).doesNotContainKey(Constants.RUNBUNDLES_STARTLEVEL_ATTRIBUTE);
assertThat(runBundles.get(1)
.getAttribs()).doesNotContainKey(Constants.RUNBUNDLES_STARTLEVEL_ATTRIBUTE);
}
@Test
public void testFailOnChanges() throws Exception {
Bndrun bndrun = Bndrun.createBndrun(workspace, IO.getFile(ws.toFile(), "test.simple/resolve.bndrun"));
// First do not fail on changes
bndrun.getModel()
.setRunBundles(Collections.emptyList());
String resolution = bndrun.resolve(false, false);
assertThat(bndrun.check()).isTrue();
// Now fail on changes
bndrun.getModel()
.setRunBundles(Collections.emptyList());
resolution = bndrun.resolve(true, false);
assertThat(bndrun.check("Fail on changes set to ", "Existing runbundles \\[\\]", "Calculated runbundles"))
.isTrue();
// Now succeed because there are no changes
resolution = bndrun.resolve(false, false);
assertThat(bndrun.check()).isTrue();
}
}