KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > astmatching > MethodsTest


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 package org.netbeans.modules.javacore.astmatching;
20
21 import java.util.Collections JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.swing.text.StyledDocument JavaDoc;
25 import junit.textui.TestRunner;
26
27 import org.netbeans.jmi.javamodel.Element;
28 import org.netbeans.jmi.javamodel.JavaClass;
29 import org.netbeans.jmi.javamodel.JavaModelPackage;
30 import org.netbeans.jmi.javamodel.Method;
31 import org.netbeans.jmi.javamodel.Parameter;
32 import org.netbeans.jmi.javamodel.Type;
33 import org.netbeans.jmi.javamodel.codegen.Utility;
34 import org.netbeans.junit.NbTestCase;
35 import org.netbeans.junit.NbTestSuite;
36
37
38 /**
39  * @author Jan Becicka
40  */

41 public class MethodsTest extends NbTestCase {
42    
43     private static final String JavaDoc method1 = "\n" +
44     "public static void main (java.lang.String args[]) {\n" +
45     "System.out.println(\"foo\");\n" +
46     "}\n";
47     
48     /** Need to be defined because of JUnit */
49     public MethodsTest(String JavaDoc name) {
50         super(name);
51         
52     }
53     
54     public static NbTestSuite suite() {
55         NbTestSuite suite = new NbTestSuite();
56         suite.addTest(new MethodsTest("testAddMethod"));
57         suite.addTest(new MethodsTest("testChangeMethod"));
58         //suite.addTest(new MethodsTest("testRemoveMethod"));
59
return suite;
60     }
61     
62     /** Use for execution inside IDE */
63     public static void main(java.lang.String JavaDoc[] args) {
64         TestRunner.run(suite());
65     }
66
67     protected void setUp() {
68     }
69     
70     public void testAddMethod() {
71         JavaClass clazz = Utility.findClass("org.netbeans.test.astmatching.ClassOne");
72         
73         StyledDocument JavaDoc doc = Utility.getDocument(clazz);
74         Element after = clazz.getField("instanceCounter", false);
75         
76         MatchUtils.addElement(doc, after, method1);
77         
78         Utility.beginTrans(true);
79         try {
80             Method me = MatchUtils.getMethod(clazz, "main", "java.lang.String[]");
81             assertNotNull(me);
82         } finally {
83             Utility.endTrans();
84         }
85     }
86     
87     public void testChangeMethod() {
88         JavaClass clazz = Utility.findClass("org.netbeans.test.astmatching.ClassOne");
89         
90         StyledDocument JavaDoc doc = Utility.getDocument(clazz);
91         Method m = MatchUtils.getMethod(clazz, "main", "java.lang.String[]");
92         
93         Parameter p = (Parameter) m.getParameters().iterator().next();
94         MatchUtils.addElement(doc, p, ", int a");
95         
96         Utility.beginTrans(true);
97         try {
98             Method me = MatchUtils.getMethod(clazz, "main", "java.lang.String[],int");
99             assertNotNull(me);
100         } finally {
101             Utility.endTrans();
102         }
103     }
104
105     public void testRemoveMethod() {
106         
107     
108     }
109     
110 }
111
Popular Tags