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 41 public class StringMethodsSigTest extends NbTestCase { 42 43 JavaClass clazz; 44 JavaModelPackage pkg; 45 JavaMetamodel model; 46 47 48 public StringMethodsSigTest() { 49 super("StringMethodsSigTest"); 50 } 51 52 protected void setUp() { 53 clazz = (JavaClass) Utility.findClass("examples.colorpicker.ColorPicker"); 54 pkg = (JavaModelPackage) clazz.refImmediatePackage(); 55 model = JavaMetamodel.getManager(); 56 } 57 58 public static NbTestSuite suite() { 59 NbTestSuite suite = new NbTestSuite(StringMethodsSigTest.class); 60 return suite; 61 } 62 63 67 public void testMethodsFromSources() { 68 boolean fail = true; 69 Utility.beginTrans(true); 70 try { 71 model.setClassPath(model.getFileObject(clazz.getResource()), true); 72 doTest(); 73 fail = false; 74 } 75 finally { 76 Utility.endTrans(fail); 77 } 78 } 79 80 public void testMethodsFromSources2() { 81 boolean fail = true; 82 Utility.beginTrans(true); 83 try { 84 model.setClassPath(model.getFileObject(clazz.getResource()), true); 85 doTest2(); 86 fail = false; 87 } 88 finally { 89 Utility.endTrans(fail); 90 } 91 } 92 93 97 public void testMethodsFromClasses() { 98 boolean fail = true; 99 Utility.beginTrans(true); 100 try { 101 model.setClassPath(model.getFileObject(clazz.getResource())); 102 doTest(); 103 fail = false; 104 } 105 finally { 106 Utility.endTrans(fail); 107 } 108 } 109 110 public void testMethodsFromClasses2() { 111 boolean fail = true; 112 Utility.beginTrans(true); 113 try { 114 model.setClassPath(model.getFileObject(clazz.getResource())); 115 doTest2(); 116 fail = false; 117 } 118 finally { 119 Utility.endTrans(fail); 120 } 121 } 122 123 126 public static void main(String [] args) { 127 TestRunner.run(suite()); 128 } 129 130 private void doTest() { 134 PrintStream log = getLog(); 135 JavaClass strType = (JavaClass) model.getDefaultExtent().getType().resolve("java.lang.String"); 136 Resource res = strType.getResource(); 137 log.println("doTest: Found in resource '" + res.getName() + "'."); 138 Method method = strType.getMethod("hashCode", Collections.EMPTY_LIST, false); 139 assertNotNull(method); 140 log.println("\tMethod '" + method.getName() + "' found."); 141 log.println("\t\tisFinal: '" + Modifier.isFinal(method.getModifiers()) + "'."); 142 assertEquals(true, Modifier.isFinal(method.getModifiers())); 143 Type objectType = model.getDefaultExtent().getType().resolve("java.lang.Object"); 144 method = strType.getMethod("equals", Collections.singletonList(objectType), false); 145 log.println("\tMethod '" + method.getName() + "' found."); 146 assertNotNull(method); 147 } 148 149 private void doTest2() { 150 PrintStream log = getLog(); 151 JavaClass strType = (JavaClass) model.getDefaultExtent().getType().resolve("java.lang.String"); 152 ParameterizedType parType = model.getDefaultExtent().getParameterizedType().resolveParameterizedType(strType, null, null); 153 Resource res = strType.getResource(); 154 log.println("doTest2: Found in resource '" + res.getName() + "'."); 155 Method method = findMethod(parType, "hashCode"); 156 assertNotNull(method); 157 log.println("\tMethod '" + method.getName() + "' found."); 158 log.println("\t\tisFinal: '" + Modifier.isFinal(method.getModifiers()) + "'."); 159 assertEquals(true, Modifier.isFinal(method.getModifiers())); 160 Type objectType = model.getDefaultExtent().getType().resolve("java.lang.Object"); 161 method = findMethod(parType, "equals"); 162 log.println("\tMethod '" + method.getName() + "' found."); 163 assertNotNull(method); 164 } 165 166 private Method findMethod(ParameterizedType type, String name) { 167 List features = type.getFeatures(); 168 for (Iterator it = features.iterator(); it.hasNext(); ) { 169 Feature feature = (Feature) it.next(); 170 if (name.equals(feature.getName()) && (feature instanceof Method)) { 171 return (Method) feature; 172 } 173 } 174 return null; 175 } 176 } 177 | Popular Tags |