View Javadoc
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.Arrays;
34  import java.util.List;
35  import org.apache.maven.model.Dependency;
36  import org.apache.maven.project.MavenProject;
37  import org.hamcrest.MatcherAssert;
38  import org.hamcrest.Matchers;
39  import org.junit.Rule;
40  import org.junit.Test;
41  import org.junit.rules.TemporaryFolder;
42  import org.mockito.Mockito;
43  import org.sonatype.aether.repository.RemoteRepository;
44  import org.sonatype.aether.util.artifact.JavaScopes;
45  
46  /**
47   * Test case for {@link Classpath}.
48   * @author Yegor Bugayenko (yegor@tpc2.com)
49   * @version $Id$
50   */
51  @SuppressWarnings("unchecked")
52  public final class ClasspathTest {
53  
54      /**
55       * Group of test artifact.
56       */
57      private static final String GROUP = "junit";
58  
59      /**
60       * Temp dir.
61       * @checkstyle VisibilityModifier (3 lines)
62       */
63      @Rule
64      public final transient TemporaryFolder temp = new TemporaryFolder();
65  
66      /**
67       * Classpath can build a classpath.
68       * @throws Exception If there is some problem inside
69       */
70      @Test
71      public void buildsClasspath() throws Exception {
72          MatcherAssert.assertThat(
73              new Classpath(
74                  this.project(
75                      this.dependency(
76                          ClasspathTest.GROUP, ClasspathTest.GROUP, "4.10"
77                      )
78                  ), this.temp.newFolder(), JavaScopes.TEST
79              ),
80              Matchers.<File>hasItems(
81                  Matchers.hasToString(
82                      Matchers.endsWith(
83                          String.format(
84                              // @checkstyle MultipleStringLiterals (2 lines)
85                              "%sas%<sdirectory",
86                              System.getProperty("file.separator")
87                          )
88                      )
89                  ),
90                  Matchers.hasToString(Matchers.endsWith("junit-4.10.jar")),
91                  Matchers.hasToString(Matchers.endsWith("hamcrest-core-1.1.jar"))
92              )
93          );
94      }
95  
96      /**
97       * Classpath should return artifact with highest version number.
98       * @throws Exception If there is some problem inside
99       */
100     @Test
101     public void returnsArtifactWithHighestVersionNumber() throws Exception {
102         MatcherAssert.assertThat(
103             new Classpath(
104                 this.project(
105                     this.dependency(
106                         ClasspathTest.GROUP, ClasspathTest.GROUP, "4.8"
107                     ),
108                     this.dependency(
109                         ClasspathTest.GROUP, ClasspathTest.GROUP, "4.8.2"
110                     )
111                 ), this.temp.newFolder(), JavaScopes.TEST
112             ),
113             Matchers.<File>hasItems(
114                 Matchers.hasToString(
115                     Matchers.endsWith(
116                         String.format(
117                             // @checkstyle MultipleStringLiterals (2 lines)
118                             "%sas%<sdirectory",
119                             System.getProperty("file.separator")
120                         )
121                     )
122                 ),
123                 Matchers.hasToString(Matchers.endsWith("junit-4.8.2.jar"))
124             )
125         );
126     }
127 
128     /**
129      * Classpath can build a classpath without optional dependencies.
130      * @throws Exception If there is some problem inside
131      */
132     @Test
133     public void buildsClasspathWithoutOptionalArtifacts() throws Exception {
134         final Dependency dep = new Dependency();
135         // @checkstyle MultipleStringLiterals (2 lines)
136         dep.setGroupId("commons-validator");
137         dep.setArtifactId("commons-validator");
138         dep.setVersion("1.3.1");
139         dep.setScope(JavaScopes.COMPILE);
140         MatcherAssert.assertThat(
141             new Classpath(
142                 this.project(dep), this.temp.newFolder(), JavaScopes.COMPILE
143             ),
144             Matchers.not(
145                 Matchers.<File>hasItems(
146                     Matchers.hasToString(
147                         Matchers.endsWith("oro-2.0.8.jar")
148                     )
149                 )
150             )
151         );
152     }
153 
154     /**
155      * Classpath can return a string when a dependency is broken.
156      * @throws Exception If there is some problem inside
157      */
158     @Test
159     public void hasToStringWithBrokenDependency() throws Exception {
160         final Dependency dep = new Dependency();
161         dep.setGroupId("junit-broken");
162         dep.setArtifactId("junit-absent");
163         dep.setVersion("1.0");
164         dep.setScope(JavaScopes.TEST);
165         final Classpath classpath = new Classpath(
166             this.project(dep), this.temp.newFolder(), JavaScopes.TEST
167         );
168         MatcherAssert.assertThat(
169             classpath.toString(),
170             Matchers.containsString(
171                 "failed to load 'junit-broken:junit-absent:jar:1.0 (compile)'"
172             )
173         );
174     }
175 
176     /**
177      * Classpath can be compared to another classpath.
178      * @throws Exception If there is some problem inside
179      */
180     @Test
181     public void comparesToAnotherClasspath() throws Exception {
182         final Dependency dep = new Dependency();
183         dep.setGroupId("org.apache.commons");
184         dep.setArtifactId("commons-lang3-absent");
185         dep.setVersion("3.0");
186         dep.setScope(JavaScopes.COMPILE);
187         final Classpath classpath = new Classpath(
188             this.project(dep), this.temp.newFolder(), JavaScopes.TEST
189         );
190         MatcherAssert.assertThat(classpath, Matchers.equalTo(classpath));
191         MatcherAssert.assertThat(
192             classpath.canEqual(classpath),
193             Matchers.is(true)
194         );
195     }
196 
197     /**
198      * Create test dependency.
199      * @param group Dependency group
200      * @param artifact Dependency artifact ID
201      * @param version Dependency Version
202      * @return Created dependency.
203      */
204     private Dependency dependency(final String group, final String artifact,
205         final String version) {
206         final Dependency dep = new Dependency();
207         dep.setGroupId(group);
208         dep.setArtifactId(artifact);
209         dep.setVersion(version);
210         dep.setScope(JavaScopes.TEST);
211         return dep;
212     }
213 
214     /**
215      * Creates project with this dependency.
216      * @param dep Dependency to add to the project
217      * @return Maven project mocked
218      * @throws Exception If there is some problem inside
219      */
220     private MavenProject project(final Dependency... dep) throws Exception {
221         final MavenProject project = Mockito.mock(MavenProject.class);
222         Mockito.doReturn(Arrays.asList("/some/path/as/directory"))
223             .when(project).getTestClasspathElements();
224         Mockito.doReturn(Arrays.asList(dep)).when(project).getDependencies();
225         final List<RemoteRepository> repos = Arrays.asList(
226             new RemoteRepository(
227                 "maven-central",
228                 "default",
229                 "http://repo1.maven.org/maven2/"
230             )
231         );
232         Mockito.doReturn(repos).when(project).getRemoteProjectRepositories();
233         return project;
234     }
235 
236 }