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