You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by pi...@apache.org on 2012/05/17 09:10:50 UTC
svn commit: r1339500 - in /karaf/trunk/tooling/exam/regression: ./
src/test/java/org/apache/karaf/tooling/exam/regression/
src/test/java/org/apache/karaf/tooling/exam/regression/supports/
Author: pieber
Date: Thu May 17 07:10:49 2012
New Revision: 1339500
URL: http://svn.apache.org/viewvc?rev=1339500&view=rev
Log:
[KARAF-1489] Add an itest showing how to correctly use tinybundles with provision in paxexam-karaf
Signed-off-by: Andreas Pieber <an...@gmail.com>
Added:
karaf/trunk/tooling/exam/regression/src/test/java/org/apache/karaf/tooling/exam/regression/KarafWithBundleTest.java
karaf/trunk/tooling/exam/regression/src/test/java/org/apache/karaf/tooling/exam/regression/supports/
karaf/trunk/tooling/exam/regression/src/test/java/org/apache/karaf/tooling/exam/regression/supports/MyServlet.java
karaf/trunk/tooling/exam/regression/src/test/java/org/apache/karaf/tooling/exam/regression/supports/ServletActivator.java
Modified:
karaf/trunk/tooling/exam/regression/pom.xml
Modified: karaf/trunk/tooling/exam/regression/pom.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/tooling/exam/regression/pom.xml?rev=1339500&r1=1339499&r2=1339500&view=diff
==============================================================================
--- karaf/trunk/tooling/exam/regression/pom.xml (original)
+++ karaf/trunk/tooling/exam/regression/pom.xml Thu May 17 07:10:49 2012
@@ -73,6 +73,31 @@
<artifactId>org.apache.karaf.features.core</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.compendium</artifactId>
+ <version>${osgi.compendium.version}</version>
+ <scope>test</scope>
+ <optional>true</optional>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.karaf</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.ops4j.pax.tinybundles</groupId>
+ <artifactId>tinybundles</artifactId>
+ <version>${pax.tinybundle.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>${servlet.spec.groupId}</groupId>
+ <artifactId>${servlet.spec.artifactId}</artifactId>
+ <version>${servlet.spec.version}</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
Added: karaf/trunk/tooling/exam/regression/src/test/java/org/apache/karaf/tooling/exam/regression/KarafWithBundleTest.java
URL: http://svn.apache.org/viewvc/karaf/trunk/tooling/exam/regression/src/test/java/org/apache/karaf/tooling/exam/regression/KarafWithBundleTest.java?rev=1339500&view=auto
==============================================================================
--- karaf/trunk/tooling/exam/regression/src/test/java/org/apache/karaf/tooling/exam/regression/KarafWithBundleTest.java (added)
+++ karaf/trunk/tooling/exam/regression/src/test/java/org/apache/karaf/tooling/exam/regression/KarafWithBundleTest.java Thu May 17 07:10:49 2012
@@ -0,0 +1,89 @@
+/*
+ * 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.
+ */
+
+package org.apache.karaf.tooling.exam.regression;
+
+import static org.apache.karaf.tooling.exam.options.KarafDistributionOption.karafDistributionConfiguration;
+import static org.apache.karaf.tooling.exam.options.KarafDistributionOption.keepRuntimeFolder;
+import static org.junit.Assert.assertEquals;
+import static org.ops4j.pax.exam.CoreOptions.maven;
+import static org.ops4j.pax.exam.CoreOptions.scanFeatures;
+import static org.ops4j.pax.exam.CoreOptions.streamBundle;
+import static org.ops4j.pax.tinybundles.core.TinyBundles.bundle;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.net.URL;
+import java.net.URLConnection;
+
+import org.apache.karaf.tooling.exam.options.KarafDistributionOption;
+import org.apache.karaf.tooling.exam.regression.supports.MyServlet;
+import org.apache.karaf.tooling.exam.regression.supports.ServletActivator;
+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.ExamReactorStrategy;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.ops4j.pax.exam.spi.reactors.AllConfinedStagedReactorFactory;
+import org.osgi.framework.Constants;
+
+@RunWith(JUnit4TestRunner.class)
+@ExamReactorStrategy(AllConfinedStagedReactorFactory.class)
+public class KarafWithBundleTest {
+ @Test
+ public void testService() throws Exception {
+ URL url = new URL("http://localhost:9080/test/services");
+ URLConnection conn = url.openConnection();
+ conn.setDoOutput(true);
+ OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
+ wr.write("This is a test");
+ wr.flush();
+
+ BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
+ String line;
+ line = rd.readLine();
+ assertEquals("Get a Wrong response", "This is a test", line);
+ wr.close();
+ rd.close();
+ }
+
+ @Configuration
+ public Option[] config() {
+ return new Option[]{ karafDistributionConfiguration().frameworkUrl(
+ maven().groupId("org.apache.karaf").artifactId("apache-karaf").type("zip")
+ .versionAsInProject()).useDeployFolder(false).unpackDirectory(new File("target/paxexam/unpack/")),
+ keepRuntimeFolder(),
+ scanFeatures(maven().groupId("org.apache.karaf.features").artifactId("standard").type("xml")
+ .classifier("features").versionAsInProject(), "war").start(),
+ // set the system property for pax web
+ KarafDistributionOption.editConfigurationFilePut("etc/org.ops4j.pax.web.cfg", "org.osgi.service.http.port",
+ "9080"),
+ // create bundle to install
+ streamBundle(bundle()
+ .add(MyServlet.class)
+ .add(ServletActivator.class)
+ .set(Constants.BUNDLE_SYMBOLICNAME, "MyBundleTest")
+ .set(Constants.BUNDLE_ACTIVATOR, ServletActivator.class.getName())
+ .set(Constants.DYNAMICIMPORT_PACKAGE, "*")
+ .build())
+
+ };
+ }
+}
Added: karaf/trunk/tooling/exam/regression/src/test/java/org/apache/karaf/tooling/exam/regression/supports/MyServlet.java
URL: http://svn.apache.org/viewvc/karaf/trunk/tooling/exam/regression/src/test/java/org/apache/karaf/tooling/exam/regression/supports/MyServlet.java?rev=1339500&view=auto
==============================================================================
--- karaf/trunk/tooling/exam/regression/src/test/java/org/apache/karaf/tooling/exam/regression/supports/MyServlet.java (added)
+++ karaf/trunk/tooling/exam/regression/src/test/java/org/apache/karaf/tooling/exam/regression/supports/MyServlet.java Thu May 17 07:10:49 2012
@@ -0,0 +1,60 @@
+/*
+ * 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.
+ */
+
+package org.apache.karaf.tooling.exam.regression.supports;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public class MyServlet extends HttpServlet {
+
+ private static final long serialVersionUID = 1068664967007496710L;
+
+ // now we just send a echo response back
+ @Override
+ protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,
+ IOException {
+ copy(request.getInputStream(), response.getOutputStream(), 4096);
+ }
+
+ public static int copy(final InputStream input, final OutputStream output, int bufferSize) throws IOException {
+ int avail = input.available();
+ if (avail > 262144) {
+ avail = 262144;
+ }
+ if (avail > bufferSize) {
+ bufferSize = avail;
+ }
+
+ final byte[] buffer = new byte[bufferSize];
+ int n = input.read(buffer);
+ int total = 0;
+ while (-1 != n) {
+ output.write(buffer, 0, n);
+ total += n;
+ n = input.read(buffer);
+ }
+ output.flush();
+ return total;
+ }
+}
Added: karaf/trunk/tooling/exam/regression/src/test/java/org/apache/karaf/tooling/exam/regression/supports/ServletActivator.java
URL: http://svn.apache.org/viewvc/karaf/trunk/tooling/exam/regression/src/test/java/org/apache/karaf/tooling/exam/regression/supports/ServletActivator.java?rev=1339500&view=auto
==============================================================================
--- karaf/trunk/tooling/exam/regression/src/test/java/org/apache/karaf/tooling/exam/regression/supports/ServletActivator.java (added)
+++ karaf/trunk/tooling/exam/regression/src/test/java/org/apache/karaf/tooling/exam/regression/supports/ServletActivator.java Thu May 17 07:10:49 2012
@@ -0,0 +1,82 @@
+/*
+ * 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.
+ */
+package org.apache.karaf.tooling.exam.regression.supports;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.http.HttpContext;
+import org.osgi.service.http.HttpService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ServletActivator implements BundleActivator {
+
+ private static final transient Logger LOG = LoggerFactory.getLogger(ServletActivator.class);
+
+ private static boolean registerService;
+
+ /**
+ * HttpService reference.
+ */
+ private ServiceReference httpServiceRef;
+
+ /**
+ * Called when the OSGi framework starts our bundle
+ */
+ @Override
+ public void start(BundleContext bc) throws Exception {
+ registerServlet(bc);
+ }
+
+ /**
+ * Called when the OSGi framework stops our bundle
+ */
+ @Override
+ public void stop(BundleContext bc) throws Exception {
+ if (httpServiceRef != null) {
+ bc.ungetService(httpServiceRef);
+ httpServiceRef = null;
+ }
+ }
+
+ protected void registerServlet(BundleContext bundleContext) throws Exception {
+ httpServiceRef = bundleContext.getServiceReference(HttpService.class.getName());
+
+ if (httpServiceRef != null && !registerService) {
+ LOG.info("Register the servlet service");
+ final HttpService httpService = (HttpService) bundleContext.getService(httpServiceRef);
+ if (httpService != null) {
+ // create a default context to share between registrations
+ final HttpContext httpContext = httpService.createDefaultHttpContext();
+ // register the hello world servlet
+ final Dictionary<String, String> initParams = new Hashtable<String, String>();
+ initParams.put("servlet-name", "TestServlet");
+ httpService.registerServlet(
+ "/test/services", // alias
+ new MyServlet(), // register servlet
+ initParams, // init params
+ httpContext // http context
+ );
+ registerService = true;
+ }
+ }
+ }
+}