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.io.File;
22  import java.util.regex.Matcher;
23  import java.util.regex.Pattern;
24  
25  import org.apache.maven.buildcache.its.junit.IntegrationTest;
26  import org.apache.maven.buildcache.util.LogFileUtils;
27  import org.apache.maven.it.VerificationException;
28  import org.apache.maven.it.Verifier;
29  import org.junit.jupiter.api.Assertions;
30  import org.junit.jupiter.api.Test;
31  
32  @IntegrationTest("src/test/projects/include-exclude")
33  public class IncludeExcludeTest {
34  
35      private static final Pattern NB_SRC_PATTERN = Pattern.compile("^(.*Found )([0-9]*)( input files.*)");
36  
37      private static final String PROJECT_NAME = "org.apache.maven.caching.test.include-exclude:include-exclude";
38  
39      /**
40       * Check the include / exclude functionnality, mixed from global configuration and from project configuration
41       * @param verifier the maven verifier instance
42       * @throws VerificationException
43       */
44      @Test
45      void includeExclude(Verifier verifier) throws VerificationException {
46          verifier.setAutoclean(false);
47          verifier.setMavenDebug(true);
48  
49          // First build
50          verifier.setLogFileName("../log-1.txt");
51          verifier.executeGoal("verify");
52          verifyLogs(verifier);
53  
54          // Second build
55          verifier.setLogFileName("../log-2.txt");
56          verifier.executeGoal("verify");
57          verifyLogs(verifier);
58          verifier.verifyTextInLog("Found cached build, restoring " + PROJECT_NAME + " from cache");
59      }
60  
61      private void verifyLogs(Verifier verifier) throws VerificationException {
62          final String nbFilesToFind = "9";
63  
64          verifier.verifyErrorFreeLog();
65  
66          // Verify that there is a line like "Found x input files." in the logs
67          String foundXFiles = LogFileUtils.findFirstLineContainingTextsInLogs(verifier, "Found ", " input files.");
68  
69          Matcher m = NB_SRC_PATTERN.matcher(foundXFiles);
70          Assertions.assertTrue(
71                  m.find(), "Found XX input files string not found in log. This test might need an update?");
72  
73          Assertions.assertEquals(nbFilesToFind, m.group(2), "Expected " + nbFilesToFind + " as source.");
74  
75          // Verify and inspect the line describing precisely which input files were chosen
76          String srcInputLine = LogFileUtils.findFirstLineContainingTextsInLogs(verifier, "Src input: [");
77          // Remove the array ending character "]"
78          srcInputLine = srcInputLine.substring(0, srcInputLine.length() - 1);
79          String[] srcInputs = srcInputLine.split(",");
80  
81          findLineContaining(srcInputs, "this_one_should_be_scanned.txt");
82          findLineContaining(srcInputs, "included_file_one.txt");
83          findLineContaining(srcInputs, "included_file_two.txt");
84          findLineContaining(srcInputs, "extraFile.txt");
85          findLineContaining(srcInputs, "from_second_folder.txt");
86          findLineContaining(srcInputs, "will_be_scanned.txt");
87          findLineContaining(srcInputs, "Test.java");
88          findLineContaining(srcInputs, "second_circle.txt");
89          findLineContaining(srcInputs, "third_circle.txt");
90  
91          // Verify that excluded directories are "cut" from inspection as soon as possible
92          String blacklistedLine = LogFileUtils.findFirstLineContainingTextsInLogs(
93                  verifier, "Skipping subtree (blacklisted)", "buildcache" + File.separator + "not");
94          Assertions.assertNotNull(
95                  blacklistedLine,
96                  "Expecting a debug line saying that \"src/main/java/org/apache/maven/buildcache/not\" is excluded from tree walking.");
97      }
98  
99      private void findLineContaining(String[] lines, String text) throws VerificationException {
100         for (String line : lines) {
101             if (line.contains(text)) {
102                 return;
103             }
104         }
105         throw new VerificationException("There is no line containing : " + text);
106     }
107 }