View Javadoc

1   package org.apache.maven.plugin.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 java.io.File;
23  import java.io.FileReader;
24  import java.io.IOException;
25  import java.io.StringReader;
26  import java.io.StringWriter;
27  
28  import junit.framework.TestCase;
29  
30  import org.apache.maven.plugin.assembly.archive.ArchiveExpansionException;
31  import org.apache.maven.plugin.assembly.format.AssemblyFormattingException;
32  import org.apache.maven.plugin.assembly.testutils.MockManager;
33  import org.apache.maven.plugin.assembly.testutils.TestFileManager;
34  import org.codehaus.plexus.archiver.ArchiverException;
35  import org.codehaus.plexus.archiver.UnArchiver;
36  import org.codehaus.plexus.archiver.manager.ArchiverManager;
37  import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
38  import org.codehaus.plexus.util.IOUtil;
39  import org.easymock.MockControl;
40  
41  public class AssemblyFileUtilsTest
42      extends TestCase
43  {
44  
45      private TestFileManager fileManager = new TestFileManager( "file-utils.test.", "" );
46  
47      public void tearDown()
48          throws IOException
49      {
50          fileManager.cleanUp();
51      }
52  
53      public void testUnpack_ShouldSetSourceAndDestinationAndCallExtract()
54          throws IOException, ArchiveExpansionException, NoSuchArchiverException
55      {
56          MockManager mockManager = new MockManager();
57  
58          File source = fileManager.createTempFile();
59          File destDir = fileManager.createTempDir();
60  
61          MockControl unarchiverCtl = MockControl.createControl( UnArchiver.class );
62          mockManager.add( unarchiverCtl );
63  
64          UnArchiver unarchiver = (UnArchiver) unarchiverCtl.getMock();
65  
66          MockControl archiverManagerCtl = MockControl.createControl( ArchiverManager.class );
67          mockManager.add( archiverManagerCtl );
68  
69          ArchiverManager archiverManager = (ArchiverManager) archiverManagerCtl.getMock();
70  
71          try
72          {
73              archiverManager.getUnArchiver( source );
74              archiverManagerCtl.setReturnValue( unarchiver );
75          }
76          catch ( NoSuchArchiverException e )
77          {
78              fail( "Should never happen." );
79          }
80  
81          unarchiver.setSourceFile( source );
82          unarchiver.setDestDirectory( destDir );
83  
84          try
85          {
86              unarchiver.extract();
87          }
88          catch ( ArchiverException e )
89          {
90              fail( "Should never happen." );
91          }
92  
93          mockManager.replayAll();
94  
95          AssemblyFileUtils.unpack( source, destDir, archiverManager );
96  
97          mockManager.verifyAll();
98      }
99  
100     public void testGetLineEndingChars_ShouldReturnDosLineEnding()
101         throws AssemblyFormattingException
102     {
103         assertEquals( "\r\n", AssemblyFileUtils.getLineEndingCharacters( "dos" ) );
104         assertEquals( "\r\n", AssemblyFileUtils.getLineEndingCharacters( "crlf" ) );
105     }
106 
107     public void testGetLineEndingChars_ShouldReturnUnixLineEnding()
108         throws AssemblyFormattingException
109     {
110         assertEquals( "\n", AssemblyFileUtils.getLineEndingCharacters( "unix" ) );
111         assertEquals( "\n", AssemblyFileUtils.getLineEndingCharacters( "lf" ) );
112     }
113 
114     public void testGetLineEndingChars_ShouldReturnNullLineEnding()
115         throws AssemblyFormattingException
116     {
117         assertNull( AssemblyFileUtils.getLineEndingCharacters( "keep" ) );
118     }
119 
120     public void testGetLineEndingChars_ShouldThrowFormattingExceptionWithInvalidHint()
121     {
122         try
123         {
124             AssemblyFileUtils.getLineEndingCharacters( "invalid" );
125 
126             fail( "Invalid line-ending hint should throw a formatting exception." );
127         }
128         catch ( AssemblyFormattingException e )
129         {
130         }
131     }
132 
133     // TODO: Fix the end-of-document problem with line-ending conversions.
134     public void testConvertLineEndings_ShouldReplaceLFWithCRLF()
135         throws IOException
136     {
137         String test = "This is a \ntest.";
138         String check = "This is a \r\ntest.\r\n";
139 
140         testConversion( test, check, "\r\n" );
141     }
142 
143     // TODO: Fix the end-of-document problem with line-ending conversions.
144     public void testConvertLineEndings_ShouldReplaceCRLFWithLF()
145         throws IOException
146     {
147         String test = "This is a \r\ntest.";
148         String check = "This is a \ntest.\n";
149 
150         testConversion( test, check, "\n" );
151     }
152 
153     // TODO: Fix the end-of-document problem with line-ending conversions.
154     public void testConvertLineEndings_ShouldReplaceLFWithLF()
155         throws IOException
156     {
157         String test = "This is a \ntest.";
158         String check = "This is a \ntest.\n";
159 
160         testConversion( test, check, "\n" );
161     }
162 
163     // TODO: Fix the end-of-document problem with line-ending conversions.
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.\r\n";
169 
170         testConversion( test, check, "\r\n" );
171     }
172 
173     private void testConversion( String test, String check, String lineEndingChars )
174         throws IOException
175     {
176         File dest = File.createTempFile( "line-conversion-test.", "" );
177         dest.deleteOnExit();
178 
179         AssemblyFileUtils.convertLineEndings( new StringReader( test ), dest, lineEndingChars );
180 
181         FileReader reader = null;
182         StringWriter writer = new StringWriter();
183 
184         try
185         {
186             reader = new FileReader( dest );
187 
188             IOUtil.copy( reader, writer );
189         }
190         finally
191         {
192             IOUtil.close( reader );
193         }
194 
195         assertEquals( check, writer.toString() );
196     }
197 
198 }