Back to Repositories

Testing AttributeStorage Management Operations in JADX

This test suite validates the functionality of AttributeStorage class in the JADX decompiler project. It ensures proper handling of attribute storage operations including adding, removing, and managing custom attribute types.

Test Coverage Overview

The test suite provides comprehensive coverage of AttributeStorage operations.

Key areas tested include:
  • Basic flag operations (add/remove)
  • Custom attribute handling
  • Attribute type verification
  • Edge cases with null checks and duplicate attributes
Integration points focus on the IJadxAttribute interface implementation and AType attribute type system.

Implementation Analysis

The testing approach utilizes JUnit Jupiter for structured unit testing. Each test method isolates specific attribute storage functionality.

Implementation patterns include:
  • Setup isolation using @BeforeEach
  • Custom attribute class implementation
  • Assertion chaining for comprehensive validation
  • Type-safe attribute handling verification

Technical Details

Testing infrastructure includes:
  • JUnit Jupiter test framework
  • Custom JadxAssertions utility
  • AttributeStorage core class
  • AType and IJadxAttribute implementations
Configuration leverages BeforeEach for clean test state management.

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices with clear separation of concerns and thorough validation.

Notable practices include:
  • Isolated test setup and teardown
  • Comprehensive positive and negative testing
  • Clear test method naming
  • Focused test scenarios
  • Strong type safety checks

skylot/jadx

jadx-core/src/test/java/jadx/tests/functional/AttributeStorageTest.java

            
package jadx.tests.functional;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import jadx.api.plugins.input.data.attributes.IJadxAttribute;
import jadx.core.dex.attributes.AType;
import jadx.core.dex.attributes.AttributeStorage;

import static jadx.core.dex.attributes.AFlag.SYNTHETIC;
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;

public class AttributeStorageTest {
	private AttributeStorage storage;

	@BeforeEach
	public void setup() {
		storage = new AttributeStorage();
	}

	@Test
	public void testAdd() {
		storage.add(SYNTHETIC);
		assertThat(storage.contains(SYNTHETIC)).isTrue();
	}

	@Test
	public void testRemove() {
		storage.add(SYNTHETIC);
		storage.remove(SYNTHETIC);
		assertThat(storage.contains(SYNTHETIC)).isFalse();
	}

	public static final AType<TestAttr> TEST = new AType<>();

	public static class TestAttr implements IJadxAttribute {
		@Override
		public AType<TestAttr> getAttrType() {
			return TEST;
		}
	}

	@Test
	public void testAddAttribute() {
		TestAttr attr = new TestAttr();
		storage.add(attr);

		assertThat(storage.contains(TEST)).isTrue();
		assertThat(storage.get(TEST)).isEqualTo(attr);
	}

	@Test
	public void testRemoveAttribute() {
		TestAttr attr = new TestAttr();
		storage.add(attr);
		storage.remove(attr);

		assertThat(storage.contains(TEST)).isFalse();
		assertThat(storage.get(TEST)).isNull();
	}

	@Test
	public void testRemoveOtherAttribute() {
		TestAttr attr = new TestAttr();
		storage.add(attr);
		storage.remove(new TestAttr());

		assertThat(storage.contains(TEST)).isTrue();
		assertThat(storage.get(TEST)).isEqualTo(attr);
	}
}