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.FileNotFoundException;
30 import java.io.FileOutputStream;
31 import java.io.FileWriter;
32 import java.io.InputStream;
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;
43 * @author Jaroslav Tulach
45 public class GrepCopyTest extends NbTestCase {
46 private static File workDir;
48 public GrepCopyTest(String s) {
52 public static Test suite() {
53 return new NbTestSuite(GrepCopyTest.class);
54 //return new GrepCopyTest("testInXML");
58 protected void setUp() throws Exception {
62 public void testMissingMethodInAnInterfaceIsDetected() throws Exception {
66 "public interface I {\n" +
68 " public void get();\n" +
71 File src = createFile(1, "I.java", c1);
76 File txt = createFile(2, "in.txt", c2);
78 execute(1, 2, "-Dfile1=" + txt, "-Dinclude1=*.java");
80 String r = readFile("xyz");
81 assertEquals("public interface I {\n}\n", r);
84 public void testURLGenerated() throws Exception {
88 "public interface I {\n" +
90 " public void get();\n" +
93 File src = createFile(1, "I.java", c1);
98 File txt = createFile(2, "in.txt", c2);
100 execute(1, 2, "-Dfile1=" + txt, "-Dinclude1=*.java", "-Dout.url=http://xyz/", "url");
102 String r = readFile("xyz");
103 assertEquals("public interface I {\n}\n", r);
105 String u = readFile("xyz.url");
106 assertEquals("http://xyz/I.java", u);
109 public void testSpacesAtBeginingAreStripped() throws Exception {
113 " public interface I {\n" +
114 " public void ahoj();\n" +
117 " public void get();\n" +
120 File src = createFile(1, "I.java", c1);
125 File txt = createFile(2, "in.txt", c2);
127 execute(1, 2, "-Dfile1=" + txt, "-Dinclude1=*.java");
129 String r = readFile("xyz");
130 String result = "public interface I {\n" +
131 " public void ahoj();\n" +
133 assertEquals(result, r);
136 public void testReportUnpairedBracesAsError() throws Exception {
140 " public interface I {\n" +
141 " public void ahoj();\n" +
144 " public void get();\n" +
147 File src = createFile(1, "I.java", c1);
152 File txt = createFile(2, "in.txt", c2);
154 File out = createFile(3, "out.txt", "");
158 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
159 } catch (ExecuteUtils.ExecutionError ex) {
163 fail("The execution of the script shall fail");
166 public void testIncludedTexts() throws Exception {
169 "// BEGIN: clazz\n" +
170 "public interface I {\n" +
171 " // BEGIN: method\n" +
172 " public void get();\n" +
173 " // END: method\n" +
177 File src = createFile(1, "I.java", c1);
182 File txt = createFile(2, "in.txt", c2);
184 execute(1, 2, "-Dfile1=" + txt, "-Dinclude1=*.java");
186 String r = readFile("clazz");
187 if (r.indexOf("BEGIN") >= 0) {
188 fail("BEGIN is there: " + r);
190 if (r.indexOf("END") >= 0) {
191 fail("END is there: " + r);
193 if (r.indexOf("interface I") < 0) {
194 fail("Missing interface: " + r);
196 if (r.indexOf("void get()") < 0) {
197 fail("Missing get: " + r);
200 public void testIncludedTextsAmper() throws Exception {
203 "public class C {\n" +
204 " // BEGIN: method\n" +
205 " public void change(int x) { x &= 10; }\n" +
206 " // END: method\n" +
209 File src = createFile(1, "C.java", c1);
214 File txt = createFile(2, "in.txt", c2);
216 execute(1, 2, "-Dfile1=" + txt, "-Dinclude1=*.java");
218 String r = readFile("method");
219 if (r.indexOf("&=") < 0) {
220 fail("No XML: " + r);
222 if (r.indexOf("&=") >= 0) {
223 fail("No XML, we need &: " + r);
226 public void testIncludedTextsAmperAndGenerics() throws Exception {
229 "package org.apidesign.api.security;\n" +
231 "import java.nio.ByteBuffer;\n" +
232 "import java.util.ServiceLoader;\n" +
233 "import org.apidesign.spi.security.Digestor;\n" +
235 "/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
236 " * for buffer of data.\n" +
238 " * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
240 "// BEGIN: day.end.bridges.Digest\n" +
241 "public final class Digest {\n" +
242 " private final DigestImplementation<?> impl;\n" +
244 " /** Factory method is better than constructor */\n" +
245 " private Digest(DigestImplementation<?> impl) {\n" +
246 " this.impl = impl;\n" +
249 " /** Factory method to create digest for an algorithm.\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" +
259 " throw new IllegalArgumentException(algorithm);\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" +
267 " public byte[] digest(ByteBuffer bb) {\n" +
268 " return impl.digest(bb);\n" +
271 "// END: day.end.bridges.Digest\n";
273 File src = createFile(1, "C.java", c1);
277 "@day.end.bridges.Digest@";
278 File txt = createFile(2, "in.txt", c2);
280 execute(1, 2, "-Dfile1=" + txt, "-Dinclude1=*.java");
282 String r = readFile("day.end.bridges.Digest");
283 if (r.indexOf("&=") >= 0) {
284 fail("Wrong XML: " + r);
286 if (r.indexOf("&") > -1) {
287 fail("Wrong XML, no &: " + r);
291 public void testInXML() throws Exception {
293 "<!-- BEGIN: clazz -->\n" +
294 "<interface name='I'/>\n" +
295 "<!-- END: clazz -->\n" +
297 File src = createFile(1, "I.xml", c1);
302 File txt = createFile(2, "in.txt", c2);
304 execute(1, 2, "-Dfile1=" + txt, "-Dinclude1=*.xml");
306 String r = readFile("clazz");
307 if (r.indexOf("BEGIN") >= 0) {
308 fail("BEGIN is there: " + r);
310 if (r.indexOf("END") >= 0) {
311 fail("END is there: " + r);
313 if (r.indexOf("<interface name='I'/>") < 0) {
314 fail("Missing interface: " + r);
318 public void testLongLineNotDetectedAsBeginsWithGen() throws Exception {
320 "package org.apidesign.api.security;\n" +
322 "import java.nio.ByteBuffer;\n" +
323 "import java.util.ServiceLoader;\n" +
324 "import org.apidesign.spi.security.Digestor;\n" +
326 "/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
328 " * for buffer of data.\n" +
331 " * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\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";
337 File src = createFile(1, "C.java", c1);
341 "@day.end.bridges.Digest@";
342 File txt = createFile(2, "in.txt", c2);
344 File out = createFile(3, "out.txt", "");
347 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
351 public void testLongLineDetected() throws Exception {
353 "package org.apidesign.api.security;\n" +
355 "import java.nio.ByteBuffer;\n" +
356 "import java.util.ServiceLoader;\n" +
357 "import org.apidesign.spi.security.Digestor;\n" +
359 "/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
360 " * for buffer of data.\n" +
362 " * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
364 "// BEGIN: day.end.bridges.Digest\n" +
365 "d; DigestImplementation<?> impl = DigestImplementation.create(digestor, algorithm);\n" +
366 "// END: day.end.bridges.Digest\n";
368 File src = createFile(1, "C.java", c1);
372 "@day.end.bridges.Digest@";
373 File txt = createFile(2, "in.txt", c2);
375 File out = createFile(3, "out.txt", "");
379 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
380 } catch (ExecuteUtils.ExecutionError ex) {
384 fail("Should fail, as there is long line");
386 public void testLongNotLineDetectedAsShortened() throws Exception {
388 "package org.apidesign.api.security;\n" +
390 "import java.nio.ByteBuffer;\n" +
391 "import java.util.ServiceLoader;\n" +
392 "import org.apidesign.spi.security.Digestor;\n" +
394 "/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
395 " * for buffer of data.\n" +
397 " * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
399 "// BEGIN: day.end.bridges.Digest\n" +
400 " DigestImplementation<?> impl = DigestImplementation.create(digestor, algorithm);\n" +
401 "// END: day.end.bridges.Digest\n";
403 File src = createFile(1, "C.java", c1);
407 "@day.end.bridges.Digest@";
408 File txt = createFile(2, "in.txt", c2);
410 File out = createFile(3, "out.txt", "");
413 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
416 public void testNotClosedSection() throws Exception {
419 "// BEGIN: clazz\n" +
422 File src = createFile(1, "I.java", c1);
427 File txt = createFile(2, "in.txt", c2);
429 File out = createFile(3, "out.txt", "");
433 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
435 } catch (ExecuteUtils.ExecutionError ex) {
439 public void testLongNotLineDetectedAsNotInTheList() throws Exception {
441 "package org.apidesign.api.security;\n" +
443 "import java.nio.ByteBuffer;\n" +
444 "import java.util.ServiceLoader;\n" +
445 "import org.apidesign.spi.security.Digestor;\n" +
447 "/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
448 " * for buffer of data.\n" +
450 " * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\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";
457 File src = createFile(1, "C.java", c1);
461 "@day.end.bridges.Digest@";
462 File txt = createFile(2, "in.txt", c2);
464 File out = createFile(3, "out.txt", "");
467 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
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);
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");
482 File build = new File(getWorkDir(), "build.xml");
483 extractResource("copy.xml", build);
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]));
493 private static final void copy(String txt, File f) throws Exception {
494 f.getParentFile().mkdirs();
495 FileWriter w = new FileWriter(f);
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);
504 FileOutputStream os = new FileOutputStream(f);
505 InputStream is = u.openStream();
518 final String readFile (String name) throws java.io.IOException {
519 File f = new File(new File(getWorkDir(), "out"), name);
521 throw new FileNotFoundException(f.getPath());
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));
528 return new String (data);
531 final File extractString (String res, String nameExt) throws Exception {
532 File f = new File(getWorkDir(), nameExt);
535 FileOutputStream os = new FileOutputStream(f);
536 InputStream is = new ByteArrayInputStream(res.getBytes("UTF-8"));