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.File;
22  import java.io.FileReader;
23  import java.io.FileWriter;
24  import java.io.IOException;
25  import java.io.StringReader;
26  import java.io.StringWriter;
27  import java.nio.file.Files;
28  
29  import org.apache.commons.io.IOUtils;
30  import org.apache.maven.plugins.assembly.format.AssemblyFormattingException;
31  import org.junit.Test;
32  
33  import static org.junit.Assert.assertEquals;
34  import static org.junit.Assert.assertNull;
35  
36  public class LineEndingsUtilsTest {
37  
38      private static final String CRLF = "\r\n";
39  
40      private static final String LF = "\n";
41  
42      @Test
43      public void shouldWorkCauseWeTestJdkEnumConversion() {
44          LineEndings lineEnding = LineEndings.valueOf("windows");
45          assertEquals(CRLF, lineEnding.getLineEndingCharacters());
46      }
47  
48      @Test
49      public void shouldReturnDosLineEnding() {
50          assertEquals(CRLF, LineEndings.windows.getLineEndingCharacters());
51          assertEquals(CRLF, LineEndings.dos.getLineEndingCharacters());
52          assertEquals(CRLF, LineEndings.crlf.getLineEndingCharacters());
53      }
54  
55      @Test
56      public void shouldReturnUnixLineEnding() {
57          assertEquals(LF, LineEndings.unix.getLineEndingCharacters());
58          assertEquals(LF, LineEndings.lf.getLineEndingCharacters());
59      }
60  
61      @Test
62      public void shouldReturnNullAsLineEndingForKeep() {
63          assertNull(LineEndings.keep.getLineEndingCharacters());
64      }
65  
66      @Test
67      public void testGetLineEndingChars_ShouldReturnDosLineEnding() throws AssemblyFormattingException {
68          assertEquals("\r\n", LineEndingsUtils.getLineEndingCharacters("windows"));
69          assertEquals("\r\n", LineEndingsUtils.getLineEndingCharacters("dos"));
70          assertEquals("\r\n", LineEndingsUtils.getLineEndingCharacters("crlf"));
71      }
72  
73      @Test
74      public void testGetLineEndingChars_ShouldReturnUnixLineEnding() throws AssemblyFormattingException {
75          assertEquals("\n", LineEndingsUtils.getLineEndingCharacters("unix"));
76          assertEquals("\n", LineEndingsUtils.getLineEndingCharacters("lf"));
77      }
78  
79      @Test
80      public void testGetLineEndingChars_ShouldReturnNullLineEnding() throws AssemblyFormattingException {
81          assertNull(LineEndingsUtils.getLineEndingCharacters("keep"));
82      }
83  
84      @Test(expected = AssemblyFormattingException.class)
85      public void testGetLineEndingChars_ShouldThrowFormattingExceptionWithInvalidHint()
86              throws AssemblyFormattingException {
87          LineEndingsUtils.getLineEndingCharacters("invalid");
88      }
89  
90      @Test
91      public void testConvertLineEndings_ShouldReplaceLFWithCRLF() throws IOException {
92          String test = "This is a \ntest.";
93          String check = "This is a \r\ntest.";
94  
95          testConversion(test, check, LineEndings.crlf, null);
96      }
97  
98      @Test
99      public void testConvertLineEndings_ShouldReplaceLFWithCRLFAtEOF() throws IOException {
100         String test = "This is a \ntest.\n";
101         String check = "This is a \r\ntest.\r\n";
102 
103         testConversion(test, check, LineEndings.crlf, null);
104     }
105 
106     @Test
107     public void testConvertLineEndings_ShouldReplaceCRLFWithLF() throws IOException {
108         String test = "This is a \r\ntest.";
109         String check = "This is a \ntest.";
110 
111         testConversion(test, check, LineEndings.lf, null);
112     }
113 
114     @Test
115     public void testConvertLineEndings_ShouldReplaceCRLFWithLFAtEOF() throws IOException {
116         String test = "This is a \r\ntest.\r\n";
117         String check = "This is a \ntest.\n";
118 
119         testConversion(test, check, LineEndings.lf, null);
120     }
121 
122     @Test
123     public void testConvertLineEndings_ShouldReplaceLFWithLF() throws IOException {
124         String test = "This is a \ntest.";
125         String check = "This is a \ntest.";
126 
127         testConversion(test, check, LineEndings.lf, null);
128     }
129 
130     @Test
131     public void testConvertLineEndings_ShouldReplaceLFWithLFAtEOF() throws IOException {
132         String test = "This is a \ntest.\n";
133         String check = "This is a \ntest.\n";
134 
135         testConversion(test, check, LineEndings.lf, null);
136     }
137 
138     @Test
139     public void testConvertLineEndings_ShouldReplaceCRLFWithCRLF() throws IOException {
140         String test = "This is a \r\ntest.";
141         String check = "This is a \r\ntest.";
142 
143         testConversion(test, check, LineEndings.crlf, null);
144     }
145 
146     @Test
147     public void testConvertLineEndings_ShouldReplaceCRLFWithCRLFAtEOF() throws IOException {
148         String test = "This is a \r\ntest.\r\n";
149         String check = "This is a \r\ntest.\r\n";
150 
151         testConversion(test, check, LineEndings.crlf, null);
152     }
153 
154     @Test
155     public void testConvertLineEndings_LFToCRLFNoEOFForceEOF() throws IOException {
156         String test = "This is a \ntest.";
157         String check = "This is a \r\ntest.\r\n";
158 
159         testConversion(test, check, LineEndings.crlf, true);
160     }
161 
162     @Test
163     public void testConvertLineEndings_LFToCRLFWithEOFForceEOF() throws IOException {
164         String test = "This is a \ntest.\n";
165         String check = "This is a \r\ntest.\r\n";
166 
167         testConversion(test, check, LineEndings.crlf, true);
168     }
169 
170     @Test
171     public void testConvertLineEndings_LFToCRLFNoEOFStripEOF() throws IOException {
172         String test = "This is a \ntest.";
173         String check = "This is a \r\ntest.";
174 
175         testConversion(test, check, LineEndings.crlf, false);
176     }
177 
178     @Test
179     public void testConvertLineEndings_LFToCRLFWithEOFStripEOF() throws IOException {
180         String test = "This is a \ntest.\n";
181         String check = "This is a \r\ntest.";
182 
183         testConversion(test, check, LineEndings.crlf, false);
184     }
185 
186     @Test
187     public void testConvertLineEndings_CRLFToLFNoEOFForceEOF() throws IOException {
188         String test = "This is a \r\ntest.";
189         String check = "This is a \ntest.\n";
190 
191         testConversion(test, check, LineEndings.lf, true);
192     }
193 
194     @Test
195     public void testConvertLineEndings_CRLFToLFWithEOFForceEOF() throws IOException {
196         String test = "This is a \r\ntest.\r\n";
197         String check = "This is a \ntest.\n";
198 
199         testConversion(test, check, LineEndings.lf, true);
200     }
201 
202     @Test
203     public void testConvertLineEndings_CRLFToLFNoEOFStripEOF() throws IOException {
204         String test = "This is a \r\ntest.";
205         String check = "This is a \ntest.";
206 
207         testConversion(test, check, LineEndings.lf, false);
208     }
209 
210     @Test
211     public void testConvertLineEndings_CRLFToLFWithEOFStripEOF() throws IOException {
212         String test = "This is a \r\ntest.\r\n";
213         String check = "This is a \ntest.";
214 
215         testConversion(test, check, LineEndings.lf, false);
216     }
217 
218     private void testConversion(String test, String check, LineEndings lineEndingChars, Boolean eof)
219             throws IOException {
220         File source = Files.createTempFile("line-conversion-test-in.", "").toFile();
221         source.deleteOnExit();
222         File dest = Files.createTempFile("line-conversion-test-out.", "").toFile();
223         dest.deleteOnExit();
224 
225         try (StringReader sourceReader = new StringReader(test);
226                 FileWriter sourceWriter = new FileWriter(source)) {
227             IOUtils.copy(sourceReader, sourceWriter);
228         }
229 
230         // Using platform encoding for the conversion tests in this class is OK
231         LineEndingsUtils.convertLineEndings(source, dest, lineEndingChars, eof, null);
232 
233         try (FileReader destReader = new FileReader(dest);
234                 StringWriter destWriter = new StringWriter()) {
235             IOUtils.copy(destReader, destWriter);
236             assertEquals(check, destWriter.toString());
237         }
238     }
239 }