sandbox/java.hints/java.hints.test/test/unit/src/org/netbeans/modules/java/hints/test/api/HintTestTest.java
1.1 --- a/sandbox/java.hints/java.hints.test/test/unit/src/org/netbeans/modules/java/hints/test/api/HintTestTest.java Mon Dec 19 11:37:36 2016 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,336 +0,0 @@
1.4 -/*
1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 - *
1.7 - * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
1.8 - *
1.9 - * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
1.10 - * Other names may be trademarks of their respective owners.
1.11 - *
1.12 - * The contents of this file are subject to the terms of either the GNU
1.13 - * General Public License Version 2 only ("GPL") or the Common
1.14 - * Development and Distribution License("CDDL") (collectively, the
1.15 - * "License"). You may not use this file except in compliance with the
1.16 - * License. You can obtain a copy of the License at
1.17 - * http://www.netbeans.org/cddl-gplv2.html
1.18 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.19 - * specific language governing permissions and limitations under the
1.20 - * License. When distributing the software, include this License Header
1.21 - * Notice in each file and include the License file at
1.22 - * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
1.23 - * particular file as subject to the "Classpath" exception as provided
1.24 - * by Oracle in the GPL Version 2 section of the License file that
1.25 - * accompanied this code. If applicable, add the following below the
1.26 - * License Header, with the fields enclosed by brackets [] replaced by
1.27 - * your own identifying information:
1.28 - * "Portions Copyrighted [year] [name of copyright owner]"
1.29 - *
1.30 - * If you wish your version of this file to be governed by only the CDDL
1.31 - * or only the GPL Version 2, indicate your decision by adding
1.32 - * "[Contributor] elects to include this software in this distribution
1.33 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.34 - * single choice of license, a recipient has the option to distribute
1.35 - * your version of this file under either the CDDL, the GPL Version 2 or
1.36 - * to extend the choice of license to its licensees as provided above.
1.37 - * However, if you add GPL Version 2 code and therefore, elected the GPL
1.38 - * Version 2 license, then the option applies only if the new code is
1.39 - * made subject to such option by the copyright holder.
1.40 - *
1.41 - * Contributor(s):
1.42 - *
1.43 - * Portions Copyrighted 2012 Sun Microsystems, Inc.
1.44 - */
1.45 -package org.netbeans.modules.java.hints.test.api;
1.46 -
1.47 -import com.sun.source.tree.Tree.Kind;
1.48 -import com.sun.source.util.TreePath;
1.49 -import java.io.ByteArrayOutputStream;
1.50 -import java.io.InputStreamReader;
1.51 -import java.io.OutputStream;
1.52 -import java.io.OutputStreamWriter;
1.53 -import java.io.Reader;
1.54 -import java.io.Writer;
1.55 -import javax.swing.text.Document;
1.56 -import org.junit.Assert;
1.57 -import org.junit.Test;
1.58 -import org.netbeans.api.java.source.ClasspathInfo.PathKind;
1.59 -import org.netbeans.api.java.source.CompilationInfo;
1.60 -import org.netbeans.api.java.source.JavaSource;
1.61 -import org.netbeans.spi.editor.hints.ChangeInfo;
1.62 -import org.netbeans.spi.editor.hints.ErrorDescription;
1.63 -import org.netbeans.spi.editor.hints.Fix;
1.64 -import org.netbeans.spi.java.hints.ErrorDescriptionFactory;
1.65 -import org.netbeans.spi.java.hints.Hint;
1.66 -import org.netbeans.spi.java.hints.HintContext;
1.67 -import org.netbeans.spi.java.hints.JavaFix;
1.68 -import org.netbeans.spi.java.hints.JavaFix.TransformationContext;
1.69 -import org.netbeans.spi.java.hints.TriggerTreeKind;
1.70 -import org.openide.LifecycleManager;
1.71 -import org.openide.cookies.EditorCookie;
1.72 -import org.openide.filesystems.FileObject;
1.73 -import org.openide.filesystems.FileUtil;
1.74 -import org.openide.loaders.DataObject;
1.75 -
1.76 -/**
1.77 - *
1.78 - * @author lahvac
1.79 - */
1.80 -public class HintTestTest {
1.81 -
1.82 - public HintTestTest() {
1.83 - }
1.84 -
1.85 - @Test
1.86 - public void testNonJavaChanges() throws Exception {
1.87 - HintTest.create()
1.88 - .input("package test;\n" +
1.89 - "public class Test { }\n")
1.90 - .input("test/test.txt", "1\n2\n", false)
1.91 - .run(NonJavaChanges.class)
1.92 - .findWarning("1:13-1:17:verifier:Test")
1.93 - .applyFix()
1.94 - .assertVerbatimOutput("test/test.txt", "2\n3\n");
1.95 - }
1.96 -
1.97 - @Test
1.98 - public void test220070() throws Exception {
1.99 - HintTest.create()
1.100 - .input("package test;\n" +
1.101 - "public class Test { }\n")
1.102 - .input("test/test.txt", "1\n2\n", false)
1.103 - .run(NonJavaChanges.class)
1.104 - .findWarning("1:13-1:17:verifier:Test")
1.105 - .applyFix()
1.106 - .assertOutput("test/test.txt", "2\r3\r");
1.107 - }
1.108 -
1.109 - @Test
1.110 - public void testNonJavaChangesOpenedInEditor() throws Exception {
1.111 - try {
1.112 - HintTest ht = HintTest.create()
1.113 - .input("package test;\n" +
1.114 - "public class Test { }\n")
1.115 - .input("test/test.txt", "1\n2\n", false);
1.116 - FileObject resource = ht.getSourceRoot().getFileObject("test/test.txt");
1.117 - DataObject od = DataObject.find(resource);
1.118 - EditorCookie ec = od.getLookup().lookup(EditorCookie.class);
1.119 - Document doc = ec.openDocument();
1.120 - doc.remove(0, doc.getLength());
1.121 - doc.insertString(0, "5\n6\n", null);
1.122 - ht.run(NonJavaChanges.class)
1.123 - .findWarning("1:13-1:17:verifier:Test")
1.124 - .applyFix(false)
1.125 - .assertVerbatimOutput("test/test.txt", "6\n7\n");
1.126 - Assert.assertEquals("1\n2\n", resource.asText("UTF-8"));
1.127 - Assert.assertEquals("6\n7\n", doc.getText(0, doc.getLength()));
1.128 - } finally {
1.129 - LifecycleManager.getDefault().saveAll();
1.130 - }
1.131 - }
1.132 -
1.133 - @Hint(displayName="testingNonJavaChanges", description="testingNonJavaChanges", category="test")
1.134 - public static final class NonJavaChanges {
1.135 - @TriggerTreeKind(Kind.CLASS)
1.136 - public static ErrorDescription hint(HintContext ctx) {
1.137 - return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), "Test", new TestingNonJavaChangesFix(ctx.getInfo(), ctx.getPath()).toEditorFix());
1.138 - }
1.139 - }
1.140 -
1.141 - private static final class TestingNonJavaChangesFix extends JavaFix {
1.142 -
1.143 - public TestingNonJavaChangesFix(CompilationInfo info, TreePath tp) {
1.144 - super(info, tp);
1.145 - }
1.146 -
1.147 - @Override protected String getText() {
1.148 - return "Test";
1.149 - }
1.150 -
1.151 - @Override protected void performRewrite(TransformationContext ctx) {
1.152 - try {
1.153 - FileObject resource = ctx.getWorkingCopy().getFileObject().getParent().getFileObject("test.txt");
1.154 - Assert.assertNotNull(resource);
1.155 - Reader r = new InputStreamReader(ctx.getResourceContent(resource), "UTF-8");
1.156 - ByteArrayOutputStream outData = new ByteArrayOutputStream();
1.157 - Writer w = new OutputStreamWriter(outData, "UTF-8");
1.158 - int read;
1.159 -
1.160 - while ((read = r.read()) != -1) {
1.161 - if (read != '\n') read++;
1.162 - w.write(read);
1.163 - }
1.164 -
1.165 - r.close();
1.166 - w.close();
1.167 -
1.168 - OutputStream out = ctx.getResourceOutput(resource);
1.169 -
1.170 - out.write(outData.toByteArray());
1.171 -
1.172 - out.close();
1.173 - } catch (Exception ex) {
1.174 - throw new IllegalStateException(ex);
1.175 - }
1.176 - }
1.177 -
1.178 - }
1.179 -
1.180 - @Test
1.181 - public void testMeaningfullSourcePath() throws Exception {
1.182 - HintTest.create()
1.183 - .input("package test;\n" +
1.184 - "public class Test { }\n")
1.185 - .run(MeaningfullSourcePath.class)
1.186 - .assertWarnings();
1.187 - }
1.188 -
1.189 - @Hint(displayName="meaningfullSourcePath", description="meaningfullSourcePath", category="test")
1.190 - public static final class MeaningfullSourcePath {
1.191 - @TriggerTreeKind(Kind.CLASS)
1.192 - public static ErrorDescription hint(HintContext ctx) {
1.193 - if (ctx.getInfo().getClasspathInfo().getClassPath(PathKind.SOURCE).findOwnerRoot(ctx.getInfo().getFileObject()) == null) {
1.194 - return ErrorDescriptionFactory.forTree(ctx, ctx.getPath(), "Broken Source Path");
1.195 - }
1.196 -
1.197 - return null;
1.198 - }
1.199 - }
1.200 -
1.201 - @Test
1.202 - public void testCompilationClassPath() throws Exception {
1.203 - HintTest.create()
1.204 - .input("package test;\n" +
1.205 - "public class Test { }\n")
1.206 - .classpath(FileUtil.getArchiveRoot(JavaSource.class.getProtectionDomain().getCodeSource().getLocation()))
1.207 - .run(CompilationClassPath.class)
1.208 - .assertWarnings();
1.209 - }
1.210 -
1.211 - @Hint(displayName="compilationClassPath", description="compilationClassPath", category="test")
1.212 - public static final class CompilationClassPath {
1.213 - @TriggerTreeKind(Kind.CLASS)
1.214 - public static ErrorDescription hint(HintContext ctx) {
1.215 - FileObject clazz = ctx.getInfo().getClasspathInfo().getClassPath(PathKind.COMPILE).findResource("org/netbeans/api/java/source/JavaSource.class");
1.216 -
1.217 - if (clazz == null) {
1.218 - return ErrorDescriptionFactory.forTree(ctx, ctx.getPath(), "Broken Compilation ClassPath");
1.219 - }
1.220 -
1.221 - return null;
1.222 - }
1.223 - }
1.224 -
1.225 - @Test
1.226 - public void testHintThrowsException() throws Exception {
1.227 - HintTest ht = HintTest.create()
1.228 - .input("package test;\n" +
1.229 - "public class Test { }\n");
1.230 - try {
1.231 - ht.run(HintThrowsException.class);
1.232 - Assert.fail("No exception thrown");
1.233 - } catch (Exception ex) {
1.234 - //ok
1.235 - Assert.assertEquals(IllegalStateException.class, ex.getClass());
1.236 - Assert.assertNotNull(ex.getCause());
1.237 - Assert.assertEquals(NullPointerException.class, ex.getCause().getClass());
1.238 - Assert.assertEquals("a", ex.getCause().getMessage());
1.239 - }
1.240 - }
1.241 -
1.242 - @Hint(displayName="hintThrowsException", description="hintThrowsException", category="test")
1.243 - public static final class HintThrowsException {
1.244 - @TriggerTreeKind(Kind.CLASS)
1.245 - public static ErrorDescription hint(HintContext ctx) {
1.246 - throw new NullPointerException("a");
1.247 - }
1.248 - }
1.249 -
1.250 - @Test
1.251 - public void testNonJavaFix() throws Exception {
1.252 - HintTest ht = HintTest.create()
1.253 - .input("package test;\n" +
1.254 - "public class Test { }\n");
1.255 - try {
1.256 - ht.run(NonJavaFix.class)
1.257 - .findWarning("1:0-1:21:verifier:Test")
1.258 - .applyFix();
1.259 - Assert.fail("No exception thrown");
1.260 - } catch (AssertionError ae) {
1.261 - //ok
1.262 - Assert.assertEquals("The fix must be a JavaFix", ae.getMessage());
1.263 - }
1.264 - }
1.265 -
1.266 - @Hint(displayName="nonJavaFix", description="nonJavaFix", category="test")
1.267 - public static final class NonJavaFix {
1.268 - @TriggerTreeKind(Kind.CLASS)
1.269 - public static ErrorDescription hint(HintContext ctx) {
1.270 - return ErrorDescriptionFactory.forTree(ctx, ctx.getPath(), "Test", new Fix() {
1.271 - @Override public String getText() {
1.272 - return "Fix";
1.273 - }
1.274 - @Override public ChangeInfo implement() throws Exception {
1.275 - return null;
1.276 - }
1.277 - });
1.278 - }
1.279 - }
1.280 -
1.281 - @Test
1.282 - public void testNotRepeatableJavaFix() throws Exception {
1.283 - HintTest ht = HintTest.create()
1.284 - .input("package test;\n" +
1.285 - "public class Test { }\n");
1.286 - try {
1.287 - ht.run(NotRepeatableJavaFix.class)
1.288 - .findWarning("1:0-1:21:verifier:Test")
1.289 - .applyFix();
1.290 - Assert.fail("No exception thrown");
1.291 - } catch (AssertionError ae) {
1.292 - //ok
1.293 - Assert.assertTrue(ae.getMessage().startsWith("The fix must be repeatable"));
1.294 - }
1.295 - }
1.296 -
1.297 - @Hint(displayName="notRepeatableJavaFix", description="notRepeatableJavaFix", category="test")
1.298 - public static final class NotRepeatableJavaFix {
1.299 - @TriggerTreeKind(Kind.CLASS)
1.300 - public static ErrorDescription hint(HintContext ctx) {
1.301 - Fix f = new JavaFix(ctx.getInfo(), ctx.getPath()) {
1.302 - private boolean wasRun;
1.303 - @Override protected String getText() {
1.304 - return "Fix";
1.305 - }
1.306 - @Override protected void performRewrite(TransformationContext ctx) throws Exception {
1.307 - if (wasRun) return ;
1.308 - ctx.getWorkingCopy().rewrite(ctx.getPath().getLeaf(), ctx.getWorkingCopy().getTreeMaker().setLabel(ctx.getPath().getLeaf(), "Nue"));
1.309 - wasRun = true;
1.310 - }
1.311 - }.toEditorFix();
1.312 - return ErrorDescriptionFactory.forTree(ctx, ctx.getPath(), "Test", f);
1.313 - }
1.314 - }
1.315 -
1.316 - public void testSourcePathReady() throws Exception {
1.317 - HintTest.create()
1.318 - .input("test/Test1.java",
1.319 - "package test;\n" +
1.320 - "public class Test1 {\n" +
1.321 - " private final Test2 test2 = null;\n" +
1.322 - "}\n")
1.323 - .input("test/Test2.java",
1.324 - "package test;\n" +
1.325 - "public class Test2 {\n" +
1.326 - " private final Test1 test1 = null;\n" +
1.327 - "}\n")
1.328 - .runGlobal(NoOp.class)
1.329 - .assertWarnings();
1.330 - }
1.331 -
1.332 - @Hint(displayName="noOp", description="noOp", category="test")
1.333 - public static final class NoOp {
1.334 - @TriggerTreeKind(Kind.CLASS)
1.335 - public static ErrorDescription hint(HintContext ctx) {
1.336 - return null;
1.337 - }
1.338 - }
1.339 -}