Back to Repositories

Testing OGNL Expression Evaluation Implementation in Alibaba Arthas

This test suite validates the OGNL (Object-Graph Navigation Language) expression functionality in Arthas, focusing on expression evaluation and property access. It verifies various OGNL expressions including conditional operations, collection manipulation, and system property access.

Test Coverage Overview

The test suite provides comprehensive coverage of OGNL expression evaluation scenarios.

Key areas tested include:
  • Conditional string operations and length checks
  • Collection membership and filtering operations
  • Recursive function definitions and execution
  • System property access and manipulation
  • Invalid expression handling and error cases

Implementation Analysis

The testing approach uses JUnit to validate OGNL expression evaluation through the Express interface. Tests utilize unpooled express instances for isolated execution environments.

Implementation patterns include:
  • Direct expression evaluation using get() method
  • System property manipulation for environment setup
  • Exception handling verification for invalid expressions
  • Complex OGNL syntax testing including projections and selections

Technical Details

Testing infrastructure:
  • JUnit 4 test framework
  • ExpressFactory for creating expression evaluators
  • OGNL expression parser and evaluator
  • System property manipulation utilities
  • Assert statements for validation

Best Practices Demonstrated

The test suite exemplifies robust testing practices for expression evaluation systems.

Notable practices include:
  • Isolated test cases with clear purpose
  • Proper cleanup of system properties
  • Comprehensive error case handling
  • Progressive complexity in test scenarios
  • Clear separation of positive and negative test cases

alibaba/arthas

core/src/test/java/com/taobao/arthas/core/command/express/OgnlExpressTest.java

            
package com.taobao.arthas.core.command.express;

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

public class OgnlExpressTest {

    @Test
    public void testValidOgnlExpr1() throws ExpressException {
        Express unpooledExpress = ExpressFactory.unpooledExpress(OgnlExpressTest.class.getClassLoader());
        Assert.assertEquals(unpooledExpress.get("\"test\".length() % 2 == 0 ? \"even length\" : \"odd length\""),
                "even length");
    }

    @Test
    public void testValidOgnlExpr2() throws ExpressException {
        System.setProperty("ognl.chain.short-circuit", String.valueOf(false));
        Express unpooledExpress = ExpressFactory.unpooledExpress(OgnlExpressTest.class.getClassLoader());
        Assert.assertEquals(unpooledExpress.get("4 in {1, 2, 3, 4}"), true);
        Assert.assertEquals(unpooledExpress.get("{1, 2, 3, 4}.{^ #this % 2 == 0}[$]"), 2);
        Assert.assertEquals(unpooledExpress.get("{1, 2, 3, 4}.{? #this % 2 == 0}[$]"), 4);
    }

    @Test
    public void testValidOgnlExpr3() throws ExpressException {
        Express unpooledExpress = ExpressFactory.unpooledExpress(OgnlExpressTest.class.getClassLoader());
        Assert.assertEquals(unpooledExpress.get("#factorial = :[#this <= 1 ? 1 : #this * #factorial(#this - 1)], #factorial(5)"),
                120);
    }

    @Test
    public void testValidOgnlExpr4() throws ExpressException {
        Express unpooledExpress = ExpressFactory.unpooledExpress(OgnlExpressTest.class.getClassLoader());
        System.setProperty("arthas.test1", "arthas");
        System.setProperty("arthas.ognl.test2", "test");
        Assert.assertEquals(unpooledExpress.get("#value1=@System@getProperty(\"arthas.test1\")," +
                        "#value2=@System@getProperty(\"arthas.ognl.test2\"), {#value1, #value2}").toString(),
                "[arthas, test]");
        System.clearProperty("arthas.test1");
        System.clearProperty("arthas.ognl.test2");
    }

    @Test
    public void testInvalidOgnlExpr() {
        try {
            Express unpooledExpress = ExpressFactory.unpooledExpress(OgnlExpressTest.class.getClassLoader());
            System.out.println(unpooledExpress.get("#[email protected](\"java.home\")," +
                            "#value2=@System@getProperty(\"java.runtime.name\"), {#value1, #value2}").toString());
        } catch (Exception e){
            Assert.assertTrue(e.getCause() instanceof ognl.ExpressionSyntaxException);
        }
    }
}