alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 * Copyright 2001-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 

package org.apache.commons.logging;


import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;


/**
 * 

Wrapper around test cases that need to have a custom class loader * hierarchy assembled. The wrapper is configured by the following * system properties:

*
    *
  • wrapper.hierarchy - Descriptive code describing how * the class loader hierarchy should be assembled: *
      *
    • API - Parent class loader contains * commons-logging-api.jar and child class loader * contains commons-logging.jar. This is like the * default configuration for Tomcat 4.1.
    • *
    • FULL - Parent class loader contains * commons-logging.jar. This is what would happen * if you replaced commons-logging-api.jar with * commons-logging.jar so that you did not need to * include the latter with your application.
    • *
    * The child class loader also unconditionally includes * commons-logging-tests.jar.
  • *
  • wrapper.junit - Fully qualified pathname of the * JUnit JAR file.
  • *
  • wrapper.log4j - Fully qualified pathname of the * Log4J JAR file, which will be placed in whichever class loader * commons-logging.jar is placed in, if specified.
  • *
  • wrapper.target - Fully qualified pathname of the * "target" directory created by the build process. This directory * must contain the commons-logging.jar, * commons-logging-api.jar, and * commons-logging-tests.jar files resulting from the * execution of the compile.tests target.
  • *
  • wrapper.testcase - Fully qualified Java class name * of a TestCase that will ultimately be executed. This class must * exist in the commons-logging-tests.jar file.
  • *
* *

When executed, the system classpath for the wrapper should include * only the wrapper class itself.

* * @author Craig R. McClanahan * @version $Revision: 1.5 $ $Date: 2004/02/28 21:46:45 $ */ public class Wrapper { public static void main(String args[]) { try { // Create variables we will need List parentList = new ArrayList(); List childList = new ArrayList(); URL urls[] = null; // Construct URLs for the various JAR files File target = new File(System.getProperty("wrapper.target")); URL commonsLogging = (new File(target, "commons-logging.jar")).toURL(); URL commonsLoggingApi = (new File(target, "commons-logging-api.jar")).toURL(); URL commonsLoggingTests = (new File(target, "commons-logging-tests.jar")).toURL(); URL junit = (new File(System.getProperty("wrapper.junit"))).toURL(); URL appender = null; URL log4j = null; if (System.getProperty("wrapper.log4j") != null) { log4j = (new File(System.getProperty("wrapper.log4j"))).toURL(); appender = (new File(target, "commons-logging-appender.jar")).toURL(); } // Construct class loader repository lists for supported scenarios if ("API".equals(System.getProperty("wrapper.hierarchy"))) { parentList.add(commonsLoggingApi); childList.add(commonsLogging); if (log4j != null) { childList.add(log4j); childList.add(appender); } } else { // Assumes "FULL" parentList.add(commonsLogging); if (log4j != null) { parentList.add(log4j); childList.add(appender); } } childList.add(commonsLoggingTests); childList.add(junit); // Construt the parent and child class loaders urls = (URL[]) parentList.toArray(new URL[parentList.size()]); ClassLoader parent = new URLClassLoader(urls, ClassLoader.getSystemClassLoader()); urls = (URL[]) childList.toArray(new URL[childList.size()]); ClassLoader child = new URLClassLoader(urls, parent); // Execute the test runner for this TestCase ClassLoader old = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(child); Class clazz = child.loadClass("junit.textui.TestRunner"); String params[] = new String[1]; params[0] = System.getProperty("wrapper.testcase"); Method method = clazz.getMethod("main", new Class[] { params.getClass() }); method.invoke(null, new Object[] { params }); Thread.currentThread().setContextClassLoader(old); } catch (Exception e) { System.out.println("Wrapper Exception Occurred: " + e); e.printStackTrace(System.out); System.exit(1); } } }
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.