Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian W. Damus2013-09-19 16:00:39 -0400
committerChristian W. Damus2013-09-19 16:33:50 -0400
commit933a8c321c50bcfe5ce62c15f2bd81bc97424356 (patch)
tree73fb50b9aae2b99249dbc657ab8753566e7ef899
parent6b2f4d8e7a4a5cfcd8f8a3ab2afa66255c9b9ad9 (diff)
downloadorg.eclipse.papyrus-933a8c321c50bcfe5ce62c15f2bd81bc97424356.tar.gz
org.eclipse.papyrus-933a8c321c50bcfe5ce62c15f2bd81bc97424356.tar.xz
org.eclipse.papyrus-933a8c321c50bcfe5ce62c15f2bd81bc97424356.zip
415373: Fix NPE with null default adapter
https://bugs.eclipse.org/bugs/show_bug.cgi?id=415373
-rw-r--r--plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/utils/AdapterUtils.java11
-rw-r--r--tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/tests/AllTests.java6
-rw-r--r--tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/utils/AdapterUtilsTest.java169
3 files changed, 184 insertions, 2 deletions
diff --git a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/utils/AdapterUtils.java b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/utils/AdapterUtils.java
index debb6ada47b..0a82472edd0 100644
--- a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/utils/AdapterUtils.java
+++ b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/utils/AdapterUtils.java
@@ -101,7 +101,16 @@ public class AdapterUtils {
* @return the best-effort adapter of the given {@code type}, else the {@code defaultAdapter}
*/
public static <T> T adapt(Object object, Class<T> type, T defaultAdapter) {
- return (object == null) ? defaultAdapter : adapt(object, type).or(defaultAdapter);
+ T result = defaultAdapter;
+
+ if (object != null) {
+ Optional<T> adapter = adapt(object, type);
+ if (adapter.isPresent()) {
+ result = adapter.get();
+ }
+ }
+
+ return result;
}
diff --git a/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/tests/AllTests.java b/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/tests/AllTests.java
index f58eb017bb5..9670eb13bbc 100644
--- a/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/tests/AllTests.java
+++ b/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/tests/AllTests.java
@@ -1,5 +1,5 @@
/*****************************************************************************
- * Copyright (c) 2010 CEA LIST.
+ * Copyright (c) 2010, 2013 CEA LIST and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
@@ -8,6 +8,7 @@
*
* Contributors:
* Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
+ * Christian W. Damus (CEA LIST) - add test for AdapterUtils
*****************************************************************************/
package org.eclipse.papyrus.infra.core.tests;
@@ -17,6 +18,7 @@ import org.eclipse.papyrus.infra.core.resource.AbstractModelWithSharedResourceTe
import org.eclipse.papyrus.infra.core.resource.ModelSetTest;
import org.eclipse.papyrus.infra.core.services.ComposedServiceTest;
import org.eclipse.papyrus.infra.core.services.ServicesRegistryTest;
+import org.eclipse.papyrus.infra.core.utils.AdapterUtilsTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@@ -32,6 +34,8 @@ ComposedServiceTest.class, ServicesRegistryTest.class,
// {oep}.core.lifecycleevents
/* SaveAndDirtyServiceTest.class, */LifeCycleEventsProviderTest.class,
NestedEditorDelegatedOutlinePageTest.class,
+//{oep}.core.utils
+AdapterUtilsTest.class
})
/**
* Suite Class for all tests in the plugin
diff --git a/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/utils/AdapterUtilsTest.java b/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/utils/AdapterUtilsTest.java
new file mode 100644
index 00000000000..5feba1ff775
--- /dev/null
+++ b/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/utils/AdapterUtilsTest.java
@@ -0,0 +1,169 @@
+/*****************************************************************************
+ * Copyright (c) 2013 CEA LIST 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:
+ * CEA LIST - Initial API and implementation
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.core.utils;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.junit.Assert.*;
+
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.emf.common.notify.Notifier;
+import org.eclipse.emf.common.notify.impl.AdapterImpl;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EcoreFactory;
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.junit.Test;
+
+import com.google.common.base.Optional;
+
+
+/**
+ * Tests for the {@link AdapterUtils} class.
+ */
+public class AdapterUtilsTest {
+
+ @Test
+ public void testAdaptInstanceOptionalPresent() {
+ String foo = "foo";
+
+ assertThat(AdapterUtils.adapt(foo, String.class), isPresent(String.class));
+ }
+
+ @Test
+ public void testAdaptAdaptableOptionalPresent() {
+ String foo = "foo";
+
+ assertThat(AdapterUtils.adapt(new AdaptableWrapper(foo), String.class), isPresent(String.class));
+ }
+
+ @Test
+ public void testAdaptEMFOptionalPresent() {
+ EObject obj = EcoreFactory.eINSTANCE.createEObject();
+ new EMFAdapter(obj);
+
+ assertThat(AdapterUtils.adapt(obj, EMFAdapter.class), isPresent(EMFAdapter.class));
+ }
+
+ @Test
+ public void testAdaptInstanceOptionalNull() {
+ String foo = "foo";
+
+ assertThat(AdapterUtils.adapt(foo, Integer.class), isNull(Integer.class));
+ }
+
+ @Test
+ public void testAdaptAdaptableOptionalNull() {
+ String foo = "foo";
+
+ assertThat(AdapterUtils.adapt(new AdaptableWrapper(foo), Integer.class), isNull(Integer.class));
+ }
+
+ @Test
+ public void testAdaptEMFOptionalNull() {
+ EObject obj = EcoreFactory.eINSTANCE.createEObject();
+
+ assertThat(AdapterUtils.adapt(obj, EMFAdapter.class), isNull(EMFAdapter.class));
+ }
+
+ @Test
+ public void testAdaptInstanceDefaultPresent() {
+ String foo = "foo";
+
+ assertThat(AdapterUtils.adapt(foo, String.class, null), is(foo));
+ }
+
+ @Test
+ public void testAdaptAdaptableDefaultPresent() {
+ String foo = "foo";
+
+ assertThat(AdapterUtils.adapt(new AdaptableWrapper(foo), String.class, null), is(foo));
+ }
+
+ @Test
+ public void testAdaptEMFDefaultPresent() {
+ EObject obj = EcoreFactory.eINSTANCE.createEObject();
+ EMFAdapter emf = new EMFAdapter(obj);
+
+ assertThat(AdapterUtils.adapt(obj, EMFAdapter.class, null), is(emf));
+ }
+
+ @Test
+ public void testAdaptAdaptableDefaultDefault() {
+ String foo = "foo";
+
+ assertThat(AdapterUtils.adapt(new AdaptableWrapper(foo), Integer.class, 42), is(42));
+ }
+
+ @Test
+ public void testAdaptAdaptableDefaultNull() {
+ String foo = "foo";
+
+ assertThat(AdapterUtils.adapt(new AdaptableWrapper(foo), Integer.class, null), nullValue());
+ }
+
+ //
+ // Test framework
+ //
+
+ static <T> Matcher<Optional<T>> isPresent(Class<T> type) {
+ return new BaseMatcher<Optional<T>>() {
+
+ public void describeTo(Description description) {
+ description.appendText("optional value is present");
+ }
+
+ public boolean matches(Object item) {
+ return (item instanceof Optional<?>) && ((Optional<?>)item).isPresent();
+ }
+ };
+ }
+
+ static <T> Matcher<Optional<T>> isNull(Class<T> type) {
+ return not(isPresent(type));
+ }
+
+ public static class AdaptableWrapper implements IAdaptable {
+
+ private final Class<?> type;
+
+ private final Object instance;
+
+ public <T> AdaptableWrapper(T instance, Class<? super T> type) {
+ this.type = type;
+ this.instance = instance;
+ }
+
+ public AdaptableWrapper(Object instance) {
+ this.type = instance.getClass();
+ this.instance = instance;
+ }
+
+ public Object getAdapter(@SuppressWarnings("rawtypes") Class adapter) {
+ return (adapter == type) ? instance : null;
+ }
+ }
+
+ public static class EMFAdapter extends AdapterImpl {
+
+ public EMFAdapter(Notifier obj) {
+ obj.eAdapters().add(this);
+ }
+
+ @Override
+ public boolean isAdapterForType(Object type) {
+ return type == EMFAdapter.class;
+ }
+ }
+}

Back to the top