/******************************************************************************* * Copyright (c) 2015 Oracle. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0, which accompanies this distribution * and is available at http://www.eclipse.org/legal/epl-v10.html. * * Contributors: * Oracle - initial API and implementation ******************************************************************************/ package org.eclipse.jpt.common.utility.tests.internal.stack; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import org.eclipse.jpt.common.utility.internal.ClassTools; import org.eclipse.jpt.common.utility.internal.ObjectTools; import org.eclipse.jpt.common.utility.internal.StringTools; import org.eclipse.jpt.common.utility.internal.queue.ArrayQueue; import org.eclipse.jpt.common.utility.internal.queue.QueueTools; import org.eclipse.jpt.common.utility.internal.stack.ArrayStack; import org.eclipse.jpt.common.utility.internal.stack.LinkedStack; import org.eclipse.jpt.common.utility.internal.stack.StackTools; import org.eclipse.jpt.common.utility.internal.stack.SynchronizedStack; import org.eclipse.jpt.common.utility.stack.Stack; import org.eclipse.jpt.common.utility.transformer.Transformer; import junit.framework.TestCase; @SuppressWarnings("nls") public class StackToolsTests extends TestCase { public StackToolsTests(String name) { super(name); } // ********** push all ********** public void testPushAllIterable() { ArrayList iterable = new ArrayList(); iterable.add("one"); iterable.add("two"); iterable.add("three"); Stack stack = StackTools.arrayStack(); assertTrue(StackTools.pushAll(stack, iterable)); assertEquals("three", stack.pop()); assertEquals("two", stack.pop()); assertEquals("one", stack.pop()); } public void testPushAllIterable_empty() { ArrayList iterable = new ArrayList(); Stack stack = StackTools.arrayStack(); assertFalse(StackTools.pushAll(stack, iterable)); assertTrue(stack.isEmpty()); } public void testPushAllIterator() { ArrayList iterable = new ArrayList(); iterable.add("one"); iterable.add("two"); iterable.add("three"); Stack stack = StackTools.arrayStack(); assertTrue(StackTools.pushAll(stack, iterable.iterator())); assertEquals("three", stack.pop()); assertEquals("two", stack.pop()); assertEquals("one", stack.pop()); } public void testPushAllArray() { ArrayList iterable = new ArrayList(); iterable.add("one"); iterable.add("two"); iterable.add("three"); Stack stack = StackTools.arrayStack(); assertTrue(StackTools.pushAll(stack, iterable.toArray(StringTools.EMPTY_STRING_ARRAY))); assertEquals("three", stack.pop()); assertEquals("two", stack.pop()); assertEquals("one", stack.pop()); } public void testPushAllArray_empty() { ArrayList iterable = new ArrayList(); Stack stack = StackTools.arrayStack(); assertFalse(StackTools.pushAll(stack, iterable.toArray(StringTools.EMPTY_STRING_ARRAY))); assertTrue(stack.isEmpty()); } // ********** pop all ********** public void testPopAll() { ArrayStack stack = StackTools.arrayStack(); stack.push("one"); stack.push("two"); stack.push("three"); ArrayList list = StackTools.popAll(stack); assertEquals("three", list.get(0)); assertEquals("two", list.get(1)); assertEquals("one", list.get(2)); } public void testPopAllToCollection() { Stack stack = StackTools.arrayStack(); stack.push("one"); stack.push("two"); stack.push("three"); ArrayList list = new ArrayList(); assertTrue(StackTools.popAllTo(stack, list)); assertEquals("three", list.get(0)); assertEquals("two", list.get(1)); assertEquals("one", list.get(2)); } public void testPopAllToCollection_empty() { ArrayStack stack = StackTools.arrayStack(); ArrayList list = new ArrayList(); assertFalse(StackTools.popAllTo(stack, list)); assertTrue(list.isEmpty()); } public void testPopAllToListIndex() { ArrayList list = new ArrayList(); list.add("one"); list.add("two"); list.add("three"); Stack stack = StackTools.arrayStack(); stack.push("aaa"); stack.push("bbb"); stack.push("ccc"); assertTrue(StackTools.popAllTo(stack, list, 2)); assertEquals("one", list.get(0)); assertEquals("two", list.get(1)); assertEquals("ccc", list.get(2)); assertEquals("bbb", list.get(3)); assertEquals("aaa", list.get(4)); assertEquals("three", list.get(5)); } public void testPopAllToListIndex_end() { ArrayList list = new ArrayList(); list.add("one"); list.add("two"); list.add("three"); Stack stack = StackTools.arrayStack(); stack.push("aaa"); stack.push("bbb"); stack.push("ccc"); assertTrue(StackTools.popAllTo(stack, list, 3)); assertEquals("one", list.get(0)); assertEquals("two", list.get(1)); assertEquals("three", list.get(2)); assertEquals("ccc", list.get(3)); assertEquals("bbb", list.get(4)); assertEquals("aaa", list.get(5)); } public void testPopAllToListIndex_empty() { ArrayList list = new ArrayList(); list.add("one"); list.add("two"); list.add("three"); Stack stack = StackTools.arrayStack(); assertFalse(StackTools.popAllTo(stack, list, 3)); assertEquals("one", list.get(0)); assertEquals("two", list.get(1)); assertEquals("three", list.get(2)); } public void testPopAllToStack() { ArrayStack stack = StackTools.arrayStack(); stack.push("one"); stack.push("two"); stack.push("three"); ArrayStack stack2 = StackTools.arrayStack(); assertTrue(StackTools.popAllTo(stack, stack2)); assertEquals("one", stack2.pop()); assertEquals("two", stack2.pop()); assertEquals("three", stack2.pop()); } public void testPopAllToStack_empty() { ArrayStack stack = StackTools.arrayStack(); ArrayStack stack2 = StackTools.arrayStack(); assertFalse(StackTools.popAllTo(stack, stack2)); assertTrue(stack2.isEmpty()); } public void testPopAllToQueue() { ArrayStack stack = StackTools.arrayStack(); stack.push("one"); stack.push("two"); stack.push("three"); ArrayQueue queue = QueueTools.arrayQueue(); assertTrue(StackTools.popAllTo(stack, queue)); assertEquals("three", queue.dequeue()); assertEquals("two", queue.dequeue()); assertEquals("one", queue.dequeue()); } public void testPopAllToQueue_empty() { ArrayStack stack = StackTools.arrayStack(); ArrayQueue queue = QueueTools.arrayQueue(); assertFalse(StackTools.popAllTo(stack, queue)); assertTrue(queue.isEmpty()); } public void testPopAllToMapTransformer() { ArrayStack stack = StackTools.arrayStack(); stack.push("zero"); stack.push("one"); stack.push("two"); Mapmap = new HashMap(); assertTrue(StackTools.popAllTo(stack, map, FIRST_LETTER_TRANSFORMER)); assertEquals("one", map.get("o")); assertEquals("two", map.get("t")); assertEquals("zero", map.get("z")); } public void testPopAllToMapTransformer_empty() { ArrayStack stack = StackTools.arrayStack(); Mapmap = new HashMap(); assertFalse(StackTools.popAllTo(stack, map, FIRST_LETTER_TRANSFORMER)); assertTrue(map.isEmpty()); } public void testPopAllToMapTransformerTransformer() { ArrayStack stack = StackTools.arrayStack(); stack.push("zero"); stack.push("one"); stack.push("two"); Mapmap = new HashMap(); assertTrue(StackTools.popAllTo(stack, map, FIRST_LETTER_TRANSFORMER, EMPHASIZER)); assertEquals("*one*", map.get("o")); assertEquals("*two*", map.get("t")); assertEquals("*zero*", map.get("z")); } public void testPopAllToMapTransformerTransformer_empty() { ArrayStack stack = StackTools.arrayStack(); Mapmap = new HashMap(); assertFalse(StackTools.popAllTo(stack, map, FIRST_LETTER_TRANSFORMER, EMPHASIZER)); assertTrue(map.isEmpty()); } public static final Transformer FIRST_LETTER_TRANSFORMER = new FirstLetterTransformer(); /* CU private */ static class FirstLetterTransformer implements Transformer { public String transform(String string) { return string.substring(0, 1); } @Override public String toString() { return this.getClass().getSimpleName(); } } public static final Transformer EMPHASIZER = new StringTools.CharDelimiter('*'); // ********** array stack ********** public void testArrayStack() { ArrayStack d = StackTools.arrayStack(); assertTrue(d.isEmpty()); } public void testArrayStackInt() { ArrayStack d = StackTools.arrayStack(20); assertTrue(d.isEmpty()); assertEquals(20, ((Object[]) ObjectTools.get(d, "elements")).length); } public void testArrayStackIterable() { ArrayList iterable = new ArrayList(); iterable.add("one"); iterable.add("two"); iterable.add("three"); Stack stack = StackTools.arrayStack(iterable); assertEquals("three", stack.pop()); assertEquals("two", stack.pop()); assertEquals("one", stack.pop()); } public void testArrayStackIterableInt() { ArrayList iterable = new ArrayList(); iterable.add("one"); iterable.add("two"); iterable.add("three"); Stack stack = StackTools.arrayStack(iterable, 5); assertEquals("three", stack.pop()); assertEquals("two", stack.pop()); assertEquals("one", stack.pop()); } public void testArrayStackIterator() { ArrayList iterable = new ArrayList(); iterable.add("one"); iterable.add("two"); iterable.add("three"); Stack stack = StackTools.arrayStack(iterable.iterator()); assertEquals("three", stack.pop()); assertEquals("two", stack.pop()); assertEquals("one", stack.pop()); } public void testArrayStackIteratorInt() { ArrayList iterable = new ArrayList(); iterable.add("one"); iterable.add("two"); iterable.add("three"); Stack stack = StackTools.arrayStack(iterable.iterator(), 5); assertEquals("three", stack.pop()); assertEquals("two", stack.pop()); assertEquals("one", stack.pop()); } public void testArrayStackArray() { ArrayList iterable = new ArrayList(); iterable.add("one"); iterable.add("two"); iterable.add("three"); Stack stack = StackTools.arrayStack(iterable.toArray(StringTools.EMPTY_STRING_ARRAY)); assertEquals("three", stack.pop()); assertEquals("two", stack.pop()); assertEquals("one", stack.pop()); } // ********** linked stack ********** public void testLinkedStack() { LinkedStack d = StackTools.linkedStack(); assertTrue(d.isEmpty()); } public void testLinkedStackInt() { LinkedStack d = StackTools.linkedStack(20); assertTrue(d.isEmpty()); } public void testLinkedStackIterable() { ArrayList iterable = new ArrayList(); iterable.add("one"); iterable.add("two"); iterable.add("three"); Stack stack = StackTools.linkedStack(iterable); assertEquals("three", stack.pop()); assertEquals("two", stack.pop()); assertEquals("one", stack.pop()); } public void testLinkedStackIterableInt() { ArrayList iterable = new ArrayList(); iterable.add("one"); iterable.add("two"); iterable.add("three"); Stack stack = StackTools.linkedStack(iterable, 5); assertEquals("three", stack.pop()); assertEquals("two", stack.pop()); assertEquals("one", stack.pop()); } public void testLinkedStackIterator() { ArrayList iterable = new ArrayList(); iterable.add("one"); iterable.add("two"); iterable.add("three"); Stack stack = StackTools.linkedStack(iterable.iterator()); assertEquals("three", stack.pop()); assertEquals("two", stack.pop()); assertEquals("one", stack.pop()); } public void testLinkedStackIteratorInt() { ArrayList iterable = new ArrayList(); iterable.add("one"); iterable.add("two"); iterable.add("three"); Stack stack = StackTools.linkedStack(iterable.iterator(), 5); assertEquals("three", stack.pop()); assertEquals("two", stack.pop()); assertEquals("one", stack.pop()); } public void testLinkedStackArray() { ArrayList iterable = new ArrayList(); iterable.add("one"); iterable.add("two"); iterable.add("three"); Stack stack = StackTools.linkedStack(iterable.toArray(StringTools.EMPTY_STRING_ARRAY)); assertEquals("three", stack.pop()); assertEquals("two", stack.pop()); assertEquals("one", stack.pop()); } public void testLinkedStackArrayInt() { ArrayList iterable = new ArrayList(); iterable.add("one"); iterable.add("two"); iterable.add("three"); Stack stack = StackTools.linkedStack(iterable.toArray(StringTools.EMPTY_STRING_ARRAY), 2); assertEquals("three", stack.pop()); assertEquals("two", stack.pop()); assertEquals("one", stack.pop()); } // ********** fixed-capacity array stack ********** public void testFixedCapacityArrayStackCollection() { ArrayList iterable = new ArrayList(); iterable.add("one"); iterable.add("two"); iterable.add("three"); Stack stack = StackTools.fixedCapacityArrayStack(iterable); assertEquals("three", stack.pop()); assertEquals("two", stack.pop()); assertEquals("one", stack.pop()); } // ********** misc ********** public void testSynchronizedStackObject() { Object lock = new Object(); SynchronizedStack stack = StackTools.synchronizedStack(lock); String first = "first"; String second = "second"; stack.push(first); stack.push(second); assertEquals(second, stack.pop()); assertEquals(first, stack.pop()); assertEquals(lock, stack.getMutex()); } public void testSynchronizedStackStackObject() { Object lock = new Object(); Stack innerStack = StackTools.arrayStack(); String first = "first"; String second = "second"; innerStack.push(first); innerStack.push(second); SynchronizedStack stack = StackTools.synchronizedStack(innerStack, lock); assertEquals(second, stack.pop()); assertEquals(first, stack.pop()); assertEquals(lock, stack.getMutex()); } public void testConstructor() { boolean exCaught = false; try { Object at = ClassTools.newInstance(StackTools.class); fail("bogus: " + at); } catch (RuntimeException ex) { if (ex.getCause() instanceof InvocationTargetException) { if (ex.getCause().getCause() instanceof UnsupportedOperationException) { exCaught = true; } } } assertTrue(exCaught); } }