View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.repository.internal;
20  
21  import java.text.SimpleDateFormat;
22  import java.util.Arrays;
23  import java.util.Date;
24  import java.util.GregorianCalendar;
25  import java.util.HashSet;
26  import java.util.Locale;
27  import java.util.Set;
28  import java.util.regex.Pattern;
29  
30  import org.apache.maven.artifact.repository.metadata.Metadata;
31  import org.eclipse.aether.artifact.DefaultArtifact;
32  import org.junit.jupiter.api.AfterEach;
33  import org.junit.jupiter.api.BeforeEach;
34  import org.junit.jupiter.api.Test;
35  
36  import static org.junit.jupiter.api.Assertions.assertEquals;
37  import static org.junit.jupiter.api.Assertions.assertTrue;
38  
39  class RemoteSnapshotMetadataTest {
40      private Locale defaultLocale;
41  
42      private static final Pattern DATE_FILTER = Pattern.compile("\\..*");
43  
44      @BeforeEach
45      void setLocaleToUseBuddhistCalendar() {
46          defaultLocale = Locale.getDefault();
47          Locale.setDefault(new Locale("th", "TH"));
48      }
49  
50      @AfterEach
51      void restoreLocale() {
52          Locale.setDefault(defaultLocale);
53      }
54  
55      static String gregorianDate() {
56          SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
57          df.setCalendar(new GregorianCalendar());
58          df.setTimeZone(RemoteSnapshotMetadata.DEFAULT_SNAPSHOT_TIME_ZONE);
59          return df.format(new Date());
60      }
61  
62      @Test
63      void gregorianCalendarIsUsed() {
64          String dateBefore = gregorianDate();
65  
66          RemoteSnapshotMetadata metadata =
67                  new RemoteSnapshotMetadata(new DefaultArtifact("a:b:1-SNAPSHOT"), new Date(), null);
68          metadata.merge(new Metadata());
69  
70          String dateAfter = gregorianDate();
71  
72          String ts = metadata.metadata.getVersioning().getSnapshot().getTimestamp();
73          String datePart = DATE_FILTER.matcher(ts).replaceAll("");
74  
75          /* Allow for this test running across midnight */
76          Set<String> expected = new HashSet<>(Arrays.asList(dateBefore, dateAfter));
77          assertTrue(expected.contains(datePart), "Expected " + datePart + " to be in " + expected);
78      }
79  
80      @Test
81      void buildNumberNotSet() {
82          RemoteSnapshotMetadata metadata =
83                  new RemoteSnapshotMetadata(new DefaultArtifact("a:b:1-SNAPSHOT"), new Date(), null);
84          metadata.merge(new Metadata());
85  
86          int buildNumber = metadata.metadata.getVersioning().getSnapshot().getBuildNumber();
87          assertEquals(1, buildNumber);
88      }
89  
90      @Test
91      void buildNumberSet() {
92          RemoteSnapshotMetadata metadata =
93                  new RemoteSnapshotMetadata(new DefaultArtifact("a:b:1-SNAPSHOT"), new Date(), 42);
94          metadata.merge(new Metadata());
95  
96          int buildNumber = metadata.metadata.getVersioning().getSnapshot().getBuildNumber();
97          assertEquals(42, buildNumber);
98      }
99  }