1   package com.atlassian.pageobjects.elements.test.timeout;
2   
3   import com.atlassian.pageobjects.elements.timeout.PropertiesBasedTimeouts;
4   import com.atlassian.pageobjects.elements.timeout.TimeoutType;
5   import com.google.common.collect.ImmutableMap;
6   import org.junit.Test;
7   
8   import java.util.Map;
9   import java.util.Properties;
10  
11  import static org.junit.Assert.assertEquals;
12  
13  /**
14   * Test case for {@link com.atlassian.pageobjects.elements.timeout.PropertiesBasedTimeouts}.
15   *
16   */
17  public class TestPropertiesBasedTimeouts
18  {
19  
20      @Test
21      public void shouldReturnExistingProperties()
22      {
23          Properties input = fromMap(ImmutableMap.of(
24                  "com.atlassian.timeout.DEFAULT", "500",
25                  "com.atlassian.timeout.UI_ACTION", "300",
26                  "com.atlassian.timeout.PAGE_LOAD", "400",
27                  "com.atlassian.timeout.WRONG_KEY", "700")
28          );
29          final PropertiesBasedTimeouts tested = new PropertiesBasedTimeouts(input);
30          assertEquals(500L, tested.timeoutFor(TimeoutType.DEFAULT));
31          assertEquals(300L, tested.timeoutFor(TimeoutType.UI_ACTION));
32          assertEquals(400L, tested.timeoutFor(TimeoutType.PAGE_LOAD));
33          // default for interval is hardcoded
34          assertEquals(100L, tested.timeoutFor(TimeoutType.EVALUATION_INTERVAL));
35          // the rest should be default
36          assertEquals(500L, tested.timeoutFor(TimeoutType.SLOW_PAGE_LOAD));
37          assertEquals(500L, tested.timeoutFor(TimeoutType.DIALOG_LOAD));
38          assertEquals(500L, tested.timeoutFor(TimeoutType.COMPONENT_LOAD));
39          assertEquals(500L, tested.timeoutFor(TimeoutType.AJAX_ACTION));
40      }
41  
42      @Test(expected = IllegalArgumentException.class)
43      public void shouldThrowExceptionGivenNoDefaultTimeoutInProperties()
44      {
45          Properties withoutDefaultTimeout = fromMap(ImmutableMap.of(
46                  "com.atlassian.timeout.UI_ACTION", "300",
47                  "com.atlassian.timeout.PAGE_LOAD", "400",
48                  "com.atlassian.timeout.WRONG_KEY", "700")
49          );
50          new PropertiesBasedTimeouts(withoutDefaultTimeout);
51      }
52  
53      @Test(expected = IllegalArgumentException.class)
54      public void shouldThrowExceptionGivenDefaultTimeoutInPropertiesIsNaN()
55      {
56          Properties withDefaultTimeoutNaN = fromMap(ImmutableMap.of(
57                  "com.atlassian.timeout.DEFAULT", "not really a number, is it?",
58                  "com.atlassian.timeout.PAGE_LOAD", "400",
59                  "com.atlassian.timeout.WRONG_KEY", "700")
60          );
61          new PropertiesBasedTimeouts(withDefaultTimeoutNaN);
62      }
63  
64      @Test
65      public void shouldReturnDefaultValueForNaN()
66      {
67          Properties withANaN = fromMap(ImmutableMap.of(
68                  "com.atlassian.timeout.DEFAULT", "500",
69                  "com.atlassian.timeout.UI_ACTION", "oh no, it's a NaN!",
70                  "com.atlassian.timeout.PAGE_LOAD", "400",
71                  "com.atlassian.timeout.WRONG_KEY", "700")
72          );
73          final PropertiesBasedTimeouts tested = new PropertiesBasedTimeouts(withANaN);
74          assertEquals(500L, tested.timeoutFor(TimeoutType.DEFAULT));
75          assertEquals(500L, tested.timeoutFor(TimeoutType.UI_ACTION));
76      }
77  
78      private Properties fromMap(Map<String,String> input)
79      {
80          Properties answer = new Properties();
81          answer.putAll(input);
82          return answer;
83      }
84  
85  }