FileDocCategorySizeDatePackage
TestCaseClassLoader.javaAPI DocAndroid 1.5 API5610Wed May 06 22:42:02 BST 2009junit.runner

TestCaseClassLoader

public class TestCaseClassLoader extends ClassLoader
A custom class loader which enables the reloading of classes for each test run. The class loader can be configured with a list of package paths that should be excluded from loading. The loading of these packages is delegated to the system class loader. They will be shared across test runs.

The list of excluded package paths is specified in a properties file "excluded.properties" that is located in the same place as the TestCaseClassLoader class.

Known limitation: the TestCaseClassLoader cannot load classes from jar files. {@hide} - Not needed for 1.0 SDK

Fields Summary
private Vector
fPathItems
scanned class path
private String[]
defaultExclusions
default excluded paths
static final String
EXCLUDED_FILE
name of excluded properties file
private Vector
fExcluded
excluded paths
Constructors Summary
public TestCaseClassLoader()
Constructs a TestCaseLoader. It scans the class path and the excluded package paths

	 
	             	 
	  
		this(System.getProperty("java.class.path"));
	
public TestCaseClassLoader(String classPath)
Constructs a TestCaseLoader. It scans the class path and the excluded package paths

		scanPath(classPath);
		readExcludedPackages();
	
Methods Summary
private byte[]getClassData(java.io.File f)

		try {
			FileInputStream stream= new FileInputStream(f);
			ByteArrayOutputStream out= new ByteArrayOutputStream(1000);
			byte[] b= new byte[1000];
			int n;
			while ((n= stream.read(b)) != -1) 
				out.write(b, 0, n);
			stream.close();
			out.close();
			return out.toByteArray();

		} catch (IOException e) {
		}
		return null;
	
public java.net.URLgetResource(java.lang.String name)

		return ClassLoader.getSystemResource(name);
	
public java.io.InputStreamgetResourceAsStream(java.lang.String name)

		return ClassLoader.getSystemResourceAsStream(name);
	
public booleanisExcluded(java.lang.String name)

		for (int i= 0; i < fExcluded.size(); i++) {
			if (name.startsWith((String) fExcluded.elementAt(i))) {
				return true;
			}
		}
		return false;	
	
booleanisJar(java.lang.String pathEntry)

		return pathEntry.endsWith(".jar") ||
		       pathEntry.endsWith(".apk") ||
                       pathEntry.endsWith(".zip");
	
public synchronized java.lang.ClassloadClass(java.lang.String name, boolean resolve)

			
		Class c= findLoadedClass(name);
		if (c != null)
			return c;
		//
		// Delegate the loading of excluded classes to the
		// standard class loader.
		//
		if (isExcluded(name)) {
			try {
				c= findSystemClass(name);
				return c;
			} catch (ClassNotFoundException e) {
				// keep searching
			}
		}
		if (c == null) {
			byte[] data= lookupClassData(name);
			if (data == null)
				throw new ClassNotFoundException();
			c= defineClass(name, data, 0, data.length);
		}
		if (resolve) 
			resolveClass(c);
		return c;
	
private byte[]loadFileData(java.lang.String path, java.lang.String fileName)

		File file= new File(path, fileName);
		if (file.exists()) { 
			return getClassData(file);
		}
		return null;
	
private byte[]loadJarData(java.lang.String path, java.lang.String fileName)

		ZipFile zipFile= null;
		InputStream stream= null;
		File archive= new File(path);
		if (!archive.exists())
			return null;
		try {
			zipFile= new ZipFile(archive);
		} catch(IOException io) {
			return null;
		}
		ZipEntry entry= zipFile.getEntry(fileName);
		if (entry == null)
			return null;
		int size= (int) entry.getSize();
		try {
			stream= zipFile.getInputStream(entry);
			byte[] data= new byte[size];
			int pos= 0;
			while (pos < size) {
				int n= stream.read(data, pos, data.length - pos);
				pos += n;
			}
			zipFile.close();
			return data;
		} catch (IOException e) {
		} finally {
			try {
				if (stream != null)
					stream.close();
			} catch (IOException e) {
			}
		}
		return null;
	
private byte[]lookupClassData(java.lang.String className)

		byte[] data= null;
		for (int i= 0; i < fPathItems.size(); i++) {
			String path= (String) fPathItems.elementAt(i);
			String fileName= className.replace('.", '/")+".class";
			if (isJar(path)) {
				data= loadJarData(path, fileName);
			} else {
				data= loadFileData(path, fileName);
			}
			if (data != null)
				return data;
		}
		throw new ClassNotFoundException(className);
	
private voidreadExcludedPackages()

		
		fExcluded= new Vector(10);
		for (int i= 0; i < defaultExclusions.length; i++)
			fExcluded.addElement(defaultExclusions[i]);
			
		InputStream is= getClass().getResourceAsStream(EXCLUDED_FILE);
		if (is == null) 
			return;
		Properties p= new Properties();
		try {
			p.load(is);
		}
		catch (IOException e) {
			return;
		} finally {
			try {
				is.close();
			} catch (IOException e) {
			}
		}
		for (Enumeration e= p.propertyNames(); e.hasMoreElements(); ) {
			String key= (String)e.nextElement();
			if (key.startsWith("excluded.")) {
				String path= p.getProperty(key);
				path= path.trim();
				if (path.endsWith("*"))
					path= path.substring(0, path.length()-1);
				if (path.length() > 0) 
					fExcluded.addElement(path);				
			}
		}
	
private voidscanPath(java.lang.String classPath)

		String separator= System.getProperty("path.separator");
		fPathItems= new Vector(10);
		StringTokenizer st= new StringTokenizer(classPath, separator);
		while (st.hasMoreTokens()) {
			fPathItems.addElement(st.nextToken());
		}