1 package com.atlassian.pageobjects.elements.mock;
2
3 import com.atlassian.pageobjects.elements.query.AbstractTimedQuery;
4 import com.atlassian.pageobjects.elements.query.ExpirationHandler;
5 import com.atlassian.pageobjects.elements.query.util.Clock;
6 import org.slf4j.LoggerFactory;
7
8 import java.util.Arrays;
9 import java.util.LinkedList;
10
11
12
13
14
15 public class MockTimedQuery<T> extends AbstractTimedQuery<T>
16 {
17 private static final org.slf4j.Logger log = LoggerFactory.getLogger(MockTimedQuery.class);
18
19 private LinkedList<T> returnValues;
20 private boolean returnLast = true;
21 private boolean returnNull = false;
22 private boolean returnAll = false;
23
24 private int currentEval = 0;
25
26 public MockTimedQuery(long defTimeout, long interval, ExpirationHandler expirationHandler)
27 {
28 super(defTimeout, interval, expirationHandler);
29 }
30
31 public MockTimedQuery(Clock clock, long defTimeout, long interval, ExpirationHandler expirationHandler)
32 {
33 super(clock, defTimeout, interval, expirationHandler);
34 }
35
36
37 public MockTimedQuery<T> returnValues(T... returnValues)
38 {
39 this.returnValues = new LinkedList<T>(Arrays.asList(returnValues));
40 return this;
41 }
42
43 public MockTimedQuery<T> returnNull()
44 {
45 this.returnNull = true;
46 this.returnLast = false;
47 this.returnAll = false;
48 return this;
49 }
50
51 public MockTimedQuery<T> returnLast()
52 {
53 this.returnLast = true;
54 this.returnNull = false;
55 this.returnAll = false;
56 return this;
57 }
58
59 public MockTimedQuery<T> returnAll()
60 {
61 this.returnAll = true;
62 this.returnNull = false;
63 this.returnLast = false;
64 return this;
65 }
66
67 @Override
68 protected boolean shouldReturn(T currentEval)
69 {
70 if (returnNull && currentEval == null)
71 {
72 return true;
73 }
74 if (returnLast && returnValues.getLast().equals(currentEval))
75 {
76 return true;
77 }
78 if (returnAll)
79 {
80 return true;
81 }
82 return false;
83 }
84
85 @Override
86 protected T currentValue()
87 {
88 log.debug("#currentValue: return values=" + returnValues + ",currentEval=" + currentEval);
89 if (returnNull)
90 {
91 return null;
92 }
93 return returnValues.get(currentEval++);
94 }
95 }