1 17 package org.apache.excalibur.event.test; 18 19 import junit.framework.TestCase; 20 21 import org.apache.excalibur.event.PreparedEnqueue; 22 import org.apache.excalibur.event.Queue; 23 24 29 public abstract class AbstractQueueTestCase extends TestCase 30 { 31 Object element = new TestQueueElement(); 32 Object [] elements = new TestQueueElement[ 10 ]; 33 34 private static final class TestQueueElement 35 { 36 } 37 38 public AbstractQueueTestCase( String name ) 39 { 40 super( name ); 41 42 for( int i = 0; i < 10; i++ ) 43 { 44 elements[ i ] = new TestQueueElement(); 45 } 46 } 47 48 protected final void performMillionIterationOneElement( Queue queue ) 49 throws Exception 50 { 51 assertEquals( 0, queue.size() ); 52 53 if( queue.maxSize() > 0 ) 54 { 55 for( int j = 0; j < 1000000; j++ ) 56 { 57 queue.enqueue( element ); 58 assertEquals( 1, queue.size() ); 59 60 assertNotNull( queue.dequeue() ); 61 assertEquals( 0, queue.size() ); 62 } 63 } 64 else 65 { 66 for( int i = 0; i < 1000; i++ ) 67 { 68 for( int j = 0; j < 1000; j++ ) 69 { 70 queue.enqueue( element ); 71 assertEquals( "Queue Size: " + queue.size(), j + 1, queue.size() ); 72 } 73 74 for( int j = 0; j < 1000; j++ ) 75 { 76 assertNotNull( "Queue Size: " + queue.size(), queue.dequeue() ); 77 assertEquals( "Queue Size: " + queue.size(), 999 - j, queue.size() ); 78 } 79 } 80 } 81 } 82 83 protected final void performMillionIterationTenElements( Queue queue ) 84 throws Exception 85 { 86 assertEquals( 0, queue.size() ); 87 88 if( queue.maxSize() > 0 ) 89 { 90 for( int j = 0; j < 1000000; j++ ) 91 { 92 queue.enqueue( elements ); 93 assertEquals( 10, queue.size() ); 94 95 Object [] results = queue.dequeueAll(); 96 assertEquals( 10, results.length ); 97 assertEquals( 0, queue.size() ); 98 } 99 } 100 else 101 { 102 for( int i = 0; i < 1000; i++ ) 103 { 104 for( int j = 0; j < 1000; j++ ) 105 { 106 queue.enqueue( elements ); 107 assertEquals( "Queue Size: " + queue.size(), 10 * ( j + 1 ), queue.size() ); 108 } 109 110 Object [] results = queue.dequeueAll(); 111 assertEquals( "Queue Size: " + queue.size(), 10 * 1000, results.length ); 112 assertEquals( "Queue Size: " + queue.size(), 0, queue.size() ); 113 } 114 } 115 } 116 117 protected final void performQueue( Queue queue ) 118 throws Exception 119 { 120 assertEquals( 0, queue.size() ); 121 122 queue.enqueue( new TestQueueElement() ); 123 assertEquals( 1, queue.size() ); 124 125 assertNotNull( queue.dequeue() ); 126 assertEquals( 0, queue.size() ); 127 128 queue.enqueue( elements ); 129 assertEquals( 10, queue.size() ); 130 131 Object [] results = queue.dequeue( 3 ); 132 assertEquals( 3, results.length ); 133 assertEquals( 7, queue.size() ); 134 135 results = queue.dequeueAll(); 136 assertEquals( 7, results.length ); 137 assertEquals( 0, queue.size() ); 138 139 PreparedEnqueue prep = queue.prepareEnqueue( elements ); 140 assertEquals( 10, queue.size() ); 141 prep.abort(); 142 assertEquals( 0, queue.size() ); 143 144 prep = queue.prepareEnqueue( elements ); 145 assertEquals( 10, queue.size() ); 146 prep.commit(); 147 assertEquals( 10, queue.size() ); 148 149 results = queue.dequeue( queue.size() ); 150 assertEquals( 0, queue.size() ); 151 } 152 } 153 | Popular Tags |