001: /* DDSteps - Data Driven JUnit Test Steps
002: * Copyright (C) 2005 Jayway AB
003: * www.ddsteps.org
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License version 2.1 as published by the Free Software Foundation.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, visit
016: * http://www.opensource.org/licenses/lgpl-license.php
017: */
018: package org.ddsteps.web.trail;
019:
020: import java.io.File;
021: import java.io.FileNotFoundException;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024: import java.io.OutputStreamWriter;
025: import java.io.UnsupportedEncodingException;
026: import java.io.Writer;
027:
028: import org.apache.commons.lang.Validate;
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.ddsteps.DDStepsException;
032: import org.ddsteps.util.FileUtils;
033:
034: /**
035: * To be used as a delegate from a web test step base class implementation.
036: *
037: * @author adamskogman
038: */
039: public class FileTrailWriter implements WebTrail {
040:
041: /**
042: * Logger for this class
043: */
044: public static final Log logger = LogFactory
045: .getLog(FileTrailWriter.class);
046:
047: /**
048: * Field: Folder for the current trail. Null if there is no current trail.
049: */
050: protected File currentTrailFolder;
051:
052: /**
053: * Field: 1-based index of pages in a trail.
054: */
055: protected int pageIndex = 1;
056:
057: /**
058: * Property: trail enabled or not. Default is false.
059: */
060: protected boolean trailEnabled = false;
061:
062: /**
063: * Dependency: Folder where HTML trails are written
064: */
065: protected File trailFolder;
066:
067: /**
068: * Property: Default encoding to use when writing pages. Default is UTF-8.
069: */
070: protected String defaultEncoding = "UTF-8";
071:
072: /**
073: * Property: If set to true, trail pages are always written using the
074: * default encoding ({@link #defaultEncoding}).
075: */
076: protected boolean alwaysUseDefaultEncoding = false;
077:
078: /**
079: * @see org.ddsteps.web.WebBrowser#endTrail()
080: */
081: public void endTrail() {
082:
083: // Just make the current folder null
084: currentTrailFolder = null;
085:
086: }
087:
088: /**
089: * @return Trail folder if set.
090: * @see org.ddsteps.web.WebBrowser#getTrailFolder()
091: */
092: public File getTrailFolder() {
093: return trailFolder;
094: }
095:
096: /**
097: * @return True if trail is enabled.
098: * @see org.ddsteps.web.WebBrowser#isTrailEnabled()
099: */
100: public boolean isTrailEnabled() {
101: return trailEnabled;
102: }
103:
104: /**
105: * @param trailEnabled
106: * @see org.ddsteps.web.WebBrowser#setTrailEnabled(boolean)
107: */
108: public void setTrailEnabled(boolean trailEnabled) {
109: this .trailEnabled = trailEnabled;
110: }
111:
112: /**
113: * @param trailFolder
114: * The folder to create trails in.
115: * @see org.ddsteps.web.WebBrowser#setTrailFolder(java.io.File)
116: */
117: public void setTrailFolder(File trailFolder) {
118:
119: Validate.notNull(trailFolder,
120: "Argument trailFolder must not be null");
121:
122: if (!trailFolder.exists()) {
123: // Create all needed dirs
124: if (!trailFolder.mkdirs()) {
125: throw new DDStepsException(
126: "Could not create trails folder: '"
127: + trailFolder + "'");
128: }
129: }
130:
131: this .trailFolder = trailFolder;
132: }
133:
134: /**
135: * @param trailName
136: * @see org.ddsteps.web.WebBrowser#startTrail(java.lang.String)
137: */
138: public void startTrail(String trailName) {
139:
140: Validate
141: .notEmpty("The trail name must not be empty", trailName);
142:
143: File newTrailFolder = new File(trailFolder, trailName);
144:
145: if (newTrailFolder.exists()) {
146: logger.debug("Deleting old trail '" + trailName + "'.");
147: if (!FileUtils.delTree(newTrailFolder)) {
148: throw new DDStepsException(
149: "Could not delete old folder for trail '"
150: + trailName + "'.");
151: }
152: }
153:
154: logger.debug("Creating trail folder '" + trailName + "'.");
155:
156: if (!newTrailFolder.mkdirs()) {
157: throw new DDStepsException(
158: "Could not create folder for trail '" + trailName
159: + "'.");
160: }
161:
162: currentTrailFolder = newTrailFolder;
163:
164: // reset index
165: pageIndex = 1;
166: }
167:
168: /**
169: * Write the current page to a file, regardless of any current trail or if
170: * trailing is enabled or not.
171: * <p>
172: * If you want a full trail of everything the test sees, use the trail
173: * functionalit instead.
174: *
175: * @param filename
176: * Relative filename in the trail folder, or a full file name.
177: * @param encoding
178: * The encoding to use.
179: * @param pageAsString
180: * The string to write.
181: */
182: public void writePage(String filename, String encoding,
183: String pageAsString) {
184:
185: // Open
186: File trailFile;
187: if (currentTrailFolder == null) {
188: trailFile = new File(filename);
189: } else {
190: trailFile = new File(currentTrailFolder, filename);
191: }
192:
193: // Write
194: try {
195: Writer writer = createWriter(trailFile, encoding);
196: writer.write(pageAsString);
197: writer.flush();
198: writer.close();
199:
200: logger.info("Wrote current page to file '" + filename
201: + "' using encoding '" + encoding + "'.");
202: } catch (IOException e) {
203: logger.error(
204: "Exception thrown when writing the current page to file '"
205: + filename + "'.", e);
206: }
207:
208: }
209:
210: /**
211: * Factory method for creating a writer.
212: *
213: * @param trailFile
214: * @param encoding
215: * @return Never null.
216: * @throws FileNotFoundException
217: * @throws UnsupportedEncodingException
218: */
219: protected Writer createWriter(File trailFile, String encoding)
220: throws FileNotFoundException, UnsupportedEncodingException {
221: FileOutputStream fos = new FileOutputStream(trailFile);
222: return new OutputStreamWriter(fos, encoding);
223: }
224:
225: /**
226: * Writes one page of the trail.
227: *
228: * @param trailPage
229: */
230: public void writeTrail(TrailPage trailPage) {
231:
232: Validate.notEmpty("Page name must not be empty.");
233:
234: if (!trailEnabled) {
235: logger.debug("Trail disabled.");
236: return;
237: }
238:
239: if (currentTrailFolder == null) {
240: logger.debug("No trail started, did not write page '"
241: + trailPage.pageName + "'.");
242: return;
243: }
244:
245: String trailFileName = pageIndex + " - " + trailPage.pageName
246: + ".html";
247:
248: // Bump up index
249: pageIndex++;
250:
251: writePage(trailFileName, trailPage.encoding,
252: trailPage.pageAsString);
253:
254: }
255:
256: }
|