1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.assembly.utils;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.IOException;
23
24 import org.junit.Test;
25
26 import static org.junit.Assert.assertEquals;
27
28 public class WindowsLineFeedInputStreamTest {
29
30 @Test
31 public void testSimpleString() throws Exception {
32 assertEquals("abc\r\n", roundtrip("abc"));
33 }
34
35 @Test
36 public void testInTheMiddleOfTheLine() throws Exception {
37 assertEquals("a\r\nbc\r\n", roundtrip("a\r\nbc"));
38 }
39
40 @Test
41 public void testMultipleBlankLines() throws Exception {
42 assertEquals("a\r\n\r\nbc\r\n", roundtrip("a\r\n\r\nbc"));
43 }
44
45 @Test
46 public void testTwoLinesAtEnd() throws Exception {
47 assertEquals("a\r\n\r\n", roundtrip("a\r\n\r\n"));
48 }
49
50 @Test
51 public void testLinuxLinefeeds() throws Exception {
52 final String roundtrip = roundtrip("ab\nc", false);
53 assertEquals("ab\r\nc", roundtrip);
54 }
55
56 @Test
57 public void testMalformed() throws Exception {
58 assertEquals("a\rbc", roundtrip("a\rbc", false));
59 }
60
61 @Test
62 public void testRetainLineFeed() throws Exception {
63 assertEquals("a\r\n\r\n", roundtrip("a\r\n\r\n", false));
64 assertEquals("a", roundtrip("a", false));
65 }
66
67 private String roundtrip(String msg) throws IOException {
68 return roundtrip(msg, true);
69 }
70
71 private String roundtrip(String msg, boolean ensure) throws IOException {
72 ByteArrayInputStream baos = new ByteArrayInputStream(msg.getBytes());
73
74 try (WindowsLineFeedInputStream lf = new WindowsLineFeedInputStream(baos, ensure)) {
75 byte[] buf = new byte[100];
76 final int read = lf.read(buf);
77 return new String(buf, 0, read);
78 }
79 }
80 }