KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > junit > result > AssertResult


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Forums JBoss Portlet *
6  * *
7  * Distributable under LGPL license. *
8  * See terms of license at gnu.org. *
9  * *
10  *****************************************/

11 package org.jboss.portal.junit.result;
12
13 import junit.framework.AssertionFailedError;
14 import org.apache.log4j.Logger;
15 import org.jboss.portal.junit.Result;
16 import org.jboss.portal.common.junit.ExtendedAssert;
17
18 /**
19  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
20  * @version $Revision: 1.2 $
21  */

22 public class AssertResult implements Result
23 {
24
25    private static Logger log = Logger.getLogger(AssertResult.class);
26
27    private int count;
28    private Throwable JavaDoc throwable;
29
30    public AssertResult()
31    {
32       this.count = 0;
33       this.throwable = null;
34    }
35
36    /**
37     * Run the test, record the error or failure if any and increment the count.
38     *
39     * @param test the test to execute
40     * @throws IllegalArgumentException if test is null
41     * @throws IllegalStateException if a throwable has already been recorded previously
42     */

43    public void execute(Test test) throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc
44    {
45       if (test == null)
46       {
47          throw new IllegalArgumentException JavaDoc("Test must not be null");
48       }
49       if (throwable != null)
50       {
51          throw new IllegalStateException JavaDoc("Already recorded a throwable, should not be called");
52       }
53       try
54       {
55          test.run();
56       }
57       catch (AssertionFailedError afe)
58       {
59          log.error("Failure during test", afe);
60          throwable = afe;
61       }
62       catch (Exception JavaDoc e)
63       {
64          log.error("Error during test", e);
65          throwable = e;
66       }
67       finally
68       {
69          count++;
70       }
71    }
72
73    /**
74     * Execute the test only if it the result is not failed.
75     *
76     * @param test the test to execute
77     * @throws IllegalArgumentException if the test is null
78     */

79    public void executeIfNotFailed(Test test) throws IllegalArgumentException JavaDoc
80    {
81       if (!isFailed())
82       {
83          execute(test);
84       }
85    }
86
87    /**
88     * Return how many times the execute method has been successfully invoked.
89     */

90    public int getCount()
91    {
92       return count;
93    }
94
95    /**
96     * Return the recorded throwable or null if nothing has been recorded.
97     */

98    public Throwable JavaDoc getThrowable()
99    {
100       return throwable;
101    }
102
103    /**
104     * Return true if throwable is null.
105     */

106    public boolean isFailed()
107    {
108       return throwable != null;
109    }
110
111    /**
112     * Check that the current count is equals to the expected count and that throwable is null.
113     *
114     * @throws AssertionFailedError if the expected count is not equals to the effective count or the recorded
115     * throwable is not null and is an AssertionFailedError
116     * @throws Throwable if the throwable is not null and is not an instance of AssertionFailedError
117     */

118    public void check(int expectedCount) throws Throwable JavaDoc
119    {
120       if (throwable != null)
121       {
122          throw throwable;
123       }
124       if (count < expectedCount)
125       {
126          throw new AssertionFailedError("Too few test have been executed, " +
127                "expectedCount=" + expectedCount + " and effective count=" + count);
128       }
129       if (count > expectedCount)
130       {
131          throw new AssertionFailedError("Too many test have been executed, " +
132                "expectedCount=" + expectedCount + " and effective count=" + count);
133       }
134    }
135
136    public static abstract class Test extends ExtendedAssert
137    {
138       public abstract void run() throws Exception JavaDoc;
139    }
140 }
141
Popular Tags