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