[ZEPPELIN-1376] Add unit test and fix impl for DependencyResolver to catch NPE

This commit is contained in:
DuyHai DOAN 2016-08-28 14:42:41 +02:00 committed by doanduyhai
parent 1913a0ac5c
commit b2641932dc
2 changed files with 23 additions and 7 deletions

View file

@ -33,6 +33,7 @@ import org.slf4j.LoggerFactory;
import org.sonatype.aether.RepositoryException;
import org.sonatype.aether.artifact.Artifact;
import org.sonatype.aether.collection.CollectRequest;
import org.sonatype.aether.collection.DependencyCollectionException;
import org.sonatype.aether.graph.Dependency;
import org.sonatype.aether.graph.DependencyFilter;
import org.sonatype.aether.repository.RemoteRepository;
@ -158,15 +159,14 @@ public class DependencyResolver extends AbstractDependencyResolver {
*/
@Override
public List<ArtifactResult> getArtifactsWithDep(String dependency,
Collection<String> excludes) throws RepositoryException {
Collection<String> excludes) throws RepositoryException {
Artifact artifact = new DefaultArtifact(dependency);
DependencyFilter classpathFilter = DependencyFilterUtils.classpathFilter(JavaScopes.COMPILE);
PatternExclusionsDependencyFilter exclusionFilter =
new PatternExclusionsDependencyFilter(excludes);
new PatternExclusionsDependencyFilter(excludes);
CollectRequest collectRequest = new CollectRequest();
final Dependency rootDependency = new Dependency(artifact, JavaScopes.COMPILE);
collectRequest.setRoot(rootDependency);
collectRequest.setRoot(new Dependency(artifact, JavaScopes.COMPILE));
synchronized (repos) {
for (RemoteRepository repo : repos) {
@ -174,8 +174,11 @@ public class DependencyResolver extends AbstractDependencyResolver {
}
}
DependencyRequest dependencyRequest = new DependencyRequest(collectRequest,
DependencyFilterUtils.andFilter(exclusionFilter, classpathFilter));
dependencyRequest.setRoot(new DefaultDependencyNode(rootDependency));
return system.resolveDependencies(session, dependencyRequest).getArtifactResults();
DependencyFilterUtils.andFilter(exclusionFilter, classpathFilter));
try {
return system.resolveDependencies(session, dependencyRequest).getArtifactResults();
} catch (NullPointerException ex) {
throw new RepositoryException(String.format("Cannot fetch dependencies for %s", dependency));
}
}
}

View file

@ -20,6 +20,7 @@ package org.apache.zeppelin.dep;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Collections;
import org.apache.commons.io.FileUtils;
@ -36,6 +37,9 @@ public class DependencyResolverTest {
private static File testCopyPath;
private static File tmpDir;
@Rule
public ExpectedException expectedException = ExpectedException.none();
@BeforeClass
public static void setUp() throws Exception {
tmpDir = new File(System.getProperty("java.io.tmpdir")+"/ZeppelinLTest_"+System.currentTimeMillis());
@ -90,4 +94,13 @@ public class DependencyResolverTest {
exception.expect(RepositoryException.class);
resolver.load("com.agimatec:agimatec-validation:0.9.3", testCopyPath);
}
@Test
public void should_throw_exception_if_dependency_not_found() throws Exception {
expectedException.expectMessage("Source 'one.two:1.0' does not exist");
expectedException.expect(FileNotFoundException.class);
resolver.load("one.two:1.0", testCopyPath);
}
}