Debian Codegraph @

src/test/java/MavenCapsuleTest.java

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