Home | History | Annotate | Download | only in loading-test-jar
      1 /*
      2  * Copyright (C) 2011 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 package test;
     18 
     19 import test2.Target2;
     20 import java.io.ByteArrayOutputStream;
     21 import java.io.IOException;
     22 import java.io.InputStream;
     23 
     24 /**
     25  * Class used as part of the class loading tests. This class uses
     26  * other classes, some of which should have come from the same jar/dex
     27  * file and others of which should have come from a different jar/dex
     28  * file. Each test method in this class is called from the same-named
     29  * method in {@code DexClassLoaderTest}.
     30  */
     31 public class TestMethods {
     32     /*
     33      * The following are all simple utility methods which, under
     34      * normal circumstances, would be part of other libraries
     35      * (specifically, JUnit and the Android libcore support library).
     36      *
     37      * However, this file gets compiled as an independent unit without
     38      * any dependencies. We could theoretically add dependencies on other
     39      * libraries, but that would make things much more complicated and
     40      * fragile, and for very little benefit.
     41      */
     42 
     43     /**
     44      * Simple sameness assertion checker.
     45      */
     46     public static void assertSame(Object expected, Object actual) {
     47         if (expected != actual) {
     48             throw new RuntimeException(
     49                 "EXPECTED: " + expected + "; ACTUAL: " + actual);
     50         }
     51     }
     52 
     53     /**
     54      * Simple sameness assertion checker.
     55      */
     56     public static void assertSame(int expected, int actual) {
     57         if (expected != actual) {
     58             throw new RuntimeException(
     59                 "EXPECTED: " + expected + "; ACTUAL: " + actual);
     60         }
     61     }
     62 
     63     /**
     64      * Fully read the contents of the given stream.
     65      */
     66     public static byte[] readFully(InputStream in) throws IOException {
     67         // This is a copy of the same-named method in libcore.io.Streams.
     68         byte[] buffer = new byte[1024];
     69         ByteArrayOutputStream bytes = new ByteArrayOutputStream();
     70         while (true) {
     71             int byteCount = in.read(buffer);
     72             if (byteCount == -1) {
     73                 return bytes.toByteArray();
     74             }
     75             bytes.write(buffer, 0, byteCount);
     76         }
     77     }
     78 
     79     /*
     80      * Test methods that use another class from the same dex/jar file
     81      */
     82 
     83     /**
     84      * Test that an instance of a sibling class can be constructed.
     85      */
     86     public static void test_constructor() {
     87         new Target();
     88     }
     89 
     90     /**
     91      * Test calling a static method on a sibling class.
     92      */
     93     public static void test_callStaticMethod() {
     94         assertSame("blort", Target.blort());
     95     }
     96 
     97     /**
     98      * Test getting a static variable of a sibling class.
     99      */
    100     public static void test_getStaticVariable() {
    101         Target.setStaticVariable(22);
    102         assertSame(22, Target.staticVariable);
    103     }
    104 
    105     /**
    106      * Test calling an instance method on a sibling class.
    107      */
    108     public static void test_callInstanceMethod() {
    109         Target target = new Target();
    110         assertSame("zorch", target.zorch());
    111     }
    112 
    113     /**
    114      * Test getting an instance variable of a sibling class.
    115      */
    116     public static void test_getInstanceVariable() {
    117         Target target = new Target();
    118         target.setInstanceVariable(10098);
    119         assertSame(10098, target.instanceVariable);
    120     }
    121 
    122     /**
    123      * Test getting a resource which should be in the same jar
    124      * file as this class.
    125      */
    126     public static void test_getResourceAsStream() throws IOException {
    127         ClassLoader cl = TestMethods.class.getClassLoader();
    128         InputStream in = cl.getResourceAsStream("test/Resource1.txt");
    129         byte[] contents = readFully(in);
    130         String s = new String(contents, "UTF-8");
    131 
    132         assertSame("Muffins are tasty!\n", s.intern());
    133     }
    134 
    135     /*
    136      * Test methods that use a class from a different dex/jar file
    137      */
    138 
    139     /**
    140      * Test that an instance of a cousin class can be constructed.
    141      */
    142     public static void test_diff_constructor() {
    143         new Target2();
    144     }
    145 
    146     /**
    147      * Test calling a static method on a cousin class.
    148      */
    149     public static void test_diff_callStaticMethod() {
    150         assertSame("frotz", Target2.frotz());
    151     }
    152 
    153     /**
    154      * Test getting a static variable of a cousin class.
    155      */
    156     public static void test_diff_getStaticVariable() {
    157         Target2.setStaticIgram(220);
    158         assertSame(220, Target2.staticIgram);
    159     }
    160 
    161     /**
    162      * Test calling an instance method on a cousin class.
    163      */
    164     public static void test_diff_callInstanceMethod() {
    165         Target2 target = new Target2();
    166         assertSame("fizmo", target.fizmo());
    167     }
    168 
    169     /**
    170      * Test getting an instance variable of a cousin class.
    171      */
    172     public static void test_diff_getInstanceVariable() {
    173         Target2 target = new Target2();
    174         target.setInstanceMagri(10098);
    175         assertSame(10098, target.instanceMagri);
    176     }
    177 
    178     /**
    179      * Test getting a resource which should be in a different jar
    180      * file as this class.
    181      */
    182     public static void test_diff_getResourceAsStream() throws IOException {
    183         ClassLoader cl = TestMethods.class.getClassLoader();
    184         InputStream in = cl.getResourceAsStream("test2/Resource2.txt");
    185         byte[] contents = readFully(in);
    186         String s = new String(contents, "UTF-8");
    187 
    188         assertSame("Who doesn't like a good biscuit?\n", s.intern());
    189     }
    190 }
    191