package com.izforge.izpack.compiler;

import com.izforge.izpack.adaptator.IXMLElement;
import com.izforge.izpack.adaptator.impl.XMLParser;
import com.izforge.izpack.compiler.data.CompilerData;
import com.izforge.izpack.compiler.data.PropertyManager;
import com.izforge.izpack.compiler.helper.CompilerHelper;
import com.izforge.izpack.compiler.helper.impl.XmlCompilerHelper;
import com.izforge.izpack.data.DynamicVariable;
import com.izforge.izpack.util.substitutor.VariableSubstitutor;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Anthonin Bonnefoy
 */
@RunWith(MockitoJUnitRunner.class)
public class
        CompilerConfigTest {
    private XMLParser xmlParser;
    private CompilerConfig compilerConfig;
    @Mock
    private CompilerData data;
    @Mock
    private VariableSubstitutor variableSubstitutor;
    @Mock
    private Compiler compiler;
    @Mock
    private CompilerHelper compilerHelper;
    @Mock
    private PropertyManager propertyManager;
    @Mock
    private XmlCompilerHelper xmlCompilerHerlper;

    @Before
    public void setUp() {
        compilerConfig = new CompilerConfig(data, variableSubstitutor, compiler, compilerHelper, xmlCompilerHerlper, propertyManager);
        xmlParser = new XMLParser();
    }

    @Test
    public void testAddTwoVariables() throws Exception {
        Map<String, List<DynamicVariable>> mapStringListDyn = Mockito.mock((Map.class));

        Mockito.when(mapStringListDyn.containsKey("myPath")).thenReturn(false);
        Mockito.when(compiler.getDynamicVariables()).thenReturn(mapStringListDyn);
        Properties variable = new Properties();
        Mockito.when(compiler.getVariables()).thenReturn(variable);

        IXMLElement element = xmlParser.parse("<root><dynamicvariables><variable name=\"myPath\" value=\"$INSTALLPATH / test\"/></dynamicvariables></root>");
        compilerConfig.addDynamicVariables(element);
        element = xmlParser.parse("<root><variables><variable name=\"INSTALLPATH\" value=\"thePath\"/></variables></root>");
        compilerConfig.addVariables(element);

        DynamicVariable dyn = new DynamicVariable();
        dyn.setName("myPath");
        dyn.setValue("thePath/test");
        ArrayList<DynamicVariable> list = new ArrayList<DynamicVariable>();
        list.add(dyn);
        Mockito.verify(mapStringListDyn).put("myPath", list);
    }

    @Test
    public void testAddDynamicVariable() throws CompilerException {
        Map<String, List<DynamicVariable>> mapStringListDyn = Mockito.mock((Map.class));

        Mockito.when(mapStringListDyn.containsKey("myPath")).thenReturn(false);
        Mockito.when(compiler.getDynamicVariables()).thenReturn(mapStringListDyn);

        IXMLElement element = xmlParser.parse("<root><dynamicvariables><variable name=\"myPath\" value=\"$INSTALLPATH / test\"/></dynamicvariables></root>");
        compilerConfig.addDynamicVariables(element);

        DynamicVariable dyn = new DynamicVariable();
        dyn.setName("myPath");
        dyn.setValue("$INSTALLPATH/test");
        ArrayList<DynamicVariable> list = new ArrayList<DynamicVariable>();
        list.add(dyn);
        Mockito.verify(mapStringListDyn).put("myPath", list);
    }

}

@@ -880,24 +880,16 @@
 ic class
-%0A       
  Compile
@@ -1290,16 +1290,91 @@
 Herlper;
+%0A    @Mock%0A    private Map%3CString, List%3CDynamicVariable%3E%3E mapStringListDyn;
 %0A%0A    @B
@@ -1655,98 +1655,8 @@
 n %7B%0A
-        Map%3CString, List%3CDynamicVariable%3E%3E mapStringListDyn = Mockito.mock((Map.class));%0A%0A
     
@@ -2326,243 +2326,23 @@
     
-DynamicVariable dyn = new DynamicVariable();%0A        dyn.setName(%22myPath%22);%0A        dyn.setValue(%22thePath/test%22);%0A        ArrayList%3CDynamicVariable%3E list = new ArrayList%3CDynamicVariable%3E();%0A        list.add(dyn);%0A        Mockito.verify
+verifyCallToMap
 (map
@@ -2346,38 +2346,34 @@
 mapStringListDyn
-).put(
+, 
 %22myPath%22, list);
@@ -2358,36 +2358,46 @@
 tDyn, %22myPath%22, 
-li
+%22thePath/te
 st
+%22
 );%0A    %7D%0A%0A    @T
@@ -2472,98 +2472,8 @@
 n %7B%0A
-        Map%3CString, List%3CDynamicVariable%3E%3E mapStringListDyn = Mockito.mock((Map.class));%0A%0A
     
@@ -2836,32 +2836,228 @@
 bles(element);%0A%0A
+        verifyCallToMap(mapStringListDyn, %22myPath%22, %22$INSTALLPATH/test%22);%0A    %7D%0A%0A    private void verifyCallToMap(Map%3CString, List%3CDynamicVariable%3E%3E mapStringListDyn, String name, String value) %7B%0A
         DynamicV
@@ -3113,24 +3113,20 @@
 setName(
-%22myPath%22
+name
 );%0A     
@@ -3141,35 +3141,21 @@
 etValue(
-%22$INSTALLPATH/test%22
+value
 );%0A     
@@ -3293,24 +3293,20 @@
 yn).put(
-%22myPath%22
+name
 , list);

Test refactoring