View Javadoc

1   /*
2    * Copyright (c) 2002 Peter Antman, Teknik i Media  <peter.antman@tim.se>
3    *
4    * $Id: AlertReporter.java,v 1.1.1.1 2004/05/19 12:33:40 pra Exp $
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version
10   * 
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.backsource.alert;
21  import java.util.Iterator;
22  import java.util.Vector;
23  import java.util.Properties;
24  import org.w3c.dom.Element;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  import org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter;
30  import org.apache.tools.ant.BuildException;
31  import junit.framework.AssertionFailedError;
32  import junit.framework.Test;
33  import junit.framework.TestCase;
34  import org.apache.tools.ant.taskdefs.optional.junit.JUnitTest;
35  /***
36   * A JUnit reporter that sends reports to the configured Senders.
37   *
38   * <p>The senders configuration is fetched from the alert xml file.</p>
39   * @author <a href="mailto:pra@tim.se">Peter Antman</a>
40   * @version $Revision: 1.1.1.1 $
41   */
42  
43  public class AlertReporter extends XMLJUnitResultFormatter {
44     static final Log log = LogFactory.getLog( AlertReporter.class);
45     private Sender[] senders = null;
46     Vector warnings = new Vector();
47     Vector errors = new Vector();
48     
49     public AlertReporter () throws Exception{
50        try {
51           setUp();
52        } catch (Exception e) {
53           log.error("Could not set up Senders", e);
54           throw e;
55        } // end of try-catch
56  
57     }
58  
59     /***
60      * Set up the senders by using PropertyUtil.
61      */
62     private void setUp() throws Exception {
63        Vector v = new Vector();
64        Iterator ss = PropertyUtil.getSenders().iterator();
65        // Iterate over the agent, fetching its code, method and agentName.
66        // and all its properties and add it to the suite
67        while (ss.hasNext()) {
68           Element sender = (Element)ss.next();
69           String code = sender.getAttribute("code");
70  
71           // Instanciate it
72           Class sc = Class.forName(code);
73           Sender s = (Sender)sc.newInstance();
74  
75           Properties prop = PropertyUtil.getProperties(sender);
76           s.setProperties(prop);
77           v.add(s);
78        } // end of while ()
79        senders = (Sender[])v.toArray(new Sender[v.size()]);
80     }
81     /***
82      * The whole testsuite started.
83      */
84     public void startTestSuite(JUnitTest suite) throws BuildException {
85        super.startTestSuite(suite);
86     }
87     
88     /***
89      * The whole testsuite ended, send the reports.
90      */
91     public void endTestSuite(JUnitTest suite) throws BuildException {
92        super.endTestSuite(suite);
93  
94        if ( errors.size() > 0) {
95           for (int  i = 0;i  < senders.length; i++) {
96              senders[i].sendErrors(errors,warnings);
97           } // end of for (int  = 0;  < ; ++)
98           
99        } else if ( warnings.size() > 0) {
100          for (int  i = 0;i  < senders.length; i++) {
101             senders[i].sendWarnings(warnings);
102          } // end of for (int  = 0;  < ; ++)
103       } // end of else if ()
104            
105    }
106    
107    /***
108      * Interface TestListener for JUnit &lt;= 3.4.
109      *
110      * <p>A Test failed.
111      */
112    public void addFailure(Test test, Throwable t) {
113        super.addFailure(test,t);
114        if ( t instanceof AlertWarning) {
115           warnings.add("WARNING:["+test + "] " + t);
116        } else {
117           errors.add("ERROR:["+test + "] " + t); 
118        } // end of else
119        
120     }
121    
122    
123    /***
124     * Interface TestListener for JUnit &gt; 3.4.
125     *
126     * <p>A Test failed.
127     */
128    public void addFailure(Test test, AssertionFailedError t) {
129       super.addFailure(test,t);
130       addFailure(test, (Throwable) t);
131    }
132    
133    /***
134     * Interface TestListener.
135     *
136     * <p>An error occured while running the test.
137     */
138    public void addError(Test test, Throwable t) {
139       super.addError(test,t);
140       // we regard these as real Errors
141       errors.add("ERROR:["+test + "] " + t); 
142    }
143    
144    public static void main(String[] args){
145       
146    }
147 } // AlertReporter