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 java.io.IOException;
045: import org.netbeans.api.java.source.CompilationInfo;
046: import java.util.LinkedList;
047: import java.util.List;
048: import org.netbeans.modules.java.hints.infrastructure.ErrorHintsTestBase;
049: import org.netbeans.spi.editor.hints.Fix;
050:
051: /**
052: *
053: * @author Jan Lahoda
054: */
055: public class CreateMethodTest extends ErrorHintsTestBase {
056:
057: /** Creates a new instance of CreateElementTest */
058: public CreateMethodTest(String name) {
059: super (name);
060: }
061:
062: public void testMoreMethods() throws Exception {
063: performAnalysisTest(
064: "test/Test.java",
065: "package test; public class Test {public void test() {test(1);}}",
066: 103 - 48, "CreateMethodFix:test(int i)void:test.Test");
067: }
068:
069: public void testConstructor() throws Exception {
070: performAnalysisTest(
071: "test/Test.java",
072: "package test; public class Test {public static void test() {new Test(1);}}",
073: 114 - 48, "CreateConstructorFix:(int i):test.Test");
074: }
075:
076: public void testNoCreateConstructorForNonExistingClass()
077: throws Exception {
078: performAnalysisTest(
079: "test/Test.java",
080: "package test; public class Test {public static void test() {new NonExisting(1);}}",
081: 114 - 48);
082: }
083:
084: public void testFieldLike() throws Exception {
085: performAnalysisTest(
086: "test/Test.java",
087: "package test; public class Test {public void test() {Collections.emptyList();}}",
088: 107 - 48);
089: }
090:
091: public void testMemberSelect1() throws Exception {
092: performAnalysisTest(
093: "test/Test.java",
094: "package test; public class Test {public void test() {emptyList().doSomething();}}",
095: 107 - 48,
096: "CreateMethodFix:emptyList()java.lang.Object:test.Test");
097: }
098:
099: public void testMemberSelect2() throws Exception {
100: performAnalysisTest(
101: "test/Test.java",
102: "package test; public class Test {public Test test() {test().doSomething();}}",
103: 112 - 48, "CreateMethodFix:doSomething()void:test.Test");
104: }
105:
106: public void testAssignment() throws Exception {
107: performAnalysisTest(
108: "test/Test.java",
109: "package test; public class Test {public void test() {int i = fff();}}",
110: 110 - 48, "CreateMethodFix:fff()int:test.Test");
111: }
112:
113: public void testNewInAnnonymousInnerclass() throws Exception {
114: performAnalysisTest(
115: "test/Test.java",
116: "package test; public class Test {public Test(){} public void test() {new Runnable() {public void run() {new Test(1);}}}}",
117: 158 - 48, "CreateConstructorFix:(int i):test.Test");
118: }
119:
120: public void testCreateMethodInInterface() throws Exception {
121: performFixTest(
122: "test/Test.java",
123: "package test; public class Test {public void test() {Int i = null; i.test(1);} public static interface Int{}}",
124: 96 - 24,
125: "CreateMethodFix:test(int i)void:test.Test.Int",
126: "package test; public class Test {public void test() {Int i = null; i.test(1);} public static interface Int{ public void test(int i); }}");
127: }
128:
129: public void testCreateMethod106255() throws Exception {
130: performFixTest(
131: "test/Test.java",
132: "package test; public class Test {public void test() {test2(null);}}",
133: 82 - 25,
134: "CreateMethodFix:test2(java.lang.Object object)void:test.Test",
135: "package test; public class Test {public void test() {test2(null);} private void test2(Object object) { throw new UnsupportedOperationException(\"Not yet implemented\"); } }");
136: }
137:
138: public void testCreateMethod77038() throws Exception {
139: performFixTest(
140: "test/Test.java",
141: "package test; public class Test {public void test() {b(test2() ? true : false);} void t(boolean b){}}",
142: 82 - 25,
143: "CreateMethodFix:test2()boolean:test.Test",
144: "package test; public class Test {public void test() {b(test2() ? true : false);} void t(boolean b){} private boolean test2() { throw new UnsupportedOperationException(\"Not yet implemented\"); } }");
145: }
146:
147: public void testCreateMethod82923() throws Exception {
148: performAnalysisTest(
149: "test/Test.java",
150: "package test; public class Test {public void test() {int i = 0; switch (i) {case 1: fff(); break;}}}",
151: 134 - 48, "CreateMethodFix:fff()void:test.Test");
152: }
153:
154: public void testCreateMethod82931() throws Exception {
155: performFixTest(
156: "test/Test.java",
157: "package test; import java.util.Collection; public class Test {public static void test() {fff(getStrings());} private static Collection<String> getStrings() {return null;}}",
158: 116 - 25,
159: "CreateMethodFix:fff(java.util.Collection<java.lang.String> strings)void:test.Test",
160: "package test; import java.util.Collection; public class Test {public static void test() {fff(getStrings());} private static void fff(Collection<String> strings) { throw new UnsupportedOperationException(\"Not yet implemented\"); } private static Collection<String> getStrings() {return null;}}");
161: }
162:
163: public void testCreateMethod74129() throws Exception {
164: performFixTest(
165: "test/Test.java",
166: "package test; public class Test {public void test() {TopLevel.fff();}} class TopLevel {}",
167: 89 - 25,
168: "CreateMethodFix:fff()void:test.TopLevel",
169: "package test; public class Test {public void test() {TopLevel.fff();}} class TopLevel { static void fff() { throw new UnsupportedOperationException(\"Not yet implemented\"); } }");
170: }
171:
172: public void testCreateMethod76498() throws Exception {
173: performFixTest(
174: "test/Test.java",
175: "package test; public class Test {public static class T extends Test {public void test() {super.fff();}}}",
176: 122 - 25,
177: "CreateMethodFix:fff()void:test.Test",
178: "package test; public class Test {public static class T extends Test {public void test() {super.fff();}} private void fff() { throw new UnsupportedOperationException(\"Not yet implemented\"); } }");
179: }
180:
181: public void testCreateMethod75069() throws Exception {
182: performFixTest(
183: "test/Test.java",
184: "package test; public class Test<T> {public void test() {this.fff();}}",
185: 88 - 25,
186: "CreateMethodFix:fff()void:test.Test",
187: "package test; public class Test<T> {public void test() {this.fff();} private void fff() { throw new UnsupportedOperationException(\"Not yet implemented\"); } }");
188: }
189:
190: public void testCreateMethod119037() throws Exception {
191: performFixTest(
192: "test/Test.java",
193: "package test; public class Test {static {f|ff();}}",
194: "CreateMethodFix:fff()void:test.Test",
195: "package test; public class Test {static {fff();} private static void fff() { throw new UnsupportedOperationException(\"Not yet implemented\"); } }");
196: }
197:
198: protected List<Fix> computeFixes(CompilationInfo info, int pos,
199: TreePath path) throws IOException {
200: List<Fix> fixes = new CreateElement().analyze(info, pos);
201: List<Fix> result = new LinkedList<Fix>();
202:
203: for (Fix f : fixes) {
204: if (f instanceof CreateMethodFix)
205: result.add(f);
206: }
207:
208: return result;
209: }
210:
211: @Override
212: protected String toDebugString(CompilationInfo info, Fix f) {
213: return ((CreateMethodFix) f).toDebugString(info);
214: }
215:
216: }
|