[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [equinox-dev] Testing OSGI Bundles with JUnit

oh.. yes.. you don't use maven, so you don't have transient dependencies in.
Well, yes, then the initial quote of the required artifacts you need
at compiletime are a bit more. We just listed the direct dependencies.

There are some more dependencies like ops4j-base (your current exception).

I will try to compile a flat list of dependencies with their
respective download urls.
Sorry for inconvinience.
For impatient: you *could* add a pom to your test project, specify the
pax exam dependencies as in the tutorial from the wiki and see what
you get when doing "mvn eclipse:eclipse"

DevCon: Yes, will be there, also with a short talk about Pax Exam.



On 5/19/09, David Conde <dconde@xxxxxxxx> wrote:
> Hi Toni,
>
> Thank you so much for your answer.
>
> If I did not misunderstand, there are a lot of things done by Pax Exam which
> are transparent to users, so just fixing a couple of configuration
> parameters I could get a context similar to the real one with Equinox.
>
>
> I am exactly not using PDE with Eclipse, what I am doing the most of times
> is to use Eclipse like a tool to create my bundles in a easier way and then
> I export them as a jar files, in order to install them by console in Equinox
> framework (using console, not Eclipse running environment). In this way I
> have to install all of bundles by myself, so, if I am using a bundle which
> use a service from OSGi, which is not included in EQuinox.jar I have to
> download it and install as well.
>
> I removed felix and knopflerfish from configuration part as you said, and I
> added :
>
>    provision(
>                     //
>             		bundle( "file:PlatformConfigurationImpl_1.0.0.jar"
> ),
>             		bundle( "file:PlatformConfiguration_1.0.0.jar" )
>
>         )
>
> These URLs are the names of my bundles, their symbolic names, and when I
> launch this class from console the result is the same, I do not get
> anything, however, if I launch from Eclipse click on "Run botton", two
> options appear: Run as JUnit Test and Run as Junit Plug-in Test. If I lauch
> the first one I get "initialization error" with the next exceptions:
>
>
>
> java.lang.NoClassDefFoundError: org/ops4j/lang/NullArgumentException
> 	at
> org.ops4j.pax.exam.junit.internal.DefaultConfigMethod.<init>(DefaultConfigMe
> thod.java:68)
> 	at
> org.ops4j.pax.exam.junit.AnnotatedWithConfiguration.getConfigMethods(Annotat
> edWithConfiguration.java:68)
> 	at
> org.ops4j.pax.exam.junit.JUnit4TestRunner.getConfigurationMethods(JUnit4Test
> Runner.java:137)
> 	at
> org.ops4j.pax.exam.junit.JUnit4TestRunner.getTestMethods(JUnit4TestRunner.ja
> va:92)
> 	at
> org.ops4j.pax.exam.junit.JUnit4TestRunner.<init>(JUnit4TestRunner.java:80)
> 	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native
> Method)
> 	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown
> Source)
> 	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown
> Source)
> 	at java.lang.reflect.Constructor.newInstance(Unknown Source)
> 	at
> org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.ja
> va:31)
> 	at
> org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder
> .java:24)
> 	at
> org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:
> 57)
> 	at
> org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(Al
> lDefaultPossibilitiesBuilder.java:29)
> 	at
> org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:
> 57)
> 	at
> org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
> 	at
> org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4Test
> Reference.java:26)
> 	at
> org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit
> 4TestClassReference.java:24)
> 	at
> org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4Tes
> tLoader.java:40)
> 	at
> org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4Test
> Loader.java:30)
> 	at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRu
> nner.java:445)
> 	at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRu
> nner.java:673)
> 	at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.
> java:386)
> 	at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner
> .java:196)
> Caused by: java.lang.ClassNotFoundException:
> org.ops4j.lang.NullArgumentException
> 	at java.net.URLClassLoader$1.run(Unknown Source)
> 	at java.security.AccessController.doPrivileged(Native Method)
> 	at java.net.URLClassLoader.findClass(Unknown Source)
> 	at java.lang.ClassLoader.loadClass(Unknown Source)
> 	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
> 	at java.lang.ClassLoader.loadClass(Unknown Source)
> 	at java.lang.ClassLoader.loadClassInternal(Unknown Source)
> 	... 23 more
>
>
>
>
> I do not really know what I am missing. In my Scenario, there are two
> bundles, one of them it is an interface, and the other one is the
> implementation of that interface.
> The only thing that my implementation do it is add new properties or remove
> them, using PreferenceService from OSGI.
> This implementation make use of PreferenceService from the framework, so it
> is like an internal bundle which I do not have Access to, I mean, it may be
> that I have to include this bundle in "provision part", but the problem is
> that I do not have this PreferenceService.jar.
>
>
> Launching as JUnit Plug-in Test (second option) I did not get any result.
>
> Do I need to include any other bundle in "provision part"?
>
>
> Any idea?
>
> Thank you in advance.
>
> I am writing all these steps so that I can write a tutorial about this work.
>
> By the way, yes I am from Spain, and I would like to go to OSGI DevCon in
> Zurich but I will be working in this date. Will you go there?
>
>
> Thank you again!
>
>
> David
> -----Mensaje original-----
> De: equinox-dev-bounces@xxxxxxxxxxx [mailto:equinox-dev-bounces@xxxxxxxxxxx]
> En nombre de Toni Menzel
> Enviado el: martes, 19 de mayo de 2009 13:18
> Para: Equinox development mailing list
> Asunto: Re: [equinox-dev] Testing OSGI Bundles with JUnit
>
> Hi David,
>
> let me see. First your configuration is _all_ what your osgi bundle
> context will look like. So, currently you specify three frameworks
> (felix,equinox,knopflerfish). This will start all three frameworks
> after each other and execute your test inside each of them.
> To get started and learn the stuff its probably easier to just specify
> one framework (equinox for this matter).
>
> Now, all resources you have in your test-classes output (most of it
> are usually your compiled testcases) are bundled up into a bundle with
> a fully managed manifest (you cannot control). This is what we call
> the "test probe" and exist just to have the test inside the osgi
> environment.
>
> Now, you launch the test just like an ordinary JUnit test (which it
> is, from eclipse point of view).
>
> This should trigger Pax Exam to download everything necessary (more on
> that below), launch a new vm per framework, start the framework,
> install you "test probe", and trigger the test from inside eclipse via
> an rmi remote hook.
>
> Now, in your case it will not find your Service (Test will fail)
> because you test setup is mostly empty. As i mentioned, the what you
> define inside the  @Configuration method (returning array of Option)
> defines your whole test setup: your target vm, your osgi framework +
> version, your bundles.
>
> For target vm you have a default (your JAVA_HOME vm), for framework
> you have specified felix,equiniox and knopflerfish (for test, just
> keep one for now).
> But, you did not mention your bundle.
> Now, there are many ways on how you can do this.
> Simpliest way is if you bundle is a maven artifact, then you can do
> (inside options(..)):
> mavenBundle().groupId("foo").artifactId("bar").version("1.0.0")
>
> If not, you can use basically any url:
> bundle(String URL)
>
> If you really want to keep using PDE, you can do this then:
> bundle("file:mybundle.jar")
> or so.
>
> Your configuration then looks like this:
> public static Option[] configuration()
>      {
>          return options(
>             equinox(),
>             provision(
>                bundle("yourbundleurl"),
>                bundle("probablyanotherurl"),
>            )
>         );
>      }
>
> To answer the last question about the activator for the test probe:
> no, there's no way. (by design if you want. Same for manifest entries.
> The Test Probe is not something you should rely on. You can assume
> that your injected bundlecontext is a valid BundleContext. And you can
> rely on that all your stuff from your test folder will be inside that
> test probe.
> Finally, the test probe has a (calculated by bytecode inspection)
> import-package part with all resolution=optional.
>
> Tell us about your experience.
>
> Sure, a detailed howto for eclipse/PDE users would be great. ;)
>
> I see you are from spain, do you intend to come to OSGi DevCon in
> Zurich , June, 22nd ?
>
> cheers,
> Toni
>
> #
> On 5/19/09, David Conde <dconde@xxxxxxxx> wrote:
>> Hi,
>>
>>
>>
>> Thanks Alin for your answer.
>>
>>
>>
>> I have tried what you told me in your email.
>>
>>
>>
>> I have downloaded
>>
>>
>>
>> *	 <http://repository.ops4j.org/maven2/org/ops4j/pax/exam/pax-exam>
>> pax-exam
>> *
>> <http://repository.ops4j.org/maven2/org/ops4j/pax/exam/pax-exam-junit>
>> pax-exam-junit
>> *
>>
> <http://repository.ops4j.org/maven2/org/ops4j/pax/exam/pax-exam-container-de
>> fault> pax-exam-container-default
>>
>>
>>
>> and I followed : http://wiki.ops4j.org//x/f4Cr
>> <http://wiki.ops4j.org/x/f4Cr> .
>>
>>
>>
>> These were my steps:
>>
>> 1.       I added the jars previously mentioned in ClassPath (Environment
>> Variables)
>>
>> 2.       I created a MyTestClass as shown below:
>>
>>
>>
>> import ….
>>
>>
>>
>> @RunWith( JUnit4TestRunner.class )
>>
>> public class MyUnitTest
>>
>> {
>>
>>
>>
>>     @Inject
>>
>>     BundleContext bundleContext;
>>
>>
>>
>>     @Configuration
>>
>>     public static Option[] configuration()
>>
>>     {
>>
>>         return options(
>>
>>             frameworks(
>>
>>                 felix(),
>>
>>                 equinox(),
>>
>>                 knopflerfish()
>>
>>             )
>>
>>
>>
>>             );
>>
>>     }
>>
>>
>>
>>     private ServiceTracker tracker;
>>
>>     @Test
>>
>>     public void testMethod()
>>
>>         throws Exception
>>
>>     {
>>
>>       tracker = new ServiceTracker(bundleContext,
>> MyServicetobeTEsted.class.getName(), null);
>>
>>             tracker.open();
>>
>>
>>
>>             MyServicetobeTEsted service = (MyServicetobeTEsted)
>> tracker.getService();
>>
>>
>>
>>             org.junit.Assert.assertTrue(service.addValue("Nivel", 5));
>>
>>
>>
>>
>>
>>     }
>>
>>
>>
>> }
>>
>>
>>
>> 3.       I have exported my TestProject in Eclipse as a jar or bundle(I
> did
>> this in the same way I exported my other Bundles using Eclipse, I mean, I
>> have exported my TestProject, which have a package where there is just one
>> class (MyUnitTest), and there is no Activator class at all.
>>
>> 4.       I have launched Equinox with MyServicetobeTEsted already
> installed
>>
>> 5.       I have installed my TestProject Bundle
>>
>> 6.       I have started my TestProject bundle but I did not get any result
>> at all. I do not get any exception neither.
>>
>>
>>
>>
>>
>>
>>
>> Am I missing anything?
>>
>>
>>
>> I have some questions about Pax Exam now:
>>
>>
>>
>> -Is it possible to get with Pax Exam the same Visual Interface than JUnit
>> uses?
>>
>> -Where do I get the result from testing?
>>
>> -Do I  have to create Activator class with start method to my TestClass?
>>
>>
>>
>> What does it mean that I have to built the TESTBUNDLE on the fly?
>> Thank you in advance
>>
>>
>>
>> David
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> De: equinox-dev-bounces@xxxxxxxxxxx
> [mailto:equinox-dev-bounces@xxxxxxxxxxx]
>> En nombre de Alin Dreghiciu
>> Enviado el: viernes, 15 de mayo de 2009 22:57
>> Para: Equinox development mailing list
>> Asunto: Re: [equinox-dev] Testing OSGI Bundles with JUnit
>>
>>
>>
>> I think an user guide for using Px Exam with Eclipse will be very useful.
> If
>> Toni does not do it before me, I will during this weekend.
>>
>> Till then the what you can do is to:
>>
>> 1. Add this jars to your classpath (take latest)
>>
>> *	 <http://repository.ops4j.org/maven2/org/ops4j/pax/exam/pax-exam>
>> pax-exam
>> *
>> <http://repository.ops4j.org/maven2/org/ops4j/pax/exam/pax-exam-junit>
>> pax-exam-junit
>> *
>>
> <http://repository.ops4j.org/maven2/org/ops4j/pax/exam/pax-exam-container-de
>> fault> pax-exam-container-default
>>
>> 2. Follow http://wiki.ops4j.org//x/f4Cr <http://wiki.ops4j.org/x/f4Cr> .
>>
>>
>>
>> On Fri, May 15, 2009 at 11:42 AM, David Conde <dconde@xxxxxxxx> wrote:
>>
>>
>> Hi Toni,
>>
>> Thank you for your idea!
>>
>> I have Reading about Pax Exam and  I am not clear about what exactly I
> have
>> to do for installing and using it in Equinox.
>>
>> I would like to test a simple Bundle which uses Preference Service from
>> Equinox. I do not use neither Maven nor other containers, just Equinox
> v34.
>>
>> As I have read in Pax Exam website, If I want to test my Bundle I should
>> create a Test Class similar to T1S2_HowToUseBundleContext example, I
>> thought, my Test Class should implements TestCase class but I do not see
> any
>> TestCase implementation in examples so I suppose that using "@RunWith(
>> JUnit4TestRunner.class )" it is enough. I asume either that I should also
>> include the next piece of code inside to fix Equinox as framework:
>>
>> @Configuration
>> public Option[] configure() {
>> return options(
>>            equinox()
>>        );
>> }
>>
>> Either I would have to fix @test in each method which I would like to
> test.
>>
>> Finally I suppose that I would have to add pax-exam-junit.jar from
>>
> (http://repository.ops4j.org/maven2/org/ops4j/pax/exam/pax-exam-junit/0.5.0/
>> ) and junit.jar from (JUnit website) to the classpath.
>>
>> So if I am not wrong, the steps to test a Bundle in Equinox would be like
> as
>> shown below:
>>
>> 1-Include pax-exam-junit.jar and junit.jar in ClassPath
>> 2-Create a new Bundle which will create a MyTestClass instance (with
>> “@RunWith( JUnit4TestRunner.class )" included.
>> 3-Call to Preference Service from BundleContext object as the same way as
> I
>> did using the real Context in real bundles.
>> 4- Launch Equinox with all the Bundles
>>
>> And now, my questions are? HOw BundleContext can call to
>> bundleContext.getBundles method if it was defined as null in
>> T1S2_HowToUseBundleContext example?
>>
>> Could I call for example
>> bundleContext.getService(“PreferenceServiceReference”)?
>>
>>
>> I either would like to ask about the results, how do I get test results?
> Is
>> it possible to use junit.swingui.TestRunner in order to get results as a
>> visual interface?
>>
>>
>> Thank you in advance
>>
>> David
>>
>> -----Mensaje original-----
>> De: equinox-dev-bounces@xxxxxxxxxxx
> [mailto:equinox-dev-bounces@xxxxxxxxxxx]
>> En nombre de Toni Menzel
>> Enviado el: jueves, 14 de mayo de 2009 12:06
>> Para: Equinox development mailing list
>> Asunto: Re: [equinox-dev] Testing OSGI Bundles with JUnit
>>
>>
>> Have you had a look at pax exam
>> (http://wiki.ops4j.org/display/paxexam/Pax+Exam) ?
>>
>> It actually lets you write Junit Tests in OSGi Framework independent way.
>> You finally wrap your test in a test probe bundle (built on the fly),
>> start your target framework (equinox in this case), provision it with
>> you setup (extra bundles), and thats about it.
>>
>> IF you use PDE (because you pointed to the fact you are using eclipse
>> a couple of times):
>> Without external means like maven you probably end up re-specifiying
>> all your dependencies again just for test setup.
>>
>> Tell us if you do or not as it might help us a lot to get you started
>> with pax exam.
>>
>> Finally, in exam you end up inside an isolated vm process running just
>> your test vm with your test framework
>> (Equinox,Felix,Knopflerfish,Concierge, all recent versions at you
>> fingertipps) exactly as specified. From there you (currently) get the
>> (real!) bundleContext from where you probably get any service you
>> want.
>>
>> Toni
>>
>>
>>
>>
>> On 5/14/09, David Conde <dconde@xxxxxxxx> wrote:
>>> Hi everyone,
>>>
>>>
>>>
>>> I am trying to do test cases for OSGI Bundles in Equinox, using Eclipse.
> I
>>> found the problem when I tried to model BundleContext and services like
>>> Preference Services, which are complex objects. I read about how I could
>>> solve this problem and I found some libraries from Spring Framework which
>>> use “mocks” to test BundleContext, but I am not sure if these ones
>> provided
>>> special Services likes Preference services. So, my question is , is there
>>> any library with mocks to simulate BundleContext and other complex
> Equinox
>>> classes? Is there any library provided by Equinox?
>>>
>>>
>>>
>>> Thank you in advance
>>>
>>>
>>>
>>>
>>>
>>> --
>>>
>>> David Conde Baena
>>>
>>>
>>> CITIC
>>> Centro Andaluz de Innovación y Tecnologías de la Información y las
>>> Comunicaciones
>>> Edificio CITIC, C/ Marie Curie, 6
>>> Parque Tecnológico de Andalucía
>>> 29590 - Campanillas (MÁLAGA)
>>>
>>> Tfno.: +34 952028610
>>> Fax: +34 951231029
>>>  <mailto:usuario@xxxxxxxx> Email: dconde@xxxxxxxx
>>>  <http://www.citic.es/> Web: www.citic.es
>>>
>>>
>>>
>>>
>>>
>>>
>>
>>
>> --
>> Toni Menzel
>> Independent Software Developer - Looking for new projects!
>> Professional Profile: http://www.osgify.com
>> Blog: tonitcom.blogspot.com
>> toni@xxxxxxxxxxxxxxx
>> http://www.ops4j.org     - New Energy for OSS Communities - Open
>> Participation Software.
>> _______________________________________________
>> equinox-dev mailing list
>> equinox-dev@xxxxxxxxxxx
>> https://dev.eclipse.org/mailman/listinfo/equinox-dev
>>
>> _______________________________________________
>> equinox-dev mailing list
>> equinox-dev@xxxxxxxxxxx
>> https://dev.eclipse.org/mailman/listinfo/equinox-dev
>>
>>
>>
>>
>> --
>> Alin Dreghiciu
>> Software Developer - Looking for new projects!
>> My profile: http://www.linkedin.com/in/alindreghiciu
>> My blog: http://adreghiciu.blogspot.com
>> http://www.ops4j.org - New Energy for OSS Communities - Open Participation
>> Software.
>> http://www.qi4j.org - New Energy for Java - Domain Driven Development.
>>
>>
>
>
> --
> Toni Menzel
> Independent Software Developer - Looking for new projects!
> Professional Profile: http://www.osgify.com
> Blog: tonitcom.blogspot.com
> toni@xxxxxxxxxxxxxxx
> http://www.ops4j.org     - New Energy for OSS Communities - Open
> Participation Software.
> _______________________________________________
> equinox-dev mailing list
> equinox-dev@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/equinox-dev
>
> _______________________________________________
> equinox-dev mailing list
> equinox-dev@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/equinox-dev
>


-- 
Toni Menzel
Independent Software Developer - Looking for new projects!
Professional Profile: http://www.osgify.com
Blog: tonitcom.blogspot.com
toni@xxxxxxxxxxxxxxx
http://www.ops4j.org     - New Energy for OSS Communities - Open
Participation Software.