From f65e40f8e7fdb2c7b87f7580d8870a06b11a9771 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Tue, 29 Apr 2014 15:44:00 -0700 Subject: [PATCH] Fixing bad EOL assumptions based on running OS (should be based on git checkout state) --- .../server/handler/ResourceHandlerTest.java | 81 +++++++++++++------ 1 file changed, 55 insertions(+), 26 deletions(-) diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ResourceHandlerTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ResourceHandlerTest.java index 9e5745b7e0a..5dd6aa28765 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ResourceHandlerTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ResourceHandlerTest.java @@ -18,6 +18,7 @@ package org.eclipse.jetty.server.handler; +import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -25,6 +26,8 @@ import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.HttpConfiguration; @@ -43,12 +46,12 @@ import org.junit.Test; /** * Resource Handler test - * + * * TODO: increase the testing going on here */ public class ResourceHandlerTest { - private static final String LN = System.getProperty("line.separator"); + private static String LN = System.getProperty("line.separator"); private static Server _server; private static HttpConfiguration _config; private static ServerConnector _connector; @@ -60,29 +63,56 @@ public class ResourceHandlerTest { File dir = MavenTestingUtils.getTargetFile("test-classes/simple"); File huge = new File(dir,"huge.txt"); - File big=new File(dir,"big.txt"); - try (OutputStream out = new FileOutputStream(huge)) { - for (int i=0;i<100;i++) + File big = new File(dir,"big.txt"); + try (OutputStream out = new FileOutputStream(huge)) + { + for (int i = 0; i < 100; i++) { - try (InputStream in=new FileInputStream(big)) + try (InputStream in = new FileInputStream(big)) { IO.copy(in,out); } } } huge.deleteOnExit(); - + + // determine how the SCM of choice checked out the big.txt EOL + // we can't just use whatever is the OS default. + // because, for example, a windows system using git can be configured for EOL handling using + // local, remote, file lists, patterns, etc, rendering assumptions about the OS EOL choice + // wrong for unit tests. + LN = System.getProperty("line.separator"); + try (BufferedReader reader = Files.newBufferedReader(big.toPath(),StandardCharsets.UTF_8)) + { + // a buffer large enough to capture at least 1 EOL + char cbuf[] = new char[128]; + reader.read(cbuf); + String sample = new String(cbuf); + if (sample.contains("\r\n")) + { + LN = "\r\n"; + } + else if (sample.contains("\n\r")) + { + LN = "\n\r"; + } + else + { + LN = "\n"; + } + } + _server = new Server(); - _config=new HttpConfiguration(); + _config = new HttpConfiguration(); _config.setOutputBufferSize(2048); _connector = new ServerConnector(_server,new HttpConnectionFactory(_config)); - + _server.setConnectors(new Connector[] { _connector }); _resourceHandler = new ResourceHandler(); _resourceHandler.setMinAsyncContentLength(4096); _resourceHandler.setMinMemoryMappedContentLength(8192); - + _resourceHandler.setResourceBase(MavenTestingUtils.getTargetFile("test-classes/simple").getAbsolutePath()); _contextHandler = new ContextHandler("/resource"); @@ -96,7 +126,7 @@ public class ResourceHandlerTest { _server.stop(); } - + @Before public void before() { @@ -107,45 +137,44 @@ public class ResourceHandlerTest public void testMissing() throws Exception { SimpleRequest sr = new SimpleRequest(new URI("http://localhost:" + _connector.getLocalPort())); - Assert.assertNotNull("missing jetty.css" , sr.getString("/resource/jetty-dir.css")); + Assert.assertNotNull("missing jetty.css",sr.getString("/resource/jetty-dir.css")); } - + @Test public void testSimple() throws Exception { SimpleRequest sr = new SimpleRequest(new URI("http://localhost:" + _connector.getLocalPort())); - Assert.assertEquals("simple text", sr.getString("/resource/simple.txt")); + Assert.assertEquals("simple text",sr.getString("/resource/simple.txt")); } - @Test public void testBigFile() throws Exception { _config.setOutputBufferSize(2048); SimpleRequest sr = new SimpleRequest(new URI("http://localhost:" + _connector.getLocalPort())); - String response=sr.getString("/resource/big.txt"); + String response = sr.getString("/resource/big.txt"); Assert.assertThat(response,Matchers.startsWith(" 1\tThis is a big file")); - Assert.assertThat(response,Matchers.endsWith(" 400\tThis is a big file"+LN)); + Assert.assertThat(response,Matchers.endsWith(" 400\tThis is a big file" + LN)); } - + @Test public void testBigFileBigBuffer() throws Exception { - _config.setOutputBufferSize(16*1024); + _config.setOutputBufferSize(16 * 1024); SimpleRequest sr = new SimpleRequest(new URI("http://localhost:" + _connector.getLocalPort())); - String response=sr.getString("/resource/big.txt"); + String response = sr.getString("/resource/big.txt"); Assert.assertThat(response,Matchers.startsWith(" 1\tThis is a big file")); - Assert.assertThat(response,Matchers.endsWith(" 400\tThis is a big file"+LN)); + Assert.assertThat(response,Matchers.endsWith(" 400\tThis is a big file" + LN)); } - + @Test public void testBigFileLittleBuffer() throws Exception { _config.setOutputBufferSize(8); SimpleRequest sr = new SimpleRequest(new URI("http://localhost:" + _connector.getLocalPort())); - String response=sr.getString("/resource/big.txt"); + String response = sr.getString("/resource/big.txt"); Assert.assertThat(response,Matchers.startsWith(" 1\tThis is a big file")); - Assert.assertThat(response,Matchers.endsWith(" 400\tThis is a big file"+LN)); + Assert.assertThat(response,Matchers.endsWith(" 400\tThis is a big file" + LN)); } @Test @@ -157,8 +186,8 @@ public class ResourceHandlerTest Thread.sleep(1000); String response = IO.toString(socket.getInputStream()); Assert.assertThat(response,Matchers.startsWith("HTTP/1.1 200 OK")); - Assert.assertThat(response,Matchers.containsString(" 400\tThis is a big file"+LN+" 1\tThis is a big file")); - Assert.assertThat(response,Matchers.endsWith(" 400\tThis is a big file"+LN)); + Assert.assertThat(response,Matchers.containsString(" 400\tThis is a big file" + LN + " 1\tThis is a big file")); + Assert.assertThat(response,Matchers.endsWith(" 400\tThis is a big file" + LN)); } } }