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.CaseTree; |
27 |
import com.sun.source.tree.StatementTree; |
28 |
import com.sun.source.tree.Tree.Kind; |
29 |
import com.sun.source.util.TreePath; |
30 |
import java.io.File; |
31 |
import java.io.IOException; |
32 |
import java.util.ArrayList; |
33 |
import java.util.List; |
34 |
import org.netbeans.api.java.source.JavaSource; |
35 |
import org.netbeans.api.java.source.JavaSource.Phase; |
36 |
import org.netbeans.api.java.source.Task; |
37 |
import org.netbeans.api.java.source.TestUtilities; |
38 |
import org.netbeans.api.java.source.TreeMaker; |
39 |
import org.netbeans.api.java.source.WorkingCopy; |
40 |
|
41 |
/** |
42 |
* The following shell script was used to generate the code snippets |
43 |
* <code>cat test/unit/data/test/Test.java | tr '\n' ' ' | tr '\t' ' ' | sed -E 's| +| |g' | sed 's|"|\\"|g'</code> * @author Samuel Halliday |
44 |
*/ |
45 |
public class SwitchTest extends GeneratorTest { |
46 |
|
47 |
public SwitchTest(String name) { |
48 |
super(name); |
49 |
} |
50 |
|
51 |
public void test158129() throws Exception { |
52 |
testFile = new File(getWorkDir(), "Test.java"); |
53 |
String test = "public class Test { void m(int p) { switch (p) { ca|se 0: } } }"; |
54 |
String golden = "public class Test { void m(int p) { switch (p) { case 0: break; } } }"; |
55 |
final int index = test.indexOf("|"); |
56 |
assertTrue(index != -1); |
57 |
TestUtilities.copyStringToFile(testFile, test.replace("|", "")); |
58 |
JavaSource src = getJavaSource(testFile); |
59 |
Task<WorkingCopy> task = new Task<WorkingCopy>() { |
60 |
|
61 |
public void run(WorkingCopy copy) throws IOException { |
62 |
if (copy.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) { |
63 |
return; |
64 |
} |
65 |
TreeMaker make = copy.getTreeMaker(); |
66 |
TreePath node = copy.getTreeUtilities().pathFor(index); |
67 |
assertTrue(node.getLeaf().getKind() == Kind.CASE); |
68 |
CaseTree original = (CaseTree) node.getLeaf(); |
69 |
List<StatementTree> st = new ArrayList<StatementTree>(); |
70 |
st.addAll(original.getStatements()); |
71 |
st.add(make.Break(null)); |
72 |
CaseTree modified = make.Case(original.getExpression(), st); |
73 |
copy.rewrite(node.getLeaf(), modified); |
74 |
} |
75 |
}; |
76 |
src.runModificationTask(task).commit(); |
77 |
String res = TestUtilities.copyFileToString(testFile); |
78 |
System.err.println(res); |
79 |
assertEquals(golden, res); |
80 |
} |
81 |
|
82 |
// XXX I don't understand what these are used for |
83 |
@Override |
84 |
String getSourcePckg() { |
85 |
return ""; |
86 |
} |
87 |
|
88 |
@Override |
89 |
String getGoldenPckg() { |
90 |
return ""; |
91 |
} |
92 |
} |