1 package org.apache.maven.plugin.dependency;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23
24 import org.apache.maven.artifact.Artifact;
25 import org.apache.maven.artifact.repository.ArtifactRepository;
26 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
27 import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
28 import org.apache.maven.artifact.repository.layout.LegacyRepositoryLayout;
29 import org.apache.maven.plugin.MojoFailureException;
30 import org.apache.maven.plugin.testing.stubs.StubArtifactRepository;
31 import org.codehaus.plexus.util.FileUtils;
32
33 public class TestGetMojo
34 extends AbstractDependencyMojoTestCase
35 {
36 GetMojo mojo;
37
38 protected void setUp()
39 throws Exception
40 {
41
42 super.setUp( "markers", false );
43
44 File testPom = new File( getBasedir(), "target/test-classes/unit/get-test/plugin-config.xml" );
45 assert testPom.exists();
46 mojo = (GetMojo) lookupMojo( "get", testPom );
47
48 assertNotNull( mojo );
49 setVariableValueToObject( mojo, "localRepository", new StubArtifactRepository( testDir.getAbsolutePath() ){
50 @Override
51 public String pathOf( Artifact artifact )
52 {
53 StringBuilder pathOf = new StringBuilder();
54 pathOf.append( artifact.getGroupId() )
55 .append( '_' )
56 .append( artifact.getArtifactId() )
57 .append( '-' )
58 .append( artifact.getVersion() );
59 if ( artifact.getClassifier() != null )
60 {
61 pathOf.append( '-' )
62 .append( artifact.getClassifier() );
63 }
64 pathOf.append( '.' ).append( artifact.getArtifactHandler().getExtension() );
65 return pathOf.toString();
66 }
67 } );
68 }
69
70
71
72
73
74
75 public void testTransitive()
76 throws Exception
77 {
78
79 setVariableValueToObject( mojo, "transitive", Boolean.FALSE );
80 setVariableValueToObject( mojo, "repositoryUrl", "http://repo1.maven.apache.org/maven2" );
81 setVariableValueToObject( mojo, "groupId", "org.apache.maven" );
82 setVariableValueToObject( mojo, "artifactId", "maven-model" );
83 setVariableValueToObject( mojo, "version", "2.0.9" );
84
85 mojo.execute();
86
87
88 setVariableValueToObject( mojo, "transitive", Boolean.FALSE );
89 mojo.execute();
90 }
91
92
93
94
95
96
97 public void testDestination()
98 throws Exception
99 {
100
101 setVariableValueToObject( mojo, "transitive", Boolean.FALSE );
102 setVariableValueToObject( mojo, "repositoryUrl", "http://repo1.maven.apache.org/maven2" );
103 setVariableValueToObject( mojo, "groupId", "org.apache.maven" );
104 setVariableValueToObject( mojo, "artifactId", "maven-model" );
105 setVariableValueToObject( mojo, "version", "2.0.9" );
106 File output = new File( getBasedir(), "target/unit-tests/get-test/destination-file/maven-model-2.0.9.jar" );
107 output.delete();
108 setVariableValueToObject( mojo, "destination", output.getAbsolutePath() );
109
110 mojo.execute();
111 assertTrue( output.exists() );
112
113
114 output = new File( getBasedir(), "target/unit-tests/get-test/destination-dir" );
115 output.mkdirs();
116 FileUtils.cleanDirectory( output );
117 setVariableValueToObject( mojo, "destination", output.getAbsolutePath() );
118
119 mojo.execute();
120 assertTrue( new File( output, "org.apache.maven_maven-model-2.0.9.jar" ).exists() );
121 }
122
123
124
125
126
127
128
129
130 public void testRemoteRepositories()
131 throws Exception
132 {
133 setVariableValueToObject( mojo, "remoteRepositories", "central::default::http://repo1.maven.apache.org/maven2,"
134 + "central::::http://repo1.maven.apache.org/maven2," + "http://repo1.maven.apache.org/maven2" );
135 setVariableValueToObject( mojo, "groupId", "org.apache.maven" );
136 setVariableValueToObject( mojo, "artifactId", "maven-model" );
137 setVariableValueToObject( mojo, "version", "2.0.9" );
138
139 mojo.execute();
140 }
141
142
143
144
145
146
147 public void testParseRepository()
148 throws Exception
149 {
150 ArtifactRepository repo;
151 ArtifactRepositoryPolicy policy = null;
152 repo = mojo.parseRepository( "central::default::http://repo1.maven.apache.org/maven2", policy );
153 assertEquals( "central", repo.getId() );
154 assertEquals( DefaultRepositoryLayout.class, repo.getLayout().getClass() );
155 assertEquals( "http://repo1.maven.apache.org/maven2", repo.getUrl() );
156
157 repo = mojo.parseRepository( "central::legacy::http://repo1.maven.apache.org/maven2", policy );
158 assertEquals( "central", repo.getId() );
159 assertEquals( LegacyRepositoryLayout.class, repo.getLayout().getClass() );
160 assertEquals( "http://repo1.maven.apache.org/maven2", repo.getUrl() );
161
162 repo = mojo.parseRepository( "central::::http://repo1.maven.apache.org/maven2", policy );
163 assertEquals( "central", repo.getId() );
164 assertEquals( DefaultRepositoryLayout.class, repo.getLayout().getClass() );
165 assertEquals( "http://repo1.maven.apache.org/maven2", repo.getUrl() );
166
167 repo = mojo.parseRepository( "http://repo1.maven.apache.org/maven2", policy );
168 assertEquals( "temp", repo.getId() );
169 assertEquals( DefaultRepositoryLayout.class, repo.getLayout().getClass() );
170 assertEquals( "http://repo1.maven.apache.org/maven2", repo.getUrl() );
171
172 try
173 {
174 repo = mojo.parseRepository( "::::http://repo1.maven.apache.org/maven2", policy );
175 fail( "Exception expected" );
176 }
177 catch ( MojoFailureException e )
178 {
179
180 }
181
182 try
183 {
184 repo = mojo.parseRepository( "central::http://repo1.maven.apache.org/maven2", policy );
185 fail( "Exception expected" );
186 }
187 catch ( MojoFailureException e )
188 {
189
190 }
191 }
192 }