PreconditionsTest.java revision dbd967a6e5c96cc1a97c5521f88dc1564ba2f81b
1/*
2 * Copyright (C) 2006 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
17package com.google.common.base;
18
19import com.google.common.annotations.GwtCompatible;
20import com.google.common.annotations.GwtIncompatible;
21import com.google.common.testing.NullPointerTester;
22
23import junit.framework.TestCase;
24
25/**
26 * Unit test for {@link Preconditions}.
27 *
28 * @author Kevin Bourrillion
29 * @author Jared Levy
30 */
31@GwtCompatible(emulated = true)
32public class PreconditionsTest extends TestCase {
33  public void testCheckArgument_simple_success() {
34    Preconditions.checkArgument(true);
35  }
36
37  public void testCheckArgument_simple_failure() {
38    try {
39      Preconditions.checkArgument(false);
40      fail("no exception thrown");
41    } catch (IllegalArgumentException expected) {
42    }
43  }
44
45  public void testCheckArgument_simpleMessage_success() {
46    Preconditions.checkArgument(true, IGNORE_ME);
47  }
48
49  public void testCheckArgument_simpleMessage_failure() {
50    try {
51      Preconditions.checkArgument(false, new Message());
52      fail("no exception thrown");
53    } catch (IllegalArgumentException expected) {
54      verifySimpleMessage(expected);
55    }
56  }
57
58  public void testCheckArgument_nullMessage_failure() {
59    try {
60      Preconditions.checkArgument(false, null);
61      fail("no exception thrown");
62    } catch (IllegalArgumentException expected) {
63      assertEquals("null", expected.getMessage());
64    }
65  }
66
67  public void testCheckArgument_complexMessage_success() {
68    Preconditions.checkArgument(true, "%s", IGNORE_ME);
69  }
70
71  public void testCheckArgument_complexMessage_failure() {
72    try {
73      Preconditions.checkArgument(false, FORMAT, 5);
74      fail("no exception thrown");
75    } catch (IllegalArgumentException expected) {
76      verifyComplexMessage(expected);
77    }
78  }
79
80  public void testCheckState_simple_success() {
81    Preconditions.checkState(true);
82  }
83
84  public void testCheckState_simple_failure() {
85    try {
86      Preconditions.checkState(false);
87      fail("no exception thrown");
88    } catch (IllegalStateException expected) {
89    }
90  }
91
92  public void testCheckState_simpleMessage_success() {
93    Preconditions.checkState(true, IGNORE_ME);
94  }
95
96  public void testCheckState_simpleMessage_failure() {
97    try {
98      Preconditions.checkState(false, new Message());
99      fail("no exception thrown");
100    } catch (IllegalStateException expected) {
101      verifySimpleMessage(expected);
102    }
103  }
104
105  public void testCheckState_nullMessage_failure() {
106    try {
107      Preconditions.checkState(false, null);
108      fail("no exception thrown");
109    } catch (IllegalStateException expected) {
110      assertEquals("null", expected.getMessage());
111    }
112  }
113
114  public void testCheckState_complexMessage_success() {
115    Preconditions.checkState(true, "%s", IGNORE_ME);
116  }
117
118  public void testCheckState_complexMessage_failure() {
119    try {
120      Preconditions.checkState(false, FORMAT, 5);
121      fail("no exception thrown");
122    } catch (IllegalStateException expected) {
123      verifyComplexMessage(expected);
124    }
125  }
126
127  private static final String NON_NULL_STRING = "foo";
128
129  public void testCheckNotNull_simple_success() {
130    String result = Preconditions.checkNotNull(NON_NULL_STRING);
131    assertSame(NON_NULL_STRING, result);
132  }
133
134  public void testCheckNotNull_simple_failure() {
135    try {
136      Preconditions.checkNotNull(null);
137      fail("no exception thrown");
138    } catch (NullPointerException expected) {
139    }
140  }
141
142  public void testCheckNotNull_simpleMessage_success() {
143    String result = Preconditions.checkNotNull(NON_NULL_STRING, IGNORE_ME);
144    assertSame(NON_NULL_STRING, result);
145  }
146
147  public void testCheckNotNull_simpleMessage_failure() {
148    try {
149      Preconditions.checkNotNull(null, new Message());
150      fail("no exception thrown");
151    } catch (NullPointerException expected) {
152      verifySimpleMessage(expected);
153    }
154  }
155
156  public void testCheckNotNull_complexMessage_success() {
157    String result = Preconditions.checkNotNull(
158        NON_NULL_STRING, "%s", IGNORE_ME);
159    assertSame(NON_NULL_STRING, result);
160  }
161
162  public void testCheckNotNull_complexMessage_failure() {
163    try {
164      Preconditions.checkNotNull(null, FORMAT, 5);
165      fail("no exception thrown");
166    } catch (NullPointerException expected) {
167      verifyComplexMessage(expected);
168    }
169  }
170
171  public void testCheckElementIndex_ok() {
172    assertEquals(0, Preconditions.checkElementIndex(0, 1));
173    assertEquals(0, Preconditions.checkElementIndex(0, 2));
174    assertEquals(1, Preconditions.checkElementIndex(1, 2));
175  }
176
177  public void testCheckElementIndex_badSize() {
178    try {
179      Preconditions.checkElementIndex(1, -1);
180      fail();
181    } catch (IllegalArgumentException expected) {
182      // don't care what the message text is, as this is an invalid usage of
183      // the Preconditions class, unlike all the other exceptions it throws
184    }
185  }
186
187  public void testCheckElementIndex_negative() {
188    try {
189      Preconditions.checkElementIndex(-1, 1);
190      fail();
191    } catch (IndexOutOfBoundsException expected) {
192      assertEquals("index (-1) must not be negative", expected.getMessage());
193    }
194  }
195
196  public void testCheckElementIndex_tooHigh() {
197    try {
198      Preconditions.checkElementIndex(1, 1);
199      fail();
200    } catch (IndexOutOfBoundsException expected) {
201      assertEquals("index (1) must be less than size (1)",
202          expected.getMessage());
203    }
204  }
205
206  public void testCheckElementIndex_withDesc_negative() {
207    try {
208      Preconditions.checkElementIndex(-1, 1, "foo");
209      fail();
210    } catch (IndexOutOfBoundsException expected) {
211      assertEquals("foo (-1) must not be negative", expected.getMessage());
212    }
213  }
214
215  public void testCheckElementIndex_withDesc_tooHigh() {
216    try {
217      Preconditions.checkElementIndex(1, 1, "foo");
218      fail();
219    } catch (IndexOutOfBoundsException expected) {
220      assertEquals("foo (1) must be less than size (1)",
221          expected.getMessage());
222    }
223  }
224
225  public void testCheckPositionIndex_ok() {
226    assertEquals(0, Preconditions.checkPositionIndex(0, 0));
227    assertEquals(0, Preconditions.checkPositionIndex(0, 1));
228    assertEquals(1, Preconditions.checkPositionIndex(1, 1));
229  }
230
231  public void testCheckPositionIndex_badSize() {
232    try {
233      Preconditions.checkPositionIndex(1, -1);
234      fail();
235    } catch (IllegalArgumentException expected) {
236      // don't care what the message text is, as this is an invalid usage of
237      // the Preconditions class, unlike all the other exceptions it throws
238    }
239  }
240
241  public void testCheckPositionIndex_negative() {
242    try {
243      Preconditions.checkPositionIndex(-1, 1);
244      fail();
245    } catch (IndexOutOfBoundsException expected) {
246      assertEquals("index (-1) must not be negative", expected.getMessage());
247    }
248  }
249
250  public void testCheckPositionIndex_tooHigh() {
251    try {
252      Preconditions.checkPositionIndex(2, 1);
253      fail();
254    } catch (IndexOutOfBoundsException expected) {
255      assertEquals("index (2) must not be greater than size (1)",
256          expected.getMessage());
257    }
258  }
259
260  public void testCheckPositionIndex_withDesc_negative() {
261    try {
262      Preconditions.checkPositionIndex(-1, 1, "foo");
263      fail();
264    } catch (IndexOutOfBoundsException expected) {
265      assertEquals("foo (-1) must not be negative", expected.getMessage());
266    }
267  }
268
269  public void testCheckPositionIndex_withDesc_tooHigh() {
270    try {
271      Preconditions.checkPositionIndex(2, 1, "foo");
272      fail();
273    } catch (IndexOutOfBoundsException expected) {
274      assertEquals("foo (2) must not be greater than size (1)",
275          expected.getMessage());
276    }
277  }
278
279  public void testCheckPositionIndexes_ok() {
280    Preconditions.checkPositionIndexes(0, 0, 0);
281    Preconditions.checkPositionIndexes(0, 0, 1);
282    Preconditions.checkPositionIndexes(0, 1, 1);
283    Preconditions.checkPositionIndexes(1, 1, 1);
284  }
285
286  public void testCheckPositionIndexes_badSize() {
287    try {
288      Preconditions.checkPositionIndexes(1, 1, -1);
289      fail();
290    } catch (IllegalArgumentException expected) {
291    }
292  }
293
294  public void testCheckPositionIndex_startNegative() {
295    try {
296      Preconditions.checkPositionIndexes(-1, 1, 1);
297      fail();
298    } catch (IndexOutOfBoundsException expected) {
299      assertEquals("start index (-1) must not be negative",
300          expected.getMessage());
301    }
302  }
303
304  public void testCheckPositionIndexes_endTooHigh() {
305    try {
306      Preconditions.checkPositionIndexes(0, 2, 1);
307      fail();
308    } catch (IndexOutOfBoundsException expected) {
309      assertEquals("end index (2) must not be greater than size (1)",
310          expected.getMessage());
311    }
312  }
313
314  public void testCheckPositionIndexes_reversed() {
315    try {
316      Preconditions.checkPositionIndexes(1, 0, 1);
317      fail();
318    } catch (IndexOutOfBoundsException expected) {
319      assertEquals("end index (0) must not be less than start index (1)",
320          expected.getMessage());
321    }
322  }
323
324  public void testFormat() {
325    assertEquals("%s", Preconditions.format("%s"));
326    assertEquals("5", Preconditions.format("%s", 5));
327    assertEquals("foo [5]", Preconditions.format("foo", 5));
328    assertEquals("foo [5, 6, 7]", Preconditions.format("foo", 5, 6, 7));
329    assertEquals("%s 1 2", Preconditions.format("%s %s %s", "%s", 1, 2));
330    assertEquals(" [5, 6]", Preconditions.format("", 5, 6));
331    assertEquals("123", Preconditions.format("%s%s%s", 1, 2, 3));
332    assertEquals("1%s%s", Preconditions.format("%s%s%s", 1));
333    assertEquals("5 + 6 = 11", Preconditions.format("%s + 6 = 11", 5));
334    assertEquals("5 + 6 = 11", Preconditions.format("5 + %s = 11", 6));
335    assertEquals("5 + 6 = 11", Preconditions.format("5 + 6 = %s", 11));
336    assertEquals("5 + 6 = 11", Preconditions.format("%s + %s = %s", 5, 6, 11));
337    assertEquals("null [null, null]",
338        Preconditions.format("%s", null, null, null));
339    assertEquals("null [5, 6]", Preconditions.format(null, 5, 6));
340  }
341
342  @GwtIncompatible("NullPointerTester")
343  public void testNullPointers() throws Exception {
344    NullPointerTester tester = new NullPointerTester();
345    tester.testAllPublicStaticMethods(Preconditions.class);
346  }
347
348  private static final Object IGNORE_ME = new Object() {
349    @Override public String toString() {
350      fail();
351      return null;
352    }
353  };
354
355  private static class Message {
356    boolean invoked;
357    @Override public String toString() {
358      assertFalse(invoked);
359      invoked = true;
360      return "A message";
361    }
362  }
363
364  private static final String FORMAT = "I ate %s pies.";
365
366  private static void verifySimpleMessage(Exception e) {
367    assertEquals("A message", e.getMessage());
368  }
369
370  private static void verifyComplexMessage(Exception e) {
371    assertEquals("I ate 5 pies.", e.getMessage());
372  }
373}
374