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

Refactor JSONPointerTest #631

Merged
merged 3 commits into from
Oct 14, 2021
Merged
Changes from all commits
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
48 changes: 23 additions & 25 deletions src/test/java/org/json/junit/JSONPointerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ of this software and associated documentation files (the "Software"), to deal
*/

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand All @@ -41,6 +40,10 @@ of this software and associated documentation files (the "Software"), to deal
public class JSONPointerTest {

private static final JSONObject document;
private static final String EXPECTED_COMPLETE_DOCUMENT = "{\"\":0,\" \":7,\"g|h\":4,\"c%d\":2,\"k\\\"l\":6,\"a/b\":1,\"i\\\\j\":5," +
"\"obj\":{\"\":{\"\":\"empty key of an object with an empty key\",\"subKey\":\"Some other value\"}," +
"\"other~key\":{\"another/key\":[\"val\"]},\"key\":\"value\"},\"foo\":[\"bar\",\"baz\"],\"e^f\":3," +
"\"m~n\":8}";

static {
@SuppressWarnings("resource")
Expand All @@ -57,7 +60,7 @@ private Object query(String pointer) {

@Test
public void emptyPointer() {
assertSame(document, query(""));
assertTrue(new JSONObject(EXPECTED_COMPLETE_DOCUMENT).similar(query("")));
}

@SuppressWarnings("unused")
Expand All @@ -68,12 +71,12 @@ public void nullPointer() {

@Test
public void objectPropertyQuery() {
assertSame(document.get("foo"), query("/foo"));
assertEquals("[\"bar\",\"baz\"]", query("/foo").toString());
}

@Test
public void arrayIndexQuery() {
assertSame(document.getJSONArray("foo").get(0), query("/foo/0"));
assertEquals("bar", query("/foo/0"));
}

@Test(expected = JSONPointerException.class)
Expand All @@ -83,38 +86,33 @@ public void stringPropOfArrayFailure() {

@Test
public void queryByEmptyKey() {
assertSame(document.get(""), query("/"));
assertEquals(0, query("/"));
}

@Test
public void queryByEmptyKeySubObject() {
assertSame(document.getJSONObject("obj").getJSONObject(""), query("/obj/"));
assertEquals( "{\"\":\"empty key of an object with an empty key\",\"subKey\":\"Some" +
" other value\"}", query("/obj/").toString());
}

@Test
public void queryByEmptyKeySubObjectSubOject() {
assertSame(
document.getJSONObject("obj").getJSONObject("").get(""),
query("/obj//")
);
assertEquals("empty key of an object with an empty key", query("/obj//"));
}

@Test
public void queryByEmptyKeySubObjectValue() {
assertSame(
document.getJSONObject("obj").getJSONObject("").get("subKey"),
query("/obj//subKey")
);
assertEquals("Some other value", query("/obj//subKey"));
}

@Test
public void slashEscaping() {
assertSame(document.get("a/b"), query("/a~1b"));
assertEquals(1, query("/a~1b"));
}

@Test
public void tildeEscaping() {
assertSame(document.get("m~n"), query("/m~0n"));
assertEquals(8, query("/m~0n"));
}

/**
Expand All @@ -124,7 +122,7 @@ public void tildeEscaping() {
*/
@Test
public void backslashHandling() {
assertSame(document.get("i\\j"), query("/i\\j"));
assertEquals(5, query("/i\\j"));
}

/**
Expand All @@ -134,30 +132,30 @@ public void backslashHandling() {
*/
@Test
public void quotationHandling() {
assertSame(document.get("k\"l"), query("/k\"l"));
assertEquals(6, query("/k\"l"));
}

@Test
public void whitespaceKey() {
assertSame(document.get(" "), query("/ "));
assertEquals(7, query("/ "));
}

@Test
public void uriFragmentNotation() {
assertSame(document.get("foo"), query("#/foo"));
assertEquals("[\"bar\",\"baz\"]", query("#/foo").toString());
}

@Test
public void uriFragmentNotationRoot() {
assertSame(document, query("#"));
assertTrue(new JSONObject(EXPECTED_COMPLETE_DOCUMENT).similar(query("#")));
}

@Test
public void uriFragmentPercentHandling() {
assertSame(document.get("c%d"), query("#/c%25d"));
assertSame(document.get("e^f"), query("#/e%5Ef"));
assertSame(document.get("g|h"), query("#/g%7Ch"));
assertSame(document.get("m~n"), query("#/m~0n"));
assertEquals(2, query("#/c%25d"));
assertEquals(3, query("#/e%5Ef"));
assertEquals(4, query("#/g%7Ch"));
assertEquals(8, query("#/m~0n"));
}

@SuppressWarnings("unused")
Expand Down