Fixing bad EOL assumptions based on running OS (should be based on git checkout state)

This commit is contained in:
Joakim Erdfelt 2014-04-29 15:44:00 -07:00
parent 808818281d
commit f65e40f8e7
1 changed files with 55 additions and 26 deletions

View File

@ -18,6 +18,7 @@
package org.eclipse.jetty.server.handler; package org.eclipse.jetty.server.handler;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -25,6 +26,8 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.Socket; import java.net.Socket;
import java.net.URI; 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.Connector;
import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.HttpConfiguration;
@ -48,7 +51,7 @@ import org.junit.Test;
*/ */
public class ResourceHandlerTest 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 Server _server;
private static HttpConfiguration _config; private static HttpConfiguration _config;
private static ServerConnector _connector; private static ServerConnector _connector;
@ -60,11 +63,12 @@ public class ResourceHandlerTest
{ {
File dir = MavenTestingUtils.getTargetFile("test-classes/simple"); File dir = MavenTestingUtils.getTargetFile("test-classes/simple");
File huge = new File(dir,"huge.txt"); File huge = new File(dir,"huge.txt");
File big=new File(dir,"big.txt"); File big = new File(dir,"big.txt");
try (OutputStream out = new FileOutputStream(huge)) { try (OutputStream out = new FileOutputStream(huge))
for (int i=0;i<100;i++)
{ {
try (InputStream in=new FileInputStream(big)) for (int i = 0; i < 100; i++)
{
try (InputStream in = new FileInputStream(big))
{ {
IO.copy(in,out); IO.copy(in,out);
} }
@ -72,8 +76,34 @@ public class ResourceHandlerTest
} }
huge.deleteOnExit(); 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(); _server = new Server();
_config=new HttpConfiguration(); _config = new HttpConfiguration();
_config.setOutputBufferSize(2048); _config.setOutputBufferSize(2048);
_connector = new ServerConnector(_server,new HttpConnectionFactory(_config)); _connector = new ServerConnector(_server,new HttpConnectionFactory(_config));
@ -107,35 +137,34 @@ public class ResourceHandlerTest
public void testMissing() throws Exception public void testMissing() throws Exception
{ {
SimpleRequest sr = new SimpleRequest(new URI("http://localhost:" + _connector.getLocalPort())); 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 @Test
public void testSimple() throws Exception public void testSimple() throws Exception
{ {
SimpleRequest sr = new SimpleRequest(new URI("http://localhost:" + _connector.getLocalPort())); 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 @Test
public void testBigFile() throws Exception public void testBigFile() throws Exception
{ {
_config.setOutputBufferSize(2048); _config.setOutputBufferSize(2048);
SimpleRequest sr = new SimpleRequest(new URI("http://localhost:" + _connector.getLocalPort())); 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.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 @Test
public void testBigFileBigBuffer() throws Exception public void testBigFileBigBuffer() throws Exception
{ {
_config.setOutputBufferSize(16*1024); _config.setOutputBufferSize(16 * 1024);
SimpleRequest sr = new SimpleRequest(new URI("http://localhost:" + _connector.getLocalPort())); 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.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 @Test
@ -143,9 +172,9 @@ public class ResourceHandlerTest
{ {
_config.setOutputBufferSize(8); _config.setOutputBufferSize(8);
SimpleRequest sr = new SimpleRequest(new URI("http://localhost:" + _connector.getLocalPort())); 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.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 @Test
@ -157,8 +186,8 @@ public class ResourceHandlerTest
Thread.sleep(1000); Thread.sleep(1000);
String response = IO.toString(socket.getInputStream()); String response = IO.toString(socket.getInputStream());
Assert.assertThat(response,Matchers.startsWith("HTTP/1.1 200 OK")); 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.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.endsWith(" 400\tThis is a big file" + LN));
} }
} }
} }