Search in sources :

Example 1 with PreferenceForwarder

use of org.eclipse.core.internal.preferences.legacy.PreferenceForwarder in project eclipse.platform.runtime by eclipse.

the class PreferenceForwarderTest method testListenerOnRemove.

/* Comment this test out until we are able to use session tests
	 * with it. - ddw
	 * 	public void testPluginPrefs() {
	 IPluginRegistry registry = InternalPlatform.getPluginRegistry();
	 IPluginDescriptor resPlugin = registry.getPluginDescriptor("org.eclipse.core.resources");
	 Preferences perfs = null;
	 try {
	 perfs = resPlugin.getPlugin().getPluginPreferences();
	 } catch (CoreException ce) {
	 fail("0.1 core exception from getPlugin");
	 }
	 boolean oneBoolean = perfs.getBoolean("OneBoolean");
	 double oneDouble = perfs.getDouble("OneDouble");
	 float oneFloat = perfs.getFloat("OneFloat");
	 int oneInt = perfs.getInt("OneInt");
	 long oneLong = perfs.getLong("OneLong");
	 String oneString = perfs.getString("OneString");
	 assertTrue("1.0 boolean", oneBoolean);
	 assertTrue("2.0 double", oneDouble == 4);
	 assertTrue("3.0 float", oneFloat ==	4.3f);
	 assertTrue("4.0 int", oneInt == 5);
	 assertTrue("5.0 long", oneLong == 6);
	 assertTrue("6.0 string", oneString.equals("A string from the plugin root directory"));
	 int a = 4;
	 int b = 3;
	 }
	 */
/*
	 * Regression test for bug 178815.
	 */
public void testListenerOnRemove() {
    // create a new log listener that will fail if anything is written
    ILogListener logListener = new ILogListener() {

        @Override
        public void logging(IStatus status, String plugin) {
            CoreException ex = new CoreException(status);
            fail("0.99", ex);
        }
    };
    // set a preference value to get everything initialized
    String id = getUniqueString();
    Preferences ps = new PreferenceForwarder(id);
    ps.setValue("key", "value");
    // add a property change listener which will cause one to be
    // added at the preference node level
    IPropertyChangeListener listener = new Preferences.IPropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent event) {
        }
    };
    ps.addPropertyChangeListener(listener);
    ps.setValue("key2", "value2");
    IEclipsePreferences node = InstanceScope.INSTANCE.getNode(id);
    // add our log listener and remove the node. nothing should be logged.
    RuntimeLog.addLogListener(logListener);
    try {
        node.removeNode();
    } catch (BackingStoreException e) {
        fail("4.99", e);
    } catch (IllegalStateException e) {
        fail("5.00", e);
    } finally {
        RuntimeLog.removeLogListener(logListener);
    }
}
Also used : IPropertyChangeListener(org.eclipse.core.runtime.Preferences.IPropertyChangeListener) PropertyChangeEvent(org.eclipse.core.runtime.Preferences.PropertyChangeEvent) IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) BackingStoreException(org.osgi.service.prefs.BackingStoreException) IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) PreferenceForwarder(org.eclipse.core.internal.preferences.legacy.PreferenceForwarder)

Example 2 with PreferenceForwarder

use of org.eclipse.core.internal.preferences.legacy.PreferenceForwarder in project eclipse.platform.runtime by eclipse.

the class PreferenceForwarderTest method test55138.

public void test55138() {
    final Preferences ps = new PreferenceForwarder(getUniqueString());
    final Tracer tracer1 = new Tracer();
    String key = "foo";
    // register one listener
    ps.addPropertyChangeListener(tracer1);
    assertEquals("1.0", "", tracer1.log.toString());
    // boolean value
    boolean booleanDefault = true;
    boolean booleanValue = false;
    ps.setDefault(key, booleanDefault);
    assertEquals("2.0", "", tracer1.log.toString());
    tracer1.log.setLength(0);
    ps.setValue(key, booleanValue);
    assertEquals("2.1", "[foo:Btrue->Bfalse]", tracer1.log.toString());
    ps.setValue(key, booleanDefault);
    assertEquals("2.2", "[foo:Btrue->Bfalse][foo:Bfalse->Btrue]", tracer1.log.toString());
    // int value
    int intDefault = 10;
    int intValue = 11;
    tracer1.log.setLength(0);
    ps.setDefault(key, intDefault);
    assertEquals("3.0", "", tracer1.log.toString());
    ps.setValue(key, intValue);
    assertEquals("3.1", "[foo:I10->I11]", tracer1.log.toString());
    ps.setValue(key, intDefault);
    assertEquals("3.2", "[foo:I10->I11][foo:I11->I10]", tracer1.log.toString());
    // double value
    double doubleDefault = 10.0;
    double doubleValue = 11.0;
    tracer1.log.setLength(0);
    ps.setDefault(key, doubleDefault);
    assertEquals("4.0", "", tracer1.log.toString());
    tracer1.log.setLength(0);
    ps.setValue(key, doubleValue);
    assertEquals("4.1", "[foo:D10.0->D11.0]", tracer1.log.toString());
    ps.setValue(key, doubleDefault);
    assertEquals("4.2", "[foo:D10.0->D11.0][foo:D11.0->D10.0]", tracer1.log.toString());
    // float value
    float floatDefault = 10.0f;
    float floatValue = 11.0f;
    tracer1.log.setLength(0);
    ps.setDefault(key, floatDefault);
    assertEquals("5.0", "", tracer1.log.toString());
    tracer1.log.setLength(0);
    ps.setValue(key, floatValue);
    assertEquals("5.1", "[foo:F10.0->F11.0]", tracer1.log.toString());
    ps.setValue(key, floatDefault);
    assertEquals("5.2", "[foo:F10.0->F11.0][foo:F11.0->F10.0]", tracer1.log.toString());
    // long value
    long longDefault = 10L;
    long longValue = 11L;
    tracer1.log.setLength(0);
    ps.setDefault(key, longDefault);
    assertEquals("6.0", "", tracer1.log.toString());
    tracer1.log.setLength(0);
    ps.setValue(key, longValue);
    assertEquals("6.1", "[foo:L10->L11]", tracer1.log.toString());
    ps.setValue(key, longDefault);
    assertEquals("6.2", "[foo:L10->L11][foo:L11->L10]", tracer1.log.toString());
}
Also used : IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) PreferenceForwarder(org.eclipse.core.internal.preferences.legacy.PreferenceForwarder)

Example 3 with PreferenceForwarder

use of org.eclipse.core.internal.preferences.legacy.PreferenceForwarder in project eclipse.platform.runtime by eclipse.

the class PreferenceForwarderTest method testBoolean.

public void testBoolean() {
    Preferences ps = new PreferenceForwarder(getUniqueString());
    final String k1 = "key1";
    assertEquals("1.0", false, Preferences.BOOLEAN_DEFAULT_DEFAULT);
    assertEquals("1.1", Preferences.BOOLEAN_DEFAULT_DEFAULT, ps.getBoolean(k1));
    ps.setValue(k1, true);
    assertEquals("1.2", true, ps.getBoolean(k1));
    ps.setValue(k1, false);
    assertEquals("1.3", false, ps.getBoolean(k1));
    ps.setDefault(k1, true);
    assertEquals("1.4", true, ps.getDefaultBoolean(k1));
    ps.setDefault(k1, false);
    assertEquals("1.5", false, ps.getDefaultBoolean(k1));
}
Also used : IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) PreferenceForwarder(org.eclipse.core.internal.preferences.legacy.PreferenceForwarder)

Example 4 with PreferenceForwarder

use of org.eclipse.core.internal.preferences.legacy.PreferenceForwarder in project eclipse.platform.runtime by eclipse.

the class PreferenceForwarderTest method testListeners.

public void testListeners() {
    final Preferences ps = new PreferenceForwarder(getUniqueString());
    final Tracer tracer1 = new Tracer();
    final Tracer tracer2 = new Tracer();
    // register one listener
    ps.addPropertyChangeListener(tracer1);
    assertEquals("1.0", "", tracer1.log.toString());
    // make sure it is notified in a type appropriate manner
    ps.setValue("a", "1");
    assertEquals("1.0.1", "[a:S->S1]", tracer1.log.toString());
    ps.setToDefault("a");
    tracer1.log.setLength(0);
    ps.setValue("a", true);
    assertEquals("1.0.2", "[a:Bfalse->Btrue]", tracer1.log.toString());
    ps.setToDefault("a");
    tracer1.log.setLength(0);
    ps.setValue("a", 100);
    assertEquals("1.0.3", "[a:I0->I100]", tracer1.log.toString());
    ps.setToDefault("a");
    tracer1.log.setLength(0);
    ps.setValue("a", 100L);
    assertEquals("1.0.4", "[a:L0->L100]", tracer1.log.toString());
    ps.setToDefault("a");
    tracer1.log.setLength(0);
    ps.setValue("a", 2.0f);
    assertEquals("1.0.5", "[a:F0.0->F2.0]", tracer1.log.toString());
    ps.setToDefault("a");
    tracer1.log.setLength(0);
    ps.setValue("a", 2.0);
    assertEquals("1.0.6", "[a:D0.0->D2.0]", tracer1.log.toString());
    // make sure it is notified of a series of events
    ps.setToDefault("a");
    tracer1.log.setLength(0);
    ps.setValue("a", "1");
    assertEquals("1.1", "[a:S->S1]", tracer1.log.toString());
    ps.setValue("a", "2");
    assertEquals("1.2", "[a:S->S1][a:S1->S2]", tracer1.log.toString());
    ps.setValue("a", ps.getDefaultString("a"));
    assertEquals("1.2.1", "[a:S->S1][a:S1->S2][a:S2->S]", tracer1.log.toString());
    ps.setValue("a", "3");
    assertEquals("1.2.2", "[a:S->S1][a:S1->S2][a:S2->S][a:S->S3]", tracer1.log.toString());
    ps.setToDefault("a");
    assertEquals("1.2.3", "[a:S->S1][a:S1->S2][a:S2->S][a:S->S3][a:S3->S]", tracer1.log.toString());
    // change to same value - no one notified
    ps.setValue("a", "2");
    tracer1.log.setLength(0);
    assertEquals("1.3", "", tracer1.log.toString());
    // register second listener
    ps.addPropertyChangeListener(tracer2);
    // make sure both are notified
    ps.setValue("a", "3");
    assertEquals("1.4", "[a:S2->S3]", tracer1.log.toString());
    assertEquals("1.5", "[a:S2->S3]", tracer2.log.toString());
    // deregister is honored
    ps.removePropertyChangeListener(tracer2);
    tracer1.log.setLength(0);
    tracer2.log.setLength(0);
    ps.setValue("a", "1");
    assertEquals("1.6", "[a:S3->S1]", tracer1.log.toString());
    assertEquals("1.7", "", tracer2.log.toString());
    // duplicate deregister is ignored
    ps.removePropertyChangeListener(tracer2);
    tracer1.log.setLength(0);
    tracer2.log.setLength(0);
    ps.setValue("a", "2");
    assertEquals("1.8", "[a:S1->S2]", tracer1.log.toString());
    assertEquals("1.9", "", tracer2.log.toString());
    // duplicate register is ignored
    ps.addPropertyChangeListener(tracer1);
    tracer1.log.setLength(0);
    ps.setValue("a", "1");
    assertEquals("1.10", "[a:S2->S1]", tracer1.log.toString());
    // last deregister is honored
    ps.removePropertyChangeListener(tracer1);
    tracer1.log.setLength(0);
    ps.setValue("a", "4");
    assertEquals("1.11", "", tracer1.log.toString());
    // adds 2 and removes 1 during during callback!
    class Trouble implements Preferences.IPropertyChangeListener {

        @Override
        public void propertyChange(Preferences.PropertyChangeEvent event) {
            ps.removePropertyChangeListener(tracer1);
            ps.addPropertyChangeListener(tracer2);
        }
    }
    ps.setValue("a", "0");
    ps.addPropertyChangeListener(tracer1);
    ps.addPropertyChangeListener(new Trouble());
    tracer1.log.setLength(0);
    tracer2.log.setLength(0);
    ps.setValue("a", "1");
    ps.setValue("a", "2");
    assertEquals("1.12", "[a:S0->S1]", tracer1.log.toString());
    assertEquals("1.13", "[a:S1->S2]", tracer2.log.toString());
}
Also used : IPropertyChangeListener(org.eclipse.core.runtime.Preferences.IPropertyChangeListener) PropertyChangeEvent(org.eclipse.core.runtime.Preferences.PropertyChangeEvent) IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) PreferenceForwarder(org.eclipse.core.internal.preferences.legacy.PreferenceForwarder)

Example 5 with PreferenceForwarder

use of org.eclipse.core.internal.preferences.legacy.PreferenceForwarder in project eclipse.platform.runtime by eclipse.

the class PreferenceForwarderTest method testFloat.

public void testFloat() {
    Preferences ps = new PreferenceForwarder(getUniqueString());
    final String k1 = "key1";
    final float[] values = { 0.0f, 1002.5f, -201788.55f, Float.MAX_VALUE, Float.MIN_VALUE };
    final float tol = 1.0e-20f;
    assertEquals("1.0", 0.0f, Preferences.FLOAT_DEFAULT_DEFAULT, tol);
    assertEquals("1.1", Preferences.FLOAT_DEFAULT_DEFAULT, ps.getFloat(k1), tol);
    for (float v1 : values) {
        float v2 = v1 + 1.0f;
        ps.setValue(k1, v1);
        assertEquals("1.2", v1, ps.getFloat(k1), tol);
        ps.setDefault(k1, v2);
        assertEquals("1.3", v2, ps.getDefaultFloat(k1), tol);
    }
    try {
        ps.setValue(k1, Float.NaN);
        assertTrue("1.4", false);
    } catch (IllegalArgumentException e) {
    // NaNs should be rejected
    }
}
Also used : IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) PreferenceForwarder(org.eclipse.core.internal.preferences.legacy.PreferenceForwarder)

Aggregations

PreferenceForwarder (org.eclipse.core.internal.preferences.legacy.PreferenceForwarder)15 IEclipsePreferences (org.eclipse.core.runtime.preferences.IEclipsePreferences)15 IPropertyChangeListener (org.eclipse.core.runtime.Preferences.IPropertyChangeListener)2 PropertyChangeEvent (org.eclipse.core.runtime.Preferences.PropertyChangeEvent)2 BackingStoreException (org.osgi.service.prefs.BackingStoreException)1