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