java/ant/test/org/apidesign/infra/ant/ColorifyTest.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 ColorifyTest extends NbTestCase {
    45     private static File workDir;
    46     
    47     public ColorifyTest(String s) {
    48         super(s);
    49     }
    50     
    51     public static Test suite() {
    52         return new NbTestSuite(ColorifyTest.class);
    53 //        return new ColorifyTest("testInXMLWithMultilineString");
    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 testTryFinally() throws Exception {
    89         String c1 =
    90             "package ahoj;\n" +
    91             "// BEGIN: xyz\n" +
    92             "  try {\n" +
    93             "    // something\n" +
    94             "  } finally {\n" +
    95             "    // else\n" +
    96             "  }\n" +
    97             "// END: xyz\n" +
    98             "  public void get();\n" +
    99             "}" +
   100             "";
   101         File src = createFile(1, "I.java", c1);
   102         
   103         
   104         String c2 =
   105             "@xyz@";
   106         File txt = createFile(2, "in.txt", c2);
   107         
   108         File out = createFile(3, "out.txt", "");
   109         out.delete();
   110         
   111         execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
   112         
   113         assertTrue("output created", out.canRead());
   114         
   115         String r = readFile(out);
   116         String exp =
   117             "try {\n" +
   118             "  // something\n" +
   119             "} finally {\n" +
   120             "  // else\n" +
   121             "}\n";
   122         assertEquals(exp, r);
   123     }
   124     
   125     public void testString() throws Exception {
   126         String c1 =
   127             "package ahoj;\n" +
   128             "public interface I {\n" +
   129             "// BEGIN: xyz\n" +
   130             "  public static final String X = \"ahoj\"\n" +
   131             "// END: xyz\n" +
   132             "}" +
   133             "";
   134         File src = createFile(1, "I.java", c1);
   135         
   136         
   137         String c2 =
   138             "@xyz@";
   139         File txt = createFile(2, "in.txt", c2);
   140         
   141         File out = createFile(3, "out.txt", "");
   142         out.delete();
   143         
   144         execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
   145         
   146         assertTrue("output created", out.canRead());
   147         
   148         String r = readFile(out);
   149         assertEquals("public static final String X = \"ahoj\"\n", r);
   150     }
   151     
   152     public void testSpacesAtBeginingAreStripped() throws Exception {
   153         String c1 =
   154             "package ahoj;\n" +
   155             "// BEGIN: xyz\n" +
   156             "   public interface I {\n" +
   157             "     public void ahoj ();\n" +
   158             "   }\n" +
   159             "// END: xyz\n" +
   160             "  public void get();\n" +
   161             "}" +
   162             "";
   163         File src = createFile(1, "I.java", c1);
   164         
   165         
   166         String c2 =
   167             "@xyz@";
   168         File txt = createFile(2, "in.txt", c2);
   169         
   170         File out = createFile(3, "out.txt", "");
   171         out.delete();
   172         
   173         execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
   174         
   175         assertTrue("output created", out.canRead());
   176         
   177         String r = readFile(out);
   178         String result = "public interface I {\n" +
   179             "  public void ahoj ();\n" +
   180             "}\n";
   181         assertEquals(result, r);
   182     }
   183         
   184     public void testIncludedTexts() throws Exception {
   185         String c1 =
   186             "package ahoj;\n" +
   187             "// BEGIN: clazz\n" +
   188             "public interface I {\n" +
   189             "  // BEGIN: method\n" +
   190             "  public void get();\n" +
   191             "  // END: method\n" +
   192             "}\n" +
   193             "// END: clazz\n" +
   194             "";
   195         File src = createFile(1, "I.java", c1);
   196         
   197         
   198         String c2 =
   199             "@clazz@";
   200         File txt = createFile(2, "in.txt", c2);
   201         
   202         File out = createFile(3, "out.txt", "");
   203         out.delete();
   204         
   205         execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
   206         
   207         assertTrue("output created", out.canRead());
   208         
   209         String r = readFile(out);
   210         if (r.indexOf("BEGIN") >= 0) {
   211             fail("BEGIN is there: " + r);
   212         }
   213         if (r.indexOf("END") >= 0) {
   214             fail("END is there: " + r);
   215         }
   216         if (r.indexOf("interface I") < 0) {
   217             fail("Missing interface: " + r);
   218         }
   219         if (r.indexOf("void get()") < 0) {
   220             fail("Missing get: " + r);
   221         }
   222     }
   223     public void testWithCommentsTexts() throws Exception {
   224         String c1 =
   225             "package ahoj;\n" +
   226             "// BEGIN: clazz\n" +
   227             "/** Beautiful interface I\n" +
   228             " */\n" +
   229             "public interface I {\n" +
   230             "  // single line\n" +
   231             "}\n" +
   232             "// END: clazz\n" +
   233             "";
   234         File src = createFile(1, "I.java", c1);
   235         
   236         
   237         String c2 =
   238             "@clazz@";
   239         File txt = createFile(2, "in.txt", c2);
   240         
   241         File out = createFile(3, "out.txt", "");
   242         out.delete();
   243         
   244         execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
   245         
   246         assertTrue("output created", out.canRead());
   247         
   248         String r = readFile(out);
   249         if (r.indexOf("BEGIN") >= 0) {
   250             fail("BEGIN is there: " + r);
   251         }
   252         if (r.indexOf("END") >= 0) {
   253             fail("END is there: " + r);
   254         }
   255         if (r.indexOf("interface I") < 0) {
   256             fail("Missing interface: " + r);
   257         }
   258         if (r.indexOf("// single line\n}") < 0) {
   259             fail("Comment indentified: " + r);
   260         }
   261     }
   262     public void testMultiLineComment() throws Exception {
   263         String c1 =
   264             "package ahoj;\n" +
   265             "// BEGIN: clazz\n" +
   266             "public void x() {\n" +
   267             "  /** Beautiful interface I\n" +
   268             "  *\n" +
   269             "  */\n" +
   270             "}\n" +
   271             "// END: clazz\n" +
   272             "";
   273         File src = createFile(1, "I.java", c1);
   274         
   275         
   276         String c2 =
   277             "@clazz@";
   278         File txt = createFile(2, "in.txt", c2);
   279         
   280         File out = createFile(3, "out.txt", "");
   281         out.delete();
   282         
   283         execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
   284         
   285         assertTrue("output created", out.canRead());
   286         
   287         String r = readFile(out);
   288         int f = r.indexOf(
   289             "/** Beautiful interface I\n" +
   290             "  *\n" +
   291             "  */\n");
   292         if (f == -1) {
   293             fail("Not found: " + r);
   294         }
   295     }
   296     public void testIncludedTextsAmper() throws Exception {
   297         String c1 =
   298             "package ahoj;\n" +
   299             "public class C {\n" +
   300             "  // BEGIN: method\n" +
   301             "  public void change(int x) { x &= 10; }\n" +
   302             "  // END: method\n" +
   303             "}\n" +
   304             "";
   305         File src = createFile(1, "C.java", c1);
   306         
   307         
   308         String c2 =
   309             "@method@";
   310         File txt = createFile(2, "in.txt", c2);
   311         
   312         File out = createFile(3, "out.txt", "");
   313         out.delete();
   314         
   315         execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
   316         
   317         assertTrue("output created", out.canRead());
   318         
   319         String r = readFile(out);
   320         if (r.indexOf("&=") < 0) {
   321             fail("No XML escapes please: " + r);
   322         }
   323         if (r.indexOf("&amp;=") >= 0) {
   324             fail("we need no xml escapes, no &amp;: " + r);
   325         }
   326     }
   327     public void testIncludedTextsAmperAndGenerics() throws Exception {
   328         String c1 =
   329 
   330 "package org.apidesign.api.security;\n" +
   331 "" +
   332 "import java.nio.ByteBuffer;\n" +
   333 "import java.util.ServiceLoader;\n" +
   334 "import org.apidesign.spi.security.Digestor;\n" +
   335 "\n" +
   336 "/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
   337 " * for buffer of data.\n" +
   338 " *\n" +
   339 " * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
   340 " */\n" +
   341 "// BEGIN: day.end.bridges.Digest\n" +
   342 "public final class Digest {\n" +
   343 "    private final DigestImplementation<?> impl;\n" +
   344 "    \n" +
   345 "    /** Factory method is better than constructor */\n" +
   346 "    private Digest(DigestImplementation<?> impl) {\n" +
   347 "        this.impl = impl;\n" +
   348 "    }\n" +
   349 "    \n" +
   350 "    /** Factory method to create digest for an algorithm.\n" +
   351 "     */\n" +
   352 "    public static Digest getInstance(String algorithm) {\n" +
   353 "        for (Digestor<?> digestor : ServiceLoader.load(Digestor.class)) {\n" +
   354 "            DigestImplementation<?> impl = \n" +
   355                 "DigestImplementation.create(digestor, algorithm);\n" +
   356 "            if (impl != null) {\n" +
   357 "                return new Digest(impl);\n" +
   358 "            }\n" +
   359 "        }\n" +
   360 "        throw new IllegalArgumentException(algorithm);\n" +
   361 "    }\n" +
   362 "      \n" +
   363 "    //\n" +
   364 "    // these methods are kept the same as in original MessageDigest,\n" +
   365 "    // but for simplicity choose just some from the original API\n" +
   366 "    //\n" +
   367 "    \n" +
   368 "    public byte[] digest(ByteBuffer bb) {\n" +
   369 "        return impl.digest(bb);\n" +
   370 "    }\n" +
   371 "}\n" +
   372 "// END: day.end.bridges.Digest\n";
   373         
   374         File src = createFile(1, "C.java", c1);
   375         
   376         
   377         String c2 =
   378             "@day.end.bridges.Digest@";
   379         File txt = createFile(2, "in.txt", c2);
   380         
   381         File out = createFile(3, "out.txt", "");
   382         out.delete();
   383         
   384         execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
   385         
   386         assertTrue("output created", out.canRead());
   387         
   388         String r = readFile(out);
   389         if (r.indexOf("&=") >= 0) {
   390             fail("Wrong XML: " + r);
   391         }
   392         if (r.indexOf("&amp;") > -1) {
   393             fail("Wrong XML, no &amp;: " + r);
   394         }
   395     }
   396     
   397     public void testInXML() throws Exception {
   398         String c1 =
   399             "<!-- BEGIN: clazz -->\n" +
   400             "<interface name='I'/>\n" +
   401             "<!-- END: clazz -->\n" +
   402             "";
   403         File src = createFile(1, "I.xml", c1);
   404         
   405         
   406         String c2 =
   407             "@clazz@";
   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=*.xml");
   414         
   415         assertTrue("output created", out.canRead());
   416         
   417         String r = readFile(out);
   418         if (r.indexOf("BEGIN") >= 0) {
   419             fail("BEGIN is there: " + r);
   420         }
   421         if (r.indexOf("END") >= 0) {
   422             fail("END is there: " + r);
   423         }
   424         assertEquals("<interface name='I'/>\n", r);
   425     }
   426     public void testInXMLWithComment() throws Exception {
   427         String c1 =
   428             "<!-- BEGIN: clazz -->\n" +
   429             "<!--\n" +
   430             " ahoj -->\n" +
   431             "<!-- END: clazz -->\n" +
   432             "";
   433         File src = createFile(1, "I.xml", c1);
   434         
   435         
   436         String c2 =
   437             "@clazz@";
   438         File txt = createFile(2, "in.txt", c2);
   439         
   440         File out = createFile(3, "out.txt", "");
   441         out.delete();
   442         
   443         execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.xml");
   444         
   445         assertTrue("output created", out.canRead());
   446         
   447         String r = readFile(out);
   448         if (r.indexOf("BEGIN") >= 0) {
   449             fail("BEGIN is there: " + r);
   450         }
   451         if (r.indexOf("END") >= 0) {
   452             fail("END is there: " + r);
   453         }
   454         assertEquals("No xml please", "<!--\n ahoj -->\n", r);
   455     }
   456     
   457     public void testInXMLWithMultilineString() throws Exception {
   458         String c1 =
   459             "<!-- BEGIN: clazz -->\n" +
   460             "<element tag='\n" +
   461             " ahoj'\n" +
   462             "/>\n" +
   463             "<!-- END: clazz -->\n" +
   464             "";
   465         File src = createFile(1, "I.xml", c1);
   466         
   467         
   468         String c2 =
   469             "@clazz@";
   470         File txt = createFile(2, "in.txt", c2);
   471         
   472         File out = createFile(3, "out.txt", "");
   473         out.delete();
   474         
   475         execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.xml");
   476         
   477         assertTrue("output created", out.canRead());
   478         
   479         String r = readFile(out);
   480         if (r.indexOf("BEGIN") >= 0) {
   481             fail("BEGIN is there: " + r);
   482         }
   483         if (r.indexOf("END") >= 0) {
   484             fail("END is there: " + r);
   485         }
   486         assertEquals("I want no XML escapes", "<element tag='\n ahoj'\n/>\n", r);
   487     }
   488     
   489     public void testLongLineDetected() throws Exception {
   490         String c1 =
   491 "package org.apidesign.api.security;\n" +
   492 "" +
   493 "import java.nio.ByteBuffer;\n" +
   494 "import java.util.ServiceLoader;\n" +
   495 "import org.apidesign.spi.security.Digestor;\n" +
   496 "\n" +
   497 "/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
   498 " * for buffer of data.\n" +
   499 " *\n" +
   500 " * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
   501 " */\n" +
   502 "// BEGIN: day.end.bridges.Digest\n" +
   503 "d;   DigestImplementation<?> impl = DigestImplementation.create(digestor, algorithm);\n" +
   504 "// END: day.end.bridges.Digest\n";
   505         
   506         File src = createFile(1, "C.java", c1);
   507         
   508         
   509         String c2 =
   510             "@day.end.bridges.Digest@";
   511         File txt = createFile(2, "in.txt", c2);
   512         
   513         File out = createFile(3, "out.txt", "");
   514         out.delete();
   515  
   516         try {
   517             execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
   518         } catch (ExecuteUtils.ExecutionError ex) {
   519             // OK
   520             return;
   521         }
   522         fail("Should fail, as there is long line");
   523     }
   524     public void testLongNotLineDetectedAsShortened() throws Exception {
   525         String c1 =
   526 "package org.apidesign.api.security;\n" +
   527 "" +
   528 "import java.nio.ByteBuffer;\n" +
   529 "import java.util.ServiceLoader;\n" +
   530 "import org.apidesign.spi.security.Digestor;\n" +
   531 "\n" +
   532 "/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
   533 " * for buffer of data.\n" +
   534 " *\n" +
   535 " * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
   536 " */\n" +
   537 "// BEGIN: day.end.bridges.Digest\n" +
   538 "   DigestImplementation<?> impl = DigestImplementation.create(digestor, algorithm);\n" +
   539 "// END: day.end.bridges.Digest\n";
   540         
   541         File src = createFile(1, "C.java", c1);
   542         
   543         
   544         String c2 =
   545             "@day.end.bridges.Digest@";
   546         File txt = createFile(2, "in.txt", c2);
   547         
   548         File out = createFile(3, "out.txt", "");
   549         out.delete();
   550  
   551         execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
   552     }
   553     
   554     public void testNotClosedSection() throws Exception {
   555         String c1 =
   556             "package ahoj;\n" +
   557             "// BEGIN: clazz\n" +
   558             "int x;\n" +
   559             "\n";
   560         File src = createFile(1, "I.java", c1);
   561         
   562         
   563         String c2 =
   564             "@clazz@";
   565         File txt = createFile(2, "in.txt", c2);
   566         
   567         File out = createFile(3, "out.txt", "");
   568         out.delete();
   569         
   570         try {
   571             execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
   572             fail("Has to fail");
   573         } catch (ExecuteUtils.ExecutionError ex) {
   574             // ok
   575         }
   576     }
   577     public void testLongNotLineDetectedAsNotInTheList() throws Exception {
   578         String c1 =
   579 "package org.apidesign.api.security;\n" +
   580 "" +
   581 "import java.nio.ByteBuffer;\n" +
   582 "import java.util.ServiceLoader;\n" +
   583 "import org.apidesign.spi.security.Digestor;\n" +
   584 "\n" +
   585 "/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
   586 " * for buffer of data.\n" +
   587 " *\n" +
   588 " * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
   589 " */\n" +
   590 "   DigestImplementation<?> impl    =      DigestImplementation.create(digestor, algorithm);\n" +
   591 "// BEGIN: day.end.bridges.Digest\n" +
   592 "   DigestImplementation<?> impl    =      null\n" +
   593 "// END: day.end.bridges.Digest\n";
   594         
   595         File src = createFile(1, "C.java", c1);
   596         
   597         
   598         String c2 =
   599             "@day.end.bridges.Digest@";
   600         File txt = createFile(2, "in.txt", c2);
   601         
   602         File out = createFile(3, "out.txt", "");
   603         out.delete();
   604  
   605         execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
   606     }
   607     
   608     protected final File createFile(int slot, String name, String content) throws Exception {
   609         File d1 = new File(getWorkDir(), "dir" + slot);
   610         File c1 = new File(d1, name);
   611         copy(content, c1);
   612         return c1;
   613     }
   614     
   615     protected final void execute(int slotFirst, int slotSecond, String... additionalArgs) throws Exception {
   616         File d1 = new File(getWorkDir(), "dir" + slotFirst);
   617         File d2 = new File(getWorkDir(), "dir" + slotSecond);
   618         
   619         File build = new File(getWorkDir(), "build.xml");
   620         extractResource("color.xml", build);
   621         
   622         List<String> args = new ArrayList<String>();
   623         args.addAll(Arrays.asList(additionalArgs));
   624         args.add("-Ddir1=" + d1);
   625         args.add("-Ddir2=" + d2);
   626         ExecuteUtils.execute(build, args.toArray(new String[0]));
   627     }
   628     
   629     private static final void copy(String txt, File f) throws Exception {
   630         f.getParentFile().mkdirs();
   631         FileWriter w = new FileWriter(f);
   632         w.append(txt);
   633         w.close();
   634     }
   635 
   636     final File extractResource(String res, File f) throws Exception {
   637         URL u = ColorifyTest.class.getResource(res);
   638         assertNotNull ("Resource should be found " + res, u);
   639         
   640         FileOutputStream os = new FileOutputStream(f);
   641         InputStream is = u.openStream();
   642         for (;;) {
   643             int ch = is.read ();
   644             if (ch == -1) {
   645                 break;
   646             }
   647             os.write (ch);
   648         }
   649         os.close ();
   650             
   651         return f;
   652     }
   653     
   654     final static String readFile (java.io.File f) throws java.io.IOException {
   655         int s = (int)f.length ();
   656         byte[] data = new byte[s];
   657         assertEquals ("Read all data", s, new java.io.FileInputStream (f).read (data));
   658         
   659         return new String (data);
   660     }
   661     
   662     final File extractString (String res, String nameExt) throws Exception {
   663         File f = new File(getWorkDir(), nameExt);
   664         f.deleteOnExit ();
   665         
   666         FileOutputStream os = new FileOutputStream(f);
   667         InputStream is = new ByteArrayInputStream(res.getBytes("UTF-8"));
   668         for (;;) {
   669             int ch = is.read ();
   670             if (ch == -1) {
   671                 break;
   672             }
   673             os.write (ch);
   674         }
   675         os.close ();
   676             
   677         return f;
   678     }
   679     
   680 }