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