[jboss-svn-commits] JBoss Common SVN: r4673 - in arquillian/trunk/impl-base/src: main/java/org/jboss/arquillian/impl/handler and 2 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Sat Jul 3 06:11:47 EDT 2010
Author: aslak
Date: 2010-07-03 06:11:46 -0400 (Sat, 03 Jul 2010)
New Revision: 4673
Added:
arquillian/trunk/impl-base/src/main/java/org/jboss/arquillian/impl/ToolingDeploymentFormatter.java
arquillian/trunk/impl-base/src/main/java/org/jboss/arquillian/impl/handler/ArchiveDeploymentToolingExporter.java
arquillian/trunk/impl-base/src/test/java/org/jboss/arquillian/impl/ToolingDeploymentFormatterTestCase.java
arquillian/trunk/impl-base/src/test/java/org/jboss/arquillian/impl/handler/ArchiveDeploymentToolingExporterTestCase.java
Log:
ARQ-187 Added EventHandler for exporting the Archive under test as XML. Used by tooling to give a Deployment overview.
Added: arquillian/trunk/impl-base/src/main/java/org/jboss/arquillian/impl/ToolingDeploymentFormatter.java
===================================================================
--- arquillian/trunk/impl-base/src/main/java/org/jboss/arquillian/impl/ToolingDeploymentFormatter.java (rev 0)
+++ arquillian/trunk/impl-base/src/main/java/org/jboss/arquillian/impl/ToolingDeploymentFormatter.java 2010-07-03 10:11:46 UTC (rev 4673)
@@ -0,0 +1,162 @@
+/*
+ * 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;
+
+import java.io.File;
+import java.lang.reflect.Field;
+import java.net.URL;
+
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.ArchivePaths;
+import org.jboss.shrinkwrap.api.Asset;
+import org.jboss.shrinkwrap.api.Node;
+import org.jboss.shrinkwrap.api.formatter.Formatter;
+import org.jboss.shrinkwrap.impl.base.asset.ArchiveAsset;
+import org.jboss.shrinkwrap.impl.base.asset.ClassAsset;
+import org.jboss.shrinkwrap.impl.base.asset.ClassLoaderAsset;
+import org.jboss.shrinkwrap.impl.base.asset.FileAsset;
+import org.jboss.shrinkwrap.impl.base.asset.UrlAsset;
+
+/**
+ * ToolingDeploymentFormatter
+ *
+ * @author <a href="mailto:aslak at redhat.com">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class ToolingDeploymentFormatter implements Formatter
+{
+ private Class<?> testClass;
+
+ public ToolingDeploymentFormatter(Class<?> testClass)
+ {
+ Validate.notNull(testClass, "TestClass must be specified");
+ this.testClass = testClass;
+ }
+
+ public String format(Archive<?> archive) throws IllegalArgumentException
+ {
+ StringBuilder xml = new StringBuilder();
+
+ xml.append("<?xml version=\"1.0\"?>\n<deployment")
+ .append(" name=\"").append(archive.getName()).append("\"")
+ .append(" testclass=\"").append(testClass.getName()).append("\"")
+ .append(">\n");
+
+ formatNode(archive.get(ArchivePaths.root()), xml);
+
+ xml.append("</deployment>").append("\n");
+ return xml.toString();
+ }
+
+ public void formatNode(Node node, StringBuilder xml)
+ {
+ if(node.getAsset() != null)
+ {
+ String source = findResourceLocation(node.getAsset());
+
+ xml.append("\t<asset")
+ .append(" type=\"").append(node.getAsset().getClass().getSimpleName()).append("\"")
+ .append(" path=\"").append(node.getPath().get()).append("\"");
+ if(source != null)
+ {
+ xml.append(" source=\"").append(source).append("\"");
+ }
+
+ if(node.getAsset().getClass() == ArchiveAsset.class)
+ {
+ xml.append(">");
+ xml.append("\n");
+ formatNode(
+ ((ArchiveAsset)node.getAsset()).getArchive().get(ArchivePaths.root()),
+ xml);
+ xml.append("</asset>").append("\n");
+ }
+ else
+ {
+ xml.append("/>").append("\n");
+ }
+
+ }
+ else
+ {
+ xml.append("\t<asset type=\"Directory\" path=\"").append(node.getPath().get()).append("\"/>\n");
+ }
+ for(Node child : node.getChildren())
+ {
+ formatNode(child, xml);
+ }
+ }
+
+ private String findResourceLocation(Asset asset)
+ {
+ Class<?> assetClass = asset.getClass();
+
+ if(assetClass == FileAsset.class)
+ {
+ return getInternalFieldValue(File.class, "file", asset).getAbsolutePath();
+ }
+ if(assetClass == ClassAsset.class)
+ {
+ return getInternalFieldValue(Class.class, "clazz", asset).getName();
+ }
+ if(assetClass == UrlAsset.class)
+ {
+ return getInternalFieldValue(URL.class, "url", asset).toExternalForm();
+ }
+ if(assetClass == ClassLoaderAsset.class)
+ {
+ return getInternalFieldValue(String.class, "resourceName", asset);
+ }
+ return null;
+ }
+
+ @SuppressWarnings("unchecked")
+ private <T> T getInternalFieldValue(Class<T> type, String fieldName, Object obj)
+ {
+ try
+ {
+ Field field = obj.getClass().getDeclaredField(fieldName);
+ field.setAccessible(true);
+
+ return (T)field.get(obj);
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("Could not extract field " + fieldName + " on " + obj, e);
+ }
+ }
+
+// private static class Deployment
+// {
+//
+// }
+//
+// private static class Node
+// {
+//
+// }
+//
+// private static class Source
+// {
+//
+// }
+//
+// private static class Content
+// {
+//
+// }
+}
Added: arquillian/trunk/impl-base/src/main/java/org/jboss/arquillian/impl/handler/ArchiveDeploymentToolingExporter.java
===================================================================
--- arquillian/trunk/impl-base/src/main/java/org/jboss/arquillian/impl/handler/ArchiveDeploymentToolingExporter.java (rev 0)
+++ arquillian/trunk/impl-base/src/main/java/org/jboss/arquillian/impl/handler/ArchiveDeploymentToolingExporter.java 2010-07-03 10:11:46 UTC (rev 4673)
@@ -0,0 +1,94 @@
+/*
+ * 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 java.io.FileOutputStream;
+
+import org.jboss.arquillian.impl.ToolingDeploymentFormatter;
+import org.jboss.arquillian.spi.Context;
+import org.jboss.arquillian.spi.TestClass;
+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.impl.base.Validate;
+
+/**
+ * Handler that will export a XML version of the Deployed Archive.
+ *
+ * Used by tooling to show a view of the ShrinkWrap archive.
+ *
+ * @author <a href="mailto:aslak at redhat.com">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class ArchiveDeploymentToolingExporter implements EventHandler<ClassEvent>
+{
+
+ static final String ARQUILLIAN_TOOLING_DEPLOYMENT_FOLDER = "arquillian.tooling.deployment.folder";
+
+ /* (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
+ {
+ String deploymentOutputFolder = System.getProperty(ARQUILLIAN_TOOLING_DEPLOYMENT_FOLDER);
+ if(deploymentOutputFolder == null) // tooling not activated, nothing to do
+ {
+ return;
+ }
+ Archive<?> deployment = context.get(Archive.class); // deployment not in context?, nothing to do
+ if(deployment == null)
+ {
+ return;
+ }
+
+ TestClass testClass = event.getTestClass();
+ String deploymentContent = deployment.toString(new ToolingDeploymentFormatter(testClass.getJavaClass()));
+ writeOutToFile(
+ new File(deploymentOutputFolder + "/" + testClass.getName() + ".xml"),
+ deploymentContent);
+ }
+
+ protected void writeOutToFile(File target, String content)
+ {
+ Validate.notNull(target, "Target must be specified");
+ Validate.notNull(content, "Content must be specified");
+
+ FileOutputStream output = null;
+ try
+ {
+ output = new FileOutputStream(target);
+ output.write(content.getBytes());
+ output.close();
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("Could not write content to file", e);
+ }
+ finally
+ {
+ if(output != null)
+ {
+ try {output.close(); }
+ catch (Exception e)
+ {
+ throw new RuntimeException("Could not close output stream", e);
+ }
+ }
+ }
+ }
+}
Added: arquillian/trunk/impl-base/src/test/java/org/jboss/arquillian/impl/ToolingDeploymentFormatterTestCase.java
===================================================================
--- arquillian/trunk/impl-base/src/test/java/org/jboss/arquillian/impl/ToolingDeploymentFormatterTestCase.java (rev 0)
+++ arquillian/trunk/impl-base/src/test/java/org/jboss/arquillian/impl/ToolingDeploymentFormatterTestCase.java 2010-07-03 10:11:46 UTC (rev 4673)
@@ -0,0 +1,58 @@
+/*
+ * 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;
+
+import java.io.File;
+
+import junit.framework.Assert;
+
+import org.jboss.arquillian.spi.ServiceLoader;
+import org.jboss.shrinkwrap.api.ArchivePaths;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.Test;
+
+/**
+ * ToolingDeploymentFormatterTestCase
+ *
+ * @author <a href="mailto:aslak at redhat.com">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class ToolingDeploymentFormatterTestCase
+{
+
+ @Test
+ public void shouldBeAbleToExportArchive() throws Exception
+ {
+ String content = ShrinkWrap.create("test.jar", WebArchive.class)
+ .addResource(new File("src/test/resources/arquillian.xml"), ArchivePaths.create("resource.xml"))
+ .addResource("arquillian.xml", ArchivePaths.create("resource2.xml"))
+ .addResource(new File("src/test/resources/arquillian.xml").toURI().toURL(), ArchivePaths.create("resource3.xml"))
+ .addClass(ToolingDeploymentFormatterTestCase.class)
+ .addServiceProvider(ServiceLoader.class, DynamicServiceLoader.class)
+ .addLibrary(
+ ShrinkWrap.create("test.jar", JavaArchive.class)
+ .addClass(ToolingDeploymentFormatter.class)
+ )
+ .toString(new ToolingDeploymentFormatter(getClass()));
+
+ // TODO: do some output Assertions..
+ Assert.assertNotNull(content);
+ System.out.println(content);
+ }
+}
Added: arquillian/trunk/impl-base/src/test/java/org/jboss/arquillian/impl/handler/ArchiveDeploymentToolingExporterTestCase.java
===================================================================
--- arquillian/trunk/impl-base/src/test/java/org/jboss/arquillian/impl/handler/ArchiveDeploymentToolingExporterTestCase.java (rev 0)
+++ arquillian/trunk/impl-base/src/test/java/org/jboss/arquillian/impl/handler/ArchiveDeploymentToolingExporterTestCase.java 2010-07-03 10:11:46 UTC (rev 4673)
@@ -0,0 +1,63 @@
+/*
+ * 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.impl.context.ClassContext;
+import org.jboss.arquillian.impl.context.SuiteContext;
+import org.jboss.arquillian.spi.ServiceLoader;
+import org.jboss.arquillian.spi.event.suite.ClassEvent;
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+
+/**
+ * ArchiveDeploymentToolingExporterTestCase
+ *
+ * @author <a href="mailto:aslak at redhat.com">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+ at RunWith(MockitoJUnitRunner.class)
+public class ArchiveDeploymentToolingExporterTestCase
+{
+ private static final String EXPORT_FOLDER = "target/";
+
+ @Mock
+ private ServiceLoader serviceLoader;
+
+ @Test
+ public void shouldThrowIllegalStateExceptionOnMissingDeploymentGenerator() throws Exception
+ {
+ System.setProperty(ArchiveDeploymentToolingExporter.ARQUILLIAN_TOOLING_DEPLOYMENT_FOLDER, EXPORT_FOLDER);
+
+ ClassContext context = new ClassContext(new SuiteContext(serviceLoader));
+ context.add(Archive.class, ShrinkWrap.create("test.jar", JavaArchive.class));
+
+ ArchiveDeploymentToolingExporter handler = new ArchiveDeploymentToolingExporter();
+ handler.callback(context, new ClassEvent(getClass()));
+
+ Assert.assertTrue(new File(EXPORT_FOLDER + getClass().getName() + ".xml").exists());
+ }
+
+}
More information about the jboss-svn-commits
mailing list