1 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 ; 26 import java.util.Collections ; 27 import org.netbeans.api.java.source.transform.Transformer; 28 import org.netbeans.junit.NbTestSuite; 29 30 35 public class MethodTest4 extends GeneratorTestMDRCompat { 36 37 38 public MethodTest4(String 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 53 return suite; 54 } 55 56 protected void setUp() throws Exception { 57 super.setUp(); 58 testFile = getFile(getSourceDir(), getSourcePckg() + "MethodTest4.java"); 59 } 60 61 public void testAddFirstThrows() throws IOException { 62 System.err.println("testAddFirstThrows"); 63 process( 64 new Transformer<Void , Object >() { 65 public Void visitMethod(MethodTree node, Object 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 { 79 System.err.println("testAddSecondThrows"); 80 process( 81 new Transformer<Void , Object >() { 82 public Void visitMethod(MethodTree node, Object 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 { 96 System.err.println("testAddThirdThrows"); 97 process( 98 new Transformer<Void , Object >() { 99 public Void visitMethod(MethodTree node, Object 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 { 113 System.err.println("testRemoveFirstThrows"); 114 process( 115 new Transformer<Void , Object >() { 116 public Void visitMethod(MethodTree node, Object 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 { 130 System.err.println("testRemoveLastThrows"); 131 process( 132 new Transformer<Void , Object >() { 133 public Void visitMethod(MethodTree node, Object p) { 134 super.visitMethod(node, p); 135 if ("fooMethod".contentEquals(node.getName())) { 136 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 { 149 System.err.println("testRemoveAllThrows"); 150 process( 151 new Transformer<Void , Object >() { 152 public Void visitMethod(MethodTree node, Object p) { 153 super.visitMethod(node, p); 154 if ("fooMethod".contentEquals(node.getName())) { 155 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 { 167 System.err.println("testAnnotationAndThrows"); 168 process( 169 new Transformer<Void , Object >() { 170 public Void visitMethod(MethodTree node, Object 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 { 192 System.err.println("testRemoveAnnAndAddThrows"); 193 process( 194 new Transformer<Void , Object >() { 195 public Void visitMethod(MethodTree node, Object 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 { 211 System.err.println("testAddTypeParam"); 212 process( 213 new Transformer<Void , Object >() { 214 public Void visitMethod(MethodTree node, Object 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 getSourcePckg() { 231 return "org/netbeans/test/codegen/"; 232 } 233 234 String getGoldenPckg() { 235 return "org/netbeans/jmi/javamodel/codegen/MethodTest4/"; 236 } 237 238 } 239 | Popular Tags |