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