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

Comparing jsr166/src/test/jtreg/util/Collections/EmptyNavigableMap.java (file contents):
Revision 1.5 by jsr166, Tue May 2 14:15:31 2017 UTC vs.
Revision 1.6 by jsr166, Mon May 15 21:39:24 2017 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 36 | Line 36 | import java.util.Iterator;
36   import java.util.NavigableMap;
37   import java.util.SortedMap;
38   import java.util.TreeMap;
39 +
40 + import org.testng.Assert;
41 + import org.testng.Assert.ThrowingRunnable;
42   import org.testng.annotations.Test;
43   import org.testng.annotations.DataProvider;
44  
42 import static org.testng.Assert.fail;
45   import static org.testng.Assert.assertTrue;
46   import static org.testng.Assert.assertFalse;
47  
# Line 65 | Line 67 | public class EmptyNavigableMap {
67              ((null != message) ? message : "") + " Not empty. ");
68      }
69  
70 <    public interface Thrower<T extends Throwable> {
71 <
72 <        public void run() throws T;
70 >    private <T extends Throwable> void assertThrows(Class<T> throwableClass,
71 >                                                    ThrowingRunnable runnable,
72 >                                                    String message) {
73 >        try {
74 >            Assert.assertThrows(throwableClass, runnable);
75 >        } catch (AssertionError e) {
76 >            throw new AssertionError(String.format("%s%n%s",
77 >                    ((null != message) ? message : ""), e.getMessage()), e);
78 >        }
79      }
80  
81 <    public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) {
82 <        assertThrows(thrower, throwable, null);
81 >    private void assertThrowsCCE(ThrowingRunnable r, String s) {
82 >        assertThrows(ClassCastException.class, r, s);
83      }
84  
85 <    public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable, String message) {
86 <        Throwable result;
87 <        try {
80 <            thrower.run();
81 <            fail(((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");
82 <            return;
83 <        } catch (Throwable caught) {
84 <            result = caught;
85 <        }
85 >    private void assertThrowsNPE(ThrowingRunnable r, String s) {
86 >        assertThrows(NullPointerException.class, r, s);
87 >    }
88  
89 <        assertInstance(result, throwable, ((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");
89 >    private void assertThrowsIAE(ThrowingRunnable r, String s) {
90 >        assertThrows(IllegalArgumentException.class, r, s);
91      }
92  
93      public static final boolean isDescending(SortedMap<?,?> set) {
# Line 121 | Line 124 | public class EmptyNavigableMap {
124       */
125      @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
126      public void testContainsRequiresComparable(String description, NavigableMap<?,?> navigableMap) {
127 <        assertThrows(() -> {
127 >        assertThrowsCCE(() -> {
128              navigableMap.containsKey(new Object());
129          },
130 <            ClassCastException.class,
128 <            description + ": Comparable should be required");
130 >            description + ": Compareable should be required");
131      }
132  
133      /**
# Line 175 | Line 177 | public class EmptyNavigableMap {
177       */
178      @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
179      public void testHeadMap(String description, NavigableMap navigableMap) {
180 <        assertThrows(
180 >        assertThrowsNPE(
181              () -> { NavigableMap ss = navigableMap.headMap(null, false); },
180            NullPointerException.class,
182              description + ": Must throw NullPointerException for null element");
183  
184 <        assertThrows(
184 >        assertThrowsCCE(
185              () -> { NavigableMap ss = navigableMap.headMap(new Object(), true); },
185            ClassCastException.class,
186              description + ": Must throw ClassCastException for non-Comparable element");
187  
188          NavigableMap ss = navigableMap.headMap("1", false);
# Line 203 | Line 203 | public class EmptyNavigableMap {
203       */
204      @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
205      public void testSubMap(String description, NavigableMap navigableMap) {
206 <        assertThrows(
206 >        assertThrowsNPE(
207              () -> {
208                  SortedMap ss = navigableMap.subMap(null, BigInteger.TEN);
209              },
210            NullPointerException.class,
210              description + ": Must throw NullPointerException for null element");
211  
212 <        assertThrows(
212 >        assertThrowsNPE(
213              () -> {
214                  SortedMap ss = navigableMap.subMap(BigInteger.ZERO, null);
215              },
217            NullPointerException.class,
216              description + ": Must throw NullPointerException for null element");
217  
218 <        assertThrows(
218 >        assertThrowsNPE(
219              () -> {
220                  SortedMap ss = navigableMap.subMap(null, null);
221              },
224            NullPointerException.class,
222              description + ": Must throw NullPointerException for null element");
223  
224          Object obj1 = new Object();
225          Object obj2 = new Object();
226  
227 <        assertThrows(
227 >        assertThrowsCCE(
228              () -> {
229                  SortedMap ss = navigableMap.subMap(obj1, BigInteger.TEN);
230              },
231 <            ClassCastException.class, description
235 <            + ": Must throw ClassCastException for parameter which is not Comparable.");
231 >            description + ": Must throw ClassCastException for parameter which is not Comparable.");
232  
233 <        assertThrows(
233 >        assertThrowsCCE(
234              () -> {
235                  SortedMap ss = navigableMap.subMap(BigInteger.ZERO, obj2);
236              },
237 <            ClassCastException.class, description
242 <            + ": Must throw ClassCastException for parameter which is not Comparable.");
237 >            description + ": Must throw ClassCastException for parameter which is not Comparable.");
238  
239 <        assertThrows(
239 >        assertThrowsCCE(
240              () -> {
241                  SortedMap ss = navigableMap.subMap(obj1, obj2);
242              },
243 <            ClassCastException.class, description
249 <            + ": Must throw ClassCastException for parameter which is not Comparable.");
243 >            description + ": Must throw ClassCastException for parameter which is not Comparable.");
244  
245          // minimal range
246          navigableMap.subMap(BigInteger.ZERO, false, BigInteger.ZERO, false);
# Line 257 | Line 251 | public class EmptyNavigableMap {
251          Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO;
252          Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;
253  
254 <            assertThrows(
254 >            assertThrowsIAE(
255                  () -> {
256                      navigableMap.subMap(last, true, first, false);
257                  },
258 <                IllegalArgumentException.class, description
265 <                + ": Must throw IllegalArgumentException when fromElement is not less than toElement.");
258 >                description + ": Must throw IllegalArgumentException when fromElement is not less than toElement.");
259  
260          navigableMap.subMap(first, true, last, false);
261      }
# Line 280 | Line 273 | public class EmptyNavigableMap {
273          // slightly smaller
274          NavigableMap ns = subMap.subMap(first, false, last, false);
275          // slight expansion
276 <        assertThrows(() -> {
276 >        assertThrowsIAE(() -> {
277              ns.subMap(first, true, last, true);
278          },
286            IllegalArgumentException.class,
279              description + ": Expansion should not be allowed");
280  
281          // much smaller
# Line 301 | Line 293 | public class EmptyNavigableMap {
293          NavigableMap ns = subMap.headMap(BigInteger.ONE, false);
294  
295          // slight expansion
296 <        assertThrows(() -> {
296 >        assertThrowsIAE(() -> {
297              ns.headMap(BigInteger.ONE, true);
298          },
307            IllegalArgumentException.class,
299              description + ": Expansion should not be allowed");
300  
301          // much smaller
# Line 322 | Line 313 | public class EmptyNavigableMap {
313          NavigableMap ns = subMap.tailMap(BigInteger.ONE, false);
314  
315          // slight expansion
316 <        assertThrows(() -> {
316 >        assertThrowsIAE(() -> {
317              ns.tailMap(BigInteger.ONE, true);
318          },
328            IllegalArgumentException.class,
319              description + ": Expansion should not be allowed");
320  
321          // much smaller
# Line 337 | Line 327 | public class EmptyNavigableMap {
327       */
328      @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
329      public void testTailMap(String description, NavigableMap navigableMap) {
330 <        assertThrows(() -> {
330 >        assertThrowsNPE(() -> {
331              navigableMap.tailMap(null);
332          },
343            NullPointerException.class,
333              description + ": Must throw NullPointerException for null element");
334  
335 <        assertThrows(() -> {
335 >        assertThrowsCCE(() -> {
336              navigableMap.tailMap(new Object());
337 <        }, ClassCastException.class);
337 >        },
338 >            description);
339  
340          NavigableMap ss = navigableMap.tailMap("1", true);
341  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines