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  
23  import junit.framework.JUnit4TestAdapter;
24  import org.apache.maven.resolver.internal.ant.types.Pom;
25  import org.apache.tools.ant.Project;
26  import org.eclipse.aether.artifact.Artifact;
27  import org.eclipse.aether.artifact.DefaultArtifact;
28  import org.junit.Before;
29  import org.junit.Test;
30  
31  import static org.hamcrest.Matchers.*;
32  import static org.junit.Assert.*;
33  
34  public class ProjectWorkspaceReaderTest {
35      public static junit.framework.Test suite() {
36          return new JUnit4TestAdapter(ProjectWorkspaceReaderTest.class);
37      }
38  
39      private ProjectWorkspaceReader reader;
40  
41      private Project project;
42  
43      @Before
44      public void setUp() throws Exception {
45          this.reader = new ProjectWorkspaceReader();
46  
47          this.project = new Project();
48          project.setProperty("user.home", System.getProperty("user.home"));
49      }
50  
51      private Artifact artifact(String coords) {
52          return new DefaultArtifact(coords);
53      }
54  
55      private File getFile(String name) {
56          return new File("src/test/resources/ProjectWorkspaceReader", name);
57      }
58  
59      @Test
60      public void testFindPom() {
61          Pom pom = new Pom();
62          pom.setProject(project);
63          pom.setFile(getFile("dummy-pom.xml"));
64  
65          reader.addPom(pom);
66  
67          assertEquals(pom.getFile(), reader.findArtifact(artifact("test:dummy:pom:0.1-SNAPSHOT")));
68          assertNull(reader.findArtifact(artifact("unavailable:test:pom:0.1-SNAPSHOT")));
69      }
70  
71      @Test
72      public void testFindArtifact() {
73          Pom pom = new Pom();
74          pom.setProject(project);
75          pom.setFile(getFile("dummy-pom.xml"));
76  
77          reader.addPom(pom);
78  
79          org.apache.maven.resolver.internal.ant.types.Artifact artifact =
80                  new org.apache.maven.resolver.internal.ant.types.Artifact();
81          artifact.setProject(project);
82          artifact.addPom(pom);
83          artifact.setFile(getFile("dummy-file.txt"));
84  
85          reader.addArtifact(artifact);
86  
87          assertEquals(artifact.getFile(), reader.findArtifact(artifact("test:dummy:txt:0.1-SNAPSHOT")));
88          assertNull(reader.findArtifact(artifact("unavailable:test:jar:0.1-SNAPSHOT")));
89      }
90  
91      @Test
92      public void testFindVersions() {
93          Pom pom1 = new Pom();
94          pom1.setProject(project);
95          pom1.setCoords("test:dummy:1-SNAPSHOT");
96  
97          org.apache.maven.resolver.internal.ant.types.Artifact artifact1 =
98                  new org.apache.maven.resolver.internal.ant.types.Artifact();
99          artifact1.setProject(project);
100         artifact1.addPom(pom1);
101         artifact1.setFile(getFile("dummy-file.txt"));
102 
103         reader.addArtifact(artifact1);
104 
105         Pom pom2 = new Pom();
106         pom2.setProject(project);
107         pom2.setCoords("test:dummy:2-SNAPSHOT");
108 
109         org.apache.maven.resolver.internal.ant.types.Artifact artifact2 =
110                 new org.apache.maven.resolver.internal.ant.types.Artifact();
111         artifact2.setProject(project);
112         artifact2.addPom(pom2);
113         artifact2.setFile(getFile("dummy-file.txt"));
114 
115         reader.addArtifact(artifact2);
116 
117         assertThat(
118                 reader.findVersions(artifact("test:dummy:txt:[0,)")), containsInAnyOrder("1-SNAPSHOT", "2-SNAPSHOT"));
119     }
120 }