Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6f9c5087473241bad3a644815b234bd10b86770e (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
/*******************************************************************************
 * Copyright (c) 2009 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 - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.tests.engine;

import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.preferences.IPreferencesService;
import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper;
import org.eclipse.equinox.internal.p2.engine.ProfilePreferences;
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;
import org.eclipse.equinox.p2.tests.TestActivator;
import org.osgi.service.prefs.BackingStoreException;
import org.osgi.service.prefs.Preferences;

public class ProfilePreferencesTest extends AbstractProvisioningTest {
	private IPreferencesService prefServ;

	protected void setUp() throws Exception {
		super.setUp();

		prefServ = (IPreferencesService) ServiceHelper.getService(TestActivator.context, IPreferencesService.class.getName());
	}

	/**
	 * Tests that a node corresponding to a non-existent profile cannot be persisted.
	 */
	public void testInvalidProfile() {
		try {
			//reading and storing for a non-existent profile shouldn't cause any errors
			Preferences node = prefServ.getRootNode().node("/profile/NonExistantProfile/testing");
			node.sync();
		} catch (BackingStoreException e) {
			fail("1.0", e);
		}
	}

	public void testProfilePreference() {
		Preferences pref = null;
		String key = "Test";
		String value = "Value";

		try {
			pref = prefServ.getRootNode().node("/profile/_SELF_/testing");
		} catch (IllegalArgumentException e) {
			fail("IllegalArgumentException when accessing preferences for self profile");
		}

		pref.put(key, value);
		assertTrue("Unable to retrieve value from preferences", value.equals(pref.get(key, null)));

		try {
			pref.flush();
		} catch (BackingStoreException e) {
			fail("Unable to write to preferences: " + e.getMessage());
		}
		waitForSave();

		try {
			pref.parent().removeNode();
		} catch (BackingStoreException e) {
			//
		}
		waitForSave();
		pref = prefServ.getRootNode().node("/profile/_SELF_/testing");
		assertEquals("Value not present after load", value, pref.get(key, null));
	}

	/**
	 * Wait for preferences to be flushed to disk
	 */
	private void waitForSave() {
		try {
			Job.getJobManager().join(ProfilePreferences.PROFILE_SAVE_JOB_FAMILY, null);
		} catch (InterruptedException e) {
			fail("4.99", e);
		}
	}
}

Back to the top