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