From 078173e2dcf24124a4a2d7c273ab71ecd508e121 Mon Sep 17 00:00:00 2001 From: Christopher Tubbs Date: Mon, 28 Feb 2022 22:19:26 -0500 Subject: [PATCH] Clean up JsonFormatter and JsonFormatterTest Clean up unnecessary code changes and dramatically simplify prematurely merged PR #595 --- pom.xml | 5 -- .../code/formatter/json/JsonFormatter.java | 28 +------- .../formatter/json/JsonFormatterTest.java | 36 ++++++++++ .../json/MultipleJsonFormatterTest.java | 69 ------------------- .../json/NormalJsonFormatterTest.java | 69 ------------------- 5 files changed, 37 insertions(+), 170 deletions(-) delete mode 100644 src/test/java/net/revelc/code/formatter/json/MultipleJsonFormatterTest.java delete mode 100644 src/test/java/net/revelc/code/formatter/json/NormalJsonFormatterTest.java diff --git a/pom.xml b/pom.xml index 1d1145990..9e1523963 100644 --- a/pom.xml +++ b/pom.xml @@ -334,11 +334,6 @@ junit-jupiter-engine test - - org.junit.jupiter - junit-jupiter-params - test - diff --git a/src/main/java/net/revelc/code/formatter/json/JsonFormatter.java b/src/main/java/net/revelc/code/formatter/json/JsonFormatter.java index 04d749236..0a140ae49 100644 --- a/src/main/java/net/revelc/code/formatter/json/JsonFormatter.java +++ b/src/main/java/net/revelc/code/formatter/json/JsonFormatter.java @@ -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 options, final ConfigurationSource cfg) { super.initCfg(cfg); @@ -49,8 +47,6 @@ public void init(final Map 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) @@ -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 @@ -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; } } diff --git a/src/test/java/net/revelc/code/formatter/json/JsonFormatterTest.java b/src/test/java/net/revelc/code/formatter/json/JsonFormatterTest.java index a3b91e113..eb8877973 100644 --- a/src/test/java/net/revelc/code/formatter/json/JsonFormatterTest.java +++ b/src/test/java/net/revelc/code/formatter/json/JsonFormatterTest.java @@ -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; @@ -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); + } + } + } diff --git a/src/test/java/net/revelc/code/formatter/json/MultipleJsonFormatterTest.java b/src/test/java/net/revelc/code/formatter/json/MultipleJsonFormatterTest.java deleted file mode 100644 index aa0aa65f7..000000000 --- a/src/test/java/net/revelc/code/formatter/json/MultipleJsonFormatterTest.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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 - * - * https://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.revelc.code.formatter.json; - -import java.io.IOException; -import java.io.InputStream; -import java.util.Map; -import java.util.Objects; -import java.util.stream.Stream; - -import org.codehaus.plexus.util.IOUtil; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; - -import net.revelc.code.formatter.AbstractFormatterTest; -import net.revelc.code.formatter.LineEnding; - -public class MultipleJsonFormatterTest extends AbstractFormatterTest { - - private static String BEFORE_STRING; - - private static String AFTER_STRING; - - @BeforeAll - public static void init() throws IOException { - try (InputStream inputStream = NormalJsonFormatterTest.class - .getResourceAsStream("/multiple-json/before.json")) { - BEFORE_STRING = IOUtil.toString(Objects.requireNonNull(inputStream), "UTF-8"); - } - try (InputStream inputStream = NormalJsonFormatterTest.class.getResourceAsStream("/multiple-json/after.json")) { - AFTER_STRING = IOUtil.toString(Objects.requireNonNull(inputStream), "UTF-8"); - } - } - - public static Stream testParamsProvider() { - return Stream.of(Arguments.of(LineEnding.CRLF, "true"), Arguments.of(LineEnding.LF, "true"), - Arguments.of(LineEnding.CR, "true")); - } - - @ParameterizedTest - @MethodSource("testParamsProvider") - void test(LineEnding currentTestedLineEnding, String multipleJsonObjectFileAllowed) throws IOException { - final JsonFormatter jsonFormatter = new JsonFormatter(); - Assertions.assertFalse(jsonFormatter.isInitialized()); - jsonFormatter.init( - Map.of("lineending", currentTestedLineEnding.getChars(), "multipleJsonObjectFileAllowed", - multipleJsonObjectFileAllowed), - new TestConfigurationSource(AbstractFormatterTest.TEST_OUTPUT_PRIMARY_DIR)); - Assertions.assertTrue(jsonFormatter.isInitialized()); - String result = jsonFormatter.doFormat(BEFORE_STRING, currentTestedLineEnding); - Assertions.assertEquals(AFTER_STRING.replaceAll(LineEnding.CRLF.getChars(), LineEnding.LF.getChars()) - .replaceAll(LineEnding.LF.getChars(), currentTestedLineEnding.getChars()), result); - } - -} diff --git a/src/test/java/net/revelc/code/formatter/json/NormalJsonFormatterTest.java b/src/test/java/net/revelc/code/formatter/json/NormalJsonFormatterTest.java deleted file mode 100644 index dc7101c93..000000000 --- a/src/test/java/net/revelc/code/formatter/json/NormalJsonFormatterTest.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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 - * - * https://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.revelc.code.formatter.json; - -import java.io.IOException; -import java.io.InputStream; -import java.util.Map; -import java.util.Objects; -import java.util.stream.Stream; - -import org.codehaus.plexus.util.IOUtil; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; - -import net.revelc.code.formatter.AbstractFormatterTest; -import net.revelc.code.formatter.LineEnding; - -public class NormalJsonFormatterTest extends AbstractFormatterTest { - - private static String BEFORE_STRING; - - private static String AFTER_STRING; - - @BeforeAll - public static void init() throws IOException { - try (InputStream inputStream = NormalJsonFormatterTest.class.getResourceAsStream("/normal-json/before.json")) { - BEFORE_STRING = IOUtil.toString(Objects.requireNonNull(inputStream), "UTF-8"); - } - try (InputStream inputStream = NormalJsonFormatterTest.class.getResourceAsStream("/normal-json/after.json")) { - AFTER_STRING = IOUtil.toString(Objects.requireNonNull(inputStream), "UTF-8"); - } - } - - public static Stream testParamsProvider() { - return Stream.of(Arguments.of(LineEnding.CRLF, "true"), Arguments.of(LineEnding.LF, "true"), - Arguments.of(LineEnding.CR, "true"), Arguments.of(LineEnding.CRLF, "false"), - Arguments.of(LineEnding.LF, "false"), Arguments.of(LineEnding.CR, "false")); - } - - @ParameterizedTest - @MethodSource("testParamsProvider") - void test(LineEnding currentTestedLineEnding, String multipleJsonObjectFileAllowed) throws IOException { - final JsonFormatter jsonFormatter = new JsonFormatter(); - Assertions.assertFalse(jsonFormatter.isInitialized()); - jsonFormatter.init( - Map.of("lineending", currentTestedLineEnding.getChars(), "multipleJsonObjectFileAllowed", - multipleJsonObjectFileAllowed), - new TestConfigurationSource(AbstractFormatterTest.TEST_OUTPUT_PRIMARY_DIR)); - Assertions.assertTrue(jsonFormatter.isInitialized()); - String result = jsonFormatter.doFormat(BEFORE_STRING, currentTestedLineEnding); - Assertions.assertEquals(AFTER_STRING.replaceAll(LineEnding.CRLF.getChars(), LineEnding.LF.getChars()) - .replaceAll(LineEnding.LF.getChars(), currentTestedLineEnding.getChars()), result); - } - -}