• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Guava Authors
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 
17 package com.google.common.collect;
18 
19 import com.google.common.annotations.GwtCompatible;
20 import com.google.common.annotations.GwtIncompatible;
21 import com.google.common.testing.NullPointerTester;
22 import com.google.common.testing.SerializableTester;
23 import java.util.AbstractList;
24 import java.util.List;
25 import java.util.NoSuchElementException;
26 import junit.framework.TestCase;
27 
28 /**
29  * Tests for {@link EvictingQueue}.
30  *
31  * @author Kurt Alfred Kluever
32  */
33 @GwtCompatible(emulated = true)
34 public class EvictingQueueTest extends TestCase {
35 
testCreateWithNegativeSize()36   public void testCreateWithNegativeSize() throws Exception {
37     try {
38       EvictingQueue.create(-1);
39       fail();
40     } catch (IllegalArgumentException expected) {
41     }
42   }
43 
testCreateWithZeroSize()44   public void testCreateWithZeroSize() throws Exception {
45     EvictingQueue<String> queue = EvictingQueue.create(0);
46     assertEquals(0, queue.size());
47 
48     assertTrue(queue.add("hi"));
49     assertEquals(0, queue.size());
50 
51     assertTrue(queue.offer("hi"));
52     assertEquals(0, queue.size());
53 
54     assertFalse(queue.remove("hi"));
55     assertEquals(0, queue.size());
56 
57     try {
58       queue.element();
59       fail();
60     } catch (NoSuchElementException expected) {
61     }
62 
63     assertNull(queue.peek());
64     assertNull(queue.poll());
65     try {
66       queue.remove();
67       fail();
68     } catch (NoSuchElementException expected) {
69     }
70   }
71 
testRemainingCapacity_maxSize0()72   public void testRemainingCapacity_maxSize0() {
73     EvictingQueue<String> queue = EvictingQueue.create(0);
74     assertEquals(0, queue.remainingCapacity());
75   }
76 
testRemainingCapacity_maxSize1()77   public void testRemainingCapacity_maxSize1() {
78     EvictingQueue<String> queue = EvictingQueue.create(1);
79     assertEquals(1, queue.remainingCapacity());
80     queue.add("hi");
81     assertEquals(0, queue.remainingCapacity());
82   }
83 
testRemainingCapacity_maxSize3()84   public void testRemainingCapacity_maxSize3() {
85     EvictingQueue<String> queue = EvictingQueue.create(3);
86     assertEquals(3, queue.remainingCapacity());
87     queue.add("hi");
88     assertEquals(2, queue.remainingCapacity());
89     queue.add("hi");
90     assertEquals(1, queue.remainingCapacity());
91     queue.add("hi");
92     assertEquals(0, queue.remainingCapacity());
93   }
94 
testEvictingAfterOne()95   public void testEvictingAfterOne() throws Exception {
96     EvictingQueue<String> queue = EvictingQueue.create(1);
97     assertEquals(0, queue.size());
98     assertEquals(1, queue.remainingCapacity());
99 
100     assertTrue(queue.add("hi"));
101     assertEquals("hi", queue.element());
102     assertEquals("hi", queue.peek());
103     assertEquals(1, queue.size());
104     assertEquals(0, queue.remainingCapacity());
105 
106     assertTrue(queue.add("there"));
107     assertEquals("there", queue.element());
108     assertEquals("there", queue.peek());
109     assertEquals(1, queue.size());
110     assertEquals(0, queue.remainingCapacity());
111 
112     assertEquals("there", queue.remove());
113     assertEquals(0, queue.size());
114     assertEquals(1, queue.remainingCapacity());
115   }
116 
testEvictingAfterThree()117   public void testEvictingAfterThree() throws Exception {
118     EvictingQueue<String> queue = EvictingQueue.create(3);
119     assertEquals(0, queue.size());
120     assertEquals(3, queue.remainingCapacity());
121 
122     assertTrue(queue.add("one"));
123     assertTrue(queue.add("two"));
124     assertTrue(queue.add("three"));
125     assertEquals("one", queue.element());
126     assertEquals("one", queue.peek());
127     assertEquals(3, queue.size());
128     assertEquals(0, queue.remainingCapacity());
129 
130     assertTrue(queue.add("four"));
131     assertEquals("two", queue.element());
132     assertEquals("two", queue.peek());
133     assertEquals(3, queue.size());
134     assertEquals(0, queue.remainingCapacity());
135 
136     assertEquals("two", queue.remove());
137     assertEquals(2, queue.size());
138     assertEquals(1, queue.remainingCapacity());
139   }
140 
testAddAll()141   public void testAddAll() throws Exception {
142     EvictingQueue<String> queue = EvictingQueue.create(3);
143     assertEquals(0, queue.size());
144     assertEquals(3, queue.remainingCapacity());
145 
146     assertTrue(queue.addAll(ImmutableList.of("one", "two", "three")));
147     assertEquals("one", queue.element());
148     assertEquals("one", queue.peek());
149     assertEquals(3, queue.size());
150     assertEquals(0, queue.remainingCapacity());
151 
152     assertTrue(queue.addAll(ImmutableList.of("four")));
153     assertEquals("two", queue.element());
154     assertEquals("two", queue.peek());
155     assertEquals(3, queue.size());
156     assertEquals(0, queue.remainingCapacity());
157 
158     assertEquals("two", queue.remove());
159     assertEquals(2, queue.size());
160     assertEquals(1, queue.remainingCapacity());
161   }
162 
testAddAll_largeList()163   public void testAddAll_largeList() {
164     final List<String> list = ImmutableList.of("one", "two", "three", "four", "five");
165     List<String> misbehavingList =
166         new AbstractList<String>() {
167           @Override
168           public int size() {
169             return list.size();
170           }
171 
172           @Override
173           public String get(int index) {
174             if (index < 2) {
175               throw new AssertionError();
176             }
177             return list.get(index);
178           }
179         };
180 
181     EvictingQueue<String> queue = EvictingQueue.create(3);
182     assertTrue(queue.addAll(misbehavingList));
183 
184     assertEquals("three", queue.remove());
185     assertEquals("four", queue.remove());
186     assertEquals("five", queue.remove());
187     assertTrue(queue.isEmpty());
188   }
189 
190   @GwtIncompatible // NullPointerTester
testNullPointerExceptions()191   public void testNullPointerExceptions() {
192     NullPointerTester tester = new NullPointerTester();
193     tester.testAllPublicStaticMethods(EvictingQueue.class);
194     tester.testAllPublicConstructors(EvictingQueue.class);
195     EvictingQueue<String> queue = EvictingQueue.create(5);
196     // The queue must be non-empty so it throws a NPE correctly
197     queue.add("one");
198     tester.testAllPublicInstanceMethods(queue);
199   }
200 
testSerialization()201   public void testSerialization() {
202     EvictingQueue<String> original = EvictingQueue.create(5);
203     original.add("one");
204     original.add("two");
205     original.add("three");
206 
207     EvictingQueue<String> copy = SerializableTester.reserialize(original);
208     assertEquals(copy.maxSize, original.maxSize);
209     assertEquals("one", copy.remove());
210     assertEquals("two", copy.remove());
211     assertEquals("three", copy.remove());
212     assertTrue(copy.isEmpty());
213   }
214 }
215