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.3 by jsr166, Mon Feb 8 22:13:07 2016 UTC vs.
Revision 1.13 by jsr166, Sun Jul 22 20:33:01 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;
44 import static org.testng.Assert.assertEquals;
45 import static org.testng.Assert.assertTrue;
47   import static org.testng.Assert.assertFalse;
48   import static org.testng.Assert.assertSame;
49 + import static org.testng.Assert.assertTrue;
50  
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 68 | 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 {
83 <            thrower.run();
84 <            fail(((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");
85 <            return;
86 <        } catch (Throwable caught) {
87 <            result = caught;
88 <        }
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 124 | 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());
136 <        },
130 <            ClassCastException.class,
131 <            description + ": Compareable should be required");
134 >        assertThrowsCCE(
135 >            () -> navigableSet.contains(new Object()),
136 >            description + ": Comparable should be required");
137      }
138  
139      /**
# Line 158 | Line 163 | public class EmptyNavigableSet {
163       */
164      @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
165      public void testEmptyIterator(String description, NavigableSet<?> navigableSet) {
166 <        Iterator emptyIterator = navigableSet.iterator();
162 <
163 <        assertFalse((emptyIterator != null) && (emptyIterator.hasNext()),
164 <            "The iterator is not empty.");
166 >        assertFalse(navigableSet.iterator().hasNext(), "The iterator is not empty.");
167      }
168  
169      /**
# Line 177 | Line 179 | public class EmptyNavigableSet {
179       */
180      @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
181      public void testFirst(String description, NavigableSet<?> navigableSet) {
182 <        assertThrows(() -> {
181 <            navigableSet.first();
182 <        }, NoSuchElementException.class, description);
182 >        assertThrowsNSEE(navigableSet::first, description);
183      }
184  
185      /**
# Line 187 | Line 187 | public class EmptyNavigableSet {
187       */
188      @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
189      public void testHeadSet(String description, NavigableSet navigableSet) {
190 <        assertThrows(
190 >        assertThrowsNPE(
191              () -> { NavigableSet ns = navigableSet.headSet(null, false); },
192            NullPointerException.class,
192              description + ": Must throw NullPointerException for null element");
193  
194 <        assertThrows(
194 >        assertThrowsCCE(
195              () -> { NavigableSet ns = navigableSet.headSet(new Object(), true); },
197            ClassCastException.class,
196              description + ": Must throw ClassCastException for non-Comparable element");
197  
198          NavigableSet ns = navigableSet.headSet("1", false);
# Line 207 | Line 205 | public class EmptyNavigableSet {
205       */
206      @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
207      public void testLast(String description, NavigableSet<?> navigableSet) {
208 <        assertThrows(() -> {
211 <            navigableSet.last();
212 <        }, NoSuchElementException.class, description);
208 >        assertThrowsNSEE(navigableSet::last, description);
209      }
210  
211      /**
# Line 225 | Line 221 | public class EmptyNavigableSet {
221       */
222      @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
223      public void testSubSet(String description, NavigableSet navigableSet) {
224 <        assertThrows(
224 >        assertThrowsNPE(
225              () -> {
226                  SortedSet ss = navigableSet.subSet(null, BigInteger.TEN);
227              },
232            NullPointerException.class,
228              description + ": Must throw NullPointerException for null element");
229  
230 <        assertThrows(
230 >        assertThrowsNPE(
231              () -> {
232                  SortedSet ss = navigableSet.subSet(BigInteger.ZERO, null);
233              },
239            NullPointerException.class,
234              description + ": Must throw NullPointerException for null element");
235  
236 <        assertThrows(
236 >        assertThrowsNPE(
237              () -> {
238                  SortedSet ss = navigableSet.subSet(null, null);
239              },
246            NullPointerException.class,
240              description + ": Must throw NullPointerException for null element");
241  
242          Object obj1 = new Object();
243          Object obj2 = new Object();
244  
245 <        assertThrows(
245 >        assertThrowsCCE(
246              () -> {
247                  SortedSet ss = navigableSet.subSet(obj1, BigInteger.TEN);
248              },
249 <            ClassCastException.class, description
257 <            + ": Must throw ClassCastException for parameter which is not Comparable.");
249 >            description + ": Must throw ClassCastException for parameter which is not Comparable.");
250  
251 <        assertThrows(
251 >        assertThrowsCCE(
252              () -> {
253                  SortedSet ss = navigableSet.subSet(BigInteger.ZERO, obj2);
254              },
255 <            ClassCastException.class, description
264 <            + ": Must throw ClassCastException for parameter which is not Comparable.");
255 >            description + ": Must throw ClassCastException for parameter which is not Comparable.");
256  
257 <        assertThrows(
257 >        assertThrowsCCE(
258              () -> {
259                  SortedSet ss = navigableSet.subSet(obj1, obj2);
260              },
261 <            ClassCastException.class, description
271 <            + ": Must throw ClassCastException for parameter which is not Comparable.");
261 >            description + ": Must throw ClassCastException for parameter which is not Comparable.");
262  
263          // minimal range
264          navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, false);
# Line 279 | Line 269 | public class EmptyNavigableSet {
269          Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;
270          Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;
271  
272 <            assertThrows(
273 <                () -> {
274 <                    navigableSet.subSet(last, true, first, false);
285 <                },
286 <                IllegalArgumentException.class, description
272 >            assertThrowsIAE(
273 >                () -> navigableSet.subSet(last, true, first, false),
274 >                description
275                  + ": Must throw IllegalArgumentException when fromElement is not less than toElement.");
276  
277          navigableSet.subSet(first, true, last, false);
# Line 302 | Line 290 | public class EmptyNavigableSet {
290          // slightly smaller
291          NavigableSet ns = subSet.subSet(first, false, last, false);
292          // slight expansion
293 <        assertThrows(() -> {
294 <            ns.subSet(first, true, last, true);
307 <        },
308 <            IllegalArgumentException.class,
293 >        assertThrowsIAE(
294 >            () -> ns.subSet(first, true, last, true),
295              description + ": Expansion should not be allowed");
296  
297          // much smaller
# Line 323 | Line 309 | public class EmptyNavigableSet {
309          NavigableSet ns = subSet.headSet(BigInteger.ONE, false);
310  
311          // slight expansion
312 <        assertThrows(() -> {
313 <            ns.headSet(BigInteger.ONE, true);
328 <        },
329 <            IllegalArgumentException.class,
312 >        assertThrowsIAE(
313 >            () -> ns.headSet(BigInteger.ONE, true),
314              description + ": Expansion should not be allowed");
315  
316          // much smaller
# Line 344 | Line 328 | public class EmptyNavigableSet {
328          NavigableSet ns = subSet.tailSet(BigInteger.ONE, false);
329  
330          // slight expansion
331 <        assertThrows(() -> {
332 <            ns.tailSet(BigInteger.ONE, true);
349 <        },
350 <            IllegalArgumentException.class,
331 >        assertThrowsIAE(
332 >            () -> ns.tailSet(BigInteger.ONE, true),
333              description + ": Expansion should not be allowed");
334  
335          // much smaller
# Line 359 | Line 341 | public class EmptyNavigableSet {
341       */
342      @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
343      public void testTailSet(String description, NavigableSet navigableSet) {
344 <        assertThrows(() -> {
345 <            navigableSet.tailSet(null);
364 <        },
365 <            NullPointerException.class,
344 >        assertThrowsNPE(
345 >            () -> navigableSet.tailSet(null),
346              description + ": Must throw NullPointerException for null element");
347  
348 <        assertThrows(() -> {
349 <            navigableSet.tailSet(new Object());
350 <        }, ClassCastException.class);
348 >        assertThrowsCCE(
349 >            () -> navigableSet.tailSet(new Object()),
350 >            description);
351  
352          NavigableSet ss = navigableSet.tailSet("1", true);
353  
# Line 389 | Line 369 | public class EmptyNavigableSet {
369  
370          assertSame(emptyNavigableSetArray, result);
371  
372 <        assertTrue(result[0] == null);
372 >        assertNull(result[0]);
373      }
374  
375      @DataProvider(name = "NavigableSet<?>", parallel = true)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines