1 20 package org.openi.web.controller.admin; 21 22 import org.openi.application.Application; 23 import org.openi.util.*; 24 import org.openi.web.controller.BaseControllerTestCase; 25 import org.springframework.mock.web.*; 26 import org.springframework.web.servlet.ModelAndView; 27 import java.io.File ; 28 import java.util.*; 29 import javax.servlet.http.HttpServletResponse ; 30 31 32 40 public class ManageFilesFormControllerTest extends BaseControllerTestCase { 41 private static String dirNameForAddTest = "newtest"; 43 44 private static String dirNameForRenameTest = "newtest1"; 46 47 private static String testPath = ""; 50 private ManageFilesFormController ac1; 51 private ManageFilesFormController ac2; 52 private MockHttpServletRequest request; 53 private MockHttpServletRequest postrequest; 54 private HttpServletResponse response; 55 private ModelAndView mv; 56 57 62 public void setUp() throws Exception { 63 logger.info("Setting up"); 64 65 super.setUp(); 66 67 ac1 = (ManageFilesFormController) appContext.getBean( 68 "manageFilesFormController"); 69 ac2 = new ManageFilesFormController(); 70 71 ac2.setApplicationContext(webAppContext); 72 request = newGet("/managefiles.htm"); 73 postrequest = newPost("/managefiles.htm"); 74 request.getSession() 75 .setAttribute("projectContext", getTestProjectContext()); 76 postrequest.getSession() 77 .setAttribute("projectContext", getTestProjectContext()); 78 response = new MockHttpServletResponse(); 79 } 80 81 86 public void testControllerConfig() throws Exception { 87 logger.info("Testing form"); 88 89 assertFalse("Session form is not false", ac1.isSessionForm()); 90 assertEquals("Command name is not equal", "model", ac1.getCommandName()); 91 assertEquals("Command class type is not equal", "java.util.Map", 92 ac1.getCommandClass().getName()); 93 assertEquals("Form view name is not equal", "manageFilesFormView", 94 ac1.getFormView()); 95 assertEquals("Success view is not equal", "manageFilesRedirect", 96 ac1.getSuccessView()); 97 assertEquals("Folder UI xsl template is not equal", 98 "WEB-INF/openi/folderui.xsl", ac1.getXslTemplate()); 99 } 100 101 private void initController() { 102 ac2.setSessionForm(ac1.isSessionForm()); 103 ac2.setFormView(ac1.getFormView()); 104 ac2.setSuccessView(ac1.getSuccessView()); 105 ac2.setCommandClass(ac1.getCommandClass()); 106 ac2.setCommandName(ac1.getCommandName()); 107 ac2.setXslTemplate("openi/" + ac1.getXslTemplate()); 109 } 110 111 115 public void testGet() throws Exception { 116 initController(); 117 mv = ac2.handleRequest(request, response); 118 assertNotNull("Model is null", mv); 119 120 Map command = (Map) mv.getModel().get(ac2.getCommandName()); 121 assertNotNull("command is null", command); 122 assertNotNull("Folder Html is null", command.get("FolderXML")); 123 assertTrue("Folder html is empty", 124 command.get("FolderXML").toString().length() > 0); 125 testPath = getFirstSubFolderName(ac2.getFolderRoot()); 126 assertNotNull("There is no folder under project root to test for", 127 testPath); 128 } 129 130 136 private String getFirstSubFolderName(Folder root) { 137 List folders = (List) root.getChildren(); 138 139 Iterator iter = folders.iterator(); 141 String folderpath = null; 142 143 while (iter.hasNext()) { 144 Object object = iter.next(); 145 146 if (object instanceof Folder) { 147 folderpath = ((Folder) object).getPath(); 148 149 break; 150 } 151 } 152 153 return folderpath; 154 } 155 156 159 public void testCreateFolderAlreadyExists() throws Exception { 160 assertNotNull("There is no folder under project root to test for", 161 testPath); 162 163 String testdir = getTestProjectContext().getProjectDirectory(); 164 String fullpath = testdir + "/" + dirNameForAddTest; 165 File adddir = new File (fullpath); 166 assertFalse("the folder '" + dirNameForAddTest 167 + "' you specified is already exists. Please select another name for 'dirNameForAddTest' property ", 168 adddir.exists()); 169 fullpath = testdir + "/" + dirNameForRenameTest; 170 171 File renamedir = new File (fullpath); 172 assertFalse("the folder folder '" + dirNameForRenameTest 173 + "' is already exists. Please another name for 'dirNameForRenameTest' property ", 174 renamedir.exists()); 175 } 176 177 181 public void testNewPost() throws Exception { 182 initController(); 183 184 assertNotNull("There is no folder under project root to test for", 185 testPath); 186 187 postrequest.addParameter("newaction::" + testPath + ".x", "0"); 188 mv = ac2.handleRequest(postrequest, response); 189 assertNotNull("model is null", mv); 190 assertEquals("Returned view is not valied", ac2.NEW_FOLDER_VIEW, 191 mv.getViewName()); 192 193 Map command = (Map) mv.getModel().get(ac2.getCommandName()); 194 assertNotNull("command is null", command); 195 assertNotNull("selected path is null", command.get("selectedPath")); 196 assertEquals("selected path is not equals", testPath, 197 command.get("selectedPath").toString()); 198 postrequest.addParameter("folderName", dirNameForAddTest); 200 postrequest.addParameter("action", "Create"); 201 postrequest.addParameter("selectedPath", testPath); 202 mv = ac2.handleRequest(postrequest, response); 203 assertNotNull("Model for new folder post request is null", mv); 204 assertEquals("Returned view for new folder post request is not valied", 205 ac2.getSuccessView(), mv.getViewName()); 206 } 207 208 212 public void testRenamePost() throws Exception { 213 assertNotNull("test path is not initialized! please make testNewPost() method to be called before this", 214 testPath); 215 initController(); 216 217 String pathtorename = testPath + "/" + dirNameForAddTest; 218 postrequest.addParameter("renameaction::" + pathtorename + ".x", "0"); 219 mv = ac2.handleRequest(postrequest, response); 220 221 assertNotNull("model is null", mv); 222 223 assertEquals("Returned view is not valied", 224 ac2.RENAME_FOLDER_FILE_VIEW, mv.getViewName()); 225 226 Map command = (Map) mv.getModel().get(ac2.getCommandName()); 227 assertNotNull("command is null", command); 228 assertNotNull("selected path is null", command.get("selectedPath")); 229 assertEquals("selected path is not equals", pathtorename, 230 command.get("selectedPath").toString()); 231 postrequest.addParameter("folderName", dirNameForRenameTest); 233 postrequest.addParameter("action", "Rename"); 234 postrequest.addParameter("selectedPath", pathtorename); 235 mv = ac2.handleRequest(postrequest, response); 236 assertNotNull("Model for rename folder post request is null", mv); 237 assertEquals("Returned view for rename folder post request is not valied", 238 ac2.getSuccessView(), mv.getViewName()); 239 } 240 241 245 public void testDeletePost() throws Exception { 246 assertNotNull("test path is not initialized! please make testNewPost() method to be called before this", 247 testPath); 248 initController(); 249 250 String pathtodelete = testPath + "/" + dirNameForRenameTest; 251 postrequest.addParameter("deleteaction::" + pathtodelete + ".x", "0"); 252 253 mv = ac2.handleRequest(postrequest, response); 254 assertNotNull("model is null", mv); 257 258 266 assertEquals("Returned view is not valied", ac2.DELETE_FOLDERFILE_VIEW, 267 mv.getViewName()); 268 269 Map command = (Map) mv.getModel().get(ac2.getCommandName()); 270 assertNotNull("command is null", command); 271 assertNotNull("selected path is null", command.get("selectedPath")); 272 assertNotNull("Selected folder is null", command.get("foldersList")); 273 assertEquals("Selected folder is not equal", pathtodelete, 274 ((List) command.get("foldersList")).get(0).toString()); 275 276 postrequest.addParameter("filesList", pathtodelete); 278 postrequest.addParameter("action", "Delete"); 279 postrequest.addParameter("selectedPath", pathtodelete); 280 mv = ac2.handleRequest(postrequest, response); 281 assertNotNull("Model for delete folder post request is null", mv); 282 assertEquals("Returned view for delete folder post request is not valied", 283 ac2.getSuccessView(), mv.getViewName()); 284 } 285 286 protected void tearDown() { 287 ac1 = null; 288 ac2 = null; 289 mv = null; 290 postrequest = null; 291 response = null; 292 request = null; 293 } 294 } 295 | Popular Tags |