Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 import other.Mutant;
     18 import other.InaccessibleClass;
     19 import other.InaccessibleMethod;
     20 
     21 /**
     22  * Test some problematic situations that the verifier detects.
     23  */
     24 public class Main {
     25     public static final boolean VERBOSE = false;
     26 
     27     public static void main(String[] args) {
     28         testClassNewInstance();
     29         testMissingStuff();
     30         testBadAccess();
     31     }
     32 
     33     /**
     34      * Try to create a new instance of an abstract class.
     35      */
     36     static void testClassNewInstance() {
     37         try {
     38             MaybeAbstract ma = new MaybeAbstract();
     39             System.err.println("ERROR: MaybeAbstract succeeded unexpectedly");
     40         } catch (InstantiationError ie) {
     41             System.out.println("Got expected InstantationError");
     42             if (VERBOSE) System.out.println("--- " + ie);
     43         } catch (Exception ex) {
     44             System.err.println("Got unexpected MaybeAbstract failure");
     45         }
     46     }
     47 
     48     /**
     49      * Test stuff that disappears.
     50      */
     51     static void testMissingStuff() {
     52         Mutant mutant = new Mutant();
     53 
     54         try {
     55             int x = mutant.disappearingField;
     56         } catch (NoSuchFieldError nsfe) {
     57             System.out.println("Got expected NoSuchFieldError");
     58             if (VERBOSE) System.out.println("--- " + nsfe);
     59         }
     60 
     61         try {
     62             int y = Mutant.disappearingStaticField;
     63         } catch (NoSuchFieldError nsfe) {
     64             System.out.println("Got expected NoSuchFieldError");
     65             if (VERBOSE) System.out.println("--- " + nsfe);
     66         }
     67 
     68         try {
     69             mutant.disappearingMethod();
     70         } catch (NoSuchMethodError nsme) {
     71             System.out.println("Got expected NoSuchMethodError");
     72             if (VERBOSE) System.out.println("--- " + nsme);
     73         }
     74 
     75         try {
     76             Mutant.disappearingStaticMethod();
     77         } catch (NoSuchMethodError nsme) {
     78             System.out.println("Got expected NoSuchMethodError");
     79             if (VERBOSE) System.out.println("--- " + nsme);
     80         }
     81     }
     82 
     83     /**
     84      * Test stuff that becomes inaccessible.
     85      */
     86     static void testBadAccess() {
     87         Mutant mutant = new Mutant();
     88 
     89         try {
     90             int x = mutant.inaccessibleField;
     91             System.err.println("ERROR: bad access succeeded\n");
     92         } catch (IllegalAccessError iae) {
     93             System.out.println("Got expected IllegalAccessError (ifield)");
     94             if (VERBOSE) System.out.println("--- " + iae);
     95         }
     96 
     97         try {
     98             int y = Mutant.inaccessibleStaticField;
     99             System.err.println("ERROR: bad access succeeded\n");
    100         } catch (IllegalAccessError iae) {
    101             System.out.println("Got expected IllegalAccessError (sfield)");
    102             if (VERBOSE) System.out.println("--- " + iae);
    103         }
    104 
    105         try {
    106             mutant.inaccessibleMethod();
    107             System.err.println("ERROR: bad access succeeded\n");
    108         } catch (IllegalAccessError iae) {
    109             System.out.println("Got expected IllegalAccessError (method)");
    110             if (VERBOSE) System.out.println("--- " + iae);
    111         }
    112 
    113         try {
    114             Mutant.inaccessibleStaticMethod();
    115             System.err.println("ERROR: bad access succeeded\n");
    116         } catch (IllegalAccessError iae) {
    117             System.out.println("Got expected IllegalAccessError (smethod)");
    118             if (VERBOSE) System.out.println("--- " + iae);
    119         }
    120 
    121         try {
    122             /* accessible static method in an inaccessible class */
    123             InaccessibleClass.test();
    124             System.err.println("ERROR: bad meth-class access succeeded\n");
    125         } catch (IllegalAccessError iae) {
    126             System.out.println("Got expected IllegalAccessError (meth-class)");
    127             if (VERBOSE) System.out.println("--- " + iae);
    128         }
    129 
    130         try {
    131             /* accessible static field in an inaccessible class */
    132             int blah = InaccessibleClass.blah;
    133             System.err.println("ERROR: bad field-class access succeeded\n");
    134         } catch (IllegalAccessError iae) {
    135             System.out.println("Got expected IllegalAccessError (field-class)");
    136             if (VERBOSE) System.out.println("--- " + iae);
    137         }
    138 
    139         try {
    140             /* inaccessible static method in an accessible class */
    141             InaccessibleMethod.test();
    142             System.err.println("ERROR: bad access succeeded\n");
    143         } catch (IllegalAccessError iae) {
    144             System.out.println("Got expected IllegalAccessError (meth-meth)");
    145             if (VERBOSE) System.out.println("--- " + iae);
    146         }
    147     }
    148 }
    149