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 javax.swing.text.Document;
045: import org.netbeans.api.java.lexer.JavaTokenId;
046: import org.netbeans.api.java.source.CompilationInfo;
047: import org.netbeans.api.java.source.JavaSource;
048: import org.netbeans.api.java.source.JavaSource.Phase;
049: import org.netbeans.api.java.source.SourceUtilsTestUtil;
050: import org.netbeans.api.java.source.TestUtilities;
051: import org.netbeans.api.lexer.Language;
052: import org.netbeans.junit.NbTestCase;
053: import org.openide.cookies.EditorCookie;
054: import org.openide.filesystems.FileObject;
055: import org.openide.filesystems.FileUtil;
056: import org.openide.loaders.DataObject;
057:
058: /**
059: *
060: * @author Jan Lahoda
061: */
062: public class UtilitiesTest extends NbTestCase {
063:
064: public UtilitiesTest(String testName) {
065: super (testName);
066: }
067:
068: protected @Override
069: void setUp() throws Exception {
070: SourceUtilsTestUtil.prepareTest(new String[0], new Object[0]);
071: super .setUp();
072: }
073:
074: public void testNameGuess1() throws Exception {
075: performNameGuessTest(
076: "package test; public class Test {public void t() {toString();}}",
077: 54, "toString");
078: }
079:
080: public void testNameGuess2() throws Exception {
081: performNameGuessTest(
082: "package test; public class Test {public void t() {getX();} public int getX() {return 0;}}",
083: 54, "x");
084: }
085:
086: public void testNameGuess3() throws Exception {
087: performNameGuessTest(
088: "package test; public class Test {public void t() {getData();} public int getData() {return 0;}}",
089: 54, "data");
090: }
091:
092: public void testNameGuess4() throws Exception {
093: performNameGuessTest(
094: "package test; public class Test {public void t() {getProcessedData();} public int getProcessedData() {return 0;}}",
095: 54, "processedData");
096: }
097:
098: public void testNameGuess5() throws Exception {
099: performNameGuessTest(
100: "package test; public class Test {public void t() {isEnabled();} public boolean isEnabled() {return true;}}",
101: 54, "enabled");
102: }
103:
104: public void testNameGuess6() throws Exception {
105: performNameGuessTest(
106: "package test; public class Test {public void t() {get();} public int get() {return 0;}}",
107: 52, "get");
108: }
109:
110: public void testNameGuessKeyword() throws Exception {
111: performNameGuessTest(
112: "package test; public class Test {public void t() {getDo();} public int getDo() {return 0;}}",
113: 52, "aDo");
114: }
115:
116: public void testNameGuessKeywordNoShortName() throws Exception {
117: performNameGuessTest(
118: "package test; public class Test {public void t() {t(this);}}",
119: 54, "aThis");
120: }
121:
122: public void testNameGuessKeywordNoShortName2() throws Exception {
123: assertEquals("aDo", Utilities.adjustName("do"));
124: }
125:
126: protected void prepareTest(String code) throws Exception {
127: clearWorkDir();
128: FileObject workFO = FileUtil.toFileObject(getWorkDir());
129:
130: assertNotNull(workFO);
131:
132: FileObject sourceRoot = workFO.createFolder("src");
133: FileObject buildRoot = workFO.createFolder("build");
134: FileObject cache = workFO.createFolder("cache");
135:
136: FileObject data = FileUtil.createData(sourceRoot,
137: "test/Test.java");
138:
139: TestUtilities.copyStringToFile(FileUtil.toFile(data), code);
140:
141: SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, cache);
142:
143: DataObject od = DataObject.find(data);
144: EditorCookie ec = od.getCookie(EditorCookie.class);
145:
146: assertNotNull(ec);
147:
148: Document doc = ec.openDocument();
149:
150: doc.putProperty(Language.class, JavaTokenId.language());
151:
152: JavaSource js = JavaSource.forFileObject(data);
153:
154: assertNotNull(js);
155:
156: info = SourceUtilsTestUtil.getCompilationInfo(js,
157: Phase.RESOLVED);
158:
159: assertNotNull(info);
160: }
161:
162: private CompilationInfo info;
163:
164: private void performNameGuessTest(String code, int position,
165: String desiredName) throws Exception {
166: prepareTest(code);
167:
168: TreePath tp = info.getTreeUtilities().pathFor(position);
169:
170: String name = Utilities.guessName(info, tp);
171:
172: assertEquals(desiredName, name);
173: }
174:
175: }
|