diff --git a/build/org.eclipse.cdt.managedbuilder.core.tests/plugin.xml b/build/org.eclipse.cdt.managedbuilder.core.tests/plugin.xml index 7a25e5e08f9..4b200cbdd1a 100644 --- a/build/org.eclipse.cdt.managedbuilder.core.tests/plugin.xml +++ b/build/org.eclipse.cdt.managedbuilder.core.tests/plugin.xml @@ -3589,5 +3589,246 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 561a081a1ca..7582d4c4bd4 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 @@ -10,19 +10,22 @@ **********************************************************************/ package org.eclipse.cdt.managedbuilder.tests.suite; +import junit.framework.Test; +import junit.framework.TestSuite; + import org.eclipse.cdt.core.CCorePlugin; -import org.eclipse.cdt.managedbuilder.core.tests.ManagedBuildCoreTests_SharedToolOptions; import org.eclipse.cdt.managedbuilder.core.tests.ManagedBuildCoreTests; import org.eclipse.cdt.managedbuilder.core.tests.ManagedBuildCoreTests20; +import org.eclipse.cdt.managedbuilder.core.tests.ManagedBuildCoreTests_SharedToolOptions; +import org.eclipse.cdt.managedbuilder.core.tests.ManagedBuildEnvironmentTests; +import org.eclipse.cdt.managedbuilder.core.tests.ManagedBuildMacrosTests; +import org.eclipse.cdt.managedbuilder.core.tests.ManagedBuildTCSupportedTest; import org.eclipse.cdt.managedbuilder.core.tests.ManagedCommandLineGeneratorTest; import org.eclipse.cdt.managedbuilder.core.tests.ManagedProject21MakefileTests; import org.eclipse.cdt.managedbuilder.core.tests.ManagedProject30MakefileTests; import org.eclipse.cdt.managedbuilder.core.tests.ManagedProjectUpdateTests; import org.eclipse.cdt.managedbuilder.core.tests.ResourceBuildCoreTests; -import junit.framework.Test; -import junit.framework.TestSuite; - /** * */ @@ -49,6 +52,9 @@ public class AllManagedBuildTests { suite.addTest(ManagedProject21MakefileTests.suite()); suite.addTest(ManagedProject30MakefileTests.suite()); suite.addTest(ManagedBuildCoreTests_SharedToolOptions.suite()); + suite.addTest(ManagedBuildEnvironmentTests.suite()); + suite.addTest(ManagedBuildMacrosTests.suite()); + suite.addTest(ManagedBuildTCSupportedTest.suite()); //$JUnit-END$ return suite; } diff --git a/build/org.eclipse.cdt.managedbuilder.core.tests/tests/org/eclipse/cdt/managedbuilder/core/tests/ManagedBuildEnvironmentTests.java b/build/org.eclipse.cdt.managedbuilder.core.tests/tests/org/eclipse/cdt/managedbuilder/core/tests/ManagedBuildEnvironmentTests.java new file mode 100644 index 00000000000..e530a939178 --- /dev/null +++ b/build/org.eclipse.cdt.managedbuilder.core.tests/tests/org/eclipse/cdt/managedbuilder/core/tests/ManagedBuildEnvironmentTests.java @@ -0,0 +1,599 @@ +/********************************************************************** + * 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.managedbuilder.core.tests; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.eclipse.cdt.managedbuilder.core.IConfiguration; +import org.eclipse.cdt.managedbuilder.core.IManagedProject; +import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager; +import org.eclipse.cdt.managedbuilder.envvar.IBuildEnvironmentVariable; +import org.eclipse.cdt.managedbuilder.envvar.IEnvironmentBuildPathsChangeListener; +import org.eclipse.cdt.managedbuilder.envvar.IEnvironmentVariableProvider; +import org.eclipse.cdt.managedbuilder.envvar.IEnvironmentVariableSupplier; +import org.eclipse.cdt.managedbuilder.internal.envvar.UserDefinedEnvironmentSupplier; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IWorkspace; +import org.eclipse.core.resources.IncrementalProjectBuilder; + +/** + * + * */ +public class ManagedBuildEnvironmentTests extends TestCase { + final private String REQUIRED_TYPE = "cdt.managedbuild.target.testgnu.exe"; //$NON-NLS-1$ + // test variable names + final private String NAME_CWD = "CWD"; //$NON-NLS-1$ + final private String NAME_PWD = "PWD"; //$NON-NLS-1$ + final private String NAME_CMN = "COMMON"; //$NON-NLS-1$ + final private String NAME_WSP = "WSP"; //$NON-NLS-1$ + final private String NAME_PRJI = "PRJI"; //$NON-NLS-1$ + final private String NAME_PRJL = "PRJL"; //$NON-NLS-1$ + final private String NAME_CFGI = "CFGI"; //$NON-NLS-1$ + final private String NAME_CFGL = "CFGL"; //$NON-NLS-1$ + final private String NAME_CFGX = "CFGX"; //$NON-NLS-1$ + final private String NAME_CFG = "CFG"; //$NON-NLS-1$ + final private String NAME_REM1 = "REMTST1";//$NON-NLS-1$ + final private String NAME_REM2 = "REMTST2";//$NON-NLS-1$ + // test variable values + final private String VAL_CWDPWD = "CWD_&_PWD_should not be changed"; //$NON-NLS-1$ + final private String VAL_DUMMY1 = "/a/b/c"; //$NON-NLS-1$ + final private String VAL_DUMMY2 = "/d/e/f"; //$NON-NLS-1$ + final private String VAL_PRO_INC = "/project/inc"; //$NON-NLS-1$ + final private String VAL_PRO_LIB = "/project/lib"; //$NON-NLS-1$ + + final private String LISTENER_DATA = "O1T1O1O2T2T1O1T1O2T2"; //$NON-NLS-1$ + + // delimiters + final private String DEL_WIN = ";"; //$NON-NLS-1$ + final private String DEL_UNIX = ":"; //$NON-NLS-1$ + + IEnvironmentVariableProvider envProvider = null; + IWorkspace worksp = null; + IProject proj = null; + IManagedProject mproj = null; + String listenerResult = ""; //$NON-NLS-1$ + + IEnvironmentBuildPathsChangeListener listener = new IEnvironmentBuildPathsChangeListener(){ + public void buildPathsChanged(IConfiguration configuration, int buildPathType){ + listenerResult = listenerResult + configuration.getName().charAt(0) + buildPathType; + } + }; + + public ManagedBuildEnvironmentTests() { super(); } + public ManagedBuildEnvironmentTests(String name) { super(name); } + + public static Test suite() { + TestSuite suite = new TestSuite(ManagedBuildEnvironmentTests.class.getName()); + suite.addTest(new ManagedBuildEnvironmentTests("testEnvNotDef")); //$NON-NLS-1$ + suite.addTest(new ManagedBuildEnvironmentTests("testEnvUpper")); //$NON-NLS-1$ + suite.addTest(new ManagedBuildEnvironmentTests("testEnvAppend")); //$NON-NLS-1$ + suite.addTest(new ManagedBuildEnvironmentTests("testEnvCWDPWD")); //$NON-NLS-1$ + suite.addTest(new ManagedBuildEnvironmentTests("testEnvSuppliers")); //$NON-NLS-1$ + suite.addTest(new ManagedBuildEnvironmentTests("testEnvGetPath")); //$NON-NLS-1$ + suite.addTest(new ManagedBuildEnvironmentTests("testEnvSubscribe")); //$NON-NLS-1$ + suite.addTest(new ManagedBuildEnvironmentTests("testEnvGetParams")); //$NON-NLS-1$ + suite.addTest(new ManagedBuildEnvironmentTests("testEnvRemove")); //$NON-NLS-1$ + suite.addTest(new ManagedBuildEnvironmentTests("testEnvProvider")); //$NON-NLS-1$ + return suite; + } + + // Checking behaviour when vars are not defined (except system) + public void testEnvNotDef(){ + doInit(); + assertNotNull("System vars", envProvider.getVariables(null, true, false)); //$NON-NLS-1$ + assertNotNull("Worksp. vars", envProvider.getVariables(worksp, true, false)); //$NON-NLS-1$ + assertNotNull("Project vars", envProvider.getVariables(mproj, true, false)); //$NON-NLS-1$ + IConfiguration[] cfgs = mproj.getConfigurations(); + for (int k=0; k + ... + + + + + ... + + + + + */ + public void testEnvGetPath(){ + doInit(); + IConfiguration[] configs = mproj.getConfigurations(); + + for (int i=0; i<2; i++) { // only 2 first configs are affected + String[] val_inc = {"/config/include/"+i, "/config"+i+"/include", VAL_PRO_INC}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + String[] val_lib = {"/config/lib/"+i, VAL_PRO_LIB}; //$NON-NLS-1$ + String[] s1, s2, s3; + s1 = envProvider.getBuildPaths(configs[i], 1); // include + s2 = envProvider.getBuildPaths(configs[i], 2); // library + s3 = envProvider.getBuildPaths(configs[i], 0); // unknown + + assertNotNull("Include path is null", s1); //$NON-NLS-1$ + assertNotNull("Library path is null", s2); //$NON-NLS-1$ + assertNotNull("Bad path type returns null", s3); //$NON-NLS-1$ + assertEquals("Include path should contain 3 entries !", s1.length, 3); //$NON-NLS-1$ + assertEquals("Library path should contain 2 entries !", s2.length, 2); //$NON-NLS-1$ + assertEquals("Request with bad path type should return 0 entries !", s3.length, 0); //$NON-NLS-1$ + + compareStringLists(configs[i].getName()+"-include", s1, val_inc); //$NON-NLS-1$ + compareStringLists(configs[i].getName()+"-library", s2, val_lib); //$NON-NLS-1$ + } + } + + public void testEnvSubscribe(){ + doInit(); + IConfiguration[] configs = mproj.getConfigurations(); + + IConfiguration cfg = configs[0]; + UserDefinedEnvironmentSupplier usup = getSupplier(cfg, cfg.getName()); + assertNotNull(usup); + try { + + usup.deleteVariable(NAME_CFGI,configs[0]); + usup.deleteVariable(NAME_CFGI,configs[1]); + usup.deleteVariable(NAME_CFG+"0",configs[0]); + usup.deleteVariable(NAME_CFG+"1",configs[0]); + usup.deleteVariable(NAME_CFG+"0",configs[1]); + usup.deleteVariable(NAME_CFG+"1",configs[1]); + usup.deleteVariable(NAME_CFGL,configs[0]); + usup.deleteVariable(NAME_CFGL,configs[1]); + usup.deleteVariable(NAME_PRJI,mproj); + usup.deleteVariable(NAME_PRJL,mproj); + usup.deleteVariable(NAME_CFGX,mproj); + listenerResult = ""; //$NON-NLS-1$ + + envProvider.subscribe(listener); + // should affect config Deb + usup.createVariable(NAME_CFGI,VAL_DUMMY2,IBuildEnvironmentVariable.ENVVAR_REPLACE, DEL_UNIX, configs[0]); + assertEquals("Step 1", listenerResult, LISTENER_DATA.substring(0,2)); //$NON-NLS-1$ + // should affect config Rel + usup.createVariable(NAME_CFGI,VAL_DUMMY2,IBuildEnvironmentVariable.ENVVAR_REPLACE, DEL_UNIX, configs[1]); + assertEquals("Step 2", listenerResult, LISTENER_DATA.substring(0,4)); //$NON-NLS-1$ + + // should affect config Deb + usup.createVariable(NAME_CFG+"0",VAL_DUMMY2,IBuildEnvironmentVariable.ENVVAR_REPLACE, DEL_UNIX, configs[0]); //$NON-NLS-1$ + assertEquals("Step 3", listenerResult, LISTENER_DATA.substring(0,6)); //$NON-NLS-1$ + // should not affect anything - variable not in path of cfg 0 + usup.createVariable(NAME_CFG+"1",VAL_DUMMY2,IBuildEnvironmentVariable.ENVVAR_REPLACE, DEL_UNIX, configs[0]); //$NON-NLS-1$ + assertEquals("Step 4", listenerResult, LISTENER_DATA.substring(0,6)); //$NON-NLS-1$ + + // should affect config Deb + usup.createVariable(NAME_CFGL,VAL_DUMMY2,IBuildEnvironmentVariable.ENVVAR_REPLACE, DEL_UNIX, configs[0]); + assertEquals("Step 5", listenerResult, LISTENER_DATA.substring(0,8)); //$NON-NLS-1$ + // should affect config Rel + usup.createVariable(NAME_CFGL,VAL_DUMMY2,IBuildEnvironmentVariable.ENVVAR_REPLACE, DEL_UNIX, configs[1]); + assertEquals("Step 6", listenerResult, LISTENER_DATA.substring(0,10)); //$NON-NLS-1$ + + // should not affect anything - variable not in path of cfg 1 + usup.createVariable(NAME_CFG+"0",VAL_DUMMY2,IBuildEnvironmentVariable.ENVVAR_REPLACE, DEL_UNIX, configs[1]); //$NON-NLS-1$ + assertEquals("Step 7", listenerResult, LISTENER_DATA.substring(0,10)); //$NON-NLS-1$ + // should affect config Rel + usup.createVariable(NAME_CFG+"1",VAL_DUMMY2,IBuildEnvironmentVariable.ENVVAR_REPLACE, DEL_UNIX, configs[1]); //$NON-NLS-1$ + assertEquals("Step 8", listenerResult, LISTENER_DATA.substring(0,12)); //$NON-NLS-1$ + + // should affect both configurations + usup.createVariable(NAME_PRJI,VAL_DUMMY2,IBuildEnvironmentVariable.ENVVAR_REPLACE, DEL_UNIX, mproj); + assertEquals("Step 9", listenerResult, LISTENER_DATA.substring(0,16)); //$NON-NLS-1$ + // should affect both configurations + usup.createVariable(NAME_PRJL,VAL_DUMMY2,IBuildEnvironmentVariable.ENVVAR_REPLACE, DEL_UNIX, mproj); + assertEquals("Step 10", listenerResult, LISTENER_DATA); //$NON-NLS-1$ + + + // should not affect anything - no changes + usup.createVariable(NAME_PRJL,VAL_DUMMY2,IBuildEnvironmentVariable.ENVVAR_REPLACE, DEL_UNIX, mproj); + assertEquals("Step 11", listenerResult, LISTENER_DATA); //$NON-NLS-1$ + // should not affect anything - variable not in path + usup.createVariable(NAME_CFGX,VAL_DUMMY2, IBuildEnvironmentVariable.ENVVAR_REPLACE, DEL_UNIX, mproj); + assertEquals("Step 12", listenerResult, LISTENER_DATA); //$NON-NLS-1$ + + envProvider.unsubscribe(listener); + + // should NOT affect anything - subscription cancelled + usup.createVariable(NAME_PRJI,VAL_DUMMY1,IBuildEnvironmentVariable.ENVVAR_REPLACE, DEL_UNIX, mproj); + usup.createVariable(NAME_CFGI,VAL_DUMMY1,IBuildEnvironmentVariable.ENVVAR_REPLACE, DEL_UNIX, configs[0]); + usup.createVariable(NAME_CFGI,VAL_DUMMY1,IBuildEnvironmentVariable.ENVVAR_REPLACE, DEL_UNIX, configs[1]); + assertEquals("Step 13", listenerResult, LISTENER_DATA); //$NON-NLS-1$ + + } catch (Exception e) { + fail("Failed to create configuration vars !"); //$NON-NLS-1$ + } + } + + /** + * + * + */ + public void testEnvGetParams(){ + doInit(); + IEnvironmentVariableProvider envProvider = ManagedBuildManager.getEnvironmentVariableProvider(); + IBuildEnvironmentVariable x = null; + IBuildEnvironmentVariable y = null; + if (System.getProperty("os.name").toLowerCase().startsWith("windows")) { //$NON-NLS-1$ //$NON-NLS-2$ + assertEquals(envProvider.getDefaultDelimiter(), DEL_WIN); + assertFalse(envProvider.isVariableCaseSensitive()); + // these var instances are different although contents is equal. + x = envProvider.getVariable("PATH", mproj, true, false); + assertNotNull(x); + y = envProvider.getVariable("path", mproj, true, false); + assertNotNull(y); + assertEquals(x.getName(), y.getName()); + assertEquals(x.getValue(), y.getValue()); + } else { + assertEquals(envProvider.getDefaultDelimiter(), DEL_UNIX); + assertTrue(envProvider.isVariableCaseSensitive()); + // "path" is different var (may absent); + x = envProvider.getVariable("PATH", mproj, true, false); + assertNotNull(x); + y = envProvider.getVariable("path", mproj, true, false); + if (y != null) { + assertFalse(x.getName().equals(y.getName())); + } + } + } + /** + * + * + */ + public void testEnvRemove(){ + doInit(); + IEnvironmentVariableProvider env = ManagedBuildManager.getEnvironmentVariableProvider(); + UserDefinedEnvironmentSupplier usup = null; + + // create vars for removal tests + assertNotNull(usup = getSupplier(worksp, "Workspace")); //$NON-NLS-1$ + try { + assertNotNull(usup.createVariable(NAME_REM1, VAL_DUMMY1, IBuildEnvironmentVariable.ENVVAR_REPLACE, DEL_UNIX, worksp)); + assertNotNull(usup.createVariable(NAME_REM2, VAL_DUMMY1, IBuildEnvironmentVariable.ENVVAR_REPLACE, DEL_UNIX, worksp)); + } catch (Exception e) { fail(e.getLocalizedMessage()); } + + assertNotNull(usup = getSupplier(mproj, "Project")); //$NON-NLS-1$ + try { + assertNotNull(usup.createVariable(NAME_REM1, VAL_DUMMY2, IBuildEnvironmentVariable.ENVVAR_REMOVE, DEL_UNIX, mproj)); + assertNotNull(usup.createVariable(NAME_REM2, VAL_DUMMY2, IBuildEnvironmentVariable.ENVVAR_APPEND, DEL_UNIX, mproj)); + } catch (Exception e) { fail(e.getLocalizedMessage()); } + + IConfiguration cfg = mproj.getConfigurations()[0]; + assertNotNull(usup = getSupplier(cfg, "Configuration 0")); //$NON-NLS-1$ + try { + assertNotNull(usup.createVariable(NAME_REM1, VAL_CWDPWD, IBuildEnvironmentVariable.ENVVAR_REMOVE, DEL_UNIX, cfg)); + assertNotNull(usup.createVariable(NAME_REM2, VAL_CWDPWD, IBuildEnvironmentVariable.ENVVAR_REMOVE, DEL_UNIX, cfg)); + } catch (Exception e) { fail(e.getLocalizedMessage()); } + + // Check vars presence/absence on different levels + IBuildEnvironmentVariable a = env.getVariable(NAME_REM1, worksp, true, false); + IBuildEnvironmentVariable b = env.getVariable(NAME_REM2, worksp, true, false); + assertNotNull(a); + assertNotNull(b); + a = env.getVariable(NAME_REM1, mproj, true, false); + b = env.getVariable(NAME_REM2, mproj, true, false); + assertNull(a); + assertNotNull(b); + assertEquals(b.getValue(), VAL_DUMMY1 + DEL_UNIX + VAL_DUMMY2); + a = env.getVariable(NAME_REM1, cfg, true, false); + b = env.getVariable(NAME_REM2, cfg, true, false); + assertNull(a); + assertNull(b); + } + /** + * testEnvProvider() - + */ + public void testEnvProvider() { + doInit(); + IBuildEnvironmentVariable a = envProvider.getVariable(TestMacro.PRJ_VAR, mproj, true, false); + assertNotNull(a); + assertEquals(TestMacro.PRJ_VAR + mproj.getName(), a.getValue()); + + IConfiguration[] cfgs = mproj.getConfigurations(); + a = envProvider.getVariable(TestMacro.CFG_VAR, cfgs[0], true, false); + assertNotNull(a); + assertEquals(TestMacro.CFG_VAR + cfgs[0].getName(), a.getValue()); + + // no provider for another configurations + a = envProvider.getVariable(TestMacro.CFG_VAR, cfgs[1], true, false); + assertNull(a); + + // combination user-defined and provided variables + UserDefinedEnvironmentSupplier usup = getSupplier(cfgs[0], cfgs[0].getName()); + usup.createVariable(TestMacro.PRJ_VAR, VAL_DUMMY1, IBuildEnvironmentVariable.ENVVAR_PREPEND, DEL_UNIX, cfgs[0]); + a = envProvider.getVariable(TestMacro.PRJ_VAR, cfgs[0], true, false); + assertNotNull(a); + assertEquals(VAL_DUMMY1+DEL_UNIX+TestMacro.PRJ_VAR+mproj.getName(), a.getValue()); + } + + /** + * This test is not used iun suite. It just prints variabes + */ + public void testEnvPrint(){ + doInit(); + printVar("s-Var", envProvider.getVariables(null, false, false)); //$NON-NLS-1$ + printVar("w-Var", envProvider.getVariables(worksp, false, false)); //$NON-NLS-1$ + printVar("p-Var", envProvider.getVariables(mproj, false, false)); //$NON-NLS-1$ + IConfiguration[] cfgs = mproj.getConfigurations(); + for (int k=0; k - "+ e.getLocalizedMessage()); //$NON-NLS-1$ //$NON-NLS-2$ + } + } + try { + proj.build(IncrementalProjectBuilder.FULL_BUILD, null); + } catch (Exception e) {} + } + private void doInit() { + envProvider = ManagedBuildManager.getEnvironmentVariableProvider(); + assertNotNull(envProvider); + ManagedBuildMacrosTests.createManagedProject("Merde"); //$NON-NLS-1$ + proj = ManagedBuildMacrosTests.proj; + assertNotNull(proj); + mproj = ManagedBuildMacrosTests.mproj; + assertNotNull(mproj); + worksp = proj.getWorkspace(); + assertNotNull(worksp); + } + + /* + * Print contents of env.var array, with given header. + */ + private void printVar(String head, IBuildEnvironmentVariable[] vars) { + if (vars != null) { + if (vars.length > 0) { + for (int i=0; i < vars.length; i++) { + System.out.println(head + "[" + i + "] " + //$NON-NLS-1$ //$NON-NLS-2$ + vars[i].getName() + " = " + //$NON-NLS-1$ + vars[i].getValue() + " / " + //$NON-NLS-1$ + vars[i].getOperation() + vars[i].getDelimiter()); + } + } else { System.out.println(head + ": array is empty"); } //$NON-NLS-1$ + } else { System.out.println(head + ": array is null"); } //$NON-NLS-1$ + } + + /* + * check that ALL variables from list "a" have correspondence + * in list "b" + * empty list or null are treated as corresponding to anything + */ + private boolean varListContainNames(IBuildEnvironmentVariable[] a, IBuildEnvironmentVariable[] b) { + if (a == null) return true; + else if (a.length == 0) return true; + else if (b == null) return false; + + for (int i=0; i", "LIST=x|y|z"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + try { + res1[5] = mp.resolveValue("${PATH}",UNKNOWN,LISTSEP,IBuildMacroProvider.CONTEXT_OPTION, ocd); //$NON-NLS-1$ + } catch (BuildMacroException e) { fail(e.getLocalizedMessage()); } + + opt = cfgs[0].setOption(t, opt, set1); + assertNotNull(opt); + + ArrayList ar = new ArrayList(1); + for (int i=0; i ${lst}", UNKNOWN, LISTSEP, //$NON-NLS-1$ + IBuildMacroProvider.CONTEXT_CONFIGURATION, cfgs[0]); + + assertEquals(a, "EIN - ZWEI - DREI -> SCHEISE|MERDE|SHIT"); //$NON-NLS-1$ + assertEquals(b, "EIN - ZWEI - -> SCHEISE|MERDE|SHIT"); //$NON-NLS-1$ + assertEquals(c, "EIN - - -> SCHEISE|MERDE|SHIT"); //$NON-NLS-1$ + assertEquals(d, "nested L1-L2-L3-L4-L3-L2-L1"); //$NON-NLS-1$ + assertEquals(e, " - - -> "); //$NON-NLS-1$ + } catch (BuildMacroException e) { + fail("Exception while resolving: " + e.getLocalizedMessage()); //$NON-NLS-1$ + } + } + + /** + * testMacroResolveExceptions() + */ + public void testMacroResolveExceptions () { + doInit(); + + boolean exceptionRaised = false; + try { // ZERO is undefined macro + String a = mp.resolveValue("${ONE} - ${ZERO}", null, null, //$NON-NLS-1$ + IBuildMacroProvider.CONTEXT_CONFIGURATION, cfgs[0]); + } catch (BuildMacroException e) { + exceptionRaised = true; + } + assertTrue("Exception not raised for undefined macro", exceptionRaised); //$NON-NLS-1$ + + exceptionRaised = false; + try { // delimiter is undefined for list + String a = mp.resolveValue("${LST}", null, null, //$NON-NLS-1$ + IBuildMacroProvider.CONTEXT_CONFIGURATION, cfgs[0]); + } catch (BuildMacroException e) { + exceptionRaised = true; + } + assertTrue("Exception not raised for undefined delimiter", exceptionRaised); //$NON-NLS-1$ + } + + /** + * testMacroResolveLoop() + */ + public void testMacroResolveLoop() { + doInit(); + int ctx = IBuildMacroProvider.CONTEXT_WORKSPACE; + Object obj = worksp; + ms = mp.getSuppliers(ctx, obj); + assertNotNull(ms); + + // check state before macros added (should be OK) + try { + mp.checkIntegrity(ctx, obj); + } catch (BuildMacroException e) { + fail("Macros integrity check is failed"); //$NON-NLS-1$ + } + + // create macro which references to undefined macro + assertTrue(addMacro("B1", IBuildMacro.VALUE_TEXT, "B1-${B2}", ctx, obj)); //$NON-NLS-1$ //$NON-NLS-2$ + rmMacro("B2", ctx, obj); // usually it does not exist, but to be sure... //$NON-NLS-1$ + + // check state after macro added (should be exception) + try { + mp.checkIntegrity(ctx, obj); + fail("Macros misintegrity (ref to undefined) is not detected"); //$NON-NLS-1$ + } catch (BuildMacroException e) {} + + // create "dead loop" of nested macros + assertTrue(addMacro("B2", IBuildMacro.VALUE_TEXT, "B2-${B3}", ctx, obj)); //$NON-NLS-1$ //$NON-NLS-2$ + assertTrue(addMacro("B3", IBuildMacro.VALUE_TEXT, "B3-${B1}", ctx, obj)); //$NON-NLS-1$ //$NON-NLS-2$ + + // check state after macros added (should be exception) + try { + mp.checkIntegrity(ctx, obj); + fail("Macros misintegrity (dead loop) is not detected"); //$NON-NLS-1$ + } catch (BuildMacroException e) {} + + // remove "dead loop" of nested macros + assertTrue(rmMacro("B1", ctx, obj)); //$NON-NLS-1$ + assertTrue(rmMacro("B2", ctx, obj)); //$NON-NLS-1$ + assertTrue(rmMacro("B3", ctx, obj)); //$NON-NLS-1$ + + // check state after macros removed (should be OK) + try { + mp.checkIntegrity(ctx, obj); + } catch (BuildMacroException e) { + fail("Macros integrity check is failed " + e.getLocalizedMessage()); //$NON-NLS-1$ + } + } + /** + * testMacroResolveMake() + */ + public void testMacroResolveMake(){ + final String p1 = "USERNAME: "; //$NON-NLS-1$ + final String p2 = "${USERNAME} "; //$NON-NLS-1$ + final String p3 = "PATH: "; //$NON-NLS-1$ + final String p4 = "${PATH} "; //$NON-NLS-1$ + final String p5 = "HostOsName: ${HostOsName} WorkspaceDirPath: ${WorkspaceDirPath}"; //$NON-NLS-1$ + final String ein1 = p1 + p2 + p3; + final String ein2 = p4 + p5; + final String ein = ein1 + ein2; + final String aus1 = "@USERNAME "; //$NON-NLS-1$ + final String aus2 = "@PATH "; //$NON-NLS-1$ + doInit(); + // Config #0 contains "variableFormat" macro = "@=". Result: 2 first macros NOT resolved + try { + functionCalled = 0; + String a = mp.resolveValueToMakefileFormat(ein, UNKNOWN, LISTSEP, IBuildMacroProvider.CONTEXT_CONFIGURATION, cfgs[0]); + String b = p1 + aus1 + p3 + mp.resolveValue(ein2, UNKNOWN, LISTSEP, IBuildMacroProvider.CONTEXT_CONFIGURATION, cfgs[0]); + assertEquals(a, b); // Env var names should not be resolved but converted to Makefile format + a = mp.resolveValueToMakefileFormat(ein, UNKNOWN, LISTSEP, IBuildMacroProvider.CONTEXT_PROJECT, mproj); + assertEquals(a, b); // Project context should return the same as default configuration + } catch (BuildMacroException e) { + fail(e.getLocalizedMessage()); + } + // Config #1 does not contain "variableFormat" macro. Result: all macros resolved. + try { + String a = mp.resolveValue(ein, UNKNOWN, LISTSEP, IBuildMacroProvider.CONTEXT_CONFIGURATION, cfgs[1]); + String b = mp.resolveValueToMakefileFormat(ein, UNKNOWN, LISTSEP, IBuildMacroProvider.CONTEXT_CONFIGURATION, cfgs[1]); + assertEquals(a, b); + } catch (BuildMacroException e) { + fail(e.getLocalizedMessage()); + } + // check that "isReservedName" was not called before + assertEquals(functionCalled & RESERVED_NAME, 0); + + // Config #2 contains "...Supplier" macro. Result: PATH unresolved, USERNAME resolved. + try { + String a = mp.resolveValue(p1 + p2, UNKNOWN, LISTSEP, IBuildMacroProvider.CONTEXT_CONFIGURATION, cfgs[2]); + String b = mp.resolveValue(p5, UNKNOWN, LISTSEP, IBuildMacroProvider.CONTEXT_CONFIGURATION, cfgs[2]); + a = a + p3 + aus2 + b; // USERNAME: xxx PATH: @PATH HostOsName: xxx ... + b = mp.resolveValueToMakefileFormat(ein, UNKNOWN, LISTSEP, IBuildMacroProvider.CONTEXT_CONFIGURATION, cfgs[2]); + assertEquals(a, b); + // check that "isReservedName" has been called + assertEquals(functionCalled & RESERVED_NAME, RESERVED_NAME); + } catch (BuildMacroException e) { + fail(e.getLocalizedMessage()); + } + } + + /** + * testMacroResolveCase() + */ + public void testMacroResolveCase(){ + doInit(); + addVars(); + final String winOut1 = "@CASETEST uppercase uppercase uppercase"; //$NON-NLS-1$ + final String winOut2 = "@CASETEST @CASETEST @CASETEST @CASETEST"; //$NON-NLS-1$ + + final String unixOut1 = "@CASETEST capitalize lowercase upper2low"; //$NON-NLS-1$ + final String unixOut2 = "@CASETEST @CaseTest @casetest @CaSeTeSt"; //$NON-NLS-1$ + + final String ein = "${CASETEST} ${CaseTest} ${casetest} ${CaSeTeSt}"; //$NON-NLS-1$ + final int ctx = IBuildMacroProvider.CONTEXT_CONFIGURATION; + String a=null, b=null; + try { + // Config #0 contains isVariableCaseSensitive = false + a = mp.resolveValueToMakefileFormat(ein, UNKNOWN, LISTSEP, ctx, cfgs[0]); + // Config #3 contains isVariableCaseSensitive = true + b = mp.resolveValueToMakefileFormat(ein, UNKNOWN, LISTSEP, ctx, cfgs[3]); + } catch (BuildMacroException e) { + fail(e.getLocalizedMessage()); + } + if (windows) { + assertEquals(a, winOut1); + assertEquals(b, winOut2); + } else { // linux + assertEquals(a, unixOut1); + assertEquals(b, unixOut2); + } + } + + /** + * testMacroSave() + */ + + public void testMacroSave(){ + final String TO_SAVE_P = "TO_SAVE_P"; //$NON-NLS-1$ + final String TO_SAVE_W = "TO_SAVE_W"; //$NON-NLS-1$ + doInit(); + ms = mp.getSuppliers(IBuildMacroProvider.CONTEXT_PROJECT, mproj); + assertNotNull(ms); + assertTrue(addMacro(TO_SAVE_P, IBuildMacro.VALUE_TEXT, TO_SAVE_P, + IBuildMacroProvider.CONTEXT_PROJECT, mproj)); + ms = mp.getSuppliers(IBuildMacroProvider.CONTEXT_WORKSPACE, worksp); + assertNotNull(ms); + assertTrue(addMacro(TO_SAVE_W, IBuildMacro.VALUE_TEXT, TO_SAVE_W, + IBuildMacroProvider.CONTEXT_WORKSPACE, worksp)); + try { + proj.close(null); + proj.open(null); + } catch (CoreException e) { + fail("Failed on project close/open: " + e.getLocalizedMessage()); //$NON-NLS-1$ + } + ms = mp.getSuppliers(IBuildMacroProvider.CONTEXT_PROJECT, mproj); + assertNotNull(ms); + String[] a = printMacros(mp.getMacros(IBuildMacroProvider.CONTEXT_PROJECT, mproj, flag), TO_SAVE_P); + String[] b1 = {TO_SAVE_P}; + assertTrue(arrayContains(b1, a)); + + ms = mp.getSuppliers(IBuildMacroProvider.CONTEXT_WORKSPACE, worksp); + assertNotNull(ms); + a = printMacros(mp.getMacros(IBuildMacroProvider.CONTEXT_WORKSPACE, worksp, flag), TO_SAVE_W); + String[] b2 = {TO_SAVE_W}; + assertTrue(arrayContains(b2, a)); + } + +/* + * Below are service methods + */ + //TODO: comments for all methods + + // returns a list of macro's NAMES (not values). + private String[] printMacros(IBuildMacro[] vars, String head) { + ArrayList ar = new ArrayList(0); + if (vars != null) { + if (vars.length > 0) { + for (int i=0; i < vars.length; i++) { + try { + ar.add(vars[i].getName()); + if (!print) continue; + if ((vars[i].getMacroValueType() % 2) == 1) // not-list + //if (vars[i] instanceof EclipseVarMacro) { + if (vars[i].getName().endsWith("prompt")) { //$NON-NLS-1$ + System.out.println(head + "[" + i + "] " + //$NON-NLS-1$ //$NON-NLS-2$ + vars[i].getName() + " = "); //$NON-NLS-1$ + } else { + System.out.println(head + "[" + i + "] " + //$NON-NLS-1$ //$NON-NLS-2$ + vars[i].getName() + " = " + vars[i].getStringValue()); //$NON-NLS-1$ + } + else { + System.out.println(head + "[" + i + "] " + //$NON-NLS-1$ //$NON-NLS-2$ + vars[i].getName() + ":"); //$NON-NLS-1$ + String[] m = vars[i].getStringListValue(); + printStrings(m, " "); //$NON-NLS-1$ + } + } catch (Exception e) {} + } + } else { if (print) System.out.println(head + ": array is empty"); } //$NON-NLS-1$ + } else { if (print) System.out.println(head + ": array is null"); } //$NON-NLS-1$ + return (String[])ar.toArray(new String[0]); + } + + private void printStrings(String[] vars, String head) { + if (!print) return; + if (vars != null) { + if (vars.length > 0) { + for (int j=0; j 0 ? mproj.createConfiguration(cfgs[0], projType.getId() + ".0") : null; //$NON-NLS-1$ + for (int i = 1; i < cfgs.length; ++i) { // sic ! from 1 + mproj.createConfiguration(cfgs[i], projType.getId() + "." + i); //$NON-NLS-1$ + } + ManagedBuildManager.setDefaultConfiguration(proj, defcfg); + } + // open project w/o progress monitor; no action performed if it's opened + try { + proj.open(null); + } catch (CoreException e) {} + } + /** + * doInit() - call it at the beginning of every test + * + */ + private void doInit() { + createManagedProject("Test"); //$NON-NLS-1$ + assertNotNull(proj); + assertNotNull(mproj); + worksp = proj.getWorkspace(); + assertNotNull(worksp); + mp = ManagedBuildManager.getBuildMacroProvider(); + assertNotNull(mp); + cfgs = mproj.getConfigurations(); + assertNotNull(cfgs); + windows = System.getProperty("os.name").toLowerCase().startsWith("windows"); //$NON-NLS-1$ //$NON-NLS-2$ + } + + /** + * arrayContains + * check that ALL variables from list a have correspondence in list b + * @param a + * @param b + * @return + */ + private boolean arrayContains(String[] a, String[] b) { + assertNotNull(a); + assertNotNull(b); + for (int i=0; i 0) { + newms = new BuildMacro[ms.length + 1]; + System.arraycopy(ms, 0, newms, 0, ms.length); + } else { + newms = new BuildMacro[1]; + } + newms[ms.length] = new BuildMacro("NEW_FOR_CFG", 1, "NewMacrosForConfigContext"); //$NON-NLS-1$ //$NON-NLS-2$ + return newms; + } + return null; + } + + /** + * + */ + public IBuildMacro[] getMacros(IManagedProject mproj, + IBuildMacroProvider provider) { + ManagedBuildMacrosTests.functionCalled |= ManagedBuildMacrosTests.GET_MANY_PROJECT; + IBuildMacro[] ms = null; + if (!(provider instanceof TestMacro)) { + ms = provider.getMacros(IBuildMacroProvider.CONTEXT_PROJECT, mproj, false); + IBuildMacro[] newms = null; + if (ms != null && ms.length > 0) { + newms = new BuildMacro[ms.length + 1]; + System.arraycopy(ms, 0, newms, 0, ms.length); + } else { + newms = new BuildMacro[1]; + } + newms[newms.length - 1] = new BuildMacro("NEW_FOR_PRJ", 1, "NewMacrosForProjectContext"); //$NON-NLS-1$ //$NON-NLS-2$ + return newms; + } + return null; + } + +// IReservedMacroNameSupplier + + /** + * + */ + public boolean isReservedName(String macroName, IConfiguration configuration) { + ManagedBuildMacrosTests.functionCalled |= ManagedBuildMacrosTests.RESERVED_NAME; + if (macroName.equalsIgnoreCase("USERNAME")) return true; //$NON-NLS-1$ + return false; + } + +// IConfigurationEnvironmentVariableSupplier + + /** + * + */ + public IBuildEnvironmentVariable getVariable(String variableName, + IConfiguration configuration, + IEnvironmentVariableProvider provider) { + if (CFG_VAR.equals(variableName)) { + return new BuildEnvVar(CFG_VAR, CFG_VAR + configuration.getName()); + } else + return null; + } + + /** + * + */ + public IBuildEnvironmentVariable[] getVariables (IConfiguration configuration, + IEnvironmentVariableProvider provider) { + IBuildEnvironmentVariable v = getVariable(CFG_VAR, configuration, provider); + if (v != null) { + IBuildEnvironmentVariable[] vs = new IBuildEnvironmentVariable[1]; + vs[0] = v; + return(vs); + } else + return null; + } + +// IProjectEnvironmentVariableSupplier + + /** + * + */ + public IBuildEnvironmentVariable getVariable(String variableName, + IManagedProject project, + IEnvironmentVariableProvider provider) { + if (PRJ_VAR.equals(variableName)) { + return new BuildEnvVar(PRJ_VAR, PRJ_VAR + project.getName()); + } else + return null; + } + + /** + * + */ + public IBuildEnvironmentVariable[] getVariables (IManagedProject project, + IEnvironmentVariableProvider provider) { + IBuildEnvironmentVariable v = getVariable(PRJ_VAR, project, provider); + if (v != null) { + IBuildEnvironmentVariable[] vs = new IBuildEnvironmentVariable[1]; + vs[0] = v; + return(vs); + } else + return null; + } + + +// IManagedIsToolChainSupported + + /** + * + */ + public boolean isSupported(IToolChain toolChain, + PluginVersionIdentifier version, + String instance) { + if ("One".equals(toolChain.getParent().getName())) return supported[0]; //$NON-NLS-1$ + if ("Two".equals(toolChain.getParent().getName())) return supported[1]; //$NON-NLS-1$ + if ("Three".equals(toolChain.getParent().getName())) return supported[2]; //$NON-NLS-1$ + if ("Four".equals(toolChain.getParent().getName())) return supported[3]; //$NON-NLS-1$ + return false; + } + + +// IManagedBuilderMakefileGenerator + + /** + */ + public IPath getBuildWorkingDir() { +// System.out.println("---- getBuildWorkingDir: " + topBuildDir); + return topBuildDir; + } + public void generateDependencies() {} + public MultiStatus generateMakefiles(IResourceDelta delta) { return null; } + public String getMakefileName() {return "test_instead_make"; } //$NON-NLS-1$ + public void initialize(IProject project, IManagedBuildInfo info, IProgressMonitor monitor) { +// System.out.println("---- init: " + topBuildDir); + } + public boolean isGeneratedResource(IResource resource) { return false; } + public void regenerateDependencies(boolean force) {} + public MultiStatus regenerateMakefiles() { return null; } +}