1 /**
2 * Copyright (c) 2012-2014, jcabi.com
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met: 1) Redistributions of source code must retain the above
8 * copyright notice, this list of conditions and the following
9 * disclaimer. 2) Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution. 3) Neither the name of the jcabi.com nor
13 * the names of its contributors may be used to endorse or promote
14 * products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 package com.jcabi.aether;
31
32 import java.io.File;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.List;
36 import org.apache.maven.model.Exclusion;
37 import org.apache.maven.project.MavenProject;
38 import org.hamcrest.MatcherAssert;
39 import org.hamcrest.Matchers;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.rules.TemporaryFolder;
43 import org.mockito.Mockito;
44 import org.sonatype.aether.artifact.Artifact;
45 import org.sonatype.aether.repository.RemoteRepository;
46 import org.sonatype.aether.util.artifact.DefaultArtifact;
47
48 /**
49 * Test case for {@link RootArtifact}.
50 * @author Yegor Bugayenko (yegor@tpc2.com)
51 * @version $Id$
52 */
53 public final class RootArtifactTest {
54
55 /**
56 * Temp dir.
57 * @checkstyle VisibilityModifier (3 lines)
58 */
59 @Rule
60 public final transient TemporaryFolder temp = new TemporaryFolder();
61
62 /**
63 * RootArtifact can resolve a root artifact.
64 * @throws Exception If there is some problem inside
65 */
66 @Test
67 @SuppressWarnings("unchecked")
68 public void resolvesRootArtifact() throws Exception {
69 final RootArtifact root = new RootArtifact(
70 this.aether(),
71 // @checkstyle MultipleStringLiterals (1 line)
72 new DefaultArtifact("junit", "junit", "", "jar", "4.10"),
73 new ArrayList<Exclusion>(0)
74 );
75 MatcherAssert.assertThat(
76 root,
77 Matchers.hasToString(Matchers.containsString("junit:junit:4.10"))
78 );
79 MatcherAssert.assertThat(
80 root.children(),
81 Matchers.<Artifact>hasItems(
82 Matchers.hasToString("junit:junit:jar:4.10"),
83 Matchers.hasToString("org.hamcrest:hamcrest-core:jar:1.1")
84 )
85 );
86 }
87
88 /**
89 * RootArtifact can gracefully resolve a root artifact.
90 * @throws Exception If there is some problem inside
91 */
92 @Test
93 public void gracefullyResolvesBrokenRootArtifact() throws Exception {
94 final RootArtifact root = new RootArtifact(
95 this.aether(),
96 new DefaultArtifact("junit-broken", "junit-absent", "", "", "1.0"),
97 new ArrayList<Exclusion>(0)
98 );
99 MatcherAssert.assertThat(
100 root,
101 Matchers.hasToString(
102 Matchers.containsString("failed to load 'junit-broken:")
103 )
104 );
105 }
106
107 /**
108 * Build aether.
109 * @return The aether
110 * @throws Exception If fails
111 */
112 private Aether aether() throws Exception {
113 final File local = this.temp.newFolder();
114 final MavenProject project = Mockito.mock(MavenProject.class);
115 final List<RemoteRepository> repos = Arrays.asList(
116 new RemoteRepository(
117 "maven-central",
118 "default",
119 "http://repo1.maven.org/maven2/"
120 )
121 );
122 Mockito.doReturn(repos).when(project).getRemoteProjectRepositories();
123 return new Aether(project, local);
124 }
125
126 }