1 package net.avcompris.guixer.core;
2
3 import static com.google.common.base.Preconditions.checkNotNull;
4 import static com.google.common.base.Preconditions.checkState;
5
6 import javax.annotation.Nullable;
7
8 final class Assertion {
9
10 private final String assertionName;
11 private final String arg0;
12 private final String arg1;
13
14 @Nullable
15 private Throwable exception;
16
17 private boolean success = false;
18
19 public Assertion(final String assertionName, final String arg0, final String arg1) {
20
21 this.assertionName = checkNotNull(assertionName, "assertionName");
22 this.arg0 = checkNotNull(arg0, "arg0");
23 this.arg1 = checkNotNull(arg1, "arg1");
24 }
25
26 @Override
27 public String toString() {
28
29 final StringBuilder sb = new StringBuilder(assertionName + "(\"" + arg0 + "\", \"" + arg1 + "\"): ");
30
31 if (exception != null) {
32
33 sb.append("ERROR: " + exception.getClass().getSimpleName());
34
35 } else if (success) {
36
37 sb.append("SUCCESS");
38
39 } else {
40
41 sb.append("PENDING");
42 }
43
44 return sb.toString();
45 }
46
47 public void setException(@Nullable final Throwable e) {
48
49 checkState(!success || e == null,
50 "exception should not be non null when success == true: %s", e);
51
52 checkState(exception == null || e == null,
53 "exception should not be non null when a previous exception exists: %s (%s)", e, exception);
54
55 checkState(exception == null || e != null,
56 "exception should not be set to null when a previous exception exists: %s", exception);
57
58 exception = e;
59 }
60
61 public void setSuccess() {
62
63 checkState(exception == null,
64 "success should not be set when a previous exception exists: %s", exception);
65
66 success = true;
67 }
68
69 public boolean isFailure() {
70
71 return exception != null;
72 }
73
74 public boolean isPending() {
75
76 return !success && exception == null;
77 }
78
79 public boolean isSuccess() {
80
81 return success;
82 }
83 }