package dev.caskeleton.adapter.outbound.persistence.config; import dev.caskeleton.adapter.outbound.persistence.failure.SqlStateErrorMapping; import dev.caskeleton.adapter.outbound.persistence.h2.H2PersistenceConfig; import dev.caskeleton.adapter.outbound.persistence.postgresql.PostgreSqlPersistenceConfig; import java.io.PrintWriter; import java.io.StringWriter; import org.junit.jupiter.api.Test; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; /** Analysis-only probe: shipped-shape vendor selection with an unknown vendor value. */ class AnalysisVendorSelectorProbe { @Test void unknownVendorUnderShippedShape() { new ApplicationContextRunner() .withUserConfiguration(ShippedShape.class) .withPropertyValues(PersistenceVendorSettings.VENDOR_PROPERTY + "=mysql") .run( context -> { Throwable failure = context.getStartupFailure(); System.out.println("shipped.unknownVendor.contextFailed=" + (failure != null)); if (failure == null) { System.out.println( "shipped.unknownVendor.vendorSettingsBeans=" + context.getBeanNamesForType(PersistenceVendorSettings.class).length); System.out.println( "shipped.unknownVendor.sqlStateErrorMappingBeans=" + context.getBeanNamesForType(SqlStateErrorMapping.class).length); System.out.println( "shipped.unknownVendor.postgreSqlConfigBeans=" + context.getBeanNamesForType(PostgreSqlPersistenceConfig.class).length); System.out.println( "shipped.unknownVendor.h2ConfigBeans=" + context.getBeanNamesForType(H2PersistenceConfig.class).length); } else { System.out.println("shipped.unknownVendor.failure=" + stackTraceOf(failure)); } }); } @Test void unknownVendorWhenTheSettingsTypeIsEnabled() { new ApplicationContextRunner() .withUserConfiguration(ShippedShapeWithSettingsEnabled.class) .withPropertyValues(PersistenceVendorSettings.VENDOR_PROPERTY + "=mysql") .run( context -> { Throwable failure = context.getStartupFailure(); System.out.println("enabled.unknownVendor.contextFailed=" + (failure != null)); System.out.println( "enabled.unknownVendor.mentionsProperty=" + (failure != null && stackTraceOf(failure) .contains(PersistenceVendorSettings.VENDOR_PROPERTY))); }); } @Test void absentVendorUnderShippedShape() { new ApplicationContextRunner() .withUserConfiguration(ShippedShape.class) .run( context -> { Throwable failure = context.getStartupFailure(); System.out.println("shipped.absentVendor.contextFailed=" + (failure != null)); if (failure != null) { String trace = stackTraceOf(failure); System.out.println( "shipped.absentVendor.firstFailureLine=" + trace.substring(0, Math.min(220, trace.length())).replace('\n', '|')); } }); } @Test void unknownVendorWithTheShippedComponentScan() { new ApplicationContextRunner() .withUserConfiguration(ShippedShapeWithComponents.class) .withPropertyValues(PersistenceVendorSettings.VENDOR_PROPERTY + "=mysql") .run( context -> { Throwable failure = context.getStartupFailure(); System.out.println("components.unknownVendor.contextFailed=" + (failure != null)); if (failure != null) { String trace = stackTraceOf(failure); System.out.println( "components.unknownVendor.mentionsVendorProperty=" + trace.contains(PersistenceVendorSettings.VENDOR_PROPERTY)); System.out.println( "components.unknownVendor.firstFailureLine=" + trace.substring(0, Math.min(300, trace.length())).replace('\n', '|')); } }); } private static String stackTraceOf(Throwable throwable) { StringWriter writer = new StringWriter(); throwable.printStackTrace(new PrintWriter(writer)); return writer.toString(); } @Configuration(proxyBeanMethods = false) @Import({PostgreSqlPersistenceConfig.class, H2PersistenceConfig.class}) static class ShippedShape {} @Configuration(proxyBeanMethods = false) @Import({ dev.caskeleton.adapter.outbound.persistence.config.JpaAdapterComponentsConfig.class, PostgreSqlPersistenceConfig.class, H2PersistenceConfig.class }) static class ShippedShapeWithComponents {} @Configuration(proxyBeanMethods = false) @EnableConfigurationProperties(PersistenceVendorSettings.class) @Import({PostgreSqlPersistenceConfig.class, H2PersistenceConfig.class}) static class ShippedShapeWithSettingsEnabled {} }