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 WindowsLineFeedInputStreamTest
31 {
32
33 @Test
34 public void testSimpleString()
35 throws Exception
36 {
37 assertEquals( "abc\r\n", roundtrip( "abc" ) );
38 }
39
40 @Test
41 public void testInTheMiddleOfTheLine()
42 throws Exception
43 {
44 assertEquals( "a\r\nbc\r\n", roundtrip( "a\r\nbc" ) );
45 }
46
47 @Test
48 public void testMultipleBlankLines()
49 throws Exception
50 {
51 assertEquals( "a\r\n\r\nbc\r\n", roundtrip( "a\r\n\r\nbc" ) );
52 }
53
54 @Test
55 public void testTwoLinesAtEnd()
56 throws Exception
57 {
58 assertEquals( "a\r\n\r\n", roundtrip( "a\r\n\r\n" ) );
59 }
60
61 @Test
62 public void testLinuxLinefeeds()
63 throws Exception
64 {
65 final String roundtrip = roundtrip( "ab\nc", false );
66 assertEquals( "ab\r\nc", roundtrip );
67 }
68
69
70 @Test
71 public void testMalformed()
72 throws Exception
73 {
74 assertEquals( "a\rbc", roundtrip( "a\rbc", false ) );
75 }
76
77 @Test
78 public void testRetainLineFeed()
79 throws Exception
80 {
81 assertEquals( "a\r\n\r\n", roundtrip( "a\r\n\r\n", false ) );
82 assertEquals( "a", roundtrip( "a", false ) );
83 }
84
85 private String roundtrip( String msg )
86 throws IOException
87 {
88 return roundtrip( msg, true );
89 }
90
91 private String roundtrip( String msg, boolean ensure )
92 throws IOException
93 {
94 ByteArrayInputStream baos = new ByteArrayInputStream( msg.getBytes() );
95
96 WindowsLineFeedInputStream lf = null;
97 try
98 {
99 lf = new WindowsLineFeedInputStream( baos, ensure );
100 byte[] buf = new byte[ 100 ];
101 final int read = lf.read( buf );
102 final String string = new String( buf, 0, read );
103 lf.close();
104 lf = null;
105 return string;
106 }
107 finally
108 {
109 IOUtil.close( lf );
110 }
111 }
112 }