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.index.artifact;
20  
21  import java.text.ParseException;
22  import java.text.SimpleDateFormat;
23  
24  import org.junit.Test;
25  
26  import static org.junit.Assert.assertEquals;
27  
28  public class M1GavCalculatorTest {
29      private M1GavCalculator gavCalculator = new M1GavCalculator();
30  
31      private SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd.HHmmss");
32  
33      protected Long parseTimestamp(String timeStamp) throws ParseException {
34          if (timeStamp == null) {
35              return null;
36          } else {
37              return formatter.parse(timeStamp).getTime();
38          }
39      }
40  
41      @Test
42      public void testGav() throws Exception {
43          Gav gav = gavCalculator.pathToGav("/org.jruby/javadocs/jruby-1.0RC1-SNAPSHOT-javadoc.jar");
44  
45          assertEquals("org.jruby", gav.getGroupId());
46          assertEquals("jruby", gav.getArtifactId());
47          assertEquals("1.0RC1-SNAPSHOT", gav.getVersion());
48          assertEquals("1.0RC1-SNAPSHOT", gav.getBaseVersion());
49          assertEquals("javadoc", gav.getClassifier());
50          assertEquals("jar", gav.getExtension());
51          assertEquals(null, gav.getSnapshotBuildNumber());
52          assertEquals(null, gav.getSnapshotTimeStamp());
53          assertEquals("jruby-1.0RC1-SNAPSHOT-javadoc.jar", gav.getName());
54          assertEquals(true, gav.isSnapshot());
55          assertEquals(false, gav.isHash());
56          assertEquals(null, gav.getHashType());
57  
58          String path = gavCalculator.gavToPath(gav);
59          assertEquals("/org.jruby/javadocs/jruby-1.0RC1-SNAPSHOT-javadoc.jar", path);
60  
61          gav = gavCalculator.pathToGav("/org.jruby/jars/jruby-1.0RC1-SNAPSHOT.jar");
62  
63          assertEquals("org.jruby", gav.getGroupId());
64          assertEquals("jruby", gav.getArtifactId());
65          assertEquals("1.0RC1-SNAPSHOT", gav.getVersion());
66          assertEquals("1.0RC1-SNAPSHOT", gav.getBaseVersion());
67          assertEquals(null, gav.getClassifier());
68          assertEquals("jar", gav.getExtension());
69          assertEquals(null, gav.getSnapshotBuildNumber());
70          assertEquals(null, gav.getSnapshotTimeStamp());
71          assertEquals("jruby-1.0RC1-SNAPSHOT.jar", gav.getName());
72          assertEquals(true, gav.isSnapshot());
73          assertEquals(false, gav.isHash());
74          assertEquals(null, gav.getHashType());
75  
76          path = gavCalculator.gavToPath(gav);
77          assertEquals("/org.jruby/jars/jruby-1.0RC1-SNAPSHOT.jar", path);
78  
79          gav = gavCalculator.pathToGav("/org.jruby/jars/jruby-1.0RC1-SNAPSHOT.jar.md5");
80  
81          assertEquals("org.jruby", gav.getGroupId());
82          assertEquals("jruby", gav.getArtifactId());
83          assertEquals("1.0RC1-SNAPSHOT", gav.getVersion());
84          assertEquals("1.0RC1-SNAPSHOT", gav.getBaseVersion());
85          assertEquals(null, gav.getClassifier());
86          assertEquals("jar", gav.getExtension());
87          assertEquals(null, gav.getSnapshotBuildNumber());
88          assertEquals(null, gav.getSnapshotTimeStamp());
89          assertEquals("jruby-1.0RC1-SNAPSHOT.jar.md5", gav.getName());
90          assertEquals(true, gav.isSnapshot());
91          assertEquals(true, gav.isHash());
92          assertEquals(Gav.HashType.md5, gav.getHashType());
93  
94          path = gavCalculator.gavToPath(gav);
95          assertEquals("/org.jruby/jars/jruby-1.0RC1-SNAPSHOT.jar.md5", path);
96  
97          gav = gavCalculator.pathToGav("/org.jruby/javadocs/jruby-1.0-javadoc.jar");
98  
99          assertEquals("org.jruby", gav.getGroupId());
100         assertEquals("jruby", gav.getArtifactId());
101         assertEquals("1.0", gav.getVersion());
102         assertEquals("1.0", gav.getBaseVersion());
103         assertEquals("javadoc", gav.getClassifier());
104         assertEquals("jar", gav.getExtension());
105         assertEquals(null, gav.getSnapshotBuildNumber());
106         assertEquals(null, gav.getSnapshotTimeStamp());
107         assertEquals("jruby-1.0-javadoc.jar", gav.getName());
108         assertEquals(false, gav.isSnapshot());
109         assertEquals(false, gav.isHash());
110         assertEquals(null, gav.getHashType());
111 
112         path = gavCalculator.gavToPath(gav);
113         assertEquals("/org.jruby/javadocs/jruby-1.0-javadoc.jar", path);
114 
115         gav = gavCalculator.pathToGav("/org.jruby/javadocs/jruby-1.0-javadoc.jar.sha1");
116 
117         assertEquals("org.jruby", gav.getGroupId());
118         assertEquals("jruby", gav.getArtifactId());
119         assertEquals("1.0", gav.getVersion());
120         assertEquals("1.0", gav.getBaseVersion());
121         assertEquals("javadoc", gav.getClassifier());
122         assertEquals("jar", gav.getExtension());
123         assertEquals(null, gav.getSnapshotBuildNumber());
124         assertEquals(null, gav.getSnapshotTimeStamp());
125         assertEquals("jruby-1.0-javadoc.jar.sha1", gav.getName());
126         assertEquals(false, gav.isSnapshot());
127         assertEquals(true, gav.isHash());
128         assertEquals(Gav.HashType.sha1, gav.getHashType());
129 
130         path = gavCalculator.gavToPath(gav);
131         assertEquals("/org.jruby/javadocs/jruby-1.0-javadoc.jar.sha1", path);
132 
133         gav = gavCalculator.pathToGav("/org.jruby/jars/jruby-1.0.jar");
134 
135         assertEquals("org.jruby", gav.getGroupId());
136         assertEquals("jruby", gav.getArtifactId());
137         assertEquals("1.0", gav.getVersion());
138         assertEquals("1.0", gav.getBaseVersion());
139         assertEquals(null, gav.getClassifier());
140         assertEquals("jar", gav.getExtension());
141         assertEquals(null, gav.getSnapshotBuildNumber());
142         assertEquals(null, gav.getSnapshotTimeStamp());
143         assertEquals("jruby-1.0.jar", gav.getName());
144         assertEquals(false, gav.isSnapshot());
145         assertEquals(false, gav.isHash());
146         assertEquals(null, gav.getHashType());
147 
148         path = gavCalculator.gavToPath(gav);
149         assertEquals("/org.jruby/jars/jruby-1.0.jar", path);
150 
151         gav = gavCalculator.pathToGav("/maven/jars/dom4j-1.7-20060614.jar");
152 
153         assertEquals("maven", gav.getGroupId());
154         assertEquals("dom4j", gav.getArtifactId());
155         assertEquals("1.7-20060614", gav.getVersion());
156         assertEquals("1.7-20060614", gav.getBaseVersion());
157         assertEquals(null, gav.getClassifier());
158         assertEquals("jar", gav.getExtension());
159         assertEquals(null, gav.getSnapshotBuildNumber());
160         assertEquals(null, gav.getSnapshotTimeStamp());
161         assertEquals("dom4j-1.7-20060614.jar", gav.getName());
162         assertEquals(false, gav.isSnapshot());
163         assertEquals(false, gav.isHash());
164         assertEquals(null, gav.getHashType());
165 
166         path = gavCalculator.gavToPath(gav);
167         assertEquals("/maven/jars/dom4j-1.7-20060614.jar", path);
168 
169         gav = gavCalculator.pathToGav("maven/java-sources/velocity-1.5-SNAPSHOT-sources.jar");
170 
171         assertEquals("maven", gav.getGroupId());
172         assertEquals("velocity", gav.getArtifactId());
173         assertEquals("1.5-SNAPSHOT", gav.getVersion());
174         assertEquals("1.5-SNAPSHOT", gav.getBaseVersion());
175         assertEquals("sources", gav.getClassifier());
176         assertEquals("jar", gav.getExtension());
177         assertEquals(null, gav.getSnapshotBuildNumber());
178         assertEquals(null, gav.getSnapshotTimeStamp());
179         assertEquals("velocity-1.5-SNAPSHOT-sources.jar", gav.getName());
180         assertEquals(true, gav.isSnapshot());
181         assertEquals(false, gav.isHash());
182         assertEquals(null, gav.getHashType());
183 
184         path = gavCalculator.gavToPath(gav);
185         assertEquals("/maven/java-sources/velocity-1.5-SNAPSHOT-sources.jar", path);
186 
187         gav = gavCalculator.pathToGav("castor/jars/castor-0.9.9-xml.jar");
188 
189         assertEquals("castor", gav.getGroupId());
190         assertEquals("castor", gav.getArtifactId());
191         assertEquals("0.9.9-xml", gav.getVersion());
192         assertEquals("0.9.9-xml", gav.getBaseVersion());
193         assertEquals(null, gav.getClassifier());
194         assertEquals("jar", gav.getExtension());
195         assertEquals(null, gav.getSnapshotBuildNumber());
196         assertEquals(null, gav.getSnapshotTimeStamp());
197         assertEquals("castor-0.9.9-xml.jar", gav.getName());
198         assertEquals(false, gav.isSnapshot());
199         assertEquals(false, gav.isHash());
200         assertEquals(null, gav.getHashType());
201 
202         path = gavCalculator.gavToPath(gav);
203         assertEquals("/castor/jars/castor-0.9.9-xml.jar", path);
204 
205         gav = gavCalculator.pathToGav("/org.slf4j/poms/slf4j-log4j12-1.4.3.pom");
206 
207         assertEquals("org.slf4j", gav.getGroupId());
208         assertEquals("slf4j-log4j12", gav.getArtifactId());
209         assertEquals("1.4.3", gav.getVersion());
210         assertEquals("1.4.3", gav.getBaseVersion());
211         assertEquals(null, gav.getClassifier());
212         assertEquals("pom", gav.getExtension());
213         assertEquals(null, gav.getSnapshotBuildNumber());
214         assertEquals(null, gav.getSnapshotTimeStamp());
215         assertEquals("slf4j-log4j12-1.4.3.pom", gav.getName());
216         assertEquals(false, gav.isSnapshot());
217         assertEquals(false, gav.isHash());
218         assertEquals(null, gav.getHashType());
219 
220         path = gavCalculator.gavToPath(gav);
221         assertEquals("/org.slf4j/poms/slf4j-log4j12-1.4.3.pom", path);
222 
223         // TODO: fix this!
224         /* There is an "Oh" letter at the end, not a zero! */
225         gav = gavCalculator.pathToGav("/xpp3/poms/xpp3_min-1.1.3.4.O.pom");
226 
227         assertEquals("xpp3", gav.getGroupId());
228         assertEquals("xpp3_min", gav.getArtifactId());
229         assertEquals("1.1.3.4.O", gav.getVersion());
230         assertEquals("1.1.3.4.O", gav.getBaseVersion());
231         assertEquals(null, gav.getClassifier());
232         assertEquals("pom", gav.getExtension());
233         assertEquals(null, gav.getSnapshotBuildNumber());
234         assertEquals(null, gav.getSnapshotTimeStamp());
235         assertEquals("xpp3_min-1.1.3.4.O.pom", gav.getName());
236         assertEquals(false, gav.isSnapshot());
237         assertEquals(false, gav.isHash());
238         assertEquals(null, gav.getHashType());
239 
240         path = gavCalculator.gavToPath(gav);
241         assertEquals("/xpp3/poms/xpp3_min-1.1.3.4.O.pom", path);
242     }
243 
244     @Test
245     public void testNEXUS1336() throws Exception {
246         Gav gav = gavCalculator.pathToGav("/castor/ejbs/castor-ejb-1.0.7-SNAPSHOT-client.jar");
247 
248         assertEquals("castor", gav.getGroupId());
249         assertEquals("castor-ejb", gav.getArtifactId());
250         assertEquals("1.0.7-SNAPSHOT", gav.getVersion());
251         assertEquals("1.0.7-SNAPSHOT", gav.getBaseVersion());
252         assertEquals("client", gav.getClassifier());
253         assertEquals("jar", gav.getExtension());
254         assertEquals(null, gav.getSnapshotBuildNumber());
255         assertEquals(null, gav.getSnapshotTimeStamp());
256         assertEquals("castor-ejb-1.0.7-SNAPSHOT-client.jar", gav.getName());
257         assertEquals(true, gav.isSnapshot());
258         assertEquals(false, gav.isHash());
259         assertEquals(null, gav.getHashType());
260 
261         String path = gavCalculator.gavToPath(gav);
262         assertEquals("/castor/ejbs/castor-ejb-1.0.7-SNAPSHOT-client.jar", path);
263 
264         gav = gavCalculator.pathToGav("/castor/ejbs/castor-ejb-1.0.7.jar");
265 
266         assertEquals("castor", gav.getGroupId());
267         assertEquals("castor-ejb", gav.getArtifactId());
268         assertEquals("1.0.7", gav.getVersion());
269         assertEquals("1.0.7", gav.getBaseVersion());
270         assertEquals(null, gav.getClassifier());
271         assertEquals("jar", gav.getExtension());
272         assertEquals(null, gav.getSnapshotBuildNumber());
273         assertEquals(null, gav.getSnapshotTimeStamp());
274         assertEquals("castor-ejb-1.0.7.jar", gav.getName());
275         assertEquals(false, gav.isSnapshot());
276         assertEquals(false, gav.isHash());
277         assertEquals(null, gav.getHashType());
278 
279         path = gavCalculator.gavToPath(gav);
280         assertEquals("/castor/jars/castor-ejb-1.0.7.jar", path);
281 
282         gav = gavCalculator.pathToGav("/castor/ejbs/castor-ejb-1.0.7-SNAPSHOT-client.jar.sha1");
283 
284         assertEquals("castor", gav.getGroupId());
285         assertEquals("castor-ejb", gav.getArtifactId());
286         assertEquals("1.0.7-SNAPSHOT", gav.getVersion());
287         assertEquals("1.0.7-SNAPSHOT", gav.getBaseVersion());
288         assertEquals("client", gav.getClassifier());
289         assertEquals("jar", gav.getExtension());
290         assertEquals(null, gav.getSnapshotBuildNumber());
291         assertEquals(null, gav.getSnapshotTimeStamp());
292         assertEquals("castor-ejb-1.0.7-SNAPSHOT-client.jar.sha1", gav.getName());
293         assertEquals(true, gav.isSnapshot());
294         assertEquals(true, gav.isHash());
295         assertEquals(Gav.HashType.sha1, gav.getHashType());
296 
297         path = gavCalculator.gavToPath(gav);
298         assertEquals("/castor/ejbs/castor-ejb-1.0.7-SNAPSHOT-client.jar.sha1", path);
299 
300         gav = gavCalculator.pathToGav("/castor/ejbs/castor-ejb-1.0.7-client.jar");
301 
302         assertEquals("castor", gav.getGroupId());
303         assertEquals("castor-ejb", gav.getArtifactId());
304         assertEquals("1.0.7", gav.getVersion());
305         assertEquals("1.0.7", gav.getBaseVersion());
306         assertEquals("client", gav.getClassifier());
307         assertEquals("jar", gav.getExtension());
308         assertEquals(null, gav.getSnapshotBuildNumber());
309         assertEquals(null, gav.getSnapshotTimeStamp());
310         assertEquals("castor-ejb-1.0.7-client.jar", gav.getName());
311         assertEquals(false, gav.isSnapshot());
312         assertEquals(false, gav.isHash());
313         assertEquals(null, gav.getHashType());
314 
315         path = gavCalculator.gavToPath(gav);
316         assertEquals("/castor/ejbs/castor-ejb-1.0.7-client.jar", path);
317     }
318 
319     @Test
320     public void testGavExtreme() throws Exception {
321         Gav gav = gavCalculator.pathToGav("/");
322         assertEquals(null, gav);
323 
324         gav = gavCalculator.pathToGav("/some/stupid/path");
325         assertEquals(null, gav);
326 
327         gav = gavCalculator.pathToGav("/some/stupid/path/more/in/it");
328         assertEquals(null, gav);
329 
330         gav = gavCalculator.pathToGav("/something/that/looks/");
331         assertEquals(null, gav);
332 
333         gav = gavCalculator.pathToGav("/something/that/like-an-artifact.blah");
334         assertEquals(null, gav);
335         // assertEquals( false, gav.isChecksum() );
336         // assertEquals( false, gav.isPrimary() );
337         // assertEquals( false, gav.isSnapshot() );
338 
339         gav = gavCalculator.pathToGav("/something/that/like-an-artifact.pom");
340         assertEquals(null, gav);
341         // assertEquals( false, gav.isChecksum() );
342         // assertEquals( false, gav.isPrimary() );
343         // assertEquals( false, gav.isSnapshot() );
344 
345         gav = gavCalculator.pathToGav("/something/that/maven-metadata.xml");
346         assertEquals(null, gav);
347         // assertEquals( false, gav.isChecksum() );
348         // assertEquals( false, gav.isPrimary() );
349         // assertEquals( false, gav.isSnapshot() );
350 
351         gav = gavCalculator.pathToGav("/something/that/like-SNAPSHOT/maven-metadata.xml");
352         assertEquals(null, gav);
353     }
354 }