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.14 by jsr166, Mon Jul 23 01:11:22 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.assertNull;
49   import static org.testng.Assert.assertSame;
50   import static org.testng.Assert.assertTrue;
51  
52   public class EmptyNavigableSet {
53  
54      public static <T> void assertInstance(T actual, Class<? extends T> expected) {
55 <        assertInstance(expected.isInstance(actual), null);
55 >        assertInstance(actual, expected, null);
56      }
57  
58      public static <T> void assertInstance(T actual, Class<? extends T> expected, String message) {
# Line 67 | Line 71 | public class EmptyNavigableSet {
71              ((null != message) ? message : "") + " Not empty. ");
72      }
73  
74 <    public interface Thrower<T extends Throwable> {
74 >    private <T extends Throwable> void assertThrows(Class<T> throwableClass,
75 >                                                    ThrowingRunnable runnable,
76 >                                                    String message) {
77 >        try {
78 >            Assert.assertThrows(throwableClass, runnable);
79 >        } catch (AssertionError e) {
80 >            throw new AssertionError(String.format("%s%n%s",
81 >                    ((null != message) ? message : ""), e.getMessage()), e);
82 >        }
83 >    }
84  
85 <        public void run() throws T;
85 >    private void assertThrowsCCE(ThrowingRunnable r, String s) {
86 >        assertThrows(ClassCastException.class, r, s);
87      }
88  
89 <    public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) {
90 <        assertThrows(thrower, throwable, null);
89 >    private void assertThrowsNPE(ThrowingRunnable r, String s) {
90 >        assertThrows(NullPointerException.class, r, s);
91      }
92  
93 <    public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable, String message) {
94 <        Throwable result;
95 <        try {
82 <            thrower.run();
83 <            fail(((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");
84 <            return;
85 <        } catch (Throwable caught) {
86 <            result = caught;
87 <        }
93 >    private void assertThrowsIAE(ThrowingRunnable r, String s) {
94 >        assertThrows(IllegalArgumentException.class, r, s);
95 >    }
96  
97 <        assertInstance(result, throwable, ((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");
97 >    private void assertThrowsNSEE(ThrowingRunnable r, String s) {
98 >        assertThrows(NoSuchElementException.class, r, s);
99      }
100  
101      public static final boolean isDescending(SortedSet<?> set) {
# Line 123 | Line 132 | public class EmptyNavigableSet {
132       */
133      @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
134      public void testContainsRequiresComparable(String description, NavigableSet<?> navigableSet) {
135 <        assertThrows(() -> {
136 <            navigableSet.contains(new Object());
128 <        },
129 <            ClassCastException.class,
135 >        assertThrowsCCE(
136 >            () -> navigableSet.contains(new Object()),
137              description + ": Comparable should be required");
138      }
139  
# Line 157 | Line 164 | public class EmptyNavigableSet {
164       */
165      @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
166      public void testEmptyIterator(String description, NavigableSet<?> navigableSet) {
167 <        Iterator emptyIterator = navigableSet.iterator();
161 <
162 <        assertFalse((emptyIterator != null) && (emptyIterator.hasNext()),
163 <            "The iterator is not empty.");
167 >        assertFalse(navigableSet.iterator().hasNext(), "The iterator is not empty.");
168      }
169  
170      /**
# Line 176 | Line 180 | public class EmptyNavigableSet {
180       */
181      @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
182      public void testFirst(String description, NavigableSet<?> navigableSet) {
183 <        assertThrows(() -> {
180 <            navigableSet.first();
181 <        }, NoSuchElementException.class, description);
183 >        assertThrowsNSEE(navigableSet::first, description);
184      }
185  
186      /**
# Line 186 | Line 188 | public class EmptyNavigableSet {
188       */
189      @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
190      public void testHeadSet(String description, NavigableSet navigableSet) {
191 <        assertThrows(
191 >        assertThrowsNPE(
192              () -> { NavigableSet ns = navigableSet.headSet(null, false); },
191            NullPointerException.class,
193              description + ": Must throw NullPointerException for null element");
194  
195 <        assertThrows(
195 >        assertThrowsCCE(
196              () -> { NavigableSet ns = navigableSet.headSet(new Object(), true); },
196            ClassCastException.class,
197              description + ": Must throw ClassCastException for non-Comparable element");
198  
199          NavigableSet ns = navigableSet.headSet("1", false);
# Line 206 | Line 206 | public class EmptyNavigableSet {
206       */
207      @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
208      public void testLast(String description, NavigableSet<?> navigableSet) {
209 <        assertThrows(() -> {
210 <            navigableSet.last();
211 <        }, NoSuchElementException.class, description);
209 >        assertThrowsNSEE(navigableSet::last, description);
210      }
211  
212      /**
# Line 224 | Line 222 | public class EmptyNavigableSet {
222       */
223      @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
224      public void testSubSet(String description, NavigableSet navigableSet) {
225 <        assertThrows(
225 >        assertThrowsNPE(
226              () -> {
227                  SortedSet ss = navigableSet.subSet(null, BigInteger.TEN);
228              },
231            NullPointerException.class,
229              description + ": Must throw NullPointerException for null element");
230  
231 <        assertThrows(
231 >        assertThrowsNPE(
232              () -> {
233                  SortedSet ss = navigableSet.subSet(BigInteger.ZERO, null);
234              },
238            NullPointerException.class,
235              description + ": Must throw NullPointerException for null element");
236  
237 <        assertThrows(
237 >        assertThrowsNPE(
238              () -> {
239                  SortedSet ss = navigableSet.subSet(null, null);
240              },
245            NullPointerException.class,
241              description + ": Must throw NullPointerException for null element");
242  
243          Object obj1 = new Object();
244          Object obj2 = new Object();
245  
246 <        assertThrows(
246 >        assertThrowsCCE(
247              () -> {
248                  SortedSet ss = navigableSet.subSet(obj1, BigInteger.TEN);
249              },
250 <            ClassCastException.class, description
256 <            + ": Must throw ClassCastException for parameter which is not Comparable.");
250 >            description + ": Must throw ClassCastException for parameter which is not Comparable.");
251  
252 <        assertThrows(
252 >        assertThrowsCCE(
253              () -> {
254                  SortedSet ss = navigableSet.subSet(BigInteger.ZERO, obj2);
255              },
256 <            ClassCastException.class, description
263 <            + ": Must throw ClassCastException for parameter which is not Comparable.");
256 >            description + ": Must throw ClassCastException for parameter which is not Comparable.");
257  
258 <        assertThrows(
258 >        assertThrowsCCE(
259              () -> {
260                  SortedSet ss = navigableSet.subSet(obj1, obj2);
261              },
262 <            ClassCastException.class, description
270 <            + ": Must throw ClassCastException for parameter which is not Comparable.");
262 >            description + ": Must throw ClassCastException for parameter which is not Comparable.");
263  
264          // minimal range
265          navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, false);
# Line 278 | Line 270 | public class EmptyNavigableSet {
270          Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;
271          Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;
272  
273 <            assertThrows(
274 <                () -> {
275 <                    navigableSet.subSet(last, true, first, false);
284 <                },
285 <                IllegalArgumentException.class, description
273 >            assertThrowsIAE(
274 >                () -> navigableSet.subSet(last, true, first, false),
275 >                description
276                  + ": Must throw IllegalArgumentException when fromElement is not less than toElement.");
277  
278          navigableSet.subSet(first, true, last, false);
# Line 301 | Line 291 | public class EmptyNavigableSet {
291          // slightly smaller
292          NavigableSet ns = subSet.subSet(first, false, last, false);
293          // slight expansion
294 <        assertThrows(() -> {
295 <            ns.subSet(first, true, last, true);
306 <        },
307 <            IllegalArgumentException.class,
294 >        assertThrowsIAE(
295 >            () -> ns.subSet(first, true, last, true),
296              description + ": Expansion should not be allowed");
297  
298          // much smaller
# Line 322 | Line 310 | public class EmptyNavigableSet {
310          NavigableSet ns = subSet.headSet(BigInteger.ONE, false);
311  
312          // slight expansion
313 <        assertThrows(() -> {
314 <            ns.headSet(BigInteger.ONE, true);
327 <        },
328 <            IllegalArgumentException.class,
313 >        assertThrowsIAE(
314 >            () -> ns.headSet(BigInteger.ONE, true),
315              description + ": Expansion should not be allowed");
316  
317          // much smaller
# Line 343 | Line 329 | public class EmptyNavigableSet {
329          NavigableSet ns = subSet.tailSet(BigInteger.ONE, false);
330  
331          // slight expansion
332 <        assertThrows(() -> {
333 <            ns.tailSet(BigInteger.ONE, true);
348 <        },
349 <            IllegalArgumentException.class,
332 >        assertThrowsIAE(
333 >            () -> ns.tailSet(BigInteger.ONE, true),
334              description + ": Expansion should not be allowed");
335  
336          // much smaller
# Line 358 | Line 342 | public class EmptyNavigableSet {
342       */
343      @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
344      public void testTailSet(String description, NavigableSet navigableSet) {
345 <        assertThrows(() -> {
346 <            navigableSet.tailSet(null);
363 <        },
364 <            NullPointerException.class,
345 >        assertThrowsNPE(
346 >            () -> navigableSet.tailSet(null),
347              description + ": Must throw NullPointerException for null element");
348  
349 <        assertThrows(() -> {
350 <            navigableSet.tailSet(new Object());
351 <        }, ClassCastException.class);
349 >        assertThrowsCCE(
350 >            () -> navigableSet.tailSet(new Object()),
351 >            description);
352  
353          NavigableSet ss = navigableSet.tailSet("1", true);
354  
# Line 388 | Line 370 | public class EmptyNavigableSet {
370  
371          assertSame(emptyNavigableSetArray, result);
372  
373 <        assertTrue(result[0] == null);
373 >        assertNull(result[0]);
374      }
375  
376      @DataProvider(name = "NavigableSet<?>", parallel = true)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines