Debian Codegraph @

src/test/java/ch/grengine/load/LayeredClassLoaderMatrixTest.java

raw symbols SCIP
1 /*
2 Copyright 2014-now by Alain Stalder. Made in Switzerland.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 https://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package ch.grengine.load;
18
19 import ch.grengine.TestUtil;
20 import ch.grengine.code.Code;
21 import ch.grengine.code.groovy.DefaultGroovyCompiler;
22 import ch.grengine.except.LoadException;
23 import ch.grengine.source.DefaultTextSource;
24 import ch.grengine.source.MockFile;
25 import ch.grengine.source.MockFileSource;
26 import ch.grengine.source.Source;
27 import ch.grengine.source.SourceUtil;
28 import ch.grengine.sources.Sources;
29 import ch.grengine.sources.SourcesUtil;
30
31 import java.io.File;
32 import java.util.Arrays;
33 import java.util.LinkedList;
34 import java.util.List;
35 import java.util.Set;
36
37 import org.junit.jupiter.api.Test;
38
39 import static ch.grengine.load.LayeredClassLoaderMatrixTest.CodeLayersType.CODE_LAYERS_CURRENT_FIRST;
40 import static ch.grengine.load.LayeredClassLoaderMatrixTest.CodeLayersType.CODE_LAYERS_PARENT_FIRST;
41 import static ch.grengine.load.LayeredClassLoaderMatrixTest.ParentClassLoaderType.PARENT_IS_REGULAR_CLASS_LOADER;
42 import static ch.grengine.load.LayeredClassLoaderMatrixTest.ParentClassLoaderType.PARENT_IS_SOURCE_CLASS_LOADER;
43 import static ch.grengine.load.LayeredClassLoaderMatrixTest.SourcesChangedState.SOURCES_CHANGED;
44 import static ch.grengine.load.LayeredClassLoaderMatrixTest.SourcesChangedState.SOURCES_UNCHANGED;
45 import static ch.grengine.load.LayeredClassLoaderMatrixTest.TopCodeCacheType.TOP_CODE_CACHE_CURRENT_FIRST;
46 import static ch.grengine.load.LayeredClassLoaderMatrixTest.TopCodeCacheType.TOP_CODE_CACHE_OFF;
47 import static ch.grengine.load.LayeredClassLoaderMatrixTest.TopCodeCacheType.TOP_CODE_CACHE_PARENT_FIRST;
48 import static org.hamcrest.CoreMatchers.is;
49 import static org.hamcrest.CoreMatchers.not;
50 import static org.hamcrest.CoreMatchers.notNullValue;
51 import static org.hamcrest.CoreMatchers.nullValue;
52 import static org.hamcrest.CoreMatchers.sameInstance;
53 import static org.hamcrest.MatcherAssert.assertThat;
54
55
56 class LayeredClassLoaderMatrixTest {
57
58 // whether the parent class loader of the layered class loader to test
59 // is a source class loader or not
60 enum ParentClassLoaderType {
61 PARENT_IS_SOURCE_CLASS_LOADER,
62 PARENT_IS_REGULAR_CLASS_LOADER
63 }
64
65 // current first: try to load from top code layer first, down to parent
66 // parent first: try to load from parent first, up to top code layer
67 enum CodeLayersType {
68 CODE_LAYERS_CURRENT_FIRST,
69 CODE_LAYERS_PARENT_FIRST
70 }
71
72 // off: no top code cache, try to load directly from code layers / parent
73 // current first: try to load first from top code cache, then from code layers / parent
74 // parent first: try to load first from code layers / parent, then from top code cache
75 enum TopCodeCacheType {
76 TOP_CODE_CACHE_OFF,
77 TOP_CODE_CACHE_CURRENT_FIRST,
78 TOP_CODE_CACHE_PARENT_FIRST
79 }
80
81 // whether sources have changed their state (modification date)
82 // after compiling code layers; if yes, they are loaded from top code cache
83 // instead of from layers, depending on other settings
84 enum SourcesChangedState {
85 SOURCES_CHANGED,
86 SOURCES_UNCHANGED
87 }
88
89
90 static class TestContext {
91
92 ParentClassLoaderType parentClassLoaderType;
93 CodeLayersType codeLayersType;
94 TopCodeCacheType topCodeCacheType;
95 SourcesChangedState sourcesChangedState;
96
97 MockFile fMain;
98 Source sMain;
99 MockFile fExpando;
100 Source sExpando;
101 Source sNotInCodeLayers;
102 Code codeParent;
103 List<Code> codeLayers;
104
105 ClassLoader parent;
106 LoadMode layerLoadMode;
107 boolean isWithTopLoadMode;
108 TopCodeCache topCodeCache;
109 LoadMode topLoadMode;
110
111 LayeredClassLoader.Builder builder;
112 }
113
114 private void testGeneric(final ParentClassLoaderType parentClassLoaderType,
115 final CodeLayersType codeLayersType, final TopCodeCacheType topCodeCacheType,
116 final SourcesChangedState sourcesChangedState) throws Exception {
117
118 TestContext ctx = new TestContext();
119 ctx.parentClassLoaderType = parentClassLoaderType;
120 ctx.codeLayersType = codeLayersType;
121 ctx.topCodeCacheType = topCodeCacheType;
122 ctx.sourcesChangedState = sourcesChangedState;
123
124 prepareCode(ctx);
125 prepareBuilder(ctx);
126
127 testFindBytecodeClassLoaderBySource(ctx);
128 testLoadMainClassBySource(ctx);
129 testLoadClassBySourceAndName(ctx);
130 testLoadClassByName(ctx);
131 }
132
133 private void prepareCode(final TestContext ctx) throws Exception {
134 final File dir = TestUtil.createTestDir();
135 ctx.fMain = new MockFile(dir, "Main.groovy");
136 ctx.sMain = new MockFileSource(ctx.fMain);
137 ctx.fExpando = new MockFile(dir, "Expando.groovy");
138 ctx.sExpando = new MockFileSource(ctx.fExpando);
139 ctx.sNotInCodeLayers = new DefaultTextSource("class NotInCodeLayers {}");
140 final Set<Source> sourceSet = SourceUtil.sourceArrayToSourceSet(ctx.sMain, ctx.sExpando);
141 final Sources sources = SourcesUtil.sourceSetToSources(sourceSet, "test");
142
143 // code parent class loader if source class loader
144 TestUtil.setFileText(ctx.fMain, "class Main { def methodParent() {} }\nclass Side { def methodParent() {} }");
145 TestUtil.setFileText(ctx.fExpando, "package groovy.util\nclass Expando { def methodParent() {} }");
146 ctx.codeParent = new DefaultGroovyCompiler().compile(sources);
147
148 // code layer 0
149 TestUtil.setFileText(ctx.fMain, "class Main { def methodLayer0() {} }\nclass Side { def methodLayer0() {} }");
150 TestUtil.setFileText(ctx.fExpando, "package groovy.util\nclass Expando { def methodLayer0() {} }");
151 final Code codeLayer0 = new DefaultGroovyCompiler().compile(sources);
152
153 // code layer 1
154 TestUtil.setFileText(ctx.fMain, "class Main { def methodLayer1() {} }\nclass Side { def methodLayer1() {} }");
155 TestUtil.setFileText(ctx.fExpando, "package groovy.util\nclass Expando { def methodLayer1() {} }");
156 final Code codeLayer1 = new DefaultGroovyCompiler().compile(sources);
157
158 ctx.codeLayers = Arrays.asList(codeLayer0, codeLayer1);
159
160 // prepare files for top code cache
161 TestUtil.setFileText(ctx.fMain, "class Main { def methodTop() {} }\nclass Side { def methodTop() {} }");
162 TestUtil.setFileText(ctx.fExpando, "package groovy.util\nclass Expando { def methodTop() {} }");
163
164 if (ctx.sourcesChangedState == SOURCES_CHANGED) {
165 assertThat(ctx.fMain.setLastModified(100), is(true));
166 assertThat(ctx.fExpando.setLastModified(100), is(true));
167 }
168 }
169
170 private void prepareBuilder(final TestContext ctx) {
171
172 switch (ctx.parentClassLoaderType) {
173 case PARENT_IS_SOURCE_CLASS_LOADER:
174 ctx.parent = new BytecodeClassLoader(Thread.currentThread().getContextClassLoader(),
175 LoadMode.CURRENT_FIRST, ctx.codeParent);
176 break;
177 case PARENT_IS_REGULAR_CLASS_LOADER:
178 ctx.parent = Thread.currentThread().getContextClassLoader();
179 break;
180 }
181
182 switch (ctx.codeLayersType) {
183 case CODE_LAYERS_CURRENT_FIRST:
184 ctx.layerLoadMode = LoadMode.CURRENT_FIRST;
185 break;
186 case CODE_LAYERS_PARENT_FIRST:
187 ctx.layerLoadMode = LoadMode.PARENT_FIRST;
188 break;
189 }
190
191 switch (ctx.topCodeCacheType) {
192 case TOP_CODE_CACHE_OFF:
193 ctx.isWithTopLoadMode = false;
194 ctx.topCodeCache = null;
195 ctx.topLoadMode = null;
196 break;
197 case TOP_CODE_CACHE_CURRENT_FIRST:
198 ctx.isWithTopLoadMode = true;
199 ctx.topCodeCache = new DefaultTopCodeCache.Builder(ctx.parent).build();
200 ctx.topLoadMode = LoadMode.CURRENT_FIRST;
201 break;
202 case TOP_CODE_CACHE_PARENT_FIRST:
203 ctx.isWithTopLoadMode = true;
204 ctx.topCodeCache = new DefaultTopCodeCache.Builder(ctx.parent).build();
205 ctx.topLoadMode = LoadMode.PARENT_FIRST;
206 break;
207 }
208
209 ctx.builder = new LayeredClassLoader.Builder()
210 .setParent(ctx.parent)
211 .setLoadMode(ctx.layerLoadMode)
212 .setCodeLayers(ctx.codeLayers)
213 .setWithTopCodeCache(ctx.isWithTopLoadMode, ctx.topCodeCache)
214 .setTopLoadMode(ctx.topLoadMode);
215 }
216
217 private static String getExpectedMethod(final TestContext ctx, final boolean loadBySource) {
218 if (ctx.topCodeCacheType == TOP_CODE_CACHE_CURRENT_FIRST
219 && ctx.sourcesChangedState == SOURCES_CHANGED
220 && loadBySource) {
221 return "methodTop";
222 }
223 switch (ctx.codeLayersType) {
224 case CODE_LAYERS_CURRENT_FIRST:
225 return "methodLayer1";
226 case CODE_LAYERS_PARENT_FIRST:
227 switch (ctx.parentClassLoaderType) {
228 case PARENT_IS_REGULAR_CLASS_LOADER:
229 return "methodLayer0";
230 case PARENT_IS_SOURCE_CLASS_LOADER:
231 return "methodParent";
232 }
233 }
234 throw new RuntimeException("never gets here");
235 }
236
237 private void testFindBytecodeClassLoaderBySource(final TestContext ctx) {
238
239 // given
240
241 final LayeredClassLoader loader = ctx.builder.buildFromCodeLayers();
242
243 final boolean expectParentClassLoader =
244 ctx.parentClassLoaderType == PARENT_IS_SOURCE_CLASS_LOADER &&
245 ctx.codeLayersType == CODE_LAYERS_PARENT_FIRST;
246
247 // when
248
249 BytecodeClassLoader loaderFound = loader.findBytecodeClassLoaderBySource(ctx.sMain);
250
251 // then
252
253 assertThat(loaderFound, is(notNullValue()));
254 if (expectParentClassLoader) {
255 assertThat(loaderFound, sameInstance(ctx.parent));
256 } else {
257 assertThat(loaderFound, not(sameInstance(ctx.parent)));
258 }
259
260 // when
261
262 loaderFound = loader.findBytecodeClassLoaderBySource(ctx.sExpando);
263
264 // then
265
266 assertThat(loaderFound, is(notNullValue()));
267 if (expectParentClassLoader) {
268 assertThat(loaderFound, sameInstance(ctx.parent));
269 } else {
270 assertThat(loaderFound, not(sameInstance(ctx.parent)));
271 }
272
273 // when
274
275 loaderFound = loader.findBytecodeClassLoaderBySource(ctx.sNotInCodeLayers);
276
277 // then
278
279 assertThat(loaderFound, is(nullValue()));
280 }
281
282 private void testLoadMainClassBySource(final TestContext ctx) throws Exception {
283
284 // given
285
286 final LayeredClassLoader loader = ctx.builder.buildFromCodeLayers();
287 final String expectedMethod = getExpectedMethod(ctx, true);
288 final boolean expectLoadIfNotInCodeLayers = ctx.topCodeCacheType != TOP_CODE_CACHE_OFF;
289
290 // when
291
292 Class<?> clazz = loader.loadMainClass(ctx.sMain);
293
294 // then
295
296 assertThat(clazz.getName(), is("Main"));
297 clazz.getDeclaredMethod(expectedMethod);
298
299 // when
300
301 clazz = loader.loadMainClass(ctx.sMain);
302
303 // then
304
305 assertThat(clazz.getName(), is("Main"));
306 clazz.getDeclaredMethod(expectedMethod);
307
308 // when
309
310 if (ctx.topCodeCache != null) {
311 ctx.topCodeCache.clear();
312 }
313 clazz = loader.loadMainClass(ctx.sMain);
314
315 // then
316
317 assertThat(clazz.getName(), is("Main"));
318 clazz.getDeclaredMethod(expectedMethod);
319
320 // when
321
322 clazz = loader.loadMainClass(ctx.sExpando);
323
324 // when
325
326 assertThat(clazz.getName(), is("groovy.util.Expando"));
327 clazz.getDeclaredMethod(expectedMethod);
328
329 // when/then
330
331 if (expectLoadIfNotInCodeLayers) {
332 assertThat(loader.loadMainClass(ctx.sNotInCodeLayers), notNullValue());
333 } else {
334 TestUtil.assertThrowsMessageStartsWith(LoadException.class,
335 () -> loader.loadMainClass(ctx.sNotInCodeLayers),
336 "Source not found: ");
337 }
338
339 }
340
341 private void testLoadClassBySourceAndName(final TestContext ctx) throws Exception {
342
343 // given
344
345 final LayeredClassLoader loader = ctx.builder.buildFromCodeLayers();
346 final String expectedMethod = getExpectedMethod(ctx, true);
347 final boolean expectLoadIfNotInCodeLayers = ctx.topCodeCacheType != TOP_CODE_CACHE_OFF;
348
349 // when
350
351 Class<?> clazz = loader.loadClass(ctx.sMain, "Main");
352
353 // then
354
355 assertThat(clazz.getName(), is("Main"));
356 clazz.getDeclaredMethod(expectedMethod);
357
358 // when
359
360 clazz = loader.loadClass(ctx.sMain, "Main");
361
362 // then
363
364 assertThat(clazz.getName(), is("Main"));
365 clazz.getDeclaredMethod(expectedMethod);
366
367 // when
368
369 clazz = loader.loadClass(ctx.sMain, "Side");
370
371 // then
372
373 assertThat(clazz.getName(), is("Side"));
374 clazz.getDeclaredMethod(expectedMethod);
375
376
377 // when/then (wrong source, not found)
378 TestUtil.assertThrowsMessageStartsWith(LoadException.class,
379 () -> loader.loadClass(ctx.sMain, "groovy.util.Expando"),
380 "Class 'groovy.util.Expando' not found for source. Source: " + ctx.sMain.toString());
381
382 // when (correct source)
383
384 clazz = loader.loadClass(ctx.sExpando, "groovy.util.Expando");
385
386 // then
387
388 assertThat(clazz.getName(), is("groovy.util.Expando"));
389 clazz.getDeclaredMethod(expectedMethod);
390
391 // when/then
392
393 if (expectLoadIfNotInCodeLayers) {
394 assertThat(loader.loadClass(ctx.sNotInCodeLayers, "NotInCodeLayers"), notNullValue());
395 } else {
396 TestUtil.assertThrowsMessageStartsWith(LoadException.class,
397 () -> loader.loadClass(ctx.sNotInCodeLayers, "NotInCodeLayers"),
398 "Source not found: ");
399 }
400
401 }
402
403 private void testLoadClassByName(final TestContext ctx) throws Exception {
404
405 // given
406
407 final LayeredClassLoader loader = ctx.builder.buildFromCodeLayers();
408 final String expectedMethod = getExpectedMethod(ctx, false);
409 final boolean expectedLoadGroovyUtilExpando =
410 ctx.codeLayersType == CODE_LAYERS_PARENT_FIRST
411 && ctx.parentClassLoaderType == PARENT_IS_REGULAR_CLASS_LOADER;
412 final String expectedExpandoMethod = expectedLoadGroovyUtilExpando ?
413 "createMap" : expectedMethod;
414
415
416 // when
417
418 Class<?> clazz = loader.loadClass("Main");
419
420 // then
421
422 assertThat(clazz.getName(), is("Main"));
423 clazz.getDeclaredMethod(expectedMethod);
424
425 // when
426
427 clazz = loader.loadClass("Side");
428
429 // then
430
431 assertThat(clazz.getName(), is("Side"));
432 clazz.getDeclaredMethod(expectedMethod);
433
434 // when
435
436 clazz = loader.loadClass("groovy.util.Expando");
437
438 // then
439
440 clazz.getDeclaredMethod(expectedExpandoMethod);
441
442 // when/then
443
444 TestUtil.assertThrowsMessageStartsWith(ClassNotFoundException.class,
445 () -> loader.loadClass("NotInCodeLayers"),
446 "NotInCodeLayers");
447
448
449 // when/then (extra: load class with resolve (protected method))
450
451 loader.loadClass("Main", true);
452 }
453
454
455
456 // top off, top sources changed
457 @Test
458 void testParentRegularClassLoader_LayersParentFirst_TopOff_SourcesChanged() throws Exception {
459 testGeneric(PARENT_IS_REGULAR_CLASS_LOADER, CODE_LAYERS_PARENT_FIRST,
460 TOP_CODE_CACHE_OFF, SOURCES_CHANGED);
461 }
462 @Test
463 void testParentSourceClassLoader_LayersParentFirst_TopOff_SourcesChanged() throws Exception {
464 testGeneric(PARENT_IS_SOURCE_CLASS_LOADER, CODE_LAYERS_PARENT_FIRST,
465 TOP_CODE_CACHE_OFF, SOURCES_CHANGED);
466 }
467 @Test
468 void testParentRegularClassLoader_LayersCurrentFirst_TopOff_SourcesChanged() throws Exception {
469 testGeneric(PARENT_IS_REGULAR_CLASS_LOADER, CODE_LAYERS_CURRENT_FIRST,
470 TOP_CODE_CACHE_OFF, SOURCES_CHANGED);
471 }
472 @Test
473 void testParentSourceClassLoader_LayersCurrentFirst_TopOff_SourcesChanged() throws Exception {
474 testGeneric(PARENT_IS_SOURCE_CLASS_LOADER, CODE_LAYERS_CURRENT_FIRST,
475 TOP_CODE_CACHE_OFF, SOURCES_CHANGED);
476 }
477
478 // top parent first, top sources changed
479 @Test
480 void testParentRegularClassLoader_LayersParentFirst_TopParentFirst_SourcesChanged() throws Exception {
481 testGeneric(PARENT_IS_REGULAR_CLASS_LOADER, CODE_LAYERS_PARENT_FIRST,
482 TOP_CODE_CACHE_PARENT_FIRST, SOURCES_CHANGED);
483 }
484 @Test
485 void testParentSourceClassLoader_LayersParentFirst_TopParentFirst_SourcesChanged() throws Exception {
486 testGeneric(PARENT_IS_SOURCE_CLASS_LOADER, CODE_LAYERS_PARENT_FIRST,
487 TOP_CODE_CACHE_PARENT_FIRST, SOURCES_CHANGED);
488 }
489 @Test
490 void testParentRegularClassLoader_LayersCurrentFirst_TopParentFirst_SourcesChanged() throws Exception {
491 testGeneric(PARENT_IS_REGULAR_CLASS_LOADER, CODE_LAYERS_CURRENT_FIRST,
492 TOP_CODE_CACHE_PARENT_FIRST, SOURCES_CHANGED);
493 }
494 @Test
495 void testParentSourceClassLoader_LayersCurrentFirst_TopParentFirst_SourcesChanged() throws Exception {
496 testGeneric(PARENT_IS_SOURCE_CLASS_LOADER, CODE_LAYERS_CURRENT_FIRST,
497 TOP_CODE_CACHE_PARENT_FIRST, SOURCES_CHANGED);
498 }
499
500 // top current first, top sources changed
501 @Test
502 void testParentRegularClassLoader_LayersParentFirst_TopCurrentFirst_SourcesChanged() throws Exception {
503 testGeneric(PARENT_IS_REGULAR_CLASS_LOADER, CODE_LAYERS_PARENT_FIRST,
504 TOP_CODE_CACHE_CURRENT_FIRST, SOURCES_CHANGED);
505 }
506 @Test
507 void testParentSourceClassLoader_LayersParentFirst_TopCurrentFirst_SourcesChanged() throws Exception {
508 testGeneric(PARENT_IS_SOURCE_CLASS_LOADER, CODE_LAYERS_PARENT_FIRST,
509 TOP_CODE_CACHE_CURRENT_FIRST, SOURCES_CHANGED);
510 }
511 @Test
512 void testParentRegularClassLoader_LayersCurrentFirst_TopCurrentFirst_SourcesChanged() throws Exception {
513 testGeneric(PARENT_IS_REGULAR_CLASS_LOADER, CODE_LAYERS_CURRENT_FIRST,
514 TOP_CODE_CACHE_CURRENT_FIRST, SOURCES_CHANGED);
515 }
516 @Test
517 void testParentSourceClassLoader_LayersCurrentFirst_TopCurrentFirst_SourcesChanged() throws Exception {
518 testGeneric(PARENT_IS_SOURCE_CLASS_LOADER, CODE_LAYERS_CURRENT_FIRST,
519 TOP_CODE_CACHE_CURRENT_FIRST, SOURCES_CHANGED);
520 }
521
522
523 // top off, top sources unchanged
524 @Test
525 void testParentRegularClassLoader_LayersParentFirst_TopOff_SourcesUnchanged() throws Exception {
526 testGeneric(PARENT_IS_REGULAR_CLASS_LOADER, CODE_LAYERS_PARENT_FIRST,
527 TOP_CODE_CACHE_OFF, SOURCES_UNCHANGED);
528 }
529 @Test
530 void testParentSourceClassLoader_LayersParentFirst_TopOff_SourcesUnchanged() throws Exception {
531 testGeneric(PARENT_IS_SOURCE_CLASS_LOADER, CODE_LAYERS_PARENT_FIRST,
532 TOP_CODE_CACHE_OFF, SOURCES_UNCHANGED);
533 }
534 @Test
535 void testParentRegularClassLoader_LayersCurrentFirst_TopOff_SourcesUnchanged() throws Exception {
536 testGeneric(PARENT_IS_REGULAR_CLASS_LOADER, CODE_LAYERS_CURRENT_FIRST,
537 TOP_CODE_CACHE_OFF, SOURCES_UNCHANGED);
538 }
539 @Test
540 void testParentSourceClassLoader_LayersCurrentFirst_TopOff_SourcesUnchanged() throws Exception {
541 testGeneric(PARENT_IS_SOURCE_CLASS_LOADER, CODE_LAYERS_CURRENT_FIRST,
542 TOP_CODE_CACHE_OFF, SOURCES_UNCHANGED);
543 }
544
545 // top parent first, top sources unchanged
546 @Test
547 void testParentRegularClassLoader_LayersParentFirst_TopParentFirst_SourcesUnchanged() throws Exception {
548 testGeneric(PARENT_IS_REGULAR_CLASS_LOADER, CODE_LAYERS_PARENT_FIRST,
549 TOP_CODE_CACHE_PARENT_FIRST, SOURCES_UNCHANGED);
550 }
551 @Test
552 void testParentSourceClassLoader_LayersParentFirst_TopParentFirst_SourcesUnchanged() throws Exception {
553 testGeneric(PARENT_IS_SOURCE_CLASS_LOADER, CODE_LAYERS_PARENT_FIRST,
554 TOP_CODE_CACHE_PARENT_FIRST, SOURCES_UNCHANGED);
555 }
556 @Test
557 void testParentRegularClassLoader_LayersCurrentFirst_TopParentFirst_SourcesUnchanged() throws Exception {
558 testGeneric(PARENT_IS_REGULAR_CLASS_LOADER, CODE_LAYERS_CURRENT_FIRST,
559 TOP_CODE_CACHE_PARENT_FIRST, SOURCES_UNCHANGED);
560 }
561 @Test
562 void testParentSourceClassLoader_LayersCurrentFirst_TopParentFirst_SourcesUnchanged() throws Exception {
563 testGeneric(PARENT_IS_SOURCE_CLASS_LOADER, CODE_LAYERS_CURRENT_FIRST,
564 TOP_CODE_CACHE_PARENT_FIRST, SOURCES_UNCHANGED);
565 }
566
567 // top current first, top sources unchanged
568 @Test
569 void testParentRegularClassLoader_LayersParentFirst_TopCurrentFirst_SourcesUnchanged() throws Exception {
570 testGeneric(PARENT_IS_REGULAR_CLASS_LOADER, CODE_LAYERS_PARENT_FIRST,
571 TOP_CODE_CACHE_CURRENT_FIRST, SOURCES_UNCHANGED);
572 }
573 @Test
574 void testParentSourceClassLoader_LayersParentFirst_TopCurrentFirst_SourcesUnchanged() throws Exception {
575 testGeneric(PARENT_IS_SOURCE_CLASS_LOADER, CODE_LAYERS_PARENT_FIRST,
576 TOP_CODE_CACHE_CURRENT_FIRST, SOURCES_UNCHANGED);
577 }
578 @Test
579 void testParentRegularClassLoader_LayersCurrentFirst_TopCurrentFirst_SourcesUnchanged() throws Exception {
580 testGeneric(PARENT_IS_REGULAR_CLASS_LOADER, CODE_LAYERS_CURRENT_FIRST,
581 TOP_CODE_CACHE_CURRENT_FIRST, SOURCES_UNCHANGED);
582 }
583 @Test
584 void testParentSourceClassLoader_LayersCurrentFirst_TopCurrentFirst_SourcesUnchanged() throws Exception {
585 testGeneric(PARENT_IS_SOURCE_CLASS_LOADER, CODE_LAYERS_CURRENT_FIRST,
586 TOP_CODE_CACHE_CURRENT_FIRST, SOURCES_UNCHANGED);
587 }
588
589
590 @Test
591 void testExtraNoCodeLayers_ParentRegularClassLoader_LayersParentFirst_TopOff_SourcesChanged() throws Exception {
592
593 // given
594
595 final TestContext ctx = new TestContext();
596 ctx.parentClassLoaderType = PARENT_IS_REGULAR_CLASS_LOADER;
597 ctx.codeLayersType = CODE_LAYERS_PARENT_FIRST;
598 ctx.topCodeCacheType = TOP_CODE_CACHE_OFF;
599 ctx.sourcesChangedState = SOURCES_CHANGED;
600
601 prepareCode(ctx);
602 ctx.codeLayers = new LinkedList<>();
603 prepareBuilder(ctx);
604
605 final LayeredClassLoader loader = ctx.builder.buildFromCodeLayers();
606
607 // when/then (findBytecodeClassLoaderBySource(source))
608
609 BytecodeClassLoader loaderFound = loader.findBytecodeClassLoaderBySource(ctx.sMain);
610 assertThat(loaderFound, is(nullValue()));
611 loaderFound = loader.findBytecodeClassLoaderBySource(ctx.sExpando);
612 assertThat(loaderFound, is(nullValue()));
613 loaderFound = loader.findBytecodeClassLoaderBySource(ctx.sNotInCodeLayers);
614 assertThat(loaderFound, is(nullValue()));
615 }
616
617 @Test
618 void testExtraSourcesChangedTop_ParentSourceClassLoader_LayersParentFirst_TopCurrentFirst_SourcesChanged() throws Exception {
619
620 // given
621
622 final TestContext ctx = new TestContext();
623 ctx.parentClassLoaderType = PARENT_IS_SOURCE_CLASS_LOADER;
624 ctx.codeLayersType = CODE_LAYERS_PARENT_FIRST;
625 ctx.topCodeCacheType = TOP_CODE_CACHE_CURRENT_FIRST;
626 ctx.sourcesChangedState = SOURCES_CHANGED;
627
628 prepareCode(ctx);
629 prepareBuilder(ctx);
630
631 final LayeredClassLoader loader1 = ctx.builder.buildFromCodeLayers();
632
633 // when/then (loadMainClass(source))
634
635 Class<?> clazz = loader1.loadMainClass(ctx.sMain);
636 assertThat(clazz.getName(), is("Main"));
637 clazz.getDeclaredMethod("methodTop");
638
639 assertThat(ctx.fMain.setLastModified(55555), is(true));
640
641 clazz = loader1.loadMainClass(ctx.sMain);
642 assertThat(clazz.getName(), is("Main"));
643 clazz.getDeclaredMethod("methodTop");
644
645 // when/then (loadClass(source, name))
646
647 final LayeredClassLoader loader2 = ctx.builder.buildFromCodeLayers();
648
649 clazz = loader2.loadClass(ctx.sMain, "Side");
650 assertThat(clazz.getName(), is("Side"));
651 clazz.getDeclaredMethod("methodTop");
652
653 assertThat(ctx.fMain.setLastModified(77777), is(true));
654
655 clazz = loader2.loadClass(ctx.sMain, "Side");
656 assertThat(clazz.getName(), is("Side"));
657 clazz.getDeclaredMethod("methodTop");
658 }
659
660 }