Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 347446 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/rwt/test/RWTAllTestSuite.java (-10 / +114 lines)
Lines 10-32 Link Here
10
*******************************************************************************/
10
*******************************************************************************/
11
package org.eclipse.rwt.test;
11
package org.eclipse.rwt.test;
12
12
13
import java.io.*;
14
import java.net.URL;
15
import java.net.URLClassLoader;
16
import java.util.jar.JarEntry;
17
import java.util.jar.JarInputStream;
18
13
import junit.framework.Test;
19
import junit.framework.Test;
14
import junit.framework.TestSuite;
20
import junit.framework.TestSuite;
15
21
16
import org.eclipse.RWTHostTestSuite;
17
import org.eclipse.RWTQ07TestSuite;
18
import org.eclipse.rap.rwt.themes.test.ThemesTestSuite;
19
20
22
21
public class RWTAllTestSuite {
23
public class RWTAllTestSuite {
24
  private static final String JAR_EXTENSION = ".jar";
25
  private static final String CLASS_EXTENSION = ".class";
22
26
23
  public static Test suite() {
27
  public static Test suite() {
24
    TestSuite suite = new TestSuite( "Test for all RWT Tests" );
28
    return new RWTAllTestSuite().createSuite();
25
    // $JUnit-BEGIN$
29
  }
26
    suite.addTest( RWTHostTestSuite.suite() );
30
  
27
    suite.addTest( RWTQ07TestSuite.suite() );
31
  private final TestSuite suite;
28
    suite.addTest( ThemesTestSuite.suite() );
32
  private final URLClassLoader classLoader;
29
    // $JUnit-END$
33
34
  RWTAllTestSuite() {
35
    suite = new TestSuite( "RWT Test Suite" );
36
    classLoader = ( URLClassLoader )getClass().getClassLoader();
37
  }
38
  
39
  Test createSuite() {
40
    try {
41
      scanClasspath();
42
    } catch( IOException ioe ) {
43
      throw new RuntimeException( ioe );
44
    }
30
    return suite;
45
    return suite;
31
  }
46
  }
47
48
  private void scanClasspath() throws IOException {
49
    URL[] urls = classLoader.getURLs();
50
    for( int i = 0; i < urls.length; i++ ) {
51
      File file = new File( urls[ i ].getFile() );
52
      if( file.exists() ) {
53
        if( file.getName().endsWith( JAR_EXTENSION ) ) {
54
          scanJar( file );
55
        } else {
56
          scanDirectory( file, "", file.getPath() );
57
        }
58
      }  
59
    }
60
  }
61
62
  private void scanJar( File file ) throws IOException {
63
    JarInputStream inputStream = new JarInputStream( new FileInputStream( file ), false );
64
    JarEntry jarEntry = inputStream.getNextJarEntry();
65
    while( jarEntry != null ) {
66
      if( isClassEntry( jarEntry ) ) {
67
        String className = toClassName( jarEntry );
68
        addToSuite( className );
69
      }
70
      jarEntry = inputStream.getNextJarEntry();
71
    }
72
    inputStream.close();
73
  }
74
75
  private void scanDirectory( File file, String initialPackagePath, String rootDirectory ) {
76
    if( file.isDirectory() ) {
77
      String packagePath = computePackagePath( file, initialPackagePath, rootDirectory );
78
      String[] files = file.list();
79
      for( int i = 0; i < files.length; i++ ) {
80
        File directory = new File( file, files[ i ] );
81
        scanDirectory( directory, packagePath, rootDirectory );
82
      }
83
    } else if( isClassFile( file ) ) {
84
      String className = toClassName( file, initialPackagePath );
85
      addToSuite( className );
86
    }
87
  }
88
89
  private static String computePackagePath( File file, String initialPackagePath, String rootDir ) {
90
    String result;
91
    if( file.getPath().equals( rootDir ) ) {
92
      result = "";
93
    } else if( initialPackagePath.length() == 0 ) {
94
      result = file.getName();
95
    } else {
96
      result = initialPackagePath + "." + file.getName();
97
    }
98
    return result;
99
  }
100
101
  private void addToSuite( String className ) {
102
    if( className.endsWith( "_Test" ) ) {
103
      try {
104
        suite.addTestSuite( classLoader.loadClass( className ) );
105
      } catch( ClassNotFoundException cnfe ) {
106
        throw new RuntimeException( cnfe );
107
      }
108
    }
109
  }
110
111
  private static String toClassName( JarEntry jarEntry ) {
112
    String result = removeExtension( jarEntry.getName(), CLASS_EXTENSION );
113
    result = result.replace( '/', '.' );
114
    return result;
115
  }
116
117
  private static String toClassName( File file, String packageName ) {
118
    String result = removeExtension( file.getName(), CLASS_EXTENSION );
119
    if( packageName.length() > 0 ) {
120
      result = packageName + "." + result;
121
    } 
122
    return result;
123
  }
124
125
  private static String removeExtension( String name, String extension ) {
126
    return name.substring( 0, name.lastIndexOf( extension ) );
127
  }
128
129
  private static boolean isClassEntry( JarEntry jarEntry ) {
130
    return ( !jarEntry.isDirectory() ) && jarEntry.getName().endsWith( CLASS_EXTENSION );
131
  }
132
133
  private static boolean isClassFile( File file ) {
134
    return file.isFile() && file.getName().endsWith( CLASS_EXTENSION );
135
  }
32
}
136
}

Return to bug 347446