View Javadoc
1   package org.apache.maven.repository.internal;
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 static org.junit.Assert.assertTrue;
23  
24  import java.text.SimpleDateFormat;
25  import java.util.Arrays;
26  import java.util.Date;
27  import java.util.GregorianCalendar;
28  import java.util.HashSet;
29  import java.util.Locale;
30  import java.util.Set;
31  
32  import org.apache.maven.artifact.repository.metadata.Metadata;
33  import org.eclipse.aether.artifact.DefaultArtifact;
34  import org.junit.After;
35  import org.junit.Before;
36  import org.junit.Test;
37  
38  public class RemoteSnapshotMetadataTest
39  {
40      private Locale defaultLocale;
41  
42      @Before
43      public void setLocaleToUseBuddhistCalendar()
44      {
45          defaultLocale = Locale.getDefault();
46          Locale.setDefault( new Locale( "th", "TH" ) );
47      }
48  
49      @After
50      public void restoreLocale()
51      {
52          Locale.setDefault( defaultLocale );
53      }
54  
55      static String gregorianDate()
56      {
57          SimpleDateFormat df = new SimpleDateFormat( "yyyyMMdd" );
58          df.setCalendar( new GregorianCalendar() );
59          df.setTimeZone( RemoteSnapshotMetadata.DEFAULT_SNAPSHOT_TIME_ZONE );
60          return df.format( new Date() );
61      }
62  
63      @Test
64      public void gregorianCalendarIsUsed()
65      {
66          String dateBefore = gregorianDate();
67  
68          RemoteSnapshotMetadata metadata = new RemoteSnapshotMetadata(
69                  new DefaultArtifact( "a:b:1-SNAPSHOT" ), false);
70          metadata.merge( new Metadata() );
71  
72          String dateAfter = gregorianDate();
73  
74          String ts = metadata.metadata.getVersioning().getSnapshot().getTimestamp();
75          String datePart = ts.replaceAll( "\\..*", "" );
76  
77          /* Allow for this test running across midnight */
78          Set<String> expected = new HashSet<String>( Arrays.asList( dateBefore, dateAfter ) );
79          assertTrue( "Expected " + datePart + " to be in " + expected,
80                  expected.contains( datePart ) );
81      }
82  }