Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5feba1ff775f8de1909b751f8a235a44241be03d (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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