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