    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.service;
    19
    20  import com.sleepycat.db.DbException;
    21  import junit.framework.TestCase;
    22
    23  import javax.jms.JMSException;
    24
    25  /***
    26   * @version $Revision: 1.6 $
    27   */
    28  public abstract class QueueListTestSupport extends TestCase {
    29      protected QueueList list;
    30
    31      public void testAddFirst() throws Exception {
    32          beginTransaction();
    33
    34          list.clear();
    35          assertTrue(list.isEmpty());
    36
    37          list.addFirst("D");
    38          list.addFirst("C");
    39          list.add(0, "B");
    40          list.add(0, "A");
    41
    42          assertListEquals(list, new Object[]{"A", "B", "C", "D"});
    43
    44          QueueListEntry entry = list.getFirstEntry();
    45          assertEquals("A", entry.getElement());
    46          entry = list.getNextEntry(entry);
    47          assertEquals("B", entry.getElement());
    48          entry = list.getNextEntry(entry);
    49          assertEquals("C", entry.getElement());
    50          entry = list.getNextEntry(entry);
    51          assertEquals("D", entry.getElement());
    52          entry = list.getNextEntry(entry);
    53          assertEquals(null, entry);
    54
    55          entry = list.getLastEntry();
    56          assertEquals("D", entry.getElement());
    57          entry = list.getPrevEntry(entry);
    58          assertEquals("C", entry.getElement());
    59          entry = list.getPrevEntry(entry);
    60          assertEquals("B", entry.getElement());
    61          entry = list.getPrevEntry(entry);
    62          assertEquals("A", entry.getElement());
    63          entry = list.getPrevEntry(entry);
    64          assertEquals(null, entry);
    65
    66          list.set(0, "X");
    67          list.set(1, "Y");
    68          list.set(2, "Z");
    69          list.set(3, "!");
    70          assertListEquals(list, new Object[]{"X", "Y", "Z", "!"});
    71
    72          list.clear();
    73          assertTrue("List should now be empty", list.isEmpty());
    74
    75          commitTransaction();
    76      }
    77
    78
    79      public void testAddAndAddLast() throws Exception {
    80          beginTransaction();
    81
    82          list.clear();
    83          assertTrue(list.isEmpty());
    84
    85          list.add("A");
    86          list.add("B");
    87          list.addLast("C");
    88          list.addLast("D");
    89
    90          assertListEquals(list, new Object[]{"A", "B", "C", "D"});
    91
    92          QueueListEntry entry = list.getFirstEntry();
    93          assertEquals("A", entry.getElement());
    94          entry = list.getNextEntry(entry);
    95          assertEquals("B", entry.getElement());
    96          entry = list.getNextEntry(entry);
    97          assertEquals("C", entry.getElement());
    98          entry = list.getNextEntry(entry);
    99          assertEquals("D", entry.getElement());
    100         entry = list.getNextEntry(entry);
    101         assertEquals(null, entry);
    102
    103         entry = list.getLastEntry();
    104         assertEquals("D", entry.getElement());
    105         entry = list.getPrevEntry(entry);
    106         assertEquals("C", entry.getElement());
    107         entry = list.getPrevEntry(entry);
    108         assertEquals("B", entry.getElement());
    109         entry = list.getPrevEntry(entry);
    110         assertEquals("A", entry.getElement());
    111         entry = list.getPrevEntry(entry);
    112         assertEquals(null, entry);
    113
    114         list.clear();
    115         assertTrue("List should now be empty", list.isEmpty());
    116
    117         commitTransaction();
    118     }
    119
    120     public void testAddAtFrontBackAndMiddle() throws Exception {
    121         beginTransaction();
    122
    123         list.clear();
    124         assertTrue(list.isEmpty());
    125
    126         list.add("A");
    127         list.add("B");
    128         list.addFirst("C");
    129         list.addLast("D");
    130         list.add(1, "E");
    131         list.add(5, "F");
    132
    133         assertListEquals(list, new Object[]{"C", "E", "A", "B", "D", "F"});
    134
    135         assertEquals("C", list.getFirst());
    136         assertEquals("F", list.getLast());
    137
    138         QueueListEntry entry = list.getLastEntry();
    139         assertEquals("F", entry.getElement());
    140         entry = list.getPrevEntry(entry);
    141         assertEquals("D", entry.getElement());
    142         entry = list.getPrevEntry(entry);
    143         assertEquals("B", entry.getElement());
    144         entry = list.getPrevEntry(entry);
    145         assertEquals("A", entry.getElement());
    146         entry = list.getPrevEntry(entry);
    147         assertEquals("E", entry.getElement());
    148         entry = list.getPrevEntry(entry);
    149         assertEquals("C", entry.getElement());
    150         entry = list.getPrevEntry(entry);
    151         assertEquals(null, entry);
    152
    153
    154         entry = list.getFirstEntry();
    155         assertEquals("C", entry.getElement());
    156         entry = list.getNextEntry(entry);
    157         assertEquals("E", entry.getElement());
    158         entry = list.getNextEntry(entry);
    159         assertEquals("A", entry.getElement());
    160         entry = list.getNextEntry(entry);
    161         assertEquals("B", entry.getElement());
    162         entry = list.getNextEntry(entry);
    163         assertEquals("D", entry.getElement());
    164         entry = list.getNextEntry(entry);
    165         assertEquals("F", entry.getElement());
    166         entry = list.getNextEntry(entry);
    167         assertEquals(null, entry);
    168
    169         assertTrue(list.contains("C"));
    170         assertTrue(list.contains("F"));
    171         assertTrue(list.contains("B"));
    172         assertTrue(!list.contains("Z"));
    173
    174         assertTrue(!list.isEmpty());
    175         assertEquals(6, list.size());
    176
    177         list.removeFirst();
    178         assertEquals("E", list.getFirst());
    179         list.removeLast();
    180         assertEquals("D", list.getLast());
    181
    182         list.clear();
    183         assertTrue("List should now be empty", list.isEmpty());
    184
    185         commitTransaction();
    186     }
    187
    188     public void testEmptyList() throws Exception {
    189         beginTransaction();
    190
    191         list.clear();
    192         assertTrue(list.isEmpty());
    193         assertEquals(0, list.size());
    194
    195         assertEquals(null, list.getFirst());
    196         assertEquals(null, list.getLast());
    197         assertEquals(-1, list.indexOf("foo"));
    198         assertEquals(-1, list.lastIndexOf("foo"));
    199
    200         commitTransaction();
    201     }
    202
    203     public void testIndexOf() throws Exception {
    204         beginTransaction();
    205
    206         list.clear();
    207
    208         list.add("A");
    209         list.add("B");
    210         list.add("C");
    211         list.add("B");
    212         list.add("D");
    213
    214         assertListEquals(list, new Object[]{"A", "B", "C", "B", "D"});
    215
    216         assertEquals(0, list.indexOf("A"));
    217         assertEquals(1, list.indexOf("B"));
    218         assertEquals(2, list.indexOf("C"));
    219         assertEquals(4, list.indexOf("D"));
    220         assertEquals(-1, list.indexOf("cheese"));
    221
    222         assertEquals(0, list.lastIndexOf("A"));
    223         assertEquals(3, list.lastIndexOf("B"));
    224         assertEquals(2, list.lastIndexOf("C"));
    225         assertEquals(4, list.lastIndexOf("D"));
    226         assertEquals(-1, list.lastIndexOf("cheese"));
    227
    228         commitTransaction();
    229     }
    230
    231     protected void beginTransaction() throws DbException {
    232     }
    233
    234     protected void commitTransaction() throws JMSException {
    235     }
    236
    237     protected void setUp() throws Exception {
    238         super.setUp();
    239         list = createQueueList();
    240     }
    241
    242     protected abstract QueueList createQueueList() throws JMSException;
    243
    244     protected void assertListEquals(QueueList list, Object[] expected) throws JMSException {
    245         assertArrayEquals(expected, list.toArray());
    246
    247         for (int i = 0; i < expected.length; i++) {
    248             Object expectedValue = expected[i];
    249             Object value = list.get(i);
    250             assertEquals("get(int) at index: " + i, expectedValue, value);
    251         }
    252
    253         QueueListEntry entry = list.getFirstEntry();
    254         for (int i = 0; i < expected.length; i++) {
    255             //System.out.println("Entry: " + entry);
    256             Object expectedValue = expected[i];
    257             assertTrue("Should have an entry for index: " + i, entry != null);
    258             assertEquals("element for entry at index: " + i, expectedValue, entry.getElement());
    259             entry = list.getNextEntry(entry);
    260         }
    261         assertTrue("entry should now be null", entry == null);
    262
    263         entry = list.getLastEntry();
    264         for (int i = expected.length - 1; i >= 0; i--) {
    265             Object expectedValue = expected[i];
    266             assertTrue("Should have an entry for index: " + i, entry != null);
    267             assertEquals("element for entry at index: " + i, expectedValue, entry.getElement());
    268             entry = list.getPrevEntry(entry);
    269         }
    270         assertTrue("entry should now be null", entry == null);
    271
    272         if (expected.length > 0) {
    273             assertEquals("first value", expected[0], list.getFirst());
    274             assertEquals("last value", expected[expected.length - 1], list.getLast());
    275             assertTrue("Should not be empty!", !list.isEmpty());
    276         }
    277         else {
    278             assertTrue("Should be empty!", list.isEmpty());
    279         }
    280     }
    281
    282     protected void assertArrayEquals(Object[] expected, Object[] objects) {
    283         String expectedText = asText(expected);
    284         String valueText = asText(objects);
    285         assertEquals("Sizes wrong. Expected: " + expectedText + " was: " + valueText, expected.length, objects.length);
    286         for (int i = 0; i < expected.length; i++) {
    287             Object expectedValue = expected[i];
    288             Object value = objects[i];
    289             assertEquals("Value at index: " + i + ". Expected: " + expectedText + " was: " + valueText, expectedValue, value);
    290         }
    291     }
    292
    293     protected String asText(Object[] objects) {
    294         StringBuffer buffer = new StringBuffer("[ ");
    295         for (int i = 0; i < objects.length; i++) {
    296             if (i > 0) {
    297                 buffer.append(", ");
    298             }
    299             buffer.append(objects[i]);
    300         }
    301         buffer.append(" ]");
    302         return buffer.toString();
    303     }
    304 }