From e9ab081201669bd708e169d7b57c91ae9d306647 Mon Sep 17 00:00:00 2001 From: "Egor.Ushakov" Date: Wed, 3 May 2017 19:19:38 +0300 Subject: [PATCH] speedup: cache zip files in fixture --- .../decompiler/DecompilerTestFixture.java | 52 ++++++++++++++++--- 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/test/org/jetbrains/java/decompiler/DecompilerTestFixture.java b/test/org/jetbrains/java/decompiler/DecompilerTestFixture.java index df27781..2dbcce6 100644 --- a/test/org/jetbrains/java/decompiler/DecompilerTestFixture.java +++ b/test/org/jetbrains/java/decompiler/DecompilerTestFixture.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. + * Copyright 2000-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,9 +21,9 @@ import org.jetbrains.java.decompiler.util.InterpreterUtil; import java.io.File; import java.io.IOException; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; +import java.util.*; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; import static org.junit.Assert.assertTrue; @@ -32,7 +32,7 @@ public class DecompilerTestFixture { private File testDataDir; private File tempDir; private File targetDir; - private ConsoleDecompiler decompiler; + private TestConsoleDecompiler decompiler; public void setUp(String... optionPairs) throws IOException { assertThat(optionPairs.length % 2).isEqualTo(0); @@ -62,13 +62,14 @@ public class DecompilerTestFixture { for (int i = 0; i < optionPairs.length; i += 2) { options.put(optionPairs[i], optionPairs[i + 1]); } - decompiler = new ConsoleDecompiler(targetDir, options); + decompiler = new TestConsoleDecompiler(targetDir, options); } public void tearDown() { if (tempDir != null) { delete(tempDir); } + decompiler.close(); } public File getTestDataDir() { @@ -122,4 +123,43 @@ public class DecompilerTestFixture { throw new RuntimeException(e); } } + + // cache zip files + private static class TestConsoleDecompiler extends ConsoleDecompiler { + private final HashMap zipFiles = new HashMap<>(); + + public TestConsoleDecompiler(File destination, Map options) { + super(destination, options); + } + + @Override + public byte[] getBytecode(String externalPath, String internalPath) throws IOException { + File file = new File(externalPath); + if (internalPath == null) { + return InterpreterUtil.getBytes(file); + } + else { + ZipFile archive = zipFiles.get(file.getName()); + if (archive == null) { + archive = new ZipFile(file); + zipFiles.put(file.getName(), archive); + } + ZipEntry entry = archive.getEntry(internalPath); + if (entry == null) throw new IOException("Entry not found: " + internalPath); + return InterpreterUtil.getBytes(archive, entry); + } + } + + void close() { + for (ZipFile file : zipFiles.values()) { + try { + file.close(); + } + catch (IOException e) { + e.printStackTrace(); + } + } + zipFiles.clear(); + } + } } \ No newline at end of file