Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 194917 | Differences between
and this patch

Collapse All | Expand All

(-)Eclipse UI Tests/org/eclipse/ui/tests/api/IAggregateWorkingSetTest.java (-103 / +158 lines)
Lines 1-103 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2008 IBM Corporation and others.
2
 * Copyright (c) 2000, 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.ui.tests.api;
11
package org.eclipse.ui.tests.api;
12
12
13
import org.eclipse.core.resources.IWorkspace;
13
import org.eclipse.core.resources.IWorkspace;
14
import org.eclipse.core.resources.ResourcesPlugin;
14
import org.eclipse.core.resources.ResourcesPlugin;
15
import org.eclipse.core.runtime.IAdaptable;
15
import org.eclipse.core.runtime.IAdaptable;
16
import org.eclipse.ui.IAggregateWorkingSet;
16
import org.eclipse.ui.IAggregateWorkingSet;
17
import org.eclipse.ui.IMemento;
17
import org.eclipse.ui.IMemento;
18
import org.eclipse.ui.IWorkingSet;
18
import org.eclipse.ui.IWorkingSet;
19
import org.eclipse.ui.IWorkingSetManager;
19
import org.eclipse.ui.IWorkingSetManager;
20
import org.eclipse.ui.XMLMemento;
20
import org.eclipse.ui.XMLMemento;
21
import org.eclipse.ui.internal.IWorkbenchConstants;
21
import org.eclipse.ui.internal.IWorkbenchConstants;
22
import org.eclipse.ui.tests.harness.util.ArrayUtil;
22
import org.eclipse.ui.tests.harness.util.ArrayUtil;
23
import org.eclipse.ui.tests.harness.util.UITestCase;
23
import org.eclipse.ui.tests.harness.util.UITestCase;
24
24
25
public class IAggregateWorkingSetTest extends UITestCase {
25
public class IAggregateWorkingSetTest extends UITestCase {
26
26
27
	final static String WORKING_SET_NAME = "testws";
27
	final static String WORKING_SET_NAME = "testws";
28
	final static String AGGREGATE_WORKING_SET_NAME_ = "testaggregatews";
28
	final static String AGGREGATE_WORKING_SET_NAME_ = "testaggregatews";
29
29
30
	IWorkspace fWorkspace;
30
	IWorkspace fWorkspace;
31
31
32
	IWorkingSet[] components;
32
	IWorkingSet[] components;
33
	IAggregateWorkingSet fWorkingSet;
33
	IAggregateWorkingSet fWorkingSet;
34
34
35
	public IAggregateWorkingSetTest(String testName) {
35
	public IAggregateWorkingSetTest(String testName) {
36
		super(testName);
36
		super(testName);
37
	}
37
	}
38
38
39
	protected void doSetUp() throws Exception {
39
	protected void doSetUp() throws Exception {
40
		super.doSetUp();
40
		super.doSetUp();
41
		IWorkingSetManager workingSetManager = fWorkbench
41
		IWorkingSetManager workingSetManager = fWorkbench
42
		.getWorkingSetManager();
42
		.getWorkingSetManager();
43
43
44
		fWorkspace = ResourcesPlugin.getWorkspace();
44
		fWorkspace = ResourcesPlugin.getWorkspace();
45
		components = new IWorkingSet[4];
45
		components = new IWorkingSet[4];
46
		for (int i = 0; i < 4; i++) {
46
		for (int i = 0; i < 4; i++) {
47
			components[i] = workingSetManager.createWorkingSet(WORKING_SET_NAME
47
			components[i] = workingSetManager.createWorkingSet(WORKING_SET_NAME
48
					+ i, new IAdaptable[] {});
48
					+ i, new IAdaptable[] {});
49
			workingSetManager.addWorkingSet(components[i]);
49
			workingSetManager.addWorkingSet(components[i]);
50
		}
50
		}
51
		fWorkingSet = (IAggregateWorkingSet) workingSetManager
51
		fWorkingSet = (IAggregateWorkingSet) workingSetManager
52
		.createAggregateWorkingSet(AGGREGATE_WORKING_SET_NAME_,
52
		.createAggregateWorkingSet(AGGREGATE_WORKING_SET_NAME_,
53
				AGGREGATE_WORKING_SET_NAME_, components);
53
				AGGREGATE_WORKING_SET_NAME_, components);
54
54
55
		workingSetManager.addWorkingSet(fWorkingSet);
55
		workingSetManager.addWorkingSet(fWorkingSet);
56
	}
56
	}
57
	protected void doTearDown() throws Exception {
57
	protected void doTearDown() throws Exception {
58
		IWorkingSetManager workingSetManager = fWorkbench.getWorkingSetManager();
58
		IWorkingSetManager workingSetManager = fWorkbench.getWorkingSetManager();
59
		workingSetManager.removeWorkingSet(fWorkingSet);
59
		workingSetManager.removeWorkingSet(fWorkingSet);
60
		for (int i = 0; i < components.length; i++) {
60
		for (int i = 0; i < components.length; i++) {
61
			workingSetManager.removeWorkingSet(components[i]);
61
			workingSetManager.removeWorkingSet(components[i]);
62
		}    	
62
		}    	
63
		super.doTearDown();
63
		super.doTearDown();
64
	}
64
	}
65
65
66
	public void testSaveWSet() throws Throwable {
66
	public void testSaveWSet() throws Throwable {
67
		//<possible client code>
67
		//<possible client code>
68
		IWorkingSetManager workingSetManager = fWorkbench
68
		IWorkingSetManager workingSetManager = fWorkbench
69
		.getWorkingSetManager();
69
		.getWorkingSetManager();
70
		IWorkingSet set=workingSetManager.getWorkingSet(AGGREGATE_WORKING_SET_NAME_);
70
		IWorkingSet set=workingSetManager.getWorkingSet(AGGREGATE_WORKING_SET_NAME_);
71
		if(set.isAggregateWorkingSet()){
71
		if(set.isAggregateWorkingSet()){
72
			IWorkingSet[] sets=((IAggregateWorkingSet)set).getComponents();
72
			IWorkingSet[] sets=((IAggregateWorkingSet)set).getComponents();
73
			if(sets.length>=1){
73
			if(sets.length>=1){
74
				sets[0]=null; //client fails to pay enough attention to specs or unknowingly does this
74
				sets[0]=null; //client fails to pay enough attention to specs or unknowingly does this
75
			}
75
			}
76
		}
76
		}
77
		//</possible client code>    	
77
		//</possible client code>    	
78
		//error makes it look like it comes from workingsets api, with no clue about the actual culprit 
78
		//error makes it look like it comes from workingsets api, with no clue about the actual culprit 
79
		IMemento memento=XMLMemento.createWriteRoot(IWorkbenchConstants.TAG_WORKING_SET);
79
		IMemento memento=XMLMemento.createWriteRoot(IWorkbenchConstants.TAG_WORKING_SET);
80
		set.saveState(memento);
80
		set.saveState(memento);
81
	}
81
	}
82
	public void testGetElemets() throws Throwable {
82
	public void testGetElemets() throws Throwable {
83
		//<possible client code>
83
		//<possible client code>
84
		IWorkingSetManager workingSetManager = fWorkbench
84
		IWorkingSetManager workingSetManager = fWorkbench
85
		.getWorkingSetManager();
85
		.getWorkingSetManager();
86
		IWorkingSet set=workingSetManager.getWorkingSet(AGGREGATE_WORKING_SET_NAME_);
86
		IWorkingSet set=workingSetManager.getWorkingSet(AGGREGATE_WORKING_SET_NAME_);
87
		if(set.isAggregateWorkingSet()){
87
		if(set.isAggregateWorkingSet()){
88
			IWorkingSet[] sets=((IAggregateWorkingSet)set).getComponents();
88
			IWorkingSet[] sets=((IAggregateWorkingSet)set).getComponents();
89
			if(sets.length>1){
89
			if(sets.length>1){
90
				//code 2 fails to pay enough attention to specs or unknowingly does this
90
				//code 2 fails to pay enough attention to specs or unknowingly does this
91
				sets[0]=workingSetManager.createWorkingSet(WORKING_SET_NAME, new IAdaptable[] { fWorkspace.getRoot() }); 
91
				sets[0]=workingSetManager.createWorkingSet(WORKING_SET_NAME, new IAdaptable[] { fWorkspace.getRoot() }); 
92
				//code 1 part  removes a workingset
92
				//code 1 part  removes a workingset
93
				workingSetManager.removeWorkingSet(sets[1]);
93
				workingSetManager.removeWorkingSet(sets[1]);
94
			}
94
			}
95
		}
95
		}
96
		//</possible client code>    	
96
		//</possible client code>    	
97
		
97
		
98
		//unexpected 
98
		//unexpected 
99
		assertTrue(ArrayUtil.equals(
99
		assertTrue(ArrayUtil.equals(
100
				new IAdaptable[] {},
100
				new IAdaptable[] {},
101
				fWorkingSet.getElements()));
101
				fWorkingSet.getElements()));
102
	}
102
	}
103
}
103
104
	/**
105
	 * Core of the problem: while Eclipse is running, name collisions among working sets
106
	 * don't matter. However, on save and restart names will be used to identify working
107
	 * sets, which could possibly lead to cycles in aggregate working sets.
108
	 * 
109
	 * Bottom line: if there are multiple aggregate working sets with the same name, expect
110
	 * trouble on restart.
111
	 * 
112
	 * To create a cycle we have to be creative:
113
	 * - create an aggregate1 with an ID = "testCycle"
114
	 * - create an aggregate2 with an ID = "testCycle" containing aggregate1
115
	 * - save it into IMemento
116
	 * 
117
	 * Now the IMememnto creates a self reference:
118
	 * 
119
	 * <workingSet name="testCycle" label="testCycle" aggregate="true">
120
	 * 	<workingSet IMemento.internal.id="testCycle" />
121
	 * </workingSet>
122
	 * 
123
	 * All we have to do to emulate stack overflow is to create a working set based on this IMemento.
124
	 * 
125
	 * @throws Throwable
126
	 */
127
	public void testDirectCycle() throws Throwable {
128
		IWorkingSetManager manager = fWorkbench.getWorkingSetManager();
129
		
130
		// 1) create an IMemento with a cycle in it
131
		IAggregateWorkingSet aggregate = (IAggregateWorkingSet) manager
132
		.createAggregateWorkingSet("testCycle","testCycle", new IWorkingSet[0]);
133
		
134
		IAggregateWorkingSet aggregate2 = (IAggregateWorkingSet) manager
135
		.createAggregateWorkingSet("testCycle","testCycle", new IWorkingSet[] {aggregate});
136
		
137
		IMemento memento=XMLMemento.createWriteRoot(IWorkbenchConstants.TAG_WORKING_SET);
138
		aggregate2.saveState(memento);
139
		
140
		// 2) clean the slate
141
		manager.removeWorkingSet(aggregate);
142
		manager.removeWorkingSet(aggregate2);
143
		
144
		// 3) load the IMemento
145
		try {
146
			IAggregateWorkingSet aggregateReloaded = (IAggregateWorkingSet) manager.createWorkingSet(memento);
147
			manager.addWorkingSet(aggregateReloaded);
148
			aggregateReloaded.getComponents();
149
			// TBD test updates as well
150
		} catch (StackOverflowError e) {
151
			e.printStackTrace();
152
			fail("Stack overflow for self-referenced aggregate working set", e);
153
		}
154
	}
155
	// TBD create a test for indirect inclusions: A contains B contains A
156
157
158
}

Return to bug 194917