1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.jameleon.util;
20
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.Set;
24
25 /***
26 * A singleton that keeps track of the assert levels accross all test cases. This is implemented by calling an "assert" method
27 * that takes an <code>assertLevel</code> parameter in it. If an "assert" method without that parameter is sent, this singleton will
28 * no be used to check whether the assert can happen or not. The order of test levels follows:
29 * <ol>
30 * <li>SMOKE
31 * <li>LEVEL1
32 * <li>LEVEL2
33 * <li>FUNCTION
34 * <li>LEVEL4
35 * <li>LEVEL5
36 * <li>REGRESSION
37 * <li>LEVEL7
38 * <li>LEVEL8
39 * <li>ACCEPTANCE
40 * </ol>
41 * The <code>LEVEL</code> are used when there is a test level that is somewhere between two of the test levels with meaning. It is suggested
42 * that SMOKE, FUNCTION, REGRESSION, and ACCEPTANCE are exclusively used until a need is found for more levels.
43 */
44 public class AssertLevel {
45
46 /***
47 * A Smoke level test. A Smoke test is usually run before QA even begins to start their testing.
48 * This tells the tester that application is in some kind of functional state. In other words, these are tests that MUST pass in order for QA to
49 * even bother further test.
50 */
51 public final static int SMOKE = 0;
52 /***
53 * A test level between a Smoke and Function. Used just in case more levels are needed.
54 */
55 public final static int LEVEL1 = 1;
56 /***
57 * A test level between a Smoke and Function. Used just in case more levels are needed.
58 */
59 public final static int LEVEL2 = 2;
60 /***
61 * A Function level test. A Function test is usually run by QA to see if the application is mostly working correctly.
62 * Usually these kinds of tests test that a feature works as documented.
63 */
64 public final static int FUNCTION = 3;
65 /***
66 * A test level between a Function and Regression. Used just in case more levels are needed.
67 */
68 public final static int LEVEL4 = 4;
69 /***
70 * A test level between a Function and Regression. Used just in case more levels are needed.
71 */
72 public final static int LEVEL5 = 5;
73 /***
74 * A Regression level test. When a bug/feature is initially agreed upon, QA writes up tests to expose these.
75 * These are then qualify as regression tests as they are used to prove that the software isn't regressing.
76 */
77 public final static int REGRESSION = 6;
78 /***
79 * A test level between a Regression and Acceptance. Used just in case more levels are needed.
80 */
81 public final static int LEVEL7 = 7;
82 /***
83 * A test level between a Regression and Acceptance. Used just in case more levels are needed.
84 */
85 public final static int LEVEL8 = 8;
86 /***
87 * An Acceptance level test. Acceptance tests are usually very picky. They are the ultimate in proving that the application
88 * is ready for the best customer experience.
89 */
90 public final static int ACCEPTANCE = 9;
91 /***
92 * An invalid level. Used for validation.
93 */
94 public final static int INVALID_LEVEL = 100;
95 /***
96 * The default setting of every level
97 */
98 public final static int NO_LEVEL = -1;
99
100 protected int greaterThanLevel, lessThanLevel, level;
101 protected Set levels = null;
102
103 private static AssertLevel instance = null;
104
105 /***
106 * Since this is a singleton, a private constructor is in order.
107 * Initializes all values to their defaults.
108 */
109 private AssertLevel(){
110 clearAll();
111 }
112
113 /***
114 * @return the instance available to everyone of AssertLevel
115 */
116 public static AssertLevel getInstance(){
117 if (instance == null) {
118 instance = new AssertLevel();
119 }
120 return instance;
121 }
122 /***
123 * Clears any assert levels that may have been set.
124 */
125 public void clearAll() {
126 levels = new HashSet();
127 greaterThanLevel = NO_LEVEL;
128 lessThanLevel = NO_LEVEL;
129 level = NO_LEVEL;
130 }
131 /***
132 * Tests whether an assert should be performed at a certain level. The greaterThanlevel and lessThanLevel are
133 * actually greaterThanOrEqualTo and lessThanOrEqualTo. All possible combinations may be set and all will be checked.
134 * @param assertLevel - The level the assert is going to be executed against.
135 * @return true if the assert should be performed.
136 */
137 public boolean assertPerformable(int assertLevel){
138 boolean perform = false;
139 if ( levels.size() > 0 ) {
140 Iterator it = levels.iterator();
141 Integer i = null;
142 while ( it.hasNext() && !perform ) {
143 i = (Integer)it.next();
144 perform = ( i.intValue() == assertLevel );
145 }
146 }
147 if ( level != NO_LEVEL && assertLevel == level ) {
148 perform = true;
149 }
150 if ( greaterThanLevel != NO_LEVEL && assertLevel >= greaterThanLevel ) {
151 perform = true;
152 }
153 if ( lessThanLevel != NO_LEVEL && assertLevel <= lessThanLevel ) {
154 perform = true;
155 }
156 if ( lessThanLevel == NO_LEVEL &&
157 greaterThanLevel == NO_LEVEL &&
158 level == NO_LEVEL &&
159 levels.size() == 0){
160 perform = true;
161 }
162 if (assertLevel == NO_LEVEL) {
163 perform = true;
164 }
165 return perform;
166 }
167
168 /***
169 * @return The level at which asserts with set to or greater than the value this is set may be executed.
170 */
171 public int getGreaterThanLevel(){
172 return greaterThanLevel;
173 }
174
175 /***
176 * @return The level at which asserts with levels set to or less than the value this is set may be executed.
177 */
178 public int getLessThanLevel(){
179 return lessThanLevel;
180 }
181
182 /***
183 * @return The level at which asserts with levels set to this may be executed.
184 */
185 public int getLevel(){
186 return level;
187 }
188
189 /***
190 * @return The levels at which asserts with levels set to this may be executed.
191 */
192 public Set getLevels(){
193 return levels;
194 }
195
196 /***
197 * Sets the level at which asserts with set to or greater than the value this is set may be executed.
198 * @param greaterThanLevel - The level at which asserts with set to or greater than the value this is set may be executed.
199 */
200 public void setGreaterThanLevel(int greaterThanLevel){
201 this.greaterThanLevel = greaterThanLevel;
202 }
203
204 /***
205 * Sets the level at which asserts with levels set to or less than the value this is set may be executed.
206 * @param lessThanLevel - The level at which asserts with levels set to or less than the value this is set may be executed.
207 */
208 public void setLessThanLevel(int lessThanLevel){
209 this.lessThanLevel = lessThanLevel;
210 }
211
212 /***
213 * Sets the level at which asserts with levels set to this may be executed.
214 * @param level - The level at which asserts with levels set to this may be executed.
215 */
216 public void setLevel(int level){
217 this.level = level;
218 }
219
220 /***
221 * Adds at level at which asserts with levels set to this may be executed to the list of possible levels.
222 * @param level - The level at which asserts with levels set to this may be executed.
223 */
224 public void addLevel(int level){
225 levels.add(new Integer(level));
226 }
227
228 /***
229 * Removes at level at which asserts with levels set to this may be executed to the list of possible levels.
230 * @param level - The level at which asserts with levels set to this may be executed.
231 */
232 public void removeLevel(int level){
233 levels.remove(new Integer(level));
234 }
235 }