Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Checkstyle to work on Windows #576

Merged
merged 3 commits into from
Sep 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
@@ -36,7 +36,9 @@
<property name="fileExtensions" value="java"/>
</module>

<module name="NewlineAtEndOfFile"/>
<module name="NewlineAtEndOfFile">
<property name="lineSeparator" value="lf_cr_crlf"/>
</module>

<!-- Checks for whitespace -->
<!-- See http://checkstyle.sf.net/config_whitespace.html -->
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import software.amazon.smithy.build.FileManifest;
import software.amazon.smithy.build.PluginContext;
import software.amazon.smithy.build.SmithyBuildPlugin;
@@ -91,7 +92,9 @@ public void execute(PluginContext context) {
LOGGER.info(String.format("Skipping `%s` manifest because no Smithy sources found", projectionName));
} else {
LOGGER.fine(() -> String.format("Writing `%s` manifest", projectionName));
context.getFileManifest().writeFile("manifest", String.join("\n", names) + "\n");
// Normalize filenames to Unix style.
String manifest = names.stream().map(name -> name.replace("\\", "/")).collect(Collectors.joining("\n"));
context.getFileManifest().writeFile("manifest", manifest + "\n");
}
}

Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -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));
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
});

Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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,16 +60,16 @@ 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);
});

assertThat(e.getMessage(), containsString("1 ERROR(s)"));
}

@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);
}
}
Original file line number Diff line number Diff line change
@@ -42,21 +42,21 @@ public static Collection<Object[]> 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<Object[]> 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"
+ "}")
}
});
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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<DynamicTest> generateTests() throws IOException {
public Stream<DynamicTest> 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();
Original file line number Diff line number Diff line change
@@ -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)"
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -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