Skip to content

Commit e141b2b

Browse files
committed
Add a unit test for makeJar()
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 01880be commit e141b2b

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* #%L
3+
* JSR-223-compliant Java scripting language plugin.
4+
* %%
5+
* Copyright (C) 2008 - 2014 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.plugins.scripting.java;
33+
34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertTrue;
36+
37+
import java.io.File;
38+
import java.io.IOException;
39+
import java.io.StringWriter;
40+
import java.util.Arrays;
41+
import java.util.Set;
42+
import java.util.TreeSet;
43+
import java.util.jar.JarEntry;
44+
import java.util.jar.JarFile;
45+
46+
import org.junit.Test;
47+
import org.scijava.util.FileUtils;
48+
import org.scijava.util.IteratorPlus;
49+
50+
public class MakeJarTest {
51+
@Test
52+
public void testSingle() throws Exception {
53+
final StringWriter writer = new StringWriter();
54+
final JavaEngine engine = new JavaEngine();
55+
final File file = FileUtils.urlToFile(getClass().getResource("/Dummy.java"));
56+
final File output = File.createTempFile("jar-test-", ".jar");
57+
engine.makeJar(file, false, output, writer);
58+
assertJarEntries(output, "META-INF/MANIFEST.MF",
59+
"META-INF/maven/net.imagej/Dummy/pom.xml", "Dummy.class");
60+
}
61+
62+
private void assertJarEntries(File output, String... paths) throws IOException {
63+
final Set<String> set = new TreeSet<String>(Arrays.asList(paths));
64+
final JarFile jar = new JarFile(output);
65+
for (final JarEntry entry : new IteratorPlus<JarEntry>(jar.entries())) {
66+
final String path = entry.getName();
67+
assertTrue("Unexpected: " + path, set.remove(path));
68+
}
69+
jar.close();
70+
assertEquals("Missing: " + set, 0, set.size());
71+
}
72+
}

src/test/resources/Dummy.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* #%L
3+
* JSR-223-compliant Java scripting language plugin.
4+
* %%
5+
* Copyright (C) 2008 - 2014 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
/**
33+
* Dummy class for testing {@code .jar} file packaging.
34+
*
35+
* @author Johannes Schindelin
36+
*/
37+
public class Dummy {
38+
public static void main(final String... args) {
39+
throw new RuntimeException("Civilized people do not execute!");
40+
}
41+
}

0 commit comments

Comments
 (0)