View Javadoc

1   package com.atlassian.webdriver.testing.runner;
2   
3   import com.atlassian.pageobjects.TestedProduct;
4   import com.atlassian.pageobjects.TestedProductFactory;
5   import com.atlassian.pageobjects.util.InjectingTestedProducts;
6   import com.atlassian.util.concurrent.LazyReference;
7   import com.atlassian.webdriver.testing.annotation.TestedProductClass;
8   import org.junit.runners.model.InitializationError;
9   
10  /**
11   * <p/>
12   * 'Default' product context runner that reads the tested product class from the
13   * {@link com.atlassian.webdriver.testing.annotation.TestedProductClass} annotation that the test class
14   * MUST be annotated with, and uses {@link com.atlassian.pageobjects.TestedProductFactory} to instantiate
15   * the product and inject context into the test instance.
16   *
17   * @since 2.1
18   */
19  public class ProductContextRunner extends AbstractProductContextRunner
20  {
21  
22      private final Class<? extends TestedProduct<?>> productClass;
23      private final LazyReference<TestedProduct<?>> product;
24  
25      /**
26       * Constructor compatible with the underlying default JUnit4 runner.
27       *
28       * @throws org.junit.runners.model.InitializationError
29       *          if the test class is malformed.
30       */
31      public ProductContextRunner(Class<?> klass) throws InitializationError
32      {
33          super(klass);
34          this.productClass = validateAndGetProductClass(klass);
35          this.product = new LazyReference<TestedProduct<?>>()
36          {
37              @Override
38              protected TestedProduct<?> create() throws Exception
39              {
40                  final TestedProduct<?> theProduct = createProduct(productClass);
41                  if (!InjectingTestedProducts.supportsInjection(theProduct))
42                  {
43                      throw new AssertionError("TestedProduct instance " + theProduct.getClass().getName() + " does not "
44                              + "support injection");
45                  }
46                  return theProduct;
47              }
48          };
49  
50      }
51  
52      /**
53       * Override to implement custom factory method for tested product.
54       *
55       * @param testedProductClass tested product class
56       * @return tested product instance
57       */
58      protected TestedProduct<?> createProduct(Class<? extends TestedProduct<?>> testedProductClass)
59      {
60          return TestedProductFactory.create(testedProductClass);
61      }
62  
63      private Class<? extends TestedProduct<?>> validateAndGetProductClass(Class<?> testClass) throws InitializationError
64      {
65          final TestedProductClass testedProductClass = testClass.getAnnotation(TestedProductClass.class);
66          if (testedProductClass == null)
67          {
68              throw new InitializationError("Test class " + testClass.getName() + " is missing the "
69                      + TestedProductClass.class.getName() + " annotation");
70          }
71          return testedProductClass.value();
72  
73      }
74  
75      @Override
76      protected final TestedProduct<?> getProduct()
77      {
78          return product.get();
79      }
80  }