KickJava   Java API By Example, From Geeks To Geeks.

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


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.ExpressionTree;
23 import com.sun.source.tree.MethodTree;
24 import com.sun.source.tree.ModifiersTree;
25 import java.io.IOException JavaDoc;
26 import java.util.Collections JavaDoc;
27 import org.netbeans.api.java.source.transform.Transformer;
28 import org.netbeans.junit.NbTestSuite;
29
30 /**
31  * Testing method throws() section.
32  *
33  * @author Pavel Flaska
34  */

35 public class MethodTest4 extends GeneratorTestMDRCompat {
36     
37     /** Need to be defined because of JUnit */
38     public MethodTest4(String JavaDoc name) {
39         super(name);
40     }
41     
42     public static NbTestSuite suite() {
43         NbTestSuite suite = new NbTestSuite();
44         suite.addTest(new MethodTest4("testAddFirstThrows"));
45         suite.addTest(new MethodTest4("testAddSecondThrows"));
46         suite.addTest(new MethodTest4("testAddThirdThrows"));
47         suite.addTest(new MethodTest4("testRemoveFirstThrows"));
48         suite.addTest(new MethodTest4("testRemoveLastThrows"));
49         suite.addTest(new MethodTest4("testRemoveAllThrows"));
50 /* suite.addTest(new MethodTest4("testAnnotationAndThrows"));
51         suite.addTest(new MethodTest4("testRemoveAnnAndAddThrows"));
52         suite.addTest(new MethodTest4("testAddTypeParam"));*/

53         return suite;
54     }
55     
56     protected void setUp() throws Exception JavaDoc {
57         super.setUp();
58         testFile = getFile(getSourceDir(), getSourcePckg() + "MethodTest4.java");
59     }
60
61     public void testAddFirstThrows() throws IOException JavaDoc {
62         System.err.println("testAddFirstThrows");
63         process(
64             new Transformer<Void JavaDoc, Object JavaDoc>() {
65                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
66                     super.visitMethod(node, p);
67                     if ("fooMethod".contentEquals(node.getName())) {
68                         MethodTree copy = make.addMethodThrows(node, make.Identifier("java.io.IOException"));
69                         changes.rewrite(node, copy);
70                     }
71                     return null;
72                 }
73             }
74         );
75         assertFiles("testAddFirstThrows.pass");
76     }
77
78     public void testAddSecondThrows() throws IOException JavaDoc {
79         System.err.println("testAddSecondThrows");
80         process(
81             new Transformer<Void JavaDoc, Object JavaDoc>() {
82                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
83                     super.visitMethod(node, p);
84                     if ("fooMethod".contentEquals(node.getName())) {
85                         MethodTree copy = make.addMethodThrows(node, make.Identifier("java.io.FileNotFoundException"));
86                         changes.rewrite(node, copy);
87                     }
88                     return null;
89                 }
90             }
91         );
92         assertFiles("testAddSecondThrows.pass");
93     }
94     
95     public void testAddThirdThrows() throws IOException JavaDoc {
96         System.err.println("testAddThirdThrows");
97         process(
98             new Transformer<Void JavaDoc, Object JavaDoc>() {
99                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
100                     super.visitMethod(node, p);
101                     if ("fooMethod".contentEquals(node.getName())) {
102                         MethodTree copy = make.insertMethodThrows(node, 0, make.Identifier("java.io.WriteAbortedException"));
103                         changes.rewrite(node, copy);
104                     }
105                     return null;
106                 }
107             }
108         );
109         assertFiles("testAddThirdThrows.pass");
110     }
111     
112     public void testRemoveFirstThrows() throws IOException JavaDoc {
113         System.err.println("testRemoveFirstThrows");
114         process(
115             new Transformer<Void JavaDoc, Object JavaDoc>() {
116                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
117                     super.visitMethod(node, p);
118                     if ("fooMethod".contentEquals(node.getName())) {
119                         MethodTree copy = make.removeMethodThrows(node, 0);
120                         changes.rewrite(node, copy);
121                     }
122                     return null;
123                 }
124             }
125         );
126         assertFiles("testRemoveFirstThrows.pass");
127     }
128     
129     public void testRemoveLastThrows() throws IOException JavaDoc {
130         System.err.println("testRemoveLastThrows");
131         process(
132             new Transformer<Void JavaDoc, Object JavaDoc>() {
133                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
134                     super.visitMethod(node, p);
135                     if ("fooMethod".contentEquals(node.getName())) {
136                         // just to test the method
137
ExpressionTree lastThrows = node.getThrows().get(1);
138                         MethodTree copy = make.removeMethodThrows(node, lastThrows);
139                         changes.rewrite(node, copy);
140                     }
141                     return null;
142                 }
143             }
144         );
145         assertFiles("testRemoveLastThrows.pass");
146     }
147     
148     public void testRemoveAllThrows() throws IOException JavaDoc {
149         System.err.println("testRemoveAllThrows");
150         process(
151             new Transformer<Void JavaDoc, Object JavaDoc>() {
152                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
153                     super.visitMethod(node, p);
154                     if ("fooMethod".contentEquals(node.getName())) {
155                         // there will be nothing in throws section.
156
MethodTree copy = make.removeMethodThrows(node, 0);
157                         changes.rewrite(node, copy);
158                     }
159                     return null;
160                 }
161             }
162         );
163         assertFiles("testRemoveAllThrows.pass");
164     }
165     
166     public void testAnnotationAndThrows() throws IOException JavaDoc {
167         System.err.println("testAnnotationAndThrows");
168         process(
169             new Transformer<Void JavaDoc, Object JavaDoc>() {
170                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
171                     super.visitMethod(node, p);
172                     if ("fooMethod".contentEquals(node.getName())) {
173                         ModifiersTree newMods = make.addModifiersAnnotation(
174                             node.getModifiers(),
175                             make.Annotation(
176                                 make.Identifier("Deprecated"),
177                                 Collections.<ExpressionTree>emptyList()
178                             )
179                         );
180                         changes.rewrite(node.getModifiers(), newMods);
181                         MethodTree copy = make.addMethodThrows(node, make.Identifier("java.io.IOException"));
182                         changes.rewrite(node, copy);
183                     }
184                     return null;
185                 }
186             }
187         );
188         assertFiles("testAnnotationAndThrows.pass");
189     }
190     
191     public void testRemoveAnnAndAddThrows() throws IOException JavaDoc {
192         System.err.println("testRemoveAnnAndAddThrows");
193         process(
194             new Transformer<Void JavaDoc, Object JavaDoc>() {
195                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
196                     super.visitMethod(node, p);
197                     if ("fooMethod".contentEquals(node.getName())) {
198                         ModifiersTree newMods = make.removeModifiersAnnotation(node.getModifiers(), 0);
199                         changes.rewrite(node.getModifiers(), newMods);
200                         MethodTree copy = make.insertMethodThrows(node, 0, make.Identifier("java.io.WriteAbortedException"));
201                         changes.rewrite(node, copy);
202                     }
203                     return null;
204                 }
205             }
206         );
207         assertFiles("testRemoveAnnAndAddThrows.pass");
208     }
209     
210     public void testAddTypeParam() throws IOException JavaDoc {
211         System.err.println("testAddTypeParam");
212         process(
213             new Transformer<Void JavaDoc, Object JavaDoc>() {
214                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
215                     super.visitMethod(node, p);
216                     if ("fooMethod".contentEquals(node.getName())) {
217                         MethodTree copy = make.addMethodTypeParameter(
218                             node,
219                             make.TypeParameter("T", Collections.<ExpressionTree>emptyList())
220                         );
221                         changes.rewrite(node, copy);
222                     }
223                     return null;
224                 }
225             }
226         );
227         assertFiles("testAddTypeParam.pass");
228     }
229
230     String JavaDoc getSourcePckg() {
231         return "org/netbeans/test/codegen/";
232     }
233
234     String JavaDoc getGoldenPckg() {
235         return "org/netbeans/jmi/javamodel/codegen/MethodTest4/";
236     }
237     
238 }
239
Popular Tags