1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.extension.repository.aether.internal

File AetherDefaultRepositoryManagerTest.java

 

Code metrics

0
158
22
1
439
324
30
0.19
7.18
22
1.36

Classes

Class Line # Actions
AetherDefaultRepositoryManagerTest 62 158 0% 30 0
1.0100%
 

Contributing tests

This file is covered by 21 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.extension.repository.aether.internal;
21   
22    import java.io.File;
23    import java.io.IOException;
24    import java.io.InputStream;
25    import java.nio.charset.StandardCharsets;
26    import java.util.ArrayList;
27    import java.util.Arrays;
28    import java.util.HashSet;
29    import java.util.Iterator;
30    import java.util.List;
31   
32    import org.apache.commons.io.FileUtils;
33    import org.apache.commons.io.IOUtils;
34    import org.eclipse.aether.artifact.Artifact;
35    import org.eclipse.aether.artifact.DefaultArtifact;
36    import org.eclipse.aether.graph.Dependency;
37    import org.junit.Assert;
38    import org.junit.Before;
39    import org.junit.Rule;
40    import org.junit.Test;
41    import org.xwiki.extension.DefaultExtensionAuthor;
42    import org.xwiki.extension.DefaultExtensionDependency;
43    import org.xwiki.extension.Extension;
44    import org.xwiki.extension.ExtensionAuthor;
45    import org.xwiki.extension.ExtensionDependency;
46    import org.xwiki.extension.ExtensionException;
47    import org.xwiki.extension.ExtensionId;
48    import org.xwiki.extension.ExtensionLicenseManager;
49    import org.xwiki.extension.ExtensionNotFoundException;
50    import org.xwiki.extension.ResolveException;
51    import org.xwiki.extension.repository.ExtensionRepositoryDescriptor;
52    import org.xwiki.extension.repository.ExtensionRepositoryManager;
53    import org.xwiki.extension.repository.result.IterableResult;
54    import org.xwiki.extension.test.MockitoRepositoryUtilsRule;
55    import org.xwiki.extension.version.Version;
56    import org.xwiki.extension.version.internal.DefaultVersionConstraint;
57    import org.xwiki.test.annotation.AllComponents;
58   
59    import static org.junit.jupiter.api.Assertions.assertEquals;
60   
61    @AllComponents
 
62    public class AetherDefaultRepositoryManagerTest
63    {
64    private static final String GROUPID = "groupid";
65   
66    private static final String ARTIfACTID = "artifactid";
67   
68    @Rule
69    public MockitoRepositoryUtilsRule repositoryUtil = new MockitoRepositoryUtilsRule();
70   
71    private ExtensionRepositoryManager repositoryManager;
72   
73    private ExtensionId extensionId;
74   
75    private ExtensionId snapshotExtensionId;
76   
77    private ExtensionId extensionDependencyId;
78   
79    private ExtensionId extensionIdClassifier;
80   
81    private ExtensionDependency dependencyExtensionId;
82   
83    private ExtensionDependency dependencyExtensionIdRange;
84   
85    private ExtensionLicenseManager extensionLicenseManager;
86   
87    private ExtensionId bundleExtensionId;
88   
89    private ExtensionId sextensionId;
90   
91    private ExtensionId sextensionDependencyId;
92   
 
93  21 toggle @Before
94    public void setUp() throws Exception
95    {
96  21 this.extensionId = new ExtensionId(GROUPID + ':' + ARTIfACTID, "version");
97  21 this.snapshotExtensionId = new ExtensionId(GROUPID + ':' + ARTIfACTID, "1.0-SNAPSHOT");
98  21 this.extensionDependencyId = new ExtensionId("dgroupid:dartifactid", "dversion");
99   
100  21 this.extensionIdClassifier = new ExtensionId(GROUPID + ':' + ARTIfACTID + ":classifier", "version");
101  21 this.dependencyExtensionId = new DefaultExtensionDependency(this.extensionDependencyId.getId(),
102    new DefaultVersionConstraint(this.extensionDependencyId.getVersion().getValue()));
103  21 this.dependencyExtensionIdRange = new DefaultExtensionDependency(this.extensionDependencyId.getId(),
104    new DefaultVersionConstraint("[dversion,)"));
105   
106  21 this.bundleExtensionId = new ExtensionId("groupid:bundleartifactid", "version");
107   
108  21 this.sextensionId = new ExtensionId("sgroupid:sartifactid", "version");
109  21 this.sextensionDependencyId = new ExtensionId("sgroupid:sdartifactid", "version");
110   
111    // lookup
112   
113  21 this.repositoryManager =
114    this.repositoryUtil.getComponentManager().getInstance(ExtensionRepositoryManager.class);
115  21 this.extensionLicenseManager =
116    this.repositoryUtil.getComponentManager().getInstance(ExtensionLicenseManager.class);
117    }
118   
119    // Tests
120   
 
121  1 toggle @Test
122    public void testResolve() throws ResolveException, IOException
123    {
124  1 Extension extension = this.repositoryManager.resolve(this.extensionId);
125   
126  1 Assert.assertNotNull(extension);
127  1 Assert.assertEquals(this.extensionId.getId(), extension.getId().getId());
128  1 Assert.assertEquals(this.extensionId.getVersion(), extension.getId().getVersion());
129  1 Assert.assertEquals("type", extension.getType());
130  1 Assert.assertEquals(this.repositoryUtil.getMavenRepositoryId(),
131    extension.getRepository().getDescriptor().getId());
132  1 Assert.assertEquals("name", extension.getName());
133  1 Assert.assertEquals("summary", extension.getSummary());
134  1 Assert.assertEquals("http://website", extension.getWebSite());
135  1 Assert.assertEquals("category", extension.getCategory());
136  1 Assert.assertEquals(Arrays.asList(new DefaultExtensionAuthor("Full Name", "http://profile")),
137    new ArrayList<ExtensionAuthor>(extension.getAuthors()));
138  1 Assert.assertEquals(new HashSet<String>(Arrays.asList("groupid1:feature1", "groupid2:feature2")),
139    new HashSet<String>(extension.getFeatures()));
140  1 Assert.assertEquals(
141    new HashSet<ExtensionId>(Arrays.asList(new ExtensionId("groupid1:feature1", this.extensionId.getVersion()),
142    new ExtensionId("groupid2:feature2", "42"))),
143    new HashSet<ExtensionId>(extension.getExtensionFeatures()));
144  1 Assert.assertEquals(new HashSet<String>(Arrays.asList("namespace1", "{root}")),
145    new HashSet<String>(extension.getAllowedNamespaces()));
146  1 Assert.assertSame(this.extensionLicenseManager.getLicense("GNU Lesser General Public License 2.1"),
147    extension.getLicenses().iterator().next());
148   
149  1 Assert.assertEquals("http://url", extension.getIssueManagement().getURL());
150  1 Assert.assertEquals("system", extension.getIssueManagement().getSystem());
151   
152  1 Assert.assertEquals("http://url", extension.getScm().getUrl());
153  1 Assert.assertEquals("git", extension.getScm().getConnection().getSystem());
154  1 Assert.assertEquals("git://url.git", extension.getScm().getConnection().getPath());
155  1 Assert.assertEquals("git", extension.getScm().getDeveloperConnection().getSystem());
156  1 Assert.assertEquals("git@url.git", extension.getScm().getDeveloperConnection().getPath());
157   
158  1 List<ExtensionRepositoryDescriptor> repositories = new ArrayList<>();
159  1 repositories.add(extension.getRepository().getDescriptor());
160   
161  1 Assert.assertEquals(repositories, extension.getRepositories());
162   
163  1 Iterator<ExtensionDependency> dependencyIterator = extension.getDependencies().iterator();
164   
165  1 ExtensionDependency dependency = dependencyIterator.next();
166  1 Assert.assertEquals(this.dependencyExtensionId.getId(), dependency.getId());
167  1 Assert.assertEquals(this.dependencyExtensionId.getVersionConstraint(), dependency.getVersionConstraint());
168  1 Assert.assertEquals(repositories, dependency.getRepositories());
169   
170  1 dependency = dependencyIterator.next();
171  1 Assert.assertEquals("legacygroupid:legacyartifactid", dependency.getId());
172  1 Assert.assertEquals("legacyversion", dependency.getVersionConstraint().getValue());
173  1 Assert.assertEquals(repositories, dependency.getRepositories());
174   
175    // check that a new resolve of an already resolved extension provide the proper repository
176  1 extension = this.repositoryManager.resolve(this.extensionId);
177  1 Assert.assertEquals(this.repositoryUtil.getMavenRepositoryId(),
178    extension.getRepository().getDescriptor().getId());
179   
180    // Modify the file on the descriptor on the repository
181  1 File pomFile = new File(this.repositoryUtil.getMavenRepository(),
182    this.extensionId.getId().replace('.', '/').replace(':', '/') + '/' + this.extensionId.getVersion() + '/'
183    + ARTIfACTID + '-' + this.extensionId.getVersion() + ".pom");
184  1 FileUtils.writeStringToFile(pomFile, FileUtils.readFileToString(pomFile, "UTF-8")
185    .replace("<description>summary</description>", "<description>modified summary</description>"), "UTF-8");
186  1 extension = this.repositoryManager.resolve(this.extensionId);
187  1 Assert.assertEquals("modified summary", extension.getSummary());
188    }
189   
 
190  1 toggle @Test
191    public void testResolveSNAPSHOT() throws ResolveException
192    {
193  1 Extension extension = this.repositoryManager.resolve(this.snapshotExtensionId);
194   
195  1 Assert.assertNotNull(extension);
196  1 Assert.assertEquals(this.snapshotExtensionId.getId(), extension.getId().getId());
197  1 Assert.assertEquals(this.snapshotExtensionId.getVersion(), extension.getId().getVersion());
198  1 Assert.assertEquals("type", extension.getType());
199  1 Assert.assertEquals(this.repositoryUtil.getMavenRepositoryId(),
200    extension.getRepository().getDescriptor().getId());
201    }
202   
 
203  1 toggle @Test
204    public void testResolveAetherDependency() throws ResolveException
205    {
206  1 Artifact artifact = new DefaultArtifact("groupid", "artifactid", "", "type", "version");
207  1 Dependency aetherDependency = new Dependency(artifact, null);
208  1 AetherExtensionDependency dependency = new AetherExtensionDependency(aetherDependency);
209   
210  1 Extension extension = this.repositoryManager.resolve(dependency);
211   
212  1 Assert.assertNotNull(extension);
213  1 Assert.assertEquals(this.extensionId.getId(), extension.getId().getId());
214  1 Assert.assertEquals(this.extensionId.getVersion(), extension.getId().getVersion());
215  1 Assert.assertEquals("type", extension.getType());
216    }
217   
 
218  1 toggle @Test
219    public void testResolveAetherDependencySNAPSHOT() throws ResolveException
220    {
221  1 Artifact artifact = new DefaultArtifact("groupid", "artifactid", "", "type", "1.0-SNAPSHOT");
222  1 Dependency aetherDependency = new Dependency(artifact, null);
223  1 AetherExtensionDependency dependency = new AetherExtensionDependency(aetherDependency);
224   
225  1 Extension extension = this.repositoryManager.resolve(dependency);
226   
227  1 Assert.assertNotNull(extension);
228  1 Assert.assertEquals(this.snapshotExtensionId.getId(), extension.getId().getId());
229  1 Assert.assertEquals(this.snapshotExtensionId.getVersion(), extension.getId().getVersion());
230  1 Assert.assertEquals("type", extension.getType());
231  1 Assert.assertEquals(this.repositoryUtil.getMavenRepositoryId(),
232    extension.getRepository().getDescriptor().getId());
233    }
234   
 
235  1 toggle @Test
236    public void testResolveAetherDependencyRange() throws ResolveException
237    {
238  1 Artifact artifact = new DefaultArtifact("groupid", "artifactid", "", "type", "[1.0,)");
239  1 Dependency aetherDependency = new Dependency(artifact, null);
240  1 AetherExtensionDependency dependency = new AetherExtensionDependency(aetherDependency);
241   
242  1 Extension extension = this.repositoryManager.resolve(dependency);
243   
244  1 Assert.assertNotNull(extension);
245  1 Assert.assertEquals(this.extensionId.getId(), extension.getId().getId());
246  1 Assert.assertEquals("2.0", extension.getId().getVersion().toString());
247    }
248   
 
249  1 toggle @Test
250    public void testResolveAetherDependencyRange2() throws ResolveException
251    {
252  1 Artifact artifact = new DefaultArtifact("groupid", "artifactid", "", "type", "[1.0,2.0)");
253  1 Dependency aetherDependency = new Dependency(artifact, null);
254  1 AetherExtensionDependency dependency = new AetherExtensionDependency(aetherDependency);
255   
256  1 Extension extension = this.repositoryManager.resolve(dependency);
257   
258  1 Assert.assertNotNull(extension);
259  1 Assert.assertEquals(this.extensionId.getId(), extension.getId().getId());
260  1 Assert.assertEquals("1.0", extension.getId().getVersion().toString());
261    }
262   
 
263  1 toggle @Test
264    public void testResolveOverriddenProperties() throws ResolveException
265    {
266  1 Extension extension = this.repositoryManager.resolve(new ExtensionId("groupid:oartifactid", "version"));
267   
268  1 Assert.assertNotNull(extension);
269  1 Assert.assertEquals("oname", extension.getName());
270  1 Assert.assertEquals("osummary", extension.getSummary());
271  1 Assert.assertEquals("http://owebsite", extension.getWebSite());
272    }
273   
274    /**
275    * Make sure any <code>version</code> property coming from system properties will not be resolved instead of the
276    * actual pom version.
277    */
 
278  1 toggle @Test
279    public void testResolveWithVersionAsSystemProperty() throws ResolveException
280    {
281  1 System.setProperty("version", "systemversion");
282  1 System.setProperty("groupId", "systemgroupId");
283   
284  1 Extension extension = this.repositoryManager.resolve(this.sextensionId);
285   
286  1 Assert.assertNotNull(extension);
287  1 Assert.assertEquals(this.sextensionId.getId(), extension.getId().getId());
288  1 Assert.assertEquals(this.sextensionId.getVersion(), extension.getId().getVersion());
289   
290  1 ExtensionDependency dependency = extension.getDependencies().iterator().next();
291  1 Assert.assertEquals(this.sextensionDependencyId.getId(), dependency.getId());
292  1 Assert.assertEquals(this.sextensionDependencyId.getVersion().getValue(),
293    dependency.getVersionConstraint().getValue());
294    }
295   
 
296  1 toggle @Test
297    public void testResolveVersionClassifier() throws ResolveException
298    {
299  1 Extension extension = this.repositoryManager.resolve(this.extensionIdClassifier);
300   
301  1 Assert.assertNotNull(extension);
302  1 Assert.assertEquals(this.extensionIdClassifier.getId(), extension.getId().getId());
303  1 Assert.assertEquals(this.extensionIdClassifier.getVersion(), extension.getId().getVersion());
304    }
305   
 
306  1 toggle @Test
307    public void testResolveVersionRange() throws ResolveException
308    {
309  1 Extension extension = this.repositoryManager.resolve(this.dependencyExtensionIdRange);
310   
311  1 Assert.assertNotNull(extension);
312  1 Assert.assertEquals(this.extensionDependencyId.getId(), extension.getId().getId());
313  1 Assert.assertEquals(this.extensionDependencyId.getVersion(), extension.getId().getVersion());
314    }
315   
 
316  1 toggle @Test
317    public void testDownload() throws ExtensionException, IOException
318    {
319  1 Extension extension = this.repositoryManager.resolve(this.extensionId);
320   
321  1 try (InputStream is = extension.getFile().openStream()) {
322  1 Assert.assertEquals("content", IOUtils.toString(is));
323    }
324    }
325   
 
326  1 toggle @Test
327    public void testDownloadSNAPSHOT() throws ExtensionException, IOException
328    {
329  1 Extension extension = this.repositoryManager.resolve(this.snapshotExtensionId);
330   
331  1 try (InputStream is = extension.getFile().openStream()) {
332  1 Assert.assertEquals("snapshot content", IOUtils.toString(is));
333    }
334    }
335   
 
336  1 toggle @Test
337    public void testDownloadClassifier() throws ExtensionException, IOException
338    {
339  1 Extension extension = this.repositoryManager.resolve(this.extensionIdClassifier);
340   
341  1 try (InputStream is = extension.getFile().openStream()) {
342  1 Assert.assertEquals("classifier content", IOUtils.toString(is));
343    }
344    }
345   
 
346  1 toggle @Test
347    public void testDownloadBundle() throws ExtensionException, IOException
348    {
349  1 Extension extension = this.repositoryManager.resolve(this.bundleExtensionId);
350   
351  1 try (InputStream is = extension.getFile().openStream()) {
352  1 Assert.assertEquals("content", IOUtils.toString(is));
353    }
354    }
355   
 
356  1 toggle @Test
357    public void testResolveVersions() throws ExtensionException
358    {
359  1 IterableResult<Version> versions = this.repositoryManager.resolveVersions(this.extensionId.getId(), 0, -1);
360   
361  1 Assert.assertEquals(4, versions.getTotalHits());
362  1 Assert.assertEquals(4, versions.getSize());
363  1 Assert.assertEquals(0, versions.getOffset());
364    }
365   
 
366  1 toggle @Test
367    public void testResolveWithExternalParent() throws ResolveException
368    {
369  1 ExtensionId extensionId = new ExtensionId("lgroupid:lartifactid", "version");
370   
371  1 Extension extension = this.repositoryManager.resolve(extensionId);
372   
373  1 Assert.assertNotNull(extension);
374  1 Assert.assertEquals(extensionId.getId(), extension.getId().getId());
375  1 Assert.assertEquals(extensionId.getVersion(), extension.getId().getVersion());
376  1 Assert.assertEquals("type", extension.getType());
377  1 Assert.assertEquals(this.repositoryUtil.getMavenRepositoryId(),
378    extension.getRepository().getDescriptor().getId());
379   
380  1 Assert.assertEquals("parent description", extension.getSummary());
381    }
382   
 
383  1 toggle @Test
384    public void testResolveWithEmptyScmAndIssueManagement() throws ResolveException
385    {
386  1 ExtensionId extensionId = new ExtensionId("groupid:emptyscmandissuemanagement", "version");
387   
388  1 Extension extension = this.repositoryManager.resolve(extensionId);
389   
390  1 Assert.assertNotNull(extension);
391  1 Assert.assertEquals(extensionId.getId(), extension.getId().getId());
392  1 Assert.assertEquals(extensionId.getVersion(), extension.getId().getVersion());
393  1 Assert.assertEquals("type", extension.getType());
394  1 Assert.assertEquals(this.repositoryUtil.getMavenRepositoryId(),
395    extension.getRepository().getDescriptor().getId());
396   
397  1 Assert.assertNull(extension.getIssueManagement());
398  1 Assert.assertNull(extension.getScm());
399    }
400   
 
401  1 toggle @Test
402    public void testResolveDependencyFromUnknownRepository() throws ResolveException, IOException
403    {
404    // Make sure to put a proper repository in the pom
405  1 File pomFile =
406    new File(this.repositoryUtil.getMavenRepository(), "eugroupid/euartifactid/version/euartifactid-version.pom");
407  1 String pom = FileUtils.readFileToString(pomFile, StandardCharsets.UTF_8);
408  1 pom.replace("${{repository.mavenunknown}}", this.repositoryUtil.getMavenUnknownRepository().toURI().toString());
409  1 FileUtils.write(pomFile, pom, StandardCharsets.UTF_8);
410   
411  1 Extension extension = this.repositoryManager.resolve(new ExtensionId("eugroupid:euartifactid", "version"));
412   
413  1 ExtensionDependency extensionDependency = extension.getDependencies().iterator().next();
414   
415  1 Extension dependency = this.repositoryManager.resolve(extensionDependency);
416   
417  1 assertEquals(extensionDependency.getId(), dependency.getId().getId());
418    }
419   
420    // Failures
421   
 
422  1 toggle @Test(expected = ExtensionNotFoundException.class)
423    public void testResolveWithNotExistingVersion() throws ResolveException
424    {
425  1 this.repositoryManager.resolve(new ExtensionId(this.extensionId.getId(), "noversion"));
426    }
427   
 
428  1 toggle @Test(expected = ExtensionNotFoundException.class)
429    public void testResolveNotExistingExtension() throws ResolveException
430    {
431  1 this.repositoryManager.resolve(new ExtensionId("nogroupid:noartifactid", "noversion"));
432    }
433   
 
434  1 toggle @Test(expected = ExtensionNotFoundException.class)
435    public void testResolveNotExistingExtensionVersions() throws ExtensionException
436    {
437  1 this.repositoryManager.resolveVersions("nogroupid:noartifactid", 0, -1);
438    }
439    }