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.1 by jsr166, Wed Sep 9 22:33:33 2015 UTC vs.
Revision 1.10 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.emptyNavigableMap
28   * @run testng EmptyNavigableMap
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.NavigableMap;
43   import java.util.SortedMap;
44   import java.util.TreeMap;
40 import org.testng.annotations.Test;
41 import org.testng.annotations.DataProvider;
45  
43 import static org.testng.Assert.fail;
44 import static org.testng.Assert.assertEquals;
45 import static org.testng.Assert.assertTrue;
46   import static org.testng.Assert.assertFalse;
47 < import static org.testng.Assert.assertSame;
47 > import static org.testng.Assert.assertTrue;
48  
49   public class EmptyNavigableMap {
50  
51      public static <T> void assertInstance(T actual, Class<? extends T> expected) {
52 <        assertInstance(expected.isInstance(actual), null);
52 >        assertInstance(actual, expected, null);
53      }
54  
55      public static <T> void assertInstance(T actual, Class<? extends T> expected, String message) {
# Line 68 | Line 68 | public class EmptyNavigableMap {
68              ((null != message) ? message : "") + " Not empty. ");
69      }
70  
71 <    public interface Thrower<T extends Throwable> {
72 <
73 <        public void run() throws T;
71 >    private <T extends Throwable> void assertThrows(Class<T> throwableClass,
72 >                                                    ThrowingRunnable runnable,
73 >                                                    String message) {
74 >        try {
75 >            Assert.assertThrows(throwableClass, runnable);
76 >        } catch (AssertionError e) {
77 >            throw new AssertionError(String.format("%s%n%s",
78 >                    ((null != message) ? message : ""), e.getMessage()), e);
79 >        }
80      }
81  
82 <    public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) {
83 <        assertThrows(thrower, throwable, null);
82 >    private void assertThrowsCCE(ThrowingRunnable r, String s) {
83 >        assertThrows(ClassCastException.class, r, s);
84      }
85  
86 <    public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable, String message) {
87 <        Throwable result;
88 <        try {
83 <            thrower.run();
84 <            fail(((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");
85 <            return;
86 <        } catch (Throwable caught) {
87 <            result = caught;
88 <        }
86 >    private void assertThrowsNPE(ThrowingRunnable r, String s) {
87 >        assertThrows(NullPointerException.class, r, s);
88 >    }
89  
90 <        assertInstance(result, throwable, ((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");
90 >    private void assertThrowsIAE(ThrowingRunnable r, String s) {
91 >        assertThrows(IllegalArgumentException.class, r, s);
92      }
93  
94      public static final boolean isDescending(SortedMap<?,?> set) {
# Line 124 | Line 125 | public class EmptyNavigableMap {
125       */
126      @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
127      public void testContainsRequiresComparable(String description, NavigableMap<?,?> navigableMap) {
128 <        assertThrows(() -> {
129 <            navigableMap.containsKey(new Object());
130 <        },
130 <            ClassCastException.class,
131 <            description + ": Compareable should be required");
128 >        assertThrowsCCE(
129 >            () -> navigableMap.containsKey(new Object()),
130 >            description + ": Comparable should be required");
131      }
132  
133      /**
# Line 178 | 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); },
183            NullPointerException.class,
182              description + ": Must throw NullPointerException for null element");
183  
184 <        assertThrows(
184 >        assertThrowsCCE(
185              () -> { NavigableMap ss = navigableMap.headMap(new Object(), true); },
188            ClassCastException.class,
186              description + ": Must throw ClassCastException for non-Comparable element");
187  
188          NavigableMap ss = navigableMap.headMap("1", false);
# Line 206 | 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              },
213            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              },
220            NullPointerException.class,
216              description + ": Must throw NullPointerException for null element");
217  
218 <        assertThrows(
218 >        assertThrowsNPE(
219              () -> {
220                  SortedMap ss = navigableMap.subMap(null, null);
221              },
227            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
238 <            + ": 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
245 <            + ": 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
252 <            + ": 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 260 | 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(
255 <                () -> {
256 <                    navigableMap.subMap(last, true, first, false);
266 <                },
267 <                IllegalArgumentException.class, description
268 <                + ": Must throw IllegalArgumentException when fromElement is not less then then toElement.");
254 >            assertThrowsIAE(
255 >                () -> navigableMap.subMap(last, true, first, false),
256 >                description + ": Must throw IllegalArgumentException when fromElement is not less than toElement.");
257  
258          navigableMap.subMap(first, true, last, false);
259      }
# Line 282 | Line 270 | public class EmptyNavigableMap {
270  
271          // slightly smaller
272          NavigableMap ns = subMap.subMap(first, false, last, false);
273 <        // slight exapansion
274 <        assertThrows(() -> {
275 <            ns.subMap(first, true, last, true);
288 <        },
289 <            IllegalArgumentException.class,
273 >        // slight expansion
274 >        assertThrowsIAE(
275 >            () -> ns.subMap(first, true, last, true),
276              description + ": Expansion should not be allowed");
277  
278          // much smaller
# Line 303 | Line 289 | public class EmptyNavigableMap {
289          // slightly smaller
290          NavigableMap ns = subMap.headMap(BigInteger.ONE, false);
291  
292 <        // slight exapansion
293 <        assertThrows(() -> {
294 <            ns.headMap(BigInteger.ONE, true);
309 <        },
310 <            IllegalArgumentException.class,
292 >        // slight expansion
293 >        assertThrowsIAE(
294 >            () -> ns.headMap(BigInteger.ONE, true),
295              description + ": Expansion should not be allowed");
296  
297          // much smaller
# Line 324 | Line 308 | public class EmptyNavigableMap {
308          // slightly smaller
309          NavigableMap ns = subMap.tailMap(BigInteger.ONE, false);
310  
311 <        // slight exapansion
312 <        assertThrows(() -> {
313 <            ns.tailMap(BigInteger.ONE, true);
330 <        },
331 <            IllegalArgumentException.class,
311 >        // slight expansion
312 >        assertThrowsIAE(
313 >            () -> ns.tailMap(BigInteger.ONE, true),
314              description + ": Expansion should not be allowed");
315  
316          // much smaller
# Line 340 | Line 322 | public class EmptyNavigableMap {
322       */
323      @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
324      public void testTailMap(String description, NavigableMap navigableMap) {
325 <        assertThrows(() -> {
326 <            navigableMap.tailMap(null);
345 <        },
346 <            NullPointerException.class,
325 >        assertThrowsNPE(
326 >            () -> navigableMap.tailMap(null),
327              description + ": Must throw NullPointerException for null element");
328  
329 <        assertThrows(() -> {
330 <            navigableMap.tailMap(new Object());
331 <        }, ClassCastException.class);
329 >        assertThrowsCCE(
330 >            () -> navigableMap.tailMap(new Object()),
331 >            description);
332  
333          NavigableMap ss = navigableMap.tailMap("1", true);
334  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines