Skip to content

Commit 965c09d

Browse files
committedMay 12, 2013
Taken care of trailing slash depending on if it is included or not which depends on the servlet container's Implementation of ServletContext.getRealPath - TRUNK-1859
1 parent 61fc1af commit 965c09d

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed
 

‎web/src/main/java/org/openmrs/module/web/WebModuleUtil.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -945,13 +945,24 @@ public static HttpServlet getServlet(String servletName) {
945945
* @return a path to a folder that stores web files or null if not in a web environment
946946
* @should return the correct module folder
947947
* @should return null if the dispatcher servlet is not yet set
948+
* @should return the correct module folder if real path has a trailing slash
948949
*/
949950
public static String getModuleWebFolder(String moduleId) {
950951
if (dispatcherServlet == null) {
951952
throw new ModuleException("Dispatcher servlet must be present in the web environment");
952953
}
954+
955+
String moduleFolder = "WEB-INF/view/module/";
953956
String realPath = dispatcherServlet.getServletContext().getRealPath("");
954-
String moduleWebFolder = (realPath + "WEB-INF/view/module/" + moduleId).replace("/", File.separator);
957+
String moduleWebFolder;
958+
if (realPath.endsWith("/"))
959+
moduleWebFolder = realPath + moduleFolder;
960+
else
961+
moduleWebFolder = realPath + "/" + moduleFolder;
962+
963+
moduleWebFolder += moduleId;
964+
moduleWebFolder.replace("/", File.separator);
965+
955966
return moduleWebFolder;
956967
}
957968

‎web/src/test/java/org/openmrs/module/web/WebModuleUtilTest.java

+25-7
Original file line numberDiff line numberDiff line change
@@ -118,22 +118,40 @@ public void getModuleWebFolder_shouldReturnNullIfTheDispatcherServletIsNotYetSet
118118
*/
119119
@Test
120120
public void getModuleWebFolder_shouldReturnTheCorrectModuleFolder() throws Exception {
121+
setupMocks(false);
122+
String moduleId = "basicmodule";
123+
String expectedPath = (REAL_PATH + "/WEB-INF/view/module/" + moduleId).replace("/", File.separator);
124+
String actualPath = WebModuleUtil.getModuleWebFolder(moduleId);
125+
126+
assertEquals(expectedPath, actualPath);
127+
}
128+
129+
/**
130+
* @see WebModuleUtil#getModuleWebFolder(String)
131+
* @verifies return the correct module folder if real path has a trailing slash
132+
*/
133+
@Test
134+
public void getModuleWebFolder_shouldReturnTheCorrectModuleFolderIfRealPathHasATrailingSlash() throws Exception {
135+
setupMocks(true);
136+
String moduleId = "basicmodule";
137+
String expectedPath = (REAL_PATH + "/WEB-INF/view/module/" + moduleId).replace("/", File.separator);
138+
String actualPath = WebModuleUtil.getModuleWebFolder(moduleId);
139+
140+
assertEquals(expectedPath, actualPath);
141+
}
142+
143+
private static void setupMocks(boolean includeTrailingSlash) {
121144
ServletConfig servletConfig = mock(ServletConfig.class);
122145

123146
ServletContext servletContext = mock(ServletContext.class);
124-
when(servletContext.getRealPath("")).thenReturn(REAL_PATH);
147+
String realPath = (includeTrailingSlash) ? REAL_PATH + "/" : REAL_PATH;
148+
when(servletContext.getRealPath("")).thenReturn(realPath);
125149

126150
DispatcherServlet dispatcherServlet = mock(DispatcherServlet.class);
127151
when(dispatcherServlet.getServletConfig()).thenReturn(servletConfig);
128152
when(dispatcherServlet.getServletContext()).thenReturn(servletContext);
129153

130154
WebModuleUtil.setDispatcherServlet(dispatcherServlet);
131-
132-
String moduleId = "basicmodule";
133-
String expectedPath = (REAL_PATH + "WEB-INF/view/module/" + moduleId).replace("/", File.separator);
134-
String actualPath = WebModuleUtil.getModuleWebFolder(moduleId);
135-
136-
assertEquals(expectedPath, actualPath);
137155
}
138156

139157
}

0 commit comments

Comments
 (0)
Please sign in to comment.