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