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