View Javadoc
1   package org.apache.maven.plugins.assembly.utils;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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 }