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