Back to Repositories

Testing String Manipulation Utils and Character Escaping in JADX

This test suite validates string manipulation utilities in the JADX decompiler, focusing on character escaping and unescaping operations. The tests ensure proper handling of special characters, Unicode sequences, and resource string value escaping.

Test Coverage Overview

The test suite provides comprehensive coverage of string manipulation operations in JADX.

Key areas tested include:
  • String unescaping with various special characters
  • Character unescaping functionality
  • Resource string value escaping
  • Unicode character handling
  • Special character sequences (\n, \t, \r, etc.)

Implementation Analysis

The testing approach utilizes JUnit Jupiter for structured unit testing, with separate test methods for each string manipulation scenario.

Implementation features:
  • Helper methods for validation (checkStringUnescape, checkCharUnescape)
  • Custom assertions using JadxAssertions
  • Configurable string handling through JadxArgs
  • Systematic test case organization

Technical Details

Testing infrastructure includes:
  • JUnit Jupiter test framework
  • JADX custom assertions (JadxAssertions)
  • StringUtils utility class
  • JadxArgs configuration
  • SuppressWarnings annotations for Unicode handling

Best Practices Demonstrated

The test suite exemplifies several testing best practices.

Notable elements include:
  • Isolated test methods for specific functionality
  • Comprehensive edge case coverage
  • Clear test method naming
  • Reusable validation helper methods
  • Proper test setup and configuration

skylot/jadx

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

            
package jadx.tests.functional;

import org.junit.jupiter.api.Test;

import jadx.api.JadxArgs;
import jadx.core.utils.StringUtils;

import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;

class StringUtilsTest {

	private StringUtils stringUtils;

	@Test
	@SuppressWarnings("AvoidEscapedUnicodeCharacters")
	public void testStringUnescape() {
		JadxArgs args = new JadxArgs();
		args.setEscapeUnicode(true);
		stringUtils = new StringUtils(args);

		checkStringUnescape("", "");
		checkStringUnescape("'", "'");
		checkStringUnescape("a", "a");
		checkStringUnescape("\n", "\\n");
		checkStringUnescape("\t", "\\t");
		checkStringUnescape("\r", "\\r");
		checkStringUnescape("\b", "\\b");
		checkStringUnescape("\f", "\\f");
		checkStringUnescape("\\", "\\\\");
		checkStringUnescape("\"", "\\\"");
		checkStringUnescape("\u1234", "\\u1234");
	}

	private void checkStringUnescape(String input, String result) {
		assertThat(stringUtils.unescapeString(input)).isEqualTo('"' + result + '"');
	}

	@Test
	public void testCharUnescape() {
		stringUtils = new StringUtils(new JadxArgs());

		checkCharUnescape('a', "a");
		checkCharUnescape(' ', " ");
		checkCharUnescape('\n', "\\n");
		checkCharUnescape('\'', "\\'");

		assertThat(stringUtils.unescapeChar('\0')).isEqualTo("0");
	}

	private void checkCharUnescape(char input, String result) {
		assertThat(stringUtils.unescapeChar(input)).isEqualTo('\'' + result + '\'');
	}

	@Test
	public void testResStrValueEscape() {
		checkResStrValueEscape("line\nnew line", "line\\nnew line");
		checkResStrValueEscape("can't", "can\\'t");
		checkResStrValueEscape("quote\"end", "quote\\\"end");
	}

	private void checkResStrValueEscape(String input, String result) {
		assertThat(StringUtils.escapeResStrValue(input)).isEqualTo(result);
	}
}