| 1 |
/* |
| 2 |
* Capsule |
| 3 |
* Copyright (c) 2014-2016, Parallel Universe Software Co. All rights reserved. |
| 4 |
* |
| 5 |
* This program and the accompanying materials are licensed under the terms |
| 6 |
* of the Eclipse Public License v1.0, available at |
| 7 |
* http://www.eclipse.org/legal/epl-v10.html |
| 8 |
*/ |
| 9 |
|
| 10 |
import capsule.DependencyManager; |
| 11 |
import co.paralleluniverse.capsule.Jar; |
| 12 |
import co.paralleluniverse.capsule.test.CapsuleTestUtils; |
| 13 |
import static co.paralleluniverse.capsule.test.CapsuleTestUtils.*; |
| 14 |
import com.google.common.jimfs.Jimfs; |
| 15 |
import java.io.ByteArrayInputStream; |
| 16 |
import java.io.ByteArrayOutputStream; |
| 17 |
import java.io.IOException; |
| 18 |
import java.io.InputStream; |
| 19 |
import static java.nio.charset.StandardCharsets.UTF_8; |
| 20 |
import java.nio.file.FileSystem; |
| 21 |
import java.nio.file.Path; |
| 22 |
import java.util.ArrayList; |
| 23 |
import java.util.Arrays; |
| 24 |
import java.util.Collection; |
| 25 |
import java.util.Collections; |
| 26 |
import java.util.List; |
| 27 |
import java.util.regex.Matcher; |
| 28 |
import java.util.regex.Pattern; |
| 29 |
import org.apache.maven.model.Dependency; |
| 30 |
import org.apache.maven.model.Exclusion; |
| 31 |
import org.apache.maven.model.Model; |
| 32 |
import org.apache.maven.model.io.xpp3.MavenXpp3Writer; |
| 33 |
import org.junit.Test; |
| 34 |
import static org.junit.Assert.*; |
| 35 |
import static com.google.common.truth.Truth.*; |
| 36 |
import java.util.HashMap; |
| 37 |
import java.util.Map; |
| 38 |
import java.util.Properties; |
| 39 |
import org.junit.Before; |
| 40 |
import static org.mockito.Mockito.*; |
| 41 |
import org.mockito.invocation.InvocationOnMock; |
| 42 |
import org.mockito.stubbing.Answer; |
| 43 |
|
| 44 |
/** |
| 45 |
* |
| 46 |
* @author pron |
| 47 |
*/ |
| 48 |
public class MavenCapsuleTest { |
| 49 |
private final FileSystem fs = Jimfs.newFileSystem(); |
| 50 |
private final Path cache = fs.getPath("/cache"); |
| 51 |
private final Path tmp = fs.getPath("/tmp"); |
| 52 |
private Map<String, List<Path>> deps; |
| 53 |
private Properties props; |
| 54 |
|
| 55 |
@Before |
| 56 |
public void setUp() throws Exception { |
| 57 |
deps = null; |
| 58 |
props = new Properties(System.getProperties()); |
| 59 |
setProperties(props); |
| 60 |
setCacheDir(cache); |
| 61 |
} |
| 62 |
|
| 63 |
@Test |
| 64 |
public void whenNoNameAndPomTakeIdFromPom() throws Exception { |
| 65 |
Model pom = newPom(); |
| 66 |
pom.setGroupId("com.acme"); |
| 67 |
pom.setArtifactId("foo"); |
| 68 |
pom.setVersion("1.0"); |
| 69 |
|
| 70 |
Jar jar = newCapsuleJar() |
| 71 |
.setAttribute("Application-Class", "com.acme.Foo") |
| 72 |
.setAttribute("Extract-Capsule", "false") |
| 73 |
.addEntry("foo.jar", emptyInputStream()) |
| 74 |
.addEntry("lib/a.jar", emptyInputStream()) |
| 75 |
.addEntry("pom.xml", toInputStream(pom)); |
| 76 |
|
| 77 |
Capsule capsule = newCapsule(jar); |
| 78 |
|
| 79 |
assertEquals("com.acme.foo_1.0", capsule.getAppId()); |
| 80 |
} |
| 81 |
|
| 82 |
@Test |
| 83 |
public void testPomDependencies() throws Exception { |
| 84 |
List<String> ds = list("com.acme:bar:1.2", "com.acme:baz:3.4:jdk8(org.asd:qqq,com.gogo:bad)"); |
| 85 |
|
| 86 |
Model pom = newPom(); |
| 87 |
pom.setGroupId("com.acme"); |
| 88 |
pom.setArtifactId("foo"); |
| 89 |
pom.setVersion("1.0"); |
| 90 |
for (String d : ds) |
| 91 |
pom.addDependency(coordsToDependency(d)); |
| 92 |
|
| 93 |
Jar jar = newCapsuleJar() |
| 94 |
.setAttribute("Application-Class", "com.acme.Foo") |
| 95 |
.addEntry("foo.jar", emptyInputStream()) |
| 96 |
.addEntry("pom.xml", toInputStream(pom)); |
| 97 |
|
| 98 |
Capsule capsule = newCapsule(jar); |
| 99 |
for (String d : ds) |
| 100 |
assert_().that(capsule.getAttribute(Capsule.ATTR_DEPENDENCIES)).contains(DependencyManager.toDependency(d, "jar")); |
| 101 |
} |
| 102 |
|
| 103 |
//<editor-fold defaultstate="collapsed" desc="POM Utilities"> |
| 104 |
/////////// POM Utilities /////////////////////////////////// |
| 105 |
private Model newPom() { |
| 106 |
return new Model(); |
| 107 |
} |
| 108 |
|
| 109 |
private InputStream toInputStream(Model model) { |
| 110 |
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
| 111 |
new MavenXpp3Writer().write(baos, model); |
| 112 |
return new ByteArrayInputStream(baos.toByteArray()); |
| 113 |
} catch (IOException e) { |
| 114 |
throw new AssertionError(e); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
private static final Pattern PAT_DEPENDENCY = Pattern.compile("(?<groupId>[^:\\(\\)]+):(?<artifactId>[^:\\(\\)]+)(:(?<version>[^:\\(\\)]*))?(:(?<classifier>[^:\\(\\)]+))?(\\((?<exclusions>[^\\(\\)]*)\\))?"); |
| 119 |
|
| 120 |
static Dependency coordsToDependency(String depString) { |
| 121 |
final Matcher m = PAT_DEPENDENCY.matcher(depString); |
| 122 |
if (!m.matches()) |
| 123 |
throw new IllegalArgumentException("Could not parse dependency: " + depString); |
| 124 |
|
| 125 |
final Dependency d = new Dependency(); |
| 126 |
d.setGroupId(m.group("groupId")); |
| 127 |
d.setArtifactId(m.group("artifactId")); |
| 128 |
String version = m.group("version"); |
| 129 |
if (version == null || version.isEmpty()) |
| 130 |
version = "[0,)"; |
| 131 |
d.setVersion(version); |
| 132 |
d.setClassifier(m.group("classifier")); |
| 133 |
d.setScope("runtime"); |
| 134 |
for (Exclusion ex : getExclusions(depString)) |
| 135 |
d.addExclusion(ex); |
| 136 |
return d; |
| 137 |
} |
| 138 |
|
| 139 |
static Collection<Exclusion> getExclusions(String depString) { |
| 140 |
final Matcher m = PAT_DEPENDENCY.matcher(depString); |
| 141 |
if (!m.matches()) |
| 142 |
throw new IllegalArgumentException("Could not parse dependency: " + depString); |
| 143 |
|
| 144 |
if (m.group("exclusions") == null || m.group("exclusions").isEmpty()) |
| 145 |
return Collections.emptyList(); |
| 146 |
|
| 147 |
final List<String> exclusionPatterns = Arrays.asList(m.group("exclusions").split(",")); |
| 148 |
final List<Exclusion> exclusions = new ArrayList<Exclusion>(); |
| 149 |
for (String expat : exclusionPatterns) { |
| 150 |
String[] coords = expat.trim().split(":"); |
| 151 |
if (coords.length != 2) |
| 152 |
throw new IllegalArgumentException("Illegal exclusion dependency coordinates: " + depString + " (in exclusion " + expat + ")"); |
| 153 |
Exclusion ex = new Exclusion(); |
| 154 |
ex.setGroupId(coords[0]); |
| 155 |
ex.setArtifactId(coords[1]); |
| 156 |
exclusions.add(ex); |
| 157 |
} |
| 158 |
return exclusions; |
| 159 |
} |
| 160 |
//</editor-fold> |
| 161 |
|
| 162 |
//<editor-fold defaultstate="collapsed" desc="Dependency Manager Utilities"> |
| 163 |
/////////// Dependency Manager Utilities /////////////////////////////////// |
| 164 |
private Path mockDep(DependencyManager dm, String dep, String type) { |
| 165 |
return mockDep(dm, dep, type, dep).get(0); |
| 166 |
} |
| 167 |
|
| 168 |
private List<Path> mockDep(DependencyManager dm, String dep, String type, String... artifacts) { |
| 169 |
List<Path> as = new ArrayList<>(artifacts.length); |
| 170 |
for (String a : artifacts) |
| 171 |
as.add(artifact(a, type)); |
| 172 |
|
| 173 |
if (deps == null) |
| 174 |
this.deps = new HashMap<>(); |
| 175 |
deps.put(dep, as); |
| 176 |
|
| 177 |
when(dm.resolveDependency(dep, type)).thenReturn(as); |
| 178 |
when(dm.resolveDependencies(anyList(), eq(type))).thenAnswer(new Answer<List<Path>>() { |
| 179 |
@Override |
| 180 |
public List<Path> answer(InvocationOnMock invocation) throws Throwable { |
| 181 |
List<String> coords = (List<String>) invocation.getArguments()[0]; |
| 182 |
List<Path> res = new ArrayList<>(); |
| 183 |
for (String dep : coords) |
| 184 |
res.addAll(deps.get(dep)); |
| 185 |
|
| 186 |
return res; |
| 187 |
} |
| 188 |
}); |
| 189 |
|
| 190 |
return as; |
| 191 |
} |
| 192 |
|
| 193 |
private Path artifact(String x, String type) { |
| 194 |
String[] coords = x.split(":"); |
| 195 |
String group = coords[0]; |
| 196 |
String artifact = coords[1]; |
| 197 |
String artifactDir = artifact.split("-")[0]; // arbitrary |
| 198 |
String version = coords[2] + (coords.length > 3 ? "-" + coords[3] : ""); |
| 199 |
return cache.resolve("deps").resolve(group).resolve(artifactDir).resolve(artifact + "-" + version + '.' + type); |
| 200 |
} |
| 201 |
//</editor-fold> |
| 202 |
|
| 203 |
//<editor-fold defaultstate="collapsed" desc="Utilities"> |
| 204 |
/////////// Utilities /////////////////////////////////// |
| 205 |
// may be called once per test (always writes jar into /capsule.jar) |
| 206 |
private Capsule newCapsule(Jar jar) { |
| 207 |
return (Capsule) CapsuleTestUtils.newCapsule(jar, path("capsule.jar")); |
| 208 |
} |
| 209 |
|
| 210 |
private Jar newCapsuleJar() { |
| 211 |
return new Jar() |
| 212 |
.setAttribute("Manifest-Version", "1.0") |
| 213 |
.setAttribute("Premain-Class", "Capsule") |
| 214 |
.setAttribute("Main-Class", "Capsule") |
| 215 |
.setListAttribute("Caplets", list("MavenCapsule")); |
| 216 |
} |
| 217 |
|
| 218 |
private Path path(String first, String... more) { |
| 219 |
return fs.getPath(first, more); |
| 220 |
} |
| 221 |
|
| 222 |
@SafeVarargs |
| 223 |
private static <T> List<T> list(T... xs) { |
| 224 |
return Arrays.asList(xs); |
| 225 |
} |
| 226 |
|
| 227 |
private InputStream emptyInputStream() { |
| 228 |
return Jar.toInputStream("", UTF_8); |
| 229 |
} |
| 230 |
//</editor-fold> |
| 231 |
} |