/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.io;

import java.io.File;
import java.util.EnumSet;
import java.util.List;
import static net.ranides.assira.io.FileFilter.AcceptMode.*;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author ranides
 */
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public class DirectoryHelperTest {
    
    public DirectoryHelperTest() {
    }

    @Test
    public void testSourceListing() {
        
        FileFilters.Wildcard wfilter = 
            new FileFilters.Wildcard()
                .include("*")
                .exclude("*/.svn")
                .exclude("*/.svn/*");
        
        wfilter.setMode(EnumSet.of(FILE, DIRECTORY, RECURSIVE));
        List<File> wall = DirectoryHelper.list(new File("./src/main/java"), wfilter);
        
        wfilter.setMode(EnumSet.of(DIRECTORY, RECURSIVE));
        List<File> wdirs = DirectoryHelper.list(new File("./src/main/java"), wfilter);
        
        wfilter.setMode(EnumSet.of(FILE, RECURSIVE));
        List<File> wfiles = DirectoryHelper.list(new File("./src/main/java"), wfilter);
        
        assertTrue("(files + dirs) should be equal (all)", wall.size() == wdirs.size()+wfiles.size()); // NOPMD
        
        assertTrue("should find many '.java' files", wall.size() > 190);
        
        
        FileFilters.Regex rfilter = 
            new FileFilters.Regex()
                .include(".*")
                .exclude(".*/\\.svn")
                .exclude(".*/\\.svn/.*");
        
        rfilter.setMode(EnumSet.of(FILE, DIRECTORY, RECURSIVE));
        List<File> rall = DirectoryHelper.list(new File("./src/main/java"), rfilter);
        
        rfilter.setMode(EnumSet.of(DIRECTORY, RECURSIVE));
        List<File> rdirs = DirectoryHelper.list(new File("./src/main/java"), rfilter);
        
        rfilter.setMode(EnumSet.of(FILE, RECURSIVE));
        List<File> rfiles = DirectoryHelper.list(new File("./src/main/java"), rfilter);
        
        assertTrue("result should have the same entries, order is not important", wall.containsAll(rall) && rall.containsAll(wall) );
        assertTrue("result should have the same entries, order is not important", wdirs.containsAll(rdirs) && rdirs.containsAll(wdirs) );
        assertTrue("result should have the same entries, order is not important", wfiles.containsAll(rfiles) && rfiles.containsAll(wfiles) );
        
    }
    
    @Test
    public void testSourceFind() {
        
        List<File> a = DirectoryHelper.list(
            new File("./src/main/java"), 
            new FileFilters.Wildcard(EnumSet.of(FILE, RECURSIVE))
                .include("*Event.java")
                .include("*/Event*.java")
                .exclude("*/.svn")
                .exclude("*/.svn/*")
        );
        
        List<File> b = DirectoryHelper.list(
            new File("./src/main/java"), 
            new FileFilters.Wildcard(EnumSet.of(FILE, RECURSIVE))
                .include("*Event*")
                .exclude("*/.svn")
                .exclude("*/.svn/*")
        );
        
        assertTrue(a.size() < b.size() && a.size()>1 );
    }
    
    @Test
    public void testList() {
        List<File> files = DirectoryHelper.listFiles(
            new File("./src/main/java/net/ranides/assira/collection"), 
            "*.java"
        );
        
        List<File> packages = DirectoryHelper.listDirs(
            new File("./src/main/java/net/ranides/assira"),
            "*"
        );
        
        assertTrue("should find many '.java' files", files.size() > 5 && files.size() < 30);
        assertTrue("should find many packages", packages.size() > 10 && packages.size() < 30);

    }
    
    
    
}
