Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Equinox » ExtensionRegistryTestSupport
ExtensionRegistryTestSupport [message #504074] Wed, 16 December 2009 22:52 Go to next message
Ralf Ebert is currently offline Ralf Ebert
Messages: 72
Registered: July 2009
Member
This is a multi-part message in MIME format.
--------------000707050408040109060800
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

I just tried to write a unit test that checks its class under test for correct
evaluation of extensions. Trying that, I found it a bit cumbersome to create an
IExtensionRegistry instance that is populated from plain text files outside of
an OSGi environment. It's possible, but IMHO it is not as easy as it should be.

Also, I couldn't find any unit tests for the extension registry itself that
could be used as a starting point - did I miss something here?

After finding out how this works, I wrote an ExtensionRegistryTestSupport class
that helps with setting up an IExtensionRegistry in unit tests. For example:

============================================================ ====
ExtensionRegistryTestSupport ext = new ExtensionRegistryTestSupport();

// create plugin.xml defining extension points on-the-fly
// as extension points need to defined for extensions to be visible
ext.contributeExtensionPoints("com.example",
new String[] { "test1", "test2", "test3" });

// contribute using streams or strings on behalf of a bundleId
ext.contribute("com.example.a", getClass().getResourceAsStream("plugin-a.xml"));
ext.contribute("com.example.b", getClass().getResourceAsStream("plugin-b.xml"));

extensionRegistry = ext.getExtensionRegistry();
============================================================ ====

I attached my implementation of the class. This needs to be polished and
important things might be missing. I'd appreciate your feedback:

- do you see value in such a test support class?
- did I miss something in my implementation?
- if you see value in it, where could such a class be provided so that Eclipse
project unit tests could use it? (I wrote this as part of a unit test for a
contribution to org.eclipse.ui.workbench I'm working on)

Greetings,

Ralf


--
http://www.ralfebert.de/blog/eclipsercp/




--------------000707050408040109060800
Content-Type: text/x-java;
name="ExtensionRegistryTestSupport.java"
Content-Transfer-Encoding: base64
Content-Disposition: inline;
filename="ExtensionRegistryTestSupport.java"

cGFja2FnZSBleHRlbnNpb25tb2NrczsKCmltcG9ydCBqYXZhLmlvLkJ5dGVB cnJheUlucHV0
U3RyZWFtOwppbXBvcnQgamF2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qb2ludE5hbWVzW2ldOwoJCQlz dHIuYXBwZW5k
KFN0cmluZy5mb3JtYXQoIlx0PGV4dGVuc2lvbi1wb2ludCBpZD1cIiVzXCIg bmFtZT1cIiVz
XCIvPlxuIiwgbmV3IE9iamVjdFtdIHsgZXh0UG9pbnROYW1lLAoJCQkJCWV4 dFBvaW50TmFt
ZSB9KSk7CgkJfQoJCXN0ci5hcHBlbmQoIjwvcGx1Z2luPlxuIik7CgkJY29u dHJpYnV0ZShi
dW5kbGVJZCwgc3RyLnRvU3RyaW5nKCkpOwoJfQoKfQ==
--------------000707050408040109060800--
Re: ExtensionRegistryTestSupport [message #504184 is a reply to message #504074] Thu, 17 December 2009 15:34 Go to previous messageGo to next message
Ralf Ebert is currently offline Ralf Ebert
Messages: 72
Registered: July 2009
Member
If you want to give it a try, an enhanced version of the
ExtensionRegistryTestSupport class including tests is available here:
http://github.com/ralfebert/extensiontestsupport/blob/master /extensiontestsupport/src/extensiontestsupport/ExtensionRegi stryTestSupport.java

--
http://www.ralfebert.de/blog/eclipsercp/
Re: ExtensionRegistryTestSupport [message #504237 is a reply to message #504074] Thu, 17 December 2009 14:50 Go to previous messageGo to next message
Oleg Besedin is currently offline Oleg Besedin
Messages: 41
Registered: July 2009
Member
Hi Ralf,
The extension registry JUnits are in the
:pserver:anonymous@dev.eclipse.org:/cvsroot/eclipse
CVS repository under
org.eclipse.core.tests.runtime
(see, for example, packages org.eclipse.core.tests.internal.registry and
org.eclipse.core.tests.internal.registry.simple).

(Historically, extension registry was a part of the Platorm/Runtime and when
the registry moved to Equinox the tests stayed in the old location.)

There are several types of tests: the tests that check simple add/remove
functionality; tests that work based on the bundle contributions, and
session tests. If you end up with a different (or more straightforward) test
harness for the extension registry JUnits, you are very welcome to
contribute it. The best way to do that would be to open a Bugzilla
enhancement for Equinox and attach patches to it.

Sincerely,
Oleg Besedin

"Ralf Ebert" <info@ralfebert.de> wrote in message
news:hgc9a3$fg1$1@build.eclipse.org...
>I just tried to write a unit test that checks its class under test for
>correct
> evaluation of extensions. Trying that, I found it a bit cumbersome to
> create an
> IExtensionRegistry instance that is populated from plain text files
> outside of
> an OSGi environment. It's possible, but IMHO it is not as easy as it
> should be.
>
> Also, I couldn't find any unit tests for the extension registry itself
> that
> could be used as a starting point - did I miss something here?
>
> After finding out how this works, I wrote an ExtensionRegistryTestSupport
> class
> that helps with setting up an IExtensionRegistry in unit tests. For
> example:
>
> ============================================================ ====
> ExtensionRegistryTestSupport ext = new ExtensionRegistryTestSupport();
>
> // create plugin.xml defining extension points on-the-fly
> // as extension points need to defined for extensions to be visible
> ext.contributeExtensionPoints("com.example",
> new String[] { "test1", "test2", "test3" });
>
> // contribute using streams or strings on behalf of a bundleId
> ext.contribute("com.example.a",
> getClass().getResourceAsStream("plugin-a.xml"));
> ext.contribute("com.example.b",
> getClass().getResourceAsStream("plugin-b.xml"));
>
> extensionRegistry = ext.getExtensionRegistry();
> ============================================================ ====
>
> I attached my implementation of the class. This needs to be polished and
> important things might be missing. I'd appreciate your feedback:
>
> - do you see value in such a test support class?
> - did I miss something in my implementation?
> - if you see value in it, where could such a class be provided so that
> Eclipse
> project unit tests could use it? (I wrote this as part of a unit test for
> a
> contribution to org.eclipse.ui.workbench I'm working on)
>
> Greetings,
>
> Ralf
>
>
> --
> http://www.ralfebert.de/blog/eclipsercp/
>
>
>
>


------------------------------------------------------------ --------------------


> package extensionmocks;
>
> import java.io.ByteArrayInputStream;
> import java.io.IOException;
> import java.io.InputStream;
>
> import org.eclipse.core.runtime.Assert;
> import org.eclipse.core.runtime.IExtensionRegistry;
> import org.eclipse.core.runtime.RegistryFactory;
> import org.eclipse.core.runtime.spi.RegistryContributor;
>
> /**
> * ExtensionRegistryTestSupport simplifies the creation of an
> * {@link IExtensionRegistry} instance for unit testing purposes.
> * ExtensionRegistryTestSupport creates and manages an extension registry
> * instance and allows to contribute contents on the fly using its
> contribute
> * methods.
> */
> public class ExtensionRegistryTestSupport {
>
> private final IExtensionRegistry extensionRegistry =
> RegistryFactory.createRegistry(null, this, this);
>
> /**
> * Returns the extensionRegistry instance managed by this
> * ExtensionRegistryTestSupport object.
> */
> public IExtensionRegistry getExtensionRegistry() {
> return extensionRegistry;
> }
>
> /**
> * Contributes the contents of the given pluginXmlStream on behalf of
> * bundleId. The given pluginXmlStream is closed after contributing.
> */
> public void contribute(String bundleId, InputStream pluginXmlStream) {
> Assert.isNotNull(bundleId, "bundleId");
> Assert.isNotNull(pluginXmlStream, "pluginXmlStream");
> try {
> RegistryContributor contributor = new RegistryContributor(bundleId,
> bundleId, null, null);
> extensionRegistry.addContribution(pluginXmlStream, contributor, false,
> null, null, this);
> } catch (Exception e) {
> throw new RuntimeException(e);
> } finally {
> try {
> pluginXmlStream.close();
> } catch (IOException e) {
> throw new RuntimeException(e);
> }
> }
> }
>
> /**
> * Contributes the contents of the given pluginXmlString on behalf of
> * bundleId.
> */
> public void contribute(String bundleId, String pluginXmlString) {
> contribute(bundleId, new
> ByteArrayInputStream(pluginXmlString.getBytes()));
> }
>
> /**
> * Creates a plugin.xml that defines the given extension points on the fly
> * and contributes it on behalf of bundleId.
> */
> public void contributeExtensionPoints(String bundleId, String[]
> extensionPointNames) {
> StringBuilder str = new StringBuilder();
> str.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
> str.append("<?eclipse version=\"3.0\"?>\n");
> str.append("<plugin>\n");
> for (int i = 0; i < extensionPointNames.length; i++) {
> String extPointName = extensionPointNames[i];
> str.append(String.format("\t<extension-point id=\"%s\" name=\"%s\"/>\n",
> new Object[] { extPointName,
> extPointName }));
> }
> str.append("</plugin>\n");
> contribute(bundleId, str.toString());
> }
>
> }
Re: ExtensionRegistryTestSupport [message #504660 is a reply to message #504237] Mon, 21 December 2009 23:00 Go to previous message
Ralf Ebert is currently offline Ralf Ebert
Messages: 72
Registered: July 2009
Member
Hi Oleg,

thanks for your answer!

> The extension registry JUnits are in the
> :pserver:anonymous@dev.eclipse.org:/cvsroot/eclipse
> CVS repository under
> org.eclipse.core.tests.runtime
> (see, for example, packages org.eclipse.core.tests.internal.registry and
> org.eclipse.core.tests.internal.registry.simple).
>
> (Historically, extension registry was a part of the Platorm/Runtime and when
> the registry moved to Equinox the tests stayed in the old location.)

is this intentional or was there just no time to move them yet? I find tests
really hard to spot if they are not co-located with the code under test.

> There are several types of tests: the tests that check simple add/remove
> functionality; tests that work based on the bundle contributions, and
> session tests. If you end up with a different (or more straightforward) test
> harness for the extension registry JUnits, you are very welcome to
> contribute it. The best way to do that would be to open a Bugzilla
> enhancement for Equinox and attach patches to it.

The proposed class is not of great value for the test (harness) for the
extension registry itself. It is intended to be used in tests where you have
something under test that is driven by the ExtensionRegistry. It just makes it
easy to create and populate an ExtensionRegistry instance using a simple API.

I provided the bundle on GitHub: http://github.com/ralfebert/extensiontestsupport

If somebody should find this helpful generally, just drop me a note, then I will
contribute it via Bugzilla. It's licensed under EPL already.

Greetings,

Ralf



--
http://www.ralfebert.de/blog/eclipsercp/
Previous Topic:p2 install UI wants to reinstall core platform?
Next Topic:- P2 - No action found for: org.eclipse.equinox.p2.touchpoint.eclipse.addRepository.
Goto Forum:
  


Current Time: Tue Oct 21 12:16:41 GMT 2014

Powered by FUDForum. Page generated in 0.03355 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software