KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > TestArrayStack


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections;
17
18 import java.util.EmptyStackException JavaDoc;
19 import java.util.List JavaDoc;
20
21 import junit.framework.Test;
22
23 /**
24  * Tests ArrayStack.
25  *
26  * @version $Revision: 1.12 $ $Date: 2004/02/18 01:20:35 $
27  *
28  * @author Craig McClanahan
29  */

30 public class TestArrayStack extends TestArrayList {
31     
32     protected ArrayStack stack = null;
33
34     public TestArrayStack(String JavaDoc testName) {
35         super(testName);
36     }
37
38     public static Test suite() {
39         return BulkTest.makeSuite(TestArrayStack.class);
40     }
41
42     public static void main(String JavaDoc args[]) {
43         String JavaDoc[] testCaseName = { TestArrayStack.class.getName() };
44         junit.textui.TestRunner.main(testCaseName);
45     }
46
47     public List JavaDoc makeEmptyList() {
48         return new ArrayStack();
49     }
50
51     public void setUp() {
52         stack = (ArrayStack) makeEmptyList();
53         list = stack;
54     }
55
56     //-----------------------------------------------------------------------
57
public void testNewStack() {
58
59         assertTrue("New stack is empty", stack.empty());
60         assertEquals("New stack has size zero", stack.size(), 0);
61
62         try {
63             stack.peek();
64             fail("peek() should have thrown EmptyStackException");
65         } catch (EmptyStackException JavaDoc e) {
66             ; // Expected result
67
}
68
69         try {
70             stack.pop();
71             fail("pop() should have thrown EmptyStackException");
72         } catch (EmptyStackException JavaDoc e) {
73             ; // Expected result
74
}
75
76     }
77
78     public void testPushPeekPop() {
79
80         stack.push("First Item");
81         assertTrue("Stack is not empty", !stack.empty());
82         assertEquals("Stack size is one", stack.size(), 1);
83         assertEquals("Top item is 'First Item'",
84                      (String JavaDoc) stack.peek(), "First Item");
85         assertEquals("Stack size is one", stack.size(), 1);
86
87         stack.push("Second Item");
88         assertEquals("Stack size is two", stack.size(), 2);
89         assertEquals("Top item is 'Second Item'",
90                      (String JavaDoc) stack.peek(), "Second Item");
91         assertEquals("Stack size is two", stack.size(), 2);
92
93         assertEquals("Popped item is 'Second Item'",
94                      (String JavaDoc) stack.pop(), "Second Item");
95         assertEquals("Top item is 'First Item'",
96                      (String JavaDoc) stack.peek(), "First Item");
97         assertEquals("Stack size is one", stack.size(), 1);
98
99         assertEquals("Popped item is 'First Item'",
100                      (String JavaDoc) stack.pop(), "First Item");
101         assertEquals("Stack size is zero", stack.size(), 0);
102
103     }
104
105     public void testSearch() {
106
107         stack.push("First Item");
108         stack.push("Second Item");
109         assertEquals("Top item is 'Second Item'",
110                      stack.search("Second Item"), 1);
111         assertEquals("Next Item is 'First Item'",
112                      stack.search("First Item"), 2);
113         assertEquals("Cannot find 'Missing Item'",
114                      stack.search("Missing Item"), -1);
115
116     }
117
118 }
119
Popular Tags