speedup: cache zip files in fixture

master
Egor.Ushakov 7 years ago
parent 601cae8cf8
commit e9ab081201
  1. 52
      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<String, ZipFile> zipFiles = new HashMap<>();
public TestConsoleDecompiler(File destination, Map<String, Object> 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();
}
}
}
Loading…
Cancel
Save