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 testSpacesAtBeginingAreStripped() throws Exception {
88 " public interface I {\n" +
89 " public void ahoj();\n" +
92 " public void get();\n" +
95 File src = createFile(1, "I.java", c1);
100 File txt = createFile(2, "in.txt", c2);
102 execute(1, 2, "-Dfile1=" + txt, "-Dinclude1=*.java");
104 String r = readFile("xyz");
105 String result = "public interface I {\n" +
106 " public void ahoj();\n" +
108 assertEquals(result, r);
111 public void testReportUnpairedBracesAsError() throws Exception {
115 " public interface I {\n" +
116 " public void ahoj();\n" +
119 " public void get();\n" +
122 File src = createFile(1, "I.java", c1);
127 File txt = createFile(2, "in.txt", c2);
129 File out = createFile(3, "out.txt", "");
133 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
134 } catch (ExecuteUtils.ExecutionError ex) {
138 fail("The execution of the script shall fail");
141 public void testIncludedTexts() throws Exception {
144 "// BEGIN: clazz\n" +
145 "public interface I {\n" +
146 " // BEGIN: method\n" +
147 " public void get();\n" +
148 " // END: method\n" +
152 File src = createFile(1, "I.java", c1);
157 File txt = createFile(2, "in.txt", c2);
159 execute(1, 2, "-Dfile1=" + txt, "-Dinclude1=*.java");
161 String r = readFile("clazz");
162 if (r.indexOf("BEGIN") >= 0) {
163 fail("BEGIN is there: " + r);
165 if (r.indexOf("END") >= 0) {
166 fail("END is there: " + r);
168 if (r.indexOf("interface I") < 0) {
169 fail("Missing interface: " + r);
171 if (r.indexOf("void get()") < 0) {
172 fail("Missing get: " + r);
175 public void testIncludedTextsAmper() throws Exception {
178 "public class C {\n" +
179 " // BEGIN: method\n" +
180 " public void change(int x) { x &= 10; }\n" +
181 " // END: method\n" +
184 File src = createFile(1, "C.java", c1);
189 File txt = createFile(2, "in.txt", c2);
191 execute(1, 2, "-Dfile1=" + txt, "-Dinclude1=*.java");
193 String r = readFile("method");
194 if (r.indexOf("&=") < 0) {
195 fail("No XML: " + r);
197 if (r.indexOf("&=") >= 0) {
198 fail("No XML, we need &: " + r);
201 public void testIncludedTextsAmperAndGenerics() throws Exception {
204 "package org.apidesign.api.security;\n" +
206 "import java.nio.ByteBuffer;\n" +
207 "import java.util.ServiceLoader;\n" +
208 "import org.apidesign.spi.security.Digestor;\n" +
210 "/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
211 " * for buffer of data.\n" +
213 " * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
215 "// BEGIN: day.end.bridges.Digest\n" +
216 "public final class Digest {\n" +
217 " private final DigestImplementation<?> impl;\n" +
219 " /** Factory method is better than constructor */\n" +
220 " private Digest(DigestImplementation<?> impl) {\n" +
221 " this.impl = impl;\n" +
224 " /** Factory method to create digest for an algorithm.\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" +
234 " throw new IllegalArgumentException(algorithm);\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" +
242 " public byte[] digest(ByteBuffer bb) {\n" +
243 " return impl.digest(bb);\n" +
246 "// END: day.end.bridges.Digest\n";
248 File src = createFile(1, "C.java", c1);
252 "@day.end.bridges.Digest@";
253 File txt = createFile(2, "in.txt", c2);
255 execute(1, 2, "-Dfile1=" + txt, "-Dinclude1=*.java");
257 String r = readFile("day.end.bridges.Digest");
258 if (r.indexOf("&=") >= 0) {
259 fail("Wrong XML: " + r);
261 if (r.indexOf("&") > -1) {
262 fail("Wrong XML, no &: " + r);
266 public void testInXML() throws Exception {
268 "<!-- BEGIN: clazz -->\n" +
269 "<interface name='I'/>\n" +
270 "<!-- END: clazz -->\n" +
272 File src = createFile(1, "I.xml", c1);
277 File txt = createFile(2, "in.txt", c2);
279 execute(1, 2, "-Dfile1=" + txt, "-Dinclude1=*.xml");
281 String r = readFile("clazz");
282 if (r.indexOf("BEGIN") >= 0) {
283 fail("BEGIN is there: " + r);
285 if (r.indexOf("END") >= 0) {
286 fail("END is there: " + r);
288 if (r.indexOf("<interface name='I'/>") < 0) {
289 fail("Missing interface: " + r);
293 public void testLongLineNotDetectedAsBeginsWithGen() throws Exception {
295 "package org.apidesign.api.security;\n" +
297 "import java.nio.ByteBuffer;\n" +
298 "import java.util.ServiceLoader;\n" +
299 "import org.apidesign.spi.security.Digestor;\n" +
301 "/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
303 " * for buffer of data.\n" +
306 " * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\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";
312 File src = createFile(1, "C.java", c1);
316 "@day.end.bridges.Digest@";
317 File txt = createFile(2, "in.txt", c2);
319 File out = createFile(3, "out.txt", "");
322 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
326 public void testLongLineDetected() throws Exception {
328 "package org.apidesign.api.security;\n" +
330 "import java.nio.ByteBuffer;\n" +
331 "import java.util.ServiceLoader;\n" +
332 "import org.apidesign.spi.security.Digestor;\n" +
334 "/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
335 " * for buffer of data.\n" +
337 " * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
339 "// BEGIN: day.end.bridges.Digest\n" +
340 "d; DigestImplementation<?> impl = DigestImplementation.create(digestor, algorithm);\n" +
341 "// END: day.end.bridges.Digest\n";
343 File src = createFile(1, "C.java", c1);
347 "@day.end.bridges.Digest@";
348 File txt = createFile(2, "in.txt", c2);
350 File out = createFile(3, "out.txt", "");
354 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
355 } catch (ExecuteUtils.ExecutionError ex) {
359 fail("Should fail, as there is long line");
361 public void testLongNotLineDetectedAsShortened() throws Exception {
363 "package org.apidesign.api.security;\n" +
365 "import java.nio.ByteBuffer;\n" +
366 "import java.util.ServiceLoader;\n" +
367 "import org.apidesign.spi.security.Digestor;\n" +
369 "/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
370 " * for buffer of data.\n" +
372 " * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\n" +
374 "// BEGIN: day.end.bridges.Digest\n" +
375 " DigestImplementation<?> impl = DigestImplementation.create(digestor, algorithm);\n" +
376 "// END: day.end.bridges.Digest\n";
378 File src = createFile(1, "C.java", c1);
382 "@day.end.bridges.Digest@";
383 File txt = createFile(2, "in.txt", c2);
385 File out = createFile(3, "out.txt", "");
388 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
391 public void testNotClosedSection() throws Exception {
394 "// BEGIN: clazz\n" +
397 File src = createFile(1, "I.java", c1);
402 File txt = createFile(2, "in.txt", c2);
404 File out = createFile(3, "out.txt", "");
408 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
410 } catch (ExecuteUtils.ExecutionError ex) {
414 public void testLongNotLineDetectedAsNotInTheList() throws Exception {
416 "package org.apidesign.api.security;\n" +
418 "import java.nio.ByteBuffer;\n" +
419 "import java.util.ServiceLoader;\n" +
420 "import org.apidesign.spi.security.Digestor;\n" +
422 "/** Simplified version of a Digest class that allows to compute a fingerprint\n" +
423 " * for buffer of data.\n" +
425 " * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>\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";
432 File src = createFile(1, "C.java", c1);
436 "@day.end.bridges.Digest@";
437 File txt = createFile(2, "in.txt", c2);
439 File out = createFile(3, "out.txt", "");
442 execute(1, 2, "-Dfile1=" + txt, "-Dfile2=" + out, "-Dinclude1=*.java");
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);
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");
457 File build = new File(getWorkDir(), "build.xml");
458 extractResource("copy.xml", build);
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]));
468 private static final void copy(String txt, File f) throws Exception {
469 f.getParentFile().mkdirs();
470 FileWriter w = new FileWriter(f);
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);
479 FileOutputStream os = new FileOutputStream(f);
480 InputStream is = u.openStream();
493 final String readFile (String name) throws java.io.IOException {
494 File f = new File(new File(getWorkDir(), "out"), name);
496 throw new FileNotFoundException(f.getPath());
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));
503 return new String (data);
506 final File extractString (String res, String nameExt) throws Exception {
507 File f = new File(getWorkDir(), nameExt);
510 FileOutputStream os = new FileOutputStream(f);
511 InputStream is = new ByteArrayInputStream(res.getBytes("UTF-8"));