Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a058941092495674921951c0339c22ba23bc8518 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
/*******************************************************************************
 * Copyright (c) 2009, 2015 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.utility.tests.internal.queue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import org.eclipse.jpt.common.utility.internal.queue.LinkedQueue;
import org.eclipse.jpt.common.utility.internal.queue.QueueTools;
import org.eclipse.jpt.common.utility.internal.queue.SynchronizedQueue;
import org.eclipse.jpt.common.utility.internal.stack.StackTools;
import org.eclipse.jpt.common.utility.queue.Queue;
import org.eclipse.jpt.common.utility.stack.Stack;
import org.eclipse.jpt.common.utility.tests.internal.deque.DequeToolsTests;

@SuppressWarnings("nls")
public class SynchronizedQueueTests
	extends QueueTests
{
	private volatile SynchronizedQueue<String> sq;
	volatile boolean timeoutOccurred;
	volatile long startTime;
	volatile long endTime;
	volatile Object dequeuedObject;

	boolean commandExecuted;

	static final String ITEM_1 = new String();
	static final String ITEM_2 = new String();

	public SynchronizedQueueTests(String name) {
		super(name);
	}

	@Override
	Queue<String> buildQueue() {
		return QueueTools.synchronizedQueue();
	}

	@Override
	public void testClone() {
		// synchronized queue is not cloneable
	}

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		this.sq = QueueTools.synchronizedQueue();
		this.timeoutOccurred = false;
		this.startTime = 0;
		this.endTime = 0;
		this.dequeuedObject = null;
	}

	// ********** constructor **********

	public void testConstructorQueue() throws Exception {
		Queue<String> innerQueue = QueueTools.arrayQueue();
		SynchronizedQueue<String> stack = QueueTools.synchronizedQueue(innerQueue);
		assertNotNull(stack);
		assertSame(stack, stack.getMutex());
	}

	public void testConstructorQueue_NPE() throws Exception {
		boolean exCaught = false;
		try {
			Queue<String> stack = QueueTools.synchronizedQueue(null);
			fail("bogus stack: " + stack);
		} catch (NullPointerException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);
	}

	public void testConstructorQueueObject() throws Exception {
		String mutex = "mutex";
		Queue<String> innerQueue = QueueTools.arrayQueue();
		SynchronizedQueue<String> stack = QueueTools.synchronizedQueue(innerQueue, mutex);
		assertNotNull(stack);
		assertSame(mutex, stack.getMutex());
	}

	public void testConstructorQueueObject_NPE1() throws Exception {
		String mutex = "mutex";
		boolean exCaught = false;
		try {
			Queue<String> stack = QueueTools.synchronizedQueue(null, mutex);
			fail("bogus stack: " + stack);
		} catch (NullPointerException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);
	}

	public void testConstructorQueueObject_NPE2() throws Exception {
		Queue<String> innerQueue = QueueTools.arrayQueue();
		boolean exCaught = false;
		try {
			Queue<String> stack = QueueTools.synchronizedQueue(innerQueue, null);
			fail("bogus stack: " + stack);
		} catch (NullPointerException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);
	}

	// ********** concurrent access **********

	/**
	 * test first with an unsynchronized queue,
	 * then with a synchronized queue
	 */
	public void testConcurrentDequeue() throws Exception {
		this.verifyConcurrentDequeue(new SlowLinkedQueue<String>(), "first");
		this.verifyConcurrentDequeue(new SlowSynchronizedQueue<String>(), "second");
	}

	private void verifyConcurrentDequeue(SlowQueue<String> slowQueue, String expected) throws Exception {
		slowQueue.enqueue("first");
		slowQueue.enqueue("second");

		Thread thread = this.buildThread(this.buildRunnableDequeue(slowQueue));
		thread.start();
		Thread.sleep(TWO_TICKS);

		assertEquals(expected, slowQueue.dequeue());
		thread.join();
		assertTrue(slowQueue.isEmpty());
	}

	private Runnable buildRunnableDequeue(final SlowQueue<String> slowQueue) {
		return new Runnable() {
			public void run() {
				slowQueue.slowDequeue();
			}
		};
	}

	/**
	 * test first with an unsynchronized queue,
	 * then with a synchronized queue
	 */
	public void testConcurrentEnqueue() throws Exception {
		this.verifyConcurrentEnqueue(new SlowLinkedQueue<String>(), "second", "first");
		this.verifyConcurrentEnqueue(new SlowSynchronizedQueue<String>(), "first", "second");
	}

	private void verifyConcurrentEnqueue(SlowQueue<String> slowQueue, String first, String second) throws Exception {
		Thread thread = this.buildThread(this.buildRunnableEnqueue(slowQueue, "first"));
		thread.start();
		Thread.sleep(TWO_TICKS);

		slowQueue.enqueue("second");
		thread.join();
		assertEquals(first, slowQueue.dequeue());
		assertEquals(second, slowQueue.dequeue());
		assertTrue(slowQueue.isEmpty());
	}

	private Runnable buildRunnableEnqueue(final SlowQueue<String> slowQueue, final String element) {
		return new Runnable() {
			public void run() {
				slowQueue.slowEnqueue(element);
			}
		};
	}

	/**
	 * test first with an unsynchronized queue,
	 * then with a synchronized queue
	 */
	public void testConcurrentIsEmpty() throws Exception {
		this.verifyConcurrentIsEmpty(new SlowLinkedQueue<String>(), true);
		this.verifyConcurrentIsEmpty(new SlowSynchronizedQueue<String>(), false);
	}

	private void verifyConcurrentIsEmpty(SlowQueue<String> slowQueue, boolean empty) throws Exception {
		Thread thread = this.buildThread(this.buildRunnableEnqueue(slowQueue, "first"));
		thread.start();
		Thread.sleep(TWO_TICKS);

		assertEquals(empty, slowQueue.isEmpty());
		thread.join();
		assertEquals("first", slowQueue.dequeue());
		assertTrue(slowQueue.isEmpty());
	}


	private interface SlowQueue<E> extends Queue<E> {
		Object slowDequeue();
		void slowEnqueue(E element);
	}

	private class SlowLinkedQueue<E> extends LinkedQueue<E> implements SlowQueue<E> {
		private static final long serialVersionUID = 1L;
		SlowLinkedQueue() {
			super();
		}
		public Object slowDequeue() {
			try {
				Thread.sleep(5 * TICK);
			} catch (InterruptedException ex) {
				throw new RuntimeException(ex);
			}
			return this.dequeue();
		}
		public void slowEnqueue(E element) {
			try {
				Thread.sleep(5 * TICK);
			} catch (InterruptedException ex) {
				throw new RuntimeException(ex);
			}
			this.enqueue(element);
		}
	}

	private class SlowSynchronizedQueue<E> extends SynchronizedQueue<E> implements SlowQueue<E> {
		private static final long serialVersionUID = 1L;
		SlowSynchronizedQueue() {
			super(QueueTools.<E>linkedQueue());
		}
		public Object slowDequeue() {
			synchronized (this.getMutex()) {
				try {
					Thread.sleep(5 * TICK);
				} catch (InterruptedException ex) {
					throw new RuntimeException(ex);
				}
				return this.dequeue();
			}
		}
		public void slowEnqueue(E element) {
			synchronized (this.getMutex()) {
				try {
					Thread.sleep(5 * TICK);
				} catch (InterruptedException ex) {
					throw new RuntimeException(ex);
				}
				this.enqueue(element);
			}
		}
	}


	// ********** waits **********

	public void testWaitUntilEmpty() throws Exception {
		this.verifyWaitUntilEmpty(-1);
		// no timeout occurs...
		assertFalse(this.timeoutOccurred);
		// ...and an item should have been dequeued by t1...
		assertSame(ITEM_1, this.dequeuedObject);
		// ...and the queue should be empty
		assertTrue(this.sq.isEmpty());
		// make a reasonable guess about how long t2 took
		assertTrue(this.calculateElapsedTime() > TICK);
	}

	public void testWaitUntilEmpty2() throws Exception {
		this.verifyWaitUntilEmpty(0);
		// no timeout occurs...
		assertFalse(this.timeoutOccurred);
		// ...and an item should have been dequeued by t1...
		assertSame(ITEM_1, this.dequeuedObject);
		// ...and the queue should be empty
		assertTrue(this.sq.isEmpty());
		// make a reasonable guess about how long t2 took
		assertTrue(this.calculateElapsedTime() > TICK);
	}

	public void testWaitUntilEmptyTimeout() throws Exception {
		this.verifyWaitUntilEmpty(TICK);
		// timeout occurs...
		assertTrue(this.timeoutOccurred);
		// ...and the queue was dequeued...
		assertSame(ITEM_1, this.dequeuedObject);
		// ...and the queue should be empty
		assertTrue(this.sq.isEmpty());
		// make a reasonable guess about how long t2 took
		assertTrue(this.calculateElapsedTime() < THREE_TICKS);
	}

	private void verifyWaitUntilEmpty(long timeout) throws Exception {
		this.sq.enqueue(ITEM_1);
		Runnable r1 = this.buildRunnable(this.buildDequeueCommand(), this.sq, TWO_TICKS);
		Runnable r2 = this.buildRunnable(this.buildWaitUntilEmptyCommand(timeout), this.sq, 0);
		Thread t1 = this.buildThread(r1);
		Thread t2 = this.buildThread(r2);
		t1.start();
		t2.start();
		t1.join();
		t2.join();
	}

	private Command buildWaitUntilEmptyCommand(final long timeout) {
		return new Command() {
			public void execute(SynchronizedQueue<String> synchronizedQueue) throws InterruptedException {
				SynchronizedQueueTests.this.startTime = System.currentTimeMillis();
				SynchronizedQueueTests.this.timeoutOccurred = this.timeoutOccurred(synchronizedQueue);
				SynchronizedQueueTests.this.endTime = System.currentTimeMillis();
			}
			private boolean timeoutOccurred(SynchronizedQueue<String> synchronizedQueue) throws InterruptedException {
				if (timeout < 0) {
					synchronizedQueue.waitUntilEmpty();
					return false;
				}
				return ! synchronizedQueue.waitUntilEmpty(timeout);
			}
		};
	}

	public void testWaitUntilNotEmpty() throws Exception {
		this.verifyWaitUntilNotEmpty(-1);
		// no timeout occurs...
		assertFalse(this.timeoutOccurred);
		// ...and an item should have been enqueued by t1...
		assertFalse(this.sq.isEmpty());
		assertSame(ITEM_1, this.sq.peek());
		// make a reasonable guess about how long t2 took
		assertTrue(this.calculateElapsedTime() > TICK);
	}

	public void testWaitUntilNotEmpty2() throws Exception {
		this.verifyWaitUntilNotEmpty(0);
		// no timeout occurs...
		assertFalse(this.timeoutOccurred);
		// ...and an item should have been enqueued by t1...
		assertFalse(this.sq.isEmpty());
		assertSame(ITEM_1, this.sq.peek());
		// make a reasonable guess about how long t2 took
		assertTrue(this.calculateElapsedTime() > TICK);
	}

	public void testWaitUntilNotEmptyTimeout() throws Exception {
		this.verifyWaitUntilNotEmpty(TICK);
		// timeout occurs...
		assertTrue(this.timeoutOccurred);
		// ...and an item should have been enqueued by t1...
		assertFalse(this.sq.isEmpty());
		assertSame(ITEM_1, this.sq.peek());
		// make a reasonable guess about how long t2 took
		assertTrue(this.calculateElapsedTime() < THREE_TICKS);
	}

	private void verifyWaitUntilNotEmpty(long timeout) throws Exception {
		Runnable r1 = this.buildRunnable(this.buildEnqueueCommand(), this.sq, TWO_TICKS);
		Runnable r2 = this.buildRunnable(this.buildWaitUntilNotEmptyCommand(timeout), this.sq, 0);
		Thread t1 = this.buildThread(r1);
		Thread t2 = this.buildThread(r2);
		t1.start();
		t2.start();
		t1.join();
		t2.join();
	}

	private Command buildWaitUntilNotEmptyCommand(final long timeout) {
		return new Command() {
			public void execute(SynchronizedQueue<String> synchronizedQueue) throws InterruptedException {
				SynchronizedQueueTests.this.startTime = System.currentTimeMillis();
				SynchronizedQueueTests.this.timeoutOccurred = this.timeoutOccurred(synchronizedQueue);
				SynchronizedQueueTests.this.endTime = System.currentTimeMillis();
			}
			private boolean timeoutOccurred(SynchronizedQueue<String> synchronizedQueue) throws InterruptedException {
				if (timeout < 0) {
					synchronizedQueue.waitUntilNotEmpty();
					return false;
				}
				return ! synchronizedQueue.waitUntilNotEmpty(timeout);
			}
		};
	}

	public void testWaitToDequeue() throws Exception {
		this.verifyWaitToDequeue(-1);
		// no timeout occurs...
		assertFalse(this.timeoutOccurred);
		// ...and an item should have been dequeued by t2...
		assertSame(ITEM_1, this.dequeuedObject);
		// ...and the queue should be empty
		assertTrue(this.sq.isEmpty());
		// make a reasonable guess about how long t2 took
		assertTrue(this.calculateElapsedTime() > TICK);
	}

	public void testWaitToDequeue2() throws Exception {
		this.verifyWaitToDequeue(0);
		// no timeout occurs...
		assertFalse(this.timeoutOccurred);
		// ...and an item should have been dequeued by t2...
		assertSame(ITEM_1, this.dequeuedObject);
		// ...and the queue should be empty
		assertTrue(this.sq.isEmpty());
		// make a reasonable guess about how long t2 took
		assertTrue(this.calculateElapsedTime() > TICK);
	}

	public void testWaitToDequeueTimeout() throws Exception {
		this.verifyWaitToDequeue(TICK);
		// timeout occurs...
		assertTrue(this.timeoutOccurred);
		// ...and the queue was never dequeued...
		assertNull(this.dequeuedObject);
		// ...and it still holds the item
		assertSame(ITEM_1, this.sq.peek());
		// make a reasonable guess about how long t2 took
		assertTrue(this.calculateElapsedTime() < THREE_TICKS);
	}

	private void verifyWaitToDequeue(long timeout) throws Exception {
		Runnable r1 = this.buildRunnable(this.buildEnqueueCommand(), this.sq, TWO_TICKS);
		Runnable r2 = this.buildRunnable(this.buildWaitToDequeueCommand(timeout), this.sq, 0);
		Thread t1 = this.buildThread(r1);
		Thread t2 = this.buildThread(r2);
		t1.start();
		t2.start();
		t1.join();
		t2.join();
	}

	private Command buildWaitToDequeueCommand(final long timeout) {
		return new Command() {
			public void execute(SynchronizedQueue<String> synchronizedQueue) throws InterruptedException {
				SynchronizedQueueTests.this.startTime = System.currentTimeMillis();
				this.waitToDequeue(synchronizedQueue);
				SynchronizedQueueTests.this.endTime = System.currentTimeMillis();
			}
			private void waitToDequeue(SynchronizedQueue<String> synchronizedQueue) throws InterruptedException {
				if (timeout < 0) {
					SynchronizedQueueTests.this.dequeuedObject = synchronizedQueue.waitToDequeue();
					return;
				}
				try {
					SynchronizedQueueTests.this.dequeuedObject = synchronizedQueue.waitToDequeue(timeout);
				} catch (NoSuchElementException ex) {
					SynchronizedQueueTests.this.timeoutOccurred = true;
				}
			}
		};
	}

	public void testWaitToEnqueue() throws Exception {
		this.verifyWaitToEnqueue(-1);
		// no timeout occurs...
		assertFalse(this.timeoutOccurred);
		// ...and the queue gets dequeued by t1...
		assertSame(ITEM_1, this.dequeuedObject);
		// ...and an item is enqueued on to the queue by t2
		assertFalse(this.sq.isEmpty());
		assertSame(ITEM_2, this.sq.peek());
		// make a reasonable guess about how long t2 took
		assertTrue(this.calculateElapsedTime() > TICK);
	}

	public void testWaitToEnqueue2() throws Exception {
		this.verifyWaitToEnqueue(0);
		// no timeout occurs...
		assertFalse(this.timeoutOccurred);
		// ...and the queue gets dequeued by t1...
		assertSame(ITEM_1, this.dequeuedObject);
		// ...and an item is enqueued on to the queue by t2
		assertFalse(this.sq.isEmpty());
		assertSame(ITEM_2, this.sq.peek());
		// make a reasonable guess about how long t2 took
		assertTrue(this.calculateElapsedTime() > TICK);
	}

	public void testWaitToEnqueueTimeout() throws Exception {
		this.verifyWaitToEnqueue(TICK);
		// timeout occurs...
		assertTrue(this.timeoutOccurred);
		// ...and the queue is eventually dequeued by t1...
		assertSame(ITEM_1, this.dequeuedObject);
		// ...but nothing is enqueued on to the queue by t2
		assertTrue(this.sq.isEmpty());
		// make a reasonable guess about how long t2 took
		assertTrue(this.calculateElapsedTime() < THREE_TICKS);
	}

	private void verifyWaitToEnqueue(long timeout) throws Exception {
		this.sq.enqueue(ITEM_1);
		Runnable r1 = this.buildRunnable(this.buildDequeueCommand(), this.sq, TWO_TICKS);
		Runnable r2 = this.buildRunnable(this.buildWaitToEnqueueCommand(timeout), this.sq, 0);
		Thread t1 = this.buildThread(r1);
		Thread t2 = this.buildThread(r2);
		t1.start();
		t2.start();
		t1.join();
		t2.join();
	}

	private Command buildWaitToEnqueueCommand(final long timeout) {
		return new Command() {
			public void execute(SynchronizedQueue<String> synchronizedQueue) throws InterruptedException {
				SynchronizedQueueTests.this.startTime = System.currentTimeMillis();
				SynchronizedQueueTests.this.timeoutOccurred = this.timeoutOccurred(synchronizedQueue);
				SynchronizedQueueTests.this.endTime = System.currentTimeMillis();
			}
			private boolean timeoutOccurred(SynchronizedQueue<String> synchronizedQueue) throws InterruptedException {
				if (timeout < 0) {
					synchronizedQueue.waitToEnqueue(ITEM_2);
					return false;
				}
				return ! synchronizedQueue.waitToEnqueue(ITEM_2, timeout);
			}
		};
	}

	private Command buildEnqueueCommand() {
		return new Command() {
			public void execute(SynchronizedQueue<String> synchronizedQueue) {
				synchronizedQueue.enqueue(ITEM_1);
			}
		};
	}

	private Command buildDequeueCommand() {
		return new Command() {
			public void execute(SynchronizedQueue<String> synchronizedQueue) {
				SynchronizedQueueTests.this.dequeuedObject = synchronizedQueue.dequeue();
			}
		};
	}

	private Runnable buildRunnable(final Command command, final SynchronizedQueue<String> synchronizedQueue, final long sleep) {
		return new TestRunnable() {
			@Override
			protected void run_() throws Throwable {
				if (sleep != 0) {
					Thread.sleep(sleep);
				}
				command.execute(synchronizedQueue);
			}
		};
	}

	long calculateElapsedTime() {
		return this.endTime - this.startTime;
	}


	// ********** Command interface **********

	private interface Command {
		void execute(SynchronizedQueue<String> synchronizedQueue) throws InterruptedException;
	}


	// ********** execute **********

	public void testExecute() throws Exception {
		org.eclipse.jpt.common.utility.command.Command command = new org.eclipse.jpt.common.utility.command.Command() {
			public void execute() {
				SynchronizedQueueTests.this.commandExecuted = true;
			}
		};
		this.commandExecuted = false;
		this.sq.execute(command);
		assertTrue(this.commandExecuted);
	}


	// ********** additional protocol **********

	public void testEnqueueAllIterable() throws Exception {
		ArrayList<String> list = new ArrayList<String>();
		list.add("one");
		list.add("two");
		list.add("three");
		this.sq.enqueueAll(list);
		assertEquals("one", this.sq.dequeue());
		assertEquals("two", this.sq.dequeue());
		assertEquals("three", this.sq.dequeue());
		assertTrue(this.sq.isEmpty());
	}

	public void testEnqueueAllIterable_empty() throws Exception {
		ArrayList<String> list = new ArrayList<String>();
		this.sq.enqueueAll(list);
		assertTrue(this.sq.isEmpty());
	}

	public void testEnqueueAllObjectArray() throws Exception {
		this.sq.enqueueAll(new String[] { "one", "two", "three" });
		assertEquals("one", this.sq.dequeue());
		assertEquals("two", this.sq.dequeue());
		assertEquals("three", this.sq.dequeue());
		assertTrue(this.sq.isEmpty());
	}

	public void testEnqueueAllObjectArray_empty() throws Exception {
		this.sq.enqueueAll(new String[0]);
		assertTrue(this.sq.isEmpty());
	}

	public void testEnqueueAllStack() throws Exception {
		Stack<String> stack = StackTools.arrayStack();
		stack.push("one");
		stack.push("two");
		stack.push("three");
		this.sq.enqueueAll(stack);
		assertEquals("three", this.sq.dequeue());
		assertEquals("two", this.sq.dequeue());
		assertEquals("one", this.sq.dequeue());
		assertTrue(this.sq.isEmpty());
	}

	public void testEnqueueAllStack_empty() throws Exception {
		Stack<String> stack = StackTools.arrayStack();
		this.sq.enqueueAll(stack);
		assertTrue(this.sq.isEmpty());
	}

	public void testEnqueueAllQueue() throws Exception {
		Queue<String> queue = QueueTools.arrayQueue();
		queue.enqueue("one");
		queue.enqueue("two");
		queue.enqueue("three");
		this.sq.enqueueAll(queue);
		assertEquals("one", this.sq.dequeue());
		assertEquals("two", this.sq.dequeue());
		assertEquals("three", this.sq.dequeue());
		assertTrue(this.sq.isEmpty());
	}

	public void testEnqueueAllQueue_empty() throws Exception {
		Queue<String> queue = QueueTools.arrayQueue();
		this.sq.enqueueAll(queue);
		assertTrue(this.sq.isEmpty());
	}

	public void testDrain() throws Exception {
		this.sq.enqueue("one");
		this.sq.enqueue("two");
		this.sq.enqueue("three");
		ArrayList<String> list = this.sq.drain();
		assertTrue(this.sq.isEmpty());
		assertEquals("one", list.get(0));
		assertEquals("two", list.get(1));
		assertEquals("three", list.get(2));
	}

	public void testDrain_empty() throws Exception {
		ArrayList<String> list = this.sq.drain();
		assertTrue(this.sq.isEmpty());
		assertTrue(list.isEmpty());
	}

	public void testDrainToCollection() throws Exception {
		this.sq.enqueue("one");
		this.sq.enqueue("two");
		this.sq.enqueue("three");
		ArrayList<String> list = new ArrayList<String>();
		assertTrue(this.sq.drainTo(list));
		assertTrue(this.sq.isEmpty());
		assertEquals("one", list.get(0));
		assertEquals("two", list.get(1));
		assertEquals("three", list.get(2));
	}

	public void testDrainToCollection_empty() throws Exception {
		ArrayList<String> list = new ArrayList<String>();
		assertFalse(this.sq.drainTo(list));
		assertTrue(this.sq.isEmpty());
		assertTrue(list.isEmpty());
	}

	public void testDrainToListInt() throws Exception {
		this.sq.enqueue("one");
		this.sq.enqueue("two");
		this.sq.enqueue("three");
		ArrayList<String> list = new ArrayList<String>();
		list.add("aaa");
		list.add("bbb");
		list.add("ccc");
		assertTrue(this.sq.drainTo(list, 2));
		assertEquals("aaa", list.get(0));
		assertEquals("bbb", list.get(1));
		assertEquals("one", list.get(2));
		assertEquals("two", list.get(3));
		assertEquals("three", list.get(4));
		assertEquals("ccc", list.get(5));
	}

	public void testDrainToListInt_end() throws Exception {
		this.sq.enqueue("one");
		this.sq.enqueue("two");
		this.sq.enqueue("three");
		ArrayList<String> list = new ArrayList<String>();
		list.add("aaa");
		list.add("bbb");
		list.add("ccc");
		assertTrue(this.sq.drainTo(list, 3));
		assertEquals("aaa", list.get(0));
		assertEquals("bbb", list.get(1));
		assertEquals("ccc", list.get(2));
		assertEquals("one", list.get(3));
		assertEquals("two", list.get(4));
		assertEquals("three", list.get(5));
	}

	public void testDrainToListInt_empty() throws Exception {
		ArrayList<String> list = new ArrayList<String>();
		list.add("aaa");
		list.add("bbb");
		list.add("ccc");
		assertFalse(this.sq.drainTo(list, 2));
		assertEquals("aaa", list.get(0));
		assertEquals("bbb", list.get(1));
		assertEquals("ccc", list.get(2));
	}

	public void testDrainToStack() throws Exception {
		this.sq.enqueue("one");
		this.sq.enqueue("two");
		this.sq.enqueue("three");
		Stack<String> stack = StackTools.arrayStack();
		assertTrue(this.sq.drainTo(stack));
		assertTrue(this.sq.isEmpty());
		assertEquals("three", stack.pop());
		assertEquals("two", stack.pop());
		assertEquals("one", stack.pop());
		assertTrue(stack.isEmpty());
	}

	public void testDrainToStack_empty() throws Exception {
		Stack<String> stack = StackTools.arrayStack();
		assertFalse(this.sq.drainTo(stack));
		assertTrue(this.sq.isEmpty());
		assertTrue(stack.isEmpty());
	}

	public void testDrainToQueue() throws Exception {
		this.sq.enqueue("one");
		this.sq.enqueue("two");
		this.sq.enqueue("three");
		Queue<String> queue = QueueTools.arrayQueue();
		assertTrue(this.sq.drainTo(queue));
		assertTrue(this.sq.isEmpty());
		assertEquals("one", queue.dequeue());
		assertEquals("two", queue.dequeue());
		assertEquals("three", queue.dequeue());
		assertTrue(queue.isEmpty());
	}

	public void testDrainToQueue_empty() throws Exception {
		Queue<String> queue = QueueTools.arrayQueue();
		assertFalse(this.sq.drainTo(queue));
		assertTrue(this.sq.isEmpty());
		assertTrue(queue.isEmpty());
	}

	public void testDrainToMapTransformer() {
		this.sq.enqueue("one");
		this.sq.enqueue("two");
		this.sq.enqueue("zero");
		Map<String, String>map = new HashMap<String, String>();
		assertTrue(this.sq.drainTo(map, DequeToolsTests.FIRST_LETTER_TRANSFORMER));
		assertEquals("one", map.get("o"));
		assertEquals("two", map.get("t"));
		assertEquals("zero", map.get("z"));
	}

	public void testDrainToMapTransformer_empty() {
		Map<String, String>map = new HashMap<String, String>();
		assertFalse(this.sq.drainTo(map, DequeToolsTests.FIRST_LETTER_TRANSFORMER));
		assertTrue(map.isEmpty());
	}

	public void testDrainToMapTransformerTransformer() {
		this.sq.enqueue("one");
		this.sq.enqueue("two");
		this.sq.enqueue("zero");
		Map<String, String>map = new HashMap<String, String>();
		assertTrue(this.sq.drainTo(map, DequeToolsTests.FIRST_LETTER_TRANSFORMER, DequeToolsTests.EMPHASIZER));
		assertEquals("*one*", map.get("o"));
		assertEquals("*two*", map.get("t"));
		assertEquals("*zero*", map.get("z"));
	}

	public void testDrainToMapTransformerTransformer_empty() {
		Map<String, String>map = new HashMap<String, String>();
		assertFalse(this.sq.drainTo(map, DequeToolsTests.FIRST_LETTER_TRANSFORMER, DequeToolsTests.EMPHASIZER));
		assertTrue(map.isEmpty());
	}
}

Back to the top