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