/******************************************************************************* * Copyright (c) 1998, 2015 Oracle and/or its affiliates. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 * which accompanies this distribution. * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html * and the Eclipse Distribution License is available at * http://www.eclipse.org/org/documents/edl-v10.php. * * Contributors: * Oracle - initial API and implementation from Oracle TopLink ******************************************************************************/ package org.eclipse.persistence.tools.workbench.test.utility; import java.util.EmptyStackException; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import org.eclipse.persistence.tools.workbench.utility.SimpleStack; import org.eclipse.persistence.tools.workbench.utility.Stack; public class SimpleStackTests extends TestCase { public static Test suite() { return new TestSuite(SimpleStackTests.class); } public SimpleStackTests(String name) { super(name); } public void testIsEmpty() { Stack stack = new SimpleStack(); assertTrue(stack.isEmpty()); stack.push("first"); assertFalse(stack.isEmpty()); stack.push("second"); assertFalse(stack.isEmpty()); stack.pop(); assertFalse(stack.isEmpty()); stack.pop(); assertTrue(stack.isEmpty()); } public void testPushAndPop() { Stack stack = new SimpleStack(); String first = "first"; String second = "second"; stack.push(first); stack.push(second); assertEquals(second, stack.pop()); assertEquals(first, stack.pop()); } public void testPushAndPeek() { Stack stack = new SimpleStack(); String first = "first"; String second = "second"; stack.push(first); stack.push(second); assertEquals(second, stack.peek()); assertEquals(second, stack.peek()); assertEquals(second, stack.pop()); assertEquals(first, stack.peek()); assertEquals(first, stack.peek()); assertEquals(first, stack.pop()); } public void testEmptyStackExceptionPeek() { Stack stack = new SimpleStack(); String first = "first"; String second = "second"; stack.push(first); stack.push(second); assertEquals(second, stack.peek()); assertEquals(second, stack.pop()); assertEquals(first, stack.peek()); assertEquals(first, stack.pop()); boolean exCaught = false; try { stack.peek(); } catch (EmptyStackException ex) { exCaught = true; } assertTrue(exCaught); } public void testEmptyStackExceptionPop() { Stack stack = new SimpleStack(); String first = "first"; String second = "second"; stack.push(first); stack.push(second); assertEquals(second, stack.peek()); assertEquals(second, stack.pop()); assertEquals(first, stack.peek()); assertEquals(first, stack.pop()); boolean exCaught = false; try { stack.pop(); } catch (EmptyStackException ex) { exCaught = true; } assertTrue(exCaught); } public void testClone() { SimpleStack stack = new SimpleStack(); stack.push("first"); stack.push("second"); stack.push("third"); this.verifyClone(stack, (SimpleStack) stack.clone()); } public void testSerialization() throws Exception { SimpleStack stack = new SimpleStack(); stack.push("first"); stack.push("second"); stack.push("third"); this.verifyClone(stack, (SimpleStack) TestTools.serialize(stack)); } private void verifyClone(Stack original, Stack clone) { assertNotSame(original, clone); assertEquals(original.peek(), clone.peek()); assertEquals(original.pop(), clone.pop()); assertEquals(original.peek(), clone.peek()); assertEquals(original.pop(), clone.pop()); assertEquals(original.isEmpty(), clone.isEmpty()); assertEquals(original.peek(), clone.peek()); assertEquals(original.pop(), clone.pop()); assertTrue(original.isEmpty()); assertEquals(original.isEmpty(), clone.isEmpty()); original.push("fourth"); assertFalse(original.isEmpty()); // clone should still be empty assertTrue(clone.isEmpty()); } }