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