1/* 2 * Copyright (C) 2008 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 17package android.core; 18 19import junit.framework.TestCase; 20 21import java.lang.reflect.InvocationTargetException; 22import java.lang.reflect.Method; 23 24import android.test.suitebuilder.annotation.Suppress; 25 26/** 27 * Test for basic ClassLoader functionality. 28 */ 29@Suppress 30public class ClassLoaderTest extends TestCase { 31 /* 32 package my.pkg; 33 public class CLTest { 34 public CLTest() {} 35 36 public String test() { return "This is test 1"; } 37 } 38 */ 39 static private byte[] test1class = { 40 (byte) 0xca, (byte) 0xfe, (byte) 0xba, (byte) 0xbe, 41 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x31, 42 (byte) 0x00, (byte) 0x11, (byte) 0x0a, (byte) 0x00, 43 (byte) 0x04, (byte) 0x00, (byte) 0x0d, (byte) 0x08, 44 (byte) 0x00, (byte) 0x0e, (byte) 0x07, (byte) 0x00, 45 (byte) 0x0f, (byte) 0x07, (byte) 0x00, (byte) 0x10, 46 (byte) 0x01, (byte) 0x00, (byte) 0x06, (byte) 0x3c, 47 (byte) 0x69, (byte) 0x6e, (byte) 0x69, (byte) 0x74, 48 (byte) 0x3e, (byte) 0x01, (byte) 0x00, (byte) 0x03, 49 (byte) 0x28, (byte) 0x29, (byte) 0x56, (byte) 0x01, 50 (byte) 0x00, (byte) 0x04, (byte) 0x43, (byte) 0x6f, 51 (byte) 0x64, (byte) 0x65, (byte) 0x01, (byte) 0x00, 52 (byte) 0x0f, (byte) 0x4c, (byte) 0x69, (byte) 0x6e, 53 (byte) 0x65, (byte) 0x4e, (byte) 0x75, (byte) 0x6d, 54 (byte) 0x62, (byte) 0x65, (byte) 0x72, (byte) 0x54, 55 (byte) 0x61, (byte) 0x62, (byte) 0x6c, (byte) 0x65, 56 (byte) 0x01, (byte) 0x00, (byte) 0x04, (byte) 0x74, 57 (byte) 0x65, (byte) 0x73, (byte) 0x74, (byte) 0x01, 58 (byte) 0x00, (byte) 0x14, (byte) 0x28, (byte) 0x29, 59 (byte) 0x4c, (byte) 0x6a, (byte) 0x61, (byte) 0x76, 60 (byte) 0x61, (byte) 0x2f, (byte) 0x6c, (byte) 0x61, 61 (byte) 0x6e, (byte) 0x67, (byte) 0x2f, (byte) 0x53, 62 (byte) 0x74, (byte) 0x72, (byte) 0x69, (byte) 0x6e, 63 (byte) 0x67, (byte) 0x3b, (byte) 0x01, (byte) 0x00, 64 (byte) 0x0a, (byte) 0x53, (byte) 0x6f, (byte) 0x75, 65 (byte) 0x72, (byte) 0x63, (byte) 0x65, (byte) 0x46, 66 (byte) 0x69, (byte) 0x6c, (byte) 0x65, (byte) 0x01, 67 (byte) 0x00, (byte) 0x0b, (byte) 0x43, (byte) 0x4c, 68 (byte) 0x54, (byte) 0x65, (byte) 0x73, (byte) 0x74, 69 (byte) 0x2e, (byte) 0x6a, (byte) 0x61, (byte) 0x76, 70 (byte) 0x61, (byte) 0x0c, (byte) 0x00, (byte) 0x05, 71 (byte) 0x00, (byte) 0x06, (byte) 0x01, (byte) 0x00, 72 (byte) 0x0e, (byte) 0x54, (byte) 0x68, (byte) 0x69, 73 (byte) 0x73, (byte) 0x20, (byte) 0x69, (byte) 0x73, 74 (byte) 0x20, (byte) 0x74, (byte) 0x65, (byte) 0x73, 75 (byte) 0x74, (byte) 0x20, (byte) 0x31, (byte) 0x01, 76 (byte) 0x00, (byte) 0x0d, (byte) 0x6d, (byte) 0x79, 77 (byte) 0x2f, (byte) 0x70, (byte) 0x6b, (byte) 0x67, 78 (byte) 0x2f, (byte) 0x43, (byte) 0x4c, (byte) 0x54, 79 (byte) 0x65, (byte) 0x73, (byte) 0x74, (byte) 0x01, 80 (byte) 0x00, (byte) 0x10, (byte) 0x6a, (byte) 0x61, 81 (byte) 0x76, (byte) 0x61, (byte) 0x2f, (byte) 0x6c, 82 (byte) 0x61, (byte) 0x6e, (byte) 0x67, (byte) 0x2f, 83 (byte) 0x4f, (byte) 0x62, (byte) 0x6a, (byte) 0x65, 84 (byte) 0x63, (byte) 0x74, (byte) 0x00, (byte) 0x21, 85 (byte) 0x00, (byte) 0x03, (byte) 0x00, (byte) 0x04, 86 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 87 (byte) 0x00, (byte) 0x02, (byte) 0x00, (byte) 0x01, 88 (byte) 0x00, (byte) 0x05, (byte) 0x00, (byte) 0x06, 89 (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x07, 90 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x1d, 91 (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, 92 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x05, 93 (byte) 0x2a, (byte) 0xb7, (byte) 0x00, (byte) 0x01, 94 (byte) 0xb1, (byte) 0x00, (byte) 0x00, (byte) 0x00, 95 (byte) 0x01, (byte) 0x00, (byte) 0x08, (byte) 0x00, 96 (byte) 0x00, (byte) 0x00, (byte) 0x06, (byte) 0x00, 97 (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, 98 (byte) 0x04, (byte) 0x00, (byte) 0x01, (byte) 0x00, 99 (byte) 0x09, (byte) 0x00, (byte) 0x0a, (byte) 0x00, 100 (byte) 0x01, (byte) 0x00, (byte) 0x07, (byte) 0x00, 101 (byte) 0x00, (byte) 0x00, (byte) 0x1b, (byte) 0x00, 102 (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, 103 (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x12, 104 (byte) 0x02, (byte) 0xb0, (byte) 0x00, (byte) 0x00, 105 (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x08, 106 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x06, 107 (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, 108 (byte) 0x00, (byte) 0x06, (byte) 0x00, (byte) 0x01, 109 (byte) 0x00, (byte) 0x0b, (byte) 0x00, (byte) 0x00, 110 (byte) 0x00, (byte) 0x02, (byte) 0x00, (byte) 0x0c 111 }; 112 113 /* 114 package my.pkg; 115 public class CLTest { 116 public CLTest() {} 117 118 public String test() { return "This is test 2"; } 119 } 120 */ 121 static private byte[] test2class = { 122 (byte) 0xca, (byte) 0xfe, (byte) 0xba, (byte) 0xbe, 123 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x31, 124 (byte) 0x00, (byte) 0x11, (byte) 0x0a, (byte) 0x00, 125 (byte) 0x04, (byte) 0x00, (byte) 0x0d, (byte) 0x08, 126 (byte) 0x00, (byte) 0x0e, (byte) 0x07, (byte) 0x00, 127 (byte) 0x0f, (byte) 0x07, (byte) 0x00, (byte) 0x10, 128 (byte) 0x01, (byte) 0x00, (byte) 0x06, (byte) 0x3c, 129 (byte) 0x69, (byte) 0x6e, (byte) 0x69, (byte) 0x74, 130 (byte) 0x3e, (byte) 0x01, (byte) 0x00, (byte) 0x03, 131 (byte) 0x28, (byte) 0x29, (byte) 0x56, (byte) 0x01, 132 (byte) 0x00, (byte) 0x04, (byte) 0x43, (byte) 0x6f, 133 (byte) 0x64, (byte) 0x65, (byte) 0x01, (byte) 0x00, 134 (byte) 0x0f, (byte) 0x4c, (byte) 0x69, (byte) 0x6e, 135 (byte) 0x65, (byte) 0x4e, (byte) 0x75, (byte) 0x6d, 136 (byte) 0x62, (byte) 0x65, (byte) 0x72, (byte) 0x54, 137 (byte) 0x61, (byte) 0x62, (byte) 0x6c, (byte) 0x65, 138 (byte) 0x01, (byte) 0x00, (byte) 0x04, (byte) 0x74, 139 (byte) 0x65, (byte) 0x73, (byte) 0x74, (byte) 0x01, 140 (byte) 0x00, (byte) 0x14, (byte) 0x28, (byte) 0x29, 141 (byte) 0x4c, (byte) 0x6a, (byte) 0x61, (byte) 0x76, 142 (byte) 0x61, (byte) 0x2f, (byte) 0x6c, (byte) 0x61, 143 (byte) 0x6e, (byte) 0x67, (byte) 0x2f, (byte) 0x53, 144 (byte) 0x74, (byte) 0x72, (byte) 0x69, (byte) 0x6e, 145 (byte) 0x67, (byte) 0x3b, (byte) 0x01, (byte) 0x00, 146 (byte) 0x0a, (byte) 0x53, (byte) 0x6f, (byte) 0x75, 147 (byte) 0x72, (byte) 0x63, (byte) 0x65, (byte) 0x46, 148 (byte) 0x69, (byte) 0x6c, (byte) 0x65, (byte) 0x01, 149 (byte) 0x00, (byte) 0x0b, (byte) 0x43, (byte) 0x4c, 150 (byte) 0x54, (byte) 0x65, (byte) 0x73, (byte) 0x74, 151 (byte) 0x2e, (byte) 0x6a, (byte) 0x61, (byte) 0x76, 152 (byte) 0x61, (byte) 0x0c, (byte) 0x00, (byte) 0x05, 153 (byte) 0x00, (byte) 0x06, (byte) 0x01, (byte) 0x00, 154 (byte) 0x0e, (byte) 0x54, (byte) 0x68, (byte) 0x69, 155 (byte) 0x73, (byte) 0x20, (byte) 0x69, (byte) 0x73, 156 (byte) 0x20, (byte) 0x74, (byte) 0x65, (byte) 0x73, 157 (byte) 0x74, (byte) 0x20, (byte) 0x32, (byte) 0x01, 158 (byte) 0x00, (byte) 0x0d, (byte) 0x6d, (byte) 0x79, 159 (byte) 0x2f, (byte) 0x70, (byte) 0x6b, (byte) 0x67, 160 (byte) 0x2f, (byte) 0x43, (byte) 0x4c, (byte) 0x54, 161 (byte) 0x65, (byte) 0x73, (byte) 0x74, (byte) 0x01, 162 (byte) 0x00, (byte) 0x10, (byte) 0x6a, (byte) 0x61, 163 (byte) 0x76, (byte) 0x61, (byte) 0x2f, (byte) 0x6c, 164 (byte) 0x61, (byte) 0x6e, (byte) 0x67, (byte) 0x2f, 165 (byte) 0x4f, (byte) 0x62, (byte) 0x6a, (byte) 0x65, 166 (byte) 0x63, (byte) 0x74, (byte) 0x00, (byte) 0x21, 167 (byte) 0x00, (byte) 0x03, (byte) 0x00, (byte) 0x04, 168 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 169 (byte) 0x00, (byte) 0x02, (byte) 0x00, (byte) 0x01, 170 (byte) 0x00, (byte) 0x05, (byte) 0x00, (byte) 0x06, 171 (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x07, 172 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x1d, 173 (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, 174 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x05, 175 (byte) 0x2a, (byte) 0xb7, (byte) 0x00, (byte) 0x01, 176 (byte) 0xb1, (byte) 0x00, (byte) 0x00, (byte) 0x00, 177 (byte) 0x01, (byte) 0x00, (byte) 0x08, (byte) 0x00, 178 (byte) 0x00, (byte) 0x00, (byte) 0x06, (byte) 0x00, 179 (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, 180 (byte) 0x04, (byte) 0x00, (byte) 0x01, (byte) 0x00, 181 (byte) 0x09, (byte) 0x00, (byte) 0x0a, (byte) 0x00, 182 (byte) 0x01, (byte) 0x00, (byte) 0x07, (byte) 0x00, 183 (byte) 0x00, (byte) 0x00, (byte) 0x1b, (byte) 0x00, 184 (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, 185 (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x12, 186 (byte) 0x02, (byte) 0xb0, (byte) 0x00, (byte) 0x00, 187 (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x08, 188 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x06, 189 (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, 190 (byte) 0x00, (byte) 0x06, (byte) 0x00, (byte) 0x01, 191 (byte) 0x00, (byte) 0x0b, (byte) 0x00, (byte) 0x00, 192 (byte) 0x00, (byte) 0x02, (byte) 0x00, (byte) 0x0c 193 }; 194 195 /* 196 * Custom class loader. 197 */ 198 private class MyLoader extends ClassLoader { 199 public MyLoader(byte[] data) { 200 super(); 201 mData = data; 202 } 203 204 protected Class<?> findClass(String name) throws ClassNotFoundException { 205 assertEquals("my.pkg.CLTest", name); 206 return defineClass(name, mData, 0, mData.length); 207 } 208 209 byte[] mData; 210 } 211 212 213 /* 214 * Simple test: manually load two class files that have the same class 215 * name but different contents. 216 */ 217 public void testClassLoader() throws Exception { 218 Class test1, test2; 219 MyLoader loader1 = new MyLoader(test1class); 220 MyLoader loader2 = new MyLoader(test2class); 221 222 test1 = loader1.loadClass("my.pkg.CLTest"); 223 test2 = loader2.loadClass("my.pkg.CLTest"); 224 225 methodTest(test1, "This is test 1"); 226 methodTest(test2, "This is test 2"); 227 } 228 229 /* 230 * Invoke the test() method and verify that the string returned 231 * matches what we expect. 232 */ 233 private static void methodTest(Class clazz, String expect) 234 throws NoSuchMethodException, InstantiationException, 235 IllegalAccessException, InvocationTargetException { 236 Method meth = clazz.getMethod("test", (Class[]) null); 237 Object obj = clazz.newInstance(); 238 Object result = meth.invoke(obj, (Object[]) null); 239 240 assertEquals(result, expect); 241 } 242} 243 244