/aerie/test/aerie/servlet/UriParserTest.java

http://aerie.googlecode.com/ · Java · 128 lines · 86 code · 18 blank · 24 comment · 0 complexity · d064f9d03eab1e4bf3c76dcc429a7ab3 MD5 · raw file

  1. /**
  2. * Copyright (c) 2006 George Malamidis, nutrun.com
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  5. * software and associated documentation files (the "Software"), to deal in the Software
  6. * without restriction, including without limitation the rights to use, copy, modify,
  7. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  8. * permit persons to whom the Software is furnished to do so, subject to the following
  9. * conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all copies
  12. * or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  15. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  16. * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  18. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  19. * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. /**
  22. * $Author$
  23. * $Rev$
  24. */
  25. package aerie.servlet;
  26. import aerie.context.AerieContext;
  27. import junit.framework.TestCase;
  28. public class UriParserTest extends TestCase {
  29. private AerieContext context;
  30. protected void setUp() {
  31. context = new AerieContext();
  32. }
  33. public void testShouldExtractControllerFromUri() {
  34. context.path = "";
  35. context.webAppContext = "";
  36. String uri = "/controller/action/1";
  37. assertEquals("controller", new UriParser(uri, context).extractController());
  38. }
  39. public void testShouldDefaultToWelcomeControllerIfControllerNotFoundInUri() {
  40. context.welcomeController = "welcome";
  41. context.path = "";
  42. context.webAppContext = "";
  43. assertEquals("welcome", new UriParser("/", context).extractController());
  44. }
  45. public void testShouldHaveWelcomeControllerForNonDefaultWebAppContext() {
  46. context.path = "";
  47. context.welcomeController = "welcome";
  48. context.webAppContext = "/webAppContext";
  49. assertEquals("welcome", new UriParser("/webAppContext", context).extractController());
  50. }
  51. public void testShouldHaveWelcomeControllerForNonRootContextPath() {
  52. context.path = "private";
  53. context.welcomeController = "welcome";
  54. context.webAppContext = "";
  55. assertEquals("private/welcome", new UriParser("/private", context).extractController());
  56. }
  57. public void testShouldExtractActionFromUri() {
  58. context.path = "";
  59. context.webAppContext = "";
  60. assertEquals("action", new UriParser("/controller/action/1", context).extractAction());
  61. }
  62. public void testshouldDefaultToIndexIfNoActionInUri() {
  63. context.path = "";
  64. context.webAppContext = "";
  65. assertEquals("index", new UriParser("/controller", context).extractAction());
  66. }
  67. public void testShouldExtractParameterFromUri() {
  68. context.path = "";
  69. context.webAppContext = "";
  70. assertEquals("1", new UriParser("/controller/action/1", context).extractParameter());
  71. }
  72. public void testShouldReturnNullIfNoParameterInUri() {
  73. context.path = "";
  74. context.webAppContext = "";
  75. assertNull(new UriParser("/controller", context).extractParameter());
  76. }
  77. public void testShouldHandleUriWithNonRootWebAppContext() {
  78. context.path = "";
  79. context.webAppContext = "/context";
  80. assertEquals("controller", new UriParser("/context/controller", context).extractController());
  81. assertEquals("action", new UriParser("/context/controller/action", context).extractAction());
  82. assertEquals("1", new UriParser("/context/controller/action/1", context).extractParameter());
  83. }
  84. public void testShouldHandleUriWithNonRootContextPath() {
  85. context.path = "private";
  86. context.webAppContext = "";
  87. assertEquals("private/controller", new UriParser("/private/controller", context).extractController());
  88. assertEquals("action", new UriParser("/private/controller/action", context).extractAction());
  89. assertEquals("1", new UriParser("/private/controller/action/1", context).extractParameter());
  90. }
  91. public void testShouldHandleUriWithCompositeContextPath() {
  92. context.path = "/private/users";
  93. context.webAppContext = "";
  94. assertEquals("/private/users/controller", new UriParser("/private/users/controller", context).extractController());
  95. assertEquals("action", new UriParser("/private/users/controller/action", context).extractAction());
  96. assertEquals("1", new UriParser("/private/users/controller/action/1", context).extractParameter());
  97. }
  98. public void testShouldHandleUriWithNonRootContextPathAndNonRootWebAppContext() {
  99. context.path = "path";
  100. context.webAppContext = "/context";
  101. assertEquals("path/controller", new UriParser("/context/path/controller", context).extractController());
  102. assertEquals("action", new UriParser("/context/path/controller/action", context).extractAction());
  103. assertEquals("1", new UriParser("/context/path/controller/action/1", context).extractParameter());
  104. }
  105. public void testShouldLowerCaseController() {
  106. context.path = "";
  107. context.webAppContext = "";
  108. assertEquals("controller", new UriParser("/Controller", context).extractController());
  109. }
  110. }