|
| 1 | +/* |
| 2 | + * Copyright 2021 - 2022 the original author or authors. |
| 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 | +package org.springframework.sbm.build; |
| 17 | + |
| 18 | +import org.assertj.core.api.Assertions; |
| 19 | +import org.intellij.lang.annotations.Language; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.openrewrite.maven.MavenParser; |
| 22 | +import org.openrewrite.maven.internal.RawPom; |
| 23 | +import org.openrewrite.maven.tree.Dependency; |
| 24 | +import org.openrewrite.maven.tree.MavenResolutionResult; |
| 25 | +import org.openrewrite.maven.tree.Pom; |
| 26 | +import org.openrewrite.xml.tree.Xml; |
| 27 | +import org.springframework.sbm.GitHubIssue; |
| 28 | +import org.springframework.sbm.engine.context.ProjectContext; |
| 29 | +import org.springframework.sbm.project.resource.TestProjectContext; |
| 30 | + |
| 31 | +import java.io.ByteArrayInputStream; |
| 32 | +import java.util.List; |
| 33 | + |
| 34 | +import static org.assertj.core.api.Assertions.assertThat; |
| 35 | + |
| 36 | +/** |
| 37 | + * @author Fabian Krüger |
| 38 | + */ |
| 39 | +@GitHubIssue("https://github.com/spring-projects-experimental/spring-boot-migrator/issues/54") |
| 40 | +public class Issue54Test { |
| 41 | + |
| 42 | + @Test |
| 43 | + void scanMultiModuleProjectWithOptionalPropertyProvidedByParent() { |
| 44 | + |
| 45 | + @Language("xml") |
| 46 | + String pomA = |
| 47 | + """ |
| 48 | + <?xml version="1.0" encoding="UTF-8"?> |
| 49 | + <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 50 | + <modelVersion>4.0.0</modelVersion> |
| 51 | + <groupId>com.acme</groupId> |
| 52 | + <artifactId>a</artifactId> |
| 53 | + <version>1.1.0</version> |
| 54 | + <packaging>pom</packaging> |
| 55 | + <properties> |
| 56 | + <boolean-variable>false</boolean-variable> |
| 57 | + </properties> |
| 58 | + </project> |
| 59 | + """; |
| 60 | + |
| 61 | + @Language("xml") |
| 62 | + String pomB = |
| 63 | + """ |
| 64 | + <?xml version="1.0" encoding="UTF-8"?> |
| 65 | + <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 66 | + <modelVersion>4.0.0</modelVersion> |
| 67 | + <parent> |
| 68 | + <groupId>com.acme</groupId> |
| 69 | + <artifactId>a</artifactId> |
| 70 | + <version>1.1.0</version> |
| 71 | + </parent> |
| 72 | + <artifactId>b</artifactId> |
| 73 | + <dependencies> |
| 74 | + <dependency> |
| 75 | + <groupId>jakarta.validation</groupId> |
| 76 | + <artifactId>jakarta.validation-api</artifactId> |
| 77 | + <version>3.0.2</version> |
| 78 | + <optional>${boolean-variable}</optional> |
| 79 | + </dependency> |
| 80 | + </dependencies> |
| 81 | + </project> |
| 82 | + """; |
| 83 | + |
| 84 | + List<Xml.Document> poms = MavenParser.builder().build().parse(pomA, pomB); |
| 85 | + |
| 86 | + assertThat( |
| 87 | + poms.get(1).getMarkers().findFirst(MavenResolutionResult.class).get().getPom().getProperties().get("boolean-variable") |
| 88 | + ).isEqualTo("false"); |
| 89 | + |
| 90 | + List<Dependency> requestedDependencies = poms.get(1).getMarkers().findFirst(MavenResolutionResult.class).get().getPom().getRequestedDependencies(); |
| 91 | + |
| 92 | + assertThat(requestedDependencies).hasSize(1); |
| 93 | + assertThat(requestedDependencies.get(0).getOptional()).isEqualTo("${boolean-variable}"); |
| 94 | + |
| 95 | + ProjectContext projectContext = TestProjectContext |
| 96 | + .buildProjectContext() |
| 97 | + .withMavenBuildFileSource("pom.xml", pomA) |
| 98 | + .withMavenBuildFileSource("a/pom.xml", pomB) |
| 99 | + .build(); |
| 100 | + assertThat(projectContext.getApplicationModules().getModule("").getBuildFile().getProperty("boolean-variable")).isEqualTo("false"); |
| 101 | + assertThat(projectContext.getApplicationModules().getModule("a").getBuildFile().getRequestedDependencies()).hasSize(1); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void scanMultiModuleProjectWithVersionPropertyProvidedByParent() { |
| 106 | + |
| 107 | + @Language("xml") |
| 108 | + String pomA = |
| 109 | + """ |
| 110 | + <?xml version="1.0" encoding="UTF-8"?> |
| 111 | + <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 112 | + <modelVersion>4.0.0</modelVersion> |
| 113 | + <groupId>com.acme</groupId> |
| 114 | + <artifactId>a</artifactId> |
| 115 | + <version>1.1.0</version> |
| 116 | + <packaging>pom</packaging> |
| 117 | + <properties> |
| 118 | + <scope-variable>3.0.2</scope-variable> |
| 119 | + </properties> |
| 120 | + </project> |
| 121 | + """; |
| 122 | + |
| 123 | + @Language("xml") |
| 124 | + String pomB = |
| 125 | + """ |
| 126 | + <?xml version="1.0" encoding="UTF-8"?> |
| 127 | + <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 128 | + <modelVersion>4.0.0</modelVersion> |
| 129 | + <parent> |
| 130 | + <groupId>com.acme</groupId> |
| 131 | + <artifactId>a</artifactId> |
| 132 | + <version>1.1.0</version> |
| 133 | + </parent> |
| 134 | + <artifactId>b</artifactId> |
| 135 | + <dependencies> |
| 136 | + <dependency> |
| 137 | + <groupId>jakarta.validation</groupId> |
| 138 | + <artifactId>jakarta.validation-api</artifactId> |
| 139 | + <version>${scope-variable}</version> |
| 140 | + </dependency> |
| 141 | + </dependencies> |
| 142 | + </project> |
| 143 | + """; |
| 144 | + |
| 145 | + List<Xml.Document> poms = MavenParser.builder().build().parse(pomA, pomB); |
| 146 | + |
| 147 | + assertThat(poms |
| 148 | + .get(1) |
| 149 | + .getMarkers() |
| 150 | + .findFirst(MavenResolutionResult.class) |
| 151 | + .get().getPom().getProperties().get("scope-variable")).isEqualTo("3.0.2"); |
| 152 | + |
| 153 | + List<Dependency> requestedDependencies = poms |
| 154 | + .get(1) |
| 155 | + .getMarkers() |
| 156 | + .findFirst(MavenResolutionResult.class) |
| 157 | + .get() |
| 158 | + .getPom() |
| 159 | + .getRequestedDependencies(); |
| 160 | + |
| 161 | + assertThat(requestedDependencies).hasSize(1); |
| 162 | + assertThat(requestedDependencies.get(0).getVersion()).isEqualTo("${scope-variable}"); |
| 163 | + |
| 164 | + ProjectContext projectContext = TestProjectContext |
| 165 | + .buildProjectContext() |
| 166 | + .withMavenBuildFileSource("pom.xml", pomA) |
| 167 | + .withMavenBuildFileSource("a/pom.xml", pomB) |
| 168 | + .build(); |
| 169 | + assertThat(projectContext.getApplicationModules().getModule("").getBuildFile().getProperty("scope-variable")).isEqualTo("3.0.2"); |
| 170 | + assertThat(projectContext.getApplicationModules().getModule("a").getBuildFile().getRequestedDependencies()).hasSize(1); |
| 171 | + assertThat(projectContext.getApplicationModules().getModule("a").getBuildFile().getRequestedDependencies().get(0).getVersion()).isEqualTo("3.0.2"); |
| 172 | + } |
| 173 | + |
| 174 | +} |
0 commit comments