Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b7e75ffa6c0786aea1d38d91d619451a261fbc94 (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
/*******************************************************************************
 * Copyright (c) 2011 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - ongoing maintenance
 *******************************************************************************/
package org.eclipse.team.tests.ccvs.core.jsch;

import java.io.File;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jsch.internal.core.IConstants;
import org.eclipse.jsch.internal.core.JSchCorePlugin;
import org.eclipse.jsch.internal.core.PreferenceInitializer;
import org.eclipse.team.tests.ccvs.core.EclipseTest;

public class PreferenceInitializerTest extends EclipseTest {

	private boolean PREF_HAS_CHANGED_DEFAULT_WIN32_SSH_HOME_value = false;
	private boolean PREF_HAS_MIGRATED_SSH2_PREFS_value = false;
	private String KEY_SSH2HOME_value = null;
	private String KEY_PRIVATEKEY_value = null;

	protected void setUp() throws Exception {
		if (!Platform.getOS().equals(Platform.OS_WIN32))
			return;

		super.setUp();
		// remembering preferences changed in test
		IEclipsePreferences preferences = InstanceScope.INSTANCE
				.getNode(JSchCorePlugin.ID);
		PREF_HAS_CHANGED_DEFAULT_WIN32_SSH_HOME_value = preferences.getBoolean(
				IConstants.PREF_HAS_CHANGED_DEFAULT_WIN32_SSH_HOME, false);
		PREF_HAS_MIGRATED_SSH2_PREFS_value = preferences.getBoolean(
				IConstants.PREF_HAS_MIGRATED_SSH2_PREFS, false);
		KEY_SSH2HOME_value = preferences.get(IConstants.KEY_SSH2HOME, null);
		KEY_PRIVATEKEY_value = preferences.get(IConstants.KEY_PRIVATEKEY, null);
	}

	protected void tearDown() throws Exception {
		if (!Platform.getOS().equals(Platform.OS_WIN32))
			return;

		super.tearDown();
		// restoring preferences changed in test
		IEclipsePreferences preferences = InstanceScope.INSTANCE
				.getNode(JSchCorePlugin.ID);
		preferences.putBoolean(
				IConstants.PREF_HAS_CHANGED_DEFAULT_WIN32_SSH_HOME,
				PREF_HAS_CHANGED_DEFAULT_WIN32_SSH_HOME_value);
		preferences.putBoolean(IConstants.PREF_HAS_MIGRATED_SSH2_PREFS,
				PREF_HAS_MIGRATED_SSH2_PREFS_value);
		if (KEY_SSH2HOME_value != null)
			preferences.put(IConstants.KEY_SSH2HOME, KEY_SSH2HOME_value);
		else
			preferences.remove(IConstants.KEY_SSH2HOME);
		if (KEY_PRIVATEKEY_value != null)
			preferences.put(IConstants.KEY_PRIVATEKEY, KEY_PRIVATEKEY_value);
		else
			preferences.remove(IConstants.KEY_PRIVATEKEY);
	}

	public void testChangeDefaultWin32SshHomeOldWorkspace() {
		if (!Platform.getOS().equals(Platform.OS_WIN32))
			return;

		if (PreferenceInitializer.SSH_OLD_WIN32_HOME_DEFAULT != null) {
			File file = new File(
					PreferenceInitializer.SSH_OLD_WIN32_HOME_DEFAULT);
			if (!file.exists()) {
				file.mkdir();
			}
		}

		IEclipsePreferences preferences = InstanceScope.INSTANCE
				.getNode(JSchCorePlugin.ID);
		preferences.remove(IConstants.PREF_HAS_CHANGED_DEFAULT_WIN32_SSH_HOME);
		// this value indicates that this is an old workspace
		preferences.putBoolean(IConstants.PREF_HAS_MIGRATED_SSH2_PREFS, true);
		preferences.remove(IConstants.KEY_SSH2HOME);

		// verify that the preference is not set
		assertFalse(preferences.getBoolean(
				IConstants.PREF_HAS_CHANGED_DEFAULT_WIN32_SSH_HOME, false));
		// verify if this is an old workspace
		assertTrue(preferences.getBoolean(
				IConstants.PREF_HAS_MIGRATED_SSH2_PREFS, false));

		PreferenceInitializer preferenceInitializer = new PreferenceInitializer();
		preferenceInitializer.initializeDefaultPreferences();

		// verify that the preference is set now
		assertTrue(preferences.getBoolean(
				IConstants.PREF_HAS_CHANGED_DEFAULT_WIN32_SSH_HOME, false));
		assertEquals(PreferenceInitializer.SSH_OLD_WIN32_HOME_DEFAULT,
				preferences.get(IConstants.KEY_SSH2HOME, null));
	}

	public void _testDontChangeDefaultWin32SshHomeNewWorkspace() {
		if (!Platform.getOS().equals(Platform.OS_WIN32))
			return;

		if (PreferenceInitializer.SSH_OLD_WIN32_HOME_DEFAULT != null) {
			File file = new File(
					PreferenceInitializer.SSH_OLD_WIN32_HOME_DEFAULT);
			if (!file.exists()) {
				file.mkdir();
			}
		}

		IEclipsePreferences preferences = InstanceScope.INSTANCE
				.getNode(JSchCorePlugin.ID);
		preferences.remove(IConstants.PREF_HAS_CHANGED_DEFAULT_WIN32_SSH_HOME);
		// this value indicates that this is a new workspace
		preferences.remove(IConstants.PREF_HAS_MIGRATED_SSH2_PREFS);
		preferences.remove(IConstants.KEY_SSH2HOME);

		// verify that the preference is not set
		assertFalse(preferences.getBoolean(
				IConstants.PREF_HAS_CHANGED_DEFAULT_WIN32_SSH_HOME, false));
		// verify if this is a new workspace
		assertFalse(preferences.getBoolean(
				IConstants.PREF_HAS_MIGRATED_SSH2_PREFS, false));

		PreferenceInitializer preferenceInitializer = new PreferenceInitializer();
		preferenceInitializer.initializeDefaultPreferences();

		// verify that the preference is set now
		assertTrue(preferences.getBoolean(
				IConstants.PREF_HAS_CHANGED_DEFAULT_WIN32_SSH_HOME, false));
		assertNull(preferences.get(IConstants.KEY_SSH2HOME, null));
	}

	public static Test suite() {
		return new TestSuite(PreferenceInitializerTest.class);
	}
}

Back to the top