package cn.edu.seu.ise.dca.model.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.PropertyConfigurator;
import org.junit.BeforeClass;
import org.junit.Test;
import org.tmatesoft.svn.core.io.SVNRepository;

import cn.edu.seu.ise.dca.analysis.RepositoryFactory;
import cn.edu.seu.ise.dca.analysis.SVNCollector;
import cn.edu.seu.ise.dca.global.Global;
import cn.edu.seu.ise.dca.model.ChangedFile;
import cn.edu.seu.ise.dca.model.Revision;



public class TestRevision {
	
	private static SVNRepository repository;
	
	//Before testing, you need to select the project you want to test
	@BeforeClass
	public static void init() {
		//Set the project you want to test
		String project = "tikiwiki";
		
		//Set the log properties
		PropertyConfigurator.configure( "log4j.properties" );
		
		//Get the properties defined for project settings
		String settings = "cn/edu/seu/ise/dca/properties/projects.properties";
		Properties properties = new Properties();
		InputStream in = ClassLoader.getSystemResourceAsStream(settings);  
		try {
			properties.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}  
		//Get the properties of the selected project
		String projectSettings = properties.getProperty(project);
		in = ClassLoader.getSystemResourceAsStream(projectSettings);
		try {
			properties.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		//Get the information of selected project
		String projectName = properties.getProperty("name");
		String url = properties.getProperty("url");
		String username = properties.getProperty("username");
		String password = properties.getProperty("password");
		
		
		

		//Set the output path for checkout and file path
		Global.setOutputDir(projectName);
		
		/*
		 * Get the repository of the selected projects
		 */
		repository = RepositoryFactory.getRepository(url, username, password);
	}
	
	//@Test
	public void testRevision() {
		String schemaLoc = "/trunk/e107_0.7/e107_admin/sql/core_sql.php";
		//String coreLoc = "/trunk/phase3/includes/";
		long startRevision = 11346;
		long endRevision = 11346;
		SVNCollector collector = new SVNCollector(repository);
		List<Revision> revisions = collector.getRevisions(new String[] {schemaLoc}, startRevision, endRevision, null, null, null, true);
		for(Revision revision : revisions) {
			System.out.println(revision);
			System.out.println("=====================================================");
			System.out.println(revision.reportChangedFiles());
			//System.out.println(revision.reportChangedFiles(new String[]{coreLoc}));
			System.out.println(revision.reportChangedLines(true));
			//System.out.println(revision.reportChangedLines(new String[]{coreLoc}, true));
			System.out.println(revision.reportChangedClasses());
			//System.out.println(revision.reportChangedClasses(new String[]{coreLoc}));
			System.out.println(revision.reportChangedGlobalFunctions());
			//System.out.println(revision.reportChangedGlobalFunctions(new String[]{coreLoc}));
			System.out.println(revision.reportChangedMemberFunctions());
			//System.out.println(revision.reportChangedMemberFunctions(new String[]{coreLoc}));
		}
	}
	
	
	@Test
	public void testChangedFiles() {
		String schemaLoc = "/trunk/db/tiki.sql";
		//String coreLoc = "/trunk/phase3/includes/";
		long startRevision = 13743;
		long endRevision = 13743;
		SVNCollector collector = new SVNCollector(repository);
		//If use the suffix check
		List<Revision> revisions = collector.getRevisions(new String[] {schemaLoc}, startRevision, endRevision, null, null, null, true);
		for(Revision revision : revisions) {
			List<ChangedFile> changedFiles = revision.getChangedFiles();
			System.out.println("The number of changed files is " + changedFiles.size());
		}
		//If do not use the suffix check
		revisions = collector.getRevisions(new String[] {schemaLoc}, startRevision, endRevision, null, null, null, false);
		for(Revision revision : revisions) {
			List<ChangedFile> changedFiles = revision.getChangedFiles();
			System.out.println("The number of changed files is " + changedFiles.size());
		}
	}
}
