Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ARQ-1776] Create branch for OSGi containers compatible with OSGi 4.2 #15

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -88,7 +88,7 @@ public Class<?> loadTestClass(String className) throws ClassNotFoundException {

// Load the the test class from bundle that defines a Bundle-ClassPath
for (Bundle aux : bundles) {
String bundlecp = aux.getHeaders().get(Constants.BUNDLE_CLASSPATH);
String bundlecp = (String) aux.getHeaders().get(Constants.BUNDLE_CLASSPATH);
if (bundlecp != null) {
try {
return aux.loadClass(className);
Expand Down
5 changes: 5 additions & 0 deletions container/common/pom.xml
Expand Up @@ -41,6 +41,11 @@
<artifactId>org.osgi.core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.enterprise</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
Expand Up @@ -105,13 +105,13 @@ private void enhanceApplicationArchive(Archive<?> appArchive, TestClass testClas
continue;

if (key.equals(Constants.IMPORT_PACKAGE)) {
String[] imports = value.split(",");
String[] imports = splitWithComma(value);
builder.addImportPackages(imports);
continue;
}

if (key.equals(Constants.EXPORT_PACKAGE)) {
String[] exports = value.split(",");
String[] exports = splitWithComma(value);
builder.addExportPackages(exports);
continue;
}
Expand Down Expand Up @@ -160,4 +160,24 @@ private Manifest getBundleManifest(Archive<?> archive) {
}
}

private static String[] splitWithComma(String value) {
// Header clauses are split with comma but comma can also appear in version parameter or in a custom parameter for "Attribute Matching"
// e.g. Import-Package: org.jboss.arquillian.junit;version="[X.0.0,Y.0.0)";extra="A,B",...

// After each comma must be even number of double-quotes
return value.split(
"(?x) " + // Free-Spacing Mode
", " + // Split with comma
"(?= " + // Followed by
" (?: " + // Start a non-capture group
" [^\"]* " + // 0 or more non-quote characters
" \" " + // 1 quote
" [^\"]* " + // 0 or more non-quote characters
" \" " + // 1 quote
" )* " + // 0 or more repetition of non-capture group (multiple of 2 quotes will be even)
" [^\"]* " + // Finally 0 or more non-quotes
" $ " + // Till the end (This is necessary, else every comma will satisfy the condition)
") " // End look-ahead
);
}
}
Expand Up @@ -174,7 +174,7 @@ protected void awaitArquillianBundleActive(BundleContext syscontext, long timeou
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Bundle> bundleRef = new AtomicReference<Bundle>();
int states = Bundle.INSTALLED | Bundle.RESOLVED | Bundle.STARTING | Bundle.ACTIVE;
BundleTracker<Bundle> tracker = new BundleTracker<Bundle>(syscontext, states, null) {
BundleTracker tracker = new BundleTracker(syscontext, states, null) {
@Override
public Bundle addingBundle(Bundle bundle, BundleEvent event) {
if ("arquillian-osgi-bundle".equals(bundle.getSymbolicName())) {
Expand All @@ -186,7 +186,7 @@ public Bundle addingBundle(Bundle bundle, BundleEvent event) {
}

@Override
public void modifiedBundle(Bundle bundle, BundleEvent event, Bundle tracked) {
public void modifiedBundle(Bundle bundle, BundleEvent event, Object tracked) {
if (event != null && event.getType() == BundleEvent.STARTED) {
latch.countDown();
}
Expand All @@ -213,7 +213,7 @@ public void modifiedBundle(Bundle bundle, BundleEvent event, Bundle tracked) {
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void awaitBootstrapCompleteService(BundleContext syscontext, String serviceName, long timeout, TimeUnit unit) {
final CountDownLatch latch = new CountDownLatch(1);
ServiceTracker<?, ?> tracker = new ServiceTracker(syscontext, serviceName, null) {
ServiceTracker tracker = new ServiceTracker(syscontext, serviceName, null) {
@Override
public Object addingService(ServiceReference sref) {
Object service = super.addingService(sref);
Expand Down Expand Up @@ -289,10 +289,11 @@ public void undeploy(Archive<?> archive) throws DeploymentException {
try {
String location = archive.getName();
log.info("Uninstalling bundle: " + location);

Bundle bundle = syscontext.getBundle(location);
if (bundle != null && bundle.getState() != Bundle.UNINSTALLED) {
uninstallBundle(bundle);
for (Bundle aux : syscontext.getBundles()) {
if (aux.getLocation().equals(location) && aux.getState() != Bundle.UNINSTALLED) {
aux.uninstall();
break;
}
}
} catch (BundleException ex) {
log.warn("Cannot undeploy: " + archive, ex);
Expand Down
@@ -0,0 +1,79 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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.jboss.test.arquillian.container.osgi;

import static org.junit.Assert.assertTrue;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jboss.arquillian.container.osgi.AbstractOSGiApplicationArchiveProcessor;
import org.junit.Test;

/**
* Test {@link AbstractOSGiApplicationArchiveProcessor}
*
* @author mbasovni@redhat.com
* @since 19-May-2014
*/
public class AbstractOSGiApplicationArchiveProcessorTestCase {

@Test
public void splitWithComma() throws Exception {
Map<String, List<String>> map = new HashMap<String, List<String>>();
map.put(
"org.jboss.arquillian.junit",
Arrays.asList("org.jboss.arquillian.junit")
);
map.put(
"org.jboss.arquillian.junit,org.junit.runner,org.osgi.framework",
Arrays.asList("org.jboss.arquillian.junit", "org.junit.runner", "org.osgi.framework")
);
map.put(
"org.junit.runner;version=\"[0.0.0,5.0.0)\"",
Arrays.asList("org.junit.runner;version=\"[0.0.0,5.0.0)\"")
);
map.put(
"org.junit.runner;version=\"[0.0.0,5.0.0)\",org.jboss.arquillian.junit",
Arrays.asList("org.junit.runner;version=\"[0.0.0,5.0.0)\"", "org.jboss.arquillian.junit")
);
map.put(
"org.jboss.arquillian.junit;version=\"[0.0.0,3.0.0)\",org.junit.runner;version=\"[0.0.0,5.0.0)\"",
Arrays.asList("org.jboss.arquillian.junit;version=\"[0.0.0,3.0.0)\"", "org.junit.runner;version=\"[0.0.0,5.0.0)\"")
);
map.put(
"org.jboss.arquillian.junit;version=\"[0.0.0,3.0.0)\";extra=\"A,B\"",
Arrays.asList("org.jboss.arquillian.junit;version=\"[0.0.0,3.0.0)\";extra=\"A,B\"")
);
map.put(
"org.jboss.arquillian.junit;extra=\"A,B,C\",org.osgi.framework;version=\"[1.0.0,2.0.0)\"",
Arrays.asList("org.jboss.arquillian.junit;extra=\"A,B,C\"", "org.osgi.framework;version=\"[1.0.0,2.0.0)\"")
);
for (Map.Entry<String,List<String>> entry : map.entrySet()) {
assertTrue("Packages are not split correctly", splitWithComma(entry.getKey()).equals(entry.getValue()));
}
}

private static List<String> splitWithComma(String value) throws Exception{
Method method = AbstractOSGiApplicationArchiveProcessor.class.getDeclaredMethod("splitWithComma", String.class);
method.setAccessible(true);
return Arrays.asList((String [])method.invoke(null, value));
}
}
6 changes: 5 additions & 1 deletion container/equinox/embedded/pom.xml
Expand Up @@ -44,6 +44,10 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</dependency>

<!-- Provided Dependencies -->
<dependency>
Expand Down Expand Up @@ -88,7 +92,7 @@
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<test.archive.directory>${project.build.directory}/test-libs</test.archive.directory>
<log4j.configuration>${basedir}/src/test/resources/logging.properties</log4j.configuration>
<log4j.configuration>file://${basedir}/src/test/resources/logging.properties</log4j.configuration>
</systemPropertyVariables>
</configuration>
</plugin>
Expand Down
Expand Up @@ -42,12 +42,12 @@
@RunWith(Arquillian.class)
public class ArquillianDeployerTestCase {

private static final String GOOD_BUNDLE = "good-bundle";
private static final String BAD_BUNDLE = "bad-bundle";
private static final String GOOD_BUNDLE = "good-bundle.jar";
private static final String BAD_BUNDLE = "bad-bundle.jar";

@Deployment
public static JavaArchive create() {
return ShrinkWrap.create(JavaArchive.class, "deployer-tests");
return ShrinkWrap.create(JavaArchive.class, "deployer-tests.jar");
}

@ArquillianResource
Expand Down Expand Up @@ -147,7 +147,7 @@ public InputStream openStream() {
OSGiManifestBuilder builder = OSGiManifestBuilder.newInstance();
builder.addBundleSymbolicName(BAD_BUNDLE);
builder.addBundleManifestVersion(2);
builder.addImportPackage("org.acme.foo");
builder.addImportPackages("org.acme.foo");
return builder.openStream();
}
});
Expand Down
Expand Up @@ -41,13 +41,15 @@
@RunWith(Arquillian.class)
public class AutostartTestCase {

private static final String BUNDLE = "autostart-bundle.jar";

@ArquillianResource
Bundle bundle;

@Deployment
@StartLevelAware(autostart = true)
public static JavaArchive create() {
final JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "autostart-bundle");
final JavaArchive archive = ShrinkWrap.create(JavaArchive.class, BUNDLE);
archive.setManifest(new Asset() {
@Override
public InputStream openStream() {
Expand All @@ -64,7 +66,7 @@ public InputStream openStream() {
public void testStartLevel() throws Exception {

assertEquals("Bundle ACTIVE", Bundle.ACTIVE, bundle.getState());
assertEquals("autostart-bundle", bundle.getSymbolicName());
assertEquals(BUNDLE, bundle.getSymbolicName());

bundle.uninstall();
assertEquals("Bundle UNINSTALLED", Bundle.UNINSTALLED, bundle.getState());
Expand Down
Expand Up @@ -16,19 +16,6 @@
*/
package org.jboss.test.arquillian.container.equinox;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.eclipse.osgi.launch.Equinox;
import org.jboss.osgi.metadata.OSGiManifestBuilder;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.Asset;
Expand All @@ -40,13 +27,19 @@
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.eclipse.osgi.launch.Equinox;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.BundleReference;
import org.osgi.framework.FrameworkEvent;
import org.osgi.framework.FrameworkListener;
import org.osgi.framework.wiring.FrameworkWiring;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.concurrent.TimeoutException;

/**
* The arquillian-osgi-bundle loads test cases dynamically from the test archive.
Expand Down Expand Up @@ -84,7 +77,7 @@ public void setUp() throws Exception {
syscontext = framework.getBundleContext();
}

@Test
@Test @Ignore
public void testBundleContextInjection() throws Exception {

// The loader bundle has Dynamic-ImportPackage: *
Expand Down Expand Up @@ -115,28 +108,28 @@ public void testBundleContextInjection() throws Exception {
}

private void refreshBundle(Bundle bundle) throws TimeoutException {

final CountDownLatch latch = new CountDownLatch(1);
FrameworkListener listener = new FrameworkListener() {
@Override
public void frameworkEvent(FrameworkEvent event) {
if (event.getType() == FrameworkEvent.PACKAGES_REFRESHED) {
latch.countDown();
}
}
};

FrameworkWiring fwWiring = syscontext.getBundle().adapt(FrameworkWiring.class);
fwWiring.refreshBundles(Collections.singleton(bundle), listener);

// Wait for the refresh to complete
try {
if (!latch.await(10, TimeUnit.SECONDS)) {
throw new TimeoutException();
}
} catch (InterruptedException ex) {
// ignore
}
throw new UnsupportedOperationException("Bundle refreshing is not implemented");
// final CountDownLatch latch = new CountDownLatch(1);
// FrameworkListener listener = new FrameworkListener() {
// @Override
// public void frameworkEvent(FrameworkEvent event) {
// if (event.getType() == FrameworkEvent.PACKAGES_REFRESHED) {
// latch.countDown();
// }
// }
// };
//
// FrameworkWiring fwWiring = syscontext.getBundle().adapt(FrameworkWiring.class);
// fwWiring.refreshBundles(Collections.singleton(bundle), listener);
//
// // Wait for the refresh to complete
// try {
// if (!latch.await(10, TimeUnit.SECONDS)) {
// throw new TimeoutException();
// }
// } catch (InterruptedException ex) {
// // ignore
// }
}

private Bundle installBundle(JavaArchive archive) throws BundleException {
Expand All @@ -154,7 +147,7 @@ public InputStream openStream() {
OSGiManifestBuilder builder = OSGiManifestBuilder.newInstance();
builder.addBundleSymbolicName(archive.getName());
builder.addBundleManifestVersion(2);
builder.addDynamicImportPackage("*");
builder.addDynamicImportPackages("*");
return builder.openStream();
}
});
Expand Down