|
| 1 | +/* |
| 2 | + * Copyright 2023 Flamingock (https://www.flamingock.io) |
| 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 | + * http://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 | +package io.flamingock.internal.core.context; |
| 17 | + |
| 18 | +import io.flamingock.internal.common.core.context.Dependency; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import java.io.InputStream; |
| 22 | +import java.net.URL; |
| 23 | + |
| 24 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 26 | + |
| 27 | +/** |
| 28 | + * Reproduces https://github.com/flamingock/flamingock-java/issues/951 |
| 29 | + * <p> |
| 30 | + * Simulates what Spring Boot DevTools' RestartClassLoader does: the same |
| 31 | + * fully-qualified class is loaded twice by two different classloaders, |
| 32 | + * producing two distinct {@code Class<?>} instances with the same name. |
| 33 | + * SimpleContext keys its dependency map by exact {@code Class<?>} identity, |
| 34 | + * so a dependency registered under one loader's Class instance is not found |
| 35 | + * when looked up with the other loader's Class instance. |
| 36 | + */ |
| 37 | +class ClassloaderMismatchReproTest { |
| 38 | + |
| 39 | + private static final String TARGET_CLASS = "io.flamingock.internal.core.context.repro.MigrationConfiguration"; |
| 40 | + |
| 41 | + @Test |
| 42 | + void dependencyRegisteredUnderOneClassloaderIsNotFoundUnderAnother() throws Exception { |
| 43 | + ClassLoader appLoader = ClassloaderMismatchReproTest.class.getClassLoader(); |
| 44 | + Class<?> typeFromRegistration = loadIsolated(appLoader).loadClass(TARGET_CLASS); |
| 45 | + Class<?> typeFromLookup = loadIsolated(appLoader).loadClass(TARGET_CLASS); |
| 46 | + |
| 47 | + assertFalse(typeFromRegistration.equals(typeFromLookup), |
| 48 | + "precondition: the two loaders must produce distinct Class instances"); |
| 49 | + |
| 50 | + SimpleContext context = new SimpleContext(); |
| 51 | + Object instance = typeFromRegistration.getConstructor(String.class) |
| 52 | + .newInstance("some-config"); |
| 53 | + context.addDependency(new Dependency(typeFromRegistration, instance)); |
| 54 | + |
| 55 | + boolean found = context.getDependency(typeFromLookup).isPresent(); |
| 56 | + |
| 57 | + assertTrue(found, "dependency should be found by name across classloader boundaries"); |
| 58 | + } |
| 59 | + |
| 60 | + private static IsolatedClassLoader loadIsolated(ClassLoader parent) { |
| 61 | + return new IsolatedClassLoader(parent); |
| 62 | + } |
| 63 | + |
| 64 | + /** Loads only classes under the repro package in isolation; delegates everything else to parent. */ |
| 65 | + private static class IsolatedClassLoader extends ClassLoader { |
| 66 | + private final ClassLoader resourceLoader; |
| 67 | + |
| 68 | + IsolatedClassLoader(ClassLoader resourceLoader) { |
| 69 | + super(null); |
| 70 | + this.resourceLoader = resourceLoader; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { |
| 75 | + if (name.equals(TARGET_CLASS)) { |
| 76 | + synchronized (getClassLoadingLock(name)) { |
| 77 | + Class<?> loaded = findLoadedClass(name); |
| 78 | + if (loaded == null) { |
| 79 | + String path = name.replace('.', '/') + ".class"; |
| 80 | + try (InputStream is = resourceLoader.getResourceAsStream(path)) { |
| 81 | + if (is == null) { |
| 82 | + throw new ClassNotFoundException(name); |
| 83 | + } |
| 84 | + java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(); |
| 85 | + byte[] buf = new byte[4096]; |
| 86 | + int n; |
| 87 | + while ((n = is.read(buf)) != -1) { |
| 88 | + baos.write(buf, 0, n); |
| 89 | + } |
| 90 | + byte[] bytes = baos.toByteArray(); |
| 91 | + loaded = defineClass(name, bytes, 0, bytes.length); |
| 92 | + } catch (Exception e) { |
| 93 | + throw new ClassNotFoundException(name, e); |
| 94 | + } |
| 95 | + } |
| 96 | + if (resolve) { |
| 97 | + resolveClass(loaded); |
| 98 | + } |
| 99 | + return loaded; |
| 100 | + } |
| 101 | + } |
| 102 | + return Class.forName(name, resolve, resourceLoader); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public URL getResource(String name) { |
| 107 | + return resourceLoader.getResource(name); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments