View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4;
18  
19  import java.util.EmptyStackException;
20  
21  import junit.framework.Test;
22  
23  /**
24   * Tests ArrayStack.
25   *
26   */
27  @SuppressWarnings("deprecation") // we test a deprecated class
28  public class ArrayStackTest<E> extends AbstractArrayListTest<E> {
29  
30      public ArrayStackTest(final String testName) {
31          super(testName);
32      }
33  
34      public static Test suite() {
35          return BulkTest.makeSuite(ArrayStackTest.class);
36      }
37  
38      @Override
39      public ArrayStack<E> makeObject() {
40          return new ArrayStack<>();
41      }
42  
43      //-----------------------------------------------------------------------
44      public void testNewStack() {
45          final ArrayStack<E> stack = makeObject();
46          assertTrue("New stack is empty", stack.empty());
47          assertEquals("New stack has size zero", 0, stack.size());
48  
49          try {
50              stack.peek();
51              fail("peek() should have thrown EmptyStackException");
52          } catch (final EmptyStackException e) {
53              // Expected result
54          }
55  
56          try {
57              stack.pop();
58              fail("pop() should have thrown EmptyStackException");
59          } catch (final EmptyStackException e) {
60              // Expected result
61          }
62  
63      }
64  
65      @SuppressWarnings("unchecked")
66      public void testPushPeekPop() {
67          final ArrayStack<E> stack = makeObject();
68  
69          stack.push((E) "First Item");
70          assertTrue("Stack is not empty", !stack.empty());
71          assertEquals("Stack size is one", 1, stack.size());
72          assertEquals("Top item is 'First Item'",
73                       "First Item", (String) stack.peek());
74          assertEquals("Stack size is one", 1, stack.size());
75  
76          stack.push((E) "Second Item");
77          assertEquals("Stack size is two", 2, stack.size());
78          assertEquals("Top item is 'Second Item'",
79                       "Second Item", (String) stack.peek());
80          assertEquals("Stack size is two", 2, stack.size());
81  
82          assertEquals("Popped item is 'Second Item'",
83                       "Second Item", (String) stack.pop());
84          assertEquals("Top item is 'First Item'",
85                       "First Item", (String) stack.peek());
86          assertEquals("Stack size is one", 1, stack.size());
87  
88          assertEquals("Popped item is 'First Item'",
89                       "First Item", (String) stack.pop());
90          assertEquals("Stack size is zero", 0, stack.size());
91  
92      }
93  
94      @Override
95      @SuppressWarnings("unchecked")
96      public void testSearch() {
97          final ArrayStack<E> stack = makeObject();
98  
99          stack.push((E) "First Item");
100         stack.push((E) "Second Item");
101         assertEquals("Top item is 'Second Item'",
102                      1, stack.search("Second Item"));
103         assertEquals("Next Item is 'First Item'",
104                      2, stack.search("First Item"));
105         assertEquals("Cannot find 'Missing Item'",
106                      -1, stack.search("Missing Item"));
107 
108     }
109 
110     @Override
111     public String getCompatibilityVersion() {
112         return "4";
113     }
114 
115 //    public void testCreate() throws Exception {
116 //        resetEmpty();
117 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/ArrayStack.emptyCollection.version4.obj");
118 //        resetFull();
119 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/ArrayStack.fullCollection.version4.obj");
120 //    }
121 
122 }