/*
* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package tools.javac.combo;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import javax.tools.ToolProvider;
import com.sun.source.util.JavacTask;
import com.sun.tools.javac.util.Pair;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static org.testng.Assert.fail;
/**
* Base class for template-driven TestNG javac tests that support on-the-fly
* source file generation, compilation, classloading, execution, and separate
* compilation.
*
*
Manages a set of templates (which have embedded tags of the form
* {@code #\{NAME\}}), source files (which are also templates), and compile
* options. Test cases can register templates and source files, cause them to
* be compiled, validate whether the set of diagnostic messages output by the
* compiler is correct, and optionally load and run the compiled classes.
*
* @author Brian Goetz
*/
@Test
public abstract class JavacTemplateTestBase {
private static final Set suiteErrors = Collections.synchronizedSet(new HashSet<>());
private static final AtomicInteger counter = new AtomicInteger();
private static final File root = new File("gen");
private static final File nullDir = new File("empty");
protected final Map templates = new HashMap<>();
protected final Diagnostics diags = new Diagnostics();
protected final List> sourceFiles = new ArrayList<>();
protected final List compileOptions = new ArrayList<>();
protected final List classpaths = new ArrayList<>();
protected final Template.Resolver defaultResolver = new MapResolver(templates);
private Template.Resolver currentResolver = defaultResolver;
/** Add a template with a specified name */
protected void addTemplate(String name, Template t) {
templates.put(name, t);
}
/** Add a template with a specified name */
protected void addTemplate(String name, String s) {
templates.put(name, new StringTemplate(s));
}
/** Add a source file */
protected void addSourceFile(String name, Template t) {
sourceFiles.add(new Pair<>(name, t));
}
/** Add a File to the class path to be used when loading classes; File values
* will generally be the result of a previous call to {@link #compile()}.
* This enables testing of separate compilation scenarios if the class path
* is set up properly.
*/
protected void addClassPath(File path) {
classpaths.add(path);
}
/**
* Add a set of compilation command-line options
*/
protected void addCompileOptions(String... opts) {
Collections.addAll(compileOptions, opts);
}
/** Reset the compile options to the default (empty) value */
protected void resetCompileOptions() { compileOptions.clear(); }
/** Remove all templates */
protected void resetTemplates() { templates.clear(); }
/** Remove accumulated diagnostics */
protected void resetDiagnostics() { diags.reset(); }
/** Remove all source files */
protected void resetSourceFiles() { sourceFiles.clear(); }
/** Remove registered class paths */
protected void resetClassPaths() { classpaths.clear(); }
// Before each test method, reset everything
@BeforeMethod
public void reset() {
resetCompileOptions();
resetDiagnostics();
resetSourceFiles();
resetTemplates();
resetClassPaths();
}
// After each test method, if the test failed, capture source files and diagnostics and put them in the log
@AfterMethod
public void copyErrors(ITestResult result) {
if (!result.isSuccess()) {
suiteErrors.addAll(diags.errorKeys());
List