2 * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
25 package org.apidesign.infra.ant;
27 import java.io.ByteArrayInputStream;
29 import java.io.FileOutputStream;
30 import java.io.FileWriter;
31 import java.io.InputStream;
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;
42 * @author Jaroslav Tulach
44 public class GrepTest extends NbTestCase {
45 private static File workDir;
47 public GrepTest(String s) {
51 public static Test suite() {
52 return new NbTestSuite(GrepTest.class);
53 //return new GrepTest("testInXML");
57 protected void setUp() throws Exception {
61 public void testMissingMethodInAnInterfaceIsDetected() throws Exception {
65 "public interface I {\n" +
67 " public void get();\n" +
70 File src = createFile(1, "I.java", c1);
75 File txt = createFile(2, "in.txt", c2);
77 File out = createFile(3, "out.txt", "");
80 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
82 assertTrue("output created", out.canRead());
84 String r = readFile(out);
85 assertEquals("public interface I {\n}\n", r);
88 public void testSpacesAtBeginingAreStripped() throws Exception {
92 " public interface I {\n" +
93 " public void ahoj();\n" +
96 " public void get();\n" +
99 File src = createFile(1, "I.java", c1);
104 File txt = createFile(2, "in.txt", c2);
106 File out = createFile(3, "out.txt", "");
109 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
111 assertTrue("output created", out.canRead());
113 String r = readFile(out);
114 String result = "public interface I {\n" +
115 " public void ahoj();\n" +
117 assertEquals(result, r);
120 public void testReportUnpairedBracesAsError() throws Exception {
124 " public interface I {\n" +
125 " public void ahoj();\n" +
128 " public void get();\n" +
131 File src = createFile(1, "I.java", c1);
136 File txt = createFile(2, "in.txt", c2);
138 File out = createFile(3, "out.txt", "");
142 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
143 } catch (ExecuteUtils.ExecutionError ex) {
147 fail("The execution of the script shall fail");
150 public void testIncludedTexts() throws Exception {
153 "// BEGIN: clazz\n" +
154 "public interface I {\n" +
155 " // BEGIN: method\n" +
156 " public void get();\n" +
157 " // END: method\n" +
161 File src = createFile(1, "I.java", c1);
166 File txt = createFile(2, "in.txt", c2);
168 File out = createFile(3, "out.txt", "");
171 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
173 assertTrue("output created", out.canRead());
175 String r = readFile(out);
176 if (r.indexOf("BEGIN") >= 0) {
177 fail("BEGIN is there: " + r);
179 if (r.indexOf("END") >= 0) {
180 fail("END is there: " + r);
182 if (r.indexOf("interface I") < 0) {
183 fail("Missing interface: " + r);
185 if (r.indexOf("void get()") < 0) {
186 fail("Missing get: " + r);
189 public void testIncludedTextsAmper() throws Exception {
192 "public class C {\n" +
193 " // BEGIN: method\n" +
194 " public void change(int x) { x &= 10; }\n" +
195 " // END: method\n" +
198 File src = createFile(1, "C.java", c1);
203 File txt = createFile(2, "in.txt", c2);
205 File out = createFile(3, "out.txt", "");
208 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
210 assertTrue("output created", out.canRead());
212 String r = readFile(out);
213 if (r.indexOf("&=") < 0) {
214 fail("No XML: " + r);
216 if (r.indexOf("&=") >= 0) {
217 fail("No XML, we need &: " + r);
220 public void testIncludedTextsAmperAndGenerics() throws Exception {
223 "package org.apidesign.api.security;\n" +
225 "import java.nio.ByteBuffer;\n" +
226 "import java.util.ServiceLoader;\n" +
227 "import org.apidesign.spi.security.Digestor;\n" +
229 "/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
230 " * for buffer of data.\n" +
232 " * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
234 "// BEGIN: day.end.bridges.Digest\n" +
235 "public final class Digest {\n" +
236 " private final DigestImplementation<?> impl;\n" +
238 " /** Factory method is better than constructor */\n" +
239 " private Digest(DigestImplementation<?> impl) {\n" +
240 " this.impl = impl;\n" +
243 " /** Factory method to create digest for an algorithm.\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" +
253 " throw new IllegalArgumentException(algorithm);\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" +
261 " public byte[] digest(ByteBuffer bb) {\n" +
262 " return impl.digest(bb);\n" +
265 "// END: day.end.bridges.Digest\n";
267 File src = createFile(1, "C.java", c1);
271 "@day.end.bridges.Digest@";
272 File txt = createFile(2, "in.txt", c2);
274 File out = createFile(3, "out.txt", "");
277 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
279 assertTrue("output created", out.canRead());
281 String r = readFile(out);
282 if (r.indexOf("&=") >= 0) {
283 fail("Wrong XML: " + r);
285 if (r.indexOf("&") > -1) {
286 fail("Wrong XML, no &: " + r);
290 public void testInXML() throws Exception {
292 "<!-- BEGIN: clazz -->\n" +
293 "<interface name='I'/>\n" +
294 "<!-- END: clazz -->\n" +
296 File src = createFile(1, "I.xml", c1);
301 File txt = createFile(2, "in.txt", c2);
303 File out = createFile(3, "out.txt", "");
306 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.xml");
308 assertTrue("output created", out.canRead());
310 String r = readFile(out);
311 if (r.indexOf("BEGIN") >= 0) {
312 fail("BEGIN is there: " + r);
314 if (r.indexOf("END") >= 0) {
315 fail("END is there: " + r);
317 if (r.indexOf("<interface name='I'/>") < 0) {
318 fail("Missing interface: " + r);
322 public void testLongLineNotDetectedAsBeginsWithGen() throws Exception {
324 "package org.apidesign.api.security;\n" +
326 "import java.nio.ByteBuffer;\n" +
327 "import java.util.ServiceLoader;\n" +
328 "import org.apidesign.spi.security.Digestor;\n" +
330 "/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
332 " * for buffer of data.\n" +
335 " * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\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";
341 File src = createFile(1, "C.java", c1);
345 "@day.end.bridges.Digest@";
346 File txt = createFile(2, "in.txt", c2);
348 File out = createFile(3, "out.txt", "");
351 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
355 public void testLongLineDetected() throws Exception {
357 "package org.apidesign.api.security;\n" +
359 "import java.nio.ByteBuffer;\n" +
360 "import java.util.ServiceLoader;\n" +
361 "import org.apidesign.spi.security.Digestor;\n" +
363 "/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
364 " * for buffer of data.\n" +
366 " * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
368 "// BEGIN: day.end.bridges.Digest\n" +
369 "d; DigestImplementation<?> impl = DigestImplementation.create(digestor, algorithm);\n" +
370 "// END: day.end.bridges.Digest\n";
372 File src = createFile(1, "C.java", c1);
376 "@day.end.bridges.Digest@";
377 File txt = createFile(2, "in.txt", c2);
379 File out = createFile(3, "out.txt", "");
383 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
384 } catch (ExecuteUtils.ExecutionError ex) {
388 fail("Should fail, as there is long line");
390 public void testLongNotLineDetectedAsShortened() throws Exception {
392 "package org.apidesign.api.security;\n" +
394 "import java.nio.ByteBuffer;\n" +
395 "import java.util.ServiceLoader;\n" +
396 "import org.apidesign.spi.security.Digestor;\n" +
398 "/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
399 " * for buffer of data.\n" +
401 " * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
403 "// BEGIN: day.end.bridges.Digest\n" +
404 " DigestImplementation<?> impl = DigestImplementation.create(digestor, algorithm);\n" +
405 "// END: day.end.bridges.Digest\n";
407 File src = createFile(1, "C.java", c1);
411 "@day.end.bridges.Digest@";
412 File txt = createFile(2, "in.txt", c2);
414 File out = createFile(3, "out.txt", "");
417 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
420 public void testNotClosedSection() throws Exception {
423 "// BEGIN: clazz\n" +
426 File src = createFile(1, "I.java", c1);
431 File txt = createFile(2, "in.txt", c2);
433 File out = createFile(3, "out.txt", "");
437 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
439 } catch (ExecuteUtils.ExecutionError ex) {
443 public void testLongNotLineDetectedAsNotInTheList() throws Exception {
445 "package org.apidesign.api.security;\n" +
447 "import java.nio.ByteBuffer;\n" +
448 "import java.util.ServiceLoader;\n" +
449 "import org.apidesign.spi.security.Digestor;\n" +
451 "/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
452 " * for buffer of data.\n" +
454 " * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\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";
461 File src = createFile(1, "C.java", c1);
465 "@day.end.bridges.Digest@";
466 File txt = createFile(2, "in.txt", c2);
468 File out = createFile(3, "out.txt", "");
471 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
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);
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);
485 File build = new File(getWorkDir(), "build.xml");
486 extractResource("build.xml", build);
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]));
495 private static final void copy(String txt, File f) throws Exception {
496 f.getParentFile().mkdirs();
497 FileWriter w = new FileWriter(f);
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);
506 FileOutputStream os = new FileOutputStream(f);
507 InputStream is = u.openStream();
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));
525 return new String (data);
528 final File extractString (String res, String nameExt) throws Exception {
529 File f = new File(getWorkDir(), nameExt);
532 FileOutputStream os = new FileOutputStream(f);
533 InputStream is = new ByteArrayInputStream(res.getBytes("UTF-8"));