View Javadoc
1   package org.apache.maven.it;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.it.util.ResourceExtractor;
23  import org.apache.maven.shared.utils.io.FileUtils;
24  
25  import java.io.File;
26  
27  /**
28   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-4368">MNG-4368</a>.
29   *
30   * @author Benjamin Bentmann
31   */
32  public class MavenITmng4368TimestampAwareArtifactInstallerTest
33      extends AbstractMavenIntegrationTestCase
34  {
35  
36      public MavenITmng4368TimestampAwareArtifactInstallerTest()
37      {
38          super( "[2.0.3,3.0-alpha-1),[3.0-alpha-6,)" );
39      }
40  
41      /**
42       * Verify that the artifact installer copies POMs to the local repo even if they have an older timestamp as the
43       * copy in the local repo.
44       *
45       * @throws Exception in case of failure
46       */
47      public void testitPomPackaging()
48          throws Exception
49      {
50          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-4368/pom" );
51  
52          File aDir = new File( testDir, "branch-a" );
53          File aPom = new File( aDir, "pom.xml" );
54          File bDir = new File( testDir, "branch-b" );
55          File bPom = new File( bDir, "pom.xml" );
56  
57          aPom.setLastModified( System.currentTimeMillis() );
58          bPom.setLastModified( aPom.lastModified() - 1000 * 60 );
59  
60          Verifier verifier = newVerifier( aDir.getAbsolutePath() );
61          verifier.setAutoclean( false );
62          verifier.deleteDirectory( "target" );
63          verifier.deleteArtifacts( "org.apache.maven.its.mng4368" );
64          verifier.executeGoal( "initialize" );
65          verifier.verifyErrorFreeLog();
66          verifier.resetStreams();
67  
68          File installedPom =
69              new File( verifier.getArtifactPath( "org.apache.maven.its.mng4368", "test", "0.1-SNAPSHOT", "pom" ) );
70  
71          String pom = FileUtils.fileRead( installedPom, "UTF-8" );
72          assertTrue( pom.indexOf( "Branch-A" ) > 0 );
73          assertFalse( pom.contains( "Branch-B" ) );
74  
75          assertEquals( aPom.length(), bPom.length() );
76          assertTrue( aPom.lastModified() > bPom.lastModified() );
77          assertTrue( installedPom.lastModified() > bPom.lastModified() );
78  
79          verifier = newVerifier( bDir.getAbsolutePath() );
80          verifier.setAutoclean( false );
81          verifier.deleteDirectory( "target" );
82          verifier.executeGoal( "initialize" );
83          verifier.verifyErrorFreeLog();
84          verifier.resetStreams();
85  
86          pom = FileUtils.fileRead( installedPom, "UTF-8" );
87          assertFalse( pom.contains( "Branch-A" ) );
88          assertTrue( pom.indexOf( "Branch-B" ) > 0 );
89      }
90  
91      /**
92       * Verify that the artifact installer copies files to the local repo only if their timestamp differs from the copy
93       * already in the local repo.
94       *
95       * @throws Exception in case of failure
96       */
97      public void testitJarPackaging()
98          throws Exception
99      {
100         requiresMavenVersion( "[2.2.2,3.0-alpha-1),[3.0-alpha-6,)" );
101 
102         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-4368/jar" );
103 
104         File aDir = new File( testDir, "branch-a" );
105         File aArtifact = new File( aDir, "artifact.jar" );
106         File bDir = new File( testDir, "branch-b" );
107         File bArtifact = new File( bDir, "artifact.jar" );
108 
109         FileUtils.fileWrite( aArtifact.getPath(), "UTF-8", "from Branch-A" );
110         aArtifact.setLastModified( System.currentTimeMillis() );
111         FileUtils.fileWrite( bArtifact.getPath(), "UTF-8", "from Branch-B" );
112         bArtifact.setLastModified( aArtifact.lastModified() - 1000 * 60 );
113 
114         Verifier verifier = newVerifier( aDir.getAbsolutePath() );
115         verifier.setAutoclean( false );
116         verifier.deleteDirectory( "target" );
117         verifier.deleteArtifacts( "org.apache.maven.its.mng4368" );
118         verifier.executeGoal( "initialize" );
119         verifier.verifyErrorFreeLog();
120         verifier.resetStreams();
121 
122         File installedArtifact =
123             new File( verifier.getArtifactPath( "org.apache.maven.its.mng4368", "test", "0.1-SNAPSHOT", "jar" ) );
124 
125         String data = FileUtils.fileRead( installedArtifact, "UTF-8" );
126         assertTrue( data.indexOf( "Branch-A" ) > 0 );
127         assertFalse( data.contains( "Branch-B" ) );
128 
129         assertEquals( aArtifact.length(), bArtifact.length() );
130         assertTrue( aArtifact.lastModified() > bArtifact.lastModified() );
131         assertTrue( installedArtifact.lastModified() > bArtifact.lastModified() );
132 
133         verifier = newVerifier( bDir.getAbsolutePath() );
134         verifier.setAutoclean( false );
135         verifier.deleteDirectory( "target" );
136         verifier.executeGoal( "initialize" );
137         verifier.verifyErrorFreeLog();
138         verifier.resetStreams();
139 
140         data = FileUtils.fileRead( installedArtifact, "UTF-8" );
141         assertFalse( data.contains( "Branch-A" ) );
142         assertTrue( data.indexOf( "Branch-B" ) > 0 );
143 
144         long lastModified = installedArtifact.lastModified();
145         FileUtils.fileWrite( installedArtifact.getPath(), "UTF-8", "from Branch-C" );
146         installedArtifact.setLastModified( lastModified );
147 
148         verifier = newVerifier( bDir.getAbsolutePath() );
149         verifier.setAutoclean( false );
150         verifier.deleteDirectory( "target" );
151         verifier.setLogFileName( "log-b.txt" );
152         verifier.executeGoal( "initialize" );
153         verifier.verifyErrorFreeLog();
154         verifier.resetStreams();
155 
156         data = FileUtils.fileRead( installedArtifact, "UTF-8" );
157         assertFalse( data.contains( "Branch-B" ) );
158         assertTrue( data.indexOf( "Branch-C" ) > 0 );
159     }
160 
161 }