]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/SimpleResultPart.java
Improve demo
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / test / SimpleResultPart.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.slc.core.test;
18
19 import java.io.Serializable;
20
21 import org.argeo.slc.test.TestResultPart;
22 import org.argeo.slc.test.TestRun;
23 import org.argeo.slc.test.TestRunAware;
24 import org.argeo.slc.test.TestStatus;
25
26 /**
27 * <p>
28 * Basic implementation of a result part, implementing the standard three status
29 * approach for test results.
30 * </p>
31 *
32 * @see TestStatus
33 */
34 public class SimpleResultPart implements TestResultPart, TestStatus,
35 TestRunAware, Serializable {
36 private static final long serialVersionUID = 6669675957685071901L;
37
38 private Long tid;
39
40 private String testRunUuid;
41
42 /** The status. Default to ERROR since it should always be explicitely set. */
43 private Integer status = ERROR;
44 private String message;
45 private String exceptionMessage;
46
47 public SimpleResultPart() {
48 }
49
50 public SimpleResultPart(Integer status, String message) {
51 this(status, message, null);
52 }
53
54 public SimpleResultPart(Integer status, String message, Exception exception) {
55 this.status = status;
56 this.message = message;
57 setException(exception);
58 }
59
60 public String getMessage() {
61 return message;
62 }
63
64 public void setMessage(String message) {
65 this.message = message;
66 }
67
68 public void setStatus(Integer status) {
69 this.status = status;
70 }
71
72 public Integer getStatus() {
73 return status;
74 }
75
76 public String getExceptionMessage() {
77 return exceptionMessage;
78 }
79
80 public void setException(Exception exception) {
81 if (exception == null)
82 return;
83
84 StringBuffer buf = new StringBuffer("");
85 buf.append(exception.toString());
86 buf.append('\n');
87 for (StackTraceElement elem : exception.getStackTrace()) {
88 buf.append('\t').append(elem.toString()).append('\n');
89 }
90
91 if (exception.getCause() != null)
92 addRootCause(buf, exception.getCause());
93
94 this.exceptionMessage = buf.toString();
95 }
96
97 protected void addRootCause(StringBuffer buf, Throwable cause) {
98 if (cause == null)
99 return;
100
101 buf.append("Caused by: " + cause.getMessage());
102 for (StackTraceElement elem : cause.getStackTrace()) {
103 buf.append('\t').append(elem.toString()).append('\n');
104 }
105
106 if (cause.getCause() != null) {
107 addRootCause(buf, cause.getCause());
108 }
109 }
110
111 @Override
112 public String toString() {
113 StringBuffer buf = new StringBuffer("");
114 buf.append(SlcTestUtils.statusToString(status));
115 if (status == PASSED || status == FAILED) {
116 buf.append(' ');
117 } else if (status == ERROR) {
118 buf.append(" ");
119 }
120 buf.append(message);
121 return buf.toString();
122 }
123
124 /** @deprecated */
125 Long getTid() {
126 return tid;
127 }
128
129 /** @deprecated */
130 void setTid(Long tid) {
131 this.tid = tid;
132 }
133
134 public String getTestRunUuid() {
135 return testRunUuid;
136 }
137
138 /** For ORM */
139 public void setTestRunUuid(String testRunUuid) {
140 this.testRunUuid = testRunUuid;
141 }
142
143 public void notifyTestRun(TestRun testRun) {
144 testRunUuid = testRun.getUuid();
145 }
146
147 public void setExceptionMessage(String exceptionMessage) {
148 this.exceptionMessage = exceptionMessage;
149 }
150
151 }