KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > parsing > TokenIdentityTest


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  */

20 package org.netbeans.modules.javacore.parsing;
21
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
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 /**
37  * Used for test that token return the same token when
38  * calling getFirstToken() and getLastToken() method on the token.
39  *
40  * @author Pavel Flaska
41  */

42 public class TokenIdentityTest extends NbTestCase {
43     
44     private static final boolean DEBUG = true;
45     
46     /** Creates a new instance of TokenIdentityTest */
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 JavaDoc resources = pkg.getResource().refAllOfClass();
69             for (Iterator JavaDoc 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     /**
119      * @param args the command line arguments
120      */

121     public static void main(String JavaDoc[] args) {
122         TestRunner.run(suite());
123     }
124     
125 }
126
Popular Tags