public void testBuildElements() {
    Queue<String> queue = this.buildQueue();
    String first = "first";
    String second = "second";
    String third = "third";
    queue.enqueue(first);
    queue.enqueue(second);
    queue.enqueue(third);

    Object[] elements = new Object[] {first, second, third};
    assertTrue(Arrays.equals(elements, ((Object[]) ObjectTools.execute(queue, "buildElements"))));
  }
  public void testNodeToString() {
    Queue<String> queue = QueueTools.linkedQueue();
    String first = "first";
    String second = "second";
    String third = "third";
    queue.enqueue(first);
    queue.enqueue(second);
    queue.enqueue(third);

    Object head = ObjectTools.get(queue, "head");
    assertTrue(head.toString().startsWith("LinkedQueue.Node"));
    assertTrue(head.toString().endsWith("(first)"));
  }
  public void testSize() {
    Queue<String> queue = this.buildQueue();
    String first = "first";
    String second = "second";
    String third = "third";

    assertEquals(0, ((Integer) ObjectTools.execute(queue, "size")).intValue());
    queue.enqueue(first);
    queue.enqueue(second);
    assertEquals(2, ((Integer) ObjectTools.execute(queue, "size")).intValue());
    queue.enqueue(third);
    assertEquals(3, ((Integer) ObjectTools.execute(queue, "size")).intValue());
    queue.dequeue();
    assertEquals(2, ((Integer) ObjectTools.execute(queue, "size")).intValue());
    queue.dequeue();
    queue.dequeue();
    assertEquals(0, ((Integer) ObjectTools.execute(queue, "size")).intValue());
  }
  public void testSerialization_caching() throws Exception {
    Queue<String> original = QueueTools.linkedQueue(20);
    original.enqueue("first");

    Queue<String> clone = TestTools.serialize(original);
    assertEquals(original.peek(), clone.peek());
    assertEquals(original.dequeue(), clone.dequeue());
    assertNotSame(original, clone);
    assertTrue(original.isEmpty());
    assertEquals(original.isEmpty(), clone.isEmpty());

    original.enqueue("second");
    assertFalse(original.isEmpty());
    // clone should still be empty
    assertTrue(clone.isEmpty());

    Object factory = ObjectTools.get(original, "nodeFactory");
    assertTrue(factory.toString().startsWith("LinkedQueue.CachingNodeFactory"));
  }
  public void testNodeCache_unlimited() {
    Queue<String> queue = new LinkedQueue<String>(-1);
    String first = "first";
    String second = "second";
    String third = "third";
    String fourth = "fourth";
    String fifth = "fifth";

    Object factory = ObjectTools.get(queue, "nodeFactory");

    this.verifyNodeCache(0, factory);
    queue.enqueue(first);
    this.verifyNodeCache(0, factory);
    queue.enqueue(second);
    queue.enqueue(third);
    queue.enqueue(fourth);
    queue.enqueue(fifth);
    this.verifyNodeCache(0, factory);
    assertNull(ObjectTools.get(factory, "cacheHead"));

    queue.dequeue();
    this.verifyNodeCache(1, factory);
    queue.dequeue();
    this.verifyNodeCache(2, factory);
    queue.dequeue();
    this.verifyNodeCache(3, factory);
    queue.dequeue();
    this.verifyNodeCache(4, factory);
    queue.dequeue();
    this.verifyNodeCache(5, factory);
    queue.enqueue(first);
    this.verifyNodeCache(4, factory);
    queue.enqueue(second);
    this.verifyNodeCache(3, factory);
    queue.enqueue(third);
    this.verifyNodeCache(2, factory);
    queue.enqueue(fourth);
    this.verifyNodeCache(1, factory);
    queue.enqueue(fifth);
    this.verifyNodeCache(0, factory);
  }