Source Code Cross Referenced for FileTrailWriter.java in  » Testing » DDSteps » org » ddsteps » web » trail » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » DDSteps » org.ddsteps.web.trail 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.