KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > artifact > AbstractArtifactComponentTestCase


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.maven.artifact;
18
19 import org.apache.maven.artifact.factory.ArtifactFactory;
20 import org.apache.maven.artifact.repository.ArtifactRepository;
21 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
22 import org.apache.maven.artifact.repository.DefaultArtifactRepository;
23 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
24 import org.codehaus.plexus.PlexusTestCase;
25
26 import java.io.File JavaDoc;
27 import java.io.FileWriter JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.Writer JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.List JavaDoc;
32
33 /**
34  * @author <a HREF="mailto:jason@maven.org">Jason van Zyl </a>
35  * @version $Id: AbstractArtifactComponentTestCase.java,v 1.5 2004/10/23 13:33:59
36  * jvanzyl Exp $
37  */

38 public abstract class AbstractArtifactComponentTestCase
39     extends PlexusTestCase
40 {
41     protected abstract String JavaDoc component();
42
43     /**
44      * Return an existing file, not a directory - causes creation to fail.
45      *
46      * @throws Exception
47      */

48     protected ArtifactRepository badLocalRepository()
49         throws Exception JavaDoc
50     {
51         String JavaDoc path = "target/test-classes/repositories/" + component() + "/bad-local-repository";
52
53         File JavaDoc f = new File JavaDoc( getBasedir(), path );
54
55         f.createNewFile();
56
57         ArtifactRepositoryLayout repoLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE,
58                                                                                  "legacy" );
59
60         return new DefaultArtifactRepository( "test", "file://" + f.getPath(), repoLayout );
61     }
62
63     protected String JavaDoc getRepositoryLayout()
64     {
65         return "legacy";
66     }
67
68     protected ArtifactRepository localRepository()
69         throws Exception JavaDoc
70     {
71         String JavaDoc path = "target/test-classes/repositories/" + component() + "/local-repository";
72
73         File JavaDoc f = new File JavaDoc( getBasedir(), path );
74
75         ArtifactRepositoryLayout repoLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE,
76                                                                                  "legacy" );
77
78         return new DefaultArtifactRepository( "local", "file://" + f.getPath(), repoLayout );
79     }
80
81     protected ArtifactRepository remoteRepository()
82         throws Exception JavaDoc
83     {
84         String JavaDoc path = "target/test-classes/repositories/" + component() + "/remote-repository";
85
86         File JavaDoc f = new File JavaDoc( getBasedir(), path );
87
88         ArtifactRepositoryLayout repoLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE,
89                                                                                  "legacy" );
90
91         return new DefaultArtifactRepository( "test", "file://" + f.getPath(), repoLayout,
92                                               new ArtifactRepositoryPolicy(), new ArtifactRepositoryPolicy() );
93     }
94
95     protected ArtifactRepository badRemoteRepository()
96         throws Exception JavaDoc
97     {
98         ArtifactRepositoryLayout repoLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE,
99                                                                                  "legacy" );
100
101         return new DefaultArtifactRepository( "test", "http://foo.bar/repository", repoLayout );
102     }
103
104     protected void assertRemoteArtifactPresent( Artifact artifact )
105         throws Exception JavaDoc
106     {
107         ArtifactRepository remoteRepo = remoteRepository();
108
109         String JavaDoc path = remoteRepo.pathOf( artifact );
110
111         File JavaDoc file = new File JavaDoc( remoteRepo.getBasedir(), path );
112
113         if ( !file.exists() )
114         {
115             fail( "Remote artifact " + file + " should be present." );
116         }
117     }
118
119     protected void assertLocalArtifactPresent( Artifact artifact )
120         throws Exception JavaDoc
121     {
122         ArtifactRepository localRepo = localRepository();
123
124         String JavaDoc path = localRepo.pathOf( artifact );
125
126         File JavaDoc file = new File JavaDoc( localRepo.getBasedir(), path );
127
128         if ( !file.exists() )
129         {
130             fail( "Local artifact " + file + " should be present." );
131         }
132     }
133
134     protected void assertRemoteArtifactNotPresent( Artifact artifact )
135         throws Exception JavaDoc
136     {
137         ArtifactRepository remoteRepo = remoteRepository();
138
139         String JavaDoc path = remoteRepo.pathOf( artifact );
140
141         File JavaDoc file = new File JavaDoc( remoteRepo.getBasedir(), path );
142
143         if ( file.exists() )
144         {
145             fail( "Remote artifact " + file + " should not be present." );
146         }
147     }
148
149     protected void assertLocalArtifactNotPresent( Artifact artifact )
150         throws Exception JavaDoc
151     {
152         ArtifactRepository localRepo = localRepository();
153
154         String JavaDoc path = localRepo.pathOf( artifact );
155
156         File JavaDoc file = new File JavaDoc( localRepo.getBasedir(), path );
157
158         if ( file.exists() )
159         {
160             fail( "Local artifact " + file + " should not be present." );
161         }
162     }
163
164     // ----------------------------------------------------------------------
165
//
166
// ----------------------------------------------------------------------
167

168     protected List JavaDoc remoteRepositories()
169         throws Exception JavaDoc
170     {
171         List JavaDoc remoteRepositories = new ArrayList JavaDoc();
172
173         remoteRepositories.add( remoteRepository() );
174
175         return remoteRepositories;
176     }
177
178     // ----------------------------------------------------------------------
179
// Test artifact generation for unit tests
180
// ----------------------------------------------------------------------
181

182     protected Artifact createLocalArtifact( String JavaDoc artifactId, String JavaDoc version )
183         throws Exception JavaDoc
184     {
185         Artifact artifact = createArtifact( artifactId, version );
186
187         createArtifact( artifact, localRepository() );
188
189         return artifact;
190     }
191
192     protected Artifact createRemoteArtifact( String JavaDoc artifactId, String JavaDoc version )
193         throws Exception JavaDoc
194     {
195         Artifact artifact = createArtifact( artifactId, version );
196
197         createArtifact( artifact, remoteRepository() );
198
199         return artifact;
200     }
201
202     protected void createLocalArtifact( Artifact artifact )
203         throws Exception JavaDoc
204     {
205         createArtifact( artifact, localRepository() );
206     }
207
208     protected void createRemoteArtifact( Artifact artifact )
209         throws Exception JavaDoc
210     {
211         createArtifact( artifact, remoteRepository() );
212     }
213
214     protected void createArtifact( Artifact artifact, ArtifactRepository repository )
215         throws Exception JavaDoc
216     {
217         String JavaDoc path = repository.pathOf( artifact );
218
219         File JavaDoc artifactFile = new File JavaDoc( repository.getBasedir(), path );
220
221         if ( !artifactFile.getParentFile().exists() )
222         {
223             artifactFile.getParentFile().mkdirs();
224         }
225
226         Writer JavaDoc writer = new FileWriter JavaDoc( artifactFile );
227
228         writer.write( artifact.getId() );
229
230         writer.close();
231     }
232
233     protected Artifact createArtifact( String JavaDoc artifactId, String JavaDoc version )
234         throws Exception JavaDoc
235     {
236         return createArtifact( artifactId, version, "jar" );
237     }
238
239     protected Artifact createArtifact( String JavaDoc artifactId, String JavaDoc version, String JavaDoc type )
240         throws Exception JavaDoc
241     {
242         return createArtifact( "org.apache.maven", artifactId, version, type );
243     }
244
245     protected Artifact createArtifact( String JavaDoc groupId, String JavaDoc artifactId, String JavaDoc version, String JavaDoc type )
246         throws Exception JavaDoc
247     {
248         ArtifactFactory artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
249
250         return artifactFactory.createBuildArtifact( groupId, artifactId, version, type );
251     }
252
253     protected void deleteLocalArtifact( Artifact artifact )
254         throws Exception JavaDoc
255     {
256         deleteArtifact( artifact, localRepository() );
257     }
258
259     protected void deleteArtifact( Artifact artifact, ArtifactRepository repository )
260         throws Exception JavaDoc
261     {
262         String JavaDoc path = repository.pathOf( artifact );
263
264         File JavaDoc artifactFile = new File JavaDoc( repository.getBasedir(), path );
265
266         if ( artifactFile.exists() )
267         {
268             if ( !artifactFile.delete() )
269             {
270                 throw new IOException JavaDoc( "Failure while attempting to delete artifact " + artifactFile );
271             }
272         }
273     }
274 }
275
276
Popular Tags