Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 98ac5c5288442dabb53f95cee30f8f4b166c90d7 (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
272
273
274
275
276
277
278
/*******************************************************************************
 * Copyright (c) 2008, 2015 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.ui.tests.wizards.settingswizards;

import java.io.FileWriter;
import java.util.Arrays;
import java.util.List;

import org.eclipse.cdt.core.dom.IPDOMManager;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.settings.model.CIncludePathEntry;
import org.eclipse.cdt.core.settings.model.CMacroEntry;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.core.settings.model.ICFolderDescription;
import org.eclipse.cdt.core.settings.model.ICLanguageSetting;
import org.eclipse.cdt.core.settings.model.ICLanguageSettingEntry;
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
import org.eclipse.cdt.core.settings.model.ICSettingEntry;
import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.internal.ui.wizards.settingswizards.ISettingsProcessor;
import org.eclipse.cdt.internal.ui.wizards.settingswizards.IncludePathsSettingsProcessor;
import org.eclipse.cdt.internal.ui.wizards.settingswizards.MacroSettingsProcessor;
import org.eclipse.cdt.internal.ui.wizards.settingswizards.ProjectSettingsExportStrategy;
import org.eclipse.cdt.internal.ui.wizards.settingswizards.ProjectSettingsImportStrategy;
import org.eclipse.cdt.ui.tests.BaseUITestCase;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.IMessageProvider;

public class SettingsImportExportTest extends BaseUITestCase {

	private static ICLanguageSettingEntry[] EXPORTED_MACROS = new ICLanguageSettingEntry[] {
			new CMacroEntry("MAC1", "value1", 0), new CMacroEntry("anothermacro", "", 0),
			new CMacroEntry("smac", "blah", 0) };

	private static ICLanguageSettingEntry[] EXPORTED_INCLUDES = new ICLanguageSettingEntry[] {
			new CIncludePathEntry("/path/to/somewhere", 0), new CIncludePathEntry("/blah/blah/blah", 0),
			new CIncludePathEntry("pantera/is/awesome", 0) };

	public SettingsImportExportTest() {
	}

	public SettingsImportExportTest(String name) {
		super(name);
	}

	// This could be replaced with an extension point
	private static final List<ISettingsProcessor> processors = Arrays
			.<ISettingsProcessor>asList(new IncludePathsSettingsProcessor(), new MacroSettingsProcessor());

	private static void createFile(String contents, String filePath) throws Exception {
		IPath path = new Path(filePath);
		FileWriter writer = new FileWriter(path.toFile());
		writer.write(contents);
		writer.close();
	}

	private static void deleteFile(String filePath) {
		new Path(filePath).toFile().delete();
	}

	private static String getFilePath(String fileName) {
		IPath workspaceLocation = ResourcesPlugin.getWorkspace().getRoot().getLocation();
		return workspaceLocation.toOSString() + IPath.SEPARATOR + fileName;
	}

	private void setUpProjectSettings(ICProject cProject) throws Exception {
		IProject project = cProject.getProject();
		ICProjectDescription desc = CoreModel.getDefault().getProjectDescription(project, true);
		ICConfigurationDescription config = desc.getActiveConfiguration();
		ICFolderDescription folder = config.getRootFolderDescription();
		ICLanguageSetting[] languageSettings = folder.getLanguageSettings();
		ICLanguageSetting languageSetting = languageSettings[0];
		languageSetting.setSettingEntries(ICSettingEntry.MACRO, Arrays.asList(EXPORTED_MACROS));
		languageSetting.setSettingEntries(ICSettingEntry.INCLUDE_PATH, EXPORTED_INCLUDES);
		CoreModel.getDefault().setProjectDescription(project, desc);
	}

	public void testNormalExportImport() throws Exception {
		ICProject exportProject = CProjectHelper.createNewStyleCProject("TempProject1", IPDOMManager.ID_NO_INDEXER);
		ICProject importProject = CProjectHelper.createNewStyleCProject("TempProject2", IPDOMManager.ID_NO_INDEXER);
		setUpProjectSettings(exportProject);

		ProjectSettingsWizardPageMock page = new ProjectSettingsWizardPageMock() {
			@Override
			public void setMessage(String message, int flag) {
				if (flag == IMessageProvider.ERROR)
					fail("there should be no error message displayed");
			}

			@Override
			public void showErrorDialog(String dialogTitle, String message) {
				fail("the error dialog should not be displayed");
			}
		};

		page.setDestinationFilePath(getFilePath("settings.xml"));
		page.setSettingsProcessors(processors);
		page.setSelectedSettingsProcessors(processors);
		ICProjectDescription desc = CoreModel.getDefault().getProjectDescription(exportProject.getProject(), false);
		ICConfigurationDescription config = desc.getActiveConfiguration();
		page.setSelectedConfiguration(config);

		ProjectSettingsExportStrategy exporter = new ProjectSettingsExportStrategy();
		exporter.finish(page);

		// now import into another project

		desc = CoreModel.getDefault().getProjectDescription(importProject.getProject(), true);
		config = desc.getActiveConfiguration();
		page.setSelectedConfiguration(config);

		ProjectSettingsImportStrategy importer = new ProjectSettingsImportStrategy();
		importer.finish(page);

		desc = CoreModel.getDefault().getProjectDescription(importProject.getProject(), true);
		config = desc.getActiveConfiguration();
		ICFolderDescription folder = config.getRootFolderDescription();
		ICLanguageSetting languageSetting = folder.getLanguageSettings()[0];

		ICLanguageSettingEntry[] importedMacros = languageSetting.getSettingEntries(ICSettingEntry.MACRO);

		assertEquals(EXPORTED_MACROS.length, importedMacros.length);
		for (int i = 0; i < importedMacros.length; i++) {
			assertEquals(EXPORTED_MACROS[i].getName(), importedMacros[i].getName());
			assertEquals(EXPORTED_MACROS[i].getValue(), importedMacros[i].getValue());
		}

		ICLanguageSettingEntry[] importedIncludes = languageSetting.getSettingEntries(ICSettingEntry.INCLUDE_PATH);

		assertEquals(EXPORTED_INCLUDES.length, importedIncludes.length);
		for (int i = 0; i < importedIncludes.length; i++) {
			assertTrue(importedIncludes[i].getName().endsWith(EXPORTED_INCLUDES[i].getName()));
		}

		CProjectHelper.delete(importProject);
		CProjectHelper.delete(exportProject);
	}

	public static void vaidateCorrectErrorHandling(String xmlContent) throws Exception {
		String filePath = getFilePath("test.txt");
		createFile(xmlContent, filePath);

		ICProject project = CProjectHelper.createNewStyleCProject("VaidateProject", IPDOMManager.ID_NO_INDEXER);

		ICProjectDescription desc = CoreModel.getDefault().getProjectDescription(project.getProject(), false);
		ICConfigurationDescription config = desc.getActiveConfiguration();

		final boolean[] errorDialogShown = new boolean[] { false };
		ProjectSettingsWizardPageMock page = new ProjectSettingsWizardPageMock() {
			@Override
			public void setMessage(String message, int flag) {
				fail();
			}

			@Override
			public void showErrorDialog(String dialogTitle, String message) {
				errorDialogShown[0] = true;
			}
		};
		page.setDestinationFilePath(filePath);
		page.setSettingsProcessors(processors);
		page.setSelectedSettingsProcessors(processors);
		page.setSelectedConfiguration(config);

		ProjectSettingsImportStrategy importer = new ProjectSettingsImportStrategy();
		importer.finish(page);

		assertTrue(xmlContent, errorDialogShown[0]);

		assertNoSettingsImported(project.getProject());

		// TODO test that no macros or includes were imported
		CProjectHelper.delete(project);
		deleteFile(filePath);
	}

	private static void assertNoSettingsImported(IProject project) {
		ICProjectDescription desc = CoreModel.getDefault().getProjectDescription(project, true);
		ICConfigurationDescription config = desc.getActiveConfiguration();
		ICFolderDescription folder = config.getRootFolderDescription();
		ICLanguageSetting[] languageSettings = folder.getLanguageSettings();
		for (ICLanguageSetting languageSetting : languageSettings) {
			ICLanguageSettingEntry[] entries = languageSetting.getSettingEntries(ICSettingEntry.MACRO);
			for (ICLanguageSettingEntry entry : entries) {
				assertTrue(entry.isBuiltIn());
			}
			entries = languageSetting.getSettingEntries(ICSettingEntry.INCLUDE_PATH);
			for (ICLanguageSettingEntry entry : entries) {
				assertTrue(entry.isBuiltIn());
			}
		}
	}

	// {badXML1}
	// blah blah blah
	//
	// {badXML2}
	// <cdtprojectproperties></cdtprojectproperties>
	//
	// {badXML3}
	// <cdtprojectproperties>
	// <section name="invalidsectionname">
	// </section>
	// </cdtprojectproperties>
	//
	// {badXML4}
	// <cdtprojectproperties>
	// <section name="org.eclipse.cdt.internal.ui.wizards.settingswizards.Macros">
	// <invalidtag>
	// </invalidtag>
	// </section>
	// </cdtprojectproperties>
	//
	// {badXML5}
	// <cdtprojectproperties>
	// <section name="org.eclipse.cdt.internal.ui.wizards.settingswizards.Macros">
	// <macro>
	// <name>aaaa</name><value></value>
	// </macro>
	// </section>
	// </cdtprojectproperties>
	//
	// {badXML6}
	// <cdtprojectproperties>
	// <section name="org.eclipse.cdt.internal.ui.wizards.settingswizards.Macros">
	// <language name="GNU C++">
	// <macro>
	// <name>aaaa</name>
	// </macro>
	// </language>
	// </section>
	// </cdtprojectproperties>
	//
	// {badXML7}
	// <cdtprojectproperties>
	// <section name="org.eclipse.cdt.internal.ui.wizards.settingswizards.Macros">
	// <language name="GNU C++">
	// <macro>
	// <name>aaaa</name><value></value><value></value>
	// </macro>
	// </language>
	// </section>
	// </cdtprojectproperties>
	//
	// {badXML8}
	// <cdtprojectproperties>
	// <section name="org.eclipse.cdt.internal.ui.wizards.settingswizards.IncludePaths">
	// <language name="GNU C++">
	// <includepath>C:\WINDOWS</includepath><invalid></invalid>
	// </language>
	// </section>
	// </cdtprojectproperties>
	public void testNotValid() throws Exception {
		vaidateCorrectErrorHandling(readTaggedComment("badXML1"));
		vaidateCorrectErrorHandling(readTaggedComment("badXML2"));
		vaidateCorrectErrorHandling(readTaggedComment("badXML3"));
		vaidateCorrectErrorHandling(readTaggedComment("badXML4"));
		vaidateCorrectErrorHandling(readTaggedComment("badXML5")); // missing <language> tag
		vaidateCorrectErrorHandling(readTaggedComment("badXML6")); // missing <value> tag
		vaidateCorrectErrorHandling(readTaggedComment("badXML7")); // extra <value> tag
		vaidateCorrectErrorHandling(readTaggedComment("badXML8"));
	}
}

Back to the top