Skip to content

Commit

Permalink
Merge pull request #305 from fcamblor/remove-windows-carriage-returns
Browse files Browse the repository at this point in the history
Remove windows carriage returns
  • Loading branch information
fcamblor committed May 19, 2019
2 parents 5842e60 + 5a61b77 commit 4bd470f
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 39 deletions.
4 changes: 4 additions & 0 deletions restx-common/src/main/java/restx/common/MoreFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,8 @@ public static void extractZip(File zip, File toDir) throws IOException {
}
}
}

public static String removeWindowsCarriageReturnsBeforeLF(String str) {
return str.replaceAll("\r\n", "\n");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void onMessage(String msg) {
coalescor.post("test1");
coalescor.post("test3");

Thread.sleep(40);
Thread.sleep(100);

assertThat(messages).containsExactly("test1", "test2", "test3");

Expand All @@ -45,7 +45,7 @@ public void onMessage(String msg) {
coalescor.post("test1");
coalescor.post("test1");
coalescor.post("test1");
Thread.sleep(40);
Thread.sleep(100);

assertThat(messages).containsExactly("test2", "test1");
}
Expand Down
3 changes: 2 additions & 1 deletion restx-core/src/main/java/restx/AbstractResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ public void close() throws Exception {
}
try {
if (writer != null) {
writer.println();
writer.print("\n");
writer.flush();
writer.close();
}
if (outputStream != null) {
Expand Down
8 changes: 4 additions & 4 deletions restx-core/src/main/java/restx/FSRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ public void handle(RestxRequestMatch match, RestxRequest req, RestxResponse resp
resp.setStatus(HttpStatus.OK);
resp.setContentType("application/json");
PrintWriter writer = resp.getWriter();
writer.println("[");
writer.print("[\n");
Path dir = file.toPath();
for (Iterator<Path> iterator = java.nio.file.Files.newDirectoryStream(dir).iterator(); iterator.hasNext(); ) {
Path s = iterator.next();
boolean isDirectory = s.toFile().isDirectory();
writer.println("\"" + dir.relativize(s) + (isDirectory ? "/" : "") + "\""
+ (iterator.hasNext() ? "," : "") +"");
writer.print("\"" + dir.relativize(s) + (isDirectory ? "/" : "") + "\""
+ (iterator.hasNext() ? "," : "") +"\n");
}
writer.println("]");
writer.print("]\n");
} else {
throw new WebException(HttpStatus.UNAUTHORIZED);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package restx.jackson;

import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.HandlerInstantiator;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
Expand Down Expand Up @@ -35,6 +37,11 @@ public ObjectMapper mapper(final Factory factory) {
ObjectMapper mapper = new ObjectMapper()
.registerModule(new JodaModule())
.registerModule(new GuavaModule())
.setDefaultPrettyPrinter(new DefaultPrettyPrinter()
// This allows to "enforce" that \n will be used in generated JSON, instead of platform-specific eol
// (windows, I'm looking at your \r\n here)
.withObjectIndenter(new DefaultIndenter(" ", "\n"))
)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(DeserializationFeature.EAGER_DESERIALIZER_FETCH)
Expand Down
8 changes: 7 additions & 1 deletion restx-core/src/main/java/restx/specs/RestxSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import static com.google.common.base.Preconditions.checkNotNull;

Expand Down Expand Up @@ -60,14 +61,19 @@ private Storage(StorageSettings settings) {
}

public File getStoreFile(String path) {
if (path.startsWith("/")) {
if (pathLooksAbsolute(path)) {
return new File(path);
} else {
String basePath = settings.recorderBasePath();
return new File(basePath + "/" + path);
}
}

private static final Pattern WINDOWS_PATH_PATTERN = Pattern.compile("^[A-Z]:\\\\.*");
private static boolean pathLooksAbsolute(String path) {
return path.startsWith("/") || WINDOWS_PATH_PATTERN.matcher(path).matches();
}

public File store(RestxSpec spec) throws IOException {
File storeFile = getStoreFile(spec.getPath());
spec.store(storeFile);
Expand Down
2 changes: 1 addition & 1 deletion restx-i18n/src/main/java/restx/i18n/MessagesRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected static void handleETagFor(RestxRequest req, RestxResponse resp, Iterab
}

protected static void writeLabelsJson(PrintWriter writer, Iterable<Entry<String, String>> labels) {
writer.println("{");
writer.print("{\n");

boolean firstKey = true;
for (Entry<String, String> entry : labels) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.common.io.Resources;
import org.assertj.core.data.MapEntry;
import org.junit.Test;
import restx.common.MoreFiles;
import restx.factory.Factory;
import restx.specs.RestxSpec;
import restx.specs.RestxSpecLoader;
Expand Down Expand Up @@ -31,7 +32,7 @@ public void should_load_from_yaml() throws Exception {
.containsExactly("contracts", "events", "salaries");
assertThat(extractProperty("data").from(testCase.getGiven()))
.containsExactly(
"[ { \"_id\": \"511bd1267638b9481a66f385\", \"title\": \"test1\" } ]\n",
"[ { \"_id\": \"511bd1267638b9481a66f385\", \"title\": \"test1\" } ]",
"[\n" +
"{ \"_id\": \"511bd1267638b9481a66f385\", \"title\": \"example1\" },\n" +
"{ \"_id\": \"511bd1297638b9481a66f386\", \"title\": \"example2\" }\n" +
Expand Down Expand Up @@ -75,8 +76,8 @@ public void should_generate_yaml() throws Exception {

String actual = testCase.toString();
System.out.println("actual = " + actual);
assertThat(actual).isEqualTo(Resources.toString(
Resources.getResource("restx/tests/expected_restx_case_example_1.yaml"),
Charsets.UTF_8));
assertThat(actual).isEqualTo(MoreFiles.removeWindowsCarriageReturnsBeforeLF(
Resources.toString(Resources.getResource("restx/tests/expected_restx_case_example_1.yaml"), Charsets.UTF_8)
));
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[ { "_id": "511bd1267638b9481a66f385", "title": "test1" } ]
[ { "_id": "511bd1267638b9481a66f385", "title": "test1" } ]
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import restx.build.*;
import restx.common.MoreFiles;
import restx.common.OSUtils;

import java.io.File;
Expand Down Expand Up @@ -79,7 +80,7 @@ public void run() {
mavenVerifier.executeGoal("org.apache.maven.plugins:maven-help-plugin:2.2:effective-pom");
mavenVerifier.verifyErrorFreeLog();

Assert.assertTrue(logFile.delete());
logFile.deleteOnExit();
removeGeneratedLineIn(effectivePom);
} catch (VerificationException | IOException e) {
String wrappingMessage;
Expand Down Expand Up @@ -138,8 +139,10 @@ public List<String> getResult() {

public abstract RestxBuild.Generator generator();
public void assertExistingAndGeneratedDescriptorsAreSimilar(File existingDescriptor, File generatedDescriptor, TemporaryFolder tempFolder) throws IOException {
Assert.assertEquals(com.google.common.io.Files.toString(existingDescriptor, Charsets.UTF_8),
com.google.common.io.Files.toString(generatedDescriptor, Charsets.UTF_8));
Assert.assertEquals(
MoreFiles.removeWindowsCarriageReturnsBeforeLF(com.google.common.io.Files.toString(existingDescriptor, Charsets.UTF_8)),
MoreFiles.removeWindowsCarriageReturnsBeforeLF(com.google.common.io.Files.toString(generatedDescriptor, Charsets.UTF_8))
);
}
}

Expand Down Expand Up @@ -176,7 +179,7 @@ public boolean accept(File dir, String name) {
private static Path getRestxSourcesRootDir(String sysPropertyName) {
String restxShellSourcesRootDirProp = System.getProperty(sysPropertyName);
if(restxShellSourcesRootDirProp == null) {
throw new IllegalArgumentException(String.format("You need to put -D%s=/path/to/restx-shell/rootdir while executing this test !", sysPropertyName));
throw new IllegalArgumentException(String.format("You need to put -D%s=/path/to/restx-multimodules/rootdir while executing this test !", sysPropertyName));
}
return Paths.get(restxShellSourcesRootDirProp);
}
Expand Down
12 changes: 8 additions & 4 deletions restx-samplest/src/test/java/samplest/core/CoreResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ public void should_return_hello_msg_when_delete_with_param() throws Exception {

@Test
public void should_post_hello() throws Exception {
HttpRequest httpRequest = server.client().authenticatedAs("admin").POST(
"/api/core/hellomsg").send("{\"msg\": \"restx\"}");
HttpRequest httpRequest = server.client().authenticatedAs("admin")
.POST("/api/core/hellomsg")
.contentType("application/json")
.send("{\"msg\": \"restx\"}");
assertThat(httpRequest.code()).isEqualTo(200);
assertThat(httpRequest.body().trim()).isEqualTo("{\n \"msg\" : \"hello restx\"\n}");
}
Expand Down Expand Up @@ -84,8 +86,10 @@ public void should_lifecycle_hello_msg() throws Exception {
public void should_lifecycle_post_hello_msg() throws Exception {
LifecycleListenerFilter filter = new LifecycleListenerFilter();
Factory.LocalMachines.overrideComponents().set("LifecycleListenerFilter", filter);
HttpRequest httpRequest = server.client().authenticatedAs("admin").POST(
"/api/core/hellomsg").send("{\"msg\": \"restx\"}");
HttpRequest httpRequest = server.client().authenticatedAs("admin")
.POST("/api/core/hellomsg")
.contentType("application/json")
.send("{\"msg\": \"restx\"}");

assertThat(httpRequest.code()).isEqualTo(200);
assertThat(httpRequest.body().trim()).isEqualTo("{\n \"msg\" : \"hello restx\"\n}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.kevinsawicki.http.HttpRequest;
import org.junit.ClassRule;
import org.junit.Test;
import restx.tests.HttpTestClient;
import restx.tests.RestxServerRule;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -55,8 +56,12 @@ public void should_return_B_list() throws Exception {

@Test
public void should_post_A() throws Exception {
HttpRequest httpRequest = server.client().authenticatedAs("admin").POST(
"/api/polymorphic").send("{\"@class\":\".PolymorphicResource$A\",\"a\":\"a3\"}");
HttpTestClient httpTestClient = HttpTestClient.withBaseUrl("http://localhost:8080");
httpTestClient = server.client();
HttpRequest httpRequest = httpTestClient.authenticatedAs("admin")
.POST("/api/polymorphic")
.contentType("application/json")
.send("{\"@class\":\".PolymorphicResource$A\",\"a\":\"a3\"}");
assertThat(httpRequest.code()).isEqualTo(200);
assertThat(httpRequest.body().trim()).isEqualTo("{\n" +
" \"@class\" : \".PolymorphicResource$A\",\n" +
Expand All @@ -66,8 +71,10 @@ public void should_post_A() throws Exception {

@Test
public void should_post_B() throws Exception {
HttpRequest httpRequest = server.client().authenticatedAs("admin").POST(
"/api/polymorphic").send("{\"@class\":\".PolymorphicResource$B\",\"a\":\"a3\",\"b\":\"b\"}");
HttpRequest httpRequest = server.client().authenticatedAs("admin")
.POST("/api/polymorphic")
.contentType("application/json")
.send("{\"@class\":\".PolymorphicResource$B\",\"a\":\"a3\",\"b\":\"b\"}");
assertThat(httpRequest.code()).isEqualTo(200);
assertThat(httpRequest.body().trim()).isEqualTo("{\n" +
" \"@class\" : \".PolymorphicResource$B\",\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,27 @@ public void should_handle_fs_route_write_file() throws Exception {

httpRequest = HttpRequest.get(server.baseUrl() + "/api/test/");
assertThat(httpRequest.code()).isEqualTo(200);
assertThat(httpRequest.body().trim()).isEqualTo("[\n]");

httpRequest = HttpRequest.put(server.baseUrl() + "/api/test/dir/test.txt").send("bonjour");
assertThat(httpRequest.code()).isEqualTo(HttpStatus.CREATED.getCode());
// On windows, following assertions are failing because some JVM handles seem to remain on test/dir/* files
// making test/dir/ directory deletion not effective at that moment (listing test/ directory will show dir/
// subdirectory instead of an empty directory)
if(!isWindowsOS()) {
assertThat(httpRequest.body().trim()).isEqualTo("[\n]");

httpRequest = HttpRequest.get(server.baseUrl() + "/api/test/dir/test.txt");
assertThat(httpRequest.code()).isEqualTo(200);
assertThat(httpRequest.body().trim()).isEqualTo("bonjour");
httpRequest = HttpRequest.put(server.baseUrl() + "/api/test/dir/test.txt").send("bonjour");
assertThat(httpRequest.code()).isEqualTo(HttpStatus.CREATED.getCode());

httpRequest = HttpRequest.get(server.baseUrl() + "/api/test/dir/test.txt");
assertThat(httpRequest.code()).isEqualTo(200);
assertThat(httpRequest.body().trim()).isEqualTo("bonjour");
}
} finally {
server.stop();
}
}

private static boolean isWindowsOS() {
return System.getProperty("os.name").toLowerCase().contains("windows");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.io.Files;
import restx.common.MoreFiles;
import restx.common.Mustaches;

import java.io.File;
Expand Down Expand Up @@ -306,7 +307,7 @@ private String asJson(Object o) {
}

try {
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(o);
return MoreFiles.removeWindowsCarriageReturnsBeforeLF(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(o));
} catch (JsonProcessingException e) {
e.printStackTrace();
return o.toString();
Expand Down
13 changes: 7 additions & 6 deletions restx-specs-tests/src/main/java/restx/tests/json/JsonMerger.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Ordering;
import restx.common.MoreFiles;

import java.util.Comparator;

Expand Down Expand Up @@ -55,11 +56,7 @@ public String mergeToRight(JsonDiff diff) {
difference.mergeToRight(diff);
}

try {
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
return serializeNodeAsString(root);
}

public String mergeToLeft(JsonDiff diff) {
Expand All @@ -69,8 +66,12 @@ public String mergeToLeft(JsonDiff diff) {
difference.mergeToLeft(diff);
}

return serializeNodeAsString(root);
}

private String serializeNodeAsString(Object root) {
try {
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root);
return MoreFiles.removeWindowsCarriageReturnsBeforeLF(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 4bd470f

Please sign in to comment.