KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > command > DefaultProcessorTest


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.core.command;
17
18
19 import junit.framework.TestCase;
20
21 import org.columba.api.command.IWorkerStatusController;
22
23 /**
24  * @author Timo Stich (tstich@users.sourceforge.net)
25  *
26  */

27 public class DefaultProcessorTest extends TestCase {
28     private CommandProcessor processor;
29
30     int executedID;
31
32     int finishedID;
33
34     /**
35      * Constructor for DefaultProcessorTest.
36      *
37      * @param arg0
38      */

39     public DefaultProcessorTest(String JavaDoc arg0) {
40         super(arg0);
41     }
42
43     public void testAddOp_PriorityOrdering() {
44         processor = new CommandProcessor(false);
45
46         TestCommand command1 = new TestCommand(this, 1);
47         command1.setPriority(Command.NORMAL_PRIORITY);
48         processor.addOp(command1);
49
50         TestCommand command2 = new TestCommand(this, 2);
51         command2.setPriority(Command.NORMAL_PRIORITY);
52         processor.addOp(command2);
53
54         TestCommand command3 = new TestCommand(this, 3);
55         command3.setPriority(Command.REALTIME_PRIORITY);
56         processor.addOp(command3);
57
58         TestCommand command4 = new TestCommand(this, 4);
59         command4.setPriority(Command.DAEMON_PRIORITY);
60         processor.addOp(command4);
61
62         TestCommand command5 = new TestCommand(this, 5);
63         command5.setPriority(Command.NORMAL_PRIORITY);
64         processor.addOp(command5);
65
66         assertEquals(
67                 3,
68                 ((TestCommand) ((OperationItem) processor.operationQueue.get(0))
69                         .getOperation()).getId());
70         assertEquals(
71                 1,
72                 ((TestCommand) ((OperationItem) processor.operationQueue.get(1))
73                         .getOperation()).getId());
74         assertEquals(
75                 2,
76                 ((TestCommand) ((OperationItem) processor.operationQueue.get(2))
77                         .getOperation()).getId());
78         assertEquals(
79                 5,
80                 ((TestCommand) ((OperationItem) processor.operationQueue.get(3))
81                         .getOperation()).getId());
82         assertEquals(
83                 4,
84                 ((TestCommand) ((OperationItem) processor.operationQueue.get(4))
85                         .getOperation()).getId());
86     }
87
88     public void testAddOp_PriorityOrderingWithSynchronized() {
89         processor = new CommandProcessor(false);
90
91         TestCommand command1 = new TestCommand(this, 1);
92         command1.setPriority(Command.NORMAL_PRIORITY);
93         processor.addOp(command1, Command.FIRST_EXECUTION);
94
95         TestCommand command2 = new TestCommand(this, 2);
96         command2.setPriority(Command.NORMAL_PRIORITY);
97         command2.setSynchronize(true);
98         processor.addOp(command2, Command.FIRST_EXECUTION);
99
100         TestCommand command3 = new TestCommand(this, 3);
101         command3.setPriority(Command.REALTIME_PRIORITY);
102         processor.addOp(command3, Command.FIRST_EXECUTION);
103
104         TestCommand command4 = new TestCommand(this, 4);
105         command4.setPriority(Command.DAEMON_PRIORITY);
106         command4.setSynchronize(true);
107         processor.addOp(command4, Command.FIRST_EXECUTION);
108
109         TestCommand command5 = new TestCommand(this, 5);
110         command5.setPriority(Command.NORMAL_PRIORITY);
111         processor.addOp(command5, Command.FIRST_EXECUTION);
112
113         assertEquals(
114                 1,
115                 ((TestCommand) ((OperationItem) processor.operationQueue.get(0))
116                         .getOperation()).getId());
117         assertEquals(
118                 2,
119                 ((TestCommand) ((OperationItem) processor.operationQueue.get(1))
120                         .getOperation()).getId());
121         assertEquals(
122                 3,
123                 ((TestCommand) ((OperationItem) processor.operationQueue.get(2))
124                         .getOperation()).getId());
125         assertEquals(
126                 4,
127                 ((TestCommand) ((OperationItem) processor.operationQueue.get(3))
128                         .getOperation()).getId());
129         assertEquals(
130                 5,
131                 ((TestCommand) ((OperationItem) processor.operationQueue.get(4))
132                         .getOperation()).getId());
133     }
134
135     public void testReserveForRealtime() throws Exception JavaDoc {
136         processor = new CommandProcessor(false);
137
138         // empty the worker list until one is left
139
while (processor.getWorker(Command.NORMAL_PRIORITY) != null)
140             ;
141
142         assertEquals(1, processor.worker.size());
143
144         assertTrue(processor.getWorker(Command.REALTIME_PRIORITY) != null);
145     }
146
147     public void testRunOne() throws Exception JavaDoc {
148         processor = new CommandProcessor(false);
149
150         TestCommand command1 = new TestCommand(this, 1);
151         command1.setPriority(Command.NORMAL_PRIORITY);
152         processor.addOp(command1, Command.FIRST_EXECUTION);
153
154         TestCommand command2 = new TestCommand(this, 2);
155         command2.setPriority(Command.NORMAL_PRIORITY);
156         command2.setSynchronize(true);
157         processor.addOp(command2, Command.FIRST_EXECUTION);
158
159         processor.startOperation();
160         assertEquals(CommandProcessor.MAX_WORKERS - 1, processor.worker.size());
161
162         Thread.sleep(1000);
163
164         assertEquals(1, executedID);
165         assertEquals(CommandProcessor.MAX_WORKERS, processor.worker.size());
166
167         processor.startOperation();
168         assertEquals(CommandProcessor.MAX_WORKERS - 1, processor.worker.size());
169
170         Thread.sleep(1000);
171
172         assertEquals(2, executedID);
173         assertEquals(CommandProcessor.MAX_WORKERS, processor.worker.size());
174     }
175
176     public void testRunMultiple() throws Exception JavaDoc {
177         processor = new CommandProcessor(false);
178
179         TestCommand command1 = new TestCommand(this, 1);
180         command1.setPriority(Command.NORMAL_PRIORITY);
181         processor.addOp(command1, Command.FIRST_EXECUTION);
182
183         TestCommand command2 = new TestCommand(this, 2);
184         command2.setPriority(Command.NORMAL_PRIORITY);
185         command2.setSynchronize(true);
186         processor.addOp(command2, Command.FIRST_EXECUTION);
187
188         processor.startOperation();
189         processor.startOperation();
190         assertEquals(CommandProcessor.MAX_WORKERS - 2, processor.worker.size());
191
192         Thread.sleep(1000);
193
194         assertEquals(2, executedID);
195         assertEquals(CommandProcessor.MAX_WORKERS, processor.worker.size());
196     }
197
198     public void testRunMax() throws Exception JavaDoc {
199         processor = new CommandProcessor(false);
200
201         processor.addOp(new TestCommand(this, 1));
202         processor.addOp(new TestCommand(this, 2));
203         processor.addOp(new TestCommand(this, 3));
204         processor.addOp(new TestCommand(this, 4));
205         processor.addOp(new TestCommand(this, 5));
206
207         processor.startOperation();
208         processor.startOperation();
209         processor.startOperation();
210         processor.startOperation();
211         processor.startOperation();
212         assertEquals(1, processor.worker.size());
213
214         Thread.sleep(1000);
215
216         assertEquals(1, processor.operationQueue.size());
217         assertEquals(CommandProcessor.MAX_WORKERS, processor.worker.size());
218
219         processor.startOperation();
220         assertEquals(CommandProcessor.MAX_WORKERS - 1, processor.worker.size());
221         assertEquals(0, processor.operationQueue.size());
222     }
223
224     public void testRunRealtime() throws Exception JavaDoc {
225         processor = new CommandProcessor(true);
226
227         processor.addOp(new TestCommand(this, 1));
228         processor.addOp(new TestCommand(this, 2));
229         processor.addOp(new TestCommand(this, 3));
230         processor.addOp(new TestCommand(this, 4, Command.REALTIME_PRIORITY));
231         processor.addOp(new TestCommand(this, 5));
232         processor.addOp(new TestCommand(this, 1));
233         processor.addOp(new TestCommand(this, 2));
234         processor.addOp(new TestCommand(this, 3));
235         processor.addOp(new TestCommand(this, 4));
236         processor.addOp(new TestCommand(this, 5, Command.REALTIME_PRIORITY));
237         processor.addOp(new TestCommand(this, 1));
238         processor.addOp(new TestCommand(this, 2));
239         processor.addOp(new TestCommand(this, 3));
240         processor.addOp(new TestCommand(this, 4));
241         processor.addOp(new TestCommand(this, 5));
242
243         Thread.sleep(3000);
244
245         assertEquals(0, processor.operationQueue.size());
246         assertEquals(CommandProcessor.MAX_WORKERS, processor.worker.size());
247     }
248
249 }
250
Popular Tags