1 20 package org.netbeans.modules.javacore.parsing; 21 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 import junit.textui.TestRunner; 25 import org.netbeans.jmi.javamodel.JavaClass; 26 import org.netbeans.jmi.javamodel.JavaModelPackage; 27 import org.netbeans.jmi.javamodel.NamedElement; 28 import org.netbeans.jmi.javamodel.codegen.Utility; 29 import org.netbeans.junit.NbTestCase; 30 import org.netbeans.junit.NbTestSuite; 31 import org.netbeans.lib.java.parser.ASTree; 32 import org.netbeans.lib.java.parser.Token; 33 import org.netbeans.modules.javacore.jmiimpl.javamodel.MetadataElement; 34 import org.openide.filesystems.FileStateInvalidException; 35 36 42 public class TokenIdentityTest extends NbTestCase { 43 44 private static final boolean DEBUG = true; 45 46 47 public TokenIdentityTest() { 48 super("TokenIdentityTest"); 49 } 50 51 public static NbTestSuite suite() { 52 NbTestSuite suite = new NbTestSuite(TokenIdentityTest.class); 53 return suite; 54 } 55 56 JavaModelPackage pkg; 57 58 protected void setUp() throws FileStateInvalidException { 59 JavaClass clazz = Utility.findClass("org.netbeans.test.codegen.MethodTest1"); 60 pkg = (JavaModelPackage) clazz.refImmediatePackage(); 61 } 62 63 public void test() { 64 boolean fail = false; 65 int files = 0; 66 Utility.beginTrans(false); 67 try { 68 Collection resources = pkg.getResource().refAllOfClass(); 69 for (Iterator it = resources.iterator(); it.hasNext(); ) { 70 MetadataElement element = (MetadataElement) it.next(); 71 getLog().print("Resource " + ((NamedElement) element).getName() + ' '); 72 if (element.getASTree() != null) { 73 ASTree tree = element.getASTree(); 74 if (!isCorrect(tree)) { 75 getLog().println("\tError!\n"); 76 fail = true; 77 } else { 78 getLog().println(""); 79 } 80 files++; 81 } 82 } 83 } finally { 84 Utility.endTrans(); 85 } 86 getLog().println("Files: " + files); 87 assertFalse(fail); 88 } 89 90 private boolean isCorrect(ASTree tree) { 91 if (tree == null) 92 return true; 93 ASTree[] sub = tree.getSubTrees(); 94 if (sub == null) { 95 return true; 96 } 97 for (int i = 0; i < sub.length; i++) { 98 if (sub[i] instanceof Token) { 99 getLog().print("."); 100 Token firstToken = sub[i].getASTContext().getToken(sub[i].getFirstToken()); 101 Token lastToken = sub[i].getASTContext().getToken(sub[i].getLastToken()); 102 103 if (sub[i] != firstToken || sub[i] != lastToken) { 104 getLog().println("\n\tToken " + sub[i] + ", hash #" + System.identityHashCode(sub[i]) + "\n" + 105 "\t\tfirst token: " + firstToken + ", hash #" + System.identityHashCode(firstToken) + "\n" + 106 "\t\tlast token: " + lastToken + ", hash #" + System.identityHashCode(lastToken) +"."); 107 return false; 108 } 109 } else { 110 if (!isCorrect(sub[i])) { 111 return false; 112 } 113 } 114 } 115 return true; 116 } 117 118 121 public static void main(String [] args) { 122 TestRunner.run(suite()); 123 } 124 125 } 126 | Popular Tags |