Skip to content

Commit 4cf79dc

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 4cf79dc

File tree

1 file changed

+135
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)