From 60d54cc4df05e3328f8b8d64ea3b44d5d22c9ed7 Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 29 Jul 2024 18:33:08 +0100 Subject: Add some unit tests --- .../spark/common/heapdump/HeapDumpSummaryTest.java | 47 ++++++++++++++++++++++ .../lucko/spark/common/heapdump/HeapDumpTest.java | 41 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpSummaryTest.java create mode 100644 spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpTest.java (limited to 'spark-common/src/test/java/me/lucko/spark/common/heapdump') diff --git a/spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpSummaryTest.java b/spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpSummaryTest.java new file mode 100644 index 0000000..42492d1 --- /dev/null +++ b/spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpSummaryTest.java @@ -0,0 +1,47 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.lucko.spark.common.heapdump; + +import me.lucko.spark.test.TestClass; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class HeapDumpSummaryTest { + + @Test + public void testHeapDumpSummary() throws Exception { + TestClass testClass1 = new TestClass(); + TestClass testClass2 = new TestClass(); + + HeapDumpSummary dump = HeapDumpSummary.createNew(); + List entries = dump.getEntries(); + + HeapDumpSummary.Entry thisClassEntry = entries.stream().filter(entry -> entry.getType().equals(TestClass.class.getName())).findAny().orElse(null); + assertNotNull(thisClassEntry); + assertEquals(2, thisClassEntry.getInstances()); + assertEquals(32, thisClassEntry.getBytes()); + } + +} diff --git a/spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpTest.java b/spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpTest.java new file mode 100644 index 0000000..5df5c5d --- /dev/null +++ b/spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpTest.java @@ -0,0 +1,41 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.lucko.spark.common.heapdump; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class HeapDumpTest { + + @Test + public void testHeapDump(@TempDir Path tempDir) throws Exception { + Path file = tempDir.resolve("heapdump.hprof"); + HeapDump.dumpHeap(file, false); + assertTrue(Files.exists(file)); + Files.delete(file); + } + +} -- cgit