c.s.tools.ide.analysis.modernize/test/unit/src/ide/analysis/modernize/TidyTestCase.java
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
27 * If you wish your version of this file to be governed by only the CDDL
28 * or only the GPL Version 2, indicate your decision by adding
29 * "[Contributor] elects to include this software in this distribution
30 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31 * single choice of license, a recipient has the option to distribute
32 * your version of this file under either the CDDL, the GPL Version 2 or
33 * to extend the choice of license to its licensees as provided above.
34 * However, if you add GPL Version 2 code and therefore, elected the GPL
35 * Version 2 license, then the option applies only if the new code is
36 * made subject to such option by the copyright holder.
40 * Portions Copyrighted 2011 Sun Microsystems, Inc.
42 package ide.analysis.modernize;
44 import com.sun.tools.ide.analysis.modernize.impl.DiagnosticsTool;
45 import com.sun.tools.ide.analysis.modernize.impl.ModernizeAnalyzerImpl;
46 import com.sun.tools.ide.analysis.modernize.impl.ModernizeFix;
47 import com.sun.tools.ide.analysis.modernize.impl.YamlParser;
48 import com.sun.tools.ide.analysis.modernize.utils.AnalyticsTools;
50 import java.nio.file.Files;
51 import java.nio.file.attribute.PosixFilePermissions;
52 import java.util.ArrayList;
53 import java.util.Arrays;
54 import java.util.Collection;
55 import java.util.Collections;
56 import java.util.List;
57 import java.util.logging.ConsoleHandler;
58 import java.util.logging.Level;
59 import java.util.logging.LogManager;
60 import java.util.prefs.Preferences;
61 import org.netbeans.api.progress.ProgressHandle;
62 import org.netbeans.api.project.Project;
63 import org.netbeans.api.project.ProjectManager;
64 import org.netbeans.api.project.ui.OpenProjects;
65 import org.netbeans.modules.analysis.SPIAccessor;
66 import org.netbeans.modules.analysis.spi.Analyzer;
67 import org.netbeans.modules.cnd.api.model.CsmFile;
68 import org.netbeans.modules.cnd.api.model.CsmModel;
69 import org.netbeans.modules.cnd.api.model.CsmModelAccessor;
70 import org.netbeans.modules.cnd.api.model.CsmProject;
71 import org.netbeans.modules.cnd.api.model.syntaxerr.AuditPreferences;
72 import org.netbeans.modules.cnd.api.model.syntaxerr.CodeAudit;
73 import org.netbeans.modules.cnd.api.model.syntaxerr.CsmErrorProvider;
74 import org.netbeans.modules.cnd.api.project.NativeProject;
75 import org.netbeans.modules.cnd.api.project.NativeProjectRegistry;
76 import org.netbeans.modules.cnd.api.toolchain.CompilerSet;
77 import org.netbeans.modules.cnd.api.toolchain.CompilerSetManager;
78 import org.netbeans.modules.cnd.makeproject.api.MakeProject;
79 import org.netbeans.modules.cnd.makeproject.api.configurations.Item;
80 import org.netbeans.modules.cnd.modelutil.CsmUtilities;
81 import org.netbeans.modules.cnd.test.CndBaseTestCase;
82 import org.netbeans.modules.nativeexecution.api.ExecutionEnvironment;
83 import org.netbeans.modules.nativeexecution.api.ExecutionEnvironmentFactory;
84 import org.netbeans.modules.refactoring.api.Scope;
85 import org.openide.filesystems.FileObject;
86 import org.openide.filesystems.FileUtil;
87 import org.openide.util.Exceptions;
93 public abstract class TidyTestCase extends CndBaseTestCase {
95 private final String projectName;
97 private List<FileObject> fileObjects;
98 private MakeProject project;
99 private FileObject projectDirFileObject;
101 private final String CLANG_TIDY_PATH = "clang-tidy-3.9p";
103 public TidyTestCase(String projectName) {
106 this.projectName = projectName;
109 protected static CodeAudit findAudit(Collection<CodeAudit> audits, String id) {
110 for (CodeAudit audit : audits) {
111 if (audit.getID().equals(id)) {
119 protected void setUp() throws Exception {
121 LogManager.getLogManager().reset();
123 java.util.logging.Logger LOG = java.util.logging.Logger.getLogger("ide.analysis.tidy");
124 LOG.setLevel(Level.ALL);
126 ConsoleHandler consoleHandler = new ConsoleHandler();
127 consoleHandler.setLevel(Level.ALL);
128 LOG.addHandler(consoleHandler);
130 File clangTidyFile = getBinaryFile();
131 Files.setPosixFilePermissions(clangTidyFile.toPath(), PosixFilePermissions.fromString("rwxrwxrwx"));
133 assertTrue(getBinaryFile().exists());
135 File projectDirFile = getDataFile(projectName);
136 projectDirFileObject = FileUtil.toFileObject(projectDirFile);
138 fileObjects = new ArrayList<FileObject>();
140 project = (MakeProject) ProjectManager.getDefault().findProject(projectDirFileObject);
142 OpenProjects.getDefault().open(new Project[]{project}, false);
143 while (!OpenProjects.getDefault().isProjectOpen(project)) {
146 } catch (InterruptedException ex) {
147 Exceptions.printStackTrace(ex);
151 CsmModel model = CsmModelAccessor.getModel();
152 assertNotNull(project);
154 NativeProject np = project.getLookup().lookup(NativeProject.class);
155 NativeProjectRegistry.getDefault().register(np);
156 model.enableProject(np);
159 CsmProject csmProject = model.getProject(np);
160 assertNotNull(csmProject);
162 csmProject.waitParse();
166 public ExecutionEnvironment getExecEnv() {
167 ExecutionEnvironment execEnv = ExecutionEnvironmentFactory.getLocal();
171 public CompilerSet getCompilerSet() {
172 CompilerSetManager csm = CompilerSetManager.get(getExecEnv());
173 return csm.getDefaultCompilerSet();
176 public MakeProject getProject() {
180 public FileObject getProjectDir() {
181 return projectDirFileObject;
184 public FileObject getSourceFile(String name) {
185 return FileUtil.toFileObject(new File(FileUtil.toFile(getProjectDir()), name));
188 public File getBinaryFile() {
189 return new File(getDataDir(), CLANG_TIDY_PATH);
192 protected final void performTest(String startFileName, String checkName, boolean isStartFile) {
194 FileObject startFile = getSourceFile(startFileName);
195 assertTrue(startFile.isValid());
197 Scope scope = Scope.create(null, null, Arrays.asList(new FileObject[]{startFile}));
198 Analyzer.Context context = SPIAccessor.ACCESSOR.createContext(scope, null, null, ProgressHandle.createHandle("dummy"), 0, 0);
199 Analyzer.AnalyzerFactory factory = new ModernizeAnalyzerImpl.AnalyzerFactoryImpl();
201 ModernizeAnalyzerImpl analyzer = (ModernizeAnalyzerImpl) factory.createAnalyzer(context);
202 ExecutionEnvironment execEnv = ExecutionEnvironmentFactory.getLocal();
203 final CsmFile csmFile = CsmUtilities.getCsmFile(startFile, true, false);
204 Item item = AnalyticsTools.findItem(csmFile, getProject());
206 Collection<CodeAudit> audits = DiagnosticsTool.getAudits(getBinaryFile().getAbsolutePath(), execEnv, new AuditPreferences(Preferences.userNodeForPackage(this.getClass())));
207 CodeAudit audit = findAudit(audits, checkName);
208 assertNotNull(audit);
210 // ----- BEGIN diagnostics -----
211 DiagnosticsTool diagnosticsTool = new DiagnosticsTool(execEnv, item, getProject(), getBinaryFile().getAbsolutePath());
213 int exitCode = diagnosticsTool.process(Collections.singleton(audit.getID()), csmFile, true);
214 if (exitCode != DiagnosticsTool.STATUS_OK) {
218 List<YamlParser.Diagnostics> diags = YamlParser.getDefault().parseYaml(diagnosticsTool.getYamlAsString());
219 List<ModernizeFix> fixes = new ArrayList<ModernizeFix>();
221 CsmErrorProvider.Request request = new RequestImpl(csmFile);
222 CsmErrorProvider.Response response = new ResponseImpl(fixes);
224 analyzer.getErrorProvider(null).postProcess(false, csmFile, getProject(), diags, request, response);
225 // ----- END diagnostics -----
227 processTestResults(fixes, startFileName);
228 } catch (Exception ex) {
229 ex.printStackTrace(System.err);
234 protected void processTestResults(List<ModernizeFix> fixes, String startFileName) throws Exception {