KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > event > test > AbstractQueueTestCase


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.event.test;
9
10 import junit.framework.TestCase;
11 import org.apache.avalon.excalibur.event.Queue;
12 import org.apache.avalon.excalibur.event.PreparedEnqueue;
13 import org.apache.avalon.excalibur.event.QueueElement;
14 import org.apache.avalon.excalibur.event.SourceException;
15
16 /**
17  * The default queue implementation is a variabl size queue.
18  *
19  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
20  */

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