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