1   package com.atlassian.pageobjects.binder;
2   
3   import com.atlassian.pageobjects.Page;
4   import com.atlassian.pageobjects.PageBinder;
5   import com.atlassian.pageobjects.ProductInstance;
6   import com.atlassian.pageobjects.TestedProduct;
7   import com.atlassian.pageobjects.Tester;
8   import com.google.inject.Binder;
9   import com.google.inject.Module;
10  import junit.framework.Assert;
11  import org.junit.Before;
12  import org.junit.Test;
13  
14  import javax.inject.Inject;
15  
16  import java.util.Collection;
17  import java.util.Collections;
18  import java.util.Map;
19  
20  import static com.google.common.collect.Lists.asList;
21  import static java.util.Collections.singletonMap;
22  import static junit.framework.Assert.assertNotNull;
23  import static junit.framework.Assert.assertTrue;
24  import static org.junit.Assert.assertEquals;
25  import static org.mockito.Mockito.mock;
26  
27  /**
28   *
29   */
30  public class TestInjectPageBinder
31  {
32      private MyTestedProduct product;
33  
34      @Before
35      public void setUp() throws Exception
36      {
37          product = new MyTestedProduct(new SetTester());
38      }
39  
40      private InjectPageBinder createBinder()
41      {
42          return createBinder(null, null);
43      }
44      private InjectPageBinder createBinder(final Class key, final Class impl)
45      {
46          return new InjectPageBinder(mock(ProductInstance.class), mock(Tester.class), new StandardModule(product),
47                  new Module()
48                  {
49                      public void configure(Binder binder)
50                      {
51                          if (key != null)
52                          {
53                              binder.bind(key).to(impl);
54                          }
55                      }
56                  });
57      }
58  
59      @Test
60      public void testInject()
61      {
62          PageBinder binder = createBinder(StringField.class, StringFieldImpl.class);
63          OneFieldPage page = binder.bind(OneFieldPage.class);
64          assertEquals("Bob", page.name.getValue());
65      }
66  
67      @Test
68      public void testInjectDefaults()
69      {
70          PageBinder binder = createBinder();
71          DefaultsPage page = binder.bind(DefaultsPage.class);
72          assertNotNull(page.testedProduct);
73          assertNotNull(page.myTestedProduct);
74          assertNotNull(page.tester);
75          assertNotNull(page.setTester);
76          assertNotNull(page.pageBinder);
77          assertNotNull(page.productInstance);
78      }
79  
80      @Test(expected = IllegalArgumentException.class)
81      public void testInjectMissing()
82      {
83          PageBinder binder = createBinder();
84          OneFieldPage page = binder.bind(OneFieldPage.class);
85          int x = 0;
86      }
87  
88      @Test
89      public void testInjectWithArgument()
90      {
91          PageBinder binder = createBinder();
92          ConstructorArgumentPage page = binder.bind(ConstructorArgumentPage.class, "foo");
93          assertEquals("foo", page.name);
94      }
95  
96      @Test
97      public void testInstantiateWithPrimitiveArguments()
98      {
99          PageBinder binder = createBinder();
100         ConstructorArgumentPrimitive object = binder.bind(ConstructorArgumentPrimitive.class, 5, true);
101         assertNotNull(object);
102         assertEquals(5, object.intField);
103         assertTrue(object.booleanField);
104     }
105 
106     @Test
107     public void testInjectWithArgumentSubclass()
108     {
109         PageBinder binder = createBinder();
110         ConstructorArgumentPage page = binder.bind(ConstructorArgumentPage.class, 43);
111         assertEquals(43, page.age);
112     }
113 
114     @Test
115     public void testInitAfterInject()
116     {
117         PageBinder binder = createBinder(StringField.class, StringFieldImpl.class);
118         OneFieldWithInitPage page = binder.bind(OneFieldWithInitPage.class);
119         assertEquals("Bob Jones", page.name);
120     }
121 
122     @Test
123     public void testPrivateInitAfterInject()
124     {
125         PageBinder binder = createBinder(StringField.class, StringFieldImpl.class);
126         OneFieldWithPrivateInitPage page = binder.bind(OneFieldWithPrivateInitPage.class);
127         assertEquals("Bob Private", page.name);
128     }
129 
130     @Test
131     public void testOneFieldWithSuperClassInit()
132     {
133         PageBinder binder = createBinder(StringField.class, StringFieldImpl.class);
134         OneFieldWithSuperClassInitPage page = binder.bind(OneFieldWithSuperClassInitPage.class);
135         assertEquals("Bob Private", page.getName());
136 
137     }
138 
139     @Test
140     public void testProtectedInitAfterInject()
141     {
142         PageBinder binder = createBinder(StringField.class, StringFieldImpl.class);
143         OneFieldWithProtectedInitPage page = binder.bind(OneFieldWithProtectedInitPage.class);
144         assertEquals("Bob Protected", page.name);
145     }
146 
147     @Test
148     public void testParentInject()
149     {
150         PageBinder binder = createBinder(StringField.class, StringFieldImpl.class);
151         ChildNoNamePage page = binder.bind(ChildNoNamePage.class);
152         assertEquals("Bob", page.name.getValue());
153     }
154 
155 
156     static class AbstractPage implements Page
157     {
158         public String getUrl()
159         {
160             return null;
161         }
162     }
163 
164     static class OneFieldPage extends AbstractPage
165     {
166         @Inject
167         private StringField name;
168     }
169 
170     static class ConstructorArgumentPrimitive
171     {
172         private final int intField;
173         private final boolean booleanField;
174 
175         public ConstructorArgumentPrimitive(int intArg, boolean booleanArg)
176         {
177             this.intField = intArg;
178             this.booleanField = booleanArg;
179         }
180     }
181 
182 
183     static class ConstructorArgumentPage extends AbstractPage
184     {
185         private final String name;
186         private final Number age;
187 
188         public ConstructorArgumentPage(String name)
189         {
190             this.name = name;
191             this.age = null;
192         }
193 
194         public ConstructorArgumentPage(Number age)
195         {
196             this.age = age;
197             this.name = null;
198         }
199     }
200 
201     static class ParentNamePage extends AbstractPage
202     {
203         @Inject
204         protected StringField name;
205     }
206 
207     static class ChildNoNamePage extends ParentNamePage
208     {
209     }
210 
211     static class DefaultsPage extends AbstractPage
212     {
213         @Inject
214         private ProductInstance productInstance;
215 
216         @Inject
217         private TestedProduct testedProduct;
218 
219         @Inject
220         private MyTestedProduct myTestedProduct;
221 
222         @Inject
223         private Tester tester;
224 
225         @Inject
226         private SetTester setTester;
227 
228         @Inject
229         private PageBinder pageBinder;
230     }
231 
232     static class OneFieldWithInitPage extends AbstractPage
233     {
234         @Inject
235         private StringField field;
236 
237         private String name;
238         @Init
239         public void init()
240         {
241             name = field.getValue() + " Jones";
242         }
243     }
244 
245     static interface StringField
246     {
247         String getValue();
248     }
249 
250     static class StringFieldImpl implements StringField
251     {
252         public String getValue()
253         {
254             return "Bob";
255         }
256     }
257 
258     static class OneFieldWithPrivateInitPage extends AbstractPage
259     {
260         @Inject
261         private StringField field;
262 
263         private String name;
264 
265         @Init
266         private void init()
267         {
268             name = field.getValue() + " Private";
269         }
270 
271         public String getName()
272         {
273             return name;
274         }
275     }
276 
277     static class OneFieldWithProtectedInitPage extends AbstractPage
278     {
279         @Inject
280         private StringField field;
281 
282         private String name;
283 
284         @Init
285         protected void init()
286         {
287             name = field.getValue() + " Protected";
288         }
289     }
290 
291     static class OneFieldWithSuperClassInitPage extends OneFieldWithPrivateInitPage
292     {
293     }
294 
295 
296 }