Skip to main content
summaryrefslogtreecommitdiffstats
blob: bca284a8fdf57a8c2fab93120d5ad5f9aed31c2d (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
/*******************************************************************************
 * Copyright (c) 2007, 2008 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.utility.tests.internal.model.value.prefs;

import java.util.EventObject;
import java.util.List;
import java.util.prefs.AbstractPreferences;
import java.util.prefs.Preferences;

import org.eclipse.jpt.utility.internal.ClassTools;
import org.eclipse.jpt.utility.tests.internal.TestTools;

import junit.framework.TestCase;

/**
 * set up and tear down a test node for any subclass that
 * needs to test preferences-related stuff
 */
public abstract class PreferencesTestCase extends TestCase {
	protected Preferences classNode;
	public Preferences testNode;
	protected static final String TEST_NODE_NAME = "test node";

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

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		Preferences packageNode = Preferences.userNodeForPackage(this.getClass());
		this.classNode = packageNode.node(ClassTools.shortClassNameForObject(this));
		// clean out any leftover crap...
		if ((this.classNode.keys().length > 0) || (this.classNode.childrenNames().length > 0)) {
			this.classNode.removeNode();
			// ...and re-create the node
			this.classNode = packageNode.node(ClassTools.shortClassNameForObject(this));
		}
		this.testNode = this.classNode.node(TEST_NODE_NAME);
	}

	@Override
	protected void tearDown() throws Exception {
		// wait for all the events to be delivered before tearing down
		this.waitForEventQueueToClear();
		Preferences node = this.classNode.parent();
		this.classNode.removeNode();
		while (this.nodeIsVestigial(node)) {
			Preferences parent = node.parent();
			node.removeNode();
			node = parent;
		}
		TestTools.clear(this);
		super.tearDown();
	}

	private boolean nodeIsVestigial(Preferences node) throws Exception {
		return (node != null)
			&& (node.keys().length == 0)
			&& (node.childrenNames().length == 0)
			&& (node != Preferences.userRoot());
	}

	protected void waitForEventQueueToClear() {
		try {
			while ( ! this.preferencesEventQueue().isEmpty()) {
				Thread.sleep(100);
			}
			Thread.sleep(100);
		} catch (InterruptedException ex) {
			throw new RuntimeException(ex);
		}
	}

	@SuppressWarnings("unchecked")
	private List<EventObject> preferencesEventQueue() {
		return (List<EventObject>) ClassTools.staticFieldValue(AbstractPreferences.class, "eventQueue");
	}

}

Back to the top