001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.util;
019:
020: import java.util.EmptyStackException;
021: import java.util.Stack;
022:
023: public class StackTest extends junit.framework.TestCase {
024:
025: Stack s;
026:
027: /**
028: * @tests java.util.Stack#Stack()
029: */
030: public void test_Constructor() {
031: // Test for method java.util.Stack()
032: assertEquals("Stack creation failed", 0, s.size());
033: }
034:
035: /**
036: * @tests java.util.Stack#empty()
037: */
038: public void test_empty() {
039: // Test for method boolean java.util.Stack.empty()
040: assertTrue("New stack answers non-empty", s.empty());
041: s.push("blah");
042: assertTrue("Stack should not be empty but answers empty", !s
043: .empty());
044: s.pop();
045: assertTrue("Stack should be empty but answers non-empty", s
046: .empty());
047: s.push(null);
048: assertTrue(
049: "Stack with null should not be empty but answers empty",
050: !s.empty());
051: }
052:
053: /**
054: * @tests java.util.Stack#peek()
055: */
056: public void test_peek() {
057: // Test for method java.lang.Object java.util.Stack.peek()
058: String item1 = "Ichi";
059: String item2 = "Ni";
060: String item3 = "San";
061: s.push(item1);
062: assertTrue(
063: "Peek did not return top item when it was the only item",
064: s.peek() == item1);
065: s.push(item2);
066: s.push(item3);
067: assertTrue(
068: "Peek did not return top item amoung many other items",
069: s.peek() == item3);
070: s.pop();
071: assertTrue("Peek did not return top item after a pop",
072: s.pop() == item2);
073: s.push(null);
074: assertNull("Peek did not return top item (wanted: null)", s
075: .peek());
076: }
077:
078: /**
079: * @tests java.util.Stack#pop()
080: */
081: public void test_pop() {
082: // Test for method java.lang.Object java.util.Stack.pop()
083: String item1 = "Ichi";
084: String item2 = "Ni";
085: Object lastPopped;
086: s.push(item1);
087: s.push(item2);
088:
089: try {
090: lastPopped = s.pop();
091: assertTrue("a) Pop did not return top item",
092: lastPopped == item2);
093: } catch (EmptyStackException e) {
094: fail("a) Pop threw EmptyStackException when stack should not have been empty");
095: }
096:
097: try {
098: lastPopped = s.pop();
099: assertTrue("b) Pop did not return top item",
100: lastPopped == item1);
101: } catch (EmptyStackException e) {
102: fail("b) Pop threw EmptyStackException when stack should not have been empty");
103: }
104:
105: s.push(null);
106: try {
107: lastPopped = s.pop();
108: assertNull("c) Pop did not return top item", lastPopped);
109: } catch (EmptyStackException e) {
110: fail("c) Pop threw EmptyStackException when stack should not have been empty");
111: }
112:
113: try {
114: lastPopped = s.pop();
115: fail("d) Pop did not throw EmptyStackException when stack should have been empty");
116: } catch (EmptyStackException e) {
117: return;
118: }
119:
120: }
121:
122: /**
123: * @tests java.util.Stack#push(java.lang.Object)
124: */
125: public void test_pushLjava_lang_Object() {
126: // Test for method java.lang.Object
127: // java.util.Stack.push(java.lang.Object)
128: assertTrue("Used to test", true);
129: }
130:
131: /**
132: * @tests java.util.Stack#search(java.lang.Object)
133: */
134: public void test_searchLjava_lang_Object() {
135: // Test for method int java.util.Stack.search(java.lang.Object)
136: String item1 = "Ichi";
137: String item2 = "Ni";
138: String item3 = "San";
139: s.push(item1);
140: s.push(item2);
141: s.push(item3);
142: assertEquals(
143: "Search returned incorrect value for equivalent object",
144: 3, s.search(item1));
145: assertEquals(
146: "Search returned incorrect value for equal object", 3,
147: s.search("Ichi"));
148: s.pop();
149: assertEquals(
150: "Search returned incorrect value for equivalent object at top of stack",
151: 1, s.search(item2));
152: assertEquals(
153: "Search returned incorrect value for equal object at top of stack",
154: 1, s.search("Ni"));
155: s.push(null);
156: assertEquals(
157: "Search returned incorrect value for search for null at top of stack",
158: 1, s.search(null));
159: s.push("Shi");
160: assertEquals(
161: "Search returned incorrect value for search for null",
162: 2, s.search(null));
163: s.pop();
164: s.pop();
165: assertEquals(
166: "Search returned incorrect value for search for null--wanted -1",
167: -1, s.search(null));
168: }
169:
170: static class BugStack<E> extends Stack<E> {
171: /**
172: *
173: */
174: private static final long serialVersionUID = -9133762075342926141L;
175:
176: /**
177: *
178: */
179: public void setLength(int elementCount) {
180: this .elementCount = elementCount;
181: }
182:
183: public int getLength() {
184: return elementCount;
185: }
186: }
187:
188: //test for wrong exception threw by pop method
189: public void test_pop_modify_elementCount() {
190: BugStack<String> testStack = new BugStack<String>();
191: testStack.push("A");
192: testStack.push("B");
193: testStack.setLength(20);
194: try {
195: testStack.pop();
196: fail("Should throw ArrayIndexOutOfBoundsException here");
197: } catch (ArrayIndexOutOfBoundsException e) {
198: //Expected to throw ArrayIndexOutOfBoundsException here
199: } catch (EmptyStackException e) {
200: fail("Should throw ArrayIndexOutOfBoundsException here");
201: }
202: }
203:
204: /**
205: * Sets up the fixture, for example, open a network connection. This method
206: * is called before a test is executed.
207: */
208: protected void setUp() {
209: s = new Stack();
210: }
211:
212: /**
213: * Tears down the fixture, for example, close a network connection. This
214: * method is called after a test is executed.
215: */
216: protected void tearDown() {
217: }
218: }
|