Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MatCalciteTest/dumps/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.index
*.threads
*.mat-version
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
import org.eclipse.mat.snapshot.SnapshotFactory;
import org.eclipse.mat.util.VoidProgressListener;
import org.junit.Assert;
import org.osgi.framework.FrameworkUtil;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand All @@ -22,11 +28,40 @@ public abstract class AbstractQueriesTests {

protected abstract ISnapshot getSnapshot();

protected static ISnapshot openSnapshot(File heapDump) throws SnapshotException {
protected static ISnapshot openSnapshot(File heapDump) throws SnapshotException, IOException {
System.out.println("exists = " + heapDump.exists() + ", file = " + heapDump.getAbsolutePath());
deleteIndexesOfOtherMatVersion(heapDump.toPath());
return SnapshotFactory.openSnapshot(heapDump, new VoidProgressListener());
}

/**
* Deletes the index files of the heap dump unless the running MAT version wrote them.
*
* <p>MAT opens the {@code *.index} and {@code *.threads} files next to the dump instead of
* parsing the dump again, so indexes left by an older MAT would make the tests check that
* version's output. The MAT version that wrote the indexes is kept next to them, in a file named
* after the dump with the {@code mat-version} extension, such as {@code mvn1m_jdk18.mat-version}.
*/
private static void deleteIndexesOfOtherMatVersion(Path heapDump) throws IOException {
String matVersion = FrameworkUtil.getBundle(SnapshotFactory.class).getVersion().toString();
String fileName = heapDump.getFileName().toString();
// For mvn1m_jdk18.hprof, MAT writes mvn1m_jdk18.index, mvn1m_jdk18.idx.index, mvn1m_jdk18.threads, and so on
String prefix = fileName.substring(0, fileName.lastIndexOf('.') + 1);
Path dir = heapDump.toAbsolutePath().getParent();
Path versionFile = dir.resolve(prefix + "mat-version");
if (Files.exists(versionFile)
&& new String(Files.readAllBytes(versionFile), StandardCharsets.UTF_8).equals(matVersion)) {
return;
}
try (DirectoryStream<Path> files = Files.newDirectoryStream(dir, prefix + "{index,*.index,threads}")) {
for (Path file : files) {
System.out.println("Deleting " + file + ": no record that MAT " + matVersion + " wrote it");
Files.delete(file);
}
}
Files.write(versionFile, matVersion.getBytes(StandardCharsets.UTF_8));
}

public static void closeSnapshot(ISnapshot snapshot) {
SnapshotFactory.dispose(snapshot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
import org.junit.BeforeClass;

import java.io.File;
import java.io.IOException;

public abstract class SampleHeapDumpTests extends AbstractQueriesTests {
protected static ISnapshot snapshot;

@BeforeClass
public static void openSnapshot() throws SnapshotException {
public static void openSnapshot() throws SnapshotException, IOException {
snapshot = openSnapshot(new File("dumps", "mvn1m_jdk18.hprof"));
}

Expand Down
Loading