1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.source.tree.*; 22 import static com.sun.source.tree.Tree.*; 23 import java.io.File ; 24 import org.netbeans.api.java.source.*; 25 import static org.netbeans.api.java.source.JavaSource.*; 26 import org.netbeans.api.java.source.TestUtilities; 27 import org.netbeans.api.java.source.WorkingCopy; 28 import org.netbeans.junit.NbTestSuite; 29 import org.openide.filesystems.FileUtil; 30 31 36 public class OperatorsTest extends GeneratorTestMDRCompat { 37 38 42 public OperatorsTest(String name) { 43 super(name); 44 } 45 46 public static NbTestSuite suite() { 47 NbTestSuite suite = new NbTestSuite(); 48 suite.addTestSuite(OperatorsTest.class); 49 return suite; 53 } 54 55 58 public void testAndToOrOperAssign() throws Exception { 59 testFile = new File (getWorkDir(), "Test.java"); 60 TestUtilities.copyStringToFile(testFile, 61 "package hierbas.del.litoral;\n\n" + 62 "import java.io.*;\n\n" + 63 "public class Test {\n" + 64 " public void taragui() {\n" + 65 " int a = 10;\n" + 66 " int b = 20;\n" + 67 " a &= b;\n" + 68 " }\n" + 69 "}\n" 70 ); 71 String golden = 72 "package hierbas.del.litoral;\n\n" + 73 "import java.io.*;\n\n" + 74 "public class Test {\n" + 75 " public void taragui() {\n" + 76 " int a = 10;\n" + 77 " int b = 20;\n" + 78 " a |= b;\n" + 79 " }\n" + 80 "}\n"; 81 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 82 CancellableTask task = new CancellableTask<WorkingCopy>() { 83 84 public void run(WorkingCopy workingCopy) throws java.io.IOException { 85 workingCopy.toPhase(Phase.RESOLVED); 86 TreeMaker make = workingCopy.getTreeMaker(); 87 88 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 89 MethodTree method = (MethodTree) clazz.getMembers().get(1); 90 ExpressionStatementTree est = (ExpressionStatementTree) method.getBody().getStatements().get(2); 91 CompoundAssignmentTree t = (CompoundAssignmentTree) est.getExpression(); 92 workingCopy.rewrite(t, make.CompoundAssignment(Kind.OR_ASSIGNMENT, t.getVariable(), t.getExpression())); 93 } 94 95 public void cancel() { 96 } 97 }; 98 testSource.runModificationTask(task).commit(); 99 String res = TestUtilities.copyFileToString(testFile); 100 System.err.println(res); 101 assertEquals(golden, res); 102 } 103 104 107 public void testChangeBinaryOperator() throws Exception { 108 testFile = new File (getWorkDir(), "Test.java"); 109 TestUtilities.copyStringToFile(testFile, 110 "package hierbas.del.litoral;\n\n" + 111 "import java.io.*;\n\n" + 112 "public class Test {\n" + 113 " public void taragui() {\n" + 114 " int c = (0x0f | 7);\n" + 115 " }\n" + 116 "}\n" 117 ); 118 String golden = 119 "package hierbas.del.litoral;\n\n" + 120 "import java.io.*;\n\n" + 121 "public class Test {\n" + 122 " public void taragui() {\n" + 123 " int c = (0x0f & 7);\n" + 124 " }\n" + 125 "}\n"; 126 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 127 CancellableTask task = new CancellableTask<WorkingCopy>() { 128 129 public void run(WorkingCopy workingCopy) throws java.io.IOException { 130 workingCopy.toPhase(Phase.RESOLVED); 131 TreeMaker make = workingCopy.getTreeMaker(); 132 133 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 134 MethodTree method = (MethodTree) clazz.getMembers().get(1); 135 VariableTree lvd = (VariableTree) method.getBody().getStatements().get(0); 136 ParenthesizedTree pt = (ParenthesizedTree) lvd.getInitializer(); 137 BinaryTree bt = (BinaryTree) pt.getExpression(); 138 workingCopy.rewrite(bt, make.Binary(Kind.AND, bt.getLeftOperand(), bt.getRightOperand())); 139 } 140 141 public void cancel() { 142 } 143 }; 144 testSource.runModificationTask(task).commit(); 145 String res = TestUtilities.copyFileToString(testFile); 146 System.err.println(res); 147 assertEquals(golden, res); 148 } 149 150 153 public void testChangeUnaryOperator() throws Exception { 154 testFile = new File (getWorkDir(), "Test.java"); 155 TestUtilities.copyStringToFile(testFile, 156 "package hierbas.del.litoral;\n\n" + 157 "import java.io.*;\n\n" + 158 "public class Test {\n" + 159 " public void taragui() {\n" + 160 " int c;\n" + 161 " c++;\n" + 162 " }\n" + 163 "}\n" 164 ); 165 String golden = 166 "package hierbas.del.litoral;\n\n" + 167 "import java.io.*;\n\n" + 168 "public class Test {\n" + 169 " public void taragui() {\n" + 170 " int c;\n" + 171 " c--;\n" + 172 " }\n" + 173 "}\n"; 174 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 175 CancellableTask task = new CancellableTask<WorkingCopy>() { 176 177 public void run(WorkingCopy workingCopy) throws java.io.IOException { 178 workingCopy.toPhase(Phase.RESOLVED); 179 TreeMaker make = workingCopy.getTreeMaker(); 180 181 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 182 MethodTree method = (MethodTree) clazz.getMembers().get(1); 183 ExpressionStatementTree est = (ExpressionStatementTree) method.getBody().getStatements().get(1); 184 UnaryTree ut = (UnaryTree) est.getExpression(); 185 workingCopy.rewrite(ut, make.Unary(Kind.POSTFIX_DECREMENT, ut.getExpression())); 186 } 187 188 public void cancel() { 189 } 190 }; 191 testSource.runModificationTask(task).commit(); 192 String res = TestUtilities.copyFileToString(testFile); 193 System.err.println(res); 194 assertEquals(golden, res); 195 } 196 String getGoldenPckg() { 197 return ""; 198 } 199 200 String getSourcePckg() { 201 return ""; 202 } 203 204 205 206 } 207 | Popular Tags |