KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > sitraka > ClassFileTest


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
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 org.apache.tools.ant.taskdefs.optional.sitraka;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21
22 import junit.framework.TestCase;
23 import org.apache.tools.ant.util.JavaEnvUtils;
24 import org.apache.tools.ant.taskdefs.optional.sitraka.bytecode.ClassFile;
25 import org.apache.tools.ant.taskdefs.optional.sitraka.bytecode.MethodInfo;
26
27 /**
28  * Nothing special about this testcase...
29  *
30  */

31 public class ClassFileTest extends TestCase {
32     public ClassFileTest(String JavaDoc s) {
33         super(s);
34     }
35
36     public void testVector() throws IOException JavaDoc {
37         String JavaDoc classname = ClassTest.class.getName().replace('.', '/') + ".class";
38         InputStream JavaDoc is = getClass().getClassLoader().getResourceAsStream(classname);
39         assertNotNull("Unable to find resource " + classname + "in caller classloader");
40         ClassFile clazzfile = new ClassFile(is);
41         assertEquals("ClassTest", clazzfile.getName());
42         assertEquals("ClassFileTest.java", clazzfile.getSourceFile());
43         MethodInfo[] methods = clazzfile.getMethods();
44         assertEquals(3, methods.length);
45         assertHasMethod("void <init>()", 1, methods);
46         assertHasMethod("void testTwoLines()", 2, methods);
47         assertHasMethod("void testOneLine()", 3, methods);
48     }
49
50     protected void assertHasMethod(String JavaDoc methodsig, int line, MethodInfo[] methods) {
51         boolean found = false;
52         for (int i = 0; i < methods.length; i++) {
53             MethodInfo method = methods[i];
54             if (methodsig.equals(method.getFullSignature())) {
55
56                 assertTrue(methodsig, method.getNumberOfLines() >= line);
57                 return;
58             }
59         }
60         fail("Could not find method " + methodsig);
61     }
62 }
63
64 class ClassTest {
65
66     // 2 lines
67
public ClassTest() {
68     }
69
70     // 2 lines
71
public void testTwoLines() {
72         System.out.println("This is 1 line");
73     }
74
75     // 1 line
76
public void testOneLine() {
77         try {
78             throw new Exception JavaDoc();
79         } catch (Exception JavaDoc e) {
80         }
81     }
82 }
83
Popular Tags