|
| 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 {} |
0 commit comments