Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Qlexpress #2978

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
【支持qlexpress表达式】
  • Loading branch information
taokan committed Nov 10, 2024
commit ec0f951dc098ca8917641462f1b2d96e2ecb2d64
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public void setOptionValue(String optionValue) {
@Override
public void process(CommandProcess process) {
try {
logger.info("start");
ExitStatus status = null;
if (isShow()) {
status = processShow(process);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package com.taobao.arthas.core.command.express;

import com.alibaba.arthas.deps.org.slf4j.Logger;
import com.alibaba.arthas.deps.org.slf4j.LoggerFactory;
import com.taobao.arthas.core.GlobalOptions;
import com.taobao.arthas.core.command.klass100.OgnlCommand;
import com.taobao.arthas.core.command.model.ExpressTypeEnum;


/**
* ExpressFactory
* @author ralf0131 2017-01-04 14:40.
* @author hengyunabc 2018-10-08
*/
public class ExpressFactory {
private static final Logger logger = LoggerFactory.getLogger(ExpressFactory.class);

private static final ThreadLocal<Express> expressRef = ThreadLocal.withInitial(() -> new OgnlExpress());
private static final ThreadLocal<Express> expressRefQLExpress = ThreadLocal.withInitial(() -> new QLExpress());

Expand All @@ -18,6 +24,7 @@ public class ExpressFactory {
* @return
*/
public static Express threadLocalExpress(Object object) {
logger.info("expressType:"+GlobalOptions.ExpressType+ "_"+ExpressTypeEnum.QLEXPRESS.getExpressType());
if (GlobalOptions.ExpressType == ExpressTypeEnum.QLEXPRESS.getExpressType()) {
return expressRefQLExpress.get().reset().bind(object);
}
Expand All @@ -28,6 +35,7 @@ public static Express unpooledExpress(ClassLoader classloader) {
if (classloader == null) {
classloader = ClassLoader.getSystemClassLoader();
}
logger.info("expressType:"+GlobalOptions.ExpressType+ "_"+ExpressTypeEnum.QLEXPRESS.getExpressType());
if (GlobalOptions.ExpressType == ExpressTypeEnum.QLEXPRESS.getExpressType()) {
return new QLExpress(classloader);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.taobao.arthas.core.command.express;

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

/**
* @Author TaoKan
* @Date 2024/10/7 9:21 PM
*/
public class QLExpressTest {

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

@Test
public void testValidQLExpr2() throws ExpressException {
System.setProperty("ognl.chain.short-circuit", String.valueOf(false));
Express unpooledExpress = ExpressFactory.unpooledExpress(QLExpressTest.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 testValidQLExpr3() throws ExpressException {
Express unpooledExpress = ExpressFactory.unpooledExpress(QLExpressTest.class.getClassLoader());
Assert.assertEquals(unpooledExpress.get("#factorial = :[#this <= 1 ? 1 : #this * #factorial(#this - 1)], #factorial(5)"),
120);
}

@Test
public void testValidQLExpr4() throws ExpressException {
Express unpooledExpress = ExpressFactory.unpooledExpress(QLExpressTest.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 testInvalidQLExpr() {
try {
Express unpooledExpress = ExpressFactory.unpooledExpress(QLExpressTest.class.getClassLoader());
System.out.println(unpooledExpress.get("#value1=@System.getProperty(\"java.home\")," +
"#value2=@System@getProperty(\"java.runtime.name\"), {#value1, #value2}").toString());
} catch (Exception e){
Assert.assertTrue(e.getCause() instanceof ognl.ExpressionSyntaxException);
}
}
}