001package org.eclipse.aether.internal.impl; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import static org.junit.Assert.*; 023 024import java.io.File; 025import java.io.IOException; 026import java.net.URI; 027import java.util.Arrays; 028import java.util.Collection; 029import java.util.Collections; 030 031import org.eclipse.aether.RepositorySystemSession; 032import org.eclipse.aether.artifact.Artifact; 033import org.eclipse.aether.artifact.DefaultArtifact; 034import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManager; 035import org.eclipse.aether.internal.test.util.TestFileUtils; 036import org.eclipse.aether.internal.test.util.TestUtils; 037import org.eclipse.aether.metadata.DefaultMetadata; 038import org.eclipse.aether.metadata.Metadata; 039import org.eclipse.aether.metadata.Metadata.Nature; 040import org.eclipse.aether.repository.LocalArtifactRegistration; 041import org.eclipse.aether.repository.LocalArtifactRequest; 042import org.eclipse.aether.repository.LocalArtifactResult; 043import org.eclipse.aether.repository.LocalMetadataRequest; 044import org.eclipse.aether.repository.LocalMetadataResult; 045import org.eclipse.aether.repository.RemoteRepository; 046import org.junit.After; 047import org.junit.Before; 048import org.junit.Test; 049 050public class EnhancedLocalRepositoryManagerTest 051{ 052 053 private Artifact artifact; 054 055 private Artifact snapshot; 056 057 private File basedir; 058 059 private EnhancedLocalRepositoryManager manager; 060 061 private File artifactFile; 062 063 private RemoteRepository repository; 064 065 private String testContext = "project/compile"; 066 067 private RepositorySystemSession session; 068 069 private Metadata metadata; 070 071 private Metadata noVerMetadata; 072 073 @Before 074 public void setup() 075 throws Exception 076 { 077 String url = TestFileUtils.createTempDir( "enhanced-remote-repo" ).toURI().toURL().toString(); 078 repository = 079 new RemoteRepository.Builder( "enhanced-remote-repo", "default", url ).setRepositoryManager( true ).build(); 080 081 artifact = 082 new DefaultArtifact( "gid", "aid", "", "jar", "1-test", Collections.<String, String> emptyMap(), 083 TestFileUtils.createTempFile( "artifact" ) ); 084 085 snapshot = 086 new DefaultArtifact( "gid", "aid", "", "jar", "1.0-20120710.231549-9", 087 Collections.<String, String> emptyMap(), TestFileUtils.createTempFile( "artifact" ) ); 088 089 metadata = 090 new DefaultMetadata( "gid", "aid", "1-test", "maven-metadata.xml", Nature.RELEASE, 091 TestFileUtils.createTempFile( "metadata" ) ); 092 093 noVerMetadata = 094 new DefaultMetadata( "gid", "aid", null, "maven-metadata.xml", Nature.RELEASE, 095 TestFileUtils.createTempFile( "metadata" ) ); 096 097 basedir = TestFileUtils.createTempDir( "enhanced-repo" ); 098 session = TestUtils.newSession(); 099 manager = new EnhancedLocalRepositoryManager( basedir, session ); 100 101 artifactFile = new File( basedir, manager.getPathForLocalArtifact( artifact ) ); 102 } 103 104 @After 105 public void tearDown() 106 throws Exception 107 { 108 TestFileUtils.deleteFile( basedir ); 109 TestFileUtils.deleteFile( new File( new URI( repository.getUrl() ) ) ); 110 111 session = null; 112 manager = null; 113 repository = null; 114 artifact = null; 115 } 116 117 private long addLocalArtifact( Artifact artifact ) 118 throws IOException 119 { 120 manager.add( session, new LocalArtifactRegistration( artifact ) ); 121 String path = manager.getPathForLocalArtifact( artifact ); 122 123 return copy( artifact, path ); 124 } 125 126 private long addRemoteArtifact( Artifact artifact ) 127 throws IOException 128 { 129 Collection<String> contexts = Arrays.asList( testContext ); 130 manager.add( session, new LocalArtifactRegistration( artifact, repository, contexts ) ); 131 String path = manager.getPathForRemoteArtifact( artifact, repository, testContext ); 132 return copy( artifact, path ); 133 } 134 135 private long copy( Metadata metadata, String path ) 136 throws IOException 137 { 138 if ( metadata.getFile() == null ) 139 { 140 return -1L; 141 } 142 return TestFileUtils.copyFile( metadata.getFile(), new File( basedir, path ) ); 143 } 144 145 private long copy( Artifact artifact, String path ) 146 throws IOException 147 { 148 if ( artifact.getFile() == null ) 149 { 150 return -1L; 151 } 152 File artifactFile = new File( basedir, path ); 153 return TestFileUtils.copyFile( artifact.getFile(), artifactFile ); 154 } 155 156 @Test 157 public void testGetPathForLocalArtifact() 158 { 159 Artifact artifact = new DefaultArtifact( "g.i.d:a.i.d:1.0-SNAPSHOT" ); 160 assertEquals( "1.0-SNAPSHOT", artifact.getBaseVersion() ); 161 assertEquals( "g/i/d/a.i.d/1.0-SNAPSHOT/a.i.d-1.0-SNAPSHOT.jar", manager.getPathForLocalArtifact( artifact ) ); 162 163 artifact = new DefaultArtifact( "g.i.d:a.i.d:1.0-20110329.221805-4" ); 164 assertEquals( "1.0-SNAPSHOT", artifact.getBaseVersion() ); 165 assertEquals( "g/i/d/a.i.d/1.0-SNAPSHOT/a.i.d-1.0-SNAPSHOT.jar", manager.getPathForLocalArtifact( artifact ) ); 166 } 167 168 @Test 169 public void testGetPathForRemoteArtifact() 170 { 171 RemoteRepository remoteRepo = new RemoteRepository.Builder( "repo", "default", "ram:/void" ).build(); 172 173 Artifact artifact = new DefaultArtifact( "g.i.d:a.i.d:1.0-SNAPSHOT" ); 174 assertEquals( "1.0-SNAPSHOT", artifact.getBaseVersion() ); 175 assertEquals( "g/i/d/a.i.d/1.0-SNAPSHOT/a.i.d-1.0-SNAPSHOT.jar", 176 manager.getPathForRemoteArtifact( artifact, remoteRepo, "" ) ); 177 178 artifact = new DefaultArtifact( "g.i.d:a.i.d:1.0-20110329.221805-4" ); 179 assertEquals( "1.0-SNAPSHOT", artifact.getBaseVersion() ); 180 assertEquals( "g/i/d/a.i.d/1.0-SNAPSHOT/a.i.d-1.0-20110329.221805-4.jar", 181 manager.getPathForRemoteArtifact( artifact, remoteRepo, "" ) ); 182 } 183 184 @Test 185 public void testFindLocalArtifact() 186 throws Exception 187 { 188 addLocalArtifact( artifact ); 189 190 LocalArtifactRequest request = new LocalArtifactRequest( artifact, null, null ); 191 LocalArtifactResult result = manager.find( session, request ); 192 assertTrue( result.isAvailable() ); 193 assertEquals( null, result.getRepository() ); 194 195 snapshot = snapshot.setVersion( snapshot.getBaseVersion() ); 196 addLocalArtifact( snapshot ); 197 198 request = new LocalArtifactRequest( snapshot, null, null ); 199 result = manager.find( session, request ); 200 assertTrue( result.isAvailable() ); 201 assertEquals( null, result.getRepository() ); 202 } 203 204 @Test 205 public void testFindRemoteArtifact() 206 throws Exception 207 { 208 addRemoteArtifact( artifact ); 209 210 LocalArtifactRequest request = new LocalArtifactRequest( artifact, Arrays.asList( repository ), testContext ); 211 LocalArtifactResult result = manager.find( session, request ); 212 assertTrue( result.isAvailable() ); 213 assertEquals( repository, result.getRepository() ); 214 215 addRemoteArtifact( snapshot ); 216 217 request = new LocalArtifactRequest( snapshot, Arrays.asList( repository ), testContext ); 218 result = manager.find( session, request ); 219 assertTrue( result.isAvailable() ); 220 assertEquals( repository, result.getRepository() ); 221 } 222 223 @Test 224 public void testDoNotFindDifferentContext() 225 throws Exception 226 { 227 addRemoteArtifact( artifact ); 228 229 LocalArtifactRequest request = new LocalArtifactRequest( artifact, Arrays.asList( repository ), "different" ); 230 LocalArtifactResult result = manager.find( session, request ); 231 assertFalse( result.isAvailable() ); 232 } 233 234 @Test 235 public void testDoNotFindNullFile() 236 throws Exception 237 { 238 artifact = artifact.setFile( null ); 239 addLocalArtifact( artifact ); 240 241 LocalArtifactRequest request = new LocalArtifactRequest( artifact, Arrays.asList( repository ), testContext ); 242 LocalArtifactResult result = manager.find( session, request ); 243 assertFalse( result.isAvailable() ); 244 } 245 246 @Test 247 public void testDoNotFindDeletedFile() 248 throws Exception 249 { 250 addLocalArtifact( artifact ); 251 assertTrue( "could not delete artifact file", artifactFile.delete() ); 252 253 LocalArtifactRequest request = new LocalArtifactRequest( artifact, Arrays.asList( repository ), testContext ); 254 LocalArtifactResult result = manager.find( session, request ); 255 assertFalse( result.isAvailable() ); 256 } 257 258 @Test 259 public void testFindUntrackedFile() 260 throws Exception 261 { 262 copy( artifact, manager.getPathForLocalArtifact( artifact ) ); 263 264 LocalArtifactRequest request = new LocalArtifactRequest( artifact, Arrays.asList( repository ), testContext ); 265 LocalArtifactResult result = manager.find( session, request ); 266 assertTrue( result.isAvailable() ); 267 } 268 269 private long addMetadata( Metadata metadata, RemoteRepository repo ) 270 throws IOException 271 { 272 String path; 273 if ( repo == null ) 274 { 275 path = manager.getPathForLocalMetadata( metadata ); 276 } 277 else 278 { 279 path = manager.getPathForRemoteMetadata( metadata, repo, testContext ); 280 } 281 System.err.println( path ); 282 283 return copy( metadata, path ); 284 } 285 286 @Test 287 public void testFindLocalMetadata() 288 throws Exception 289 { 290 addMetadata( metadata, null ); 291 292 LocalMetadataRequest request = new LocalMetadataRequest( metadata, null, testContext ); 293 LocalMetadataResult result = manager.find( session, request ); 294 295 assertNotNull( result.getFile() ); 296 } 297 298 @Test 299 public void testFindLocalMetadataNoVersion() 300 throws Exception 301 { 302 addMetadata( noVerMetadata, null ); 303 304 LocalMetadataRequest request = new LocalMetadataRequest( noVerMetadata, null, testContext ); 305 LocalMetadataResult result = manager.find( session, request ); 306 307 assertNotNull( result.getFile() ); 308 } 309 310 @Test 311 public void testDoNotFindRemoteMetadataDifferentContext() 312 throws Exception 313 { 314 addMetadata( noVerMetadata, repository ); 315 addMetadata( metadata, repository ); 316 317 LocalMetadataRequest request = new LocalMetadataRequest( noVerMetadata, repository, "different" ); 318 LocalMetadataResult result = manager.find( session, request ); 319 assertNull( result.getFile() ); 320 321 request = new LocalMetadataRequest( metadata, repository, "different" ); 322 result = manager.find( session, request ); 323 assertNull( result.getFile() ); 324 } 325 326 @Test 327 public void testFindArtifactUsesTimestampedVersion() 328 throws Exception 329 { 330 Artifact artifact = new DefaultArtifact( "g.i.d:a.i.d:1.0-SNAPSHOT" ); 331 File file = new File( basedir, manager.getPathForLocalArtifact( artifact ) ); 332 TestFileUtils.writeString( file, "test" ); 333 addLocalArtifact( artifact ); 334 335 artifact = artifact.setVersion( "1.0-20110329.221805-4" ); 336 LocalArtifactRequest request = new LocalArtifactRequest(); 337 request.setArtifact( artifact ); 338 LocalArtifactResult result = manager.find( session, request ); 339 assertNull( result.toString(), result.getFile() ); 340 assertFalse( result.toString(), result.isAvailable() ); 341 } 342 343}