Skip to content

Commit

Permalink
Add JSON resources with various kinds of indentations
Browse files Browse the repository at this point in the history
  • Loading branch information
leadpony committed Aug 4, 2019
1 parent cc75deb commit 00401de
Show file tree
Hide file tree
Showing 46 changed files with 126,070 additions and 20 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.2.0 - 2019-08-04
### Added
- JSON examples with various kinds of indentations.

## 1.1.0 - 2019-07-28
### Added
- Tests for `getConfigInUse()` method.
- Tests for `JsonParser.skipArray()` and `JsonParser.skipObject()` against unclosed array and object.
- Tests for `JsonParser.skipArray()` and `JsonParser.skipObject()` against JSON array and object not properly closed.

## 1.0.0 - 2019-07-23
### Added
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ The jar-packaged artifact of this test suite is available from [JitPack] reposit
<dependency>
<groupId>org.leadpony</groupId>
<artifactId>jsonp-test-suite</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -49,6 +49,7 @@ The test suite can be run by using Maven Surefire Plugin.
<dependenciesToScan>
<dependency>org.leadpony:jsonp-test-suite</dependency>
</dependenciesToScan>
<excludedGroups>ambiguous</excludedGroups>
<excludes>
<exclude />
</excludes>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.leadpony</groupId>
<artifactId>jsonp-test-suite</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>
<packaging>jar</packaging>

<name>JSON-P Test Suite</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void writeShouldGeneratePrettyPrintedJson(JsonResource test) {
}

String actual = writer.toString().trim();
String expected = test.getJsonAsString();
String expected = test.getJsonIndentedWithSpacesAsString(4);
assertThat(actual).isEqualTo(expected);
}

Expand Down
85 changes: 70 additions & 15 deletions src/main/java/org/leadpony/jsonp/testsuite/tests/JsonResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*
* @author leadpony
*/
enum JsonResource {
public enum JsonResource {
GLOSSARY("/org/json/example/glossary.json", ValueType.OBJECT),
MENU("/org/json/example/menu.json", ValueType.OBJECT),
MENU2("/org/json/example/menu2.json", ValueType.OBJECT),
Expand All @@ -58,11 +58,11 @@ enum JsonResource {
this.charset = charset;
}

InputStream openStream() {
public InputStream openStream() {
return getClass().getResourceAsStream(name);
}

Reader createReader() {
public Reader createReader() {
return new InputStreamReader(openStream(), getCharset());
}

Expand All @@ -71,23 +71,43 @@ Reader createReader() {
*
* @return the type of this JSON value.
*/
ValueType getType() {
public ValueType getType() {
return type;
}

Charset getCharset() {
/**
* Returns the character set of this JSON resource.
*
* @return the character set.
*/
public Charset getCharset() {
return charset;
}

boolean isArray() {
/**
* Checks if this JSON is an array or not.
*
* @return {@code true} if this JSON is an array, {@code false} otherwise.
*/
public boolean isArray() {
return type == ValueType.ARRAY;
}

boolean isObject() {
/**
* Checks if this JSON is an object or not.
*
* @return {@code true} if this JSON is an object, {@code false} otherwise.
*/
public boolean isObject() {
return type == ValueType.OBJECT;
}

boolean isStructure() {
/**
* Checks if this JSON is an array or object.
*
* @return {@code true} if this JSON is an array or object, {@code false} otherwise.
*/
public boolean isStructure() {
return type == ValueType.ARRAY || type == ValueType.OBJECT;
}

Expand All @@ -96,33 +116,68 @@ boolean isStructure() {
*
* @return the JSON as a string.
*/
String getJsonAsString() {
public String getJsonAsString() {
return getResourceAsString(this.name);
}

/**
* Returns the indented version of the JSON as a string.
* Tab is used for every indentation.
*
* @param spaces the number of spaces used as an indentation.
* @return the indented JSON as a string.
*/
public String getJsonIndentedWithTabAsString() {
String name = new StringBuilder(getNameWithoutExtension())
.append(".tab.json")
.toString();
return getResourceAsString(name);
}

/**
* Returns the indented version of the JSON as a string.
* Spaces are used for every indentation.
*
* @param spaces the number of spaces used as an indentation.
* @return the indented JSON as a string.
*/
public String getJsonIndentedWithSpacesAsString(int spaces) {
String name = new StringBuilder(getNameWithoutExtension())
.append(".sp")
.append(spaces)
.append(".json")
.toString();
return getResourceAsString(name);
}

/**
* Returns the minified version of the JSON as a string.
*
* @return the minified JSON as a string.
*/
String getMinifiedJsonAsString() {
String filename = this.name.substring(0, this.name.lastIndexOf('.'));
String name = filename.concat(".min.json");
public String getMinifiedJsonAsString() {
String name = new StringBuilder(getNameWithoutExtension())
.append(".min.json")
.toString();
return getResourceAsString(name);
}

static Stream<JsonResource> getArraysAsStream() {
public static Stream<JsonResource> getArraysAsStream() {
return Stream.of(values()).filter(JsonResource::isArray);
}

static Stream<JsonResource> getObjectsAsStream() {
public static Stream<JsonResource> getObjectsAsStream() {
return Stream.of(values()).filter(JsonResource::isObject);
}

static Stream<JsonResource> getStructuresAsStream() {
public static Stream<JsonResource> getStructuresAsStream() {
return Stream.of(values()).filter(JsonResource::isStructure);
}

private String getNameWithoutExtension() {
return this.name.substring(0, this.name.lastIndexOf('.'));
}

private String getResourceAsString(String name) {
InputStream in = getClass().getResourceAsStream(name);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void writeShouldGeneratePrettyPrintedJson(JsonResource test) {
}

String actual = writer.toString().trim();
String expected = test.getJsonAsString();
String expected = test.getJsonIndentedWithSpacesAsString(4);
assertThat(actual).isEqualTo(expected);
}

Expand Down
Loading

0 comments on commit 00401de

Please sign in to comment.