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;
042:
043: import com.sun.source.util.TreePath;
044: import java.util.List;
045: import java.util.Locale;
046: import org.netbeans.api.java.source.CompilationInfo;
047: import org.netbeans.api.java.source.SourceUtilsTestUtil;
048: import org.netbeans.modules.java.hints.infrastructure.TreeRuleTestBase;
049: import org.netbeans.spi.editor.hints.ErrorDescription;
050: import org.openide.util.NbBundle;
051:
052: /**
053: *
054: * @author Jaroslav Tulach
055: */
056: public class StaticAccessTest extends TreeRuleTestBase {
057:
058: public StaticAccessTest(String testName) {
059: super (testName);
060: }
061:
062: @Override
063: protected void setUp() throws Exception {
064: Locale.setDefault(Locale.US);
065: SourceUtilsTestUtil.setLookup(new Object[0], getClass()
066: .getClassLoader());
067: }
068:
069: public void testCallingStaticMethodInInitializer() throws Exception {
070: String before = "package test; class Test {\n" + "{\n"
071: + "Boolean b = null;\n" + "b = b.valu";
072: String after = "eOf(true);\n" + "}\n" + "}\n";
073:
074: String golden = (before + after).replace('\n', ' ').replace(
075: "b.value", "Boolean.value");
076:
077: performFixTest("test/Test.java", before + after, before
078: .length(), "3:4-3:5:verifier:AS1valueOf",
079: "FixStaticAccess", golden);
080: }
081:
082: public void testCallingStaticMethod() throws Exception {
083: String before = "package test; class Test {\n"
084: + "public void nic() {\n" + "Boolean b = null;\n"
085: + "b = b.valu";
086: String after = "eOf(true);\n" + "}\n" + "}\n";
087:
088: String golden = ("package test; class Test {\n"
089: + "public void nic() {\n" + "Boolean b = null;\n"
090: + "b = Boolean.valueOf(true);\n" + "}\n" + "}\n")
091: .replace('\n', ' ');
092:
093: performFixTest("test/Test.java", before + after, before
094: .length(), "3:4-3:5:verifier:AS1valueOf",
095: "FixStaticAccess", golden);
096: }
097:
098: public void testCallingNonStaticStringMethod() throws Exception {
099: String before = "package test; class Test {\n"
100: + "public void nic() {\n" + "String s = \"some\";\n"
101: + "int x = s.last";
102: String after = "IndexOf('x');" + "}" + "";
103:
104: performAnalysisTest("test/Test.java", before + after, before
105: .length());
106: }
107:
108: public void testOkCallingStaticMethod() throws Exception {
109: String before = "package test; class Test {" + "{"
110: + " Boolean b = null;" + " b = Boolean.valu";
111: String after = "eOf(true);";
112:
113: performAnalysisTest("test/Test.java", before + after, before
114: .length());
115: }
116:
117: public void testAccessingStaticField() throws Exception {
118: String before = "package test; class Test {" + "{"
119: + " Boolean b = null;" + " b = b.TR";
120: String after = "UE;";
121:
122: performAnalysisTest("test/Test.java", before + after, before
123: .length(), "0:50-0:51:verifier:AS0TRUE");
124: }
125:
126: public void testOkAccessingStaticField() throws Exception {
127: String before = "package test; class Test {" + "{"
128: + " Boolean b = null;" + " b = Boolean.TR";
129: String after = "UE;";
130:
131: performAnalysisTest("test/Test.java", before + after, before
132: .length());
133: }
134:
135: public void testAccessingStaticFieldViaMethod() throws Exception {
136: String before = "package test; class Test {"
137: + "static Boolean b() { return null; }" + "{"
138: + " Object x = b().TR";
139: String after = "UE;";
140:
141: performAnalysisTest("test/Test.java", before + after, before
142: .length(), "0:74-0:77:verifier:AS0TRUE");
143: }
144:
145: public void testOkToCallEqualsOnString() throws Exception {
146: String before = "package test; class Test {"
147: + "public void run() {\n" + "String s = null;\n"
148: + "boolean b = \"A\".e";
149: String after = "quals(s);\n" + "}" + "}";
150:
151: performAnalysisTest("test/Test.java", before + after, before
152: .length());
153: }
154:
155: public void testCorrectResolution() throws Exception {
156: String before = "package test; class Test {"
157: + "public void run() {\n" + "Test t = null;\n" + "t.t";
158: String after = "est(2);\n" + "}\n" + "public void test() {}\n"
159: + "public static void test(int i) {}\n" + "}";
160:
161: performAnalysisTest("test/Test.java", before + after, before
162: .length(), "2:0-2:1:verifier:AS1test");
163: }
164:
165: public void testIgnoreErrors1() throws Exception {
166: String code = "package test; class Test {\n"
167: + "public void run() {\n"
168: + "aaa.getClass().getNa|me();\n" + "}\n" + "}";
169:
170: performAnalysisTest("test/Test.java", code);
171: }
172:
173: public void testIgnoreErrors2() throws Exception {
174: String code = "package test; class Test {\n"
175: + "public void run() {\n" + "aaa.getCl|ass();\n"
176: + "}\n" + "}";
177:
178: performAnalysisTest("test/Test.java", code);
179: }
180:
181: public void testIgnoreErrors3() throws Exception {
182: String code = "package test; class Test {\n"
183: + "public void run(String aaa) {\n" + "aaa.ff|f();\n"
184: + "}\n" + "}";
185:
186: performAnalysisTest("test/Test.java", code);
187: }
188:
189: public void testIgnoreErrors4() throws Exception {
190: String code = "package test; class Test {\n"
191: + "public void run() {\n" + "super.ff|f();\n" + "}\n"
192: + "}";
193:
194: performAnalysisTest("test/Test.java", code);
195: }
196:
197: protected List<ErrorDescription> computeErrors(
198: CompilationInfo info, TreePath path) {
199: SourceUtilsTestUtil.setSourceLevel(info.getFileObject(),
200: sourceLevel);
201: return new StaticAccess().run(info, path);
202: }
203:
204: private String sourceLevel = "1.5";
205:
206: static {
207: NbBundle.setBranding("test");
208: }
209:
210: }
|