TestSuite.java :  » Development » Monolog » org » objectweb » util » monolog » Java Open Source

Java Open Source » Development » Monolog 
Monolog » org » objectweb » util » monolog » TestSuite.java
/**
 * Copyright (C) 2002
 */

package org.objectweb.util.monolog;

import junit.framework.TestCase;

import java.lang.reflect.Method;

/**
 *
 * @author Sebastien Chassande-Barrioz
 */
public class TestSuite extends junit.framework.TestSuite {

  public TestSuite(Class c, String[] setters, Object[] params)
    throws Exception {
    //super();
    Class[][] paramtypes = new Class[params.length][];
    for (int i = 0; i < params.length; i++) {
      paramtypes[i] = new Class[1];
      if (params != null) {
        paramtypes[i][0] = params[i].getClass();
      }
      else {
        paramtypes[i][0] = null;
      }
    }

    Method[] ms = c.getMethods();
    for (int i = 0; i < ms.length; i++) {
      if (ms[i].getParameterTypes().length == 0
        && ms[i].getName().startsWith("test")) {
        addTest(
          getTest(ms[i].getName(), c, setters, params, paramtypes));
      }
    }
  }

  private TestCase getTest(String name, Class c, String[] setters,
               Object[] params,
               Class[][] paramtypes) throws Exception {
    TestCase tc = (TestCase) c.newInstance();;
    tc.setName(name);
    for (int param = 0; param < params.length; param++) {
      Method m = null;
      debug("Search method: " + setters[param] + "(" + (paramtypes[param]==null ? null : paramtypes[param][0]) + ")");
      if (paramtypes[param]==null) {
        m = getFirstMethod(c, setters[param]);
      }
      else {
        m = c.getMethod(setters[param], paramtypes[param]);
      }
      debug("found method: " + m);
      if (m==null) {
        throw new Exception("Method " + setters[param] + " not found");
      }
      Object[] p = {params[param]};
      m.invoke(tc, p);
    }
    return tc;
  }

  private Method getFirstMethod(Class c, String mn) {
    Method[] ms = c.getMethods();
    int i;
    for(i=0; i<ms.length && ms[i].getName().equals(mn); i++);
    return (i<ms.length ? ms[i] : null);
  }

  protected void debug(String m) {
    //System.out.println(m);
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.