1.1 --- a/lookup/src/test/java/org/openide/util/test/AnnotationProcessorTestUtils.java Wed Jan 27 17:46:23 2010 -0500
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,139 +0,0 @@
1.4 -/*
1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 - *
1.7 - * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
1.8 - *
1.9 - * The contents of this file are subject to the terms of either the GNU
1.10 - * General Public License Version 2 only ("GPL") or the Common
1.11 - * Development and Distribution License("CDDL") (collectively, the
1.12 - * "License"). You may not use this file except in compliance with the
1.13 - * License. You can obtain a copy of the License at
1.14 - * http://www.netbeans.org/cddl-gplv2.html
1.15 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.16 - * specific language governing permissions and limitations under the
1.17 - * License. When distributing the software, include this License Header
1.18 - * Notice in each file and include the License file at
1.19 - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
1.20 - * particular file as subject to the "Classpath" exception as provided
1.21 - * by Sun in the GPL Version 2 section of the License file that
1.22 - * accompanied this code. If applicable, add the following below the
1.23 - * License Header, with the fields enclosed by brackets [] replaced by
1.24 - * your own identifying information:
1.25 - * "Portions Copyrighted [year] [name of copyright owner]"
1.26 - *
1.27 - * If you wish your version of this file to be governed by only the CDDL
1.28 - * or only the GPL Version 2, indicate your decision by adding
1.29 - * "[Contributor] elects to include this software in this distribution
1.30 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.31 - * single choice of license, a recipient has the option to distribute
1.32 - * your version of this file under either the CDDL, the GPL Version 2 or
1.33 - * to extend the choice of license to its licensees as provided above.
1.34 - * However, if you add GPL Version 2 code and therefore, elected the GPL
1.35 - * Version 2 license, then the option applies only if the new code is
1.36 - * made subject to such option by the copyright holder.
1.37 - *
1.38 - * Contributor(s):
1.39 - *
1.40 - * Portions Copyrighted 2008 Sun Microsystems, Inc.
1.41 - */
1.42 -
1.43 -package org.openide.util.test;
1.44 -
1.45 -import java.io.File;
1.46 -import java.io.FileWriter;
1.47 -import java.io.IOException;
1.48 -import java.io.OutputStream;
1.49 -import java.io.PrintWriter;
1.50 -import java.io.Writer;
1.51 -import java.util.ArrayList;
1.52 -import java.util.List;
1.53 -import java.util.regex.Pattern;
1.54 -import javax.tools.JavaCompiler;
1.55 -import javax.tools.ToolProvider;
1.56 -import junit.framework.Assert;
1.57 -
1.58 -/**
1.59 - * Utilities useful to those testing JSR 269 annotation processors.
1.60 - * <p>If you just want to test that the output of the processor is correct,
1.61 - * you do not need to do anything special:
1.62 - * just use the annotation on some sample classes nested inside your unit test.
1.63 - * They will be processed, and you check that your SPI loads them correctly.
1.64 - * These utilities are useful mainly in case you want to check that the processor
1.65 - * rejects erroneous sources, and that any messages it prints are reasonable;
1.66 - * that it behaves correctly on incremental compilations; etc.
1.67 - */
1.68 -public class AnnotationProcessorTestUtils {
1.69 -
1.70 - private AnnotationProcessorTestUtils() {}
1.71 -
1.72 - /**
1.73 - * Create a source file.
1.74 - * @param dir source root
1.75 - * @param clazz a fully-qualified class name
1.76 - * @param content lines of text (skip package decl)
1.77 - */
1.78 - public static void makeSource(File dir, String clazz, String... content) throws IOException {
1.79 - File f = new File(dir, clazz.replace('.', File.separatorChar) + ".java");
1.80 - f.getParentFile().mkdirs();
1.81 - Writer w = new FileWriter(f);
1.82 - try {
1.83 - PrintWriter pw = new PrintWriter(w);
1.84 - String pkg = clazz.replaceFirst("\\.[^.]+$", "");
1.85 - if (!pkg.equals(clazz)) {
1.86 - pw.println("package " + pkg + ";");
1.87 - }
1.88 - for (String line : content) {
1.89 - pw.println(line);
1.90 - }
1.91 - pw.flush();
1.92 - } finally {
1.93 - w.close();
1.94 - }
1.95 - }
1.96 -
1.97 - /**
1.98 - * Run the Java compiler.
1.99 - * (A JSR 199 implementation must be available.)
1.100 - * @param src a source root (runs javac on all *.java it finds matching {@code srcIncludes})
1.101 - * @param srcIncludes a pattern of source files names without path to compile (useful for testing incremental compiles), or null for all
1.102 - * @param dest a dest dir to compile classes to
1.103 - * @param cp classpath entries; if null, use Java classpath of test
1.104 - * @param stderr output stream to print messages to, or null for test console (i.e. do not capture)
1.105 - * @return true if compilation succeeded, false if it failed
1.106 - */
1.107 - public static boolean runJavac(File src, String srcIncludes, File dest, File[] cp, OutputStream stderr) {
1.108 - List<String> args = new ArrayList<String>();
1.109 - args.add("-classpath");
1.110 - if (cp != null) {
1.111 - StringBuffer b = new StringBuffer();
1.112 - for (File entry : cp) {
1.113 - b.append(File.pathSeparatorChar);
1.114 - b.append(entry.getAbsolutePath());
1.115 - }
1.116 - args.add(b.toString());
1.117 - } else {
1.118 - args.add(System.getProperty("java.class.path"));
1.119 - }
1.120 - args.add("-d");
1.121 - args.add(dest.getAbsolutePath());
1.122 - args.add("-sourcepath");
1.123 - args.add(src.getAbsolutePath());
1.124 - dest.mkdirs();
1.125 - scan(args, src, srcIncludes);
1.126 - JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
1.127 - Assert.assertNotNull("no JSR 199 compiler impl found; try e.g.: " +
1.128 - "test.unit.run.cp.extra=${nb_all}/apisupport.harness/external/openjdk-javac-6-b12.jar", compiler);
1.129 - //System.err.println("running javac with args: " + args);
1.130 - return compiler.run(null, null, stderr, args.toArray(new String[args.size()])) == 0;
1.131 - }
1.132 - private static void scan(List<String> names, File f, String includes) {
1.133 - if (f.isDirectory()) {
1.134 - for (File kid : f.listFiles()) {
1.135 - scan(names, kid, includes);
1.136 - }
1.137 - } else if (f.getName().endsWith(".java") && (includes == null || Pattern.compile(includes).matcher(f.getName()).find())) {
1.138 - names.add(f.getAbsolutePath());
1.139 - }
1.140 - }
1.141 -
1.142 -}