package org.codehaus.mojo.natives;

import java.io.File;
import java.util.List;

import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.util.FileUtils;

public class NativeSourcesTest
    extends PlexusTestCase
{
    File workDirectory;

    protected void setUp()
        throws Exception
    {
        super.setUp();

        workDirectory = new File( getBasedir(), "/target/NativeSourceTest" );
        if ( workDirectory.exists() )
        {
            FileUtils.deleteDirectory( workDirectory );
        }

        workDirectory.mkdirs();
    }

    public void testFileNamesOnly()
        throws Exception
    {
        NativeSources source = new NativeSources();
        source.setDirectory( this.workDirectory );
        String[] fileNames = { "file1.c", "file2.c" };
        source.setFileNames( fileNames );

        List files = source.getFiles();

        assertEquals( 2, files.size() );
    }

    public void testEmptyIncludes()
        throws Exception
    {
        NativeSources source = new NativeSources();
        source.setDirectory( this.workDirectory );
        String[] fileNames = { "file1.c", "file2.c" };
        source.setFileNames( fileNames );

        String[] includes = { "*.c", "*.cpp" };

        source.setIncludes( includes );

        List files = source.getFiles();

        assertEquals( 2, files.size() );
    }

    // merge include wild card list with fileNames array
    public void testMerge()
        throws Exception
    {
        NativeSources source = new NativeSources();
        source.setDirectory( this.workDirectory );
        String[] fileNames = { "file1.c", "file2.c" };
        source.setFileNames( fileNames );

        String[] includes = { "*.c", "*.cpp" };

        //this file is unique comparing to above list 
        File nodupFile = new File( this.workDirectory, "file3.c" );
        nodupFile.createNewFile();

        source.setIncludes( includes );

        List files = source.getFiles();

        assertEquals( 3, files.size() );
    }

    public void testDuplicateFiles()
        throws Exception
    {
        NativeSources source = new NativeSources();
        source.setDirectory( this.workDirectory );
        String[] fileNames = { "file1.c", "file2.c" };
        source.setFileNames( fileNames );

        String[] includes = { "*.c", "*.cpp" };

        File dupFile = new File( this.workDirectory, "file1.c" );
        dupFile.createNewFile();

        source.setIncludes( includes );

        List files = source.getFiles();

        assertEquals( 2, files.size() );
    }

    public void testEmpty()
        throws Exception
    {
        NativeSources source = new NativeSources();
        source.setDirectory( this.workDirectory );
        String[] includes = { "*.*" };
        source.setIncludes( includes );

        List files = source.getFiles();

        assertEquals( 0, files.size() );
    }

    public void testGetAllSourceFiles()
        throws Exception
    {
        NativeSources source = new NativeSources();

        source.setDirectory( this.workDirectory );

        String[] includes = { "*.c", "*.cpp" };
        source.setIncludes( includes );

        String[] fileNames = { "file1.c", "file2.c" };
        source.setFileNames( fileNames );

        //this file is unique comparing to above list 
        File nodupFile = new File( this.workDirectory, "file3.c" );
        nodupFile.createNewFile();

        NativeSources[] sources = { source, source };

        File[] files = NativeSources.getAllSourceFiles( sources );

        assertEquals( 6, files.length );
    }

    public void testEmptyFileNames()
        throws Exception
    {
        NativeSources source = new NativeSources();

        source.setDirectory( this.workDirectory );

        String[] includes = { "*.c", "*.cpp" };
        source.setIncludes( includes );

        File someFile = new File( this.workDirectory, "someFile.c" );
        someFile.createNewFile();
        
        List files = source.getFiles();

        assertEquals( 1, files.size() );

    }

}

@@ -1732,32 +1732,33 @@
 %22 %7D;%0A%0A        //
+ 
 this file is uni
@@ -1776,33 +1776,32 @@
 ng to above list
- 
 %0A        File no
@@ -3271,16 +3271,17 @@
       //
+ 
 this fil
@@ -3315,17 +3315,16 @@
 ove list
- 
 %0A       
@@ -3957,24 +3957,16 @@
 File();%0A
-        
 %0A       
@@ -4048,10 +4048,687 @@
 %0A    %7D%0A%0A
+    public void testExcludes()%0A        throws Exception%0A    %7B%0A        NativeSources source = new NativeSources();%0A%0A        source.setDirectory( this.workDirectory );%0A%0A        String%5B%5D includes = %7B %22*.*%22 %7D;%0A        source.setIncludes( includes );%0A        String%5B%5D excludes = %7B %22*.cpp%22 %7D;%0A        source.setExcludes( excludes );%0A%0A        File someFile = new File( this.workDirectory, %22someFile.c%22 );%0A        someFile.createNewFile();%0A%0A        List files = source.getFiles();%0A%0A        assertEquals( 1, files.size() );%0A        %0A        String %5B%5D excludes2 = %7B %22*.c%22 %7D;%0A        source.setExcludes( excludes2 );%0A%0A        assertEquals( 0, source.getFiles().size() );%0A        %0A%0A    %7D%0A%0A
 %7D%0A

add a NativeSources exclude test