package com.netflix.nicobar.groovy2.compile;

import java.lang.reflect.Field;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.testng.Assert.assertTrue;

import org.codehaus.groovy.control.customizers.CompilationCustomizer;
import org.codehaus.groovy.control.customizers.ImportCustomizer;
import org.testng.annotations.Test;

import com.netflix.nicobar.core.archive.PathScriptArchive;
import com.netflix.nicobar.groovy2.internal.compile.Groovy2Compiler;
import com.netflix.nicobar.groovy2.testutil.GroovyTestResourceUtil;
import com.netflix.nicobar.groovy2.testutil.GroovyTestResourceUtil.TestScript;


public class Groovy2CompilerTest {

    @Test
    public void testCustomiizerParamsProcessing() throws Exception {
        Groovy2Compiler compiler;
        List<CompilationCustomizer> customizers;
        Map<String, Object> compilerParams;

        Field f = Groovy2Compiler.class.getDeclaredField("customizerClassNames");
        f.setAccessible(true);
        
        // empty parameters map
        compiler = new Groovy2Compiler(new HashMap<String, Object>());
        customizers = (List)f.get(compiler);
        assertTrue(customizers.size() == 0, "no valid objects expected");
        
        // null value customizers parameter
        compilerParams = new HashMap<String, Object>();
        compilerParams.put(Groovy2Compiler.GROOVY2_COMPILER_PARAMS_CUSTOMIZERS, null);
        
        compiler = new Groovy2Compiler(compilerParams);
        customizers = (List)f.get(compiler);
        assertTrue(customizers.size() == 0, "no valid objects expected");
        
        // list with valid customizer
        compilerParams = new HashMap<String, Object>();
        compilerParams.put(Groovy2Compiler.GROOVY2_COMPILER_PARAMS_CUSTOMIZERS, Arrays.asList(new String[] {"org.codehaus.groovy.control.customizers.ImportCustomizer"}));
        
        compiler = new Groovy2Compiler(compilerParams);
        customizers = (List)f.get(compiler);
        assertTrue(customizers.size() == 1, "one valid object expected");

        // list with invalid objects
        compilerParams = new HashMap<String, Object>();
        compilerParams.put(Groovy2Compiler.GROOVY2_COMPILER_PARAMS_CUSTOMIZERS, Arrays.asList(new Object[] {"org.codehaus.groovy.control.customizers.ImportCustomizer", "org.codehaus.groovy.control.customizers.ImportCustomizer", new HashMap<String, Object>(), null}));
        
        compiler = new Groovy2Compiler(compilerParams);
        customizers = (List)f.get(compiler);
        assertTrue(customizers.size() == 2, "two valid objects expected");
    }
    
    @Test
    public void testCompile() throws Exception {
        Groovy2Compiler compiler;
        List<CompilationCustomizer> customizers;
        Map<String, Object> compilerParams;

        Path scriptRootPath = GroovyTestResourceUtil.findRootPathForScript(TestScript.HELLO_WORLD);
        PathScriptArchive scriptArchive = new PathScriptArchive.Builder(scriptRootPath)
            .setRecurseRoot(false)
            .addFile(TestScript.HELLO_WORLD.getScriptPath())
            .build();

        compilerParams = new HashMap<String, Object>();
        compilerParams.put(Groovy2Compiler.GROOVY2_COMPILER_PARAMS_CUSTOMIZERS, Arrays.asList(new Object[] {"testmodule.customizers.TestCompilationCustomizer"}));
        
        compiler = new Groovy2Compiler(compilerParams);
        compiler.compile(scriptArchive, null, scriptRootPath);
    }
}

@@ -35,24 +35,69 @@
 2.compile;%0A%0A
+import static org.testng.Assert.assertTrue;%0A%0A
 import java.
@@ -116,16 +116,16 @@
 .Field;%0A
-
 import j
@@ -244,53 +244,8 @@
 p;%0A%0A
-import static org.testng.Assert.assertTrue;%0A%0A
 impo
@@ -314,73 +314,8 @@
 er;%0A
-import org.codehaus.groovy.control.customizers.ImportCustomizer;%0A
 impo
@@ -659,17 +659,51 @@
 rTest %7B%0A
+    @SuppressWarnings(%22unchecked%22)
 %0A
-
     @Tes
@@ -1014,25 +1014,17 @@
 (true);%0A
-        
 %0A
+
         
@@ -1137,32 +1137,55 @@
 tomizers = (List
+%3CCompilationCustomizer%3E
 )f.get(compiler)
@@ -1256,33 +1256,25 @@
 expected%22);%0A
-        
 %0A
+
         // n
@@ -1448,25 +1448,17 @@
  null);%0A
-        
 %0A
+
         
@@ -1624,25 +1624,17 @@
 cted%22);%0A
-        
 %0A
+
         
@@ -1886,33 +1886,25 @@
 omizer%22%7D));%0A
-        
 %0A
+
         comp
@@ -2424,33 +2424,25 @@
 ), null%7D));%0A
-        
 %0A
+
         comp
@@ -2611,21 +2611,17 @@
 ;%0A    %7D%0A
-    
 %0A
+
     @Tes
@@ -3325,24 +3325,16 @@
 er%22%7D));%0A
-        
 %0A       

Fix compiler warning in Groovy2CompilerPlugin