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.util.ResourceExtractor;
23  
24  import java.io.File;
25  import java.util.Date;
26  import java.util.Properties;
27  import java.text.SimpleDateFormat;
28  
29  /**
30   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-2562">MNG-2562</a>.
31   *
32   *
33   */
34  public class MavenITmng2562TimestampTest
35      extends AbstractMavenIntegrationTestCase
36  {
37  
38      public MavenITmng2562TimestampTest()
39      {
40          super( "[2.1.0-M1,3.2.2)" ); // 2.1.0+ only
41      }
42  
43      public void testitDefaultFormat()
44          throws Exception
45      {
46          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-2562/default" );
47  
48          Verifier verifier = newVerifier( testDir.getAbsolutePath() );
49          verifier.setAutoclean( false );
50          verifier.deleteDirectory( "target" );
51          verifier.executeGoal( "validate" );
52          verifier.verifyErrorFreeLog();
53          verifier.resetStreams();
54  
55          Date now = new Date();
56  
57          Properties props = verifier.loadProperties( "target/pom.properties" );
58  
59          String timestamp1 = props.getProperty( "project.properties.timestamp1", "" );
60          assertTrue( timestamp1, timestamp1.matches( "[0-9]{8}-[0-9]{4}" ) );
61          Date date = new SimpleDateFormat( "yyyyMMdd-HHmm" ).parse( timestamp1 );
62          assertTrue( now + " vs " + date, Math.abs( now.getTime() - date.getTime() ) < 24 * 60 * 60 * 1000 );
63  
64          String timestamp2 = props.getProperty( "project.properties.timestamp2", "" );
65          assertEquals( timestamp1, timestamp2 );
66      }
67  
68      public void testitCustomFormat()
69          throws Exception
70      {
71          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-2562/custom" );
72  
73          Verifier verifier = newVerifier( testDir.getAbsolutePath() );
74          verifier.setAutoclean( false );
75          verifier.deleteDirectory( "target" );
76          verifier.executeGoal( "validate" );
77          verifier.verifyErrorFreeLog();
78          verifier.resetStreams();
79  
80          Date now = new Date();
81  
82          Properties props = verifier.loadProperties( "target/pom.properties" );
83  
84          String timestamp1 = props.getProperty( "project.properties.timestamp", "" );
85          Date date = new SimpleDateFormat( "mm:HH dd-MM-yyyy" ).parse( timestamp1 );
86          assertTrue( now + " vs " + date, Math.abs( now.getTime() - date.getTime() ) < 24 * 60 * 60 * 1000 );
87      }
88  
89      public void testitSameValueAcrossModules()
90          throws Exception
91      {
92          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-2562/reactor" );
93  
94          Verifier verifier = newVerifier( testDir.getAbsolutePath() );
95          verifier.setAutoclean( false );
96          verifier.deleteDirectory( "target" );
97          verifier.deleteDirectory( "child-1/target" );
98          verifier.deleteDirectory( "child-2/target" );
99          verifier.deleteDirectory( "child-3/target" );
100         verifier.executeGoal( "validate" );
101         verifier.verifyErrorFreeLog();
102         verifier.resetStreams();
103 
104         Properties props = verifier.loadProperties( "target/pom.properties" );
105         String timestamp = props.getProperty( "project.properties.timestamp", "" );
106 
107         Properties props1 = verifier.loadProperties( "child-1/target/pom.properties" );
108         String timestamp1 = props1.getProperty( "project.properties.timestamp", "" );
109 
110         Properties props2 = verifier.loadProperties( "child-2/target/pom.properties" );
111         String timestamp2 = props2.getProperty( "project.properties.timestamp", "" );
112 
113         Properties props3 = verifier.loadProperties( "child-3/target/pom.properties" );
114         String timestamp3 = props3.getProperty( "project.properties.timestamp", "" );
115 
116         assertEquals( timestamp, timestamp1 );
117         assertEquals( timestamp, timestamp2 );
118         assertEquals( timestamp, timestamp3 );
119     }
120 
121 }