KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > junit > MessageStackTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 2005 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.junit;
21
22 import junit.framework.TestCase;
23
24 /**
25  * Test of class <code>MessageStack</code>.
26  *
27  * @author Marian Petras
28  */

29 public class MessageStackTest extends TestCase {
30
31     public MessageStackTest(String JavaDoc testName) {
32         super(testName);
33     }
34
35     private MessageStack stack;
36
37     private boolean useVolatile = false;
38     private boolean useClear = false;
39
40     protected void setUp() throws Exception JavaDoc {
41         stack = new MessageStack(2);
42     }
43
44     public void testSetMessage() {
45         System.out.println("testSetMessage");
46         
47         useVolatile = false;
48         useClear = false;
49         runStackTest();
50     }
51     
52     public void testClearMessage() {
53         System.out.println("testClearMessage");
54         
55         useVolatile = false;
56         useClear = true;
57         runStackTest();
58     }
59
60     public void testSetVolatileMessage() {
61         System.out.println("testSetVolatileMessage");
62         
63         useVolatile = true;
64         useClear = false;
65         runStackTest();
66     }
67
68     public void testClearVolatileMessage() {
69         System.out.println("testClearVolatileMessage");
70         
71         useVolatile = true;
72         useClear = true;
73         runStackTest();
74     }
75     
76     private void runStackTest() {
77
78         String JavaDoc msg0, msg1, msg;
79         String JavaDoc toDisplay;
80         
81         msg0 = "message0";
82         toDisplay = setMessage(0, msg0);
83         assertEquals("setMessage(0, str) on an empty stack",
84                      msg0, toDisplay);
85         
86         msg0 = "message00";
87         toDisplay = setMessage(0, msg0);
88         assertEquals("setMessage(0, str) to change item 0",
89                      msg0, toDisplay);
90         
91         msg1 = "message1";
92         toDisplay = setMessage(1, msg1);
93         assertEquals("setMessage(1, str) to add message below message 0",
94                      null, toDisplay);
95         
96         toDisplay = setMessage(0, null);
97         assertEquals("setMessage(0, null) to uncover lower message",
98                      msg1, toDisplay);
99         
100         toDisplay = setMessage(0, msg0);
101         assertEquals("setMessage(0, str) to cover lower message",
102                      msg0, toDisplay);
103         
104         toDisplay = setMessage(0, msg0);
105         assertEquals("setMessage(0, str) repeated (no change on the stack)",
106                      (String JavaDoc) null, toDisplay);
107         
108         msg1 = "message11";
109         toDisplay = setMessage(1, msg1);
110         assertEquals("setMessage(1, str) to change covered lower message",
111                      (String JavaDoc) null, toDisplay);
112         
113         toDisplay = setMessage(1, null);
114         assertEquals("setMessage(1, null) to clear covered lower message",
115                      (String JavaDoc) null, toDisplay);
116         
117         toDisplay = setMessage(0, null);
118         assertEquals("setMessage(0, null) to clear the only remaining message",
119                      "", toDisplay);
120         
121         toDisplay = setMessage(1, null);
122         assertEquals("setMessage(1, null) to \"clear\" already clean message",
123                      (String JavaDoc) null, toDisplay);
124         
125         msg0 = msg1 = "MESSAGE";
126         stack.setMessage(0, null);
127         stack.setMessage(1, null);
128         stack.setMessage(1, msg1);
129         toDisplay = setMessage(0, msg0);
130         assertEquals("setMessage(0, msg) to cover the lower message "
131                      + "with the same upper message",
132                      (String JavaDoc) null, toDisplay);
133         
134         msg0 = "message0";
135         toDisplay = setMessage(0, msg0);
136         assertEquals("setMessage(0, msg) to change the upper message, "
137                      + "leaving the lower message intact",
138                      msg0, toDisplay);
139         
140         toDisplay = setMessage(0, msg0);
141         assertEquals("setMessage(0, msg) again to \"change\" the upper message",
142                      (String JavaDoc) null, toDisplay);
143         
144         msg1 = msg0;
145         toDisplay = setMessage(1, msg1);
146         assertEquals("setMessage(1, msg) to change the lower message",
147                      (String JavaDoc) null, toDisplay);
148         
149         toDisplay = setMessage(0, null);
150         assertEquals("setMessage(0, null) to uncover the lower message"
151                      + "which is the same as was the upper message",
152                      (String JavaDoc) null, toDisplay);
153         
154         /* volatile messages: */
155         
156         stack = new MessageStack(2);
157         toDisplay = setMessage(MessageStack.LAYER_VOLATILE,
158                                      msg = "volatile msg");
159         assertEquals("cover an empty stack with a volatile message",
160                      msg, toDisplay);
161         toDisplay = setMessage(MessageStack.LAYER_VOLATILE,
162                                      msg = "another volatile message");
163         assertEquals("replace the volatile message with another one",
164                      msg, toDisplay);
165         toDisplay = setMessage(MessageStack.LAYER_VOLATILE,
166                                      msg);
167         assertEquals("\"replace\" the volatile message with the same message",
168                      (String JavaDoc) null, toDisplay);
169         toDisplay = setMessage(MessageStack.LAYER_VOLATILE,
170                                      null);
171         assertEquals("clear the volatile message, "
172                      + "thus uncovering an empty stack",
173                      "", toDisplay);
174         toDisplay = setMessage(MessageStack.LAYER_VOLATILE,
175                                      null);
176         assertEquals("clear the already clean volatile message (stack empty)",
177                      (String JavaDoc) null, toDisplay);
178         
179         stack.setMessage(0, msg0 = "message0");
180         stack.setMessage(1, msg1 = "message1");
181         
182         toDisplay = setMessage(MessageStack.LAYER_VOLATILE,
183                                      msg = "volatile message");
184         assertEquals("cover a non-empty stack with a volatile message",
185                      msg, toDisplay);
186         
187         toDisplay = setMessage(MessageStack.LAYER_VOLATILE,
188                                      msg = "another volatile message");
189         assertEquals("replace the volatile message with another one",
190                      msg, toDisplay);
191         
192         toDisplay = setMessage(MessageStack.LAYER_VOLATILE,
193                                      null);
194         assertEquals("clear the volatile message, "
195                      + "thus uncovering the (non-empty) stack",
196                      msg0, toDisplay);
197         
198         toDisplay = setMessage(MessageStack.LAYER_VOLATILE,
199                                      msg0);
200         assertEquals("cover a non-empty stack with a volatile message "
201                      + "that is the same as the topmost stack message",
202                      (String JavaDoc) null, toDisplay);
203         
204         toDisplay = setMessage(MessageStack.LAYER_VOLATILE,
205                                      msg0);
206         assertEquals("clear the volatile message that is the same as"
207                      + " the topmost stack message",
208                      (String JavaDoc) null, toDisplay);
209         
210         stack.setMessage(MessageStack.LAYER_VOLATILE, msg = "volatile");
211         toDisplay = setMessage(0, msg0 = "message0");
212         assertEquals("setting a stack message when a volatile message is set",
213                      msg0, toDisplay);
214         
215         stack.setMessage(MessageStack.LAYER_VOLATILE, msg = "volatile");
216         toDisplay = setMessage(0, msg);
217         assertEquals("setting a stack message when a volatile message is set"
218                      + " if the stack message is the same as was the volatile"
219                      + " message",
220                      (String JavaDoc) null, toDisplay);
221         
222         stack.setMessage(0, msg0 = "message0");
223         stack.setMessage(1, msg1 = "message1");
224         stack.setMessage(MessageStack.LAYER_VOLATILE, msg = "volatile");
225         toDisplay = setMessage(0, null);
226         assertEquals("clearing a stack message when a volatile message is set",
227                      msg1, toDisplay);
228         
229         msg = "volatile message";
230         stack.setMessage(0, msg0 = "message0");
231         stack.setMessage(1, msg);
232         stack.setMessage(MessageStack.LAYER_VOLATILE, msg);
233         toDisplay = setMessage(0, null);
234         assertEquals("clearing a stack message when a volatile message is set"
235                      + " if the uncovered stack message is the same as was"
236                      + " the volatile message",
237                      (String JavaDoc) null, toDisplay);
238         
239         stack.setMessage(0, null);
240         stack.setMessage(1, msg1 = "message1");
241         stack.setMessage(MessageStack.LAYER_VOLATILE, msg);
242         toDisplay = setMessage(1, null);
243         assertEquals("clearing the last stack message when a volatile message"
244                      + " is set",
245                      "", toDisplay);
246         
247         stack.setMessage(0, null);
248         stack.setMessage(1, null);
249         stack.setMessage(MessageStack.LAYER_VOLATILE, msg);
250         toDisplay = setMessage(1, null);
251         assertEquals("clearing the already clean stack message when"
252                      + "a volatile message is set",
253                      "", toDisplay);
254         
255         /* expected exceptions: */
256         
257         try {
258             stack.setMessage(2, "msg");
259             fail("setMessage(2, str) should throw an IllegalArgumentException");
260         } catch (IllegalArgumentException JavaDoc ex) {
261             //expected
262
} catch (Exception JavaDoc ex) {
263             fail("setMessage(2, str) thrown an unexpected exception");
264         }
265         
266         try {
267             stack.setMessage(2, null);
268            fail("setMessage(2, null) should throw an IllegalArgumentException");
269         } catch (IllegalArgumentException JavaDoc ex) {
270             //expected
271
} catch (Exception JavaDoc ex) {
272             fail("setMessage(2, null) thrown an unexpected exception");
273         }
274         
275         try {
276             stack.clearMessage(2);
277             fail("clearMessage(2) should throw an IllegalArgumentException");
278         } catch (IllegalArgumentException JavaDoc ex) {
279             //expected
280
} catch (Exception JavaDoc ex) {
281             fail("clearMessage(2) thrown an unexpected exception");
282         }
283     }
284
285     private String JavaDoc setMessage(final int layer, final String JavaDoc msg) {
286         boolean volat = (useVolatile && (layer == MessageStack.LAYER_VOLATILE));
287         boolean clear = (useClear && (msg == null));
288         
289         final int callType = (volat ? 0x01 : 0x00) | (clear ? 0x10 : 0x00);
290         switch (callType) {
291             case 0x00:
292                 return stack.setMessage(layer, msg);
293             case 0x01:
294                 return stack.setVolatileMessage(msg);
295             case 0x10:
296                 return stack.clearMessage(layer);
297             case 0x11:
298                 return stack.clearVolatileMessage();
299             default:
300                 assert false;
301                 return "ERROR IN TEST CODE";
302         }
303     }
304     
305 }
306
Popular Tags