Last active
January 15, 2016 23:44
-
-
Save smiklosovic/efbf59bdddac725d9524 to your computer and use it in GitHub Desktop.
Arquillian Cube outside of Arquillian
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package docker.manager; | |
| import org.arquillian.cube.spi.Cube; | |
| import org.arquillian.cube.spi.CubeRegistry; | |
| import org.arquillian.cube.spi.event.CreateCube; | |
| import org.arquillian.cube.spi.event.DestroyCube; | |
| import org.arquillian.cube.spi.event.StartCube; | |
| import org.arquillian.cube.spi.event.StopCube; | |
| import org.jboss.arquillian.core.impl.loadable.LoadableExtensionLoader; | |
| import org.jboss.arquillian.core.spi.Manager; | |
| import org.jboss.arquillian.core.spi.ManagerBuilder; | |
| import org.jboss.arquillian.core.spi.Validate; | |
| /** | |
| * Usage: | |
| * | |
| * In your test case, e.g. in BeforeCLass, do this: | |
| * | |
| * DockerManager dockerManager = new DockerManager.Builder().build(); | |
| * | |
| * In test you can do just: | |
| * | |
| * Cube cube = dockerManager.start("cubeId"); | |
| * | |
| * dockerManager.stop("cubeId"); | |
| * | |
| * You can do it also like this: | |
| * | |
| * DockerManager dockerManager = new DockerManager.Builder().containerId("cubeId").build(); | |
| * | |
| * Cube cube = dockerManager.start(); | |
| * dockerManager.stop(); | |
| * | |
| * You have to have these dependencies in gradle: | |
| * | |
| * // Docker | |
| * compile "org.arquillian.cube:arquillian-cube-docker:1.0.0.Alpha7" | |
| * compile "org.jboss.arquillian.core:arquillian-core-impl-base:1.1.8.Final" | |
| * compile "org.jboss.arquillian.config:arquillian-config-impl-base:1.1.8.Final" | |
| * | |
| * Do not forget to add arquillian.xml into src/test/resources with Docker descriptor as you are used to. | |
| */ | |
| public class DockerManager { | |
| private final Manager manager; | |
| private final String defaultContainerId; | |
| private boolean isManagerStarted; | |
| private DockerManager(Builder builder) { | |
| this.manager = builder.manager; | |
| this.defaultContainerId = builder.containerId; | |
| } | |
| public Cube start() { | |
| return start(defaultContainerId); | |
| } | |
| public Cube start(final String containerId) { | |
| Validate.notNullOrEmpty(containerId, "Container ID can not be a null object nor an empty String!"); | |
| startManager(); | |
| manager.fire(new CreateCube(containerId)); | |
| manager.fire(new StartCube(containerId)); | |
| return manager.resolve(CubeRegistry.class).getCube(containerId); | |
| } | |
| public void stop() { | |
| stop(defaultContainerId); | |
| } | |
| public void stop(final String containerId) { | |
| Validate.notNull(containerId, "Container ID can not be a null object nor an empty String!"); | |
| manager.fire(new StopCube(containerId)); | |
| manager.fire(new DestroyCube(containerId)); | |
| stopManager(); | |
| } | |
| public void stop(final Cube container) { | |
| stop(container.getId()); | |
| } | |
| private void startManager() { | |
| if (!isManagerStarted) { | |
| manager.start(); | |
| isManagerStarted = true; | |
| } | |
| } | |
| private void stopManager() { | |
| if (isManagerStarted) { | |
| manager.shutdown(); | |
| } | |
| } | |
| public static class Builder { | |
| private Manager manager = createDefaultManager(); | |
| private String containerId; | |
| public Builder manager(final Manager manager) { | |
| Validate.notNull(manager, "Manager can not be a null object!"); | |
| this.manager = manager; | |
| return this; | |
| } | |
| public Builder containerId(final String containerId) { | |
| Validate.notNullOrEmpty(containerId, "Container ID can not be a null object!"); | |
| this.containerId = containerId; | |
| return this; | |
| } | |
| public DockerManager build() { | |
| return new DockerManager(this); | |
| } | |
| private Manager createDefaultManager() { | |
| return ManagerBuilder.from().extension(LoadableExtensionLoader.class).create(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment