|
| 1 | +package org.springframework.sbm.boot.upgrade_27_30.filter; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.openrewrite.SourceFile; |
| 5 | +import org.springframework.sbm.boot.properties.SpringApplicationPropertiesPathMatcher; |
| 6 | +import org.springframework.sbm.boot.properties.SpringBootApplicationPropertiesRegistrar; |
| 7 | +import org.springframework.sbm.boot.upgrade_27_30.filter.LoggingDateFormatPropertyFinder; |
| 8 | +import org.springframework.sbm.build.api.BuildFile; |
| 9 | +import org.springframework.sbm.build.impl.OpenRewriteMavenBuildFile; |
| 10 | +import org.springframework.sbm.engine.context.ProjectContext; |
| 11 | +import org.springframework.sbm.project.resource.*; |
| 12 | +import org.springframework.sbm.properties.api.PropertiesSource; |
| 13 | + |
| 14 | +import java.nio.file.Path; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 18 | + |
| 19 | +public class LoggingDateFormatPropertyFinderTest { |
| 20 | + |
| 21 | + private static final String APPLICATION_PROPERTIES_WITH_LOG_DATE_FORMAT = "foo=bar\n" + |
| 22 | + "migrate=true\n" + |
| 23 | + "logging.pattern.dateformat=xyz\n"; |
| 24 | + |
| 25 | + private static final String MULTI_MODULE_POM_XML = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + |
| 26 | + "xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" + |
| 27 | + " <modelVersion>4.0.0</modelVersion>\n" + |
| 28 | + "\n" + |
| 29 | + " <groupId>org.springframework.sbm</groupId>\n" + |
| 30 | + " <artifactId>spring-boot-migrator</artifactId>\n" + |
| 31 | + " <version>0.11.2-SNAPSHOT</version>\n" + |
| 32 | + " <packaging>pom</packaging>\n" + |
| 33 | + " <modules>\n" + |
| 34 | + " <module>module1</module>\n" + |
| 35 | + " <module>module2</module>\n" + |
| 36 | + " </modules>\n" + |
| 37 | + "</project>"; |
| 38 | + |
| 39 | + private static final String SUB_MODULE_POM_XML = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + |
| 40 | + "xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" + |
| 41 | + " <parent>\n" + |
| 42 | + " <artifactId>spring-boot-migrator</artifactId>\n" + |
| 43 | + " <groupId>org.springframework.sbm</groupId>\n" + |
| 44 | + " <version>0.11.2-SNAPSHOT</version>\n" + |
| 45 | + " <relativePath>../../pom.xml</relativePath>\n" + |
| 46 | + " </parent>\n" + |
| 47 | + " <modelVersion>4.0.0</modelVersion>\n" + |
| 48 | + "\n" + |
| 49 | + " <artifactId>{{module}}</artifactId>\n" + |
| 50 | + "</project>"; |
| 51 | + |
| 52 | + @Test |
| 53 | + public void givenProjectWithLogDateFormatCustomization_findResources_returnResource(){ |
| 54 | + ProjectContext projectContext = TestProjectContext.buildProjectContext() |
| 55 | + .addRegistrar(new SpringBootApplicationPropertiesRegistrar(new SpringApplicationPropertiesPathMatcher())) |
| 56 | + .addProjectResource(Path.of("src", "main", "resources", "application.properties"), APPLICATION_PROPERTIES_WITH_LOG_DATE_FORMAT) |
| 57 | + .build(); |
| 58 | + |
| 59 | + LoggingDateFormatPropertyFinder loggingDateFormatPropertyFinder = new LoggingDateFormatPropertyFinder(); |
| 60 | + List<? extends PropertiesSource> propertiesSources = loggingDateFormatPropertyFinder.apply(projectContext.getProjectResources()); |
| 61 | + |
| 62 | + assertThat(propertiesSources.size()).isEqualTo(1); |
| 63 | + assertThat(propertiesSources.get(0).getProperty("logging.pattern.dateformat").isPresent()).isTrue(); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void givenMultiModuleProjectWithLogDateFormatCustomization_findResources_returnResource(){ |
| 68 | + ProjectContext projectContext = TestProjectContext.buildProjectContext() |
| 69 | + .withMavenRootBuildFileSource(MULTI_MODULE_POM_XML) |
| 70 | + .addProjectResource(Path.of("module1","pom.xml"),SUB_MODULE_POM_XML.replace("{{module}}", "module1")) |
| 71 | + .addProjectResource(Path.of("module2","pom.xml"),SUB_MODULE_POM_XML.replace("{{module}}", "module2")) |
| 72 | + .addRegistrar(new SpringBootApplicationPropertiesRegistrar(new SpringApplicationPropertiesPathMatcher())) |
| 73 | + .addProjectResource(Path.of("module1","src", "main", "resources", "application.properties"), APPLICATION_PROPERTIES_WITH_LOG_DATE_FORMAT) |
| 74 | + .addProjectResource(Path.of("module2","src", "main", "resources", "application.properties"), APPLICATION_PROPERTIES_WITH_LOG_DATE_FORMAT) |
| 75 | + .build(); |
| 76 | + |
| 77 | + LoggingDateFormatPropertyFinder loggingDateFormatPropertyFinder = new LoggingDateFormatPropertyFinder(); |
| 78 | + List<? extends PropertiesSource> propertiesSources = loggingDateFormatPropertyFinder.apply(projectContext.getProjectResources()); |
| 79 | + |
| 80 | + assertThat(propertiesSources.size()).isEqualTo(2); |
| 81 | + assertThat(propertiesSources.get(0).getProperty("logging.pattern.dateformat").isPresent()).isTrue(); |
| 82 | + assertThat(propertiesSources.get(1).getProperty("logging.pattern.dateformat").isPresent()).isTrue(); |
| 83 | + } |
| 84 | +} |
0 commit comments