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 org.apache.maven.artifact.Artifact;
35  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
36  import org.apache.maven.execution.MavenSession;
37  import org.apache.maven.model.Dependency;
38  import org.apache.maven.project.MavenProject;
39  import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
40  import org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException;
41  import org.apache.maven.shared.dependency.graph.DependencyNode;
42  import org.hamcrest.MatcherAssert;
43  import org.hamcrest.Matchers;
44  import org.junit.Ignore;
45  import org.junit.Rule;
46  import org.junit.Test;
47  import org.junit.rules.TemporaryFolder;
48  import org.mockito.Mockito;
49  
50  /**
51   * Test case for {@link com.jcabi.aether.MavenClasspath}.
52   * @author Krzysztof Krason (Krzysztof.Krason@gmail.com)
53   * @version $Id$
54   */
55  @SuppressWarnings("unchecked")
56  public final class MavenClasspathTest {
57  
58      /**
59       * Temp dir.
60       * @checkstyle VisibilityModifier (3 lines)
61       */
62      @Rule
63      public final transient TemporaryFolder temp = new TemporaryFolder();
64  
65      /**
66       * Classpath can build a classpath.
67       * @throws Exception If there is some problem inside
68       */
69      @Test
70      public void buildsClasspath() throws Exception {
71          final Dependency dep = new Dependency();
72          final String group = "junit";
73          dep.setGroupId(group);
74          dep.setArtifactId(group);
75          dep.setVersion("4.10");
76          dep.setScope("test");
77          final String jar = "junit-4.10.jar";
78          final DependencyGraphBuilder builder = this.builder(jar);
79          final MavenSession session = Mockito.mock(MavenSession.class);
80          final MavenProject project = this.project(dep);
81          Mockito.when(session.getCurrentProject()).thenReturn(project);
82          MatcherAssert.assertThat(
83              new MavenClasspath(builder, session, MavenClasspath.TEST_SCOPE),
84              Matchers.<File>hasItems(
85                  Matchers.hasToString(
86                      Matchers.endsWith(
87                          String.format(
88                              "%sas%<sdirectory",
89                              System.getProperty("file.separator")
90                          )
91                      )
92                  ),
93                  Matchers.hasToString(Matchers.endsWith(jar))
94              )
95          );
96      }
97  
98      /**
99       * Classpath can return a string when a dependency is broken.
100      * @throws Exception If there is some problem inside
101      * @todo #28 Add handling of broken dependencies in toString.
102      */
103     @Test
104     @Ignore
105     public void hasToStringWithBrokenDependency() throws Exception {
106         final Dependency dep = new Dependency();
107         dep.setGroupId("junit-broken");
108         dep.setArtifactId("junit-absent");
109         dep.setVersion("1.0");
110         dep.setScope(MavenClasspath.TEST_SCOPE);
111         final DependencyGraphBuilder builder =
112             Mockito.mock(DependencyGraphBuilder.class);
113         final MavenSession session = Mockito.mock(MavenSession.class);
114         final MavenProject project = this.project(dep);
115         Mockito.when(session.getCurrentProject()).thenReturn(project);
116         final MavenClasspath classpath = new MavenClasspath(
117             builder, session, MavenClasspath.TEST_SCOPE
118         );
119         MatcherAssert.assertThat(
120             classpath.toString(),
121             Matchers.containsString(
122                 "failed to load 'junit-broken:junit-absent:jar:1.0 (compile)'"
123             )
124         );
125     }
126 
127     /**
128      * Classpath can be compared to another classpath.
129      * @throws Exception If there is some problem inside
130      */
131     @Test
132     public void comparesToAnotherClasspath() throws Exception {
133         final Dependency dep = new Dependency();
134         dep.setGroupId("org.apache.commons");
135         dep.setArtifactId("commons-lang3-absent");
136         dep.setVersion("3.0");
137         dep.setScope(MavenClasspath.COMPILE_SCOPE);
138         final DependencyGraphBuilder builder =
139             Mockito.mock(DependencyGraphBuilder.class);
140         final MavenSession session = Mockito.mock(MavenSession.class);
141         final MavenProject project = this.project(dep);
142         Mockito.when(session.getCurrentProject()).thenReturn(project);
143         final MavenClasspath classpath = new MavenClasspath(
144             builder, session, MavenClasspath.TEST_SCOPE
145         );
146         MatcherAssert.assertThat(classpath, Matchers.equalTo(classpath));
147         MatcherAssert.assertThat(
148             classpath.canEqual(classpath),
149             Matchers.is(true)
150         );
151     }
152 
153     /**
154      * Build DependencyGraphBuilder with single dependency node.
155      * @param location Location of node jar.
156      * @return Container.
157      */
158     private DependencyGraphBuilder builder(final String location) {
159         final DependencyGraphBuilder builder = Mockito
160             .mock(DependencyGraphBuilder.class);
161         final DependencyNode node = Mockito.mock(DependencyNode.class);
162         try {
163             Mockito.when(
164                 builder.buildDependencyGraph(
165                     Mockito.any(MavenProject.class),
166                     Mockito.any(ArtifactFilter.class)
167                 )
168             )
169                 .thenReturn(node);
170         } catch (final DependencyGraphBuilderException ex) {
171             throw new IllegalStateException(ex);
172         }
173         final Artifact artifact = Mockito.mock(Artifact.class);
174         Mockito.when(artifact.getFile()).thenReturn(new File(location));
175         Mockito.when(node.getArtifact()).thenReturn(artifact);
176         return builder;
177     }
178 
179     /**
180      * Creates project with this dependency.
181      * @param dep Dependency to add to the project
182      * @return Maven project mocked
183      * @throws Exception If there is some problem inside
184      */
185     private MavenProject project(final Dependency dep) throws Exception {
186         final MavenProject project = Mockito.mock(MavenProject.class);
187         Mockito.doReturn(Arrays.asList("/some/path/as/directory"))
188             .when(project).getTestClasspathElements();
189         Mockito.doReturn(Arrays.asList(dep)).when(project).getDependencies();
190         return project;
191     }
192 
193 }