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.SortedMap;
32 import java.util.SortedSet;
33 import java.util.TreeMap;
34 import java.util.TreeSet;
35 
36 public class HeadTailTypeError {
main(String[] args)37     public static void main(String[] args) throws Exception {
38         try {
39             SortedMap m = new TreeMap();
40             m.headMap(new Object());
41             throw new Exception("headMap, natural ordering");
42         } catch (ClassCastException e) {
43         }
44 
45         try {
46             SortedMap m = new TreeMap();
47             m.tailMap(new Object());
48             throw new Exception("tailMap, natural ordering");
49         } catch (ClassCastException e) {
50         }
51 
52         try {
53             SortedMap m = new TreeMap(String.CASE_INSENSITIVE_ORDER);
54             m.headMap(new Integer(0));
55             throw new Exception("headMap, explicit comparator");
56         } catch (ClassCastException e) {
57         }
58 
59         try {
60             SortedMap m = new TreeMap(String.CASE_INSENSITIVE_ORDER);
61             m.tailMap(new Integer(0));
62             throw new Exception("tailMap, explicit comparator");
63         } catch (ClassCastException e) {
64         }
65 
66         try {
67             SortedSet m = new TreeSet();
68             m.headSet(new Object());
69             throw new Exception("headSet, natural ordering");
70         } catch (ClassCastException e) {
71         }
72 
73         try {
74             SortedSet m = new TreeSet();
75             m.tailSet(new Object());
76             throw new Exception("tailSet, natural ordering");
77         } catch (ClassCastException e) {
78         }
79 
80         try {
81             SortedSet m = new TreeSet(String.CASE_INSENSITIVE_ORDER);
82             m.headSet(new Integer(0));
83             throw new Exception("headSet, explicit comparator");
84         } catch (ClassCastException e) {
85         }
86 
87         try {
88             SortedSet m = new TreeSet(String.CASE_INSENSITIVE_ORDER);
89             m.tailSet(new Integer(0));
90             throw new Exception("tailSet, explicit comparator");
91         } catch (ClassCastException e) {
92         }
93 
94         try {
95             SortedMap m = new TreeMap();
96             m.headMap(null);
97             throw new Exception("(null endpoint)headMap, natural ordering");
98         } catch (NullPointerException e) {
99         }
100 
101         try {
102             SortedMap m = new TreeMap();
103             m.tailMap(null);
104             throw new Exception("(null endpoint)tailMap, natural ordering");
105         } catch (NullPointerException e) {
106         }
107 
108 
109         try {
110             SortedMap m = new TreeMap(String.CASE_INSENSITIVE_ORDER);
111             m.headMap(null);
112             throw new Exception("(null endpoint)headMap, explicit comparator");
113         } catch (NullPointerException e) {
114         }
115 
116         try {
117             SortedMap m = new TreeMap(String.CASE_INSENSITIVE_ORDER);
118             m.tailMap(null);
119             throw new Exception("(null endpoint)tailMap, explicit comparator");
120         } catch (NullPointerException e) {
121         }
122 
123         try {
124             SortedSet m = new TreeSet();
125             m.headSet(null);
126             throw new Exception("(null endpoint)headSet, natural ordering");
127         } catch (NullPointerException e) {
128         }
129 
130         try {
131             SortedSet m = new TreeSet();
132             m.tailSet(null);
133             throw new Exception("(null endpoint)tailSet, natural ordering");
134         } catch (NullPointerException e) {
135         }
136 
137         try {
138             SortedSet m = new TreeSet(String.CASE_INSENSITIVE_ORDER);
139             m.headSet(null);
140             throw new Exception("(null endpoint)headSet, explicit comparator");
141         } catch (NullPointerException e) {
142         }
143 
144         try {
145             SortedSet m = new TreeSet(String.CASE_INSENSITIVE_ORDER);
146             m.tailSet(null);
147             throw new Exception("(null endpoint)tailSet, explicit comparator");
148         } catch (NullPointerException e) {
149         }
150 
151         // These should not fail
152         SortedMap m = new TreeMap();
153         m.headMap(new Integer(0));
154         m.tailMap(new Integer(0));
155         m = new TreeMap(String.CASE_INSENSITIVE_ORDER);
156         m.headMap("llama");
157         m.tailMap("llama");
158 
159         SortedSet s = new TreeSet();
160         s.headSet(new Integer(0));
161         s.tailSet(new Integer(0));
162         s = new TreeSet(String.CASE_INSENSITIVE_ORDER);
163         s.headSet("drama");
164         s.tailSet("drama");
165     }
166 }
167