01: //$Header$
02: /*
03: * Licensed to the Apache Software Foundation (ASF) under one or more
04: * contributor license agreements. See the NOTICE file distributed with
05: * this work for additional information regarding copyright ownership.
06: * The ASF licenses this file to You under the Apache License, Version 2.0
07: * (the "License"); you may not use this file except in compliance with
08: * the License. You may obtain a copy of the License at
09: *
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.jmeter.report.writers;
20:
21: import java.io.File;
22: import java.util.Calendar;
23:
24: import org.apache.jmeter.testelement.AbstractTestElement;
25: import org.apache.jmeter.testelement.TestElement;
26:
27: /**
28: * @author Peter Lin
29: *
30: * The abstract report writer provides the common implementation for subclasses
31: * to reuse.
32: */
33: public abstract class AbstractReportWriter extends AbstractTestElement
34: implements ReportWriter {
35:
36: public static final String TARGET_DIRECTORY = "ReportWriter.target.directory";
37:
38: /**
39: *
40: */
41: public AbstractReportWriter() {
42: super ();
43: }
44:
45: /**
46: * Subclasses need to implement this method and provide the necessary
47: * logic to produce a ReportSummary object and write the report
48: */
49: public abstract ReportSummary writeReport(TestElement element);
50:
51: /**
52: * The method simply returns the target directory and doesn't
53: * validate it. the abstract class expects some other class will
54: * validate the target directory.
55: */
56: public String getTargetDirectory() {
57: return getPropertyAsString(TARGET_DIRECTORY);
58: }
59:
60: /**
61: * Set the target directory where the report should be saved
62: */
63: public void setTargetDirectory(String directory) {
64: setProperty(TARGET_DIRECTORY, directory);
65: }
66:
67: public void makeDirectory() {
68: File output = new File(getTargetDirectory());
69: if (!output.exists() || !output.isDirectory()) {
70: output.mkdir();
71: }
72: }
73:
74: /**
75: * if the target output directory already exists, archive it
76: */
77: public void archiveDirectory() {
78: File output = new File(getTargetDirectory());
79: if (output.exists() && output.isDirectory()) {
80: // if the directory already exists and is a directory,
81: // we just renamed to "archive.date"
82: output.renameTo(new File("archive." + getDayString()));
83: }
84: }
85:
86: /**
87: * return the day in YYYYMMDD format
88: * @return
89: */
90: public String getDayString() {
91: Calendar today = Calendar.getInstance();
92: String year = String.valueOf(today.get(Calendar.YEAR));
93: String month = String.valueOf(today.get(Calendar.MONTH));
94: String day = String.valueOf(today.get(Calendar.DATE));
95: return year + month + day;
96: }
97: }
|