ClassTest2.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.luni.tests.java.lang; 19 20import dalvik.annotation.TestTargets; 21import dalvik.annotation.TestLevel; 22import dalvik.annotation.TestTargetNew; 23import dalvik.annotation.TestTargetClass; 24 25import java.io.IOException; 26import java.io.InputStream; 27 28@TestTargetClass(Class.class) 29public class ClassTest2 extends junit.framework.TestCase { 30 31 /** 32 * Sets up the fixture, for example, open a network connection. This method 33 * is called before a test is executed. 34 */ 35 protected void setUp() { 36 } 37 38 /** 39 * Tears down the fixture, for example, close a network connection. This 40 * method is called after a test is executed. 41 */ 42 protected void tearDown() { 43 } 44 45 /** 46 * Tests loading a resource with a relative name. 47 */ 48 @TestTargetNew( 49 level = TestLevel.PARTIAL_COMPLETE, 50 notes = "", 51 method = "getResourceAsStream", 52 args = {java.lang.String.class} 53 ) 54 public void testGetResourceAsStream1() throws IOException { 55 Class clazz = getClass(); 56 57 InputStream stream = clazz.getResourceAsStream("HelloWorld.txt"); 58 assert(stream != null); 59 60 byte[] buffer = new byte[20]; 61 int length = stream.read(buffer); 62 String s = new String(buffer, 0, length); 63 assert("Hello, World.".equals(s)); 64 65 stream.close(); 66 } 67 68 /** 69 * Tests loading a resource with a global name. 70 */ 71 @TestTargetNew( 72 level = TestLevel.PARTIAL_COMPLETE, 73 notes = "", 74 method = "getResourceAsStream", 75 args = {java.lang.String.class} 76 ) 77 public void testGetResourceAsStream2() throws IOException { 78 Class clazz = getClass(); 79 80 InputStream stream = clazz.getResourceAsStream("/org/apache/harmony/luni/tests/java/lang/HelloWorld.txt"); 81 assert(stream != null); 82 83 byte[] buffer = new byte[20]; 84 int length = stream.read(buffer); 85 String s = new String(buffer, 0, length); 86 assert("Hello, World.".equals(s)); 87 88 stream.close(); 89 90 try { 91 clazz.getResourceAsStream(null); 92 fail("NullPointerException is not thrown."); 93 } catch(NullPointerException npe) { 94 //expected 95 } 96 assertNull(clazz.getResourceAsStream("/NonExistentResource")); 97 assertNull(clazz.getResourceAsStream("org/apache/harmony/luni/tests/java/lang/HelloWorld.txt")); 98 } 99} 100