View Javadoc

1   /*
2    Copyright 2006 Ernest Micklei @ PhilemonWorks.com
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 com.philemonworks.selfdiagnose;
18  
19  import java.io.StringWriter;
20  import java.util.List;
21  
22  import org.apache.log4j.Logger;
23  
24  /**
25   * DiagnosticTaskResult hold information about the execution of a DiagnosticTask
26   * and known how to create a report for logging.
27   * <ul>
28   * <li>Passed means: task completed succesfully</li>
29   * <li>Failed means: execution task succes but tests failed</li>
30   * <li>Error means: execution task failed (because of Exception)</li>
31   * <li>Unknown means: result is not set and therefore not reported</li>
32   * </ul>
33   * 
34   * @author emicklei
35   */
36  public class DiagnosticTaskResult {
37  	public static final String STATUS_PASSED = "passed";
38  
39  	public static final String STATUS_FAILED = "failed";
40  
41  	public static final String STATUS_ERROR = "error";
42  	
43  	public static final String STATUS_UNKNOWN = "unknown";
44  
45  	private DiagnosticTask task;
46  
47  	private String status = STATUS_UNKNOWN;
48  
49  	private String message = "";
50  	
51  	private String comment = null;  // overrides task comment
52  	
53  	private long executionTime = 0;
54  
55  	/**
56  	 * Constructor requires a DiagnosticTask to store the result of its run.
57  	 * 
58  	 * @param task
59  	 *            DiagnosticTask
60  	 */
61  	public DiagnosticTaskResult(DiagnosticTask task) {
62  		super();
63  		this.task = task;
64  	}
65  
66  	/**
67  	 * Indicates the outcome of the task
68  	 * 
69  	 * @return STATUS_PASSED || STATUS_FAILED || STATUS_ERROR
70  	 */
71  	public String getStatus() {
72  		return status;
73  	}
74  
75  	/**
76  	 * @return boolean true if the run has failed
77  	 */
78  	public boolean isError() {
79  		return status.equals(STATUS_ERROR);
80  	}
81  
82  	/**
83  	 * @return boolean true if the run has failed
84  	 */
85  	public boolean isFailed() {
86  		return status.equals(STATUS_FAILED);
87  	}
88  
89  	/**
90  	 * @return boolean true if the run has passed
91  	 */
92  	public boolean isPassed() {
93  		return status.equals(STATUS_PASSED);
94  	}
95  	/**
96  	 * @return boolean true if the run has an unkown result
97  	 */
98  	public boolean isUnknown() {
99  		return status.equals(STATUS_UNKNOWN);
100 	}
101 	
102 	public boolean wantsToBeReported() {
103 		return task.reportResults && (!this.isUnknown());
104 	}
105 	/**
106 	 * Set the message explaining why the run has failed.
107 	 * 
108 	 * @param aMessage
109 	 */
110 	public void setErrorMessage(String aMessage) {
111 		status = STATUS_ERROR;
112 		message = aMessage;
113 	}
114 
115 	public String getMessage() {
116 		return message;
117 	}
118 
119 	/**
120 	 * Write a log entry using the logger associated with SelfDiagnose. If the
121 	 * result was an error then write a verbose report.
122 	 */
123 	public void logReport() {
124 		Logger log = Logger.getLogger(SelfDiagnose.class);
125 		StringWriter w = new StringWriter();
126 		this.writeMessagesOn(w);
127 		if (!isPassed()) {
128 			w.write(" - ");
129 			w.write(DiagnoseUtil.shortName(task.getClass()));
130 			w.write(" - ");
131 			w.write(task.getDescription());
132 		}
133 		if (task.hasComment()) {
134 			   w.write(" // ");
135 			   w.write(task.getComment());		
136 		}
137 		if (isPassed()) {
138 			log.info(w.toString());
139 		} else {
140 			log.error(w.toString());
141 		}
142 	}
143 
144 	protected void writeMessagesOn(StringWriter writer) {
145 		writer.write(message);
146 	}
147 
148 	/**
149 	 * Return the DiagnosticTask for which the result hold the results.
150 	 * 
151 	 * @return DiagnosticTask
152 	 */
153 	public DiagnosticTask getTask() {
154 		return task;
155 	}
156 
157 	/**
158 	 * Add a message to the list of reporting messages
159 	 * 
160 	 * @param passedMessage
161 	 *            String
162 	 */
163 	public void setPassedMessage(String passedMessage) {
164 		status = STATUS_PASSED;
165 		message = passedMessage;
166 	}
167 
168 	public void setFailedMessage(String failedMessage) {
169 		status = STATUS_FAILED;
170 		message = failedMessage;
171 	}
172 
173 	public long getExecutionTime() {
174 		return executionTime;
175 	}
176 
177 	public void setExecutionTime(long executionTime) {
178 		this.executionTime = executionTime;
179 	}
180 	/**
181 	 * Dispatch method exists because of CompositeDiagnosticTaskResult.
182 	 * @param results
183 	 */
184     public void addToResults(List<DiagnosticTaskResult> results) {
185         results.add(this);        
186     }
187     public String toString() {
188     	return super.toString() + "{status=" + status + ",message=" + message + "}";
189     }
190     public boolean hasComment() { return comment != null && !comment.equals(""); }
191     
192     public String getComment() { return comment; }
193     
194     public void setComment(String newComment) { comment = newComment; }
195 }