1 /*
2  * Copyright (c) 1999, 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
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25    @test
26    @bug 4251519 4251520
27    @summary indexOf and lastIndex of used to let you look outside the
28             valid range in the backing array
29  */
30 
31 import java.util.*;
32 
33 public class HeadTailTypeError {
main(String argv[])34     public static void main(String argv[]) throws Exception {
35         try{
36             SortedMap m = new TreeMap();
37             m.headMap(new Object());
38             throw new Exception("headMap, natural ordering");
39         } catch (ClassCastException e) {
40         }
41 
42         try{
43             SortedMap m = new TreeMap();
44             m.tailMap(new Object());
45             throw new Exception("tailMap, natural ordering");
46         } catch (ClassCastException e) {
47         }
48 
49 
50         try{
51             SortedMap m = new TreeMap(String.CASE_INSENSITIVE_ORDER);
52             m.headMap(new Integer(0));
53             throw new Exception("headMap, explicit comparator");
54         } catch (ClassCastException e) {
55         }
56 
57         try{
58             SortedMap m = new TreeMap(String.CASE_INSENSITIVE_ORDER);
59             m.tailMap(new Integer(0));
60             throw new Exception("tailMap, explicit comparator");
61         } catch (ClassCastException e) {
62         }
63 
64         try{
65             SortedSet m = new TreeSet();
66             m.headSet(new Object());
67             throw new Exception("headSet, natural ordering");
68         } catch (ClassCastException e) {
69         }
70 
71         try{
72             SortedSet m = new TreeSet();
73             m.tailSet(new Object());
74             throw new Exception("tailSet, natural ordering");
75         } catch (ClassCastException e) {
76         }
77 
78         try{
79             SortedSet m = new TreeSet(String.CASE_INSENSITIVE_ORDER);
80             m.headSet(new Integer(0));
81             throw new Exception("headSet, explicit comparator");
82         } catch (ClassCastException e) {
83         }
84 
85         try{
86             SortedSet m = new TreeSet(String.CASE_INSENSITIVE_ORDER);
87             m.tailSet(new Integer(0));
88             throw new Exception("tailSet, explicit comparator");
89         } catch (ClassCastException e) {
90         }
91 
92         try{
93             SortedMap m = new TreeMap();
94             m.headMap(null);
95             throw new Exception("(null endpoint)headMap, natural ordering");
96         } catch (NullPointerException e) {
97         }
98 
99         try{
100             SortedMap m = new TreeMap();
101             m.tailMap(null);
102             throw new Exception("(null endpoint)tailMap, natural ordering");
103         } catch (NullPointerException e) {
104         }
105 
106 
107         try{
108             SortedMap m = new TreeMap(String.CASE_INSENSITIVE_ORDER);
109             m.headMap(null);
110             throw new Exception("(null endpoint)headMap, explicit comparator");
111         } catch (NullPointerException e) {
112         }
113 
114         try{
115             SortedMap m = new TreeMap(String.CASE_INSENSITIVE_ORDER);
116             m.tailMap(null);
117             throw new Exception("(null endpoint)tailMap, explicit comparator");
118         } catch (NullPointerException e) {
119         }
120 
121         try{
122             SortedSet m = new TreeSet();
123             m.headSet(null);
124             throw new Exception("(null endpoint)headSet, natural ordering");
125         } catch (NullPointerException e) {
126         }
127 
128         try{
129             SortedSet m = new TreeSet();
130             m.tailSet(null);
131             throw new Exception("(null endpoint)tailSet, natural ordering");
132         } catch (NullPointerException e) {
133         }
134 
135         try{
136             SortedSet m = new TreeSet(String.CASE_INSENSITIVE_ORDER);
137             m.headSet(null);
138             throw new Exception("(null endpoint)headSet, explicit comparator");
139         } catch (NullPointerException e) {
140         }
141 
142         try{
143             SortedSet m = new TreeSet(String.CASE_INSENSITIVE_ORDER);
144             m.tailSet(null);
145             throw new Exception("(null endpoint)tailSet, explicit comparator");
146         } catch (NullPointerException e) {
147         }
148 
149         // These should not fail
150         SortedMap m = new TreeMap();
151         m.headMap(new Integer(0));
152         m.tailMap(new Integer(0));
153         m = new TreeMap(String.CASE_INSENSITIVE_ORDER);
154         m.headMap("llama");
155         m.tailMap("llama");
156 
157         SortedSet s = new TreeSet();
158         s.headSet(new Integer(0));
159         s.tailSet(new Integer(0));
160         s = new TreeSet(String.CASE_INSENSITIVE_ORDER);
161         s.headSet("drama");
162         s.tailSet("drama");
163     }
164 }
165