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 static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.util.EmptyStackException;
25  
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Tests ArrayStack.
30   */
31  @SuppressWarnings("deprecation") // we test a deprecated class
32  public class ArrayStackTest<E> extends AbstractArrayListTest<E> {
33  
34      public ArrayStackTest() {
35          super(ArrayStackTest.class.getSimpleName());
36      }
37  
38      @Override
39      public String getCompatibilityVersion() {
40          return "4";
41      }
42  
43      @Override
44      public ArrayStack<E> makeObject() {
45          return new ArrayStack<>();
46      }
47  
48      @Test
49      public void testNewStack() {
50          final ArrayStack<E> stack = makeObject();
51          assertTrue(stack.empty(), "New stack is empty");
52          assertEquals(0, stack.size(), "New stack has size zero");
53  
54          assertThrows(EmptyStackException.class, () -> stack.peek());
55  
56          assertThrows(EmptyStackException.class, () -> stack.pop());
57      }
58  
59      @Test
60      @SuppressWarnings("unchecked")
61      public void testPushPeekPop() {
62          final ArrayStack<E> stack = makeObject();
63  
64          stack.push((E) "First Item");
65          assertFalse(stack.empty(), "Stack is not empty");
66          assertEquals(1, stack.size(), "Stack size is one");
67          assertEquals("First Item", (String) stack.peek(),
68                  "Top item is 'First Item'");
69          assertEquals(1, stack.size(), "Stack size is one");
70  
71          stack.push((E) "Second Item");
72          assertEquals(2, stack.size(), "Stack size is two");
73          assertEquals("Second Item", (String) stack.peek(),
74                  "Top item is 'Second Item'");
75          assertEquals(2, stack.size(), "Stack size is two");
76  
77          assertEquals("Second Item", (String) stack.pop(),
78                  "Popped item is 'Second Item'");
79          assertEquals("First Item", (String) stack.peek(),
80                  "Top item is 'First Item'");
81          assertEquals(1, stack.size(), "Stack size is one");
82  
83          assertEquals("First Item", (String) stack.pop(),
84                  "Popped item is 'First Item'");
85          assertEquals(0, stack.size(), "Stack size is zero");
86      }
87  
88      @Test
89      @Override
90      @SuppressWarnings("unchecked")
91      public void testSearch() {
92          final ArrayStack<E> stack = makeObject();
93  
94          stack.push((E) "First Item");
95          stack.push((E) "Second Item");
96          assertEquals(1, stack.search("Second Item"),
97                  "Top item is 'Second Item'");
98          assertEquals(2, stack.search("First Item"),
99                  "Next Item is 'First Item'");
100         assertEquals(-1, stack.search("Missing Item"),
101                 "Cannot find 'Missing Item'");
102     }
103 
104 //    public void testCreate() throws Exception {
105 //        resetEmpty();
106 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/ArrayStack.emptyCollection.version4.obj");
107 //        resetFull();
108 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/ArrayStack.fullCollection.version4.obj");
109 //    }
110 
111 }