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: }
|