Package qat.parser.junitparser

Source Code of qat.parser.junitparser.JUnitTestFinder

package qat.parser.junitparser;

// JDK imports
import java.io.File;
import java.lang.reflect.Modifier;
import java.util.Properties;

import junit.framework.TestCase;
import junit.framework.TestSuite;
import qat.parser.ParserInterface;
import qat.parser.TestFinderInterface;

public class JUnitTestFinder extends Object implements TestFinderInterface {
  /**
   * This is the file extension this TestFinder uses to determine whether a file will be
   * treated as a test or not.
   */
  private static final String TEST_FILE_SUFFIX = ".class"
  private String rootDirectory;
  private FileClassLoader classLoader;
  private Properties properties;

  public JUnitTestFinder() {
  }

  /**
   * This method sets any default properties which will be required
   * for parsing this file.
   */
  public void setProperties(java.util.Properties p) {
    this.properties = p;
  }

  /**
   * This method returns all the properties obtained by parsing this test file.
   */
  public java.util.Properties getProperties() {
    return properties;
  }

  public String getProperty(String name) {
    return properties.getProperty(name);
  }

  public String getProperty(String name, String defaultValue) {
    return properties.getProperty(name,defaultValue);
  }

  public void setProjectRoot(String rootDirectory) {
    this.rootDirectory = rootDirectory;
    try {
      classLoader = new FileClassLoader(getClass().getClassLoader(),
          rootDirectory+File.pathSeparator+getProperty("junit.classpath"));
    }
    catch (Throwable t) {
      t.printStackTrace();
    }
  }

  /**
   * Return true if this file is a JUnit file.
   */
  @SuppressWarnings("unchecked")
  public boolean isTestFile(File file) {
    if ((file.getName().toLowerCase().endsWith(TEST_FILE_SUFFIX))&&
        (!isExcludePath(file))) {
      try {
        String className = convertPathToClassName(file.getCanonicalPath(),rootDirectory);
        //System.out.println("Trying too load "+className+" from root "+rootDirectory);
        Class possibleTest = classLoader.loadClass(className);
        //System.out.println("possibleTest="+possibleTest);
        if (TestCase.class.isAssignableFrom(possibleTest)) {
          if (!Modifier.isAbstract(possibleTest.getModifiers())) {
            TestSuite suite = new TestSuite(possibleTest);
            return suite.countTestCases()>0;
          }
        }
        else {
          return false;
        }
      }
      catch (Throwable t) {
        // not a test file - ignore it
        //t.printStackTrace();
      }
    }
    return false;
  }

  public static String convertPathToClassName(String pathToFile, String rootDirectory) {
    try {
      String className;
      // remove the suffix
      className = pathToFile.substring(0,pathToFile.lastIndexOf(TEST_FILE_SUFFIX));
      // remove the root path info
      className = className.substring(className.indexOf(rootDirectory)+rootDirectory.length()+1,
          className.length());
      // convert to package name
      className = className.replace('\\','.');
      className = className.replace('/','.');
      return className;
    }
    catch (Throwable t) {
      //t.printStackTrace();
      return t.toString();
    }
  }

  private boolean isExcludePath(File file) {
    String path = file.getPath();
    return ((path.indexOf("SCCS")>=0)|
        (path.indexOf("deleted_files")>=0));
  }

  /**
   * Return a parser for the specified file type, or null
   * if it's not one we recognise.
   */
  public ParserInterface getParser(File file) {
    if (isTestFile(file)) {
      JUnitParser parser = new qat.parser.junitparser.JUnitParser();
      parser.setProjectRoot(rootDirectory);
      return parser;
    }
    return null;
  }
}
TOP

Related Classes of qat.parser.junitparser.JUnitTestFinder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.