You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by lu...@apache.org on 2004/09/03 15:54:44 UTC

cvs commit: jakarta-slide/testsuite/testsuite/junit/src/org/apache/slide/testsuite/testtools/tprocessor TProcessors.java

luetzkendorf    2004/09/03 06:54:44

  Modified:    testsuite/testsuite/junit/src/org/apache/slide/testsuite/testtools/tprocessor
                        TProcessors.java
  Log:
  support for conditional execution of test cases or steps added
  
  Revision  Changes    Path
  1.86      +123 -30   jakarta-slide/testsuite/testsuite/junit/src/org/apache/slide/testsuite/testtools/tprocessor/TProcessors.java
  
  Index: TProcessors.java
  ===================================================================
  RCS file: /home/cvs/jakarta-slide/testsuite/testsuite/junit/src/org/apache/slide/testsuite/testtools/tprocessor/TProcessors.java,v
  retrieving revision 1.85
  retrieving revision 1.86
  diff -u -r1.85 -r1.86
  --- TProcessors.java	3 Aug 2004 08:44:13 -0000	1.85
  +++ TProcessors.java	3 Sep 2004 13:54:43 -0000	1.86
  @@ -77,6 +77,7 @@
   import java.io.UnsupportedEncodingException;
   import java.util.ArrayList;
   import java.util.Date;
  +import java.util.Enumeration;
   import java.util.HashSet;
   import java.util.Hashtable;
   import java.util.Iterator;
  @@ -161,6 +162,9 @@
       /** count all executed test elements **/
       private long testElementsExecutedWithError = 0;
       
  +    /** count all skiped test elements */
  +    private long testElementsSkipped = 0;
  +    
       /** The absolute path name of the execute input file **/
       private String globalAbsolutePath = null;
       
  @@ -168,7 +172,7 @@
       /** The absolute path name of the execute input file **/
       private String globalTestFileName = null;
       
  -    private final Properties properties;
  +    protected final Properties properties;
       
       /** Configures from System properties **/
       public static TProcessors create() throws IOException, JDOMException {
  @@ -186,9 +190,49 @@
                                  System.getProperty("xdav.tracingRequest",  "none"),
                                  System.getProperties());
       }
  +
  +    /**
  +     * For testing purposes only, creates a TProcessor from properties found 
  +     * in xdav.home/tp.properties (where xdav.home is a system property).
  +     */
  +    private static TProcessors createFromFileProperties() throws IOException, JDOMException {
  +        String home;
  +        File config;
  +        
  +        home = System.getProperty("xdav.home");
  +        if (home == null) {
  +            throw new IOException("xdav.home is not set!");
  +        }
  +        Properties properties = new Properties();
  +        properties.load(new FileInputStream(home + File.separator + "tp.properties"));
  +        
  +        // replace ${xdav.NAME} with %NAME% in property values to enable resolution
  +        // of test variables (which is done within ant otherwise)
  +        for(Enumeration e = properties.propertyNames(); e.hasMoreElements(); ) {
  +            String key = (String)e.nextElement();
  +            String value = properties.getProperty(key);
  +            value = replace(value, "${xdav.", "%");
  +            value = replace(value, "}", "%");
  +            properties.setProperty(key, value);
  +        }
  +        
  +        config = new File(home + File.separator + "testsuite" + 
  +                File.separator + "junit" + File.separator + "tprocessor.xml");
  +        return new TProcessors(config, 
  +                properties.getProperty("xdav.host"),
  +                properties.getProperty("xdav.port"), 
  +                properties.getProperty("xdav.user"),
  +                properties.getProperty("xdav.password"), 
  +                properties.getProperty("xdav.urlencoding"),
  +                properties.getProperty("xdav.tracingRequest",  "none"),
  +                properties);
  +    }
  +
       
       /**
        * Constructor
  +     * 
  +     * @param config configuration file (see testsuite/testsuite/junit/tprocessor.xml)
        */
       
       public TProcessors (File config, String host, String port, String defaultUser,
  @@ -254,7 +298,11 @@
           
           
           try {
  -            tp = TProcessors.create();
  +            if (arg.isOptionSet("file")) {
  +                tp = TProcessors.createFromFileProperties();
  +            } else {
  +                tp = TProcessors.create();
  +            }
           } catch (Exception e) {
               e.printStackTrace();
               System.exit(-1);
  @@ -398,19 +446,32 @@
           xmlresult.writeRootElementStart("testCase");
           xmlresult.writeElement("fileName", testfile.getAbsolutePath());
           
  -        try{
  -            globalTestFileName = testfile.getName();
  -            globalAbsolutePath = testfile.getParent();
  -            //          globalAbsolutePath = testfile.;
  -            exceuteStepOrRepeater(new SAXBuilder(true).build(testfile).getRootElement().getChildren().iterator());
  -        } catch (JDOMException e){
  -            xmlresult.writeElement("domException", e.toString());
  -            throw e;
  +        Element testElement = new SAXBuilder(true).build(testfile).getRootElement();
  +        Element specElement = testElement.getChild("specification");
  +        boolean conditionsOk = true;
  +        if (specElement != null) {
  +            conditionsOk = conditionsAreFullfilled(
  +                    specElement.getChildren("condition").iterator());
  +        }
  +        
  +        if (conditionsOk) {
  +            try{
  +                globalTestFileName = testfile.getName();
  +                globalAbsolutePath = testfile.getParent();
  +                //          globalAbsolutePath = testfile.;
  +                exceuteStepOrRepeater(testElement.getChildren().iterator());
  +            } catch (JDOMException e){
  +                xmlresult.writeElement("domException", e.toString());
  +                throw e;
  +            }
  +            xmlresult.writeElement("result",  ((testElementsExecutedWithError!=0)?"ERROR":"Success"));
  +            xmlresult.writeElement("time",  ((System.currentTimeMillis() - time)));
  +            xmlresult.writeElement("testElementCount",  testElementsExecuted);
  +            xmlresult.writeElement("testElementErrors",  testElementsExecutedWithError);
  +            xmlresult.writeElement("testElementSkipped",  testElementsSkipped);
  +        } else {
  +            xmlresult.writeElement("result",  "SKIPPED");
           }
  -        xmlresult.writeElement("result",  ((testElementsExecutedWithError!=0)?"ERROR":"Success"));
  -        xmlresult.writeElement("time",  ((System.currentTimeMillis() - time)));
  -        xmlresult.writeElement("testElementCount",  testElementsExecuted);
  -        xmlresult.writeElement("testElementErrors",  testElementsExecutedWithError);
           xmlresult.writeRootElementEnd("testCase");
           xmlresult.flush();
           
  @@ -418,7 +479,7 @@
       }
       
       
  -    private boolean exceuteStepOrRepeater(Iterator items) throws IOException, JDOMException {
  +    protected boolean exceuteStepOrRepeater(Iterator items) throws IOException, JDOMException {
           boolean result           = true;
           Vector  threadsToExceute = new Vector(10);
           Vector  cleanUpThreads   = new Vector(10);
  @@ -460,7 +521,7 @@
       }
       
       
  -    private boolean checkCondition(Element conditionContainer, String condition) throws IOException {
  +    private boolean checkRepeaterCondition(Element conditionContainer, String condition) throws IOException {
           if (condition == null) return false;
           String evaluatedCondition = replaceKnownVariable(conditionContainer, condition, "x=x");
           StringTokenizer expression = new StringTokenizer(evaluatedCondition, "=");
  @@ -508,7 +569,7 @@
                   knownVariables.put(varName, "undefined");
                   int stackMarker = knownVariables.size();
                   long maximum = new Long(replaceKnownVariable(item, item.getAttribute("repeatCount").getValue(), "0")).longValue();
  -                for (long i = 0; i < maximum && !checkCondition(item, condition); i++){
  +                for (long i = 0; i < maximum && !checkRepeaterCondition(item, condition); i++){
                       knownVariables.removeAllFramedVariables(stackMarker);
                       knownVariables.put(varName, new Long(i+1).toString());
                       result = exceuteStepOrRepeater(item.getChildren().iterator(), threadsToExceute, cleanUpThreads, client) && result;
  @@ -553,6 +614,21 @@
           long    executionTime = 0;
           HttpMethod method     = null;
           
  +        xmlresult.writeElementStart("exceuteStep");
  +        
  +        boolean conditionsOk = conditionsAreFullfilled(
  +                elt.getChildren("condition").iterator());
  +        
  +        if (!conditionsOk) {
  +            xmlresult.writeElement("result", ("SKIPPED"));
  +            xmlresult.writeElement("time",  (executionTime));
  +            xmlresult.writeElementEnd("exceuteStep");
  +            this.testElementsExecuted++;
  +            this.testElementsSkipped++;
  +            return true;
  +        }
  +        
  +
           // initialise user and password
           knownVariables.put("user", defaultUser);
           knownVariables.put("password", defaultPassword);
  @@ -575,8 +651,6 @@
           //              System.out.println("############### user " + user);
           //              System.out.println("############### pwd  " + password);
           
  -        xmlresult.writeElementStart("exceuteStep");
  -        
           try{
               method = executeRequest(elt.getChild("request"));
               if (method == null) {
  @@ -637,16 +711,14 @@
               
               result = (responseAssert(method, elt));
               
  -            
  -            method.releaseConnection();
  -            
  -
           } catch (JDOMException e) {
               xmlresult.writeException(e);
               throw e;
           } catch (IOException e) {
               xmlresult.writeException( e );
               throw e;
  +        } finally {
  +            if (method != null) method.releaseConnection();
           }
           
           if (!result) testElementsExecutedWithError ++;
  @@ -1348,6 +1420,25 @@
           }
       }
       
  +    private boolean conditionsAreFullfilled(Iterator conditions) {
  +        boolean result = true;
  +        for (;conditions.hasNext();) {
  +            result &= checkCondition((Element)conditions.next());
  +        }
  +        return result;
  +    }
  +    private boolean checkCondition(Element condition) {
  +        String name = condition.getAttributeValue("name");
  +        String requiredValue = condition.getTextTrim();
  +        String givenValue = (String)properties.get("xdav.condition." + name);
  +        
  +        boolean ok = (givenValue != null && givenValue.equals(requiredValue));
  +        if (!ok) {
  +            xmlresult.writeElement("violated-condition",
  +                    name + "=" + requiredValue + " (" + givenValue + ")");
  +        }
  +        return ok;
  +    }
       
       private Element getMethodElement(XMLResponseMethodBase m) {
           return new org.jdom.input.DOMBuilder().build(m.getResponseDocument().getDocumentElement());
  @@ -1770,9 +1861,11 @@
               Credentials cred = new UsernamePasswordCredentials(user, password);
               //        state.setURLDecodingCharset(defaultUrlEncoding);
               //        state.setURLEncodingCharset(defaultUrlEncoding);
  -            startSession((String)startUp.get("host"),
  -                             ((Integer)startUp.get("port") ).intValue());
  -            state.setCredentials(null, cred);
  +//            startSession((String)startUp.get("host"),
  +//                             ((Integer)startUp.get("port") ).intValue());
  +            getHostConfiguration().setHost((String)startUp.get("host"),
  +                    ((Integer)startUp.get("port") ).intValue());
  +            state.setCredentials(null, (String)startUp.get("host"), cred);
               state.setAuthenticationPreemptive(true); // avoid non-authenticated method invocation
               setState(state);
           }
  
  
  

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