java/ant/test/org/apidesign/infra/ant/ColorifyTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 03 Apr 2020 16:32:36 +0200
changeset 416 9ed8788a1a4e
permissions -rw-r--r--
Using HTTPS to download the libraries
jtulach@268
     1
/*
jtulach@268
     2
 * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
jtulach@268
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jtulach@268
     4
 *
jtulach@268
     5
 * This code is free software; you can redistribute it and/or modify it
jtulach@268
     6
 * under the terms of the GNU General Public License version 2 only, as
jtulach@268
     7
 * published by the Free Software Foundation.  Sun designates this
jtulach@268
     8
 * particular file as subject to the "Classpath" exception as provided
jtulach@268
     9
 * by Sun in the LICENSE file that accompanied this code.
jtulach@268
    10
 *
jtulach@268
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jtulach@268
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jtulach@268
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jtulach@268
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jtulach@268
    15
 * accompanied this code).
jtulach@268
    16
 *
jtulach@268
    17
 * You should have received a copy of the GNU General Public License version
jtulach@268
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jtulach@268
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jtulach@268
    20
 *
jtulach@268
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
jtulach@268
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
jtulach@268
    23
 * have any questions.
jtulach@268
    24
 */
jtulach@268
    25
package org.apidesign.infra.ant;
jtulach@268
    26
jtulach@268
    27
import java.io.ByteArrayInputStream;
jtulach@268
    28
import java.io.File;
jtulach@268
    29
import java.io.FileOutputStream;
jtulach@268
    30
import java.io.FileWriter;
jtulach@268
    31
import java.io.InputStream;
jtulach@268
    32
import java.net.URL;
jtulach@268
    33
import java.util.ArrayList;
jtulach@268
    34
import java.util.Arrays;
jtulach@268
    35
import java.util.List;
jtulach@268
    36
import junit.framework.Test;
jtulach@268
    37
import org.netbeans.junit.NbTestCase;
jtulach@268
    38
import org.netbeans.junit.NbTestSuite;
jtulach@268
    39
jtulach@268
    40
/**
jtulach@268
    41
 *
jtulach@268
    42
 * @author Jaroslav Tulach
jtulach@268
    43
 */
jtulach@268
    44
public class ColorifyTest extends NbTestCase {
jtulach@268
    45
    private static File workDir;
jtulach@268
    46
    
jtulach@268
    47
    public ColorifyTest(String s) {
jtulach@268
    48
        super(s);
jtulach@268
    49
    }
jtulach@268
    50
    
jtulach@268
    51
    public static Test suite() {
jtulach@268
    52
        return new NbTestSuite(ColorifyTest.class);
jtulach@268
    53
//        return new ColorifyTest("testInXMLWithMultilineString");
jtulach@268
    54
    }
jtulach@268
    55
jtulach@268
    56
    @Override
jtulach@268
    57
    protected void setUp() throws Exception {
jtulach@268
    58
        clearWorkDir();
jtulach@268
    59
    }
jtulach@268
    60
jtulach@268
    61
    public void testMissingMethodInAnInterfaceIsDetected() throws Exception {
jtulach@268
    62
        String c1 =
jtulach@268
    63
            "package ahoj;\n" +
jtulach@268
    64
            "// BEGIN: xyz\n" +
jtulach@268
    65
            "public interface I {\n" +
jtulach@268
    66
            "// FINISH: xyz\n" +
jtulach@268
    67
            "  public void get();\n" +
jtulach@268
    68
            "}" +
jtulach@268
    69
            "";
jtulach@268
    70
        File src = createFile(1, "I.java", c1);
jtulach@268
    71
        
jtulach@268
    72
        
jtulach@268
    73
        String c2 =
jtulach@268
    74
            "@xyz@";
jtulach@268
    75
        File txt = createFile(2, "in.txt", c2);
jtulach@268
    76
        
jtulach@268
    77
        File out = createFile(3, "out.txt", "");
jtulach@268
    78
        out.delete();
jtulach@268
    79
        
jtulach@268
    80
        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
jtulach@268
    81
        
jtulach@268
    82
        assertTrue("output created", out.canRead());
jtulach@268
    83
        
jtulach@268
    84
        String r = readFile(out);
jtulach@268
    85
        assertEquals("public interface I {\n}\n", r);
jtulach@268
    86
    }
jtulach@268
    87
    
jtulach@268
    88
    public void testTryFinally() throws Exception {
jtulach@268
    89
        String c1 =
jtulach@268
    90
            "package ahoj;\n" +
jtulach@268
    91
            "// BEGIN: xyz\n" +
jtulach@268
    92
            "  try {\n" +
jtulach@268
    93
            "    // something\n" +
jtulach@268
    94
            "  } finally {\n" +
jtulach@268
    95
            "    // else\n" +
jtulach@268
    96
            "  }\n" +
jtulach@268
    97
            "// END: xyz\n" +
jtulach@268
    98
            "  public void get();\n" +
jtulach@268
    99
            "}" +
jtulach@268
   100
            "";
jtulach@268
   101
        File src = createFile(1, "I.java", c1);
jtulach@268
   102
        
jtulach@268
   103
        
jtulach@268
   104
        String c2 =
jtulach@268
   105
            "@xyz@";
jtulach@268
   106
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   107
        
jtulach@268
   108
        File out = createFile(3, "out.txt", "");
jtulach@268
   109
        out.delete();
jtulach@268
   110
        
jtulach@268
   111
        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
jtulach@268
   112
        
jtulach@268
   113
        assertTrue("output created", out.canRead());
jtulach@268
   114
        
jtulach@268
   115
        String r = readFile(out);
jtulach@268
   116
        String exp =
jtulach@268
   117
            "try {\n" +
jtulach@268
   118
            "  // something\n" +
jtulach@268
   119
            "} finally {\n" +
jtulach@268
   120
            "  // else\n" +
jtulach@268
   121
            "}\n";
jtulach@268
   122
        assertEquals(exp, r);
jtulach@268
   123
    }
jtulach@268
   124
    
jtulach@268
   125
    public void testString() throws Exception {
jtulach@268
   126
        String c1 =
jtulach@268
   127
            "package ahoj;\n" +
jtulach@268
   128
            "public interface I {\n" +
jtulach@268
   129
            "// BEGIN: xyz\n" +
jtulach@268
   130
            "  public static final String X = \"ahoj\"\n" +
jtulach@268
   131
            "// END: xyz\n" +
jtulach@268
   132
            "}" +
jtulach@268
   133
            "";
jtulach@268
   134
        File src = createFile(1, "I.java", c1);
jtulach@268
   135
        
jtulach@268
   136
        
jtulach@268
   137
        String c2 =
jtulach@268
   138
            "@xyz@";
jtulach@268
   139
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   140
        
jtulach@268
   141
        File out = createFile(3, "out.txt", "");
jtulach@268
   142
        out.delete();
jtulach@268
   143
        
jtulach@268
   144
        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
jtulach@268
   145
        
jtulach@268
   146
        assertTrue("output created", out.canRead());
jtulach@268
   147
        
jtulach@268
   148
        String r = readFile(out);
jtulach@268
   149
        assertEquals("public static final String X = \"ahoj\"\n", r);
jtulach@268
   150
    }
jtulach@268
   151
    
jtulach@268
   152
    public void testSpacesAtBeginingAreStripped() throws Exception {
jtulach@268
   153
        String c1 =
jtulach@268
   154
            "package ahoj;\n" +
jtulach@268
   155
            "// BEGIN: xyz\n" +
jtulach@268
   156
            "   public interface I {\n" +
jtulach@268
   157
            "     public void ahoj ();\n" +
jtulach@268
   158
            "   }\n" +
jtulach@268
   159
            "// END: xyz\n" +
jtulach@268
   160
            "  public void get();\n" +
jtulach@268
   161
            "}" +
jtulach@268
   162
            "";
jtulach@268
   163
        File src = createFile(1, "I.java", c1);
jtulach@268
   164
        
jtulach@268
   165
        
jtulach@268
   166
        String c2 =
jtulach@268
   167
            "@xyz@";
jtulach@268
   168
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   169
        
jtulach@268
   170
        File out = createFile(3, "out.txt", "");
jtulach@268
   171
        out.delete();
jtulach@268
   172
        
jtulach@268
   173
        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
jtulach@268
   174
        
jtulach@268
   175
        assertTrue("output created", out.canRead());
jtulach@268
   176
        
jtulach@268
   177
        String r = readFile(out);
jtulach@268
   178
        String result = "public interface I {\n" +
jtulach@268
   179
            "  public void ahoj ();\n" +
jtulach@268
   180
            "}\n";
jtulach@268
   181
        assertEquals(result, r);
jtulach@268
   182
    }
jtulach@268
   183
        
jtulach@268
   184
    public void testIncludedTexts() throws Exception {
jtulach@268
   185
        String c1 =
jtulach@268
   186
            "package ahoj;\n" +
jtulach@268
   187
            "// BEGIN: clazz\n" +
jtulach@268
   188
            "public interface I {\n" +
jtulach@268
   189
            "  // BEGIN: method\n" +
jtulach@268
   190
            "  public void get();\n" +
jtulach@268
   191
            "  // END: method\n" +
jtulach@268
   192
            "}\n" +
jtulach@268
   193
            "// END: clazz\n" +
jtulach@268
   194
            "";
jtulach@268
   195
        File src = createFile(1, "I.java", c1);
jtulach@268
   196
        
jtulach@268
   197
        
jtulach@268
   198
        String c2 =
jtulach@268
   199
            "@clazz@";
jtulach@268
   200
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   201
        
jtulach@268
   202
        File out = createFile(3, "out.txt", "");
jtulach@268
   203
        out.delete();
jtulach@268
   204
        
jtulach@268
   205
        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
jtulach@268
   206
        
jtulach@268
   207
        assertTrue("output created", out.canRead());
jtulach@268
   208
        
jtulach@268
   209
        String r = readFile(out);
jtulach@268
   210
        if (r.indexOf("BEGIN") >= 0) {
jtulach@268
   211
            fail("BEGIN is there: " + r);
jtulach@268
   212
        }
jtulach@268
   213
        if (r.indexOf("END") >= 0) {
jtulach@268
   214
            fail("END is there: " + r);
jtulach@268
   215
        }
jtulach@268
   216
        if (r.indexOf("interface I") < 0) {
jtulach@268
   217
            fail("Missing interface: " + r);
jtulach@268
   218
        }
jtulach@268
   219
        if (r.indexOf("void get()") < 0) {
jtulach@268
   220
            fail("Missing get: " + r);
jtulach@268
   221
        }
jtulach@268
   222
    }
jtulach@268
   223
    public void testWithCommentsTexts() throws Exception {
jtulach@268
   224
        String c1 =
jtulach@268
   225
            "package ahoj;\n" +
jtulach@268
   226
            "// BEGIN: clazz\n" +
jtulach@268
   227
            "/** Beautiful interface I\n" +
jtulach@268
   228
            " */\n" +
jtulach@268
   229
            "public interface I {\n" +
jtulach@268
   230
            "  // single line\n" +
jtulach@268
   231
            "}\n" +
jtulach@268
   232
            "// END: clazz\n" +
jtulach@268
   233
            "";
jtulach@268
   234
        File src = createFile(1, "I.java", c1);
jtulach@268
   235
        
jtulach@268
   236
        
jtulach@268
   237
        String c2 =
jtulach@268
   238
            "@clazz@";
jtulach@268
   239
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   240
        
jtulach@268
   241
        File out = createFile(3, "out.txt", "");
jtulach@268
   242
        out.delete();
jtulach@268
   243
        
jtulach@268
   244
        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
jtulach@268
   245
        
jtulach@268
   246
        assertTrue("output created", out.canRead());
jtulach@268
   247
        
jtulach@268
   248
        String r = readFile(out);
jtulach@268
   249
        if (r.indexOf("BEGIN") >= 0) {
jtulach@268
   250
            fail("BEGIN is there: " + r);
jtulach@268
   251
        }
jtulach@268
   252
        if (r.indexOf("END") >= 0) {
jtulach@268
   253
            fail("END is there: " + r);
jtulach@268
   254
        }
jtulach@268
   255
        if (r.indexOf("interface I") < 0) {
jtulach@268
   256
            fail("Missing interface: " + r);
jtulach@268
   257
        }
jtulach@268
   258
        if (r.indexOf("// single line\n}") < 0) {
jtulach@268
   259
            fail("Comment indentified: " + r);
jtulach@268
   260
        }
jtulach@268
   261
    }
jtulach@268
   262
    public void testMultiLineComment() throws Exception {
jtulach@268
   263
        String c1 =
jtulach@268
   264
            "package ahoj;\n" +
jtulach@268
   265
            "// BEGIN: clazz\n" +
jtulach@268
   266
            "public void x() {\n" +
jtulach@268
   267
            "  /** Beautiful interface I\n" +
jtulach@268
   268
            "  *\n" +
jtulach@268
   269
            "  */\n" +
jtulach@268
   270
            "}\n" +
jtulach@268
   271
            "// END: clazz\n" +
jtulach@268
   272
            "";
jtulach@268
   273
        File src = createFile(1, "I.java", c1);
jtulach@268
   274
        
jtulach@268
   275
        
jtulach@268
   276
        String c2 =
jtulach@268
   277
            "@clazz@";
jtulach@268
   278
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   279
        
jtulach@268
   280
        File out = createFile(3, "out.txt", "");
jtulach@268
   281
        out.delete();
jtulach@268
   282
        
jtulach@268
   283
        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
jtulach@268
   284
        
jtulach@268
   285
        assertTrue("output created", out.canRead());
jtulach@268
   286
        
jtulach@268
   287
        String r = readFile(out);
jtulach@268
   288
        int f = r.indexOf(
jtulach@268
   289
            "/** Beautiful interface I\n" +
jtulach@268
   290
            "  *\n" +
jtulach@268
   291
            "  */\n");
jtulach@268
   292
        if (f == -1) {
jtulach@268
   293
            fail("Not found: " + r);
jtulach@268
   294
        }
jtulach@268
   295
    }
jtulach@268
   296
    public void testIncludedTextsAmper() throws Exception {
jtulach@268
   297
        String c1 =
jtulach@268
   298
            "package ahoj;\n" +
jtulach@268
   299
            "public class C {\n" +
jtulach@268
   300
            "  // BEGIN: method\n" +
jtulach@268
   301
            "  public void change(int x) { x &= 10; }\n" +
jtulach@268
   302
            "  // END: method\n" +
jtulach@268
   303
            "}\n" +
jtulach@268
   304
            "";
jtulach@268
   305
        File src = createFile(1, "C.java", c1);
jtulach@268
   306
        
jtulach@268
   307
        
jtulach@268
   308
        String c2 =
jtulach@268
   309
            "@method@";
jtulach@268
   310
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   311
        
jtulach@268
   312
        File out = createFile(3, "out.txt", "");
jtulach@268
   313
        out.delete();
jtulach@268
   314
        
jtulach@268
   315
        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
jtulach@268
   316
        
jtulach@268
   317
        assertTrue("output created", out.canRead());
jtulach@268
   318
        
jtulach@268
   319
        String r = readFile(out);
jtulach@268
   320
        if (r.indexOf("&=") < 0) {
jtulach@268
   321
            fail("No XML escapes please: " + r);
jtulach@268
   322
        }
jtulach@268
   323
        if (r.indexOf("&amp;=") >= 0) {
jtulach@268
   324
            fail("we need no xml escapes, no &amp;: " + r);
jtulach@268
   325
        }
jtulach@268
   326
    }
jtulach@268
   327
    public void testIncludedTextsAmperAndGenerics() throws Exception {
jtulach@268
   328
        String c1 =
jtulach@268
   329
jtulach@268
   330
"package org.apidesign.api.security;\n" +
jtulach@268
   331
"" +
jtulach@268
   332
"import java.nio.ByteBuffer;\n" +
jtulach@268
   333
"import java.util.ServiceLoader;\n" +
jtulach@268
   334
"import org.apidesign.spi.security.Digestor;\n" +
jtulach@268
   335
"\n" +
jtulach@268
   336
"/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
jtulach@268
   337
" * for buffer of data.\n" +
jtulach@268
   338
" *\n" +
jtulach@268
   339
" * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
jtulach@268
   340
" */\n" +
jtulach@268
   341
"// BEGIN: day.end.bridges.Digest\n" +
jtulach@268
   342
"public final class Digest {\n" +
jtulach@268
   343
"    private final DigestImplementation<?> impl;\n" +
jtulach@268
   344
"    \n" +
jtulach@268
   345
"    /** Factory method is better than constructor */\n" +
jtulach@268
   346
"    private Digest(DigestImplementation<?> impl) {\n" +
jtulach@268
   347
"        this.impl = impl;\n" +
jtulach@268
   348
"    }\n" +
jtulach@268
   349
"    \n" +
jtulach@268
   350
"    /** Factory method to create digest for an algorithm.\n" +
jtulach@268
   351
"     */\n" +
jtulach@268
   352
"    public static Digest getInstance(String algorithm) {\n" +
jtulach@268
   353
"        for (Digestor<?> digestor : ServiceLoader.load(Digestor.class)) {\n" +
jtulach@268
   354
"            DigestImplementation<?> impl = \n" +
jtulach@268
   355
                "DigestImplementation.create(digestor, algorithm);\n" +
jtulach@268
   356
"            if (impl != null) {\n" +
jtulach@268
   357
"                return new Digest(impl);\n" +
jtulach@268
   358
"            }\n" +
jtulach@268
   359
"        }\n" +
jtulach@268
   360
"        throw new IllegalArgumentException(algorithm);\n" +
jtulach@268
   361
"    }\n" +
jtulach@268
   362
"      \n" +
jtulach@268
   363
"    //\n" +
jtulach@268
   364
"    // these methods are kept the same as in original MessageDigest,\n" +
jtulach@268
   365
"    // but for simplicity choose just some from the original API\n" +
jtulach@268
   366
"    //\n" +
jtulach@268
   367
"    \n" +
jtulach@268
   368
"    public byte[] digest(ByteBuffer bb) {\n" +
jtulach@268
   369
"        return impl.digest(bb);\n" +
jtulach@268
   370
"    }\n" +
jtulach@268
   371
"}\n" +
jtulach@268
   372
"// END: day.end.bridges.Digest\n";
jtulach@268
   373
        
jtulach@268
   374
        File src = createFile(1, "C.java", c1);
jtulach@268
   375
        
jtulach@268
   376
        
jtulach@268
   377
        String c2 =
jtulach@268
   378
            "@day.end.bridges.Digest@";
jtulach@268
   379
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   380
        
jtulach@268
   381
        File out = createFile(3, "out.txt", "");
jtulach@268
   382
        out.delete();
jtulach@268
   383
        
jtulach@268
   384
        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
jtulach@268
   385
        
jtulach@268
   386
        assertTrue("output created", out.canRead());
jtulach@268
   387
        
jtulach@268
   388
        String r = readFile(out);
jtulach@268
   389
        if (r.indexOf("&=") >= 0) {
jtulach@268
   390
            fail("Wrong XML: " + r);
jtulach@268
   391
        }
jtulach@268
   392
        if (r.indexOf("&amp;") > -1) {
jtulach@268
   393
            fail("Wrong XML, no &amp;: " + r);
jtulach@268
   394
        }
jtulach@268
   395
    }
jtulach@268
   396
    
jtulach@268
   397
    public void testInXML() throws Exception {
jtulach@268
   398
        String c1 =
jtulach@268
   399
            "<!-- BEGIN: clazz -->\n" +
jtulach@268
   400
            "<interface name='I'/>\n" +
jtulach@268
   401
            "<!-- END: clazz -->\n" +
jtulach@268
   402
            "";
jtulach@268
   403
        File src = createFile(1, "I.xml", c1);
jtulach@268
   404
        
jtulach@268
   405
        
jtulach@268
   406
        String c2 =
jtulach@268
   407
            "@clazz@";
jtulach@268
   408
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   409
        
jtulach@268
   410
        File out = createFile(3, "out.txt", "");
jtulach@268
   411
        out.delete();
jtulach@268
   412
        
jtulach@268
   413
        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.xml");
jtulach@268
   414
        
jtulach@268
   415
        assertTrue("output created", out.canRead());
jtulach@268
   416
        
jtulach@268
   417
        String r = readFile(out);
jtulach@268
   418
        if (r.indexOf("BEGIN") >= 0) {
jtulach@268
   419
            fail("BEGIN is there: " + r);
jtulach@268
   420
        }
jtulach@268
   421
        if (r.indexOf("END") >= 0) {
jtulach@268
   422
            fail("END is there: " + r);
jtulach@268
   423
        }
jtulach@268
   424
        assertEquals("<interface name='I'/>\n", r);
jtulach@268
   425
    }
jtulach@268
   426
    public void testInXMLWithComment() throws Exception {
jtulach@268
   427
        String c1 =
jtulach@268
   428
            "<!-- BEGIN: clazz -->\n" +
jtulach@268
   429
            "<!--\n" +
jtulach@268
   430
            " ahoj -->\n" +
jtulach@268
   431
            "<!-- END: clazz -->\n" +
jtulach@268
   432
            "";
jtulach@268
   433
        File src = createFile(1, "I.xml", c1);
jtulach@268
   434
        
jtulach@268
   435
        
jtulach@268
   436
        String c2 =
jtulach@268
   437
            "@clazz@";
jtulach@268
   438
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   439
        
jtulach@268
   440
        File out = createFile(3, "out.txt", "");
jtulach@268
   441
        out.delete();
jtulach@268
   442
        
jtulach@268
   443
        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.xml");
jtulach@268
   444
        
jtulach@268
   445
        assertTrue("output created", out.canRead());
jtulach@268
   446
        
jtulach@268
   447
        String r = readFile(out);
jtulach@268
   448
        if (r.indexOf("BEGIN") >= 0) {
jtulach@268
   449
            fail("BEGIN is there: " + r);
jtulach@268
   450
        }
jtulach@268
   451
        if (r.indexOf("END") >= 0) {
jtulach@268
   452
            fail("END is there: " + r);
jtulach@268
   453
        }
jtulach@268
   454
        assertEquals("No xml please", "<!--\n ahoj -->\n", r);
jtulach@268
   455
    }
jtulach@268
   456
    
jtulach@268
   457
    public void testInXMLWithMultilineString() throws Exception {
jtulach@268
   458
        String c1 =
jtulach@268
   459
            "<!-- BEGIN: clazz -->\n" +
jtulach@268
   460
            "<element tag='\n" +
jtulach@268
   461
            " ahoj'\n" +
jtulach@268
   462
            "/>\n" +
jtulach@268
   463
            "<!-- END: clazz -->\n" +
jtulach@268
   464
            "";
jtulach@268
   465
        File src = createFile(1, "I.xml", c1);
jtulach@268
   466
        
jtulach@268
   467
        
jtulach@268
   468
        String c2 =
jtulach@268
   469
            "@clazz@";
jtulach@268
   470
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   471
        
jtulach@268
   472
        File out = createFile(3, "out.txt", "");
jtulach@268
   473
        out.delete();
jtulach@268
   474
        
jtulach@268
   475
        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.xml");
jtulach@268
   476
        
jtulach@268
   477
        assertTrue("output created", out.canRead());
jtulach@268
   478
        
jtulach@268
   479
        String r = readFile(out);
jtulach@268
   480
        if (r.indexOf("BEGIN") >= 0) {
jtulach@268
   481
            fail("BEGIN is there: " + r);
jtulach@268
   482
        }
jtulach@268
   483
        if (r.indexOf("END") >= 0) {
jtulach@268
   484
            fail("END is there: " + r);
jtulach@268
   485
        }
jtulach@268
   486
        assertEquals("I want no XML escapes", "<element tag='\n ahoj'\n/>\n", r);
jtulach@268
   487
    }
jtulach@268
   488
    
jtulach@268
   489
    public void testLongLineDetected() throws Exception {
jtulach@268
   490
        String c1 =
jtulach@268
   491
"package org.apidesign.api.security;\n" +
jtulach@268
   492
"" +
jtulach@268
   493
"import java.nio.ByteBuffer;\n" +
jtulach@268
   494
"import java.util.ServiceLoader;\n" +
jtulach@268
   495
"import org.apidesign.spi.security.Digestor;\n" +
jtulach@268
   496
"\n" +
jtulach@268
   497
"/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
jtulach@268
   498
" * for buffer of data.\n" +
jtulach@268
   499
" *\n" +
jtulach@268
   500
" * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
jtulach@268
   501
" */\n" +
jtulach@268
   502
"// BEGIN: day.end.bridges.Digest\n" +
jtulach@268
   503
"d;   DigestImplementation<?> impl = DigestImplementation.create(digestor, algorithm);\n" +
jtulach@268
   504
"// END: day.end.bridges.Digest\n";
jtulach@268
   505
        
jtulach@268
   506
        File src = createFile(1, "C.java", c1);
jtulach@268
   507
        
jtulach@268
   508
        
jtulach@268
   509
        String c2 =
jtulach@268
   510
            "@day.end.bridges.Digest@";
jtulach@268
   511
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   512
        
jtulach@268
   513
        File out = createFile(3, "out.txt", "");
jtulach@268
   514
        out.delete();
jtulach@268
   515
 
jtulach@268
   516
        try {
jtulach@268
   517
            execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
jtulach@268
   518
        } catch (ExecuteUtils.ExecutionError ex) {
jtulach@268
   519
            // OK
jtulach@268
   520
            return;
jtulach@268
   521
        }
jtulach@268
   522
        fail("Should fail, as there is long line");
jtulach@268
   523
    }
jtulach@268
   524
    public void testLongNotLineDetectedAsShortened() throws Exception {
jtulach@268
   525
        String c1 =
jtulach@268
   526
"package org.apidesign.api.security;\n" +
jtulach@268
   527
"" +
jtulach@268
   528
"import java.nio.ByteBuffer;\n" +
jtulach@268
   529
"import java.util.ServiceLoader;\n" +
jtulach@268
   530
"import org.apidesign.spi.security.Digestor;\n" +
jtulach@268
   531
"\n" +
jtulach@268
   532
"/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
jtulach@268
   533
" * for buffer of data.\n" +
jtulach@268
   534
" *\n" +
jtulach@268
   535
" * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
jtulach@268
   536
" */\n" +
jtulach@268
   537
"// BEGIN: day.end.bridges.Digest\n" +
jtulach@268
   538
"   DigestImplementation<?> impl = DigestImplementation.create(digestor, algorithm);\n" +
jtulach@268
   539
"// END: day.end.bridges.Digest\n";
jtulach@268
   540
        
jtulach@268
   541
        File src = createFile(1, "C.java", c1);
jtulach@268
   542
        
jtulach@268
   543
        
jtulach@268
   544
        String c2 =
jtulach@268
   545
            "@day.end.bridges.Digest@";
jtulach@268
   546
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   547
        
jtulach@268
   548
        File out = createFile(3, "out.txt", "");
jtulach@268
   549
        out.delete();
jtulach@268
   550
 
jtulach@268
   551
        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
jtulach@268
   552
    }
jtulach@268
   553
    
jtulach@268
   554
    public void testNotClosedSection() throws Exception {
jtulach@268
   555
        String c1 =
jtulach@268
   556
            "package ahoj;\n" +
jtulach@268
   557
            "// BEGIN: clazz\n" +
jtulach@268
   558
            "int x;\n" +
jtulach@268
   559
            "\n";
jtulach@268
   560
        File src = createFile(1, "I.java", c1);
jtulach@268
   561
        
jtulach@268
   562
        
jtulach@268
   563
        String c2 =
jtulach@268
   564
            "@clazz@";
jtulach@268
   565
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   566
        
jtulach@268
   567
        File out = createFile(3, "out.txt", "");
jtulach@268
   568
        out.delete();
jtulach@268
   569
        
jtulach@268
   570
        try {
jtulach@268
   571
            execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
jtulach@268
   572
            fail("Has to fail");
jtulach@268
   573
        } catch (ExecuteUtils.ExecutionError ex) {
jtulach@268
   574
            // ok
jtulach@268
   575
        }
jtulach@268
   576
    }
jtulach@268
   577
    public void testLongNotLineDetectedAsNotInTheList() throws Exception {
jtulach@268
   578
        String c1 =
jtulach@268
   579
"package org.apidesign.api.security;\n" +
jtulach@268
   580
"" +
jtulach@268
   581
"import java.nio.ByteBuffer;\n" +
jtulach@268
   582
"import java.util.ServiceLoader;\n" +
jtulach@268
   583
"import org.apidesign.spi.security.Digestor;\n" +
jtulach@268
   584
"\n" +
jtulach@268
   585
"/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
jtulach@268
   586
" * for buffer of data.\n" +
jtulach@268
   587
" *\n" +
jtulach@268
   588
" * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
jtulach@268
   589
" */\n" +
jtulach@268
   590
"   DigestImplementation<?> impl    =      DigestImplementation.create(digestor, algorithm);\n" +
jtulach@268
   591
"// BEGIN: day.end.bridges.Digest\n" +
jtulach@268
   592
"   DigestImplementation<?> impl    =      null\n" +
jtulach@268
   593
"// END: day.end.bridges.Digest\n";
jtulach@268
   594
        
jtulach@268
   595
        File src = createFile(1, "C.java", c1);
jtulach@268
   596
        
jtulach@268
   597
        
jtulach@268
   598
        String c2 =
jtulach@268
   599
            "@day.end.bridges.Digest@";
jtulach@268
   600
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   601
        
jtulach@268
   602
        File out = createFile(3, "out.txt", "");
jtulach@268
   603
        out.delete();
jtulach@268
   604
 
jtulach@268
   605
        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
jtulach@268
   606
    }
jtulach@268
   607
    
jtulach@268
   608
    protected final File createFile(int slot, String name, String content) throws Exception {
jtulach@268
   609
        File d1 = new File(getWorkDir(), "dir" + slot);
jtulach@268
   610
        File c1 = new File(d1, name);
jtulach@268
   611
        copy(content, c1);
jtulach@268
   612
        return c1;
jtulach@268
   613
    }
jtulach@268
   614
    
jtulach@268
   615
    protected final void execute(int slotFirst, int slotSecond, String... additionalArgs) throws Exception {
jtulach@268
   616
        File d1 = new File(getWorkDir(), "dir" + slotFirst);
jtulach@268
   617
        File d2 = new File(getWorkDir(), "dir" + slotSecond);
jtulach@268
   618
        
jtulach@268
   619
        File build = new File(getWorkDir(), "build.xml");
jtulach@268
   620
        extractResource("color.xml", build);
jtulach@268
   621
        
jtulach@268
   622
        List<String> args = new ArrayList<String>();
jtulach@268
   623
        args.addAll(Arrays.asList(additionalArgs));
jtulach@268
   624
        args.add("-Ddir1=" + d1);
jtulach@268
   625
        args.add("-Ddir2=" + d2);
jtulach@268
   626
        ExecuteUtils.execute(build, args.toArray(new String[0]));
jtulach@268
   627
    }
jtulach@268
   628
    
jtulach@268
   629
    private static final void copy(String txt, File f) throws Exception {
jtulach@268
   630
        f.getParentFile().mkdirs();
jtulach@268
   631
        FileWriter w = new FileWriter(f);
jtulach@268
   632
        w.append(txt);
jtulach@268
   633
        w.close();
jtulach@268
   634
    }
jtulach@268
   635
jtulach@268
   636
    final File extractResource(String res, File f) throws Exception {
jtulach@268
   637
        URL u = ColorifyTest.class.getResource(res);
jtulach@268
   638
        assertNotNull ("Resource should be found " + res, u);
jtulach@268
   639
        
jtulach@268
   640
        FileOutputStream os = new FileOutputStream(f);
jtulach@268
   641
        InputStream is = u.openStream();
jtulach@268
   642
        for (;;) {
jtulach@268
   643
            int ch = is.read ();
jtulach@268
   644
            if (ch == -1) {
jtulach@268
   645
                break;
jtulach@268
   646
            }
jtulach@268
   647
            os.write (ch);
jtulach@268
   648
        }
jtulach@268
   649
        os.close ();
jtulach@268
   650
            
jtulach@268
   651
        return f;
jtulach@268
   652
    }
jtulach@268
   653
    
jtulach@268
   654
    final static String readFile (java.io.File f) throws java.io.IOException {
jtulach@268
   655
        int s = (int)f.length ();
jtulach@268
   656
        byte[] data = new byte[s];
jtulach@268
   657
        assertEquals ("Read all data", s, new java.io.FileInputStream (f).read (data));
jtulach@268
   658
        
jtulach@268
   659
        return new String (data);
jtulach@268
   660
    }
jtulach@268
   661
    
jtulach@268
   662
    final File extractString (String res, String nameExt) throws Exception {
jtulach@268
   663
        File f = new File(getWorkDir(), nameExt);
jtulach@268
   664
        f.deleteOnExit ();
jtulach@268
   665
        
jtulach@268
   666
        FileOutputStream os = new FileOutputStream(f);
jtulach@268
   667
        InputStream is = new ByteArrayInputStream(res.getBytes("UTF-8"));
jtulach@268
   668
        for (;;) {
jtulach@268
   669
            int ch = is.read ();
jtulach@268
   670
            if (ch == -1) {
jtulach@268
   671
                break;
jtulach@268
   672
            }
jtulach@268
   673
            os.write (ch);
jtulach@268
   674
        }
jtulach@268
   675
        os.close ();
jtulach@268
   676
            
jtulach@268
   677
        return f;
jtulach@268
   678
    }
jtulach@268
   679
    
jtulach@268
   680
}