KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > toolkits > exceptions > ThrowableSetTest


1 package soot.toolkits.exceptions;
2
3 import soot.*;
4 import java.util.*;
5 import junit.framework.Test;
6 import junit.framework.TestCase;
7 import junit.framework.TestSuite;
8 import junit.extensions.TestSetup;
9
10 import soot.toolkits.exceptions.ExceptionTestUtility.*;
11
12 public class ThrowableSetTest extends TestCase {
13
14     final static boolean DUMP_INTERNALS = false;
15     final ThrowableSet.Manager mgr = ThrowableSet.Manager.v();
16
17
18     // A class for verifying that the sizeToSetsMap
19
// follows our expectations.
20
static class ExpectedSizeToSets {
21     private Map expectedMap = new HashMap(); // from Integer to Set.
22

23     private static class SetPair {
24         Set included;
25         Set excluded;
26
27         SetPair(Set included, Set excluded) {
28         this.included = included;
29         this.excluded = excluded;
30         }
31
32         public boolean equals(Object JavaDoc o) {
33         if (o == this) {
34             return true;
35         }
36         if (! (o instanceof SetPair)) {
37             return false;
38         }
39         SetPair sp = (SetPair) o;
40         return ( this.included.equals(sp.included)
41             && this.excluded.equals(sp.excluded));
42         }
43
44         public int hashCode() {
45         int result = 31;
46         result = 37 * result + included.hashCode();
47         result = 37 * result + excluded.hashCode();
48         return result;
49         }
50
51         public String JavaDoc toString() {
52         return ( super.toString()
53             + System.getProperty("line.separator")
54             + "+[" + included.toString() + ']'
55             + "-[" + excluded.toString() + ']');
56         }
57     }
58
59     ExpectedSizeToSets() {
60         // The empty set.
61
this.add(Collections.EMPTY_SET, Collections.EMPTY_SET);
62
63         // All Throwables set.
64
Set temp = new ExceptionHashSet();
65         temp.add(AnySubType.v(Scene.v().getRefType("java.lang.Throwable")));
66         this.add(temp, Collections.EMPTY_SET);
67     
68         // VM errors set.
69
temp = new ExceptionHashSet();
70         temp.add(Scene.v().getRefType("java.lang.InternalError"));
71         temp.add(Scene.v().getRefType("java.lang.OutOfMemoryError"));
72         temp.add(Scene.v().getRefType("java.lang.StackOverflowError"));
73         temp.add(Scene.v().getRefType("java.lang.UnknownError"));
74         temp.add(Scene.v().getRefType("java.lang.ThreadDeath"));
75         this.add(temp, Collections.EMPTY_SET);
76
77         // Resolve Class errors set.
78
Set classErrors = new ExceptionHashSet();
79         classErrors.add(Scene.v().getRefType("java.lang.ClassCircularityError"));
80         classErrors.add(AnySubType.v(Scene.v().getRefType("java.lang.ClassFormatError")));
81         classErrors.add(Scene.v().getRefType("java.lang.IllegalAccessError"));
82         classErrors.add(Scene.v().getRefType("java.lang.IncompatibleClassChangeError"));
83         classErrors.add(Scene.v().getRefType("java.lang.LinkageError"));
84         classErrors.add(Scene.v().getRefType("java.lang.NoClassDefFoundError"));
85         classErrors.add(Scene.v().getRefType("java.lang.VerifyError"));
86         this.add(classErrors, Collections.EMPTY_SET);
87
88         // Resolve Field errors set.
89
temp = new ExceptionHashSet(classErrors);
90         temp.add(Scene.v().getRefType("java.lang.NoSuchFieldError"));
91         this.add(temp, Collections.EMPTY_SET);
92
93         // Resolve method errors set.
94
temp = new ExceptionHashSet(classErrors);
95         temp.add(Scene.v().getRefType("java.lang.AbstractMethodError"));
96         temp.add(Scene.v().getRefType("java.lang.NoSuchMethodError"));
97         temp.add(Scene.v().getRefType("java.lang.UnsatisfiedLinkError"));
98         this.add(temp, Collections.EMPTY_SET);
99
100         // Initialization errors set.
101
temp = new ExceptionHashSet();
102         temp.add(AnySubType.v(Scene.v().getRefType("java.lang.Error")));
103         this.add(temp, Collections.EMPTY_SET);
104     }
105
106     void add(Set inclusions, Set exclusions) {
107         Integer JavaDoc sz = new Integer JavaDoc(inclusions.size() + exclusions.size());
108         Set values = (Set) expectedMap.get(sz);
109         if (values == null) {
110         values = new HashSet();
111         expectedMap.put(sz, values);
112         }
113         // Make sure we have our own copies of the sets.
114
values.add(new SetPair(new ExceptionHashSet(inclusions),
115                    new ExceptionHashSet(exclusions)));
116     }
117
118     void addAndCheck(Set inclusions, Set exclusions) {
119         this.add(inclusions, exclusions);
120         assertTrue(this.match());
121     }
122
123     boolean match() {
124         boolean result = true;
125         Map actualMap = ThrowableSet.Manager.v().getSizeToSets();
126         if (expectedMap.size() != actualMap.size()) {
127         result = false;
128         } else {
129         setloop:
130         for (Iterator i = expectedMap.keySet().iterator();
131              i.hasNext(); ) {
132             Integer JavaDoc key = (Integer JavaDoc) i.next();
133             Set expectedValues = (Set) expectedMap.get(key);
134
135             // To minimize restrictions on the contents of
136
// sizeToSets, use only the Collection interface
137
// to access its values:
138
Collection actualValues = (Collection) actualMap.get(key);
139         
140             if (expectedValues.size() != actualValues.size()) {
141             result = false;
142             break setloop;
143             }
144             for (Iterator j = actualValues.iterator(); j.hasNext(); ) {
145             ThrowableSet actual = (ThrowableSet) j.next();
146             SetPair actualPair
147                 = new SetPair(new ExceptionHashSet(actual.typesIncluded()),
148                       new ExceptionHashSet(actual.typesExcluded()));
149             if (! expectedValues.contains(actualPair)) {
150                 result = false;
151                 break setloop;
152             }
153             }
154         }
155         }
156         if (DUMP_INTERNALS) {
157         if (! result) System.err.println("!!!ExpectedSizeToSets.match() FAILED!!!");
158         System.err.println("expectedMap:");
159         System.err.println(expectedMap.toString());
160         System.err.println("actualMap:");
161         System.err.println(actualMap.toString());
162         System.err.flush();
163         }
164         return result;
165     }
166     }
167     private static ExpectedSizeToSets expectedSizeToSets;
168
169     // A class to check that memoized results match what we expect.
170
// Admittedly, this amounts to a reimplementation of the memoized
171
// structures within ThrowableSet -- I'm hoping that the two
172
// implementations will have different bugs!
173
static class ExpectedMemoizations {
174     Map throwableSetToMemoized = new HashMap();
175
176     void checkAdd(ThrowableSet lhs, Object JavaDoc rhs, ThrowableSet result) {
177         // rhs should be either a ThrowableSet or a RefType.
178
Map actualMemoized = lhs.getMemoizedAdds();
179         assertTrue(actualMemoized.get(rhs) == result);
180
181         Map expectedMemoized = (Map) throwableSetToMemoized.get(lhs);
182         if (expectedMemoized == null) {
183         expectedMemoized = new HashMap();
184         throwableSetToMemoized.put(lhs, expectedMemoized);
185         }
186         expectedMemoized.put(rhs, result);
187         assertEquals(expectedMemoized, actualMemoized);
188     }
189     }
190     private static ExpectedMemoizations expectedMemoizations;
191
192     private static String JavaDoc jdkLocation // Can be changed by main().
193
= "/usr/local/pkgs/jdk1.4.2_04/jre/lib/rt.jar";
194     private static ExceptionTestUtility util;
195
196     /**
197      * <code>ThrowableSetTestSetup</code> initializes the data structures
198      * that need to maintain their state throughout the test suite.
199      */

200     static class ThrowableSetTestSetup extends TestSetup {
201
202     public ThrowableSetTestSetup(Test test) {
203         super(test);
204     }
205
206     public void setUp() {
207         expectedSizeToSets = new ExpectedSizeToSets();
208         expectedMemoizations = new ExpectedMemoizations();
209         util = new ExceptionTestUtility(jdkLocation);
210     }
211     }
212
213
214     /**
215      * Asserts that the membership in the component sets of a
216      * ThrowableSet correspond to expectations.
217      *
218      * @param s The set to be checked.
219      *
220      * @param included the {@link Set} of RefLikeTypes
221      * expected to be in included in <code>s</code>.
222      *
223      * @param excluded an {@link Set} of RefLikeTypes
224      * expected to be excluded from <code>s</code>.
225      *
226      * @throws AssertionFailedError if <code>s</code> does not
227      * contain the types in <code>included</code> except for those
228      * in <code>excluded</code>.
229      */

230     public static void assertSameMembers(ThrowableSet s,
231                      Set included,
232                      Set excluded) {
233     assertTrue(ExceptionTestUtility.sameMembers(included, excluded, s));
234     }
235
236
237     /**
238      * Asserts that the membership in the component sets of a
239      * ThrowableSet correspond to expectations.
240      *
241      * @param s The set to be checked.
242      *
243      * @param included an array containing the RefLikeTypes
244      * expected to be in included in <code>s</code>.
245      *
246      * @param excluded an array containing the RefLikeTypes
247      * expected to be excluded from <code>s</code>.
248      *
249      * @throws AssertionFailedError if <code>s</code> does not
250      * contain the types in <code>included</code> except for those
251      * in <code>excluded</code>.
252      */

253     public static void assertSameMembers(ThrowableSet s,
254                      RefLikeType[] included,
255                      RefLikeType[] excluded) {
256     assertTrue(ExceptionTestUtility.sameMembers(
257                             new ExceptionHashSet(Arrays.asList(included)),
258                             new ExceptionHashSet(Arrays.asList(excluded)),
259            s));
260     }
261
262
263     /**
264      * Asserts that the membership in the component sets of a
265      * ThrowableSet.Pair correspond to expectations.
266      *
267      * @param p The pair to be checked.
268      *
269      * @param caughtIncluded the set of {@link RefLikeType}s
270      * expected to be in included in <code>p.getCaught()</code>.
271      *
272      * @param caughtExcluded the set of <code>RefLikeType</code>s
273      * expected to be excluded from <code>p.getCaught()</code>.
274      *
275      * @param uncaughtIncluded the set of <code>RefLikeType</code>s
276      * expected to be in included in <code>p.getUncaught()</code>.
277      *
278      * @param uncaughtExcluded the set of <code>RefLikeType</code>s
279      * expected to be excluded from <code>p.getUncaught()</code>.
280      *
281      * @throws AssertionFailedError if <code>s</code> does not
282      * contain the types in <code>included</code> except for those
283      * in <code>excluded</code>.
284      */

285     public static void assertSameMembers(ThrowableSet.Pair p,
286                      Set caughtIncluded,
287                      Set caughtExcluded,
288                      Set uncaughtIncluded,
289                      Set uncaughtExcluded) {
290     assertSameMembers(p.getCaught(), caughtIncluded, caughtExcluded);
291     assertSameMembers(p.getUncaught(), uncaughtIncluded, uncaughtExcluded);
292     }
293
294
295     /**
296      * Asserts that the membership in the component sets of a
297      * ThrowableSet.Pair correspond to expectations.
298      *
299      * @param p The pair to be checked.
300      *
301      * @param caughtIncluded an array containing the {@link RefLikeType}s
302      * expected to be in included in <code>p.getCaught()</code>.
303      *
304      * @param caughtExcluded an array containing the <code>RefLikeType</code>s
305      * expected to be excluded from <code>p.getCaught()</code>.
306      *
307      * @param uncaughtIncluded an array containing the <code>RefLikeType</code>s
308      * expected to be in included in <code>p.getUncaught()</code>.
309      *
310      * @param uncaughtExcluded an array containing the <code>RefLikeType</code>s
311      * expected to be excluded from <code>p.getUncaught()</code>.
312      *
313      * @throws AssertionFailedError if <code>s</code> does not
314      * contain the types in <code>included</code> except for those
315      * in <code>excluded</code>.
316      */

317     public static void assertSameMembers(ThrowableSet.Pair p,
318                      RefLikeType[] caughtIncluded,
319                      RefLikeType[] caughtExcluded,
320                      RefLikeType[] uncaughtIncluded,
321                      RefLikeType[] uncaughtExcluded) {
322     assertSameMembers(p.getCaught(), caughtIncluded, caughtExcluded);
323     assertSameMembers(p.getUncaught(), uncaughtIncluded, uncaughtExcluded);
324     }
325
326
327     private ThrowableSet checkAdd(ThrowableSet lhs, Object JavaDoc rhs,
328                   Set expectedIncluded, Set expectedExcluded,
329                   ThrowableSet actualResult) {
330     // Utility routine used by the next three add()s.
331

332     assertSameMembers(actualResult, expectedIncluded, expectedExcluded);
333     expectedSizeToSets.addAndCheck(expectedIncluded, expectedExcluded);
334     expectedMemoizations.checkAdd(lhs, rhs, actualResult);
335     return actualResult;
336     }
337
338     private ThrowableSet checkAdd(ThrowableSet lhs, Object JavaDoc rhs,
339               Set expectedResult, ThrowableSet actualResult) {
340     // Utility routine used by the next three add()s.
341
return checkAdd(lhs, rhs, expectedResult, Collections.EMPTY_SET,
342                actualResult);
343     }
344
345
346     private ThrowableSet add(ThrowableSet lhs, ThrowableSet rhs,
347                  Set expectedIncluded, Set expectedExcluded) {
348     // Add rhs to lhs, checking the results.
349

350     ThrowableSet actualResult = lhs.add(rhs);
351     return checkAdd(lhs, rhs, expectedIncluded, expectedExcluded,
352             actualResult);
353     }
354
355     private ThrowableSet add(ThrowableSet lhs, ThrowableSet rhs,
356                  Set expectedResult) {
357     // Add rhs to lhs, checking the results.
358
return add(lhs, rhs, expectedResult, Collections.EMPTY_SET);
359     }
360
361     private ThrowableSet add(ThrowableSet lhs, RefType rhs,
362                  Set expectedResult) {
363     // Add rhs to lhs, checking the results.
364

365     ThrowableSet actualResult = lhs.add(rhs);
366     return checkAdd(lhs, rhs, expectedResult, actualResult);
367     }
368
369     private ThrowableSet add(ThrowableSet lhs, AnySubType rhs,
370                  Set expectedResult) {
371     // Add rhs to lhs, checking the results.
372

373     ThrowableSet actualResult = lhs.add(rhs);
374     return checkAdd(lhs, rhs, expectedResult, actualResult);
375     }
376
377     public ThrowableSetTest(String JavaDoc name) {
378     super(name);
379     }
380
381
382     public void testInitialState() {
383     if (DUMP_INTERNALS) {
384         System.err.println("\n\ntestInitialState()");
385     }
386     assertTrue(expectedSizeToSets.match());
387     if (DUMP_INTERNALS) {
388         printAllSets();
389     }
390     }
391
392     public void testSingleInstance0() {
393     if (DUMP_INTERNALS) {
394         System.err.println("\n\ntestSingleInstance0()");
395     }
396     Set expected = new ExceptionHashSet(Arrays.asList(new RefType[] {
397         util.UNDECLARED_THROWABLE_EXCEPTION,
398     }));
399
400     ThrowableSet set0 = add(mgr.EMPTY, util.UNDECLARED_THROWABLE_EXCEPTION,
401                 expected);
402     ThrowableSet set1 = add(mgr.EMPTY, util.UNDECLARED_THROWABLE_EXCEPTION,
403                 expected);
404     assertTrue("The same ThrowableSet object should represent two sets containing the same single class.",
405            set0 == set1);
406
407     Set catchable = new ExceptionHashSet(Arrays.asList(new RefType[] {
408         util.UNDECLARED_THROWABLE_EXCEPTION,
409         util.RUNTIME_EXCEPTION,
410         util.EXCEPTION,
411         util.THROWABLE,
412     }));
413     assertEquals("Should be catchable only as UndeclaredThrowableException and its superclasses",
414              catchable, util.catchableSubset(set0));
415
416     ThrowableSet.Pair catchableAs = set0.whichCatchableAs(util.LINKAGE_ERROR);
417     assertEquals(mgr.EMPTY, catchableAs.getCaught());
418     assertEquals(set0, catchableAs.getUncaught());
419     catchableAs = set0.whichCatchableAs(util.UNDECLARED_THROWABLE_EXCEPTION);
420     assertEquals(catchableAs.getCaught(), set0);
421     assertEquals(catchableAs.getUncaught(), mgr.EMPTY);
422     catchableAs = set0.whichCatchableAs(util.RUNTIME_EXCEPTION);
423     assertEquals(catchableAs.getCaught(), set0);
424     assertEquals(catchableAs.getUncaught(), mgr.EMPTY);
425     if (DUMP_INTERNALS) {
426         printAllSets();
427     }
428     }
429
430     public void testSingleInstance1() {
431     if (DUMP_INTERNALS) {
432         System.err.println("\n\ntestSingleInstance1()");
433     }
434     Set expected0 = new ExceptionHashSet(Arrays.asList(new RefType[] {
435                    util.UNDECLARED_THROWABLE_EXCEPTION,
436     }));
437     Set expected1 = new ExceptionHashSet(Arrays.asList(new RefType[] {
438                    util.UNSUPPORTED_LOOK_AND_FEEL_EXCEPTION,
439     }));
440     Set expectedResult = new ExceptionHashSet(Arrays.asList(new RefType[] {
441                    util.UNDECLARED_THROWABLE_EXCEPTION,
442                    util.UNSUPPORTED_LOOK_AND_FEEL_EXCEPTION,
443     }));
444
445     ThrowableSet set0 = add(mgr.EMPTY, util.UNDECLARED_THROWABLE_EXCEPTION,
446                 expected0);
447     ThrowableSet set0a = add(set0, util.UNSUPPORTED_LOOK_AND_FEEL_EXCEPTION,
448                  expectedResult);
449     ThrowableSet set1 = add(mgr.EMPTY,
450                 util.UNSUPPORTED_LOOK_AND_FEEL_EXCEPTION,
451                 expected1);
452     ThrowableSet set1a = add(set1, util.UNDECLARED_THROWABLE_EXCEPTION,
453                  expectedResult);
454
455     assertTrue("The same ThrowableSet object should represent two sets containing the same two exceptions, even if added in different orders.",
456            set0a == set1a);
457
458     Set catchable = new ExceptionHashSet(expectedResult);
459     catchable.add(util.RUNTIME_EXCEPTION);
460     catchable.add(util.EXCEPTION);
461     catchable.add(util.THROWABLE);
462     assertEquals("Should be catchable only as UndeclaredThrowableException "
463              + "UnsupportedLookAndFeelException and superclasses",
464              catchable, util.catchableSubset(set0a));
465
466     if (DUMP_INTERNALS) {
467         printAllSets();
468     }
469     }
470
471
472     public void testAddingSubclasses() {
473     if (DUMP_INTERNALS) {
474         System.err.println("\n\ntestAddingSubclasses()");
475     }
476     Set expected = new ExceptionHashSet();
477     expected.add(util.INDEX_OUT_OF_BOUNDS_EXCEPTION);
478     ThrowableSet set0 = add(mgr.EMPTY, util.INDEX_OUT_OF_BOUNDS_EXCEPTION,
479                 expected);
480
481     expected.clear();
482     expected.add(AnySubType.v(util.INDEX_OUT_OF_BOUNDS_EXCEPTION));
483     ThrowableSet set1 = add(mgr.EMPTY,
484                 AnySubType.v(util.INDEX_OUT_OF_BOUNDS_EXCEPTION),
485                 expected);
486     assertTrue("ThrowableSet should distinguish the case where a single exception includes subclasses from that where it does not.",
487             set0 != set1);
488
489     Set catchable = new ExceptionHashSet(Arrays.asList(new RefType[] {
490         util.INDEX_OUT_OF_BOUNDS_EXCEPTION,
491         util.RUNTIME_EXCEPTION,
492         util.EXCEPTION,
493         util.THROWABLE,
494     }));
495     assertEquals(catchable, util.catchableSubset(set0));
496
497     catchable.add(util.ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION);
498     catchable.add(util.STRING_INDEX_OUT_OF_BOUNDS_EXCEPTION);
499     assertEquals(catchable, util.catchableSubset(set1));
500
501     if (DUMP_INTERNALS) {
502         printAllSets();
503     }
504     }
505
506     public void testAddingSets0() {
507     if (DUMP_INTERNALS) {
508         System.err.println("\n\ntestAddingSets0()");
509     }
510     Set expected = new ExceptionHashSet(Arrays.asList(new RefType[] {
511         util.INDEX_OUT_OF_BOUNDS_EXCEPTION,
512     }));
513     ThrowableSet set0 = add(mgr.EMPTY, util.INDEX_OUT_OF_BOUNDS_EXCEPTION,
514                 expected);
515
516     expected.clear();
517     expected.add(AnySubType.v(util.INDEX_OUT_OF_BOUNDS_EXCEPTION));
518     ThrowableSet set1 = add(mgr.EMPTY,
519                 AnySubType.v(util.INDEX_OUT_OF_BOUNDS_EXCEPTION),
520                 expected);
521
522     ThrowableSet result = add(set1, set0, expected);
523     assertTrue("{AnySubType(E)} union {E} should equal {AnySubType(E)}",
524            result == set1);
525
526     result = add(set1, set0, expected);
527     assertTrue("{E} union {AnySubType(E)} should equal {AnySubType(E)}",
528             result == set1);
529     
530     if (DUMP_INTERNALS) {
531         System.err.println("testAddingSets0()");
532         printAllSets();
533     }
534     }
535
536
537     public void testAddingSets1() {
538     Set expected = new ExceptionHashSet(util.VM_ERRORS);
539     expected.add(util.UNDECLARED_THROWABLE_EXCEPTION);
540     ThrowableSet set0 = add(mgr.VM_ERRORS,
541                 util.UNDECLARED_THROWABLE_EXCEPTION, expected);
542     expected.add(util.UNSUPPORTED_LOOK_AND_FEEL_EXCEPTION);
543     set0 = add(set0, util.UNSUPPORTED_LOOK_AND_FEEL_EXCEPTION, expected);
544     
545     ThrowableSet set1 = mgr.INITIALIZATION_ERRORS;
546     expected = new ExceptionHashSet();
547     expected.add(AnySubType.v(util.ERROR));
548     assertSameMembers(set1, expected, Collections.EMPTY_SET);
549
550     expected.add(util.UNDECLARED_THROWABLE_EXCEPTION);
551     expected.add(util.UNSUPPORTED_LOOK_AND_FEEL_EXCEPTION);
552     ThrowableSet result0 = add(set0, set1, expected);
553     ThrowableSet result1 = add(set1, set0, expected);
554     assertTrue("Adding sets should be commutative.", result0 == result1);
555
556     Set catchable = new ExceptionHashSet(util.ALL_TEST_ERRORS);
557     catchable.add(util.UNDECLARED_THROWABLE_EXCEPTION);
558     catchable.add(util.UNSUPPORTED_LOOK_AND_FEEL_EXCEPTION);
559     catchable.add(util.RUNTIME_EXCEPTION);// Superclasses of
560
catchable.add(util.EXCEPTION); // others.
561
catchable.add(util.ERROR);
562     catchable.add(util.THROWABLE);
563     assertEquals(catchable, util.catchableSubset(result0));
564     
565     if (DUMP_INTERNALS) {
566         printAllSets();
567     }
568     }
569
570
571     public void testAddingSets2() {
572     Set expected = new ExceptionHashSet(util.VM_ERRORS);
573     expected.add(util.UNDECLARED_THROWABLE_EXCEPTION);
574     ThrowableSet set0 = add(mgr.VM_ERRORS,
575                 util.UNDECLARED_THROWABLE_EXCEPTION, expected);
576     expected.add(util.UNSUPPORTED_LOOK_AND_FEEL_EXCEPTION);
577     set0 = add(set0, util.UNSUPPORTED_LOOK_AND_FEEL_EXCEPTION, expected);
578     
579     ThrowableSet set1 = mgr.INITIALIZATION_ERRORS;
580     expected = new ExceptionHashSet();
581     expected.add(AnySubType.v(util.ERROR));
582     assertSameMembers(set1, expected, Collections.EMPTY_SET);
583
584     expected.add(util.UNDECLARED_THROWABLE_EXCEPTION);
585     expected.add(util.UNSUPPORTED_LOOK_AND_FEEL_EXCEPTION);
586     ThrowableSet result0 = add(set0, set1, expected);
587     ThrowableSet result1 = add(set1, set0, expected);
588     assertTrue("Adding sets should be commutative.", result0 == result1);
589
590     Set catchable = new ExceptionHashSet(util.ALL_TEST_ERRORS);
591     catchable.add(util.UNDECLARED_THROWABLE_EXCEPTION);
592     catchable.add(util.UNSUPPORTED_LOOK_AND_FEEL_EXCEPTION);
593     catchable.add(util.RUNTIME_EXCEPTION);// Superclasses of
594
catchable.add(util.EXCEPTION); // others.
595
catchable.add(util.ERROR);
596     catchable.add(util.THROWABLE);
597     assertEquals(catchable, util.catchableSubset(result0));
598     
599     if (DUMP_INTERNALS) {
600         printAllSets();
601     }
602     }
603
604
605     public void testWhichCatchable0() {
606     if (DUMP_INTERNALS) {
607         System.err.println("\n\ntestWhichCatchable0()");
608     }
609     Set expected = new ExceptionHashSet(Arrays.asList(new RefType[] {
610         util.UNDECLARED_THROWABLE_EXCEPTION,
611     }));
612
613     ThrowableSet set0 = add(mgr.EMPTY, util.UNDECLARED_THROWABLE_EXCEPTION,
614                 expected);
615     Set catchable = new ExceptionHashSet(Arrays.asList(new RefType[] {
616         util.UNDECLARED_THROWABLE_EXCEPTION,
617         util.RUNTIME_EXCEPTION,
618         util.EXCEPTION,
619         util.THROWABLE,
620     }));
621
622     ThrowableSet.Pair catchableAs = set0.whichCatchableAs(util.LINKAGE_ERROR);
623     assertEquals(mgr.EMPTY, catchableAs.getCaught());
624     assertEquals(set0, catchableAs.getUncaught());
625     assertEquals(Collections.EMPTY_SET,
626              util.catchableSubset(catchableAs.getCaught()));
627     assertEquals(catchable, util.catchableSubset(catchableAs.getUncaught()));
628
629     assertTrue(set0.catchableAs(util.UNDECLARED_THROWABLE_EXCEPTION));
630     catchableAs = set0.whichCatchableAs(util.UNDECLARED_THROWABLE_EXCEPTION);
631     assertEquals(catchableAs.getCaught(), set0);
632     assertEquals(catchableAs.getUncaught(), mgr.EMPTY);
633     assertEquals(catchable, util.catchableSubset(catchableAs.getCaught()));
634     assertEquals(Collections.EMPTY_SET,
635              util.catchableSubset(catchableAs.getUncaught()));
636
637     assertTrue(set0.catchableAs(util.RUNTIME_EXCEPTION));
638     catchableAs = set0.whichCatchableAs(util.RUNTIME_EXCEPTION);
639     assertEquals(catchableAs.getCaught(), set0);
640     assertEquals(catchableAs.getUncaught(), mgr.EMPTY);
641     assertEquals(catchable, util.catchableSubset(catchableAs.getCaught()));
642     assertEquals(Collections.EMPTY_SET,
643              util.catchableSubset(catchableAs.getUncaught()));
644
645     assertTrue(set0.catchableAs(util.EXCEPTION));
646     catchableAs = set0.whichCatchableAs(util.EXCEPTION);
647     assertEquals(catchableAs.getCaught(), set0);
648     assertEquals(catchableAs.getUncaught(), mgr.EMPTY);
649     assertEquals(catchable, util.catchableSubset(catchableAs.getCaught()));
650     assertEquals(Collections.EMPTY_SET,
651              util.catchableSubset(catchableAs.getUncaught()));
652
653     assertTrue(set0.catchableAs(util.THROWABLE));
654     catchableAs = set0.whichCatchableAs(util.THROWABLE);
655     assertEquals(catchableAs.getCaught(), set0);
656     assertEquals(catchableAs.getUncaught(), mgr.EMPTY);
657     assertEquals(catchable, util.catchableSubset(catchableAs.getCaught()));
658     assertEquals(Collections.EMPTY_SET,
659              util.catchableSubset(catchableAs.getUncaught()));
660
661     assertTrue(! set0.catchableAs(util.ERROR));
662     catchableAs = set0.whichCatchableAs(util.ERROR);
663     assertEquals(catchableAs.getCaught(), mgr.EMPTY);
664     assertEquals(catchableAs.getUncaught(), set0);
665     assertEquals(Collections.EMPTY_SET,
666              util.catchableSubset(catchableAs.getCaught()));
667     assertEquals(catchable, util.catchableSubset(catchableAs.getUncaught()));
668
669     if (DUMP_INTERNALS) {
670         printAllSets();
671     }
672     }
673
674
675     public void testWhichCatchable1() {
676     if (DUMP_INTERNALS) {
677         System.err.println("\n\ntestWhichCatchable1()");
678     }
679     ThrowableSet set0 = mgr.EMPTY.add(util.LINKAGE_ERROR);
680     Set catcherTypes = new ExceptionHashSet(Arrays.asList(new RefType[] {
681         util.LINKAGE_ERROR,
682         util.ERROR,
683         util.THROWABLE,
684     }));
685
686     assertTrue(set0.catchableAs(util.ERROR));
687     ThrowableSet.Pair catchableAs = set0.whichCatchableAs(util.ERROR);
688     assertEquals(set0, catchableAs.getCaught());
689     assertEquals(mgr.EMPTY, catchableAs.getUncaught());
690     assertEquals(catcherTypes,
691              util.catchableSubset(catchableAs.getCaught()));
692     assertEquals(Collections.EMPTY_SET,
693              util.catchableSubset(catchableAs.getUncaught()));
694
695     assertTrue(set0.catchableAs(util.LINKAGE_ERROR));
696     catchableAs = set0.whichCatchableAs(util.LINKAGE_ERROR);
697     assertEquals(set0, catchableAs.getCaught());
698     assertEquals(mgr.EMPTY, catchableAs.getUncaught());
699     assertEquals(catcherTypes,
700              util.catchableSubset(catchableAs.getCaught()));
701     assertEquals(Collections.EMPTY_SET,
702              util.catchableSubset(catchableAs.getUncaught()));
703
704     assertTrue(! set0.catchableAs(util.INCOMPATIBLE_CLASS_CHANGE_ERROR));
705     catchableAs = set0.whichCatchableAs(util.INCOMPATIBLE_CLASS_CHANGE_ERROR);
706     assertEquals(mgr.EMPTY, catchableAs.getCaught());
707     assertEquals(set0, catchableAs.getUncaught());
708     assertEquals(Collections.EMPTY_SET,
709              util.catchableSubset(catchableAs.getCaught()));
710     assertEquals(catcherTypes,
711              util.catchableSubset(catchableAs.getUncaught()));
712     
713     assertTrue(! set0.catchableAs(util.INSTANTIATION_ERROR));
714     catchableAs = set0.whichCatchableAs(util.INSTANTIATION_ERROR);
715     assertEquals(mgr.EMPTY, catchableAs.getCaught());
716     assertEquals(set0, catchableAs.getUncaught());
717     assertEquals(Collections.EMPTY_SET,
718              util.catchableSubset(catchableAs.getCaught()));
719     assertEquals(catcherTypes,
720              util.catchableSubset(catchableAs.getUncaught()));
721     
722     assertTrue(! set0.catchableAs(util.INTERNAL_ERROR));
723     catchableAs = set0.whichCatchableAs(util.INTERNAL_ERROR);
724     assertEquals(mgr.EMPTY, catchableAs.getCaught());
725     assertEquals(set0, catchableAs.getUncaught());
726     assertEquals(Collections.EMPTY_SET,
727              util.catchableSubset(catchableAs.getCaught()));
728     assertEquals(catcherTypes,
729              util.catchableSubset(catchableAs.getUncaught()));
730     
731     if (DUMP_INTERNALS) {
732         printAllSets();
733     }
734     }
735
736
737     public void testWhichCatchable2() {
738     if (DUMP_INTERNALS) {
739         System.err.println("\n\ntestWhichCatchable2()");
740     }
741
742     ThrowableSet set0 = mgr.EMPTY.add(AnySubType.v(util.LINKAGE_ERROR));
743     Set catcherTypes = new ExceptionHashSet(Arrays.asList(new RefType[] {
744         util.CLASS_CIRCULARITY_ERROR,
745         util.CLASS_FORMAT_ERROR,
746         util.UNSUPPORTED_CLASS_VERSION_ERROR,
747         util.EXCEPTION_IN_INITIALIZER_ERROR,
748         util.INCOMPATIBLE_CLASS_CHANGE_ERROR,
749         util.ABSTRACT_METHOD_ERROR,
750         util.ILLEGAL_ACCESS_ERROR,
751         util.INSTANTIATION_ERROR,
752         util.NO_SUCH_FIELD_ERROR,
753         util.NO_SUCH_METHOD_ERROR,
754         util.NO_CLASS_DEF_FOUND_ERROR,
755         util.UNSATISFIED_LINK_ERROR,
756         util.VERIFY_ERROR,
757         util.LINKAGE_ERROR,
758         util.ERROR,
759         util.THROWABLE,
760     }));
761
762     assertTrue(set0.catchableAs(util.ERROR));
763     ThrowableSet.Pair catchableAs = set0.whichCatchableAs(util.ERROR);
764     assertEquals(set0, catchableAs.getCaught());
765     assertEquals(mgr.EMPTY, catchableAs.getUncaught());
766     assertEquals(catcherTypes,
767              util.catchableSubset(catchableAs.getCaught()));
768     assertEquals(Collections.EMPTY_SET,
769              util.catchableSubset(catchableAs.getUncaught()));
770
771     assertTrue(set0.catchableAs(util.LINKAGE_ERROR));
772     catchableAs = set0.whichCatchableAs(util.LINKAGE_ERROR);
773     assertEquals(set0, catchableAs.getCaught());
774     assertEquals(mgr.EMPTY, catchableAs.getUncaught());
775     assertEquals(catcherTypes,
776              util.catchableSubset(catchableAs.getCaught()));
777     assertEquals(Collections.EMPTY_SET,
778              util.catchableSubset(catchableAs.getUncaught()));
779
780     assertTrue(set0.catchableAs(util.INCOMPATIBLE_CLASS_CHANGE_ERROR));
781     catchableAs = set0.whichCatchableAs(util.INCOMPATIBLE_CLASS_CHANGE_ERROR);
782     Set expectedCaughtIncluded = new ExceptionHashSet(
783         Arrays.asList(new RefLikeType[]
784             {AnySubType.v(util.INCOMPATIBLE_CLASS_CHANGE_ERROR)}));
785     Set expectedCaughtExcluded = Collections.EMPTY_SET;
786     Set expectedUncaughtIncluded = new ExceptionHashSet(
787         Arrays.asList(new RefLikeType[]
788             {AnySubType.v(util.LINKAGE_ERROR)}));
789     Set expectedUncaughtExcluded = expectedCaughtIncluded;
790     assertSameMembers(catchableAs,
791               expectedCaughtIncluded,
792               expectedCaughtExcluded,
793               expectedUncaughtIncluded,
794               expectedUncaughtExcluded);
795     catcherTypes = new ExceptionHashSet(Arrays.asList(new RefType[] {
796         util.INCOMPATIBLE_CLASS_CHANGE_ERROR,
797         util.ABSTRACT_METHOD_ERROR,
798         util.ILLEGAL_ACCESS_ERROR,
799         util.INSTANTIATION_ERROR,
800         util.NO_SUCH_FIELD_ERROR,
801         util.NO_SUCH_METHOD_ERROR,
802         util.LINKAGE_ERROR,
803         util.ERROR,
804         util.THROWABLE,
805     }));
806     Set noncatcherTypes = new ExceptionHashSet(Arrays.asList(new RefType[] {
807         util.CLASS_CIRCULARITY_ERROR,
808         util.CLASS_FORMAT_ERROR,
809         util.UNSUPPORTED_CLASS_VERSION_ERROR,
810         util.EXCEPTION_IN_INITIALIZER_ERROR,
811         util.NO_CLASS_DEF_FOUND_ERROR,
812         util.UNSATISFIED_LINK_ERROR,
813         util.VERIFY_ERROR,
814         util.LINKAGE_ERROR,
815         util.ERROR,
816         util.THROWABLE,
817     }));
818     assertEquals(catcherTypes,
819              util.catchableSubset(catchableAs.getCaught()));
820     assertEquals(noncatcherTypes,
821              util.catchableSubset(catchableAs.getUncaught()));
822     
823     assertTrue(set0.catchableAs(util.INSTANTIATION_ERROR));
824     catchableAs = set0.whichCatchableAs(util.INSTANTIATION_ERROR);
825     expectedCaughtIncluded = new ExceptionHashSet(
826         Arrays.asList(new RefLikeType[]
827                   {AnySubType.v(util.INSTANTIATION_ERROR)}));
828     expectedCaughtExcluded = Collections.EMPTY_SET;
829     expectedUncaughtExcluded = expectedCaughtIncluded;
830     assertSameMembers(catchableAs,
831               expectedCaughtIncluded,
832               expectedCaughtExcluded,
833               expectedUncaughtIncluded,
834               expectedUncaughtExcluded);
835     catcherTypes = new ExceptionHashSet(Arrays.asList(new RefType[] {
836         util.INSTANTIATION_ERROR,
837         util.INCOMPATIBLE_CLASS_CHANGE_ERROR,
838         util.LINKAGE_ERROR,
839         util.ERROR,
840         util.THROWABLE,
841     }));
842     noncatcherTypes = new ExceptionHashSet(Arrays.asList(new RefType[] {
843         util.CLASS_CIRCULARITY_ERROR,
844         util.CLASS_FORMAT_ERROR,
845         util.UNSUPPORTED_CLASS_VERSION_ERROR,
846         util.EXCEPTION_IN_INITIALIZER_ERROR,
847         util.ABSTRACT_METHOD_ERROR,
848         util.ILLEGAL_ACCESS_ERROR,
849         util.NO_SUCH_FIELD_ERROR,
850         util.NO_SUCH_METHOD_ERROR,
851         util.NO_CLASS_DEF_FOUND_ERROR,
852         util.UNSATISFIED_LINK_ERROR,
853         util.VERIFY_ERROR,
854         util.INCOMPATIBLE_CLASS_CHANGE_ERROR,
855         util.LINKAGE_ERROR,
856         util.ERROR,
857         util.THROWABLE,
858     }));
859     assertEquals(catcherTypes,
860              util.catchableSubset(catchableAs.getCaught()));
861     assertEquals(noncatcherTypes,
862              util.catchableSubset(catchableAs.getUncaught()));
863     
864     assertTrue(! set0.catchableAs(util.INTERNAL_ERROR));
865     catchableAs = set0.whichCatchableAs(util.INTERNAL_ERROR);
866     assertEquals(mgr.EMPTY, catchableAs.getCaught());
867     assertEquals(set0, catchableAs.getUncaught());
868     noncatcherTypes = new ExceptionHashSet(Arrays.asList(new RefType[] {
869         util.CLASS_CIRCULARITY_ERROR,
870         util.CLASS_FORMAT_ERROR,
871         util.UNSUPPORTED_CLASS_VERSION_ERROR,
872         util.EXCEPTION_IN_INITIALIZER_ERROR,
873         util.ABSTRACT_METHOD_ERROR,
874         util.ILLEGAL_ACCESS_ERROR,
875         util.INCOMPATIBLE_CLASS_CHANGE_ERROR,
876         util.NO_SUCH_FIELD_ERROR,
877         util.NO_SUCH_METHOD_ERROR,
878         util.INSTANTIATION_ERROR,
879         util.NO_CLASS_DEF_FOUND_ERROR,
880         util.UNSATISFIED_LINK_ERROR,
881         util.VERIFY_ERROR,
882         util.INCOMPATIBLE_CLASS_CHANGE_ERROR,
883         util.LINKAGE_ERROR,
884         util.ERROR,
885         util.THROWABLE,
886     }));
887     assertEquals(Collections.EMPTY_SET,
888              util.catchableSubset(catchableAs.getCaught()));
889     assertEquals(noncatcherTypes,
890              util.catchableSubset(catchableAs.getUncaught()));
891     
892     if (DUMP_INTERNALS) {
893         printAllSets();
894     }
895     }
896
897
898     public void testWhichCatchable3() {
899     if (DUMP_INTERNALS) {
900         System.err.println("\n\ntestWhichCatchable3()");
901     }
902
903     ThrowableSet set0 = mgr.EMPTY;
904     set0 = set0.add(AnySubType.v(util.ERROR));
905
906     assertTrue(set0.catchableAs(util.INCOMPATIBLE_CLASS_CHANGE_ERROR));
907     ThrowableSet.Pair catchableAs = set0.whichCatchableAs(util.INCOMPATIBLE_CLASS_CHANGE_ERROR);
908     Set expectedCaughtIncluded = new ExceptionHashSet(
909         Arrays.asList(new RefLikeType[]
910             {AnySubType.v(util.INCOMPATIBLE_CLASS_CHANGE_ERROR)}));
911     Set expectedCaughtExcluded = Collections.EMPTY_SET;
912     Set expectedUncaughtIncluded = new ExceptionHashSet(
913         Arrays.asList(new RefLikeType[]
914             {AnySubType.v(util.ERROR)}));
915     Set expectedUncaughtExcluded = expectedCaughtIncluded;
916     assertTrue(util.sameMembers(expectedCaughtIncluded,
917                     expectedCaughtExcluded,
918                    catchableAs.getCaught()));
919     assertTrue(util.sameMembers(expectedUncaughtIncluded,
920                     expectedUncaughtExcluded,
921                     catchableAs.getUncaught()));
922     assertEquals(new ExceptionHashSet(Arrays.asList(new RefType[] {
923         util.THROWABLE,
924         util.ERROR,
925         util.LINKAGE_ERROR,
926         util.INCOMPATIBLE_CLASS_CHANGE_ERROR,
927         util.ABSTRACT_METHOD_ERROR,
928         util.INSTANTIATION_ERROR,
929         util.ILLEGAL_ACCESS_ERROR,
930         util.NO_SUCH_FIELD_ERROR,
931         util.NO_SUCH_METHOD_ERROR,})),
932              util.catchableSubset(catchableAs.getCaught()));
933     assertEquals(new ExceptionHashSet(Arrays.asList(new RefType[] {
934         util.THROWABLE,
935         util.ERROR,
936         util.AWT_ERROR,
937         util.LINKAGE_ERROR,
938         util.CLASS_CIRCULARITY_ERROR,
939         util.CLASS_FORMAT_ERROR,
940         util.UNSUPPORTED_CLASS_VERSION_ERROR,
941         util.EXCEPTION_IN_INITIALIZER_ERROR,
942         util.NO_CLASS_DEF_FOUND_ERROR,
943         util.UNSATISFIED_LINK_ERROR,
944         util.VERIFY_ERROR,
945         util.THREAD_DEATH,
946         util.VIRTUAL_MACHINE_ERROR,
947         util.INTERNAL_ERROR,
948         util.OUT_OF_MEMORY_ERROR,
949         util.STACK_OVERFLOW_ERROR,
950         util.UNKNOWN_ERROR,})),
951              util.catchableSubset(catchableAs.getUncaught()));
952
953     set0 = catchableAs.getUncaught();
954
955     assertTrue(set0.catchableAs(util.THROWABLE));
956     catchableAs = set0.whichCatchableAs(util.THROWABLE);
957     assertEquals(set0, catchableAs.getCaught());
958     assertEquals(mgr.EMPTY, catchableAs.getUncaught());
959     assertEquals(new ExceptionHashSet(Arrays.asList(new RefType[] {
960         util.THROWABLE,
961         util.ERROR,
962         util.AWT_ERROR,
963         util.LINKAGE_ERROR,
964         util.CLASS_CIRCULARITY_ERROR,
965         util.CLASS_FORMAT_ERROR,
966         util.UNSUPPORTED_CLASS_VERSION_ERROR,
967         util.EXCEPTION_IN_INITIALIZER_ERROR,
968         util.NO_CLASS_DEF_FOUND_ERROR,
969         util.UNSATISFIED_LINK_ERROR,
970         util.VERIFY_ERROR,
971         util.THREAD_DEATH,
972         util.VIRTUAL_MACHINE_ERROR,
973         util.INTERNAL_ERROR,
974         util.OUT_OF_MEMORY_ERROR,
975         util.STACK_OVERFLOW_ERROR,
976         util.UNKNOWN_ERROR,})),
977              util.catchableSubset(catchableAs.getCaught()));
978     assertEquals(Collections.EMPTY_SET,
979              util.catchableSubset(catchableAs.getUncaught()));
980
981     assertTrue(set0.catchableAs(util.ERROR));
982     catchableAs = set0.whichCatchableAs(util.ERROR);
983     assertEquals(set0, catchableAs.getCaught());
984     assertEquals(mgr.EMPTY, catchableAs.getUncaught());
985     assertEquals(new ExceptionHashSet(Arrays.asList(new RefType[] {
986         util.THROWABLE,
987         util.ERROR,
988         util.AWT_ERROR,
989         util.LINKAGE_ERROR,
990         util.CLASS_CIRCULARITY_ERROR,
991         util.CLASS_FORMAT_ERROR,
992         util.UNSUPPORTED_CLASS_VERSION_ERROR,
993         util.EXCEPTION_IN_INITIALIZER_ERROR,
994         util.NO_CLASS_DEF_FOUND_ERROR,
995         util.UNSATISFIED_LINK_ERROR,
996         util.VERIFY_ERROR,
997         util.THREAD_DEATH,
998         util.VIRTUAL_MACHINE_ERROR,
999         util.INTERNAL_ERROR,
1000        util.OUT_OF_MEMORY_ERROR,
1001        util.STACK_OVERFLOW_ERROR,
1002        util.UNKNOWN_ERROR,})),
1003             util.catchableSubset(catchableAs.getCaught()));
1004    assertEquals(Collections.EMPTY_SET,
1005             util.catchableSubset(catchableAs.getUncaught()));
1006
1007    assertTrue(set0.catchableAs(util.LINKAGE_ERROR));
1008    catchableAs = set0.whichCatchableAs(util.LINKAGE_ERROR);
1009    expectedCaughtIncluded = new ExceptionHashSet(
1010        Arrays.asList(new RefLikeType[]
1011            {AnySubType.v(util.LINKAGE_ERROR)}));
1012    expectedCaughtExcluded = new ExceptionHashSet(
1013        Arrays.asList(new RefLikeType[]
1014            {AnySubType.v(util.INCOMPATIBLE_CLASS_CHANGE_ERROR)}));
1015    expectedUncaughtIncluded = new ExceptionHashSet(
1016        Arrays.asList(new RefLikeType[]
1017            {AnySubType.v(util.ERROR)}));
1018    expectedUncaughtExcluded = expectedCaughtIncluded;
1019    assertTrue(util.sameMembers(expectedCaughtIncluded,
1020                    expectedCaughtExcluded,
1021                    catchableAs.getCaught()));
1022    assertTrue(util.sameMembers(expectedUncaughtIncluded,
1023                    expectedUncaughtExcluded,
1024                    catchableAs.getUncaught()));
1025    assertEquals(new ExceptionHashSet(Arrays.asList(new RefType[] {
1026        util.THROWABLE,
1027        util.ERROR,
1028        util.LINKAGE_ERROR,
1029        util.CLASS_CIRCULARITY_ERROR,
1030        util.CLASS_FORMAT_ERROR,
1031        util.UNSUPPORTED_CLASS_VERSION_ERROR,
1032        util.EXCEPTION_IN_INITIALIZER_ERROR,
1033        util.NO_CLASS_DEF_FOUND_ERROR,
1034        util.UNSATISFIED_LINK_ERROR,
1035        util.VERIFY_ERROR,})),
1036             util.catchableSubset(catchableAs.getCaught()));
1037    assertEquals(new ExceptionHashSet(Arrays.asList(new RefType[] {
1038        util.THROWABLE,
1039        util.ERROR,
1040        util.AWT_ERROR,
1041        util.THREAD_DEATH,
1042        util.VIRTUAL_MACHINE_ERROR,
1043        util.INTERNAL_ERROR,
1044        util.OUT_OF_MEMORY_ERROR,
1045        util.STACK_OVERFLOW_ERROR,
1046        util.UNKNOWN_ERROR,})),
1047             util.catchableSubset(catchableAs.getUncaught()));
1048
1049    assertTrue(! set0.catchableAs(util.INCOMPATIBLE_CLASS_CHANGE_ERROR));
1050    catchableAs = set0.whichCatchableAs(util.INCOMPATIBLE_CLASS_CHANGE_ERROR);
1051    assertEquals(mgr.EMPTY, catchableAs.getCaught());
1052    assertEquals(set0, catchableAs.getUncaught());
1053    assertEquals(Collections.EMPTY_SET,
1054             util.catchableSubset(catchableAs.getCaught()));
1055    assertEquals(new ExceptionHashSet(Arrays.asList(new RefType[] {
1056        util.THROWABLE,
1057        util.ERROR,
1058        util.LINKAGE_ERROR,
1059        util.AWT_ERROR,
1060        util.THREAD_DEATH,
1061        util.VIRTUAL_MACHINE_ERROR,
1062        util.INTERNAL_ERROR,
1063        util.OUT_OF_MEMORY_ERROR,
1064        util.STACK_OVERFLOW_ERROR,
1065        util.CLASS_CIRCULARITY_ERROR,
1066        util.CLASS_FORMAT_ERROR,
1067        util.UNSUPPORTED_CLASS_VERSION_ERROR,
1068        util.EXCEPTION_IN_INITIALIZER_ERROR,
1069        util.NO_CLASS_DEF_FOUND_ERROR,
1070        util.UNSATISFIED_LINK_ERROR,
1071        util.VERIFY_ERROR,
1072        util.UNKNOWN_ERROR,})),
1073             util.catchableSubset(catchableAs.getUncaught()));
1074
1075    catchableAs = set0.whichCatchableAs(util.ILLEGAL_ACCESS_ERROR);
1076    assertEquals(mgr.EMPTY, catchableAs.getCaught());
1077    assertEquals(set0, catchableAs.getUncaught());
1078    assertEquals(Collections.EMPTY_SET,
1079             util.catchableSubset(catchableAs.getCaught()));
1080    assertEquals(new ExceptionHashSet(Arrays.asList(new RefType[] {
1081        util.THROWABLE,
1082        util.ERROR,
1083        util.LINKAGE_ERROR,
1084        util.AWT_ERROR,
1085        util.THREAD_DEATH,
1086        util.VIRTUAL_MACHINE_ERROR,
1087        util.INTERNAL_ERROR,
1088        util.OUT_OF_MEMORY_ERROR,
1089        util.STACK_OVERFLOW_ERROR,
1090        util.CLASS_CIRCULARITY_ERROR,
1091        util.CLASS_FORMAT_ERROR,
1092        util.UNSUPPORTED_CLASS_VERSION_ERROR,
1093        util.EXCEPTION_IN_INITIALIZER_ERROR,
1094        util.NO_CLASS_DEF_FOUND_ERROR,
1095        util.UNSATISFIED_LINK_ERROR,
1096        util.VERIFY_ERROR,
1097        util.UNKNOWN_ERROR,})),
1098             util.catchableSubset(catchableAs.getUncaught()));
1099
1100    if (DUMP_INTERNALS) {
1101        printAllSets();
1102    }
1103    }
1104
1105
1106    public void testWhichCatchable10() {
1107    if (DUMP_INTERNALS) {
1108        System.err.println("\n\ntestWhichCatchable3()");
1109    }
1110
1111    ThrowableSet set0 = mgr.EMPTY;
1112    set0 = set0.add(AnySubType.v(util.THROWABLE));
1113
1114    assertTrue(set0.catchableAs(util.ARITHMETIC_EXCEPTION));
1115    ThrowableSet.Pair catchableAs = set0.whichCatchableAs(util.ARITHMETIC_EXCEPTION);
1116    assertSameMembers(catchableAs,
1117              new RefLikeType[] {
1118                          AnySubType.v(util.ARITHMETIC_EXCEPTION),
1119                          },
1120              new RefLikeType[] {
1121              },
1122              new RefLikeType[] {
1123                          AnySubType.v(util.THROWABLE),
1124                          },
1125              new RefLikeType[] {
1126                          AnySubType.v(util.ARITHMETIC_EXCEPTION),
1127                          });
1128    assertEquals(new ExceptionHashSet(Arrays.asList(new RefType[] {
1129        util.THROWABLE,
1130        util.EXCEPTION,
1131        util.RUNTIME_EXCEPTION,
1132        util.ARITHMETIC_EXCEPTION,})),
1133             util.catchableSubset(catchableAs.getCaught()));
1134    HashSet expectedUncaught = new HashSet(util.ALL_TEST_THROWABLES);
1135    expectedUncaught.remove(util.ARITHMETIC_EXCEPTION);
1136    assertEquals(expectedUncaught,
1137             util.catchableSubset(catchableAs.getUncaught()));
1138
1139    set0 = catchableAs.getUncaught();
1140    assertTrue(set0.catchableAs(util.ABSTRACT_METHOD_ERROR));
1141    catchableAs = set0.whichCatchableAs(util.ABSTRACT_METHOD_ERROR);
1142    assertSameMembers(catchableAs,
1143              new RefLikeType[] {
1144                  AnySubType.v(util.ABSTRACT_METHOD_ERROR),
1145              },
1146              new RefLikeType[] {
1147              },
1148              new RefLikeType[] {
1149                  AnySubType.v(util.THROWABLE),
1150              },
1151              new RefLikeType[] {
1152                          AnySubType.v(util.ARITHMETIC_EXCEPTION),
1153                          AnySubType.v(util.ABSTRACT_METHOD_ERROR),
1154                          });
1155    assertEquals(new ExceptionHashSet(Arrays.asList(new RefType[] {
1156        util.THROWABLE,
1157        util.ERROR,
1158        util.LINKAGE_ERROR,
1159        util.INCOMPATIBLE_CLASS_CHANGE_ERROR,
1160        util.ABSTRACT_METHOD_ERROR,})),
1161             util.catchableSubset(catchableAs.getCaught()));
1162    expectedUncaught.remove(util.ABSTRACT_METHOD_ERROR);
1163    assertEquals(expectedUncaught,
1164             util.catchableSubset(catchableAs.getUncaught()));
1165
1166    set0 = catchableAs.getUncaught();
1167    assertTrue(set0.catchableAs(util.RUNTIME_EXCEPTION));
1168    catchableAs = set0.whichCatchableAs(util.RUNTIME_EXCEPTION);
1169    assertSameMembers(catchableAs,
1170              new RefLikeType[] {
1171                  AnySubType.v(util.RUNTIME_EXCEPTION),
1172              },
1173              new RefLikeType[] {
1174                  AnySubType.v(util.ARITHMETIC_EXCEPTION),
1175              },
1176              new RefLikeType[] {
1177                  AnySubType.v(util.THROWABLE),
1178              },
1179              new RefLikeType[] {
1180                          AnySubType.v(util.RUNTIME_EXCEPTION),
1181                          AnySubType.v(util.ABSTRACT_METHOD_ERROR),
1182                          });
1183    assertEquals(new ExceptionHashSet(Arrays.asList(new RefType[] {
1184        util.THROWABLE,
1185        util.EXCEPTION,
1186        util.RUNTIME_EXCEPTION,
1187        util.ARRAY_STORE_EXCEPTION,
1188        util.CLASS_CAST_EXCEPTION,
1189        util.ILLEGAL_MONITOR_STATE_EXCEPTION,
1190        util.INDEX_OUT_OF_BOUNDS_EXCEPTION,
1191        util.ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION,
1192        util.STRING_INDEX_OUT_OF_BOUNDS_EXCEPTION,
1193        util.NEGATIVE_ARRAY_SIZE_EXCEPTION,
1194        util.NULL_POINTER_EXCEPTION,
1195        util.UNDECLARED_THROWABLE_EXCEPTION})),
1196             util.catchableSubset(catchableAs.getCaught()));
1197    expectedUncaught.remove(util.RUNTIME_EXCEPTION);
1198    expectedUncaught.remove(util.ARRAY_STORE_EXCEPTION);
1199    expectedUncaught.remove(util.CLASS_CAST_EXCEPTION);
1200    expectedUncaught.remove(util.ILLEGAL_MONITOR_STATE_EXCEPTION);
1201    expectedUncaught.remove(util.INDEX_OUT_OF_BOUNDS_EXCEPTION);
1202    expectedUncaught.remove(util.ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION);
1203    expectedUncaught.remove(util.STRING_INDEX_OUT_OF_BOUNDS_EXCEPTION);
1204    expectedUncaught.remove(util.NEGATIVE_ARRAY_SIZE_EXCEPTION);
1205    expectedUncaught.remove(util.NULL_POINTER_EXCEPTION);
1206    expectedUncaught.remove(util.UNDECLARED_THROWABLE_EXCEPTION);
1207    assertEquals(expectedUncaught,
1208             util.catchableSubset(catchableAs.getUncaught()));
1209    }
1210
1211
1212    public void testAddAfterWhichCatchableAs0() {
1213    if (DUMP_INTERNALS) {
1214        System.err.println("\n\ntestAddAfterWhichCatchable0()");
1215    }
1216
1217    ThrowableSet anyError = mgr.EMPTY.add(AnySubType.v(util.ERROR));
1218
1219    assertTrue(anyError.catchableAs(util.LINKAGE_ERROR));
1220    ThrowableSet.Pair catchableAs = anyError.whichCatchableAs(util.LINKAGE_ERROR);
1221    assertSameMembers(catchableAs,
1222              new RefLikeType[] {
1223                          AnySubType.v(util.LINKAGE_ERROR),
1224                          },
1225              new RefLikeType[] {
1226              },
1227              new RefLikeType[] {
1228                          AnySubType.v(util.ERROR),
1229                          },
1230              new RefLikeType[] {
1231                          AnySubType.v(util.LINKAGE_ERROR),
1232                          });
1233
1234    ThrowableSet anyErrorMinusLinkage = catchableAs.getUncaught();
1235    try {
1236        ThrowableSet anyErrorMinusLinkagePlusIncompatibleClassChange
1237        = anyErrorMinusLinkage.add(util.INCOMPATIBLE_CLASS_CHANGE_ERROR);
1238        fail("add(IncompatiableClassChangeError) after removing LinkageError should currently generate an exception");
1239
1240        // Following documents what we would like to be able to implement:
1241
assertSameMembers(anyErrorMinusLinkagePlusIncompatibleClassChange,
1242                  new RefLikeType[] {
1243                  AnySubType.v(util.ERROR),
1244                  util.INCOMPATIBLE_CLASS_CHANGE_ERROR,
1245                  },
1246                  new RefLikeType[] {
1247                  AnySubType.v(util.LINKAGE_ERROR),
1248                  });
1249    } catch (ThrowableSet.AlreadyHasExclusionsException e) {
1250        // this is what should happen.
1251
}
1252
1253    try {
1254        ThrowableSet anyErrorMinusLinkagePlusAnyIncompatibleClassChange
1255        = anyErrorMinusLinkage.add(AnySubType.v(util.INCOMPATIBLE_CLASS_CHANGE_ERROR));
1256        fail("add(AnySubType.v(IncompatiableClassChangeError)) after removing LinkageError should currently generate an exception");
1257
1258        // Following documents what we would like to be able to implement:
1259
assertSameMembers(anyErrorMinusLinkagePlusAnyIncompatibleClassChange,
1260                  new RefLikeType[] {
1261                  AnySubType.v(util.ERROR),
1262                  AnySubType.v(util.INCOMPATIBLE_CLASS_CHANGE_ERROR),
1263                  },
1264                  new RefLikeType[] {
1265                  AnySubType.v(util.LINKAGE_ERROR),
1266                  });
1267    } catch (ThrowableSet.AlreadyHasExclusionsException e) {
1268        // this is what should happen.
1269
}
1270
1271    // Add types that should not change the set.
1272
ThrowableSet sameSet
1273        = anyErrorMinusLinkage.add(util.VIRTUAL_MACHINE_ERROR);
1274    assertTrue(sameSet == anyErrorMinusLinkage);
1275    assertSameMembers(sameSet,
1276              new RefLikeType[] {
1277                  AnySubType.v(util.ERROR),
1278              },
1279              new RefLikeType[] {
1280                  AnySubType.v(util.LINKAGE_ERROR),
1281              });
1282    sameSet
1283        = anyErrorMinusLinkage.add(AnySubType.v(util.VIRTUAL_MACHINE_ERROR));
1284    assertTrue(sameSet == anyErrorMinusLinkage);
1285    assertSameMembers(sameSet,
1286              new RefLikeType[] {
1287                  AnySubType.v(util.ERROR),
1288              },
1289              new RefLikeType[] {
1290                  AnySubType.v(util.LINKAGE_ERROR),
1291              });
1292
1293    ThrowableSet anyErrorMinusLinkagePlusArrayIndex
1294        = anyErrorMinusLinkage.add(util.ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION);
1295    assertSameMembers(anyErrorMinusLinkagePlusArrayIndex,
1296              new RefLikeType[] {
1297                  AnySubType.v(util.ERROR),
1298                  util.ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION,
1299              },
1300              new RefLikeType[] {
1301                  AnySubType.v(util.LINKAGE_ERROR),
1302              });
1303
1304    ThrowableSet anyErrorMinusLinkagePlusAnyIndex
1305        = anyErrorMinusLinkagePlusArrayIndex.add(AnySubType.v(util.INDEX_OUT_OF_BOUNDS_EXCEPTION));
1306    assertSameMembers(anyErrorMinusLinkagePlusAnyIndex,
1307              new RefLikeType[] {
1308                  AnySubType.v(util.ERROR),
1309                  AnySubType.v(util.INDEX_OUT_OF_BOUNDS_EXCEPTION),
1310              },
1311              new RefLikeType[] {
1312                  AnySubType.v(util.LINKAGE_ERROR),
1313              });
1314
1315    ThrowableSet anyErrorMinusLinkagePlusAnyRuntime
1316        = anyErrorMinusLinkagePlusAnyIndex.add(AnySubType.v(util.RUNTIME_EXCEPTION));
1317    assertSameMembers(anyErrorMinusLinkagePlusAnyRuntime,
1318              new RefLikeType[] {
1319                  AnySubType.v(util.ERROR),
1320                  AnySubType.v(util.RUNTIME_EXCEPTION),
1321              },
1322              new RefLikeType[] {
1323                  AnySubType.v(util.LINKAGE_ERROR),
1324              });
1325
1326    try {
1327        ThrowableSet anyErrorMinusLinkagePlusAnyRuntimePlusError
1328        = anyErrorMinusLinkagePlusAnyRuntime.add(AnySubType.v(util.ERROR));
1329        fail("add(AnySubType(Error)) after removing LinkageError should currently generate an exception.");
1330
1331        // This documents what we would like to implement:
1332
assertSameMembers(anyErrorMinusLinkagePlusAnyRuntimePlusError,
1333                  new RefLikeType[] {
1334                  AnySubType.v(util.ERROR),
1335                  AnySubType.v(util.RUNTIME_EXCEPTION),
1336                  },
1337                  new RefLikeType[] {
1338                  });
1339    } catch (ThrowableSet.AlreadyHasExclusionsException e) {
1340        // This is what should happen.
1341
}
1342
1343    try {
1344        ThrowableSet anyErrorMinusLinkagePlusAnyRuntimePlusLinkageError
1345        = anyErrorMinusLinkagePlusAnyRuntime.add(AnySubType.v(util.LINKAGE_ERROR));
1346        fail("add(AnySubType(LinkageError)) after removing LinkageError should currently generate an exception.");
1347        
1348        // This documents what we would like to implement:
1349
assertSameMembers(anyErrorMinusLinkagePlusAnyRuntimePlusLinkageError,
1350              new RefLikeType[] {
1351                  AnySubType.v(util.ERROR),
1352                  AnySubType.v(util.RUNTIME_EXCEPTION),
1353              },
1354              new RefLikeType[] {
1355              });
1356    } catch (ThrowableSet.AlreadyHasExclusionsException e) {
1357        // This is what should happen.
1358
}
1359
1360    }
1361    
1362    void printAllSets() {
1363        for (Iterator i = mgr.getSizeToSets().values().iterator(); i.hasNext(); ) {
1364            List sizeList = (List) i.next();
1365            for (Iterator j = sizeList.iterator(); j.hasNext(); ) {
1366                ThrowableSet s = (ThrowableSet) j.next();
1367                System.err.println(s.toString());
1368        System.err.println("\n\tMemoized Adds:");
1369        for (Iterator k = s.getMemoizedAdds().entrySet().iterator();
1370             k.hasNext(); ) {
1371            Map.Entry entry = (Map.Entry) k.next();
1372            System.err.print(' ');
1373            if (entry.getKey() instanceof ThrowableSet) {
1374            System.err.print(((ThrowableSet) entry.getKey()).toBriefString());
1375            } else {
1376            System.err.print(entry.getKey().toString());
1377            }
1378            System.err.print('=');
1379            System.err.print(((ThrowableSet) entry.getValue()).toBriefString());
1380            System.err.print('\n');
1381        }
1382            }
1383        }
1384    }
1385
1386
1387    // Suite that uses a prescribed order, rather than whatever
1388
// order reflection produces.
1389
public static Test cannedSuite() {
1390    TestSuite suite = new TestSuite();
1391    suite.addTest(new ThrowableSetTest("testInitialState"));
1392    suite.addTest(new ThrowableSetTest("testSingleInstance0"));
1393    suite.addTest(new ThrowableSetTest("testSingleInstance1"));
1394    suite.addTest(new ThrowableSetTest("testAddingSubclasses"));
1395    suite.addTest(new ThrowableSetTest("testAddingSets0"));
1396    suite.addTest(new ThrowableSetTest("testAddingSets1"));
1397    TestSetup setup = new ThrowableSetTestSetup(suite);
1398    return setup;
1399    }
1400    
1401
1402    public static Test reflectionSuite() {
1403    TestSuite suite = new TestSuite(ThrowableSetTest.class);
1404    TestSetup setup = new ThrowableSetTestSetup(suite);
1405    return setup;
1406    }
1407
1408    public static void main(String JavaDoc arg[]) {
1409        if (arg.length > 0) {
1410            jdkLocation = arg[0];
1411        }
1412    Scene.v().loadBasicClasses();
1413    junit.textui.TestRunner.run(reflectionSuite());
1414    System.out.println(ThrowableSet.Manager.v().reportInstrumentation());
1415    }
1416}
1417
Popular Tags