001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.activemq.tool;
019
020import java.io.File;
021import java.io.FileOutputStream;
022import java.io.IOException;
023import java.io.PrintWriter;
024import java.util.Enumeration;
025import java.util.Properties;
026
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030public class ReportGenerator {
031
032    private static final Logger LOG = LoggerFactory.getLogger(ReportGenerator.class);
033    private String reportDirectory;
034    private String reportName;
035    private PrintWriter writer;
036    private File reportFile;
037    private Properties testSettings;
038
039    public ReportGenerator() {
040    }
041
042    public ReportGenerator(String reportDirectory, String reportName) {
043        this.setReportDirectory(reportDirectory);
044        this.setReportName(reportName);
045    }
046
047    public void startGenerateReport() {
048
049        File reportDir = new File(getReportDirectory());
050
051        // Create output directory if it doesn't exist.
052        if (!reportDir.exists()) {
053            reportDir.mkdirs();
054        }
055
056        if (reportDir != null) {
057            reportFile = new File(this.getReportDirectory() + File.separator + this.getReportName() + ".xml");
058        }
059
060        try {
061            this.writer = new PrintWriter(new FileOutputStream(reportFile));
062        } catch (IOException e1) {
063            e1.printStackTrace(); // To change body of catch statement use
064                                    // File | Settings | File Templates.
065        }
066    }
067
068    public void stopGenerateReport() {
069        writeWithIndent(0, "</test-report>");
070        this.getWriter().flush();
071        this.getWriter().close();
072        LOG.info(" TEST REPORT OUTPUT : " + reportFile.getAbsolutePath());
073
074    }
075
076    protected void addTestInformation() {
077
078        writeWithIndent(0, "<test-report>");
079        writeWithIndent(2, "<test-information>");
080
081        writeWithIndent(4, "<os-name>" + System.getProperty("os.name") + "</os-name>");
082        writeWithIndent(4, "<java-version>" + System.getProperty("java.version") + "</java-version>");
083
084    }
085
086    protected void addClientSettings() {
087        if (this.getTestSettings() != null) {
088            Enumeration keys = getTestSettings().propertyNames();
089
090            writeWithIndent(4, "<test-settings>");
091
092            String key;
093            while (keys.hasMoreElements()) {
094                key = (String)keys.nextElement();
095                writeWithIndent(6, "<" + key + ">" + getTestSettings().get(key) + "</" + key + ">");
096            }
097
098            writeWithIndent(4, "</test-settings>");
099        }
100    }
101
102    protected void endTestInformation() {
103        writeWithIndent(2, "</test-information>");
104
105    }
106
107    protected void startTestResult(long checkpointInterval) {
108        long intervalInSec = checkpointInterval / 1000;
109        writeWithIndent(2, "<test-result checkpoint_interval_in_sec='" + intervalInSec + "' >");
110    }
111
112    protected void endTestResult() {
113        writeWithIndent(2, "</test-result>");
114    }
115
116    protected void writeWithIndent(int indent, String result) {
117        StringBuffer buffer = new StringBuffer();
118
119        for (int i = 0; i < indent; ++i) {
120            buffer.append(" ");
121        }
122
123        buffer.append(result);
124        writer.println(buffer.toString());
125    }
126
127    public PrintWriter getWriter() {
128        return this.writer;
129    }
130
131    public String getReportDirectory() {
132        return reportDirectory;
133    }
134
135    public void setReportDirectory(String reportDirectory) {
136        this.reportDirectory = reportDirectory;
137    }
138
139    public String getReportName() {
140        return reportName;
141    }
142
143    public void setReportName(String reportName) {
144        this.reportName = reportName;
145    }
146
147    public Properties getTestSettings() {
148        return testSettings;
149    }
150
151    public void setTestSettings(Properties testSettings) {
152        this.testSettings = testSettings;
153    }
154}