1 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 ; 27 import java.io.FileWriter ; 28 import java.io.IOException ; 29 import java.io.Writer ; 30 import java.util.ArrayList ; 31 import java.util.List ; 32 33 38 public abstract class AbstractArtifactComponentTestCase 39 extends PlexusTestCase 40 { 41 protected abstract String component(); 42 43 48 protected ArtifactRepository badLocalRepository() 49 throws Exception 50 { 51 String path = "target/test-classes/repositories/" + component() + "/bad-local-repository"; 52 53 File f = new File ( 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 getRepositoryLayout() 64 { 65 return "legacy"; 66 } 67 68 protected ArtifactRepository localRepository() 69 throws Exception 70 { 71 String path = "target/test-classes/repositories/" + component() + "/local-repository"; 72 73 File f = new File ( 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 83 { 84 String path = "target/test-classes/repositories/" + component() + "/remote-repository"; 85 86 File f = new File ( 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 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 106 { 107 ArtifactRepository remoteRepo = remoteRepository(); 108 109 String path = remoteRepo.pathOf( artifact ); 110 111 File file = new File ( 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 121 { 122 ArtifactRepository localRepo = localRepository(); 123 124 String path = localRepo.pathOf( artifact ); 125 126 File file = new File ( 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 136 { 137 ArtifactRepository remoteRepo = remoteRepository(); 138 139 String path = remoteRepo.pathOf( artifact ); 140 141 File file = new File ( 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 151 { 152 ArtifactRepository localRepo = localRepository(); 153 154 String path = localRepo.pathOf( artifact ); 155 156 File file = new File ( localRepo.getBasedir(), path ); 157 158 if ( file.exists() ) 159 { 160 fail( "Local artifact " + file + " should not be present." ); 161 } 162 } 163 164 168 protected List remoteRepositories() 169 throws Exception 170 { 171 List remoteRepositories = new ArrayList (); 172 173 remoteRepositories.add( remoteRepository() ); 174 175 return remoteRepositories; 176 } 177 178 182 protected Artifact createLocalArtifact( String artifactId, String version ) 183 throws Exception 184 { 185 Artifact artifact = createArtifact( artifactId, version ); 186 187 createArtifact( artifact, localRepository() ); 188 189 return artifact; 190 } 191 192 protected Artifact createRemoteArtifact( String artifactId, String version ) 193 throws Exception 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 204 { 205 createArtifact( artifact, localRepository() ); 206 } 207 208 protected void createRemoteArtifact( Artifact artifact ) 209 throws Exception 210 { 211 createArtifact( artifact, remoteRepository() ); 212 } 213 214 protected void createArtifact( Artifact artifact, ArtifactRepository repository ) 215 throws Exception 216 { 217 String path = repository.pathOf( artifact ); 218 219 File artifactFile = new File ( repository.getBasedir(), path ); 220 221 if ( !artifactFile.getParentFile().exists() ) 222 { 223 artifactFile.getParentFile().mkdirs(); 224 } 225 226 Writer writer = new FileWriter ( artifactFile ); 227 228 writer.write( artifact.getId() ); 229 230 writer.close(); 231 } 232 233 protected Artifact createArtifact( String artifactId, String version ) 234 throws Exception 235 { 236 return createArtifact( artifactId, version, "jar" ); 237 } 238 239 protected Artifact createArtifact( String artifactId, String version, String type ) 240 throws Exception 241 { 242 return createArtifact( "org.apache.maven", artifactId, version, type ); 243 } 244 245 protected Artifact createArtifact( String groupId, String artifactId, String version, String type ) 246 throws Exception 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 255 { 256 deleteArtifact( artifact, localRepository() ); 257 } 258 259 protected void deleteArtifact( Artifact artifact, ArtifactRepository repository ) 260 throws Exception 261 { 262 String path = repository.pathOf( artifact ); 263 264 File artifactFile = new File ( repository.getBasedir(), path ); 265 266 if ( artifactFile.exists() ) 267 { 268 if ( !artifactFile.delete() ) 269 { 270 throw new IOException ( "Failure while attempting to delete artifact " + artifactFile ); 271 } 272 } 273 } 274 } 275 276 | Popular Tags |