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