1
/*******************************************************************************
2
* Copyright (c) 2005, 2006 Symbian Ltd and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Symbian - Initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.managedbuilder.core.tests;
15
import junit.framework.Test;
16
import junit.framework.TestCase;
17
import junit.framework.TestSuite;
19
import org.eclipse.cdt.core.CCorePlugin;
20
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
21
import org.eclipse.cdt.managedbuilder.core.IManagedOptionValueHandler;
22
import org.eclipse.cdt.managedbuilder.core.IManagedProject;
23
import org.eclipse.cdt.managedbuilder.core.IOption;
24
import org.eclipse.cdt.managedbuilder.core.IOptionCategory;
25
import org.eclipse.cdt.managedbuilder.core.IProjectType;
26
import org.eclipse.cdt.managedbuilder.core.ITool;
27
import org.eclipse.cdt.managedbuilder.core.IToolChain;
28
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
29
import org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin;
30
import org.eclipse.cdt.managedbuilder.core.ManagedCProjectNature;
31
import org.eclipse.cdt.managedbuilder.core.ManagedOptionValueHandler;
32
import org.eclipse.cdt.managedbuilder.testplugin.CTestPlugin;
33
import org.eclipse.cdt.managedbuilder.testplugin.ManagedBuildTestHelper;
34
import org.eclipse.core.resources.IProject;
35
import org.eclipse.core.resources.IProjectDescription;
36
import org.eclipse.core.resources.IResource;
37
import org.eclipse.core.resources.IWorkspace;
38
import org.eclipse.core.resources.IWorkspaceDescription;
39
import org.eclipse.core.resources.IWorkspaceRoot;
40
import org.eclipse.core.resources.IWorkspaceRunnable;
41
import org.eclipse.core.resources.ResourcesPlugin;
42
import org.eclipse.core.runtime.CoreException;
43
import org.eclipse.core.runtime.IProgressMonitor;
44
import org.eclipse.core.runtime.NullProgressMonitor;
45
import org.eclipse.core.runtime.Path;
46
import org.eclipse.core.runtime.Platform;
48
// TODO LK - write test for resource configurations + IOptionCategory.getOptions(config)
49
// TODO LK - write tests for EVENT_OPEN call-backs
50
// TODO LK - do not deprecate IOption.getParent() as suggested by Leo
53
* These tests exercise CDT 3.0 shared tool options extensions
55
public class ManagedBuildCoreTests_SharedToolOptions extends TestCase {
57
class IconComparator {
58
static final int None = 0;
59
static final int One = 1;
60
static final int Two = 2;
62
// The following uses a numbering scheme defined for
63
// testCategory and testOption
65
// Co-ordinates of option groups 2nd level that have icon 1
66
int One_i[] = { 3, 2, 4, 7 };
67
int One_j[] = { 1, 2, 1, 1 };
68
// Co-ordinates of option groups 2nd level that have icon 1
69
int Two_i[] = { 2, 6 };
70
int Two_j[] = { 1, 1 };
74
assertEquals(One_i.length, One_j.length);
75
assertEquals(Two_i.length, Two_j.length);
78
int Compare(int i, int j)
83
for (k=0; k < One_i.length; k++)
85
if ( (i == One_i[k]) && (j == One_j[k]) )
91
for (k=0; k < Two_i.length; k++)
93
if ( (i == Two_i[k]) && (j == Two_j[k]) )
103
class ValueHandlerComparator {
104
static final int MBS = 0;
105
static final int TEST = 1;
107
// The following uses a numbering scheme defined for
108
// testCategory and testOption
110
// Co-ordinates of option groups 2nd level that have a test value handler
111
int Test_i[] = { 2, 2, 3, 5, 7 };
112
int Test_j[] = { 1, 4, 2, 1, 1 };
113
String Test_arg[] = { "Option2.1.1", "Option2.2.2", "Option3.1.2", "Option5.1", "Option7.1" };
116
ValueHandlerComparator()
118
assertEquals(Test_i.length, Test_j.length);
119
assertEquals(Test_i.length, Test_arg.length);
123
int Compare(int i, int j) {
126
// Check for Test handler
127
for (k=0; k < Test_i.length; k++) {
128
if ( (i == Test_i[k]) && (j == Test_j[k]) ) {
129
last_arg = Test_arg[k];
144
private final String projectName = "test30_sto";
145
private final String projectID = "test30_sto.dummy";
146
private final String configID = "test30_sto.dummy.config";
147
private final String configName = "Configuration for test30_sto";
150
private boolean testExtensionElements = true;
151
private boolean testIsSetup = false;
153
// Chain leading to tool, etc
154
private IProjectType testProject;
155
private IConfiguration testConfig;
156
private IToolChain testToolChain;
158
// Direct children of toolChain
159
private ITool testTools[];
160
private IOptionCategory testCategoryTop[];
161
private IOption testOptionTop[];
163
// 2nd level children (of toolChain and tool) mapped in the following pattern
165
// tool_1: cat_1.1 cat_1.2 cat_1.3 ...
167
// tool_n: cat_n.1 catn.2
168
// The same 2d-array is used to store top-level categories
172
private Object testCategory[][];
173
private Object testOption[][];
176
private IconComparator iconComparator;
177
private ValueHandlerComparator valueHandlerComparator;
179
public ManagedBuildCoreTests_SharedToolOptions(String name) {
182
iconComparator = new IconComparator();
183
valueHandlerComparator = new ValueHandlerComparator();
186
public static Test suite() {
187
TestSuite suite = new TestSuite(ManagedBuildCoreTests_SharedToolOptions.class.getName());
189
suite.addTest(new ManagedBuildCoreTests_SharedToolOptions("testIcons"));
190
suite.addTest(new ManagedBuildCoreTests_SharedToolOptions("testValueHandlers"));
191
suite.addTest(new ManagedBuildCoreTests_SharedToolOptions("testOptions"));
192
suite.addTest(new ManagedBuildCoreTests_SharedToolOptions("testConfiguration"));
197
private void assertCorrectId(String s1, String s2) {
198
if (testExtensionElements == true) {
199
// A strict comparison is required
200
assertTrue(s1.equals(s2));
202
// Compare for non-extension element Id's
203
assertTrue(s1.startsWith(s2+"."));
207
private IProject createProject(String name) throws CoreException {
208
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
209
final IProject newProjectHandle = root.getProject(name);
210
IProject project = null;
212
if (!newProjectHandle.exists()) {
213
IWorkspace workspace = ResourcesPlugin.getWorkspace();
214
IWorkspaceDescription workspaceDesc = workspace.getDescription();
215
workspaceDesc.setAutoBuilding(false);
216
workspace.setDescription(workspaceDesc);
217
IProjectDescription description = workspace.newProjectDescription(newProjectHandle.getName());
218
//description.setLocation(root.getLocation());
219
project = CCorePlugin.getDefault().createCProject(description, newProjectHandle, new NullProgressMonitor(), ManagedBuilderCorePlugin.MANAGED_MAKE_PROJECT_ID);
221
IWorkspace workspace = ResourcesPlugin.getWorkspace();
222
IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
223
public void run(IProgressMonitor monitor) throws CoreException {
224
newProjectHandle.refreshLocal(IResource.DEPTH_INFINITE, monitor);
227
NullProgressMonitor monitor = new NullProgressMonitor();
228
workspace.run(runnable, root, IWorkspace.AVOID_UPDATE, monitor);
229
project = newProjectHandle;
232
// Open the project if we have to
233
if (!project.isOpen()) {
234
project.open(new NullProgressMonitor());
241
* Sets up the test environment for the default project
243
private void setupDefaultProject() throws Exception {
245
// The assertCorrectId() call needs to be set up
246
testExtensionElements = true;
247
// Get all the key structures of our test and do some sanity checking
249
testProject = ManagedBuildManager.getProjectType(projectID);
250
assertNotNull(testProject);
251
assertTrue(testProject.isTestProjectType());
252
assertFalse(testProject.isAbstract());
254
// Get and check project configurations - only one
256
setupConfiguration();
257
assertTrue(testConfig.isExtensionElement());
261
* Sets up the test configuration from the project set
264
private void setupConfiguration() throws Exception {
266
IConfiguration[] configs = testProject.getConfigurations();
267
assertNotNull(configs);
268
assertEquals(configs.length, 1);
269
testConfig = configs[0];
270
assertEquals(testConfig.getId(), configID);
271
assertEquals(testConfig.getName(), configName);
275
* Sets up the test environment for a project created from the
278
private void setupProject() throws Exception {
280
// The assertCorrectId() call needs to be set up
281
testExtensionElements = false;
282
// Create new project
283
IProject project = null;
285
project = createProject(projectName);
286
// Now associate the builder with the project
287
ManagedBuildTestHelper.addManagedBuildNature(project);
288
IProjectDescription description = project.getDescription();
289
// Make sure it has a managed nature
290
if (description != null) {
291
assertTrue(description.hasNature(ManagedCProjectNature.MNG_NATURE_ID));
294
} catch (CoreException e) {
295
fail("Test failed on project creation: " + e.getLocalizedMessage());
297
// Find the base project type definition
298
IProjectType[] projTypes = ManagedBuildManager.getDefinedProjectTypes();
299
IProjectType projType = ManagedBuildManager.getProjectType(projectID);
300
assertNotNull(projType);
301
// Create a managed project
302
IManagedProject newProject = ManagedBuildManager.createManagedProject(project, projType);
303
assertEquals(newProject.getName(), projType.getName());
304
assertFalse(newProject.equals(projType));
305
ManagedBuildManager.setNewProjectVersion(project);
306
// Set up the environment
307
testProject = newProject.getProjectType();
308
IConfiguration config = testProject.getConfiguration(configID);
309
testConfig = newProject.createConfiguration(config, configID + ".12345678");
313
* Sets up the test environment and does some initial checking.
314
* Do not do this in the constructor, as it is part of the test.
316
private void setupTestEnvironment() throws Exception {
322
String configID = "test30_sto.dummy.config";
323
String configName = "Configuration for test30_sto";
324
String toolChainID = "test30_sto.dummy.toolchain";
325
String toolChainName = "Toolchain for test30_sto";
328
String toolIDs = "test30_sto.dummy.tool.";
330
// Top level option categories and groups
331
int numTopCategories = 3;
332
String topCategoryIDs = "test30_sto.dummy.category.";
333
int firstTopCategoryID = 5;
335
// Sizes of some arrays
336
int sizeCategoryTop = 0;
337
int sizeOptionTop = 0;
342
testToolChain = testConfig.getToolChain();
343
assertNotNull(testToolChain);
344
assertEquals(testToolChain.getName(), toolChainName);
345
assertCorrectId(testToolChain.getId(), toolChainID);
347
// Fetch and check tools list
349
testTools = testToolChain.getTools();
350
assertNotNull(testTools);
351
assertEquals(testTools.length, numTools);
352
int toolNo = firstToolID;
353
sizeTools = numTools;
354
for (i=0; i < sizeTools; i++) {
355
assertCorrectId(testTools[i].getId(), toolIDs+toolNo);
359
// Fetch and check top level option categories and options
361
testCategoryTop = testToolChain.getChildCategories();
362
testOptionTop = testToolChain.getOptions();
364
if (testCategoryTop != null) {
365
int categoryNo = firstTopCategoryID;
366
sizeCategoryTop = testCategoryTop.length;
367
for (i=0; i < sizeCategoryTop; i++, categoryNo++)
369
String ID = ((IOptionCategory)testCategoryTop[i]).getId();
370
// Categories are always extension elements, so check
371
// for an identical match
372
assertEquals(ID, topCategoryIDs+categoryNo);
375
if (testOptionTop != null) {
376
sizeOptionTop = testOptionTop.length;
379
// Fetch and check 2nd level level option categories and options
381
testCategory = new Object[testTools.length+sizeCategoryTop][];
382
testOption = new Object[testTools.length+sizeOptionTop][];
383
for (i=0; i < sizeTools; i++) {
384
testCategory[i] = testTools[i].getChildCategories();
385
testOption[i] = testTools[i].getOptions();
386
// Make the arrays safe in case we have null references
387
if ( testCategory[i] == null ) {
388
testCategory[i] = new Object[0];
390
if ( testOption[i] == null ) {
391
testOption[i] = new Object[0];
394
// Add top level categories and options to test arrays
396
for (i=0; i < sizeCategoryTop; i++) {
397
testCategory[sizeTools+i] = new Object[1];
398
testCategory[sizeTools+i][0] = testCategoryTop[i];
400
for (i=0; i < sizeOptionTop; i++) {
401
testOption[sizeTools+i] = new Object[1];
402
testOption[sizeTools+i][0] = testOptionTop[i];
407
* Check, whether icon paths in tool1 - tool3 have been created.
408
* Check, whether icon paths in category 2.1 - 3.1 have been created
410
public void testIcons() throws Exception {
412
// Set up the environment
413
if ( testIsSetup == false ) {
414
setupDefaultProject();
415
setupTestEnvironment();
418
// Get path's of icons to compare against
419
CTestPlugin me = CTestPlugin.getDefault();
420
URL icon1 = Platform.asLocalURL( me.find(new Path("icons/one.gif"), null));
421
URL icon2 = Platform.asLocalURL( me.find(new Path("icons/two.gif"), null));
423
// Check the icons on tools
424
assertToolIcon(testTools[0], icon1);
425
assertToolIcon(testTools[1], null);
426
assertToolIcon(testTools[2], icon2);
428
// Check the top level and 2nd level categories
430
for (i=0; i < testCategory.length; i++) {
433
for (j=0; j < testCategory[i].length; j++) {
434
URL url = ((IOptionCategory)testCategory[i][j]).getIconPath();
436
switch ( iconComparator.Compare(i+1, j+1) ) {
437
case IconComparator.None:
438
assertEquals(url, null);
440
case IconComparator.One:
441
assertEquals(url, icon1);
443
case IconComparator.Two:
444
assertEquals(url, icon2);
451
private void assertToolIcon(ITool tool, URL url)
453
assertTrue(tool instanceof IOptionCategory);
454
IOptionCategory toolCategory = (IOptionCategory)tool;
455
assertEquals(toolCategory.getIconPath(), url);
459
* Test whether option objects have value handlers as expected
461
public void testValueHandlers() throws Exception {
463
// Set up the environment
464
if ( testIsSetup == false ) {
465
setupDefaultProject();
466
setupTestEnvironment();
470
for (i=0; i < testOption.length; i++) {
473
for (j=0; j < testOption[i].length; j++) {
474
IOption option = (IOption)testOption[i][j];
475
IManagedOptionValueHandler handler = option.getValueHandler();
476
String handlerExtraArg = option.getValueHandlerExtraArgument();
478
switch ( valueHandlerComparator.Compare(i+1, j+1) ) {
479
case ValueHandlerComparator.MBS:
480
assertEquals(ManagedOptionValueHandler.getManagedOptionValueHandler(), handler);
482
case ValueHandlerComparator.TEST:
483
assertTrue(handler instanceof TestValueHandler);
484
assertEquals(valueHandlerComparator.getArg(), handlerExtraArg);
492
* Test whether OptionCatgeory.getOptions(IConfiguration) works as expected
494
public void testOptions() throws Exception {
496
// Set up the environment
497
if ( testIsSetup == false ) {
498
setupDefaultProject();
499
setupTestEnvironment();
501
// Go over all option Categories
505
for (i=0; i < testCategory.length; i++) {
506
for (j=0; j < testCategory[i].length; j++) {
507
results = ((IOptionCategory)testCategory[i][j]).getOptions(testConfig);
508
// Go over results and check the following:
509
// A) results[k][0] must be the parent tool or toolChain
510
// B) results[k][1] must be in testOption[i]
511
for (int k=0; k < results[0].length; k++) {
512
if (results[k][0] == null) {
513
assertNull(results[k][1]);
516
// A) results[k][0] must be the parent tool or toolChain
523
assertTrue(results[k][0] instanceof ITool);
524
assertEquals(results[k][0], testTools[i]);
528
assertTrue(results[k][0] instanceof IToolChain);
529
assertEquals(results[k][0], testToolChain);
532
// B) results[k][1] must be in testOption[i]
533
// and its parent must be testCategory[i][j]
534
boolean found = false;
535
for (int l=0; l < testOption[i].length; l++) {
536
if (testOption[i][l] == results[k][1])
543
assertEquals(((IOption)results[k][1]).getCategory(), testCategory[i][j]);
550
* Test whether all the other tests work as expected for
551
* non-extension configurations.
553
public void testConfiguration() throws Exception {
555
// Set up the environment
557
setupTestEnvironment();
558
// Rerun the other tests, without setting the test up again