続・Singletonをテストする

固めてupはムリそうなのでコピペ。

// TestSuiteForSingleton.java
package uronim1.util.junit;

import junit.framework.Test;
import junit.framework.TestSuite;


public class TestSuiteForSingleton extends TestSuite {

    public TestSuiteForSingleton() {
    }
    
    public TestSuiteForSingleton(Class clazz, String name) {
        this(clazz);
        setName(name);
    }
        
    public TestSuiteForSingleton(String name) {
        super(name);
    }
    
    public TestSuiteForSingleton(Class clazz) {
        TestCaseBuilder builder = new TestCaseBuilder(clazz);
        
        Test[] testCases = builder.createTestCasesForSingleton();
        for(int i = 0; i < testCases.length; i++) {
            addTest(testCases[i]);            
        }
    }
}
// TestCaseBuilder.java
package uronim1.util.junit;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.runner.TestCaseClassLoader;


public class TestCaseBuilder {
    
    private Class testCase_ = null;
    
    public TestCaseBuilder(Class clazz) {
        if( !TestCase.class.isAssignableFrom(clazz) ) {
            throw new IllegalArgumentException();
        }
        testCase_ = clazz;
    }
    
    public Test createTestCaseForSingleton(String name) {
        TestCaseClassLoader loader = new TestCaseClassLoader();
        
        try {
            Class clazz = loader.loadClass(testCase_.getName(), true);
            Constructor c = clazz.getConstructor(new Class[]{String.class});
            return (Test) c.newInstance(new Object[]{name});
        } catch (ClassNotFoundException e) {
            return(warning("Cannot find class: "+name+" ("+exceptionToString(e)+")"));
        } catch (NoSuchMethodException e) {
            return(warning("Cannot find constructor: "+name+" ("+exceptionToString(e)+")"));
        } catch (InstantiationException e) {
            return(warning("Cannot instantiate test case: "+name+" ("+exceptionToString(e)+")"));
        } catch (InvocationTargetException e) {
            return(warning("Exception in constructor: "+name+" ("+exceptionToString(e.getTargetException())+")"));
        } catch (IllegalAccessException e) {
            return(warning("Cannot access test case: "+name+" ("+exceptionToString(e)+")"));
        }
    }
    
    protected Test[] createTestCasesForSingleton() {
        Method[] testMethods = getTestMethods();
        
        Test[] testCases = new TestCase[testMethods.length];
        for(int i = 0; i < testMethods.length; i++) {
            testCases[i] = createTestCaseForSingleton(testMethods[i].getName());
        }
        
        return testCases;
    }
    
    protected Method[] getTestMethods() {
        Method[] methods = testCase_.getMethods();
        List testMethods = new ArrayList();
        
        for(int i = 0; i < methods.length; i++) {
            if( Modifier.isPublic(methods[i].getModifiers()) &&
                methods[i].getName().startsWith("test") &&
                methods[i].getParameterTypes().length == 0 &&
                methods[i].getReturnType().equals(Void.TYPE) ) {
                testMethods.add(methods[i]);
            }
        }
        
        return (Method[])testMethods.toArray(new Method[0]);
    }
    
    private static String exceptionToString(Throwable t) {
        StringWriter stringWriter= new StringWriter();
        PrintWriter writer= new PrintWriter(stringWriter);
        t.printStackTrace(writer);
        return stringWriter.toString();
    }
    
    private static Test warning(final String message) {
        return new TestCase("warning") {
            protected void runTest() {
                fail(message);
            }
        };
    }
}

ぐっ。眠い...