Skip to content

Commit 3680f95

Browse files
committed
Add JAR creation and archive repackaging tests
1 parent 986451b commit 3680f95

File tree

8 files changed

+155
-1
lines changed

8 files changed

+155
-1
lines changed

saker.build

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ test(){
167167
)
168168

169169
$resolveddependencies = nest.dependency.resolve(
170-
Bundles: [saker.zip, saker.java.compiler],
170+
Bundles: [saker.zip, saker.java.compiler, saker.jar],
171171
DependencyFile: resources/META-INF/nest/dependencies,
172172
Filters: nest.dependency.filter.kind(classpath)
173173
)

src/sipka/jvm/tailrec/MainCommand.java

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
* <pre>
3030
* Tail recursion optimizer for Java bytecode.
3131
*
32+
* https://github.com/Sipkab/jvm-tail-recursion
33+
*
3234
* The program takes Java class files as its input and optimizes tail recursive calls
3335
* in a way that it won't overflow the stack.
3436
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
build(
2+
out path,
3+
out secondpath,
4+
) {
5+
$javac = saker.java.compile(src)
6+
7+
$jar = saker.jar.create(
8+
Output: first.jar,
9+
Resources: {
10+
Directory: $javac[ClassDirectory],
11+
Resources: **,
12+
},
13+
Transformers: sipka.jvm.tailrec.zip.transformer(),
14+
)
15+
16+
$secondjar = saker.jar.create(
17+
Output: second.jar,
18+
Include: $jar[Path],
19+
Transformers: sipka.jvm.tailrec.zip.transformer(),
20+
)
21+
22+
$path = $jar[Path]
23+
$secondpath = $secondjar[Path]
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package test;
2+
3+
public class Main {
4+
public static void count(int n) {
5+
if (n == 0) {
6+
return;
7+
}
8+
count(n - 1);
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
build(
2+
out path
3+
) {
4+
$javac = saker.java.compile(src)
5+
6+
$jar = saker.jar.create(
7+
Resources: {
8+
Directory: $javac[ClassDirectory],
9+
Resources: **,
10+
},
11+
Transformers: sipka.jvm.tailrec.zip.transformer(),
12+
)
13+
14+
$path = $jar[Path]
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package test;
2+
3+
public class Main {
4+
public static void count(int n) {
5+
if (n == 0) {
6+
return;
7+
}
8+
count(n - 1);
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package testing.saker.sipka.jvm.tailrec;
2+
3+
import java.io.InputStream;
4+
import java.util.Map;
5+
import java.util.TreeMap;
6+
import java.util.zip.ZipEntry;
7+
import java.util.zip.ZipInputStream;
8+
9+
import saker.build.file.path.SakerPath;
10+
import saker.build.thirdparty.saker.util.classloader.ClassLoaderDataFinder;
11+
import saker.build.thirdparty.saker.util.classloader.MultiDataClassLoader;
12+
import saker.build.thirdparty.saker.util.io.ByteArrayRegion;
13+
import saker.build.thirdparty.saker.util.io.ByteSource;
14+
import saker.build.thirdparty.saker.util.io.StreamUtils;
15+
import testing.saker.SakerTest;
16+
import testing.saker.build.tests.TestUtils;
17+
import testing.saker.nest.util.NestRepositoryCachingEnvironmentTestCase;
18+
19+
@SakerTest
20+
public class RepackageArchiveOptimizeSakerTest extends NestRepositoryCachingEnvironmentTestCase{
21+
22+
@Override
23+
protected void runNestTaskTestImpl() throws Throwable {
24+
// TODO Auto-generated method stub
25+
CombinedTargetTaskResult res = runScriptTask("build");
26+
SakerPath outpath = (SakerPath) res.getTargetTaskResult("secondpath");
27+
28+
Map<String, ByteArrayRegion> resourceBytes = new TreeMap<>();
29+
try (InputStream is = ByteSource.toInputStream(files.openInput(outpath));
30+
ZipInputStream zis = new ZipInputStream(is)) {
31+
for (ZipEntry entry; (entry = zis.getNextEntry()) != null;) {
32+
resourceBytes.put(entry.getName(), StreamUtils.readStreamFully(zis));
33+
}
34+
}
35+
36+
try (ClassLoaderDataFinder jarfinder = new TestUtils.MemoryClassLoaderDataFinder(resourceBytes)) {
37+
MultiDataClassLoader cl = new MultiDataClassLoader(jarfinder);
38+
Class<?> c = Class.forName("test.Main", false, cl);
39+
//this should succeed, as it was optimized
40+
c.getMethod("count", int.class).invoke(null, 10000000);
41+
}
42+
43+
runScriptTask("build");
44+
assertEmpty(getMetric().getRunTaskIdResults());
45+
}
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package testing.saker.sipka.jvm.tailrec;
2+
3+
import java.io.InputStream;
4+
import java.util.Map;
5+
import java.util.TreeMap;
6+
import java.util.zip.ZipEntry;
7+
import java.util.zip.ZipInputStream;
8+
9+
import saker.build.file.path.SakerPath;
10+
import saker.build.thirdparty.saker.util.classloader.ClassLoaderDataFinder;
11+
import saker.build.thirdparty.saker.util.classloader.MultiDataClassLoader;
12+
import saker.build.thirdparty.saker.util.io.ByteArrayRegion;
13+
import saker.build.thirdparty.saker.util.io.ByteSource;
14+
import saker.build.thirdparty.saker.util.io.StreamUtils;
15+
import testing.saker.SakerTest;
16+
import testing.saker.build.tests.TestUtils;
17+
import testing.saker.nest.util.NestRepositoryCachingEnvironmentTestCase;
18+
19+
@SakerTest
20+
public class SimpleJarOptimizeSakerTest extends NestRepositoryCachingEnvironmentTestCase {
21+
22+
@Override
23+
protected void runNestTaskTestImpl() throws Throwable {
24+
CombinedTargetTaskResult res = runScriptTask("build");
25+
SakerPath outpath = (SakerPath) res.getTargetTaskResult("path");
26+
27+
Map<String, ByteArrayRegion> resourceBytes = new TreeMap<>();
28+
try (InputStream is = ByteSource.toInputStream(files.openInput(outpath));
29+
ZipInputStream zis = new ZipInputStream(is)) {
30+
for (ZipEntry entry; (entry = zis.getNextEntry()) != null;) {
31+
resourceBytes.put(entry.getName(), StreamUtils.readStreamFully(zis));
32+
}
33+
}
34+
35+
try (ClassLoaderDataFinder jarfinder = new TestUtils.MemoryClassLoaderDataFinder(resourceBytes)) {
36+
MultiDataClassLoader cl = new MultiDataClassLoader(jarfinder);
37+
Class<?> c = Class.forName("test.Main", false, cl);
38+
//this should succeed, as it was optimized
39+
c.getMethod("count", int.class).invoke(null, 10000000);
40+
}
41+
42+
runScriptTask("build");
43+
assertEmpty(getMetric().getRunTaskIdResults());
44+
}
45+
46+
}

0 commit comments

Comments
 (0)