View Javadoc

1   /* QueueTestBase
2    *
3    * $Id: QueueTestBase.java 4645 2006-09-22 16:08:03Z paul_jack $
4    *
5    * Created Tue Jan 20 14:17:59 PST 2004
6    *
7    * Copyright (C) 2004 Internet Archive.
8    *
9    * This file is part of the Heritrix web crawler (crawler.archive.org).
10   *
11   * Heritrix is free software; you can redistribute it and/or modify
12   * it under the terms of the GNU Lesser Public License as published by
13   * the Free Software Foundation; either version 2.1 of the License, or
14   * any later version.
15   *
16   * Heritrix is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU Lesser Public License for more details.
20   *
21   * You should have received a copy of the GNU Lesser Public License
22   * along with Heritrix; if not, write to the Free Software
23   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24   */
25  
26  package org.archive.queue;
27  
28  import java.util.NoSuchElementException;
29  
30  import org.archive.util.TmpDirTestCase;
31  
32  /***
33   * JUnit test suite for Queue.  It's an abstract class which is implemented by
34   * each queue implementation
35   *
36   * @author <a href="mailto:me@jamesc.net">James Casey</a>
37   * @version $Id: QueueTestBase.java 4645 2006-09-22 16:08:03Z paul_jack $
38   */
39  public abstract class QueueTestBase extends TmpDirTestCase {
40      /***
41       * Create a new PaddingStringBufferTest object
42       *
43       * @param testName the name of the test
44       */
45      public QueueTestBase(final String testName) {
46          super(testName);
47      }
48  
49      public void setUp() throws Exception {
50          super.setUp();
51          queue = makeQueue();
52      }
53  
54      public void tearDown() {
55          if(queue != null) {
56              queue.release();
57          }
58      }
59  
60      /***
61       * The abstract subclass constructor.  The subclass should create an
62       * instance of the object it wishes to have tested
63       *
64       * @return the Queue object to be tested
65       */
66      protected abstract Queue<Object> makeQueue();
67  
68      /*
69       * test methods
70       */
71  
72      /*** test that queue puts things on, and they stay there :) */
73      public void testQueue() {
74          assertEquals("no items in new queue", 0, queue.length());
75          assertTrue("queue is empty", queue.isEmpty());
76          queue.enqueue("foo");
77          assertEquals("now one item in queue", 1, queue.length());
78          assertFalse("queue not empty", queue.isEmpty());
79      }
80  
81      /*** test that dequeue works */
82      public void testDequeue() {
83          assertEquals("no items in new queue", 0, queue.length());
84          assertTrue("queue is empty", queue.isEmpty());
85          queue.enqueue("foo");
86          queue.enqueue("bar");
87          queue.enqueue("baz");
88          assertEquals("now three items in queue", 3, queue.length());
89          assertEquals("foo dequeued", "foo", queue.dequeue());
90          assertEquals("bar dequeued", "bar", queue.dequeue());
91          assertEquals("baz dequeued", "baz", queue.dequeue());
92  
93          assertEquals("no items in new queue", 0, queue.length());
94          assertTrue("queue is empty", queue.isEmpty());
95  
96      }
97  
98      /*** check what happens we dequeue on empty */
99      public void testDequeueEmptyQueue() {
100         assertTrue("queue is empty", queue.isEmpty());
101 
102         try {
103             queue.dequeue();
104         } catch (NoSuchElementException e) {
105             return;
106         }
107         fail("Expected a NoSuchElementException on dequeue of empty queue");
108     }
109     /*
110      * member variables
111      */
112 
113     /*** the queue object to be tested */
114     protected Queue<Object> queue;
115 }