Skip to content

Commit bf2997f

Browse files
committed
* Remove pomCache from parser
1 parent e33b87c commit bf2997f

File tree

7 files changed

+155
-18
lines changed

7 files changed

+155
-18
lines changed

sbm-support-rewrite/README.adoc

+5-4
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,12 @@ This allows parsing a given project to it's OpenRewrite AST representation and t
8484

8585
### Maven Pom Cache
8686
OpenRewrite downloads Maven Pom files to resolve dependencies.
87-
These pom files get cached and these cache implementations exist:
87+
The pom files get cached and the cache depends on the system.
8888

89-
- InMemoryMavenPomCache
90-
- RocksdbMavenPomCache
91-
- CompositeMavenPomCache
89+
- 32-Bit systems use the `InMemoryPomCache`.
90+
- 64-Bit systems use the `RocksdbMavenPomCache`.
91+
92+
####
9293

9394
#### Pom Cache Properties
9495

sbm-support-rewrite/src/main/java/org/springframework/sbm/parsers/BuildFileParser.java

-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@
5151
@RequiredArgsConstructor
5252
class BuildFileParser {
5353

54-
private final MavenPomCache mavenPomCache;
55-
5654
/**
5755
* Parse a list of Maven Pom files to a Map of {@code Path} and their parsed {@link Xml.Document}s.
5856
* The {@link Xml.Document}s are marked with {@link org.openrewrite.maven.tree.MavenResolutionResult} and the provided provenance markers.

sbm-support-rewrite/src/main/java/org/springframework/sbm/scopes/ScopeConfiguration.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.openrewrite.ExecutionContext;
1919
import org.openrewrite.InMemoryExecutionContext;
2020
import org.openrewrite.maven.MavenExecutionContextView;
21+
import org.openrewrite.maven.cache.MavenPomCache;
2122
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
2223
import org.springframework.context.annotation.Bean;
2324
import org.springframework.context.annotation.Configuration;
@@ -52,9 +53,11 @@ Supplier<ExecutionContext> executionContextSupplier() {
5253

5354
@Bean
5455
@org.springframework.sbm.scopes.annotations.ExecutionScope
55-
ExecutionContext executionContext(ProjectMetadata projectMetadata, Supplier<ExecutionContext> executionContextSupplier) {
56+
ExecutionContext executionContext(ProjectMetadata projectMetadata, Supplier<ExecutionContext> executionContextSupplier, MavenPomCache mavenPomCache) {
5657
ExecutionContext executionContext = executionContextSupplier.get();
57-
MavenExecutionContextView.view(executionContext).setMavenSettings(projectMetadata.getMavenSettings());
58+
MavenExecutionContextView contextView = MavenExecutionContextView.view(executionContext);
59+
contextView.setMavenSettings(projectMetadata.getMavenSettings());
60+
contextView.setPomCache(mavenPomCache);
5861
return executionContext;
5962
}
6063

sbm-support-rewrite/src/test/java/org/springframework/sbm/parsers/BuildFileParserTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public class GivenSimpleMavenMultiModuleProject {
105105
</project>
106106
""";
107107

108-
private final BuildFileParser sut = new BuildFileParser(new InMemoryMavenPomCache());
108+
private final BuildFileParser sut = new BuildFileParser();
109109

110110
@Test
111111
void filterAndSortBuildFiles_shouldReturnSortedListOfFilteredBuildFiles() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright 2021 - 2023 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.parsers;
17+
18+
import org.junit.jupiter.api.*;
19+
import org.junitpioneer.jupiter.SetSystemProperty;
20+
import org.openrewrite.maven.cache.CompositeMavenPomCache;
21+
import org.openrewrite.maven.cache.InMemoryMavenPomCache;
22+
import org.openrewrite.maven.cache.MavenPomCache;
23+
import org.openrewrite.maven.cache.RocksdbMavenPomCache;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.test.context.SpringBootTest;
26+
import org.springframework.boot.test.context.TestConfiguration;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Import;
29+
import org.springframework.test.annotation.DirtiesContext;
30+
import org.springframework.test.util.ReflectionTestUtils;
31+
32+
import java.util.List;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
36+
/**
37+
* @author Fabian Krüger
38+
*/
39+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
40+
public class MavenPomCacheTest {
41+
42+
private static final String originalArchDataModel = System.getProperty("sun.arch.data.model");
43+
44+
@Nested
45+
@SetSystemProperty(key="sun.arch.data.model", value = "64")
46+
class GivenA64BitSystem {
47+
48+
@Nested
49+
@SpringBootTest(properties = {"parser.pomCacheEnabled=true", "parser.pomCacheDirectory=target"})
50+
@DirtiesContext
51+
class WhenPomCacheIsEnabledIsTrue {
52+
53+
@Autowired
54+
private MavenPomCache mavenPomCache;
55+
56+
@Test
57+
@DisplayName("When pomCacheEnabled is true a CompositeMavenPomCache gets be used")
58+
void compositePomCacheShouldBeUsed() {
59+
assertThat(mavenPomCache).isInstanceOf(CompositeMavenPomCache.class);
60+
}
61+
62+
@Test
63+
@DisplayName("The used CompositeMavenPomCache should be Rocksdb and InMemory cache")
64+
void compositePomCacheShouldBeUsed2() {
65+
assertThat(mavenPomCache).isInstanceOf(CompositeMavenPomCache.class);
66+
assertThat(
67+
List.of(
68+
ReflectionTestUtils.getField(mavenPomCache, "l1").getClass(),
69+
ReflectionTestUtils.getField(mavenPomCache, "l2").getClass()
70+
)
71+
)
72+
.containsExactly(InMemoryMavenPomCache.class, RocksdbMavenPomCache.class);
73+
}
74+
}
75+
76+
@Nested
77+
@SpringBootTest(properties = {"parser.pomCacheEnabled=false"})
78+
@DirtiesContext
79+
class WhenPomCacheIsEnabledIsFalse {
80+
81+
@Autowired
82+
private MavenPomCache mavenPomCache;
83+
84+
@Test
85+
@DisplayName("When pomCacheEnabled is false a InMemoryMavenPomCache should be used")
86+
void InMemoryMavenPomCacheShouldBeUsed() {
87+
assertThat(mavenPomCache).isInstanceOf(InMemoryMavenPomCache.class);
88+
}
89+
}
90+
91+
}
92+
93+
@Nested
94+
@DirtiesContext
95+
@SpringBootTest(properties = {"parser.pomCacheEnabled=true"})
96+
@SetSystemProperty(key = "sun.arch.data.model", value = "32")
97+
class GivenA32BitSystem {
98+
99+
@Autowired
100+
private MavenPomCache mavenPomCache;
101+
102+
@Test
103+
@DisplayName("With 32Bit an InMemory pom cache gets used")
104+
void shouldUseInMemoryMavenPomCache() {
105+
assertThat(mavenPomCache).isInstanceOf(InMemoryMavenPomCache.class);
106+
}
107+
}
108+
109+
@Nested
110+
@DirtiesContext
111+
@Import(CustomCacheConfig.class)
112+
@SpringBootTest(properties = {"parser.pomCacheEnabled=true"})
113+
class GivenCustomCacheProvided {
114+
115+
@Autowired
116+
private MavenPomCache mavenPomCache;
117+
118+
@Test
119+
@DisplayName("The custom pom cache should be used")
120+
void shouldUseTheProvidedPomCache() {
121+
assertThat(mavenPomCache).isInstanceOf(CustomPomCache.class);
122+
}
123+
}
124+
}
125+
126+
@TestConfiguration
127+
class CustomCacheConfig {
128+
// Provide custom MavenPomCache as bean
129+
// Should overwrite the existing MavenPomCache
130+
@Bean
131+
public MavenPomCache mavenPomCache() {
132+
return new CustomPomCache();
133+
}
134+
}
135+
136+
class CustomPomCache extends InMemoryMavenPomCache {}

sbm-support-rewrite/src/test/java/org/springframework/sbm/parsers/RewriteMavenProjectParserTest.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import org.openrewrite.marker.ci.GithubActionsBuildEnvironment;
3939
import org.openrewrite.maven.MavenExecutionContextView;
4040
import org.openrewrite.maven.MavenSettings;
41-
import org.openrewrite.maven.cache.CompositeMavenPomCache;
41+
import org.openrewrite.maven.cache.InMemoryMavenPomCache;
4242
import org.openrewrite.maven.tree.MavenResolutionResult;
4343
import org.openrewrite.shaded.jgit.api.Git;
4444
import org.openrewrite.shaded.jgit.api.errors.GitAPIException;
@@ -272,8 +272,8 @@ void parseMultiModule1_WithCustomParser() {
272272
RewriteProjectParser rpp = new RewriteProjectParser(
273273
new MavenExecutor(new MavenExecutionRequestFactory(new MavenConfigFileParser()), new MavenPlexusContainer()),
274274
new ProvenanceMarkerFactory(mavenMojoProjectParserFactory),
275-
new BuildFileParser(parserSettings),
276-
new SourceFileParser(mavenModelReader, parserSettings, mavenMojoParserPrivateMethods),
275+
new BuildFileParser(),
276+
new SourceFileParser(mavenModelReader, parserProperties, mavenMojoParserPrivateMethods),
277277
new StyleDetector(),
278278
parserSettings,
279279
mock(ParsingEventListener.class),
@@ -399,10 +399,9 @@ private static void verifyExecutionContext(RewriteProjectParsingResult parsingRe
399399
// 8
400400
assertThat(
401401
messages.get("org.openrewrite.maven.pomCache")
402-
).isSameAs(
403-
MavenExecutionContextView.view(resultingExecutionContext).getPomCache()
404-
);
405-
assertThat(MavenExecutionContextView.view(resultingExecutionContext).getPomCache()).isInstanceOf(CompositeMavenPomCache.class);
402+
).isNull();
403+
assertThat(MavenExecutionContextView.view(resultingExecutionContext).getPomCache()).isInstanceOf(InMemoryMavenPomCache.class);
404+
// assertThat(MavenExecutionContextView.view(resultingExecutionContext).getPomCache()).isInstanceOf(CompositeMavenPomCache.class);
406405

407406
// 9
408407
// FIXME: This fails sometimes when multiple tests are run together. The resolution time has been 0 and null

sbm-support-rewrite/src/test/java/org/springframework/sbm/parsers/RewriteProjectParserTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ void parseSimpleMavenProject(@TempDir Path tempDir) {
9393
RewriteProjectParser projectParser = new RewriteProjectParser(
9494
new MavenExecutor(new MavenExecutionRequestFactory(new MavenConfigFileParser()), new MavenPlexusContainer()),
9595
new ProvenanceMarkerFactory(mavenMojoProjectParserFactory),
96-
new BuildFileParser(parserSettings),
97-
new SourceFileParser(mavenModelReader, parserSettings, mavenMojoParserPrivateMethods),
96+
new BuildFileParser(),
97+
new SourceFileParser(mavenModelReader, parserProperties, mavenMojoParserPrivateMethods),
9898
new StyleDetector(),
9999
parserSettings,
100100
mock(ParsingEventListener.class),

0 commit comments

Comments
 (0)