1 package org.apache.maven.plugins.assembly.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.ByteArrayInputStream;
23 import java.io.IOException;
24
25 import org.codehaus.plexus.util.IOUtil;
26 import org.junit.Test;
27
28 import static org.junit.Assert.assertEquals;
29
30 public class LinuxLineFeedInputStreamTest
31 {
32
33 @Test
34 public void testSimpleString()
35 throws Exception
36 {
37 assertEquals( "abc\n", roundtrip( "abc" ) );
38 }
39
40 @Test
41 public void testInTheMiddleOfTheLine()
42 throws Exception
43 {
44 assertEquals( "a\nbc\n", roundtrip( "a\r\nbc" ) );
45 }
46
47 @Test
48 public void testCrOnly()
49 throws Exception
50 {
51 assertEquals( "a\nb\n", roundtrip( "a\rb" ) );
52 }
53
54 @Test
55 public void testCrAtEnd()
56 throws Exception
57 {
58 assertEquals( "a\n", roundtrip( "a\r" ) );
59 }
60
61
62 @Test
63 public void testMultipleBlankLines()
64 throws Exception
65 {
66 assertEquals( "a\n\nbc\n", roundtrip( "a\r\n\r\nbc" ) );
67 }
68
69 @Test
70 public void testTwoLinesAtEnd()
71 throws Exception
72 {
73 assertEquals( "a\n\n", roundtrip( "a\r\n\r\n" ) );
74 }
75
76 @Test
77 public void testRetainLineFeed()
78 throws Exception
79 {
80 assertEquals( "a\n\n", roundtrip( "a\r\n\r\n", false ) );
81 assertEquals( "a", roundtrip( "a", false ) );
82 }
83
84 private String roundtrip( String msg )
85 throws IOException
86 {
87 return roundtrip( msg, true );
88 }
89
90 private String roundtrip( String msg, boolean ensure )
91 throws IOException
92 {
93 ByteArrayInputStream baos = new ByteArrayInputStream( msg.getBytes() );
94
95 LinuxLineFeedInputStream lf = null;
96 try
97 {
98 lf = new LinuxLineFeedInputStream( baos, ensure );
99 byte[] buf = new byte[ 100 ];
100 final int read = lf.read( buf );
101 final String string = new String( buf, 0, read );
102 lf.close();
103 lf = null;
104 return string;
105 }
106 finally
107 {
108 IOUtil.close( lf );
109 }
110 }
111
112 }