maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdca...@apache.org
Subject svn commit: r425155 - in /maven/plugins/branches/MASSEMBLY-124: ./ src/main/java/org/apache/maven/plugin/assembly/format/ src/main/java/org/apache/maven/plugin/assembly/utils/ src/test/java/org/apache/maven/plugin/assembly/format/ src/test/java/org/apa...
Date Mon, 24 Jul 2006 19:38:18 GMT
Author: jdcasey
Date: Mon Jul 24 12:38:17 2006
New Revision: 425155

URL: http://svn.apache.org/viewvc?rev=425155&view=rev
Log:
Added unit tests for formatters.

Added:
    maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/format/
    maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/format/FileFormatterTest.java
  (with props)
    maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/format/FileSetFormatterTest.java
  (with props)
Modified:
    maven/plugins/branches/MASSEMBLY-124/pom.xml
    maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/format/FileFormatter.java
    maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/format/FileSetFormatter.java
    maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/AssemblyFileUtils.java
    maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/testutils/TestFileManager.java

Modified: maven/plugins/branches/MASSEMBLY-124/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugins/branches/MASSEMBLY-124/pom.xml?rev=425155&r1=425154&r2=425155&view=diff
==============================================================================
--- maven/plugins/branches/MASSEMBLY-124/pom.xml (original)
+++ maven/plugins/branches/MASSEMBLY-124/pom.xml Mon Jul 24 12:38:17 2006
@@ -90,6 +90,11 @@
   </build>
   <dependencies>
     <dependency>
+      <groupId>org.apache.maven.shared</groupId>
+      <artifactId>file-management</artifactId>
+      <version>1.1-SNAPSHOT</version>
+    </dependency>
+    <dependency>
       <groupId>org.apache.maven</groupId>
       <artifactId>maven-plugin-api</artifactId>
       <version>2.0.4</version>
@@ -123,11 +128,6 @@
       <groupId>org.apache.maven</groupId>
       <artifactId>maven-artifact</artifactId>
       <version>2.0.4</version>
-    </dependency>
-    <dependency>
-      <groupId>org.apache.maven.shared</groupId>
-      <artifactId>file-management</artifactId>
-      <version>1.1-SNAPSHOT</version>
     </dependency>
     <dependency>
       <groupId>org.apache.maven.shared</groupId>

Modified: maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/format/FileFormatter.java
URL: http://svn.apache.org/viewvc/maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/format/FileFormatter.java?rev=425155&r1=425154&r2=425155&view=diff
==============================================================================
--- maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/format/FileFormatter.java
(original)
+++ maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/format/FileFormatter.java
Mon Jul 24 12:38:17 2006
@@ -5,6 +5,7 @@
 import org.apache.maven.plugin.assembly.utils.PropertyUtils;
 import org.apache.maven.project.MavenProject;
 import org.codehaus.plexus.logging.Logger;
+import org.codehaus.plexus.util.FileUtils;
 import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.interpolation.MapBasedValueSource;
 import org.codehaus.plexus.util.interpolation.ObjectBasedValueSource;
@@ -14,6 +15,7 @@
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.io.StringReader;
 import java.io.StringWriter;
@@ -24,8 +26,6 @@
 public class FileFormatter
 {
 
-    private static final String LS = System.getProperty( "line.separator", "\n" );;
-
     private final Logger logger;
 
     private final AssemblerConfigurationSource configSource;
@@ -38,50 +38,48 @@
         this.logger = logger;
     }
 
-    public File format( final File source, final boolean filter, final String lineEnding
)
+    // used for unit testing currently.
+    protected FileFormatter( Properties filterProperties, AssemblerConfigurationSource configSource,
Logger logger )
+    {
+        this.filterProperties = filterProperties;
+        this.configSource = configSource;
+        this.logger = logger;
+    }
+
+    public File format( File source, boolean filter, String lineEnding )
         throws AssemblyFormattingException
     {
         File result = source;
 
-        String lineEndingChars;
-        if ( lineEnding != null )
-        {
-            lineEndingChars = AssemblyFileUtils.getLineEndingCharacters( lineEnding );
-        }
-        else
-        {
-            lineEndingChars = LS;
-        }
-
-        initializeFiltering();
-
         File tempRoot = configSource.getTemporaryRootDirectory();
 
         String sourceName = source.getName();
 
-        FileReader fileReader = null;
         try
         {
-            fileReader = new FileReader( source );
-            StringWriter contentWriter = new StringWriter();
-
-            IOUtil.copy( fileReader, contentWriter );
-
-            // support ${token}
-            RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
-            interpolator.addValueSource( new MapBasedValueSource( filterProperties ) );
-            interpolator.addValueSource( new ObjectBasedValueSource( configSource.getProject()
) );
+            boolean contentIsChanged = false;
 
-            String contents = contentWriter.toString();
+            String rawContents = readFile( source );
+            
+            String contents = rawContents;
+            
+            if ( filter )
+            {
+                contents = filter( contents );
+            }
 
-            contents = interpolator.interpolate( contents, "project" );
+            contentIsChanged = !contents.equals( rawContents );
 
             BufferedReader contentReader = new BufferedReader( new StringReader( contents
) );
 
-            File tempFilterFile = File.createTempFile( sourceName + ".", ".filtered", tempRoot
);
-            
-            AssemblyFileUtils.convertLineEndings( contentReader, tempFilterFile, lineEndingChars
);
+            File tempFilterFile = FileUtils.createTempFile( sourceName + ".", ".filtered",
tempRoot );
 
+            boolean fileWritten = formatLineEndings( contentReader, tempFilterFile, lineEnding,
contentIsChanged );
+            
+            if ( fileWritten )
+            {
+                result = tempFilterFile;
+            }
         }
         catch ( FileNotFoundException e )
         {
@@ -91,12 +89,81 @@
         {
             throw new AssemblyFormattingException( "Error filtering file '" + source + "':
" + e.getMessage(), e );
         }
+
+        return result;
+    }
+
+    private String readFile( File source )
+        throws IOException
+    {
+        FileReader fileReader = null;
+
+        StringWriter contentWriter = new StringWriter();
+
+        try
+        {
+            fileReader = new FileReader( source );
+
+            IOUtil.copy( fileReader, contentWriter );
+        }
         finally
         {
             IOUtil.close( fileReader );
         }
 
-        return result;
+        return contentWriter.toString();
+    }
+
+    private boolean formatLineEndings( BufferedReader contentReader, File tempFilterFile,
String lineEnding,
+                                    boolean contentIsChanged )
+        throws IOException, AssemblyFormattingException
+    {
+        boolean fileWritten = false;
+        
+        String lineEndingChars = AssemblyFileUtils.getLineEndingCharacters( lineEnding );
+
+        if ( lineEndingChars != null )
+        {
+            AssemblyFileUtils.convertLineEndings( contentReader, tempFilterFile, lineEndingChars
);
+            
+            fileWritten = true;
+        }
+        else if ( contentIsChanged )
+        {
+            FileWriter fileWriter = null;
+
+            try
+            {
+                fileWriter = new FileWriter( tempFilterFile );
+
+                IOUtil.copy( contentReader, fileWriter );
+                
+                fileWritten = true;
+            }
+            finally
+            {
+                IOUtil.close( fileWriter );
+            }
+        }
+        
+        return fileWritten;
+    }
+
+    private String filter( String rawContents )
+        throws AssemblyFormattingException
+    {
+        initializeFiltering();
+
+        String contents = rawContents;
+
+        // support ${token}
+        RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
+        interpolator.addValueSource( new MapBasedValueSource( filterProperties ) );
+        interpolator.addValueSource( new ObjectBasedValueSource( configSource.getProject()
) );
+
+        contents = interpolator.interpolate( contents, "project" );
+
+        return contents;
     }
 
     private void initializeFiltering()
@@ -123,7 +190,8 @@
 
                     try
                     {
-                        Properties properties = PropertyUtils.getInterpolatedPropertiesFromFile(
new File( filtersfile ), true, true );
+                        Properties properties = PropertyUtils
+                            .getInterpolatedPropertiesFromFile( new File( filtersfile ),
true, true );
 
                         filterProperties.putAll( properties );
                     }

Modified: maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/format/FileSetFormatter.java
URL: http://svn.apache.org/viewvc/maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/format/FileSetFormatter.java?rev=425155&r1=425154&r2=425155&view=diff
==============================================================================
--- maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/format/FileSetFormatter.java
(original)
+++ maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/format/FileSetFormatter.java
Mon Jul 24 12:38:17 2006
@@ -1,5 +1,11 @@
 package org.apache.maven.plugin.assembly.format;
 
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.Reader;
+
 import org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
 import org.apache.maven.plugin.assembly.utils.AssemblyFileUtils;
 import org.apache.maven.shared.model.fileset.FileSet;
@@ -8,19 +14,10 @@
 import org.codehaus.plexus.util.FileUtils;
 import org.codehaus.plexus.util.IOUtil;
 
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileReader;
-import java.io.IOException;
-import java.io.Reader;
-import java.util.List;
-
 
 public class FileSetFormatter
 {
     
-    private static final String[] EMPTY_STRING_ARRAY = new String[0];
-
     private final AssemblerConfigurationSource configSource;
 
     private final Logger logger;
@@ -36,35 +33,37 @@
     {
         File formattedDir = archiveBaseDir;
         
-        if ( set.getLineEnding() != null )
+        String lineEndingHint = set.getLineEnding();
+        
+        String lineEnding = AssemblyFileUtils.getLineEndingCharacters( lineEndingHint );
+
+        if ( lineEnding != null )
         {
             FileSet fileSet = new FileSet();
-            fileSet.setLineEnding( set.getLineEnding() );
+            fileSet.setLineEnding( lineEnding );
             fileSet.setDirectory( set.getDirectory() );
-            fileSet.setExcludes( set.getExcludes() );
             fileSet.setIncludes( set.getIncludes() );
             
-            String lineEnding = AssemblyFileUtils.getLineEndingCharacters( fileSet.getLineEnding()
);
-
-            formattedDir = FileUtils.createTempFile( "", "", configSource.getTemporaryRootDirectory()
);
+            formattedDir = FileUtils.createTempFile( "fileSetFormatter.", ".tmp", configSource.getTemporaryRootDirectory()
);
             
             formattedDir.delete();
             formattedDir.mkdirs();
             
-            String[] includes = (String[]) fileSet.getIncludes().toArray( EMPTY_STRING_ARRAY
);
-            if ( includes.length == 0 )
-            {
-                includes = null;
-            }
-
-            // TODO: default excludes should be in the archiver?
-            List excludesList = fileSet.getExcludes();
-            excludesList.addAll( FileUtils.getDefaultExcludesAsList() );
-            
-            fileSet.setExcludes( excludesList );
+            fileSet.setExcludes( set.getExcludes() );
+            fileSet.setUseDefaultExcludes( true );
             
             FileSetManager fsm = new FileSetManager( logger );
             String[] files = fsm.getIncludedFiles( fileSet );
+            
+            // if we don't have anything to process, let's just skip all of this mess.
+            if ( files == null || files.length == 0 )
+            {
+                logger.info( "No files selected for line-ending conversion. Skipping: " +
fileSet.getDirectory() );
+                
+                formattedDir.delete();
+                
+                return archiveBaseDir;
+            }
             
             for ( int i = 0; i < files.length; i++ )
             {

Modified: maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/AssemblyFileUtils.java
URL: http://svn.apache.org/viewvc/maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/AssemblyFileUtils.java?rev=425155&r1=425154&r2=425155&view=diff
==============================================================================
--- maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/AssemblyFileUtils.java
(original)
+++ maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/AssemblyFileUtils.java
Mon Jul 24 12:38:17 2006
@@ -13,6 +13,12 @@
 public final class AssemblyFileUtils
 {
 
+    public static final String LINE_ENDING_KEEP = "keep";
+    public static final String LINE_ENDING_DOS = "dos";
+    public static final String LINE_ENDING_UNIX = "unix";
+    public static final String LINE_ENDING_CRLF = "crlf";
+    public static final String LINE_ENDING_LF = "lf";
+
     private AssemblyFileUtils()
     {
     }
@@ -66,15 +72,15 @@
         String value = lineEnding;
         if ( lineEnding != null )
         {
-            if ( "keep".equals( lineEnding ) )
+            if ( LINE_ENDING_KEEP.equals( lineEnding ) )
             {
                 value = null;
             }
-            else if ( "dos".equals( lineEnding ) || "crlf".equals( lineEnding ) )
+            else if ( LINE_ENDING_DOS.equals( lineEnding ) || LINE_ENDING_CRLF.equals( lineEnding
) )
             {
                 value = "\r\n";
             }
-            else if ( "unix".equals( lineEnding ) || "lf".equals( lineEnding ) )
+            else if ( LINE_ENDING_UNIX.equals( lineEnding ) || LINE_ENDING_LF.equals( lineEnding
) )
             {
                 value = "\n";
             }

Added: maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/format/FileFormatterTest.java
URL: http://svn.apache.org/viewvc/maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/format/FileFormatterTest.java?rev=425155&view=auto
==============================================================================
--- maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/format/FileFormatterTest.java
(added)
+++ maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/format/FileFormatterTest.java
Mon Jul 24 12:38:17 2006
@@ -0,0 +1,249 @@
+package org.apache.maven.plugin.assembly.format;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.maven.model.Model;
+import org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
+import org.apache.maven.plugin.assembly.testutils.MockManager;
+import org.apache.maven.plugin.assembly.testutils.TestFileManager;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.logging.Logger;
+import org.codehaus.plexus.logging.console.ConsoleLogger;
+import org.easymock.MockControl;
+
+import junit.framework.TestCase;
+
+public class FileFormatterTest
+    extends TestCase
+{
+
+    private Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
+
+    private TestFileManager fileManager = new TestFileManager( "fileFormatterTest.", "" );
+
+    private MockManager mockManager = new MockManager();
+
+    private AssemblerConfigurationSource configSource;
+
+    private MockControl configSourceControl;
+
+    public void setUp()
+    {
+        configSourceControl = MockControl.createControl( AssemblerConfigurationSource.class
);
+        mockManager.add( configSourceControl );
+
+        configSource = (AssemblerConfigurationSource) configSourceControl.getMock();
+    }
+
+    public void tearDown()
+        throws IOException
+    {
+        fileManager.cleanUp();
+    }
+
+    public void testShouldNotTransformOneFile()
+        throws IOException, AssemblyFormattingException
+    {
+        File basedir = fileManager.createTempDir();
+
+        configSource.getTemporaryRootDirectory();
+        configSourceControl.setReturnValue( basedir );
+
+        //        Model model = new Model();
+        //        model.setArtifactId( "artifact" );
+        //        model.setGroupId( "group" );
+        //        model.setVersion( "version" );
+        //        
+        //        MavenProject project = new MavenProject( model );
+        //        
+        //        configSource.getProject();
+        //        configSourceControl.setReturnValue( project );
+
+        File file = fileManager.createFile( basedir, "one.txt", "This is a test." );
+
+        mockManager.replayAll();
+
+        File result = new FileFormatter( configSource, logger ).format( file, false, null
);
+
+        assertEquals( file, result );
+
+        mockManager.verifyAll();
+    }
+
+    // TODO: Should not be appending line-ending at the end if there is none in the source.
+    public void testShouldConvertCRLFLineEndingsInFile()
+        throws IOException, AssemblyFormattingException
+    {
+        File basedir = fileManager.createTempDir();
+
+        configSource.getTemporaryRootDirectory();
+        configSourceControl.setReturnValue( basedir );
+
+        File file = fileManager.createFile( basedir, "one.txt", "This is a\ntest." );
+
+        mockManager.replayAll();
+
+        File result = new FileFormatter( configSource, logger ).format( file, false, "dos"
);
+
+        assertEquals( "This is a\r\ntest.\r\n", fileManager.getFileContents( result ) );
+
+        mockManager.verifyAll();
+    }
+
+    // TODO: Should not be appending line-ending at the end if there is none in the source.
+    public void testShouldConvertLFLineEndingsInFile()
+        throws IOException, AssemblyFormattingException
+    {
+        File basedir = fileManager.createTempDir();
+
+        configSource.getTemporaryRootDirectory();
+        configSourceControl.setReturnValue( basedir );
+
+        File file = fileManager.createFile( basedir, "one.txt", "This is a\r\ntest." );
+
+        mockManager.replayAll();
+
+        File result = new FileFormatter( configSource, logger ).format( file, false, "unix"
);
+
+        assertEquals( "This is a\ntest.\n", fileManager.getFileContents( result ) );
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldFilterProjectExpressionInFile()
+        throws IOException, AssemblyFormattingException
+    {
+        File basedir = fileManager.createTempDir();
+
+        enableBasicFilteringConfiguration( basedir, Collections.EMPTY_LIST );
+
+        File file = fileManager.createFile( basedir, "one.txt", "This is a test for project:
${artifactId}." );
+
+        mockManager.replayAll();
+
+        File result = new FileFormatter( configSource, logger ).format( file, true, null
);
+
+        assertEquals( "This is a test for project: artifact.", fileManager.getFileContents(
result ) );
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldFilterExpressionFromFiltersFileInFile()
+        throws IOException, AssemblyFormattingException
+    {
+        File basedir = fileManager.createTempDir();
+
+        File filterProps = fileManager.createFile( basedir, "filter.properties", "property=Test"
);
+
+        enableBasicFilteringConfiguration( basedir, Collections.singletonList( filterProps.getCanonicalPath()
) );
+
+        File file = fileManager.createFile( basedir, "one.txt", "This is a test for project:
${property}." );
+
+        mockManager.replayAll();
+
+        File result = new FileFormatter( configSource, logger ).format( file, true, null
);
+
+        assertEquals( "This is a test for project: Test.", fileManager.getFileContents( result
) );
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldFilterExpressionsFromTwoFiltersFilesInFile()
+        throws IOException, AssemblyFormattingException
+    {
+        File basedir = fileManager.createTempDir();
+
+        File filterProps = fileManager.createFile( basedir, "filter.properties", "property=Test"
);
+        File filterProps2 = fileManager.createFile( basedir, "filter2.properties", "otherProperty=OtherValue"
);
+
+        List filters = new ArrayList();
+        filters.add( filterProps.getCanonicalPath() );
+        filters.add( filterProps2.getCanonicalPath() );
+
+        enableBasicFilteringConfiguration( basedir, filters );
+
+        File file = fileManager.createFile( basedir, "one.txt",
+                                            "property: ${property}  otherProperty: ${otherProperty}."
);
+
+        mockManager.replayAll();
+
+        File result = new FileFormatter( configSource, logger ).format( file, true, null
);
+
+        assertEquals( "property: Test  otherProperty: OtherValue.", fileManager.getFileContents(
result ) );
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldOverrideOneFilterValueWithAnotherAndFilterFile()
+        throws IOException, AssemblyFormattingException
+    {
+        File basedir = fileManager.createTempDir();
+
+        File filterProps = fileManager.createFile( basedir, "filter.properties", "property=Test"
);
+        File filterProps2 = fileManager.createFile( basedir, "filter2.properties", "property=OtherValue"
);
+
+        List filters = new ArrayList();
+        filters.add( filterProps.getCanonicalPath() );
+        filters.add( filterProps2.getCanonicalPath() );
+
+        enableBasicFilteringConfiguration( basedir, filters );
+
+        File file = fileManager.createFile( basedir, "one.txt", "property: ${property}."
);
+
+        mockManager.replayAll();
+
+        File result = new FileFormatter( configSource, logger ).format( file, true, null
);
+
+        assertEquals( "property: OtherValue.", fileManager.getFileContents( result ) );
+
+        mockManager.verifyAll();
+    }
+
+    public void testShouldOverrideProjectValueWithFilterValueAndFilterFile()
+        throws IOException, AssemblyFormattingException
+    {
+        File basedir = fileManager.createTempDir();
+
+        File filterProps = fileManager.createFile( basedir, "filter.properties", "artifactId=Test"
);
+
+        List filters = new ArrayList();
+        filters.add( filterProps.getCanonicalPath() );
+
+        enableBasicFilteringConfiguration( basedir, filters );
+
+        File file = fileManager.createFile( basedir, "one.txt", "project artifact-id: ${artifactId}."
);
+
+        mockManager.replayAll();
+
+        File result = new FileFormatter( configSource, logger ).format( file, true, null
);
+
+        assertEquals( "project artifact-id: Test.", fileManager.getFileContents( result )
);
+
+        mockManager.verifyAll();
+    }
+
+    private void enableBasicFilteringConfiguration( File basedir, List filterFilenames )
+    {
+        configSource.getTemporaryRootDirectory();
+        configSourceControl.setReturnValue( basedir );
+
+        Model model = new Model();
+        model.setArtifactId( "artifact" );
+        model.setGroupId( "group" );
+        model.setVersion( "version" );
+
+        MavenProject project = new MavenProject( model );
+
+        configSource.getProject();
+        configSourceControl.setReturnValue( project, MockControl.ONE_OR_MORE );
+
+        // list of filenames that contain filter definitions.
+        configSource.getFilters();
+        configSourceControl.setReturnValue( filterFilenames );
+    }
+
+}

Propchange: maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/format/FileFormatterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/format/FileFormatterTest.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/format/FileSetFormatterTest.java
URL: http://svn.apache.org/viewvc/maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/format/FileSetFormatterTest.java?rev=425155&view=auto
==============================================================================
--- maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/format/FileSetFormatterTest.java
(added)
+++ maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/format/FileSetFormatterTest.java
Mon Jul 24 12:38:17 2006
@@ -0,0 +1,205 @@
+package org.apache.maven.plugin.assembly.format;
+
+import java.io.File;
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+import org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
+import org.apache.maven.plugin.assembly.testutils.MockManager;
+import org.apache.maven.plugin.assembly.testutils.TestFileManager;
+import org.apache.maven.plugin.assembly.utils.AssemblyFileUtils;
+import org.apache.maven.plugins.assembly.model.FileSet;
+import org.codehaus.plexus.logging.Logger;
+import org.codehaus.plexus.logging.console.ConsoleLogger;
+import org.easymock.MockControl;
+
+public class FileSetFormatterTest
+    extends TestCase
+{
+
+    private MockManager mockManager;
+
+    private TestFileManager fileManager;
+
+    private Logger logger;
+
+    private AssemblerConfigurationSource configSource;
+
+    private MockControl configSourceControl;
+
+    public void setUp()
+    {
+        mockManager = new MockManager();
+
+        fileManager = new TestFileManager( "fileSetFormatter-test.", "" );
+
+        logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
+
+        configSourceControl = MockControl.createControl( AssemblerConfigurationSource.class
);
+        mockManager.add( configSourceControl );
+
+        configSource = (AssemblerConfigurationSource) configSourceControl.getMock();
+    }
+
+    public void tearDown()
+        throws IOException
+    {
+        fileManager.cleanUp();
+    }
+
+    public void testShouldReturnOriginalUnalteredDirectoryWhenLineEndingIsNull()
+        throws AssemblyFormattingException, IOException
+    {
+        FileSet fs = new FileSet();
+
+        FileSetFormatter formatter = new FileSetFormatter( configSource, logger );
+
+        File dir = fileManager.createTempDir();
+
+        File result = formatter.formatFileSetForAssembly( dir, fs );
+
+        assertEquals( dir, result );
+    }
+
+    public void testShouldReturnOriginalUnalteredDirectoryWhenLineEndingIsKeep()
+        throws AssemblyFormattingException, IOException
+    {
+        FileSet fs = new FileSet();
+        fs.setLineEnding( AssemblyFileUtils.LINE_ENDING_KEEP );
+
+        FileSetFormatter formatter = new FileSetFormatter( configSource, logger );
+
+        File dir = fileManager.createTempDir();
+
+        File result = formatter.formatFileSetForAssembly( dir, fs );
+
+        assertEquals( dir, result );
+    }
+
+    public void testShouldReturnOriginalUnalteredDirectoryWhenIncludedFileSetIsEmpty()
+        throws AssemblyFormattingException, IOException
+    {
+        File dir = fileManager.createTempDir();
+
+        FileSet fs = new FileSet();
+
+        fs.setLineEnding( AssemblyFileUtils.LINE_ENDING_LF );
+        fs.setDirectory( dir.getCanonicalPath() );
+        fs.addExclude( "**/*" );
+
+        FileSetFormatter formatter = new FileSetFormatter( configSource, logger );
+
+        File result = formatter.formatFileSetForAssembly( dir, fs );
+
+        assertEquals( dir, result );
+    }
+
+    public void testShouldConvertLineEndingsOnTwoFiles()
+        throws AssemblyFormattingException, IOException
+    {
+        File dir = fileManager.createTempDir();
+
+        String filename1 = "one.txt";
+        String filename2 = "two.txt";
+
+        fileManager.createFile( dir, filename1, "Hello\nThis is a test." );
+        fileManager.createFile( dir, filename2, "Hello\nThis is also a test." );
+
+        FileSet fs = new FileSet();
+
+        fs.setLineEnding( AssemblyFileUtils.LINE_ENDING_CRLF );
+        fs.setDirectory( dir.getCanonicalPath() );
+        fs.addInclude( "**/*.txt" );
+
+        FileSetFormatter formatter = new FileSetFormatter( configSource, logger );
+
+        File result = formatter.formatFileSetForAssembly( dir, fs );
+
+        assertFalse( dir.equals( result ) );
+
+        fileManager.assertFileContents( result, filename1, "Hello\r\nThis is a test.\r\n"
);
+        fileManager.assertFileContents( result, filename2, "Hello\r\nThis is also a test.\r\n"
);
+    }
+
+    public void testShouldConvertLineEndingsOnOneFileWithAnotherExplicitlyExcluded()
+        throws AssemblyFormattingException, IOException
+    {
+        File dir = fileManager.createTempDir();
+
+        String filename1 = "one.txt";
+        String filename2 = "two.txt";
+
+        fileManager.createFile( dir, filename1, "Hello\nThis is a test." );
+        fileManager.createFile( dir, filename2, "Hello\nThis is also a test." );
+
+        FileSet fs = new FileSet();
+
+        fs.setLineEnding( AssemblyFileUtils.LINE_ENDING_CRLF );
+        fs.setDirectory( dir.getCanonicalPath() );
+        fs.addExclude( "**/two.txt" );
+
+        FileSetFormatter formatter = new FileSetFormatter( configSource, logger );
+
+        File result = formatter.formatFileSetForAssembly( dir, fs );
+
+        assertFalse( dir.equals( result ) );
+
+        fileManager.assertFileContents( result, filename1, "Hello\r\nThis is a test.\r\n"
);
+        fileManager.assertFileExistence( result, filename2, false );
+    }
+
+    public void testShouldConvertLineEndingsOnOneExplicitlyIncludedFile()
+        throws AssemblyFormattingException, IOException
+    {
+        File dir = fileManager.createTempDir();
+
+        String filename1 = "one.txt";
+        String filename2 = "two.txt";
+
+        fileManager.createFile( dir, filename1, "Hello\nThis is a test." );
+        fileManager.createFile( dir, filename2, "Hello\nThis is also a test." );
+
+        FileSet fs = new FileSet();
+
+        fs.setLineEnding( AssemblyFileUtils.LINE_ENDING_CRLF );
+        fs.setDirectory( dir.getCanonicalPath() );
+        fs.addInclude( "**/one.txt" );
+
+        FileSetFormatter formatter = new FileSetFormatter( configSource, logger );
+
+        File result = formatter.formatFileSetForAssembly( dir, fs );
+
+        assertFalse( dir.equals( result ) );
+
+        fileManager.assertFileContents( result, filename1, "Hello\r\nThis is a test.\r\n"
);
+        fileManager.assertFileExistence( result, filename2, false );
+    }
+
+    public void testShouldConvertLineEndingsOnOneFileAndIgnoreFileWithinDefaultExcludedDir()
+        throws AssemblyFormattingException, IOException
+    {
+        File dir = fileManager.createTempDir();
+
+        String filename1 = "one.txt";
+        String filename2 = "CVS/two.txt";
+
+        fileManager.createFile( dir, filename1, "Hello\nThis is a test." );
+        fileManager.createFile( dir, filename2, "Hello\nThis is also a test." );
+
+        FileSet fs = new FileSet();
+
+        fs.setLineEnding( AssemblyFileUtils.LINE_ENDING_CRLF );
+        fs.setDirectory( dir.getCanonicalPath() );
+
+        FileSetFormatter formatter = new FileSetFormatter( configSource, logger );
+
+        File result = formatter.formatFileSetForAssembly( dir, fs );
+
+        assertFalse( dir.equals( result ) );
+
+        fileManager.assertFileContents( result, filename1, "Hello\r\nThis is a test.\r\n"
);
+        fileManager.assertFileExistence( result, filename2, false );
+    }
+
+}

Propchange: maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/format/FileSetFormatterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/format/FileSetFormatterTest.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Modified: maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/testutils/TestFileManager.java
URL: http://svn.apache.org/viewvc/maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/testutils/TestFileManager.java?rev=425155&r1=425154&r2=425155&view=diff
==============================================================================
--- maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/testutils/TestFileManager.java
(original)
+++ maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/testutils/TestFileManager.java
Mon Jul 24 12:38:17 2006
@@ -1,13 +1,20 @@
 package org.apache.maven.plugin.assembly.testutils;
 
 import org.codehaus.plexus.util.FileUtils;
+import org.codehaus.plexus.util.IOUtil;
 
 import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
 import java.io.IOException;
+import java.io.StringReader;
+import java.io.StringWriter;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 
+import junit.framework.Assert;
+
 public class TestFileManager
 {
 
@@ -79,6 +86,91 @@
 
             it.remove();
         }
+    }
+
+    public void assertFileExistence( File dir, String filename, boolean shouldExist )
+    {
+        File file = new File( dir, filename );
+
+        if ( shouldExist )
+        {
+            Assert.assertTrue( file.exists() );
+        }
+        else
+        {
+            Assert.assertFalse( file.exists() );
+        }
+    }
+
+    public void assertFileContents( File dir, String filename, String contentsTest )
+        throws IOException
+    {
+        assertFileExistence( dir, filename, true );
+
+        File file = new File( dir, filename );
+
+        FileReader reader = null;
+        StringWriter writer = new StringWriter();
+
+        try
+        {
+            reader = new FileReader( file );
+
+            IOUtil.copy( reader, writer );
+        }
+        finally
+        {
+            IOUtil.close( reader );
+        }
+
+        Assert.assertEquals( contentsTest, writer.toString() );
+    }
+
+    public File createFile( File dir, String filename, String contents )
+        throws IOException
+    {
+        File file = new File( dir, filename );
+
+        file.getParentFile().mkdirs();
+
+        FileWriter writer = null;
+
+        try
+        {
+            writer = new FileWriter( file );
+
+            IOUtil.copy( new StringReader( contents ), writer );
+        }
+        finally
+        {
+            IOUtil.close( writer );
+        }
+
+        return file;
+    }
+
+    public String getFileContents( File file )
+        throws IOException
+    {
+        String result = null;
+        
+        FileReader reader = null;
+        try
+        {
+            reader = new FileReader( file );
+            
+            StringWriter writer = new StringWriter();
+            
+            IOUtil.copy( reader, writer );
+            
+            result = writer.toString();
+        }
+        finally
+        {
+            IOUtil.close( reader );
+        }
+        
+        return result;
     }
 
 }



Mime
View raw message