GenericSignatureFormatErrorTest.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
1package tests.api.java.lang.reflect;
2
3import dalvik.annotation.AndroidOnly;
4import dalvik.annotation.TestLevel;
5import dalvik.annotation.TestTargetClass;
6import dalvik.annotation.TestTargetNew;
7import dalvik.system.DexFile;
8
9import junit.framework.TestCase;
10
11import java.io.File;
12import java.io.FileOutputStream;
13import java.io.IOException;
14import java.io.InputStream;
15import java.io.OutputStream;
16import java.lang.reflect.GenericSignatureFormatError;
17import java.lang.reflect.TypeVariable;
18
19@TestTargetClass(GenericSignatureFormatError.class)
20public class GenericSignatureFormatErrorTest extends TestCase{
21
22    @TestTargetNew(
23        level = TestLevel.PARTIAL_COMPLETE,
24        notes = "",
25        method = "GenericSignatureFormatError",
26        args = {}
27    )
28    public void test_Constructor() {
29        assertNotNull(new GenericSignatureFormatError());
30    }
31
32    @TestTargetNew(
33        level = TestLevel.PARTIAL_COMPLETE,
34        notes = "",
35        method = "GenericSignatureFormatError",
36        args = {}
37    )
38    public void test_readResource() throws Exception {
39        File tf = File.createTempFile("classes", ".dex");
40        // System.out.println("GenericSignatureFormatErrorTest:"
41        //         +tf.getAbsolutePath()+", canRead: "+tf.canRead()
42        //         +", canWrite: "+tf.canWrite());
43        InputStream is = this.getClass().getResourceAsStream("dex1.bytes");
44        assertNotNull(is);
45    }
46
47
48    @TestTargetNew(
49        level = TestLevel.PARTIAL_COMPLETE,
50        notes = "",
51        method = "GenericSignatureFormatError",
52        args = {}
53    )
54    @AndroidOnly("Uses Android specific class dalvik.system.DexFile " +
55            "for loading classes.")
56    public void test_signatureFormatError() throws Exception {
57        /*
58         * dex1.bytes is a jar file with a classes.dex in it.
59         * the classes.dex was javac'ed, dx'ed and patched
60         * with the following java file:
61         *
62         * package demo;
63         *  public class HelloWorld<U> {
64         *      public HelloWorld(U t) {}
65         *  }
66         *
67         * patch:
68         * the string constant (class generics signature string)
69         *  "<U:" was changed to "<<:"
70         *
71         */
72
73        File tf = File.createTempFile("classes", ".dex");
74        // System.out.println("GenericSignatureFormatErrorTest:" +
75        //         tf.getAbsolutePath() + ", canRead: " + tf.canRead() +
76        //         ", canWrite: "+tf.canWrite());
77        InputStream is = this.getClass().getResourceAsStream("dex1.bytes");
78        assertNotNull(is);
79        OutputStream fos = new FileOutputStream(tf);
80        copy(is, fos);
81        fos.close();
82
83
84        // class signature string "<U:" was changed to "<<:"
85        //System.out.println("file length:"+tf.length());
86        try {
87            DexFile df = new DexFile(tf);
88            Class clazz = df.loadClass("demo/HelloWorld", this.getClass().getClassLoader());
89            TypeVariable[] tvs = clazz.getTypeParameters();
90            fail("expecting a GenericSignatureFormatError");
91            // for (TypeVariable tv : tvs) {
92            //     System.out.println("tv:"+tv.toString());
93            // }
94        } catch (GenericSignatureFormatError gsfe) {
95            // expected
96        }
97    }
98
99    private void copy(InputStream is, OutputStream os) {
100        try {
101            int b;
102            while ((b = is.read()) != -1) {
103                os.write(b);
104            }
105            is.close();
106        } catch (IOException ex) {
107            throw new RuntimeException("io error", ex);
108        }
109    }
110}
111