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

import java.io.File;
import java.io.IOException;
import java.util.function.Function;
import net.ranides.assira.junit.TestContractRunner;
import net.ranides.assira.reflection.util.ClassUtils;
import net.ranides.assira.reflection.util.PackageScanner;
import net.ranides.assira.trace.ExceptionUtils;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public final class ContractTesters {

    private ContractTesters() { }
    
    public static final File JAR_TEST = new File("../assira.test/target/assira.test-2.1.0.jar");
    
    public static final File JAR_RULES = new File("../assira.rules/target/assira.rules-2.1.0.jar");
    
    private static final class Li { // NOPMD - lazy init idiom
        
        private static final TestContractRunner RUNNER;
        
        static {
            try {
                RUNNER = new TestContractRunner(System.err);
                
                PackageScanner.getNameStream("net.ranides.assira")
                    .map(newtester())
                    .filter(o -> o!=null)
                    .forEach(RUNNER::append);

            } catch(IOException cause) {
                throw ExceptionUtils.rethrow(cause);
            }
        }
    }
        
    
    public static TestContractRunner runner() {
        return new TestContractRunner(Li.RUNNER);
    }
    
    private static Function<String, Object> newtester() {
        ClassLoader loader = ClassUtils.loader();
        return name -> {
            try {
                return name.endsWith("Tester") ? Class.forName(name, false, loader).newInstance() : null;
            } catch(ReflectiveOperationException cause) {
                return null;
            }
        };
    }
    
}
