Skip to main content
summaryrefslogtreecommitdiffstats
blob: 07697e21f4f94f0e9c54686924b1a8440895dd2e (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*******************************************************************************
 *  Copyright (c) 2004, 2018 IBM Corporation and others.
 *
 *  This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License 2.0
 *  which accompanies this distribution, and is available at
 *  https://www.eclipse.org/legal/epl-2.0/
 *
 *  SPDX-License-Identifier: EPL-2.0
 *
 *  Contributors:
 *     IBM - Initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.common.tests.adaptable;

import java.io.IOException;
import java.net.MalformedURLException;
import junit.framework.TestCase;

import org.eclipse.core.internal.runtime.AdapterManager;
import org.eclipse.core.runtime.*;
import org.eclipse.core.tests.harness.BundleTestingHelper;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.FrameworkUtil;

/**
 * Tests API on the IAdapterManager class.
 */
public class IAdapterManagerTest extends TestCase {
	//following classes are for testComputeClassOrder
	static interface C {
	}

	static interface D {
	}

	static interface M {
	}

	static interface N {
	}

	static interface O {
	}

	interface A extends M, N {
	}

	interface B extends O {
	}

	class Y implements C, D {
	}

	class X extends Y implements A, B {
	}

	private static final String NON_EXISTING = "com.does.not.Exist";
	private static final String TEST_ADAPTER = "org.eclipse.equinox.common.tests.adaptable.TestAdapter";
	private static final String TEST_ADAPTER_CL = "testAdapter.testUnknown";
	private IAdapterManager manager;

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

	public IAdapterManagerTest() {
		super("");
	}

	@Override
	protected void setUp() throws Exception {
		manager = AdapterManager.getDefault();
	}

	/**
	 * Tests API method IAdapterManager.hasAdapter.
	 */
	public void testHasAdapter() {
		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"));
	}

	/**
	 * Tests API method IAdapterManager.getAdapter.
	 */
	public void testGetAdapter() {
		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"));
	}

	public void testGetAdapterNullArgs() {
		TestAdaptable adaptable = new TestAdaptable();
		try {
			manager.getAdapter(adaptable, (Class<?>) null);
			fail("1.0");
		} catch (RuntimeException e) {
			//expected
		}
		try {
			manager.getAdapter(null, NON_EXISTING);
			fail("1.0");
		} catch (RuntimeException e) {
			//expected
		}

	}

	/**
	 * Tests API method IAdapterManager.loadAdapter.
	 */
	public void testLoadAdapter() {
		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"));
	}

	/**
	 * Test adapting to classes not reachable by the default bundle class loader
	 * (bug 200068).
	 * NOTE: This test uses .class file compiled with 1.4 JRE. As a result,
	 * the test can not be run on pre-1.4 JRE.
	 */
	public void testAdapterClassLoader() throws MalformedURLException, BundleException, IOException {
		TestAdaptable adaptable = new TestAdaptable();
		assertTrue(manager.hasAdapter(adaptable, TEST_ADAPTER_CL));
		assertNull(manager.loadAdapter(adaptable, TEST_ADAPTER_CL));
		Bundle bundle = null;
		try {
			BundleContext bundleContext = FrameworkUtil.getBundle(getClass()).getBundleContext();
			bundle = BundleTestingHelper.installBundle("0.1", bundleContext, "Plugin_Testing/adapters/testAdapter_1.0.0");
			BundleTestingHelper.refreshPackages(bundleContext, new Bundle[] {bundle});

			assertTrue(manager.hasAdapter(adaptable, TEST_ADAPTER_CL));
			Object result = manager.loadAdapter(adaptable, TEST_ADAPTER_CL);
			assertNotNull(result);
			assertTrue(TEST_ADAPTER_CL.equals(result.getClass().getName()));
		} finally {
			if (bundle != null) {
				bundle.uninstall();
			}
		}
	}

	/**
	 * Tests for {@link IAdapterManager#computeClassOrder(Class)}.
	 */
	public void testComputeClassOrder() {
		Class<?>[] expected = new Class[] { X.class, Y.class, Object.class, A.class, B.class, M.class, N.class, O.class,
				C.class, D.class };
		Class<?>[] actual = manager.computeClassOrder(X.class);
		assertEquals("1.0", expected.length, actual.length);
		for (int i = 0; i < actual.length; i++) {
			assertEquals("1.1." + i, expected[i], actual[i]);
		}
	}

	public void testFactoryViolatingContract() {
		class Private {
		}

		IAdapterFactory fac = new IAdapterFactory() {
			@SuppressWarnings("unchecked")
			@Override
			public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
				if (adapterType == Private.class) {
					// Cast below violates the contract of the factory
					return (T) Boolean.FALSE;
				}
				return null;
			}

			@Override
			public Class<?>[] getAdapterList() {
				return new Class[] { Private.class };
			}
		};
		try {
			manager.registerAdapters(fac, Private.class);
			try {
				manager.getAdapter(new Private(), Private.class);
				fail("Should throw AssertionFailedException!");
			} catch (AssertionFailedException e) {
				assertTrue(e.getMessage().contains(fac.getClass().getName()));
				assertTrue(e.getMessage().contains(Boolean.class.getName()));
				assertTrue(e.getMessage().contains(Private.class.getName()));
			}
		} finally {
			manager.unregisterAdapters(fac, Private.class);
		}
	}
}

Back to the top