package innovimax.mixthem;

import innovimax.mixthem.arguments.Rule;
import innovimax.mixthem.exceptions.MixException;

import java.io.*;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.theories.*;
import org.junit.runner.RunWith;

/*
	Created by innovimax
	Generic tests for this application
*/
public class GenericTest {
	
   private static Logger LOGGER = Logger.getLogger(GenericTest.class.getName());

   private static void setLogging() {
        System.setProperty("java.util.logging.SimpleFormatter.format", "[%4$s] MixThemUT: %5$s [%1$tc]%n");
        String prop = System.getProperty("mixthem.logging");
        if (prop == null || prop.equals("true")) {
            LOGGER.setLevel(Level.FINE);
        } else {
            LOGGER.setLevel(Level.OFF);
        }
        LOGGER.addHandler(new ConsoleHandler());
	System.out.println("UT LOGGER NAME = "+LOGGER.getName());
	System.out.println("UT LOGGER PARENT = "+LOGGER.getParent().getName());
   }
	
   @Test
   public final void parameter() throws MixException, FileNotFoundException, IOException {
	   setLogging();
	   int testId = 1;
	   boolean result = true;
	   RuleRuns ruleRuns = new RuleRuns();
	   while (true) {
		   LOGGER.info("TEST N° " + testId);
		   String prefix = "test" + String.format("%03d", testId) +"_";
		   URL url1 = getClass().getResource(prefix + "file1.txt");
		   URL url2 = getClass().getResource(prefix + "file2.txt");
		   LOGGER.fine("--> URL 1 (" + prefix + "file1.txt"+") : " + url1);
		   LOGGER.fine("--> URL 2 (" + prefix + "file2.txt"+") : " + url2);
		   if( url1 == null || url2 == null) break;
		   for(Rule rule : Rule.values()) {
			   String resource = prefix+"output-"+ rule.getExtension()+".txt";
			   URL url = getClass().getResource(resource);
			   LOGGER.info("  RULE " + rule + " (" + (rule.isImplemented() ? "" : "NOT ") + "IMPLEMENTED)");
			   LOGGER.fine("  --> Resource (" + resource + ") : " + url);
			   if (rule.isImplemented() && url != null) {			   	
				List<RuleRun> runs = ruleRuns.getRuns(rule);
				for (RuleRun run : runs) {
					if (run.accept(testId)) {						
						boolean res = check(new File(url1.getFile()), new File(url2.getFile()), new File(url.getFile()), rule, run.getParams());
						LOGGER.info("    RUN " + (res ? "PASS" : "FAIL") + " WITH PARAMS " + run.getParams().toString());
						result &= res;   
					}
				}
			   }
		   }   
		   testId++;
	   }
	   Assert.assertTrue(result);
   }	   
   private final static boolean check(File file1, File file2, File expected, Rule rule, List<String> params)  throws MixException, FileNotFoundException, IOException  {
	   ByteArrayOutputStream baos_rule = new ByteArrayOutputStream();
	   MixThem mixThem = new MixThem(file1, file2, baos_rule);
           mixThem.process(rule, params);	   	   
	   //mixThem = new MixThem(file1, file2, System.out);
           //mixThem.process(rule, params);
	   return checkFileEquals(expected, baos_rule.toByteArray());
   }

	private static boolean checkFileEquals(File fileExpected, byte[] result) throws FileNotFoundException, IOException {
	   FileInputStream fisExpected = new FileInputStream(fileExpected);
	   int c;
	   int offset = 0;
	   while ((c = fisExpected.read()) != -1) {
		   if (offset >= result.length) return false;
		   int d = result[offset++];
		   if (c != d) return false;
	   }
	   if (offset > result.length) return false;
	   return true;
	}

}

@@ -234,87 +234,13 @@
 ing.
-ConsoleHandler;%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger
+Level
 ;%0A%0Ai
@@ -459,644 +459,8 @@
 %7B%0A%09%0A
-   private static Logger LOGGER = Logger.getLogger(GenericTest.class.getName());%0A%0A   private static void setLogging() %7B%0A        System.setProperty(%22java.util.logging.SimpleFormatter.format%22, %22%5B%254$s%5D MixThemUT: %255$s %5B%251$tc%5D%25n%22);%0A        String prop = System.getProperty(%22mixthem.logging%22);%0A        if (prop == null %7C%7C prop.equals(%22true%22)) %7B%0A            LOGGER.setLevel(Level.FINE);%0A        %7D else %7B%0A            LOGGER.setLevel(Level.OFF);%0A        %7D%0A        LOGGER.addHandler(new ConsoleHandler());%0A%09System.out.println(%22UT LOGGER NAME = %22+LOGGER.getName());%0A%09System.out.println(%22UT LOGGER PARENT = %22+LOGGER.getParent().getName());%0A   %7D%0A%09%0A
    @
@@ -559,16 +559,24 @@
 n %7B%0A%09   
+Mixthem.
 setLoggi
@@ -578,16 +578,26 @@
 Logging(
+LEVEL.FINE
 );%0A%09   i
@@ -698,24 +698,32 @@
 rue) %7B%0A%09%09   
+Mixthem.
 LOGGER.info(
@@ -931,32 +931,40 @@
 le2.txt%22);%0A%09%09   
+Mixthem.
 LOGGER.fine(%22--%3E
@@ -1013,24 +1013,32 @@
 url1);%0A%09%09   
+Mixthem.
 LOGGER.fine(
@@ -1296,24 +1296,32 @@
 rce);%0A%09%09%09   
+Mixthem.
 LOGGER.info(
@@ -1408,16 +1408,24 @@
 ;%0A%09%09%09   
+Mixthem.
 LOGGER.f
@@ -1775,22 +1775,30 @@
 ams());%0A
-
 %09%09%09%09%09%09
+Mixthem.
 LOGGER.i

use the same logger for tests