[jboss-svn-commits] JBoss Common SVN: r4403 - in arquillian/trunk: impl-base/src/main/java/org/jboss/arquillian/impl/context and 5 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu May 20 19:03:11 EDT 2010
Author: aslak
Date: 2010-05-20 19:03:10 -0400 (Thu, 20 May 2010)
New Revision: 4403
Added:
arquillian/trunk/impl-base/src/main/java/org/jboss/arquillian/impl/handler/ArchiveDeploymentExporter.java
arquillian/trunk/impl-base/src/test/java/org/jboss/arquillian/impl/handler/ArchiveDeploymentExporterTestCase.java
Modified:
arquillian/trunk/impl-base/src/main/java/org/jboss/arquillian/impl/XmlConfigurationBuilder.java
arquillian/trunk/impl-base/src/main/java/org/jboss/arquillian/impl/context/ClientProfileBuilder.java
arquillian/trunk/impl-base/src/test/java/org/jboss/arquillian/impl/XmlConfigurationBuilderTestCase.java
arquillian/trunk/impl-base/src/test/resources/arquillian.xml
arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi/Configuration.java
Log:
ARQ-140 ARQ-141 Added support for properties on Configuration object (arquillian.xml/engine). Added option to export the Archives being deployed to file.
Modified: arquillian/trunk/impl-base/src/main/java/org/jboss/arquillian/impl/XmlConfigurationBuilder.java
===================================================================
--- arquillian/trunk/impl-base/src/main/java/org/jboss/arquillian/impl/XmlConfigurationBuilder.java 2010-05-20 21:52:00 UTC (rev 4402)
+++ arquillian/trunk/impl-base/src/main/java/org/jboss/arquillian/impl/XmlConfigurationBuilder.java 2010-05-20 23:03:10 UTC (rev 4403)
@@ -41,6 +41,7 @@
* it just returns an empty {@link org.jboss.arquillian.spi.Configuration} object.
*
* @author <a href="mailto:german.escobarc at gmail.com">German Escobar</a>
+ * @author <a href="mailto:aslak at redhat.com">Aslak Knutsen</a>
* @version $Revision: $
*/
public class XmlConfigurationBuilder implements ConfigurationBuilder
@@ -89,6 +90,9 @@
this.serviceLoader = serviceLoader;
}
+ /* (non-Javadoc)
+ * @see org.jboss.arquillian.impl.ConfigurationBuilder#build()
+ */
public Configuration build() throws ConfigurationException
{
// the configuration object we are going to return
@@ -101,70 +105,104 @@
{
configuration.addContainerConfig(containerConfiguration);
}
-
+
try
{
+ Document arquillianConfiguration = loadArquillianConfiguration(resourcePath);
+ if(arquillianConfiguration != null)
+ {
+ populateConfiguration(arquillianConfiguration, containersConfigurations);
+ populateConfiguration(arquillianConfiguration, configuration);
+ }
+ }
+ catch (Exception e)
+ {
+ throw new ConfigurationException("Could not create configuration", e);
+ }
+ return configuration;
+ }
+
+ private Document loadArquillianConfiguration(String resourcePath) throws Exception
+ {
+ InputStream inputStream = null;
+ try
+ {
// load the xml configuration file
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
- InputStream inputStream = classLoader.getResourceAsStream(resourcePath);
+ inputStream = classLoader.getResourceAsStream(resourcePath);
if (inputStream != null)
{
log.info("building configuration from XML file: " + resourcePath);
- Document document = getDocument(inputStream);
-
- // load all the container nodes
- NodeList nodeList = document.getDocumentElement().getElementsByTagNameNS("*", "container");
- for (int i=0; i < nodeList.getLength(); i++)
- {
- Node containerNode = nodeList.item(i);
-
- // retrieve the package
- String pkg = containerNode.getNamespaceURI().replaceFirst("urn:arq:", "");
-
- // try to find a ContainerConfiguration that matches the package
- ContainerConfiguration containerConfig = matchContainerConfiguration(containersConfigurations, pkg);
-
- if (containerConfig != null)
- {
- // map the nodes
- mapNodesToProperties(containerConfig, containerNode);
-
- // add the ContainerConfiguration to the configuration
- }
- }
+ return getDocument(inputStream);
}
- else
+ else
{
log.fine("No " + resourcePath + " file found");
}
+ }
+ finally
+ {
+ if(inputStream != null)
+ {
+ try { inputStream.close(); } catch (Exception e) { /* NO-OP */ }
+ }
}
- catch (Exception e)
+ return null;
+ }
+
+ private void populateConfiguration(Document xmlDocument, Collection<ContainerConfiguration> containersConfigurations) throws Exception
+ {
+ // load all the container nodes
+ NodeList nodeList = xmlDocument.getDocumentElement().getElementsByTagNameNS("*", "container");
+ for (int i=0; i < nodeList.getLength(); i++)
{
- throw new ConfigurationException(e.getMessage(), e);
+ Node containerNode = nodeList.item(i);
+
+ // retrieve the package
+ String pkg = containerNode.getNamespaceURI().replaceFirst("urn:arq:", "");
+
+ // try to find a ContainerConfiguration that matches the package
+ ContainerConfiguration containerConfig = matchContainerConfiguration(containersConfigurations, pkg);
+
+ if (containerConfig != null)
+ {
+ // map the nodes
+ mapNodesToProperties(containerConfig, containerNode);
+ }
}
- return configuration;
}
+ private void populateConfiguration(Document xmlDocument, Configuration configuration) throws Exception
+ {
+ // try to map all child nodes
+ NodeList nodeList = xmlDocument.getDocumentElement().getElementsByTagNameNS("*", "engine");
+ for (int i=0; i < nodeList.getLength(); i++)
+ {
+ Node node = nodeList.item(i);
+ mapNodesToProperties(configuration, node);
+ }
+ }
+
/**
- * Fills the properties of the ContainerConfiguration implementation object with the
- * information from the container XML fragment (the containerNode argument).
- * @param containerConfig the ContainerConfiguration object to be filled from the XML fragment
- * @param containerNode the XML node that represents the container configuration.
- * @throws Exception if there is a problem filling the ContainerConfiguration object.
+ * Fills the properties of the Configuration implementation object with the
+ * information from the XML fragment.
+ * @param configurationObject the object to be filled from the XML fragment
+ * @param xmlNode the XML node that represents the configuration.
+ * @throws Exception if there is a problem filling the object.
*/
- private void mapNodesToProperties(ContainerConfiguration containerConfig, Node containerNode) throws Exception
+ private void mapNodesToProperties(Object configurationObject, Node xmlNode) throws Exception
{
// validation
- Validate.notNull(containerConfig, "No container configuration specified");
- Validate.notNull(containerNode, "No container XML Node specified");
+ Validate.notNull(configurationObject, "No ConfigurationObject specified");
+ Validate.notNull(xmlNode, "No XML Node specified");
- log.fine("filling container configuration for class: " + containerConfig.getClass().getName());
+ log.fine("filling container configuration for class: " + configurationObject.getClass().getName());
- // here we will store the properties taken from the child elements of the container node
+ // here we will store the properties taken from the child elements of the node
Map<String,String> properties = new HashMap<String,String>();
- NodeList childNodes = containerNode.getChildNodes();
+ NodeList childNodes = xmlNode.getChildNodes();
for (int i=0; i < childNodes.getLength(); i++)
{
Node child = childNodes.item(i);
@@ -176,19 +214,19 @@
}
}
- // set the properties found in the container XML fragment to the ContainerConfiguration
+ // set the properties found in the container XML fragment to the Configuration Object
for (Map.Entry<String, String> property : properties.entrySet())
{
- Field field = containerConfig.getClass().getDeclaredField(property.getKey());
+ Field field = configurationObject.getClass().getDeclaredField(property.getKey());
field.setAccessible(true);
Object value = convert(field.getType(), property.getValue());
- field.set(containerConfig, value);
+ field.set(configurationObject, value);
}
}
/**
* Creates all the properties from a single Node element. The element must be a child of the
- * container root element.
+ * 'section' root element.
* @param element the XML Node from which we are going to create the properties.
* @return a Map of properties names and values mapped from the XML Node element.
*/
Modified: arquillian/trunk/impl-base/src/main/java/org/jboss/arquillian/impl/context/ClientProfileBuilder.java
===================================================================
--- arquillian/trunk/impl-base/src/main/java/org/jboss/arquillian/impl/context/ClientProfileBuilder.java 2010-05-20 21:52:00 UTC (rev 4402)
+++ arquillian/trunk/impl-base/src/main/java/org/jboss/arquillian/impl/context/ClientProfileBuilder.java 2010-05-20 23:03:10 UTC (rev 4403)
@@ -16,8 +16,9 @@
*/
package org.jboss.arquillian.impl.context;
+import org.jboss.arquillian.impl.handler.ActivateRunModeTypeDeployment;
import org.jboss.arquillian.impl.handler.ActivateRunModeTypeLocal;
-import org.jboss.arquillian.impl.handler.ActivateRunModeTypeDeployment;
+import org.jboss.arquillian.impl.handler.ArchiveDeploymentExporter;
import org.jboss.arquillian.impl.handler.ArchiveGenerator;
import org.jboss.arquillian.impl.handler.ContainerCreator;
import org.jboss.arquillian.impl.handler.ContainerDeployer;
@@ -80,6 +81,8 @@
context.register(AfterClass.class, new ContainerUndeployer());
context.register(BeforeClass.class, new ActivateRunModeTypeLocal());
+
+ context.register(BeforeClass.class, new ArchiveDeploymentExporter());
}
/* (non-Javadoc)
Added: arquillian/trunk/impl-base/src/main/java/org/jboss/arquillian/impl/handler/ArchiveDeploymentExporter.java
===================================================================
--- arquillian/trunk/impl-base/src/main/java/org/jboss/arquillian/impl/handler/ArchiveDeploymentExporter.java (rev 0)
+++ arquillian/trunk/impl-base/src/main/java/org/jboss/arquillian/impl/handler/ArchiveDeploymentExporter.java 2010-05-20 23:03:10 UTC (rev 4403)
@@ -0,0 +1,52 @@
+/*
+ * 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.arquillian.impl.handler;
+
+import java.io.File;
+
+import org.jboss.arquillian.spi.Configuration;
+import org.jboss.arquillian.spi.Context;
+import org.jboss.arquillian.spi.event.suite.ClassEvent;
+import org.jboss.arquillian.spi.event.suite.EventHandler;
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.exporter.ZipExporter;
+
+/**
+ * Handler that will export the generated {@link Archive} to the file system. <br/>
+ * Used for debugging the deployment.
+ *
+ * @author <a href="mailto:aslak at redhat.com">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class ArchiveDeploymentExporter implements EventHandler<ClassEvent>
+{
+ /* (non-Javadoc)
+ * @see org.jboss.arquillian.spi.event.suite.EventHandler#callback(org.jboss.arquillian.spi.Context, java.lang.Object)
+ */
+ public void callback(Context context, ClassEvent event) throws Exception
+ {
+ Archive<?> deployment = context.get(Archive.class);
+ Configuration configuration = context.get(Configuration.class);
+
+ if(deployment != null && (configuration != null && configuration.getDeploymentExportPath() != null))
+ {
+ deployment.as(ZipExporter.class).exportZip(
+ new File(configuration.getDeploymentExportPath() + deployment.getName()),
+ true);
+ }
+ }
+}
Modified: arquillian/trunk/impl-base/src/test/java/org/jboss/arquillian/impl/XmlConfigurationBuilderTestCase.java
===================================================================
--- arquillian/trunk/impl-base/src/test/java/org/jboss/arquillian/impl/XmlConfigurationBuilderTestCase.java 2010-05-20 21:52:00 UTC (rev 4402)
+++ arquillian/trunk/impl-base/src/test/java/org/jboss/arquillian/impl/XmlConfigurationBuilderTestCase.java 2010-05-20 23:03:10 UTC (rev 4403)
@@ -31,6 +31,7 @@
* XmlConfigurationBuilderTestCase
*
* @author <a href="mailto:german.escobarc at gmail.com">German Escobar</a>
+ * @author <a href="mailto:aslak at redhat.com">Aslak Knutsen</a>
* @version $Revision: $
*/
public class XmlConfigurationBuilderTestCase
@@ -63,6 +64,11 @@
ConfigurationBuilder builder = new XmlConfigurationBuilder("arquillian.xml", serviceLoader);
Configuration configuration = builder.build();
Assert.assertNotNull(configuration);
+
+ Assert.assertEquals(
+ "Should set properties on " + Configuration.class.getName(),
+ "/tmp",
+ configuration.getDeploymentExportPath());
// retrieve the container configuration
MockContainerConfiguration containerConfig = configuration.getContainerConfig(MockContainerConfiguration.class);
@@ -74,6 +80,7 @@
Assert.assertEquals(2L, containerConfig.getPropertyLong());
Assert.assertEquals(3D, containerConfig.getPropertyDouble(), 0);
Assert.assertEquals(true, containerConfig.getPropertyBoolean());
+
}
/**
Added: arquillian/trunk/impl-base/src/test/java/org/jboss/arquillian/impl/handler/ArchiveDeploymentExporterTestCase.java
===================================================================
--- arquillian/trunk/impl-base/src/test/java/org/jboss/arquillian/impl/handler/ArchiveDeploymentExporterTestCase.java (rev 0)
+++ arquillian/trunk/impl-base/src/test/java/org/jboss/arquillian/impl/handler/ArchiveDeploymentExporterTestCase.java 2010-05-20 23:03:10 UTC (rev 4403)
@@ -0,0 +1,115 @@
+/*
+ * 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.arquillian.impl.handler;
+
+import java.io.File;
+
+import junit.framework.Assert;
+
+import org.jboss.arquillian.impl.context.ClassContext;
+import org.jboss.arquillian.impl.context.SuiteContext;
+import org.jboss.arquillian.spi.Configuration;
+import org.jboss.arquillian.spi.ServiceLoader;
+import org.jboss.arquillian.spi.event.suite.BeforeClass;
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+
+/**
+ * Verify the behavior of the ArchiveDeploymentExporter
+ *
+ * @author <a href="mailto:aslak at redhat.com">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+ at RunWith(MockitoJUnitRunner.class)
+public class ArchiveDeploymentExporterTestCase
+{
+ private static final String ARCHIVE_NAME = "test.jar";
+ private static final String EXPORT_PATH = "target/";
+
+ @Mock
+ private ServiceLoader serviceLoader;
+
+ @Test
+ public void shouldHandleNoArchiveInContext() throws Exception
+ {
+ ClassContext context = new ClassContext(new SuiteContext(serviceLoader));
+
+ ArchiveDeploymentExporter handler = new ArchiveDeploymentExporter();
+ handler.callback(context, new BeforeClass(getClass()));
+
+ fileShouldExist(false);
+ }
+
+ @Test
+ public void shouldHandleNoConfigurationInContext() throws Exception
+ {
+ ClassContext context = new ClassContext(new SuiteContext(serviceLoader));
+
+ context.add(Archive.class, ShrinkWrap.create(ARCHIVE_NAME, JavaArchive.class).addClass(getClass()));
+
+ ArchiveDeploymentExporter handler = new ArchiveDeploymentExporter();
+ handler.callback(context, new BeforeClass(getClass()));
+
+ fileShouldExist(false);
+ }
+
+ @Test
+ public void shouldNotExportIfDeploymentExportPathNotSet() throws Exception
+ {
+ ClassContext context = new ClassContext(new SuiteContext(serviceLoader));
+
+ context.add(Archive.class, ShrinkWrap.create(ARCHIVE_NAME, JavaArchive.class).addClass(getClass()));
+ context.add(Configuration.class, new Configuration());
+
+ ArchiveDeploymentExporter handler = new ArchiveDeploymentExporter();
+ handler.callback(context, new BeforeClass(getClass()));
+
+ fileShouldExist(false);
+ }
+
+ @Test
+ public void shouldBeExportedWhenDeploymentExportPathIsSet() throws Exception
+ {
+ Configuration configuration = new Configuration();
+ configuration.setDeploymentExportPath(EXPORT_PATH);
+
+ ClassContext context = new ClassContext(new SuiteContext(serviceLoader));
+
+ context.add(Archive.class, ShrinkWrap.create(ARCHIVE_NAME, JavaArchive.class).addClass(getClass()));
+ context.add(Configuration.class, configuration);
+
+ ArchiveDeploymentExporter handler = new ArchiveDeploymentExporter();
+ handler.callback(context, new BeforeClass(getClass()));
+
+ fileShouldExist(true);
+ }
+
+ private void fileShouldExist(boolean bol)
+ {
+ File file = new File(EXPORT_PATH + ARCHIVE_NAME);
+
+ Assert.assertEquals("File exists", bol, file.exists());
+
+ file.delete();
+ }
+}
Modified: arquillian/trunk/impl-base/src/test/resources/arquillian.xml
===================================================================
--- arquillian/trunk/impl-base/src/test/resources/arquillian.xml 2010-05-20 21:52:00 UTC (rev 4402)
+++ arquillian/trunk/impl-base/src/test/resources/arquillian.xml 2010-05-20 23:03:10 UTC (rev 4403)
@@ -3,7 +3,12 @@
<arquillian xmlns="http://jboss.com/arquillian"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mock="urn:arq:org.jboss.arquillian.impl">
+
+ <engine>
+ <deploymentExportPath>/tmp</deploymentExportPath>
+ </engine>
+
<mock:container>
<mock:property string="hola" long="2" />
<mock:propertyInt>1</mock:propertyInt>
Modified: arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi/Configuration.java
===================================================================
--- arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi/Configuration.java 2010-05-20 21:52:00 UTC (rev 4402)
+++ arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi/Configuration.java 2010-05-20 23:03:10 UTC (rev 4403)
@@ -37,6 +37,8 @@
private Map<Class<? extends ContainerConfiguration>, ContainerConfiguration> containersConfig =
new HashMap<Class<? extends ContainerConfiguration>, ContainerConfiguration>();
+ private String deploymentExportPath = null;
+
/**
* Puts a {@link ContainerConfiguration} implementation in the containersConfig
* field. If the {@link ContainerConfiguration} already exists, it just replaces
@@ -77,4 +79,23 @@
}
return null;
}
+
+ /**
+ * Sets the Path used to export deployments.
+ *
+ * @param deploymentExportPath String representation of path to use to export archives
+ */
+ public void setDeploymentExportPath(String deploymentExportPath)
+ {
+ this.deploymentExportPath = deploymentExportPath;
+ }
+
+ /**
+ * Get the set export path for deployments.
+ * @return Set path or null if not set
+ */
+ public String getDeploymentExportPath()
+ {
+ return deploymentExportPath;
+ }
}
More information about the jboss-svn-commits
mailing list