DefaultArtifactResolver implements ArtifactResolver, Service { public ArtifactResult resolveArtifact( RepositorySystemSession session, ArtifactRequest request ) throws ArtifactResolutionException { return resolveArtifacts( session, Collections.singleton( request ) ).get( 0 ); } DefaultArtifactResolver();  DefaultArtifactResolver( Logger logger, FileProcessor fileProcessor,
                                    RepositoryEventDispatcher repositoryEventDispatcher,
                                    VersionResolver versionResolver, UpdateCheckManager updateCheckManager,
                                    RemoteRepositoryManager remoteRepositoryManager,
                                    SyncContextFactory syncContextFactory ); void initService( ServiceLocator locator ); DefaultArtifactResolver setLogger( Logger logger ); DefaultArtifactResolver setFileProcessor( FileProcessor fileProcessor ); DefaultArtifactResolver setRepositoryEventDispatcher( RepositoryEventDispatcher repositoryEventDispatcher ); DefaultArtifactResolver setVersionResolver( VersionResolver versionResolver ); DefaultArtifactResolver setUpdateCheckManager( UpdateCheckManager updateCheckManager ); DefaultArtifactResolver setRemoteRepositoryManager( RemoteRepositoryManager remoteRepositoryManager ); DefaultArtifactResolver setSyncContextFactory( SyncContextFactory syncContextFactory ); ArtifactResult resolveArtifact( RepositorySystemSession session, ArtifactRequest request ); List<ArtifactResult> resolveArtifacts( RepositorySystemSession session,
                                                  Collection<? extends ArtifactRequest> requests );  }
@Test public void testResolveLocalArtifactUnsuccessful() throws IOException, ArtifactResolutionException { File tmpFile = TestFileUtils.createTempFile( "tmp" ); Map<String, String> properties = new HashMap<String, String>(); properties.put( ArtifactProperties.LOCAL_PATH, tmpFile.getAbsolutePath() ); artifact = artifact.setProperties( properties ); tmpFile.delete(); ArtifactRequest request = new ArtifactRequest( artifact, null, "" ); try { resolver.resolveArtifact( session, request ); fail( "expected exception" ); } catch ( ArtifactResolutionException e ) { assertNotNull( e.getResults() ); assertEquals( 1, e.getResults().size() ); ArtifactResult result = e.getResults().get( 0 ); assertSame( request, result.getRequest() ); assertFalse( result.getExceptions().isEmpty() ); assertTrue( result.getExceptions().get( 0 ) instanceof ArtifactNotFoundException ); Artifact resolved = result.getArtifact(); assertNull( resolved ); } }
@Test public void testResolveRemoteArtifactUnsuccessful() throws IOException, ArtifactResolutionException { RecordingRepositoryConnector connector = new RecordingRepositoryConnector() { @Override public void get( Collection<? extends ArtifactDownload> artifactDownloads, Collection<? extends MetadataDownload> metadataDownloads ) { super.get( artifactDownloads, metadataDownloads ); ArtifactDownload download = artifactDownloads.iterator().next(); ArtifactTransferException exception = new ArtifactNotFoundException( download.getArtifact(), null, "not found" ); download.setException( exception ); } }; connector.setExpectGet( artifact ); remoteRepositoryManager.setConnector( connector ); ArtifactRequest request = new ArtifactRequest( artifact, null, "" ); request.addRepository( new RemoteRepository( "id", "default", "file: try { resolver.resolveArtifact( session, request ); fail( "expected exception" ); } catch ( ArtifactResolutionException e ) { connector.assertSeenExpected(); assertNotNull( e.getResults() ); assertEquals( 1, e.getResults().size() ); ArtifactResult result = e.getResults().get( 0 ); assertSame( request, result.getRequest() ); assertFalse( result.getExceptions().isEmpty() ); assertTrue( result.getExceptions().get( 0 ) instanceof ArtifactNotFoundException ); Artifact resolved = result.getArtifact(); assertNull( resolved ); } }
@Test public void testRepositoryEventsSuccessfulLocal() throws ArtifactResolutionException, IOException { RecordingRepositoryListener listener = new RecordingRepositoryListener(); session.setRepositoryListener( listener ); File tmpFile = TestFileUtils.createTempFile( "tmp" ); Map<String, String> properties = new HashMap<String, String>(); properties.put( ArtifactProperties.LOCAL_PATH, tmpFile.getAbsolutePath() ); artifact = artifact.setProperties( properties ); ArtifactRequest request = new ArtifactRequest( artifact, null, "" ); resolver.resolveArtifact( session, request ); List<EventWrapper> events = listener.getEvents(); assertEquals( 2, events.size() ); EventWrapper event = events.get( 0 ); assertEquals( RecordingRepositoryListener.Type.ARTIFACT_RESOLVING, event.getType() ); assertNull( event.getEvent().getException() ); assertEquals( artifact, event.getEvent().getArtifact() ); event = events.get( 1 ); assertEquals( RecordingRepositoryListener.Type.ARTIFACT_RESOLVED, event.getType() ); assertNull( event.getEvent().getException() ); assertEquals( artifact, event.getEvent().getArtifact().setFile( null ) ); }
@Test public void testRepositoryEventsUnsuccessfulLocal() throws IOException { RecordingRepositoryListener listener = new RecordingRepositoryListener(); session.setRepositoryListener( listener ); Map<String, String> properties = new HashMap<String, String>(); properties.put( ArtifactProperties.LOCAL_PATH, "doesnotexist" ); artifact = artifact.setProperties( properties ); ArtifactRequest request = new ArtifactRequest( artifact, null, "" ); try { resolver.resolveArtifact( session, request ); fail( "expected exception" ); } catch ( ArtifactResolutionException e ) { } List<EventWrapper> events = listener.getEvents(); assertEquals( 2, events.size() ); EventWrapper event = events.get( 0 ); assertEquals( artifact, event.getEvent().getArtifact() ); assertEquals( Type.ARTIFACT_RESOLVING, event.getType() ); event = events.get( 1 ); assertEquals( artifact, event.getEvent().getArtifact() ); assertEquals( Type.ARTIFACT_RESOLVED, event.getType() ); assertNotNull( event.getEvent().getException() ); assertEquals( 1, event.getEvent().getExceptions().size() ); }
@Test public void testRepositoryEventsSuccessfulRemote() throws ArtifactResolutionException { RecordingRepositoryListener listener = new RecordingRepositoryListener(); session.setRepositoryListener( listener ); ArtifactRequest request = new ArtifactRequest( artifact, null, "" ); request.addRepository( new RemoteRepository( "id", "default", "file: resolver.resolveArtifact( session, request ); List<EventWrapper> events = listener.getEvents(); assertEquals( 2, events.size() ); EventWrapper event = events.get( 0 ); assertEquals( RecordingRepositoryListener.Type.ARTIFACT_RESOLVING, event.getType() ); assertNull( event.getEvent().getException() ); assertEquals( artifact, event.getEvent().getArtifact() ); event = events.get( 1 ); assertEquals( RecordingRepositoryListener.Type.ARTIFACT_RESOLVED, event.getType() ); assertNull( event.getEvent().getException() ); assertEquals( artifact, event.getEvent().getArtifact().setFile( null ) ); }
@Test public void testRepositoryEventsUnsuccessfulRemote() throws IOException, ArtifactResolutionException { RecordingRepositoryConnector connector = new RecordingRepositoryConnector() { @Override public void get( Collection<? extends ArtifactDownload> artifactDownloads, Collection<? extends MetadataDownload> metadataDownloads ) { super.get( artifactDownloads, metadataDownloads ); ArtifactDownload download = artifactDownloads.iterator().next(); ArtifactTransferException exception = new ArtifactNotFoundException( download.getArtifact(), null, "not found" ); download.setException( exception ); } }; remoteRepositoryManager.setConnector( connector ); RecordingRepositoryListener listener = new RecordingRepositoryListener(); session.setRepositoryListener( listener ); ArtifactRequest request = new ArtifactRequest( artifact, null, "" ); request.addRepository( new RemoteRepository( "id", "default", "file: try { resolver.resolveArtifact( session, request ); fail( "expected exception" ); } catch ( ArtifactResolutionException e ) { } List<EventWrapper> events = listener.getEvents(); assertEquals( 2, events.size() ); EventWrapper event = events.get( 0 ); assertEquals( artifact, event.getEvent().getArtifact() ); assertEquals( Type.ARTIFACT_RESOLVING, event.getType() ); event = events.get( 1 ); assertEquals( artifact, event.getEvent().getArtifact() ); assertEquals( Type.ARTIFACT_RESOLVED, event.getType() ); assertNotNull( event.getEvent().getException() ); assertEquals( 1, event.getEvent().getExceptions().size() ); }