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