1
/*******************************************************************************
2
* Copyright (c) 2005, 2007 Intel 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
* Intel Corporation - Initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.managedbuilder.core.tests;
13
import java.io.ByteArrayInputStream;
15
import junit.framework.Test;
16
import junit.framework.TestCase;
17
import junit.framework.TestSuite;
19
import org.eclipse.cdt.core.CCorePlugin;
20
import org.eclipse.cdt.managedbuilder.core.BuildException;
21
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
22
import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
23
import org.eclipse.cdt.managedbuilder.core.IManagedProject;
24
import org.eclipse.cdt.managedbuilder.core.IOption;
25
import org.eclipse.cdt.managedbuilder.core.IOptionCategory;
26
import org.eclipse.cdt.managedbuilder.core.IProjectType;
27
import org.eclipse.cdt.managedbuilder.core.IResourceConfiguration;
28
import org.eclipse.cdt.managedbuilder.core.ITool;
29
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
30
import org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin;
31
import org.eclipse.cdt.managedbuilder.core.ManagedCProjectNature;
32
import org.eclipse.cdt.managedbuilder.internal.core.Tool;
33
import org.eclipse.cdt.managedbuilder.testplugin.ManagedBuildTestHelper;
34
import org.eclipse.core.resources.IFile;
35
import org.eclipse.core.resources.IFolder;
36
import org.eclipse.core.resources.IProject;
37
import org.eclipse.core.resources.IProjectDescription;
38
import org.eclipse.core.resources.IResource;
39
import org.eclipse.core.resources.IWorkspace;
40
import org.eclipse.core.resources.IWorkspaceDescription;
41
import org.eclipse.core.resources.IWorkspaceRoot;
42
import org.eclipse.core.resources.IWorkspaceRunnable;
43
import org.eclipse.core.resources.ResourcesPlugin;
44
import org.eclipse.core.runtime.CoreException;
45
import org.eclipse.core.runtime.IProgressMonitor;
46
import org.eclipse.core.runtime.IStatus;
47
import org.eclipse.core.runtime.NullProgressMonitor;
50
public class ResourceBuildCoreTests extends TestCase {
51
private static final boolean boolVal = true;
52
private static IProjectType exeType;
53
private static IProjectType libType;
54
private static IProjectType dllType;
57
private static final String projectName = "T1";
58
private static final String renamedProjectName1 = "T1_1";
59
private static final String renamedProjectName2 = "T1_2";
61
public ResourceBuildCoreTests(String name) {
65
public static Test suite() {
66
TestSuite suite = new TestSuite(ResourceBuildCoreTests.class.getName());
67
suite.addTest(new ResourceBuildCoreTests("testResourceConfigurations"));
68
suite.addTest(new ResourceBuildCoreTests("testResourceConfigurationReset"));
69
// suite.addTest(new ResourceBuildCoreTests("testResourceConfigurationBuildInfo"));
70
// suite.addTest(new ResourceBuildCoreTests("testResourceRename"));
75
* Creates a couple of resource configurations.
76
* Checks whether the resource & project default build properties are same or not.
77
* Overrides project build properties and checks whether they are reflecting at resource level.
78
* Overrides resource build properties and checks whether they are reflecting at project level.
82
public void testResourceConfigurations() throws Exception {
84
// Create a new project
85
IProject project = null;
88
project = createProject(projectName);
90
// Now associate the builder with the project
91
ManagedBuildTestHelper.addManagedBuildNature(project);
92
IProjectDescription description = project.getDescription();
93
// Make sure it has a managed nature
94
if (description != null) {
95
assertTrue(description.hasNature(ManagedCProjectNature.MNG_NATURE_ID));
98
} catch (CoreException e) {
99
fail("Test failed on project creation: " + e.getLocalizedMessage());
102
// Find the base project type definition
103
IProjectType[] projTypes = ManagedBuildManager.getDefinedProjectTypes();
104
IProjectType projType = ManagedBuildManager.getProjectType("cdt.managedbuild.target.testgnu21.exe");
105
assertNotNull(projType);
107
// Create the managed-project (.cdtbuild) for our project that builds an executable.
108
IManagedProject newProject = ManagedBuildManager.createManagedProject(project, projType);
109
assertEquals(newProject.getName(), projType.getName());
110
assertFalse(newProject.equals(projType));
111
ManagedBuildManager.setNewProjectVersion(project);
113
// Create a couple of resources ( 'main.c' & 'bar.c')
114
IFile mainFile = project.getProject().getFile( "main.c" );
115
if( !mainFile.exists() ){
116
mainFile.create( new ByteArrayInputStream( "#include <stdio.h>\n extern void bar(); \n int main() { \nprintf(\"Hello, World!!\"); \n bar();\n return 0; }".getBytes() ), false, null );
119
IFile barFile = project.getProject().getFile( "bar.c" );
120
if( !barFile.exists() ){
121
barFile.create( new ByteArrayInputStream( "#include <stdio.h>\n void bar() { \nprintf(\"Hello, bar()!!\");\n return; }".getBytes() ), false, null );
124
// Get the configurations and make one of them as default configuration.
125
IConfiguration defaultConfig = null;
126
IConfiguration[] configs = projType.getConfigurations();
127
for (int i = 0; i < configs.length; ++i) {
128
// Make the first configuration the default
130
defaultConfig = newProject.createConfiguration(configs[i], projType.getId() + "." + i);
132
newProject.createConfiguration(configs[i], projType.getId() + "." + i);
135
ManagedBuildManager.setDefaultConfiguration(project, defaultConfig);
137
// Create Resource Configurations for files main.c and bar.c
138
IResourceConfiguration resMainConfig = defaultConfig.createResourceConfiguration(mainFile);
139
IResourceConfiguration resBarConfig = defaultConfig.createResourceConfiguration(barFile);
141
// Check whether defaultConfig has two resource configurations or not.
142
IResourceConfiguration resConfigs[] = defaultConfig.getResourceConfigurations();
143
assertEquals(2,resConfigs.length);
145
// Get the tools associated with the resource 'main.c'.
146
ITool resMainTools[] = resMainConfig.getTools();
147
assertNotNull(resMainTools);
148
assertEquals(1,resMainTools.length);
150
// Get the tools associated with the resource 'bar.c'.
151
ITool resBarTools[] = resBarConfig.getTools();
152
assertNotNull(resBarTools);
153
assertEquals(1,resBarTools.length);
155
// Get the build properties for the resource main.c
156
ITool resMainTool = resMainTools[0];
158
String resMainBuildProps = resMainTool.getToolFlags();
160
// Get the build properties for the resource bar.c
161
ITool resBarTool = resBarTools[0];
162
String resBarBuildProps = resBarTool.getToolFlags();
164
// Get file extension.
165
String extString = mainFile.getFileExtension();
167
// Get the project build properties.
168
ITool tools[] = defaultConfig.getFilteredTools();
169
Tool projTool = null;
170
String projBuildProps = new String();
171
for (int i = 0; i < tools.length; i++) {
172
if( tools[i].buildsFileType(extString) ) {
173
// Get the build properties of a project in default configuration
174
projTool = (Tool)tools[i];
175
projBuildProps = projTool.getToolFlags();
180
// Initially, Project build properties and resource build properties are same.
181
assertEquals(resMainBuildProps,projBuildProps);
183
// Initially, build properties of files with same extension ( example , .c files) are equal.
184
assertEquals(resMainBuildProps,resBarBuildProps);
186
// Now modify project build properties and it should reflect in resource build properties also.
188
IOption projDebugOption = projTool.getOptionById("testgnu.c.compiler.exe.debug.option.debugging.level");
190
assertNotNull(projDebugOption);
192
// Override options in the default configuration.
193
// Set the debug option value to '-g2' at Project level
194
IOption newProjDebugOption = ManagedBuildManager.setOption(defaultConfig,projTool,projDebugOption,"testgnu.c.debugging.level.default");
196
// Get the option 'id' and 'value'.
197
String newProjDebugOptionId = newProjDebugOption.getId();
198
String newProjDebugOptionValue = newProjDebugOption.getStringValue();
200
// Assert old & new(overridden) debug option values of project are different.
201
assertNotSame(projDebugOption.getStringValue(),newProjDebugOptionValue);
203
// Check whether the overridden option at project level is reflecting at resource level or not.
204
IOption resMainDebugOption = null;
206
IOption resMainOptions[] = resMainTool.getOptions();
208
for(int i=0; i< resMainOptions.length; i++){
209
IOption opt = resMainOptions[i];
211
if(opt.getId().equals(newProjDebugOptionId)){
212
// Resource Configuration doesnot have overridden value for this option.
213
resMainDebugOption = opt;
216
if ( opt.getSuperClass() != null ) {
217
if(opt.getSuperClass().getId().equals(newProjDebugOptionId)){
218
// Resource Configuration does have overridden value for this option.
219
resMainDebugOption = opt;
226
String resMainDebugOptionValue = resMainDebugOption.getStringValue();
228
// Assert Debug option values of project and resource are same.
229
assertEquals(newProjDebugOptionValue, resMainDebugOptionValue);
231
// Now, Modify the Debug option at resource level
232
// and verify whether the modified option is reflected at project level.
233
// It should not reflect at project level.
235
IOption newResMainDebugOption = ManagedBuildManager.setOption(resMainConfig,resMainTool,resMainDebugOption,"gnu.c.debugging.level.minimal");
237
//Get the latest project Debug option.
238
tools = defaultConfig.getFilteredTools();
241
for (int i = 0; i < tools.length; i++) {
242
if( tools[i].buildsFileType(extString) ) {
243
// Get the build properties of a project in default configuration
244
projTool = (Tool)tools[i];
249
projDebugOption = projTool.getOptionById(newProjDebugOptionId);
250
String projDebugOptionValue = projDebugOption.getStringValue();
252
String newResMainDebugOptionValue = newResMainDebugOption.getStringValue();
254
// Assert the debug option values of project and resource are different.
255
assertNotSame(projDebugOptionValue, newResMainDebugOptionValue);
257
// Close and remove project.
259
removeProject(projectName);
263
* Creates a project and a resource(hello.c).
264
* Overrides the build properties in resource configuration, and
265
* resets the resource configuration, verifies whether the overridden
266
* values still exist or not.
269
public void testResourceConfigurationReset() throws Exception {
271
// Create a new project
272
IProject project = null;
275
project = createProject(projectName);
277
// Now associate the builder with the project
278
ManagedBuildTestHelper.addManagedBuildNature(project);
279
IProjectDescription description = project.getDescription();
280
// Make sure it has a managed nature
281
if (description != null) {
282
assertTrue(description.hasNature(ManagedCProjectNature.MNG_NATURE_ID));
285
} catch (CoreException e) {
286
fail("Test failed on project creation: " + e.getLocalizedMessage());
289
// Find the base project type definition
290
IProjectType[] projTypes = ManagedBuildManager.getDefinedProjectTypes();
291
IProjectType projType = ManagedBuildManager.getProjectType("cdt.managedbuild.target.testgnu21.exe");
292
assertNotNull(projType);
294
// Create the managed-project (.cdtbuild) for our project that builds an executable.
295
IManagedProject newProject = ManagedBuildManager.createManagedProject(project, projType);
296
assertEquals(newProject.getName(), projType.getName());
297
assertFalse(newProject.equals(projType));
298
ManagedBuildManager.setNewProjectVersion(project);
300
// Create a resource ( 'hello.c')
301
IFile helloFile = project.getProject().getFile( "hello.c" );
302
if( !helloFile.exists() ){
303
helloFile.create( new ByteArrayInputStream( "#include <stdio.h>\n\n int main() { \nprintf(\"Hello, World!!\"); \n bar();\n return 0; }".getBytes() ), false, null );
306
// Get the configurations and make one of them as default configuration.
307
IConfiguration defaultConfig = null;
308
IConfiguration[] configs = projType.getConfigurations();
309
for (int i = 0; i < configs.length; ++i) {
310
// Make the first configuration the default
312
defaultConfig = newProject.createConfiguration(configs[i], projType.getId() + "." + i);
314
newProject.createConfiguration(configs[i], projType.getId() + "." + i);
317
ManagedBuildManager.setDefaultConfiguration(project, defaultConfig);
319
// Create Resource Configurations for hello.c
320
IResourceConfiguration resConfig = defaultConfig.createResourceConfiguration(helloFile);
322
// Check whether defaultConfig has the correct number of resource configurations or not.
323
IResourceConfiguration resConfigs[] = defaultConfig.getResourceConfigurations();
324
assertEquals(1,resConfigs.length);
326
// Get the tools associated with the resource 'hello.c'.
327
ITool resTools[] = resConfig.getTools();
328
assertNotNull(resTools);
329
assertEquals(1,resTools.length);
331
// Get the build properties for the resource hello.c
332
ITool resTool = resTools[0];
333
String defaultResToolFlags = resTool.getToolFlags();
335
// Get the Debug Option.
336
IOption resDebugOption = resTool.getOptionById("testgnu.c.compiler.exe.debug.option.debugging.level");
338
// Get the default value of debug option for resource.
339
String defaultResDebugOptVal = resDebugOption.getStringValue();
341
// Now, override the value with "gnu.c.debugging.level.minimal"
342
IOption newResDebugOption = ManagedBuildManager.setOption(resConfig,resTool,resDebugOption,"gnu.c.debugging.level.minimal");
344
// Get the overridden value of debug option.
345
String newResDebugOptVal = newResDebugOption.getStringValue();
346
String newResToolFlags = resTool.getToolFlags();
348
// Make sure, default and overridden values are different.
349
assertNotSame(defaultResDebugOptVal,newResDebugOptVal);
351
// Reset the resource configuration.
352
ManagedBuildManager.resetResourceConfiguration(project,resConfig);
353
String resetResToolFlags = resTool.getToolFlags();
355
assertNotSame(resetResToolFlags,newResToolFlags);
356
assertEquals(defaultResToolFlags,resetResToolFlags);
358
// Close and remove project.
360
removeProject(projectName);
364
* Creates a project and a couple of resources.
365
* Overrides the build properties of resources. Saves, closes, and reopens
366
* the project. Then, checks the overridden options. Basically, this function
367
* tests persisting overridden resource build properties between project sessions.
370
public void testResourceConfigurationBuildInfo() throws Exception {
372
// Create a new project
373
IProject project = null;
376
project = createProject(projectName);
378
// Now associate the builder with the project
379
ManagedBuildTestHelper.addManagedBuildNature(project);
380
IProjectDescription description = project.getDescription();
381
// Make sure it has a managed nature
382
if (description != null) {
383
assertTrue(description.hasNature(ManagedCProjectNature.MNG_NATURE_ID));
386
} catch (CoreException e) {
387
fail("Test failed on project creation: " + e.getLocalizedMessage());
390
// Find the base project type definition
391
IProjectType[] projTypes = ManagedBuildManager.getDefinedProjectTypes();
392
IProjectType projType = ManagedBuildManager.getProjectType("cdt.managedbuild.target.testgnu21.exe");
393
assertNotNull(projType);
395
// Create the managed-project (.cdtbuild) for our project that builds an executable.
396
IManagedProject newProject = ManagedBuildManager.createManagedProject(project, projType);
397
assertEquals(newProject.getName(), projType.getName());
398
assertFalse(newProject.equals(projType));
399
ManagedBuildManager.setNewProjectVersion(project);
401
// Create a couple of resources ( 'main.c' & 'bar.c')
402
IFile mainFile = project.getProject().getFile( "main.c" );
403
if( !mainFile.exists() ){
404
mainFile.create( new ByteArrayInputStream( "#include <stdio.h>\n extern void bar(); \n int main() { \nprintf(\"Hello, World!!\"); \n bar();\n return 0; }".getBytes() ), false, null );
407
IFile barFile = project.getProject().getFile( "bar.c" );
408
if( !barFile.exists() ){
409
barFile.create( new ByteArrayInputStream( "#include <stdio.h>\n void bar() { \nprintf(\"Hello, bar()!!\");\n return; }".getBytes() ), false, null );
412
// Get the configurations and make one of them as default configuration.
413
IConfiguration defaultConfig = null;
414
IConfiguration[] configs = projType.getConfigurations();
415
for (int i = 0; i < configs.length; ++i) {
416
// Make the first configuration the default
418
defaultConfig = newProject.createConfiguration(configs[i], projType.getId() + "." + i);
420
newProject.createConfiguration(configs[i], projType.getId() + "." + i);
423
ManagedBuildManager.setDefaultConfiguration(project, defaultConfig);
425
// Get the default configuration id.
426
String defaultConfigId = defaultConfig.getId();
428
// Create Resource Configurations for files main.c and bar.c
429
IResourceConfiguration resMainConfig = defaultConfig.createResourceConfiguration(mainFile);
430
IResourceConfiguration resBarConfig = defaultConfig.createResourceConfiguration(barFile);
432
// Check whether defaultConfig has two resource configurations or not.
433
IResourceConfiguration resConfigs[] = defaultConfig.getResourceConfigurations();
434
assertEquals(2,resConfigs.length);
436
// Get the paths of resource configurations.
437
String resMainPath = resMainConfig.getResourcePath();
438
String resBarPath = resBarConfig.getResourcePath();
440
// Get the tools associated with the resource 'main.c'.
441
ITool resMainTools[] = resMainConfig.getTools();
442
assertNotNull(resMainTools);
443
assertEquals(1,resMainTools.length);
445
// Get the tools associated with the resource 'bar.c'.
446
ITool resBarTools[] = resBarConfig.getTools();
447
assertNotNull(resBarTools);
448
assertEquals(1,resBarTools.length);
450
// Get the build properties for the resource main.c
451
ITool resMainTool = resMainTools[0];
453
String defaultMainBuildProps = resMainTool.getToolFlags();
455
// Get the build properties for the resource bar.c
456
ITool resBarTool = resBarTools[0];
457
String defaultBarBuildProps = resBarTool.getToolFlags();
460
// Now, override debug and optimization options.
461
// In Debug Configuration, Currently default values in resource configurations are
462
// optimization : -O0, debug : -g3
463
// Override the options in the following way.
464
// main.c : optimization : '-O1' debug : '-g1'
465
// bar.c : optimization : '-O2' debug : '-g2'
467
IOption defaultResMainOptOption = resMainTool.getOptionById("testgnu.c.compiler.exe.debug.option.optimization.level");
468
String defaultResMainOptVal = defaultResMainOptOption.getStringValue();
470
IOption resMainOptOption = ManagedBuildManager.setOption(resMainConfig,resMainTool,defaultResMainOptOption,"gnu.c.optimization.level.optimize");
471
String resMainOptVal = resMainOptOption.getStringValue();
473
IOption defaultResMainDebugOption = resMainTool.getOptionById("testgnu.c.compiler.exe.debug.option.debugging.level");
474
String defaultResMainDebugVal = defaultResMainDebugOption.getStringValue();
476
IOption resMainDebugOption = ManagedBuildManager.setOption(resMainConfig,resMainTool,defaultResMainDebugOption,"gnu.c.debugging.level.minimal");
477
String resMainDebugVal = resMainDebugOption.getStringValue();
479
IOption defaultResBarOptOption = resBarTool.getOptionById("testgnu.c.compiler.exe.debug.option.optimization.level");
480
String defaultResBarOptVal = defaultResBarOptOption.getStringValue();
482
IOption resBarOptOption = ManagedBuildManager.setOption(resBarConfig,resBarTool,defaultResBarOptOption,"gnu.c.optimization.level.more");
483
String resBarOptVal = resBarOptOption.getStringValue();
485
IOption defaultResBarDebugOption = resBarTool.getOptionById("testgnu.c.compiler.exe.debug.option.debugging.level");
486
String defaultResBarDebugVal = defaultResBarDebugOption.getStringValue();
488
IOption resBarDebugOption = ManagedBuildManager.setOption(resBarConfig,resBarTool,defaultResBarDebugOption,"gnu.c.debugging.level.default");
489
String resBarDebugVal = resBarDebugOption.getStringValue();
491
assertNotSame(defaultResMainOptVal, resMainOptVal);
492
assertNotSame(defaultResMainDebugVal, resMainDebugVal);
494
assertNotSame(defaultResBarOptVal, resBarOptVal);
495
assertNotSame(defaultResBarDebugVal, resBarDebugVal);
497
// Save and Close the project.
498
ManagedBuildManager.saveBuildInfo(project,false);
499
ManagedBuildManager.removeBuildInfo(project);
503
// Now reopen the project.
506
// Get the build info.
507
IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(project);
508
IManagedProject newManagedProj = info.getManagedProject();
510
// Verify that there are only two configurations.
511
IConfiguration[] definedConfigs = newManagedProj.getConfigurations();
512
assertEquals(2, definedConfigs.length);
514
// Get the default configuration and verify whether it is same as before.
515
IConfiguration newDefaultConfig = info.getDefaultConfiguration();
517
assertEquals(defaultConfigId, newDefaultConfig.getId());
519
// Get the resource configurations in defaultConfig
520
IResourceConfiguration newResConfigs[] = newDefaultConfig.getResourceConfigurations();
522
assertEquals(2, newResConfigs.length);
524
// Get the resource configuration for main.c using the path '/T1/main.c'
525
IResourceConfiguration newResMainConfig = newDefaultConfig.getResourceConfiguration(resMainPath);
526
assertNotNull(newResMainConfig);
528
ITool newResMainTools[] = newResMainConfig.getTools();
529
assertEquals(1,newResMainTools.length);
531
// Get the Optimization and Debug option values for the resource 'main.c'.
532
ITool newResMainTool = newResMainTools[0];
534
IOption newResMainOptOption = newResMainTool.getOptionById(resMainOptOption.getId());
535
assertNotNull(newResMainOptOption);
536
String newResMainOptVal = newResMainOptOption.getStringValue();
538
IOption newResMainDebugOption = newResMainTool.getOptionById(resMainDebugOption.getId());
539
assertNotNull(newResMainDebugOption);
540
String newResMainDebugVal = newResMainDebugOption.getStringValue();
542
// Assert that optimization & debug option values for the resource main.c are same between the sessions.
543
assertEquals(resMainOptVal, newResMainOptVal);
544
assertEquals(resMainDebugVal,newResMainDebugVal);
546
// Get the resource configuration for bar.c
547
IResourceConfiguration newResBarConfig = newDefaultConfig.getResourceConfiguration(resBarPath);
548
assertNotNull(newResBarConfig);
550
ITool newResBarTools[] = newResBarConfig.getTools();
551
assertEquals(1,newResBarTools.length);
553
//Get the Optimization and Debug option values for the resource 'bar.c'
554
ITool newResBarTool = newResBarTools[0];
556
IOption newResBarOptOption = newResBarTool.getOptionById(resBarOptOption.getId());
557
assertNotNull(newResBarOptOption);
558
String newResBarOptVal = newResBarOptOption.getStringValue();
560
IOption newResBarDebugOption = newResBarTool.getOptionById(resBarDebugOption.getId());
561
assertNotNull(newResBarDebugOption);
562
String newResBarDebugVal = newResBarDebugOption.getStringValue();
564
// Assert that optimization & debug option values for the resource main.c are same between the sessions.
565
assertEquals(resBarOptVal, newResBarOptVal);
566
assertEquals(resBarDebugVal,newResBarDebugVal);
568
// Close and remove project.
570
removeProject(projectName);
574
private IProject createProject(String name) throws CoreException {
575
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
576
final IProject newProjectHandle = root.getProject(name);
577
IProject project = null;
579
if (!newProjectHandle.exists()) {
580
IWorkspace workspace = ResourcesPlugin.getWorkspace();
581
IWorkspaceDescription workspaceDesc = workspace.getDescription();
582
workspaceDesc.setAutoBuilding(false);
583
workspace.setDescription(workspaceDesc);
584
IProjectDescription description = workspace.newProjectDescription(newProjectHandle.getName());
585
//description.setLocation(root.getLocation());
586
project = CCorePlugin.getDefault().createCProject(description, newProjectHandle, new NullProgressMonitor(), ManagedBuilderCorePlugin.MANAGED_MAKE_PROJECT_ID);
588
IWorkspace workspace = ResourcesPlugin.getWorkspace();
589
IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
590
public void run(IProgressMonitor monitor) throws CoreException {
591
newProjectHandle.refreshLocal(IResource.DEPTH_INFINITE, monitor);
594
NullProgressMonitor monitor = new NullProgressMonitor();
595
workspace.run(runnable, root, IWorkspace.AVOID_UPDATE, monitor);
596
project = newProjectHandle;
599
// Open the project if we have to
600
if (!project.isOpen()) {
601
project.open(new NullProgressMonitor());
608
* Remove the <code>IProject</code> with the name specified in the argument from the
609
* receiver's workspace.
613
private void removeProject(String name) {
614
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
615
IProject project = root.getProject(name);
616
if (project.exists()) {
619
} catch (InterruptedException e1) {
623
System.runFinalization();
624
project.delete(true, true, null);
625
} catch (CoreException e2) {
632
public void testProjectCreation() throws BuildException {
633
// Create new project
634
IProject project = null;
636
project = createProject(projectName);
637
// Now associate the builder with the project
638
ManagedBuildTestHelper.addManagedBuildNature(project);
639
IProjectDescription description = project.getDescription();
640
// Make sure it has a managed nature
641
if (description != null) {
642
assertTrue(description.hasNature(ManagedCProjectNature.MNG_NATURE_ID));
645
} catch (CoreException e) {
646
fail("Test failed on project creation: " + e.getLocalizedMessage());
649
// Find the base project type definition
650
IProjectType[] projTypes = ManagedBuildManager.getDefinedProjectTypes();
651
IProjectType projType = ManagedBuildManager.getProjectType("cdt.managedbuild.target.testgnu21.exe");
652
assertNotNull(projType);
654
// Create the managed-project (.cdtbuild) for our project that builds a dummy executable
655
IManagedProject newProject = ManagedBuildManager.createManagedProject(project, projType);
656
assertEquals(newProject.getName(), projType.getName());
657
assertFalse(newProject.equals(projType));
658
ManagedBuildManager.setNewProjectVersion(project);
660
// Copy over the configs
661
IConfiguration defaultConfig = null;
662
IConfiguration[] configs = projType.getConfigurations();
663
for (int i = 0; i < configs.length; ++i) {
664
// Make the first configuration the default
666
defaultConfig = newProject.createConfiguration(configs[i], projType.getId() + "." + i);
668
newProject.createConfiguration(configs[i], projType.getId() + "." + i);
671
ManagedBuildManager.setDefaultConfiguration(project, defaultConfig);
673
// Initialize the path entry container
674
IStatus initResult = ManagedBuildManager.initBuildInfoContainer(project);
675
if (initResult.getCode() != IStatus.OK) {
676
fail("Initializing build information failed for: " + project.getName() + " because: " + initResult.getMessage());
679
// Now test the results out
680
// checkRootManagedProject(newProject, "x");
682
// Override the "String Option in Category" option value
683
configs = newProject.getConfigurations();
684
ITool[] tools = configs[0].getTools();
685
IOptionCategory topCategory = tools[0].getTopOptionCategory();
686
IOptionCategory[] categories = topCategory.getChildCategories();
687
Object[][] options = categories[0].getOptions(configs[0]);
688
ITool tool = (ITool)options[0][0];
689
IOption option = (IOption)options[0][1];
690
configs[0].setOption(tool, option, "1");
691
options = categories[0].getOptions((IConfiguration)null);
692
tool = (ITool)options[0][0];
693
option = (IOption)options[0][1];
694
assertEquals("x", option.getStringValue());
695
options = categories[0].getOptions(configs[0]);
696
tool = (ITool)options[0][0];
697
option = (IOption)options[0][1];
698
assertEquals("z", option.getStringValue());
700
// Save, close, reopen and test again
701
ManagedBuildManager.saveBuildInfo(project, true);
702
ManagedBuildManager.removeBuildInfo(project);
705
} catch (CoreException e) {
706
fail("Failed on project close: " + e.getLocalizedMessage());
710
} catch (CoreException e) {
711
fail("Failed on project open: " + e.getLocalizedMessage());
714
// Test that the default config was remembered
715
IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(project);
716
assertEquals(defaultConfig.getId(), info.getDefaultConfiguration().getId());
718
// Check the rest of the default information
719
// checkRootManagedProject(newProject, "z");
721
// Now test the information the makefile builder needs
722
// checkBuildTestSettings(info);
723
ManagedBuildManager.removeBuildInfo(project);
726
public void testResourceRename() throws Exception {
727
// Create a new project
728
IProject project = null;
729
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
732
project = createProject(projectName);
734
// Now associate the builder with the project
735
ManagedBuildTestHelper.addManagedBuildNature(project);
736
IProjectDescription description = project.getDescription();
737
// Make sure it has a managed nature
738
if (description != null) {
739
assertTrue(description.hasNature(ManagedCProjectNature.MNG_NATURE_ID));
742
} catch (CoreException e) {
743
fail("Test failed on project creation: " + e.getLocalizedMessage());
746
// Find the base project type definition
747
IProjectType[] projTypes = ManagedBuildManager.getDefinedProjectTypes();
748
IProjectType projType = ManagedBuildManager.getProjectType("cdt.managedbuild.target.testgnu21.exe");
749
assertNotNull(projType);
751
// Create the managed-project (.cdtbuild) for our project that builds an executable.
752
IManagedProject newProject = ManagedBuildManager.createManagedProject(project, projType);
753
assertEquals(newProject.getName(), projType.getName());
754
assertFalse(newProject.equals(projType));
755
ManagedBuildManager.setNewProjectVersion(project);
757
// Create a couple of resources ( 'main.c' & 'bar.c')
758
IFile mainFile = project.getProject().getFile( "main.c" );
759
if( !mainFile.exists() ){
760
mainFile.create( new ByteArrayInputStream( "#include <stdio.h>\n extern void bar(); \n int main() { \nprintf(\"Hello, World!!\"); \n bar();\n return 0; }".getBytes() ), false, null );
763
IFile aFile = project.getProject().getFile( "a.c" );
764
if( !aFile.exists() ){
765
aFile.create( new ByteArrayInputStream( "#include <stdio.h>\n void bar() { \nprintf(\"Hello, bar()!!\");\n return; }".getBytes() ), false, null );
768
IFolder dirFolder = project.getProject().getFolder( "dir" );
769
if(!dirFolder.exists())
770
dirFolder.create(true,true,null);
771
IFile bFile = dirFolder.getFile( "b.c" );
772
if( !bFile.exists() ){
773
bFile.create( new ByteArrayInputStream( "#include <stdio.h>\n void bar1() { \nprintf(\"Hello, bar1()!!\");\n return; }".getBytes() ), false, null );
776
// Get the configurations and make one of them as default configuration.
777
IConfiguration defaultConfig = null;
778
IConfiguration[] configs = projType.getConfigurations();
779
for (int i = 0; i < configs.length; ++i) {
780
// Make the first configuration the default
782
defaultConfig = newProject.createConfiguration(configs[i], projType.getId() + "." + i);
784
newProject.createConfiguration(configs[i], projType.getId() + "." + i);
787
ManagedBuildManager.setDefaultConfiguration(project, defaultConfig);
789
// Create Resource Configurations for files main.c and bar.c
790
IResourceConfiguration resMainConfig = defaultConfig.createResourceConfiguration(mainFile);
791
IResourceConfiguration resAConfig = defaultConfig.createResourceConfiguration(aFile);
792
IResourceConfiguration resBConfig = defaultConfig.createResourceConfiguration(bFile);
794
// Check whether defaultConfig has three resource configurations or not.
795
IResourceConfiguration resConfigs[] = defaultConfig.getResourceConfigurations();
796
assertEquals(resMainConfig,defaultConfig.getResourceConfiguration(mainFile.getFullPath().toString()));
797
assertEquals(resAConfig,defaultConfig.getResourceConfiguration(aFile.getFullPath().toString()));
798
assertEquals(resBConfig,defaultConfig.getResourceConfiguration(bFile.getFullPath().toString()));
799
assertEquals(3,resConfigs.length);
802
mainFile.move(mainFile.getFullPath().removeLastSegments(1).append("main1.c"),true,false,null);
803
mainFile = (IFile)project.findMember("main1.c");
804
assertEquals(resMainConfig,defaultConfig.getResourceConfiguration(mainFile.getFullPath().toString()));
805
assertEquals(resAConfig,defaultConfig.getResourceConfiguration(aFile.getFullPath().toString()));
806
assertEquals(resBConfig,defaultConfig.getResourceConfiguration(bFile.getFullPath().toString()));
807
resConfigs = defaultConfig.getResourceConfigurations();
808
assertEquals(3,resConfigs.length);
810
dirFolder.move(dirFolder.getFullPath().removeLastSegments(1).append("dir1"),true,false,null);
811
dirFolder = (IFolder)project.findMember("dir1");
812
bFile = (IFile)dirFolder.findMember("b.c");
813
assertEquals(resMainConfig,defaultConfig.getResourceConfiguration(mainFile.getFullPath().toString()));
814
assertEquals(resAConfig,defaultConfig.getResourceConfiguration(aFile.getFullPath().toString()));
815
assertEquals(resBConfig,defaultConfig.getResourceConfiguration(bFile.getFullPath().toString()));
816
resConfigs = defaultConfig.getResourceConfigurations();
817
assertEquals(3,resConfigs.length);
819
bFile.move(bFile.getFullPath().removeLastSegments(1).append("b1.c"),true,false,null);
820
bFile = (IFile)dirFolder.findMember("b1.c");
821
assertEquals(resMainConfig,defaultConfig.getResourceConfiguration(mainFile.getFullPath().toString()));
822
assertEquals(resAConfig,defaultConfig.getResourceConfiguration(aFile.getFullPath().toString()));
823
assertEquals(resBConfig,defaultConfig.getResourceConfiguration(bFile.getFullPath().toString()));
824
resConfigs = defaultConfig.getResourceConfigurations();
825
assertEquals(3,resConfigs.length);
827
IProjectDescription des = project.getDescription();
828
des.setName(renamedProjectName1);
829
project.move(des,true,null);
830
project = (IProject)root.findMember(renamedProjectName1);
831
mainFile = (IFile)project.findMember("main1.c");
832
aFile = (IFile)project.findMember("a.c");
833
dirFolder = (IFolder)project.findMember("dir1");
834
bFile = (IFile)dirFolder.findMember("b1.c");
835
assertEquals(resMainConfig,defaultConfig.getResourceConfiguration(mainFile.getFullPath().toString()));
836
assertEquals(resAConfig,defaultConfig.getResourceConfiguration(aFile.getFullPath().toString()));
837
assertEquals(resBConfig,defaultConfig.getResourceConfiguration(bFile.getFullPath().toString()));
838
resConfigs = defaultConfig.getResourceConfigurations();
839
assertEquals(3,resConfigs.length);
841
final IResource rcBuf[] = new IResource[5];
845
rcBuf[3] = dirFolder;
848
ResourcesPlugin.getWorkspace().run( new IWorkspaceRunnable(){
850
public void run(IProgressMonitor monitor) throws CoreException {
851
IProject project = (IProject)rcBuf[0];
852
IFile mainFile = (IFile)rcBuf[1];
853
IFile aFile = (IFile)rcBuf[2];
854
IFolder dirFolder = (IFolder)rcBuf[3];
855
IFile bFile = (IFile)rcBuf[4];
856
// TODO Auto-generated method stub
857
mainFile.move(mainFile.getFullPath().removeLastSegments(1).append("main2.c"),true,false,null);
858
mainFile = (IFile)project.findMember("main2.c");
860
dirFolder.move(dirFolder.getFullPath().removeLastSegments(1).append("dir2"),true,false,null);
861
dirFolder = (IFolder)project.findMember("dir2");
862
bFile = (IFile)dirFolder.findMember("b1.c");
864
bFile.move(bFile.getFullPath().removeLastSegments(1).append("b2.c"),true,false,null);
865
bFile = (IFile)dirFolder.findMember("b2.c");
867
// project.move(project.getFullPath().removeLastSegments(1).append(renamedProjectName2),true,null);
868
IProjectDescription des = project.getDescription();
869
des.setName(renamedProjectName2);
870
project.move(des,true,null);
871
project = (IProject)ResourcesPlugin.getWorkspace().getRoot().findMember(renamedProjectName2);
873
mainFile = (IFile)project.findMember("main2.c");
874
aFile = (IFile)project.findMember("a.c");
875
dirFolder = (IFolder)project.findMember("dir2");
876
bFile = (IFile)dirFolder.findMember("b2.c");
881
rcBuf[3] = dirFolder;
887
IWorkspace.AVOID_UPDATE,
890
project = (IProject)rcBuf[0];
891
mainFile = (IFile)rcBuf[1];
892
aFile = (IFile)rcBuf[2];
893
dirFolder = (IFolder)rcBuf[3];
894
bFile = (IFile)rcBuf[4];
896
assertEquals(resMainConfig,defaultConfig.getResourceConfiguration(mainFile.getFullPath().toString()));
897
assertEquals(resAConfig,defaultConfig.getResourceConfiguration(aFile.getFullPath().toString()));
898
assertEquals(resBConfig,defaultConfig.getResourceConfiguration(bFile.getFullPath().toString()));
899
resConfigs = defaultConfig.getResourceConfigurations();
900
assertEquals(3,resConfigs.length);
902
aFile.delete(true,null);
903
// Check whether defaultConfig has two resource configurations or not.
904
assertEquals(resMainConfig,defaultConfig.getResourceConfiguration(mainFile.getFullPath().toString()));
905
assertEquals(null,defaultConfig.getResourceConfiguration(aFile.getFullPath().toString()));
906
assertEquals(resBConfig,defaultConfig.getResourceConfiguration(bFile.getFullPath().toString()));
907
resConfigs = defaultConfig.getResourceConfigurations();
908
assertEquals(2,resConfigs.length);
910
dirFolder.delete(true, null);
911
// Check whether defaultConfig has one resource configuration or not.
912
assertEquals(resMainConfig,defaultConfig.getResourceConfiguration(mainFile.getFullPath().toString()));
913
assertEquals(null,defaultConfig.getResourceConfiguration(aFile.getFullPath().toString()));
914
assertEquals(null,defaultConfig.getResourceConfiguration(bFile.getFullPath().toString()));
915
resConfigs = defaultConfig.getResourceConfigurations();
916
assertEquals(1,resConfigs.length);
918
// Close and remove project.
920
removeProject(renamedProjectName2);