buildframework/helium/sf/java/legacy/src/com/nokia/ant/conditions/AtsCondition.java
author wbernard
Fri, 13 Aug 2010 14:59:05 +0300
changeset 628 7c4a911dc066
parent 588 c7c26511138f
permissions -rw-r--r--
helium_11.0.0-e00f171ca185
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     1
/*
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
     2
 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
     3
 * All rights reserved.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
     4
 * This component and the accompanying materials are made available
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
     5
 * under the terms of the License "Eclipse Public License v1.0"
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
     6
 * which accompanies this distribution, and is available
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
     7
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
     8
 *
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
     9
 * Initial Contributors:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    10
 * Nokia Corporation - initial contribution.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    11
 *
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    12
 * Contributors:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    13
 *
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    14
 * Description: 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    15
 *
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    16
 */
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    17
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    18
package com.nokia.ant.conditions;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    19
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    20
import java.net.MalformedURLException;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    21
import java.net.URL;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    22
import java.util.Iterator;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    23
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    24
import org.apache.tools.ant.Project;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    25
import org.apache.tools.ant.ProjectComponent;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    26
import org.apache.tools.ant.taskdefs.condition.Condition;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    27
import org.dom4j.Document;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    28
import org.dom4j.DocumentException;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    29
import org.dom4j.Element;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    30
import org.dom4j.io.SAXReader;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    31
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    32
/**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    33
 * Condition to read check from diamonds and tell if ats has failed
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    34
 * 
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    35
 * @ant.type name="hasAtsPassed"
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    36
 */
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    37
public class AtsCondition extends ProjectComponent implements Condition {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    38
    private int sleeptimesecs = 60;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    39
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    40
    public void setSleeptime(int seconds) {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    41
        sleeptimesecs = seconds;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    42
    }
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    43
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    44
    /** Read from diamonds and signal if ats failed */
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    45
    public boolean eval() {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    46
        String bid = getProject().getProperty("diamonds.build.id");
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    47
        if (bid == null) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    48
            log("Diamonds not enabled");
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    49
        }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    50
        else {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    51
            boolean testsfound = false;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    52
            log("Looking for tests in diamonds");
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    53
            SAXReader xmlReader = new SAXReader();
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    54
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    55
            while (!testsfound) {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    56
                Document antDoc = null;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    57
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    58
                try {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    59
                    URL url = new URL("http://" + getProject().getProperty("diamonds.host") + bid
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    60
                        + "?fmt=xml");
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    61
                    antDoc = xmlReader.read(url);
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    62
                }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    63
                catch (MalformedURLException e) {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    64
                    // We are Ignoring the errors as no need to fail the build.
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    65
                    log("Not able to read the Diamonds URL http://"
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    66
                        + getProject().getProperty("diamonds.host") + bid + "?fmt=xml: "
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    67
                        + e.getMessage(), Project.MSG_ERR);
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    68
                }
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    69
                catch (DocumentException e) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    70
                    log("Not able to read the Diamonds URL http://"
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    71
                        + getProject().getProperty("diamonds.host") + bid + "?fmt=xml: "
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    72
                        + e.getMessage(), Project.MSG_ERR);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    73
                }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    74
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    75
                for (Iterator iterator = antDoc.selectNodes("//test/failed").iterator(); iterator.hasNext();) {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    76
                    testsfound = true;
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    77
                    Element element = (Element) iterator.next();
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    78
                    String failed = element.getText();
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    79
                    if (!failed.equals("0")) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    80
                        log("ATS tests failed", Project.MSG_ERR);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    81
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    82
                        for (Iterator iterator2 = antDoc.selectNodes("//actual_result").iterator(); iterator2.hasNext();) {
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    83
                            Element resultElement = (Element) iterator2.next();
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    84
                            log(resultElement.getText(), Project.MSG_ERR);
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    85
                        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    86
                        return false;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    87
                    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    88
                }
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    89
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    90
                int noofdrops = Integer.parseInt(getProject().getProperty("drop.file.counter"));
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    91
                if (noofdrops > 0) {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    92
                    int testsrun = antDoc.selectNodes("//test").size();
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    93
                    if (testsrun < noofdrops) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    94
                        log(testsrun + " test completed, " + noofdrops + " total");
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    95
                        testsfound = false;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    96
                    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    97
                }
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    98
                if (!testsfound) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    99
                    log("Tests not found sleeping for " + sleeptimesecs + " seconds");
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   100
                    try {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   101
                        Thread.sleep(sleeptimesecs * 1000);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   102
                    }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   103
                    catch (InterruptedException e) {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   104
                        // This will not affect the build process so ignoring.
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   105
                        log("Interrupted while reading ATS build status " + e.getMessage(), Project.MSG_DEBUG);
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   106
                    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   107
                }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   108
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   109
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   110
        return true;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   111
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   112
}