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.text.SimpleDateFormat;
24  import java.util.Date;
25  import java.util.TimeZone;
26  
27  import org.apache.maven.it.util.ResourceExtractor;
28  import org.apache.maven.shared.utils.io.FileUtils;
29  
30  /**
31   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-2790">MNG-2790</a>.
32   *
33   * @author Benjamin Bentmann
34   *
35   */
36  public class MavenITmng2790LastUpdatedMetadataTest
37      extends AbstractMavenIntegrationTestCase
38  {
39  
40      public MavenITmng2790LastUpdatedMetadataTest()
41      {
42          super( "(2.0.4,)" );
43      }
44  
45      /**
46       * Verify that the field lastUpdated of existing local repo metadata is updated upon install of new a snapshot.
47       *
48       * @throws Exception in case of failure
49       */
50      public void testitMNG2790()
51          throws Exception
52      {
53          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-2790" );
54  
55          Date now = new Date();
56  
57          /*
58           * Phase 1: Install initial snapshot into local repo.
59           */
60          Verifier verifier = newVerifier( testDir.getAbsolutePath() );
61          verifier.deleteArtifacts( "org.apache.maven.its.mng2790" );
62          verifier.setAutoclean( false );
63          verifier.executeGoal( "validate" );
64          verifier.verifyErrorFreeLog();
65          verifier.resetStreams();
66  
67          File metadataArtifactVersionFile =
68              new File( verifier.getArtifactMetadataPath( "org.apache.maven.its.mng2790", "project", "1.0-SNAPSHOT" ) );
69          File metadataArtifactFile =
70              new File( verifier.getArtifactMetadataPath( "org.apache.maven.its.mng2790", "project" ) );
71  
72          Date artifactVersionLastUpdated1 = getLastUpdated( metadataArtifactVersionFile );
73          Date artifactLastUpdated1 = getLastUpdated( metadataArtifactFile );
74  
75          // sanity check: timestamps shouldn't differ by more than 10 min from now (i.e. timezone is UTC)
76          assertTrue( artifactVersionLastUpdated1 + " ~ " + now,
77              Math.abs( artifactVersionLastUpdated1.getTime() - now.getTime() ) < 10 * 60 * 1000 );
78          assertTrue( artifactLastUpdated1 + " ~ " + now,
79              Math.abs( artifactLastUpdated1.getTime() - now.getTime() ) < 10 * 60 * 1000 );
80  
81          // enforce some advance of time
82          Thread.sleep( 1000 );
83  
84          /*
85           * Phase 2: Re-install snapshot and check for proper timestamp update in local metadata.
86           */
87          verifier = newVerifier( testDir.getAbsolutePath() );
88          verifier.setAutoclean( false );
89          verifier.executeGoal( "validate" );
90          verifier.verifyErrorFreeLog();
91          verifier.resetStreams();
92  
93          Date artifactVersionLastUpdated2 = getLastUpdated( metadataArtifactVersionFile );
94          Date artifactLastUpdated2 = getLastUpdated( metadataArtifactFile );
95  
96          // check that new timestamps are strictly later than from original install
97          assertTrue( artifactVersionLastUpdated1 + " < " + artifactVersionLastUpdated2,
98              artifactVersionLastUpdated2.after( artifactVersionLastUpdated1 ) );
99          assertTrue( artifactLastUpdated1 + " < " + artifactLastUpdated2,
100             artifactLastUpdated2.after( artifactLastUpdated1 ) );
101     }
102 
103     private Date getLastUpdated( File metadataFile )
104         throws Exception
105     {
106         String xml = FileUtils.fileRead( metadataFile, "UTF-8" );
107         String timestamp = xml.replaceAll( "(?s)\\A.*<lastUpdated>\\s*([0-9]++)\\s*</lastUpdated>.*\\z", "$1" );
108         SimpleDateFormat format = new SimpleDateFormat( "yyyyMMddHHmmss" );
109         format.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
110         return format.parse( timestamp );
111     }
112 
113 }