1
/*******************************************************************************
2
* Copyright (c) 2005, 2009 IBM Corporation and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* IBM Corporation - initial API and implementation
10
* Andrew Ferguson (Symbian)
11
* Markus Schorn (Wind River Systems)
12
* Anton Leherbauer (Wind River Systems)
13
*******************************************************************************/
14
package org.eclipse.cdt.core.testplugin;
16
import java.io.IOException;
17
import java.lang.reflect.InvocationTargetException;
18
import java.util.zip.ZipFile;
20
import junit.framework.Assert;
22
import org.eclipse.cdt.core.CCProjectNature;
23
import org.eclipse.cdt.core.CCorePlugin;
24
import org.eclipse.cdt.core.CProjectNature;
25
import org.eclipse.cdt.core.ICExtensionReference;
26
import org.eclipse.cdt.core.model.CModelException;
27
import org.eclipse.cdt.core.model.CoreModel;
28
import org.eclipse.cdt.core.model.IArchive;
29
import org.eclipse.cdt.core.model.IArchiveContainer;
30
import org.eclipse.cdt.core.model.IBinary;
31
import org.eclipse.cdt.core.model.IBinaryContainer;
32
import org.eclipse.cdt.core.model.ICContainer;
33
import org.eclipse.cdt.core.model.ICElement;
34
import org.eclipse.cdt.core.model.ICProject;
35
import org.eclipse.cdt.core.model.ISourceRoot;
36
import org.eclipse.cdt.core.model.ITranslationUnit;
37
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
38
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
39
import org.eclipse.cdt.core.settings.model.ICProjectDescriptionManager;
40
import org.eclipse.cdt.core.settings.model.TestCfgDataProvider;
41
import org.eclipse.cdt.core.settings.model.util.CDataUtil;
42
import org.eclipse.cdt.internal.core.pdom.indexer.IndexerPreferences;
43
import org.eclipse.core.resources.IFolder;
44
import org.eclipse.core.resources.IProject;
45
import org.eclipse.core.resources.IProjectDescription;
46
import org.eclipse.core.resources.IResource;
47
import org.eclipse.core.resources.IWorkspace;
48
import org.eclipse.core.resources.IWorkspaceRoot;
49
import org.eclipse.core.resources.IWorkspaceRunnable;
50
import org.eclipse.core.resources.ResourcesPlugin;
51
import org.eclipse.core.runtime.CoreException;
52
import org.eclipse.core.runtime.FileLocator;
53
import org.eclipse.core.runtime.IPath;
54
import org.eclipse.core.runtime.IProgressMonitor;
55
import org.eclipse.core.runtime.IStatus;
56
import org.eclipse.core.runtime.NullProgressMonitor;
57
import org.eclipse.core.runtime.Path;
58
import org.eclipse.core.runtime.Status;
59
import org.eclipse.ui.dialogs.IOverwriteQuery;
60
import org.eclipse.ui.wizards.datatransfer.FileSystemStructureProvider;
61
import org.eclipse.ui.wizards.datatransfer.ImportOperation;
62
import org.eclipse.ui.wizards.datatransfer.ZipFileStructureProvider;
63
import org.osgi.framework.Bundle;
65
* Helper methods to set up a ICProject.
67
public class CProjectHelper {
69
private final static IOverwriteQuery OVERWRITE_QUERY= new IOverwriteQuery() {
70
public String queryOverwrite(String file) {
75
public static ICProject createCProject(final String projectName, String binFolderName) throws CoreException {
76
return createCCProject(projectName, binFolderName, null);
80
* Creates a ICProject.
82
public static ICProject createCProject(final String projectName, String binFolderName, final String indexerID) throws CoreException {
83
final IWorkspace ws = ResourcesPlugin.getWorkspace();
84
final ICProject newProject[] = new ICProject[1];
85
ws.run(new IWorkspaceRunnable() {
87
public void run(IProgressMonitor monitor) throws CoreException {
88
IWorkspaceRoot root = ws.getRoot();
89
IProject project = root.getProject(projectName);
90
if (indexerID != null) {
91
IndexerPreferences.set(project, IndexerPreferences.KEY_INDEX_UNUSED_HEADERS_WITH_DEFAULT_LANG, "true");
92
IndexerPreferences.set(project, IndexerPreferences.KEY_INDEXER_ID, indexerID);
94
if (!project.exists()) {
97
project.refreshLocal(IResource.DEPTH_INFINITE, null);
99
if (!project.isOpen()) {
102
if (!project.hasNature(CProjectNature.C_NATURE_ID)) {
103
String projectId = CTestPlugin.PLUGIN_ID + ".TestProject";
104
addNatureToProject(project, CProjectNature.C_NATURE_ID, null);
105
CCorePlugin.getDefault().mapCProjectOwner(project, projectId, false);
107
addDefaultBinaryParser(project);
108
newProject[0] = CCorePlugin.getDefault().getCoreModel().create(project);
112
return newProject[0];
116
* Add the default binary parser if no binary parser configured.
119
* @throws CoreException
121
public static boolean addDefaultBinaryParser(IProject project) throws CoreException {
122
ICExtensionReference[] binaryParsers= CCorePlugin.getDefault().getBinaryParserExtensions(project);
123
if (binaryParsers == null || binaryParsers.length == 0) {
124
ICProjectDescription desc= CCorePlugin.getDefault().getProjectDescription(project);
129
desc.getDefaultSettingConfiguration().create(CCorePlugin.BINARY_PARSER_UNIQ_ID, CCorePlugin.DEFAULT_BINARY_PARSER_UNIQ_ID);
130
CCorePlugin.getDefault().setProjectDescription(project, desc);
136
* Creates a ICProject.
138
public static ICProject createNewStileCProject(final String projectName, final String indexerID) throws CoreException {
139
return createNewStileCProject(projectName, indexerID, false);
143
* Creates a ICProject.
145
public static ICProject createNewStileCProject(final String projectName, String providerId, final String indexerID) throws CoreException {
146
return createNewStileCProject(projectName, providerId, indexerID, false);
150
* Creates a ICProject.
152
public static ICProject createNewStileCProject(final String projectName, final String indexerID, boolean markCreating) throws CoreException {
153
return createNewStileCProject(projectName, null, indexerID, markCreating);
157
* Creates a ICProject.
159
public static ICProject createNewStileCProject(final String projectName, String cfgProviderId, final String indexerID, final boolean markCreating) throws CoreException {
160
final IWorkspace ws = ResourcesPlugin.getWorkspace();
161
final ICProject newProject[] = new ICProject[1];
162
if(cfgProviderId == null)
163
cfgProviderId = TestCfgDataProvider.PROVIDER_ID;
165
final String finalCfgProviderId = cfgProviderId;
166
ws.run(new IWorkspaceRunnable() {
168
public void run(IProgressMonitor monitor) throws CoreException {
169
IWorkspaceRoot root = ws.getRoot();
170
IProject project = root.getProject(projectName);
171
if (indexerID != null) {
172
IndexerPreferences.set(project, IndexerPreferences.KEY_INDEXER_ID, indexerID);
174
if (!project.exists()) {
175
project.create(null);
177
project.refreshLocal(IResource.DEPTH_INFINITE, null);
179
if (!project.isOpen()) {
182
if (!project.hasNature(CProjectNature.C_NATURE_ID)) {
183
addNatureToProject(project, CProjectNature.C_NATURE_ID, null);
184
ICConfigurationDescription prefCfg = CCorePlugin.getDefault().getPreferenceConfiguration(finalCfgProviderId);
185
ICProjectDescriptionManager mngr = CCorePlugin.getDefault().getProjectDescriptionManager();
186
ICProjectDescription projDes = mngr.createProjectDescription(project, false, markCreating);
187
projDes.createConfiguration(CDataUtil.genId(null), CDataUtil.genId("test"), prefCfg);
188
mngr.setProjectDescription(project, projDes);
189
// CCorePlugin.getDefault().mapCProjectOwner(project, projectId, false);
191
addDefaultBinaryParser(project);
192
newProject[0] = CCorePlugin.getDefault().getCoreModel().create(project);
196
return newProject[0];
200
private static String getMessage(IStatus status) {
201
StringBuffer message = new StringBuffer("[");
202
message.append(status.getMessage());
203
if (status.isMultiStatus()) {
204
IStatus children[] = status.getChildren();
205
for( int i = 0; i < children.length; i++) {
206
message.append(getMessage(children[i]));
210
return message.toString();
213
public static ICProject createCCProject(final String projectName, final String binFolderName) throws CoreException {
214
return createCCProject(projectName, binFolderName, null);
217
public static ICProject createCCProject(final String projectName, final String binFolderName, final String indexerID) throws CoreException {
218
final IWorkspace ws = ResourcesPlugin.getWorkspace();
219
final ICProject newProject[] = new ICProject[1];
220
ws.run(new IWorkspaceRunnable() {
222
public void run(IProgressMonitor monitor) throws CoreException {
223
ICProject cproject = createCProject(projectName, binFolderName, indexerID);
224
if (!cproject.getProject().hasNature(CCProjectNature.CC_NATURE_ID)) {
225
addNatureToProject(cproject.getProject(), CCProjectNature.CC_NATURE_ID, null);
227
newProject[0] = cproject;
230
return newProject[0];
234
* Removes a ICProject.
236
public static void delete(ICProject cproject) {
238
cproject.getProject().delete(true, true, null);
239
} catch (CoreException e) {
242
} catch (InterruptedException e1) {
246
System.runFinalization();
247
cproject.getProject().delete(true, true, null);
248
} catch (CoreException e2) {
249
Assert.fail(getMessage(e2.getStatus()));
256
* Adds a folder container to a ICProject.
258
public static ICContainer addCContainer(ICProject cproject, String containerName) throws CoreException {
259
IProject project = cproject.getProject();
260
ICContainer container = null;
261
if (containerName == null || containerName.length() == 0) {
262
ICContainer[] conts = cproject.getSourceRoots();
263
if (conts.length > 0) {
264
container = conts[0];
267
IFolder folder = project.getFolder(containerName);
268
if (!folder.exists()) {
269
folder.create(false, true, null);
271
container = CoreModel.getDefault().create(folder);
277
* Adds a folder container to a ICProject and imports all files contained in the given Zip file.
279
public static ICContainer addCContainerWithImport(ICProject cproject, String containerName, ZipFile zipFile)
280
throws InvocationTargetException, CoreException {
281
ICContainer root = addCContainer(cproject, containerName);
282
importFilesFromZip(zipFile, root.getPath(), null);
287
* Removes a folder from a ICProject.
289
public static void removeCContainer(ICProject cproject, String containerName) throws CoreException {
290
IFolder folder = cproject.getProject().getFolder(containerName);
291
folder.delete(true, null);
295
* Attempts to find an archive with the given name in the workspace
297
public static IArchive findArchive(ICProject testProject, String name) throws CModelException {
299
IArchive[] myArchives;
300
IArchiveContainer archCont;
301
/***************************************************************************************************************************
302
* Since ArchiveContainer.getArchives does not wait until all the archives in the project have been parsed before returning
303
* the list, we have to do a sync ArchiveContainer.getChildren first to make sure we find all the archives.
305
archCont = testProject.getArchiveContainer();
306
myArchives = archCont.getArchives();
307
if (myArchives.length < 1)
309
for (x = 0; x < myArchives.length; x++) {
310
if (myArchives[x].getElementName().equals(name))
311
return (myArchives[x]);
317
* Attempts to find a binary with the given name in the workspace
319
public static IBinary findBinary(ICProject testProject, String name) throws CModelException {
320
IBinaryContainer binCont;
322
IBinary[] myBinaries;
323
binCont = testProject.getBinaryContainer();
324
myBinaries = binCont.getBinaries();
325
if (myBinaries.length < 1)
327
for (x = 0; x < myBinaries.length; x++) {
328
if (myBinaries[x].getElementName().equals(name))
329
return (myBinaries[x]);
335
* Attempts to find an object with the given name in the workspace
337
public static IBinary findObject(ICProject testProject, String name) throws CModelException {
338
ICElement[] sourceRoots = testProject.getChildren();
339
for (int i = 0; i < sourceRoots.length; i++) {
340
ISourceRoot root = (ISourceRoot) sourceRoots[i];
341
ICElement[] myElements = root.getChildren();
342
for (int x = 0; x < myElements.length; x++) {
343
if (myElements[x].getElementName().equals(name)) {
344
if (myElements[x] instanceof IBinary) {
345
return ((IBinary) myElements[x]);
354
* Attempts to find a TranslationUnit with the given name in the workspace
355
* @throws InterruptedException
357
public static ITranslationUnit findTranslationUnit(ICProject testProject, String name) throws CModelException, InterruptedException {
358
for (int j=0; j<20; j++) {
359
ICElement[] sourceRoots = testProject.getChildren();
360
for (int i = 0; i < sourceRoots.length; i++) {
361
ISourceRoot root = (ISourceRoot) sourceRoots[i];
362
ICElement[] myElements = root.getChildren();
363
for (int x = 0; x < myElements.length; x++) {
364
if (myElements[x].getElementName().equals(name)) {
365
if (myElements[x] instanceof ITranslationUnit) {
366
return ((ITranslationUnit) myElements[x]);
377
* Attempts to find an element with the given name in the workspace
379
public static ICElement findElement(ICProject testProject, String name) throws CModelException {
380
ICElement[] sourceRoots = testProject.getChildren();
381
for (int i = 0; i < sourceRoots.length; i++) {
382
ISourceRoot root = (ISourceRoot) sourceRoots[i];
383
ICElement[] myElements = root.getChildren();
384
for (int x = 0; x < myElements.length; x++) {
385
if (myElements[x].getElementName().equals(name)) {
386
return myElements[x];
393
public static void addNatureToProject(IProject proj, String natureId, IProgressMonitor monitor) throws CoreException {
394
IProjectDescription description = proj.getDescription();
395
String[] prevNatures = description.getNatureIds();
396
String[] newNatures = new String[prevNatures.length + 1];
397
System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
398
newNatures[prevNatures.length] = natureId;
399
description.setNatureIds(newNatures);
400
proj.setDescription(description, monitor);
403
private static void importFilesFromZip(ZipFile srcZipFile, IPath destPath, IProgressMonitor monitor)
404
throws InvocationTargetException {
405
ZipFileStructureProvider structureProvider = new ZipFileStructureProvider(srcZipFile);
407
ImportOperation op = new ImportOperation(destPath, structureProvider.getRoot(), structureProvider,
410
} catch (InterruptedException e) {
416
public static void importSourcesFromPlugin(ICProject project, Bundle bundle, String sources) throws CoreException {
418
String baseDir= FileLocator.toFileURL(FileLocator.find(bundle, new Path(sources), null)).getFile();
419
ImportOperation importOp = new ImportOperation(project.getProject().getFullPath(),
420
new File(baseDir), FileSystemStructureProvider.INSTANCE, OVERWRITE_QUERY);
421
importOp.setCreateContainerStructure(false);
422
importOp.run(new NullProgressMonitor());
424
catch (Exception e) {
425
throw new CoreException(new Status(IStatus.ERROR, CTestPlugin.PLUGIN_ID, 0, "Import Interrupted", e));
430
* Returns the location of a newly created directory in the systems temp. area.
431
* @return the location of a newly created directory in the systems temp. area
432
* @throws IOException
434
public static File freshDir() throws IOException {
435
File tempDir = new File(System.getProperty("java.io.tmpdir")).getCanonicalFile();
436
for(int i=0; i<Integer.MAX_VALUE; i++) {
437
File candidate = new File(tempDir, "__testData/"+i);
438
if(!candidate.exists()) {
b'\\ No newline at end of file'