java/ant/test/org/apidesign/infra/ant/ColorifyTest.java
changeset 416 9ed8788a1a4e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/ant/test/org/apidesign/infra/ant/ColorifyTest.java	Fri Apr 03 16:32:36 2020 +0200
     1.3 @@ -0,0 +1,680 @@
     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 ColorifyTest extends NbTestCase {
    1.48 +    private static File workDir;
    1.49 +    
    1.50 +    public ColorifyTest(String s) {
    1.51 +        super(s);
    1.52 +    }
    1.53 +    
    1.54 +    public static Test suite() {
    1.55 +        return new NbTestSuite(ColorifyTest.class);
    1.56 +//        return new ColorifyTest("testInXMLWithMultilineString");
    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 testTryFinally() throws Exception {
    1.92 +        String c1 =
    1.93 +            "package ahoj;\n" +
    1.94 +            "// BEGIN: xyz\n" +
    1.95 +            "  try {\n" +
    1.96 +            "    // something\n" +
    1.97 +            "  } finally {\n" +
    1.98 +            "    // else\n" +
    1.99 +            "  }\n" +
   1.100 +            "// END: xyz\n" +
   1.101 +            "  public void get();\n" +
   1.102 +            "}" +
   1.103 +            "";
   1.104 +        File src = createFile(1, "I.java", c1);
   1.105 +        
   1.106 +        
   1.107 +        String c2 =
   1.108 +            "@xyz@";
   1.109 +        File txt = createFile(2, "in.txt", c2);
   1.110 +        
   1.111 +        File out = createFile(3, "out.txt", "");
   1.112 +        out.delete();
   1.113 +        
   1.114 +        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
   1.115 +        
   1.116 +        assertTrue("output created", out.canRead());
   1.117 +        
   1.118 +        String r = readFile(out);
   1.119 +        String exp =
   1.120 +            "try {\n" +
   1.121 +            "  // something\n" +
   1.122 +            "} finally {\n" +
   1.123 +            "  // else\n" +
   1.124 +            "}\n";
   1.125 +        assertEquals(exp, r);
   1.126 +    }
   1.127 +    
   1.128 +    public void testString() throws Exception {
   1.129 +        String c1 =
   1.130 +            "package ahoj;\n" +
   1.131 +            "public interface I {\n" +
   1.132 +            "// BEGIN: xyz\n" +
   1.133 +            "  public static final String X = \"ahoj\"\n" +
   1.134 +            "// END: xyz\n" +
   1.135 +            "}" +
   1.136 +            "";
   1.137 +        File src = createFile(1, "I.java", c1);
   1.138 +        
   1.139 +        
   1.140 +        String c2 =
   1.141 +            "@xyz@";
   1.142 +        File txt = createFile(2, "in.txt", c2);
   1.143 +        
   1.144 +        File out = createFile(3, "out.txt", "");
   1.145 +        out.delete();
   1.146 +        
   1.147 +        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
   1.148 +        
   1.149 +        assertTrue("output created", out.canRead());
   1.150 +        
   1.151 +        String r = readFile(out);
   1.152 +        assertEquals("public static final String X = \"ahoj\"\n", r);
   1.153 +    }
   1.154 +    
   1.155 +    public void testSpacesAtBeginingAreStripped() throws Exception {
   1.156 +        String c1 =
   1.157 +            "package ahoj;\n" +
   1.158 +            "// BEGIN: xyz\n" +
   1.159 +            "   public interface I {\n" +
   1.160 +            "     public void ahoj ();\n" +
   1.161 +            "   }\n" +
   1.162 +            "// END: xyz\n" +
   1.163 +            "  public void get();\n" +
   1.164 +            "}" +
   1.165 +            "";
   1.166 +        File src = createFile(1, "I.java", c1);
   1.167 +        
   1.168 +        
   1.169 +        String c2 =
   1.170 +            "@xyz@";
   1.171 +        File txt = createFile(2, "in.txt", c2);
   1.172 +        
   1.173 +        File out = createFile(3, "out.txt", "");
   1.174 +        out.delete();
   1.175 +        
   1.176 +        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
   1.177 +        
   1.178 +        assertTrue("output created", out.canRead());
   1.179 +        
   1.180 +        String r = readFile(out);
   1.181 +        String result = "public interface I {\n" +
   1.182 +            "  public void ahoj ();\n" +
   1.183 +            "}\n";
   1.184 +        assertEquals(result, r);
   1.185 +    }
   1.186 +        
   1.187 +    public void testIncludedTexts() throws Exception {
   1.188 +        String c1 =
   1.189 +            "package ahoj;\n" +
   1.190 +            "// BEGIN: clazz\n" +
   1.191 +            "public interface I {\n" +
   1.192 +            "  // BEGIN: method\n" +
   1.193 +            "  public void get();\n" +
   1.194 +            "  // END: method\n" +
   1.195 +            "}\n" +
   1.196 +            "// END: clazz\n" +
   1.197 +            "";
   1.198 +        File src = createFile(1, "I.java", c1);
   1.199 +        
   1.200 +        
   1.201 +        String c2 =
   1.202 +            "@clazz@";
   1.203 +        File txt = createFile(2, "in.txt", c2);
   1.204 +        
   1.205 +        File out = createFile(3, "out.txt", "");
   1.206 +        out.delete();
   1.207 +        
   1.208 +        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
   1.209 +        
   1.210 +        assertTrue("output created", out.canRead());
   1.211 +        
   1.212 +        String r = readFile(out);
   1.213 +        if (r.indexOf("BEGIN") >= 0) {
   1.214 +            fail("BEGIN is there: " + r);
   1.215 +        }
   1.216 +        if (r.indexOf("END") >= 0) {
   1.217 +            fail("END is there: " + r);
   1.218 +        }
   1.219 +        if (r.indexOf("interface I") < 0) {
   1.220 +            fail("Missing interface: " + r);
   1.221 +        }
   1.222 +        if (r.indexOf("void get()") < 0) {
   1.223 +            fail("Missing get: " + r);
   1.224 +        }
   1.225 +    }
   1.226 +    public void testWithCommentsTexts() throws Exception {
   1.227 +        String c1 =
   1.228 +            "package ahoj;\n" +
   1.229 +            "// BEGIN: clazz\n" +
   1.230 +            "/** Beautiful interface I\n" +
   1.231 +            " */\n" +
   1.232 +            "public interface I {\n" +
   1.233 +            "  // single line\n" +
   1.234 +            "}\n" +
   1.235 +            "// END: clazz\n" +
   1.236 +            "";
   1.237 +        File src = createFile(1, "I.java", c1);
   1.238 +        
   1.239 +        
   1.240 +        String c2 =
   1.241 +            "@clazz@";
   1.242 +        File txt = createFile(2, "in.txt", c2);
   1.243 +        
   1.244 +        File out = createFile(3, "out.txt", "");
   1.245 +        out.delete();
   1.246 +        
   1.247 +        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
   1.248 +        
   1.249 +        assertTrue("output created", out.canRead());
   1.250 +        
   1.251 +        String r = readFile(out);
   1.252 +        if (r.indexOf("BEGIN") >= 0) {
   1.253 +            fail("BEGIN is there: " + r);
   1.254 +        }
   1.255 +        if (r.indexOf("END") >= 0) {
   1.256 +            fail("END is there: " + r);
   1.257 +        }
   1.258 +        if (r.indexOf("interface I") < 0) {
   1.259 +            fail("Missing interface: " + r);
   1.260 +        }
   1.261 +        if (r.indexOf("// single line\n}") < 0) {
   1.262 +            fail("Comment indentified: " + r);
   1.263 +        }
   1.264 +    }
   1.265 +    public void testMultiLineComment() throws Exception {
   1.266 +        String c1 =
   1.267 +            "package ahoj;\n" +
   1.268 +            "// BEGIN: clazz\n" +
   1.269 +            "public void x() {\n" +
   1.270 +            "  /** Beautiful interface I\n" +
   1.271 +            "  *\n" +
   1.272 +            "  */\n" +
   1.273 +            "}\n" +
   1.274 +            "// END: clazz\n" +
   1.275 +            "";
   1.276 +        File src = createFile(1, "I.java", c1);
   1.277 +        
   1.278 +        
   1.279 +        String c2 =
   1.280 +            "@clazz@";
   1.281 +        File txt = createFile(2, "in.txt", c2);
   1.282 +        
   1.283 +        File out = createFile(3, "out.txt", "");
   1.284 +        out.delete();
   1.285 +        
   1.286 +        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
   1.287 +        
   1.288 +        assertTrue("output created", out.canRead());
   1.289 +        
   1.290 +        String r = readFile(out);
   1.291 +        int f = r.indexOf(
   1.292 +            "/** Beautiful interface I\n" +
   1.293 +            "  *\n" +
   1.294 +            "  */\n");
   1.295 +        if (f == -1) {
   1.296 +            fail("Not found: " + r);
   1.297 +        }
   1.298 +    }
   1.299 +    public void testIncludedTextsAmper() throws Exception {
   1.300 +        String c1 =
   1.301 +            "package ahoj;\n" +
   1.302 +            "public class C {\n" +
   1.303 +            "  // BEGIN: method\n" +
   1.304 +            "  public void change(int x) { x &= 10; }\n" +
   1.305 +            "  // END: method\n" +
   1.306 +            "}\n" +
   1.307 +            "";
   1.308 +        File src = createFile(1, "C.java", c1);
   1.309 +        
   1.310 +        
   1.311 +        String c2 =
   1.312 +            "@method@";
   1.313 +        File txt = createFile(2, "in.txt", c2);
   1.314 +        
   1.315 +        File out = createFile(3, "out.txt", "");
   1.316 +        out.delete();
   1.317 +        
   1.318 +        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
   1.319 +        
   1.320 +        assertTrue("output created", out.canRead());
   1.321 +        
   1.322 +        String r = readFile(out);
   1.323 +        if (r.indexOf("&=") < 0) {
   1.324 +            fail("No XML escapes please: " + r);
   1.325 +        }
   1.326 +        if (r.indexOf("&amp;=") >= 0) {
   1.327 +            fail("we need no xml escapes, no &amp;: " + r);
   1.328 +        }
   1.329 +    }
   1.330 +    public void testIncludedTextsAmperAndGenerics() throws Exception {
   1.331 +        String c1 =
   1.332 +
   1.333 +"package org.apidesign.api.security;\n" +
   1.334 +"" +
   1.335 +"import java.nio.ByteBuffer;\n" +
   1.336 +"import java.util.ServiceLoader;\n" +
   1.337 +"import org.apidesign.spi.security.Digestor;\n" +
   1.338 +"\n" +
   1.339 +"/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
   1.340 +" * for buffer of data.\n" +
   1.341 +" *\n" +
   1.342 +" * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
   1.343 +" */\n" +
   1.344 +"// BEGIN: day.end.bridges.Digest\n" +
   1.345 +"public final class Digest {\n" +
   1.346 +"    private final DigestImplementation<?> impl;\n" +
   1.347 +"    \n" +
   1.348 +"    /** Factory method is better than constructor */\n" +
   1.349 +"    private Digest(DigestImplementation<?> impl) {\n" +
   1.350 +"        this.impl = impl;\n" +
   1.351 +"    }\n" +
   1.352 +"    \n" +
   1.353 +"    /** Factory method to create digest for an algorithm.\n" +
   1.354 +"     */\n" +
   1.355 +"    public static Digest getInstance(String algorithm) {\n" +
   1.356 +"        for (Digestor<?> digestor : ServiceLoader.load(Digestor.class)) {\n" +
   1.357 +"            DigestImplementation<?> impl = \n" +
   1.358 +                "DigestImplementation.create(digestor, algorithm);\n" +
   1.359 +"            if (impl != null) {\n" +
   1.360 +"                return new Digest(impl);\n" +
   1.361 +"            }\n" +
   1.362 +"        }\n" +
   1.363 +"        throw new IllegalArgumentException(algorithm);\n" +
   1.364 +"    }\n" +
   1.365 +"      \n" +
   1.366 +"    //\n" +
   1.367 +"    // these methods are kept the same as in original MessageDigest,\n" +
   1.368 +"    // but for simplicity choose just some from the original API\n" +
   1.369 +"    //\n" +
   1.370 +"    \n" +
   1.371 +"    public byte[] digest(ByteBuffer bb) {\n" +
   1.372 +"        return impl.digest(bb);\n" +
   1.373 +"    }\n" +
   1.374 +"}\n" +
   1.375 +"// END: day.end.bridges.Digest\n";
   1.376 +        
   1.377 +        File src = createFile(1, "C.java", c1);
   1.378 +        
   1.379 +        
   1.380 +        String c2 =
   1.381 +            "@day.end.bridges.Digest@";
   1.382 +        File txt = createFile(2, "in.txt", c2);
   1.383 +        
   1.384 +        File out = createFile(3, "out.txt", "");
   1.385 +        out.delete();
   1.386 +        
   1.387 +        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
   1.388 +        
   1.389 +        assertTrue("output created", out.canRead());
   1.390 +        
   1.391 +        String r = readFile(out);
   1.392 +        if (r.indexOf("&=") >= 0) {
   1.393 +            fail("Wrong XML: " + r);
   1.394 +        }
   1.395 +        if (r.indexOf("&amp;") > -1) {
   1.396 +            fail("Wrong XML, no &amp;: " + r);
   1.397 +        }
   1.398 +    }
   1.399 +    
   1.400 +    public void testInXML() throws Exception {
   1.401 +        String c1 =
   1.402 +            "<!-- BEGIN: clazz -->\n" +
   1.403 +            "<interface name='I'/>\n" +
   1.404 +            "<!-- END: clazz -->\n" +
   1.405 +            "";
   1.406 +        File src = createFile(1, "I.xml", c1);
   1.407 +        
   1.408 +        
   1.409 +        String c2 =
   1.410 +            "@clazz@";
   1.411 +        File txt = createFile(2, "in.txt", c2);
   1.412 +        
   1.413 +        File out = createFile(3, "out.txt", "");
   1.414 +        out.delete();
   1.415 +        
   1.416 +        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.xml");
   1.417 +        
   1.418 +        assertTrue("output created", out.canRead());
   1.419 +        
   1.420 +        String r = readFile(out);
   1.421 +        if (r.indexOf("BEGIN") >= 0) {
   1.422 +            fail("BEGIN is there: " + r);
   1.423 +        }
   1.424 +        if (r.indexOf("END") >= 0) {
   1.425 +            fail("END is there: " + r);
   1.426 +        }
   1.427 +        assertEquals("<interface name='I'/>\n", r);
   1.428 +    }
   1.429 +    public void testInXMLWithComment() throws Exception {
   1.430 +        String c1 =
   1.431 +            "<!-- BEGIN: clazz -->\n" +
   1.432 +            "<!--\n" +
   1.433 +            " ahoj -->\n" +
   1.434 +            "<!-- END: clazz -->\n" +
   1.435 +            "";
   1.436 +        File src = createFile(1, "I.xml", c1);
   1.437 +        
   1.438 +        
   1.439 +        String c2 =
   1.440 +            "@clazz@";
   1.441 +        File txt = createFile(2, "in.txt", c2);
   1.442 +        
   1.443 +        File out = createFile(3, "out.txt", "");
   1.444 +        out.delete();
   1.445 +        
   1.446 +        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.xml");
   1.447 +        
   1.448 +        assertTrue("output created", out.canRead());
   1.449 +        
   1.450 +        String r = readFile(out);
   1.451 +        if (r.indexOf("BEGIN") >= 0) {
   1.452 +            fail("BEGIN is there: " + r);
   1.453 +        }
   1.454 +        if (r.indexOf("END") >= 0) {
   1.455 +            fail("END is there: " + r);
   1.456 +        }
   1.457 +        assertEquals("No xml please", "<!--\n ahoj -->\n", r);
   1.458 +    }
   1.459 +    
   1.460 +    public void testInXMLWithMultilineString() throws Exception {
   1.461 +        String c1 =
   1.462 +            "<!-- BEGIN: clazz -->\n" +
   1.463 +            "<element tag='\n" +
   1.464 +            " ahoj'\n" +
   1.465 +            "/>\n" +
   1.466 +            "<!-- END: clazz -->\n" +
   1.467 +            "";
   1.468 +        File src = createFile(1, "I.xml", c1);
   1.469 +        
   1.470 +        
   1.471 +        String c2 =
   1.472 +            "@clazz@";
   1.473 +        File txt = createFile(2, "in.txt", c2);
   1.474 +        
   1.475 +        File out = createFile(3, "out.txt", "");
   1.476 +        out.delete();
   1.477 +        
   1.478 +        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.xml");
   1.479 +        
   1.480 +        assertTrue("output created", out.canRead());
   1.481 +        
   1.482 +        String r = readFile(out);
   1.483 +        if (r.indexOf("BEGIN") >= 0) {
   1.484 +            fail("BEGIN is there: " + r);
   1.485 +        }
   1.486 +        if (r.indexOf("END") >= 0) {
   1.487 +            fail("END is there: " + r);
   1.488 +        }
   1.489 +        assertEquals("I want no XML escapes", "<element tag='\n ahoj'\n/>\n", r);
   1.490 +    }
   1.491 +    
   1.492 +    public void testLongLineDetected() throws Exception {
   1.493 +        String c1 =
   1.494 +"package org.apidesign.api.security;\n" +
   1.495 +"" +
   1.496 +"import java.nio.ByteBuffer;\n" +
   1.497 +"import java.util.ServiceLoader;\n" +
   1.498 +"import org.apidesign.spi.security.Digestor;\n" +
   1.499 +"\n" +
   1.500 +"/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
   1.501 +" * for buffer of data.\n" +
   1.502 +" *\n" +
   1.503 +" * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
   1.504 +" */\n" +
   1.505 +"// BEGIN: day.end.bridges.Digest\n" +
   1.506 +"d;   DigestImplementation<?> impl = DigestImplementation.create(digestor, algorithm);\n" +
   1.507 +"// END: day.end.bridges.Digest\n";
   1.508 +        
   1.509 +        File src = createFile(1, "C.java", c1);
   1.510 +        
   1.511 +        
   1.512 +        String c2 =
   1.513 +            "@day.end.bridges.Digest@";
   1.514 +        File txt = createFile(2, "in.txt", c2);
   1.515 +        
   1.516 +        File out = createFile(3, "out.txt", "");
   1.517 +        out.delete();
   1.518 + 
   1.519 +        try {
   1.520 +            execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
   1.521 +        } catch (ExecuteUtils.ExecutionError ex) {
   1.522 +            // OK
   1.523 +            return;
   1.524 +        }
   1.525 +        fail("Should fail, as there is long line");
   1.526 +    }
   1.527 +    public void testLongNotLineDetectedAsShortened() throws Exception {
   1.528 +        String c1 =
   1.529 +"package org.apidesign.api.security;\n" +
   1.530 +"" +
   1.531 +"import java.nio.ByteBuffer;\n" +
   1.532 +"import java.util.ServiceLoader;\n" +
   1.533 +"import org.apidesign.spi.security.Digestor;\n" +
   1.534 +"\n" +
   1.535 +"/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
   1.536 +" * for buffer of data.\n" +
   1.537 +" *\n" +
   1.538 +" * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
   1.539 +" */\n" +
   1.540 +"// BEGIN: day.end.bridges.Digest\n" +
   1.541 +"   DigestImplementation<?> impl = DigestImplementation.create(digestor, algorithm);\n" +
   1.542 +"// END: day.end.bridges.Digest\n";
   1.543 +        
   1.544 +        File src = createFile(1, "C.java", c1);
   1.545 +        
   1.546 +        
   1.547 +        String c2 =
   1.548 +            "@day.end.bridges.Digest@";
   1.549 +        File txt = createFile(2, "in.txt", c2);
   1.550 +        
   1.551 +        File out = createFile(3, "out.txt", "");
   1.552 +        out.delete();
   1.553 + 
   1.554 +        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
   1.555 +    }
   1.556 +    
   1.557 +    public void testNotClosedSection() throws Exception {
   1.558 +        String c1 =
   1.559 +            "package ahoj;\n" +
   1.560 +            "// BEGIN: clazz\n" +
   1.561 +            "int x;\n" +
   1.562 +            "\n";
   1.563 +        File src = createFile(1, "I.java", c1);
   1.564 +        
   1.565 +        
   1.566 +        String c2 =
   1.567 +            "@clazz@";
   1.568 +        File txt = createFile(2, "in.txt", c2);
   1.569 +        
   1.570 +        File out = createFile(3, "out.txt", "");
   1.571 +        out.delete();
   1.572 +        
   1.573 +        try {
   1.574 +            execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
   1.575 +            fail("Has to fail");
   1.576 +        } catch (ExecuteUtils.ExecutionError ex) {
   1.577 +            // ok
   1.578 +        }
   1.579 +    }
   1.580 +    public void testLongNotLineDetectedAsNotInTheList() throws Exception {
   1.581 +        String c1 =
   1.582 +"package org.apidesign.api.security;\n" +
   1.583 +"" +
   1.584 +"import java.nio.ByteBuffer;\n" +
   1.585 +"import java.util.ServiceLoader;\n" +
   1.586 +"import org.apidesign.spi.security.Digestor;\n" +
   1.587 +"\n" +
   1.588 +"/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
   1.589 +" * for buffer of data.\n" +
   1.590 +" *\n" +
   1.591 +" * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
   1.592 +" */\n" +
   1.593 +"   DigestImplementation<?> impl    =      DigestImplementation.create(digestor, algorithm);\n" +
   1.594 +"// BEGIN: day.end.bridges.Digest\n" +
   1.595 +"   DigestImplementation<?> impl    =      null\n" +
   1.596 +"// END: day.end.bridges.Digest\n";
   1.597 +        
   1.598 +        File src = createFile(1, "C.java", c1);
   1.599 +        
   1.600 +        
   1.601 +        String c2 =
   1.602 +            "@day.end.bridges.Digest@";
   1.603 +        File txt = createFile(2, "in.txt", c2);
   1.604 +        
   1.605 +        File out = createFile(3, "out.txt", "");
   1.606 +        out.delete();
   1.607 + 
   1.608 +        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
   1.609 +    }
   1.610 +    
   1.611 +    protected final File createFile(int slot, String name, String content) throws Exception {
   1.612 +        File d1 = new File(getWorkDir(), "dir" + slot);
   1.613 +        File c1 = new File(d1, name);
   1.614 +        copy(content, c1);
   1.615 +        return c1;
   1.616 +    }
   1.617 +    
   1.618 +    protected final void execute(int slotFirst, int slotSecond, String... additionalArgs) throws Exception {
   1.619 +        File d1 = new File(getWorkDir(), "dir" + slotFirst);
   1.620 +        File d2 = new File(getWorkDir(), "dir" + slotSecond);
   1.621 +        
   1.622 +        File build = new File(getWorkDir(), "build.xml");
   1.623 +        extractResource("color.xml", build);
   1.624 +        
   1.625 +        List<String> args = new ArrayList<String>();
   1.626 +        args.addAll(Arrays.asList(additionalArgs));
   1.627 +        args.add("-Ddir1=" + d1);
   1.628 +        args.add("-Ddir2=" + d2);
   1.629 +        ExecuteUtils.execute(build, args.toArray(new String[0]));
   1.630 +    }
   1.631 +    
   1.632 +    private static final void copy(String txt, File f) throws Exception {
   1.633 +        f.getParentFile().mkdirs();
   1.634 +        FileWriter w = new FileWriter(f);
   1.635 +        w.append(txt);
   1.636 +        w.close();
   1.637 +    }
   1.638 +
   1.639 +    final File extractResource(String res, File f) throws Exception {
   1.640 +        URL u = ColorifyTest.class.getResource(res);
   1.641 +        assertNotNull ("Resource should be found " + res, u);
   1.642 +        
   1.643 +        FileOutputStream os = new FileOutputStream(f);
   1.644 +        InputStream is = u.openStream();
   1.645 +        for (;;) {
   1.646 +            int ch = is.read ();
   1.647 +            if (ch == -1) {
   1.648 +                break;
   1.649 +            }
   1.650 +            os.write (ch);
   1.651 +        }
   1.652 +        os.close ();
   1.653 +            
   1.654 +        return f;
   1.655 +    }
   1.656 +    
   1.657 +    final static String readFile (java.io.File f) throws java.io.IOException {
   1.658 +        int s = (int)f.length ();
   1.659 +        byte[] data = new byte[s];
   1.660 +        assertEquals ("Read all data", s, new java.io.FileInputStream (f).read (data));
   1.661 +        
   1.662 +        return new String (data);
   1.663 +    }
   1.664 +    
   1.665 +    final File extractString (String res, String nameExt) throws Exception {
   1.666 +        File f = new File(getWorkDir(), nameExt);
   1.667 +        f.deleteOnExit ();
   1.668 +        
   1.669 +        FileOutputStream os = new FileOutputStream(f);
   1.670 +        InputStream is = new ByteArrayInputStream(res.getBytes("UTF-8"));
   1.671 +        for (;;) {
   1.672 +            int ch = is.read ();
   1.673 +            if (ch == -1) {
   1.674 +                break;
   1.675 +            }
   1.676 +            os.write (ch);
   1.677 +        }
   1.678 +        os.close ();
   1.679 +            
   1.680 +        return f;
   1.681 +    }
   1.682 +    
   1.683 +}
   1.684 \ No newline at end of file