From 630f0c15b344535aaeff3871a7d76bffed2b30a9 Mon Sep 17 00:00:00 2001 From: Alexey Makeyev Date: Mon, 12 Oct 2015 19:02:25 +0300 Subject: [PATCH] fix for PathToken.isUpstreamDefinite --- .../jsonpath/internal/token/PathToken.java | 6 +-- .../com/jayway/jsonpath/PathTokenTest.java | 53 +++++++++++++++++++ 2 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 json-path/src/test/java/com/jayway/jsonpath/PathTokenTest.java diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/token/PathToken.java b/json-path/src/main/java/com/jayway/jsonpath/internal/token/PathToken.java index 54339fd6..f40c8e7b 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/token/PathToken.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/token/PathToken.java @@ -30,7 +30,7 @@ public abstract class PathToken { private Boolean definite = null; private Boolean upstreamDefinite = null; - PathToken appendTailToken(PathToken next) { + public PathToken appendTailToken(PathToken next) { this.next = next; this.next.prev = this; return next; @@ -148,13 +148,13 @@ public abstract class PathToken { return prev == null; } - boolean isUpstreamDefinite(){ + public boolean isUpstreamDefinite(){ if(upstreamDefinite != null){ return upstreamDefinite.booleanValue(); } boolean isUpstreamDefinite = isTokenDefinite(); if (isUpstreamDefinite && !isRoot()) { - isUpstreamDefinite = prev.isPathDefinite(); + isUpstreamDefinite = prev.isUpstreamDefinite(); } upstreamDefinite = isUpstreamDefinite; return isUpstreamDefinite; diff --git a/json-path/src/test/java/com/jayway/jsonpath/PathTokenTest.java b/json-path/src/test/java/com/jayway/jsonpath/PathTokenTest.java new file mode 100644 index 00000000..c933505a --- /dev/null +++ b/json-path/src/test/java/com/jayway/jsonpath/PathTokenTest.java @@ -0,0 +1,53 @@ +package com.jayway.jsonpath; + +import com.jayway.jsonpath.internal.token.PathToken; +import com.jayway.jsonpath.internal.token.PropertyPathToken; +import com.jayway.jsonpath.internal.token.ScanPathToken; +import com.jayway.jsonpath.internal.token.WildcardPathToken; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; + +import java.util.Arrays; + +public class PathTokenTest extends BaseTest { + + @Test + public void is_upstream_definite_in_simple_case() { + assertThat(makePathReturningTail(makePPT("foo")).isUpstreamDefinite()).isTrue(); + + assertThat(makePathReturningTail(makePPT("foo"), makePPT("bar")).isUpstreamDefinite()).isTrue(); + + assertThat(makePathReturningTail(makePPT("foo", "foo2"), makePPT("bar")).isUpstreamDefinite()).isFalse(); + + assertThat(makePathReturningTail(new WildcardPathToken(), makePPT("bar")).isUpstreamDefinite()).isFalse(); + + assertThat(makePathReturningTail(new ScanPathToken(), makePPT("bar")).isUpstreamDefinite()).isFalse(); + } + + @Test + public void is_upstream_definite_in_complex_case() { + assertThat(makePathReturningTail(makePPT("foo"), makePPT("bar"), makePPT("baz")).isUpstreamDefinite()).isTrue(); + + assertThat(makePathReturningTail(makePPT("foo"), new WildcardPathToken()).isUpstreamDefinite()).isFalse(); + + assertThat(makePathReturningTail(new WildcardPathToken(), makePPT("bar"), makePPT("baz")).isUpstreamDefinite()).isFalse(); + } + + + private PathToken makePPT(final String ... properties) { + return new PropertyPathToken(Arrays.asList(properties)); + } + + private PathToken makePathReturningTail(final PathToken ... tokens) { + PathToken last = null; + for (final PathToken token : tokens) { + if (last != null) { + last.appendTailToken(token); + } + last = token; + } + return last; + } +}