1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.source.tree.ClassTree; 22 import com.sun.source.tree.CompilationUnitTree; 23 import com.sun.source.tree.ExpressionTree; 24 import com.sun.source.tree.Tree; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.util.Collections ; 28 import java.util.List ; 29 import org.netbeans.api.java.source.CancellableTask; 30 import org.netbeans.api.java.source.JavaSource; 31 import org.netbeans.api.java.source.JavaSource.Phase; 32 import org.netbeans.api.java.source.TestUtilities; 33 import org.netbeans.api.java.source.TreeMaker; 34 import org.netbeans.api.java.source.WorkingCopy; 35 36 43 public class InterfaceExtendsTest extends GeneratorTestMDRCompat { 44 45 46 public InterfaceExtendsTest(String testName) { 47 super(testName); 48 } 49 50 public void testAddExtends() throws Exception { 51 testFile = new File (getWorkDir(), "Test.java"); 52 TestUtilities.copyStringToFile(testFile, 53 "package hierbas.del.litoral;\n\n" + 54 "import java.util.*;\n\n" + 55 "public interface Test {\n" + 56 " public void taragui();\n" + 57 "}\n" 58 ); 59 String golden = 60 "package hierbas.del.litoral;\n\n" + 61 "import java.util.*;\n\n" + 62 "public interface Test extends Serializable {\n" + 63 " public void taragui();\n" + 64 "}\n"; 65 JavaSource src = getJavaSource(testFile); 66 67 CancellableTask task = new CancellableTask<WorkingCopy>() { 68 69 public void run(WorkingCopy workingCopy) throws IOException { 70 workingCopy.toPhase(Phase.RESOLVED); 71 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 72 TreeMaker make = workingCopy.getTreeMaker(); 73 74 for (Tree typeDecl : cut.getTypeDecls()) { 75 if (Tree.Kind.CLASS == typeDecl.getKind()) { 77 ClassTree classTree = (ClassTree) typeDecl; 78 List <ExpressionTree> implementz = 79 Collections.<ExpressionTree>singletonList( 80 make.Identifier("Serializable") 81 ); 82 ClassTree copy = make.Class( 83 classTree.getModifiers(), 84 classTree.getSimpleName(), 85 classTree.getTypeParameters(), 86 classTree.getExtendsClause(), 87 implementz, 88 classTree.getMembers() 89 ); 90 workingCopy.rewrite(classTree, copy); 91 } 92 } 93 } 94 95 public void cancel() { 96 } 97 }; 98 src.runModificationTask(task).commit(); 99 String res = TestUtilities.copyFileToString(testFile); 100 assertEquals(golden, res); 101 } 102 103 public void testAddTwoExtends() throws Exception { 104 testFile = new File (getWorkDir(), "Test.java"); 105 TestUtilities.copyStringToFile(testFile, 106 "package hierbas.del.litoral;\n\n" + 107 "import java.util.*;\n\n" + 108 "public interface Test {\n" + 109 " public void taragui();\n" + 110 "}\n" 111 ); 112 String golden = 113 "package hierbas.del.litoral;\n\n" + 114 "import java.util.*;\n\n" + 115 "public interface Test extends Serializable, Externalizable {\n" + 116 " public void taragui();\n" + 117 "}\n"; 118 JavaSource src = getJavaSource(testFile); 119 120 CancellableTask task = new CancellableTask<WorkingCopy>() { 121 122 public void run(WorkingCopy workingCopy) throws IOException { 123 workingCopy.toPhase(Phase.RESOLVED); 124 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 125 TreeMaker make = workingCopy.getTreeMaker(); 126 127 for (Tree typeDecl : cut.getTypeDecls()) { 128 if (Tree.Kind.CLASS == typeDecl.getKind()) { 130 ClassTree classTree = (ClassTree) typeDecl; 131 ClassTree copy = make.addClassImplementsClause( 132 classTree, make.Identifier("Serializable") 133 ); 134 copy = make.addClassImplementsClause( 135 copy, make.Identifier("Externalizable") 136 ); 137 workingCopy.rewrite(classTree, copy); 138 } 139 } 140 } 141 142 public void cancel() { 143 } 144 }; 145 src.runModificationTask(task).commit(); 146 String res = TestUtilities.copyFileToString(testFile); 147 assertEquals(golden, res); 148 } 149 150 public void testRemoveTwoExtends() throws Exception { 151 testFile = new File (getWorkDir(), "Test.java"); 152 TestUtilities.copyStringToFile(testFile, 153 "package hierbas.del.litoral;\n\n" + 154 "import java.util.*;\n\n" + 155 "public interface Test extends Serializable, Externalizable {\n" + 156 " public void taragui();\n" + 157 "}\n" 158 ); 159 String golden = 160 "package hierbas.del.litoral;\n\n" + 161 "import java.util.*;\n\n" + 162 "public interface Test {\n" + 163 " public void taragui();\n" + 164 "}\n"; 165 JavaSource src = getJavaSource(testFile); 166 167 CancellableTask task = new CancellableTask<WorkingCopy>() { 168 169 public void run(WorkingCopy workingCopy) throws IOException { 170 workingCopy.toPhase(Phase.RESOLVED); 171 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 172 TreeMaker make = workingCopy.getTreeMaker(); 173 174 for (Tree typeDecl : cut.getTypeDecls()) { 175 if (Tree.Kind.CLASS == typeDecl.getKind()) { 177 ClassTree classTree = (ClassTree) typeDecl; 178 ClassTree copy = make.removeClassImplementsClause(classTree, 0); 179 copy = make.removeClassImplementsClause(copy, 0); 180 workingCopy.rewrite(classTree, copy); 181 } 182 } 183 } 184 185 public void cancel() { 186 } 187 }; 188 src.runModificationTask(task).commit(); 189 String res = TestUtilities.copyFileToString(testFile); 190 assertEquals(golden, res); 191 } 192 193 String getGoldenPckg() { 194 return ""; 195 } 196 197 String getSourcePckg() { 198 return ""; 199 } 200 201 } 202 | Popular Tags |