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.eclipse.aether.util.artifact;
20  
21  import java.io.File;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.eclipse.aether.artifact.Artifact;
27  import org.eclipse.aether.artifact.DefaultArtifact;
28  import org.junit.Test;
29  
30  import static org.junit.Assert.*;
31  
32  /**
33   */
34  public class SubArtifactTest {
35  
36      private Artifact newMainArtifact(String coords) {
37          return new DefaultArtifact(coords);
38      }
39  
40      @Test
41      public void testMainArtifactFileNotRetained() {
42          Artifact a = newMainArtifact("gid:aid:ver").setFile(new File(""));
43          assertNotNull(a.getFile());
44          a = new SubArtifact(a, "", "pom");
45          assertNull(a.getFile());
46      }
47  
48      @Test
49      public void testMainArtifactPropertiesNotRetained() {
50          Artifact a = newMainArtifact("gid:aid:ver").setProperties(Collections.singletonMap("key", "value"));
51          assertEquals(1, a.getProperties().size());
52          a = new SubArtifact(a, "", "pom");
53          assertEquals(0, a.getProperties().size());
54          assertSame(null, a.getProperty("key", null));
55      }
56  
57      @Test(expected = NullPointerException.class)
58      public void testMainArtifactMissing() {
59          new SubArtifact(null, "", "pom");
60      }
61  
62      @Test
63      public void testEmptyClassifier() {
64          Artifact main = newMainArtifact("gid:aid:ext:cls:ver");
65          Artifact sub = new SubArtifact(main, "", "pom");
66          assertEquals("", sub.getClassifier());
67          sub = new SubArtifact(main, null, "pom");
68          assertEquals("", sub.getClassifier());
69      }
70  
71      @Test
72      public void testEmptyExtension() {
73          Artifact main = newMainArtifact("gid:aid:ext:cls:ver");
74          Artifact sub = new SubArtifact(main, "tests", "");
75          assertEquals("", sub.getExtension());
76          sub = new SubArtifact(main, "tests", null);
77          assertEquals("", sub.getExtension());
78      }
79  
80      @Test
81      public void testSameClassifier() {
82          Artifact main = newMainArtifact("gid:aid:ext:cls:ver");
83          Artifact sub = new SubArtifact(main, "*", "pom");
84          assertEquals("cls", sub.getClassifier());
85      }
86  
87      @Test
88      public void testSameExtension() {
89          Artifact main = newMainArtifact("gid:aid:ext:cls:ver");
90          Artifact sub = new SubArtifact(main, "tests", "*");
91          assertEquals("ext", sub.getExtension());
92      }
93  
94      @Test
95      public void testDerivedClassifier() {
96          Artifact main = newMainArtifact("gid:aid:ext:cls:ver");
97          Artifact sub = new SubArtifact(main, "*-tests", "pom");
98          assertEquals("cls-tests", sub.getClassifier());
99          sub = new SubArtifact(main, "tests-*", "pom");
100         assertEquals("tests-cls", sub.getClassifier());
101 
102         main = newMainArtifact("gid:aid:ext:ver");
103         sub = new SubArtifact(main, "*-tests", "pom");
104         assertEquals("tests", sub.getClassifier());
105         sub = new SubArtifact(main, "tests-*", "pom");
106         assertEquals("tests", sub.getClassifier());
107     }
108 
109     @Test
110     public void testDerivedExtension() {
111         Artifact main = newMainArtifact("gid:aid:ext:cls:ver");
112         Artifact sub = new SubArtifact(main, "", "*.asc");
113         assertEquals("ext.asc", sub.getExtension());
114         sub = new SubArtifact(main, "", "asc.*");
115         assertEquals("asc.ext", sub.getExtension());
116     }
117 
118     @Test
119     public void testImmutability() {
120         Artifact a = new SubArtifact(newMainArtifact("gid:aid:ver"), "", "pom");
121         assertNotSame(a, a.setFile(new File("file")));
122         assertNotSame(a, a.setVersion("otherVersion"));
123         assertNotSame(a, a.setProperties(Collections.singletonMap("key", "value")));
124     }
125 
126     @Test
127     public void testPropertiesCopied() {
128         Map<String, String> props = new HashMap<>();
129         props.put("key", "value1");
130 
131         Artifact a = new SubArtifact(newMainArtifact("gid:aid:ver"), "", "pom", props, null);
132         assertEquals("value1", a.getProperty("key", null));
133         props.clear();
134         assertEquals("value1", a.getProperty("key", null));
135 
136         props.put("key", "value2");
137         a = a.setProperties(props);
138         assertEquals("value2", a.getProperty("key", null));
139         props.clear();
140         assertEquals("value2", a.getProperty("key", null));
141     }
142 }