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.io.InputStream;
25  import java.nio.file.Files;
26  import java.nio.file.Path;
27  import java.nio.file.Paths;
28  import java.util.Properties;
29  
30  import org.apache.maven.artifact.repository.metadata.Metadata;
31  import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
32  import org.apache.maven.it.util.ResourceExtractor;
33  
34  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
35  
36  public class MavenITmng6754TimestampInMultimoduleProject
37          extends AbstractMavenIntegrationTestCase
38  {
39      private static final String RESOURCE_PATH = "/mng-6754-version-timestamp-in-multimodule-build";
40      private static final String VERSION = "1.0-SNAPSHOT";
41  
42  
43      public MavenITmng6754TimestampInMultimoduleProject()
44      {
45          super( "[3.8.2,)" );
46      }
47  
48      public void testArtifactsHaveSameTimestamp()
49              throws Exception
50      {
51          final File testDir = ResourceExtractor.simpleExtractResources( getClass(), RESOURCE_PATH );
52          final Verifier verifier = newVerifier( testDir.getAbsolutePath() );
53          final Path localRepoDir = Paths.get( verifier.getLocalRepository() );
54          final Path remoteRepoDir = Paths.get( verifier.getBasedir(), "repo" );
55  
56          verifier.deleteDirectory( "repo" );
57          verifier.deleteArtifacts ( "org.apache.maven.its.mng6754" );
58          verifier.addCliOption( "-Drepodir=" + remoteRepoDir );
59          verifier.executeGoal( "deploy" );
60          verifier.verifyErrorFreeLog();
61  
62          final Properties props = verifier.loadProperties( "target/timestamp.properties" );
63          // Reference timestamp
64          final String mavenBuildTimestamp = props.getProperty( "project.properties.timestamp" );
65  
66          final Metadata parentMetadataLocal = getMetadata( getLocalMetadataPath( localRepoDir, "parent", null ) );
67          final Metadata aMetadataLocal = getMetadata( getLocalMetadataPath( localRepoDir, "child-a", null ) );
68          final Metadata bMetadataLocal = getMetadata( getLocalMetadataPath( localRepoDir, "child-b", null ) );
69  
70          final String parentLastUpdatedLocal = parentMetadataLocal.getVersioning().getLastUpdated();
71          final String aLastUpdatedLocal = aMetadataLocal.getVersioning().getLastUpdated();
72          final String bLastUpdatedLocal = bMetadataLocal.getVersioning().getLastUpdated();
73  
74          assertEquals ( "parent", "local", "lastUpdated", mavenBuildTimestamp, parentLastUpdatedLocal );
75          assertEquals ( "child-a", "local", "lastUpdated", mavenBuildTimestamp, aLastUpdatedLocal );
76          assertEquals ( "child-b", "local", "lastUpdated", mavenBuildTimestamp, bLastUpdatedLocal );
77  
78          final Metadata parentVersionedMetadataLocal = getMetadata( getLocalMetadataPath( localRepoDir, "parent", VERSION ) );
79          final Metadata aVersionedMetadataLocal = getMetadata( getLocalMetadataPath( localRepoDir, "child-a", VERSION ) );
80          final Metadata bVersionedMetadataLocal = getMetadata( getLocalMetadataPath( localRepoDir, "child-b", VERSION ) );
81  
82          final String parentVersionedLastUpdatedLocal = parentVersionedMetadataLocal.getVersioning().getLastUpdated();
83          final String parentVersionedSnapshotVersionUpdatedLocal = parentVersionedMetadataLocal.getVersioning().getSnapshotVersions().get( 0 ).getUpdated();
84          final String aLastVersionedUpdatedLocal = aVersionedMetadataLocal.getVersioning().getLastUpdated();
85          final String aVersionedSnapshotVersionUpdated1Local = aVersionedMetadataLocal.getVersioning().getSnapshotVersions().get( 0 ).getUpdated();
86          final String aVersionedSnapshotVersionUpdated2Local = aVersionedMetadataLocal.getVersioning().getSnapshotVersions().get( 1 ).getUpdated();
87          final String bLastVersionedUpdatedLocal = bVersionedMetadataLocal.getVersioning().getLastUpdated();
88          final String bVersionedSnapshotVersionUpdated1Local = bVersionedMetadataLocal.getVersioning().getSnapshotVersions().get( 0 ).getUpdated();
89          final String bVersionedSnapshotVersionUpdated2Local = bVersionedMetadataLocal.getVersioning().getSnapshotVersions().get( 1 ).getUpdated();
90  
91          assertEquals ( "parent", "local", "lastUpdated", mavenBuildTimestamp, parentVersionedLastUpdatedLocal );
92          assertEquals ( "parent", "local", "snapshotVersion[0]/updated", mavenBuildTimestamp, parentVersionedSnapshotVersionUpdatedLocal );
93          assertEquals ( "child-a", "local", "lastUpdated", mavenBuildTimestamp, aLastVersionedUpdatedLocal );
94          assertEquals ( "child-a", "local", "snapshotVersion[0]/updated", mavenBuildTimestamp, aVersionedSnapshotVersionUpdated1Local );
95          assertEquals ( "child-a", "local", "snapshotVersion[1]/updated", mavenBuildTimestamp, aVersionedSnapshotVersionUpdated2Local );
96          assertEquals ( "child-b", "local", "lastUpdated", mavenBuildTimestamp, bLastVersionedUpdatedLocal );
97          assertEquals ( "child-b", "local", "snapshotVersion[0]/updated", mavenBuildTimestamp, bVersionedSnapshotVersionUpdated1Local );
98          assertEquals ( "child-b", "local", "snapshotVersion[1]/updated", mavenBuildTimestamp, bVersionedSnapshotVersionUpdated2Local );
99  
100         final Metadata parentMetadataRemote = getMetadata( getRemoteMetadataPath( remoteRepoDir, "parent", null ) );
101         final Metadata aMetadataRemote = getMetadata( getRemoteMetadataPath( remoteRepoDir, "child-a", null ) );
102         final Metadata bMetadataRemote = getMetadata( getRemoteMetadataPath( remoteRepoDir, "child-b", null ) );
103 
104         final String parentLastUpdatedRemote = parentMetadataRemote.getVersioning().getLastUpdated();
105         final String aLastUpdatedRemote = aMetadataRemote.getVersioning().getLastUpdated();
106         final String bLastUpdatedRemote = bMetadataRemote.getVersioning().getLastUpdated();
107 
108         assertEquals ( "parent", "remote", "lastUpdated", mavenBuildTimestamp, parentLastUpdatedRemote );
109         assertEquals ( "child-a", "remote", "lastUpdated", mavenBuildTimestamp, aLastUpdatedRemote );
110         assertEquals ( "child-b", "remote", "lastUpdated", mavenBuildTimestamp, bLastUpdatedRemote );
111 
112         final Metadata parentVersionedMetadataRemote = getMetadata( getRemoteMetadataPath( remoteRepoDir, "parent", VERSION ) );
113         final Metadata aVersionedMetadataRemote = getMetadata( getRemoteMetadataPath( remoteRepoDir, "child-a", VERSION ) );
114         final Metadata bVersionedMetadataRemote = getMetadata( getRemoteMetadataPath( remoteRepoDir, "child-b", VERSION ) );
115 
116         final String parentVersionedLastUpdatedRemote = parentVersionedMetadataRemote.getVersioning().getLastUpdated();
117         final String parentVersionedSnapshotTimestamp = parentVersionedMetadataRemote.getVersioning().getSnapshot().getTimestamp().replace( ".", "" );
118         final String parentVersionedSnapshotVersionUpdatedRemote = parentVersionedMetadataRemote.getVersioning().getSnapshotVersions().get( 0 ).getUpdated();
119         final String parentVersionedSnapshotVersionValueRemote = parentVersionedMetadataRemote.getVersioning().getSnapshotVersions().get( 0 ).getVersion();
120         final String aLastVersionedUpdatedRemote = aVersionedMetadataRemote.getVersioning().getLastUpdated();
121         final String aVersionedSnapshotTimestamp = aVersionedMetadataRemote.getVersioning().getSnapshot().getTimestamp().replace( ".", "" );
122         final String aVersionedSnapshotVersionUpdated1Remote = aVersionedMetadataRemote.getVersioning().getSnapshotVersions().get( 0 ).getUpdated();
123         final String aVersionedSnapshotVersionValue1Remote = aVersionedMetadataRemote.getVersioning().getSnapshotVersions().get( 0 ).getVersion();
124         final String aVersionedSnapshotVersionUpdated2Remote = aVersionedMetadataRemote.getVersioning().getSnapshotVersions().get( 1 ).getUpdated();
125         final String aVersionedSnapshotVersionValue2Remote = aVersionedMetadataRemote.getVersioning().getSnapshotVersions().get( 1 ).getVersion();
126         final String bLastVersionedUpdatedRemote = bVersionedMetadataRemote.getVersioning().getLastUpdated();
127         final String bVersionedSnapshotTimestamp = bVersionedMetadataRemote.getVersioning().getSnapshot().getTimestamp().replace( ".", "" );
128         final String bVersionedSnapshotVersionUpdated1Remote = bVersionedMetadataRemote.getVersioning().getSnapshotVersions().get( 0 ).getUpdated();
129         final String bVersionedSnapshotVersionValue1Remote = bVersionedMetadataRemote.getVersioning().getSnapshotVersions().get( 0 ).getVersion();
130         final String bVersionedSnapshotVersionUpdated2Remote = bVersionedMetadataRemote.getVersioning().getSnapshotVersions().get( 1 ).getUpdated();
131         final String bVersionedSnapshotVersionValue2Remote = bVersionedMetadataRemote.getVersioning().getSnapshotVersions().get( 1 ).getVersion();
132 
133         assertEquals ( "parent", "remote", "lastUpdated", mavenBuildTimestamp, parentVersionedLastUpdatedRemote );
134         assertEquals ( "parent", "remote", "snapshot/timestamp", mavenBuildTimestamp, parentVersionedSnapshotTimestamp );
135         assertEquals ( "parent", "remote", "snapshotVersion[0]/updated", mavenBuildTimestamp, parentVersionedSnapshotVersionUpdatedRemote );
136         assertEquals ( "parent", "remote", "snapshotVersion[0]/value", mavenBuildTimestamp, parentVersionedSnapshotVersionValueRemote.substring( 4, 19 ).replace( ".", "" ) );
137         assertEquals ( "child-a", "remote", "lastUpdated", mavenBuildTimestamp, aLastVersionedUpdatedRemote );
138         assertEquals ( "child-a", "remote", "snapshot/timestamp", mavenBuildTimestamp, aVersionedSnapshotTimestamp );
139         assertEquals ( "child-a", "remote", "snapshotVersion[0]/updated", mavenBuildTimestamp, aVersionedSnapshotVersionUpdated1Remote );
140         assertEquals ( "child-a", "remote", "snapshotVersion[0]/value", mavenBuildTimestamp, aVersionedSnapshotVersionValue1Remote.substring( 4, 19 ).replace( ".", "" ) );
141         assertEquals ( "child-a", "remote", "snapshotVersion[1]/updated", mavenBuildTimestamp, aVersionedSnapshotVersionUpdated2Remote );
142         assertEquals ( "child-a", "remote", "snapshotVersion[1]/value", mavenBuildTimestamp, aVersionedSnapshotVersionValue2Remote.substring( 4, 19 ).replace( ".", "" ) );
143         assertEquals ( "child-b", "remote", "lastUpdated", mavenBuildTimestamp, bLastVersionedUpdatedRemote );
144         assertEquals ( "child-b", "remote", "snapshot/timestamp", mavenBuildTimestamp, bVersionedSnapshotTimestamp );
145         assertEquals ( "child-b", "remote", "snapshotVersion[0]/updated", mavenBuildTimestamp, bVersionedSnapshotVersionUpdated1Remote );
146         assertEquals ( "child-b", "remote", "snapshotVersion[0]/value", mavenBuildTimestamp, bVersionedSnapshotVersionValue1Remote.substring( 4, 19 ).replace( ".", "" ) );
147         assertEquals ( "child-b", "remote", "snapshotVersion[1]/updated", mavenBuildTimestamp, bVersionedSnapshotVersionUpdated2Remote );
148         assertEquals ( "child-b", "remote", "snapshotVersion[1]/value", mavenBuildTimestamp, bVersionedSnapshotVersionValue2Remote.substring( 4, 19 ).replace( ".", "" ) );
149         assertPathExists( remoteRepoDir, "parent", "remote", VERSION, "parent-" + parentVersionedSnapshotVersionValueRemote + ".pom" );
150         assertPathExists( remoteRepoDir, "child-a", "remote", VERSION, "child-a-" + aVersionedSnapshotVersionValue1Remote + ".pom" );
151         assertPathExists( remoteRepoDir, "child-a", "remote", VERSION, "child-a-" + aVersionedSnapshotVersionValue2Remote + ".jar" );
152         assertPathExists( remoteRepoDir, "child-b", "remote", VERSION, "child-b-" + bVersionedSnapshotVersionValue1Remote + ".pom" );
153         assertPathExists( remoteRepoDir, "child-b", "remote", VERSION, "child-b-" + bVersionedSnapshotVersionValue2Remote + ".jar" );
154     }
155 
156     private Path getLocalMetadataPath( final Path repoDir, final String moduleName, String version )
157     {
158         return getRepoFile(repoDir, moduleName, version, "maven-metadata-local.xml" );
159     }
160 
161     private Path getRemoteMetadataPath( final Path repoDir, final String moduleName, String version )
162     {
163         return getRepoFile(repoDir, moduleName, version, "maven-metadata.xml" );
164     }
165 
166     private Path getRepoFile( final Path repoDir, final String moduleName, String version, String fileName )
167     {
168         final Path mng6754Path = Paths.get( "org", "apache", "maven", "its", "mng6754" );
169         Path modulePath = repoDir.resolve( mng6754Path.resolve( moduleName ) );
170         if ( version != null )
171         {
172             modulePath = modulePath.resolve( version );
173         }
174         return modulePath.resolve( fileName );
175     }
176 
177     private Metadata getMetadata( final Path metadataFile ) throws IOException, XmlPullParserException
178     {
179         MetadataXpp3Reader r = new MetadataXpp3Reader();
180         try ( InputStream is = Files.newInputStream( metadataFile ) )
181         {
182             return r.read( is );
183         }
184     }
185 
186     private void assertEquals( String moduleName, String location, String field, String expected, String actual )
187     {
188         String phase = null;
189         switch ( location )
190         {
191         case "local":
192             phase = "Installed";
193             break;
194         case "remote":
195             phase = "Deployed";
196             break;
197         }
198         assertEquals( String.format( "%s %s module should have equal %s %s with the Maven build timestamp",
199                  phase, moduleName, location, field ), expected, actual );
200     }
201 
202     private void assertPathExists( Path repoDir, String moduleName, String location, String version, String fileName )
203     {
204         String phase = null;
205         switch ( location )
206         {
207         case "local":
208             phase = "Installed";
209             break;
210         case "remote":
211             phase = "Deployed";
212             break;
213         }
214         Path file = getRepoFile( repoDir, moduleName, version, fileName );
215         assertTrue( String.format( "%s %s module %s file %s should exist",
216                  phase, moduleName, location, file ), Files.exists( file ) );
217     }
218 }