From b4c4e72ba0e3917ed6df89ded76c871ad6977b0e Mon Sep 17 00:00:00 2001 From: Xiao Date: Wed, 22 Sep 2021 01:01:11 -0400 Subject: [PATCH] JXPATH-198: Refactor Context to improve test logic --- pom.xml | 6 ++ .../ri/compiler/ExtensionFunctionTest.java | 82 +++++++++---------- 2 files changed, 45 insertions(+), 43 deletions(-) diff --git a/pom.xml b/pom.xml index 48b254dff..6432868dc 100644 --- a/pom.xml +++ b/pom.xml @@ -152,6 +152,12 @@ under the License. 3.8.1 test + + org.mockito + mockito-core + 3.12.4 + test + xml-apis xml-apis diff --git a/src/test/java/org/apache/commons/jxpath/ri/compiler/ExtensionFunctionTest.java b/src/test/java/org/apache/commons/jxpath/ri/compiler/ExtensionFunctionTest.java index 7a222e4d4..2a446b2f6 100644 --- a/src/test/java/org/apache/commons/jxpath/ri/compiler/ExtensionFunctionTest.java +++ b/src/test/java/org/apache/commons/jxpath/ri/compiler/ExtensionFunctionTest.java @@ -30,7 +30,6 @@ import org.apache.commons.jxpath.JXPathTestCase; import org.apache.commons.jxpath.NodeSet; import org.apache.commons.jxpath.PackageFunctions; -import org.apache.commons.jxpath.Pointer; import org.apache.commons.jxpath.TestBean; import org.apache.commons.jxpath.Variables; import org.apache.commons.jxpath.ri.model.NodePointer; @@ -38,6 +37,9 @@ import org.apache.commons.jxpath.util.TypeConverter; import org.apache.commons.jxpath.util.TypeUtils; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + /** * Test extension functions. * @@ -45,7 +47,8 @@ * @version $Revision$ $Date$ */ public class ExtensionFunctionTest extends JXPathTestCase { - private Functions functions; + + private Functions functions; private JXPathContext context; private TestBean testBean; private TypeConverter typeConverter; @@ -74,6 +77,16 @@ public void setUp() { typeConverter = TypeUtils.getTypeConverter(); } + public static ExpressionContext mockExpressionContext1(Object object){ + Object mockFieldVariableObject; + ExpressionContext mockInstance = mock(ExpressionContext.class); + mockFieldVariableObject=object; + when(mockInstance.getContextNodePointer()).thenAnswer((stubInvo) -> { + return NodePointer.newNodePointer(null,mockFieldVariableObject,Locale.getDefault()); + }); + return mockInstance; + } + public void tearDown() { TypeUtils.setTypeConverter(typeConverter); } @@ -82,72 +95,80 @@ public void testConstructorLookup() { Object[] args = new Object[] { new Integer(1), "x" }; Function func = functions.getFunction("test", "new", args); - assertEquals( + // Construct mock object + assertEquals( "test:new(1, x)", - func.invoke(new Context(null), args).toString(), + func.invoke(ExtensionFunctionTest.mockExpressionContext1(null), args).toString(), "foo=1; bar=x"); } public void testConstructorLookupWithExpressionContext() { Object[] args = new Object[] { "baz" }; Function func = functions.getFunction("test", "new", args); - assertEquals( + // Construct mock object + assertEquals( "test:new('baz')", - func.invoke(new Context(new Integer(1)), args).toString(), + func.invoke(ExtensionFunctionTest.mockExpressionContext1(new Integer(1)), args).toString(), "foo=1; bar=baz"); } public void testStaticMethodLookup() { Object[] args = new Object[] { new Integer(1), "x" }; Function func = functions.getFunction("test", "build", args); - assertEquals( + // Construct mock object + assertEquals( "test:build(1, x)", - func.invoke(new Context(null), args).toString(), + func.invoke(ExtensionFunctionTest.mockExpressionContext1(null), args).toString(), "foo=1; bar=x"); } public void testStaticMethodLookupWithConversion() { Object[] args = new Object[] { "7", new Integer(1)}; Function func = functions.getFunction("test", "build", args); - assertEquals( + // Construct mock object + assertEquals( "test:build('7', 1)", - func.invoke(new Context(null), args).toString(), + func.invoke(ExtensionFunctionTest.mockExpressionContext1(null), args).toString(), "foo=7; bar=1"); } public void testMethodLookup() { Object[] args = new Object[] { new TestFunctions()}; Function func = functions.getFunction("test", "getFoo", args); - assertEquals( + // Construct mock object + assertEquals( "test:getFoo($test, 1, x)", - func.invoke(new Context(null), args).toString(), + func.invoke(ExtensionFunctionTest.mockExpressionContext1(null), args).toString(), "0"); } public void testStaticMethodLookupWithExpressionContext() { Object[] args = new Object[0]; Function func = functions.getFunction("test", "path", args); - assertEquals( + // Construct mock object + assertEquals( "test:path()", - func.invoke(new Context(new Integer(1)), args), + func.invoke(ExtensionFunctionTest.mockExpressionContext1(new Integer(1)), args), "1"); } public void testMethodLookupWithExpressionContext() { Object[] args = new Object[] { new TestFunctions()}; Function func = functions.getFunction("test", "instancePath", args); - assertEquals( + // Construct mock object + assertEquals( "test:instancePath()", - func.invoke(new Context(new Integer(1)), args), + func.invoke(ExtensionFunctionTest.mockExpressionContext1(new Integer(1)), args), "1"); } public void testMethodLookupWithExpressionContextAndArgument() { Object[] args = new Object[] { new TestFunctions(), "*" }; Function func = functions.getFunction("test", "pathWithSuffix", args); - assertEquals( + // Construct mock object + assertEquals( "test:pathWithSuffix('*')", - func.invoke(new Context(new Integer(1)), args), + func.invoke(ExtensionFunctionTest.mockExpressionContext1(new Integer(1)), args), "1*"); } @@ -384,29 +405,4 @@ public void testBCNodeSetHack() { "test:isInstance(//strings, $NodeSet.class)", Boolean.TRUE); } - - private static class Context implements ExpressionContext { - private Object object; - - public Context(Object object) { - this.object = object; - } - - public Pointer getContextNodePointer() { - return NodePointer - .newNodePointer(null, object, Locale.getDefault()); - } - - public List getContextNodeList() { - return null; - } - - public JXPathContext getJXPathContext() { - return null; - } - - public int getPosition() { - return 0; - } - } } \ No newline at end of file