Skip to content

Commit 6836d46

Browse files
liufengyunsom-snytt
andcommitted
Port ZipArchiveTest
- PR: scala/scala#9230 - issue: scala/bug#11754 Co-authored-by: Som Snytt <[email protected]>
1 parent a5f8e2b commit 6836d46

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package dotty.tools.io
2+
3+
import java.io.IOException
4+
import java.net.{URI, URL, URLClassLoader}
5+
import java.nio.file.{Files, Path, Paths}
6+
import java.util.jar.{Attributes, Manifest, JarEntry, JarOutputStream}
7+
import java.lang.invoke.{MethodHandles, MethodType}
8+
9+
import org.junit.Assert._
10+
import org.junit.Test
11+
12+
import scala.util.chaining._
13+
import scala.util.Using
14+
15+
class ZipArchiveTest {
16+
17+
@Test
18+
def corruptZip(): Unit = {
19+
val f = Files.createTempFile("test", ".jar")
20+
val fza = new FileZipArchive(f)
21+
try {
22+
fza.iterator
23+
}
24+
catch {
25+
case ex: IOException =>
26+
}
27+
finally {
28+
Files.delete(f)
29+
}
30+
}
31+
32+
@Test
33+
def missingFile(): Unit = {
34+
val f = Paths.get("xxx.does.not.exist")
35+
val fza = new FileZipArchive(f)
36+
try {
37+
fza.iterator
38+
}
39+
catch {
40+
case ex: IOException =>
41+
}
42+
}
43+
44+
private val bootClassLoader: ClassLoader = {
45+
if (!util.Properties.isJavaAtLeast("9")) null
46+
else {
47+
try {
48+
MethodHandles
49+
.lookup()
50+
.findStatic(
51+
classOf[ClassLoader],
52+
"getPlatformClassLoader",
53+
MethodType.methodType(classOf[ClassLoader])
54+
)
55+
.invoke()
56+
.asInstanceOf[ClassLoader]
57+
} catch {
58+
case _: Throwable =>
59+
null
60+
}
61+
}
62+
}
63+
64+
private def classLoader(location: URI): ClassLoader =
65+
new URLClassLoader(Array(location.toURL), bootClassLoader)
66+
67+
private def manifestAt(location: URI): URL = classLoader(location).getResource("META-INF/MANIFEST.MF")
68+
69+
70+
// ZipArchive.fromManifestURL(URL)
71+
@Test def `manifest resources just works`(): Unit = {
72+
val jar = createTestJar()
73+
val archive = new ManifestResources(manifestAt(jar.toUri))
74+
try {
75+
val it = archive.iterator
76+
assertTrue(it.hasNext)
77+
val f = it.next()
78+
assertFalse(it.hasNext)
79+
assertEquals("foo.class", f.name)
80+
}
81+
finally {
82+
Files.delete(jar)
83+
}
84+
}
85+
86+
private def createTestJar(): Path = Files.createTempFile("junit", ".jar").tap { f =>
87+
val man = new Manifest()
88+
man.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0")
89+
man.getEntries().put("foo.class", new Attributes(0))
90+
Using.resource(new JarOutputStream(Files.newOutputStream(f), man)) { jout =>
91+
jout.putNextEntry(new JarEntry("foo.class"))
92+
val bytes = "hello, world".getBytes
93+
jout.write(bytes, 0, bytes.length)
94+
()
95+
}
96+
}
97+
98+
private def createTestZip(): Path = Files.createTempFile("junit", ".zip").tap { f =>
99+
import java.util.zip._
100+
Using.resource(new ZipOutputStream(Files.newOutputStream(f))) { zout =>
101+
zout.setLevel(Deflater.NO_COMPRESSION)
102+
zout.setMethod(ZipOutputStream.STORED)
103+
val entry = new ZipEntry("foo.class")
104+
val bytes = "hello, world".getBytes
105+
entry.setSize(bytes.length)
106+
entry.setCompressedSize(bytes.length)
107+
entry.setCrc(new CRC32().tap(_.update(bytes, 0, bytes.length)).getValue)
108+
zout.putNextEntry(entry)
109+
zout.write(bytes, 0, bytes.length)
110+
zout.closeEntry()
111+
()
112+
}
113+
}
114+
/* zipfs doesn't write size field in file header as required by URLZipArchive
115+
private def createTestZip2(): Path = {
116+
import java.nio.file.FileSystems
117+
import java.net.URI
118+
import scala.util.chaining._
119+
import scala.jdk.CollectionConverters._
120+
val f = Files.createTempFile("junit", ".zip")
121+
Files.delete(f)
122+
val uri = URI.create(s"jar:${f.toUri}")
123+
val env = Map("create" -> "true").asJava
124+
Using.resource(FileSystems.newFileSystem(uri, env)) { fs =>
125+
val path = fs.getPath("foo.class")
126+
val bytes = "hello, world".getBytes
127+
Files.write(path, bytes)
128+
}
129+
f.tap(println(_))
130+
}
131+
*/
132+
}

0 commit comments

Comments
 (0)