ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collections/EmptyNavigableSet.java
(Generate patch)

Comparing jsr166/src/test/jtreg/util/Collections/EmptyNavigableSet.java (file contents):
Revision 1.5 by jsr166, Tue May 2 14:15:31 2017 UTC vs.
Revision 1.11 by jsr166, Mon Jan 8 03:12:03 2018 UTC

# Line 1 | Line 1
1   /*
2 < * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
2 > * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
3   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4   *
5   * This code is free software; you can redistribute it and/or modify it
# Line 27 | Line 27
27   * @summary Unit test for Collections.emptyNavigableSet
28   * @run testng EmptyNavigableSet
29   */
30 +
31 + import org.testng.Assert;
32 + import org.testng.Assert.ThrowingRunnable;
33 + import org.testng.annotations.DataProvider;
34 + import org.testng.annotations.Test;
35 +
36   import java.math.BigInteger;
37   import java.util.Arrays;
38   import java.util.Collection;
39   import java.util.Collections;
40   import java.util.Comparator;
41   import java.util.Iterator;
36 import java.util.NoSuchElementException;
42   import java.util.NavigableSet;
43 + import java.util.NoSuchElementException;
44   import java.util.SortedSet;
45   import java.util.TreeSet;
40 import org.testng.annotations.Test;
41 import org.testng.annotations.DataProvider;
46  
43 import static org.testng.Assert.fail;
47   import static org.testng.Assert.assertFalse;
48   import static org.testng.Assert.assertSame;
49   import static org.testng.Assert.assertTrue;
# Line 48 | Line 51 | import static org.testng.Assert.assertTr
51   public class EmptyNavigableSet {
52  
53      public static <T> void assertInstance(T actual, Class<? extends T> expected) {
54 <        assertInstance(expected.isInstance(actual), null);
54 >        assertInstance(actual, expected, null);
55      }
56  
57      public static <T> void assertInstance(T actual, Class<? extends T> expected, String message) {
# Line 67 | Line 70 | public class EmptyNavigableSet {
70              ((null != message) ? message : "") + " Not empty. ");
71      }
72  
73 <    public interface Thrower<T extends Throwable> {
73 >    private <T extends Throwable> void assertThrows(Class<T> throwableClass,
74 >                                                    ThrowingRunnable runnable,
75 >                                                    String message) {
76 >        try {
77 >            Assert.assertThrows(throwableClass, runnable);
78 >        } catch (AssertionError e) {
79 >            throw new AssertionError(String.format("%s%n%s",
80 >                    ((null != message) ? message : ""), e.getMessage()), e);
81 >        }
82 >    }
83  
84 <        public void run() throws T;
84 >    private void assertThrowsCCE(ThrowingRunnable r, String s) {
85 >        assertThrows(ClassCastException.class, r, s);
86      }
87  
88 <    public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) {
89 <        assertThrows(thrower, throwable, null);
88 >    private void assertThrowsNPE(ThrowingRunnable r, String s) {
89 >        assertThrows(NullPointerException.class, r, s);
90      }
91  
92 <    public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable, String message) {
93 <        Throwable result;
94 <        try {
82 <            thrower.run();
83 <            fail(((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");
84 <            return;
85 <        } catch (Throwable caught) {
86 <            result = caught;
87 <        }
92 >    private void assertThrowsIAE(ThrowingRunnable r, String s) {
93 >        assertThrows(IllegalArgumentException.class, r, s);
94 >    }
95  
96 <        assertInstance(result, throwable, ((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");
96 >    private void assertThrowsNSEE(ThrowingRunnable r, String s) {
97 >        assertThrows(NoSuchElementException.class, r, s);
98      }
99  
100      public static final boolean isDescending(SortedSet<?> set) {
# Line 123 | Line 131 | public class EmptyNavigableSet {
131       */
132      @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
133      public void testContainsRequiresComparable(String description, NavigableSet<?> navigableSet) {
134 <        assertThrows(() -> {
135 <            navigableSet.contains(new Object());
128 <        },
129 <            ClassCastException.class,
134 >        assertThrowsCCE(
135 >            () -> navigableSet.contains(new Object()),
136              description + ": Comparable should be required");
137      }
138  
# Line 176 | Line 182 | public class EmptyNavigableSet {
182       */
183      @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
184      public void testFirst(String description, NavigableSet<?> navigableSet) {
185 <        assertThrows(() -> {
180 <            navigableSet.first();
181 <        }, NoSuchElementException.class, description);
185 >        assertThrowsNSEE(navigableSet::first, description);
186      }
187  
188      /**
# Line 186 | Line 190 | public class EmptyNavigableSet {
190       */
191      @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
192      public void testHeadSet(String description, NavigableSet navigableSet) {
193 <        assertThrows(
193 >        assertThrowsNPE(
194              () -> { NavigableSet ns = navigableSet.headSet(null, false); },
191            NullPointerException.class,
195              description + ": Must throw NullPointerException for null element");
196  
197 <        assertThrows(
197 >        assertThrowsCCE(
198              () -> { NavigableSet ns = navigableSet.headSet(new Object(), true); },
196            ClassCastException.class,
199              description + ": Must throw ClassCastException for non-Comparable element");
200  
201          NavigableSet ns = navigableSet.headSet("1", false);
# Line 206 | Line 208 | public class EmptyNavigableSet {
208       */
209      @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
210      public void testLast(String description, NavigableSet<?> navigableSet) {
211 <        assertThrows(() -> {
210 <            navigableSet.last();
211 <        }, NoSuchElementException.class, description);
211 >        assertThrowsNSEE(navigableSet::last, description);
212      }
213  
214      /**
# Line 224 | Line 224 | public class EmptyNavigableSet {
224       */
225      @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
226      public void testSubSet(String description, NavigableSet navigableSet) {
227 <        assertThrows(
227 >        assertThrowsNPE(
228              () -> {
229                  SortedSet ss = navigableSet.subSet(null, BigInteger.TEN);
230              },
231            NullPointerException.class,
231              description + ": Must throw NullPointerException for null element");
232  
233 <        assertThrows(
233 >        assertThrowsNPE(
234              () -> {
235                  SortedSet ss = navigableSet.subSet(BigInteger.ZERO, null);
236              },
238            NullPointerException.class,
237              description + ": Must throw NullPointerException for null element");
238  
239 <        assertThrows(
239 >        assertThrowsNPE(
240              () -> {
241                  SortedSet ss = navigableSet.subSet(null, null);
242              },
245            NullPointerException.class,
243              description + ": Must throw NullPointerException for null element");
244  
245          Object obj1 = new Object();
246          Object obj2 = new Object();
247  
248 <        assertThrows(
248 >        assertThrowsCCE(
249              () -> {
250                  SortedSet ss = navigableSet.subSet(obj1, BigInteger.TEN);
251              },
252 <            ClassCastException.class, description
256 <            + ": Must throw ClassCastException for parameter which is not Comparable.");
252 >            description + ": Must throw ClassCastException for parameter which is not Comparable.");
253  
254 <        assertThrows(
254 >        assertThrowsCCE(
255              () -> {
256                  SortedSet ss = navigableSet.subSet(BigInteger.ZERO, obj2);
257              },
258 <            ClassCastException.class, description
263 <            + ": Must throw ClassCastException for parameter which is not Comparable.");
258 >            description + ": Must throw ClassCastException for parameter which is not Comparable.");
259  
260 <        assertThrows(
260 >        assertThrowsCCE(
261              () -> {
262                  SortedSet ss = navigableSet.subSet(obj1, obj2);
263              },
264 <            ClassCastException.class, description
270 <            + ": Must throw ClassCastException for parameter which is not Comparable.");
264 >            description + ": Must throw ClassCastException for parameter which is not Comparable.");
265  
266          // minimal range
267          navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, false);
# Line 278 | Line 272 | public class EmptyNavigableSet {
272          Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;
273          Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;
274  
275 <            assertThrows(
276 <                () -> {
277 <                    navigableSet.subSet(last, true, first, false);
284 <                },
285 <                IllegalArgumentException.class, description
275 >            assertThrowsIAE(
276 >                () -> navigableSet.subSet(last, true, first, false),
277 >                description
278                  + ": Must throw IllegalArgumentException when fromElement is not less than toElement.");
279  
280          navigableSet.subSet(first, true, last, false);
# Line 301 | Line 293 | public class EmptyNavigableSet {
293          // slightly smaller
294          NavigableSet ns = subSet.subSet(first, false, last, false);
295          // slight expansion
296 <        assertThrows(() -> {
297 <            ns.subSet(first, true, last, true);
306 <        },
307 <            IllegalArgumentException.class,
296 >        assertThrowsIAE(
297 >            () -> ns.subSet(first, true, last, true),
298              description + ": Expansion should not be allowed");
299  
300          // much smaller
# Line 322 | Line 312 | public class EmptyNavigableSet {
312          NavigableSet ns = subSet.headSet(BigInteger.ONE, false);
313  
314          // slight expansion
315 <        assertThrows(() -> {
316 <            ns.headSet(BigInteger.ONE, true);
327 <        },
328 <            IllegalArgumentException.class,
315 >        assertThrowsIAE(
316 >            () -> ns.headSet(BigInteger.ONE, true),
317              description + ": Expansion should not be allowed");
318  
319          // much smaller
# Line 343 | Line 331 | public class EmptyNavigableSet {
331          NavigableSet ns = subSet.tailSet(BigInteger.ONE, false);
332  
333          // slight expansion
334 <        assertThrows(() -> {
335 <            ns.tailSet(BigInteger.ONE, true);
348 <        },
349 <            IllegalArgumentException.class,
334 >        assertThrowsIAE(
335 >            () -> ns.tailSet(BigInteger.ONE, true),
336              description + ": Expansion should not be allowed");
337  
338          // much smaller
# Line 358 | Line 344 | public class EmptyNavigableSet {
344       */
345      @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
346      public void testTailSet(String description, NavigableSet navigableSet) {
347 <        assertThrows(() -> {
348 <            navigableSet.tailSet(null);
363 <        },
364 <            NullPointerException.class,
347 >        assertThrowsNPE(
348 >            () -> navigableSet.tailSet(null),
349              description + ": Must throw NullPointerException for null element");
350  
351 <        assertThrows(() -> {
352 <            navigableSet.tailSet(new Object());
353 <        }, ClassCastException.class);
351 >        assertThrowsCCE(
352 >            () -> navigableSet.tailSet(new Object()),
353 >            description);
354  
355          NavigableSet ss = navigableSet.tailSet("1", true);
356  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines