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 ColorifyTest extends NbTestCase {
45 private static File workDir;
47 public ColorifyTest(String s) {
51 public static Test suite() {
52 return new NbTestSuite(ColorifyTest.class);
53 // return new ColorifyTest("testInXMLWithMultilineString");
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 testTryFinally() throws Exception {
98 " public void get();\n" +
101 File src = createFile(1, "I.java", c1);
106 File txt = createFile(2, "in.txt", c2);
108 File out = createFile(3, "out.txt", "");
111 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
113 assertTrue("output created", out.canRead());
115 String r = readFile(out);
122 assertEquals(exp, r);
125 public void testString() throws Exception {
128 "public interface I {\n" +
130 " public static final String X = \"ahoj\"\n" +
134 File src = createFile(1, "I.java", c1);
139 File txt = createFile(2, "in.txt", c2);
141 File out = createFile(3, "out.txt", "");
144 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
146 assertTrue("output created", out.canRead());
148 String r = readFile(out);
149 assertEquals("public static final String X = \"ahoj\"\n", r);
152 public void testSpacesAtBeginingAreStripped() throws Exception {
156 " public interface I {\n" +
157 " public void ahoj ();\n" +
160 " public void get();\n" +
163 File src = createFile(1, "I.java", c1);
168 File txt = createFile(2, "in.txt", c2);
170 File out = createFile(3, "out.txt", "");
173 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
175 assertTrue("output created", out.canRead());
177 String r = readFile(out);
178 String result = "public interface I {\n" +
179 " public void ahoj ();\n" +
181 assertEquals(result, r);
184 public void testIncludedTexts() throws Exception {
187 "// BEGIN: clazz\n" +
188 "public interface I {\n" +
189 " // BEGIN: method\n" +
190 " public void get();\n" +
191 " // END: method\n" +
195 File src = createFile(1, "I.java", c1);
200 File txt = createFile(2, "in.txt", c2);
202 File out = createFile(3, "out.txt", "");
205 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
207 assertTrue("output created", out.canRead());
209 String r = readFile(out);
210 if (r.indexOf("BEGIN") >= 0) {
211 fail("BEGIN is there: " + r);
213 if (r.indexOf("END") >= 0) {
214 fail("END is there: " + r);
216 if (r.indexOf("interface I") < 0) {
217 fail("Missing interface: " + r);
219 if (r.indexOf("void get()") < 0) {
220 fail("Missing get: " + r);
223 public void testWithCommentsTexts() throws Exception {
226 "// BEGIN: clazz\n" +
227 "/** Beautiful interface I\n" +
229 "public interface I {\n" +
230 " // single line\n" +
234 File src = createFile(1, "I.java", c1);
239 File txt = createFile(2, "in.txt", c2);
241 File out = createFile(3, "out.txt", "");
244 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
246 assertTrue("output created", out.canRead());
248 String r = readFile(out);
249 if (r.indexOf("BEGIN") >= 0) {
250 fail("BEGIN is there: " + r);
252 if (r.indexOf("END") >= 0) {
253 fail("END is there: " + r);
255 if (r.indexOf("interface I") < 0) {
256 fail("Missing interface: " + r);
258 if (r.indexOf("// single line\n}") < 0) {
259 fail("Comment indentified: " + r);
262 public void testMultiLineComment() throws Exception {
265 "// BEGIN: clazz\n" +
266 "public void x() {\n" +
267 " /** Beautiful interface I\n" +
273 File src = createFile(1, "I.java", c1);
278 File txt = createFile(2, "in.txt", c2);
280 File out = createFile(3, "out.txt", "");
283 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
285 assertTrue("output created", out.canRead());
287 String r = readFile(out);
289 "/** Beautiful interface I\n" +
293 fail("Not found: " + r);
296 public void testIncludedTextsAmper() throws Exception {
299 "public class C {\n" +
300 " // BEGIN: method\n" +
301 " public void change(int x) { x &= 10; }\n" +
302 " // END: method\n" +
305 File src = createFile(1, "C.java", c1);
310 File txt = createFile(2, "in.txt", c2);
312 File out = createFile(3, "out.txt", "");
315 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
317 assertTrue("output created", out.canRead());
319 String r = readFile(out);
320 if (r.indexOf("&=") < 0) {
321 fail("No XML escapes please: " + r);
323 if (r.indexOf("&=") >= 0) {
324 fail("we need no xml escapes, no &: " + r);
327 public void testIncludedTextsAmperAndGenerics() throws Exception {
330 "package org.apidesign.api.security;\n" +
332 "import java.nio.ByteBuffer;\n" +
333 "import java.util.ServiceLoader;\n" +
334 "import org.apidesign.spi.security.Digestor;\n" +
336 "/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
337 " * for buffer of data.\n" +
339 " * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
341 "// BEGIN: day.end.bridges.Digest\n" +
342 "public final class Digest {\n" +
343 " private final DigestImplementation<?> impl;\n" +
345 " /** Factory method is better than constructor */\n" +
346 " private Digest(DigestImplementation<?> impl) {\n" +
347 " this.impl = impl;\n" +
350 " /** Factory method to create digest for an algorithm.\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" +
360 " throw new IllegalArgumentException(algorithm);\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" +
368 " public byte[] digest(ByteBuffer bb) {\n" +
369 " return impl.digest(bb);\n" +
372 "// END: day.end.bridges.Digest\n";
374 File src = createFile(1, "C.java", c1);
378 "@day.end.bridges.Digest@";
379 File txt = createFile(2, "in.txt", c2);
381 File out = createFile(3, "out.txt", "");
384 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
386 assertTrue("output created", out.canRead());
388 String r = readFile(out);
389 if (r.indexOf("&=") >= 0) {
390 fail("Wrong XML: " + r);
392 if (r.indexOf("&") > -1) {
393 fail("Wrong XML, no &: " + r);
397 public void testInXML() throws Exception {
399 "<!-- BEGIN: clazz -->\n" +
400 "<interface name='I'/>\n" +
401 "<!-- END: clazz -->\n" +
403 File src = createFile(1, "I.xml", c1);
408 File txt = createFile(2, "in.txt", c2);
410 File out = createFile(3, "out.txt", "");
413 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.xml");
415 assertTrue("output created", out.canRead());
417 String r = readFile(out);
418 if (r.indexOf("BEGIN") >= 0) {
419 fail("BEGIN is there: " + r);
421 if (r.indexOf("END") >= 0) {
422 fail("END is there: " + r);
424 assertEquals("<interface name='I'/>\n", r);
426 public void testInXMLWithComment() throws Exception {
428 "<!-- BEGIN: clazz -->\n" +
431 "<!-- END: clazz -->\n" +
433 File src = createFile(1, "I.xml", c1);
438 File txt = createFile(2, "in.txt", c2);
440 File out = createFile(3, "out.txt", "");
443 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.xml");
445 assertTrue("output created", out.canRead());
447 String r = readFile(out);
448 if (r.indexOf("BEGIN") >= 0) {
449 fail("BEGIN is there: " + r);
451 if (r.indexOf("END") >= 0) {
452 fail("END is there: " + r);
454 assertEquals("No xml please", "<!--\n ahoj -->\n", r);
457 public void testInXMLWithMultilineString() throws Exception {
459 "<!-- BEGIN: clazz -->\n" +
463 "<!-- END: clazz -->\n" +
465 File src = createFile(1, "I.xml", c1);
470 File txt = createFile(2, "in.txt", c2);
472 File out = createFile(3, "out.txt", "");
475 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.xml");
477 assertTrue("output created", out.canRead());
479 String r = readFile(out);
480 if (r.indexOf("BEGIN") >= 0) {
481 fail("BEGIN is there: " + r);
483 if (r.indexOf("END") >= 0) {
484 fail("END is there: " + r);
486 assertEquals("I want no XML escapes", "<element tag='\n ahoj'\n/>\n", r);
489 public void testLongLineDetected() throws Exception {
491 "package org.apidesign.api.security;\n" +
493 "import java.nio.ByteBuffer;\n" +
494 "import java.util.ServiceLoader;\n" +
495 "import org.apidesign.spi.security.Digestor;\n" +
497 "/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
498 " * for buffer of data.\n" +
500 " * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
502 "// BEGIN: day.end.bridges.Digest\n" +
503 "d; DigestImplementation<?> impl = DigestImplementation.create(digestor, algorithm);\n" +
504 "// END: day.end.bridges.Digest\n";
506 File src = createFile(1, "C.java", c1);
510 "@day.end.bridges.Digest@";
511 File txt = createFile(2, "in.txt", c2);
513 File out = createFile(3, "out.txt", "");
517 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
518 } catch (ExecuteUtils.ExecutionError ex) {
522 fail("Should fail, as there is long line");
524 public void testLongNotLineDetectedAsShortened() throws Exception {
526 "package org.apidesign.api.security;\n" +
528 "import java.nio.ByteBuffer;\n" +
529 "import java.util.ServiceLoader;\n" +
530 "import org.apidesign.spi.security.Digestor;\n" +
532 "/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
533 " * for buffer of data.\n" +
535 " * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
537 "// BEGIN: day.end.bridges.Digest\n" +
538 " DigestImplementation<?> impl = DigestImplementation.create(digestor, algorithm);\n" +
539 "// END: day.end.bridges.Digest\n";
541 File src = createFile(1, "C.java", c1);
545 "@day.end.bridges.Digest@";
546 File txt = createFile(2, "in.txt", c2);
548 File out = createFile(3, "out.txt", "");
551 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
554 public void testNotClosedSection() throws Exception {
557 "// BEGIN: clazz\n" +
560 File src = createFile(1, "I.java", c1);
565 File txt = createFile(2, "in.txt", c2);
567 File out = createFile(3, "out.txt", "");
571 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
573 } catch (ExecuteUtils.ExecutionError ex) {
577 public void testLongNotLineDetectedAsNotInTheList() throws Exception {
579 "package org.apidesign.api.security;\n" +
581 "import java.nio.ByteBuffer;\n" +
582 "import java.util.ServiceLoader;\n" +
583 "import org.apidesign.spi.security.Digestor;\n" +
585 "/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
586 " * for buffer of data.\n" +
588 " * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\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";
595 File src = createFile(1, "C.java", c1);
599 "@day.end.bridges.Digest@";
600 File txt = createFile(2, "in.txt", c2);
602 File out = createFile(3, "out.txt", "");
605 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
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);
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);
619 File build = new File(getWorkDir(), "build.xml");
620 extractResource("color.xml", build);
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]));
629 private static final void copy(String txt, File f) throws Exception {
630 f.getParentFile().mkdirs();
631 FileWriter w = new FileWriter(f);
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);
640 FileOutputStream os = new FileOutputStream(f);
641 InputStream is = u.openStream();
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));
659 return new String (data);
662 final File extractString (String res, String nameExt) throws Exception {
663 File f = new File(getWorkDir(), nameExt);
666 FileOutputStream os = new FileOutputStream(f);
667 InputStream is = new ByteArrayInputStream(res.getBytes("UTF-8"));