001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package org.apache.activemq.tool;
020    
021    import org.apache.commons.logging.Log;
022    import org.apache.commons.logging.LogFactory;
023    
024    import java.io.File;
025    import java.io.FileOutputStream;
026    import java.io.IOException;
027    import java.io.PrintWriter;
028    import java.util.Enumeration;
029    import java.util.Properties;
030    
031    public class ReportGenerator {
032        private static final Log log = LogFactory.getLog(ReportGenerator.class);
033        private String reportDirectory = null;
034        private String reportName = null;
035        private PrintWriter writer = null;
036        private File reportFile = null;
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    
050            File reportDir = new File(getReportDirectory());
051    
052            // Create output directory if it doesn't exist.
053            if (!reportDir.exists()) {
054                reportDir.mkdirs();
055            }
056    
057    
058            if (reportDir != null) {
059                reportFile = new File(this.getReportDirectory() + File.separator + this.getReportName() + ".xml");
060            }
061    
062            try {
063                this.writer = new PrintWriter(new FileOutputStream(reportFile));
064            } catch (IOException e1) {
065                e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
066            }
067        }
068    
069        public void stopGenerateReport() {
070            writeWithIndent(0, "</test-report>");
071            this.getWriter().flush();
072            this.getWriter().close();
073            log.info(" TEST REPORT OUTPUT : " + reportFile.getAbsolutePath());
074    
075    
076        }
077    
078        protected void addTestInformation() {
079    
080            writeWithIndent(0, "<test-report>");
081            writeWithIndent(2, "<test-information>");
082    
083            writeWithIndent(4, "<os-name>" + System.getProperty("os.name") + "</os-name>");
084            writeWithIndent(4, "<java-version>" + System.getProperty("java.version") + "</java-version>");
085    
086        }
087    
088    
089        protected void addClientSettings() {
090            if (this.getTestSettings() != null) {
091                Enumeration keys = getTestSettings().propertyNames();
092    
093                writeWithIndent(4, "<test-settings>");
094    
095                String key;
096                while (keys.hasMoreElements()) {
097                    key = (String) keys.nextElement();
098                    writeWithIndent(6, "<" + key + ">" + getTestSettings().get(key) + "</" + key + ">");
099                }
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 result) {
121            StringBuffer buffer = new StringBuffer();
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 getWriter() {
132            return this.writer;
133        }
134    
135    
136        public String getReportDirectory() {
137            return reportDirectory;
138        }
139    
140        public void setReportDirectory(String reportDirectory) {
141            this.reportDirectory = reportDirectory;
142        }
143    
144        public String getReportName() {
145            return reportName;
146        }
147    
148    
149        public void setReportName(String reportName) {
150            this.reportName = reportName;
151        }
152    
153        public Properties getTestSettings() {
154            return testSettings;
155        }
156    
157        public void setTestSettings(Properties testSettings) {
158            this.testSettings = testSettings;
159        }
160    }