View Javadoc

1   package com.atlassian.pageobjects.binder;
2   
3   import com.atlassian.pageobjects.DelayedBinder;
4   import com.atlassian.pageobjects.PageBinder;
5   import com.atlassian.pageobjects.ProductInstance;
6   import com.atlassian.pageobjects.Tester;
7   import com.google.inject.Binder;
8   import com.google.inject.Module;
9   import org.junit.Before;
10  import org.junit.Test;
11  
12  import static org.junit.Assert.assertFalse;
13  import static org.junit.Assert.assertTrue;
14  import static org.mockito.Mockito.mock;
15  
16  /**
17   * Test case for {@link com.atlassian.pageobjects.DelayedBinder}.
18   */
19  public class TestDelayedBinder
20  {
21      private MyTestedProduct product;
22  
23      @Before
24      public void setUp() throws Exception
25      {
26          product = new MyTestedProduct(new SetTester());
27      }
28  
29      private InjectPageBinder createBinder()
30      {
31          return createBinder(null, null);
32      }
33      private InjectPageBinder createBinder(final Class<?> key, final Class impl)
34      {
35          return new InjectPageBinder(mock(ProductInstance.class), mock(Tester.class), new StandardModule(product),
36                  new Module()
37                  {
38                      public void configure(Binder binder)
39                      {
40                          if (key != null)
41                          {
42                              binder.bind(key).to(impl);
43                          }
44                      }
45                  });
46      }
47  
48      @Test
49      public void shouldAlwaysReturnFalseGivenWaitUntilFails()
50      {
51          final PageBinder binder = createBinder();
52          final DelayedBinder<PageObjectFail> delayedBinder = binder.delayedBind(PageObjectFail.class, 8);
53          for (int i=0; i<8; i++)
54          {
55              assertFalse(delayedBinder.canBind());
56          }
57          assertTrue(delayedBinder.canBind());
58      }
59  
60  
61      @Test
62      public void canBindMustReturnFalseIfInitFails()
63      {
64          final PageBinder binder = createBinder();
65          final DelayedBinder<PageObjectWithInitializeThrowingException> delayedBinder = binder.delayedBind(PageObjectWithInitializeThrowingException.class);
66          assertFalse(delayedBinder.canBind());
67      }
68  
69  
70      /**
71       * A typical failing page object.
72       *
73       */
74      static class PageObjectFail
75      {
76          private int numberOfFails = 0;
77  
78          public PageObjectFail(int numberOfFails)
79          {
80              this.numberOfFails = numberOfFails;
81          }
82  
83          @Init
84          protected void init()
85          {
86          }
87  
88          @WaitUntil
89          protected void failingNotWaiting()
90          {
91              if (numberOfFails > 0)
92              {
93                  numberOfFails--;
94                  throw new RuntimeException("FAIL!");
95              }
96          }
97  
98          @ValidateState
99          protected void goodValidate()
100         {
101         }
102     }
103 
104     static class PageObjectWithInitializeThrowingException
105     {
106         @Init
107         private void badStuff()
108         {
109             throw new RuntimeException("HAHAHAHAHA");
110         }
111     }
112 
113 
114 }