|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.maven.buildcache.its; |
| 20 | + |
| 21 | +import java.nio.file.Paths; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import org.apache.maven.buildcache.its.junit.BeforeEach; |
| 25 | +import org.apache.maven.buildcache.its.junit.IntegrationTest; |
| 26 | +import org.apache.maven.buildcache.its.junit.IntegrationTestExtension; |
| 27 | +import org.apache.maven.it.VerificationException; |
| 28 | +import org.apache.maven.it.Verifier; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | + |
| 31 | +/** |
| 32 | + * Integration tests for the reconcile expression feature. |
| 33 | + * This feature allows plugin execution cache to depend on arbitrary Maven expressions |
| 34 | + * such as ${project.version}, ${project.groupId}, or any custom property. |
| 35 | + */ |
| 36 | +@IntegrationTest("src/test/projects/reconcile-expressions") |
| 37 | +class ReconcileExpressionsTest { |
| 38 | + |
| 39 | + private static final String PROJECT_NAME = "org.apache.maven.caching.test.reconcile:reconcile-expressions"; |
| 40 | + private static final String RESTORED_MESSAGE = "Found cached build, restoring " + PROJECT_NAME + " from cache"; |
| 41 | + |
| 42 | + // start every test case with clear cache |
| 43 | + @BeforeEach |
| 44 | + void setUp() { |
| 45 | + IntegrationTestExtension.deleteDir(Paths.get("target/build-cache/")); |
| 46 | + } |
| 47 | + /** |
| 48 | + * Test basic expression reconciliation - cache should be restored when expressions match. |
| 49 | + */ |
| 50 | + @Test |
| 51 | + void cacheRestoredWhenExpressionsMatch(Verifier verifier) throws VerificationException { |
| 52 | + verifier.setAutoclean(false); |
| 53 | + |
| 54 | + // First build - should create cache |
| 55 | + verifier.setLogFileName("../log-1.txt"); |
| 56 | + verifier.executeGoal("compile"); |
| 57 | + verifier.verifyErrorFreeLog(); |
| 58 | + verifyTextNotInLog(verifier, RESTORED_MESSAGE); |
| 59 | + |
| 60 | + // Second build with same expressions - should restore from cache |
| 61 | + verifier.setLogFileName("../log-2.txt"); |
| 62 | + verifier.executeGoal("compile"); |
| 63 | + verifier.verifyErrorFreeLog(); |
| 64 | + verifier.verifyTextInLog(RESTORED_MESSAGE); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Test that changing project.version invalidates cache through expression reconciliation. |
| 69 | + */ |
| 70 | + @Test |
| 71 | + void cacheInvalidatedWhenVersionChanges(Verifier verifier) throws VerificationException { |
| 72 | + verifier.setAutoclean(false); |
| 73 | + |
| 74 | + // First build with initial version |
| 75 | + verifier.setLogFileName("../log-version-1.txt"); |
| 76 | + verifier.executeGoal("compile"); |
| 77 | + verifier.verifyErrorFreeLog(); |
| 78 | + verifyTextNotInLog(verifier, RESTORED_MESSAGE); |
| 79 | + |
| 80 | + // Second build - should restore from cache |
| 81 | + verifier.setLogFileName("../log-version-2.txt"); |
| 82 | + verifier.executeGoal("compile"); |
| 83 | + verifier.verifyErrorFreeLog(); |
| 84 | + verifier.verifyTextInLog(RESTORED_MESSAGE); |
| 85 | + |
| 86 | + // Change version using versions-maven-plugin |
| 87 | + verifier.setLogFileName("../log-version-set.txt"); |
| 88 | + verifier.getCliOptions().clear(); |
| 89 | + verifier.addCliOption("-DoldVersion=1.0.0-SNAPSHOT"); |
| 90 | + verifier.addCliOption("-DnewVersion=2.0.0-SNAPSHOT"); |
| 91 | + verifier.addCliOption("-DgenerateBackupPoms=false"); |
| 92 | + verifier.executeGoal("versions:set"); |
| 93 | + verifier.verifyErrorFreeLog(); |
| 94 | + |
| 95 | + // Third build with changed version - cache should be invalidated |
| 96 | + verifier.getCliOptions().clear(); |
| 97 | + verifier.setLogFileName("../log-version-3.txt"); |
| 98 | + verifier.executeGoal("compile"); |
| 99 | + verifier.verifyErrorFreeLog(); |
| 100 | + // The project.version expression changed, so mojo parameters don't match |
| 101 | + verifier.verifyTextInLog("Plugin parameter mismatch found"); |
| 102 | + |
| 103 | + // Restore original version for other tests |
| 104 | + verifier.setLogFileName("../log-version-restore.txt"); |
| 105 | + verifier.addCliOption("-DoldVersion=2.0.0-SNAPSHOT"); |
| 106 | + verifier.addCliOption("-DnewVersion=1.0.0-SNAPSHOT"); |
| 107 | + verifier.addCliOption("-DgenerateBackupPoms=false"); |
| 108 | + verifier.executeGoal("versions:set"); |
| 109 | + verifier.verifyErrorFreeLog(); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Test that changing a custom property invalidates cache through expression reconciliation. |
| 114 | + */ |
| 115 | + @Test |
| 116 | + void cacheInvalidatedWhenCustomPropertyChanges(Verifier verifier) throws VerificationException { |
| 117 | + verifier.setAutoclean(false); |
| 118 | + |
| 119 | + // First build with default product.name |
| 120 | + verifier.setLogFileName("../log-prop-1.txt"); |
| 121 | + verifier.executeGoal("process-resources"); |
| 122 | + verifier.verifyErrorFreeLog(); |
| 123 | + verifyTextNotInLog(verifier, RESTORED_MESSAGE); |
| 124 | + |
| 125 | + // Second build with same property - should restore from cache |
| 126 | + verifier.setLogFileName("../log-prop-2.txt"); |
| 127 | + verifier.executeGoal("process-resources"); |
| 128 | + verifier.verifyErrorFreeLog(); |
| 129 | + verifier.verifyTextInLog(RESTORED_MESSAGE); |
| 130 | + |
| 131 | + // Third build with different property value via command line |
| 132 | + verifier.setLogFileName("../log-prop-3.txt"); |
| 133 | + verifier.getCliOptions().clear(); |
| 134 | + verifier.addCliOption("-Dproduct.name=DifferentProduct"); |
| 135 | + verifier.executeGoal("process-resources"); |
| 136 | + verifier.verifyErrorFreeLog(); |
| 137 | + // The property changed, so mojo parameters don't match |
| 138 | + verifier.verifyTextInLog("Plugin parameter mismatch found"); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Test that multiple expressions are all evaluated correctly. |
| 143 | + */ |
| 144 | + @Test |
| 145 | + void multipleExpressionsEvaluated(Verifier verifier) throws VerificationException { |
| 146 | + verifier.setAutoclean(false); |
| 147 | + verifier.setMavenDebug(true); |
| 148 | + |
| 149 | + // Build with debug to see expression evaluation |
| 150 | + verifier.setLogFileName("../log-multi-1.txt"); |
| 151 | + verifier.executeGoal("compile"); |
| 152 | + verifier.verifyErrorFreeLog(); |
| 153 | + |
| 154 | + // Verify that the expressions are being tracked |
| 155 | + // The build info should contain the evaluated expression values |
| 156 | + verifier.setLogFileName("../log-multi-2.txt"); |
| 157 | + verifier.executeGoal("compile"); |
| 158 | + verifier.verifyErrorFreeLog(); |
| 159 | + verifier.verifyTextInLog(RESTORED_MESSAGE); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Test that expressions with undefined properties are handled gracefully. |
| 164 | + * When an expression references an undefined property, it should not crash |
| 165 | + * and should handle the missing value appropriately. |
| 166 | + */ |
| 167 | + @Test |
| 168 | + void undefinedPropertyInExpressionHandledGracefully(Verifier verifier) throws VerificationException { |
| 169 | + verifier.setAutoclean(false); |
| 170 | + |
| 171 | + // First build - expressions with undefined properties should not cause failure |
| 172 | + verifier.setLogFileName("../log-undefined-1.txt"); |
| 173 | + verifier.executeGoal("compile"); |
| 174 | + verifier.verifyErrorFreeLog(); |
| 175 | + |
| 176 | + // Second build - should still work with cache |
| 177 | + verifier.setLogFileName("../log-undefined-2.txt"); |
| 178 | + verifier.executeGoal("compile"); |
| 179 | + verifier.verifyErrorFreeLog(); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Test that expressions correctly track changes across multiple consecutive builds. |
| 184 | + */ |
| 185 | + @Test |
| 186 | + void expressionChangesTrackedAcrossBuilds(Verifier verifier) throws VerificationException { |
| 187 | + verifier.setAutoclean(false); |
| 188 | + |
| 189 | + // Build 1: with environment=development (default) |
| 190 | + verifier.setLogFileName("../log-track-1.txt"); |
| 191 | + verifier.executeGoal("process-resources"); |
| 192 | + verifier.verifyErrorFreeLog(); |
| 193 | + verifyTextNotInLog(verifier, RESTORED_MESSAGE); |
| 194 | + |
| 195 | + // Build 2: same environment - should use cache |
| 196 | + verifier.setLogFileName("../log-track-2.txt"); |
| 197 | + verifier.executeGoal("process-resources"); |
| 198 | + verifier.verifyErrorFreeLog(); |
| 199 | + verifier.verifyTextInLog(RESTORED_MESSAGE); |
| 200 | + |
| 201 | + // Build 3: change to production - should invalidate cache |
| 202 | + verifier.setLogFileName("../log-track-3.txt"); |
| 203 | + verifier.getCliOptions().clear(); |
| 204 | + verifier.addCliOption("-Dbuild.environment=production"); |
| 205 | + verifier.executeGoal("process-resources"); |
| 206 | + verifier.verifyErrorFreeLog(); |
| 207 | + verifier.verifyTextInLog("Plugin parameter mismatch found"); |
| 208 | + |
| 209 | + // Build 4: same production environment - should use cache |
| 210 | + verifier.setLogFileName("../log-track-4.txt"); |
| 211 | + verifier.getCliOptions().clear(); |
| 212 | + verifier.addCliOption("-Dbuild.environment=production"); |
| 213 | + verifier.executeGoal("process-resources"); |
| 214 | + verifier.verifyErrorFreeLog(); |
| 215 | + // Note: May or may not use cache depending on how the cache key is calculated |
| 216 | + // The important thing is it doesn't fail |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Test that expressions work correctly with the resources:resources goal. |
| 221 | + */ |
| 222 | + @Test |
| 223 | + void expressionsWorkWithResourcesGoal(Verifier verifier) throws VerificationException { |
| 224 | + verifier.setAutoclean(false); |
| 225 | + |
| 226 | + // First build targeting resources specifically |
| 227 | + verifier.setLogFileName("../log-resources-1.txt"); |
| 228 | + verifier.executeGoal("process-resources"); |
| 229 | + verifier.verifyErrorFreeLog(); |
| 230 | + |
| 231 | + // Second build - should restore from cache |
| 232 | + verifier.setLogFileName("../log-resources-2.txt"); |
| 233 | + verifier.executeGoal("process-resources"); |
| 234 | + verifier.verifyErrorFreeLog(); |
| 235 | + verifier.verifyTextInLog(RESTORED_MESSAGE); |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Helper method to verify that a specific text is NOT present in the log. |
| 240 | + */ |
| 241 | + private static void verifyTextNotInLog(Verifier verifier, String text) throws VerificationException { |
| 242 | + List<String> lines = verifier.loadFile(verifier.getBasedir(), verifier.getLogFileName(), false); |
| 243 | + for (String line : lines) { |
| 244 | + if (Verifier.stripAnsi(line).contains(text)) { |
| 245 | + throw new VerificationException("Text found in log but should not be present: " + text); |
| 246 | + } |
| 247 | + } |
| 248 | + } |
| 249 | +} |
0 commit comments