java/ant/test/org/apidesign/infra/ant/GrepTest.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 GrepTest extends NbTestCase {
jtulach@268
    45
    private static File workDir;
jtulach@268
    46
    
jtulach@268
    47
    public GrepTest(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(GrepTest.class);
jtulach@268
    53
        //return new GrepTest("testInXML");
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 testSpacesAtBeginingAreStripped() throws Exception {
jtulach@268
    89
        String c1 =
jtulach@268
    90
            "package ahoj;\n" +
jtulach@268
    91
            "// BEGIN: xyz\n" +
jtulach@268
    92
            "   public interface I {\n" +
jtulach@268
    93
            "     public void ahoj();\n" +
jtulach@268
    94
            "   }\n" +
jtulach@268
    95
            "// END: xyz\n" +
jtulach@268
    96
            "  public void get();\n" +
jtulach@268
    97
            "}" +
jtulach@268
    98
            "";
jtulach@268
    99
        File src = createFile(1, "I.java", c1);
jtulach@268
   100
        
jtulach@268
   101
        
jtulach@268
   102
        String c2 =
jtulach@268
   103
            "@xyz@";
jtulach@268
   104
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   105
        
jtulach@268
   106
        File out = createFile(3, "out.txt", "");
jtulach@268
   107
        out.delete();
jtulach@268
   108
        
jtulach@268
   109
        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
jtulach@268
   110
        
jtulach@268
   111
        assertTrue("output created", out.canRead());
jtulach@268
   112
        
jtulach@268
   113
        String r = readFile(out);
jtulach@268
   114
        String result = "public interface I {\n" +
jtulach@268
   115
            "  public void ahoj();\n" +
jtulach@268
   116
            "}\n";
jtulach@268
   117
        assertEquals(result, r);
jtulach@268
   118
    }
jtulach@268
   119
    
jtulach@268
   120
    public void testReportUnpairedBracesAsError() throws Exception {
jtulach@268
   121
        String c1 =
jtulach@268
   122
            "package ahoj;\n" +
jtulach@268
   123
            "// BEGIN: xyz\n" +
jtulach@268
   124
            "   public interface I {\n" +
jtulach@268
   125
            "     public void ahoj();\n" +
jtulach@268
   126
            "// END: xyz\n" +
jtulach@268
   127
            "   }\n" +
jtulach@268
   128
            "  public void get();\n" +
jtulach@268
   129
            "}" +
jtulach@268
   130
            "";
jtulach@268
   131
        File src = createFile(1, "I.java", c1);
jtulach@268
   132
        
jtulach@268
   133
        
jtulach@268
   134
        String c2 =
jtulach@268
   135
            "@xyz@";
jtulach@268
   136
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   137
        
jtulach@268
   138
        File out = createFile(3, "out.txt", "");
jtulach@268
   139
        out.delete();
jtulach@268
   140
        
jtulach@268
   141
        try {
jtulach@268
   142
            execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
jtulach@268
   143
        } catch (ExecuteUtils.ExecutionError ex) {
jtulach@268
   144
            // ok
jtulach@268
   145
            return;
jtulach@268
   146
        }
jtulach@268
   147
        fail("The execution of the script shall fail");
jtulach@268
   148
    }
jtulach@268
   149
        
jtulach@268
   150
    public void testIncludedTexts() throws Exception {
jtulach@268
   151
        String c1 =
jtulach@268
   152
            "package ahoj;\n" +
jtulach@268
   153
            "// BEGIN: clazz\n" +
jtulach@268
   154
            "public interface I {\n" +
jtulach@268
   155
            "  // BEGIN: method\n" +
jtulach@268
   156
            "  public void get();\n" +
jtulach@268
   157
            "  // END: method\n" +
jtulach@268
   158
            "}\n" +
jtulach@268
   159
            "// END: clazz\n" +
jtulach@268
   160
            "";
jtulach@268
   161
        File src = createFile(1, "I.java", c1);
jtulach@268
   162
        
jtulach@268
   163
        
jtulach@268
   164
        String c2 =
jtulach@268
   165
            "@clazz@";
jtulach@268
   166
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   167
        
jtulach@268
   168
        File out = createFile(3, "out.txt", "");
jtulach@268
   169
        out.delete();
jtulach@268
   170
        
jtulach@268
   171
        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
jtulach@268
   172
        
jtulach@268
   173
        assertTrue("output created", out.canRead());
jtulach@268
   174
        
jtulach@268
   175
        String r = readFile(out);
jtulach@268
   176
        if (r.indexOf("BEGIN") >= 0) {
jtulach@268
   177
            fail("BEGIN is there: " + r);
jtulach@268
   178
        }
jtulach@268
   179
        if (r.indexOf("END") >= 0) {
jtulach@268
   180
            fail("END is there: " + r);
jtulach@268
   181
        }
jtulach@268
   182
        if (r.indexOf("interface I") < 0) {
jtulach@268
   183
            fail("Missing interface: " + r);
jtulach@268
   184
        }
jtulach@268
   185
        if (r.indexOf("void get()") < 0) {
jtulach@268
   186
            fail("Missing get: " + r);
jtulach@268
   187
        }
jtulach@268
   188
    }
jtulach@268
   189
    public void testIncludedTextsAmper() throws Exception {
jtulach@268
   190
        String c1 =
jtulach@268
   191
            "package ahoj;\n" +
jtulach@268
   192
            "public class C {\n" +
jtulach@268
   193
            "  // BEGIN: method\n" +
jtulach@268
   194
            "  public void change(int x) { x &= 10; }\n" +
jtulach@268
   195
            "  // END: method\n" +
jtulach@268
   196
            "}\n" +
jtulach@268
   197
            "";
jtulach@268
   198
        File src = createFile(1, "C.java", c1);
jtulach@268
   199
        
jtulach@268
   200
        
jtulach@268
   201
        String c2 =
jtulach@268
   202
            "@method@";
jtulach@268
   203
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   204
        
jtulach@268
   205
        File out = createFile(3, "out.txt", "");
jtulach@268
   206
        out.delete();
jtulach@268
   207
        
jtulach@268
   208
        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
jtulach@268
   209
        
jtulach@268
   210
        assertTrue("output created", out.canRead());
jtulach@268
   211
        
jtulach@268
   212
        String r = readFile(out);
jtulach@268
   213
        if (r.indexOf("&=") < 0) {
jtulach@268
   214
            fail("No XML: " + r);
jtulach@268
   215
        }
jtulach@268
   216
        if (r.indexOf("&amp;=") >= 0) {
jtulach@268
   217
            fail("No XML, we need &amp;: " + r);
jtulach@268
   218
        }
jtulach@268
   219
    }
jtulach@268
   220
    public void testIncludedTextsAmperAndGenerics() throws Exception {
jtulach@268
   221
        String c1 =
jtulach@268
   222
jtulach@268
   223
"package org.apidesign.api.security;\n" +
jtulach@268
   224
"" +
jtulach@268
   225
"import java.nio.ByteBuffer;\n" +
jtulach@268
   226
"import java.util.ServiceLoader;\n" +
jtulach@268
   227
"import org.apidesign.spi.security.Digestor;\n" +
jtulach@268
   228
"\n" +
jtulach@268
   229
"/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
jtulach@268
   230
" * for buffer of data.\n" +
jtulach@268
   231
" *\n" +
jtulach@268
   232
" * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
jtulach@268
   233
" */\n" +
jtulach@268
   234
"// BEGIN: day.end.bridges.Digest\n" +
jtulach@268
   235
"public final class Digest {\n" +
jtulach@268
   236
"    private final DigestImplementation<?> impl;\n" +
jtulach@268
   237
"    \n" +
jtulach@268
   238
"    /** Factory method is better than constructor */\n" +
jtulach@268
   239
"    private Digest(DigestImplementation<?> impl) {\n" +
jtulach@268
   240
"        this.impl = impl;\n" +
jtulach@268
   241
"    }\n" +
jtulach@268
   242
"    \n" +
jtulach@268
   243
"    /** Factory method to create digest for an algorithm.\n" +
jtulach@268
   244
"     */\n" +
jtulach@268
   245
"    public static Digest getInstance(String algorithm) {\n" +
jtulach@268
   246
"        for (Digestor<?> digestor : ServiceLoader.load(Digestor.class)) {\n" +
jtulach@268
   247
"            DigestImplementation<?> impl = \n" +
jtulach@268
   248
                "DigestImplementation.create(digestor, algorithm);\n" +
jtulach@268
   249
"            if (impl != null) {\n" +
jtulach@268
   250
"                return new Digest(impl);\n" +
jtulach@268
   251
"            }\n" +
jtulach@268
   252
"        }\n" +
jtulach@268
   253
"        throw new IllegalArgumentException(algorithm);\n" +
jtulach@268
   254
"    }\n" +
jtulach@268
   255
"      \n" +
jtulach@268
   256
"    //\n" +
jtulach@268
   257
"    // these methods are kept the same as in original MessageDigest,\n" +
jtulach@268
   258
"    // but for simplicity choose just some from the original API\n" +
jtulach@268
   259
"    //\n" +
jtulach@268
   260
"    \n" +
jtulach@268
   261
"    public byte[] digest(ByteBuffer bb) {\n" +
jtulach@268
   262
"        return impl.digest(bb);\n" +
jtulach@268
   263
"    }\n" +
jtulach@268
   264
"}\n" +
jtulach@268
   265
"// END: day.end.bridges.Digest\n";
jtulach@268
   266
        
jtulach@268
   267
        File src = createFile(1, "C.java", c1);
jtulach@268
   268
        
jtulach@268
   269
        
jtulach@268
   270
        String c2 =
jtulach@268
   271
            "@day.end.bridges.Digest@";
jtulach@268
   272
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   273
        
jtulach@268
   274
        File out = createFile(3, "out.txt", "");
jtulach@268
   275
        out.delete();
jtulach@268
   276
        
jtulach@268
   277
        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
jtulach@268
   278
        
jtulach@268
   279
        assertTrue("output created", out.canRead());
jtulach@268
   280
        
jtulach@268
   281
        String r = readFile(out);
jtulach@268
   282
        if (r.indexOf("&=") >= 0) {
jtulach@268
   283
            fail("Wrong XML: " + r);
jtulach@268
   284
        }
jtulach@268
   285
        if (r.indexOf("&amp;") > -1) {
jtulach@268
   286
            fail("Wrong XML, no &amp;: " + r);
jtulach@268
   287
        }
jtulach@268
   288
    }
jtulach@268
   289
    
jtulach@268
   290
    public void testInXML() throws Exception {
jtulach@268
   291
        String c1 =
jtulach@268
   292
            "<!-- BEGIN: clazz -->\n" +
jtulach@268
   293
            "<interface name='I'/>\n" +
jtulach@268
   294
            "<!-- END: clazz -->\n" +
jtulach@268
   295
            "";
jtulach@268
   296
        File src = createFile(1, "I.xml", c1);
jtulach@268
   297
        
jtulach@268
   298
        
jtulach@268
   299
        String c2 =
jtulach@268
   300
            "@clazz@";
jtulach@268
   301
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   302
        
jtulach@268
   303
        File out = createFile(3, "out.txt", "");
jtulach@268
   304
        out.delete();
jtulach@268
   305
        
jtulach@268
   306
        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.xml");
jtulach@268
   307
        
jtulach@268
   308
        assertTrue("output created", out.canRead());
jtulach@268
   309
        
jtulach@268
   310
        String r = readFile(out);
jtulach@268
   311
        if (r.indexOf("BEGIN") >= 0) {
jtulach@268
   312
            fail("BEGIN is there: " + r);
jtulach@268
   313
        }
jtulach@268
   314
        if (r.indexOf("END") >= 0) {
jtulach@268
   315
            fail("END is there: " + r);
jtulach@268
   316
        }
jtulach@268
   317
        if (r.indexOf("<interface name='I'/>") < 0) {
jtulach@268
   318
            fail("Missing interface: " + r);
jtulach@268
   319
        }
jtulach@268
   320
    }
jtulach@268
   321
    
jtulach@268
   322
    public void testLongLineNotDetectedAsBeginsWithGen() throws Exception {
jtulach@268
   323
        String c1 =
jtulach@268
   324
"package org.apidesign.api.security;\n" +
jtulach@268
   325
"" +
jtulach@268
   326
"import java.nio.ByteBuffer;\n" +
jtulach@268
   327
"import java.util.ServiceLoader;\n" +
jtulach@268
   328
"import org.apidesign.spi.security.Digestor;\n" +
jtulach@268
   329
"\n" +
jtulach@268
   330
"/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
jtulach@268
   331
"// BEGIN: x\n" +
jtulach@268
   332
" * for buffer of data.\n" +
jtulach@268
   333
"// END: x\n" +
jtulach@268
   334
" *\n" +
jtulach@268
   335
" * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
jtulach@268
   336
" */\n" +
jtulach@268
   337
"// GEN-BEGIN: day.end.bridges.Digest\n" +
jtulach@268
   338
"d;   DigestImplementation<?> impl = DigestImplementation.create(digestor, algorithm);\n" +
jtulach@268
   339
"// GEN-END: day.end.bridges.Digest\n";
jtulach@268
   340
        
jtulach@268
   341
        File src = createFile(1, "C.java", c1);
jtulach@268
   342
        
jtulach@268
   343
        
jtulach@268
   344
        String c2 =
jtulach@268
   345
            "@day.end.bridges.Digest@";
jtulach@268
   346
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   347
        
jtulach@268
   348
        File out = createFile(3, "out.txt", "");
jtulach@268
   349
        out.delete();
jtulach@268
   350
 
jtulach@268
   351
        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
jtulach@268
   352
    }
jtulach@268
   353
    
jtulach@268
   354
    
jtulach@268
   355
    public void testLongLineDetected() throws Exception {
jtulach@268
   356
        String c1 =
jtulach@268
   357
"package org.apidesign.api.security;\n" +
jtulach@268
   358
"" +
jtulach@268
   359
"import java.nio.ByteBuffer;\n" +
jtulach@268
   360
"import java.util.ServiceLoader;\n" +
jtulach@268
   361
"import org.apidesign.spi.security.Digestor;\n" +
jtulach@268
   362
"\n" +
jtulach@268
   363
"/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
jtulach@268
   364
" * for buffer of data.\n" +
jtulach@268
   365
" *\n" +
jtulach@268
   366
" * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
jtulach@268
   367
" */\n" +
jtulach@268
   368
"// BEGIN: day.end.bridges.Digest\n" +
jtulach@268
   369
"d;   DigestImplementation<?> impl = DigestImplementation.create(digestor, algorithm);\n" +
jtulach@268
   370
"// END: day.end.bridges.Digest\n";
jtulach@268
   371
        
jtulach@268
   372
        File src = createFile(1, "C.java", c1);
jtulach@268
   373
        
jtulach@268
   374
        
jtulach@268
   375
        String c2 =
jtulach@268
   376
            "@day.end.bridges.Digest@";
jtulach@268
   377
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   378
        
jtulach@268
   379
        File out = createFile(3, "out.txt", "");
jtulach@268
   380
        out.delete();
jtulach@268
   381
 
jtulach@268
   382
        try {
jtulach@268
   383
            execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
jtulach@268
   384
        } catch (ExecuteUtils.ExecutionError ex) {
jtulach@268
   385
            // OK
jtulach@268
   386
            return;
jtulach@268
   387
        }
jtulach@268
   388
        fail("Should fail, as there is long line");
jtulach@268
   389
    }
jtulach@268
   390
    public void testLongNotLineDetectedAsShortened() throws Exception {
jtulach@268
   391
        String c1 =
jtulach@268
   392
"package org.apidesign.api.security;\n" +
jtulach@268
   393
"" +
jtulach@268
   394
"import java.nio.ByteBuffer;\n" +
jtulach@268
   395
"import java.util.ServiceLoader;\n" +
jtulach@268
   396
"import org.apidesign.spi.security.Digestor;\n" +
jtulach@268
   397
"\n" +
jtulach@268
   398
"/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
jtulach@268
   399
" * for buffer of data.\n" +
jtulach@268
   400
" *\n" +
jtulach@268
   401
" * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
jtulach@268
   402
" */\n" +
jtulach@268
   403
"// BEGIN: day.end.bridges.Digest\n" +
jtulach@268
   404
"   DigestImplementation<?> impl = DigestImplementation.create(digestor, algorithm);\n" +
jtulach@268
   405
"// END: day.end.bridges.Digest\n";
jtulach@268
   406
        
jtulach@268
   407
        File src = createFile(1, "C.java", c1);
jtulach@268
   408
        
jtulach@268
   409
        
jtulach@268
   410
        String c2 =
jtulach@268
   411
            "@day.end.bridges.Digest@";
jtulach@268
   412
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   413
        
jtulach@268
   414
        File out = createFile(3, "out.txt", "");
jtulach@268
   415
        out.delete();
jtulach@268
   416
 
jtulach@268
   417
        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
jtulach@268
   418
    }
jtulach@268
   419
    
jtulach@268
   420
    public void testNotClosedSection() throws Exception {
jtulach@268
   421
        String c1 =
jtulach@268
   422
            "package ahoj;\n" +
jtulach@268
   423
            "// BEGIN: clazz\n" +
jtulach@268
   424
            "int x;\n" +
jtulach@268
   425
            "\n";
jtulach@268
   426
        File src = createFile(1, "I.java", c1);
jtulach@268
   427
        
jtulach@268
   428
        
jtulach@268
   429
        String c2 =
jtulach@268
   430
            "@clazz@";
jtulach@268
   431
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   432
        
jtulach@268
   433
        File out = createFile(3, "out.txt", "");
jtulach@268
   434
        out.delete();
jtulach@268
   435
        
jtulach@268
   436
        try {
jtulach@268
   437
            execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
jtulach@268
   438
            fail("Has to fail");
jtulach@268
   439
        } catch (ExecuteUtils.ExecutionError ex) {
jtulach@268
   440
            // ok
jtulach@268
   441
        }
jtulach@268
   442
    }
jtulach@268
   443
    public void testLongNotLineDetectedAsNotInTheList() throws Exception {
jtulach@268
   444
        String c1 =
jtulach@268
   445
"package org.apidesign.api.security;\n" +
jtulach@268
   446
"" +
jtulach@268
   447
"import java.nio.ByteBuffer;\n" +
jtulach@268
   448
"import java.util.ServiceLoader;\n" +
jtulach@268
   449
"import org.apidesign.spi.security.Digestor;\n" +
jtulach@268
   450
"\n" +
jtulach@268
   451
"/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
jtulach@268
   452
" * for buffer of data.\n" +
jtulach@268
   453
" *\n" +
jtulach@268
   454
" * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
jtulach@268
   455
" */\n" +
jtulach@268
   456
"   DigestImplementation<?> impl    =      DigestImplementation.create(digestor, algorithm);\n" +
jtulach@268
   457
"// BEGIN: day.end.bridges.Digest\n" +
jtulach@268
   458
"   DigestImplementation<?> impl    =      null\n" +
jtulach@268
   459
"// END: day.end.bridges.Digest\n";
jtulach@268
   460
        
jtulach@268
   461
        File src = createFile(1, "C.java", c1);
jtulach@268
   462
        
jtulach@268
   463
        
jtulach@268
   464
        String c2 =
jtulach@268
   465
            "@day.end.bridges.Digest@";
jtulach@268
   466
        File txt = createFile(2, "in.txt", c2);
jtulach@268
   467
        
jtulach@268
   468
        File out = createFile(3, "out.txt", "");
jtulach@268
   469
        out.delete();
jtulach@268
   470
 
jtulach@268
   471
        execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
jtulach@268
   472
    }
jtulach@268
   473
    
jtulach@268
   474
    protected final File createFile(int slot, String name, String content) throws Exception {
jtulach@268
   475
        File d1 = new File(getWorkDir(), "dir" + slot);
jtulach@268
   476
        File c1 = new File(d1, name);
jtulach@268
   477
        copy(content, c1);
jtulach@268
   478
        return c1;
jtulach@268
   479
    }
jtulach@268
   480
    
jtulach@268
   481
    protected final void execute(int slotFirst, int slotSecond, String... additionalArgs) throws Exception {
jtulach@268
   482
        File d1 = new File(getWorkDir(), "dir" + slotFirst);
jtulach@268
   483
        File d2 = new File(getWorkDir(), "dir" + slotSecond);
jtulach@268
   484
        
jtulach@268
   485
        File build = new File(getWorkDir(), "build.xml");
jtulach@268
   486
        extractResource("build.xml", build);
jtulach@268
   487
        
jtulach@268
   488
        List<String> args = new ArrayList<String>();
jtulach@268
   489
        args.addAll(Arrays.asList(additionalArgs));
jtulach@268
   490
        args.add("-Ddir1=" + d1);
jtulach@268
   491
        args.add("-Ddir2=" + d2);
jtulach@268
   492
        ExecuteUtils.execute(build, args.toArray(new String[0]));
jtulach@268
   493
    }
jtulach@268
   494
    
jtulach@268
   495
    private static final void copy(String txt, File f) throws Exception {
jtulach@268
   496
        f.getParentFile().mkdirs();
jtulach@268
   497
        FileWriter w = new FileWriter(f);
jtulach@268
   498
        w.append(txt);
jtulach@268
   499
        w.close();
jtulach@268
   500
    }
jtulach@268
   501
jtulach@268
   502
    final File extractResource(String res, File f) throws Exception {
jtulach@268
   503
        URL u = GrepTest.class.getResource(res);
jtulach@268
   504
        assertNotNull ("Resource should be found " + res, u);
jtulach@268
   505
        
jtulach@268
   506
        FileOutputStream os = new FileOutputStream(f);
jtulach@268
   507
        InputStream is = u.openStream();
jtulach@268
   508
        for (;;) {
jtulach@268
   509
            int ch = is.read ();
jtulach@268
   510
            if (ch == -1) {
jtulach@268
   511
                break;
jtulach@268
   512
            }
jtulach@268
   513
            os.write (ch);
jtulach@268
   514
        }
jtulach@268
   515
        os.close ();
jtulach@268
   516
            
jtulach@268
   517
        return f;
jtulach@268
   518
    }
jtulach@268
   519
    
jtulach@268
   520
    final static String readFile (java.io.File f) throws java.io.IOException {
jtulach@268
   521
        int s = (int)f.length ();
jtulach@268
   522
        byte[] data = new byte[s];
jtulach@268
   523
        assertEquals ("Read all data", s, new java.io.FileInputStream (f).read (data));
jtulach@268
   524
        
jtulach@268
   525
        return new String (data);
jtulach@268
   526
    }
jtulach@268
   527
    
jtulach@268
   528
    final File extractString (String res, String nameExt) throws Exception {
jtulach@268
   529
        File f = new File(getWorkDir(), nameExt);
jtulach@268
   530
        f.deleteOnExit ();
jtulach@268
   531
        
jtulach@268
   532
        FileOutputStream os = new FileOutputStream(f);
jtulach@268
   533
        InputStream is = new ByteArrayInputStream(res.getBytes("UTF-8"));
jtulach@268
   534
        for (;;) {
jtulach@268
   535
            int ch = is.read ();
jtulach@268
   536
            if (ch == -1) {
jtulach@268
   537
                break;
jtulach@268
   538
            }
jtulach@268
   539
            os.write (ch);
jtulach@268
   540
        }
jtulach@268
   541
        os.close ();
jtulach@268
   542
            
jtulach@268
   543
        return f;
jtulach@268
   544
    }
jtulach@268
   545
    
jtulach@268
   546
}