1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 }
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
66
67 while (ss.hasNext()) {
68 Element sender = (Element)ss.next();
69 String code = sender.getAttribute("code");
70
71
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 }
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 }
98
99 } else if ( warnings.size() > 0) {
100 for (int i = 0;i < senders.length; i++) {
101 senders[i].sendWarnings(warnings);
102 }
103 }
104
105 }
106
107 /***
108 * Interface TestListener for JUnit <= 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 }
119
120 }
121
122
123 /***
124 * Interface TestListener for JUnit > 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
141 errors.add("ERROR:["+test + "] " + t);
142 }
143
144 public static void main(String[] args){
145
146 }
147 }