woolfel     2005/08/08 21:02:19

  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 sampler so that it first looks to see if the test class has a 
string
  constructor. if none is found, it then looks for empty constructor.
  peter
  
  Revision  Changes    Path
  1.9       +59 -35    
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.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- JUnitSampler.java 4 Aug 2005 03:34:10 -0000       1.8
  +++ JUnitSampler.java 9 Aug 2005 04:02:19 -0000       1.9
  @@ -34,9 +34,9 @@
    * @author pete
    *
    * This is a basic implementation that runs a single test method of
  - * a JUnit test case. The current implementation doesn't support
  - * oneTimeSetUp yet. Still need to think about that more thoroughly
  - * and decide how it should be called.
  + * a JUnit test case. The current implementation will use the string
  + * constructor first. If the test class does not declare a string
  + * constructor, the sampler will try empty constructor.
    */
   public class JUnitSampler extends AbstractSampler {
   
  @@ -45,6 +45,7 @@
        * user.
        */
       public static final String CLASSNAME = "junitSampler.classname";
  +    public static final String CONSTRUCTORSTRING = 
"junitsampler.constructorstring";
       public static final String METHOD = "junitsampler.method";
       public static final String ERROR = "junitsampler.error";
       public static final String ERRORCODE = "junitsampler.error.code";
  @@ -110,6 +111,25 @@
       }
       
       /**
  +     * Set the string label used to create an instance of the
  +     * test with the string constructor.
  +     * @param constr
  +     */
  +    public void setConstructorString(String constr)
  +    {
  +        setProperty(CONSTRUCTORSTRING,constr);
  +    }
  +    
  +    /**
  +     * get the string passed to the string constructor
  +     * @return
  +     */
  +    public String getConstructorString()
  +    {
  +        return getPropertyAsString(CONSTRUCTORSTRING);
  +    }
  +    
  +    /**
        * Return the name of the method to test
        * @return
        */
  @@ -257,13 +277,19 @@
         */
        public SampleResult sample(Entry entry) {
                SampleResult sresult = new SampleResult();
  -        sresult.setSampleLabel(JUnitSampler.class.getName());
  +        String rlabel = null;
  +        if (getConstructorString().length() > 0) {
  +            rlabel = getConstructorString();
  +        } else {
  +            rlabel = JUnitSampler.class.getName();
  +        }
  +        sresult.setSampleLabel(rlabel);
           sresult.setSamplerData(getClassname() + "." + getMethod());
           // check to see if the test class is null. if it is, we create
           // 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.getClassname());
  +            this.TEST_INSTANCE = 
(TestCase)getClassInstance(getClassname(),rlabel);
           }
           if (this.TEST_INSTANCE != null){
               initMethodObjects(this.TEST_INSTANCE);
  @@ -282,11 +308,11 @@
                       TDOWN_METHOD.invoke(TEST_INSTANCE,new Class[0]);
                   }
               } catch (InvocationTargetException e) {
  -                log.warn(e.getMessage());
  +                // log.warn(e.getMessage());
                   sresult.setResponseCode(getErrorCode());
                   sresult.setResponseMessage(getError());
               } catch (IllegalAccessException e) {
  -                log.warn(e.getMessage());
  +                // log.warn(e.getMessage());
                   sresult.setResponseCode(getErrorCode());
                   sresult.setResponseMessage(getError());
               } catch (Exception e) {
  @@ -324,12 +350,12 @@
        * warning level.
        * @return
        */
  -    public static Object getClassInstance(String className){
  +    public static Object getClassInstance(String className, String label){
           Object testclass = null;
           if (className != null){
               Constructor con = null;
  -            Class clazz = null;
               Class theclazz = null;
  +            Object[] params = null;
               try
               {
                   theclazz = Class.forName(
  @@ -338,42 +364,40 @@
                               Thread.currentThread().getContextClassLoader()
                           );
               } catch (ClassNotFoundException e) {
  -                log.warn(e.getMessage());
  +                log.warn("ClassNotFoundException:: " + e.getMessage());
               }
               if (theclazz != null) {
  +                // first we see if the class declares a string
  +                // constructor. if it is doesn't we look for
  +                // empty constructor.
                   try {
  -                    con = theclazz.getDeclaredConstructor(new Class[0]);
  -                    if (con != null){
  -                        testclass = (TestCase)theclazz.newInstance();
  -                    }
  +                    con = theclazz.getDeclaredConstructor(
  +                            new Class[] {String.class});
  +                    params = new Object[]{label};
                   } catch (NoSuchMethodException e) {
  -                    log.info(e.getMessage());
  -                } catch (InstantiationException e) {
  -                    log.info(e.getMessage());
  -                } catch (IllegalAccessException e) {
  -                    log.info(e.getMessage());
  +                    log.info("String constructor:: " + 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 ){
  +                if (con == null ){
                       try {
  -                        Constructor con2 = theclazz.getDeclaredConstructor(
  -                                new Class[] {String.class});
  -                        if (con2 != null){
  -                            Object[] pm = {className};
  -                            testclass = (TestCase)con2.newInstance(pm);
  +                        con = theclazz.getDeclaredConstructor(new Class[0]);
  +                        if (con != null){
  +                            params = new Object[]{};
                           }
                       } 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());
  +                        log.info("Empty constructor:: " + e.getMessage());
                       }
                   }
  +                try {
  +                    if (con != null){
  +                        testclass = (TestCase)con.newInstance(params);
  +                    }
  +                } catch (InvocationTargetException e) {
  +                    log.warn(e.getMessage());
  +                } catch (InstantiationException e) {
  +                    log.info(e.getMessage());
  +                } catch (IllegalAccessException e) {
  +                    log.info(e.getMessage());
  +                }
               }
           }
           return testclass;
  
  
  
  1.12      +9 -2      
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.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- JUnitTestSamplerGui.java  5 Aug 2005 04:04:11 -0000       1.11
  +++ JUnitTestSamplerGui.java  9 Aug 2005 04:02:19 -0000       1.12
  @@ -62,6 +62,10 @@
       public static final String SUITE = "suite";
       protected String[] SPATHS = null;
   
  +    JLabeledTextField constructorLabel =
  +        new JLabeledTextField(
  +            JMeterUtils.getResString("junit_constructor_string"));
  +
       JLabel methodLabel =
           new JLabel(
               JMeterUtils.getResString("junit_test_method"));
  @@ -180,6 +184,7 @@
           if (classnameCombo != null){
               panel.add(classnameCombo);
           }
  +        panel.add(constructorLabel);
           panel.add(methodLabel);
           if (methodName != null){
               panel.add(methodName);
  @@ -212,6 +217,7 @@
                   classnameCombo.getSelectedItem() instanceof String) {
               sampler.setClassname((String)classnameCombo.getSelectedItem());
           }
  +        sampler.setConstructorString(constructorLabel.getText());
           if (methodName.getSelectedItem() != null) {
               Object mobj = methodName.getSelectedItem();
               sampler.setMethod((String)mobj);
  @@ -233,6 +239,7 @@
           instantiateClass();
           methodName.setSelectedItem(sampler.getMethod());
           filterpkg.setText(sampler.getFilterString());
  +        constructorLabel.setText(sampler.getConstructorString());
           if (sampler.getSuccessCode().length() > 0) {
               successCode.setText(sampler.getSuccessCode());
           } else {
  @@ -270,7 +277,7 @@
           String className =
               ((String) classnameCombo.getSelectedItem());
           if (className != null) {
  -            TESTCLASS = (TestCase)JUnitSampler.getClassInstance(className);
  +            TESTCLASS = 
(TestCase)JUnitSampler.getClassInstance(className,"");
               if (TESTCLASS == null) {
                   clearMethodCombo();
               }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to