Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9a2987efbfb7cc4e2f5d8e51db440dbc361cee8c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*******************************************************************************
 * Copyright (c) 2007, 2010 Symbian Software Systems and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * Andrew Ferguson (Symbian) - Initial implementation
 * James Blackburn (Broadcom Corp.)
 *******************************************************************************/
package org.eclipse.cdt.core.settings.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.CoreModelUtil;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.settings.model.extension.impl.CDefaultConfigurationData;
import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.core.testplugin.util.BaseTestCase;
import org.eclipse.core.runtime.CoreException;

import junit.framework.TestSuite;

/**
 * Test ICConfigurationDescription reference behaviours
 */
public class CConfigurationDescriptionReferenceTests extends BaseTestCase {
	ICProject p1, p2, p3, p4;
	ICConfigurationDescription p1cd1, p1cd2, p1cd3;
	ICConfigurationDescription p2cd1, p2cd2, p2cd3;
	ICConfigurationDescription p3cd1, p3cd2, p3cd3;
	ICConfigurationDescription p4cd1, p4cd2, p4cd3;

	public static TestSuite suite() {
		return suite(CConfigurationDescriptionReferenceTests.class, "_");
	}

	@Override
	protected void setUp() throws Exception {
		p1 = CProjectHelper.createCCProject("p1", "bin");
		p2 = CProjectHelper.createCCProject("p2", "bin");
		p3 = CProjectHelper.createCCProject("p3", "bin");
		p4 = CProjectHelper.createCCProject("p4", "bin");

		CoreModel coreModel = CoreModel.getDefault();
		ICProjectDescription des1 = coreModel.getProjectDescription(p1.getProject());
		ICProjectDescription des2 = coreModel.getProjectDescription(p2.getProject());
		ICProjectDescription des3 = coreModel.getProjectDescription(p3.getProject());
		ICProjectDescription des4 = coreModel.getProjectDescription(p4.getProject());

		p1cd1 = newCfg(des1, "p1", "cd1");
		p1cd2 = newCfg(des1, "p1", "cd2");
		p1cd3 = newCfg(des1, "p1", "cd3");

		p2cd1 = newCfg(des2, "p2", "cd1");
		p2cd2 = newCfg(des2, "p2", "cd2");
		p2cd3 = newCfg(des2, "p2", "cd3");

		p3cd1 = newCfg(des3, "p3", "cd1");
		p3cd2 = newCfg(des3, "p3", "cd2");
		p3cd3 = newCfg(des3, "p3", "cd3");

		p4cd1 = newCfg(des4, "p4", "cd1");
		p4cd2 = newCfg(des4, "p4", "cd2");
		p4cd3 = newCfg(des4, "p4", "cd3");

		/*
		 * Setup references:
		 *
		 * p1: cd1 cd2 cd3
		 *        \ | /
		 *         \|/
		 *          *
		 *         /|\
		 *        / | \
		 * p2: cd1 cd2 cd3
		 *      |   |   |
		 * p3: cd1 cd2 cd3
		 *        \ | /
		 *         \|/
		 * p4: cd1 cd2 cd3
		 */

		setRefs(p1cd1, new ICConfigurationDescription[] { p2cd3 });
		setRefs(p1cd2, new ICConfigurationDescription[] { p2cd2 });
		setRefs(p1cd3, new ICConfigurationDescription[] { p2cd1 });

		setRefs(p2cd1, new ICConfigurationDescription[] { p3cd1 });
		setRefs(p2cd2, new ICConfigurationDescription[] { p3cd2 });
		setRefs(p2cd3, new ICConfigurationDescription[] { p3cd3 });

		setRefs(p3cd1, new ICConfigurationDescription[] { p4cd2 });
		setRefs(p3cd2, new ICConfigurationDescription[] { p4cd2 });
		setRefs(p3cd3, new ICConfigurationDescription[] { p4cd2 });

		coreModel.setProjectDescription(p1.getProject(), des1);
		coreModel.setProjectDescription(p2.getProject(), des2);
		coreModel.setProjectDescription(p3.getProject(), des3);
		coreModel.setProjectDescription(p4.getProject(), des4);
	}

	private void setRefs(ICConfigurationDescription node, ICConfigurationDescription[] refs) {
		Map p1RefData = new LinkedHashMap<String, String>();
		for (ICConfigurationDescription ref : refs) {
			String projectName = ref.getProjectDescription().getName();
			p1RefData.put(projectName, ref.getId());
		}
		node.setReferenceInfo(p1RefData);
	}

	private ICConfigurationDescription newCfg(ICProjectDescription des, String project, String config)
			throws CoreException {
		CDefaultConfigurationData data = new CDefaultConfigurationData(project + "." + config,
				project + " " + config + " name", null);
		data.initEmptyData();
		return des.createConfiguration(CCorePlugin.DEFAULT_PROVIDER_ID, data);
	}

	public void testConfigurationDescriptionReference() throws CoreException {
		// references

		assertEdges(p1cd1, new ICConfigurationDescription[] { p2cd3 }, true);
		assertEdges(p1cd2, new ICConfigurationDescription[] { p2cd2 }, true);
		assertEdges(p1cd3, new ICConfigurationDescription[] { p2cd1 }, true);

		assertEdges(p2cd1, new ICConfigurationDescription[] { p3cd1 }, true);
		assertEdges(p2cd2, new ICConfigurationDescription[] { p3cd2 }, true);
		assertEdges(p2cd3, new ICConfigurationDescription[] { p3cd3 }, true);

		assertEdges(p3cd1, new ICConfigurationDescription[] { p4cd2 }, true);
		assertEdges(p3cd2, new ICConfigurationDescription[] { p4cd2 }, true);
		assertEdges(p3cd3, new ICConfigurationDescription[] { p4cd2 }, true);

		assertEdges(p4cd1, new ICConfigurationDescription[] {}, true);
		assertEdges(p4cd2, new ICConfigurationDescription[] {}, true);
		assertEdges(p4cd3, new ICConfigurationDescription[] {}, true);
	}

	public void testConfigurationDescriptionReferencing() throws CoreException {
		// referencing

		assertEdges(p1cd1, new ICConfigurationDescription[] {}, false);
		assertEdges(p1cd2, new ICConfigurationDescription[] {}, false);
		assertEdges(p1cd3, new ICConfigurationDescription[] {}, false);

		assertEdges(p2cd1, new ICConfigurationDescription[] { p1cd3 }, false);
		assertEdges(p2cd2, new ICConfigurationDescription[] { p1cd2 }, false);
		assertEdges(p2cd3, new ICConfigurationDescription[] { p1cd1 }, false);

		assertEdges(p3cd1, new ICConfigurationDescription[] { p2cd1 }, false);
		assertEdges(p3cd2, new ICConfigurationDescription[] { p2cd2 }, false);
		assertEdges(p3cd3, new ICConfigurationDescription[] { p2cd3 }, false);

		assertEdges(p4cd1, new ICConfigurationDescription[] {}, false);
		assertEdges(p4cd2, new ICConfigurationDescription[] { p3cd1, p3cd2, p3cd3 }, false);
		assertEdges(p4cd3, new ICConfigurationDescription[] {}, false);
	}

	/**
	 * Test that the the referencing mechanism preserves order
	 */
	public void testDependencyOrder() throws CoreException {
		ICProject p1 = null;
		ICProject p2 = null;
		ICProject p3 = null;
		try {
			String p1Name = "referenceDependency";
			String p2Name = "refereeDependency";
			String p3Name = "referee2Dependency";
			p1 = CProjectHelper.createCCProject(p1Name, "bin");
			p2 = CProjectHelper.createCCProject(p2Name, "bin");
			p3 = CProjectHelper.createCCProject(p3Name, "bin");

			CoreModel coreModel = CoreModel.getDefault();
			ICProjectDescription des1 = coreModel.getProjectDescription(p1.getProject());
			ICProjectDescription des2 = coreModel.getProjectDescription(p2.getProject());
			ICProjectDescription des3 = coreModel.getProjectDescription(p3.getProject());
			ICConfigurationDescription p1cd1 = newCfg(des1, p1Name, "p1cd1");
			ICConfigurationDescription p2cd1 = newCfg(des2, p2Name, "p2cd1");
			ICConfigurationDescription p3cd1 = newCfg(des3, p2Name, "p3cd1");

			/* Setup references:
			 *
			 * p1: cd1
			 *      | \
			 *      |  \
			 * p2: cd1  \
			 * p3:      cd1
			 */
			setRefs(p1cd1, new ICConfigurationDescription[] { p2cd1, p3cd1 });
			coreModel.setProjectDescription(p1.getProject(), des1);
			coreModel.setProjectDescription(p2.getProject(), des2);
			coreModel.setProjectDescription(p3.getProject(), des3);

			// Check that the order is persisted
			ICConfigurationDescription[] cfgs;
			cfgs = CoreModelUtil.getReferencedConfigurationDescriptions(p1cd1, false);
			assertTrue(cfgs.length == 2);
			assertEquals(cfgs[0].getId(), p2cd1.getId());
			assertEquals(cfgs[1].getId(), p3cd1.getId());

			// Swap them round and check that the order is still persisted...
			setRefs(p1cd1, new ICConfigurationDescription[] { p3cd1, p2cd1 });
			coreModel.setProjectDescription(p1.getProject(), des1);
			cfgs = CoreModelUtil.getReferencedConfigurationDescriptions(p1cd1, false);
			assertTrue(cfgs.length == 2);
			assertEquals(cfgs[0].getId(), p3cd1.getId());
			assertEquals(cfgs[1].getId(), p2cd1.getId());
		} finally {
			if (p1 != null)
				try {
					p1.getProject().delete(true, npm());
				} catch (CoreException e) {
				}
			if (p2 != null)
				try {
					p2.getProject().delete(true, npm());
				} catch (CoreException e) {
				}
			if (p3 != null)
				try {
					p3.getProject().delete(true, npm());
				} catch (CoreException e) {
				}
		}
	}

	protected void assertEdges(ICConfigurationDescription cfgDes, ICConfigurationDescription[] expected,
			boolean references) {
		ICConfigurationDescription[] actual;

		if (references) {
			actual = CoreModelUtil.getReferencedConfigurationDescriptions(cfgDes, false);
		} else {
			actual = CoreModelUtil.getReferencingConfigurationDescriptions(cfgDes, false);
		}

		assertEquals(expected.length, actual.length);

		List actualIds = new ArrayList();
		for (ICConfigurationDescription element : actual) {
			actualIds.add(element.getId());
		}
		// check for each ID, don't use a Set so we detect duplicates
		for (ICConfigurationDescription element : expected) {
			assertTrue(element.getId() + " is missing", actualIds.contains(element.getId()));
		}
	}

	@Override
	protected void tearDown() throws Exception {
		for (Object element : Arrays.asList(new ICProject[] { p1, p2, p3, p4 })) {
			ICProject project = (ICProject) element;
			try {
				project.getProject().delete(true, npm());
			} catch (CoreException ce) {
				// try next one..
			}
		}
	}
}

Back to the top