sandbox/java.hints/spi.java.hints/test/unit/src/org/netbeans/modules/java/hints/spiimpl/pm/BulkSearchTestPerformer.java
1.1 --- a/sandbox/java.hints/spi.java.hints/test/unit/src/org/netbeans/modules/java/hints/spiimpl/pm/BulkSearchTestPerformer.java Mon Dec 19 11:37:36 2016 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,694 +0,0 @@
1.4 -/*
1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 - *
1.7 - * Copyright 2010 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 2009 Sun Microsystems, Inc.
1.44 - */
1.45 -
1.46 -package org.netbeans.modules.java.hints.spiimpl.pm;
1.47 -
1.48 -import org.netbeans.modules.java.hints.spiimpl.pm.BulkSearch.BulkPattern;
1.49 -import org.netbeans.modules.java.hints.spiimpl.pm.BulkSearch.EncodingContext;
1.50 -import com.sun.source.util.TreePath;
1.51 -import java.io.ByteArrayInputStream;
1.52 -import java.io.ByteArrayOutputStream;
1.53 -import java.io.File;
1.54 -import java.util.Arrays;
1.55 -import java.util.Collection;
1.56 -import java.util.Collections;
1.57 -import java.util.HashMap;
1.58 -import java.util.HashSet;
1.59 -import java.util.LinkedList;
1.60 -import java.util.List;
1.61 -import java.util.Map;
1.62 -import java.util.Map.Entry;
1.63 -import java.util.Set;
1.64 -import java.util.concurrent.atomic.AtomicBoolean;
1.65 -import java.util.regex.Pattern;
1.66 -import javax.swing.text.Document;
1.67 -import org.netbeans.api.java.lexer.JavaTokenId;
1.68 -import org.netbeans.api.java.source.CompilationInfo;
1.69 -import org.netbeans.api.java.source.JavaSource;
1.70 -import org.netbeans.api.java.source.JavaSource.Phase;
1.71 -import org.netbeans.api.java.source.SourceUtilsTestUtil;
1.72 -import org.netbeans.api.java.source.TestUtilities;
1.73 -import org.netbeans.api.java.source.TreePathHandle;
1.74 -import org.netbeans.api.lexer.Language;
1.75 -import org.netbeans.junit.NbTestCase;
1.76 -import org.netbeans.modules.java.source.TreeLoader;
1.77 -import org.openide.cookies.EditorCookie;
1.78 -import org.openide.filesystems.FileObject;
1.79 -import org.openide.filesystems.FileUtil;
1.80 -import org.openide.loaders.DataObject;
1.81 -import static org.junit.Assert.*;
1.82 -import org.netbeans.junit.RandomlyFails;
1.83 -
1.84 -/**
1.85 - *
1.86 - * @author lahvac
1.87 - */
1.88 -public abstract class BulkSearchTestPerformer extends NbTestCase {
1.89 -
1.90 - public BulkSearchTestPerformer(String name) {
1.91 - super(name);
1.92 - }
1.93 -
1.94 - @Override
1.95 - protected void setUp() throws Exception {
1.96 - super.setUp();
1.97 - SourceUtilsTestUtil.prepareTest(new String[] {"org/netbeans/modules/java/editor/resources/layer.xml"}, new Object[0]);
1.98 - TreeLoader.DISABLE_CONFINEMENT_TEST = true;
1.99 - }
1.100 -
1.101 -// public static TestSuite suite() {
1.102 -// NbTestSuite s = new NbTestSuite();
1.103 -//
1.104 -// s.addTestSuite(NFABasedBulkSearchTest.class);
1.105 -//
1.106 -// return s;
1.107 -// }
1.108 -
1.109 - public void testSimple1() throws Exception {
1.110 - performTest("package test; public class Test { private void test() { System.err./**/println(\"\");}}",
1.111 - Collections.singletonMap("System.err.println(\"\")", Arrays.asList("System.err./**/println(\"\")")),
1.112 - Arrays.asList("System.err.println(\"\" + \"\")"));
1.113 - }
1.114 -
1.115 - public void testDontCare() throws Exception {
1.116 - performTest("package test; public class Test { private void test() { System.err./**/println(\"\" + \"\");}}",
1.117 - Collections.singletonMap("System.err.println($1)", Arrays.asList("System.err./**/println(\"\" + \"\")")),
1.118 - Collections.<String>emptyList());
1.119 - }
1.120 -
1.121 - public void testMemberSelectAndIdentifier() throws Exception {
1.122 - performTest("package test; public class Test { private static void test() { test();}}",
1.123 - Collections.singletonMap("test.Test.test()", Arrays.asList("test()")),
1.124 - Collections.<String>emptyList());
1.125 - }
1.126 -
1.127 - public void testUnpureMemberSelect() throws Exception {
1.128 - performTest("package test; public class Test { private static void test() { new StringBuilder().append(\"\");}}",
1.129 - Collections.<String, List<String>>emptyMap(),
1.130 - Arrays.asList("test.append(\"\")"));
1.131 - }
1.132 -
1.133 - public void testMemberSelectWithVariables1() throws Exception {
1.134 - performTest("package test; public class Test { private static void test() { new StringBuilder().append(\"\");}}",
1.135 - Collections.singletonMap("$0.append(\"\")", Arrays.asList("new StringBuilder().append(\"\")")),
1.136 - Collections.<String>emptyList());
1.137 - }
1.138 -
1.139 - public void testMemberSelectWithVariables2() throws Exception {
1.140 - performTest("package test; public class Test { private void append(char c) { append(\"\");}}",
1.141 - Collections.singletonMap("$0.append(\"\")", Arrays.asList("append(\"\")")),
1.142 - Collections.<String>emptyList());
1.143 - }
1.144 -
1.145 - public void testLocalVariables() throws Exception {
1.146 - performTest("package test; public class Test { private void test() { { int y; y = 1; } }}",
1.147 - Collections.singletonMap("{ int $1; $1 = 1; }", Arrays.asList("{ int y; y = 1; }")),
1.148 - Collections.<String>emptyList());
1.149 - }
1.150 -
1.151 - public void testAssert() throws Exception {
1.152 - performTest("package test; public class Test { private void test() { assert true : \"\"; }}",
1.153 - Collections.singletonMap("assert $1 : $2;", Arrays.asList("assert true : \"\";")),
1.154 - Collections.<String>emptyList());
1.155 - }
1.156 -
1.157 - public void testStatementAndSingleBlockStatementAreSame1() throws Exception {
1.158 - performTest("package test; public class Test { private void test() { { int y; { y = 1; } } }}",
1.159 - Collections.singletonMap("{ int $1; $1 = 1; }", Arrays.asList("{ int y; { y = 1; } }")),
1.160 - Collections.<String>emptyList());
1.161 - }
1.162 -
1.163 - public void testStatementAndSingleBlockStatementAreSame2() throws Exception {
1.164 - performTest("package test; public class Test { private void test() { { int y; y = 1; } }}",
1.165 - Collections.singletonMap("{ int $1; { $1 = 1; } }", Arrays.asList("{ int y; y = 1; }")),
1.166 - Collections.<String>emptyList());
1.167 - }
1.168 -
1.169 - public void testStatementVariables1() throws Exception {
1.170 - performTest("package test; public class Test { public int test1() { if (true) return 1; else return 2; } }",
1.171 - Collections.singletonMap("if ($1) $2; else $3;", Arrays.asList("if (true) return 1; else return 2;")),
1.172 - Collections.<String>emptyList());
1.173 - }
1.174 -
1.175 - public void testMultiStatementVariables1() throws Exception {
1.176 - performTest("package test; public class Test { public int test1(int i) { System.err.println(i); System.err.println(i); i = 3; System.err.println(i); System.err.println(i); return i; } }",
1.177 - Collections.singletonMap("{ $s1$; i = 3; $s2$; return i; }", Arrays.asList("{ System.err.println(i); System.err.println(i); i = 3; System.err.println(i); System.err.println(i); return i; }")),
1.178 - Collections.<String>emptyList());
1.179 - }
1.180 -
1.181 - public void testMultiStatementVariables2() throws Exception {
1.182 - performTest("package test; public class Test { public int test1(int i) { i = 3; return i; } }",
1.183 - Collections.singletonMap("{ $s1$; i = 3; $s2$; return i; }", Arrays.asList("{ i = 3; return i; }")),
1.184 - Collections.<String>emptyList());
1.185 - }
1.186 -
1.187 - public void testMultiStatementVariablesAndBlocks1() throws Exception {
1.188 - performTest("package test; public class Test { public void test1() { if (true) System.err.println(); } }",
1.189 - Collections.singletonMap("if ($c) {$s1$; System.err.println(); $s2$; }", Arrays.asList("if (true) System.err.println();")),
1.190 - Collections.<String>emptyList());
1.191 - }
1.192 -
1.193 - public void testMultiStatementVariablesAndBlocks2() throws Exception {
1.194 - performTest("package test; public class Test { public void test1() { if (true) System.err.println(); } }",
1.195 - Collections.singletonMap("if ($c) {$s1$; System.err.println(); }", Arrays.asList("if (true) System.err.println();")),
1.196 - Collections.<String>emptyList());
1.197 - }
1.198 -
1.199 - public void testMultiStatementVariablesAndBlocks3() throws Exception {
1.200 - performTest("package test; public class Test { public void test1() { if (true) System.err.println(); } }",
1.201 - Collections.singletonMap("if ($c) {System.err.println(); $s2$; }", Arrays.asList("if (true) System.err.println();")),
1.202 - Collections.<String>emptyList());
1.203 - }
1.204 -
1.205 - public void testMultiStatementVariablesAndBlocks4() throws Exception {
1.206 - performTest("package test; public class Test { public void test1() { if (true) System.err.println(); } }",
1.207 - Collections.singletonMap("{ $s1$; System.err.println(); $s2$; }", Arrays.asList("System.err.println();")),
1.208 - Collections.<String>emptyList());
1.209 - }
1.210 -
1.211 - public void testTwoPatterns() throws Exception {
1.212 - Map<String, List<String>> contained = new HashMap<String, List<String>>();
1.213 -
1.214 - contained.put("if ($a) $ret = $b; else $ret = $c;", Arrays.asList("if (b) q = 2; else q = 3;"));
1.215 - contained.put("{ $p$; $T $v; if($a) $v = $b; else $v = $c; $q$; }", Arrays.asList("{ int q; if (b) q = 2; else q = 3; }"));
1.216 -
1.217 - performTest("package test; public class Test { public void test1(boolean b) { int q; if (b) q = 2; else q = 3; } }",
1.218 - contained,
1.219 - Collections.<String>emptyList());
1.220 - }
1.221 -
1.222 - public void testEffectiveNewClass() throws Exception {
1.223 - performTest("package test; import javax.swing.ImageIcon; public class Test { public void test1(java.awt.Image i) { new ImageIcon(i); new String(i); } }",
1.224 - Collections.singletonMap("new javax.swing.ImageIcon($1)", Arrays.asList("new ImageIcon(i)")),
1.225 - Collections.<String>emptyList());
1.226 - }
1.227 -
1.228 - public void testSynchronizedAndMultiStatementVariables() throws Exception {
1.229 - performTest("package test; public class Test {public void test() { Object o = null; int i = 0; synchronized (o) {} } }",
1.230 - Collections.singletonMap("synchronized($var) {$stmts$;}", Arrays.asList("synchronized (o) {}")),
1.231 - Collections.<String>emptyList());
1.232 - }
1.233 -
1.234 - public void testJackpot30_2() throws Exception {
1.235 - String code = "package test;\n" +
1.236 - "public class Test {\n" +
1.237 - " private void m() {\n" +
1.238 - " a(c.i().getFileObject());\n" +
1.239 - " if (span != null && span[0] != (-1) && span[1] != (-1));\n" +
1.240 - " }\n" +
1.241 - "}\n";
1.242 -
1.243 - performTest(code,
1.244 - Collections.<String, List<String>>emptyMap(),
1.245 - Arrays.asList("$0.getFileObject($1)"));
1.246 - }
1.247 -
1.248 - public void testIdentifierInPureMemberSelect() throws Exception {
1.249 - String code = "package test;\n" +
1.250 - "public class Test {\n" +
1.251 - " public Test test;\n" +
1.252 - " public String name;\n" +
1.253 - " private void test() {\n" +
1.254 - " Test t = null;\n" +
1.255 - " String s = t.test.name;\n" +
1.256 - " }\n" +
1.257 - "}\n";
1.258 -
1.259 - performTest(code,
1.260 - Collections.singletonMap("$Test.test", Arrays.asList("test", "t.test")),
1.261 - Collections.<String>emptyList());
1.262 - }
1.263 -
1.264 - @RandomlyFails
1.265 - public void testNoExponentialTimeComplexity() throws Exception {
1.266 - try {
1.267 - String code = "package test;\n" +
1.268 - "public class Test {\n" +
1.269 - " private void test() {\n" +
1.270 - " Object o;\n" +
1.271 - " if(o == null) {\n" +
1.272 - " f(\"\");\n" +
1.273 - " }|\n" +
1.274 - " }\n" +
1.275 - "}";
1.276 - String pattern = "{ $p$; $T $v; if($a) $v = $b; else $v = $c; }";
1.277 -
1.278 - measure(code, "\na(\"\");", 5, pattern); //to load needed classes, etc.
1.279 -
1.280 - int rep = 1;
1.281 - long baseline;
1.282 -
1.283 - while (true) {
1.284 - baseline = measure(code, "\na(\"\");", rep, pattern);
1.285 -
1.286 - if (baseline >= 2000) {
1.287 - break;
1.288 - }
1.289 -
1.290 - rep *= 2;
1.291 - }
1.292 -
1.293 - long doubleSize = measure(code, "\na(\"\");", 2 * rep, pattern);
1.294 -
1.295 - assertTrue("baseline=" + baseline + ", actual=" + String.valueOf(doubleSize), doubleSize <= 4 * baseline);
1.296 - } catch (OutOfMemoryError oome) {
1.297 - //OK
1.298 - }
1.299 - }
1.300 -
1.301 - public void testMultiParameter1() throws Exception {
1.302 - performTest("package test; public class Test { { java.util.Arrays.asList(\"a\", \"b\", \"c\"); } }",
1.303 - Collections.singletonMap("java.util.Arrays.asList($params$)", Arrays.asList("java.util.Arrays.asList(\"a\", \"b\", \"c\")")),
1.304 - Collections.<String>emptyList());
1.305 - }
1.306 -
1.307 - public void testMultiParameter2() throws Exception {
1.308 - performTest("package test; public class Test { { java.util.Arrays.asList(); } }",
1.309 - Collections.singletonMap("java.util.Arrays.asList($params$)", Arrays.asList("java.util.Arrays.asList()")),
1.310 - Collections.<String>emptyList());
1.311 - }
1.312 -
1.313 - public void testTypeParameter() throws Exception {
1.314 - performTest("package test; public class Test { { java.util.Arrays.<String>asList(); } }",
1.315 - Collections.singletonMap("java.util.Arrays.<$1>asList($params$)", Arrays.asList("java.util.Arrays.<String>asList()")),
1.316 - Collections.<String>emptyList());
1.317 - }
1.318 -
1.319 - public void testField1() throws Exception {
1.320 - String code = "package test;\n" +
1.321 - "public class Test {\n" +
1.322 - " String name = null;\n" +
1.323 - "}\n";
1.324 -
1.325 - performTest(code,
1.326 - Collections.singletonMap("$modifiers$ java.lang.String $name = $initializer;", Arrays.asList("String name = null;")),
1.327 - Collections.<String>emptyList());
1.328 - }
1.329 -
1.330 - public void testField2() throws Exception {
1.331 - String code = "package test;\n" +
1.332 - "public class Test {\n" +
1.333 - " private String name = null;\n" +
1.334 - "}\n";
1.335 -
1.336 - performTest(code,
1.337 - Collections.singletonMap("$modifiers$ java.lang.String $name = $initializer;", Arrays.asList("private String name = null;")),
1.338 - Collections.<String>emptyList());
1.339 - }
1.340 -
1.341 - public void testMemberSelectWithVariable() throws Exception {
1.342 - String code = "package test;\n" +
1.343 - "import java.util.Arrays;\n" +
1.344 - "public class Test {" +
1.345 - " {\n" +
1.346 - " foo.bar(0, 3, 4);\n" +
1.347 - " }\n" +
1.348 - "}\n";
1.349 -
1.350 - performTest(code,
1.351 - Collections.singletonMap("$foo.$bar($p1, $p2$)", Arrays.asList("foo.bar(0, 3, 4)")),
1.352 - Collections.<String>emptyList());
1.353 - }
1.354 -
1.355 - public void testCheckIdentifiers1() throws Exception {
1.356 - String code = "package test;\n" +
1.357 - "import static java.util.Arrays.*;\n" +
1.358 - "public class Test {" +
1.359 - " {\n" +
1.360 - " toString(new int[] {0, 3, 4});\n" +
1.361 - " }\n" +
1.362 - "}\n";
1.363 -
1.364 - performTest(code,
1.365 - Collections.singletonMap("java.util.Arrays.toString($x)", Arrays.asList("toString(new int[] {0, 3, 4})")),
1.366 - Collections.<String>emptyList());
1.367 - }
1.368 -
1.369 - public void testCheckIdentifiers2() throws Exception {
1.370 - String code = "package test;\n" +
1.371 - "public class Test {" +
1.372 - " {\n" +
1.373 - " toString(new int[] {0, 3, 4});\n" +
1.374 - " }\n" +
1.375 - "}\n";
1.376 -
1.377 - performTest(code,
1.378 - Collections.<String, List<String>>emptyMap(),
1.379 - Collections.singletonList("java.util.Arrays.toString($x)"));
1.380 - }
1.381 -
1.382 - public void testCheckIdentifiers3() throws Exception {
1.383 - String code = "package test;\n" +
1.384 - "import static java.util.Arrays.*;\n" +
1.385 - "public class Test {" +
1.386 - " {\n" +
1.387 - " Foo.toString(new int[] {0, 3, 4});\n" +
1.388 - " }\n" +
1.389 - "}\n";
1.390 -
1.391 - performTest(code,
1.392 - Collections.<String, List<String>>emptyMap(),
1.393 - Collections.singletonList("java.util.Arrays.toString($x)"));
1.394 - }
1.395 -
1.396 - public void testCheckIdentifiers4() throws Exception {
1.397 - String code = "package test;\n" +
1.398 - "public class Test {" +
1.399 - " {\n" +
1.400 - " java.util.Arrays.toString(new int[] {0, 3, 4});\n" +
1.401 - " }\n" +
1.402 - "}\n";
1.403 -
1.404 - performTest(code,
1.405 - Collections.singletonMap("Arrays", Arrays.asList("java.util.Arrays")), //could be imported in the input pattern
1.406 - Collections.<String>emptyList());
1.407 - }
1.408 -
1.409 - public void testLambdaInput() throws Exception {
1.410 - String code = "package test; public class Test {public void test() { new java.io.FilenameFilter() { public boolean accept(java.io.File dir, String name) { return true; } }; } }";
1.411 -
1.412 - performTest(code,
1.413 - Collections.singletonMap("new $type() {public $retType $name($params$) { $body$; } }", Arrays.asList("new java.io.FilenameFilter() { public boolean accept(java.io.File dir, String name) { return true; } }")),
1.414 -
1.415 - Collections.<String>emptyList());
1.416 - }
1.417 -
1.418 - public void testDoubleCheckedLockingWithVariable() throws Exception {
1.419 - String dcl = "if (o == null) {\n" +
1.420 - " Object o1 = new Object();\n" +
1.421 - " synchronized (Test.class) {\n" +
1.422 - " if (o == null) {\n" +
1.423 - " o = o1;\n" +
1.424 - " }\n" +
1.425 - " }\n" +
1.426 - " }";
1.427 - String code = "package test;\n" +
1.428 - "public class Test {\n" +
1.429 - " private Object o;\n" +
1.430 - " private void t() {\n" +
1.431 - " " + dcl + "\n" +
1.432 - " }\n" +
1.433 - "}\n";
1.434 -
1.435 - performTest(code,
1.436 - Collections.singletonMap("if ($var == null) {$pref$; synchronized ($lock) { if ($var == null) { $init$; } } }", Arrays.asList(dcl)),
1.437 - Collections.<String>emptyList());
1.438 - }
1.439 -
1.440 - public void testMethodName1() throws Exception {
1.441 - String code = "package test; public class Test {public void test() { clone(); } }";
1.442 -
1.443 - performTest(code,
1.444 - Collections.<String, List<String>>emptyMap(),
1.445 - Collections.<String>singletonList("public void clone() {$stmts$;}"));
1.446 - }
1.447 -
1.448 - public void testMethodName2() throws Exception {
1.449 - String code = "package test; public class Test {public void test() { clone(); } }";
1.450 -
1.451 - performTest(code,
1.452 - Collections.singletonMap("public void test() {$stmts$;}", Arrays.asList("public void test() { clone(); }")),
1.453 - Collections.<String>emptyList());
1.454 - }
1.455 -
1.456 - public void testBooleanLiterals() throws Exception {
1.457 - String code = "package test; public class Test { public void test() { if (false) { System.err.println(\"false\"); } if (true) { System.err.println(\"true\"); } } }";
1.458 -
1.459 - performTest(code,
1.460 - Collections.singletonMap("if (true) $then; else $else$;", Arrays.asList("if (true) { System.err.println(\"true\"); }")),
1.461 - Collections.<String>emptyList());
1.462 - }
1.463 -
1.464 - public void testEfficientMultiMatching() throws Exception {
1.465 - String code = "package test; public class Test { private void m() {} }";
1.466 -
1.467 - performTest(code,
1.468 - Collections.<String, List<String>>emptyMap(),
1.469 - Collections.singletonList("$mods$ class $name implements $i$ { }"));
1.470 - }
1.471 -
1.472 - private long measure(String baseCode, String toInsert, int repetitions, String pattern) throws Exception {
1.473 - int pos = baseCode.indexOf('|');
1.474 -
1.475 - assertTrue(pos != (-1));
1.476 -
1.477 - baseCode = baseCode.replaceAll(Pattern.quote("|"), "");
1.478 -
1.479 - StringBuilder code = new StringBuilder(baseCode.length() + repetitions * toInsert.length());
1.480 -
1.481 - code.append(baseCode);
1.482 -
1.483 - while (repetitions-- > 0) {
1.484 - code.insert(pos, toInsert);
1.485 - }
1.486 -
1.487 - long startTime = System.currentTimeMillis();
1.488 -
1.489 - performTest(code.toString(),
1.490 - Collections.<String, List<String>>emptyMap(),
1.491 - Arrays.asList(pattern));
1.492 -
1.493 - long endTime = System.currentTimeMillis();
1.494 -
1.495 - return endTime - startTime;
1.496 - }
1.497 -
1.498 - public void XtestMeasureTime() throws Exception {
1.499 - String code = TestUtilities.copyFileToString(new File("/usr/local/home/lahvac/src/nb//outgoing/java.editor/src/org/netbeans/modules/editor/java/JavaCompletionProvider.java"));
1.500 - List<String> patterns = new LinkedList<String>();
1.501 -
1.502 - for (int cntr = 0; cntr < 1000; cntr++) {
1.503 - patterns.add("System.err.println($1)");
1.504 - }
1.505 -
1.506 - performTest(code,
1.507 - Collections.<String, List<String>>emptyMap(),
1.508 - patterns);
1.509 - }
1.510 -
1.511 - public void testMatches1() throws Exception {
1.512 - performMatchesTest("package test; public class Test { private void test() { f.isDirectory(); } }", Arrays.asList("$1.isDirectory()", "new ImageIcon($1)"), true);
1.513 - }
1.514 -
1.515 - public void testSerialization() throws Exception {
1.516 - String text = "package test; public class Test { public void test1(boolean b) { int q; if (b) q = 2; else q = 3; } }";
1.517 -
1.518 - prepareTest("test/Test.java", text);
1.519 -
1.520 - ByteArrayOutputStream out = new ByteArrayOutputStream();
1.521 - EncodingContext ec = new EncodingContext(out, false);
1.522 -
1.523 - createSearch().encode(info.getCompilationUnit(), ec, new AtomicBoolean());
1.524 -
1.525 - boolean matches = createSearch().matches(new ByteArrayInputStream(out.toByteArray()), new AtomicBoolean(), createSearch().create(info, new AtomicBoolean(), "{ $p$; $T $v; if($a) $v = $b; else $v = $c; $q$; }"));
1.526 -
1.527 - assertTrue(matches);
1.528 - }
1.529 -
1.530 - public void testFrequencies() throws Exception {
1.531 - String text = "package test; public class Test { public void test1(boolean b) { java.io.File f = null; f.isDirectory(); f.isDirectory(); new javax.swing.ImageIcon(null); } }";
1.532 -
1.533 - prepareTest("test/Test.java", text);
1.534 -
1.535 - ByteArrayOutputStream out = new ByteArrayOutputStream();
1.536 - EncodingContext ec = new EncodingContext(out, false);
1.537 -
1.538 - createSearch().encode(info.getCompilationUnit(), ec, new AtomicBoolean());
1.539 -
1.540 - Map<String, Integer> actual = createSearch().matchesWithFrequencies(new ByteArrayInputStream(out.toByteArray()), createSearch().create(info, new AtomicBoolean(), "$1.isDirectory()", "new ImageIcon($1)"), new AtomicBoolean());
1.541 - Map<String, Integer> golden = new HashMap<String, Integer>();
1.542 -
1.543 - golden.put("$1.isDirectory()", 2);
1.544 - golden.put("new ImageIcon($1)", 1);
1.545 -
1.546 - assertEquals(golden, actual);
1.547 - }
1.548 -
1.549 - public void testPatternEncodingAndIdentifiers() throws Exception {
1.550 - String text = "package test; public class Test { }";
1.551 -
1.552 - prepareTest("test/Test.java", text);
1.553 -
1.554 - BulkPattern bp = createSearch().create(info, new AtomicBoolean(), "$0.isDirectory()");
1.555 -
1.556 - assertEquals(Arrays.asList(new HashSet<String>(Arrays.asList("isDirectory"))), bp.getIdentifiers());
1.557 - //TODO: the actual code for kinds differs for NFABased search and REBased search:
1.558 -// assertEquals(Arrays.asList(new HashSet<String>(Arrays.asList(Kind.METHOD_INVOCATION.name()))), bp.getKinds());
1.559 - }
1.560 -
1.561 - public void testModifiersMultiVariable() throws Exception {
1.562 - String code = "package test;\n" +
1.563 - "public class Test {\n" +
1.564 - " @Deprecated @Override public Test test;\n" +
1.565 - "}\n";
1.566 -
1.567 - performTest(code,
1.568 - Collections.singletonMap("$mods$ @Deprecated public $type $name = $init$;", Arrays.asList("@Deprecated @Override public Test test;")),
1.569 - Collections.<String>emptyList());
1.570 - }
1.571 -
1.572 - protected abstract BulkSearch createSearch();
1.573 -
1.574 - private void performMatchesTest(String text, List<String> patterns, boolean golden) throws Exception {
1.575 - prepareTest("test/Test.java", text);
1.576 -
1.577 - BulkPattern p = createSearch().create(info, new AtomicBoolean(), patterns);
1.578 -
1.579 - boolean result = createSearch().matches(info, new AtomicBoolean(), new TreePath(info.getCompilationUnit()), p);
1.580 -
1.581 - assertEquals(golden, result);
1.582 - }
1.583 -
1.584 - private void performTest(String text, Map<String, List<String>> containedPatterns, Collection<String> notContainedPatterns) throws Exception {
1.585 - prepareTest("test/Test.java", text);
1.586 -
1.587 - List<String> patterns = new LinkedList<String>();
1.588 -
1.589 - patterns.addAll(containedPatterns.keySet());
1.590 - patterns.addAll(notContainedPatterns);
1.591 -
1.592 - long s1 = System.currentTimeMillis();
1.593 - BulkPattern p = createSearch().create(info, new AtomicBoolean(), patterns);
1.594 - long e1 = System.currentTimeMillis();
1.595 -
1.596 -// System.err.println("create: " + (e1 - s1));
1.597 -
1.598 - long s2 = System.currentTimeMillis();
1.599 - Map<String, Collection<TreePath>> result = createSearch().match(info, new AtomicBoolean(), new TreePath(info.getCompilationUnit()), p);
1.600 - long e2 = System.currentTimeMillis();
1.601 -
1.602 -// System.err.println("match: " + (e2 - s2));
1.603 -
1.604 - assertTrue(result.toString(), result.keySet().containsAll(containedPatterns.keySet()));
1.605 -
1.606 - for (Entry<String, Collection<TreePath>> e : result.entrySet()) {
1.607 - List<String> actual = new LinkedList<String>();
1.608 -
1.609 - for (TreePath tp : e.getValue()) {
1.610 - assertNotNull(TreePathHandle.create(tp, info).resolve(info));
1.611 -
1.612 - int start = (int) info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), tp.getLeaf());
1.613 - int end = (int) info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), tp.getLeaf());
1.614 -
1.615 - actual.add(info.getText().substring(start, end));
1.616 - }
1.617 -
1.618 - assertEquals(e.getKey(), containedPatterns.get(e.getKey()), actual);
1.619 - }
1.620 -
1.621 -
1.622 - Set<String> none = new HashSet<String>(result.keySet());
1.623 -
1.624 - none.retainAll(notContainedPatterns);
1.625 -
1.626 - assertTrue(none.isEmpty());
1.627 -
1.628 - if (!verifyIndexingData())
1.629 - return ;
1.630 -
1.631 - //ensure the returned identifiers/treeKinds are correct:
1.632 - ByteArrayOutputStream data = new ByteArrayOutputStream();
1.633 - EncodingContext ec = new EncodingContext(data, false);
1.634 -
1.635 - createSearch().encode(info.getCompilationUnit(), ec, new AtomicBoolean());
1.636 -
1.637 - for (int i = 0; i < containedPatterns.size(); i++) {
1.638 - assertTrue("expected: " + p.getIdentifiers().get(i) + ", but exist only: " + ec.getIdentifiers(), ec.getIdentifiers().containsAll(p.getIdentifiers().get(i)));
1.639 -
1.640 - for (List<String> phrase : p.getRequiredContent().get(i)) {
1.641 - assertTrue("expected: " + phrase + ", but exist only: " + ec.getContent() + "(all phrases: " + p.getRequiredContent().get(i) + ")", Collections.indexOfSubList(ec.getContent(), phrase) != (-1));
1.642 - }
1.643 - }
1.644 -
1.645 - data.close();
1.646 - assertEquals(!containedPatterns.isEmpty(), createSearch().matches(new ByteArrayInputStream(data.toByteArray()), new AtomicBoolean(), p));
1.647 - }
1.648 -
1.649 - private void prepareTest(String fileName, String code) throws Exception {
1.650 - clearWorkDir();
1.651 -
1.652 - FileUtil.refreshFor(File.listRoots());
1.653 -
1.654 - FileObject workFO = FileUtil.toFileObject(getWorkDir());
1.655 -
1.656 - assertNotNull(workFO);
1.657 -
1.658 - workFO.refresh();
1.659 -
1.660 - sourceRoot = workFO.createFolder("src");
1.661 - FileObject buildRoot = workFO.createFolder("build");
1.662 - FileObject cache = workFO.createFolder("cache");
1.663 -
1.664 - FileObject data = FileUtil.createData(sourceRoot, fileName);
1.665 - File dataFile = FileUtil.toFile(data);
1.666 -
1.667 - assertNotNull(dataFile);
1.668 -
1.669 - TestUtilities.copyStringToFile(dataFile, code);
1.670 -
1.671 - SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, cache);
1.672 -
1.673 - DataObject od = DataObject.find(data);
1.674 - EditorCookie ec = od.getLookup().lookup(EditorCookie.class);
1.675 -
1.676 - assertNotNull(ec);
1.677 -
1.678 - doc = ec.openDocument();
1.679 - doc.putProperty(Language.class, JavaTokenId.language());
1.680 -
1.681 - JavaSource js = JavaSource.forFileObject(data);
1.682 -
1.683 - assertNotNull(js);
1.684 -
1.685 - info = SourceUtilsTestUtil.getCompilationInfo(js, Phase.RESOLVED);
1.686 -
1.687 - assertNotNull(info);
1.688 - }
1.689 -
1.690 - private FileObject sourceRoot;
1.691 - private CompilationInfo info;
1.692 - private Document doc;
1.693 -
1.694 - protected boolean verifyIndexingData() {
1.695 - return true;
1.696 - }
1.697 -}