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 java.io.File;
23  import java.io.IOException;
24  import java.text.SimpleDateFormat;
25  import java.util.Calendar;
26  import java.util.Date;
27  import java.util.Locale;
28  
29  import org.apache.maven.it.util.ResourceExtractor;
30  import org.apache.maven.shared.utils.io.FileUtils;
31  
32  /**
33   * Downloads a snapshot dependency that was deployed with uniqueVersion = false, and checks it can be
34   * updated. See MNG-1908.
35   */
36  public class MavenIT0108SnapshotUpdateTest
37      extends AbstractMavenIntegrationTestCase
38  {
39      public MavenIT0108SnapshotUpdateTest()
40      {
41          super( ALL_MAVEN_VERSIONS );
42      }
43  
44      private Verifier verifier;
45  
46      private File artifact;
47  
48      private File repository;
49  
50      private File localRepoFile;
51  
52      private static final int TIME_OFFSET = 50000;
53  
54      protected void setUp()
55          throws Exception
56      {
57          super.setUp();
58  
59          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/it0108" );
60          verifier = newVerifier( testDir.getAbsolutePath() );
61          localRepoFile = getLocalRepoFile( verifier );
62          deleteLocalArtifact( verifier, localRepoFile );
63  
64          repository = new File( testDir, "repository" );
65          recreateRemoteRepository( repository );
66  
67          // create artifact in repository (TODO: into verifier)
68          artifact = new File( repository,
69                               "org/apache/maven/maven-core-it-support/1.0-SNAPSHOT/maven-core-it-support-1.0-SNAPSHOT.jar" );
70          artifact.getParentFile().mkdirs();
71          FileUtils.fileWrite( artifact.getAbsolutePath(), "originalArtifact" );
72  
73          verifier.assertArtifactNotPresent( "org.apache.maven", "maven-core-it-support", "1.0-SNAPSHOT", "jar" );
74      }
75  
76      public void testSnapshotUpdated()
77          throws Exception
78      {
79          verifier.executeGoal( "package" );
80  
81          verifier.verifyErrorFreeLog();
82          verifier.resetStreams();
83  
84          assertArtifactContents( "originalArtifact" );
85  
86          // set in the past to ensure it is downloaded
87          localRepoFile.setLastModified( System.currentTimeMillis() - TIME_OFFSET );
88  
89          FileUtils.fileWrite( artifact.getAbsolutePath(), "updatedArtifact" );
90  
91          verifier.executeGoal( "package" );
92  
93          assertArtifactContents( "updatedArtifact" );
94  
95          verifier.verifyErrorFreeLog();
96          verifier.resetStreams();
97      }
98  
99      public void testSnapshotUpdatedWithMetadata()
100         throws Exception
101     {
102         File metadata =
103             new File( repository, "org/apache/maven/maven-core-it-support/1.0-SNAPSHOT/maven-metadata.xml" );
104         FileUtils.fileWrite( metadata.getAbsolutePath(),
105                              constructMetadata( "1", System.currentTimeMillis() - TIME_OFFSET, true ) );
106 
107         verifier.executeGoal( "package" );
108 
109         verifier.verifyErrorFreeLog();
110         verifier.resetStreams();
111 
112         assertArtifactContents( "originalArtifact" );
113 
114         FileUtils.fileWrite( artifact.getAbsolutePath(), "updatedArtifact" );
115         metadata = new File( repository, "org/apache/maven/maven-core-it-support/1.0-SNAPSHOT/maven-metadata.xml" );
116         FileUtils.fileWrite( metadata.getAbsolutePath(), constructMetadata( "2", System.currentTimeMillis(), true ) );
117 
118         verifier.executeGoal( "package" );
119 
120         assertArtifactContents( "updatedArtifact" );
121 
122         verifier.verifyErrorFreeLog();
123         verifier.resetStreams();
124     }
125 
126     public void testSnapshotUpdatedWithLocalMetadata()
127         throws Exception
128     {
129         File localMetadata = getMetadataFile( "org/apache/maven", "maven-core-it-support", "1.0-SNAPSHOT" );
130 
131         FileUtils.deleteDirectory( localMetadata.getParentFile() );
132         assertFalse( localMetadata.getParentFile().exists() );
133         localMetadata.getParentFile().mkdirs();
134 
135         File metadata =
136             new File( repository, "org/apache/maven/maven-core-it-support/1.0-SNAPSHOT/maven-metadata.xml" );
137         FileUtils.fileWrite( metadata.getAbsolutePath(),
138                              constructMetadata( "1", System.currentTimeMillis() - TIME_OFFSET, true ) );
139 
140         verifier.executeGoal( "package" );
141 
142         verifier.verifyErrorFreeLog();
143         verifier.resetStreams();
144 
145         assertArtifactContents( "originalArtifact" );
146         assertFalse( localMetadata.exists() );
147 
148         FileUtils.fileWrite( localRepoFile.getAbsolutePath(), "localArtifact" );
149         FileUtils.fileWrite( localMetadata.getAbsolutePath(), constructLocalMetadata( "org.apache.maven",
150                                                                                       "maven-core-it-support",
151                                                                                       System.currentTimeMillis(),
152                                                                                       true ) );
153         // update the remote file, but we shouldn't be looking
154         artifact.setLastModified( System.currentTimeMillis() );
155 
156         verifier.executeGoal( "package" );
157 
158         assertArtifactContents( "localArtifact" );
159 
160         verifier.verifyErrorFreeLog();
161         verifier.resetStreams();
162 
163         Calendar cal = Calendar.getInstance();
164         cal.add( Calendar.YEAR, -1 );
165         FileUtils.fileWrite( localMetadata.getAbsolutePath(), constructLocalMetadata( "org.apache.maven",
166                                                                                       "maven-core-it-support",
167                                                                                       cal.getTimeInMillis(), true ) );
168         FileUtils.fileWrite( metadata.getAbsolutePath(),
169                              constructMetadata( "2", System.currentTimeMillis() - 2000, true ) );
170         artifact.setLastModified( System.currentTimeMillis() );
171 
172         verifier.executeGoal( "package" );
173 
174         assertArtifactContents( "originalArtifact" );
175 
176         verifier.verifyErrorFreeLog();
177         verifier.resetStreams();
178     }
179 
180     public void testSnapshotUpdatedWithMetadataUsingFileTimestamp()
181         throws Exception
182     {
183         File metadata =
184             new File( repository, "org/apache/maven/maven-core-it-support/1.0-SNAPSHOT/maven-metadata.xml" );
185         FileUtils.fileWrite( metadata.getAbsolutePath(),
186                              constructMetadata( "1", System.currentTimeMillis() - TIME_OFFSET, false ) );
187         metadata.setLastModified( System.currentTimeMillis() - TIME_OFFSET );
188 
189         verifier.executeGoal( "package" );
190 
191         verifier.verifyErrorFreeLog();
192         verifier.resetStreams();
193 
194         assertArtifactContents( "originalArtifact" );
195 
196         FileUtils.fileWrite( artifact.getAbsolutePath(), "updatedArtifact" );
197         metadata = new File( repository, "org/apache/maven/maven-core-it-support/1.0-SNAPSHOT/maven-metadata.xml" );
198         FileUtils.fileWrite( metadata.getAbsolutePath(), constructMetadata( "2", System.currentTimeMillis(), false ) );
199 
200         verifier.executeGoal( "package" );
201 
202         assertArtifactContents( "updatedArtifact" );
203 
204         verifier.verifyErrorFreeLog();
205         verifier.resetStreams();
206     }
207 
208     private File getMetadataFile( String groupId, String artifactId, String version )
209     {
210         return new File( verifier.getArtifactMetadataPath( groupId, artifactId, version, "maven-metadata-local.xml" ) );
211     }
212 
213     private void assertArtifactContents( String s )
214         throws IOException
215     {
216         verifier.assertArtifactPresent( "org.apache.maven", "maven-core-it-support", "1.0-SNAPSHOT", "jar" );
217         verifier.assertArtifactContents( "org.apache.maven", "maven-core-it-support", "1.0-SNAPSHOT", "jar", s );
218     }
219 
220     private static File deleteLocalArtifact( Verifier verifier, File localRepoFile )
221         throws IOException
222     {
223         verifier.deleteArtifact( "org.apache.maven", "maven-core-it-support", "1.0-SNAPSHOT", "jar" );
224         // this is to delete metadata - TODO: incorporate into deleteArtifact in verifier
225         FileUtils.deleteDirectory( localRepoFile.getParentFile() );
226         return localRepoFile;
227     }
228 
229     private static File getLocalRepoFile( Verifier verifier )
230     {
231         return new File(
232             verifier.getArtifactPath( "org.apache.maven", "maven-core-it-support", "1.0-SNAPSHOT", "jar" ) );
233     }
234 
235     private static void recreateRemoteRepository( File repository )
236         throws IOException
237     {
238         // create a repository (TODO: into verifier)
239         FileUtils.deleteDirectory( repository );
240         assertFalse( repository.exists() );
241         repository.mkdirs();
242     }
243 
244     private String constructMetadata( String buildNumber, long timestamp, boolean writeLastUpdated )
245     {
246         String ts = new SimpleDateFormat( "yyyyMMddHHmmss", Locale.US ).format( new Date( timestamp ) );
247 
248         return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><metadata>\n" + "<groupId>org.apache.maven</groupId>\n" +
249             "<artifactId>maven-core-it-support</artifactId>\n" + "<version>1.0-SNAPSHOT</version>\n" +
250             "<versioning>\n" + "<snapshot>\n" + "<buildNumber>" + buildNumber + "</buildNumber>\n" + "</snapshot>\n" +
251             ( writeLastUpdated ? "<lastUpdated>" + ts + "</lastUpdated>\n" : "" ) + "</versioning>\n" + "</metadata>";
252     }
253 
254     private String constructLocalMetadata( String groupId, String artifactId, long timestamp, boolean writeLastUpdated )
255     {
256         String ts = new SimpleDateFormat( "yyyyMMddHHmmss", Locale.US ).format( new Date( timestamp ) );
257 
258         return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><metadata>\n" + "  <groupId>" + groupId + "</groupId>\n" +
259             "  <artifactId>" + artifactId + "</artifactId>\n" + "  <version>1.0-SNAPSHOT</version>\n" +
260             "  <versioning>\n" + "    <snapshot>\n" + "      <localCopy>true</localCopy>\n" + "    </snapshot>\n" +
261             ( writeLastUpdated ? "    <lastUpdated>" + ts + "</lastUpdated>\n" : "" ) + "  </versioning>\n" +
262             "</metadata>";
263     }
264 }