Back to Repositories

Testing Environment Property Resolution in Alibaba Arthas

This test suite validates the ArthasEnvironment class functionality, focusing on placeholder resolution and property source management in the Alibaba Arthas core environment. The tests ensure proper handling of system properties and custom property sources.

Test Coverage Overview

The test suite provides comprehensive coverage of the ArthasEnvironment class functionality.

Key areas tested include:
  • System property placeholder resolution
  • Custom property source management
  • Property override behavior
  • Fallback handling for undefined placeholders

Implementation Analysis

The testing approach utilizes JUnit 4 with AssertJ assertions for clear test verification. The implementation follows a systematic pattern of environment setup, property manipulation, and assertion verification.

Key patterns include:
  • Property source layering tests
  • System property integration checks
  • Placeholder resolution verification

Technical Details

Testing infrastructure includes:
  • JUnit 4 test framework
  • AssertJ assertion library
  • Properties API for configuration
  • System property manipulation
  • Custom PropertySource implementation

Best Practices Demonstrated

The test suite exemplifies several testing best practices including isolated test methods, proper cleanup of system properties, and clear assertion messages. The code organization follows a logical structure with separate test cases for system properties and custom property sources.

Notable practices:
  • Clean separation of test scenarios
  • Proper resource cleanup
  • Comprehensive assertion coverage
  • Clear test method naming

alibaba/arthas

core/src/test/java/com/taobao/arthas/core/env/ArthasEnvironmentTest.java

            
package com.taobao.arthas.core.env;

import java.util.Properties;

import org.assertj.core.api.Assertions;
import org.junit.Test;

/**
 * 
 * @author hengyunabc 2019-12-27
 *
 */
public class ArthasEnvironmentTest {

    @Test
    public void test() {
        ArthasEnvironment arthasEnvironment = new ArthasEnvironment();

        Assertions.assertThat(arthasEnvironment.resolvePlaceholders("hello, ${java.version}"))
                .isEqualTo("hello, " + System.getProperty("java.version"));

        Assertions.assertThat(arthasEnvironment.resolvePlaceholders("hello, ${xxxxxxxxxxxxxxx}"))
                .isEqualTo("hello, ${xxxxxxxxxxxxxxx}");

        System.setProperty("xxxxxxxxxxxxxxx", "vvv");

        Assertions.assertThat(arthasEnvironment.resolvePlaceholders("hello, ${xxxxxxxxxxxxxxx}"))
                .isEqualTo("hello, vvv");

        System.clearProperty("xxxxxxxxxxxxxxx");
    }

    @Test
    public void test_properties() {
        ArthasEnvironment arthasEnvironment = new ArthasEnvironment();

        Properties properties1 = new Properties();
        Properties properties2 = new Properties();
        arthasEnvironment.addLast(new PropertiesPropertySource("test1", properties1));
        arthasEnvironment.addLast(new PropertiesPropertySource("test2", properties2));

        properties2.put("test.key", "2222");

        Assertions.assertThat(arthasEnvironment.resolvePlaceholders("hello, ${test.key}")).isEqualTo("hello, 2222");

        properties1.put("java.version", "test");
        properties1.put("test.key", "test");

        Assertions.assertThat(arthasEnvironment.resolvePlaceholders("hello, ${java.version}"))
                .isEqualTo("hello, " + System.getProperty("java.version"));

        Assertions.assertThat(arthasEnvironment.resolvePlaceholders("hello, ${test.key}")).isEqualTo("hello, test");
    }
}