Back to Repositories

Testing NameMapper Identifier Validation in skylot/jadx

This test suite evaluates the NameMapper functionality in the JADX decompiler, focusing on identifier validation and character handling. The tests verify the proper handling of valid and invalid Java identifiers, along with methods for sanitizing invalid character sequences.

Test Coverage Overview

The test suite provides comprehensive coverage of the NameMapper class functionality, including:

  • Validation of legal Java identifiers
  • Detection of invalid identifier patterns
  • Character sanitization in identifier strings
  • Prefix handling for invalid identifiers
Key edge cases include numeric prefixes, special characters, and hyphenated names.

Implementation Analysis

The testing approach utilizes JUnit Jupiter’s modular structure with focused test methods for each functionality aspect. The implementation leverages AssertJ custom assertions through JadxAssertions, providing fluent assertion syntax for enhanced readability and maintainability.

Each test method isolates specific validation scenarios using clear input-output patterns.

Technical Details

Testing infrastructure includes:

  • JUnit Jupiter test framework
  • Custom JadxAssertions for enhanced verification
  • Static imports for direct access to NameMapper methods
  • Standardized naming conventions for test methods

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Single responsibility principle in test methods
  • Clear method naming reflecting test scenarios
  • Consistent assertion patterns
  • Comprehensive coverage of edge cases
  • Efficient test organization and readability

skylot/jadx

jadx-core/src/test/java/jadx/core/deobf/NameMapperTest.java

            
package jadx.core.deobf;

import org.junit.jupiter.api.Test;

import static jadx.core.deobf.NameMapper.isValidIdentifier;
import static jadx.core.deobf.NameMapper.removeInvalidChars;
import static jadx.core.deobf.NameMapper.removeInvalidCharsMiddle;
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;

public class NameMapperTest {

	@Test
	public void validIdentifiers() {
		assertThat(isValidIdentifier("ACls")).isTrue();
	}

	@Test
	public void notValidIdentifiers() {
		assertThat(isValidIdentifier("1cls")).isFalse();
		assertThat(isValidIdentifier("-cls")).isFalse();
		assertThat(isValidIdentifier("A-cls")).isFalse();
	}

	@Test
	public void testRemoveInvalidCharsMiddle() {
		assertThat(removeInvalidCharsMiddle("1cls")).isEqualTo("1cls");
		assertThat(removeInvalidCharsMiddle("-cls")).isEqualTo("cls");
		assertThat(removeInvalidCharsMiddle("A-cls")).isEqualTo("Acls");
	}

	@Test
	public void testRemoveInvalidChars() {
		assertThat(removeInvalidChars("1cls", "C")).isEqualTo("C1cls");
		assertThat(removeInvalidChars("-cls", "C")).isEqualTo("cls");
		assertThat(removeInvalidChars("A-cls", "C")).isEqualTo("Acls");
	}
}