001: /*
002: * (C) Janne Jalkanen 2005
003: *
004: */
005: package com.ecyrd.jspwiki.ui;
006:
007: import java.util.Properties;
008:
009: import junit.framework.Test;
010: import junit.framework.TestCase;
011: import junit.framework.TestSuite;
012:
013: import com.ecyrd.jspwiki.TestEngine;
014: import com.ecyrd.jspwiki.TestHttpServletRequest;
015: import com.ecyrd.jspwiki.WikiContext;
016: import com.ecyrd.jspwiki.WikiEngine;
017: import com.ecyrd.jspwiki.WikiPage;
018: import com.ecyrd.jspwiki.auth.GroupPrincipal;
019:
020: public class CommandResolverTest extends TestCase {
021: TestEngine testEngine;
022: CommandResolver resolver;
023:
024: protected void setUp() throws Exception {
025: Properties props = new Properties();
026: props.load(TestEngine.findTestProperties());
027: props.put(WikiEngine.PROP_MATCHPLURALS, "yes");
028: testEngine = new TestEngine(props);
029: resolver = testEngine.getCommandResolver();
030: testEngine.saveText("SinglePage", "This is a test.");
031: testEngine.saveText("PluralPages", "This is a test.");
032: }
033:
034: protected void tearDown() throws Exception {
035: testEngine.deletePage("TestPage");
036: testEngine.deletePage("SinglePage");
037: testEngine.deletePage("PluralPage");
038: }
039:
040: public void testFindStaticWikiAction() {
041: Command a;
042:
043: // If we look for action with "edit" request context, we get EDIT action
044: a = CommandResolver.findCommand(WikiContext.EDIT);
045: assertEquals(PageCommand.EDIT, a);
046: assertEquals(WikiContext.EDIT, a.getRequestContext());
047:
048: // Ditto for prefs context
049: a = CommandResolver.findCommand(WikiContext.PREFS);
050: assertEquals(WikiCommand.PREFS, a);
051: assertEquals(WikiContext.PREFS, a.getRequestContext());
052:
053: // Ditto for group view context
054: a = CommandResolver.findCommand(WikiContext.VIEW_GROUP);
055: assertEquals(GroupCommand.VIEW_GROUP, a);
056: assertEquals(WikiContext.VIEW_GROUP, a.getRequestContext());
057:
058: // Looking for non-existent context; should result in exception
059: try {
060: a = CommandResolver.findCommand("nonExistentContext");
061: assertFalse("Context supported, strangely...", true);
062: } catch (IllegalArgumentException e) {
063: // Good; this is what we expect
064: }
065: }
066:
067: public void testFindWikiActionNoParams() {
068: Command a;
069: TestHttpServletRequest request = new TestHttpServletRequest();
070:
071: // Passing an EDIT request with no explicit page params means the EDIT action
072: a = resolver.findCommand(request, WikiContext.EDIT);
073: assertEquals(PageCommand.EDIT, a);
074: assertEquals("EditContent.jsp", a.getContentTemplate());
075: assertEquals("Edit.jsp", a.getJSP());
076: assertEquals("%uEdit.jsp?page=%n", a.getURLPattern());
077: assertNull(a.getTarget());
078:
079: // Ditto for prefs context
080: a = resolver.findCommand(request, WikiContext.PREFS);
081: assertEquals(WikiCommand.PREFS, a);
082: assertNull(a.getTarget());
083:
084: // Ditto for group view context
085: a = resolver.findCommand(request, WikiContext.VIEW_GROUP);
086: assertEquals(GroupCommand.VIEW_GROUP, a);
087: assertNull(a.getTarget());
088:
089: // Looking for non-existent context; should result in exception
090: try {
091: a = resolver.findCommand(request, "nonExistentContext");
092: assertFalse("Context supported, strangely...", true);
093: } catch (IllegalArgumentException e) {
094: // Good; this is what we expect
095: }
096:
097: // Request for "UserPreference.jsp" should resolve to PREFS action
098: request.setServletPath("/UserPreferences.jsp");
099: a = resolver.findCommand(request, WikiContext.EDIT);
100: assertEquals(WikiCommand.PREFS, a);
101: assertNull(a.getTarget());
102:
103: // Request for "NewGroup.jsp" should resolve to CREATE_GROUP action
104: // but targeted at the wiki
105: request.setServletPath("/NewGroup.jsp");
106: a = resolver.findCommand(request, WikiContext.EDIT);
107: assertNotSame(WikiCommand.CREATE_GROUP, a);
108: assertEquals(WikiCommand.CREATE_GROUP.getRequestContext(), a
109: .getRequestContext());
110: assertEquals(testEngine.getApplicationName(), a.getTarget());
111:
112: // But request for JSP not mapped to action should get default
113: request.setServletPath("/NonExistent.jsp");
114: a = resolver.findCommand(request, WikiContext.EDIT);
115: assertEquals(PageCommand.EDIT, a);
116: assertNull(a.getTarget());
117: }
118:
119: public void testFindWikiActionWithParams() throws Exception {
120: Command a;
121: WikiPage page = testEngine.getPage("SinglePage");
122:
123: // Passing an EDIT request with page param yields a wrapped action
124: TestHttpServletRequest request = new TestHttpServletRequest();
125: request.setParameter("page", "SinglePage");
126: request.setServletPath("/Edit.jsp?page=SinglePage");
127: a = resolver.findCommand(request, WikiContext.EDIT);
128: assertNotSame(PageCommand.EDIT, a);
129: assertEquals("EditContent.jsp", a.getContentTemplate());
130: assertEquals("Edit.jsp", a.getJSP());
131: assertEquals("%uEdit.jsp?page=%n", a.getURLPattern());
132: assertEquals(page, a.getTarget());
133:
134: // Passing an EDIT request with page=FindPage yields FIND action, *not* edit
135: request.setParameter("page", "FindPage");
136: request.setServletPath("/Edit.jsp?page=FindPage");
137: a = resolver.findCommand(request, WikiContext.EDIT);
138: assertEquals(WikiCommand.FIND, a);
139: assertEquals("FindContent.jsp", a.getContentTemplate());
140: assertEquals("Search.jsp", a.getJSP());
141: assertEquals("%uSearch.jsp", a.getURLPattern());
142: assertNull(a.getTarget());
143:
144: // Passing an EDIT request with group="Foo" yields wrapped VIEW_GROUP
145: request = new TestHttpServletRequest();
146: request.setParameter("group", "Foo");
147: request.setServletPath("/Group.jsp?group=Foo");
148: a = resolver.findCommand(request, WikiContext.EDIT);
149: assertNotSame(GroupCommand.VIEW_GROUP, a);
150: assertEquals("GroupContent.jsp", a.getContentTemplate());
151: assertEquals("Group.jsp", a.getJSP());
152: assertEquals("%uGroup.jsp?group=%n", a.getURLPattern());
153: assertEquals(new GroupPrincipal("Foo"), a.getTarget());
154: }
155:
156: public void testFindWikiActionWithPath() {
157: TestHttpServletRequest request;
158: Command a;
159:
160: // Passing an EDIT request with View JSP yields EDIT of the Front page
161: request = new TestHttpServletRequest();
162: request.setServletPath("/Wiki.jsp");
163: a = resolver.findCommand(request, WikiContext.EDIT);
164: assertNotNull(a.getTarget());
165: assertEquals(((WikiPage) a.getTarget()).getName(), testEngine
166: .getFrontPage());
167:
168: // Passing an EDIT request with Group JSP yields VIEW_GROUP
169: request.setServletPath("/Group.jsp");
170: a = resolver.findCommand(request, WikiContext.EDIT);
171: assertEquals(GroupCommand.VIEW_GROUP, a);
172: assertNull(a.getTarget());
173:
174: // Passing an EDIT request with UserPreferences JSP yields PREFS
175: request.setServletPath("/UserPreferences.jsp");
176: a = resolver.findCommand(request, WikiContext.EDIT);
177: assertEquals(WikiCommand.PREFS, a);
178: assertNull(a.getTarget());
179: }
180:
181: public void testFinalPageName() throws Exception {
182: String page;
183: page = resolver.getFinalPageName("SinglePage");
184: assertEquals("SinglePage", page);
185: page = resolver.getFinalPageName("SinglePages");
186: assertEquals("SinglePage", page);
187:
188: page = resolver.getFinalPageName("PluralPages");
189: assertEquals("PluralPages", page);
190: page = resolver.getFinalPageName("PluralPage");
191: assertEquals("PluralPages", page);
192:
193: page = resolver.getFinalPageName("NonExistentPage");
194: assertNull(page);
195: }
196:
197: public void testSpecialPageReference() {
198: String url;
199: url = resolver.getSpecialPageReference("RecentChanges");
200: assertEquals("http://localhost/RecentChanges.jsp", url);
201:
202: url = resolver.getSpecialPageReference("FindPage");
203: assertEquals("http://localhost/Search.jsp", url);
204:
205: url = resolver.getSpecialPageReference("FindPage");
206: assertEquals("http://localhost/Search.jsp", url);
207:
208: // UserPrefs doesn't exist in our test properties
209: url = resolver.getSpecialPageReference("UserPrefs");
210: assertNull(url);
211: }
212:
213: public static Test suite() {
214: return new TestSuite(CommandResolverTest.class);
215: }
216: }
|