01: // Copyright 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.ioc.util;
16:
17: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newStack;
18:
19: import org.apache.tapestry.ioc.test.TestBase;
20: import org.testng.annotations.Test;
21:
22: public class StackTest extends TestBase {
23: @Test
24: public void peek_in_empty_stack_is_failure() {
25: Stack<Integer> stack = newStack();
26:
27: try {
28: stack.peek();
29: unreachable();
30: } catch (IllegalStateException ex) {
31: assertEquals(ex.getMessage(), "Stack is empty.");
32: }
33: }
34:
35: @Test
36: public void pop_in_empty_stack_is_failure() {
37: Stack<Integer> stack = newStack();
38:
39: try {
40: stack.pop();
41: unreachable();
42: } catch (IllegalStateException ex) {
43: assertEquals(ex.getMessage(), "Stack is empty.");
44: }
45: }
46:
47: @Test
48: public void basic_operations() {
49: Stack<String> stack = newStack();
50:
51: assertTrue(stack.isEmpty());
52:
53: stack.push("fred");
54: assertEquals(stack.peek(), "fred");
55: assertFalse(stack.isEmpty());
56:
57: stack.push("barney");
58: assertEquals(stack.peek(), "barney");
59:
60: assertEquals(stack.toString(), "Stack[barney, fred]");
61:
62: assertEquals(stack.pop(), "barney");
63: assertEquals(stack.peek(), "fred");
64:
65: assertEquals(stack.pop(), "fred");
66: assertTrue(stack.isEmpty());
67: }
68:
69: @Test
70: public void expansion_of_inner_data() {
71: final int LIMIT = 1000;
72:
73: Stack<Integer> stack = newStack();
74:
75: for (int i = 0; i < LIMIT; i++) {
76: stack.push(i);
77: }
78:
79: for (int i = LIMIT - 1; i >= 0; i--) {
80: assertEquals(stack.pop().intValue(), i);
81: }
82: }
83: }
|