View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
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  package org.argeo.slc.core.test;
17  
18  import java.io.Serializable;
19  
20  import org.argeo.slc.test.TestResultPart;
21  import org.argeo.slc.test.TestRun;
22  import org.argeo.slc.test.TestRunAware;
23  import org.argeo.slc.test.TestStatus;
24  
25  /**
26   * <p>
27   * Basic implementation of a result part, implementing the standard three status
28   * approach for test results.
29   * </p>
30   * 
31   * @see TestStatus
32   */
33  public class SimpleResultPart implements TestResultPart, TestStatus,
34  		TestRunAware, Serializable {
35  	private static final long serialVersionUID = 6669675957685071901L;
36  
37  	private Long tid;
38  
39  	private String testRunUuid;
40  
41  	/** The status. Default to ERROR since it should always be explicitely set. */
42  	private Integer status = ERROR;
43  	private String message;
44  	private String exceptionMessage;
45  
46  	public SimpleResultPart() {
47  	}
48  
49  	public SimpleResultPart(Integer status, String message) {
50  		this(status, message, null);
51  	}
52  
53  	public SimpleResultPart(Integer status, String message, Exception exception) {
54  		this.status = status;
55  		this.message = message;
56  		setException(exception);
57  	}
58  
59  	public String getMessage() {
60  		return message;
61  	}
62  
63  	public void setMessage(String message) {
64  		this.message = message;
65  	}
66  
67  	public void setStatus(Integer status) {
68  		this.status = status;
69  	}
70  
71  	public Integer getStatus() {
72  		return status;
73  	}
74  
75  	public String getExceptionMessage() {
76  		return exceptionMessage;
77  	}
78  
79  	public void setException(Exception exception) {
80  		if (exception == null)
81  			return;
82  
83  		StringBuffer buf = new StringBuffer("");
84  		buf.append(exception.toString());
85  		buf.append('\n');
86  		for (StackTraceElement elem : exception.getStackTrace()) {
87  			buf.append('\t').append(elem.toString()).append('\n');
88  		}
89  
90  		if (exception.getCause() != null)
91  			addRootCause(buf, exception.getCause());
92  
93  		this.exceptionMessage = buf.toString();
94  	}
95  
96  	protected void addRootCause(StringBuffer buf, Throwable cause) {
97  		if (cause == null)
98  			return;
99  
100 		buf.append("Caused by: " + cause.getMessage());
101 		for (StackTraceElement elem : cause.getStackTrace()) {
102 			buf.append('\t').append(elem.toString()).append('\n');
103 		}
104 
105 		if (cause.getCause() != null) {
106 			addRootCause(buf, cause.getCause());
107 		}
108 	}
109 
110 	@Override
111 	public String toString() {
112 		StringBuffer buf = new StringBuffer("");
113 		buf.append(SlcTestUtils.statusToString(status));
114 		if (status == PASSED || status == FAILED) {
115 			buf.append(' ');
116 		} else if (status == ERROR) {
117 			buf.append("  ");
118 		}
119 		buf.append(message);
120 		return buf.toString();
121 	}
122 
123 	/** @deprecated */
124 	Long getTid() {
125 		return tid;
126 	}
127 
128 	/** @deprecated */
129 	void setTid(Long tid) {
130 		this.tid = tid;
131 	}
132 
133 	public String getTestRunUuid() {
134 		return testRunUuid;
135 	}
136 
137 	/** For ORM */
138 	public void setTestRunUuid(String testRunUuid) {
139 		this.testRunUuid = testRunUuid;
140 	}
141 
142 	public void notifyTestRun(TestRun testRun) {
143 		testRunUuid = testRun.getUuid();
144 	}
145 
146 	public void setExceptionMessage(String exceptionMessage) {
147 		this.exceptionMessage = exceptionMessage;
148 	}
149 
150 }