1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.codehaus.mojo.nbm;
19
20 import java.io.File;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import junit.framework.TestCase;
28 import org.apache.maven.artifact.Artifact;
29 import org.apache.maven.artifact.DefaultArtifact;
30 import org.apache.maven.artifact.versioning.VersionRange;
31 import org.apache.maven.plugin.logging.Log;
32 import org.apache.maven.plugin.logging.SystemStreamLog;
33 import org.apache.maven.shared.dependency.graph.DependencyNode;
34 import org.apache.maven.shared.dependency.graph.internal.DefaultDependencyNode;
35 import org.codehaus.mojo.nbm.model.Dependency;
36 import org.codehaus.mojo.nbm.model.NetBeansModule;
37 import org.codehaus.mojo.nbm.utils.ExamineManifest;
38
39
40
41
42
43 public class AbstractNbmMojoTest extends TestCase {
44 Log log = null;
45 DefaultDependencyNode treeRoot = null;
46
47 public AbstractNbmMojoTest(String testName) {
48 super(testName);
49 }
50
51 @Override
52 protected void setUp() throws Exception {
53 super.setUp();
54 log = new SystemStreamLog();
55 treeRoot = createNode(null, "root", "root", "1.0", "jar", "", true, new ArrayList<Artifact>(), new HashMap<Artifact,ExamineManifest>());
56 }
57
58 @Override
59 protected void tearDown() throws Exception {
60 super.tearDown();
61 }
62
63
64
65
66 public void testMatchesLibrary() {
67 System.out.println("matchesLibrary");
68 Artifact artifact = createArtifact("group", "artifact", "1.0", "jar", "compile");
69 List<String> libraries = new ArrayList<String>();
70 libraries.add("group:artifact");
71 ExamineManifest depExaminator = createNonModule();
72 boolean result = AbstractNbmMojo.matchesLibrary(artifact, libraries, depExaminator, log, false);
73 assertTrue("explicitly defined libraries in descriptor are included", result);
74
75 artifact = createArtifact("group", "artifact", "1.0", "jar", "provided");
76 libraries = new ArrayList<String>();
77 result = AbstractNbmMojo.matchesLibrary(artifact, libraries, depExaminator, log, false);
78 assertFalse("provided artifacts are not included", result);
79
80 artifact = createArtifact("group", "artifact", "1.0", "jar", "system");
81 libraries = new ArrayList<String>();
82 result = AbstractNbmMojo.matchesLibrary(artifact, libraries, depExaminator, log, false);
83 assertFalse("system artifacts are not included", result);
84
85 artifact = createArtifact("group", "artifact", "1.0", "jar", "compile");
86 libraries = new ArrayList<String>();
87 libraries.add("group:artifact");
88 depExaminator = createModule();
89 result = AbstractNbmMojo.matchesLibrary(artifact, libraries, depExaminator, log, false);
90 assertTrue("netbeans modules are included if explicitly marked in descriptor", result);
91
92 libraries = new ArrayList<String>();
93 result = AbstractNbmMojo.matchesLibrary(artifact, libraries, depExaminator, log, false);
94 assertFalse("netbeans modules are omitted", result);
95
96 artifact = createArtifact("group", "artifact", "1.0", "nbm", "compile");
97 libraries = new ArrayList<String>();
98 result = AbstractNbmMojo.matchesLibrary(artifact, libraries, depExaminator, log, false);
99 assertFalse("netbeans modules are omitted", result);
100
101 }
102
103
104
105
106 public void testResolveNetBeansDependency() {
107 Artifact artifact = createArtifact("group", "artifact", "1.0", "jar", "compile");
108 List<Dependency> deps = new ArrayList<Dependency>();
109 ExamineManifest manifest = createNonModule();
110 Dependency result = AbstractNbmMojo.resolveNetBeansDependency(artifact, deps, manifest, log);
111 assertNull("not a NetBeans module", result);
112
113 manifest = createModule();
114 result = AbstractNbmMojo.resolveNetBeansDependency(artifact, deps, manifest, log);
115 assertNotNull("is a NetBeans module", result);
116
117 artifact = createArtifact("group", "artifact", "1.0", "nbm", "compile");
118 manifest = createNonModule();
119 result = AbstractNbmMojo.resolveNetBeansDependency(artifact, deps, manifest, log);
120 assertNotNull("nbm type is a NetBeans module", result);
121
122
123 artifact = createArtifact("group", "artifact", "1.0", "jar", "compile");
124 deps = new ArrayList<Dependency>();
125 Dependency d = new Dependency();
126 d.setId("group:artifact");
127 deps.add(d);
128 manifest = createNonModule();
129 result = AbstractNbmMojo.resolveNetBeansDependency(artifact, deps, manifest, log);
130 assertNull("not a NetBeans module, declared in deps but without explicit value", result);
131
132 d.setExplicitValue("XXX > 1.0");
133 result = AbstractNbmMojo.resolveNetBeansDependency(artifact, deps, manifest, log);
134 assertEquals("not a NetBeans module but declared with explicit value", result, d);
135
136 d.setExplicitValue(null);
137 manifest = createModule();
138 result = AbstractNbmMojo.resolveNetBeansDependency(artifact, deps, manifest, log);
139 assertEquals("netbeans module defined in descriptor", result, d);
140 }
141
142
143
144
145 public void testGetLibraryArtifacts1() throws Exception {
146 System.out.println("getLibraryArtifacts1");
147 Map<Artifact, ExamineManifest> examinerCache = new HashMap<Artifact, ExamineManifest>();
148 List<Artifact> runtimes = new ArrayList<Artifact>();
149 DependencyNode module = createNode(treeRoot, "gr1", "ar1", "1.0", "jar", "compile", true, runtimes, examinerCache);
150 treeRoot.setChildren( Collections.singletonList( module ));
151 NetBeansModule mdl = new NetBeansModule();
152 List<Artifact> result = AbstractNbmMojo.getLibraryArtifacts(treeRoot, mdl, runtimes, examinerCache, log, false);
153 assertEquals(0, result.size());
154 }
155
156
157
158
159 public void testGetLibraryArtifact2() throws Exception {
160 System.out.println("getLibraryArtifacts2");
161 Map<Artifact, ExamineManifest> examinerCache = new HashMap<Artifact, ExamineManifest>();
162 List<Artifact> runtimes = new ArrayList<Artifact>();
163 DependencyNode library = createNode(treeRoot, "gr1", "ar1", "1.0", "jar", "compile", false, runtimes, examinerCache);
164 treeRoot.setChildren( Collections.singletonList( library ));
165 NetBeansModule mdl = new NetBeansModule();
166 List<Artifact> result = AbstractNbmMojo.getLibraryArtifacts(treeRoot, mdl, runtimes, examinerCache, log, false);
167 assertEquals(1, result.size());
168 }
169
170
171
172
173
174 public void testGetLibraryArtifact3() throws Exception {
175 System.out.println("getLibraryArtifacts3");
176 Map<Artifact, ExamineManifest> examinerCache = new HashMap<Artifact, ExamineManifest>();
177 List<Artifact> runtimes = new ArrayList<Artifact>();
178 DependencyNode library = createNode(treeRoot, "gr1", "ar1", "1.0", "jar", "compile", false, runtimes, examinerCache);
179 treeRoot.setChildren( Collections.singletonList( library ));
180 DependencyNode translibrary = createNode(library, "gr2", "ar2", "1.0", "jar", "runtime", false, runtimes, examinerCache);
181 ((DefaultDependencyNode)library).setChildren( Collections.singletonList( translibrary ) );
182
183 NetBeansModule mdl = new NetBeansModule();
184 List<Artifact> result = AbstractNbmMojo.getLibraryArtifacts(treeRoot, mdl, runtimes, examinerCache, log, false);
185 assertEquals(2, result.size());
186 }
187
188
189
190
191 public void testGetLibraryArtifact4() throws Exception {
192 System.out.println("getLibraryArtifacts4");
193 Map<Artifact, ExamineManifest> examinerCache = new HashMap<Artifact, ExamineManifest>();
194 List<Artifact> runtimes = new ArrayList<Artifact>();
195 DependencyNode module = createNode(treeRoot, "gr1", "ar1", "1.0", "jar", "compile", true, runtimes, examinerCache);
196 treeRoot.setChildren( Collections.singletonList( module ));
197 DependencyNode translibrary = createNode(module, "gr2", "ar2", "1.0", "jar", "runtime", false, runtimes, examinerCache);
198 ((DefaultDependencyNode)module).setChildren( Collections.singletonList( translibrary ) );
199 NetBeansModule mdl = new NetBeansModule();
200 List<Artifact> result = AbstractNbmMojo.getLibraryArtifacts(treeRoot, mdl, runtimes, examinerCache, log, false);
201 assertEquals(0, result.size());
202 }
203
204
205
206
207
208 public void testGetLibraryArtifact5() throws Exception {
209 System.out.println("getLibraryArtifacts5");
210 Map<Artifact, ExamineManifest> examinerCache = new HashMap<Artifact, ExamineManifest>();
211 List<Artifact> runtimes = new ArrayList<Artifact>();
212 DependencyNode module = createNode(treeRoot, "gr1", "ar1", "1.0", "jar", "compile", true, runtimes, examinerCache);
213 DependencyNode translibrary = createNode(module, "gr2", "ar2", "1.0", "jar", "runtime", false, runtimes, examinerCache);
214 ((DefaultDependencyNode)module).setChildren( Collections.singletonList( translibrary ) );
215
216 DependencyNode library = createNode(treeRoot, "gr3", "ar3", "1.0", "jar", "compile", false, runtimes, examinerCache);
217 DependencyNode translibrary2 = createNode(library, "gr4", "ar4", "1.0", "jar", "runtime", false, runtimes, examinerCache);
218 ((DefaultDependencyNode)library).setChildren( Collections.singletonList( translibrary2 ) );
219 treeRoot.setChildren( Arrays.asList( new DependencyNode[] { module, library}));
220
221
222 NetBeansModule mdl = new NetBeansModule();
223 List<Artifact> result = AbstractNbmMojo.getLibraryArtifacts(treeRoot, mdl, runtimes, examinerCache, log, false);
224 assertEquals(2, result.size());
225 assertEquals(result.get(0).getId(), library.getArtifact().getId());
226 assertEquals(result.get(1).getId(), translibrary2.getArtifact().getId());
227 }
228
229 private DefaultDependencyNode createNode(DependencyNode parent, String gr, String art, String ver, String pack, String scope, boolean isModule, List<Artifact> runtimes, Map<Artifact, ExamineManifest> cache) {
230 Artifact a = createArtifact(gr, art, ver, pack, scope);
231 DefaultDependencyNode nd = new DefaultDependencyNode(parent, a, ver, scope, ver);
232 ExamineManifest manifest = isModule ? createModule() : createNonModule();
233 runtimes.add(a);
234 cache.put(a, manifest);
235 nd.setChildren( Collections.<DependencyNode>emptyList() );
236 return nd;
237 }
238
239
240
241
242
243
244 private Artifact createArtifact(String gr, String art, String ver, String pack, String scope) {
245 VersionRange rng = VersionRange.createFromVersion(ver);
246 Artifact a = new DefaultArtifact(gr, art, rng, scope, pack, "classifier", null);
247 a.setDependencyTrail(Collections.EMPTY_LIST);
248 a.setFile(new File(gr + File.separator + art + File.separator + art + "-"+ ver + ".jar"));
249 return a;
250 }
251
252 private ExamineManifest createNonModule() {
253 ExamineManifest manifest = new ExamineManifest(log);
254 manifest.setNetBeansModule(false);
255 return manifest;
256 }
257
258 private ExamineManifest createModule() {
259 ExamineManifest manifest = new ExamineManifest(log);
260 manifest.setNetBeansModule(true);
261 return manifest;
262 }
263 }