core/com.nokia.carbide.cpp.codescanner.tests/src/com/nokia/carbide/cpp/codescanner/tests/CSConfigManagerTest.java
author wpaul
Mon, 24 May 2010 21:20:25 -0500
changeset 1395 0ca4f91f68bb
parent 34 60053ab1f6b7
permissions -rw-r--r--
add project validation to our launch types as it's not done by CDT with us passing the DONT_CHECK_PROGRAM flag.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
cawthron
parents:
diff changeset
     1
/*
cawthron
parents:
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
cawthron
parents:
diff changeset
     3
* All rights reserved.
cawthron
parents:
diff changeset
     4
* This component and the accompanying materials are made available
cawthron
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
cawthron
parents:
diff changeset
     6
* which accompanies this distribution, and is available
cawthron
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
cawthron
parents:
diff changeset
     8
*
cawthron
parents:
diff changeset
     9
* Initial Contributors:
cawthron
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
cawthron
parents:
diff changeset
    11
*
cawthron
parents:
diff changeset
    12
* Contributors:
cawthron
parents:
diff changeset
    13
*
cawthron
parents:
diff changeset
    14
* Description: 
cawthron
parents:
diff changeset
    15
*
cawthron
parents:
diff changeset
    16
*/
cawthron
parents:
diff changeset
    17
cawthron
parents:
diff changeset
    18
package com.nokia.carbide.cpp.codescanner.tests;
cawthron
parents:
diff changeset
    19
cawthron
parents:
diff changeset
    20
import java.io.File;
cawthron
parents:
diff changeset
    21
cawthron
parents:
diff changeset
    22
import junit.framework.TestCase;
cawthron
parents:
diff changeset
    23
cawthron
parents:
diff changeset
    24
import org.eclipse.core.resources.IProject;
cawthron
parents:
diff changeset
    25
import org.eclipse.core.runtime.CoreException;
cawthron
parents:
diff changeset
    26
import org.eclipse.core.runtime.IProgressMonitor;
cawthron
parents:
diff changeset
    27
import org.eclipse.core.runtime.NullProgressMonitor;
cawthron
parents:
diff changeset
    28
import org.eclipse.jface.dialogs.IDialogSettings;
cawthron
parents:
diff changeset
    29
import org.eclipse.jface.preference.IPreferenceStore;
cawthron
parents:
diff changeset
    30
cawthron
parents:
diff changeset
    31
import com.nokia.carbide.cpp.internal.codescanner.CSPlugin;
cawthron
parents:
diff changeset
    32
import com.nokia.carbide.cpp.internal.codescanner.config.CSConfigManager;
cawthron
parents:
diff changeset
    33
import com.nokia.carbide.cpp.internal.codescanner.config.CSConfigSettings;
cawthron
parents:
diff changeset
    34
import com.nokia.carbide.cpp.internal.codescanner.config.CSProjectSettings;
cawthron
parents:
diff changeset
    35
import com.nokia.carbide.cpp.internal.codescanner.ui.CSFileFiltersTabPage;
cawthron
parents:
diff changeset
    36
import com.nokia.carbide.cpp.internal.codescanner.ui.CSGeneralTabPage;
cawthron
parents:
diff changeset
    37
import com.nokia.carbide.cpp.internal.codescanner.ui.CSPreferenceConstants;
cawthron
parents:
diff changeset
    38
import com.nokia.carbide.cpp.internal.codescanner.ui.CSPreferenceInitializer;
cawthron
parents:
diff changeset
    39
import com.nokia.carbide.cpp.internal.codescanner.ui.CSRulesTabPage;
cawthron
parents:
diff changeset
    40
import com.nokia.carbide.cpp.project.core.ProjectCorePlugin;
cawthron
parents:
diff changeset
    41
cawthron
parents:
diff changeset
    42
/**
cawthron
parents:
diff changeset
    43
 * Test cases for Class CSConfigManager.
cawthron
parents:
diff changeset
    44
 * 
cawthron
parents:
diff changeset
    45
 */
cawthron
parents:
diff changeset
    46
public class CSConfigManagerTest extends TestCase {
cawthron
parents:
diff changeset
    47
cawthron
parents:
diff changeset
    48
	private static CSConfigManager configManager;
cawthron
parents:
diff changeset
    49
	private static IProgressMonitor monitor;
cawthron
parents:
diff changeset
    50
	private static IProject project;
cawthron
parents:
diff changeset
    51
cawthron
parents:
diff changeset
    52
	protected void setUp() throws Exception {
cawthron
parents:
diff changeset
    53
		super.setUp();
cawthron
parents:
diff changeset
    54
		try {
cawthron
parents:
diff changeset
    55
			if (configManager == null) {
cawthron
parents:
diff changeset
    56
				configManager = new CSConfigManager();				
cawthron
parents:
diff changeset
    57
			}
cawthron
parents:
diff changeset
    58
			if (monitor == null) {
cawthron
parents:
diff changeset
    59
				monitor = new NullProgressMonitor();				
cawthron
parents:
diff changeset
    60
			}
cawthron
parents:
diff changeset
    61
			if (project == null) {
cawthron
parents:
diff changeset
    62
				project = ProjectCorePlugin.createProject("CSConfigManagerTest", null);
cawthron
parents:
diff changeset
    63
			}
cawthron
parents:
diff changeset
    64
			project.open(monitor);
cawthron
parents:
diff changeset
    65
		} catch (CoreException e) {
cawthron
parents:
diff changeset
    66
			fail();
cawthron
parents:
diff changeset
    67
		}
cawthron
parents:
diff changeset
    68
	}
cawthron
parents:
diff changeset
    69
cawthron
parents:
diff changeset
    70
	protected void tearDown() throws Exception {
cawthron
parents:
diff changeset
    71
		try {
cawthron
parents:
diff changeset
    72
			project.close(monitor);
cawthron
parents:
diff changeset
    73
		} catch (CoreException e) {
cawthron
parents:
diff changeset
    74
			fail();
cawthron
parents:
diff changeset
    75
		}
cawthron
parents:
diff changeset
    76
		super.tearDown();
cawthron
parents:
diff changeset
    77
	}
cawthron
parents:
diff changeset
    78
cawthron
parents:
diff changeset
    79
	public void testGetDefaultConfig() {
cawthron
parents:
diff changeset
    80
		CSConfigSettings settings = configManager.getDefaultConfig();
cawthron
parents:
diff changeset
    81
		assertNotNull(settings);
cawthron
parents:
diff changeset
    82
	}
cawthron
parents:
diff changeset
    83
cawthron
parents:
diff changeset
    84
	public void testCreateProjectSettings() {
cawthron
parents:
diff changeset
    85
		CSProjectSettings projectSettings = configManager.createProjectSettings(project);
cawthron
parents:
diff changeset
    86
		assertNotNull(projectSettings);
cawthron
parents:
diff changeset
    87
	}
cawthron
parents:
diff changeset
    88
cawthron
parents:
diff changeset
    89
	public void testGetProjectSettings() {
cawthron
parents:
diff changeset
    90
		CSProjectSettings projectSettings = configManager.getProjectSettings(project);
cawthron
parents:
diff changeset
    91
		assertNotNull(projectSettings);
cawthron
parents:
diff changeset
    92
	}
cawthron
parents:
diff changeset
    93
cawthron
parents:
diff changeset
    94
	public void testCreateProjectConfigFile() {
cawthron
parents:
diff changeset
    95
		CSProjectSettings projectSettings = configManager.getProjectSettings(project);
cawthron
parents:
diff changeset
    96
		getProjPropertySettings(projectSettings);
cawthron
parents:
diff changeset
    97
		String configFilePath = getConfigFilePath();
cawthron
parents:
diff changeset
    98
		File configFile = configManager.createProjectConfigFile(project, configFilePath);
cawthron
parents:
diff changeset
    99
		assertNotNull(configFile);
cawthron
parents:
diff changeset
   100
		assertTrue(configFile.exists());
cawthron
parents:
diff changeset
   101
		configFile.delete();
cawthron
parents:
diff changeset
   102
	}
cawthron
parents:
diff changeset
   103
cawthron
parents:
diff changeset
   104
	public void testCreateGlobalConfigFile() {
cawthron
parents:
diff changeset
   105
		CSPreferenceInitializer prefInitializer = new CSPreferenceInitializer();
cawthron
parents:
diff changeset
   106
		prefInitializer.initializeDefaultPreferences();
cawthron
parents:
diff changeset
   107
		String configFilePath = getConfigFilePath();
cawthron
parents:
diff changeset
   108
		File configFile = configManager.createGlobalConfigFile(null, configFilePath);
cawthron
parents:
diff changeset
   109
		assertNotNull(configFile);
cawthron
parents:
diff changeset
   110
		assertTrue(configFile.exists());
cawthron
parents:
diff changeset
   111
		configFile.delete();
cawthron
parents:
diff changeset
   112
	}
cawthron
parents:
diff changeset
   113
cawthron
parents:
diff changeset
   114
	public void testCreateConfigFile() {
cawthron
parents:
diff changeset
   115
		CSProjectSettings projectSettings = configManager.getProjectSettings(project);
cawthron
parents:
diff changeset
   116
		getProjPropertySettings(projectSettings);
cawthron
parents:
diff changeset
   117
		CSPreferenceInitializer prefInitializer = new CSPreferenceInitializer();
cawthron
parents:
diff changeset
   118
		prefInitializer.initializeDefaultPreferences();
cawthron
parents:
diff changeset
   119
		String configFilePath = getConfigFilePath();
cawthron
parents:
diff changeset
   120
		File configFile = configManager.createConfigFile(null, configFilePath);
cawthron
parents:
diff changeset
   121
		assertNotNull(configFile);
cawthron
parents:
diff changeset
   122
		assertTrue(configFile.exists());
cawthron
parents:
diff changeset
   123
		configFile.delete();
cawthron
parents:
diff changeset
   124
		configFile = configManager.createConfigFile(project, configFilePath);
cawthron
parents:
diff changeset
   125
		assertNotNull(configFile);
cawthron
parents:
diff changeset
   126
		assertTrue(configFile.exists());
cawthron
parents:
diff changeset
   127
		configFile.delete();
cawthron
parents:
diff changeset
   128
		projectSettings.getDialogSettings().getSection(CSPreferenceConstants.PROPERTY_SETTINGS_ID).put(CSPreferenceConstants.PROJ_SETTINGS, false);
cawthron
parents:
diff changeset
   129
		configFile = configManager.createConfigFile(project, configFilePath);
cawthron
parents:
diff changeset
   130
		assertNotNull(configFile);
cawthron
parents:
diff changeset
   131
		assertTrue(configFile.exists());
cawthron
parents:
diff changeset
   132
		configFile.delete();
cawthron
parents:
diff changeset
   133
	}
cawthron
parents:
diff changeset
   134
cawthron
parents:
diff changeset
   135
	public void testLoadProjectConfigSettings() {
cawthron
parents:
diff changeset
   136
		CSProjectSettings projectSettings = configManager.getProjectSettings(project);
cawthron
parents:
diff changeset
   137
		getProjPropertySettings(projectSettings);
cawthron
parents:
diff changeset
   138
		assertTrue(configManager.loadProjectConfigSettings(project));
cawthron
parents:
diff changeset
   139
		projectSettings.getDialogSettings().getSection(CSPreferenceConstants.PROPERTY_SETTINGS_ID).put(CSPreferenceConstants.PROJ_SETTINGS, false);
cawthron
parents:
diff changeset
   140
		assertFalse(configManager.loadProjectConfigSettings(project));
cawthron
parents:
diff changeset
   141
	}
cawthron
parents:
diff changeset
   142
cawthron
parents:
diff changeset
   143
	public void testLoadGlobalConfigSettings() {
cawthron
parents:
diff changeset
   144
		CSPreferenceInitializer prefInitializer = new CSPreferenceInitializer();
cawthron
parents:
diff changeset
   145
		prefInitializer.initializeDefaultPreferences();
cawthron
parents:
diff changeset
   146
		assertTrue(configManager.loadGlobalConfigSettings());
cawthron
parents:
diff changeset
   147
	}
cawthron
parents:
diff changeset
   148
cawthron
parents:
diff changeset
   149
	public void testLoadConfigSettings() {
cawthron
parents:
diff changeset
   150
		CSProjectSettings projectSettings = configManager.getProjectSettings(project);
cawthron
parents:
diff changeset
   151
		getProjPropertySettings(projectSettings);
cawthron
parents:
diff changeset
   152
		CSPreferenceInitializer prefInitializer = new CSPreferenceInitializer();
cawthron
parents:
diff changeset
   153
		prefInitializer.initializeDefaultPreferences();
cawthron
parents:
diff changeset
   154
		assertTrue(configManager.loadConfigSettings(null));
cawthron
parents:
diff changeset
   155
		assertTrue(configManager.loadConfigSettings(project));
cawthron
parents:
diff changeset
   156
		projectSettings.getDialogSettings().getSection(CSPreferenceConstants.PROPERTY_SETTINGS_ID).put(CSPreferenceConstants.PROJ_SETTINGS, false);
cawthron
parents:
diff changeset
   157
		assertTrue(configManager.loadConfigSettings(project));
cawthron
parents:
diff changeset
   158
	}
cawthron
parents:
diff changeset
   159
cawthron
parents:
diff changeset
   160
	public void testLoadProjectConfigFile() {
cawthron
parents:
diff changeset
   161
		CSProjectSettings projectSettings = configManager.getProjectSettings(project);
cawthron
parents:
diff changeset
   162
		IDialogSettings settings = getProjPropertySettings(projectSettings);
cawthron
parents:
diff changeset
   163
		String configFilePath = getConfigFilePath();
cawthron
parents:
diff changeset
   164
		File configFile = new File(configFilePath);
cawthron
parents:
diff changeset
   165
		assertNotNull(configFile);
cawthron
parents:
diff changeset
   166
		configManager.getDefaultConfig().saveConfig(configFile);
cawthron
parents:
diff changeset
   167
		assertTrue(configFile.exists());
cawthron
parents:
diff changeset
   168
		configManager.loadProjectConfigFile(configFilePath, settings);
cawthron
parents:
diff changeset
   169
		assertTrue(settings.get(CSPreferenceConstants.RULE_SCRIPTS).length() > 0);
cawthron
parents:
diff changeset
   170
		assertTrue(settings.get(CSPreferenceConstants.RULE_CATEGORIES).length() > 0);
cawthron
parents:
diff changeset
   171
		assertTrue(settings.get(CSPreferenceConstants.RULE_SEVERITIES).length() > 0);
cawthron
parents:
diff changeset
   172
		assertTrue(settings.get(CSPreferenceConstants.RULES_ENABLED).length() > 0);
cawthron
parents:
diff changeset
   173
		assertTrue(settings.get(CSPreferenceConstants.CCLASSIGNORE).length() > 0);
34
60053ab1f6b7 Added support for IAD rules; fix for Bug 8251.
stechong
parents: 2
diff changeset
   174
		assertTrue(settings.get(CSPreferenceConstants.ICONS).length() == 0);
2
cawthron
parents:
diff changeset
   175
		assertTrue(settings.get(CSPreferenceConstants.LFUNCTIONIGNORE).length() > 0);
cawthron
parents:
diff changeset
   176
		assertTrue(settings.get(CSPreferenceConstants.LONGLINES_LENGTH).length() > 0);
cawthron
parents:
diff changeset
   177
		assertTrue(settings.get(CSPreferenceConstants.OPENIGNORE).length() > 0);
cawthron
parents:
diff changeset
   178
		assertTrue(settings.get(CSPreferenceConstants.WORRYINGCOMMENTS).length() > 0);
cawthron
parents:
diff changeset
   179
		assertTrue(settings.getInt(CSPreferenceConstants.LONGLINES_LENGTH) > 0);
cawthron
parents:
diff changeset
   180
		configFile.delete();
cawthron
parents:
diff changeset
   181
	}
cawthron
parents:
diff changeset
   182
cawthron
parents:
diff changeset
   183
	public void testLoadGlobalConfigFile() {
cawthron
parents:
diff changeset
   184
		CSPreferenceInitializer prefInitializer = new CSPreferenceInitializer();
cawthron
parents:
diff changeset
   185
		prefInitializer.initializeDefaultPreferences();
cawthron
parents:
diff changeset
   186
		String configFilePath = getConfigFilePath();
cawthron
parents:
diff changeset
   187
		File configFile = new File(configFilePath);
cawthron
parents:
diff changeset
   188
		assertNotNull(configFile);
cawthron
parents:
diff changeset
   189
		configManager.getDefaultConfig().saveConfig(configFile);
cawthron
parents:
diff changeset
   190
		assertTrue(configFile.exists());
cawthron
parents:
diff changeset
   191
		configManager.loadGlobalConfigFile(configFilePath);
cawthron
parents:
diff changeset
   192
		IPreferenceStore store = CSPlugin.getCSPrefsStore();
cawthron
parents:
diff changeset
   193
		assertTrue(store.getString(CSPreferenceConstants.RULE_SCRIPTS).length() > 0);
cawthron
parents:
diff changeset
   194
		assertTrue(store.getString(CSPreferenceConstants.RULE_CATEGORIES).length() > 0);
cawthron
parents:
diff changeset
   195
		assertTrue(store.getString(CSPreferenceConstants.RULE_SEVERITIES).length() > 0);
cawthron
parents:
diff changeset
   196
		assertTrue(store.getString(CSPreferenceConstants.RULES_ENABLED).length() > 0);
cawthron
parents:
diff changeset
   197
		assertTrue(store.getString(CSPreferenceConstants.CCLASSIGNORE).length() > 0);
34
60053ab1f6b7 Added support for IAD rules; fix for Bug 8251.
stechong
parents: 2
diff changeset
   198
		assertTrue(store.getString(CSPreferenceConstants.ICONS).length() >= 0);
2
cawthron
parents:
diff changeset
   199
		assertTrue(store.getString(CSPreferenceConstants.LFUNCTIONIGNORE).length() > 0);
cawthron
parents:
diff changeset
   200
		assertTrue(store.getString(CSPreferenceConstants.LONGLINES_LENGTH).length() > 0);
cawthron
parents:
diff changeset
   201
		assertTrue(store.getString(CSPreferenceConstants.OPENIGNORE).length() > 0);
cawthron
parents:
diff changeset
   202
		assertTrue(store.getString(CSPreferenceConstants.WORRYINGCOMMENTS).length() > 0);
cawthron
parents:
diff changeset
   203
		assertTrue(store.getInt(CSPreferenceConstants.LONGLINES_LENGTH) > 0);
cawthron
parents:
diff changeset
   204
		configFile.delete();
cawthron
parents:
diff changeset
   205
	}
cawthron
parents:
diff changeset
   206
cawthron
parents:
diff changeset
   207
	private String getConfigFilePath() {
cawthron
parents:
diff changeset
   208
		String configFilePath = project.getLocation() + CSConfigManager.CS_CONFIG_SETTINGS_FILE;
cawthron
parents:
diff changeset
   209
		return configFilePath;
cawthron
parents:
diff changeset
   210
	}
cawthron
parents:
diff changeset
   211
cawthron
parents:
diff changeset
   212
	private IDialogSettings getProjPropertySettings(CSProjectSettings projectSettings) {
cawthron
parents:
diff changeset
   213
		assertNotNull(projectSettings);
cawthron
parents:
diff changeset
   214
		IDialogSettings cSettings = projectSettings.getDialogSettings();
cawthron
parents:
diff changeset
   215
		IDialogSettings pageSettings = cSettings.getSection(CSPreferenceConstants.PROPERTY_SETTINGS_ID);
cawthron
parents:
diff changeset
   216
		if (pageSettings == null) {
cawthron
parents:
diff changeset
   217
			pageSettings = cSettings.addNewSection(CSPreferenceConstants.PROPERTY_SETTINGS_ID);
cawthron
parents:
diff changeset
   218
			pageSettings.put(CSPreferenceConstants.PROJ_SETTINGS, true);
cawthron
parents:
diff changeset
   219
			CSGeneralTabPage.initializePropertyValues(pageSettings);
cawthron
parents:
diff changeset
   220
			CSFileFiltersTabPage.initializePropertyValues(pageSettings);
cawthron
parents:
diff changeset
   221
			CSRulesTabPage.initializePropertyValues(pageSettings);
cawthron
parents:
diff changeset
   222
		}
cawthron
parents:
diff changeset
   223
		else {
cawthron
parents:
diff changeset
   224
			pageSettings.put(CSPreferenceConstants.PROJ_SETTINGS, true);
cawthron
parents:
diff changeset
   225
		}
cawthron
parents:
diff changeset
   226
		return pageSettings;
cawthron
parents:
diff changeset
   227
	}
cawthron
parents:
diff changeset
   228
cawthron
parents:
diff changeset
   229
}