001: package com.ecyrd.jspwiki.xmlrpc;
002:
003: import com.ecyrd.jspwiki.*;
004: import com.ecyrd.jspwiki.attachment.Attachment;
005: import junit.framework.*;
006: import java.util.*;
007: import org.apache.xmlrpc.*;
008: import com.ecyrd.jspwiki.xmlrpc.RPCHandler;
009:
010: public class RPCHandlerTest extends TestCase {
011: TestEngine m_engine;
012: RPCHandler m_handler;
013: Properties m_props;
014:
015: static final String NAME1 = "Test";
016:
017: public RPCHandlerTest(String s) {
018: super (s);
019: }
020:
021: public void setUp() throws Exception {
022: m_props = new Properties();
023: m_props.load(TestEngine.findTestProperties());
024:
025: m_engine = new TestEngine(m_props);
026:
027: m_handler = new RPCHandler();
028: WikiContext ctx = new WikiContext(m_engine, new WikiPage(
029: m_engine, "Dummy"));
030: m_handler.initialize(ctx);
031: }
032:
033: public void tearDown() {
034: TestEngine.deleteTestPage(NAME1);
035: m_engine.deleteAttachments(NAME1);
036: TestEngine.emptyWorkDir();
037: }
038:
039: public void testNonexistantPage() {
040: try {
041: m_handler.getPage("NoSuchPage");
042: fail("No exception for missing page.");
043: } catch (XmlRpcException e) {
044: assertEquals("Wrong error code.", RPCHandler.ERR_NOPAGE,
045: e.code);
046: }
047: }
048:
049: public void testRecentChanges() throws Exception {
050: Date time = getCalendarTime(Calendar.getInstance().getTime());
051: Vector previousChanges = m_handler.getRecentChanges(time);
052:
053: m_engine.saveText(NAME1, "Foo");
054: WikiPage directInfo = m_engine.getPage(NAME1);
055: time = getCalendarTime(directInfo.getLastModified());
056: Vector recentChanges = m_handler.getRecentChanges(time);
057:
058: assertEquals("wrong number of changes", 1, recentChanges.size()
059: - previousChanges.size());
060: }
061:
062: public void testRecentChangesWithAttachments() throws Exception {
063: Date time = getCalendarTime(Calendar.getInstance().getTime());
064: Vector previousChanges = m_handler.getRecentChanges(time);
065:
066: m_engine.saveText(NAME1, "Foo");
067: Attachment att = new Attachment(m_engine, NAME1, "TestAtt.txt");
068: att.setAuthor("FirstPost");
069: m_engine.getAttachmentManager().storeAttachment(att,
070: m_engine.makeAttachmentFile());
071: WikiPage directInfo = m_engine.getPage(NAME1);
072: time = getCalendarTime(directInfo.getLastModified());
073: Vector recentChanges = m_handler.getRecentChanges(time);
074:
075: assertEquals("wrong number of changes", 1, recentChanges.size()
076: - previousChanges.size());
077: }
078:
079: public void testPageInfo() throws Exception {
080: m_engine.saveText(NAME1, "Foobar.[{ALLOW view Anonymous}]");
081: WikiPage directInfo = m_engine.getPage(NAME1);
082:
083: Hashtable ht = m_handler.getPageInfo(NAME1);
084: assertEquals("name", (String) ht.get("name"), NAME1);
085:
086: Date d = (Date) ht.get("lastModified");
087:
088: Calendar cal = Calendar.getInstance();
089: cal.setTime(d);
090:
091: System.out.println("Real: " + directInfo.getLastModified());
092: System.out.println("RPC: " + d);
093:
094: // Offset the ZONE offset and DST offset away. DST only
095: // if we're actually in DST.
096: cal.add(Calendar.MILLISECOND,
097: (cal.get(Calendar.ZONE_OFFSET) + (cal.getTimeZone()
098: .inDaylightTime(d) ? cal
099: .get(Calendar.DST_OFFSET) : 0)));
100: System.out.println("RPC2: " + cal.getTime());
101:
102: assertEquals("date", cal.getTime().getTime(), directInfo
103: .getLastModified().getTime());
104: }
105:
106: /**
107: * Tests if listLinks() works with a single, non-existant local page.
108: */
109: public void testListLinks() throws Exception {
110: String text = "[Foobar]";
111: String pageName = NAME1;
112:
113: m_engine.saveText(pageName, text);
114:
115: Vector links = m_handler.listLinks(pageName);
116:
117: assertEquals("link count", 1, links.size());
118:
119: Hashtable linkinfo = (Hashtable) links.elementAt(0);
120:
121: assertEquals("name", "Foobar", linkinfo.get("page"));
122: assertEquals("type", "local", linkinfo.get("type"));
123: assertEquals("href", "http://localhost/Edit.jsp?page=Foobar",
124: linkinfo.get("href"));
125: }
126:
127: public void testListLinksWithAttachments() throws Exception {
128: String text = "[Foobar] [Test/TestAtt.txt]";
129: String pageName = NAME1;
130:
131: m_engine.saveText(pageName, text);
132:
133: Attachment att = new Attachment(m_engine, NAME1, "TestAtt.txt");
134: att.setAuthor("FirstPost");
135: m_engine.getAttachmentManager().storeAttachment(att,
136: m_engine.makeAttachmentFile());
137:
138: // Test.
139:
140: Vector links = m_handler.listLinks(pageName);
141:
142: assertEquals("link count", 2, links.size());
143:
144: Hashtable linkinfo = (Hashtable) links.elementAt(0);
145:
146: assertEquals("edit name", "Foobar", linkinfo.get("page"));
147: assertEquals("edit type", "local", linkinfo.get("type"));
148: assertEquals("edit href",
149: "http://localhost/Edit.jsp?page=Foobar", linkinfo
150: .get("href"));
151:
152: linkinfo = (Hashtable) links.elementAt(1);
153:
154: assertEquals("att name", NAME1 + "/TestAtt.txt", linkinfo
155: .get("page"));
156: assertEquals("att type", "local", linkinfo.get("type"));
157: assertEquals("att href", "http://localhost/attach/" + NAME1
158: + "/TestAtt.txt", linkinfo.get("href"));
159: }
160:
161: private Date getCalendarTime(Date modifiedDate) {
162: Calendar cal = Calendar.getInstance();
163: cal.setTime(modifiedDate);
164: cal.add(Calendar.HOUR, -1);
165:
166: // Go to UTC
167: // Offset the ZONE offset and DST offset away. DST only
168: // if we're actually in DST.
169: cal.add(Calendar.MILLISECOND,
170: -(cal.get(Calendar.ZONE_OFFSET) + (cal.getTimeZone()
171: .inDaylightTime(modifiedDate) ? cal
172: .get(Calendar.DST_OFFSET) : 0)));
173:
174: return cal.getTime();
175: }
176:
177: /*
178: * TODO: ENABLE
179: public void testPermissions()
180: throws Exception
181: {
182: String text ="Blaa. [{DENY view Guest}] [{ALLOW view NamedGuest}]";
183:
184: m_engine.saveText( NAME1, text );
185:
186: try
187: {
188: Vector links = m_handler.listLinks( NAME1 );
189: fail("Didn't get an exception in listLinks()");
190: }
191: catch( XmlRpcException e ) {}
192:
193: try
194: {
195: Hashtable ht = m_handler.getPageInfo( NAME1 );
196: fail("Didn't get an exception in getPageInfo()");
197: }
198: catch( XmlRpcException e ) {}
199: }
200: */
201:
202: public static Test suite() {
203: return new TestSuite(RPCHandlerTest.class);
204: }
205: }
|