From de404eb1191f4fa30b8be0fb24d0fc5a259ddfa2 Mon Sep 17 00:00:00 2001 From: Maziz Date: Sun, 12 May 2024 21:31:47 +0800 Subject: [PATCH] 1. Change Scanner instantiation to use reflection in order to cater flyway 10 and flyway 9 2. Added test for flyway scanner 9. --- ...NativeImageResourceProviderCustomizer.java | 40 ++++++++++++++++- ...eImageResourceProviderCustomizerTests.java | 44 +++++++++++++++++++ 2 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/Flyway9NativeImageResourceProviderCustomizerTests.java diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/NativeImageResourceProviderCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/NativeImageResourceProviderCustomizer.java index 615a33180cd3..332eba757e6e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/NativeImageResourceProviderCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/NativeImageResourceProviderCustomizer.java @@ -16,6 +16,8 @@ package org.springframework.boot.autoconfigure.flyway; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.util.Arrays; import org.flywaydb.core.api.configuration.FluentConfiguration; @@ -23,6 +25,9 @@ import org.flywaydb.core.internal.scanner.LocationScannerCache; import org.flywaydb.core.internal.scanner.ResourceNameCache; import org.flywaydb.core.internal.scanner.Scanner; +import org.jetbrains.annotations.NotNull; + +import org.springframework.util.ClassUtils; /** * Registers {@link NativeImageResourceProvider} as a Flyway @@ -35,8 +40,23 @@ class NativeImageResourceProviderCustomizer extends ResourceProviderCustomizer { @Override public void customize(FluentConfiguration configuration) { if (configuration.getResourceProvider() == null) { - Scanner scanner = new Scanner<>(JavaMigration.class, false, new ResourceNameCache(), - new LocationScannerCache(), configuration); + Constructor scannerConstructor; + Scanner scanner; + try { + scannerConstructor = ClassUtils.forName("org.flywaydb.core.internal.scanner.Scanner", null).getDeclaredConstructors()[0]; + if(scannerConstructor.getParameters().length <= 5) { + scanner = getFlyway10ScannerObject(configuration, scannerConstructor); + } else { + scanner = getFlyway9Scanner(configuration, scannerConstructor); + } + } + catch (ClassNotFoundException e) { + throw new IllegalStateException(e); + } + catch (InvocationTargetException | InstantiationException | + IllegalAccessException e) { + throw new RuntimeException(e); + } NativeImageResourceProvider resourceProvider = new NativeImageResourceProvider(scanner, configuration.getClassLoader(), Arrays.asList(configuration.getLocations()), configuration.getEncoding(), configuration.isFailOnMissingLocations()); @@ -44,4 +64,20 @@ public void customize(FluentConfiguration configuration) { } } + private static @NotNull Scanner getFlyway10ScannerObject(FluentConfiguration configuration, Constructor scannerConstructor) throws InstantiationException, IllegalAccessException, InvocationTargetException { + Scanner scanner; + scanner = (Scanner) scannerConstructor.newInstance(JavaMigration.class, false, new ResourceNameCache(), + new LocationScannerCache(), configuration); + return scanner; + } + + private static @NotNull Scanner getFlyway9Scanner(FluentConfiguration configuration, Constructor scannerConstructor) throws InstantiationException, IllegalAccessException, InvocationTargetException { + Scanner scanner; + scanner = (Scanner) scannerConstructor.newInstance(JavaMigration.class, + Arrays.asList(configuration.getLocations()), configuration.getClassLoader(), + configuration.getEncoding(), configuration.isDetectEncoding(), false, new ResourceNameCache(), + new LocationScannerCache(), configuration.isFailOnMissingLocations()); + return scanner; + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/Flyway9NativeImageResourceProviderCustomizerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/Flyway9NativeImageResourceProviderCustomizerTests.java new file mode 100644 index 000000000000..abcf2ffc25dc --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/Flyway9NativeImageResourceProviderCustomizerTests.java @@ -0,0 +1,44 @@ +/* + * Copyright 2012-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.flyway; + +import java.util.Collection; + +import org.flywaydb.core.api.ResourceProvider; +import org.flywaydb.core.api.configuration.FluentConfiguration; +import org.flywaydb.core.api.resource.LoadableResource; +import org.junit.jupiter.api.Test; + + +import org.springframework.boot.testsupport.classpath.ClassPathOverrides; + +import static org.assertj.core.api.Assertions.assertThat; + +@ClassPathOverrides("org.flywaydb:flyway-core:9.22.3") +class Flyway9NativeImageResourceProviderCustomizerTests { + private final NativeImageResourceProviderCustomizer customizer = new NativeImageResourceProviderCustomizer(); + + @Test + void nativeImageResourceProviderShouldFindMigrations() { + FluentConfiguration configuration = new FluentConfiguration(); + this.customizer.customize(configuration); + ResourceProvider resourceProvider = configuration.getResourceProvider(); + Collection migrations = resourceProvider.getResources("V", new String[] { ".sql" }); + LoadableResource migration = resourceProvider.getResource("V1__init.sql"); + assertThat(migrations).containsExactly(migration); + } +}