/sitebricks/src/test/java/com/google/sitebricks/routing/PageBookImplTest.java

http://github.com/dhanji/sitebricks · Java · 814 lines · 612 code · 193 blank · 9 comment · 8 complexity · a86b4d23f9f64f7c29be0003ab21e632 MD5 · raw file

  1. package com.google.sitebricks.routing;
  2. import static org.easymock.EasyMock.createMock;
  3. import static org.easymock.EasyMock.expect;
  4. import static org.easymock.EasyMock.replay;
  5. import java.io.IOException;
  6. import java.util.Date;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.Set;
  11. import javax.servlet.http.HttpServletRequest;
  12. import org.testng.annotations.BeforeTest;
  13. import org.testng.annotations.DataProvider;
  14. import org.testng.annotations.Test;
  15. import com.google.common.collect.Iterators;
  16. import com.google.inject.Guice;
  17. import com.google.inject.Injector;
  18. import com.google.inject.name.Named;
  19. import com.google.sitebricks.At;
  20. import com.google.sitebricks.Renderable;
  21. import com.google.sitebricks.Respond;
  22. import com.google.sitebricks.SitebricksModule;
  23. import com.google.sitebricks.TestRequestCreator;
  24. import com.google.sitebricks.headless.Reply;
  25. import com.google.sitebricks.headless.Request;
  26. import com.google.sitebricks.http.Get;
  27. import com.google.sitebricks.http.Post;
  28. import com.google.sitebricks.http.Select;
  29. import com.google.sitebricks.rendering.EmbedAs;
  30. /**
  31. * @author Dhanji R. Prasanna (dhanji@gmail.com)
  32. */
  33. public class PageBookImplTest {
  34. private static final String FIRST_PATH_ELEMENTS = "firstPathElements";
  35. private static final String URI_TEMPLATES_AND_MATCHES = "uriTemplatesAndMatches";
  36. private static final String NOT_URIS_AND_TEMPLATES = "noturisandTemplates";
  37. private static final String REDIRECTED_GET = "/redirected_get";
  38. private static final String REDIRECTED_POST = "/redirected_post";
  39. private Injector injector;
  40. @BeforeTest
  41. public final void pre() {
  42. injector = Guice.createInjector(new SitebricksModule());
  43. }
  44. @Test
  45. public final void storeAndRetrievePageInstance() {
  46. final Respond respond = new MockRespond();
  47. Renderable mock = new Renderable() {
  48. public void render(Object bound, Respond respond) {
  49. }
  50. public <T extends Renderable> Set<T> collect(Class<T> clazz) {
  51. return null;
  52. }
  53. };
  54. final PageBook pageBook = new DefaultPageBook(injector);
  55. pageBook.at("/wiki", MyPage.class);
  56. PageBook.Page page = pageBook.get("/wiki");
  57. page.apply(mock);
  58. page.widget().render(new Object(), respond);
  59. assert page.widget().equals(mock);
  60. }
  61. @Test
  62. public final void fireGetMethodOnPage() throws IOException {
  63. Renderable mock = new Renderable() {
  64. public void render(Object bound, Respond respond) {
  65. }
  66. public <T extends Renderable> Set<T> collect(Class<T> clazz) {
  67. return null;
  68. }
  69. };
  70. final PageBook pageBook = new DefaultPageBook(injector);
  71. pageBook.at("/wiki", MyPage.class);
  72. PageBook.Page page = pageBook.get("/wiki");
  73. page.apply(mock);
  74. final MyPage bound = new MyPage();
  75. page.doMethod("get", bound, "/wiki", fakeRequestWithParams(new HashMap<String, String[]>()));
  76. assert page.widget().equals(mock);
  77. assert bound.getted : "@Get method was not fired, on doGet()";
  78. }
  79. @Test
  80. public final void fireGetMethodOnPageAndRedirectToURL() throws IOException {
  81. Renderable mock = new Renderable() {
  82. public void render(Object bound, Respond respond) {
  83. }
  84. public <T extends Renderable> Set<T> collect(Class<T> clazz) {
  85. return null;
  86. }
  87. };
  88. final PageBook pageBook = new DefaultPageBook(injector);
  89. pageBook.at("/wiki", MyRedirectingPage.class);
  90. PageBook.Page page = pageBook.get("/wiki");
  91. page.apply(mock);
  92. final MyRedirectingPage bound = new MyRedirectingPage();
  93. Object redirect = page.doMethod("get", bound, "/wiki", fakeRequestWithParams(new HashMap<String, String[]>()));
  94. assert REDIRECTED_GET.equals(redirect);
  95. assert page.widget().equals(mock);
  96. }
  97. @Test
  98. public final void firePostMethodOnPageAndRedirectToURL() throws IOException {
  99. Renderable mock = new
  100. Renderable() {
  101. public void render(Object bound, Respond respond) {
  102. }
  103. public <T extends Renderable> Set<T> collect(Class<T> clazz) {
  104. return null;
  105. }
  106. };
  107. final PageBook pageBook = new DefaultPageBook(injector);
  108. pageBook.at("/wiki", MyRedirectingPage.class);
  109. PageBook.Page page = pageBook.get("/wiki");
  110. page.apply(mock);
  111. final MyRedirectingPage bound = new MyRedirectingPage();
  112. Object redirect = page.doMethod("post", bound, "/wiki", fakeRequestWithParams(new HashMap<String, String[]>()));
  113. assert REDIRECTED_POST.equals(redirect);
  114. assert page.widget().equals(mock);
  115. }
  116. @Test
  117. public final void fireGetMethodOnPageAndReply403() throws IOException {
  118. Renderable mock = new Renderable() {
  119. public void render(Object bound, Respond respond) {
  120. }
  121. public <T extends Renderable> Set<T> collect(Class<T> clazz) {
  122. return null;
  123. }
  124. };
  125. final PageBook pageBook = new DefaultPageBook(injector);
  126. pageBook.at("/forbidden", MyForbiddenPage.class);
  127. PageBook.Page page = pageBook.get("/forbidden");
  128. page.apply(mock);
  129. final MyForbiddenPage bound = new MyForbiddenPage();
  130. Object redirect = page.doMethod("get", bound, "/forbidden", fakeRequestWithParams(new HashMap<String, String[]>()));
  131. assert Reply.saying().forbidden().equals(redirect);
  132. assert page.widget().equals(mock);
  133. }
  134. @Test
  135. public final void firePostMethodOnPageAndReply403() throws IOException {
  136. Renderable mock = new Renderable() {
  137. public void render(Object bound, Respond respond) {
  138. }
  139. public <T extends Renderable> Set<T> collect(Class<T> clazz) {
  140. return null;
  141. }
  142. };
  143. final PageBook pageBook = new DefaultPageBook(injector);
  144. pageBook.at("/forbidden", MyForbiddenPage.class);
  145. PageBook.Page page = pageBook.get("/forbidden");
  146. page.apply(mock);
  147. final MyForbiddenPage bound = new MyForbiddenPage();
  148. Object redirect = page.doMethod("post", bound, "/forbidden", fakeRequestWithParams(new HashMap<String, String[]>()));
  149. assert Reply.saying().forbidden().equals(redirect);
  150. assert page.widget().equals(mock);
  151. }
  152. @Test
  153. public final void fireGetMethodOnPageToCorrectHandler() throws IOException {
  154. Renderable mock = new Renderable() {
  155. public void render(Object bound, Respond respond) {
  156. }
  157. public <T extends Renderable> Set<T> collect(Class<T> clazz) {
  158. return null;
  159. }
  160. };
  161. Map<String, String[]> params = new HashMap<String, String[]>() {
  162. {
  163. put("event", new String[]{"1", "2"});
  164. }
  165. };
  166. final PageBook pageBook = new DefaultPageBook(injector);
  167. pageBook.at("/wiki", MyEventSupportingPage.class);
  168. PageBook.Page page = pageBook.get("/wiki");
  169. page.apply(mock);
  170. final MyEventSupportingPage bound = new MyEventSupportingPage();
  171. page.doMethod("get", bound, "/wiki", fakeRequestWithParams(params));
  172. assert page.widget().equals(mock);
  173. assert bound.getted1 : "@Get @On method was not fired, on doGet() for [event=1]";
  174. assert bound.getted2 : "@Get @On method was not fired, on doGet() for [event=2]";
  175. }
  176. @Test
  177. public final void firePostMethodOnPageToCorrectHandler() throws IOException {
  178. Renderable mock = new Renderable() {
  179. public void render(Object bound, Respond respond) {
  180. }
  181. public <T extends Renderable> Set<T> collect(Class<T> clazz) {
  182. return null;
  183. }
  184. };
  185. Map<String, String[]> params = new HashMap<String, String[]>() {
  186. {
  187. put("event", new String[]{"1", "2"});
  188. }
  189. };
  190. final PageBook pageBook = new DefaultPageBook(injector);
  191. pageBook.at("/wiki", MyEventSupportingPage.class);
  192. PageBook.Page page = pageBook.get("/wiki");
  193. page.apply(mock);
  194. final MyEventSupportingPage bound = new MyEventSupportingPage();
  195. page.doMethod("post", bound, "/wiki", fakeRequestWithParams(params));
  196. assert page.widget().equals(mock);
  197. assert bound.posted1 : "@Post @On method was not fired, on doPost() for [event=1]";
  198. assert bound.posted2 : "@Post @On method was not fired, on doPost() for [event=2]";
  199. }
  200. @Test
  201. public final void fireGetMethodOnPageToCorrectHandlerOnlyOnce() throws IOException {
  202. Renderable mock = new Renderable() {
  203. public void render(Object bound, Respond respond) {
  204. }
  205. public <T extends Renderable> Set<T> collect(Class<T> clazz) {
  206. return null;
  207. }
  208. };
  209. Map<String, String[]> params = new HashMap<String, String[]>() {
  210. {
  211. put("event", new String[]{"2"});
  212. }
  213. };
  214. final PageBook pageBook = new DefaultPageBook(injector);
  215. pageBook.at("/wiki", MyEventSupportingPage.class);
  216. PageBook.Page page = pageBook.get("/wiki");
  217. page.apply(mock);
  218. final MyEventSupportingPage bound = new MyEventSupportingPage();
  219. page.doMethod("get", bound, "/wiki", fakeRequestWithParams(params));
  220. assert page.widget().equals(mock);
  221. assert !bound.getted1 : "@Get @On method was fired, on doGet() for [event=1]";
  222. assert bound.getted2 : "@Get @On method was not fired, on doGet() for [event=2]";
  223. }
  224. @Test
  225. public final void firePostMethodOnPageToCorrectHandlerOnlyOnce() throws IOException {
  226. Renderable mock = new Renderable() {
  227. public void render(Object bound, Respond respond) {
  228. }
  229. public <T extends Renderable> Set<T> collect(Class<T> clazz) {
  230. return null;
  231. }
  232. };
  233. Map<String, String[]> params = new HashMap<String, String[]>() {
  234. {
  235. put("event", new String[]{"2"});
  236. }
  237. };
  238. final PageBook pageBook = new DefaultPageBook(injector);
  239. pageBook.at("/wiki", MyEventSupportingPage.class);
  240. PageBook.Page page = pageBook.get("/wiki");
  241. page.apply(mock);
  242. final MyEventSupportingPage bound = new MyEventSupportingPage();
  243. page.doMethod("post", bound, "/wiki", fakeRequestWithParams(params));
  244. assert page.widget().equals(mock);
  245. assert !bound.posted1 : "@Post @On method was fired, on doGet() for [event=1]";
  246. assert bound.posted2 : "@Post @On method was not fired, on doGet() for [event=2]";
  247. }
  248. @Test
  249. public final void fireGetMethodOnPageToDefaultHandler() throws IOException {
  250. Renderable mock = new Renderable() {
  251. public void render(Object bound, Respond respond) {
  252. }
  253. public <T extends Renderable> Set<T> collect(Class<T> clazz) {
  254. return null;
  255. }
  256. };
  257. Map<String, String[]> params = new HashMap<String, String[]>() {
  258. {
  259. put("event", new String[]{"3"});
  260. }
  261. };
  262. final PageBook pageBook = new DefaultPageBook(injector);
  263. pageBook.at("/wiki", MyEventSupportingPage.class);
  264. PageBook.Page page = pageBook.get("/wiki");
  265. page.apply(mock);
  266. final MyEventSupportingPage bound = new MyEventSupportingPage();
  267. page.doMethod("get", bound, "/wiki", fakeRequestWithParams(params));
  268. assert page.widget().equals(mock);
  269. assert !bound.getted1 : "@Get @On method was fired, on doGet() for [event=1]";
  270. assert !bound.getted2 : "@Get @On method was fired, on doGet() for [event=2]";
  271. assert bound.defaultGet : "@Get @On default method was not fired, on doGet() for [event=...]";
  272. }
  273. @Test
  274. public final void firePostMethodOnPageToDefaultHandler() throws IOException {
  275. Renderable mock = new Renderable() {
  276. public void render(Object bound, Respond respond) {
  277. }
  278. public <T extends Renderable> Set<T> collect(Class<T> clazz) {
  279. return null;
  280. }
  281. };
  282. Map<String, String[]> params = new HashMap<String, String[]>() {
  283. {
  284. put("event", new String[]{"3"});
  285. }
  286. };
  287. final PageBook pageBook = new DefaultPageBook(injector);
  288. pageBook.at("/wiki", MyEventSupportingPage.class);
  289. PageBook.Page page = pageBook.get("/wiki");
  290. page.apply(mock);
  291. final MyEventSupportingPage bound = new MyEventSupportingPage();
  292. page.doMethod("post", bound, "/wiki", fakeRequestWithParams(params));
  293. assert page.widget().equals(mock);
  294. assert !bound.getted2 : "@Get @On method was fired, on doPost() for [event=2]";
  295. assert !bound.getted1 : "@Get @On method was fired, on doPost() for [event=1]";
  296. assert !bound.posted1 : "@Post @On method was fired, on doPost() for [event=1]";
  297. assert !bound.posted2 : "@Post @On method was fired, on doPost() for [event=2]";
  298. assert bound.defaultPost : "@Post @On default method was not fired, on doPost() for [event=...]";
  299. }
  300. @Test
  301. public final void fireGetMethodWithArgsOnPage() throws IOException {
  302. Renderable mock = new Renderable() {
  303. public void render(Object bound, Respond respond) {
  304. }
  305. public <T extends Renderable> Set<T> collect(Class<T> clazz) {
  306. return null;
  307. }
  308. };
  309. final PageBook pageBook = new DefaultPageBook(injector);
  310. pageBook.at("/wiki/:title", MyPageWithTemplate.class);
  311. PageBook.Page page = pageBook.get("/wiki/IMAX");
  312. page.apply(mock);
  313. final MyPageWithTemplate bound = new MyPageWithTemplate();
  314. page.doMethod("get", bound, "/wiki/IMAX", fakeRequestWithParams(new HashMap<String, String[]>()));
  315. assert page.widget().equals(mock);
  316. assert "IMAX".equals(bound.title) : "@Get method was not fired, on doGet() with the right arg, instead: " + bound.title;
  317. }
  318. @Test
  319. public final void fireGetMethodWithPrimitiveArgsOnPage() throws IOException {
  320. Renderable mock = new Renderable() {
  321. public void render(Object bound, Respond respond) {
  322. }
  323. public <T extends Renderable> Set<T> collect(Class<T> clazz) {
  324. return null;
  325. }
  326. };
  327. // SimpleDateFormat sdf = new SimpleDateFormat(StringToDateTimeCalendarConverter.USA_SHORT);
  328. final PageBook pageBook = new DefaultPageBook(injector);
  329. // pageBook.at("/wiki/:title/cat/:int/:bool/:float/:date", MyPageWithPrimitivesTemplate.class);
  330. pageBook.at("/wiki/:title/cat/:int/:bool/:float", MyPageWithPrimitivesTemplate.class);
  331. String targetURL = "/wiki/IMAX/cat/1/true/2.5";
  332. PageBook.Page page = pageBook.get(targetURL);
  333. page.apply(mock);
  334. final MyPageWithPrimitivesTemplate bound = new MyPageWithPrimitivesTemplate();
  335. page.doMethod("get", bound, targetURL, fakeRequestWithParams(new HashMap<String, String[]>()));
  336. assert page.widget().equals(mock);
  337. assert "IMAX".equals(bound.title) && bound.id == 1 && bound.bool == true && bound.flt == 2.5
  338. // && sdf.format(date).equals(sdf.format(bound.date)):
  339. : "@Get method did not bind in args correctly, title: " + bound.title +
  340. // " id: " + bound.id + " bool: " + bound.bool + " float: " + bound.flt + " date: " + sdf.format(bound.date);
  341. " id: " + bound.id + " bool: " + bound.bool + " float: " + bound.flt;
  342. ;
  343. }
  344. @Test
  345. public final void firePostMethodWithArgsOnPage() throws IOException {
  346. Renderable mock = new
  347. Renderable() {
  348. public void render(Object bound, Respond respond) {
  349. }
  350. public <T extends Renderable> Set<T> collect(Class<T> clazz) {
  351. return null;
  352. }
  353. };
  354. final PageBook pageBook = new DefaultPageBook(injector);
  355. pageBook.at("/wiki/:title/cat/:id", MyPageWithTemplate.class);
  356. PageBook.Page page = pageBook.get("/wiki/IMAX_P/cat/12");
  357. page.apply(mock);
  358. final MyPageWithTemplate bound = new MyPageWithTemplate();
  359. page.doMethod("post", bound, "/wiki/IMAX_P/cat/12", fakeRequestWithParams(new HashMap<String, String[]>()));
  360. assert page.widget().equals(mock);
  361. assert "IMAX_P".equals(bound.post) && "12".equals(bound.id)
  362. : "@Post method was not fired, on doPost() with the right arg, instead: " + bound.post;
  363. }
  364. @Test(expectedExceptions = InvalidEventHandlerException.class)
  365. public final void errorOnPostMethodWithUnnamedArgs() throws IOException {
  366. Renderable mock = new Renderable() {
  367. public void render(Object bound, Respond respond) {
  368. }
  369. public <T extends Renderable> Set<T> collect(Class<T> clazz) {
  370. return null;
  371. }
  372. };
  373. final PageBook pageBook = new DefaultPageBook(injector);
  374. pageBook.at("/wiki/:title/cat/:id", MyBrokenPageWithTemplate.class);
  375. PageBook.Page page = pageBook.get("/wiki/IMAX_P/cat/12");
  376. final MyBrokenPageWithTemplate bound = new MyBrokenPageWithTemplate();
  377. page.doMethod("post", bound, "/wiki/IMAX_P/cat/12", fakeRequestWithParams(new HashMap<String, String[]>()));
  378. assert page.widget().equals(mock);
  379. }
  380. @Test
  381. public final void firePostMethodOnPage() throws IOException {
  382. Renderable mock = new Renderable() {
  383. public void render(Object bound, Respond respond) {
  384. }
  385. public <T extends Renderable> Set<T> collect(Class<T> clazz) {
  386. return null;
  387. }
  388. };
  389. final PageBook pageBook = new DefaultPageBook(injector);
  390. pageBook.at("/wiki", MyPage.class);
  391. PageBook.Page page = pageBook.get("/wiki");
  392. final MyPage bound = new MyPage();
  393. page.apply(mock);
  394. page.doMethod("post", bound, "/wiki", fakeRequestWithParams(new HashMap<String, String[]>()));
  395. assert page.widget().equals(mock);
  396. assert bound.posted : "@Post method was not fired, on doPost()";
  397. }
  398. @DataProvider(name = URI_TEMPLATES_AND_MATCHES)
  399. public Object[][] getUriTemplatesAndMatches() {
  400. return new Object[][]{
  401. {"/wiki/:title", "/wiki/HelloPage"},
  402. {"/wiki/:title", "/wiki/HelloPage%20"},
  403. {"/wiki/:title/dude", "/wiki/HelloPage/dude"},
  404. {"/:title/thing", "/wiki/thing"},
  405. {"/:title", "/aposkdapoksd"},
  406. };
  407. }
  408. @Test(dataProvider = URI_TEMPLATES_AND_MATCHES)
  409. public final void matchPageByUriTemplate(final String template, final String toMatch) {
  410. final Respond respond = new MockRespond();
  411. Renderable mock = new Renderable() {
  412. public void render(Object bound, Respond respond) {
  413. }
  414. public <T extends Renderable> Set<T> collect(Class<T> clazz) {
  415. return null;
  416. }
  417. };
  418. final PageBook pageBook = new DefaultPageBook(injector);
  419. pageBook.at(template, MyPage.class);
  420. PageBook.Page page = pageBook.get(toMatch);
  421. final MyPage myPage = new MyPage();
  422. page.apply(mock);
  423. page.widget().render(myPage, respond);
  424. assert mock.equals(page.widget());
  425. }
  426. @DataProvider(name = NOT_URIS_AND_TEMPLATES)
  427. public Object[][] getNotUriTemplatesAndMatches() {
  428. return new Object[][]{
  429. {"/wiki/:title", "/tiki/HelloPage"},
  430. {"/wiki/:title", "/wiki/HelloPage%20/didle"},
  431. {"/wiki/:title/dude", "/wiki/HelloPage"},
  432. {"/:title/thing", "/wiki/thing/thingaling"},
  433. {"/:title", "/aposkdapoksd/12"},
  434. };
  435. }
  436. @Test(dataProvider = NOT_URIS_AND_TEMPLATES)
  437. public final void notMatchPageByUriTemplate(final String template, final String toMatch) {
  438. final PageBook pageBook = new DefaultPageBook(injector);
  439. pageBook.at(template, MyPage.class);
  440. //cant find
  441. assert null == pageBook.get(toMatch);
  442. }
  443. public static Request fakeRequestWithParams(Map<String, String[]> map) {
  444. HttpServletRequest request = createMock(HttpServletRequest.class);
  445. expect(request.getParameterMap()).andReturn(map);
  446. expect(request.getHeaderNames()).andReturn(Iterators.asEnumeration(Iterators.<Object>emptyIterator()));
  447. replay(request);
  448. return TestRequestCreator.from(request, null);
  449. }
  450. @At("/wiki")
  451. @Select("event")
  452. public static class MyEventSupportingPage {
  453. private boolean getted1;
  454. private boolean getted2;
  455. private boolean posted1;
  456. private boolean posted2;
  457. private boolean defaultGet;
  458. private boolean defaultPost;
  459. @Get("1")
  460. public void get1() {
  461. getted1 = true;
  462. }
  463. @Get("2")
  464. public void get2() {
  465. getted2 = true;
  466. }
  467. @Get
  468. public void defaultGet() {
  469. defaultGet = true;
  470. }
  471. @Post("1")
  472. public void post1() {
  473. posted1 = true;
  474. }
  475. @Post("2")
  476. public void post2() {
  477. posted2 = true;
  478. }
  479. @Post
  480. public void defaultPost() {
  481. defaultPost = true;
  482. }
  483. }
  484. @At("/wiki")
  485. @EmbedAs("Hi")
  486. public static class MyPage {
  487. private boolean getted;
  488. private boolean posted;
  489. @Get
  490. public void get() {
  491. getted = true;
  492. }
  493. @Post
  494. public void post() {
  495. posted = true;
  496. }
  497. }
  498. @At("/wiki/:title/cat/:id")
  499. @EmbedAs("Hi")
  500. public static class MyPageWithTemplate {
  501. private String title;
  502. private boolean posted;
  503. private String post;
  504. private String id;
  505. @Get
  506. public void get(@Named("title") String title) {
  507. this.title = title;
  508. }
  509. @Post
  510. public void post(@Named("title") String title, @Named("id") String id) {
  511. this.post = title;
  512. this.id = id;
  513. }
  514. }
  515. @At("/wiki/:title/cat/:int/:bool/:float/:date")
  516. @EmbedAs("Hi")
  517. public static class MyPageWithPrimitivesTemplate {
  518. private String title;
  519. private int id;
  520. private boolean bool;
  521. private Float flt;
  522. private Date date;
  523. @Get
  524. public void get(@Named("title") String title, @Named("int") Integer id, @Named("bool") Boolean bool,
  525. // @Named("float") float flt, @Named("date") Date date) {
  526. @Named("float") float flt) {
  527. this.title = title;
  528. this.id = id;
  529. this.bool = bool;
  530. this.flt = flt;
  531. }
  532. }
  533. @At("/wiki/:title/cat/:id")
  534. @EmbedAs("Hi")
  535. public static class MyBrokenPageWithTemplate {
  536. @Post
  537. public void post(@Named("title") String title, int x, @Named("id") String id) {
  538. }
  539. }
  540. @DataProvider(name = FIRST_PATH_ELEMENTS)
  541. public Object[][] get() {
  542. return new Object[][]{
  543. {"/wiki/:title", "wiki"},
  544. {"/wiki/:title/:thing", "wiki"},
  545. {"/wiki/other/thing/dude", "wiki"},
  546. {"/wiki", "wiki"},
  547. {"/wiki/", "wiki"},
  548. {"/", ""},
  549. };
  550. }
  551. @Test(dataProvider = FIRST_PATH_ELEMENTS)
  552. public final void firstPathElement(final String uri, final String answer) {
  553. final String fPath = new DefaultPageBook(injector)
  554. .firstPathElement(uri);
  555. assert answer.equals(fPath) : "wrong path: " + fPath;
  556. }
  557. private static class MockRespond implements Respond {
  558. public void write(String text) {
  559. }
  560. public HtmlTagBuilder withHtml() {
  561. throw new AssertionError();
  562. }
  563. public void write(char c) {
  564. }
  565. public void chew() {
  566. }
  567. public void writeToHead(String text) {
  568. }
  569. public void require(String requireString) {
  570. }
  571. public void redirect(String to) {
  572. }
  573. public String getContentType() {
  574. return null;
  575. }
  576. public String getRedirect() {
  577. return null;
  578. }
  579. public Renderable include(String argument) {
  580. return null;
  581. }
  582. public String getHead() {
  583. return null;
  584. }
  585. @Override
  586. public void clear() {
  587. }
  588. @Override public Object pageObject() {
  589. return null;
  590. }
  591. @Override
  592. public List<String> getErrors() {
  593. return null;
  594. }
  595. @Override
  596. public void setErrors(List<String> errors) {
  597. }
  598. }
  599. @At("/wiki")
  600. private class MyRedirectingPage {
  601. @Get
  602. public String get() {
  603. return REDIRECTED_GET;
  604. }
  605. @Post
  606. public String post() {
  607. return REDIRECTED_POST;
  608. }
  609. }
  610. @At("/forbidden")
  611. private class MyForbiddenPage {
  612. @Get
  613. public Object get() {
  614. return Reply.saying().forbidden();
  615. }
  616. @Post
  617. public Object post() {
  618. return Reply.saying().forbidden();
  619. }
  620. }
  621. }