| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
| <html> |
| <!-- |
| Licensed to the Apache Software Foundation (ASF) under one or more |
| contributor license agreements. See the NOTICE file distributed with |
| this work for additional information regarding copyright ownership. |
| The ASF licenses this file to You under the Apache License, Version 2.0 |
| (the "License"); you may not use this file except in compliance with |
| the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE- 2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| --> |
| |
| <head> |
| <title>Apache Felix - apache-felix-ipojo-junit4osgi-methods</title> |
| <link rel="icon" href="/res/favicon.ico"> |
| |
| <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| <meta name="description" content="The most powerful component model for OSGi"> |
| |
| |
| <link href="/ipojo/web/bootstrap/css/bootstrap-cerulean.css" rel="stylesheet"> |
| <link href="/ipojo/web/bootstrap/css/bootstrap-responsive.css" rel="stylesheet"> |
| <link href="/ipojo/web/bootstrap/css/font-awesome.min.css" rel="stylesheet"> |
| <link href="/ipojo/web/style.css" rel="stylesheet"> |
| |
| <!-- Overide alert's colors --> |
| <link href="/ipojo/web/bootstrap/css/alert.css" rel="stylesheet"> |
| |
| <link rel="stylesheet" href="/ipojo/web/github.css" type="text/css" media="all"> |
| |
| |
| <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> |
| <script src="/ipojo/web/bootstrap/js/bootstrap.min.js"></script> |
| |
| </head> |
| |
| <body data-spy="scroll" data-target=".subnav"> |
| <div class="navbar navbar-fixed-top navbar-inverse"> |
| <div class="navbar-inner"> |
| <div class="container"> |
| <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"> |
| <span class="icon-bar"></span> |
| <span class="icon-bar"></span> |
| <span class="icon-bar"></span> |
| </a> |
| <a class="brand" href="/documentation/subprojects/apache-felix-ipojo.html">Apache Felix iPOJO</a> |
| |
| <div class="nav-collapse" id="main-menu"> |
| <ul class="nav" id="main-menu-left"> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/ipojo-news.html">News</a></li> |
| <li><a href="http://felix.apache.org/downloads.cgi">Downloads</a></li> |
| |
| <li class="dropdown"> |
| <a class="dropdown-toggle" data-toggle="dropdown" href="#">Tutorials <b class="caret"></b></a> |
| <ul class="dropdown-menu" id="tutorials-menu"> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-why-choose-ipojo.html">Why choose iPOJO</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-successstories.html">Success stories</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-feature-overview.html">Features</a></li> |
| <li class="divider"></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-gettingstarted/ipojo-in-10-minutes.html">iPOJO in 10 minutes</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-gettingstarted/how-to-use-ipojo-annotations.html">Using Annotations</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-gettingstarted/ipojo-hello-word-maven-based-tutorial.html">Maven tutorial</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-gettingstarted/ipojo-advanced-tutorial.html">Advanced tutorial</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-gettingstarted/apache-felix-ipojo-dosgi.html">Using Distributed OSGi</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-gettingstarted/ipojo-composition-tutorial.html">Application Composition</a></li> |
| </ul> |
| </li> |
| |
| <li class="dropdown"> |
| <a class="dropdown-toggle" data-toggle="dropdown" href="#">Documentation <b class="caret"></b></a> |
| <ul class="dropdown-menu" id="user-guide-menu"> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/service-requirement-handler.html">Requiring a service</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/providing-osgi-services.html">Providing a service</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/lifecycle-callback-handler.html">Lifecycle management</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/configuration-handler.html">Configuration</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/architecture-handler.html">Introspection</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/controller-lifecycle-handler.html">Impacting the lifecycle</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/injecting-bundle-context.html">Accessing the Bundle Context</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/apache-felix-ipojo-instances.html">Creating instances</a></li> |
| <li class="divider"></li> |
| <li class="dropdown-submenu"> |
| <a tabindex="-1" href="#">External <em>handlers</em></a> |
| <ul class="dropdown-menu"> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/event-admin-handlers.html">Asynchronous communication</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/ipojo-jmx-handler.html">JMX management</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/extender-pattern-handler.html">Extender pattern</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/white-board-pattern-handler.html">Whiteboard pattern</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/temporal-service-dependency.html">Temporal dependencies</a></li> |
| </ul> |
| </li> |
| <li class="dropdown-submenu"> |
| <a tabindex="-1" href="#">Configuration Admin & Factories</a> |
| <ul class="dropdown-menu"> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/combining-ipojo-and-configuration-admin.html">iPOJO and config admin</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/ipojo-factory-service.html">Using the iPOJO Factory service</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/how-to-use-ipojo-factories.html">Factories and Instances</a></li> |
| </ul> |
| </li> |
| <li class="divider"></li> |
| <li class="dropdown-submenu"> |
| <a tabindex="-1" href="#">Advanced topics</a> |
| <ul class="dropdown-menu"> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/instance-vs-service-controller.html">Instance vs. Service Controllers</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/service-binding-interceptors.html">Service Binding Interceptors</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/using-xml-schemas.html">XML Schemas</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/apache-felix-ipojo-api.html">Using the iPOJO API</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/constructing-pojo-objects-with-factory-methods.html">Constructing service objects with factory methods</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/using-ipojo-introspection-api.html">Using the introspection API</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-testing-components.html">Testing components</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/using-stereotypes.html">Using @Stereotypes</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-eclipse-integration.html">Eclipse Integration</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/ipojo-extender-configuration.html">Configuring iPOJO's Extender</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-faq.html">FAQ</a></li> |
| <li class="divider"></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-devguide/how-to-write-your-own-handler.html">Handler development</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-devguide/how-to-use-ipojo-manipulation-metadata.html">Manipulation Metadata </a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-devguide/dive-into-the-ipojo-manipulation-depths.html">Dive into the iPOJO Manipulation depths</a></li> |
| <li><a href="http://felix.apache.org/ipojo/api/1.12.1">Javadoc</a></li> |
| </ul> |
| </li> |
| </ul> |
| </li> |
| |
| <li class="dropdown" id="tools-menu"> |
| <a class="dropdown-toggle" data-toggle="dropdown" href="#">Tools <b class="caret"></b></a> |
| <ul class="dropdown-menu" id="swatch-menu"> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-tools/ipojo-ant-task.html">Ant Task</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-tools/ipojo-maven-plug-in.html">Maven Plugin</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-tools/ipojo-arch-command.html">Architecture commands</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-tools/apache-felix-ipojo-online-manipulator.html">Online Manipulator</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-tools/ipojo-webconsole-plugin.html">Webconsole plugin</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-tools/ipojo-karaf-feature.html">Apache Karaf Features</a></li> |
| </ul> |
| </li> |
| |
| <li class="dropdown" id="community-menu"> |
| <a class="dropdown-toggle" data-toggle="dropdown" href="#">Community <b class="caret"></b></a> |
| <ul class="dropdown-menu" id="swatch-menu"> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/ipojo-support.html">Support</a></li> |
| <li><a href="http://www.apache.org/">ASF</a></li> |
| <li><a href="http://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li> |
| <li><a href="http://www.apache.org/foundation/thanks.html">Sponsors</a></li> |
| </ul> |
| </li> |
| |
| <li class="dropdown" id="misc-menu"> |
| <a class="dropdown-toggle" data-toggle="dropdown" href="#">Misc <b class="caret"></b></a> |
| <ul class="dropdown-menu" id="swatch-menu"> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-supportedvms.html">Supported JVMs</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-supportedosgi.html">Supported OSGi Implementations</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/articles-and-presentations.html">Article & Presentations</a></li> |
| <li><a href="/documentation/subprojects/apache-felix-ipojo/developing-camel-mediators-with-ipojo.html">Developping Camel mediators with iPOJO</a></li> |
| </ul> |
| </li> |
| </ul> |
| <ul class="nav pull-right" id="main-menu-right"> |
| <li><a rel="tooltip" target="_blank" href="http://felix.apache.org">Apache Felix <i class="icon-share-alt"></i></a></li> |
| </ul> |
| </div> |
| </div> |
| </div> |
| </div> |
| |
| <div class="container"> |
| <div class="content"> |
| <style type="text/css"> |
| /* The following code is added by mdx_elementid.py |
| It was originally lifted from http://subversion.apache.org/style/site.css */ |
| /* |
| * Hide class="elementid-permalink", except when an enclosing heading |
| * has the :hover property. |
| */ |
| .headerlink, .elementid-permalink { |
| visibility: hidden; |
| } |
| h2:hover > .headerlink, h3:hover > .headerlink, h1:hover > .headerlink, h6:hover > .headerlink, h4:hover > .headerlink, h5:hover > .headerlink, dt:hover > .elementid-permalink { visibility: visible }</style> |
| <h1 id="osgi-and-friends-methods">OSGi and friends methods<a class="headerlink" href="#osgi-and-friends-methods" title="Permanent link">¶</a></h1> |
| <p><em>Developing OSGi tests can be definitely boring. First, testing is generally not a very exciting experience, but imagine if you have to handle all the OSGi issues in your tests... Don't worry junit4osgi provides methods allowing to interact easily with OSGi!</em></p> |
| <p div="div">{div:class=toc} |
| [TOC]</p> |
| <h2 id="osgi-methods">OSGi methods<a class="headerlink" href="#osgi-methods" title="Permanent link">¶</a></h2> |
| <p>junit4osgi test case extends the <code>OSGiTestCase</code> class. This class provides useful methods allowing to find services, get them, get the <code>PackageAdmin</code> service... The description of these methods can be found afterward.</p> |
| <p>The most part of the methods are available statically and non-statically. Static methods require a <code>Bundle</code>. Non static methods use the current bundle context, and track get services to release them when the test is done. So, we advise you to use the non-static methods. Static methods just allow you to check that a specific bundle can access to services / resources.</p> |
| <h3 id="service-interaction">Service interaction<a class="headerlink" href="#service-interaction" title="Permanent link">¶</a></h3> |
| <ul> |
| <li><code>boolean isServiceAvailable(String svc)</code> : returns <code>true</code> if the service <code>svc</code> is available.</li> |
| <li> |
| <p><code>boolean isServiceAvailableByPID(String itf, String pid)</code> : return <code>true</code> if the service <code>svc</code> is available and exposed with <code>pid</code> as <code>service.pid</code>.</p> |
| </li> |
| <li> |
| <p><code>ServiceReference getServiceReference(String itf)</code>: returns a service reference exposing the <code>itf</code> service specification, or <code>null</code> if not available.</p> |
| </li> |
| <li><code>ServiceReference getServiceReference(String itf, String filter)</code>: returns a service reference matching with the \<service interface, filter> request, or <code>null</code> is not available.</li> |
| <li><code>ServiceReference getServiceReferenceByPID(String itf, String pid)</code>: returns a service reference exposing the service <code>itf</code> and exposed with a <code>service.pid</code> equals to <code>pid</code>, or <code>null</code> if not available.</li> |
| <li> |
| <p><code>ServiceReference[]() getServiceReferences(String itf, String filter)</code>: returns all the service references matching with the \<service interface, filter> request, or an empty array is not available.</p> |
| </li> |
| <li> |
| <p><code>Object getServiceObject(String itf, String filter)</code>: returns a service object matching with the \<service interface, filter> request or <code>null</code> is not available.</p> |
| </li> |
| <li><code>Object getServiceObject(ServiceReference ref)</code>: returns the service object associated with the given <code>service reference</code> or <code>null</code> if not available.</li> |
| <li> |
| <p><code>Object[]() getServiceObjects(String itf, String filter)</code>: gets all the service objects matching with the \<service interface, filter> request or an empty array is no providers are available.</p> |
| </li> |
| <li> |
| <p><code>void waitForService(String itf, String filter, long timeout)</code>: waits for a service arrival matching with the \<service interface, filter> request. If the timeout expires, this method fails. </p> |
| </li> |
| </ul> |
| <h3 id="get-the-bundle-context">Get the bundle context<a class="headerlink" href="#get-the-bundle-context" title="Permanent link">¶</a></h3> |
| <ul> |
| <li><code>BundleContext getContext()</code> : gives access to the OSGiTestCase bundle context.</li> |
| </ul> |
| <h3 id="installstartuninstall-bundles">Install/Start/Uninstall bundles<a class="headerlink" href="#installstartuninstall-bundles" title="Permanent link">¶</a></h3> |
| <ul> |
| <li><code>Bundle installBundle(String url)</code>: installs a bundle from the given url. This method fails if the bundle cannot be installed.</li> |
| <li><code>Bundle installBundle(String url, InputStream stream)</code>: installs a bundle from the given input stream. This methods fails if the bundle cannot be installed.</li> |
| <li><code>Bundle installAndStart(String url)</code>: installs a bundle from the given url and starts it. This methods fails if the bundle cannot be installed and started correctly.</li> |
| <li><code>Bundle installAndStart(String url, InputStream stream)</code>: installs a bundle from the given input stream and starts it. This methods fails if the bundle cannot be installed and started correctly.</li> |
| <li><code>Bundle getBundle(long bundleId)</code>: gets an installed bundle by its bundle id, or <code>null</code> if not found.</li> |
| <li><code>Bundle getBundle(String name)</code>: gets an installed bundle by its symbolic name. Fails if not found.</li> |
| </ul> |
| <h3 id="packageadmin">PackageAdmin<a class="headerlink" href="#packageadmin" title="Permanent link">¶</a></h3> |
| <ul> |
| <li><code>PackageAdmin getPackageAdmin()</code>: gives access to the <code>Package Admin</code> service exposed by the framework.</li> |
| <li><code>refresh()</code>: refresh package wires.</li> |
| </ul> |
| <h2 id="extensibility-helper-objects">Extensibility: Helper objects<a class="headerlink" href="#extensibility-helper-objects" title="Permanent link">¶</a></h2> |
| <p>junit4osgi provides an extensibility mechanism to reduce the pain of testing. So, if you're interacting with specific services or environment, you can use <code>Helper</code> objects. Those object have to be created in the <code>setUp</code> method and <code>disposed</code> in the <code>tearDown</code> method. </p> |
| <p>So, for example, if you write iPOJO tests, you can use the iPOJO helper providing a lot of utility functions simplifying the development of tests. |
| {code:java} |
| public class MyTest extends OSGiTestCase { |
| ComponentInstance fooProvider1, fooProvider2;</p> |
| <div class="codehilite"><pre><span class="n">IPOJOHelper</span> <span class="n">helper</span><span class="p">;</span> <span class="o">//</span> <span class="n">Helper</span> <span class="n">object</span> |
| |
| <span class="n">public</span> <span class="n">void</span> <span class="n">setUp</span><span class="p">()</span> <span class="p">{</span> |
| <span class="n">helper</span> <span class="p">=</span> <span class="n">new</span> <span class="n">IPOJOHelper</span><span class="p">(</span><span class="n">this</span><span class="p">);</span> <span class="o">//</span> <span class="n">Create</span> <span class="n">the</span> <span class="n">object</span><span class="p">.</span> |
| <span class="n">String</span> <span class="n">type2</span> <span class="p">=</span> "<span class="n">PS</span><span class="o">-</span><span class="n">FooProviderType</span>"<span class="p">;</span> |
| |
| <span class="n">fooProvider1</span> <span class="p">=</span> <span class="n">helper</span><span class="p">.</span><span class="n">createComponentInstance</span><span class="p">(</span><span class="n">type2</span><span class="p">,</span> <span class="n">p3</span><span class="p">);</span> |
| <span class="n">fooProvider2</span> <span class="p">=</span> <span class="n">helper</span><span class="p">.</span><span class="n">createComponentInstance</span><span class="p">(</span><span class="n">type2</span><span class="p">,</span> "<span class="n">FooProvider</span><span class="o">-</span>4"<span class="p">);</span> |
| <span class="p">}</span> |
| |
| <span class="n">public</span> <span class="n">void</span> <span class="n">tearDown</span><span class="p">()</span> <span class="p">{</span> |
| <span class="n">helper</span><span class="p">.</span><span class="n">dispose</span><span class="p">();</span> <span class="o">//</span> <span class="n">Dispose</span> <span class="n">it</span><span class="p">,</span> <span class="n">instances</span> <span class="n">will</span> <span class="n">be</span> <span class="n">disposed</span> <span class="n">too</span><span class="p">.</span> |
| <span class="p">}</span> |
| |
| |
| <span class="n">You</span> <span class="n">can</span> <span class="n">also</span> <span class="n">implements</span> <span class="n">your</span> <span class="n">own</span> <span class="n">helper</span> <span class="p">(</span><span class="k">for</span> <span class="n">specific</span> <span class="n">purpose</span><span class="p">)</span> <span class="n">by</span> <span class="n">just</span> <span class="n">implementing</span> <span class="n">the</span> <span class="n">interface</span><span class="p">.</span> |
| <span class="o">\\</span> |
| <span class="o">\\</span> |
| </pre></div> |
| </div> |
| </div> |
| |
| <hr/> |
| |
| <div class="container"> |
| <footer id="footer"> |
| <div class="row"> |
| <div class="trademarkFooter span7"> |
| Apache Felix, Felix, Apache, the Apache feather logo, and the Apache Felix project |
| logo are trademarks of The Apache Software Foundation. All other marks mentioned |
| may be trademarks or registered trademarks of their respective owners. |
| </div> |
| <div class="timestamp span3 offset2"> |
| Rev. 1700393 by cziegeler on Tue, 1 Sep 2015 06:04:06 +0000 |
| </div> |
| </div> |
| </footer> |
| </div> |
| </body> |
| |
| <script type="text/javascript"> |
| var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); |
| document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); |
| </script> |
| |
| <script type="text/javascript"> |
| try{ |
| var pageTracker = _gat._getTracker("UA-1518442-4"); |
| pageTracker._trackPageview(); |
| } catch(err) {} |
| </script> |
| |
| </html> |