From 1e95e361b62ef48a5dc9be0e12480c41870bf80e Mon Sep 17 00:00:00 2001 From: Leo Treggiari Date: Wed, 2 Feb 2005 20:07:34 +0000 Subject: [PATCH] Add tests for resource configurations --- .../tests/suite/AllManagedBuildTests.java | 4 +- .../core/tests/ResourceBuildCoreTests.java | 744 ++++++++++++++++++ 2 files changed, 747 insertions(+), 1 deletion(-) create mode 100644 build/org.eclipse.cdt.managedbuilder.core.tests/tests/org/eclipse/cdt/managedbuild/core/tests/ResourceBuildCoreTests.java diff --git a/build/org.eclipse.cdt.managedbuilder.core.tests/suite/org/eclipse/cdt/managedbuilder/tests/suite/AllManagedBuildTests.java b/build/org.eclipse.cdt.managedbuilder.core.tests/suite/org/eclipse/cdt/managedbuilder/tests/suite/AllManagedBuildTests.java index 53668ce37c8..b6b3f3729de 100644 --- a/build/org.eclipse.cdt.managedbuilder.core.tests/suite/org/eclipse/cdt/managedbuilder/tests/suite/AllManagedBuildTests.java +++ b/build/org.eclipse.cdt.managedbuilder.core.tests/suite/org/eclipse/cdt/managedbuilder/tests/suite/AllManagedBuildTests.java @@ -15,6 +15,7 @@ import org.eclipse.cdt.managedbuild.core.tests.ManagedBuildCoreTests; import org.eclipse.cdt.managedbuild.core.tests.ManagedBuildCoreTests20; import org.eclipse.cdt.managedbuild.core.tests.ManagedCommandLineGeneratorTest; import org.eclipse.cdt.managedbuild.core.tests.ManagedProjectUpdateTests; +import org.eclipse.cdt.managedbuild.core.tests.ResourceBuildCoreTests; import junit.framework.Test; import junit.framework.TestSuite; @@ -35,7 +36,8 @@ public class AllManagedBuildTests { suite.addTest(ManagedBuildCoreTests20.suite()); suite.addTest(ManagedBuildCoreTests.suite()); suite.addTest(ManagedProjectUpdateTests.suite()); - suite.addTest( ManagedCommandLineGeneratorTest.suite() ); + suite.addTest(ManagedCommandLineGeneratorTest.suite()); + suite.addTest(ResourceBuildCoreTests.suite()); //$JUnit-END$ return suite; } diff --git a/build/org.eclipse.cdt.managedbuilder.core.tests/tests/org/eclipse/cdt/managedbuild/core/tests/ResourceBuildCoreTests.java b/build/org.eclipse.cdt.managedbuilder.core.tests/tests/org/eclipse/cdt/managedbuild/core/tests/ResourceBuildCoreTests.java new file mode 100644 index 00000000000..d1566ae3db5 --- /dev/null +++ b/build/org.eclipse.cdt.managedbuilder.core.tests/tests/org/eclipse/cdt/managedbuild/core/tests/ResourceBuildCoreTests.java @@ -0,0 +1,744 @@ +/********************************************************************** + * Copyright (c) 2005 Intel Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * Intel Corporation - Initial API and implementation + **********************************************************************/ +package org.eclipse.cdt.managedbuild.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.core.ICDescriptor; +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.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.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +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 + 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.testgnu.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 \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 \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 + 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.testgnu.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 \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 + 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.testgnu.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 \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 \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(); + 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 { + newProjectHandle.refreshLocal(IResource.DEPTH_INFINITE, null); + project = newProjectHandle; + } + + // Open the project if we have to + if (!project.isOpen()) { + project.open(new NullProgressMonitor()); + } + + return project; + } + + /** + * Remove the IProject 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 + 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.testgnu.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); + } + + private void addManagedBuildNature (IProject project) { + // Create the buildinformation object for the project + IManagedBuildInfo info = ManagedBuildManager.createBuildInfo(project); + info.setValid(true); + + // Add the managed build nature + try { + ManagedCProjectNature.addManagedNature(project, new NullProgressMonitor()); + // ManagedCProjectNature.addManagedBuilder(project, new NullProgressMonitor()); + } catch (CoreException e) { + fail("Test failed on adding managed build nature or builder: " + e.getLocalizedMessage()); + } + + // Associate the project with the managed builder so the clients can get proper information + ICDescriptor desc = null; + try { + desc = CCorePlugin.getDefault().getCProjectDescription(project, true); + desc.remove(CCorePlugin.BUILD_SCANNER_INFO_UNIQ_ID); + desc.create(CCorePlugin.BUILD_SCANNER_INFO_UNIQ_ID, ManagedBuildManager.INTERFACE_IDENTITY); + } catch (CoreException e) { + fail("Test failed on adding managed builder as scanner info provider: " + e.getLocalizedMessage()); + } + try { + desc.saveProjectData(); + } catch (CoreException e) { + fail("Test failed on saving the ICDescriptor data: " + e.getLocalizedMessage()); } + } + + +} \ No newline at end of file