Skip to content

Commit

Permalink
Clean up JsonFormatter and JsonFormatterTest
Browse files Browse the repository at this point in the history
Clean up unnecessary code changes and dramatically simplify prematurely
merged PR #595
  • Loading branch information
ctubbsii committed Mar 1, 2022
1 parent 445ffab commit 078173e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 170 deletions.
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,6 @@
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<!-- TODO: Allow pull from snapshots while on jsoup snapshot, remove once finalized -->
Expand Down
28 changes: 1 addition & 27 deletions src/main/java/net/revelc/code/formatter/json/JsonFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public class JsonFormatter extends AbstractCacheableFormatter implements Formatt
/** The formatter. */
private ObjectMapper formatter;

private boolean multipleJsonObjectFileAllowed;

@Override
public void init(final Map<String, String> options, final ConfigurationSource cfg) {
super.initCfg(cfg);
Expand All @@ -49,8 +47,6 @@ public void init(final Map<String, String> options, final ConfigurationSource cf
final var lineEnding = options.getOrDefault("lineending", System.lineSeparator());
final var spaceBeforeSeparator = Boolean.parseBoolean(options.getOrDefault("spaceBeforeSeparator", "true"));
final var useAlphabeticalOrder = Boolean.parseBoolean(options.getOrDefault("alphabeticalOrder", "false"));
this.multipleJsonObjectFileAllowed = Boolean
.parseBoolean(options.getOrDefault("multipleJsonObjectFileAllowed", "true"));
this.formatter = new ObjectMapper();

// Setup a pretty printer with an indenter (indenter has 4 spaces in this case)
Expand Down Expand Up @@ -81,25 +77,6 @@ public DefaultPrettyPrinter withSeparators(final Separators separators) {

@Override
protected String doFormat(final String code, final LineEnding ending) throws IOException {
if (this.multipleJsonObjectFileAllowed) {
return doFormatWithMultipleJsonFileAllowed(code, ending);
}
return doFormatWithMultipleJsonFileForbidden(code, ending);
}

private String doFormatWithMultipleJsonFileForbidden(final String code, final LineEnding ending)
throws IOException {
// note: line ending set in init for this usecase
final var json = this.formatter.readValue(code, Object.class);
var formattedCode = this.formatter.writer().writeValueAsString(json);
formattedCode = formattedCode + ending.getChars();
if (code.equals(formattedCode)) {
return null;
}
return formattedCode;
}

private String doFormatWithMultipleJsonFileAllowed(final String code, final LineEnding ending) throws IOException {
try (StringWriter stringWriter = new StringWriter()) {
JsonParser jsonParser = this.formatter.createParser(code);
// note: line ending set in init for this usecase
Expand All @@ -110,10 +87,7 @@ private String doFormatWithMultipleJsonFileAllowed(final String code, final Line
stringWriter.write(ending.getChars());
}
String formattedCode = stringWriter.toString();
if (code.equals(formattedCode)) {
return null;
}
return formattedCode;
return code.equals(formattedCode) ? null : formattedCode;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
*/
package net.revelc.code.formatter.json;

import java.io.IOException;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import org.codehaus.plexus.util.IOUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -81,4 +85,36 @@ void testDoFormatFileWithConfig() throws Exception {
this.twoPassTest(jsonFormattingOptions, new JsonFormatter(), "someFile.json", expectedHash, lineEnding);
}

@Test
public void testMultipleJson() throws IOException {
testFormattingObjects("/multiple-json");
}

@Test
public void testNormalJson() throws IOException {
testFormattingObjects("/normal-json");
}

private void testFormattingObjects(String testpath) throws IOException {
String originalJson;
String expectedFormattedJson;
try (var in = getClass().getResourceAsStream(testpath + "/before.json")) {
originalJson = IOUtil.toString(Objects.requireNonNull(in), "UTF-8");
}
try (var in = getClass().getResourceAsStream(testpath + "/after.json")) {
expectedFormattedJson = IOUtil.toString(Objects.requireNonNull(in), "UTF-8");
}
for (LineEnding currentTestedLineEnding : EnumSet.of(LineEnding.CRLF, LineEnding.LF, LineEnding.CR)) {
final var jsonFormatter = new JsonFormatter();
Assertions.assertFalse(jsonFormatter.isInitialized());
jsonFormatter.init(Map.of("lineending", currentTestedLineEnding.getChars()),
new TestConfigurationSource(AbstractFormatterTest.TEST_OUTPUT_PRIMARY_DIR));
Assertions.assertTrue(jsonFormatter.isInitialized());
String result = jsonFormatter.doFormat(originalJson, currentTestedLineEnding);
Assertions
.assertEquals(expectedFormattedJson.replaceAll(LineEnding.CRLF.getChars(), LineEnding.LF.getChars())
.replaceAll(LineEnding.LF.getChars(), currentTestedLineEnding.getChars()), result);
}
}

}

This file was deleted.

This file was deleted.

0 comments on commit 078173e

Please sign in to comment.