1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/ant/test/org/apidesign/infra/ant/GrepTest.java Fri Apr 03 16:32:36 2020 +0200
1.3 @@ -0,0 +1,546 @@
1.4 +/*
1.5 + * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Sun designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Sun in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
1.26 + * have any questions.
1.27 + */
1.28 +package org.apidesign.infra.ant;
1.29 +
1.30 +import java.io.ByteArrayInputStream;
1.31 +import java.io.File;
1.32 +import java.io.FileOutputStream;
1.33 +import java.io.FileWriter;
1.34 +import java.io.InputStream;
1.35 +import java.net.URL;
1.36 +import java.util.ArrayList;
1.37 +import java.util.Arrays;
1.38 +import java.util.List;
1.39 +import junit.framework.Test;
1.40 +import org.netbeans.junit.NbTestCase;
1.41 +import org.netbeans.junit.NbTestSuite;
1.42 +
1.43 +/**
1.44 + *
1.45 + * @author Jaroslav Tulach
1.46 + */
1.47 +public class GrepTest extends NbTestCase {
1.48 + private static File workDir;
1.49 +
1.50 + public GrepTest(String s) {
1.51 + super(s);
1.52 + }
1.53 +
1.54 + public static Test suite() {
1.55 + return new NbTestSuite(GrepTest.class);
1.56 + //return new GrepTest("testInXML");
1.57 + }
1.58 +
1.59 + @Override
1.60 + protected void setUp() throws Exception {
1.61 + clearWorkDir();
1.62 + }
1.63 +
1.64 + public void testMissingMethodInAnInterfaceIsDetected() throws Exception {
1.65 + String c1 =
1.66 + "package ahoj;\n" +
1.67 + "// BEGIN: xyz\n" +
1.68 + "public interface I {\n" +
1.69 + "// FINISH: xyz\n" +
1.70 + " public void get();\n" +
1.71 + "}" +
1.72 + "";
1.73 + File src = createFile(1, "I.java", c1);
1.74 +
1.75 +
1.76 + String c2 =
1.77 + "@xyz@";
1.78 + File txt = createFile(2, "in.txt", c2);
1.79 +
1.80 + File out = createFile(3, "out.txt", "");
1.81 + out.delete();
1.82 +
1.83 + execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
1.84 +
1.85 + assertTrue("output created", out.canRead());
1.86 +
1.87 + String r = readFile(out);
1.88 + assertEquals("public interface I {\n}\n", r);
1.89 + }
1.90 +
1.91 + public void testSpacesAtBeginingAreStripped() throws Exception {
1.92 + String c1 =
1.93 + "package ahoj;\n" +
1.94 + "// BEGIN: xyz\n" +
1.95 + " public interface I {\n" +
1.96 + " public void ahoj();\n" +
1.97 + " }\n" +
1.98 + "// END: xyz\n" +
1.99 + " public void get();\n" +
1.100 + "}" +
1.101 + "";
1.102 + File src = createFile(1, "I.java", c1);
1.103 +
1.104 +
1.105 + String c2 =
1.106 + "@xyz@";
1.107 + File txt = createFile(2, "in.txt", c2);
1.108 +
1.109 + File out = createFile(3, "out.txt", "");
1.110 + out.delete();
1.111 +
1.112 + execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
1.113 +
1.114 + assertTrue("output created", out.canRead());
1.115 +
1.116 + String r = readFile(out);
1.117 + String result = "public interface I {\n" +
1.118 + " public void ahoj();\n" +
1.119 + "}\n";
1.120 + assertEquals(result, r);
1.121 + }
1.122 +
1.123 + public void testReportUnpairedBracesAsError() throws Exception {
1.124 + String c1 =
1.125 + "package ahoj;\n" +
1.126 + "// BEGIN: xyz\n" +
1.127 + " public interface I {\n" +
1.128 + " public void ahoj();\n" +
1.129 + "// END: xyz\n" +
1.130 + " }\n" +
1.131 + " public void get();\n" +
1.132 + "}" +
1.133 + "";
1.134 + File src = createFile(1, "I.java", c1);
1.135 +
1.136 +
1.137 + String c2 =
1.138 + "@xyz@";
1.139 + File txt = createFile(2, "in.txt", c2);
1.140 +
1.141 + File out = createFile(3, "out.txt", "");
1.142 + out.delete();
1.143 +
1.144 + try {
1.145 + execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
1.146 + } catch (ExecuteUtils.ExecutionError ex) {
1.147 + // ok
1.148 + return;
1.149 + }
1.150 + fail("The execution of the script shall fail");
1.151 + }
1.152 +
1.153 + public void testIncludedTexts() throws Exception {
1.154 + String c1 =
1.155 + "package ahoj;\n" +
1.156 + "// BEGIN: clazz\n" +
1.157 + "public interface I {\n" +
1.158 + " // BEGIN: method\n" +
1.159 + " public void get();\n" +
1.160 + " // END: method\n" +
1.161 + "}\n" +
1.162 + "// END: clazz\n" +
1.163 + "";
1.164 + File src = createFile(1, "I.java", c1);
1.165 +
1.166 +
1.167 + String c2 =
1.168 + "@clazz@";
1.169 + File txt = createFile(2, "in.txt", c2);
1.170 +
1.171 + File out = createFile(3, "out.txt", "");
1.172 + out.delete();
1.173 +
1.174 + execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
1.175 +
1.176 + assertTrue("output created", out.canRead());
1.177 +
1.178 + String r = readFile(out);
1.179 + if (r.indexOf("BEGIN") >= 0) {
1.180 + fail("BEGIN is there: " + r);
1.181 + }
1.182 + if (r.indexOf("END") >= 0) {
1.183 + fail("END is there: " + r);
1.184 + }
1.185 + if (r.indexOf("interface I") < 0) {
1.186 + fail("Missing interface: " + r);
1.187 + }
1.188 + if (r.indexOf("void get()") < 0) {
1.189 + fail("Missing get: " + r);
1.190 + }
1.191 + }
1.192 + public void testIncludedTextsAmper() throws Exception {
1.193 + String c1 =
1.194 + "package ahoj;\n" +
1.195 + "public class C {\n" +
1.196 + " // BEGIN: method\n" +
1.197 + " public void change(int x) { x &= 10; }\n" +
1.198 + " // END: method\n" +
1.199 + "}\n" +
1.200 + "";
1.201 + File src = createFile(1, "C.java", c1);
1.202 +
1.203 +
1.204 + String c2 =
1.205 + "@method@";
1.206 + File txt = createFile(2, "in.txt", c2);
1.207 +
1.208 + File out = createFile(3, "out.txt", "");
1.209 + out.delete();
1.210 +
1.211 + execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
1.212 +
1.213 + assertTrue("output created", out.canRead());
1.214 +
1.215 + String r = readFile(out);
1.216 + if (r.indexOf("&=") < 0) {
1.217 + fail("No XML: " + r);
1.218 + }
1.219 + if (r.indexOf("&=") >= 0) {
1.220 + fail("No XML, we need &: " + r);
1.221 + }
1.222 + }
1.223 + public void testIncludedTextsAmperAndGenerics() throws Exception {
1.224 + String c1 =
1.225 +
1.226 +"package org.apidesign.api.security;\n" +
1.227 +"" +
1.228 +"import java.nio.ByteBuffer;\n" +
1.229 +"import java.util.ServiceLoader;\n" +
1.230 +"import org.apidesign.spi.security.Digestor;\n" +
1.231 +"\n" +
1.232 +"/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
1.233 +" * for buffer of data.\n" +
1.234 +" *\n" +
1.235 +" * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
1.236 +" */\n" +
1.237 +"// BEGIN: day.end.bridges.Digest\n" +
1.238 +"public final class Digest {\n" +
1.239 +" private final DigestImplementation<?> impl;\n" +
1.240 +" \n" +
1.241 +" /** Factory method is better than constructor */\n" +
1.242 +" private Digest(DigestImplementation<?> impl) {\n" +
1.243 +" this.impl = impl;\n" +
1.244 +" }\n" +
1.245 +" \n" +
1.246 +" /** Factory method to create digest for an algorithm.\n" +
1.247 +" */\n" +
1.248 +" public static Digest getInstance(String algorithm) {\n" +
1.249 +" for (Digestor<?> digestor : ServiceLoader.load(Digestor.class)) {\n" +
1.250 +" DigestImplementation<?> impl = \n" +
1.251 + "DigestImplementation.create(digestor, algorithm);\n" +
1.252 +" if (impl != null) {\n" +
1.253 +" return new Digest(impl);\n" +
1.254 +" }\n" +
1.255 +" }\n" +
1.256 +" throw new IllegalArgumentException(algorithm);\n" +
1.257 +" }\n" +
1.258 +" \n" +
1.259 +" //\n" +
1.260 +" // these methods are kept the same as in original MessageDigest,\n" +
1.261 +" // but for simplicity choose just some from the original API\n" +
1.262 +" //\n" +
1.263 +" \n" +
1.264 +" public byte[] digest(ByteBuffer bb) {\n" +
1.265 +" return impl.digest(bb);\n" +
1.266 +" }\n" +
1.267 +"}\n" +
1.268 +"// END: day.end.bridges.Digest\n";
1.269 +
1.270 + File src = createFile(1, "C.java", c1);
1.271 +
1.272 +
1.273 + String c2 =
1.274 + "@day.end.bridges.Digest@";
1.275 + File txt = createFile(2, "in.txt", c2);
1.276 +
1.277 + File out = createFile(3, "out.txt", "");
1.278 + out.delete();
1.279 +
1.280 + execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
1.281 +
1.282 + assertTrue("output created", out.canRead());
1.283 +
1.284 + String r = readFile(out);
1.285 + if (r.indexOf("&=") >= 0) {
1.286 + fail("Wrong XML: " + r);
1.287 + }
1.288 + if (r.indexOf("&") > -1) {
1.289 + fail("Wrong XML, no &: " + r);
1.290 + }
1.291 + }
1.292 +
1.293 + public void testInXML() throws Exception {
1.294 + String c1 =
1.295 + "<!-- BEGIN: clazz -->\n" +
1.296 + "<interface name='I'/>\n" +
1.297 + "<!-- END: clazz -->\n" +
1.298 + "";
1.299 + File src = createFile(1, "I.xml", c1);
1.300 +
1.301 +
1.302 + String c2 =
1.303 + "@clazz@";
1.304 + File txt = createFile(2, "in.txt", c2);
1.305 +
1.306 + File out = createFile(3, "out.txt", "");
1.307 + out.delete();
1.308 +
1.309 + execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.xml");
1.310 +
1.311 + assertTrue("output created", out.canRead());
1.312 +
1.313 + String r = readFile(out);
1.314 + if (r.indexOf("BEGIN") >= 0) {
1.315 + fail("BEGIN is there: " + r);
1.316 + }
1.317 + if (r.indexOf("END") >= 0) {
1.318 + fail("END is there: " + r);
1.319 + }
1.320 + if (r.indexOf("<interface name='I'/>") < 0) {
1.321 + fail("Missing interface: " + r);
1.322 + }
1.323 + }
1.324 +
1.325 + public void testLongLineNotDetectedAsBeginsWithGen() throws Exception {
1.326 + String c1 =
1.327 +"package org.apidesign.api.security;\n" +
1.328 +"" +
1.329 +"import java.nio.ByteBuffer;\n" +
1.330 +"import java.util.ServiceLoader;\n" +
1.331 +"import org.apidesign.spi.security.Digestor;\n" +
1.332 +"\n" +
1.333 +"/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
1.334 +"// BEGIN: x\n" +
1.335 +" * for buffer of data.\n" +
1.336 +"// END: x\n" +
1.337 +" *\n" +
1.338 +" * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
1.339 +" */\n" +
1.340 +"// GEN-BEGIN: day.end.bridges.Digest\n" +
1.341 +"d; DigestImplementation<?> impl = DigestImplementation.create(digestor, algorithm);\n" +
1.342 +"// GEN-END: day.end.bridges.Digest\n";
1.343 +
1.344 + File src = createFile(1, "C.java", c1);
1.345 +
1.346 +
1.347 + String c2 =
1.348 + "@day.end.bridges.Digest@";
1.349 + File txt = createFile(2, "in.txt", c2);
1.350 +
1.351 + File out = createFile(3, "out.txt", "");
1.352 + out.delete();
1.353 +
1.354 + execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
1.355 + }
1.356 +
1.357 +
1.358 + public void testLongLineDetected() throws Exception {
1.359 + String c1 =
1.360 +"package org.apidesign.api.security;\n" +
1.361 +"" +
1.362 +"import java.nio.ByteBuffer;\n" +
1.363 +"import java.util.ServiceLoader;\n" +
1.364 +"import org.apidesign.spi.security.Digestor;\n" +
1.365 +"\n" +
1.366 +"/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
1.367 +" * for buffer of data.\n" +
1.368 +" *\n" +
1.369 +" * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
1.370 +" */\n" +
1.371 +"// BEGIN: day.end.bridges.Digest\n" +
1.372 +"d; DigestImplementation<?> impl = DigestImplementation.create(digestor, algorithm);\n" +
1.373 +"// END: day.end.bridges.Digest\n";
1.374 +
1.375 + File src = createFile(1, "C.java", c1);
1.376 +
1.377 +
1.378 + String c2 =
1.379 + "@day.end.bridges.Digest@";
1.380 + File txt = createFile(2, "in.txt", c2);
1.381 +
1.382 + File out = createFile(3, "out.txt", "");
1.383 + out.delete();
1.384 +
1.385 + try {
1.386 + execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
1.387 + } catch (ExecuteUtils.ExecutionError ex) {
1.388 + // OK
1.389 + return;
1.390 + }
1.391 + fail("Should fail, as there is long line");
1.392 + }
1.393 + public void testLongNotLineDetectedAsShortened() throws Exception {
1.394 + String c1 =
1.395 +"package org.apidesign.api.security;\n" +
1.396 +"" +
1.397 +"import java.nio.ByteBuffer;\n" +
1.398 +"import java.util.ServiceLoader;\n" +
1.399 +"import org.apidesign.spi.security.Digestor;\n" +
1.400 +"\n" +
1.401 +"/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
1.402 +" * for buffer of data.\n" +
1.403 +" *\n" +
1.404 +" * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
1.405 +" */\n" +
1.406 +"// BEGIN: day.end.bridges.Digest\n" +
1.407 +" DigestImplementation<?> impl = DigestImplementation.create(digestor, algorithm);\n" +
1.408 +"// END: day.end.bridges.Digest\n";
1.409 +
1.410 + File src = createFile(1, "C.java", c1);
1.411 +
1.412 +
1.413 + String c2 =
1.414 + "@day.end.bridges.Digest@";
1.415 + File txt = createFile(2, "in.txt", c2);
1.416 +
1.417 + File out = createFile(3, "out.txt", "");
1.418 + out.delete();
1.419 +
1.420 + execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
1.421 + }
1.422 +
1.423 + public void testNotClosedSection() throws Exception {
1.424 + String c1 =
1.425 + "package ahoj;\n" +
1.426 + "// BEGIN: clazz\n" +
1.427 + "int x;\n" +
1.428 + "\n";
1.429 + File src = createFile(1, "I.java", c1);
1.430 +
1.431 +
1.432 + String c2 =
1.433 + "@clazz@";
1.434 + File txt = createFile(2, "in.txt", c2);
1.435 +
1.436 + File out = createFile(3, "out.txt", "");
1.437 + out.delete();
1.438 +
1.439 + try {
1.440 + execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
1.441 + fail("Has to fail");
1.442 + } catch (ExecuteUtils.ExecutionError ex) {
1.443 + // ok
1.444 + }
1.445 + }
1.446 + public void testLongNotLineDetectedAsNotInTheList() throws Exception {
1.447 + String c1 =
1.448 +"package org.apidesign.api.security;\n" +
1.449 +"" +
1.450 +"import java.nio.ByteBuffer;\n" +
1.451 +"import java.util.ServiceLoader;\n" +
1.452 +"import org.apidesign.spi.security.Digestor;\n" +
1.453 +"\n" +
1.454 +"/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
1.455 +" * for buffer of data.\n" +
1.456 +" *\n" +
1.457 +" * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
1.458 +" */\n" +
1.459 +" DigestImplementation<?> impl = DigestImplementation.create(digestor, algorithm);\n" +
1.460 +"// BEGIN: day.end.bridges.Digest\n" +
1.461 +" DigestImplementation<?> impl = null\n" +
1.462 +"// END: day.end.bridges.Digest\n";
1.463 +
1.464 + File src = createFile(1, "C.java", c1);
1.465 +
1.466 +
1.467 + String c2 =
1.468 + "@day.end.bridges.Digest@";
1.469 + File txt = createFile(2, "in.txt", c2);
1.470 +
1.471 + File out = createFile(3, "out.txt", "");
1.472 + out.delete();
1.473 +
1.474 + execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
1.475 + }
1.476 +
1.477 + protected final File createFile(int slot, String name, String content) throws Exception {
1.478 + File d1 = new File(getWorkDir(), "dir" + slot);
1.479 + File c1 = new File(d1, name);
1.480 + copy(content, c1);
1.481 + return c1;
1.482 + }
1.483 +
1.484 + protected final void execute(int slotFirst, int slotSecond, String... additionalArgs) throws Exception {
1.485 + File d1 = new File(getWorkDir(), "dir" + slotFirst);
1.486 + File d2 = new File(getWorkDir(), "dir" + slotSecond);
1.487 +
1.488 + File build = new File(getWorkDir(), "build.xml");
1.489 + extractResource("build.xml", build);
1.490 +
1.491 + List<String> args = new ArrayList<String>();
1.492 + args.addAll(Arrays.asList(additionalArgs));
1.493 + args.add("-Ddir1=" + d1);
1.494 + args.add("-Ddir2=" + d2);
1.495 + ExecuteUtils.execute(build, args.toArray(new String[0]));
1.496 + }
1.497 +
1.498 + private static final void copy(String txt, File f) throws Exception {
1.499 + f.getParentFile().mkdirs();
1.500 + FileWriter w = new FileWriter(f);
1.501 + w.append(txt);
1.502 + w.close();
1.503 + }
1.504 +
1.505 + final File extractResource(String res, File f) throws Exception {
1.506 + URL u = GrepTest.class.getResource(res);
1.507 + assertNotNull ("Resource should be found " + res, u);
1.508 +
1.509 + FileOutputStream os = new FileOutputStream(f);
1.510 + InputStream is = u.openStream();
1.511 + for (;;) {
1.512 + int ch = is.read ();
1.513 + if (ch == -1) {
1.514 + break;
1.515 + }
1.516 + os.write (ch);
1.517 + }
1.518 + os.close ();
1.519 +
1.520 + return f;
1.521 + }
1.522 +
1.523 + final static String readFile (java.io.File f) throws java.io.IOException {
1.524 + int s = (int)f.length ();
1.525 + byte[] data = new byte[s];
1.526 + assertEquals ("Read all data", s, new java.io.FileInputStream (f).read (data));
1.527 +
1.528 + return new String (data);
1.529 + }
1.530 +
1.531 + final File extractString (String res, String nameExt) throws Exception {
1.532 + File f = new File(getWorkDir(), nameExt);
1.533 + f.deleteOnExit ();
1.534 +
1.535 + FileOutputStream os = new FileOutputStream(f);
1.536 + InputStream is = new ByteArrayInputStream(res.getBytes("UTF-8"));
1.537 + for (;;) {
1.538 + int ch = is.read ();
1.539 + if (ch == -1) {
1.540 + break;
1.541 + }
1.542 + os.write (ch);
1.543 + }
1.544 + os.close ();
1.545 +
1.546 + return f;
1.547 + }
1.548 +
1.549 +}
1.550 \ No newline at end of file