View Javadoc
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.util.Arrays;
22  import java.util.List;
23  
24  import org.apache.maven.buildcache.its.junit.IntegrationTest;
25  import org.apache.maven.it.VerificationException;
26  import org.apache.maven.it.Verifier;
27  import org.junit.jupiter.api.Assertions;
28  import org.junit.jupiter.api.Test;
29  
30  import static org.apache.maven.buildcache.util.LogFileUtils.findFirstLineContainingTextsInLogs;
31  
32  @IntegrationTest("src/test/projects/build-extension")
33  class SkipBuildExtensionTest {
34  
35      @Test
36      void simple(Verifier verifier) throws VerificationException {
37          verifier.setAutoclean(false);
38  
39          verifier.setLogFileName("../log-1.txt");
40          verifier.executeGoal("clean");
41          verifier.verifyErrorFreeLog();
42  
43          verifier.verifyTextInLog("Build cache is disabled for 'clean' goal.");
44      }
45  
46      @Test
47      void multipleGoals(Verifier verifier) throws VerificationException {
48          verifier.setAutoclean(false);
49  
50          verifier.setLogFileName("../log-2.txt");
51          String[] goals = {"clean", "install"};
52          List<String> goalsList = Arrays.asList(goals);
53          verifier.executeGoals(goalsList);
54          verifier.verifyErrorFreeLog();
55  
56          verifyNoTextInLog(verifier, "Build cache is disabled for 'clean' goal.");
57      }
58  
59      /**
60       * Verifies that running with -Dmaven.build.cache.enabled=false does not cause
61       * IllegalStateException and the build completes successfully.
62       * <p>
63       * This tests the fix for the regression where stagePreExistingArtifacts() was called
64       * without checking if the cache was initialized, causing IllegalStateException when
65       * cache is disabled via command line.
66       *
67       * @see <a href="https://github.com/apache/maven-build-cache-extension/pull/394#issuecomment-3714680789">PR #394 comment</a>
68       */
69      @Test
70      void cacheDisabledViaCommandLine(Verifier verifier) throws VerificationException {
71          verifier.setAutoclean(false);
72          verifier.addCliOption("-Dmaven.build.cache.enabled=false");
73  
74          verifier.setLogFileName("../log-cache-disabled.txt");
75          verifier.executeGoals(Arrays.asList("clean", "install"));
76          verifier.verifyErrorFreeLog();
77  
78          // Verify cache was actually disabled
79          verifier.verifyTextInLog("Cache disabled by command line flag");
80      }
81  
82      private static void verifyNoTextInLog(Verifier verifier, String text) throws VerificationException {
83          Assertions.assertNull(findFirstLineContainingTextsInLogs(verifier, text));
84      }
85  }