/hazelcast/src/test/java/com/hazelcast/util/SimpleBoundedQueueTest.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 103 lines · 69 code · 13 blank · 21 comment · 4 complexity · 52ce1beb1965057ece5372ae7c556eef MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.hazelcast.util;
  17. import org.junit.After;
  18. import org.junit.Test;
  19. import org.junit.runner.RunWith;
  20. import static junit.framework.Assert.*;
  21. @RunWith(com.hazelcast.util.RandomBlockJUnit4ClassRunner.class)
  22. public class SimpleBoundedQueueTest {
  23. private SimpleBoundedQueue<String> queue = new SimpleBoundedQueue<String>(10);
  24. @After
  25. public void clear() {
  26. queue.clear();
  27. }
  28. @Test
  29. public void testAdd() {
  30. queue.add("hello");
  31. assertEquals("hello", queue.poll());
  32. }
  33. @Test
  34. public void testSize() {
  35. queue.add("hello");
  36. assertEquals(1, queue.size());
  37. queue.add("world");
  38. assertEquals(2, queue.size());
  39. }
  40. @Test
  41. public void testOffer() {
  42. queue.offer("hello");
  43. queue.offer("world");
  44. assertEquals("hello", queue.poll());
  45. assertEquals("world", queue.poll());
  46. }
  47. @Test
  48. public void testPeek() {
  49. queue.offer("hello");
  50. queue.add("world");
  51. assertEquals("hello", queue.peek());
  52. }
  53. @Test
  54. public void testPoll() {
  55. queue.offer("hello");
  56. queue.offer("hello");
  57. assertEquals("hello", queue.poll());
  58. assertEquals("hello", queue.poll());
  59. }
  60. @Test
  61. public void testCapacity() {
  62. queue.clear();
  63. assertEquals(0, queue.size());
  64. for (int i = 0; i < 10; i++) {
  65. assertTrue(queue.offer(String.valueOf(i)));
  66. assertEquals(i + 1, queue.size());
  67. }
  68. for (int i = 0; i < 5; i++) {
  69. assertFalse(queue.offer(String.valueOf(i)));
  70. assertEquals(10, queue.size());
  71. }
  72. for (int i = 0; i < 5; i++) {
  73. assertEquals(String.valueOf(i), queue.poll());
  74. assertEquals(10 - i - 1, queue.size());
  75. }
  76. for (int i = 0; i < 5; i++) {
  77. assertTrue(queue.offer(String.valueOf(i)));
  78. }
  79. assertEquals(10, queue.size());
  80. }
  81. @Test
  82. public void testIterator() {
  83. // Unsupported
  84. /*queue.offer("hello");
  85. queue.offer("hello");
  86. for(String message: queue){
  87. assertEquals("hello", message);
  88. }*/
  89. }
  90. }