package operias.coverage;

import static org.junit.Assert.*;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import operias.OperiasStatus;
import operias.coverage.CoberturaClass;
import operias.coverage.CoberturaLine;
import operias.coverage.CoberturaPackage;
import operias.coverage.CoverageReport;
import operias.test.general.ExitException;
import operias.test.general.NoExitSecurityManager;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class CoverageReportTest {

	/**
	 * Set up the security manager
	 */
	@Before
	public void setUp() {
		System.setSecurityManager(new NoExitSecurityManager());	
	}
	
	/**
	 * Tear down the security manager
	 */
	@After
	public void tearDown() {
		System.setSecurityManager(null);	
	}
	
	/**
	 * Test invalid coverage XML files
	 */
	@Test
	public void testInvalidCoverageXML() {
		boolean exceptionThrown = false;
		
		try {
			new CoverageReport((File)null, "").constructReport();;
		} catch(ExitException e) {
			exceptionThrown = true;
			assertEquals("Wrong exit code", OperiasStatus.ERROR_COBERTURA_INVALID_XML.ordinal(), e.status);
		}
		assertTrue("No exception was thrown", exceptionThrown);
		
		exceptionThrown = false;
		
		try {
			new CoverageReport(new File(""), "").constructReport();;
		} catch(ExitException e) {
			exceptionThrown = true;
			assertEquals("Wrong exit code", OperiasStatus.ERROR_COBERTURA_INVALID_XML.ordinal(), e.status);
		}
		assertTrue("No exception was thrown", exceptionThrown);
		
		exceptionThrown = false;
		
		try {
			new CoverageReport(new File("src/test/resources/nonExistingFile.xml"), "").constructReport();;
		} catch(ExitException e) {
			exceptionThrown = true;
			assertEquals("Wrong exit code", OperiasStatus.ERROR_COBERTURA_INVALID_XML.ordinal(), e.status);
		}
		assertTrue("No exception was thrown", exceptionThrown);
	}
	
	/**
	 * Test the loading of a coverage xml
	 */
	@Test
	public void testLoadXML() {
		double delta = 0.0001;
		File file = new File("src/test/resources/coverage.xml");

		CoverageReport report = new CoverageReport(file, "src/test/resources/sureFireReports/").constructReport();
		
		assertEquals("Line rate not equal", 0.7578125, report.getLineRate(),  delta);
		assertEquals(0.75,report.getConditionRate(), delta);
		
		
		ArrayList<CoberturaPackage> packages = (ArrayList<CoberturaPackage>) report.getPackages();
		assertEquals(2, packages.size());
		
		CoberturaPackage cPackage = packages.get(0);
		
		assertEquals("operias", cPackage.getName());
		assertEquals(0.6376811594202898, cPackage.getLineRate(), delta);
		assertEquals(0.6, cPackage.getConditionRate(), delta);
		
		
		// Check classes
		ArrayList<CoberturaClass> classes = (ArrayList<CoberturaClass>) cPackage.getClasses();
		
		assertEquals(4, classes.size());
		
		CoberturaClass cClass = classes.get(0);
		assertEquals("operias.Configuration", cClass.getName());
		assertEquals("operias/Configuration.java", cClass.getFileName());
		assertEquals(0.9230769230769231, cClass.getLineRate(), delta);
		assertEquals(1.0, cClass.getConditionRate(), delta);
		
		// Check lines in class
		LinkedList<CoberturaLine> classLines = (LinkedList<CoberturaLine>) cClass.getLines();
		
		assertEquals(26, classLines.size());
		
		List<TestReport> testcases = report.getTests();
		
		assertEquals(3, testcases.size());
		
		TestReport succesfullReport = testcases.get(2);
		assertEquals(TestResultType.SUCCESS, succesfullReport.getResult());
		assertEquals("example.MusicTest", succesfullReport.getClassName());
		assertEquals("testMusic", succesfullReport.getCaseName());
		

		TestReport errorReport = testcases.get(0);
		assertEquals(TestResultType.ERROR, errorReport.getResult());
		assertEquals("example.CalculationsTest", errorReport.getClassName());
		assertEquals("testSomeCalcution", errorReport.getCaseName());
		
		TestReport failReport = testcases.get(1);
		assertEquals(TestResultType.FAILURE, failReport.getResult());
		assertEquals("example.LoopsTest", failReport.getClassName());
		assertEquals("testLoop", failReport.getCaseName());
	}
}

@@ -3371,32 +3371,36 @@
 s.size());%0A%09%09%0A%09%09
+for(
 TestReport succe
@@ -3398,26 +3398,17 @@
 ort 
-succesfull
+t
 Report 
-=
+:
  tes
@@ -3417,87 +3417,70 @@
 ases
-.get(2);%0A%09%09assertEquals(TestResultType.SUCCESS, succesfullReport.getResult());%0A
+) %7B%0A%09%09%09if (tReport.getResult() == TestResultType.SUCCESS) %7B%0A%09%09
 %09%09as
@@ -3511,26 +3511,17 @@
 cTest%22, 
-succesfull
+t
 Report.g
@@ -3530,32 +3530,34 @@
 ClassName());%0A%09%09
+%09
+%09
 assertEquals(%22te
@@ -3570,18 +3570,9 @@
 c%22, 
-succesfull
+t
 Repo
@@ -3596,118 +3596,69 @@
 ;%0A%09%09
-%0A%0A%09%09TestReport errorReport = testcases.get(0);%0A%09%09assertEquals(TestResultType.ERROR, errorReport.getResult());%0A
+%09%7D else if (tReport.getResult() == TestResultType.ERROR) %7B%0A%09%09
 %09%09as
@@ -3696,21 +3696,17 @@
 sTest%22, 
-error
+t
 Report.g
@@ -3713,32 +3713,34 @@
 etClassName());%0A
+%09%09
 %09%09assertEquals(%22
@@ -3763,13 +3763,9 @@
 n%22, 
-error
+t
 Repo
@@ -3789,117 +3789,71 @@
 ;%0A%09%09
-%0A%09%09TestReport failReport = testcases.get(1);%0A%09%09assertEquals(TestResultType.FAILURE, failReport.getResult());%0A
+%09%7D else if (tReport.getResult() == TestResultType.FAILURE) %7B%0A%09%09
 %09%09as
@@ -3884,20 +3884,17 @@
 sTest%22, 
-fail
+t
 Report.g
@@ -3901,32 +3901,34 @@
 etClassName());%0A
+%09%09
 %09%09assertEquals(%22
@@ -3942,12 +3942,9 @@
 p%22, 
-fail
+t
 Repo
@@ -3962,13 +3962,23 @@
 ame());%0A
+%09%09%09%7D %0A%09%09%7D%0A
 %09%7D%0A%7D%0A

Fix in test suite