Skip to content
This repository has been archived by the owner on Jun 7, 2021. It is now read-only.

Commit

Permalink
Support @ArquillianResource URLs [SWARM-200]
Browse files Browse the repository at this point in the history
This only works if the user is using the default bind address, port, and
context path, or override them via `jboss.bind.address`,
`jboss.http.port`, and `wildfly.swarm.context.path`,
respectively. Overriding them via code won't affect the injected URL.
  • Loading branch information
tobias committed Dec 16, 2015
1 parent a063a52 commit fe60ac2
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 19 deletions.
@@ -0,0 +1,48 @@
/*
* Copyright 2015 Red Hat, Inc, and 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.wildfly.swarm.arquillian.adapter;

import org.jboss.arquillian.container.test.impl.RemoteExtensionLoader;
import org.jboss.arquillian.container.test.spi.RemoteLoadableExtension;
import org.jboss.arquillian.container.test.spi.client.deployment.CachedAuxilliaryArchiveAppender;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ExtensionLoader;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;

public class WildFlySwarmDeploymentAppender extends CachedAuxilliaryArchiveAppender {
@Override
protected Archive<?> buildArchive() {
return ShrinkWrap.create(JavaArchive.class)
.addPackages(
true,
// "org.jboss.arquillian.core",
// "org.jboss.arquillian.container.spi",
// "org.jboss.arquillian.container.impl",
// "org.jboss.arquillian.container.test.api",
"org.jboss.arquillian.container.test.spi",
"org.wildfly.swarm.arquillian.resources"
// "org.jboss.arquillian.container.test.impl",
// "org.jboss.arquillian.config",
// "org.jboss.arquillian.test",
// "org.jboss.shrinkwrap.api",
// "org.jboss.shrinkwrap.descriptor.api"
)
.addClass(WildFlySwarmRemoteExtension.class)
.addAsServiceProvider(RemoteLoadableExtension.class, WildFlySwarmRemoteExtension.class)
.addAsServiceProvider(ExtensionLoader.class, RemoteExtensionLoader.class);
}
}
Expand Up @@ -16,9 +16,15 @@
package org.wildfly.swarm.arquillian.adapter;

import org.jboss.arquillian.container.spi.client.container.DeployableContainer;
import org.jboss.arquillian.container.test.impl.enricher.resource.URIResourceProvider;
import org.jboss.arquillian.container.test.impl.enricher.resource.URLResourceProvider;
import org.jboss.arquillian.container.test.spi.client.deployment.AuxiliaryArchiveAppender;
import org.jboss.arquillian.container.test.spi.client.protocol.Protocol;
import org.jboss.arquillian.core.spi.LoadableExtension;
import org.jboss.arquillian.test.spi.enricher.resource.ResourceProvider;
import org.wildfly.swarm.arquillian.daemon.protocol.DaemonProtocol;
import org.wildfly.swarm.arquillian.resources.SwarmURIResourceProvider;
import org.wildfly.swarm.arquillian.resources.SwarmURLResourceProvider;

/**
* @author Bob McWhirter
Expand All @@ -27,7 +33,10 @@ public class WildFlySwarmExtension implements LoadableExtension {
@Override
public void register(ExtensionBuilder builder) {
builder.service(Protocol.class, DaemonProtocol.class)
.service(AuxiliaryArchiveAppender.class, WildFlySwarmDeploymentAppender.class)
.service(DeployableContainer.class, WildFlySwarmContainer.class)
.override(ResourceProvider.class, URLResourceProvider.class, SwarmURLResourceProvider.class)
.override(ResourceProvider.class, URIResourceProvider.class, SwarmURIResourceProvider.class)
.observer(WildFlySwarmObserver.class);
}
}
@@ -0,0 +1,31 @@
/**
* Copyright 2015 Red Hat, Inc, and 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.wildfly.swarm.arquillian.adapter;

import org.jboss.arquillian.container.test.impl.enricher.resource.URIResourceProvider;
import org.jboss.arquillian.container.test.impl.enricher.resource.URLResourceProvider;
import org.jboss.arquillian.container.test.spi.RemoteLoadableExtension;
import org.jboss.arquillian.test.spi.enricher.resource.ResourceProvider;
import org.wildfly.swarm.arquillian.resources.SwarmURIResourceProvider;
import org.wildfly.swarm.arquillian.resources.SwarmURLResourceProvider;

public class WildFlySwarmRemoteExtension implements RemoteLoadableExtension {
@Override
public void register(ExtensionBuilder builder) {
builder.override(ResourceProvider.class, URLResourceProvider.class, SwarmURLResourceProvider.class)
.override(ResourceProvider.class, URIResourceProvider.class, SwarmURIResourceProvider.class);
}
}
@@ -0,0 +1,43 @@
/*
* Copyright 2015 Red Hat, Inc, and 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.wildfly.swarm.arquillian.resources;

import org.jboss.arquillian.test.api.ArquillianResource;

import java.lang.annotation.Annotation;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class SwarmURIResourceProvider extends SwarmURLResourceProvider {
@Override
public Object lookup(ArquillianResource resource, Annotation... qualifiers) {
Object object = super.lookup(resource, qualifiers);
if (object == null) {
return null;
}
try {
return ((URL) object).toURI();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}

@Override
public boolean canProvide(Class<?> type) {
return URI.class.isAssignableFrom(type);
}
}
@@ -0,0 +1,60 @@
/**
* Copyright 2015 Red Hat, Inc, and 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.wildfly.swarm.arquillian.resources;

import org.jboss.arquillian.container.test.impl.enricher.resource.OperatesOnDeploymentAwareProvider;
import org.jboss.arquillian.test.api.ArquillianResource;

import java.lang.annotation.Annotation;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class SwarmURLResourceProvider extends OperatesOnDeploymentAwareProvider {
@Override
public boolean canProvide(Class<?> type) {
return URL.class.isAssignableFrom(type);
}

@Override
public Object doLookup(ArquillianResource resource, Annotation... __) {

// first cut - try to get the data from the sysprops
// this will fail if the user sets any of these via code
String host = System.getProperty("jboss.bind.address");
if (host == null || host.equals("0.0.0.0")) {
host = "localhost";
}

int port = 8080;
final String portString = System.getProperty("jboss.http.port");
if (portString != null) {
port = Integer.parseInt(portString);
}

String contextPath = System.getProperty("wildfly.swarm.context.path");
if (contextPath == null) {
contextPath = "/";
}

try {
return new URI("http", null, host, port, contextPath, null, null).toURL();
} catch (MalformedURLException | URISyntaxException e) {
throw new RuntimeException(e);
}
}
}
Expand Up @@ -16,33 +16,30 @@
*/
package org.wildfly.swarm.arquillian.daemon.protocol;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import org.jboss.arquillian.container.spi.client.deployment.Validate;
import org.jboss.arquillian.container.test.spi.TestDeployment;
import org.jboss.arquillian.container.test.spi.client.deployment.DeploymentPackager;
import org.jboss.arquillian.container.test.spi.client.deployment.ProtocolArchiveProcessor;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.Filter;
import org.jboss.shrinkwrap.api.Node;
import org.jboss.shrinkwrap.api.asset.Asset;
import org.jboss.shrinkwrap.api.container.LibraryContainer;
import org.jboss.shrinkwrap.api.container.WebContainer;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.impl.base.asset.ServiceProviderAsset;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
* {@link DeploymentPackager} to merge auxiliar archive contents with the archive provided by the user
*
Expand Down
Expand Up @@ -16,27 +16,65 @@
package org.wildfly.swarm.arquillian;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wildfly.swarm.container.JARArchive;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

@RunWith(Arquillian.class)
public class ArquillianRemoteTest {
public class ArquillianTest {
@ArquillianResource
URL url;

@ArquillianResource
URI uri;

static final URI EXPECTED_URI;
static final URL EXPECTED_URL;

static {
try {
EXPECTED_URI = new URI("http://127.0.0.1:8080/");
EXPECTED_URL = EXPECTED_URI.toURL();
} catch (MalformedURLException | URISyntaxException e) {
throw new RuntimeException(e);
}
}

@Deployment
public static Archive createDeployment() {
return ShrinkWrap.create(JARArchive.class)
.addModule("progress")
.addClass(ArquillianRemoteTest.class);
.addClass(ArquillianTest.class);
}

@Test
public void testBasic() throws Exception {
@RunAsClient
public void testOutside() throws Exception{
// confirm the resource injectors work
assertEquals(EXPECTED_URL, url);
assertEquals(EXPECTED_URI, uri);
}

@Test
public void testInside() throws Exception {
// confirm we can load a custom module from a custom repo
assertNotNull(ArquillianRemoteTest.class.getClassLoader().getResource("progress/bar.clj"));
assertNotNull(ArquillianTest.class.getClassLoader().getResource("progress/bar.clj"));

// confirm the resource injectors work
assertEquals(EXPECTED_URL, url);
assertEquals(EXPECTED_URI, uri);
}
}

0 comments on commit fe60ac2

Please sign in to comment.