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