diff --git a/src/main/java/com/avaje/ebeaninternal/server/util/ArrayStack.java b/src/main/java/com/avaje/ebeaninternal/server/util/ArrayStack.java index d819a9464..951282205 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/util/ArrayStack.java +++ b/src/main/java/com/avaje/ebeaninternal/server/util/ArrayStack.java @@ -10,80 +10,80 @@ import java.util.EmptyStackException; */ public class ArrayStack { - private final ArrayList list; + private final ArrayList list; - /** - * Creates an empty Stack with an initial size. - */ - public ArrayStack(int size) { - this.list = new ArrayList(size); - } + /** + * Creates an empty Stack with an initial size. + */ + public ArrayStack(int size) { + this.list = new ArrayList(size); + } - /** - * Creates an empty Stack. - */ - public ArrayStack() { - this.list = new ArrayList(); - } + /** + * Creates an empty Stack. + */ + public ArrayStack() { + this.list = new ArrayList(); + } - /** - * Pushes an item onto the top of this stack. - */ - public E push(E item) { - list.add(item); - return item; - } + /** + * Pushes an item onto the top of this stack. + */ + public void push(E item) { + list.add(item); + } - /** - * Removes the object at the top of this stack and returns that object as - * the value of this function. - */ - public E pop() { - int len = list.size(); - E obj = peek(); - list.remove(len - 1); - return obj; + /** + * Removes the object at the top of this stack and returns that object as + * the value of this function. + */ + public E pop() { + int len = list.size(); + if (len == 0) { + throw new EmptyStackException(); } + return list.remove(len - 1); + } - protected E peekZero(boolean retNull) { - int len = list.size(); - if (len == 0) { - if (retNull) { - return null; - } - throw new EmptyStackException(); - } - return list.get(len - 1); + protected E peekZero(boolean retNull) { + int len = list.size(); + if (len == 0) { + if (retNull) { + return null; + } + throw new EmptyStackException(); } + return list.get(len - 1); + } - /** - * Returns the object at the top of this stack without removing it. - */ - public E peek() { - return peekZero(false); - } - - /** - * Returns the object at the top of this stack without removing it. - * If the stack is empty this returns null. - */ - public E peekWithNull() { - return peekZero(true); - } - - /** - * Tests if this stack is empty. - */ - public boolean isEmpty() { - return list.isEmpty(); - } + /** + * Returns the object at the top of this stack without removing it. + */ + public E peek() { + return peekZero(false); + } - public int size(){ - return list.size(); - } - - public boolean contains(Object o){ - //noinspection SuspiciousMethodCalls - return list.contains(o); - } + /** + * Returns the object at the top of this stack without removing it. + * If the stack is empty this returns null. + */ + public E peekWithNull() { + return peekZero(true); + } + + /** + * Tests if this stack is empty. + */ + public boolean isEmpty() { + return list.isEmpty(); + } + + public int size() { + return list.size(); + } + + public boolean contains(Object o) { + //noinspection SuspiciousMethodCalls + return list.contains(o); + } } diff --git a/src/test/java/com/avaje/ebeaninternal/server/util/ArrayStackTest.java b/src/test/java/com/avaje/ebeaninternal/server/util/ArrayStackTest.java new file mode 100644 index 000000000..e794b72b2 --- /dev/null +++ b/src/test/java/com/avaje/ebeaninternal/server/util/ArrayStackTest.java @@ -0,0 +1,103 @@ +package com.avaje.ebeaninternal.server.util; + +import org.junit.Test; + +import java.util.EmptyStackException; + +import static org.assertj.core.api.Assertions.assertThat; + + +public class ArrayStackTest { + + @Test + public void testPushPop() throws Exception { + + ArrayStack stack = new ArrayStack(); + stack.push("1"); + stack.push("2"); + stack.push("3"); + + assertThat(stack.pop()).isEqualTo("3"); + assertThat(stack.pop()).isEqualTo("2"); + assertThat(stack.pop()).isEqualTo("1"); + } + + @Test + public void testPushPop_given_stackInitialSizeExceeded() throws Exception { + + ArrayStack stack = new ArrayStack(2); + stack.push("1"); + stack.push("2"); + stack.push("3"); + + assertThat(stack.pop()).isEqualTo("3"); + assertThat(stack.pop()).isEqualTo("2"); + assertThat(stack.pop()).isEqualTo("1"); + } + + @Test(expected = EmptyStackException.class) + public void testPop_given_emptyStack_throws() throws Exception { + + ArrayStack stack = new ArrayStack(); + stack.pop(); + } + + @Test(expected = EmptyStackException.class) + public void testPeek_given_empty_throws() throws Exception { + + ArrayStack stack = new ArrayStack(); + + assertThat(stack.peek()); + } + + @Test + public void testPeek_given_notEmpty() throws Exception { + + ArrayStack stack = new ArrayStack(); + stack.push("1"); + assertThat(stack.peek()).isEqualTo("1"); + } + + @Test + public void testPeekWithNull() throws Exception { + + ArrayStack stack = new ArrayStack(); + + assertThat(stack.peekWithNull()).isNull(); + + stack.push("1"); + assertThat(stack.peekWithNull()).isEqualTo("1"); + } + + @Test + public void testIsEmpty() throws Exception { + + ArrayStack stack = new ArrayStack(); + assertThat(stack.isEmpty()).isTrue(); + + stack.push("1"); + assertThat(stack.isEmpty()).isFalse(); + } + + @Test + public void testSize() throws Exception { + + ArrayStack stack = new ArrayStack(); + assertThat(stack.size()).isEqualTo(0); + + stack.push("1"); + assertThat(stack.size()).isEqualTo(1); + stack.push("w"); + assertThat(stack.size()).isEqualTo(2); + } + + @Test + public void testContains() throws Exception { + + ArrayStack stack = new ArrayStack(); + stack.push("1"); + + assertThat(stack.contains("1")).isTrue(); + assertThat(stack.contains("2")).isFalse(); + } +} \ No newline at end of file