001: /*
002: * Copyright 2001-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections;
017:
018: import java.util.EmptyStackException;
019: import java.util.List;
020:
021: import junit.framework.Test;
022:
023: /**
024: * Tests ArrayStack.
025: *
026: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
027: *
028: * @author Craig McClanahan
029: */
030: public class TestArrayStack extends TestArrayList {
031:
032: protected ArrayStack stack = null;
033:
034: public TestArrayStack(String testName) {
035: super (testName);
036: }
037:
038: public static Test suite() {
039: return BulkTest.makeSuite(TestArrayStack.class);
040: }
041:
042: public static void main(String args[]) {
043: String[] testCaseName = { TestArrayStack.class.getName() };
044: junit.textui.TestRunner.main(testCaseName);
045: }
046:
047: public List makeEmptyList() {
048: return new ArrayStack();
049: }
050:
051: public void setUp() {
052: stack = (ArrayStack) makeEmptyList();
053: list = stack;
054: }
055:
056: //-----------------------------------------------------------------------
057: public void testNewStack() {
058:
059: assertTrue("New stack is empty", stack.empty());
060: assertEquals("New stack has size zero", stack.size(), 0);
061:
062: try {
063: stack.peek();
064: fail("peek() should have thrown EmptyStackException");
065: } catch (EmptyStackException e) {
066: ; // Expected result
067: }
068:
069: try {
070: stack.pop();
071: fail("pop() should have thrown EmptyStackException");
072: } catch (EmptyStackException e) {
073: ; // Expected result
074: }
075:
076: }
077:
078: public void testPushPeekPop() {
079:
080: stack.push("First Item");
081: assertTrue("Stack is not empty", !stack.empty());
082: assertEquals("Stack size is one", stack.size(), 1);
083: assertEquals("Top item is 'First Item'", (String) stack.peek(),
084: "First Item");
085: assertEquals("Stack size is one", stack.size(), 1);
086:
087: stack.push("Second Item");
088: assertEquals("Stack size is two", stack.size(), 2);
089: assertEquals("Top item is 'Second Item'",
090: (String) stack.peek(), "Second Item");
091: assertEquals("Stack size is two", stack.size(), 2);
092:
093: assertEquals("Popped item is 'Second Item'", (String) stack
094: .pop(), "Second Item");
095: assertEquals("Top item is 'First Item'", (String) stack.peek(),
096: "First Item");
097: assertEquals("Stack size is one", stack.size(), 1);
098:
099: assertEquals("Popped item is 'First Item'", (String) stack
100: .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'", stack
110: .search("Second Item"), 1);
111: assertEquals("Next Item is 'First Item'", stack
112: .search("First Item"), 2);
113: assertEquals("Cannot find 'Missing Item'", stack
114: .search("Missing Item"), -1);
115:
116: }
117:
118: }
|