package jetbrains.buildServer.autotools.agent; /**
 * Created on 08.08.2017.
 * Author     : Nadezhda Demina
 */

import com.intellij.openapi.util.Pair;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.testng.Assert;
import org.testng.annotations.Test;
import static jetbrains.buildServer.autotools.common.AutotoolsBuildConstants.*;


/**
 * Test Class for jetbrains.buildServer.autotools.agent package
 */
public class AutotoolsAgentTest {

  @Test
  public void isThisExtensionFileTest(){
    Assert.assertTrue(AutotoolsTestsReporter.isThisExtensionFile(new File("fortest.log"), ".log"));
    Assert.assertFalse(AutotoolsTestsReporter.isThisExtensionFile(new File("fortest.log"), "log"));
    Assert.assertTrue(AutotoolsTestsReporter.isThisExtensionFile(new File("for.test.log"), ".log"));
  }

  @Test
  public void findVersionTest(){
    String text = "Expect version is       5.45\n" +
                  "Tcl version is          8.5\n" +
                  "Framework version is    1.4.4";
    Assert.assertEquals((new RuntestToolProvider("runtest", "--version")).findVersion(text), "1.4.4");

    text = "Expect version is       5.45\n" +
           "Tcl version is          8.6\n" +
           "Framework version is    1.5.1";
    Assert.assertEquals((new RuntestToolProvider("runtest", "--version")).findVersion(text), "1.5.1");

    text = "Expect version is       5.45\n" +
           "Tcl version is          8.6\n" +
           "Framework version is    1.5.3";
    Assert.assertEquals((new RuntestToolProvider("runtest", "--version")).findVersion(text), "1.5.3");

    text = "DejaGnu version 1.6\n" +
           "Expect version  5.45\n" +
           "Tcl version     8.6";
    Assert.assertEquals((new RuntestToolProvider("runtest", "--version")).findVersion(text), "1.6");

  }

  @Test
  public void getDejagnuParametersTest(){
    List<Pair<String, String>> answer = new ArrayList<Pair<String, String>>();
    answer.add(new Pair<String, String>(HAS_RUNTEST_VAR, "1"));
    answer.add(new Pair<String, String>(MY_RUNTESTFLAGS, "RUNTESTFLAGS=--all"));
    Assert.assertEquals(RuntestToolProvider.getDejagnuParameters("1.4.4"), answer,"for version 1.4.4");
    answer.clear();

    answer.add(new Pair<String, String>(HAS_RUNTEST_VAR, "1"));
    answer.add(new Pair<String, String>(MY_RUNTESTFLAGS, "RUNTESTFLAGS=--all --xml"));
    Assert.assertEquals(RuntestToolProvider.getDejagnuParameters("1.5.1"), answer, "for version 1.5.1");
    answer.clear();

    answer.add(new Pair<String, String>(HAS_RUNTEST_VAR, "1"));
    //answer.add(new Pair<String, String>(MY_RUNTESTFLAGS, "'--all --xml'"));
    answer.add(new Pair<String, String>(MY_RUNTESTFLAGS, "RUNTESTFLAGS=--all --xml=\"testresults.xml\""));
    Assert.assertEquals(RuntestToolProvider.getDejagnuParameters("1.5.3"), answer, "for version 1.5.3");
    Assert.assertEquals(RuntestToolProvider.getDejagnuParameters("1.6"), answer, "for version 1.6");
  }
  @Test
  public void getRelativePathTest(){
   Assert.assertEquals(new AutotoolsTestsReporter("C:/Users/naduxa/someprojects/teamcity-autotools-plugin/autotools-agent/src/test").getRelativePath("C:/Users/naduxa/someprojects/teamcity-autotools-plugin/autotools-agent/src/test/resources/testng.xml"),
                        "/resources/testng.xml");
  }

  @Test
  public void stringArrayToStringTest(){
    String text = "DejaGnu version 1.6\n" +
           "Expect version  5.45\n" +
           "Tcl version     8.6";
    Assert.assertEquals(AutotoolsToolProvider.stringArrayToString(text.split("\n")), text);
  }
}

@@ -167,16 +167,44 @@
 o.File;%0A
+import java.io.IOException;%0A
 import j
@@ -240,20 +240,119 @@
 va.util.
-List
+HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport javax.persistence.criteria.CriteriaBuilder
 ;%0Aimport
@@ -3695,11 +3695,992 @@
 xt);%0A  %7D
+%0A%0A  @Test%0A  public void DejagnuXmlPasringTest() throws IOException %7B%0A    File dejgnuOutput =  new File(getClass().getClassLoader().getResource(%22dejagnu-xml-output%22).getFile());%0A    if (!dejgnuOutput.isDirectory())%7B%0A      Assert.fail();%0A    %7D%0A    Map%3CString, Integer%3E testCount = new HashMap%3CString, Integer%3E();%0A    testCount.put(%22binutils.xml%22, 173);%0A    testCount.put(%22correctResults.xml%22, 10);%0A    testCount.put(%22gas.xml%22, 595);%0A    testCount.put(%22ld.xml%22, 1698);%0A    for (File xmlFile : dejgnuOutput.listFiles())%7B%0A      if (testCount.containsKey(xmlFile.getName()))%7B%0A        Assert.assertEquals(new DejagnuTestsXMLParser(null, true, true).handleXmlResults(xmlFile),%0A                            (int)testCount.get(xmlFile.getName()), %22test for file %22 + xmlFile.getName());%0A      %7D%0A      else%7B%0A        Assert.assertTrue(new DejagnuTestsXMLParser(null, true, true).handleXmlResults(xmlFile) %3E 1000,%0A                          %22test for file %22 + xmlFile.getName());%0A      %7D%0A    %7D%0A  %7D
 %0A%7D%0A

add test DejagnuXmlPasringTest