From 065104a4853d4e1f45253d1a94e190541ca5a573 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 21 Oct 2014 12:37:56 +0200 Subject: [PATCH] Restored compatibility with JsonPath 0.9.x Issue: SPR-12299 --- .../test/util/JsonPathExpectationsHelper.java | 55 +++++++++++++++---- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java b/spring-test/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java index 89476cc3cb..827a3287ae 100644 --- a/spring-test/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java +++ b/spring-test/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,8 @@ package org.springframework.test.util; +import java.lang.reflect.Array; +import java.lang.reflect.Method; import java.text.ParseException; import java.util.List; @@ -23,36 +25,64 @@ import com.jayway.jsonpath.InvalidPathException; import com.jayway.jsonpath.JsonPath; import org.hamcrest.Matcher; +import org.springframework.util.Assert; +import org.springframework.util.ReflectionUtils; + import static org.springframework.test.util.AssertionErrors.*; import static org.springframework.test.util.MatcherAssertionErrors.*; /** - * A helper class for applying assertions via JSONPath expressions. + * A helper class for applying assertions via JSON path expressions. + * + *

Based on the JsonPath + * project: requiring version 0.9+, with 1.1+ strongly recommended. * * @author Rossen Stoyanchev + * @author Juergen Hoeller * @since 3.2 */ public class JsonPathExpectationsHelper { + private static Method compileMethod; + + private static Object emptyFilters; + + static { + // Reflective bridging between JsonPath 0.9.x and 1.x + for (Method candidate : JsonPath.class.getMethods()) { + if (candidate.getName().equals("compile")) { + Class[] paramTypes = candidate.getParameterTypes(); + if (paramTypes.length == 2 && paramTypes[0].equals(String.class) && paramTypes[1].isArray()) { + compileMethod = candidate; + emptyFilters = Array.newInstance(paramTypes[1].getComponentType(), 0); + break; + } + } + } + Assert.state(compileMethod != null, "Unexpected JsonPath API - no compile(String, ...) method found"); + } + + private final String expression; private final JsonPath jsonPath; /** - * Class constructor. - * - * @param expression the JSONPath expression - * @param args arguments to parameterize the JSONPath expression with using the + * Construct a new JsonPathExpectationsHelper. + * @param expression the JsonPath expression + * @param args arguments to parameterize the JSON path expression with * formatting specifiers defined in {@link String#format(String, Object...)} */ - public JsonPathExpectationsHelper(String expression, Object ... args) { + public JsonPathExpectationsHelper(String expression, Object... args) { this.expression = String.format(expression, args); - this.jsonPath = JsonPath.compile(this.expression); + this.jsonPath = (JsonPath) ReflectionUtils.invokeMethod( + compileMethod, null, this.expression, emptyFilters); } + /** - * Evaluate the JSONPath and assert the resulting value with the given {@code Matcher}. + * Evaluate the JSON path and assert the resulting value with the given {@code Matcher}. * @param content the response content * @param matcher the matcher to assert on the resulting json path */ @@ -79,14 +109,14 @@ public class JsonPathExpectationsHelper { } /** - * Apply the JSONPath and assert the resulting value. + * Apply the JSON path and assert the resulting value. */ public void assertValue(String responseContent, Object expectedValue) throws ParseException { Object actualValue = evaluateJsonPath(responseContent); if ((actualValue instanceof List) && !(expectedValue instanceof List)) { @SuppressWarnings("rawtypes") List actualValueList = (List) actualValue; - if (actualValueList.size() == 0) { + if (actualValueList.isEmpty()) { fail("No matching value for JSON path \"" + this.expression + "\""); } if (actualValueList.size() != 1) { @@ -102,7 +132,7 @@ public class JsonPathExpectationsHelper { } /** - * Apply the JSONPath and assert the resulting value is an array. + * Apply the JSON path and assert the resulting value is an array. */ public void assertValueIsArray(String responseContent) throws ParseException { Object actualValue = evaluateJsonPath(responseContent); @@ -142,4 +172,5 @@ public class JsonPathExpectationsHelper { assertTrue(reason, value == null); } } + }