KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 /**
25  * The default queue implementation is a variabl size queue.
26  *
27  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
28  */

29 public abstract class AbstractQueueTestCase extends TestCase
30 {
31     Object JavaDoc element = new TestQueueElement();
32     Object JavaDoc[] elements = new TestQueueElement[ 10 ];
33
34     private static final class TestQueueElement
35     {
36     }
37
38     public AbstractQueueTestCase( String JavaDoc 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 JavaDoc
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 JavaDoc
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 JavaDoc[] 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 JavaDoc[] 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 JavaDoc
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 JavaDoc[] 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