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