/hudson-rest/hudson-rest-client/src/test/java/org/hudsonci/rest/client/ClientTestSupport.java

https://github.com/andyhorng/hudson · Java · 132 lines · 80 code · 23 blank · 29 comment · 5 complexity · df21fd02e82dcad9ee1524d97f5fd5f8 MD5 · raw file

  1. /**
  2. * The MIT License
  3. *
  4. * Copyright (c) 2010-2011 Sonatype, Inc. All rights reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package org.hudsonci.rest.client;
  25. import com.google.inject.Binder;
  26. import com.google.inject.Guice;
  27. import com.google.inject.Injector;
  28. import com.google.inject.Module;
  29. import com.google.inject.Stage;
  30. import org.hudsonci.rest.client.HudsonClient;
  31. import org.hudsonci.rest.client.OpenOptions;
  32. import org.hudsonci.rest.client.internal.HudsonClientModule;
  33. import org.junit.After;
  34. import org.junit.Before;
  35. import org.slf4j.Logger;
  36. import org.slf4j.LoggerFactory;
  37. import org.sonatype.guice.bean.binders.WireModule;
  38. import org.sonatype.guice.bean.locators.DefaultBeanLocator;
  39. import org.sonatype.guice.bean.locators.MutableBeanLocator;
  40. import java.net.URI;
  41. import java.net.URISyntaxException;
  42. import java.util.ArrayList;
  43. import java.util.List;
  44. import static org.junit.Assert.*;
  45. /**
  46. * Support for client tests.
  47. *
  48. * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
  49. * @since 2.1.0
  50. */
  51. public abstract class ClientTestSupport
  52. {
  53. protected final Logger log = LoggerFactory.getLogger(getClass());
  54. private DefaultBeanLocator container;
  55. private HudsonClient client;
  56. public HudsonClient getClient() {
  57. return client;
  58. }
  59. @Before
  60. public void setUp() throws Exception {
  61. container = new DefaultBeanLocator();
  62. log.debug("Container: {}", container);
  63. client = createClient();
  64. log.debug("Client: {}", client);
  65. }
  66. protected HudsonClient createClient() {
  67. List<Module> modules = new ArrayList<Module>();
  68. modules.add(new Module()
  69. {
  70. public void configure(Binder binder) {
  71. binder.bind(MutableBeanLocator.class).toInstance(container);
  72. }
  73. });
  74. modules.add(new HudsonClientModule());
  75. configureModules(modules);
  76. Injector injector = Guice.createInjector(Stage.DEVELOPMENT, new WireModule(modules));
  77. container.add(injector, 0);
  78. return injector.getInstance(HudsonClient.class);
  79. }
  80. protected void configureModules(final List<Module> modules) {
  81. assert modules != null;
  82. }
  83. @After
  84. public void tearDown() throws Exception {
  85. if (client != null) {
  86. client.close();
  87. client = null;
  88. }
  89. if (container != null) {
  90. container.clear();
  91. container = null;
  92. }
  93. }
  94. protected URI getServerUri() throws URISyntaxException {
  95. String uri = System.getProperty("hudson.uri", "http://localhost:8080");
  96. return new URI(uri);
  97. }
  98. protected OpenOptions getOpenOptions() {
  99. return new OpenOptions()
  100. .setRetries(OpenOptions.UNLIMITED_RETRIES)
  101. .setTimeout(5 * 60); // timeout after 5 minutes
  102. }
  103. protected void open() throws Exception {
  104. HudsonClient client = getClient();
  105. assertFalse(getClient().isOpen());
  106. client.open(getServerUri(), getOpenOptions());
  107. assertTrue(getClient().isOpen());
  108. }
  109. }