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