KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > gen > CompareTreeTest


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.api.java.source.gen;
21
22 import com.sun.source.tree.MethodTree;
23 import com.sun.source.tree.ModifiersTree;
24 import java.io.IOException JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.lang.model.element.Modifier;
29 import org.netbeans.api.java.source.CancellableTask;
30 import org.netbeans.api.java.source.CompilationController;
31 import org.netbeans.api.lexer.TokenHierarchy;
32 import org.netbeans.junit.NbTestSuite;
33 import org.netbeans.api.java.source.transform.Transformer;
34
35 /**
36  *
37  * @author Pavel Flaska
38  */

39
40 public class CompareTreeTest extends GeneratorTest {
41     
42     /** Creates a new instance of CompareTreeTest */
43     public CompareTreeTest(String JavaDoc name) {
44         super(name);
45     }
46     
47     public static NbTestSuite suite() {
48         NbTestSuite suite = new NbTestSuite(CompareTreeTest.class);
49         return suite;
50     }
51     
52     protected void setUp() throws Exception JavaDoc {
53         super.setUp();
54         testFile = getFile(getSourceDir(), getSourcePckg() + "MethodTest1.java");
55     }
56     /*
57     public void testMethodModifiers() throws IOException {
58         final TokenHierarchy[] cut = new TokenHierarchy[2];
59         getJavaSource(getTestFile()).runUserActionTask(new CancellableTask<CompilationController>() {
60             public void cancel() {
61             }
62             public void run(CompilationController cc) {
63                 cut[0] = cc.getTokenHierarchy();
64             }
65         });
66         process(
67             new Transformer<Void, Object>() {
68                 public Void visitMethod(MethodTree node, Object p) {
69                     super.visitMethod(node, p);
70                     if ("firstMethod".contentEquals(node.getName())) {
71                         ModifiersTree origMods = node.getModifiers();
72                         Set<Modifier> njuMods = new HashSet<Modifier>();
73                         njuMods.add(Modifier.PRIVATE);
74                         njuMods.add(Modifier.STATIC);
75                         changes.rewrite(origMods, make.Modifiers(njuMods));
76                     }
77                     return null;
78                 }
79             }
80         );
81         getJavaSource(getTestFile()).runUserActionTask(new CancellableTask<CompilationController>() {
82             public void cancel() {
83             }
84             public void run(CompilationController cc) {
85                 cut[1] = cc.getTokenHierarchy();
86             }
87         });
88         Map<Object, CharSequence[]> result = TreeChecker.compareTokens(cut[0], cut[1]);
89         //CharSequence[][] cs = .iterator().next().
90         for (Map.Entry<Object, CharSequence[]> item : result.entrySet()) {
91             System.out.println(item.getKey() + ": '" + item.getValue()[0] + "' != '" + item.getValue()[1] + "'");
92         }
93     }*/

94     
95     public void testMethodName() throws IOException JavaDoc {
96         final TokenHierarchy[] cut = new TokenHierarchy[2];
97         getJavaSource(getTestFile()).runUserActionTask(new CancellableTask<CompilationController>() {
98             public void cancel() {
99             }
100             public void run(CompilationController cc) {
101                 cut[0] = cc.getTokenHierarchy();
102             }
103         },true);
104         process(
105             new Transformer<Void JavaDoc, Object JavaDoc>() {
106                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
107                     super.visitMethod(node, p);
108                     if ("secondMethod".contentEquals(node.getName())) {
109                         MethodTree njuMethod = make.setLabel(node, "druhaMetoda");
110                         changes.rewrite(node, njuMethod);
111                     }
112                     return null;
113                 }
114             }
115         );
116         getJavaSource(getTestFile()).runUserActionTask(new CancellableTask<CompilationController>() {
117             public void cancel() {
118             }
119             public void run(CompilationController cc) {
120                 cut[1] = cc.getTokenHierarchy();
121             }
122         },true);
123         Map JavaDoc<Object JavaDoc, CharSequence JavaDoc[]> result = TreeChecker.compareTokens(cut[0], cut[1]);
124         //CharSequence[][] cs = .iterator().next().
125
for (Map.Entry JavaDoc<Object JavaDoc, CharSequence JavaDoc[]> item : result.entrySet()) {
126             System.out.println(item.getKey() + ": '" + item.getValue()[0] + "' != '" + item.getValue()[1] + "'");
127         }
128     }
129
130     String JavaDoc getGoldenPckg() {
131         return "org/netbeans/jmi/javamodel/codegen/MethodTest1/MethodTest1/";
132     }
133
134     String JavaDoc getSourcePckg() {
135         return "org/netbeans/test/codegen/";
136     }
137     
138 }
139
Popular Tags