001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.rewriter.test.util;
006:
007: import com.sun.portal.rewriter.util.Resource;
008: import com.sun.portal.rewriter.util.StringHelper;
009: import junit.framework.Test;
010: import junitx.util.ArchiveSuiteBuilder;
011: import junitx.util.DirectorySuiteBuilder;
012: import junitx.util.TestFilter;
013:
014: public class PackageSuiteBuilder {
015: public static final String DEFAULT_TEST_FILTER_CLASS = "DEFAULT_TEST_FILETER_CLASS";
016:
017: private final Class dirRootClass;
018: private TestFilter filterClass = null;
019:
020: public PackageSuiteBuilder(final String aClassName) {
021: this (aClassName, System.getProperty(DEFAULT_TEST_FILTER_CLASS));
022: }//constructor
023:
024: public PackageSuiteBuilder(final String aClassName,
025: final String aTestFilterClass) {
026: dirRootClass = getClass(aClassName);
027:
028: try {
029: filterClass = (TestFilter) (getClass(aTestFilterClass)
030: .newInstance());
031: } catch (Exception e) {
032: System.out
033: .println("Unable to Instantiate the TestFilter Instance");
034: e.printStackTrace();
035: System.exit(0);
036: }
037: }//constructor
038:
039: public Test suite() {
040: String file = "";
041: try {
042: file = dirRootClass.getProtectionDomain().getCodeSource()
043: .getLocation().toString().substring(
044: "file:".length());
045: if (file.endsWith(".jar")) {
046: ArchiveSuiteBuilder builder = new ArchiveSuiteBuilder(
047: filterClass);
048: return builder.suite(file);
049: } else {
050: String classLocation = file
051: + dirRootClass.getPackage().getName().replace(
052: '.', '/');
053: DirectorySuiteBuilder builder = new DirectorySuiteBuilder(
054: filterClass);
055: return builder.suite(classLocation);
056: }
057: } catch (Exception e) {
058: System.out.println("Unable to fetch the suite from File: "
059: + file);
060: e.printStackTrace();
061: return null;
062: }
063: }//suite()
064:
065: private static Class getClass(String aClassName) {
066: if (aClassName.indexOf('/') != -1) {
067: aClassName = StringHelper.searchAndReplace(aClassName, "/",
068: ".");
069: }
070:
071: if (aClassName.endsWith(".class")) {
072: aClassName = aClassName.substring(0, aClassName.length()
073: - ".class".length());
074: }
075:
076: try {
077: return Class.forName(aClassName);
078: } catch (ClassNotFoundException e) {
079: System.out.println("Class:" + aClassName
080: + " does not exists!! aborting..");
081: e.printStackTrace();
082: System.exit(0);
083: return null;
084: }
085: }//getClass()
086:
087: public static void main(String[] args) {
088: if (args.length == 0 || args.length > 2) {
089: System.out
090: .println("Ussage: PackageSuiteBuilder <Fully Qualified Class Name> [<TestFilterClass>]");
091: System.exit(0);
092: }
093:
094: PackageSuiteBuilder lSuiteBuilder = new PackageSuiteBuilder(
095: args[0]);
096: if (args.length == 2) {
097: System.setProperty(DEFAULT_TEST_FILTER_CLASS, args[1]);
098: }
099:
100: BasicTestCase.run(lSuiteBuilder.suite());
101: }//main()
102:
103: private static class DirectoryClassLoader extends ClassLoader {
104: public Class findClass(String name)
105: throws ClassNotFoundException {
106: byte classBytes[] = Resource.readBytes(name);
107: Class cl = defineClass(null, classBytes, 0,
108: classBytes.length);
109: cl = Class.forName(cl.getName());
110: return cl;
111: }
112: }//class DirectoryClassLoader
113:
114: }//class PackageSuiteBuilder
|