|
| 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 | + // The following results in IOException on Windows (file in use by another process). |
| 86 | + // As jar created with Files.createTempFile, it will be deleted automatically. |
| 87 | + try Files.delete(jar) catch case _: IOException => () |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private def createTestJar(): Path = Files.createTempFile("junit", ".jar").tap { f => |
| 92 | + val man = new Manifest() |
| 93 | + man.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0") |
| 94 | + man.getEntries().put("foo.class", new Attributes(0)) |
| 95 | + Using.resource(new JarOutputStream(Files.newOutputStream(f), man)) { jout => |
| 96 | + jout.putNextEntry(new JarEntry("foo.class")) |
| 97 | + val bytes = "hello, world".getBytes |
| 98 | + jout.write(bytes, 0, bytes.length) |
| 99 | + () |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private def createTestZip(): Path = Files.createTempFile("junit", ".zip").tap { f => |
| 104 | + import java.util.zip._ |
| 105 | + Using.resource(new ZipOutputStream(Files.newOutputStream(f))) { zout => |
| 106 | + zout.setLevel(Deflater.NO_COMPRESSION) |
| 107 | + zout.setMethod(ZipOutputStream.STORED) |
| 108 | + val entry = new ZipEntry("foo.class") |
| 109 | + val bytes = "hello, world".getBytes |
| 110 | + entry.setSize(bytes.length) |
| 111 | + entry.setCompressedSize(bytes.length) |
| 112 | + entry.setCrc(new CRC32().tap(_.update(bytes, 0, bytes.length)).getValue) |
| 113 | + zout.putNextEntry(entry) |
| 114 | + zout.write(bytes, 0, bytes.length) |
| 115 | + zout.closeEntry() |
| 116 | + () |
| 117 | + } |
| 118 | + } |
| 119 | + /* zipfs doesn't write size field in file header as required by URLZipArchive |
| 120 | + private def createTestZip2(): Path = { |
| 121 | + import java.nio.file.FileSystems |
| 122 | + import java.net.URI |
| 123 | + import scala.util.chaining._ |
| 124 | + import scala.jdk.CollectionConverters._ |
| 125 | + val f = Files.createTempFile("junit", ".zip") |
| 126 | + Files.delete(f) |
| 127 | + val uri = URI.create(s"jar:${f.toUri}") |
| 128 | + val env = Map("create" -> "true").asJava |
| 129 | + Using.resource(FileSystems.newFileSystem(uri, env)) { fs => |
| 130 | + val path = fs.getPath("foo.class") |
| 131 | + val bytes = "hello, world".getBytes |
| 132 | + Files.write(path, bytes) |
| 133 | + } |
| 134 | + f.tap(println(_)) |
| 135 | + } |
| 136 | + */ |
| 137 | +} |
0 commit comments