KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > tool > ReportGenerator


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.activemq.tool;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import java.io.File JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 public class ReportGenerator {
32     private static final Log log = LogFactory.getLog(ReportGenerator.class);
33     private String JavaDoc reportDirectory = null;
34     private String JavaDoc reportName = null;
35     private PrintWriter JavaDoc writer = null;
36     private File JavaDoc reportFile = null;
37     private Properties JavaDoc testSettings;
38
39     public ReportGenerator() {
40     }
41
42     public ReportGenerator(String JavaDoc reportDirectory, String JavaDoc reportName) {
43         this.setReportDirectory(reportDirectory);
44         this.setReportName(reportName);
45     }
46
47     public void startGenerateReport() {
48
49
50         File JavaDoc reportDir = new File JavaDoc(getReportDirectory());
51
52         // Create output directory if it doesn't exist.
53
if (!reportDir.exists()) {
54             reportDir.mkdirs();
55         }
56
57
58         if (reportDir != null) {
59             reportFile = new File JavaDoc(this.getReportDirectory() + File.separator + this.getReportName() + ".xml");
60         }
61
62         try {
63             this.writer = new PrintWriter JavaDoc(new FileOutputStream JavaDoc(reportFile));
64         } catch (IOException JavaDoc e1) {
65             e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
66
}
67     }
68
69     public void stopGenerateReport() {
70         writeWithIndent(0, "</test-report>");
71         this.getWriter().flush();
72         this.getWriter().close();
73         log.info(" TEST REPORT OUTPUT : " + reportFile.getAbsolutePath());
74
75
76     }
77
78     protected void addTestInformation() {
79
80         writeWithIndent(0, "<test-report>");
81         writeWithIndent(2, "<test-information>");
82
83         writeWithIndent(4, "<os-name>" + System.getProperty("os.name") + "</os-name>");
84         writeWithIndent(4, "<java-version>" + System.getProperty("java.version") + "</java-version>");
85
86     }
87
88
89     protected void addClientSettings() {
90         if (this.getTestSettings() != null) {
91             Enumeration JavaDoc keys = getTestSettings().propertyNames();
92
93             writeWithIndent(4, "<test-settings>");
94
95             String JavaDoc key;
96             while (keys.hasMoreElements()) {
97                 key = (String JavaDoc) keys.nextElement();
98                 writeWithIndent(6, "<" + key + ">" + getTestSettings().get(key) + "</" + key + ">");
99             }
100
101             writeWithIndent(4, "</test-settings>");
102         }
103     }
104
105     protected void endTestInformation() {
106         writeWithIndent(2, "</test-information>");
107
108     }
109
110     protected void startTestResult(long checkpointInterval) {
111         long intervalInSec = checkpointInterval / 1000;
112         writeWithIndent(2, "<test-result checkpoint_interval_in_sec=" + intervalInSec + " >");
113     }
114
115     protected void endTestResult() {
116         writeWithIndent(2, "</test-result>");
117     }
118
119
120     protected void writeWithIndent(int indent, String JavaDoc result) {
121         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
122
123         for (int i = 0; i < indent; ++i) {
124             buffer.append(" ");
125         }
126
127         buffer.append(result);
128         writer.println(buffer.toString());
129     }
130
131     public PrintWriter JavaDoc getWriter() {
132         return this.writer;
133     }
134
135
136     public String JavaDoc getReportDirectory() {
137         return reportDirectory;
138     }
139
140     public void setReportDirectory(String JavaDoc reportDirectory) {
141         this.reportDirectory = reportDirectory;
142     }
143
144     public String JavaDoc getReportName() {
145         return reportName;
146     }
147
148
149     public void setReportName(String JavaDoc reportName) {
150         this.reportName = reportName;
151     }
152
153     public Properties JavaDoc getTestSettings() {
154         return testSettings;
155     }
156
157     public void setTestSettings(Properties JavaDoc testSettings) {
158         this.testSettings = testSettings;
159     }
160 }
161
Popular Tags