1 19 package org.netbeans.modules.javacore.jmiimpl; 20 21 import java.io.PrintStream ; 22 import java.lang.reflect.Method ; 23 import java.lang.reflect.Modifier ; 24 import java.util.Iterator ; 25 import java.util.Set ; 26 import java.util.TreeSet ; 27 import javax.jmi.reflect.RefPackage; 28 import junit.textui.TestRunner; 29 import org.netbeans.junit.NbTestCase; 30 import org.netbeans.junit.NbTestSuite; 31 import org.netbeans.mdr.handlers.BaseObjectHandler; 32 import org.netbeans.mdr.persistence.StorageException; 33 import org.netbeans.mdr.storagemodel.StorableBaseObject; 34 import org.netbeans.mdr.storagemodel.StorableClass; 35 import org.netbeans.mdr.storagemodel.TransientStorableObject; 36 import org.netbeans.modules.javacore.api.JavaModel; 37 38 43 public class AbstractMethodsTest extends NbTestCase { 44 private Set methods; 45 46 public static NbTestSuite suite() { 47 return new NbTestSuite(AbstractMethodsTest.class); 48 } 49 50 51 public static void main(java.lang.String [] args) { 52 TestRunner.run(suite()); 53 } 54 55 56 public AbstractMethodsTest(String name) { 57 super(name); 58 } 59 60 public void setUp() { 61 methods = new TreeSet (); 62 } 63 64 public void testAllMethodsAreImplemented() { 65 checkMetamodel(JavaModel.getDefaultExtent()); 66 if (!methods.isEmpty()) { 67 PrintStream log = getLog(); 68 for (Iterator it = methods.iterator(); it.hasNext();) { 69 log.println(it.next()); 70 } 71 assertTrue(methods.size() + " abstract methods found. See log for details.", false); 72 } 73 } 74 75 private void checkMetamodel(RefPackage pkg) { 76 try { 77 for (Iterator it = pkg.refAllPackages().iterator(); it.hasNext();) { 78 Object obj = it.next(); 79 checkMetamodel((RefPackage) obj); 80 checkObject(obj); 81 } 82 for (Iterator it = pkg.refAllAssociations().iterator(); it.hasNext();) { 83 checkObject(it.next()); 84 } 85 for (Iterator it = pkg.refAllClasses().iterator(); it.hasNext();) { 86 Object obj = it.next(); 87 checkObject(obj); 88 checkInstance(obj); 89 } 90 } catch (StorageException e) { 91 throw new RuntimeException (e); 92 } 93 } 94 95 private void checkObject(Object obj) throws StorageException { 96 StorableBaseObject storable = ((BaseObjectHandler) obj)._getDelegate(); 97 checkObject(storable); 98 } 99 100 private void checkObject(StorableBaseObject storable) { 101 Class ifc = BaseObjectHandler.resolveClass(storable); 102 checkClass(BaseObjectHandler.getHandlerClass(ifc, storable)); 103 } 104 105 private void checkInstance(Object obj) throws StorageException { 106 StorableClass storable = (StorableClass) ((BaseObjectHandler) obj)._getDelegate(); 107 108 if (!storable.isAbstract()) { 109 checkObject(new TransientStorableObject(storable.getMdrStorage(), storable.getImmediatePackageId(), storable.getMetaObjectId(), storable.getMofId())); 110 } 111 } 112 113 private void checkClass(Class cls) { 114 try { 115 Method [] methods = cls.getMethods(); 116 for (int i = 0; i < methods.length; i++) { 117 if (Modifier.isAbstract(methods[i].getModifiers())) { 118 this.methods.add(cls.getName() + "." + getMethodSignature(methods[i])); 119 } 120 } 121 } catch (SecurityException e) { 122 throw new RuntimeException (e); 123 } 124 } 125 126 private static String getMethodSignature(Method method) { 127 StringBuffer result = new StringBuffer (); 128 result.append(method.getName() + '('); 129 Class [] paramTypes = method.getParameterTypes(); 130 for (int i = 0; i < paramTypes.length; i++) { 131 result.append(paramTypes[i].getName()); 132 if (i < paramTypes.length - 1) { 133 result.append(", "); 134 } 135 } 136 result.append(')'); 137 return result.toString(); 138 } 139 } 140 | Popular Tags |