-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Port ZipArchiveTest #10099
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Port ZipArchiveTest #10099
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package dotty.tools.io | ||
|
||
import java.io.IOException | ||
import java.net.{URI, URL, URLClassLoader} | ||
import java.nio.file.{Files, Path, Paths} | ||
import java.util.jar.{Attributes, Manifest, JarEntry, JarOutputStream} | ||
import java.lang.invoke.{MethodHandles, MethodType} | ||
|
||
import org.junit.Assert._ | ||
import org.junit.Test | ||
|
||
import scala.util.chaining._ | ||
import scala.util.Using | ||
|
||
class ZipArchiveTest { | ||
|
||
@Test | ||
def corruptZip(): Unit = { | ||
val f = Files.createTempFile("test", ".jar") | ||
val fza = new FileZipArchive(f) | ||
try { | ||
fza.iterator | ||
assert(false) | ||
} | ||
catch { | ||
case ex: IOException => | ||
} | ||
finally { | ||
Files.delete(f) | ||
} | ||
} | ||
|
||
@Test | ||
def missingFile(): Unit = { | ||
val f = Paths.get("xxx.does.not.exist") | ||
val fza = new FileZipArchive(f) | ||
try { | ||
fza.iterator | ||
assert(false) | ||
} | ||
catch { | ||
case ex: IOException => | ||
} | ||
} | ||
|
||
private val bootClassLoader: ClassLoader = { | ||
if (!util.Properties.isJavaAtLeast("9")) null | ||
else { | ||
try { | ||
MethodHandles | ||
.lookup() | ||
.findStatic( | ||
classOf[ClassLoader], | ||
"getPlatformClassLoader", | ||
MethodType.methodType(classOf[ClassLoader]) | ||
) | ||
.invoke() | ||
.asInstanceOf[ClassLoader] | ||
} catch { | ||
case _: Throwable => | ||
null | ||
} | ||
} | ||
} | ||
|
||
private def classLoader(location: URI): ClassLoader = | ||
new URLClassLoader(Array(location.toURL), bootClassLoader) | ||
|
||
private def manifestAt(location: URI): URL = classLoader(location).getResource("META-INF/MANIFEST.MF") | ||
|
||
|
||
// ZipArchive.fromManifestURL(URL) | ||
@Test def `manifest resources just works`(): Unit = { | ||
val jar = createTestJar() | ||
val archive = new ManifestResources(manifestAt(jar.toUri)) | ||
try { | ||
val it = archive.iterator | ||
assertTrue(it.hasNext) | ||
val f = it.next() | ||
assertFalse(it.hasNext) | ||
assertEquals("foo.class", f.name) | ||
} | ||
finally { | ||
archive.close() | ||
// The following results in IOException on Windows (file in use by another process). | ||
// As jar created with Files.createTempFile, it will be deleted automatically. | ||
try Files.delete(jar) catch case _: IOException => () | ||
} | ||
} | ||
|
||
private def createTestJar(): Path = Files.createTempFile("junit", ".jar").tap { f => | ||
val man = new Manifest() | ||
man.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0") | ||
man.getEntries().put("foo.class", new Attributes(0)) | ||
Using.resource(new JarOutputStream(Files.newOutputStream(f), man)) { jout => | ||
jout.putNextEntry(new JarEntry("foo.class")) | ||
val bytes = "hello, world".getBytes | ||
jout.write(bytes, 0, bytes.length) | ||
() | ||
} | ||
} | ||
|
||
private def createTestZip(): Path = Files.createTempFile("junit", ".zip").tap { f => | ||
import java.util.zip._ | ||
Using.resource(new ZipOutputStream(Files.newOutputStream(f))) { zout => | ||
zout.setLevel(Deflater.NO_COMPRESSION) | ||
zout.setMethod(ZipOutputStream.STORED) | ||
val entry = new ZipEntry("foo.class") | ||
val bytes = "hello, world".getBytes | ||
entry.setSize(bytes.length) | ||
entry.setCompressedSize(bytes.length) | ||
entry.setCrc(new CRC32().tap(_.update(bytes, 0, bytes.length)).getValue) | ||
zout.putNextEntry(entry) | ||
zout.write(bytes, 0, bytes.length) | ||
zout.closeEntry() | ||
() | ||
} | ||
} | ||
/* zipfs doesn't write size field in file header as required by URLZipArchive | ||
private def createTestZip2(): Path = { | ||
import java.nio.file.FileSystems | ||
import java.net.URI | ||
import scala.util.chaining._ | ||
import scala.jdk.CollectionConverters._ | ||
val f = Files.createTempFile("junit", ".zip") | ||
Files.delete(f) | ||
val uri = URI.create(s"jar:${f.toUri}") | ||
val env = Map("create" -> "true").asJava | ||
Using.resource(FileSystems.newFileSystem(uri, env)) { fs => | ||
val path = fs.getPath("foo.class") | ||
val bytes = "hello, world".getBytes | ||
Files.write(path, bytes) | ||
} | ||
f.tap(println(_)) | ||
} | ||
*/ | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can simpli