Source Code Cross Referenced for TestEngine.java in  » Wiki-Engine » JSPWiki » com » ecyrd » jspwiki » 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 » Wiki Engine » JSPWiki » com.ecyrd.jspwiki 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.ecyrd.jspwiki;
002:
003:        import java.util.Properties;
004:        import java.io.*;
005:
006:        import javax.servlet.http.HttpSession;
007:
008:        import org.apache.log4j.Logger;
009:
010:        import com.ecyrd.jspwiki.attachment.Attachment;
011:        import com.ecyrd.jspwiki.auth.Users;
012:        import com.ecyrd.jspwiki.providers.*;
013:
014:        /**
015:         *  Simple test engine that always assumes pages are found.
016:         */
017:        public class TestEngine extends WikiEngine {
018:            static Logger log = Logger.getLogger(TestEngine.class);
019:
020:            private HttpSession m_adminSession;
021:            private HttpSession m_janneSession;
022:            private WikiSession m_adminWikiSession;
023:            private WikiSession m_janneWikiSession;
024:            private WikiSession m_guestWikiSession;
025:
026:            /**
027:             * Creates WikiSession with the privileges of the administrative user.
028:             * For testing purposes, obviously.
029:             * @return the wiki session
030:             */
031:            public WikiSession adminSession() {
032:                return m_adminWikiSession;
033:            }
034:
035:            /**
036:             * Creates guest WikiSession with the no privileges.
037:             * For testing purposes, obviously.
038:             * @return the wiki session
039:             */
040:            public WikiSession guestSession() {
041:                return m_guestWikiSession;
042:            }
043:
044:            /**
045:             * Creates WikiSession with the privileges of the Janne.
046:             * For testing purposes, obviously.
047:             * @return the wiki session
048:             */
049:            public WikiSession janneSession() {
050:                return m_janneWikiSession;
051:            }
052:
053:            public TestEngine(Properties props) throws WikiException {
054:                super (props);
055:
056:                // Set up long-running admin session
057:                TestHttpServletRequest request = new TestHttpServletRequest();
058:                request.setRemoteAddr("53.33.128.9");
059:                m_adminWikiSession = WikiSession.getWikiSession(this , request);
060:                this .getAuthenticationManager().login(m_adminWikiSession,
061:                        Users.ADMIN, Users.ADMIN_PASS);
062:                m_adminSession = request.getSession();
063:
064:                // Set up a test Janne session
065:                request = new TestHttpServletRequest();
066:                request.setRemoteAddr("42.22.17.8");
067:                m_janneWikiSession = WikiSession.getWikiSession(this , request);
068:                this .getAuthenticationManager().login(m_janneWikiSession,
069:                        Users.JANNE, Users.JANNE_PASS);
070:                m_janneSession = request.getSession();
071:
072:                // Set up guest session
073:                request = new TestHttpServletRequest();
074:                request.setRemoteAddr("42.22.17.8");
075:                m_guestWikiSession = WikiSession.getWikiSession(this , request);
076:            }
077:
078:            public static void emptyWorkDir() {
079:                Properties properties = new Properties();
080:
081:                try {
082:                    properties.load(findTestProperties());
083:
084:                    String workdir = properties
085:                            .getProperty(WikiEngine.PROP_WORKDIR);
086:                    if (workdir != null) {
087:                        File f = new File(workdir);
088:
089:                        if (f.exists() && f.isDirectory()
090:                                && new File(f, "refmgr.ser").exists()) {
091:                            deleteAll(f);
092:                        }
093:                    }
094:                } catch (IOException e) {
095:                } // Fine
096:            }
097:
098:            public static final InputStream findTestProperties() {
099:                return findTestProperties("/jspwiki.properties");
100:            }
101:
102:            public static final InputStream findTestProperties(String properties) {
103:                InputStream in = TestEngine.class
104:                        .getResourceAsStream(properties);
105:
106:                if (in == null)
107:                    throw new InternalWikiException(
108:                            "Unable to locate test property resource: "
109:                                    + properties);
110:
111:                return in;
112:            }
113:
114:            /**
115:             *  Deletes all files under this directory, and does them recursively.
116:             */
117:            public static void deleteAll(File file) {
118:                if (file != null) {
119:                    if (file.isDirectory()) {
120:                        File[] files = file.listFiles();
121:
122:                        if (files != null) {
123:                            for (int i = 0; i < files.length; i++) {
124:                                if (files[i].isDirectory()) {
125:                                    deleteAll(files[i]);
126:                                }
127:
128:                                files[i].delete();
129:                            }
130:                        }
131:                    }
132:
133:                    file.delete();
134:                }
135:            }
136:
137:            /**
138:             *  Copied from FileSystemProvider
139:             */
140:            protected static String mangleName(String pagename)
141:                    throws IOException {
142:                Properties properties = new Properties();
143:                String m_encoding = properties.getProperty(
144:                        WikiEngine.PROP_ENCODING,
145:                        AbstractFileProvider.DEFAULT_ENCODING);
146:
147:                pagename = TextUtil.urlEncode(pagename, m_encoding);
148:                pagename = TextUtil.replaceString(pagename, "/", "%2F");
149:                return pagename;
150:            }
151:
152:            /**
153:             *  Removes a page, but not any auxiliary information.  Works only
154:             *  with FileSystemProvider.
155:             */
156:            public static void deleteTestPage(String name) {
157:                Properties properties = new Properties();
158:
159:                try {
160:                    properties.load(findTestProperties());
161:                    String files = properties
162:                            .getProperty(FileSystemProvider.PROP_PAGEDIR);
163:
164:                    File f = new File(files, mangleName(name)
165:                            + FileSystemProvider.FILE_EXT);
166:
167:                    f.delete();
168:
169:                    // Remove the property file, too
170:                    f = new File(files, mangleName(name) + ".properties");
171:
172:                    if (f.exists())
173:                        f.delete();
174:                } catch (Exception e) {
175:                    log.error("Couldn't delete " + name, e);
176:                }
177:            }
178:
179:            /**
180:             *  Deletes all attachments related to the given page.
181:             */
182:            public void deleteAttachments(String page) {
183:                try {
184:                    String files = getWikiProperties().getProperty(
185:                            BasicAttachmentProvider.PROP_STORAGEDIR);
186:
187:                    File f = new File(files, TextUtil.urlEncodeUTF8(page)
188:                            + BasicAttachmentProvider.DIR_EXTENSION);
189:
190:                    deleteAll(f);
191:                } catch (Exception e) {
192:                    log.error("Could not remove attachments.", e);
193:                }
194:            }
195:
196:            /**
197:             *  Makes a temporary file with some content, and returns a handle to it.
198:             */
199:            public File makeAttachmentFile() throws Exception {
200:                File tmpFile = File.createTempFile("test", "txt");
201:                tmpFile.deleteOnExit();
202:
203:                FileWriter out = new FileWriter(tmpFile);
204:
205:                FileUtil.copyContents(new StringReader(
206:                        "asdfa???dfzbvasdjkfbwfkUg783gqdwog"), out);
207:
208:                out.close();
209:
210:                return tmpFile;
211:            }
212:
213:            /**
214:             *  Adds an attachment to a page for testing purposes.
215:             * @param pageName
216:             * @param attachmentName
217:             * @param data
218:             */
219:            public void addAttachment(String pageName, String attachmentName,
220:                    byte[] data) throws ProviderException, IOException {
221:                Attachment att = new Attachment(this , pageName, attachmentName);
222:
223:                getAttachmentManager().storeAttachment(att,
224:                        new ByteArrayInputStream(data));
225:            }
226:
227:            /**
228:             * Convenience method that saves a wiki page by constructing a fake
229:             * WikiContext and HttpServletRequest. We always want to do this using a
230:             * WikiContext whose subject contains Role.ADMIN.
231:             * @param pageName
232:             * @param content
233:             * @throws WikiException
234:             */
235:            public void saveText(String pageName, String content)
236:                    throws WikiException {
237:                // Build new request and associate our admin session
238:                TestHttpServletRequest request = new TestHttpServletRequest();
239:                request.m_session = m_adminSession;
240:
241:                // Create page and wiki context
242:                WikiPage page = new WikiPage(this , pageName);
243:                WikiContext context = new WikiContext(this , request, page);
244:                saveText(context, content);
245:            }
246:
247:            public void saveTextAsJanne(String pageName, String content)
248:                    throws WikiException {
249:                // Build new request and associate our Janne session
250:                TestHttpServletRequest request = new TestHttpServletRequest();
251:                request.m_session = m_janneSession;
252:
253:                // Create page and wiki context
254:                WikiPage page = new WikiPage(this , pageName);
255:                WikiContext context = new WikiContext(this , request, page);
256:                saveText(context, content);
257:            }
258:
259:            public static void trace() {
260:                try {
261:                    throw new Exception("Foo");
262:                } catch (Exception e) {
263:                    e.printStackTrace();
264:                }
265:            }
266:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.