Skip to main content
summaryrefslogtreecommitdiffstats
blob: 651b11a09c5798456e0d573f2221c1b6d09d713d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/*******************************************************************************
 * Copyright (c) 2005 Intel Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Intel Corporation - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.managedbuilder.core.tests;

import java.io.ByteArrayInputStream;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.make.core.MakeCorePlugin;
import org.eclipse.cdt.managedbuilder.core.BuildException;
import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
import org.eclipse.cdt.managedbuilder.core.IProjectType;
import org.eclipse.cdt.managedbuilder.core.IManagedProject;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IResourceConfiguration;
import org.eclipse.cdt.managedbuilder.core.ITool;
import org.eclipse.cdt.managedbuilder.core.IOption;
import org.eclipse.cdt.managedbuilder.core.IOptionCategory;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.cdt.managedbuilder.core.ManagedCProjectNature;
import org.eclipse.cdt.managedbuilder.internal.core.Tool;
import org.eclipse.cdt.managedbuilder.testplugin.ManagedBuildTestHelper;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceDescription;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;


public class ResourceBuildCoreTests extends TestCase {
	private static final boolean boolVal = true;
	private static IProjectType exeType;	
	private static IProjectType libType;
	private static IProjectType dllType;
	
	
	private static final String projectName = "T1";
	
		
	
	public ResourceBuildCoreTests(String name) {
		super(name);
	}
	
	public static Test suite() {
		TestSuite suite = new TestSuite(ResourceBuildCoreTests.class.getName());		
		suite.addTest(new ResourceBuildCoreTests("testResourceConfigurations"));
		suite.addTest(new ResourceBuildCoreTests("testResourceConfigurationReset"));
		suite.addTest(new ResourceBuildCoreTests("testResourceConfigurationBuildInfo"));
		return suite;
	}

	/**
	 * Creates a couple of resource configurations.
	 * Checks whether the  resource & project default build properties are same or not. 
	 * Overrides project build properties and checks whether they are reflecting at resource level.
	 * Overrides resource build properties and checks whether they are reflecting at project level.
	 * 
	 */
	
	public void testResourceConfigurations() throws Exception {
			
		// Create a new project
		IProject project = null;
		
		try {
			project = createProject(projectName);
			
			// Now associate the builder with the project
			ManagedBuildTestHelper.addManagedBuildNature(project);
			IProjectDescription description = project.getDescription();
			// Make sure it has a managed nature
			if (description != null) {
				assertTrue(description.hasNature(ManagedCProjectNature.MNG_NATURE_ID));
			}

		} catch (CoreException e) {
			fail("Test failed on project creation: " + e.getLocalizedMessage());
		}
	
		// Find the base project type definition
		IProjectType[] projTypes = ManagedBuildManager.getDefinedProjectTypes();
		IProjectType projType = ManagedBuildManager.getProjectType("cdt.managedbuild.target.testgnu21.exe");
		assertNotNull(projType);
		
		// Create the managed-project (.cdtbuild) for our project that builds an executable.
		IManagedProject newProject = ManagedBuildManager.createManagedProject(project, projType);
		assertEquals(newProject.getName(), projType.getName());
		assertFalse(newProject.equals(projType));
		ManagedBuildManager.setNewProjectVersion(project);
			
		// Create a couple of resources ( 'main.c' & 'bar.c')
		IFile mainFile = project.getProject().getFile( "main.c" );
		if( !mainFile.exists() ){
			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 );
		}
		
		IFile barFile = project.getProject().getFile( "bar.c" );
		if( !barFile.exists() ){
			barFile.create( new ByteArrayInputStream( "#include <stdio.h>\n void bar() { \nprintf(\"Hello, bar()!!\");\n return; }".getBytes() ), false, null );
		}
	
		// Get the configurations and make one of them as default configuration.
		IConfiguration defaultConfig = null;
		IConfiguration[] configs = projType.getConfigurations();
		for (int i = 0; i < configs.length; ++i) {
			// Make the first configuration the default 
			if (i == 0) {
				defaultConfig = newProject.createConfiguration(configs[i], projType.getId() + "." + i);
			} else {
				newProject.createConfiguration(configs[i], projType.getId() + "." + i);
			}
		}
		ManagedBuildManager.setDefaultConfiguration(project, defaultConfig);

		// Create Resource Configurations for files main.c and bar.c
		IResourceConfiguration resMainConfig = defaultConfig.createResourceConfiguration(mainFile);
		IResourceConfiguration resBarConfig = defaultConfig.createResourceConfiguration(barFile);
		
		// Check whether defaultConfig has two resource configurations or not.
		IResourceConfiguration resConfigs[] = defaultConfig.getResourceConfigurations();
		assertEquals(2,resConfigs.length);
		
		// Get the tools associated with the resource 'main.c'.
		ITool resMainTools[] = resMainConfig.getTools();
		assertNotNull(resMainTools);
		assertEquals(1,resMainTools.length);
		
		// Get the tools associated with the resource 'bar.c'.
		ITool resBarTools[] = resBarConfig.getTools();
		assertNotNull(resBarTools);
		assertEquals(1,resBarTools.length);

		// Get the build properties for the resource main.c
		ITool resMainTool = resMainTools[0];
		
		String resMainBuildProps = resMainTool.getToolFlags();
			
		// Get the build properties for the resource bar.c
		ITool resBarTool = resBarTools[0];
		String resBarBuildProps = resBarTool.getToolFlags();
		
		//	Get file extension.
		String extString = mainFile.getFileExtension();
		
		// Get the project build properties.
		ITool tools[] = defaultConfig.getFilteredTools();
		Tool projTool = null;
		String projBuildProps = new String();
		for (int i = 0; i < tools.length; i++) {
			if( tools[i].buildsFileType(extString) ) {
				// Get the build properties of a project in default configuration
				projTool = (Tool)tools[i];
				projBuildProps = projTool.getToolFlags();
				break;
			}
		}
		
		// Initially, Project build properties and resource build properties are same.
		assertEquals(resMainBuildProps,projBuildProps);
				
		// Initially, build properties of files with same extension ( example , .c files) are equal.
		assertEquals(resMainBuildProps,resBarBuildProps);
		
		// Now modify project build properties and it should reflect in resource build properties also.
			
		IOption projDebugOption = projTool.getOptionById("testgnu.c.compiler.exe.debug.option.debugging.level");
			
		assertNotNull(projDebugOption);
		
		// Override options in the default configuration.
		// Set the debug option value to '-g2' at Project level
		IOption newProjDebugOption = ManagedBuildManager.setOption(defaultConfig,projTool,projDebugOption,"testgnu.c.debugging.level.default");
		
		// Get the option 'id' and 'value'.
		String newProjDebugOptionId = newProjDebugOption.getId();
		String newProjDebugOptionValue  = newProjDebugOption.getStringValue();
		
		// Assert old & new(overridden) debug option values of project are different.
		assertNotSame(projDebugOption.getStringValue(),newProjDebugOptionValue);
		
		// Check whether the overridden option at project level is reflecting at resource level or not.			
		IOption resMainDebugOption = null;
				
		IOption resMainOptions[] = resMainTool.getOptions();
		
		for(int i=0; i< resMainOptions.length; i++){
			IOption opt = resMainOptions[i];
			if( opt != null ) {
				if(opt.getId().equals(newProjDebugOptionId)){
					// Resource Configuration doesnot have overridden value for this option.
					resMainDebugOption = opt;
					break;
				}
				if ( opt.getSuperClass() != null ) {
					if(opt.getSuperClass().getId().equals(newProjDebugOptionId)){
						// Resource Configuration does have overridden value for this option.
						resMainDebugOption = opt;
						break;	
					}	
				}
			}
		}
		
		String resMainDebugOptionValue = resMainDebugOption.getStringValue();
		
		// Assert Debug option values of project and resource are same.
		assertEquals(newProjDebugOptionValue, resMainDebugOptionValue);
		
		// Now, Modify the Debug option at resource level
		// and verify whether the modified option is reflected at project level. 
		// It should not reflect at project level.
		
		IOption newResMainDebugOption = ManagedBuildManager.setOption(resMainConfig,resMainTool,resMainDebugOption,"gnu.c.debugging.level.minimal");		
				
		//Get the latest project Debug option.
		tools = defaultConfig.getFilteredTools();
		projTool = null;
		
		for (int i = 0; i < tools.length; i++) {
			if( tools[i].buildsFileType(extString) ) {
				// Get the build properties of a project in default configuration
				projTool = (Tool)tools[i];
				break;
			}
		}
		
		projDebugOption = projTool.getOptionById(newProjDebugOptionId);
		String projDebugOptionValue = projDebugOption.getStringValue();
		
		String newResMainDebugOptionValue = newResMainDebugOption.getStringValue();
		
		// Assert the debug option values of project and resource are different.		
		assertNotSame(projDebugOptionValue, newResMainDebugOptionValue);
		
		// Close and remove project.
		project.close(null);
		removeProject(projectName);
	}
	
	/*
	 * 	Creates a project and a resource(hello.c).
	 * 	Overrides the build properties in resource configuration, and
	 * 	resets the resource configuration, verifies whether the overridden
	 * 	values still exist or not.
	 */
	
	public void testResourceConfigurationReset() throws Exception {
		
		// Create a new project
		IProject project = null;
		
		try {
			project = createProject(projectName);
			
			// Now associate the builder with the project
			ManagedBuildTestHelper.addManagedBuildNature(project);
			IProjectDescription description = project.getDescription();
			// Make sure it has a managed nature
			if (description != null) {
				assertTrue(description.hasNature(ManagedCProjectNature.MNG_NATURE_ID));
			}

		} catch (CoreException e) {
			fail("Test failed on project creation: " + e.getLocalizedMessage());
		}
	
		// Find the base project type definition
		IProjectType[] projTypes = ManagedBuildManager.getDefinedProjectTypes();
		IProjectType projType = ManagedBuildManager.getProjectType("cdt.managedbuild.target.testgnu21.exe");
		assertNotNull(projType);
		
		// Create the managed-project (.cdtbuild) for our project that builds an executable.
		IManagedProject newProject = ManagedBuildManager.createManagedProject(project, projType);
		assertEquals(newProject.getName(), projType.getName());
		assertFalse(newProject.equals(projType));
		ManagedBuildManager.setNewProjectVersion(project);
			
		// Create a resource ( 'hello.c')
		IFile helloFile = project.getProject().getFile( "hello.c" );
		if( !helloFile.exists() ){
			helloFile.create( new ByteArrayInputStream( "#include <stdio.h>\n\n int main() { \nprintf(\"Hello, World!!\"); \n bar();\n return 0; }".getBytes() ), false, null );
		}
	
		// Get the configurations and make one of them as default configuration.
		IConfiguration defaultConfig = null;
		IConfiguration[] configs = projType.getConfigurations();
		for (int i = 0; i < configs.length; ++i) {
			// Make the first configuration the default 
			if (i == 0) {
				defaultConfig = newProject.createConfiguration(configs[i], projType.getId() + "." + i);
			} else {
				newProject.createConfiguration(configs[i], projType.getId() + "." + i);
			}
		}
		ManagedBuildManager.setDefaultConfiguration(project, defaultConfig);

		// Create Resource Configurations for hello.c
		IResourceConfiguration resConfig = defaultConfig.createResourceConfiguration(helloFile);
		
		// Check whether defaultConfig has the correct number of resource configurations or not.
		IResourceConfiguration resConfigs[] = defaultConfig.getResourceConfigurations();
		assertEquals(1,resConfigs.length);
		
		// Get the tools associated with the resource 'hello.c'.
		ITool resTools[] = resConfig.getTools();
		assertNotNull(resTools);
		assertEquals(1,resTools.length);
		
		// Get the build properties for the resource hello.c
		ITool resTool = resTools[0];
		String defaultResToolFlags = resTool.getToolFlags();
		
		// Get the Debug Option.
		IOption resDebugOption = resTool.getOptionById("testgnu.c.compiler.exe.debug.option.debugging.level");
		
		// Get the default value of debug option for resource.
		String defaultResDebugOptVal = resDebugOption.getStringValue();
		
		// Now, override the value with "gnu.c.debugging.level.minimal"
		IOption newResDebugOption = ManagedBuildManager.setOption(resConfig,resTool,resDebugOption,"gnu.c.debugging.level.minimal");
		
		// Get the overridden value of debug option.
		String newResDebugOptVal = newResDebugOption.getStringValue();
		String newResToolFlags = resTool.getToolFlags();
		
		// Make sure, default and overridden values are different.
		assertNotSame(defaultResDebugOptVal,newResDebugOptVal);
		
		// Reset the resource configuration.
		ManagedBuildManager.resetResourceConfiguration(project,resConfig);
		String resetResToolFlags = resTool.getToolFlags();
		
		assertNotSame(resetResToolFlags,newResToolFlags);
		assertEquals(defaultResToolFlags,resetResToolFlags);
		
		// Close and remove project.
		project.close(null);
		removeProject(projectName);
	}
	
	/*
	 * 	Creates a project and a couple of resources.
	 * 	Overrides the build properties of resources. Saves, closes, and reopens 
	 * 	the project. Then, checks the overridden options. Basically, this function
	 * 	tests persisting overridden resource build properties between project sessions.
	 */
	
	public void testResourceConfigurationBuildInfo() throws Exception {
		
		//	Create a new project
		IProject project = null;
		
		try {
			project = createProject(projectName);
			
			// Now associate the builder with the project
			ManagedBuildTestHelper.addManagedBuildNature(project);
			IProjectDescription description = project.getDescription();
			// Make sure it has a managed nature
			if (description != null) {
				assertTrue(description.hasNature(ManagedCProjectNature.MNG_NATURE_ID));
			}

		} catch (CoreException e) {
			fail("Test failed on project creation: " + e.getLocalizedMessage());
		}
	
		// Find the base project type definition
		IProjectType[] projTypes = ManagedBuildManager.getDefinedProjectTypes();
		IProjectType projType = ManagedBuildManager.getProjectType("cdt.managedbuild.target.testgnu21.exe");
		assertNotNull(projType);
		
		// Create the managed-project (.cdtbuild) for our project that builds an executable.
		IManagedProject newProject = ManagedBuildManager.createManagedProject(project, projType);
		assertEquals(newProject.getName(), projType.getName());
		assertFalse(newProject.equals(projType));
		ManagedBuildManager.setNewProjectVersion(project);
			
		// Create a couple of resources ( 'main.c' & 'bar.c')
		IFile mainFile = project.getProject().getFile( "main.c" );
		if( !mainFile.exists() ){
			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 );
		}
		
		IFile barFile = project.getProject().getFile( "bar.c" );
		if( !barFile.exists() ){
			barFile.create( new ByteArrayInputStream( "#include <stdio.h>\n void bar() { \nprintf(\"Hello, bar()!!\");\n return; }".getBytes() ), false, null );
		}
	
		// Get the configurations and make one of them as default configuration.
		IConfiguration defaultConfig = null;
		IConfiguration[] configs = projType.getConfigurations();
		for (int i = 0; i < configs.length; ++i) {
			// Make the first configuration the default 
			if (i == 0) {
				defaultConfig = newProject.createConfiguration(configs[i], projType.getId() + "." + i);
			} else {
				newProject.createConfiguration(configs[i], projType.getId() + "." + i);
			}
		}
		ManagedBuildManager.setDefaultConfiguration(project, defaultConfig);
		
		// Get the default configuration id.
		String defaultConfigId = defaultConfig.getId();

		// Create Resource Configurations for files main.c and bar.c
		IResourceConfiguration resMainConfig = defaultConfig.createResourceConfiguration(mainFile);
		IResourceConfiguration resBarConfig = defaultConfig.createResourceConfiguration(barFile);
		
		// Check whether defaultConfig has two resource configurations or not.
		IResourceConfiguration resConfigs[] = defaultConfig.getResourceConfigurations();
		assertEquals(2,resConfigs.length);

		// Get the paths of resource configurations.
		String resMainPath = resMainConfig.getResourcePath();
		String resBarPath  = resBarConfig.getResourcePath();
		
		// Get the tools associated with the resource 'main.c'.
		ITool resMainTools[] = resMainConfig.getTools();
		assertNotNull(resMainTools);
		assertEquals(1,resMainTools.length);
		
		// Get the tools associated with the resource 'bar.c'.
		ITool resBarTools[] = resBarConfig.getTools();
		assertNotNull(resBarTools);
		assertEquals(1,resBarTools.length);

		// Get the build properties for the resource main.c
		ITool resMainTool = resMainTools[0];
		
		String defaultMainBuildProps = resMainTool.getToolFlags();
			
		// Get the build properties for the resource bar.c
		ITool resBarTool = resBarTools[0];
		String defaultBarBuildProps = resBarTool.getToolFlags();
		
		
		// Now, override debug and optimization options.
		// In Debug Configuration, Currently default values in resource configurations are
		//	optimization : -O0,  debug : -g3
		//  Override the options in the following way.
		//  main.c :    optimization : '-O1'  debug : '-g1'
		//  bar.c  :    optimization : '-O2'  debug : '-g2'
		
		IOption defaultResMainOptOption = resMainTool.getOptionById("testgnu.c.compiler.exe.debug.option.optimization.level");
		String defaultResMainOptVal = defaultResMainOptOption.getStringValue();
		
		IOption resMainOptOption = ManagedBuildManager.setOption(resMainConfig,resMainTool,defaultResMainOptOption,"gnu.c.optimization.level.optimize");
		String resMainOptVal = resMainOptOption.getStringValue();
		
		IOption defaultResMainDebugOption = resMainTool.getOptionById("testgnu.c.compiler.exe.debug.option.debugging.level");
		String defaultResMainDebugVal = defaultResMainDebugOption.getStringValue();
	
		IOption resMainDebugOption = ManagedBuildManager.setOption(resMainConfig,resMainTool,defaultResMainDebugOption,"gnu.c.debugging.level.minimal");
		String resMainDebugVal = resMainDebugOption.getStringValue();
		
		IOption defaultResBarOptOption = resBarTool.getOptionById("testgnu.c.compiler.exe.debug.option.optimization.level");
		String defaultResBarOptVal = defaultResBarOptOption.getStringValue();
		
		IOption resBarOptOption = ManagedBuildManager.setOption(resBarConfig,resBarTool,defaultResBarOptOption,"gnu.c.optimization.level.more");
		String resBarOptVal  = resBarOptOption.getStringValue();
		
		IOption defaultResBarDebugOption = resBarTool.getOptionById("testgnu.c.compiler.exe.debug.option.debugging.level");
		String defaultResBarDebugVal = defaultResBarDebugOption.getStringValue();
		
		IOption resBarDebugOption = ManagedBuildManager.setOption(resBarConfig,resBarTool,defaultResBarDebugOption,"gnu.c.debugging.level.default");
		String resBarDebugVal = resBarDebugOption.getStringValue();
		
		assertNotSame(defaultResMainOptVal, resMainOptVal);
		assertNotSame(defaultResMainDebugVal, resMainDebugVal);
		
		assertNotSame(defaultResBarOptVal, resBarOptVal);
		assertNotSame(defaultResBarDebugVal, resBarDebugVal);
		
		// Save and Close the project.
		ManagedBuildManager.saveBuildInfo(project,false);
		ManagedBuildManager.removeBuildInfo(project);
		
		project.close(null);
		
		// Now reopen the project.
		project.open(null);
		
		// Get the build info.
		IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(project);
		IManagedProject newManagedProj = info.getManagedProject();
		
		// Verify that there are only two configurations.
		IConfiguration[] definedConfigs = newManagedProj.getConfigurations(); 		
		assertEquals(2, definedConfigs.length);
		
		// Get the default configuration and verify whether it is same as before.
		IConfiguration newDefaultConfig = info.getDefaultConfiguration();
		
		assertEquals(defaultConfigId, newDefaultConfig.getId());
		
		// Get the resource configurations in defaultConfig
		IResourceConfiguration newResConfigs[] = newDefaultConfig.getResourceConfigurations();
		
		assertEquals(2, newResConfigs.length);
		
		// Get the resource configuration for main.c using the path '/T1/main.c' 
		IResourceConfiguration newResMainConfig = newDefaultConfig.getResourceConfiguration(resMainPath);
		assertNotNull(newResMainConfig);
		
		ITool newResMainTools[] = newResMainConfig.getTools();
		assertEquals(1,newResMainTools.length);
		
		// Get the Optimization and Debug option values for the resource 'main.c'.
		ITool newResMainTool = newResMainTools[0];
		
		IOption newResMainOptOption = newResMainTool.getOptionById(resMainOptOption.getId());
		assertNotNull(newResMainOptOption);
		String newResMainOptVal = newResMainOptOption.getStringValue();
		
		IOption newResMainDebugOption = newResMainTool.getOptionById(resMainDebugOption.getId());
		assertNotNull(newResMainDebugOption);
		String newResMainDebugVal = newResMainDebugOption.getStringValue();
		
		// Assert that optimization & debug option values for the resource main.c are same between the sessions.
		assertEquals(resMainOptVal, newResMainOptVal);
		assertEquals(resMainDebugVal,newResMainDebugVal);
		
		// Get the resource configuration for bar.c
		IResourceConfiguration newResBarConfig = newDefaultConfig.getResourceConfiguration(resBarPath);
		assertNotNull(newResBarConfig);
		
		ITool newResBarTools[] = newResBarConfig.getTools();
		assertEquals(1,newResBarTools.length);
		
		//Get the Optimization and Debug option values for the resource 'bar.c'
		ITool newResBarTool = newResBarTools[0];
		
		IOption newResBarOptOption = newResBarTool.getOptionById(resBarOptOption.getId());
		assertNotNull(newResBarOptOption);
		String newResBarOptVal = newResBarOptOption.getStringValue();
		
		IOption newResBarDebugOption = newResBarTool.getOptionById(resBarDebugOption.getId());
		assertNotNull(newResBarDebugOption);
		String newResBarDebugVal = newResBarDebugOption.getStringValue();
		
		// Assert that optimization & debug option values for the resource main.c are same between the sessions.
		assertEquals(resBarOptVal, newResBarOptVal);
		assertEquals(resBarDebugVal,newResBarDebugVal);
		
		//	Close and remove project.
		project.close(null);
		removeProject(projectName);
		
	}
	
	private IProject createProject(String name) throws CoreException {
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		final IProject newProjectHandle = root.getProject(name);
		IProject project = null;
		
		if (!newProjectHandle.exists()) {
			IWorkspace workspace = ResourcesPlugin.getWorkspace();
			IWorkspaceDescription workspaceDesc = workspace.getDescription();
			workspaceDesc.setAutoBuilding(false);
			workspace.setDescription(workspaceDesc);
			IProjectDescription description = workspace.newProjectDescription(newProjectHandle.getName());
			//description.setLocation(root.getLocation());
			project = CCorePlugin.getDefault().createCProject(description, newProjectHandle, new NullProgressMonitor(), MakeCorePlugin.MAKE_PROJECT_ID);
		} else {
			IWorkspace workspace = ResourcesPlugin.getWorkspace();
			IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
				public void run(IProgressMonitor monitor) throws CoreException {
					newProjectHandle.refreshLocal(IResource.DEPTH_INFINITE, monitor);
				}
			};
			NullProgressMonitor monitor = new NullProgressMonitor();
			workspace.run(runnable, root, IWorkspace.AVOID_UPDATE, monitor);
			project = newProjectHandle;
		}
        
		// Open the project if we have to
		if (!project.isOpen()) {
			project.open(new NullProgressMonitor());
		}
				
		return project;	
	}
	
	/**
	 * Remove the <code>IProject</code> with the name specified in the argument from the 
	 * receiver's workspace.
	 *  
	 * @param name
	 */
	private void removeProject(String name) {
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		IProject project = root.getProject(name);
		if (project.exists()) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e1) {
			} finally {
				try {
					System.gc();
					System.runFinalization();
					project.delete(true, true, null);
				} catch (CoreException e2) {
					assertTrue(false);
				}
			}
		}
	}
	
	public void testProjectCreation() throws BuildException {
		// Create new project
		IProject project = null;
		try {
			project = createProject(projectName);
			// Now associate the builder with the project
			ManagedBuildTestHelper.addManagedBuildNature(project);
			IProjectDescription description = project.getDescription();
			// Make sure it has a managed nature
			if (description != null) {
				assertTrue(description.hasNature(ManagedCProjectNature.MNG_NATURE_ID));
			}

		} catch (CoreException e) {
			fail("Test failed on project creation: " + e.getLocalizedMessage());
		}
	
		// Find the base project type definition
		IProjectType[] projTypes = ManagedBuildManager.getDefinedProjectTypes();
		IProjectType projType = ManagedBuildManager.getProjectType("cdt.managedbuild.target.testgnu21.exe");
		assertNotNull(projType);
		
		// Create the managed-project (.cdtbuild) for our project that builds a dummy executable
		IManagedProject newProject = ManagedBuildManager.createManagedProject(project, projType);
		assertEquals(newProject.getName(), projType.getName());
		assertFalse(newProject.equals(projType));
		ManagedBuildManager.setNewProjectVersion(project);
		
		// Copy over the configs
		IConfiguration defaultConfig = null;
		IConfiguration[] configs = projType.getConfigurations();
		for (int i = 0; i < configs.length; ++i) {
			// Make the first configuration the default 
			if (i == 0) {
				defaultConfig = newProject.createConfiguration(configs[i], projType.getId() + "." + i);
			} else {
				newProject.createConfiguration(configs[i], projType.getId() + "." + i);
			}
		}
		ManagedBuildManager.setDefaultConfiguration(project, defaultConfig);

		// Initialize the path entry container
		IStatus initResult = ManagedBuildManager.initBuildInfoContainer(project);
		if (initResult.getCode() != IStatus.OK) {
			fail("Initializing build information failed for: " + project.getName() + " because: " + initResult.getMessage());
		}
		
		// Now test the results out
	//	checkRootManagedProject(newProject, "x");
		
		// Override the "String Option in Category" option value
		configs = newProject.getConfigurations();
		ITool[] tools = configs[0].getTools();
		IOptionCategory topCategory = tools[0].getTopOptionCategory();
		IOptionCategory[] categories = topCategory.getChildCategories();
		Object[][] options = categories[0].getOptions(configs[0]);
		ITool tool = (ITool)options[0][0];
		IOption option = (IOption)options[0][1];
		configs[0].setOption(tool, option, "1");
		options = categories[0].getOptions((IConfiguration)null);
		tool = (ITool)options[0][0];
		option = (IOption)options[0][1];
		assertEquals("x", option.getStringValue());
		options = categories[0].getOptions(configs[0]);
		tool = (ITool)options[0][0];
		option = (IOption)options[0][1];
		assertEquals("z", option.getStringValue());
		
		// Save, close, reopen and test again
		ManagedBuildManager.saveBuildInfo(project, true);
		ManagedBuildManager.removeBuildInfo(project);
		try {
			project.close(null);
		} catch (CoreException e) {
			fail("Failed on project close: " + e.getLocalizedMessage());
		}
		try {
			project.open(null);
		} catch (CoreException e) {
			fail("Failed on project open: " + e.getLocalizedMessage());
		}
		
		// Test that the default config was remembered
		IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(project);
		assertEquals(defaultConfig.getId(), info.getDefaultConfiguration().getId());

		// Check the rest of the default information
//		checkRootManagedProject(newProject, "z");
		
		// Now test the information the makefile builder needs
	//	checkBuildTestSettings(info);
		ManagedBuildManager.removeBuildInfo(project);
	}
	
}	

Back to the top