1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.jupiter.api.Test;
29
30 import static org.junit.jupiter.api.Assertions.*;
31
32
33
34 public class SubArtifactTest {
35
36 private Artifact newMainArtifact(String coords) {
37 return new DefaultArtifact(coords);
38 }
39
40 @Test
41 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 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
58 void testMainArtifactMissing() {
59 assertThrows(NullPointerException.class, () -> new SubArtifact(null, "", "pom"));
60 }
61
62 @Test
63 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 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 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 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 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 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 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 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 }