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