Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2009 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
*/ |
24 |
package org.netbeans.api.java.source.gen; |
25 |
|
26 |
import com.sun.source.tree.BreakTree; |
27 |
import com.sun.source.tree.ContinueTree; |
28 |
import com.sun.source.tree.IfTree; |
29 |
import com.sun.source.tree.Tree; |
30 |
import com.sun.source.tree.Tree.Kind; |
31 |
import com.sun.source.util.TreePath; |
32 |
import java.io.File; |
33 |
import org.netbeans.api.java.source.JavaSource; |
34 |
import org.netbeans.api.java.source.JavaSource.Phase; |
35 |
import org.netbeans.api.java.source.Task; |
36 |
import org.netbeans.api.java.source.TestUtilities; |
37 |
import org.netbeans.api.java.source.TreeMaker; |
38 |
import org.netbeans.api.java.source.WorkingCopy; |
39 |
|
40 |
/** |
41 |
* The following shell script was used to generate the code snippets |
42 |
* <code>cat test/unit/data/test/Test.java | tr '\n' ' ' | tr '\t' ' ' | sed -E 's| +| |g' | sed 's|"|\\"|g'</code> |
43 |
* @author Samuel Halliday |
44 |
*/ |
45 |
public class BreakContinueTest extends GeneratorTest { |
46 |
|
47 |
public BreakContinueTest(String name) { |
48 |
super(name); |
49 |
} |
50 |
|
51 |
private interface Delegate { |
52 |
|
53 |
public void run(WorkingCopy copy, Tree tree); |
54 |
} |
55 |
|
56 |
private void testHelper(String test, String golden, final Kind kind, final Delegate delegate) throws Exception { |
57 |
testFile = new File(getWorkDir(), "Test.java"); |
58 |
final int index = test.indexOf("|"); |
59 |
assertTrue(index != -1); |
60 |
TestUtilities.copyStringToFile(testFile, test.replace("|", "")); |
61 |
JavaSource src = getJavaSource(testFile); |
62 |
Task<WorkingCopy> task = new Task<WorkingCopy>() { |
63 |
|
64 |
public void run(WorkingCopy copy) throws Exception { |
65 |
if (copy.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) { |
66 |
return; |
67 |
} |
68 |
TreePath node = copy.getTreeUtilities().pathFor(index); |
69 |
assertTrue(node.getLeaf().getKind() == kind); |
70 |
delegate.run(copy, node.getLeaf()); |
71 |
} |
72 |
}; |
73 |
src.runModificationTask(task).commit(); |
74 |
String res = TestUtilities.copyFileToString(testFile); |
75 |
assertEquals(golden, res); |
76 |
} |
77 |
|
78 |
public void XtestBreak158130() throws Exception { |
79 |
String test = "public class Test { void m(int p) { loop: while (true) { if (p == 0) { b|reak loop; } } } }"; |
80 |
String golden = "public class Test { void m(int p) { loop: while (true) { if (p == 0) { break ; } } } }"; |
81 |
testHelper(test, golden, Kind.BREAK, new Delegate() { |
82 |
|
83 |
public void run(WorkingCopy copy, Tree tree) { |
84 |
BreakTree original = (BreakTree) tree; |
85 |
TreeMaker make = copy.getTreeMaker(); |
86 |
BreakTree modified = make.Break(null); |
87 |
copy.rewrite(original, modified); |
88 |
} |
89 |
}); |
90 |
} |
91 |
|
92 |
public void testBreak158130b() throws Exception { |
93 |
String test = "public class Test { void m(int p) { loop: while (true) { i|f (p == 0) { break loop; } else { break ; } } } }"; |
94 |
String golden = "public class Test { void m(int p) { loop: while (true) { if (p == 0) { break ; } } } }"; |
95 |
testHelper(test, golden, Kind.IF, new Delegate() { |
96 |
|
97 |
public void run(WorkingCopy copy, Tree tree) { |
98 |
IfTree original = (IfTree) tree; |
99 |
IfTree modified = copy.getTreeMaker().If(original.getCondition(), original.getElseStatement(), null); |
100 |
copy.rewrite(original, modified); |
101 |
} |
102 |
}); |
103 |
} |
104 |
|
105 |
public void testContinue158130() throws Exception { |
106 |
String test = "public class Test { void m(int p) { loop: while (true) { if (p == 0) { con|tinue loop; } } } }"; |
107 |
String golden = "public class Test { void m(int p) { loop: while (true) { if (p == 0) { continue ; } } } }"; |
108 |
testHelper(test, golden, Kind.CONTINUE, new Delegate() { |
109 |
|
110 |
public void run(WorkingCopy copy, Tree tree) { |
111 |
ContinueTree original = (ContinueTree) tree; |
112 |
TreeMaker make = copy.getTreeMaker(); |
113 |
ContinueTree modified = make.Continue(null); |
114 |
copy.rewrite(original, modified); |
115 |
} |
116 |
}); |
117 |
} |
118 |
|
119 |
@Override |
120 |
String getSourcePckg() { |
121 |
return ""; |
122 |
} |
123 |
|
124 |
@Override |
125 |
String getGoldenPckg() { |
126 |
return ""; |
127 |
} |
128 |
} |