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.IOException;
22  import java.nio.file.Files;
23  import java.nio.file.Path;
24  import java.nio.file.Paths;
25  import java.nio.file.attribute.FileTime;
26  import java.time.Instant;
27  import java.util.Arrays;
28  
29  import org.apache.maven.buildcache.its.junit.IntegrationTest;
30  import org.apache.maven.it.VerificationException;
31  import org.apache.maven.it.Verifier;
32  import org.junit.jupiter.api.Test;
33  
34  import static org.junit.jupiter.api.Assertions.assertTrue;
35  
36  /**
37   * Tests that stale artifacts from source changes are not cached.
38   * Simulates the scenario:
39   * 1. Build version A (creates target/classes with old content)
40   * 2. Source changes (e.g., branch switch, external update), but target/classes remains
41   * 3. Build without 'mvn clean' - should NOT cache stale target/classes
42   */
43  @IntegrationTest("src/test/projects/stale-artifact")
44  class StaleArtifactTest {
45  
46      @Test
47      void staleDirectoryNotCached(Verifier verifier) throws VerificationException, IOException {
48          verifier.setAutoclean(false);
49  
50          // Build version A: compile project
51          verifier.setLogFileName("../log-version-a.txt");
52          verifier.executeGoals(Arrays.asList("clean", "compile"));
53          verifier.verifyErrorFreeLog();
54  
55          Path classesDir = Paths.get(verifier.getBasedir(), "target", "classes");
56          Path appClass = classesDir.resolve("org/example/App.class");
57          assertTrue(Files.exists(appClass), "App.class should exist after compile");
58  
59          // Simulate source change (e.g., branch switch, external update) by:
60          // 1. Modifying source file (simulates different source version)
61          // 2. Making class file appear OLDER than build start time (stale)
62          Path sourceFile = Paths.get(verifier.getBasedir(), "src/main/java/org/example/App.java");
63          String content = new String(Files.readAllBytes(sourceFile), "UTF-8");
64          Files.write(sourceFile, content.replace("Version A", "Version B").getBytes("UTF-8"));
65  
66          // Backdate the class file to simulate stale artifact from previous build
67          FileTime oldTime = FileTime.from(Instant.now().minusSeconds(3600)); // 1 hour ago
68          Files.setLastModifiedTime(appClass, oldTime);
69  
70          // Try to build without clean (simulates developer workflow)
71          verifier.setLogFileName("../log-version-b.txt");
72          verifier.executeGoals(Arrays.asList("compile"));
73          verifier.verifyErrorFreeLog();
74  
75          // Verify that compiler detected source change and recompiled
76          // (class file should have new timestamp after recompile)
77          FileTime newTime = Files.getLastModifiedTime(appClass);
78          assertTrue(
79                  newTime.toMillis() > oldTime.toMillis(),
80                  "Compiler should have recompiled stale class (new timestamp: " + newTime + ", old timestamp: " + oldTime
81                          + ")");
82      }
83  }