lukas-krecan / JsonUnit

Compare JSON in your Unit Tests
Apache License 2.0
894 stars 115 forks source link

Path.doStep() replaces '\' + any char with '.' #176

Closed nyeeil closed 5 years ago

nyeeil commented 5 years ago

Tested in 2.4.0, 2.6.0, master of junit-assertj. Reproduces every time.

https://github.com/lukas-krecan/JsonUnit/blob/8eeffd138405b7e1b05b1a188846e78030eced95/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/Path.java#L131

I believe this regex is intended to convent \. -> . to unescape the dot operator. Instead, this regex replaces \ + any char with .. This makes things like Windows paths as keys in objects impossible to use with .node(...) in the AssertJ integration.

Example (that I observed while debugging this issue):

    @Test
    public void testIdentifyPomFile_HasPom_Windows() {
        final String json = "{\"C:\\\\path\\\\file.ext\": {\"Status\": \"OK\"}}";
        final String pomPath = "C:\\path\\file.ext";

        System.out.println(json);
        assertThatJson(json)
            .isObject()
            .containsKey(pomPath);

        assertThatJson(json)
            .node(pomPath.replace(".", "\\."))
            .isPresent()   // <--- FAILS:
            .isObject()
            .contains(entry("Status", "OK"));
    }

Output of running the test:

 {"C:\\path\\file.ext": {"Status": "OK"}}

java.lang.AssertionError: Different value found in node "C:\path\file\.ext", expected: <node to be present> but was: <missing>.
Expected :<node to be present> 
Actual   :<missing>.
<Click to see difference>

    at net.javacrumbs.jsonunit.assertj.JsonAssert.failWithMessage(JsonAssert.java:119)
    at net.javacrumbs.jsonunit.assertj.JsonAssert.failOnDifference(JsonAssert.java:266)
    at net.javacrumbs.jsonunit.assertj.JsonAssert.isPresent(JsonAssert.java:261)
    at net.javacrumbs.jsonunit.assertj.JsonAssert.isPresent(JsonAssert.java:221)
    at com.company.MainTest.testIdentifyPomFile_HasPom_Windows(MainTest.java:143)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

I debugged the failure above and Path.doStep converts C:\path\file\.ext to: C:.ath.ile.ext

lukas-krecan commented 5 years ago

Thanks, it's indeed a bug https://github.com/lukas-krecan/JsonUnit/pull/177

lukas-krecan commented 5 years ago

Released as 2.6.1