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: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.java.hints.errors;
042:
043: import com.sun.source.util.TreePath;
044: import org.netbeans.api.java.source.CompilationInfo;
045: import java.util.LinkedList;
046: import java.util.List;
047: import org.netbeans.modules.java.hints.errors.ChangeType;
048: import org.netbeans.modules.java.hints.infrastructure.ErrorHintsTestBase;
049: import org.netbeans.spi.editor.hints.Fix;
050:
051: /**
052: *
053: * @author Sandip Chitale
054: */
055: public class ChangeTypeTest extends ErrorHintsTestBase {
056:
057: /** Creates a new instance of ChangeTypeTest */
058: public ChangeTypeTest(String name) {
059: super (name);
060: }
061:
062: public void testIntToString() throws Exception {
063: performAnalysisTest("test/Test.java",
064: "package test; public class Test {int i = \"s\";}", 41,
065: "Change type of i to String");
066: }
067:
068: public void testIntToStringFix() throws Exception {
069: performFixTest("test/Test.java",
070: "package test; public class Test {int i = \"s\";}", 41,
071: "Change type of i to String",
072: "package test; public class Test { String i = \"s\";}");
073: }
074:
075: public void testStringToInt() throws Exception {
076: performAnalysisTest("test/Test.java",
077: "package test; public class Test {String s = 5;}", 44,
078: "Change type of s to int");
079: }
080:
081: public void testStringToIntFix() throws Exception {
082: performFixTest("test/Test.java",
083: "package test; public class Test {String s = 5;}", 44,
084: "Change type of s to int",
085: "package test; public class Test { int s = 5;}");
086: }
087:
088: public void testStringToObject() throws Exception {
089: performAnalysisTest(
090: "test/Test.java",
091: "package test; public class Test {String s = new Object();}",
092: 44, "Change type of s to Object");
093: }
094:
095: public void testStringToObjectFix() throws Exception {
096: performFixTest(
097: "test/Test.java",
098: "package test; public class Test {String s = new Object();}",
099: 44, "Change type of s to Object",
100: "package test; public class Test {Object s = new Object();}");
101: }
102:
103: public void testLocalVariableIntToString() throws Exception {
104: performAnalysisTest(
105: "test/Test.java",
106: "package test; public class Test {private void test() {int i = \"s\";}}",
107: 62, "Change type of i to String");
108: }
109:
110: public void testLocalVariableIntToStringFix() throws Exception {
111: performFixTest(
112: "test/Test.java",
113: "package test; public class Test {private void test() {int i = \"s\";}}",
114: 62, "Change type of i to String",
115: "package test; public class Test {private void test() { String i = \"s\";}}");
116: }
117:
118: public void testLocalVariableStringToInt() throws Exception {
119: performAnalysisTest(
120: "test/Test.java",
121: "package test; public class Test {private void test() {String s = 5;}}",
122: 65, "Change type of s to int");
123: }
124:
125: public void testLocalVariableStringToIntFix() throws Exception {
126: performFixTest(
127: "test/Test.java",
128: "package test; public class Test {private void test() {String s = 5;}}",
129: 65, "Change type of s to int",
130: "package test; public class Test {private void test() { int s = 5;}}");
131: }
132:
133: public void testLocalVariableStringToObject() throws Exception {
134: performAnalysisTest(
135: "test/Test.java",
136: "package test; public class Test {private void test() {String s = new Object();}}",
137: 65, "Change type of s to Object");
138: }
139:
140: public void testLocalVariableStringToObjectFix() throws Exception {
141: performFixTest(
142: "test/Test.java",
143: "package test; public class Test {private void test() {String s = new Object();}}",
144: 65,
145: "Change type of s to Object",
146: "package test; public class Test {private void test() {Object s = new Object();}}");
147: }
148:
149: public void testCapturedWildcard1() throws Exception {
150: performFixTest(
151: "test/Test.java",
152: "package test; import java.util.List; public class Test {private void test() {String o = |test1();} private List<? extends CharSequence> test1() {return null;}}",
153: "Change type of o to List<? extends CharSequence>",
154: "package test; import java.util.List; public class Test {private void test() { List<? extends CharSequence> o = test1();} private List<? extends CharSequence> test1() {return null;}}");
155: }
156:
157: public void testCapturedWildcard2() throws Exception {
158: performFixTest(
159: "test/Test.java",
160: "package test; import java.util.List; public class Test {private void test() {List<? extends CharSequence> l = null; Number o = |l.get(0);}}",
161: "Change type of o to CharSequence",
162: "package test; import java.util.List; public class Test {private void test() {List<? extends CharSequence> l = null; CharSequence o = l.get(0);}}");
163: }
164:
165: protected List<Fix> computeFixes(CompilationInfo info, int pos,
166: TreePath path) {
167: List<Fix> fixes = new ChangeType().run(info, null, pos, path,
168: null);
169: List<Fix> result = new LinkedList<Fix>();
170:
171: for (Fix f : fixes) {
172: if (f instanceof ChangeTypeFix)
173: result.add(f);
174: }
175:
176: return result;
177: }
178:
179: @Override
180: protected String toDebugString(CompilationInfo info, Fix f) {
181: return ((ChangeTypeFix) f).getText();
182: }
183:
184: }
|