View Javadoc

1   package com.atlassian.pageobjects.elements.test.query;
2   
3   import com.atlassian.pageobjects.elements.mock.MockTimedQuery;
4   import com.atlassian.pageobjects.elements.mock.clock.StrictMockClock;
5   import com.atlassian.pageobjects.elements.query.ExpirationHandler;
6   import com.atlassian.pageobjects.elements.query.Poller;
7   import com.atlassian.pageobjects.elements.query.StaticQuery;
8   import com.atlassian.pageobjects.elements.query.TimedQuery;
9   import org.junit.Test;
10  
11  import java.util.ArrayList;
12  import java.util.List;
13  
14  import static com.atlassian.pageobjects.elements.query.Poller.by;
15  import static com.atlassian.pageobjects.elements.query.Poller.byDefaultTimeout;
16  import static com.atlassian.pageobjects.elements.query.Poller.now;
17  import static org.hamcrest.Matchers.equalTo;
18  import static org.hamcrest.Matchers.notNullValue;
19  import static org.hamcrest.Matchers.nullValue;
20  import static org.junit.Assert.assertEquals;
21  
22  /**
23   * Test case for {@link com.atlassian.pageobjects.elements.query.Poller} with various timed queries.
24   *
25   */
26  public class TestPollerQueryWaits
27  {
28      private static final int DEFAULT_INTERVAL = 100;
29  
30      @Test
31      public void queryEqualsAssertionShouldPassForPassingQuery()
32      {
33          Poller.waitUntil(queryFor("test"), equalTo("test"), now());
34          Poller.waitUntil(queryFor("test"), equalTo("test"), byDefaultTimeout());
35          Poller.waitUntil(queryFor("test"), equalTo("test"), by(500));
36      }
37  
38      @Test
39      public void queryNotNullAssertionShouldPassForNotNullQuery()
40      {
41          Poller.waitUntil(queryFor("test"), notNullValue(String.class), now());
42          Poller.waitUntil(queryFor("test"), notNullValue(String.class), byDefaultTimeout());
43          Poller.waitUntil(queryFor("test"), notNullValue(String.class), by(500));
44      }
45  
46      @Test
47      public void queryIsNullAssertionShouldPassForNullQuery()
48      {
49          Poller.waitUntil(nullQuery(), nullValue(String.class), now());
50          Poller.waitUntil(nullQuery(), nullValue(String.class), byDefaultTimeout());
51          Poller.waitUntil(nullQuery(), nullValue(String.class), by(500));
52      }
53  
54  
55      @Test
56      public void queryEqualsAssertionShouldFailForDifferentQueryResultNow()
57      {
58          try
59          {
60              Poller.waitUntil(queryFor("something"), equalTo("somethingelse"), now());
61              throw new IllegalStateException("Should fail");
62          }
63          catch(AssertionError e)
64          {
65              assertEquals("Query <com.atlassian.pageobjects.elements.query.StaticQuery[interval=100,defaultTimeout=500][value=something]>\n"
66                      + "Expected: \"somethingelse\" immediately\n"
67                      + "Got (last value): \"something\"",
68                      e.getMessage());
69          }
70      }
71  
72      @Test
73      public void queryEqualsAssertionShouldFailForDifferentQueryResultByDefaultTimeout()
74      {
75          try
76          {
77              Poller.waitUntil(forMultipleReturns("something", "somethingdifferent", "somethingmore").returnAll(),
78                      equalTo("somethingelse"), byDefaultTimeout());
79              throw new IllegalStateException("Should fail");
80          }
81          catch(AssertionError e)
82          {
83              assertEquals("Query <com.atlassian.pageobjects.elements.mock.MockTimedQuery[interval=100,defaultTimeout=200]>\n"
84                      + "Expected: \"somethingelse\" by 200ms (default timeout)\n"
85                      + "Got (last value): \"somethingmore\"",
86                      e.getMessage());
87          }
88      }
89  
90      @Test
91      public void queryEqualsAssertionShouldFailForDifferentQueryResultByCustomTimeout()
92      {
93          try
94          {
95              Poller.waitUntil(forMultipleReturns("something", "somethingdifferent", "somethingmore",
96                      "andnowforsomethingcompletelydifferent").returnAll(), equalTo("somethingelse"), by(100));
97              throw new IllegalStateException("Should fail");
98          }
99          catch(AssertionError e)
100         {
101             assertEquals("Query <com.atlassian.pageobjects.elements.mock.MockTimedQuery[interval=100,defaultTimeout=300]>\n"
102                     + "Expected: \"somethingelse\" by 100ms\n"
103                     + "Got (last value): \"somethingdifferent\"",
104                     e.getMessage());
105         }
106     }
107 
108     @Test
109     public void isNotNullAssertionShouldFailForNullQueryNow()
110     {
111         try
112         {
113             Poller.waitUntil(nullQuery(), notNullValue(String.class), now());
114             throw new IllegalStateException("Should fail");
115         }
116         catch(AssertionError e)
117         {
118             assertEquals("Query <com.atlassian.pageobjects.elements.mock.MockTimedQuery[interval=100,defaultTimeout=500]>\n"
119                     + "Expected: not null immediately\n"
120                     + "Got (last value): null",
121                     e.getMessage());
122         }
123     }
124 
125     @Test
126     public void isNotNullAssertionShouldFailForNullQueryByDefaultTimeout()
127     {
128         try
129         {
130             Poller.waitUntil(forMultipleReturns("one", "two", "three", "four", "five").returnNull(), notNullValue(String.class),
131                     byDefaultTimeout());
132             throw new IllegalStateException("Should fail");
133         }
134         catch(AssertionError e)
135         {
136             assertEquals("Query <com.atlassian.pageobjects.elements.mock.MockTimedQuery[interval=100,defaultTimeout=400]>\n"
137                     + "Expected: not null by 400ms (default timeout)\n"
138                     + "Got (last value): null",
139                     e.getMessage());
140         }
141     }
142 
143     @Test
144     public void isNotNullAssertionShouldFailForNullQueryByCustomTimeout()
145     {
146         try
147         {
148             Poller.waitUntil(forMultipleReturns("five", "six", "seven", "eight").returnLast(), notNullValue(String.class), by(200));
149             throw new IllegalStateException("Should fail");
150         }
151         catch(AssertionError e)
152         {
153             assertEquals("Query <com.atlassian.pageobjects.elements.mock.MockTimedQuery[interval=100,"
154                     + "defaultTimeout=300]>\n"
155                     + "Expected: not null by 200ms\n"
156                     + "Got (last value): null",
157                     e.getMessage());
158         }
159     }
160 
161     @Test
162     public void isNullAssertionShouldFailForNonNullQueryNow()
163     {
164         try
165         {
166             Poller.waitUntil(forMultipleReturns("one", "two", "three", "four").returnAll(), nullValue(String.class), now());
167             throw new IllegalStateException("Should fail");
168         }
169         catch(AssertionError e)
170         {
171             assertEquals("Query <com.atlassian.pageobjects.elements.mock.MockTimedQuery[interval=100,"
172                     + "defaultTimeout=300]>\n"
173                     + "Expected: null immediately\n"
174                     + "Got (last value): \"one\"",
175                     e.getMessage());
176         }
177     }
178 
179     @Test
180     public void isNullAssertionShouldFailForNonNullQueryByDefaultTimeout()
181     {
182         try
183         {
184             Poller.waitUntil(forMultipleReturns("one", "two", "three", "four", "five").returnAll(), nullValue(String.class),
185                     byDefaultTimeout());
186             throw new IllegalStateException("Should fail");
187         }
188         catch(AssertionError e)
189         {
190             assertEquals("Query <com.atlassian.pageobjects.elements.mock.MockTimedQuery[interval=100,"
191                     + "defaultTimeout=400]>\n"
192                     + "Expected: null by 400ms (default timeout)\n"
193                     + "Got (last value): \"five\"",
194                     e.getMessage());
195         }
196     }
197 
198     @Test
199     public void isNullAssertionShouldFailForNonNullQueryByCustomTimeout()
200     {
201         try
202         {
203             Poller.waitUntil(forMultipleReturns("five", "six", "seven", "eight").returnAll(), nullValue(String.class), by(200));
204             throw new IllegalStateException("Should fail");
205         }
206         catch(AssertionError e)
207         {
208             assertEquals("Query <com.atlassian.pageobjects.elements.mock.MockTimedQuery[interval=100,"
209                     + "defaultTimeout=300]>\n"
210                     + "Expected: null by 200ms\n"
211                     + "Got (last value): \"seven\"",
212                     e.getMessage());
213         }
214     }
215 
216 
217     private TimedQuery<String> queryFor(String value)
218     {
219         return new StaticQuery<String>(value, 500, DEFAULT_INTERVAL);
220     }
221 
222     private TimedQuery<String> nullQuery()
223     {
224         return new MockTimedQuery<String>(500, DEFAULT_INTERVAL, ExpirationHandler.RETURN_NULL).returnNull();
225     }
226 
227     private MockTimedQuery<String> forMultipleReturns(String... returns)
228     {
229         StrictMockClock clock = clockFor(returns.length);
230         return new MockTimedQuery<String>(clock, clock.last(), DEFAULT_INTERVAL, ExpirationHandler.RETURN_NULL)
231                 .returnValues(returns);
232     }
233 
234     private StrictMockClock clockFor(int returnSize)
235     {
236         final List<Long> times = new ArrayList<Long>(returnSize);
237         long time = 0;
238         times.add(time); // initial call before loop
239         for (int i=0; i<returnSize; i++)
240         {
241             // each loop in AbstractTimedQuery/Condition calls currentTime() 2 times
242             times.add(time);
243             times.add(time);
244             time += DEFAULT_INTERVAL;
245         }
246         return new StrictMockClock(times);
247     }
248 
249 
250 }