KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > jmi > javamodel > getters > ThrowsClauseTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.jmi.javamodel.getters;
20
21 import java.io.PrintStream JavaDoc;
22 import java.lang.reflect.Modifier JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import junit.textui.TestRunner;
27 import org.netbeans.jmi.javamodel.*;
28 import org.netbeans.jmi.javamodel.codegen.Utility;
29 import org.netbeans.junit.NbTestCase;
30 import org.netbeans.junit.NbTestSuite;
31 import org.netbeans.modules.javacore.ClassIndex;
32 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
33
34 /**
35  * @author Pavel Flaska
36  */

37 public class ThrowsClauseTest extends NbTestCase {
38     
39     JavaClass clazz;
40     JavaModelPackage pkg;
41     JavaMetamodel model;
42     
43     private String JavaDoc resultSource =
44         "doTest: Found in resource 'java/lang/Object.java'." +
45         "Method 'wait' found." +
46         "throws:" +
47         "'java.lang.InterruptedException'";
48     private String JavaDoc resultClass =
49         "doTest: Found in resource 'java/lang/Object.class'." +
50         "Method 'wait' found." +
51         "throws:" +
52         "'java.lang.InterruptedException'";
53     
54     /** Creates a new instance of StringMethodsSigTest */
55     public ThrowsClauseTest() {
56         super("ThrowsClauseTest");
57     }
58
59     protected void setUp() {
60         clazz = (JavaClass) Utility.findClass("examples.colorpicker.ColorPicker");
61         pkg = (JavaModelPackage) clazz.refImmediatePackage();
62         model = JavaMetamodel.getManager();
63     }
64     
65     public static NbTestSuite suite() {
66         NbTestSuite suite = new NbTestSuite(ThrowsClauseTest.class);
67         return suite;
68     }
69
70     /**
71      * Tries to find String source file in src.zip, resolves it and check if
72      * correct equals and hashCode methods are resolved.
73      */

74     public void testThrowsFromSources() {
75         boolean fail = true;
76         Utility.beginTrans(true);
77         try {
78             model.setClassPath(model.getFileObject(clazz.getResource()), true);
79             doTest(resultSource);
80             fail = false;
81         }
82         finally {
83             Utility.endTrans(fail);
84         }
85     }
86     
87     /**
88      * Tries to find String class in rt.jar, resolves it and check if
89      * correct equals and hashCode methods are resolved.
90      */

91     public void testThrowsFromClasses() {
92         boolean fail = true;
93         Utility.beginTrans(true);
94         try {
95             model.setClassPath(model.getFileObject(clazz.getResource()));
96             doTest(resultClass);
97             fail = false;
98         }
99         finally {
100             Utility.endTrans(fail);
101         }
102     }
103     
104     /**
105      * @param args the command line arguments
106      */

107     public static void main(String JavaDoc[] args) {
108         TestRunner.run(suite());
109     }
110     
111     ////////////////////////////////////////////////////////////////////////////
112
// PRIVATE METHODS
113
////////////////////////////////////////////////////////////////////////////
114
private void doTest(String JavaDoc result) {
115         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(487);
116         JavaClass strType = (JavaClass) model.getDefaultExtent().getType().resolve("java.lang.Object");
117         Resource res = strType.getResource();
118         buf.append("doTest: Found in resource '" + res.getName() + "'.");
119         Method method = strType.getMethod("wait", Collections.EMPTY_LIST, false);
120         assertNotNull(method);
121         buf.append("Method '" + method.getName() + "' found.");
122         buf.append("throws:");
123         for (Iterator JavaDoc it = method.getExceptions().iterator(); it.hasNext(); ) {
124             buf.append("'" + ((JavaClass) it.next()).getName() + "'");
125         }
126         getLog().println(buf);
127         assertEquals("Error.", buf.toString(), result);
128     }
129     
130 }
131
Popular Tags