View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.assembly.utils;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.IOException;
23  
24  import org.junit.Test;
25  
26  import static org.junit.Assert.assertEquals;
27  
28  public class LinuxLineFeedInputStreamTest {
29  
30      @Test
31      public void testSimpleString() throws Exception {
32          assertEquals("abc\n", roundtrip("abc"));
33      }
34  
35      @Test
36      public void testInTheMiddleOfTheLine() throws Exception {
37          assertEquals("a\nbc\n", roundtrip("a\r\nbc"));
38      }
39  
40      @Test
41      public void testCrOnly() throws Exception {
42          assertEquals("a\nb\n", roundtrip("a\rb"));
43      }
44  
45      @Test
46      public void testCrAtEnd() throws Exception {
47          assertEquals("a\n", roundtrip("a\r"));
48      }
49  
50      @Test
51      public void testMultipleBlankLines() throws Exception {
52          assertEquals("a\n\nbc\n", roundtrip("a\r\n\r\nbc"));
53      }
54  
55      @Test
56      public void testTwoLinesAtEnd() throws Exception {
57          assertEquals("a\n\n", roundtrip("a\r\n\r\n"));
58      }
59  
60      @Test
61      public void testRetainLineFeed() throws Exception {
62          assertEquals("a\n\n", roundtrip("a\r\n\r\n", false));
63          assertEquals("a", roundtrip("a", false));
64      }
65  
66      private String roundtrip(String msg) throws IOException {
67          return roundtrip(msg, true);
68      }
69  
70      private String roundtrip(String msg, boolean ensure) throws IOException {
71          ByteArrayInputStream baos = new ByteArrayInputStream(msg.getBytes());
72  
73          try (LinuxLineFeedInputStream lf = new LinuxLineFeedInputStream(baos, ensure)) {
74              byte[] buf = new byte[100];
75              final int read = lf.read(buf);
76              return new String(buf, 0, read);
77          }
78      }
79  }