1 19 package org.netbeans.jmi.javamodel.getters; 20 21 import java.io.PrintStream ; 22 import java.lang.reflect.Modifier ; 23 import java.util.Collections ; 24 import java.util.Iterator ; 25 import java.util.List ; 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 37 public class ThrowsClauseTest extends NbTestCase { 38 39 JavaClass clazz; 40 JavaModelPackage pkg; 41 JavaMetamodel model; 42 43 private String resultSource = 44 "doTest: Found in resource 'java/lang/Object.java'." + 45 "Method 'wait' found." + 46 "throws:" + 47 "'java.lang.InterruptedException'"; 48 private String resultClass = 49 "doTest: Found in resource 'java/lang/Object.class'." + 50 "Method 'wait' found." + 51 "throws:" + 52 "'java.lang.InterruptedException'"; 53 54 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 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 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 107 public static void main(String [] args) { 108 TestRunner.run(suite()); 109 } 110 111 private void doTest(String result) { 115 StringBuffer buf = new StringBuffer (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 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 |