Back to Repositories

Testing Disk-Based Code Cache Implementation in JADX

This test suite validates the disk-based code caching functionality in the JADX decompiler’s GUI component. It ensures proper storage and retrieval of decompiled code along with its associated metadata and annotations.

Test Coverage Overview

The test suite provides comprehensive coverage of the DiskCodeCache implementation.

Key areas tested include:
  • Code storage and retrieval operations
  • Metadata preservation and consistency
  • Line mapping validation
  • Code annotation persistence
Integration points with ClassNode and ICodeInfo interfaces are thoroughly verified.

Implementation Analysis

The testing approach utilizes JUnit 5 with temporary directory support for isolated testing. The implementation follows a clear pattern of setup, execution, and verification phases.

Framework-specific features leveraged include:
  • @TempDir for automatic test directory management
  • AssertJ fluent assertions for detailed comparisons
  • Integration with SLF4J for logging test details

Technical Details

Testing tools and configuration:
  • JUnit Jupiter test framework
  • IntegrationTest base class extension
  • NoOpCodeCache for isolation
  • SLF4J logging framework
  • Temporary directory injection
  • AssertJ assertion library

Best Practices Demonstrated

The test exhibits several quality testing practices and patterns.

Notable implementations include:
  • Proper resource cleanup and management
  • Isolated test environment using temporary directories
  • Comprehensive assertion coverage
  • Detailed metadata verification
  • Clear test method organization
  • Effective use of logging for debugging

skylot/jadx

jadx-gui/src/test/java/jadx/gui/utils/cache/code/DiskCodeCacheTest.java

            
package jadx.gui.utils.cache.code;

import java.io.IOException;
import java.nio.file.Path;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jadx.api.ICodeInfo;
import jadx.api.impl.NoOpCodeCache;
import jadx.core.dex.nodes.ClassNode;
import jadx.gui.cache.code.disk.DiskCodeCache;
import jadx.tests.api.IntegrationTest;

import static org.assertj.core.api.Assertions.assertThat;

class DiskCodeCacheTest extends IntegrationTest {
	private static final Logger LOG = LoggerFactory.getLogger(DiskCodeCacheTest.class);

	@TempDir
	public Path tempDir;

	@Test
	public void test() throws IOException {
		disableCompilation();
		getArgs().setCodeCache(NoOpCodeCache.INSTANCE);
		ClassNode clsNode = getClassNode(DiskCodeCacheTest.class);
		ICodeInfo codeInfo = clsNode.getCode();

		DiskCodeCache cache = new DiskCodeCache(clsNode.root(), tempDir);

		String clsKey = clsNode.getFullName();
		cache.add(clsKey, codeInfo);

		ICodeInfo readCodeInfo = cache.get(clsKey);

		assertThat(readCodeInfo).isNotNull();
		assertThat(readCodeInfo.getCodeStr()).isEqualTo(codeInfo.getCodeStr());
		assertThat(readCodeInfo.getCodeMetadata().getLineMapping()).isEqualTo(codeInfo.getCodeMetadata().getLineMapping());
		LOG.info("Disk code annotations: {}", readCodeInfo.getCodeMetadata().getAsMap());
		assertThat(readCodeInfo.getCodeMetadata().getAsMap()).hasSameSizeAs(codeInfo.getCodeMetadata().getAsMap());

		cache.close();
	}
}