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.infrastructure;
042:
043: import com.sun.source.util.TreePath;
044: import java.io.File;
045: import java.util.Arrays;
046: import java.util.Collections;
047: import java.util.LinkedList;
048: import java.util.List;
049: import javax.swing.text.Document;
050: import org.netbeans.api.java.lexer.JavaTokenId;
051: import org.netbeans.api.java.source.CompilationInfo;
052: import org.netbeans.api.java.source.JavaSource;
053: import org.netbeans.api.java.source.JavaSource.Phase;
054: import org.netbeans.api.java.source.SourceUtilsTestUtil;
055: import org.netbeans.api.java.source.TestUtilities;
056: import org.netbeans.api.lexer.Language;
057: import org.netbeans.junit.NbTestCase;
058: import org.netbeans.modules.java.JavaDataLoader;
059: import org.netbeans.spi.editor.hints.Fix;
060: import org.netbeans.spi.editor.hints.Fix;
061: import org.openide.LifecycleManager;
062: import org.openide.cookies.EditorCookie;
063: import org.openide.filesystems.FileObject;
064: import org.openide.filesystems.FileUtil;
065: import org.openide.loaders.DataObject;
066:
067: /**
068: * @author Jan Lahoda
069: */
070: public abstract class ErrorHintsTestBase extends NbTestCase {
071:
072: public ErrorHintsTestBase(String name) {
073: super (name);
074:
075: }
076:
077: @Override
078: protected void setUp() throws Exception {
079: super .setUp();
080: String[] additionalLayers = getAdditionalLayers();
081: String[] layers = new String[additionalLayers.length + 1];
082:
083: System.arraycopy(additionalLayers, 0, layers, 1,
084: additionalLayers.length);
085: layers[0] = "org/netbeans/modules/java/editor/resources/layer.xml";
086:
087: SourceUtilsTestUtil.prepareTest(layers,
088: new Object[] { JavaDataLoader.class });
089: }
090:
091: private void prepareTest(String fileName, String code)
092: throws Exception {
093: clearWorkDir();
094:
095: FileObject workFO = FileUtil.toFileObject(getWorkDir());
096:
097: assertNotNull(workFO);
098:
099: sourceRoot = workFO.createFolder("src");
100:
101: FileObject buildRoot = workFO.createFolder("build");
102: FileObject cache = workFO.createFolder("cache");
103:
104: FileObject data = FileUtil.createData(sourceRoot, fileName);
105: File dataFile = FileUtil.toFile(data);
106:
107: assertNotNull(dataFile);
108:
109: TestUtilities.copyStringToFile(dataFile, code);
110:
111: SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, cache,
112: getExtraClassPathElements());
113:
114: DataObject od = DataObject.find(data);
115: EditorCookie ec = od.getCookie(EditorCookie.class);
116:
117: assertNotNull(ec);
118:
119: doc = ec.openDocument();
120: doc.putProperty(Language.class, JavaTokenId.language());
121:
122: JavaSource js = JavaSource.forFileObject(data);
123:
124: assertNotNull(js);
125:
126: info = SourceUtilsTestUtil.getCompilationInfo(js,
127: Phase.RESOLVED);
128:
129: assertNotNull(info);
130: }
131:
132: private FileObject sourceRoot;
133: private CompilationInfo info;
134: private Document doc;
135:
136: protected abstract List<Fix> computeFixes(CompilationInfo info,
137: int pos, TreePath path) throws Exception;
138:
139: protected String toDebugString(CompilationInfo info, Fix f) {
140: return f.toString();
141: }
142:
143: protected String[] getAdditionalLayers() {
144: return new String[0];
145: }
146:
147: protected FileObject[] getExtraClassPathElements() {
148: return new FileObject[0];
149: }
150:
151: protected void performAnalysisTest(String fileName, String code,
152: String... golden) throws Exception {
153: int[] caretPosition = new int[1];
154:
155: code = org.netbeans.modules.java.hints.TestUtilities
156: .detectOffsets(code, caretPosition);
157:
158: performAnalysisTest(fileName, code, caretPosition[0], golden);
159: }
160:
161: protected void performAnalysisTest(String fileName, String code,
162: int pos, String... golden) throws Exception {
163: prepareTest(fileName, code);
164:
165: TreePath path = info.getTreeUtilities().pathFor(pos);
166:
167: List<Fix> fixes = computeFixes(info, pos, path);
168: List<String> fixesNames = new LinkedList<String>();
169:
170: fixes = fixes != null ? fixes : Collections.<Fix> emptyList();
171:
172: for (Fix e : fixes) {
173: fixesNames.add(toDebugString(info, e));
174: }
175:
176: assertTrue(fixesNames.toString(), Arrays.equals(golden,
177: fixesNames.toArray(new String[0])));
178: }
179:
180: protected void performFixTest(String fileName, String code,
181: String fixCode, String golden) throws Exception {
182: int[] caretPosition = new int[1];
183:
184: code = org.netbeans.modules.java.hints.TestUtilities
185: .detectOffsets(code, caretPosition);
186:
187: performFixTest(fileName, code, caretPosition[0], fixCode,
188: golden);
189: }
190:
191: protected void performFixTest(String fileName, String code,
192: int pos, String fixCode, String golden) throws Exception {
193: performFixTest(fileName, code, pos, fixCode, fileName, golden);
194: }
195:
196: protected void performFixTest(String fileName, String code,
197: int pos, String fixCode, String goldenFileName,
198: String golden) throws Exception {
199: prepareTest(fileName, code);
200:
201: TreePath path = info.getTreeUtilities().pathFor(pos);
202:
203: List<Fix> fixes = computeFixes(info, pos, path);
204: List<String> fixesNames = new LinkedList<String>();
205:
206: fixes = fixes != null ? fixes : Collections.<Fix> emptyList();
207:
208: Fix fix = null;
209:
210: for (Fix e : fixes) {
211: String debugString = toDebugString(info, e);
212:
213: fixesNames.add(debugString);
214:
215: if (fixCode.equals(debugString))
216: fix = e;
217: }
218:
219: assertNotNull(fixesNames.toString(), fix);
220:
221: fix.implement();
222:
223: FileObject toCheck = sourceRoot.getFileObject(goldenFileName);
224:
225: assertNotNull(toCheck);
226:
227: DataObject toCheckDO = DataObject.find(toCheck);
228: EditorCookie ec = toCheckDO.getLookup().lookup(
229: EditorCookie.class);
230: Document toCheckDocument = ec.openDocument();
231:
232: String realCode = toCheckDocument.getText(0, toCheckDocument
233: .getLength());
234:
235: //ignore whitespaces:
236: realCode = realCode.replaceAll("[ \t\n]+", " ");
237:
238: assertEquals(golden, realCode);
239:
240: LifecycleManager.getDefault().saveAll();
241: }
242:
243: }
|