View Javadoc

1   package com.atlassian.pageobjects.util;
2   
3   import com.atlassian.pageobjects.TestedProduct;
4   import com.atlassian.pageobjects.inject.ConfigurableInjectionContext;
5   import com.atlassian.pageobjects.inject.InjectionContext;
6   
7   /**
8    * Manipulating and verifying {@link com.atlassian.pageobjects.TestedProduct}s abilities to inject dependencies.
9    *
10   * @since 2.1
11   */
12  public final class InjectingTestedProducts
13  {
14  
15      private InjectingTestedProducts()
16      {
17          throw new AssertionError("Don't instantiate me");
18      }
19  
20  
21      /**
22       * Checks whether given product supports injection. That is whether the product itself, or the associated page
23       * binder is instance of {@link com.atlassian.pageobjects.inject.InjectionContext}.
24       *
25       * @param product product to verify
26       * @return <code>true</code> if <tt>product</tt> supports injection
27       */
28      public static boolean supportsInjection(TestedProduct<?> product)
29      {
30          if (product == null)
31          {
32              return false;
33          }
34          return product instanceof InjectionContext || product.getPageBinder() instanceof InjectionContext;
35      }
36  
37      /**
38       * Checks whether given product supports injection. That is whether the product itself, or the associated page
39       * binder is instance of {@link com.atlassian.pageobjects.inject.ConfigurableInjectionContext}.
40       *
41       * @param product product to verify
42       * @return <code>true</code> if <tt>product</tt> supports configurable injection
43       */
44      public static boolean supportsConfigurableInjection(TestedProduct<?> product)
45      {
46          if (product == null)
47          {
48              return false;
49          }
50          return product instanceof ConfigurableInjectionContext || product.getPageBinder() instanceof ConfigurableInjectionContext;
51      }
52  
53      /**
54       * Returns injection context associated with given <tt>product</tt>.
55       *
56       * @param product product to retrieve context from
57       * @return associated injection context, either from the product, or its page binder
58       * @throws IllegalArgumentException if the product is not providing injection (which can be verified by
59       * calling {@link #supportsInjection(com.atlassian.pageobjects.TestedProduct)}
60       */
61      public static InjectionContext asInjectionContext(TestedProduct<?> product)
62      {
63          if (product instanceof InjectionContext)
64          {
65              return (InjectionContext) product;
66          }
67          else if (product.getPageBinder() instanceof InjectionContext)
68          {
69              return (InjectionContext) product.getPageBinder();
70          }
71          else
72          {
73              throw new IllegalArgumentException("Product <" + product + "> does not support injection");
74          }
75      }
76  
77  
78      /**
79       * Returns configurable injection context associated with given <tt>product</tt>.
80       *
81       * @param product product to retrieve context from
82       * @return associated injection context, either from the product, or its page binder
83       * @throws IllegalArgumentException if the product is not providing configurable injection (which can be verified by
84       * calling {@link #supportsConfigurableInjection(com.atlassian.pageobjects.TestedProduct)}.
85       */
86      public static ConfigurableInjectionContext asConfigurableInjectionContext(TestedProduct<?> product)
87      {
88          if (product instanceof ConfigurableInjectionContext)
89          {
90              return (ConfigurableInjectionContext) product;
91          }
92          else if (product.getPageBinder() instanceof InjectionContext)
93          {
94              return (ConfigurableInjectionContext) product.getPageBinder();
95          }
96          else
97          {
98              throw new IllegalArgumentException("Product <" + product + "> does not support configurable injection");
99          }
100     }
101 }