/plugins/JUnitPlugin/trunk/JUnitPlugin/junit/PluginTestCollector.java

# · Java · 123 lines · 62 code · 13 blank · 48 comment · 8 complexity · a005b039c1ea5824fbdf8de6a773e9d8 MD5 · raw file

  1. /*
  2. * PluginTestCollector.java
  3. * Copyright (c) 2002 Calvin Yu
  4. * Copyright (c) 2011 Eric Le Lay
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. package junit;
  21. import java.io.File;
  22. import java.io.FileFilter;
  23. import org.junit.runner.Description;
  24. import org.gjt.sp.jedit.jEdit;
  25. import org.gjt.sp.util.Log;
  26. /**
  27. * Test collector that uses the junit configured class path.
  28. *
  29. * It does all the work by itself, but does it badly:
  30. * - any .class file in a directory is returned
  31. * - lookup of tests in jar files in not supported
  32. *
  33. * the classes are actually loaded, using a fresh JEditTestCaseClassLoader
  34. * configured with the classPath.
  35. * They could be tested with annotations or subclass of TestCase
  36. * for compatibility with junit 3
  37. */
  38. public class PluginTestCollector{
  39. private String classPath;
  40. /**
  41. * Create a new <code>PluginTestCollector</code>.
  42. */
  43. public PluginTestCollector(String aClassPath) {
  44. classPath = aClassPath;
  45. }
  46. /**
  47. * Create a new <code>PluginTestCollector</code>.
  48. */
  49. public PluginTestCollector() {
  50. classPath = jEdit.getProperty("java.class.path");
  51. if(classPath == null)classPath = "";
  52. }
  53. /**
  54. * @return a Description containing every class found in the classpath
  55. */
  56. public Description collectTests() {
  57. JEditTestCaseClassLoader loader = new JEditTestCaseClassLoader(classPath);
  58. String[] paths = classPath.split(File.pathSeparator);
  59. Description d = Description.createSuiteDescription("All Tests");
  60. for(int i=0;i<paths.length;i++){
  61. collectTests(paths[i], loader, d);
  62. }
  63. return d;
  64. }
  65. private void collectTests(String path, JEditTestCaseClassLoader loader, Description d){
  66. File f = new File(path);
  67. if(f.isDirectory()){
  68. collectTestsFromDir(f,loader,"",d);
  69. }else{
  70. collectTestsFromJar(f,loader,d);
  71. }
  72. }
  73. /**
  74. * accepts directories and classes (not internal classes)
  75. */
  76. private FileFilter filter = new FileFilter(){
  77. public boolean accept(File f){
  78. return f.isDirectory()
  79. || (f.getName().endsWith(".class")
  80. && !f.getName().contains("$"));
  81. }
  82. };
  83. /**
  84. * @param prefix current prefix of the would-be package (includes final dot or is empty)
  85. */
  86. private void collectTestsFromDir(File f, JEditTestCaseClassLoader loader, String prefix, Description d){
  87. File[] contents = f.listFiles(filter);
  88. for(File c: contents){
  89. if(c.isDirectory()){
  90. String nPrefix = prefix+c.getName()+".";
  91. collectTestsFromDir(c,loader,nPrefix,d);
  92. }else{
  93. // remove .class
  94. String className = prefix+c.getName().substring(0,c.getName().length()-6);
  95. try{
  96. Class cl = loader.loadClass(className,false);
  97. d.addChild(Description.createSuiteDescription(cl));
  98. }catch(ClassNotFoundException e){
  99. Log.log(Log.WARNING,PluginTestCollector.class,"test class not found: "+className);
  100. }catch(ClassFormatError e){
  101. Log.log(Log.WARNING,PluginTestCollector.class,"this is not a test class: "+className);
  102. }
  103. }
  104. }
  105. }
  106. private void collectTestsFromJar(File f, JEditTestCaseClassLoader loader, Description d){
  107. // FIXME: implement
  108. Log.log(Log.NOTICE,PluginTestCollector.class,"collecting tests from jar is not supported ("+f+")");
  109. }
  110. }