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 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 }