001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.modules.java.hints.errors;
029:
030: import com.sun.source.util.TreePath;
031: import java.io.IOException;
032: import java.util.LinkedList;
033: import java.util.List;
034: import org.netbeans.api.java.source.CompilationInfo;
035: import org.netbeans.modules.java.hints.infrastructure.ErrorHintsTestBase;
036: import org.netbeans.spi.editor.hints.Fix;
037:
038: /**
039: *
040: * @author Jan Lahoda
041: */
042: public class AddParameterOrLocalFixTest extends ErrorHintsTestBase {
043:
044: private boolean parameter = true;
045:
046: public AddParameterOrLocalFixTest(String testName) {
047: super (testName);
048: }
049:
050: public void testAddBeforeVararg() throws Exception {
051: performFixTest(
052: "test/Test.java",
053: "package test; public class Test {public void test(String... a) {bbb = 0;}}",
054: 91 - 25,
055: "AddParameterOrLocalFix:bbb:int:true",
056: "package test; public class Test {public void test(int bbb,String... a) {bbb = 0;}}");
057: }
058:
059: public void testAddToTheEnd() throws Exception {
060: performFixTest(
061: "test/Test.java",
062: "package test; public class Test {public void test(String[] a) {bbb = 0;}}",
063: 90 - 25,
064: "AddParameterOrLocalFix:bbb:int:true",
065: "package test; public class Test {public void test(String[] a,int bbb) {bbb = 0;}}");
066: }
067:
068: public void testAddToTheEmptyParamsList() throws Exception {
069: performFixTest(
070: "test/Test.java",
071: "package test; public class Test {public void test() {bbb = 0;}}",
072: 80 - 25, "AddParameterOrLocalFix:bbb:int:true",
073: "package test; public class Test {public void test(int bbb) {bbb = 0;}}");
074: }
075:
076: public void testAddLocalVariableWithComments() throws Exception {
077: parameter = false;
078:
079: performFixTest(
080: "test/Test.java",
081: "package test; public class Test {public void test() {int a;\n //test\n |bbb = 0;\n int c; }}",
082: "AddParameterOrLocalFix:bbb:int:false",
083: "package test; public class Test {public void test() {int a; //test int bbb = 0; int c; }}");
084: }
085:
086: public void testAddLocalVariableNotInPlace() throws Exception {
087: parameter = false;
088: boolean orig = ErrorFixesFakeHint
089: .isCreateLocalVariableInPlace();
090:
091: try {
092: ErrorFixesFakeHint.setCreateLocalVariableInPlace(false);
093:
094: performFixTest(
095: "test/Test.java",
096: "package test; public class Test {public void test() {int a;\n |bbb = 0;\n int c; }}",
097: "AddParameterOrLocalFix:bbb:int:false",
098: "package test; public class Test {public void test() {int bbb; int a; bbb = 0; int c; }}");
099: } finally {
100: ErrorFixesFakeHint.setCreateLocalVariableInPlace(orig);
101: }
102: }
103:
104: public void testAddLocalVariableNotInPlaceInConstr()
105: throws Exception {
106: parameter = false;
107:
108: boolean orig = ErrorFixesFakeHint
109: .isCreateLocalVariableInPlace();
110:
111: try {
112: ErrorFixesFakeHint.setCreateLocalVariableInPlace(false);
113:
114: performFixTest(
115: "test/Test.java",
116: "package test; public class Test {public Test() {super();\n int a;\n |bbb = 0;\n int c; }}",
117: "AddParameterOrLocalFix:bbb:int:false",
118: "package test; public class Test {public Test() {super(); int bbb; int a; bbb = 0; int c; }}");
119: } finally {
120: ErrorFixesFakeHint.setCreateLocalVariableInPlace(orig);
121: }
122: }
123:
124: protected List<Fix> computeFixes(CompilationInfo info, int pos,
125: TreePath path) throws IOException {
126: List<Fix> fixes = CreateElement.analyze(info, pos);
127: List<Fix> result = new LinkedList<Fix>();
128:
129: for (Fix f : fixes) {
130: if (f instanceof AddParameterOrLocalFix) {
131: if (((AddParameterOrLocalFix) f).isParameter() == parameter)
132: result.add(f);
133: }
134: }
135:
136: return result;
137: }
138:
139: @Override
140: protected String toDebugString(CompilationInfo info, Fix f) {
141: return ((AddParameterOrLocalFix) f).toDebugString(info);
142: }
143: }
|