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

import java.util.List;

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

import cn.edu.seu.ise.dca.analysis.SVNCollector;
import cn.edu.seu.ise.dca.analysis.RepositoryFactory;
import cn.edu.seu.ise.dca.app.php.model.PHPFunction;
import cn.edu.seu.ise.dca.model.ChangedFile;
import cn.edu.seu.ise.dca.model.Revision;


public class TestGlobalFunctions {
	private static SVNRepository repository;
	
	@BeforeClass
	public static void init() {
		// Set default values
		String url = "http://svn.wikimedia.org/svnroot/mediawiki";
		String username = "anonymous";
		String password = "anonymous";
		/* Get the repository of the selected projects */
		repository = RepositoryFactory.getRepository(url, username, password);
	}
	
	@Test
	public void testGFunc() {
		//printGlobalFunctions("/trunk/phase3/maintenance/indexes.sql", 1914);
		printGlobalFunctuins("/trunk/phase3/maintenance/tables.sql", 17968);
		printGlobalFunctuins("/trunk/phase3/maintenance/tables.sql", 17969);
	}
	
	/**
	 * Print the global functions of specific revision
	 * @param schemaUrl
	 * @param revision
	 */
	private void printGlobalFunctuins(String schemaUrl, long revision) {
		printGlobalFunctions(schemaUrl, revision, revision);
	}
	
	/**
	 * Print the global functions from start revision to the end revision
	 * @param schemaUrl
	 * @param startRevision
	 * @param endRevision
	 */
	private void printGlobalFunctions(String schemaUrl, long startRevision, long endRevision) {
		//String schemaUrl = ;
		//String coreUrl = "/trunk/phase3/includes/";
		SVNCollector collector = new SVNCollector(repository);
		List<Revision> revisions = collector.getRevisions(new String[] {schemaUrl}, startRevision, endRevision, null, null, null, true);
		for(Revision revision : revisions) {
			for(ChangedFile changedFile : revision.getChangedFiles()) {
				System.out.println(changedFile.getUrl());
				List<PHPFunction> globalFunctions = changedFile.getPHPFileChunk().getRevised().getGlobalFunctions();
				if(globalFunctions.size() > 0) {
					System.out.println("The global functions in revised version:");
					for(PHPFunction function : globalFunctions) {
						System.out.println(function.getFullName());
					}
				} 
			}
		}
	}
}
