001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 2005 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.junit;
043:
044: import junit.framework.TestCase;
045:
046: /**
047: * Test of class <code>MessageStack</code>.
048: *
049: * @author Marian Petras
050: */
051: public class MessageStackTest extends TestCase {
052:
053: public MessageStackTest(String testName) {
054: super (testName);
055: }
056:
057: private MessageStack stack;
058:
059: private boolean useVolatile = false;
060: private boolean useClear = false;
061:
062: protected void setUp() throws Exception {
063: stack = new MessageStack(2);
064: }
065:
066: public void testSetMessage() {
067: System.out.println("testSetMessage");
068:
069: useVolatile = false;
070: useClear = false;
071: runStackTest();
072: }
073:
074: public void testClearMessage() {
075: System.out.println("testClearMessage");
076:
077: useVolatile = false;
078: useClear = true;
079: runStackTest();
080: }
081:
082: public void testSetVolatileMessage() {
083: System.out.println("testSetVolatileMessage");
084:
085: useVolatile = true;
086: useClear = false;
087: runStackTest();
088: }
089:
090: public void testClearVolatileMessage() {
091: System.out.println("testClearVolatileMessage");
092:
093: useVolatile = true;
094: useClear = true;
095: runStackTest();
096: }
097:
098: private void runStackTest() {
099:
100: String msg0, msg1, msg;
101: String toDisplay;
102:
103: msg0 = "message0";
104: toDisplay = setMessage(0, msg0);
105: assertEquals("setMessage(0, str) on an empty stack", msg0,
106: toDisplay);
107:
108: msg0 = "message00";
109: toDisplay = setMessage(0, msg0);
110: assertEquals("setMessage(0, str) to change item 0", msg0,
111: toDisplay);
112:
113: msg1 = "message1";
114: toDisplay = setMessage(1, msg1);
115: assertEquals(
116: "setMessage(1, str) to add message below message 0",
117: null, toDisplay);
118:
119: toDisplay = setMessage(0, null);
120: assertEquals("setMessage(0, null) to uncover lower message",
121: msg1, toDisplay);
122:
123: toDisplay = setMessage(0, msg0);
124: assertEquals("setMessage(0, str) to cover lower message", msg0,
125: toDisplay);
126:
127: toDisplay = setMessage(0, msg0);
128: assertEquals(
129: "setMessage(0, str) repeated (no change on the stack)",
130: (String) null, toDisplay);
131:
132: msg1 = "message11";
133: toDisplay = setMessage(1, msg1);
134: assertEquals(
135: "setMessage(1, str) to change covered lower message",
136: (String) null, toDisplay);
137:
138: toDisplay = setMessage(1, null);
139: assertEquals(
140: "setMessage(1, null) to clear covered lower message",
141: (String) null, toDisplay);
142:
143: toDisplay = setMessage(0, null);
144: assertEquals(
145: "setMessage(0, null) to clear the only remaining message",
146: "", toDisplay);
147:
148: toDisplay = setMessage(1, null);
149: assertEquals(
150: "setMessage(1, null) to \"clear\" already clean message",
151: (String) null, toDisplay);
152:
153: msg0 = msg1 = "MESSAGE";
154: stack.setMessage(0, null);
155: stack.setMessage(1, null);
156: stack.setMessage(1, msg1);
157: toDisplay = setMessage(0, msg0);
158: assertEquals("setMessage(0, msg) to cover the lower message "
159: + "with the same upper message", (String) null,
160: toDisplay);
161:
162: msg0 = "message0";
163: toDisplay = setMessage(0, msg0);
164: assertEquals("setMessage(0, msg) to change the upper message, "
165: + "leaving the lower message intact", msg0, toDisplay);
166:
167: toDisplay = setMessage(0, msg0);
168: assertEquals(
169: "setMessage(0, msg) again to \"change\" the upper message",
170: (String) null, toDisplay);
171:
172: msg1 = msg0;
173: toDisplay = setMessage(1, msg1);
174: assertEquals("setMessage(1, msg) to change the lower message",
175: (String) null, toDisplay);
176:
177: toDisplay = setMessage(0, null);
178: assertEquals("setMessage(0, null) to uncover the lower message"
179: + "which is the same as was the upper message",
180: (String) null, toDisplay);
181:
182: /* volatile messages: */
183:
184: stack = new MessageStack(2);
185: toDisplay = setMessage(MessageStack.LAYER_VOLATILE,
186: msg = "volatile msg");
187: assertEquals("cover an empty stack with a volatile message",
188: msg, toDisplay);
189: toDisplay = setMessage(MessageStack.LAYER_VOLATILE,
190: msg = "another volatile message");
191: assertEquals("replace the volatile message with another one",
192: msg, toDisplay);
193: toDisplay = setMessage(MessageStack.LAYER_VOLATILE, msg);
194: assertEquals(
195: "\"replace\" the volatile message with the same message",
196: (String) null, toDisplay);
197: toDisplay = setMessage(MessageStack.LAYER_VOLATILE, null);
198: assertEquals("clear the volatile message, "
199: + "thus uncovering an empty stack", "", toDisplay);
200: toDisplay = setMessage(MessageStack.LAYER_VOLATILE, null);
201: assertEquals(
202: "clear the already clean volatile message (stack empty)",
203: (String) null, toDisplay);
204:
205: stack.setMessage(0, msg0 = "message0");
206: stack.setMessage(1, msg1 = "message1");
207:
208: toDisplay = setMessage(MessageStack.LAYER_VOLATILE,
209: msg = "volatile message");
210: assertEquals("cover a non-empty stack with a volatile message",
211: msg, toDisplay);
212:
213: toDisplay = setMessage(MessageStack.LAYER_VOLATILE,
214: msg = "another volatile message");
215: assertEquals("replace the volatile message with another one",
216: msg, toDisplay);
217:
218: toDisplay = setMessage(MessageStack.LAYER_VOLATILE, null);
219: assertEquals("clear the volatile message, "
220: + "thus uncovering the (non-empty) stack", msg0,
221: toDisplay);
222:
223: toDisplay = setMessage(MessageStack.LAYER_VOLATILE, msg0);
224: assertEquals("cover a non-empty stack with a volatile message "
225: + "that is the same as the topmost stack message",
226: (String) null, toDisplay);
227:
228: toDisplay = setMessage(MessageStack.LAYER_VOLATILE, msg0);
229: assertEquals("clear the volatile message that is the same as"
230: + " the topmost stack message", (String) null,
231: toDisplay);
232:
233: stack.setMessage(MessageStack.LAYER_VOLATILE, msg = "volatile");
234: toDisplay = setMessage(0, msg0 = "message0");
235: assertEquals(
236: "setting a stack message when a volatile message is set",
237: msg0, toDisplay);
238:
239: stack.setMessage(MessageStack.LAYER_VOLATILE, msg = "volatile");
240: toDisplay = setMessage(0, msg);
241: assertEquals(
242: "setting a stack message when a volatile message is set"
243: + " if the stack message is the same as was the volatile"
244: + " message", (String) null, toDisplay);
245:
246: stack.setMessage(0, msg0 = "message0");
247: stack.setMessage(1, msg1 = "message1");
248: stack.setMessage(MessageStack.LAYER_VOLATILE, msg = "volatile");
249: toDisplay = setMessage(0, null);
250: assertEquals(
251: "clearing a stack message when a volatile message is set",
252: msg1, toDisplay);
253:
254: msg = "volatile message";
255: stack.setMessage(0, msg0 = "message0");
256: stack.setMessage(1, msg);
257: stack.setMessage(MessageStack.LAYER_VOLATILE, msg);
258: toDisplay = setMessage(0, null);
259: assertEquals(
260: "clearing a stack message when a volatile message is set"
261: + " if the uncovered stack message is the same as was"
262: + " the volatile message", (String) null,
263: toDisplay);
264:
265: stack.setMessage(0, null);
266: stack.setMessage(1, msg1 = "message1");
267: stack.setMessage(MessageStack.LAYER_VOLATILE, msg);
268: toDisplay = setMessage(1, null);
269: assertEquals(
270: "clearing the last stack message when a volatile message"
271: + " is set", "", toDisplay);
272:
273: stack.setMessage(0, null);
274: stack.setMessage(1, null);
275: stack.setMessage(MessageStack.LAYER_VOLATILE, msg);
276: toDisplay = setMessage(1, null);
277: assertEquals("clearing the already clean stack message when"
278: + "a volatile message is set", "", toDisplay);
279:
280: /* expected exceptions: */
281:
282: try {
283: stack.setMessage(2, "msg");
284: fail("setMessage(2, str) should throw an IllegalArgumentException");
285: } catch (IllegalArgumentException ex) {
286: //expected
287: } catch (Exception ex) {
288: fail("setMessage(2, str) thrown an unexpected exception");
289: }
290:
291: try {
292: stack.setMessage(2, null);
293: fail("setMessage(2, null) should throw an IllegalArgumentException");
294: } catch (IllegalArgumentException ex) {
295: //expected
296: } catch (Exception ex) {
297: fail("setMessage(2, null) thrown an unexpected exception");
298: }
299:
300: try {
301: stack.clearMessage(2);
302: fail("clearMessage(2) should throw an IllegalArgumentException");
303: } catch (IllegalArgumentException ex) {
304: //expected
305: } catch (Exception ex) {
306: fail("clearMessage(2) thrown an unexpected exception");
307: }
308: }
309:
310: private String setMessage(final int layer, final String msg) {
311: boolean volat = (useVolatile && (layer == MessageStack.LAYER_VOLATILE));
312: boolean clear = (useClear && (msg == null));
313:
314: final int callType = (volat ? 0x01 : 0x00)
315: | (clear ? 0x10 : 0x00);
316: switch (callType) {
317: case 0x00:
318: return stack.setMessage(layer, msg);
319: case 0x01:
320: return stack.setVolatileMessage(msg);
321: case 0x10:
322: return stack.clearMessage(layer);
323: case 0x11:
324: return stack.clearVolatileMessage();
325: default:
326: assert false;
327: return "ERROR IN TEST CODE";
328: }
329: }
330:
331: }
|