From 717858a12b414dfe7e928bf8d7e1b7504264e007 Mon Sep 17 00:00:00 2001 From: Michael Dowling Date: Tue, 22 Sep 2020 17:04:53 -0700 Subject: [PATCH] Fix various Windows issues with tests * Fix tests with resources to work on Windows. * Fix line-ending sensitive tests. --- .../CloudFormationSubstitutionTest.java | 4 +- .../build/model/SmithyBuildConfigTest.java | 7 ++- .../ConfigurableSmithyBuildPluginTest.java | 2 +- .../build/plugins/SourcesPluginTest.java | 51 +++++++++++-------- .../smithy/cli/BuildParameterBuilderTest.java | 22 ++++---- .../smithy/cli/commands/AstCommandTest.java | 10 ++-- .../smithy/cli/commands/BuildCommandTest.java | 9 ++-- .../cli/commands/SelectCommandTest.java | 9 ++-- .../cli/commands/ValidateCommandTest.java | 14 ++--- .../model/node/DefaultNodeWriterTest.java | 40 +++++++-------- .../smithy/model/node/NodeDiffTest.java | 4 +- .../smithy/model/node/NodeMapperTest.java | 14 +++-- .../amazon/smithy/model/node/NodeTest.java | 2 +- .../shapes/SmithyIdlModelSerializerTest.java | 11 ++-- ...ontextualValidationEventFormatterTest.java | 4 ++ .../software/amazon/smithy/utils/IoUtils.java | 24 ++++++--- .../amazon/smithy/utils/IoUtilsTest.java | 8 ++- 17 files changed, 138 insertions(+), 97 deletions(-) diff --git a/smithy-aws-apigateway-openapi/src/test/java/software/amazon/smithy/aws/apigateway/openapi/CloudFormationSubstitutionTest.java b/smithy-aws-apigateway-openapi/src/test/java/software/amazon/smithy/aws/apigateway/openapi/CloudFormationSubstitutionTest.java index 46d7b064994..0c720325d3c 100644 --- a/smithy-aws-apigateway-openapi/src/test/java/software/amazon/smithy/aws/apigateway/openapi/CloudFormationSubstitutionTest.java +++ b/smithy-aws-apigateway-openapi/src/test/java/software/amazon/smithy/aws/apigateway/openapi/CloudFormationSubstitutionTest.java @@ -34,7 +34,7 @@ public void performsSubstitutionsByDefault() { .unwrap(); ObjectNode expected = Node.parse( - IoUtils.readUtf8File(getClass().getResource("substitution-performed.json").getPath())) + IoUtils.readUtf8Resource(getClass(), "substitution-performed.json")) .expectObjectNode(); OpenApiConfig config = new OpenApiConfig(); config.setService(ShapeId.from("example.smithy#MyService")); @@ -55,7 +55,7 @@ public void pluginCanBeDisabled() { .unwrap(); ObjectNode expected = Node.parse( - IoUtils.readUtf8File(getClass().getResource("substitution-not-performed.json").getPath())) + IoUtils.readUtf8Resource(getClass(), "substitution-not-performed.json")) .expectObjectNode(); OpenApiConfig config = new OpenApiConfig(); diff --git a/smithy-build/src/test/java/software/amazon/smithy/build/model/SmithyBuildConfigTest.java b/smithy-build/src/test/java/software/amazon/smithy/build/model/SmithyBuildConfigTest.java index f286a550f87..a4474b2ad0c 100644 --- a/smithy-build/src/test/java/software/amazon/smithy/build/model/SmithyBuildConfigTest.java +++ b/smithy-build/src/test/java/software/amazon/smithy/build/model/SmithyBuildConfigTest.java @@ -25,6 +25,7 @@ import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.notNullValue; +import java.net.URISyntaxException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Optional; @@ -149,6 +150,10 @@ public void rewritesArgsArrayToUnderscoreArgs() { } private String getResourcePath(String name) { - return SmithyBuildTest.class.getResource(name).getPath(); + try { + return Paths.get(SmithyBuildTest.class.getResource(name).toURI()).toString(); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } } } diff --git a/smithy-build/src/test/java/software/amazon/smithy/build/plugins/ConfigurableSmithyBuildPluginTest.java b/smithy-build/src/test/java/software/amazon/smithy/build/plugins/ConfigurableSmithyBuildPluginTest.java index 34f0b7f690a..b3801c0971e 100644 --- a/smithy-build/src/test/java/software/amazon/smithy/build/plugins/ConfigurableSmithyBuildPluginTest.java +++ b/smithy-build/src/test/java/software/amazon/smithy/build/plugins/ConfigurableSmithyBuildPluginTest.java @@ -14,7 +14,7 @@ public class ConfigurableSmithyBuildPluginTest { @Test public void loadsConfigurationClass() { Model model = Model.assembler() - .addImport(getClass().getResource("sources/a.smithy").getPath()) + .addImport(getClass().getResource("sources/a.smithy")) .assemble() .unwrap(); MockManifest manifest = new MockManifest(); diff --git a/smithy-build/src/test/java/software/amazon/smithy/build/plugins/SourcesPluginTest.java b/smithy-build/src/test/java/software/amazon/smithy/build/plugins/SourcesPluginTest.java index 906aef3b8b2..03f0232e7e7 100644 --- a/smithy-build/src/test/java/software/amazon/smithy/build/plugins/SourcesPluginTest.java +++ b/smithy-build/src/test/java/software/amazon/smithy/build/plugins/SourcesPluginTest.java @@ -5,6 +5,7 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.not; +import java.net.URISyntaxException; import java.nio.file.Paths; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -18,10 +19,10 @@ public class SourcesPluginTest { @Test - public void copiesFilesForSourceProjection() { + public void copiesFilesForSourceProjection() throws URISyntaxException { Model model = Model.assembler() - .addImport(getClass().getResource("sources/a.smithy").getPath()) - .addImport(getClass().getResource("sources/b.smithy").getPath()) + .addImport(getClass().getResource("sources/a.smithy")) + .addImport(getClass().getResource("sources/b.smithy")) .addImport(getClass().getResource("sources/c/c.json")) .addImport(getClass().getResource("notsources/d.smithy")) .assemble() @@ -31,10 +32,12 @@ public void copiesFilesForSourceProjection() { .fileManifest(manifest) .model(model) .originalModel(model) - .sources(ListUtils.of(Paths.get(getClass().getResource("sources/a.smithy").getPath()).getParent())) + .sources(ListUtils.of(Paths.get(getClass().getResource("sources/a.smithy").toURI()).getParent())) .build(); new SourcesPlugin().execute(context); String manifestString = manifest.getFileString("manifest").get(); + // Normalize for Windows. + manifestString = manifestString.replace("\\", "/"); assertThat(manifestString, containsString("a.smithy\n")); assertThat(manifestString, containsString("b.smithy\n")); @@ -46,9 +49,9 @@ public void copiesFilesForSourceProjection() { } @Test - public void copiesModelFromJarWithSourceProjection() { + public void copiesModelFromJarWithSourceProjection() throws URISyntaxException { Model model = Model.assembler() - .addImport(getClass().getResource("sources/jar-import.jar").getPath()) + .addImport(getClass().getResource("sources/jar-import.jar")) .addImport(getClass().getResource("notsources/d.smithy")) .assemble() .unwrap(); @@ -57,10 +60,12 @@ public void copiesModelFromJarWithSourceProjection() { .fileManifest(manifest) .model(model) .originalModel(model) - .sources(ListUtils.of(Paths.get(getClass().getResource("sources/jar-import.jar").getPath()))) + .sources(ListUtils.of(Paths.get(getClass().getResource("sources/jar-import.jar").toURI()))) .build(); new SourcesPlugin().execute(context); String manifestString = manifest.getFileString("manifest").get(); + // Normalize for Windows. + manifestString = manifestString.replace("\\", "/"); assertThat(manifestString, containsString("jar-import/a.smithy\n")); assertThat(manifestString, containsString("jar-import/b/b.smithy\n")); @@ -72,9 +77,9 @@ public void copiesModelFromJarWithSourceProjection() { } @Test - public void copiesModelFromJarWithNonSourceProjection() { + public void copiesModelFromJarWithNonSourceProjection() throws URISyntaxException { Model model = Model.assembler() - .addImport(getClass().getResource("sources/jar-import.jar").getPath()) + .addImport(getClass().getResource("sources/jar-import.jar")) .addImport(getClass().getResource("notsources/d.smithy")) .assemble() .unwrap(); @@ -85,10 +90,12 @@ public void copiesModelFromJarWithNonSourceProjection() { .projection("foo", projection) .model(model) .originalModel(model) - .sources(ListUtils.of(Paths.get(getClass().getResource("sources/jar-import.jar").getPath()))) + .sources(ListUtils.of(Paths.get(getClass().getResource("sources/jar-import.jar").toURI()))) .build(); new SourcesPlugin().execute(context); String manifestString = manifest.getFileString("manifest").get(); + // Normalize for Windows. + manifestString = manifestString.replace("\\", "/"); assertThat(manifestString, containsString("model.json")); assertThat(manifestString, not(containsString("jar-import"))); @@ -98,11 +105,11 @@ public void copiesModelFromJarWithNonSourceProjection() { } @Test - public void copiesOnlyFilesFromSourcesForProjection() { + public void copiesOnlyFilesFromSourcesForProjection() throws URISyntaxException { Model model = Model.assembler() - .addImport(getClass().getResource("sources/a.smithy").getPath()) - .addImport(getClass().getResource("sources/b.smithy").getPath()) - .addImport(getClass().getResource("sources/c/c.json").getPath()) + .addImport(getClass().getResource("sources/a.smithy")) + .addImport(getClass().getResource("sources/b.smithy")) + .addImport(getClass().getResource("sources/c/c.json")) .addImport(getClass().getResource("notsources/d.smithy")) .assemble() .unwrap(); @@ -113,7 +120,7 @@ public void copiesOnlyFilesFromSourcesForProjection() { .fileManifest(manifest) .model(model) .originalModel(model) - .sources(ListUtils.of(Paths.get(getClass().getResource("sources/a.smithy").getPath()).getParent())) + .sources(ListUtils.of(Paths.get(getClass().getResource("sources/a.smithy").toURI()).getParent())) .build(); new SourcesPlugin().execute(context); String manifestString = manifest.getFileString("manifest").get(); @@ -135,7 +142,7 @@ public void copiesOnlyFilesFromSourcesForProjection() { } @Test - public void treatsNewlyAddedShapesAsNewSources() { + public void treatsNewlyAddedShapesAsNewSources() throws URISyntaxException { Model originalModel = Model.assembler().assemble().unwrap(); Model newModel = Model.assembler() .addShape(StringShape.builder().id("a.b#MyString").build()) @@ -148,7 +155,7 @@ public void treatsNewlyAddedShapesAsNewSources() { .fileManifest(manifest) .originalModel(originalModel) .model(newModel) - .sources(ListUtils.of(Paths.get(getClass().getResource("sources/a.smithy").getPath()).getParent())) + .sources(ListUtils.of(Paths.get(getClass().getResource("sources/a.smithy").toURI()).getParent())) .build(); new SourcesPlugin().execute(context); String modelString = manifest.getFileString("model.json").get(); @@ -157,10 +164,10 @@ public void treatsNewlyAddedShapesAsNewSources() { } @Test - public void doesNotAllowConflicts() { + public void doesNotAllowConflicts() throws URISyntaxException { Model model = Model.assembler() - .addImport(getClass().getResource("sources/a.smithy").getPath()) - .addImport(getClass().getResource("conflicting/a.smithy").getPath()) + .addImport(getClass().getResource("sources/a.smithy")) + .addImport(getClass().getResource("conflicting/a.smithy")) .assemble() .unwrap(); MockManifest manifest = new MockManifest(); @@ -169,8 +176,8 @@ public void doesNotAllowConflicts() { .model(model) .originalModel(model) .sources(ListUtils.of( - Paths.get(getClass().getResource("sources/a.smithy").getPath()), - Paths.get(getClass().getResource("conflicting/a.smithy").getPath()))) + Paths.get(getClass().getResource("sources/a.smithy").toURI()), + Paths.get(getClass().getResource("conflicting/a.smithy").toURI()))) .build(); Assertions.assertThrows(SourcesConflictException.class, () -> new SourcesPlugin().execute(context)); diff --git a/smithy-cli/src/test/java/software/amazon/smithy/cli/BuildParameterBuilderTest.java b/smithy-cli/src/test/java/software/amazon/smithy/cli/BuildParameterBuilderTest.java index 1fa5a0178f1..7f5b31a5575 100644 --- a/smithy-cli/src/test/java/software/amazon/smithy/cli/BuildParameterBuilderTest.java +++ b/smithy-cli/src/test/java/software/amazon/smithy/cli/BuildParameterBuilderTest.java @@ -9,6 +9,8 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; +import java.net.URISyntaxException; +import java.nio.file.Paths; import java.util.Collection; import java.util.HashSet; import java.util.Set; @@ -38,8 +40,8 @@ public void skipsMissingConfigFiles() { } @Test - public void addsConfigFilesWhenFound() { - String configFile = getClass().getResource("smithy-build-a.json").getPath(); + public void addsConfigFilesWhenFound() throws URISyntaxException { + String configFile = Paths.get(getClass().getResource("smithy-build-a.json").toURI()).toString(); BuildParameterBuilder.Result result = new BuildParameterBuilder() .addConfigIfExists(configFile) .build(); @@ -213,10 +215,10 @@ public void projectionBuildTaggedSourcesRemovedFromModelDiscovery() { } @Test - public void findsProjectionJarsWithSourceTags() { - String a = getClass().getResource("jars/a/a.jar").getPath(); - String b = getClass().getResource("jars/b/b.jar").getPath(); - String c = getClass().getResource("jars/c/c.jar").getPath(); + public void findsProjectionJarsWithSourceTags() throws URISyntaxException { + String a = Paths.get(getClass().getResource("jars/a/a.jar").toURI()).toString(); + String b = Paths.get(getClass().getResource("jars/b/b.jar").toURI()).toString(); + String c = Paths.get(getClass().getResource("jars/c/c.jar").toURI()).toString(); String separator = System.getProperty("path.separator"); String buildCp = a + separator + b + separator + c; @@ -237,14 +239,14 @@ public void findsProjectionJarsWithSourceTags() { } @Test - public void usesCustomSeparator() { + public void usesCustomSeparator() throws URISyntaxException { String currentSeparator = System.getProperty("path.separator"); try { System.setProperty("path.separator", "|"); - String a = getClass().getResource("jars/a/a.jar").getPath(); - String b = getClass().getResource("jars/b/b.jar").getPath(); - String c = getClass().getResource("jars/c/c.jar").getPath(); + String a = Paths.get(getClass().getResource("jars/a/a.jar").toURI()).toString(); + String b = Paths.get(getClass().getResource("jars/b/b.jar").toURI()).toString(); + String c = Paths.get(getClass().getResource("jars/c/c.jar").toURI()).toString(); String buildCp = a + "|" + b + "|" + c; BuildParameterBuilder.Result result = new BuildParameterBuilder() diff --git a/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/AstCommandTest.java b/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/AstCommandTest.java index e7f5b34199f..1cd26205f84 100644 --- a/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/AstCommandTest.java +++ b/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/AstCommandTest.java @@ -5,6 +5,8 @@ import java.io.ByteArrayOutputStream; import java.io.PrintStream; +import java.net.URISyntaxException; +import java.nio.file.Paths; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import software.amazon.smithy.cli.CliError; @@ -31,7 +33,7 @@ public void usesModelDiscoveryWithCustomValidClasspath() throws Exception { PrintStream printStream = new PrintStream(outputStream); System.setOut(printStream); - String dir = getClass().getResource("valid.jar").getPath(); + String dir = Paths.get(getClass().getResource("valid.jar").toURI()).toString(); SmithyCli.create().run("ast", "--debug", "--discover-classpath", dir); System.setOut(out); @@ -42,7 +44,7 @@ public void usesModelDiscoveryWithCustomValidClasspath() throws Exception { @Test public void usesModelDiscoveryWithCustomInvalidClasspath() { CliError e = Assertions.assertThrows(CliError.class, () -> { - String dir = getClass().getResource("invalid.jar").getPath(); + String dir = Paths.get(getClass().getResource("invalid.jar").toURI()).toString(); SmithyCli.create().run("ast", "--debug", "--discover-classpath", dir); }); @@ -60,8 +62,8 @@ public void failsOnUnknownTrait() { } @Test - public void allowsUnknownTrait() { - String model = getClass().getResource("unknown-trait.smithy").getPath(); + public void allowsUnknownTrait() throws URISyntaxException { + String model = Paths.get(getClass().getResource("unknown-trait.smithy").toURI()).toString(); SmithyCli.create().run("ast", "--allow-unknown-traits", model); } } diff --git a/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/BuildCommandTest.java b/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/BuildCommandTest.java index 2fd27f199ae..253f8894be9 100644 --- a/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/BuildCommandTest.java +++ b/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/BuildCommandTest.java @@ -20,6 +20,7 @@ import java.io.ByteArrayOutputStream; import java.io.PrintStream; +import java.nio.file.Paths; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import software.amazon.smithy.cli.CliError; @@ -47,7 +48,7 @@ public void dumpsOutValidationErrorsAndFails() throws Exception { System.setOut(printStream); CliError e = Assertions.assertThrows(CliError.class, () -> { - String model = getClass().getResource("unknown-trait.smithy").getPath(); + String model = Paths.get(getClass().getResource("unknown-trait.smithy").toURI()).toString(); SmithyCli.create().run("build", model); }); @@ -61,7 +62,7 @@ public void dumpsOutValidationErrorsAndFails() throws Exception { @Test public void printsSuccessfulProjections() throws Exception { - String model = getClass().getResource("valid-model.smithy").getPath(); + String model = Paths.get(getClass().getResource("valid-model.smithy").toURI()).toString(); PrintStream out = System.out; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); @@ -83,8 +84,8 @@ public void validationFailuresCausedByProjectionsAreDetected() throws Exception System.setOut(printStream); CliError e = Assertions.assertThrows(CliError.class, () -> { - String model = getClass().getResource("valid-model.smithy").getPath(); - String config = getClass().getResource("projection-build-failure.json").getPath(); + String model = Paths.get(getClass().getResource("valid-model.smithy").toURI()).toString(); + String config = Paths.get(getClass().getResource("projection-build-failure.json").toURI()).toString(); SmithyCli.create().run("build", "--debug", "--config", config, model); }); diff --git a/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/SelectCommandTest.java b/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/SelectCommandTest.java index a15fbd01fad..fb080667b0c 100644 --- a/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/SelectCommandTest.java +++ b/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/SelectCommandTest.java @@ -8,6 +8,7 @@ import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.PrintStream; +import java.nio.file.Paths; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import software.amazon.smithy.cli.CliError; @@ -44,7 +45,7 @@ public void dumpsOutValidationErrorsAndFails() throws Exception { System.setOut(outPrintStream); CliError e = Assertions.assertThrows(CliError.class, () -> { - String model = getClass().getResource("unknown-trait.smithy").getPath(); + String model = Paths.get(getClass().getResource("unknown-trait.smithy").toURI()).toString(); SmithyCli.create().run("select", "--selector", "string", model); }); @@ -64,7 +65,7 @@ public void dumpsOutValidationErrorsAndFails() throws Exception { @Test public void printsSuccessfulMatchesToStdout() throws Exception { - String model = getClass().getResource("valid-model.smithy").getPath(); + String model = Paths.get(getClass().getResource("valid-model.smithy").toURI()).toString(); PrintStream out = System.out; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); @@ -82,7 +83,7 @@ public void printsSuccessfulMatchesToStdout() throws Exception { @Test public void printsJsonVarsToStdout() throws Exception { - String model = getClass().getResource("valid-model.smithy").getPath(); + String model = Paths.get(getClass().getResource("valid-model.smithy").toURI()).toString(); // Take over stdout. PrintStream out = System.out; @@ -101,7 +102,7 @@ public void printsJsonVarsToStdout() throws Exception { @Test public void readsSelectorFromStdinToo() throws Exception { - String model = getClass().getResource("valid-model.smithy").getPath(); + String model = Paths.get(getClass().getResource("valid-model.smithy").toURI()).toString(); // Send the selector through input stream. InputStream in = System.in; diff --git a/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/ValidateCommandTest.java b/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/ValidateCommandTest.java index b2681d85441..9b967762ea6 100644 --- a/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/ValidateCommandTest.java +++ b/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/ValidateCommandTest.java @@ -20,6 +20,8 @@ import java.io.ByteArrayOutputStream; import java.io.PrintStream; +import java.net.URISyntaxException; +import java.nio.file.Paths; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import software.amazon.smithy.cli.CliError; @@ -40,15 +42,15 @@ public void hasValidateCommand() throws Exception { } @Test - public void usesModelDiscoveryWithCustomValidClasspath() { - String dir = getClass().getResource("valid.jar").getPath(); + public void usesModelDiscoveryWithCustomValidClasspath() throws URISyntaxException { + String dir = Paths.get(getClass().getResource("valid.jar").toURI()).toString(); SmithyCli.create().run("validate", "--debug", "--discover-classpath", dir); } @Test public void usesModelDiscoveryWithCustomInvalidClasspath() { CliError e = Assertions.assertThrows(CliError.class, () -> { - String dir = getClass().getResource("invalid.jar").getPath(); + String dir = Paths.get(getClass().getResource("invalid.jar").toURI()).toString(); SmithyCli.create().run("validate", "--debug", "--discover-classpath", dir); }); @@ -58,7 +60,7 @@ public void usesModelDiscoveryWithCustomInvalidClasspath() { @Test public void failsOnUnknownTrait() { CliError e = Assertions.assertThrows(CliError.class, () -> { - String model = getClass().getResource("unknown-trait.smithy").getPath(); + String model = Paths.get(getClass().getResource("unknown-trait.smithy").toURI()).toString(); SmithyCli.create().run("validate", model); }); @@ -66,8 +68,8 @@ public void failsOnUnknownTrait() { } @Test - public void allowsUnknownTrait() { - String model = getClass().getResource("unknown-trait.smithy").getPath(); + public void allowsUnknownTrait() throws URISyntaxException { + String model = Paths.get(getClass().getResource("unknown-trait.smithy").toURI()).toString(); SmithyCli.create().run("validate", "--allow-unknown-traits", model); } } diff --git a/smithy-model/src/test/java/software/amazon/smithy/model/node/DefaultNodeWriterTest.java b/smithy-model/src/test/java/software/amazon/smithy/model/node/DefaultNodeWriterTest.java index bafd4e05372..d03d1297141 100644 --- a/smithy-model/src/test/java/software/amazon/smithy/model/node/DefaultNodeWriterTest.java +++ b/smithy-model/src/test/java/software/amazon/smithy/model/node/DefaultNodeWriterTest.java @@ -42,21 +42,21 @@ public static Collection data() { {new NullNode(SourceLocation.none()), "null"}, {new ArrayNode( Arrays.asList(Node.from(1), Node.from(2)), - SourceLocation.none()), String.format("[%n 1,%n 2%n]")}, + SourceLocation.none()), String.format("[\n 1,\n 2\n]")}, {Node.objectNode() .withMember("foo", Node.from("foo")) .withMember("baz", Node.from(1)) .withMember("bar", Node.objectNode() .withMember("qux", Node.arrayNode() .withValue(Node.from("ipsum")))), - String.format("{%n" - + " \"foo\": \"foo\",%n" - + " \"baz\": 1,%n" - + " \"bar\": {%n" - + " \"qux\": [%n" - + " \"ipsum\"%n" - + " ]%n" - + " }%n" + String.format("{\n" + + " \"foo\": \"foo\",\n" + + " \"baz\": 1,\n" + + " \"bar\": {\n" + + " \"qux\": [\n" + + " \"ipsum\"\n" + + " ]\n" + + " }\n" + "}") }, {Node.objectNode() @@ -66,17 +66,17 @@ public static Collection data() { .withMember("bam", Node.arrayNode() .withValue(Node.objectNode() .withMember("abc", Node.from(123)))), - String.format("{%n" - + " \"foo\": {%n" - + " \"bar\": [%n" - + " \"baz\"%n" - + " ]%n" - + " },%n" - + " \"bam\": [%n" - + " {%n" - + " \"abc\": 123%n" - + " }%n" - + " ]%n" + String.format("{\n" + + " \"foo\": {\n" + + " \"bar\": [\n" + + " \"baz\"\n" + + " ]\n" + + " },\n" + + " \"bam\": [\n" + + " {\n" + + " \"abc\": 123\n" + + " }\n" + + " ]\n" + "}") } }); diff --git a/smithy-model/src/test/java/software/amazon/smithy/model/node/NodeDiffTest.java b/smithy-model/src/test/java/software/amazon/smithy/model/node/NodeDiffTest.java index c368ad79086..206dfd43791 100644 --- a/smithy-model/src/test/java/software/amazon/smithy/model/node/NodeDiffTest.java +++ b/smithy-model/src/test/java/software/amazon/smithy/model/node/NodeDiffTest.java @@ -10,8 +10,8 @@ public class NodeDiffTest { @Test public void detectsNodeTypeDifference() { - assertThat(NodeDiff.diff(Node.nullNode(), Node.from(true)), contains( - "[]: Expected node of type `boolean` but found node of type `null`.\n\nExpected: true\n\n Found: null")); + assertThat(NodeDiff.diff(Node.nullNode(), Node.from(true)), contains(String.format( + "[]: Expected node of type `boolean` but found node of type `null`.%n%nExpected: true%n%n Found: null"))); } @Test diff --git a/smithy-model/src/test/java/software/amazon/smithy/model/node/NodeMapperTest.java b/smithy-model/src/test/java/software/amazon/smithy/model/node/NodeMapperTest.java index 66463f98606..0c0b07ee52d 100644 --- a/smithy-model/src/test/java/software/amazon/smithy/model/node/NodeMapperTest.java +++ b/smithy-model/src/test/java/software/amazon/smithy/model/node/NodeMapperTest.java @@ -14,6 +14,7 @@ import java.net.URI; import java.net.URL; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -1293,9 +1294,10 @@ public void failsWhenUnableToParsePattern() { public void pathSerde() { NodeMapper mapper = new NodeMapper(); Path path = mapper.deserialize(Node.from("/foo/baz"), Path.class); + String expected = path.toUri().toString(); // e.g., file:///foo/baz - assertThat(path.toAbsolutePath().toUri().toString(), equalTo("file:///foo/baz")); - assertThat(mapper.serialize(path), equalTo(Node.from("file:///foo/baz"))); + assertThat(path.toAbsolutePath().toUri().toString(), equalTo(expected)); + assertThat(mapper.serialize(path), equalTo(Node.from(expected))); } @Test @@ -1318,11 +1320,13 @@ public void urlSerde() { @Test public void fileSerde() { + // Wonky because of Windows. + String bogusPath = new File("/does/not/exist/blah/blah").getAbsolutePath().toString(); NodeMapper mapper = new NodeMapper(); - File file = mapper.deserialize(Node.from("/does/not/exist/blah/blah"), File.class); + File file = mapper.deserialize(Node.from(bogusPath), File.class); - assertThat(file.toString(), equalTo("/does/not/exist/blah/blah")); - assertThat(mapper.serialize(file), equalTo(Node.from("/does/not/exist/blah/blah"))); + assertThat(file.toString(), equalTo(bogusPath)); + assertThat(mapper.serialize(file), equalTo(Node.from(bogusPath))); } @Test diff --git a/smithy-model/src/test/java/software/amazon/smithy/model/node/NodeTest.java b/smithy-model/src/test/java/software/amazon/smithy/model/node/NodeTest.java index d0a124289b0..91c646e5e5f 100644 --- a/smithy-model/src/test/java/software/amazon/smithy/model/node/NodeTest.java +++ b/smithy-model/src/test/java/software/amazon/smithy/model/node/NodeTest.java @@ -178,7 +178,7 @@ public void deepSortsNodesWithComparator() { @Test public void prettyPrintsJson() { assertThat(Node.prettyPrintJson(Node.parse("{\"foo\": true}")), - equalTo(String.format("{%n \"foo\": true%n}"))); + equalTo(String.format("{\n \"foo\": true\n}"))); } @Test diff --git a/smithy-model/src/test/java/software/amazon/smithy/model/shapes/SmithyIdlModelSerializerTest.java b/smithy-model/src/test/java/software/amazon/smithy/model/shapes/SmithyIdlModelSerializerTest.java index 0dd089da1a1..55928d35106 100644 --- a/smithy-model/src/test/java/software/amazon/smithy/model/shapes/SmithyIdlModelSerializerTest.java +++ b/smithy-model/src/test/java/software/amazon/smithy/model/shapes/SmithyIdlModelSerializerTest.java @@ -1,14 +1,15 @@ package software.amazon.smithy.model.shapes; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.aMapWithSize; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.aMapWithSize; import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.not; import java.io.IOException; +import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -26,9 +27,9 @@ public class SmithyIdlModelSerializerTest { @TestFactory - public Stream generateTests() throws IOException { + public Stream generateTests() throws IOException, URISyntaxException { return Files.list(Paths.get( - SmithyIdlModelSerializer.class.getResource("idl-serialization/cases").getPath())) + SmithyIdlModelSerializer.class.getResource("idl-serialization/cases").toURI())) .map(path -> DynamicTest.dynamicTest(path.getFileName().toString(), () -> testConversion(path))); } @@ -46,12 +47,12 @@ public void testConversion(Path path) { } @Test - public void multipleNamespacesGenerateMultipleFiles() { + public void multipleNamespacesGenerateMultipleFiles() throws Exception { Model model = Model.assembler() .addImport(getClass().getResource("idl-serialization/multiple-namespaces/input.json")) .assemble() .unwrap(); - Path outputDir = Paths.get(getClass().getResource("idl-serialization/multiple-namespaces/output").getFile()); + Path outputDir = Paths.get(getClass().getResource("idl-serialization/multiple-namespaces/output").toURI()); SmithyIdlModelSerializer serializer = SmithyIdlModelSerializer.builder() .basePath(outputDir) .build(); diff --git a/smithy-model/src/test/java/software/amazon/smithy/model/validation/ContextualValidationEventFormatterTest.java b/smithy-model/src/test/java/software/amazon/smithy/model/validation/ContextualValidationEventFormatterTest.java index 1f1dd0d0eb6..97dca0494cc 100644 --- a/smithy-model/src/test/java/software/amazon/smithy/model/validation/ContextualValidationEventFormatterTest.java +++ b/smithy-model/src/test/java/software/amazon/smithy/model/validation/ContextualValidationEventFormatterTest.java @@ -29,6 +29,8 @@ public void loadsContext() { .build(); String format = new ContextualValidationEventFormatter().format(event); + // Normalize line endings for Windows. + format = format.replace("\r\n", "\n"); assertThat(format, startsWith("ERROR: example.smithy#Foo (foo)")); assertThat(format, containsString("\n @ ")); @@ -50,6 +52,8 @@ public void doesNotLoadSourceLocationNone() { .build(); String format = new ContextualValidationEventFormatter().format(event); + // Normalize line endings for Windows. + format = format.replace("\r\n", "\n"); assertThat(format, equalTo( "ERROR: - (foo)" diff --git a/smithy-utils/src/main/java/software/amazon/smithy/utils/IoUtils.java b/smithy-utils/src/main/java/software/amazon/smithy/utils/IoUtils.java index 5199c7e4cfa..e2e7b772ac0 100644 --- a/smithy-utils/src/main/java/software/amazon/smithy/utils/IoUtils.java +++ b/smithy-utils/src/main/java/software/amazon/smithy/utils/IoUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.UncheckedIOException; +import java.net.URL; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -101,11 +102,7 @@ public static String readUtf8File(Path path) { * @throws UncheckedIOException if the resource cannot be loaded. */ public static String readUtf8Resource(ClassLoader classLoader, String resourcePath) { - try (InputStream is = classLoader.getResourceAsStream(resourcePath)) { - return toUtf8String(is); - } catch (IOException e) { - throw new UncheckedIOException(e); - } + return readUtf8Url(classLoader.getResource(resourcePath)); } /** @@ -119,8 +116,19 @@ public static String readUtf8Resource(ClassLoader classLoader, String resourcePa * @return Returns the loaded resource. * @throws UncheckedIOException if the resource cannot be loaded. */ - public static String readUtf8Resource(Class clazz, String resourcePath) { - try (InputStream is = clazz.getResourceAsStream(resourcePath)) { + public static String readUtf8Resource(Class clazz, String resourcePath) { + return readUtf8Url(clazz.getResource(resourcePath)); + } + + /** + * Reads a URL resource into a UTF-8 string. + * + * @param url URL to load from. + * @return Returns the loaded resource. + * @throws UncheckedIOException if the resource cannot be loaded. + */ + public static String readUtf8Url(URL url) { + try (InputStream is = url.openStream()) { return toUtf8String(is); } catch (IOException e) { throw new UncheckedIOException(e); diff --git a/smithy-utils/src/test/java/software/amazon/smithy/utils/IoUtilsTest.java b/smithy-utils/src/test/java/software/amazon/smithy/utils/IoUtilsTest.java index 5d11a9a80d7..6ec35ccad8a 100644 --- a/smithy-utils/src/test/java/software/amazon/smithy/utils/IoUtilsTest.java +++ b/smithy-utils/src/test/java/software/amazon/smithy/utils/IoUtilsTest.java @@ -60,8 +60,12 @@ public int read() throws IOException { } @Test - public void readsFromStringPath() { - assertEquals("This is a test.\n", IoUtils.readUtf8File(getClass().getResource("test.txt").getPath())); + public void readsFromStringPath() throws Exception { + // Windows doesn't like the result of URL#getPath, so to test this + // we create a Path from the URI, convert that to a string, then pass + // it to the helper method which uses Paths.get again. + assertEquals("This is a test.\n", + IoUtils.readUtf8File(Paths.get(getClass().getResource("test.txt").toURI()).toString())); } @Test