/ui-tests/src/test/java/com/atlassian/upm/test/UpmUiTestRunner.java

https://bitbucket.org/atlassian/atlassian-universal-plugin-manager · Java · 121 lines · 101 code · 20 blank · 0 comment · 1 complexity · e77116e7646bf9d198fae7b2f29d5aeb MD5 · raw file

  1. package com.atlassian.upm.test;
  2. import com.atlassian.integrationtesting.ApplicationPropertiesImpl;
  3. import com.atlassian.integrationtesting.runner.ApplicationLogDelimiter;
  4. import com.atlassian.integrationtesting.runner.CompositeTestRunner;
  5. import com.atlassian.integrationtesting.runner.TestGroupRunner;
  6. import com.atlassian.integrationtesting.ui.RunningTestGroup;
  7. import com.atlassian.pageobjects.TestedProduct;
  8. import com.atlassian.pageobjects.TestedProductFactory;
  9. import com.atlassian.sal.api.ApplicationProperties;
  10. import com.atlassian.webdriver.confluence.ConfluenceTestedProduct;
  11. import com.atlassian.webdriver.jira.JiraTestedProduct;
  12. import com.atlassian.webdriver.pageobjects.WebDriverTester;
  13. import com.atlassian.webdriver.refapp.RefappTestedProduct;
  14. import com.google.common.base.Function;
  15. import com.google.common.collect.ImmutableList;
  16. import com.google.inject.AbstractModule;
  17. import com.google.inject.ConfigurationException;
  18. import com.google.inject.Guice;
  19. import com.google.inject.Injector;
  20. import com.google.inject.Provides;
  21. import com.google.inject.Singleton;
  22. import com.google.inject.spi.Message;
  23. import org.junit.runners.model.InitializationError;
  24. import static org.apache.commons.lang.StringUtils.isBlank;
  25. public final class UpmUiTestRunner extends CompositeTestRunner
  26. {
  27. public UpmUiTestRunner(Class<?> klass) throws InitializationError
  28. {
  29. super(klass, compose());
  30. }
  31. public static Composer compose()
  32. {
  33. Injector injector = Guice.createInjector(new UpmUiModule());
  34. return CompositeTestRunner.compose().
  35. from(TestGroupRunner.compose()).
  36. from(ApplicationLogDelimiter.compose(injector)).
  37. beforeTestClass(new InjectStatics(injector));
  38. }
  39. static final class UpmUiModule extends AbstractModule
  40. {
  41. @Override
  42. protected void configure() {}
  43. @Provides @Singleton @RunningTestGroup String getRunningTestGroup()
  44. {
  45. if (isBlank(TestGroupRunner.getRunningTestGroup()))
  46. {
  47. throw new ConfigurationException(ImmutableList.of(new Message("No testGroup configured - can't figure out which UiTester to use without a test group in the form {application}-v{version}")));
  48. }
  49. return TestGroupRunner.getRunningTestGroup();
  50. }
  51. @Provides @Singleton ApplicationProperties getApplicationProperties()
  52. {
  53. return ApplicationPropertiesImpl.getStandardApplicationProperties();
  54. }
  55. @Provides @Singleton TestedProduct<WebDriverTester> getTestedProduct(@RunningTestGroup String runningTestGroup)
  56. {
  57. return Testers.valueOf(runningTestGroup.toUpperCase()).get();
  58. }
  59. enum Testers
  60. {
  61. REFAPP(RefappTestedProduct.class),
  62. JIRA(JiraTestedProduct.class),
  63. CONFLUENCE(ConfluenceTestedProduct.class);
  64. private final Class<? extends TestedProduct<WebDriverTester>> testedProductClass;
  65. private Testers(Class<? extends TestedProduct<WebDriverTester>> testedProductClass)
  66. {
  67. this.testedProductClass = testedProductClass;
  68. }
  69. public final TestedProduct<WebDriverTester> get()
  70. {
  71. return TestedProductFactory.create(testedProductClass);
  72. }
  73. }
  74. }
  75. private static final class InjectStatics implements Function<BeforeTestClass, Void>
  76. {
  77. private final Injector injector;
  78. public InjectStatics(Injector injector)
  79. {
  80. this.injector = injector;
  81. }
  82. public Void apply(BeforeTestClass test)
  83. {
  84. injector.createChildInjector(new StaticInjectionModule(test.testClass.getJavaClass()));
  85. return null;
  86. }
  87. private static final class StaticInjectionModule extends AbstractModule
  88. {
  89. private final Class<?> testClass;
  90. public StaticInjectionModule(Class<?> testClass)
  91. {
  92. this.testClass = testClass;
  93. }
  94. @Override
  95. protected void configure()
  96. {
  97. requestStaticInjection(testClass);
  98. }
  99. }
  100. }
  101. }