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 LinuxLineFeedInputStreamTest {
29
30 @Test
31 public void testSimpleString() throws Exception {
32 assertEquals("abc\n", roundtrip("abc"));
33 }
34
35 @Test
36 public void testInTheMiddleOfTheLine() throws Exception {
37 assertEquals("a\nbc\n", roundtrip("a\r\nbc"));
38 }
39
40 @Test
41 public void testCrOnly() throws Exception {
42 assertEquals("a\nb\n", roundtrip("a\rb"));
43 }
44
45 @Test
46 public void testCrAtEnd() throws Exception {
47 assertEquals("a\n", roundtrip("a\r"));
48 }
49
50 @Test
51 public void testMultipleBlankLines() throws Exception {
52 assertEquals("a\n\nbc\n", roundtrip("a\r\n\r\nbc"));
53 }
54
55 @Test
56 public void testTwoLinesAtEnd() throws Exception {
57 assertEquals("a\n\n", roundtrip("a\r\n\r\n"));
58 }
59
60 @Test
61 public void testRetainLineFeed() throws Exception {
62 assertEquals("a\n\n", roundtrip("a\r\n\r\n", false));
63 assertEquals("a", roundtrip("a", false));
64 }
65
66 private String roundtrip(String msg) throws IOException {
67 return roundtrip(msg, true);
68 }
69
70 private String roundtrip(String msg, boolean ensure) throws IOException {
71 ByteArrayInputStream baos = new ByteArrayInputStream(msg.getBytes());
72
73 try (LinuxLineFeedInputStream lf = new LinuxLineFeedInputStream(baos, ensure)) {
74 byte[] buf = new byte[100];
75 final int read = lf.read(buf);
76 return new String(buf, 0, read);
77 }
78 }
79 }