Skip to content

Commit

Permalink
Trying to make it work with android-json
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-krecan committed Mar 27, 2017
1 parent 1c8f1a8 commit aadd3b3
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Node convertToNode(Object source, String label, boolean lenient) {
} else if (source instanceof Node) {
return (Node) source;
} else if (source instanceof String && ((String) source).trim().length() > 0) {
return readValue(new StringReader((String) source), label, lenient);
return readValue((String) source, label, lenient);
} else if (source instanceof Reader) {
return readValue((Reader) source, label, lenient);
} else {
Expand All @@ -40,5 +40,9 @@ public Node convertToNode(Object source, String label, boolean lenient) {

protected abstract Node readValue(Reader reader, String label, boolean lenient);

protected Node readValue(String source, String label, boolean lenient) {
return readValue(new StringReader(source), label, lenient);
}

protected abstract Node nullNode();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.IOException;
import java.io.Reader;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import static net.javacrumbs.jsonunit.core.internal.Utils.closeQuietly;
import static net.javacrumbs.jsonunit.core.internal.Utils.readAsString;

/**
* Deserializes node using org.json.JSONObject
Expand All @@ -43,11 +45,28 @@ protected Node nullNode() {
return newNode(null);
}

@Override
protected Node readValue(String source, String label, boolean lenient) {
try {
return newNode(new JSONTokener(source).nextValue());
} catch (JSONException e) {
throw new IllegalArgumentException("Can not parse " + label + " value.", e);
}
}

@Override
protected Node readValue(Reader value, String label, boolean lenient) {
try {
return newNode(new JSONTokener(value).nextValue());
} catch (JSONException e) {
throw new IllegalArgumentException("Can not parse " + label + " value.", e);
} catch (NoSuchMethodError e) {
// support for old json.org implementations
try {
return readValue(readAsString(value), label, lenient);
} catch (IOException e1) {
throw new IllegalArgumentException("Can not parse " + label + " value.", e);
}
} finally {
closeQuietly(value);
}
Expand Down Expand Up @@ -185,18 +204,23 @@ public Node element(int index) {

@Override
public Iterator<Node> arrayElements() {
final Iterator<Object> iterator = value.iterator();
return new Iterator<Node>() {
private int index = 0;

public boolean hasNext() {
return iterator.hasNext();
return index < value.length();
}

public Node next() {
return newNode(iterator.next());
try {
return newNode(value.get(index++));
} catch (JSONException e) {
throw new IllegalStateException(e);
}
}

public void remove() {
iterator.remove();
throw new UnsupportedOperationException();
}
};
}
Expand Down Expand Up @@ -231,7 +255,11 @@ public void remove() {

public KeyValue next() {
String fieldName = iterator.next();
return new KeyValue(fieldName, newNode(jsonObject.get(fieldName)));
try {
return new KeyValue(fieldName, newNode(jsonObject.get(fieldName)));
} catch (JSONException e) {
throw new IllegalStateException(e);
}
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@
* Resource reading utility
*/
class Utils {
static String readAsString(final Reader resourceReader) throws IOException {
StringBuilder builder = new StringBuilder();
char[] arr = new char[8 * 1024];
int numCharsRead;
while ((numCharsRead = resourceReader.read(arr, 0, arr.length)) != -1) {
builder.append(arr, 0, numCharsRead);
}
resourceReader.close();
return builder.toString();
}

static void closeQuietly(final Reader resourceReader) {
if (resourceReader != null) {
try {
Expand Down
1 change: 1 addition & 0 deletions tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<module>test-jackson2</module>
<module>test-gson</module>
<module>test-jsonorg</module>
<module>test-androidjson</module>
</modules>

<build>
Expand Down
26 changes: 26 additions & 0 deletions tests/test-androidjson/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>tests</artifactId>
<groupId>net.javacrumbs.json-unit</groupId>
<version>1.20.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>test-androidjson</artifactId>
<version>1.20.1-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>net.javacrumbs.json-unit</groupId>
<artifactId>test-base</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright 2009-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.javacrumbs.jsonunit.test.androidjson;

import net.javacrumbs.jsonunit.test.base.AbstractJsonAssertTest;
import net.javacrumbs.jsonunit.test.base.JsonTestUtils;
import org.junit.Test;

import java.io.IOException;

import static net.javacrumbs.jsonunit.JsonAssert.assertJsonEquals;
import static net.javacrumbs.jsonunit.test.base.JsonTestUtils.failIfNoException;
import static net.javacrumbs.jsonunit.test.base.JsonTestUtils.readByJsonOrg;
import static org.junit.Assert.assertEquals;

public class JsonOrgJsonAssertTest extends AbstractJsonAssertTest {
protected Object readValue(String value) {
return JsonTestUtils.readByJsonOrg(value);
}

@Test
public void shouldParseExpectedValueLeniently() {
// JSONObject lenient parsing works differently
}

@Test
public void shouldFailIfQuotationMarksMissingOnActualKeys() {
// it's lenient by default
}

@Test
public void testEqualsNodeFailJsonOrgArray() throws IOException {
try {
assertJsonEquals(readByJsonOrg("[1, 2]"), readByJsonOrg("[1, 2, 3]"));
failIfNoException();
} catch (AssertionError e) {
assertEquals("JSON documents are different:\nArray \"\" has different length. Expected 2, got 3.\n", e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright 2009-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.javacrumbs.jsonunit.test.androidjson;

import net.javacrumbs.jsonunit.test.base.AbstractJsonFluentAssertTest;
import net.javacrumbs.jsonunit.test.base.JsonTestUtils;
import org.junit.Test;

import static net.javacrumbs.jsonunit.fluent.JsonFluentAssert.assertThatJson;

public class JsonOrgJsonFluentAssertTest extends AbstractJsonFluentAssertTest {
@Override
protected Object readValue(String value) {
return JsonTestUtils.readByJsonOrg(value);
}

@Test
@Override
public void shouldAllowUnquotedKeysAndCommentInExpectedValue() {
assertThatJson("{\"test\":1}").isEqualTo("{test:1}");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright 2009-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.javacrumbs.jsonunit.test.androidjson;

import net.javacrumbs.jsonunit.test.base.AbstractJsonMatchersTest;
import net.javacrumbs.jsonunit.test.base.JsonTestUtils;

public class JsonOrgJsonMatchersTest extends AbstractJsonMatchersTest {
protected Object readValue(String value) {
return JsonTestUtils.readByJsonOrg(value);
}
}

0 comments on commit aadd3b3

Please sign in to comment.