Author: [email protected]
Date: Fri Jan 27 13:31:48 2012
New Revision: 2014
Log:
[AMDATUOPENSOCIAL-193] Integration test runs successfully now, changes:
disabled ehCache, added classloader swithching for Guice injector instantiation
to solve classloader issues and added config dependency to
ShindigRegistrationService to prevent stop/start of this service during the
test.
Added:
trunk/amdatu-opensocial/test-integration/tests/src/test/java/org/amdatu/opensocial/test/integration/tests/util/ServiceUtil.java
Modified:
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/osgi/Activator.java
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/service/GuiceInjectorServletImpl.java
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/service/ShindigRegistrationServiceImpl.java
trunk/amdatu-opensocial/test-integration/base/src/main/java/org/amdatu/opensocial/test/integration/base/OpenSocialFixture.java
trunk/amdatu-opensocial/test-integration/pom.xml
trunk/amdatu-opensocial/test-integration/tests/pom.xml
trunk/amdatu-opensocial/test-integration/tests/src/test/java/org/amdatu/opensocial/test/integration/tests/framework/OpenSocialTest.java
Modified:
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/osgi/Activator.java
==============================================================================
---
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/osgi/Activator.java
(original)
+++
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/osgi/Activator.java
Fri Jan 27 13:31:48 2012
@@ -37,7 +37,6 @@
import
org.amdatu.opensocial.shindig.service.TenantHostnameDispatchExtenderFilter;
import org.amdatu.web.dispatcher.DispatchExtenderFilter;
import org.amdatu.web.dispatcher.DispatcherService;
-import org.amdatu.web.httpcontext.HttpContextManagerService;
import org.amdatu.web.httpcontext.ResourceProvider;
import org.amdatu.web.resource.ResourceSupport;
@@ -82,7 +81,7 @@
.setInterface(ShindigConfigurationService.class.getName(),
null)
.add(createConfigurationDependency().setPid(ShindigService.SHINDIG_CONFIG_PID)));
- // Create and register the Processor service
+ // Create and register the Processor service
manager.add(
createComponent()
.setImplementation(GadgetSpecProcessorImpl.class)
@@ -159,11 +158,10 @@
.setInterface(new String[] { ShindigService.class.getName(),
ResourceProvider.class.getName() },
properties)
.setImplementation(ShindigRegistrationServiceImpl.class)
-
.add(createServiceDependency().setService(DispatcherService.class).setRequired(true))
-
.add(createServiceDependency().setService(HttpContextManagerService.class).setRequired(true))
.add(createServiceDependency().setService(LogService.class).setRequired(true))
.add(createServiceDependency().setService(SocialApiModule.class).setRequired(true))
-
.add(createServiceDependency().setService(OAuthModule.class).setRequired(true)));
+
.add(createServiceDependency().setService(OAuthModule.class).setRequired(true))
+
.add(createServiceDependency().setService(ShindigConfigurationService.class).setRequired(true)));
// Register the Shindig OpenSocial implementation service
manager.add(
Modified:
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/service/GuiceInjectorServletImpl.java
==============================================================================
---
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/service/GuiceInjectorServletImpl.java
(original)
+++
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/service/GuiceInjectorServletImpl.java
Fri Jan 27 13:31:48 2012
@@ -87,7 +87,7 @@
// Instance variables
private boolean m_jmxInitialized;
private Component m_serviceComponent;
-
+
@Inject
private Processor m_processor;
@@ -107,7 +107,7 @@
ServletContext context = config.getServletContext();
context.setAttribute(GuiceServletContextListener.INJECTOR_ATTRIBUTE,
injector);
registerService();
-
+
// The Processor has been injected by injectMembers, pass it to the
processor service
m_processorService.setProcessor(m_processor);
}
@@ -149,7 +149,7 @@
if (isEhCacheEnabled()) {
moduleNames.add("org.apache.shindig.common.cache.ehcache.EhCacheModule");
}
-
+
// Now add all Guice-injected modules
for (String moduleName : moduleNames) {
try {
@@ -179,18 +179,29 @@
// Finally add our own social API module
modules.add(m_socialApiModule);
- }
- Injector injector = Guice.createInjector(Stage.PRODUCTION, modules);
- injector.injectMembers(this);
- try {
- if (!m_jmxInitialized) {
- Manager.manage("ShindigGuiceContext", injector);
- m_jmxInitialized = true;
+ }
+
+
+ // Switch the context classloader to the bundle classloader while
creating the Guice injector
+ ClassLoader cl = Thread.currentThread().getContextClassLoader();
+ Injector injector = null;
+ try {
+
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
+ injector = Guice.createInjector(Stage.PRODUCTION, modules);
+ injector.injectMembers(this);
+ try {
+ if (!m_jmxInitialized) {
+ Manager.manage("ShindigGuiceContext", injector);
+ m_jmxInitialized = true;
+ }
}
- }
- catch (Exception e) {
- // Ignore errors
- }
+ catch (Exception e) {
+ // Ignore errors
+ }
+ } finally {
+ Thread.currentThread().setContextClassLoader(cl);
+ }
+
return injector;
}
Modified:
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/service/ShindigRegistrationServiceImpl.java
==============================================================================
---
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/service/ShindigRegistrationServiceImpl.java
(original)
+++
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/service/ShindigRegistrationServiceImpl.java
Fri Jan 27 13:31:48 2012
@@ -43,7 +43,6 @@
import org.apache.felix.dm.DependencyManager;
import org.apache.felix.dm.ServiceDependency;
import org.apache.shindig.auth.AuthenticationServletFilter;
-import org.apache.shindig.gadgets.process.Processor;
import org.apache.shindig.gadgets.servlet.ConcatProxyServlet;
import org.apache.shindig.gadgets.servlet.GadgetRenderingServlet;
import org.apache.shindig.gadgets.servlet.JsServlet;
Modified:
trunk/amdatu-opensocial/test-integration/base/src/main/java/org/amdatu/opensocial/test/integration/base/OpenSocialFixture.java
==============================================================================
---
trunk/amdatu-opensocial/test-integration/base/src/main/java/org/amdatu/opensocial/test/integration/base/OpenSocialFixture.java
(original)
+++
trunk/amdatu-opensocial/test-integration/base/src/main/java/org/amdatu/opensocial/test/integration/base/OpenSocialFixture.java
Fri Jan 27 13:31:48 2012
@@ -19,10 +19,12 @@
import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
import static org.ops4j.pax.exam.CoreOptions.wrappedBundle;
-import java.util.Properties;
-
import org.amdatu.core.itest.base.TestContext;
import org.amdatu.opensocial.shindig.ShindigService;
+
+import java.util.Properties;
+
+import org.ops4j.pax.exam.CoreOptions;
import org.ops4j.pax.exam.Option;
public class OpenSocialFixture {
@@ -31,18 +33,17 @@
public static final String PORTNR = "8080";
public Option provision() {
- // System.setProperty("jaxp.debug", "true");
- //System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
"org.apache.crimson.jaxp.DocumentBuilderFactoryImpl");
return composite(
- //systemProperty("jaxp.debug").value("true"),
- // Add sun.misc and com.sun.management to system packages
-
//systemProperty("javax.xml.parsers.DocumentBuilderFactory").value("org.apache.crimson.jaxp.DocumentBuilderFactoryImpl"),
+ // NB: enable this line to enable JAXB debugging
+ //
org.ops4j.pax.exam.CoreOptions.systemProperty("jaxp.debug").value("true"),
- org.ops4j.pax.exam.CoreOptions.provision(
+ // Add sun.misc and com.sun.management to system packages
+
CoreOptions.systemProperty("javax.xml.parsers.DocumentBuilderFactory").value("org.apache.crimson.jaxp.DocumentBuilderFactoryImpl"),
+ CoreOptions.provision(
// Wrap libraries we depend on as OSGi bundles and provision
those
wrappedBundle(mavenBundle().groupId("commons-httpclient").artifactId("commons-httpclient").versionAsInProject()),
wrappedBundle(mavenBundle().groupId("commons-codec").artifactId("commons-codec").versionAsInProject()),
-
wrappedBundle(mavenBundle().groupId("commons-logging").artifactId("commons-logging").versionAsInProject()),
+ //
wrappedBundle(mavenBundle().groupId("commons-logging").artifactId("commons-logging").versionAsInProject()),
wrappedBundle(mavenBundle().groupId("org.json").artifactId("json").versionAsInProject()),
// The following 2 artifacts are necessary for default tenant
resolving
@@ -56,14 +57,15 @@
mavenBundle().groupId("org.amdatu.opensocial").artifactId("org.amdatu.opensocial.dashboard").versionAsInProject(),
mavenBundle().groupId("org.amdatu.opensocial").artifactId("org.amdatu.opensocial.gadgetmanagement").versionAsInProject(),
mavenBundle().groupId("org.amdatu.opensocial").artifactId("org.amdatu.opensocial.shindig").versionAsInProject(),
-
+
mavenBundle().groupId("org.ops4j.pax.swissbox").artifactId("pax-swissbox-core").versionAsInProject(),
mavenBundle().groupId("org.ops4j.base").artifactId("ops4j-base-lang").versionAsInProject()));
+
}
public void configureDefaults(TestContext testContext) throws Exception {
+ configureHttpService(testContext);
configureShindig(testContext);
- configureHttpService(testContext);
}
public void configureShindig(TestContext testContext) throws Exception {
@@ -73,7 +75,7 @@
private Properties getShindigCfg() {
Properties properties = new Properties();
properties.put("shindig.features.default",
"res://features/features.txt");
- properties.put("shindig.containers.default",
"res://conf/container.js");
+ properties.put("shindig.containers.default", "");
properties.put("shindig.blacklist.file", "");
properties.put("shindig.oauth.base-url", "/oauth/");
properties.put("shindig.oauth.authorize-action",
"/dashboard/authorize.jsp");
@@ -109,8 +111,7 @@
properties.put("shindig.cache.lru.gadgetSpecs.capacity", "1000");
properties.put("shindig.cache.lru.messageBundles.capacity", "1000");
properties.put("shindig.cache.lru.httpResponses.capacity", "10000");
- properties.put("shindig.cache.ehcache.config",
- "res://org/apache/shindig/common/cache/ehcache/ehcacheConfig.xml");
+ properties.put("shindig.cache.ehcache.config", "");
properties.put("shindig.cache.ehcache.jmx.enabled", "true");
properties.put("shindig.cache.ehcache.jmx.stats", "true");
properties.put("shindig.http.fast-encoding-detection", "true");
@@ -122,16 +123,16 @@
properties.put("org.apache.shindig.serviceExpirationDurationMinutes",
"60");
properties.put("shindig.json-rpc.result-field", "result");
properties.put("shindig.accelerate.remapInternalServerError", "true");
- properties.put("shindig.host", HOSTNAME);
+ properties.put("shindig.host", "null");
properties.put("shindig.port", PORTNR);
properties.put("shindig.proxy.remapInternalServerError", "false");
return properties;
}
-
+
public void configureHttpService(TestContext testContext) throws Exception
{
testContext.updateConfig("org.apache.felix.http", getHttpServiceCfg());
}
-
+
private Properties getHttpServiceCfg() {
Properties properties = new Properties();
properties.put("org.osgi.service.http.hostname", HOSTNAME);
Modified: trunk/amdatu-opensocial/test-integration/pom.xml
==============================================================================
--- trunk/amdatu-opensocial/test-integration/pom.xml (original)
+++ trunk/amdatu-opensocial/test-integration/pom.xml Fri Jan 27 13:31:48 2012
@@ -30,7 +30,6 @@
<org.amdatu.core.version>0.2.1</org.amdatu.core.version>
<org.amdatu.web.version>0.2.1</org.amdatu.web.version>
<org.amdatu.auth.version>0.2.0</org.amdatu.auth.version>
- <org.apache.felix.http.version>2.2.0</org.apache.felix.http.version>
</properties>
<modules>
@@ -38,108 +37,47 @@
<module>tests</module>
</modules>
- <dependencyManagement>
- <dependencies>
- <!--
- Amdatu core and web itest base is crosscutting to both the base and
tests
- module. This brings in the all dependencies required to work with
- the core and web fixtures. Scope is intentionally set to compile in
this
- section so that depending (on the base) itest modules will in turn
- recieve these transitive dependencies.
- -->
- <dependency>
- <groupId>org.amdatu.core</groupId>
- <artifactId>org.amdatu.core.itest.base</artifactId>
- <version>${org.amdatu.core.version}</version>
- <scope>compile</scope>
- <type>bundle</type>
- </dependency>
- <dependency>
- <groupId>org.amdatu.web</groupId>
- <artifactId>org.amdatu.web.itest.base</artifactId>
- <version>${org.amdatu.web.version}</version>
- <scope>compile</scope>
- <type>bundle</type>
- </dependency>
- <dependency>
- <groupId>org.amdatu.opensocial</groupId>
- <artifactId>org.amdatu.opensocial.test.integration.base</artifactId>
- <version>${project.version}</version>
- <scope>compile</scope>
- <type>bundle</type>
- </dependency>
+ <dependencyManagement>
+ <dependencies>
+ <!--
+ Amdatu core and web itest base is crosscutting to both the base and
tests
+ module. This brings in the all dependencies required to work with
+ the core and web fixtures. Scope is intentionally set to compile in
this
+ section so that depending (on the base) itest modules will in turn
+ recieve these transitive dependencies.
+ -->
+ <dependency>
+ <groupId>org.amdatu.core</groupId>
+ <artifactId>org.amdatu.core.itest.base</artifactId>
+ <version>${org.amdatu.core.version}</version>
+ <scope>compile</scope>
+ <type>bundle</type>
+ </dependency>
+ <dependency>
+ <groupId>org.amdatu.web</groupId>
+ <artifactId>org.amdatu.web.itest.base</artifactId>
+ <version>${org.amdatu.web.version}</version>
+ <scope>compile</scope>
+ <type>bundle</type>
+ </dependency>
+ </dependencies>
+ </dependencyManagement>
+
+ <dependencies>
+ <!--
+ See explanation in the dependencyManagement section above.
+ -->
+ <dependency>
+ <groupId>org.amdatu.core</groupId>
+ <artifactId>org.amdatu.core.itest.base</artifactId>
+ <type>bundle</type>
+ </dependency>
+ <dependency>
+ <groupId>org.amdatu.web</groupId>
+ <artifactId>org.amdatu.web.itest.base</artifactId>
+ <type>bundle</type>
+ </dependency>
- <!--
- Some dependency scopes are intentionally set to compile in this
- section so that depending (on the base) itest modules will in turn
recieve
- these transitive dependencies.
- -->
-
- <dependency>
- <groupId>org.apache.felix</groupId>
- <artifactId>org.apache.felix.http.jetty</artifactId>
- <version>${org.apache.felix.http.version}</version>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.amdatu.web</groupId>
- <artifactId>org.amdatu.web.dispatcher</artifactId>
- <version>${org.amdatu.web.version}</version>
- <scope>compile</scope>
- <type>bundle</type>
- </dependency>
- <dependency>
- <groupId>org.amdatu.web</groupId>
- <artifactId>org.amdatu.web.httpcontext</artifactId>
- <version>${org.amdatu.web.version}</version>
- <scope>compile</scope>
- <type>bundle</type>
- </dependency>
- <dependency>
- <groupId>org.amdatu.web</groupId>
- <artifactId>org.amdatu.web.resource</artifactId>
- <version>${org.amdatu.web.version}</version>
- <scope>compile</scope>
- <type>bundle</type>
- </dependency>
- <dependency>
- <groupId>org.amdatu.web</groupId>
- <artifactId>org.amdatu.web.jsp</artifactId>
- <version>${org.amdatu.web.version}</version>
- <scope>compile</scope>
- <type>bundle</type>
- </dependency>
- <dependency>
- <groupId>org.amdatu.web</groupId>
- <artifactId>org.amdatu.web.jaxrs</artifactId>
- <version>${org.amdatu.web.version}</version>
- <scope>compile</scope>
- <type>bundle</type>
- </dependency>
- <dependency>
- <groupId>org.amdatu.web</groupId>
- <artifactId>org.amdatu.web.wink</artifactId>
- <version>${org.amdatu.web.version}</version>
- <scope>compile</scope>
- <type>bundle</type>
- </dependency>
- </dependencies>
- </dependencyManagement>
-
- <dependencies>
- <!--
- See explanation in the dependencyManagement section above.
- -->
- <dependency>
- <groupId>org.amdatu.core</groupId>
- <artifactId>org.amdatu.core.itest.base</artifactId>
- <type>bundle</type>
- </dependency>
- <dependency>
- <groupId>org.amdatu.web</groupId>
- <artifactId>org.amdatu.web.itest.base</artifactId>
- <type>bundle</type>
- </dependency>
- </dependencies>
+ </dependencies>
</project>
\ No newline at end of file
Modified: trunk/amdatu-opensocial/test-integration/tests/pom.xml
==============================================================================
--- trunk/amdatu-opensocial/test-integration/tests/pom.xml (original)
+++ trunk/amdatu-opensocial/test-integration/tests/pom.xml Fri Jan 27
13:31:48 2012
@@ -219,6 +219,7 @@
<plugin>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>maven-paxexam-plugin</artifactId>
+ <version>1.2.4</version>
<executions>
<execution>
<phase>integration-test</phase>
Modified:
trunk/amdatu-opensocial/test-integration/tests/src/test/java/org/amdatu/opensocial/test/integration/tests/framework/OpenSocialTest.java
==============================================================================
---
trunk/amdatu-opensocial/test-integration/tests/src/test/java/org/amdatu/opensocial/test/integration/tests/framework/OpenSocialTest.java
(original)
+++
trunk/amdatu-opensocial/test-integration/tests/src/test/java/org/amdatu/opensocial/test/integration/tests/framework/OpenSocialTest.java
Fri Jan 27 13:31:48 2012
@@ -15,6 +15,7 @@
*/
package org.amdatu.opensocial.test.integration.tests.framework;
+import static
org.amdatu.opensocial.test.integration.tests.util.ServiceUtil.assertAvailable;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertThat;
@@ -23,9 +24,11 @@
import org.amdatu.core.itest.base.CoreFixture;
import org.amdatu.core.itest.base.TestContext;
+import org.amdatu.opensocial.shindig.ShindigService;
import org.amdatu.opensocial.test.integration.base.OpenSocialFixture;
import org.amdatu.opensocial.test.integration.tests.DashboardTest;
import org.amdatu.web.itest.base.WebFixture;
+
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Option;
@@ -35,9 +38,8 @@
import org.ops4j.pax.exam.junit.JUnit4TestRunner;
import org.ops4j.pax.exam.spi.reactors.AllConfinedStagedReactorFactory;
import org.osgi.framework.BundleContext;
-import org.osgi.framework.Constants;
-import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogService;
+
@RunWith(JUnit4TestRunner.class)
@ExamReactorStrategy(AllConfinedStagedReactorFactory.class)
@ExamFactory(AmdatuFactory.class)
@@ -48,7 +50,7 @@
private OpenSocialFixture m_openSocialFixture = new OpenSocialFixture();
private LogService m_logService;
-
+
@Configuration
public Option[] config() {
return options(
@@ -72,66 +74,11 @@
// FIXME quickfix to let configadmin settle to
// prevent felix dropping configs
- Thread.sleep(1000);
+ Thread.sleep(5000);
return testContext;
}
- // FIXME: [AMDATU-379] This method is here since TestContext.getService
doesn't (always) work.
- // For optimal performance we do this in 3 steps:
- // 1. Try to get the service directly using getAllServiceReferences,
return it if available
- // 2. Invoke super.getService using the service tracker, this will wait
until the service is available
- // 3. Again try to get the service directly using getAllServiceReferences,
as step 2. might ave failed
- @SuppressWarnings("unchecked")
- <T extends Object> T getService(TestContext testContext, Class<T> clazz)
throws Exception {
- BundleContext bundleContext = testContext.getBundleContext();
- ServiceReference[] servRef =
bundleContext.getAllServiceReferences(clazz.getName(), null);
- if (servRef != null && servRef.length > 0) {
- return (T)
bundleContext.getService(getServiceRefWithHighestRank(servRef));
- }
-
- T service = testContext.getService(clazz);
- if (service != null) {
- return service;
- }
-
- servRef = bundleContext.getAllServiceReferences(clazz.getName(), null);
- if (servRef != null && servRef.length > 0) {
- return (T)
bundleContext.getService(getServiceRefWithHighestRank(servRef));
- }
- return null;
- }
-
- private ServiceReference getServiceRefWithHighestRank(ServiceReference[]
servRefs) {
- int maxRank = -1;
- if (servRefs.length == 1) {
- return servRefs[0];
- }
- ServiceReference maxServRef = null;
- for (ServiceReference servRef : servRefs) {
- Object prop = servRef.getProperty(Constants.SERVICE_RANKING);
- int rank;
- if (prop == null) {
- rank = 0;
- } else if (prop instanceof Integer) {
- rank = (Integer)
servRef.getProperty(Constants.SERVICE_RANKING);
- } else {
- rank =
Integer.parseInt(servRef.getProperty(Constants.SERVICE_RANKING).toString());
- }
- if (rank >= maxRank) {
- maxServRef = servRef;
- maxRank = rank;
- }
- }
- return maxServRef;
- }
-
- private <T> T assertAvailable(TestContext testContext, Class<T>
serviceClass) throws Exception {
- T service = getService(testContext, serviceClass);
- assertThat("Expected a " + serviceClass.getName(), service,
is(notNullValue()));
- return service;
- }
-
@Test
public void runTest(BundleContext bundleContext) throws Exception {
// Setup test context
@@ -140,19 +87,21 @@
// Initialize services
m_logService = assertAvailable(testContext, LogService.class);
+ assertAvailable(testContext, ShindigService.class);
+
// Run the tests
test(DashboardTest.class);
-
+
// And we are done
testContext.tearDown();
}
-
+
private <T extends OpenSocialTestBase> void test(Class<T> testClass)
throws Exception {
T test = testClass.newInstance();
init(test);
test.execute();
}
-
+
private void init(OpenSocialTestBase test) {
test.setLogService(m_logService);
}
Added:
trunk/amdatu-opensocial/test-integration/tests/src/test/java/org/amdatu/opensocial/test/integration/tests/util/ServiceUtil.java
==============================================================================
--- (empty file)
+++
trunk/amdatu-opensocial/test-integration/tests/src/test/java/org/amdatu/opensocial/test/integration/tests/util/ServiceUtil.java
Fri Jan 27 13:31:48 2012
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * Licensed 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.
+ */
+package org.amdatu.opensocial.test.integration.tests.util;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNull.notNullValue;
+import static org.junit.Assert.assertThat;
+
+import org.amdatu.core.itest.base.TestContext;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+
+public class ServiceUtil {
+ private static final long SERVICE_TIMEOUT = 100000;
+
+ // FIXME: [AMDATU-379] This method is here since TestContext.getService
doesn't (always) work.
+ // For optimal performance we do this in 3 steps:
+ // 1. Try to get the service directly using getAllServiceReferences,
return it if available
+ // 2. Invoke super.getService using the service tracker, this will wait
until the service is available
+ // 3. Again try to get the service directly using getAllServiceReferences,
as step 2. might have failed
+ public static <T extends Object> T getService(TestContext testContext,
Class<T> clazz) throws Exception {
+ return getService(testContext, clazz, null);
+ }
+
+ @SuppressWarnings("unchecked")
+ public static <T extends Object> T getService(TestContext testContext,
Class<T> clazz, String filter) throws Exception {
+ BundleContext bundleContext = testContext.getBundleContext();
+ ServiceReference[] servRef =
bundleContext.getAllServiceReferences(clazz.getName(), filter);
+ if (servRef != null && servRef.length > 0) {
+ return (T)
bundleContext.getService(getServiceRefWithHighestRank(servRef));
+ }
+
+ T service = testContext.getService(clazz);
+ if (service != null) {
+ return service;
+ }
+
+ servRef = bundleContext.getAllServiceReferences(clazz.getName(), null);
+ if (servRef != null && servRef.length > 0) {
+ return (T)
bundleContext.getService(getServiceRefWithHighestRank(servRef));
+ }
+ return null;
+ }
+
+ private static ServiceReference
getServiceRefWithHighestRank(ServiceReference[] servRefs) {
+ int maxRank = -1;
+ if (servRefs.length == 1) {
+ return servRefs[0];
+ }
+ ServiceReference maxServRef = null;
+ for (ServiceReference servRef : servRefs) {
+ Object prop = servRef.getProperty(Constants.SERVICE_RANKING);
+ int rank;
+ if (prop == null) {
+ rank = 0;
+ } else if (prop instanceof Integer) {
+ rank = (Integer)
servRef.getProperty(Constants.SERVICE_RANKING);
+ } else {
+ rank =
Integer.parseInt(servRef.getProperty(Constants.SERVICE_RANKING).toString());
+ }
+ if (rank >= maxRank) {
+ maxServRef = servRef;
+ maxRank = rank;
+ }
+ }
+ return maxServRef;
+ }
+
+ public static <T> T assertAvailable(TestContext testContext, Class<T>
serviceClass) throws Exception {
+ T service = getService(testContext, serviceClass);
+ long timeout = System.currentTimeMillis() + SERVICE_TIMEOUT;
+ while (service == null && System.currentTimeMillis() < timeout) {
+ // Wait a second
+ Thread.sleep(1000);
+
+ // ... and try again
+ service = getService(testContext, serviceClass);
+ }
+ assertThat("Expected a " + serviceClass.getName(), service,
is(notNullValue()));
+ return service;
+ }
+}
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits