Author: [email protected]
Date: Fri May 20 09:17:22 2011
New Revision: 1144
Log:
[AMDATUOPENSOCIAL-40] Implemented dashboard integration test
Added:
trunk/amdatu-opensocial/test-integration/tests/src/test/java/org/amdatu/opensocial/test/integration/tests/AmdatuFrameworkFactory.java
trunk/amdatu-opensocial/test-integration/tests/src/test/java/org/amdatu/opensocial/test/integration/tests/DashboardTest.java
trunk/amdatu-opensocial/test-integration/tests/src/test/java/org/amdatu/opensocial/test/integration/tests/framework/
trunk/amdatu-opensocial/test-integration/tests/src/test/java/org/amdatu/opensocial/test/integration/tests/framework/AmdatuFactory.java
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/OpenSocialTestBase.java
Added:
trunk/amdatu-opensocial/test-integration/tests/src/test/java/org/amdatu/opensocial/test/integration/tests/AmdatuFrameworkFactory.java
==============================================================================
--- (empty file)
+++
trunk/amdatu-opensocial/test-integration/tests/src/test/java/org/amdatu/opensocial/test/integration/tests/AmdatuFrameworkFactory.java
Fri May 20 09:17:22 2011
@@ -0,0 +1,42 @@
+/*
+ * 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;
+
+import java.util.Map;
+
+import org.apache.felix.framework.Felix;
+import org.apache.felix.framework.util.FelixConstants;
+import org.osgi.framework.launch.Framework;
+
+/**
+ * The purpose of this custom Framework factory is to append certain packages
to the org.osgi.framework.system.packages.extra
+ * system property. Unfortunately, in Pax Exam 2.0.0 this system property is
overruled by the native test container
+ * (see org.ops4j.pax.exam.nat.internal.NativeTestContainer line 209).
Probably this is a unintended bug in Pax Exam 2.0.0
+ * (it worked fine in 1.2) and this should be filed. However, at the time of
this writing (18-05-2011) it is impossible to sign up for
+ * an OPS4J JIRA account (due to server maintenance???)
+ *
+ * @author ivol
+ */
+public class AmdatuFrameworkFactory implements
org.osgi.framework.launch.FrameworkFactory {
+
+ @SuppressWarnings("unchecked")
+ public Framework newFramework(Map configuration) {
+ String extraSystemPackages =
configuration.get(FelixConstants.FRAMEWORK_SYSTEMPACKAGES_EXTRA).toString();
+ extraSystemPackages += ",sun.misc,com.sun.management,com.sun.org";
+ configuration.put(FelixConstants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
extraSystemPackages);
+ return new Felix(configuration);
+ }
+}
Added:
trunk/amdatu-opensocial/test-integration/tests/src/test/java/org/amdatu/opensocial/test/integration/tests/DashboardTest.java
==============================================================================
--- (empty file)
+++
trunk/amdatu-opensocial/test-integration/tests/src/test/java/org/amdatu/opensocial/test/integration/tests/DashboardTest.java
Fri May 20 09:17:22 2011
@@ -0,0 +1,27 @@
+/*
+ * 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;
+
+import
org.amdatu.opensocial.test.integration.tests.framework.OpenSocialTestBase;
+import org.apache.commons.httpclient.HttpStatus;
+
+public class DashboardTest extends OpenSocialTestBase {
+ @Override
+ public void execute() throws Exception {
+ // 1. Test if the dashboard is accessible
+ invokeHTTP("/dashboard/jsp/dashboard.jsp", javax.ws.rs.HttpMethod.GET,
HttpStatus.SC_OK);
+ }
+}
Added:
trunk/amdatu-opensocial/test-integration/tests/src/test/java/org/amdatu/opensocial/test/integration/tests/framework/AmdatuFactory.java
==============================================================================
--- (empty file)
+++
trunk/amdatu-opensocial/test-integration/tests/src/test/java/org/amdatu/opensocial/test/integration/tests/framework/AmdatuFactory.java
Fri May 20 09:17:22 2011
@@ -0,0 +1,47 @@
+/*
+ * 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.framework;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.amdatu.opensocial.test.integration.tests.AmdatuFrameworkFactory;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.TestContainer;
+import org.ops4j.pax.exam.TestContainerException;
+import org.ops4j.pax.exam.TestContainerFactory;
+import org.ops4j.pax.exam.nat.internal.NativeTestContainer;
+import org.ops4j.pax.exam.nat.internal.NativeTestContainerParser;
+import org.ops4j.pax.exam.options.ProvisionOption;
+
+/**
+ * The sole purpose of this class is to allow the use of an alternative
Framework Factory.
+ *
+ * @author ivol
+ */
+public class AmdatuFactory implements TestContainerFactory {
+
+ @SuppressWarnings("rawtypes")
+ public TestContainer[] parse( Option... options ) throws
TestContainerException {
+ NativeTestContainerParser parser = new NativeTestContainerParser(
options );
+ List<ProvisionOption> bundles = parser.getBundles();
+ Map<String, String> properties = parser.getSystemProperties();
+ List<TestContainer> containers = new ArrayList<TestContainer>();
+ containers.add(new NativeTestContainer(new AmdatuFrameworkFactory() ,
bundles, properties ));
+ return containers.toArray( new TestContainer[containers.size()] );
+ }
+}
Added:
trunk/amdatu-opensocial/test-integration/tests/src/test/java/org/amdatu/opensocial/test/integration/tests/framework/OpenSocialTest.java
==============================================================================
--- (empty file)
+++
trunk/amdatu-opensocial/test-integration/tests/src/test/java/org/amdatu/opensocial/test/integration/tests/framework/OpenSocialTest.java
Fri May 20 09:17:22 2011
@@ -0,0 +1,159 @@
+/*
+ * 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.framework;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNull.notNullValue;
+import static org.junit.Assert.assertThat;
+import static org.ops4j.pax.exam.CoreOptions.options;
+import static org.ops4j.pax.exam.LibraryOptions.junitBundles;
+
+import org.amdatu.core.itest.base.CoreFixture;
+import org.amdatu.core.itest.base.TestContext;
+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;
+import org.ops4j.pax.exam.junit.Configuration;
+import org.ops4j.pax.exam.junit.ExamFactory;
+import org.ops4j.pax.exam.junit.ExamReactorStrategy;
+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)
+public class OpenSocialTest {
+ // Fixtures used by the OpenSocial integration tests
+ private CoreFixture m_coreFixture = new CoreFixture();
+ private WebFixture m_webFixture = new WebFixture();
+ private OpenSocialFixture m_openSocialFixture = new OpenSocialFixture();
+
+ private LogService m_logService;
+
+ @Configuration
+ public Option[] config() {
+ return options(
+ junitBundles(),
+ m_coreFixture.provisionWithFsStorage(),
+ m_webFixture.provision(),
+ m_openSocialFixture.provision());
+ }
+
+ public TestContext testContextSetUp(BundleContext bundleContext) throws
Exception {
+ assertThat(bundleContext, is(notNullValue()));
+ TestContext testContext = new TestContext(bundleContext);
+
+ // FIXME quickfix to let startlevel complete to
+ // prevent felix bundle locking issues
+ Thread.sleep(1000);
+
+ // Run the default configurations
+ m_coreFixture.configureDefaults(testContext);
+ m_openSocialFixture.configureDefaults(testContext);
+
+ // FIXME quickfix to let configadmin settle to
+ // prevent felix dropping configs
+ Thread.sleep(1000);
+
+ 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
+ TestContext testContext = testContextSetUp(bundleContext);
+
+ // Initialize services
+ m_logService = assertAvailable(testContext, LogService.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/framework/OpenSocialTestBase.java
==============================================================================
--- (empty file)
+++
trunk/amdatu-opensocial/test-integration/tests/src/test/java/org/amdatu/opensocial/test/integration/tests/framework/OpenSocialTestBase.java
Fri May 20 09:17:22 2011
@@ -0,0 +1,73 @@
+/*
+ * 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.framework;
+
+import static junit.framework.Assert.assertTrue;
+
+import org.amdatu.opensocial.test.integration.base.OpenSocialFixture;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.methods.DeleteMethod;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.PutMethod;
+import org.osgi.service.log.LogService;
+
+public abstract class OpenSocialTestBase {
+ protected volatile LogService m_logService;
+ private String m_baseUrl = null;
+
+ public abstract void execute() throws Exception;
+
+ public void setLogService(LogService logService) {
+ m_logService = logService;
+ }
+
+ protected String getBaseUrl() throws Exception {
+ if (m_baseUrl == null) {
+ m_baseUrl = "http://" + OpenSocialFixture.HOSTNAME + ":" +
OpenSocialFixture.PORTNR;
+ }
+ return m_baseUrl;
+ }
+
+ protected String invokeHTTP(String urlPostfix, String httpMethod, int
expectedStatus) throws Exception {
+ String url = getBaseUrl() + urlPostfix;
+ HttpClient httpClient = new HttpClient();
+ m_logService.log(LogService.LOG_DEBUG, "Invoking REST API " +
httpMethod + " '" + url + "'");
+ HttpMethod method = null;
+ if (httpMethod.equals(javax.ws.rs.HttpMethod.GET)) {
+ method = new GetMethod(url);
+ } else if (httpMethod.equals(javax.ws.rs.HttpMethod.PUT)) {
+ method = new PutMethod(url);
+ } else if (httpMethod.equals(javax.ws.rs.HttpMethod.DELETE)) {
+ method = new DeleteMethod(url);
+ } else if (httpMethod.equals(javax.ws.rs.HttpMethod.POST)) {
+ method = new PostMethod(url);
+ }
+ try {
+ // Execute the method, this should return a 200
+ int statusCode = httpClient.executeMethod(method);
+ assertTrue("HTTP '" + httpMethod + "' to '" + url + "' returned "
+ statusCode, statusCode == expectedStatus);
+
+ // Read the response body and return it.
+ return new String(method.getResponseBody(), "UTF-8");
+ }
+ finally {
+ // Release the connection.
+ method.releaseConnection();
+ }
+ }
+}
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits