1 package org.apache.maven.artifact;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.factory.ArtifactFactory;
23 import org.apache.maven.artifact.repository.ArtifactRepository;
24 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
25 import org.apache.maven.artifact.repository.DefaultArtifactRepository;
26 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
27 import org.codehaus.plexus.PlexusTestCase;
28
29 import java.io.File;
30 import java.io.FileWriter;
31 import java.io.IOException;
32 import java.io.Writer;
33 import java.util.ArrayList;
34 import java.util.List;
35
36
37
38
39
40
41 public abstract class AbstractArtifactComponentTestCase
42 extends PlexusTestCase
43 {
44 protected abstract String component();
45
46
47
48
49
50
51 protected ArtifactRepository badLocalRepository()
52 throws Exception
53 {
54 String path = "target/test-classes/repositories/" + component() + "/bad-local-repository";
55
56 File f = new File( getBasedir(), path );
57
58 f.createNewFile();
59
60 ArtifactRepositoryLayout repoLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE,
61 "legacy" );
62
63 return new DefaultArtifactRepository( "test", "file://" + f.getPath(), repoLayout );
64 }
65
66 protected String getRepositoryLayout()
67 {
68 return "legacy";
69 }
70
71 protected ArtifactRepository localRepository()
72 throws Exception
73 {
74 String path = "target/test-classes/repositories/" + component() + "/local-repository";
75
76 File f = new File( getBasedir(), path );
77
78 ArtifactRepositoryLayout repoLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE,
79 "legacy" );
80
81 return new DefaultArtifactRepository( "local", "file://" + f.getPath(), repoLayout );
82 }
83
84 protected ArtifactRepository remoteRepository()
85 throws Exception
86 {
87 String path = "target/test-classes/repositories/" + component() + "/remote-repository";
88
89 File f = new File( getBasedir(), path );
90
91 ArtifactRepositoryLayout repoLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE,
92 "legacy" );
93
94 return new DefaultArtifactRepository( "test", "file://" + f.getPath(), repoLayout,
95 new ArtifactRepositoryPolicy(), new ArtifactRepositoryPolicy() );
96 }
97
98 protected ArtifactRepository badRemoteRepository()
99 throws Exception
100 {
101 ArtifactRepositoryLayout repoLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE,
102 "legacy" );
103
104 return new DefaultArtifactRepository( "test", "http://foo.bar/repository", repoLayout );
105 }
106
107 protected void assertRemoteArtifactPresent( Artifact artifact )
108 throws Exception
109 {
110 ArtifactRepository remoteRepo = remoteRepository();
111
112 String path = remoteRepo.pathOf( artifact );
113
114 File file = new File( remoteRepo.getBasedir(), path );
115
116 if ( !file.exists() )
117 {
118 fail( "Remote artifact " + file + " should be present." );
119 }
120 }
121
122 protected void assertLocalArtifactPresent( Artifact artifact )
123 throws Exception
124 {
125 ArtifactRepository localRepo = localRepository();
126
127 String path = localRepo.pathOf( artifact );
128
129 File file = new File( localRepo.getBasedir(), path );
130
131 if ( !file.exists() )
132 {
133 fail( "Local artifact " + file + " should be present." );
134 }
135 }
136
137 protected void assertRemoteArtifactNotPresent( Artifact artifact )
138 throws Exception
139 {
140 ArtifactRepository remoteRepo = remoteRepository();
141
142 String path = remoteRepo.pathOf( artifact );
143
144 File file = new File( remoteRepo.getBasedir(), path );
145
146 if ( file.exists() )
147 {
148 fail( "Remote artifact " + file + " should not be present." );
149 }
150 }
151
152 protected void assertLocalArtifactNotPresent( Artifact artifact )
153 throws Exception
154 {
155 ArtifactRepository localRepo = localRepository();
156
157 String path = localRepo.pathOf( artifact );
158
159 File file = new File( localRepo.getBasedir(), path );
160
161 if ( file.exists() )
162 {
163 fail( "Local artifact " + file + " should not be present." );
164 }
165 }
166
167
168
169
170
171 protected List remoteRepositories()
172 throws Exception
173 {
174 List remoteRepositories = new ArrayList();
175
176 remoteRepositories.add( remoteRepository() );
177
178 return remoteRepositories;
179 }
180
181
182
183
184
185 protected Artifact createLocalArtifact( String artifactId, String version )
186 throws Exception
187 {
188 Artifact artifact = createArtifact( artifactId, version );
189
190 createArtifact( artifact, localRepository() );
191
192 return artifact;
193 }
194
195 protected Artifact createRemoteArtifact( String artifactId, String version )
196 throws Exception
197 {
198 Artifact artifact = createArtifact( artifactId, version );
199
200 createArtifact( artifact, remoteRepository() );
201
202 return artifact;
203 }
204
205 protected void createLocalArtifact( Artifact artifact )
206 throws Exception
207 {
208 createArtifact( artifact, localRepository() );
209 }
210
211 protected void createRemoteArtifact( Artifact artifact )
212 throws Exception
213 {
214 createArtifact( artifact, remoteRepository() );
215 }
216
217 protected void createArtifact( Artifact artifact, ArtifactRepository repository )
218 throws Exception
219 {
220 String path = repository.pathOf( artifact );
221
222 File artifactFile = new File( repository.getBasedir(), path );
223
224 if ( !artifactFile.getParentFile().exists() )
225 {
226 artifactFile.getParentFile().mkdirs();
227 }
228
229 Writer writer = new FileWriter( artifactFile );
230
231 writer.write( artifact.getId() );
232
233 writer.close();
234 }
235
236 protected Artifact createArtifact( String artifactId, String version )
237 throws Exception
238 {
239 return createArtifact( artifactId, version, "jar" );
240 }
241
242 protected Artifact createArtifact( String artifactId, String version, String type )
243 throws Exception
244 {
245 return createArtifact( "org.apache.maven", artifactId, version, type );
246 }
247
248 protected Artifact createArtifact( String groupId, String artifactId, String version, String type )
249 throws Exception
250 {
251 ArtifactFactory artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
252
253 return artifactFactory.createBuildArtifact( groupId, artifactId, version, type );
254 }
255
256 protected void deleteLocalArtifact( Artifact artifact )
257 throws Exception
258 {
259 deleteArtifact( artifact, localRepository() );
260 }
261
262 protected void deleteArtifact( Artifact artifact, ArtifactRepository repository )
263 throws Exception
264 {
265 String path = repository.pathOf( artifact );
266
267 File artifactFile = new File( repository.getBasedir(), path );
268
269 if ( artifactFile.exists() )
270 {
271 if ( !artifactFile.delete() )
272 {
273 throw new IOException( "Failure while attempting to delete artifact " + artifactFile );
274 }
275 }
276 }
277 }
278