Back to Repositories

Testing Properties Injection and Environment Configuration in Alibaba Arthas

This test suite validates the properties injection functionality in Arthas, focusing on server configuration and environment property binding. It ensures proper mapping of various property types including primitives, network configurations, and SSL settings.

Test Coverage Overview

The test suite provides comprehensive coverage of the PropertiesInjectUtil functionality in Arthas core configuration.

Key areas tested include:
  • Basic property injection for primitive types (integers, strings, booleans)
  • Network configuration properties (host, port, address)
  • SSL configuration with multiple property types
  • Array property parsing and injection
  • Enum value binding for error properties

Implementation Analysis

The testing approach utilizes JUnit to verify property injection through the BinderUtils class. Properties are configured programmatically and injected into a Server object using ArthasEnvironment.

Testing patterns include:
  • Property source configuration through PropertiesPropertySource
  • Environment-based property injection
  • Type conversion validation
  • Nested property object handling

Technical Details

Testing infrastructure includes:
  • JUnit 4 testing framework
  • ArthasEnvironment for property management
  • Custom Server configuration class
  • InetAddress handling for network properties
  • BinderUtils for property injection
  • PropertiesPropertySource for property source management

Best Practices Demonstrated

The test implementation showcases several testing best practices:

  • Comprehensive assertion coverage for all property types
  • Proper test isolation and setup
  • Clear test case organization
  • Validation of both positive and negative scenarios
  • Thorough verification of type conversion and nested properties

alibaba/arthas

core/src/test/java/com/taobao/arthas/core/config/PropertiesInjectUtilTest.java

            
package com.taobao.arthas.core.config;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties;

import org.junit.Assert;
import org.junit.Test;

import com.taobao.arthas.core.config.ErrorProperties.IncludeStacktrace;
import com.taobao.arthas.core.env.ArthasEnvironment;
import com.taobao.arthas.core.env.PropertiesPropertySource;



public class PropertiesInjectUtilTest {

	@Test
	public void test() throws UnknownHostException {
		Properties p = new Properties();
		p.put("server.port", "8080");

		p.put("server.host", "localhost");
		p.put("server.flag", "true");

		p.put("server.address", "192.168.1.1");

		p.put("server.ssl.enabled", "true");
		p.put("server.ssl.protocol", "TLS");
		p.put("server.ssl.testBoolean", "false");
		p.put("server.ssl.testLong", "123");
		p.put("server.ssl.ciphers", "abc, efg ,hij");

		p.put("server.error.includeStacktrace", "ALWAYS");
		
		
        ArthasEnvironment arthasEnvironment = new ArthasEnvironment();

        arthasEnvironment.addLast(new PropertiesPropertySource("test1", p));
        

		Server server = new Server();

		BinderUtils.inject(arthasEnvironment, server);

		System.out.println(server);

		Assert.assertEquals(server.getPort(), 8080);
		Assert.assertEquals(server.getHost(), "localhost");
		Assert.assertTrue(server.isFlag());

		Assert.assertEquals(server.getAddress(), InetAddress.getByName("192.168.1.1"));

		Assert.assertEquals(server.ssl.getProtocol(), "TLS");
		Assert.assertTrue(server.ssl.isEnabled());
		Assert.assertFalse(server.ssl.getTestBoolean());
		Assert.assertEquals(server.ssl.getTestLong(), Long.valueOf(123));
		Assert.assertNull(server.ssl.getTestDouble());

		Assert.assertArrayEquals(server.ssl.getCiphers(), new String[] { "abc", "efg", "hij" });

		Assert.assertEquals(server.error.getIncludeStacktrace(), IncludeStacktrace.ALWAYS);

	}

}