|
| 1 | +package io.quarkus.arc.arquillian; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | + |
| 6 | +import jakarta.enterprise.event.Shutdown; |
| 7 | +import jakarta.enterprise.event.Startup; |
| 8 | + |
| 9 | +import org.jboss.arquillian.container.spi.client.container.DeployableContainer; |
| 10 | +import org.jboss.arquillian.container.spi.client.container.DeploymentException; |
| 11 | +import org.jboss.arquillian.container.spi.client.protocol.ProtocolDescription; |
| 12 | +import org.jboss.arquillian.container.spi.client.protocol.metadata.ProtocolMetaData; |
| 13 | +import org.jboss.arquillian.container.spi.context.annotation.DeploymentScoped; |
| 14 | +import org.jboss.arquillian.core.api.Instance; |
| 15 | +import org.jboss.arquillian.core.api.InstanceProducer; |
| 16 | +import org.jboss.arquillian.core.api.annotation.Inject; |
| 17 | +import org.jboss.arquillian.test.spi.TestClass; |
| 18 | +import org.jboss.shrinkwrap.api.Archive; |
| 19 | +import org.jboss.shrinkwrap.api.exporter.ZipExporter; |
| 20 | +import org.jboss.shrinkwrap.descriptor.api.Descriptor; |
| 21 | + |
| 22 | +import io.quarkus.arc.Arc; |
| 23 | +import io.quarkus.arc.ArcContainer; |
| 24 | +import io.quarkus.arc.InjectableInstance; |
| 25 | +import io.quarkus.arc.InstanceHandle; |
| 26 | +import io.quarkus.arc.arquillian.utils.ClassLoading; |
| 27 | +import io.quarkus.arc.arquillian.utils.Directories; |
| 28 | + |
| 29 | +public class ArcDeployableContainer implements DeployableContainer<ArcContainerConfiguration> { |
| 30 | + @Inject |
| 31 | + @DeploymentScoped |
| 32 | + private InstanceProducer<DeploymentDir> deploymentDir; |
| 33 | + |
| 34 | + @Inject |
| 35 | + @DeploymentScoped |
| 36 | + private InstanceProducer<DeploymentClassLoader> deploymentClassLoader; |
| 37 | + |
| 38 | + @Inject |
| 39 | + @DeploymentScoped |
| 40 | + private InstanceProducer<ArcContainer> runningArc; |
| 41 | + |
| 42 | + @Inject |
| 43 | + private Instance<TestClass> testClass; |
| 44 | + |
| 45 | + static Object testInstance; |
| 46 | + |
| 47 | + @Override |
| 48 | + public Class<ArcContainerConfiguration> getConfigurationClass() { |
| 49 | + return ArcContainerConfiguration.class; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void setup(ArcContainerConfiguration configuration) { |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public ProtocolDescription getDefaultProtocol() { |
| 58 | + return new ProtocolDescription("ArC"); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public ProtocolMetaData deploy(Archive<?> archive) throws DeploymentException { |
| 63 | + if (System.getProperty("saveArchive") != null) { |
| 64 | + File file = new File(archive.getName()); |
| 65 | + archive.as(ZipExporter.class).exportTo(file); |
| 66 | + System.out.println("Archive for test " + testClass.get().getName() + " saved in: " + file.getAbsolutePath()); |
| 67 | + } |
| 68 | + |
| 69 | + if (testClass.get() == null) { |
| 70 | + throw new IllegalStateException("Test class not available"); |
| 71 | + } |
| 72 | + String testClassName = testClass.get().getName(); |
| 73 | + |
| 74 | + ClassLoader old = Thread.currentThread().getContextClassLoader(); |
| 75 | + try { |
| 76 | + DeploymentDir deploymentDir = new DeploymentDir(); |
| 77 | + this.deploymentDir.set(deploymentDir); |
| 78 | + |
| 79 | + DeploymentClassLoader deploymentClassLoader = new Deployer(archive, deploymentDir, testClassName).deploy(); |
| 80 | + this.deploymentClassLoader.set(deploymentClassLoader); |
| 81 | + |
| 82 | + Thread.currentThread().setContextClassLoader(deploymentClassLoader); |
| 83 | + |
| 84 | + ArcContainer arcContainer = Arc.initialize(); |
| 85 | + runningArc.set(arcContainer); |
| 86 | + arcContainer.beanManager().getEvent().fire(new Startup()); |
| 87 | + |
| 88 | + Class<?> actualTestClass = Class.forName(testClassName, true, deploymentClassLoader); |
| 89 | + testInstance = findTest(arcContainer, actualTestClass); |
| 90 | + } catch (Throwable t) { |
| 91 | + // clone the exception into the correct class loader |
| 92 | + Throwable nt = ClassLoading.cloneExceptionIntoSystemCL(t); |
| 93 | + throw new DeploymentException("Unable to start ArC", nt); |
| 94 | + } finally { |
| 95 | + Thread.currentThread().setContextClassLoader(old); |
| 96 | + } |
| 97 | + |
| 98 | + return new ProtocolMetaData(); |
| 99 | + } |
| 100 | + |
| 101 | + private Object findTest(ArcContainer arc, Class<?> testClass) { |
| 102 | + InjectableInstance<?> instance = arc.select(testClass); |
| 103 | + if (instance.isResolvable()) { |
| 104 | + return instance.get(); |
| 105 | + } |
| 106 | + |
| 107 | + // fallback for generic test classes, whose set of bean types does not contain a `Class` |
| 108 | + // but a `ParameterizedType` instead |
| 109 | + for (InstanceHandle<Object> handle : arc.listAll(Object.class)) { |
| 110 | + if (testClass.equals(handle.getBean().getBeanClass())) { |
| 111 | + return handle.get(); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + throw new IllegalStateException("No bean: " + testClass); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void undeploy(Archive<?> archive) throws DeploymentException { |
| 120 | + ClassLoader old = Thread.currentThread().getContextClassLoader(); |
| 121 | + try { |
| 122 | + DeploymentClassLoader deploymentClassLoader = this.deploymentClassLoader.get(); |
| 123 | + |
| 124 | + ArcContainer arcContainer = runningArc.get(); |
| 125 | + if (arcContainer != null) { |
| 126 | + Thread.currentThread().setContextClassLoader(deploymentClassLoader); |
| 127 | + arcContainer.beanManager().getEvent().fire(new Shutdown()); |
| 128 | + Arc.shutdown(); |
| 129 | + } |
| 130 | + testInstance = null; |
| 131 | + |
| 132 | + try { |
| 133 | + deploymentClassLoader.close(); |
| 134 | + } catch (IOException e) { |
| 135 | + throw new DeploymentException("Failed to close deployment classloader", e); |
| 136 | + } |
| 137 | + |
| 138 | + DeploymentDir deploymentDir = this.deploymentDir.get(); |
| 139 | + if (deploymentDir != null) { |
| 140 | + if (System.getProperty("retainDeployment") == null) { |
| 141 | + Directories.deleteDirectory(deploymentDir.root); |
| 142 | + } else { |
| 143 | + System.out.println("Deployment for test " + testClass.get().getName() |
| 144 | + + " retained in: " + deploymentDir.root); |
| 145 | + } |
| 146 | + } |
| 147 | + } finally { |
| 148 | + Thread.currentThread().setContextClassLoader(old); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public void deploy(Descriptor descriptor) { |
| 154 | + throw new UnsupportedOperationException(); |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public void undeploy(Descriptor descriptor) { |
| 160 | + throw new UnsupportedOperationException(); |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public void start() { |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public void stop() { |
| 170 | + } |
| 171 | +} |
0 commit comments