KickJava   Java API By Example, From Geeks To Geeks.

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


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
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 /**
32  * Modifying operator through the API methods.
33  *
34  * @author Pavel Flaska
35  */

36 public class OperatorsTest extends GeneratorTestMDRCompat {
37     
38     /** Creates a new instance of OperatorsTest
39      *
40      * @param name
41      */

42     public OperatorsTest(String JavaDoc name) {
43         super(name);
44     }
45     
46     public static NbTestSuite suite() {
47         NbTestSuite suite = new NbTestSuite();
48         suite.addTestSuite(OperatorsTest.class);
49 // suite.addTest(new OperatorsTest("testAndToOrOperAssign"));
50
// suite.addTest(new OperatorsTest("testChangeBinaryOperator"));
51
// suite.addTest(new OperatorsTest("testChangeUnaryOperator"));
52
return suite;
53     }
54
55     /**
56      *
57      */

58     public void testAndToOrOperAssign() throws Exception JavaDoc {
59         testFile = new File JavaDoc(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 JavaDoc 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 JavaDoc {
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 JavaDoc res = TestUtilities.copyFileToString(testFile);
100         System.err.println(res);
101         assertEquals(golden, res);
102     }
103     
104     /**
105      *
106      */

107     public void testChangeBinaryOperator() throws Exception JavaDoc {
108         testFile = new File JavaDoc(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 JavaDoc 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 JavaDoc {
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 JavaDoc res = TestUtilities.copyFileToString(testFile);
146         System.err.println(res);
147         assertEquals(golden, res);
148     }
149     
150     /**
151      *
152      */

153     public void testChangeUnaryOperator() throws Exception JavaDoc {
154         testFile = new File JavaDoc(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 JavaDoc 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 JavaDoc {
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 JavaDoc res = TestUtilities.copyFileToString(testFile);
193         System.err.println(res);
194         assertEquals(golden, res);
195     }
196     String JavaDoc getGoldenPckg() {
197         return "";
198     }
199
200     String JavaDoc getSourcePckg() {
201         return "";
202     }
203
204     
205     
206 }
207
Popular Tags