View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.resolver.internal.ant;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.Arrays;
24  import java.util.Iterator;
25  import java.util.Map;
26  
27  import junit.framework.JUnit4TestAdapter;
28  import org.apache.tools.ant.types.Path;
29  import org.apache.tools.ant.types.ResourceCollection;
30  import org.apache.tools.ant.types.resources.FileResource;
31  import org.junit.Test;
32  
33  import static org.hamcrest.MatcherAssert.*;
34  import static org.hamcrest.Matchers.*;
35  
36  public class ResolveTest extends AntBuildsTest {
37      public static junit.framework.Test suite() {
38          return new JUnit4TestAdapter(ResolveTest.class);
39      }
40  
41      @Test
42      public void testResolveGlobalPom() {
43          executeTarget("testResolveGlobalPom");
44  
45          String prop = getProject().getProperty("test.resolve.path.org.eclipse.aether:aether-api:jar");
46          assertThat("aether-api was not resolved as a property", prop, notNullValue());
47          assertThat(
48                  "aether-api was not resolved to default local repository",
49                  prop,
50                  allOf(containsString("aether-api"), endsWith(".jar")));
51      }
52  
53      @Test
54      public void testResolveOverrideGlobalPom() {
55          executeTarget("testResolveOverrideGlobalPom");
56  
57          String prop = getProject().getProperty("test.resolve.path.org.eclipse.aether:aether-api:jar");
58          assertThat("aether-api was not resolved as a property", prop, notNullValue());
59          assertThat(
60                  "aether-api was not resolved to default local repository",
61                  prop,
62                  allOf(containsString("aether-api"), endsWith(".jar")));
63      }
64  
65      @Test
66      public void testResolveGlobalPomIntoOtherLocalRepo() {
67          executeTarget("testResolveGlobalPomIntoOtherLocalRepo");
68  
69          String prop = getProject().getProperty("test.resolve.path.org.eclipse.aether:aether-api:jar");
70          assertThat("aether-api was not resolved as a property", prop, notNullValue());
71          assertThat(
72                  "aether-api was not resolved to default local repository",
73                  prop.replace('\\', '/'),
74                  endsWith("local-repo-custom/org/eclipse/aether/aether-api/0.9.0.M3/aether-api-0.9.0.M3.jar"));
75      }
76  
77      @Test
78      public void testResolveCustomFileLayout() throws IOException {
79          File dir = new File(BUILD_DIR, "resolve-custom-layout");
80          executeTarget("testResolveCustomFileLayout");
81  
82          assertThat(
83                  "aether-api was not saved with custom file layout",
84                  new File(dir, "org.eclipse.aether/aether-api/org/eclipse/aether/jar").exists());
85      }
86  
87      @Test
88      public void testResolveAttachments() throws IOException {
89          File dir = new File(BUILD_DIR, "resolve-attachments");
90          executeTarget("testResolveAttachments");
91  
92          File jdocDir = new File(dir, "javadoc");
93  
94          assertThat(
95                  "aether-api-javadoc was not saved with custom file layout",
96                  new File(jdocDir, "org.eclipse.aether-aether-api-javadoc.jar").exists());
97  
98          assertThat("found non-javadoc files", Arrays.asList(jdocDir.list()), everyItem(endsWith("javadoc.jar")));
99  
100         File sourcesDir = new File(dir, "sources");
101         assertThat(
102                 "aether-api-sources was not saved with custom file layout",
103                 new File(sourcesDir, "org.eclipse.aether-aether-api-sources.jar").exists());
104         assertThat("found non-sources files", Arrays.asList(sourcesDir.list()), everyItem(endsWith("sources.jar")));
105     }
106 
107     @Test
108     public void testResolvePath() {
109         executeTarget("testResolvePath");
110         Map<?, ?> refs = getProject().getReferences();
111         Object obj = refs.get("out");
112         assertThat("ref 'out' is no path", obj, instanceOf(Path.class));
113         Path path = (Path) obj;
114         String[] elements = path.list();
115         assertThat(
116                 "no aether-api on classpath",
117                 elements,
118                 hasItemInArray(allOf(containsString("aether-api"), endsWith(".jar"))));
119     }
120 
121     @Test
122     public void testResolveDepsFromFile() {
123         executeTarget("testResolveDepsFromFile");
124 
125         String prop = getProject().getProperty("test.resolve.path.org.eclipse.aether:aether-spi:jar");
126         assertThat("aether-spi was not resolved as a property", prop, notNullValue());
127         assertThat(
128                 "aether-spi was not resolved to default local repository",
129                 prop,
130                 allOf(containsString("aether-spi"), endsWith(".jar")));
131         prop = getProject().getProperty("test.resolve.path.org.eclipse.aether:aether-api:jar");
132         assertThat("aether-api was resolved as a property", prop, nullValue());
133     }
134 
135     @Test
136     public void testResolveNestedDependencyCollections() {
137         executeTarget("testResolveNestedDependencyCollections");
138 
139         String prop = getProject().getProperty("test.resolve.path.org.eclipse.aether:aether-spi:jar");
140         assertThat("aether-spi was not resolved as a property", prop, notNullValue());
141         prop = getProject().getProperty("test.resolve.path.org.eclipse.aether:aether-util:jar");
142         assertThat("aether-util was not resolved as a property", prop, notNullValue());
143         prop = getProject().getProperty("test.resolve.path.org.eclipse.aether:aether-api:jar");
144         assertThat("aether-api was resolved as a property", prop, nullValue());
145     }
146 
147     @Test
148     public void testResolveResourceCollectionOnly() {
149         executeTarget("testResolveResourceCollectionOnly");
150 
151         ResourceCollection resources = (ResourceCollection) getProject().getReference("files");
152         assertThat(resources, is(notNullValue()));
153         assertThat(resources.size(), is(2));
154         assertThat(resources.isFilesystemOnly(), is(true));
155         Iterator<?> it = resources.iterator();
156         FileResource file = (FileResource) it.next();
157         assertThat(file.getFile().getName(), is("aether-spi-0.9.0.v20140226.jar"));
158         file = (FileResource) it.next();
159         assertThat(file.getFile().getName(), is("aether-api-0.9.0.v20140226.jar"));
160     }
161 
162     @Test
163     public void testResolveTransitiveDependencyManagement() {
164         executeTarget("testResolveTransitiveDependencyManagement");
165 
166         String prop = getProject().getProperty("test.resolve.path.org.slf4j:slf4j-api:jar");
167         assertThat("slf4j-api was not resolved as a property", prop, notNullValue());
168         assertThat(
169                 "slf4j-api was not resolved to default local repository",
170                 prop,
171                 allOf(containsString("slf4j-api"), endsWith("slf4j-api-2.0.6.jar")));
172 
173         prop = getProject().getProperty("test.resolve.path.org.apiguardian:apiguardian-api:jar");
174         assertThat("apiguardian-api was not resolved as a property", prop, notNullValue());
175         assertThat(
176                 "apiguardian-api was not resolved to default local repository",
177                 prop,
178                 allOf(containsString("apiguardian-api"), endsWith("apiguardian-api-1.1.1.jar")));
179     }
180 }