You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jmeter-dev@jakarta.apache.org by wo...@apache.org on 2005/08/03 06:33:28 UTC

cvs commit: jakarta-jmeter/src/junit/org/apache/jmeter/protocol/java/control/gui JUnitTestSamplerGui.java

woolfel     2005/08/02 21:33:28

  Modified:    src/junit/org/apache/jmeter/protocol/java/sampler
                        JUnitSampler.java
               src/junit/org/apache/jmeter/protocol/java/control/gui
                        JUnitTestSamplerGui.java
  Log:
  updated the junitsampler so it correctly handles classes that do not declare an
  empty constructor, or declare the empty constructor private. If a class does
  either case, the sampler will log it and attempt to create a new instance with
  string constructor. if both fail, it isn't a valid JUnit test.
  peter
  
  Revision  Changes    Path
  1.7       +53 -17    jakarta-jmeter/src/junit/org/apache/jmeter/protocol/java/sampler/JUnitSampler.java
  
  Index: JUnitSampler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jmeter/src/junit/org/apache/jmeter/protocol/java/sampler/JUnitSampler.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- JUnitSampler.java	30 Jul 2005 02:41:18 -0000	1.6
  +++ JUnitSampler.java	3 Aug 2005 04:33:28 -0000	1.7
  @@ -16,6 +16,7 @@
    */
   package org.apache.jmeter.protocol.java.sampler;
   
  +import java.lang.reflect.Constructor;
   import java.lang.reflect.InvocationTargetException;
   import java.lang.reflect.Method;
   import java.util.Enumeration;
  @@ -223,7 +224,7 @@
           // a new instance. this should only happen at the start of a
           // test run
           if (this.TEST_INSTANCE == null) {
  -            this.TEST_INSTANCE = (TestCase)getClassInstance();
  +            this.TEST_INSTANCE = (TestCase)getClassInstance(this.getClassname());
           }
           if (this.TEST_INSTANCE != null){
               initMethodObjects(this.TEST_INSTANCE);
  @@ -279,24 +280,59 @@
        * warning level.
        * @return
        */
  -    public Object getClassInstance(){
  -        if (getClassname() != null){
  -            try {
  -                Class clazz = Class.forName(getClassname());
  -                return clazz.getDeclaredConstructor(new Class[0]).newInstance(null);
  -            } catch (ClassNotFoundException ex){
  -                log.warn(ex.getMessage());
  -            } catch (IllegalAccessException ex){
  -                log.warn(ex.getMessage());
  -            } catch (NoSuchMethodException ex){
  -                log.warn(ex.getMessage());
  -            } catch (InvocationTargetException ex){
  -                log.warn(ex.getMessage());
  -            } catch (InstantiationException ex){
  -                log.warn(ex.getMessage());
  +    public static Object getClassInstance(String className){
  +        Object testclass = null;
  +        if (className != null){
  +            Constructor con = null;
  +            Class clazz = null;
  +            Class theclazz = null;
  +            try
  +            {
  +                theclazz = Class.forName(
  +                            className.trim(),
  +                            true,
  +                            Thread.currentThread().getContextClassLoader()
  +                        );
  +            } catch (ClassNotFoundException e) {
  +                log.warn(e.getMessage());
  +            }
  +            if (theclazz != null) {
  +                try {
  +                    con = theclazz.getDeclaredConstructor(new Class[0]);
  +                    if (con != null){
  +                        testclass = (TestCase)theclazz.newInstance();
  +                    }
  +                } catch (NoSuchMethodException e) {
  +                    log.info(e.getMessage());
  +                } catch (InstantiationException e) {
  +                    log.info(e.getMessage());
  +                } catch (IllegalAccessException e) {
  +                    log.info(e.getMessage());
  +                }
  +                // only if we weren't able to create an instance of the class
  +                // with a null constructor do we try to create one with the
  +                // string constructor.
  +                if (testclass == null ){
  +                    try {
  +                        Constructor con2 = theclazz.getDeclaredConstructor(
  +                                new Class[] {String.class});
  +                        if (con2 != null){
  +                            Object[] pm = {className};
  +                            testclass = (TestCase)con2.newInstance(pm);
  +                        }
  +                    } catch (NoSuchMethodException e) {
  +                        log.info(e.getMessage());
  +                    } catch (InvocationTargetException e) {
  +                        log.warn(e.getMessage());
  +                    } catch (IllegalAccessException e) {
  +                        log.info(e.getMessage());
  +                    } catch (InstantiationException e) {
  +                        log.info(e.getMessage());
  +                    }
  +                }
               }
           }
  -        return null;
  +        return testclass;
       }
       
       /**
  
  
  
  1.9       +4 -53     jakarta-jmeter/src/junit/org/apache/jmeter/protocol/java/control/gui/JUnitTestSamplerGui.java
  
  Index: JUnitTestSamplerGui.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jmeter/src/junit/org/apache/jmeter/protocol/java/control/gui/JUnitTestSamplerGui.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- JUnitTestSamplerGui.java	30 Jul 2005 02:41:18 -0000	1.8
  +++ JUnitTestSamplerGui.java	3 Aug 2005 04:33:28 -0000	1.9
  @@ -20,8 +20,6 @@
   import java.awt.BorderLayout;
   import java.awt.event.ActionEvent;
   import java.awt.event.ActionListener;
  -import java.lang.reflect.Constructor;
  -import java.lang.reflect.InvocationTargetException;
   import java.lang.reflect.Method;
   import java.util.ArrayList;
   import java.util.List;
  @@ -251,56 +249,9 @@
           String className =
               ((String) classnameCombo.getSelectedItem());
           if (className != null) {
  -            TESTCLASS = null;
  -            Class theclazz = null;
  -            try
  -            {
  -                theclazz = Class.forName(
  -                            className.trim(),
  -                            true,
  -                            Thread.currentThread().getContextClassLoader()
  -                        );
  -            } catch (ClassNotFoundException e) {
  -                log.warn(e.getMessage());
  -            }
  -            if (theclazz != null) {
  -                try {
  -                    Constructor con = theclazz.getDeclaredConstructor(new Class[0]);
  -                    if (con != null){
  -                        TESTCLASS = (TestCase)theclazz.newInstance();
  -                    }
  -                } catch (NoSuchMethodException e) {
  -                    log.info("The class " + className + " does not have " +
  -                    "an empty constructor");
  -                    clearMethodCombo();
  -                } catch (InstantiationException e) {
  -                    log.info(e.getMessage());
  -                } catch (IllegalAccessException e) {
  -                    log.info(e.getMessage());
  -                }
  -                // only if we weren't able to create an instance of the class
  -                // with a null constructor do we try to create one with the
  -                // string constructor.
  -                if (TESTCLASS == null ){
  -                    try {
  -                        Constructor con2 = theclazz.getDeclaredConstructor(
  -                                new Class[] {String.class});
  -                        if (con2 != null){
  -                            Object[] pm = {className};
  -                            TESTCLASS = (TestCase)con2.newInstance(pm);
  -                        }
  -                    } catch (NoSuchMethodException e) {
  -                        log.info("The class " + className + " does not have " +
  -                        "a string constructor");
  -                        clearMethodCombo();
  -                    } catch (InvocationTargetException e) {
  -                        log.warn(e.getMessage());
  -                    } catch (IllegalAccessException e) {
  -                        log.info(e.getMessage());
  -                    } catch (InstantiationException e) {
  -                        log.info(e.getMessage());
  -                    }
  -                }
  +            TESTCLASS = (TestCase)JUnitSampler.getClassInstance(className);
  +            if (TESTCLASS == null) {
  +                clearMethodCombo();
               }
               configureMethodCombo();
           }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org