Search in sources :

Example 1 with IAdapterFactory

use of org.eclipse.core.runtime.IAdapterFactory in project eclipse.platform.runtime by eclipse.

the class IAdapterManagerServiceTest method testHasAdapter.

/**
 * Tests API method IAdapterManager.hasAdapter.
 */
public void testHasAdapter() {
    IAdapterManager manager = getAdapterManager();
    TestAdaptable adaptable = new TestAdaptable();
    // request non-existing adaptable
    assertTrue("1.0", !manager.hasAdapter("", NON_EXISTING));
    // request adapter that is in XML but has no registered factory
    assertTrue("1.1", manager.hasAdapter(adaptable, TEST_ADAPTER));
    // request adapter that is not in XML
    assertTrue("1.2", !manager.hasAdapter(adaptable, "java.lang.String"));
    // register an adapter factory that maps adaptables to strings
    IAdapterFactory fac = new IAdapterFactory() {

        @Override
        public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
            if (adapterType == String.class) {
                return adapterType.cast(adaptableObject.toString());
            }
            return null;
        }

        @Override
        public Class<?>[] getAdapterList() {
            return new Class[] { String.class };
        }
    };
    manager.registerAdapters(fac, TestAdaptable.class);
    try {
        // request adapter for factory that we've just added
        assertTrue("1.3", manager.hasAdapter(adaptable, "java.lang.String"));
    } finally {
        manager.unregisterAdapters(fac, TestAdaptable.class);
    }
    // request adapter that was unloaded
    assertTrue("1.4", !manager.hasAdapter(adaptable, "java.lang.String"));
}
Also used : IAdapterFactory(org.eclipse.core.runtime.IAdapterFactory) IAdapterManager(org.eclipse.core.runtime.IAdapterManager)

Example 2 with IAdapterFactory

use of org.eclipse.core.runtime.IAdapterFactory in project eclipse.platform.runtime by eclipse.

the class IAdapterManagerServiceTest method testGetAdapter.

/**
 * Tests API method IAdapterManager.getAdapter.
 */
public void testGetAdapter() {
    IAdapterManager manager = getAdapterManager();
    TestAdaptable adaptable = new TestAdaptable();
    // request non-existing adaptable
    assertNull("1.0", manager.getAdapter("", NON_EXISTING));
    // request adapter that is in XML but has no registered factory
    Object result = manager.getAdapter(adaptable, TEST_ADAPTER);
    assertTrue("1.1", result instanceof TestAdapter);
    // request adapter that is not in XML
    assertNull("1.2", manager.getAdapter(adaptable, "java.lang.String"));
    // register an adapter factory that maps adaptables to strings
    IAdapterFactory fac = new IAdapterFactory() {

        @Override
        public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
            if (adapterType == String.class) {
                return adapterType.cast(adaptableObject.toString());
            }
            return null;
        }

        @Override
        public Class<?>[] getAdapterList() {
            return new Class[] { String.class };
        }
    };
    manager.registerAdapters(fac, TestAdaptable.class);
    try {
        // request adapter for factory that we've just added
        result = manager.getAdapter(adaptable, "java.lang.String");
        assertTrue("1.3", result instanceof String);
    } finally {
        manager.unregisterAdapters(fac, TestAdaptable.class);
    }
    // request adapter that was unloaded
    assertNull("1.4", manager.getAdapter(adaptable, "java.lang.String"));
}
Also used : IAdapterFactory(org.eclipse.core.runtime.IAdapterFactory) IAdapterManager(org.eclipse.core.runtime.IAdapterManager)

Example 3 with IAdapterFactory

use of org.eclipse.core.runtime.IAdapterFactory in project eclipse.platform.runtime by eclipse.

the class IAdapterManagerServiceTest method testLoadAdapter.

/**
 * Tests API method IAdapterManager.loadAdapter.
 */
public void testLoadAdapter() {
    IAdapterManager manager = getAdapterManager();
    TestAdaptable adaptable = new TestAdaptable();
    // request non-existing adaptable
    assertNull("1.0", manager.loadAdapter("", NON_EXISTING));
    // request adapter that is in XML but has no registered factory
    Object result = manager.loadAdapter(adaptable, TEST_ADAPTER);
    assertTrue("1.1", result instanceof TestAdapter);
    // request adapter that is not in XML
    assertNull("1.2", manager.loadAdapter(adaptable, "java.lang.String"));
    // register an adapter factory that maps adaptables to strings
    IAdapterFactory fac = new IAdapterFactory() {

        @Override
        public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
            if (adapterType == String.class) {
                return adapterType.cast(adaptableObject.toString());
            }
            return null;
        }

        @Override
        public Class<?>[] getAdapterList() {
            return new Class[] { String.class };
        }
    };
    manager.registerAdapters(fac, TestAdaptable.class);
    try {
        // request adapter for factory that we've just added
        result = manager.loadAdapter(adaptable, "java.lang.String");
        assertTrue("1.3", result instanceof String);
    } finally {
        manager.unregisterAdapters(fac, TestAdaptable.class);
    }
    // request adapter that was unloaded
    assertNull("1.4", manager.loadAdapter(adaptable, "java.lang.String"));
}
Also used : IAdapterFactory(org.eclipse.core.runtime.IAdapterFactory) IAdapterManager(org.eclipse.core.runtime.IAdapterManager)

Aggregations

IAdapterFactory (org.eclipse.core.runtime.IAdapterFactory)3 IAdapterManager (org.eclipse.core.runtime.IAdapterManager)3