c.s.tools.ide.analysis.modernize/test/unit/src/ide/analysis/modernize/TidyTestCase.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/c.s.tools.ide.analysis.modernize/test/unit/src/ide/analysis/modernize/TidyTestCase.java Wed Jun 07 20:23:29 2017 +0300
1.3 @@ -0,0 +1,237 @@
1.4 +/*
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 + *
1.7 + * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
1.8 + *
1.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
1.10 + * Other names may be trademarks of their respective owners.
1.11 + *
1.12 + * The contents of this file are subject to the terms of either the GNU
1.13 + * General Public License Version 2 only ("GPL") or the Common
1.14 + * Development and Distribution License("CDDL") (collectively, the
1.15 + * "License"). You may not use this file except in compliance with the
1.16 + * License. You can obtain a copy of the License at
1.17 + * http://www.netbeans.org/cddl-gplv2.html
1.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.19 + * specific language governing permissions and limitations under the
1.20 + * License. When distributing the software, include this License Header
1.21 + * Notice in each file and include the License file at
1.22 + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
1.23 + * particular file as subject to the "Classpath" exception as provided
1.24 + * by Oracle in the GPL Version 2 section of the License file that
1.25 + * accompanied this code. If applicable, add the following below the
1.26 + * License Header, with the fields enclosed by brackets [] replaced by
1.27 + * your own identifying information:
1.28 + * "Portions Copyrighted [year] [name of copyright owner]"
1.29 + *
1.30 + * If you wish your version of this file to be governed by only the CDDL
1.31 + * or only the GPL Version 2, indicate your decision by adding
1.32 + * "[Contributor] elects to include this software in this distribution
1.33 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.34 + * single choice of license, a recipient has the option to distribute
1.35 + * your version of this file under either the CDDL, the GPL Version 2 or
1.36 + * to extend the choice of license to its licensees as provided above.
1.37 + * However, if you add GPL Version 2 code and therefore, elected the GPL
1.38 + * Version 2 license, then the option applies only if the new code is
1.39 + * made subject to such option by the copyright holder.
1.40 + *
1.41 + * Contributor(s):
1.42 + *
1.43 + * Portions Copyrighted 2011 Sun Microsystems, Inc.
1.44 + */
1.45 +package ide.analysis.modernize;
1.46 +
1.47 +import com.sun.tools.ide.analysis.modernize.impl.DiagnosticsTool;
1.48 +import com.sun.tools.ide.analysis.modernize.impl.ModernizeAnalyzerImpl;
1.49 +import com.sun.tools.ide.analysis.modernize.impl.ModernizeFix;
1.50 +import com.sun.tools.ide.analysis.modernize.impl.YamlParser;
1.51 +import com.sun.tools.ide.analysis.modernize.utils.AnalyticsTools;
1.52 +import java.io.File;
1.53 +import java.nio.file.Files;
1.54 +import java.nio.file.attribute.PosixFilePermissions;
1.55 +import java.util.ArrayList;
1.56 +import java.util.Arrays;
1.57 +import java.util.Collection;
1.58 +import java.util.Collections;
1.59 +import java.util.List;
1.60 +import java.util.logging.ConsoleHandler;
1.61 +import java.util.logging.Level;
1.62 +import java.util.logging.LogManager;
1.63 +import java.util.prefs.Preferences;
1.64 +import org.netbeans.api.progress.ProgressHandle;
1.65 +import org.netbeans.api.project.Project;
1.66 +import org.netbeans.api.project.ProjectManager;
1.67 +import org.netbeans.api.project.ui.OpenProjects;
1.68 +import org.netbeans.modules.analysis.SPIAccessor;
1.69 +import org.netbeans.modules.analysis.spi.Analyzer;
1.70 +import org.netbeans.modules.cnd.api.model.CsmFile;
1.71 +import org.netbeans.modules.cnd.api.model.CsmModel;
1.72 +import org.netbeans.modules.cnd.api.model.CsmModelAccessor;
1.73 +import org.netbeans.modules.cnd.api.model.CsmProject;
1.74 +import org.netbeans.modules.cnd.api.model.syntaxerr.AuditPreferences;
1.75 +import org.netbeans.modules.cnd.api.model.syntaxerr.CodeAudit;
1.76 +import org.netbeans.modules.cnd.api.model.syntaxerr.CsmErrorProvider;
1.77 +import org.netbeans.modules.cnd.api.project.NativeProject;
1.78 +import org.netbeans.modules.cnd.api.project.NativeProjectRegistry;
1.79 +import org.netbeans.modules.cnd.api.toolchain.CompilerSet;
1.80 +import org.netbeans.modules.cnd.api.toolchain.CompilerSetManager;
1.81 +import org.netbeans.modules.cnd.makeproject.api.MakeProject;
1.82 +import org.netbeans.modules.cnd.makeproject.api.configurations.Item;
1.83 +import org.netbeans.modules.cnd.modelutil.CsmUtilities;
1.84 +import org.netbeans.modules.cnd.test.CndBaseTestCase;
1.85 +import org.netbeans.modules.nativeexecution.api.ExecutionEnvironment;
1.86 +import org.netbeans.modules.nativeexecution.api.ExecutionEnvironmentFactory;
1.87 +import org.netbeans.modules.refactoring.api.Scope;
1.88 +import org.openide.filesystems.FileObject;
1.89 +import org.openide.filesystems.FileUtil;
1.90 +import org.openide.util.Exceptions;
1.91 +
1.92 +/**
1.93 + *
1.94 + * @author Ilia Gromov
1.95 + */
1.96 +public abstract class TidyTestCase extends CndBaseTestCase {
1.97 +
1.98 + private final String projectName;
1.99 +
1.100 + private List<FileObject> fileObjects;
1.101 + private MakeProject project;
1.102 + private FileObject projectDirFileObject;
1.103 +
1.104 + private final String CLANG_TIDY_PATH = "clang-tidy-3.9p";
1.105 +
1.106 + public TidyTestCase(String projectName) {
1.107 + super(projectName);
1.108 +
1.109 + this.projectName = projectName;
1.110 + }
1.111 +
1.112 + protected static CodeAudit findAudit(Collection<CodeAudit> audits, String id) {
1.113 + for (CodeAudit audit : audits) {
1.114 + if (audit.getID().equals(id)) {
1.115 + return audit;
1.116 + }
1.117 + }
1.118 + return null;
1.119 + }
1.120 +
1.121 + @Override
1.122 + protected void setUp() throws Exception {
1.123 + super.setUp();
1.124 + LogManager.getLogManager().reset();
1.125 +
1.126 + java.util.logging.Logger LOG = java.util.logging.Logger.getLogger("ide.analysis.tidy");
1.127 + LOG.setLevel(Level.ALL);
1.128 +
1.129 + ConsoleHandler consoleHandler = new ConsoleHandler();
1.130 + consoleHandler.setLevel(Level.ALL);
1.131 + LOG.addHandler(consoleHandler);
1.132 +
1.133 + File clangTidyFile = getBinaryFile();
1.134 + Files.setPosixFilePermissions(clangTidyFile.toPath(), PosixFilePermissions.fromString("rwxrwxrwx"));
1.135 +
1.136 + assertTrue(getBinaryFile().exists());
1.137 +
1.138 + File projectDirFile = getDataFile(projectName);
1.139 + projectDirFileObject = FileUtil.toFileObject(projectDirFile);
1.140 +
1.141 + fileObjects = new ArrayList<FileObject>();
1.142 +
1.143 + project = (MakeProject) ProjectManager.getDefault().findProject(projectDirFileObject);
1.144 +
1.145 + OpenProjects.getDefault().open(new Project[]{project}, false);
1.146 + while (!OpenProjects.getDefault().isProjectOpen(project)) {
1.147 + try {
1.148 + Thread.sleep(100);
1.149 + } catch (InterruptedException ex) {
1.150 + Exceptions.printStackTrace(ex);
1.151 + }
1.152 + }
1.153 +
1.154 + CsmModel model = CsmModelAccessor.getModel();
1.155 + assertNotNull(project);
1.156 +
1.157 + NativeProject np = project.getLookup().lookup(NativeProject.class);
1.158 + NativeProjectRegistry.getDefault().register(np);
1.159 + model.enableProject(np);
1.160 + assertNotNull(np);
1.161 +
1.162 + CsmProject csmProject = model.getProject(np);
1.163 + assertNotNull(csmProject);
1.164 +
1.165 + csmProject.waitParse();
1.166 +
1.167 + }
1.168 +
1.169 + public ExecutionEnvironment getExecEnv() {
1.170 + ExecutionEnvironment execEnv = ExecutionEnvironmentFactory.getLocal();
1.171 + return execEnv;
1.172 + }
1.173 +
1.174 + public CompilerSet getCompilerSet() {
1.175 + CompilerSetManager csm = CompilerSetManager.get(getExecEnv());
1.176 + return csm.getDefaultCompilerSet();
1.177 + }
1.178 +
1.179 + public MakeProject getProject() {
1.180 + return project;
1.181 + }
1.182 +
1.183 + public FileObject getProjectDir() {
1.184 + return projectDirFileObject;
1.185 + }
1.186 +
1.187 + public FileObject getSourceFile(String name) {
1.188 + return FileUtil.toFileObject(new File(FileUtil.toFile(getProjectDir()), name));
1.189 + }
1.190 +
1.191 + public File getBinaryFile() {
1.192 + return new File(getDataDir(), CLANG_TIDY_PATH);
1.193 + }
1.194 +
1.195 + protected final void performTest(String startFileName, String checkName, boolean isStartFile) {
1.196 + try {
1.197 + FileObject startFile = getSourceFile(startFileName);
1.198 + assertTrue(startFile.isValid());
1.199 +
1.200 + Scope scope = Scope.create(null, null, Arrays.asList(new FileObject[]{startFile}));
1.201 + Analyzer.Context context = SPIAccessor.ACCESSOR.createContext(scope, null, null, ProgressHandle.createHandle("dummy"), 0, 0);
1.202 + Analyzer.AnalyzerFactory factory = new ModernizeAnalyzerImpl.AnalyzerFactoryImpl();
1.203 +
1.204 + ModernizeAnalyzerImpl analyzer = (ModernizeAnalyzerImpl) factory.createAnalyzer(context);
1.205 + ExecutionEnvironment execEnv = ExecutionEnvironmentFactory.getLocal();
1.206 + final CsmFile csmFile = CsmUtilities.getCsmFile(startFile, true, false);
1.207 + Item item = AnalyticsTools.findItem(csmFile, getProject());
1.208 +
1.209 + Collection<CodeAudit> audits = DiagnosticsTool.getAudits(getBinaryFile().getAbsolutePath(), execEnv, new AuditPreferences(Preferences.userNodeForPackage(this.getClass())));
1.210 + CodeAudit audit = findAudit(audits, checkName);
1.211 + assertNotNull(audit);
1.212 +
1.213 + // ----- BEGIN diagnostics -----
1.214 + DiagnosticsTool diagnosticsTool = new DiagnosticsTool(execEnv, item, getProject(), getBinaryFile().getAbsolutePath());
1.215 +
1.216 + int exitCode = diagnosticsTool.process(Collections.singleton(audit.getID()), csmFile, true);
1.217 + if (exitCode != DiagnosticsTool.STATUS_OK) {
1.218 + fail();
1.219 + }
1.220 +
1.221 + List<YamlParser.Diagnostics> diags = YamlParser.getDefault().parseYaml(diagnosticsTool.getYamlAsString());
1.222 + List<ModernizeFix> fixes = new ArrayList<ModernizeFix>();
1.223 +
1.224 + CsmErrorProvider.Request request = new RequestImpl(csmFile);
1.225 + CsmErrorProvider.Response response = new ResponseImpl(fixes);
1.226 +
1.227 + analyzer.getErrorProvider(null).postProcess(false, csmFile, getProject(), diags, request, response);
1.228 + // ----- END diagnostics -----
1.229 +
1.230 + processTestResults(fixes, startFileName);
1.231 + } catch (Exception ex) {
1.232 + ex.printStackTrace(System.err);
1.233 + fail(ex.toString());
1.234 + }
1.235 + }
1.236 +
1.237 + protected void processTestResults(List<ModernizeFix> fixes, String startFileName) throws Exception {
1.238 +
1.239 + }
1.240 +}