1 package com.atlassian.scheduler.core.status;
2
3 import org.junit.Test;
4
5 import java.util.Date;
6
7 import static com.atlassian.scheduler.core.status.RunDetailsImpl.MAXIMUM_MESSAGE_LENGTH;
8 import static com.atlassian.scheduler.status.RunOutcome.ABORTED;
9 import static com.atlassian.scheduler.status.RunOutcome.FAILED;
10 import static com.atlassian.scheduler.status.RunOutcome.SUCCESS;
11 import static com.atlassian.util.concurrent.Assertions.notNull;
12 import static org.hamcrest.Matchers.is;
13 import static org.hamcrest.Matchers.startsWith;
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertThat;
17
18
19
20
21 @SuppressWarnings({"ResultOfObjectAllocationIgnored", "ConstantConditions"})
22 public class RunDetailsImplTest {
23 private static final Date NOW = new Date();
24
25 @Test(expected = IllegalArgumentException.class)
26 public void testLastRunTimeNull() {
27 new RunDetailsImpl(null, ABORTED, 0L, "Info");
28 }
29
30 @Test
31 public void testLastRunTimeIsDefensivelyCopied() {
32 final Date original = new Date();
33 final long originalTime = original.getTime();
34 final RunDetailsImpl js = new RunDetailsImpl(original, ABORTED, 0L, "Info");
35 original.setTime(42L);
36
37 Date copy = notNull("copy", js.getStartTime());
38 assertThat("Modifying the original after it is submitted does not change the value",
39 copy.getTime(), is(originalTime));
40
41 copy.setTime(42L);
42 copy = notNull("copy", js.getStartTime());
43 assertThat("Modifying the returned copy does not change the value",
44 copy.getTime(), is(originalTime));
45 }
46
47 @Test(expected = IllegalArgumentException.class)
48 public void testRunOutcomeNull() {
49 new RunDetailsImpl(NOW, null, 0L, "Info");
50 }
51
52 @Test
53 public void testDurationIsNegative() {
54 assertEquals("Negative durations should force to 0L", 0L, new RunDetailsImpl(NOW, ABORTED, -42L, "Info").getDurationInMillis());
55 }
56
57 @Test
58 public void testInfoIsNull() {
59 assertThat(new RunDetailsImpl(NOW, ABORTED, 0L, null).getMessage(), is(""));
60 }
61
62 @Test
63 public void testMessageIsMaximumLength() {
64 final String message = generate(MAXIMUM_MESSAGE_LENGTH);
65 final RunDetailsImpl js = new RunDetailsImpl(NOW, ABORTED, 0L, message);
66 assertThat(js.getMessage(), is(message));
67 }
68
69 @Test
70 public void testMessageExceedsMaximumLength() {
71 final String message = generate(MAXIMUM_MESSAGE_LENGTH + 42);
72 final RunDetailsImpl js = new RunDetailsImpl(NOW, ABORTED, 0L, message);
73 assertNotNull(js.getMessage());
74 assertThat(message, startsWith(js.getMessage()));
75 assertThat(js.getMessage().length(), is(MAXIMUM_MESSAGE_LENGTH));
76 }
77
78 @Test
79 public void testValues1() {
80 final Date later = new Date(NOW.getTime() + 42L);
81 final RunDetailsImpl js = new RunDetailsImpl(later, SUCCESS, 42L, "Info");
82 assertThat(js.getStartTime(), is(later));
83 assertThat(js.getRunOutcome(), is(SUCCESS));
84 assertThat(js.getDurationInMillis(), is(42L));
85 assertThat(js.getMessage(), is("Info"));
86 }
87
88 @Test
89 public void testValues2() {
90 final Date later = new Date(NOW.getTime() + 42L);
91 final RunDetailsImpl js = new RunDetailsImpl(later, FAILED, 2495L, "Hello, world!");
92 assertThat(js.getStartTime(), is(later));
93 assertThat(js.getRunOutcome(), is(FAILED));
94 assertThat(js.getDurationInMillis(), is(2495L));
95 assertThat(js.getMessage(), is("Hello, world!"));
96 }
97
98
99
100
101
102
103
104
105 private static String generate(final int len) {
106 char c = '!';
107 final StringBuilder sb = new StringBuilder(len);
108 for (int i = 0; i < len; ++i) {
109 sb.append(c);
110 if (c >= '~') {
111 c = '!';
112 } else {
113 c++;
114 }
115 }
116 return sb.toString();
117 }
118 }