KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hansel > probes > BranchProbe


1 package org.hansel.probes;
2
3 import org.hansel.Probe;
4 import org.hansel.ProbeData;
5 import org.hansel.stack.HanselValue;
6
7
8 /**
9  * A probe for a conditional branch. For decision coverage both possible
10  * conditions for the branch have to be encountered.
11  *
12  * @author Niklas Mehner.
13  */

14 public abstract class BranchProbe extends Probe {
15     /** Wether the branch has been executed with the condition beeing true. */
16     private boolean coverTrue;
17
18     /** Wether the branch has been executed with the condition beeing false. */
19     private boolean coverFalse;
20
21     private HanselValue value;
22
23     public BranchProbe(ProbeData pd,
24                        HanselValue value) {
25         super(pd);
26
27         this.value = value;
28         this.coverTrue = false;
29         this.coverFalse = false;
30     }
31
32   
33     /**
34      * Return the filure message for this probe.
35      * @return Failure message.
36      */

37     public String JavaDoc getFailureMessage() {
38         String JavaDoc expression;
39         if (coverTrue) {
40             expression = value.invert().toString();
41         } else {
42             expression = value.toString();
43         }
44
45         String JavaDoc result = "Branch not completely covered. Condition '"
46         + expression
47         + "' is not fulfilled.";
48
49         return result;
50     }
51
52     protected void cover(boolean condition) {
53         if (condition) {
54             coverTrue = true;
55         } else {
56             coverFalse = true;
57         }
58     }
59
60     /**
61      * Return wether this probe failed to be covered.
62      * A probe for a conditional branch fails, if the branch is only taken,
63      * or only omitted. If both cases are encountered, the probe is fully
64      * covered. If the probe is not executed at all, this method still returns
65      * false, because in that case, another probe has to fail (otherwise this
66      * probe had been reached). Because the other failure is more important,
67      * the result of this probe is left out in this case.
68      * @return true If covering this probe failed.
69      */

70     public boolean displayFailure() {
71         return coverTrue ^ coverFalse;
72     }
73
74     public boolean coverageFailure() {
75         return !(coverTrue & coverFalse);
76     }
77
78 }
79
Free Books   Free Magazines  
Popular Tags