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 testNodeCache_max() {
    Queue<String> queue = new LinkedQueue<String>(2);
    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(2, factory);
    queue.dequeue();
    this.verifyNodeCache(2, factory);
    queue.dequeue();
    this.verifyNodeCache(2, factory);
    queue.enqueue(first);
    this.verifyNodeCache(1, factory);
    queue.enqueue(second);
    this.verifyNodeCache(0, factory);
    queue.enqueue(third);
    this.verifyNodeCache(0, factory);
  }
  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"));
  }