001: /*
002: * @(#)AbstractNewFileServer.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Part of the GroboUtils package at:
009: * http://groboutils.sourceforge.net
010: *
011: * Permission is hereby granted, free of charge, to any person obtaining a
012: * copy of this software and associated documentation files (the "Software"),
013: * to deal in the Software without restriction, including without limitation
014: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
015: * and/or sell copies of the Software, and to permit persons to whom the
016: * Software is furnished to do so, subject to the following conditions:
017: *
018: * The above copyright notice and this permission notice shall be included in
019: * all copies or substantial portions of the Software.
020: *
021: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
022: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
024: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
025: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
026: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
027: * DEALINGS IN THE SOFTWARE.
028: */
029: package net.sourceforge.groboutils.pmti.v1.autodoc.v1.server;
030:
031: import java.io.File;
032: import java.io.IOException;
033: import java.io.Writer;
034: import java.io.FileWriter;
035:
036: import net.sourceforge.groboutils.autodoc.v1.testserver.AbstractWriterServer;
037: import net.sourceforge.groboutils.autodoc.v1.testserver.TestData;
038: import net.sourceforge.groboutils.autodoc.v1.testserver.TestInfo;
039:
040: import org.apache.log4j.Logger;
041:
042: /**
043: * An interface which corresponds to a part of the framework that knows how
044: * to deal with the framework's <tt>TestData</tt>. It may directly deal with
045: * the data, or pass it off to a remote server.
046: *
047: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
048: * @since March 17, 2002
049: * @version $Date: 2003/02/10 22:51:56 $
050: */
051: public abstract class AbstractNewFileServer extends
052: AbstractWriterServer {
053: private static Logger LOG = Logger
054: .getLogger(AbstractNewFileServer.class);
055:
056: private String prefix;
057: private String postfix;
058: private boolean useTimestamp = true;
059:
060: /**
061: * Creates a new file for every BugTestResult, in the format
062: * <code>prefix + BugTestResult.getSuiteName() + '-' +
063: * BugTestResuilt.getTestMethodName() + '-' +
064: * Long.toString( System.currentTimeMillis() ) +
065: * postfix</code>. <tt>prefix</tt> should include the directory.
066: */
067: public AbstractNewFileServer(String prefix, String postfix) {
068: this (prefix, postfix, true);
069: }
070:
071: /**
072: * Creates a new file for every BugTestResult, in the format
073: * <code>prefix + BugTestResult.getSuiteName() + '-' +
074: * BugTestResuilt.getTestMethodName() + '-' +
075: * Long.toString( System.currentTimeMillis() ) +
076: * postfix</code>. <tt>prefix</tt> should include the directory.
077: */
078: public AbstractNewFileServer(String prefix, String postfix,
079: boolean useTimestamp) {
080: if (prefix == null || postfix == null) {
081: throw new IllegalArgumentException("no null arguments");
082: }
083: this .prefix = prefix;
084: this .postfix = postfix;
085: this .useTimestamp = useTimestamp;
086: }
087:
088: /**
089: * Open a writer stream. This will be done once per result, so log-like
090: * actions may need to append to the previous results.
091: */
092: protected Writer openOutput(TestData td) throws IOException {
093: File f = new File(createFileName(td));
094: LOG.debug("Opening file " + f.getAbsolutePath()
095: + " for output.");
096: FileWriter fw = new FileWriter(f);
097: return fw;
098: }
099:
100: /**
101: *
102: */
103: protected String createFileName(TestData td) {
104: TestInfo ti = td.getTestInfo();
105: String suite = "suite";
106: String method = "method";
107: if (ti != null) {
108: if (ti.getSuite() != null) {
109: suite = ti.getSuite();
110: }
111: if (ti.getMethod() != null) {
112: method = ti.getMethod();
113: }
114: }
115: StringBuffer sb = new StringBuffer(this .prefix);
116: sb.append(suite).append('-').append(method);
117: if (this .useTimestamp) {
118: sb.append('-').append(System.currentTimeMillis());
119: }
120: sb.append(this .postfix);
121: String name = sb.toString();
122: return name;
123: }
124: }
|