001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.res;
022:
023: import java.util.*;
024: import java.io.*;
025: import java.sql.*;
026: import junit.framework.*;
027: import org.apache.log4j.*;
028: import com.methodhead.persistable.*;
029: import com.methodhead.tree.*;
030: import com.methodhead.test.*;
031: import com.methodhead.auth.*;
032: import com.methodhead.util.*;
033: import com.methodhead.*;
034: import com.methodhead.sitecontext.*;
035: import servletunit.struts.*;
036: import org.apache.struts.action.*;
037: import org.apache.cactus.*;
038: import org.apache.commons.io.*;
039: import org.apache.commons.httpclient.methods.multipart.*;
040: import java.nio.*;
041:
042: public class ResActionTest extends CactusStrutsTestCase {
043:
044: private File file = null;
045: private List files = null;
046: private FoldingTreeNode node = null;
047: private DynaActionForm form = null;
048: private FileTree fileTree = null;
049:
050: private File testDir_ = null;
051: private File subDir1_ = null;
052: private File subDir2_ = null;
053: private File subDir3_ = null;
054: private File testFile1_ = null;
055: private File testFile2_ = null;
056: private File testFile3_ = null;
057: private File testFile4_ = null;
058: private File testFile5_ = null;
059:
060: static {
061: TestUtils.initLogger();
062: }
063:
064: public ResActionTest(String name) {
065: super (name);
066: }
067:
068: private void setUpFiles() throws Exception {
069: //
070: // create a resources directory though we won't use it
071: //
072: File privateTestDir = new File(session.getServletContext()
073: .getRealPath("/WEB-INF/resources/1"));
074: if (privateTestDir.exists())
075: FileUtils.deleteDirectory(privateTestDir);
076: privateTestDir.mkdirs();
077:
078: //
079: // delete testdir if it exists
080: //
081: testDir_ = new File(session.getServletContext().getRealPath(
082: "/1/testdir"));
083: if (testDir_.exists())
084: FileUtils.deleteDirectory(testDir_);
085:
086: //
087: // create testdir
088: //
089: testDir_.mkdir();
090:
091: //
092: // testdir
093: // subdir1
094: // subdir3
095: // testfile4.txt
096: // testfile3.txt
097: // subdir2
098: // testfile5.txt
099: // testfile1.txt
100: // testfile2.txt
101: //
102: subDir1_ = new File(testDir_, "subdir1");
103: subDir1_.mkdir();
104:
105: subDir3_ = new File(testDir_, "subdir1/subdir3");
106: subDir3_.mkdir();
107:
108: testFile4_ = new File(testDir_, "subdir1/subdir3/testfile4.txt");
109: testFile4_.createNewFile();
110:
111: testFile3_ = new File(testDir_, "subdir1/testfile3.txt");
112: testFile3_.createNewFile();
113:
114: subDir2_ = new File(testDir_, "subdir2");
115: subDir2_.mkdir();
116:
117: testFile5_ = new File(testDir_, "subdir2/testfile5.txt");
118: testFile5_.createNewFile();
119:
120: testFile1_ = new File(testDir_, "testfile1.txt");
121: testFile1_.createNewFile();
122:
123: testFile2_ = new File(testDir_, "testfile2.txt");
124: testFile2_.createNewFile();
125:
126: FileUtils.copyFileToDirectory(new File("support/test.zip"),
127: testDir_);
128: }
129:
130: public void setUp() {
131: //setLogLevel( Level.DEBUG );
132: try {
133: super .setUp();
134:
135: TestData.createWebappFiles(ServletUtils.getRealFile(
136: request, ""));
137:
138: ConnectionSingleton.runBatchUpdate(new FileReader(
139: "webapp/WEB-INF/db/transfer-reset.sql"));
140:
141: TestData.createUsers();
142: SiteContext.setContext(request, TestData.siteContext1);
143: AuthUtil.setUser(request, TestData.user1);
144: } catch (Exception e) {
145: fail(e.getMessage());
146: }
147: }
148:
149: public void tearDown() throws Exception {
150: super .tearDown();
151: }
152:
153: public void testDoListFilesNoPath() {
154: try {
155: setUpFiles();
156:
157: //
158: // list the contents of testdir
159: //
160: setRequestPathInfo("/listFiles");
161: addRequestParameter("path", "");
162:
163: actionPerform();
164: verifyForward("form");
165:
166: form = (DynaActionForm) getActionForm();
167:
168: assertEquals("Private", form.get("path"));
169: files = (List) form.get("files");
170: assertNotNull(files);
171:
172: //
173: // this doesn't test for shit
174: //
175: assertEquals(0, files.size());
176: } catch (Exception e) {
177: e.printStackTrace();
178: fail();
179: }
180: }
181:
182: public void testDoListFiles() {
183: try {
184: setUpFiles();
185:
186: //
187: // list the contents of testdir
188: //
189: setRequestPathInfo("/listFiles");
190: addRequestParameter("path", "Public/testdir");
191:
192: actionPerform();
193: verifyForward("form");
194:
195: form = (DynaActionForm) getActionForm();
196:
197: files = (List) form.get("files");
198: assertNotNull(files);
199: assertEquals(5, files.size());
200:
201: node = (FoldingTreeNode) files.get(0);
202: assertEquals("subdir1", node.getLabel());
203:
204: file = (File) node.getUserObject();
205: assertNotNull(file);
206: assertEquals("subdir1", file.getName());
207: assertTrue(file.isDirectory());
208:
209: node = (FoldingTreeNode) files.get(1);
210: assertEquals("subdir2", node.getLabel());
211:
212: file = (File) node.getUserObject();
213: assertNotNull(file);
214: assertEquals("subdir2", file.getName());
215: assertTrue(file.isDirectory());
216:
217: node = (FoldingTreeNode) files.get(2);
218: assertEquals("test.zip", node.getLabel());
219:
220: file = (File) node.getUserObject();
221: assertNotNull(file);
222: assertEquals("test.zip", file.getName());
223:
224: node = (FoldingTreeNode) files.get(3);
225: assertEquals("testfile1.txt", node.getLabel());
226:
227: file = (File) node.getUserObject();
228: assertNotNull(file);
229: assertEquals("testfile1.txt", file.getName());
230:
231: node = (FoldingTreeNode) files.get(4);
232: assertEquals("testfile2.txt", node.getLabel());
233:
234: file = (File) node.getUserObject();
235: assertNotNull(file);
236: assertEquals("testfile2.txt", file.getName());
237: } catch (Exception e) {
238: e.printStackTrace();
239: fail();
240: }
241: }
242:
243: public void testDoManageFilesFormSingleDirectory() {
244: try {
245: setUpFiles();
246:
247: //
248: // list the contents of testdir
249: //
250: setRequestPathInfo("/manageFilesForm.do");
251: addRequestParameter("path", "Public/testdir");
252: addRequestParameter("file1", "subdir1");
253: actionPerform();
254:
255: verifyForward("form");
256:
257: form = (DynaActionForm) getActionForm();
258: assertEquals("true", form.get("showcreate"));
259: assertEquals("true", form.get("showmove"));
260: assertEquals("true", form.get("showcopy"));
261: assertEquals("true", form.get("showdelete"));
262: assertEquals("", form.get("showedit"));
263: assertEquals("", form.get("showunzip"));
264: assertEquals("subdir1", form.get("name"));
265: assertEquals("subdir1", form.get("movetoname"));
266: assertEquals("subdir1", form.get("copytoname"));
267: assertEquals("Public/testdir", form.get("moveto"));
268: assertEquals("Public/testdir", form.get("copyto"));
269: } catch (Exception e) {
270: e.printStackTrace();
271: fail();
272: }
273: }
274:
275: public void testDoManageFilesFormSingleFile() {
276: try {
277: setUpFiles();
278:
279: //
280: // list the contents of testdir
281: //
282: setRequestPathInfo("/manageFilesForm.do");
283: addRequestParameter("path", "Public/testdir");
284: addRequestParameter("file1", "testfile1.txt");
285: actionPerform();
286:
287: verifyForward("form");
288:
289: form = (DynaActionForm) getActionForm();
290: assertEquals("", form.get("showcreate"));
291: assertEquals("true", form.get("showmove"));
292: assertEquals("true", form.get("showcopy"));
293: assertEquals("true", form.get("showdelete"));
294: assertEquals("true", form.get("showedit"));
295: assertEquals("", form.get("showunzip"));
296: assertEquals("testfile1.txt", form.get("name"));
297: assertEquals("testfile1.txt", form.get("movetoname"));
298: assertEquals("testfile1.txt", form.get("copytoname"));
299: assertEquals("Public/testdir", form.get("moveto"));
300: assertEquals("Public/testdir", form.get("copyto"));
301: } catch (Exception e) {
302: e.printStackTrace();
303: fail();
304: }
305: }
306:
307: public void testDoManageFilesFormZipFile() {
308: try {
309: setUpFiles();
310:
311: //
312: // list the contents of testdir
313: //
314: setRequestPathInfo("/manageFilesForm.do");
315: addRequestParameter("path", "Public/testdir");
316: addRequestParameter("file1", "test.zip");
317: actionPerform();
318:
319: verifyForward("form");
320:
321: form = (DynaActionForm) getActionForm();
322: assertEquals("", form.get("showcreate"));
323: assertEquals("true", form.get("showmove"));
324: assertEquals("true", form.get("showcopy"));
325: assertEquals("true", form.get("showdelete"));
326: assertEquals("", form.get("showedit"));
327: assertEquals("true", form.get("showunzip"));
328: assertEquals("test.zip", form.get("name"));
329: assertEquals("test.zip", form.get("movetoname"));
330: assertEquals("test.zip", form.get("copytoname"));
331: assertEquals("Public/testdir", form.get("moveto"));
332: assertEquals("Public/testdir", form.get("copyto"));
333: } catch (Exception e) {
334: e.printStackTrace();
335: fail();
336: }
337: }
338:
339: public void testDoManageFilesFormFileSet() {
340: try {
341: setUpFiles();
342:
343: //
344: // list the contents of testdir
345: //
346: setRequestPathInfo("/manageFilesForm.do");
347: addRequestParameter("path", "Public/testdir");
348: addRequestParameter("file1", "testfile1.txt");
349: addRequestParameter("file2", "subdir1");
350: actionPerform();
351:
352: verifyForward("form");
353:
354: form = (DynaActionForm) getActionForm();
355: assertEquals("", form.get("showcreate"));
356: assertEquals("true", form.get("showmove"));
357: assertEquals("true", form.get("showcopy"));
358: assertEquals("true", form.get("showdelete"));
359: assertEquals("", form.get("showedit"));
360: assertEquals("", form.get("showunzip"));
361: assertEquals("", form.get("name"));
362: assertEquals("", form.get("movetoname"));
363: assertEquals("", form.get("copytoname"));
364: assertEquals("Public/testdir", form.get("moveto"));
365: assertEquals("Public/testdir", form.get("copyto"));
366: } catch (Exception e) {
367: e.printStackTrace();
368: fail();
369: }
370: }
371:
372: public void testManageMove() {
373: try {
374: setUpFiles();
375:
376: setRequestPathInfo("/manageFiles");
377: addRequestParameter("action", "move");
378: addRequestParameter("path", "Public/testdir");
379: addRequestParameter("file1", "testfile1.txt");
380: addRequestParameter("moveto", "Public/testdir");
381: addRequestParameter("movetoname", "newtestfile1.txt");
382: actionPerform();
383:
384: verifyForwardPath("/listFiles.do?path=Public/testdir");
385:
386: file = new File(session.getServletContext().getRealPath(
387: "/1/testdir/testfile1.txt"));
388: assertTrue(!file.exists());
389:
390: file = new File(session.getServletContext().getRealPath(
391: "/1/testdir/newtestfile1.txt"));
392: assertTrue(file.exists());
393: } catch (Exception e) {
394: e.printStackTrace();
395: fail();
396: }
397: }
398:
399: public void testManageMoveSet() {
400: try {
401: setUpFiles();
402:
403: setRequestPathInfo("/manageFiles.do");
404: addRequestParameter("action", "move");
405: addRequestParameter("path", "Public/testdir");
406: addRequestParameter("file1", "testfile2.txt");
407: addRequestParameter("file2", "subdir1");
408: addRequestParameter("moveto", "Public/testdir/subdir2");
409: addRequestParameter("movetoname", "");
410: actionPerform();
411:
412: verifyForwardPath("/listFiles.do?path=Public/testdir");
413:
414: file = new File(session.getServletContext().getRealPath(
415: "/1/testdir/testfile2.txt"));
416: assertTrue(!file.exists());
417:
418: file = new File(session.getServletContext().getRealPath(
419: "/1/testdir/subdir1"));
420: assertTrue(!file.exists());
421:
422: file = new File(session.getServletContext().getRealPath(
423: "/1/testdir/subdir2/testfile2.txt"));
424: assertTrue(file.exists());
425:
426: file = new File(session.getServletContext().getRealPath(
427: "/1/testdir/subdir2/subdir1"));
428: assertTrue(file.exists());
429: assertTrue(file.isDirectory());
430:
431: //
432: // make sure the file tree got updated
433: //
434: fileTree = ResUtils.getFileTree(new DefaultResPolicy(),
435: request);
436: assertNotNull(fileTree);
437:
438: node = fileTree.find("Public/testdir/subdir2/subdir1");
439: assertNotNull(node);
440: } catch (Exception e) {
441: e.printStackTrace();
442: fail();
443: }
444: }
445:
446: public void testManageCopyFile() throws Exception {
447: //
448: // copy a file
449: //
450: setUpFiles();
451:
452: setRequestPathInfo("/manageFiles.do");
453: addRequestParameter("action", "copy");
454: addRequestParameter("path", "Public/testdir");
455: addRequestParameter("file1", "testfile1.txt");
456: addRequestParameter("copyto", "Public/testdir");
457: addRequestParameter("copytoname", "newtestfile1.txt");
458: actionPerform();
459:
460: verifyForwardPath("/listFiles.do?path=Public/testdir");
461:
462: file = new File(session.getServletContext().getRealPath(
463: "/1/testdir/testfile1.txt"));
464: assertTrue(file.exists());
465:
466: file = new File(session.getServletContext().getRealPath(
467: "/1/testdir/newtestfile1.txt"));
468: assertTrue(file.exists());
469: }
470:
471: public void testManageCopyDirectory() throws Exception {
472: //
473: // copy a directory
474: //
475: setUpFiles();
476:
477: setRequestPathInfo("/manageFiles.do");
478: addRequestParameter("action", "copy");
479: addRequestParameter("path", "Public/testdir");
480: addRequestParameter("file1", "subdir1");
481: addRequestParameter("copyto", "Public/testdir/subdir2");
482: addRequestParameter("copytoname", "newsubdir1");
483: actionPerform();
484:
485: verifyForwardPath("/listFiles.do?path=Public/testdir");
486:
487: file = new File(session.getServletContext().getRealPath(
488: "/1/testdir/subdir1"));
489: assertTrue(file.exists());
490:
491: file = new File(session.getServletContext().getRealPath(
492: "/1/testdir/subdir2/newsubdir1"));
493: assertTrue(file.exists());
494: assertTrue(file.isDirectory());
495:
496: file = new File(session.getServletContext().getRealPath(
497: "/1/testdir/subdir2/newsubdir1/subdir3"));
498: assertTrue(file.exists());
499: assertTrue(file.isDirectory());
500:
501: //
502: // make sure the file tree got updated
503: //
504: fileTree = ResUtils
505: .getFileTree(new DefaultResPolicy(), request);
506: assertNotNull(fileTree);
507:
508: node = fileTree.find("Public/testdir/subdir2/newsubdir1");
509: assertNotNull(node);
510: }
511:
512: public void testManageDeleteSingleFile() throws Exception {
513: //
514: // delete a file
515: //
516: setUpFiles();
517:
518: setRequestPathInfo("/manageFiles.do");
519: addRequestParameter("action", "delete");
520: addRequestParameter("path", "Public/testdir");
521: addRequestParameter("file1", "testfile1.txt");
522: actionPerform();
523:
524: verifyForward("confirm");
525: verifyActionMessages(new String[] { "res.deletefiles" });
526: }
527:
528: public void testManageDeleteSingleFileConfirm() throws Exception {
529: //
530: // delete a file
531: //
532: setUpFiles();
533:
534: setRequestPathInfo("/manageFiles.do");
535: addRequestParameter("action", "delete");
536: addRequestParameter("path", "Public/testdir");
537: addRequestParameter("file1", "testfile1.txt");
538: addRequestParameter("confirm", "Confirm");
539: actionPerform();
540:
541: verifyForwardPath("/listFiles.do?path=Public/testdir");
542:
543: file = new File(testDir_, "testfile1.txt");
544: assertTrue(!file.exists());
545: }
546:
547: public void testManageDeleteSingleDirectory() throws Exception {
548: setUpFiles();
549:
550: setRequestPathInfo("/manageFiles.do");
551: addRequestParameter("action", "delete");
552: addRequestParameter("path", "Public/testdir");
553: addRequestParameter("file1", "subdir1");
554: actionPerform();
555:
556: verifyForward("confirm");
557: verifyActionMessages(new String[] { "res.deletefiles" });
558: }
559:
560: public void testManageDeleteSingleDirectoryConfirm()
561: throws Exception {
562: setUpFiles();
563:
564: setRequestPathInfo("/manageFiles.do");
565: addRequestParameter("action", "delete");
566: addRequestParameter("path", "Public/testdir");
567: addRequestParameter("file1", "subdir1");
568: addRequestParameter("confirm", "Confirm");
569: actionPerform();
570:
571: verifyForwardPath("/listFiles.do?path=Public/testdir");
572:
573: assertTrue(!subDir1_.exists());
574: assertTrue(testFile1_.exists());
575: assertTrue(testFile2_.exists());
576:
577: //
578: // make sure the file tree got updated
579: //
580: fileTree = ResUtils
581: .getFileTree(new DefaultResPolicy(), request);
582: assertNotNull(fileTree);
583:
584: node = fileTree.find("Public/testdir/subdir1");
585: assertNull(node);
586: }
587:
588: public void testManageDeleteSet() throws Exception {
589: setUpFiles();
590:
591: setRequestPathInfo("/manageFiles.do");
592: addRequestParameter("action", "delete");
593: addRequestParameter("path", "Public/testdir");
594: addRequestParameter("file1", "subdir1");
595: addRequestParameter("file2", "testfile1.txt");
596: actionPerform();
597:
598: verifyForward("confirm");
599: verifyActionMessages(new String[] { "res.deletefiles" });
600: }
601:
602: public void testManageDeleteSetConfirm() throws Exception {
603: setUpFiles();
604:
605: setRequestPathInfo("/manageFiles.do");
606: addRequestParameter("action", "delete");
607: addRequestParameter("path", "Public/testdir");
608: addRequestParameter("file1", "subdir1");
609: addRequestParameter("file2", "testfile1.txt");
610: addRequestParameter("confirm", "Confirm");
611: actionPerform();
612:
613: verifyForwardPath("/listFiles.do?path=Public/testdir");
614:
615: assertTrue(!subDir1_.exists());
616: assertTrue(!testFile1_.exists());
617: assertTrue(testFile2_.exists());
618:
619: //
620: // make sure the file tree got updated
621: //
622: fileTree = ResUtils
623: .getFileTree(new DefaultResPolicy(), request);
624: assertNotNull(fileTree);
625:
626: node = fileTree.find("Public/testdir/subdir1");
627: assertNull(node);
628: }
629:
630: public void testManageEdit() throws Exception {
631: setUpFiles();
632:
633: setRequestPathInfo("/manageFiles.do");
634: addRequestParameter("action", "edit");
635: addRequestParameter("path", "Public/testdir");
636: addRequestParameter("file1", "testfile1.txt");
637: actionPerform();
638:
639: verifyForward("editFileForm");
640:
641: form = (DynaActionForm) getActionForm();
642: assertEquals("", form.get("text"));
643: }
644:
645: public void testManageEditConfirm() throws Exception {
646: setUpFiles();
647:
648: setRequestPathInfo("/manageFiles.do");
649: addRequestParameter("action", "edit");
650: addRequestParameter("path", "Public/testdir");
651: addRequestParameter("file1", "testfile1.txt");
652: addRequestParameter("save", "yes");
653: addRequestParameter("text", "New text");
654: actionPerform();
655:
656: verifyForward("editFileForm");
657:
658: form = (DynaActionForm) getActionForm();
659: assertEquals("New text", form.get("text"));
660:
661: assertEquals("New text", FileUtils.readFileToString(testFile1_,
662: "ISO-8859-1"));
663: }
664:
665: public void testManageUnzip() throws Exception {
666: setUpFiles();
667:
668: setRequestPathInfo("/manageFiles.do");
669: addRequestParameter("action", "unzip");
670: addRequestParameter("path", "Public/testdir");
671: addRequestParameter("file1", "test.zip");
672: actionPerform();
673:
674: verifyForward("confirm");
675: verifyActionMessages(new String[] { "res.confirmunzip" });
676: }
677:
678: public void testManageUnzipConfirm() throws Exception {
679: setUpFiles();
680:
681: setRequestPathInfo("/manageFiles.do");
682: addRequestParameter("action", "unzip");
683: addRequestParameter("path", "Public/testdir");
684: addRequestParameter("file1", "test.zip");
685: addRequestParameter("confirm", "Confirm");
686: actionPerform();
687:
688: verifyForwardPath("/listFiles.do?path=Public/testdir");
689:
690: file = new File(testDir_, "test.txt");
691: assertTrue(file.exists());
692: assertTrue(file.isFile());
693:
694: file = new File(testDir_, "subdir");
695: assertTrue(file.exists());
696: assertTrue(file.isDirectory());
697:
698: //
699: // make sure the file tree got updated
700: //
701: fileTree = ResUtils
702: .getFileTree(new DefaultResPolicy(), request);
703: assertNotNull(fileTree);
704:
705: node = fileTree.find("Public/testdir/subdir");
706: assertNotNull(node);
707: }
708:
709: public void testDoUploadFileForm() {
710: try {
711:
712: //
713: // upload a file for the first time
714: //
715: setUpFiles();
716:
717: addRequestParameter("path", "Public/testdir");
718: setRequestPathInfo("/uploadFileForm.do");
719: actionPerform();
720:
721: verifyForward("form");
722: } catch (Exception e) {
723: e.printStackTrace();
724: fail();
725: }
726: }
727:
728: public void beginDoUploadFile(WebRequest webRequest)
729: throws Exception {
730:
731: File file = new File("build.properties");
732:
733: FilePart part = new FilePart("application/octet-stream", file);
734: part.setName("file");
735:
736: Part[] parts = new Part[] { part };
737:
738: if (Part.getBoundary() != null) {
739: webRequest.setContentType("multipart/form-data"
740: + "; boundary=" + Part.getBoundary());
741: }
742:
743: ByteArrayOutputStream out = new ByteArrayOutputStream();
744: Part.sendParts(out, parts);
745:
746: ByteArrayInputStream in = new ByteArrayInputStream(out
747: .toByteArray());
748: webRequest.setUserData(in);
749: }
750:
751: public void testDoUploadFile() throws Exception {
752: //
753: // upload a file for the first time
754: //
755: setUpFiles();
756:
757: addRequestParameter("path", "Public/testdir");
758: setRequestPathInfo("/uploadFile.do");
759: actionPerform();
760:
761: verifyForwardPath("/listFiles.do?path=Public/testdir");
762:
763: file = new File(testDir_, "build.properties");
764: assertTrue(file.exists());
765: }
766:
767: public void beginDoUploadFileConfirm(WebRequest webRequest)
768: throws Exception {
769:
770: File file = new File("build.properties");
771:
772: FilePart part = new FilePart("application/octet-stream", file);
773: part.setName("file");
774:
775: Part[] parts = new Part[] { part };
776:
777: if (Part.getBoundary() != null) {
778: webRequest.setContentType("multipart/form-data"
779: + "; boundary=" + Part.getBoundary());
780: }
781:
782: ByteArrayOutputStream out = new ByteArrayOutputStream();
783: Part.sendParts(out, parts);
784:
785: ByteArrayInputStream in = new ByteArrayInputStream(out
786: .toByteArray());
787: webRequest.setUserData(in);
788: }
789:
790: public void testDoUploadFileConfirm() throws Exception {
791: setUpFiles();
792:
793: //
794: // copy the file into place before the upload
795: //
796: FileUtils.copyFileToDirectory(new File("build.properties"),
797: testDir_);
798:
799: addRequestParameter("path", "Public/testdir");
800: setRequestPathInfo("/uploadFile.do");
801: actionPerform();
802:
803: verifyForward("confirm");
804: verifyActionMessages(new String[] { "res.fileexists" });
805: }
806:
807: public void testDoUploadFileCancel() {
808: try {
809:
810: //
811: // upload a file for the first time
812: //
813: setUpFiles();
814:
815: setRequestPathInfo("/uploadFile.do");
816: addRequestParameter("path", "Public/testdir");
817: addRequestParameter("cancel", "Cancel");
818: actionPerform();
819:
820: verifyForwardPath("/listFiles.do?path=Public/testdir");
821: } catch (Exception e) {
822: e.printStackTrace();
823: fail();
824: }
825: }
826:
827: public void testDoCreateFileForm() {
828: try {
829:
830: //
831: // upload a file for the first time
832: //
833: setUpFiles();
834:
835: addRequestParameter("path", "Public/testdir");
836: setRequestPathInfo("/createFileForm.do");
837: actionPerform();
838:
839: verifyForward("form");
840: } catch (Exception e) {
841: e.printStackTrace();
842: fail();
843: }
844: }
845:
846: public void testDoCreateFile() throws Exception {
847: //
848: // create a file
849: //
850: setUpFiles();
851:
852: addRequestParameter("path", "Public/testdir");
853: addRequestParameter("name", "newfile.txt");
854: setRequestPathInfo("/createFile.do");
855: actionPerform();
856:
857: verifyForwardPath("/listFiles.do?path=Public/testdir");
858:
859: file = new File(testDir_, "newfile.txt");
860: assertTrue(file.exists());
861: assertTrue(file.isFile());
862: }
863:
864: public void testDoCreateFileConfirm() throws Exception {
865: setUpFiles();
866:
867: //
868: // create file before we execute the action
869: //
870: FileUtils.copyFile(new File("build.properties"), new File(
871: testDir_, "newfile.txt"));
872:
873: addRequestParameter("path", "Public/testdir");
874: addRequestParameter("name", "newfile.txt");
875: setRequestPathInfo("/createFile.do");
876: actionPerform();
877:
878: verifyForward("confirm");
879: verifyActionMessages(new String[] { "res.fileexists" });
880: }
881:
882: public void testDoCreateFileCancel() {
883: try {
884:
885: //
886: // upload a file for the first time
887: //
888: setUpFiles();
889:
890: setRequestPathInfo("/createFile.do");
891: addRequestParameter("path", "Public/testdir");
892: addRequestParameter("name", "test.text");
893: addRequestParameter("cancel", "Cancel");
894: actionPerform();
895:
896: verifyForwardPath("/listFiles.do?path=Public/testdir");
897: } catch (Exception e) {
898: e.printStackTrace();
899: fail();
900: }
901: }
902:
903: public void testDoCreateDir() throws Exception {
904: setUpFiles();
905:
906: addRequestParameter("path", "Public/testdir");
907: addRequestParameter("name", "newdir");
908: addRequestParameter("createdir", "yes");
909: setRequestPathInfo("/createFile.do");
910: actionPerform();
911:
912: verifyForwardPath("/listFiles.do?path=Public/testdir");
913:
914: file = new File(testDir_, "newdir");
915: assertTrue(file.exists());
916: assertTrue(file.isDirectory());
917: }
918:
919: public void testDoCreateDirCantOverwrite() throws Exception {
920: setUpFiles();
921:
922: //
923: // create the dir first
924: //
925: (new File(testDir_, "newdir")).mkdir();
926:
927: addRequestParameter("path", "Public/testdir");
928: addRequestParameter("name", "newdir");
929: addRequestParameter("createdir", "yes");
930: setRequestPathInfo("/createFile.do");
931: actionPerform();
932:
933: verifyInputForward();
934: verifyActionErrors(new String[] { "res.cantoverwrite" });
935:
936: //
937: // make sure the file tree got updated
938: //
939: fileTree = ResUtils
940: .getFileTree(new DefaultResPolicy(), request);
941: assertNotNull(fileTree);
942:
943: node = fileTree.find("Public/testdir/newdir");
944: assertNotNull(node);
945: }
946: }
|