/
MavenTest.java
94 lines (64 loc) · 2.94 KB
/
MavenTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.vonhof.webi.maven;
import org.junit.Test;
import org.sonatype.aether.artifact.Artifact;
import org.sonatype.aether.resolution.ArtifactResolutionException;
import org.sonatype.aether.util.artifact.DefaultArtifact;
import java.io.File;
import java.util.List;
import static org.junit.Assert.*;
public class MavenTest {
@Test
public void testResolveArtifact() throws Exception {
Maven mvn = new Maven();
final Artifact artifact = mvn.resolveArtifact("com.hazelcast:hazelcast:2.5",true);
assertTrue("Got a jar file that exists",artifact.getFile().exists());
}
@Test
public void testResolveDependencies() throws Exception {
Maven mvn = new Maven();
final List<Artifact> artifacts = mvn.resolveDependencies("org.springframework:spring-core:3.2.1.RELEASE");
assertTrue("Found more than 1 dependency (result includes itself)",artifacts.size() > 1);
}
@Test
public void testAddRepository() throws Exception {
Maven mvn = new Maven();
mvn.removeLocalArtifact("com.caucho", "resin-hessian", "3.2.1");
boolean gotException = false;
try {
mvn.resolveArtifact("com.caucho", "resin-hessian", "3.2.1",true);
} catch(ArtifactResolutionException ex) {
gotException = true;
}
assertTrue("Artifact could not be resolved from maven central",gotException);
//Add additional maven repo.
mvn.addRepository("caucho","http://caucho.com/m2");
final Artifact artifact = mvn.resolveArtifact("com.caucho", "resin-hessian", "3.2.1",true);
assertTrue("Got jar file that doesn't exist in Maven Central repo", artifact.getFile().exists());
}
@Test
public void testLocalRepository() throws Exception {
Maven mvn = new Maven();
mvn.addRepository("caucho","http://caucho.com/m2");
mvn.resolveArtifact("com.caucho", "resin-hessian", "3.2.1");
//Init new maven instance (without the extra repo)
mvn = new Maven();
Artifact artifact = mvn.resolveArtifact("com.caucho", "resin-hessian", "3.2.1");
assertTrue("Got jar file that doesn't exist in Maven Central repo from local repo",artifact.getFile().exists());
}
@Test
public void testAddArtifact() throws Exception {
Maven mvn = new Maven();
boolean gotException = false;
try {
mvn.resolveArtifact("com.example:nothing:6.6.6");
} catch(ArtifactResolutionException ex) {
gotException = true;
}
assertTrue("Artifact could not be resolved",gotException);
Artifact artifact = new DefaultArtifact("com.example:nothing:6.6.6")
.setFile(new File("com/example/nothing-6.6.6.jar"));
mvn.addArtifact(artifact);
Artifact resolvedArtifact = mvn.resolveArtifact("com.example:nothing:6.6.6");
assertNotNull("Artifact can now be resolved",resolvedArtifact);
}
}