KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > res > ResActionTest


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.res;
22
23 import java.util.*;
24 import java.io.*;
25 import java.sql.*;
26 import junit.framework.*;
27 import org.apache.log4j.*;
28 import com.methodhead.persistable.*;
29 import com.methodhead.tree.*;
30 import com.methodhead.test.*;
31 import com.methodhead.auth.*;
32 import com.methodhead.util.*;
33 import com.methodhead.*;
34 import com.methodhead.sitecontext.*;
35 import servletunit.struts.*;
36 import org.apache.struts.action.*;
37 import org.apache.cactus.*;
38 import org.apache.commons.io.*;
39 import org.apache.commons.httpclient.methods.multipart.*;
40 import java.nio.*;
41
42 public class ResActionTest extends CactusStrutsTestCase {
43
44   private File file = null;
45   private List files = null;
46   private FoldingTreeNode node = null;
47   private DynaActionForm form = null;
48   private FileTree fileTree = null;
49
50   private File testDir_ = null;
51   private File subDir1_ = null;
52   private File subDir2_ = null;
53   private File subDir3_ = null;
54   private File testFile1_ = null;
55   private File testFile2_ = null;
56   private File testFile3_ = null;
57   private File testFile4_ = null;
58   private File testFile5_ = null;
59
60   static {
61     TestUtils.initLogger();
62   }
63
64   public ResActionTest( String JavaDoc name ) {
65     super( name );
66   }
67
68   private void setUpFiles() throws Exception JavaDoc {
69     //
70
// create a resources directory though we won't use it
71
//
72
File privateTestDir = new File( session.getServletContext().getRealPath( "/WEB-INF/resources/1" ) );
73     if ( privateTestDir.exists() )
74       FileUtils.deleteDirectory( privateTestDir );
75     privateTestDir.mkdirs();
76
77     //
78
// delete testdir if it exists
79
//
80
testDir_ = new File( session.getServletContext().getRealPath( "/1/testdir" ) );
81     if ( testDir_.exists() )
82       FileUtils.deleteDirectory( testDir_ );
83
84     //
85
// create testdir
86
//
87
testDir_.mkdir();
88
89     //
90
// testdir
91
// subdir1
92
// subdir3
93
// testfile4.txt
94
// testfile3.txt
95
// subdir2
96
// testfile5.txt
97
// testfile1.txt
98
// testfile2.txt
99
//
100
subDir1_ = new File( testDir_, "subdir1" );
101     subDir1_.mkdir();
102
103     subDir3_ = new File( testDir_, "subdir1/subdir3" );
104     subDir3_.mkdir();
105
106     testFile4_ = new File( testDir_, "subdir1/subdir3/testfile4.txt" );
107     testFile4_.createNewFile();
108
109     testFile3_ = new File( testDir_, "subdir1/testfile3.txt" );
110     testFile3_.createNewFile();
111
112     subDir2_ = new File( testDir_, "subdir2" );
113     subDir2_.mkdir();
114
115     testFile5_ = new File( testDir_, "subdir2/testfile5.txt" );
116     testFile5_.createNewFile();
117
118     testFile1_ = new File( testDir_, "testfile1.txt" );
119     testFile1_.createNewFile();
120
121     testFile2_ = new File( testDir_, "testfile2.txt" );
122     testFile2_.createNewFile();
123
124     FileUtils.copyFileToDirectory( new File( "support/test.zip" ), testDir_ );
125   }
126
127   public void setUp() {
128     //setLogLevel( Level.DEBUG );
129
try {
130       super.setUp();
131
132       TestData.createWebappFiles( ServletUtils.getRealFile( request, "" ) );
133
134       ConnectionSingleton.runBatchUpdate( new FileReader( "webapp/WEB-INF/db/transfer-reset.sql" ) );
135
136       TestData.createUsers();
137       SiteContext.setContext( request, TestData.siteContext1 );
138       AuthUtil.setUser( request, TestData.user1 );
139     }
140     catch ( Exception JavaDoc e ) {
141       fail( e.getMessage() );
142     }
143   }
144
145   public void tearDown()
146   throws
147     Exception JavaDoc {
148     super.tearDown();
149   }
150
151   public void testDoListFilesNoPath() {
152     try {
153       setUpFiles();
154
155       //
156
// list the contents of testdir
157
//
158
setRequestPathInfo( "/listFiles" );
159       addRequestParameter( "path", "" );
160
161       actionPerform();
162       verifyForward( "form" );
163
164       form = ( DynaActionForm )getActionForm();
165
166       assertEquals( "Private", form.get( "path" ) );
167       files = ( List )form.get( "files" );
168       assertNotNull( files );
169
170       //
171
// this doesn't test for shit
172
//
173
assertEquals( 0, files.size() );
174     }
175     catch ( Exception JavaDoc e ) {
176       e.printStackTrace();
177       fail();
178     }
179   }
180
181   public void testDoListFiles() {
182     try {
183       setUpFiles();
184
185       //
186
// list the contents of testdir
187
//
188
setRequestPathInfo( "/listFiles" );
189       addRequestParameter( "path", "Public/testdir" );
190
191       actionPerform();
192       verifyForward( "form" );
193
194       form = ( DynaActionForm )getActionForm();
195
196       files = ( List )form.get( "files" );
197       assertNotNull( files );
198       assertEquals( 5, files.size() );
199
200       node = ( FoldingTreeNode )files.get( 0 );
201       assertEquals( "subdir1", node.getLabel() );
202
203       file = ( File )node.getUserObject();
204       assertNotNull( file );
205       assertEquals( "subdir1", file.getName() );
206       assertTrue( file.isDirectory() );
207
208       node = ( FoldingTreeNode )files.get( 1 );
209       assertEquals( "subdir2", node.getLabel() );
210
211       file = ( File )node.getUserObject();
212       assertNotNull( file );
213       assertEquals( "subdir2", file.getName() );
214       assertTrue( file.isDirectory() );
215
216       node = ( FoldingTreeNode )files.get( 2 );
217       assertEquals( "test.zip", node.getLabel() );
218
219       file = ( File )node.getUserObject();
220       assertNotNull( file );
221       assertEquals( "test.zip", file.getName() );
222
223       node = ( FoldingTreeNode )files.get( 3 );
224       assertEquals( "testfile1.txt", node.getLabel() );
225
226       file = ( File )node.getUserObject();
227       assertNotNull( file );
228       assertEquals( "testfile1.txt", file.getName() );
229
230       node = ( FoldingTreeNode )files.get( 4 );
231       assertEquals( "testfile2.txt", node.getLabel() );
232
233       file = ( File )node.getUserObject();
234       assertNotNull( file );
235       assertEquals( "testfile2.txt", file.getName() );
236     }
237     catch ( Exception JavaDoc 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     }
270     catch ( Exception JavaDoc e ) {
271       e.printStackTrace();
272       fail();
273     }
274   }
275
276   public void testDoManageFilesFormSingleFile() {
277     try {
278       setUpFiles();
279
280       //
281
// list the contents of testdir
282
//
283
setRequestPathInfo( "/manageFilesForm.do" );
284       addRequestParameter( "path", "Public/testdir" );
285       addRequestParameter( "file1", "testfile1.txt" );
286       actionPerform();
287
288       verifyForward( "form" );
289
290       form = ( DynaActionForm )getActionForm();
291       assertEquals( "", form.get( "showcreate" ) );
292       assertEquals( "true", form.get( "showmove" ) );
293       assertEquals( "true", form.get( "showcopy" ) );
294       assertEquals( "true", form.get( "showdelete" ) );
295       assertEquals( "true", form.get( "showedit" ) );
296       assertEquals( "", form.get( "showunzip" ) );
297       assertEquals( "testfile1.txt", form.get( "name" ) );
298       assertEquals( "testfile1.txt", form.get( "movetoname" ) );
299       assertEquals( "testfile1.txt", form.get( "copytoname" ) );
300       assertEquals( "Public/testdir", form.get( "moveto" ) );
301       assertEquals( "Public/testdir", form.get( "copyto" ) );
302     }
303     catch ( Exception JavaDoc e ) {
304       e.printStackTrace();
305       fail();
306     }
307   }
308
309   public void testDoManageFilesFormZipFile() {
310     try {
311       setUpFiles();
312
313       //
314
// list the contents of testdir
315
//
316
setRequestPathInfo( "/manageFilesForm.do" );
317       addRequestParameter( "path", "Public/testdir" );
318       addRequestParameter( "file1", "test.zip" );
319       actionPerform();
320
321       verifyForward( "form" );
322
323       form = ( DynaActionForm )getActionForm();
324       assertEquals( "", form.get( "showcreate" ) );
325       assertEquals( "true", form.get( "showmove" ) );
326       assertEquals( "true", form.get( "showcopy" ) );
327       assertEquals( "true", form.get( "showdelete" ) );
328       assertEquals( "", form.get( "showedit" ) );
329       assertEquals( "true", form.get( "showunzip" ) );
330       assertEquals( "test.zip", form.get( "name" ) );
331       assertEquals( "test.zip", form.get( "movetoname" ) );
332       assertEquals( "test.zip", form.get( "copytoname" ) );
333       assertEquals( "Public/testdir", form.get( "moveto" ) );
334       assertEquals( "Public/testdir", form.get( "copyto" ) );
335     }
336     catch ( Exception JavaDoc e ) {
337       e.printStackTrace();
338       fail();
339     }
340   }
341
342   public void testDoManageFilesFormFileSet() {
343     try {
344       setUpFiles();
345
346       //
347
// list the contents of testdir
348
//
349
setRequestPathInfo( "/manageFilesForm.do" );
350       addRequestParameter( "path", "Public/testdir" );
351       addRequestParameter( "file1", "testfile1.txt" );
352       addRequestParameter( "file2", "subdir1" );
353       actionPerform();
354
355       verifyForward( "form" );
356
357       form = ( DynaActionForm )getActionForm();
358       assertEquals( "", form.get( "showcreate" ) );
359       assertEquals( "true", form.get( "showmove" ) );
360       assertEquals( "true", form.get( "showcopy" ) );
361       assertEquals( "true", form.get( "showdelete" ) );
362       assertEquals( "", form.get( "showedit" ) );
363       assertEquals( "", form.get( "showunzip" ) );
364       assertEquals( "", form.get( "name" ) );
365       assertEquals( "", form.get( "movetoname" ) );
366       assertEquals( "", form.get( "copytoname" ) );
367       assertEquals( "Public/testdir", form.get( "moveto" ) );
368       assertEquals( "Public/testdir", form.get( "copyto" ) );
369     }
370     catch ( Exception JavaDoc e ) {
371       e.printStackTrace();
372       fail();
373     }
374   }
375
376   public void testManageMove() {
377     try {
378       setUpFiles();
379
380       setRequestPathInfo( "/manageFiles" );
381       addRequestParameter( "action", "move" );
382       addRequestParameter( "path", "Public/testdir" );
383       addRequestParameter( "file1", "testfile1.txt" );
384       addRequestParameter( "moveto", "Public/testdir" );
385       addRequestParameter( "movetoname", "newtestfile1.txt" );
386       actionPerform();
387
388       verifyForwardPath( "/listFiles.do?path=Public/testdir" );
389
390       file = new File( session.getServletContext().getRealPath( "/1/testdir/testfile1.txt" ) );
391       assertTrue( !file.exists() );
392
393       file = new File( session.getServletContext().getRealPath( "/1/testdir/newtestfile1.txt" ) );
394       assertTrue( file.exists() );
395     }
396     catch ( Exception JavaDoc e ) {
397       e.printStackTrace();
398       fail();
399     }
400   }
401
402   public void testManageMoveSet() {
403     try {
404       setUpFiles();
405
406       setRequestPathInfo( "/manageFiles.do" );
407       addRequestParameter( "action", "move" );
408       addRequestParameter( "path", "Public/testdir" );
409       addRequestParameter( "file1", "testfile2.txt" );
410       addRequestParameter( "file2", "subdir1" );
411       addRequestParameter( "moveto", "Public/testdir/subdir2" );
412       addRequestParameter( "movetoname", "" );
413       actionPerform();
414
415       verifyForwardPath( "/listFiles.do?path=Public/testdir" );
416
417       file = new File( session.getServletContext().getRealPath( "/1/testdir/testfile2.txt" ) );
418       assertTrue( !file.exists() );
419
420       file = new File( session.getServletContext().getRealPath( "/1/testdir/subdir1" ) );
421       assertTrue( !file.exists() );
422
423       file = new File( session.getServletContext().getRealPath( "/1/testdir/subdir2/testfile2.txt" ) );
424       assertTrue( file.exists() );
425
426       file = new File( session.getServletContext().getRealPath( "/1/testdir/subdir2/subdir1" ) );
427       assertTrue( file.exists() );
428       assertTrue( file.isDirectory() );
429
430       //
431
// make sure the file tree got updated
432
//
433
fileTree = ResUtils.getFileTree( new DefaultResPolicy(), request );
434       assertNotNull( fileTree );
435
436       node = fileTree.find( "Public/testdir/subdir2/subdir1" );
437       assertNotNull( node );
438     }
439     catch ( Exception JavaDoc e ) {
440       e.printStackTrace();
441       fail();
442     }
443   }
444
445   public void testManageCopyFile() throws Exception JavaDoc {
446     //
447
// copy a file
448
//
449
setUpFiles();
450
451     setRequestPathInfo( "/manageFiles.do" );
452     addRequestParameter( "action", "copy" );
453     addRequestParameter( "path", "Public/testdir" );
454     addRequestParameter( "file1", "testfile1.txt" );
455     addRequestParameter( "copyto", "Public/testdir" );
456     addRequestParameter( "copytoname", "newtestfile1.txt" );
457     actionPerform();
458
459     verifyForwardPath( "/listFiles.do?path=Public/testdir" );
460
461     file = new File( session.getServletContext().getRealPath( "/1/testdir/testfile1.txt" ) );
462     assertTrue( file.exists() );
463
464     file = new File( session.getServletContext().getRealPath( "/1/testdir/newtestfile1.txt" ) );
465     assertTrue( file.exists() );
466   }
467
468   public void testManageCopyDirectory() throws Exception JavaDoc {
469     //
470
// copy a directory
471
//
472
setUpFiles();
473
474     setRequestPathInfo( "/manageFiles.do" );
475     addRequestParameter( "action", "copy" );
476     addRequestParameter( "path", "Public/testdir" );
477     addRequestParameter( "file1", "subdir1" );
478     addRequestParameter( "copyto", "Public/testdir/subdir2" );
479     addRequestParameter( "copytoname", "newsubdir1" );
480     actionPerform();
481
482     verifyForwardPath( "/listFiles.do?path=Public/testdir" );
483
484     file = new File( session.getServletContext().getRealPath( "/1/testdir/subdir1" ) );
485     assertTrue( file.exists() );
486
487     file = new File( session.getServletContext().getRealPath( "/1/testdir/subdir2/newsubdir1" ) );
488     assertTrue( file.exists() );
489     assertTrue( file.isDirectory() );
490
491     file = new File( session.getServletContext().getRealPath( "/1/testdir/subdir2/newsubdir1/subdir3" ) );
492     assertTrue( file.exists() );
493     assertTrue( file.isDirectory() );
494
495     //
496
// make sure the file tree got updated
497
//
498
fileTree = ResUtils.getFileTree( new DefaultResPolicy(), request );
499     assertNotNull( fileTree );
500
501     node = fileTree.find( "Public/testdir/subdir2/newsubdir1" );
502     assertNotNull( node );
503   }
504
505   public void testManageDeleteSingleFile() throws Exception JavaDoc {
506     //
507
// delete a file
508
//
509
setUpFiles();
510
511     setRequestPathInfo( "/manageFiles.do" );
512     addRequestParameter( "action", "delete" );
513     addRequestParameter( "path", "Public/testdir" );
514     addRequestParameter( "file1", "testfile1.txt" );
515     actionPerform();
516
517     verifyForward( "confirm" );
518     verifyActionMessages( new String JavaDoc[] { "res.deletefiles" } );
519   }
520
521   public void testManageDeleteSingleFileConfirm() throws Exception JavaDoc {
522     //
523
// delete a file
524
//
525
setUpFiles();
526
527     setRequestPathInfo( "/manageFiles.do" );
528     addRequestParameter( "action", "delete" );
529     addRequestParameter( "path", "Public/testdir" );
530     addRequestParameter( "file1", "testfile1.txt" );
531     addRequestParameter( "confirm", "Confirm" );
532     actionPerform();
533
534     verifyForwardPath( "/listFiles.do?path=Public/testdir" );
535
536     file = new File( testDir_, "testfile1.txt" );
537     assertTrue( !file.exists() );
538   }
539
540   public void testManageDeleteSingleDirectory() throws Exception JavaDoc {
541     setUpFiles();
542
543     setRequestPathInfo( "/manageFiles.do" );
544     addRequestParameter( "action", "delete" );
545     addRequestParameter( "path", "Public/testdir" );
546     addRequestParameter( "file1", "subdir1" );
547     actionPerform();
548
549     verifyForward( "confirm" );
550     verifyActionMessages( new String JavaDoc[] { "res.deletefiles" } );
551   }
552
553   public void testManageDeleteSingleDirectoryConfirm() throws Exception JavaDoc {
554     setUpFiles();
555
556     setRequestPathInfo( "/manageFiles.do" );
557     addRequestParameter( "action", "delete" );
558     addRequestParameter( "path", "Public/testdir" );
559     addRequestParameter( "file1", "subdir1" );
560     addRequestParameter( "confirm", "Confirm" );
561     actionPerform();
562
563     verifyForwardPath( "/listFiles.do?path=Public/testdir" );
564
565     assertTrue( !subDir1_.exists() );
566     assertTrue( testFile1_.exists() );
567     assertTrue( testFile2_.exists() );
568
569     //
570
// make sure the file tree got updated
571
//
572
fileTree = ResUtils.getFileTree( new DefaultResPolicy(), request );
573     assertNotNull( fileTree );
574
575     node = fileTree.find( "Public/testdir/subdir1" );
576     assertNull( node );
577   }
578
579   public void testManageDeleteSet() throws Exception JavaDoc {
580     setUpFiles();
581
582     setRequestPathInfo( "/manageFiles.do" );
583     addRequestParameter( "action", "delete" );
584     addRequestParameter( "path", "Public/testdir" );
585     addRequestParameter( "file1", "subdir1" );
586     addRequestParameter( "file2", "testfile1.txt" );
587     actionPerform();
588
589     verifyForward( "confirm" );
590     verifyActionMessages( new String JavaDoc[] { "res.deletefiles" } );
591   }
592
593   public void testManageDeleteSetConfirm() throws Exception JavaDoc {
594     setUpFiles();
595
596     setRequestPathInfo( "/manageFiles.do" );
597     addRequestParameter( "action", "delete" );
598     addRequestParameter( "path", "Public/testdir" );
599     addRequestParameter( "file1", "subdir1" );
600     addRequestParameter( "file2", "testfile1.txt" );
601     addRequestParameter( "confirm", "Confirm" );
602     actionPerform();
603
604     verifyForwardPath( "/listFiles.do?path=Public/testdir" );
605
606     assertTrue( !subDir1_.exists() );
607     assertTrue( !testFile1_.exists() );
608     assertTrue( testFile2_.exists() );
609
610     //
611
// make sure the file tree got updated
612
//
613
fileTree = ResUtils.getFileTree( new DefaultResPolicy(), request );
614     assertNotNull( fileTree );
615
616     node = fileTree.find( "Public/testdir/subdir1" );
617     assertNull( node );
618   }
619
620   public void testManageEdit() throws Exception JavaDoc {
621     setUpFiles();
622
623     setRequestPathInfo( "/manageFiles.do" );
624     addRequestParameter( "action", "edit" );
625     addRequestParameter( "path", "Public/testdir" );
626     addRequestParameter( "file1", "testfile1.txt" );
627     actionPerform();
628
629     verifyForward( "editFileForm" );
630
631     form = ( DynaActionForm )getActionForm();
632     assertEquals( "", form.get( "text" ) );
633   }
634
635   public void testManageEditConfirm() throws Exception JavaDoc {
636     setUpFiles();
637
638     setRequestPathInfo( "/manageFiles.do" );
639     addRequestParameter( "action", "edit" );
640     addRequestParameter( "path", "Public/testdir" );
641     addRequestParameter( "file1", "testfile1.txt" );
642     addRequestParameter( "save", "yes" );
643     addRequestParameter( "text", "New text" );
644     actionPerform();
645
646     verifyForward( "editFileForm" );
647
648     form = ( DynaActionForm )getActionForm();
649     assertEquals( "New text", form.get( "text" ) );
650
651     assertEquals( "New text", FileUtils.readFileToString( testFile1_, "ISO-8859-1" ) );
652   }
653
654   public void testManageUnzip() throws Exception JavaDoc {
655     setUpFiles();
656
657     setRequestPathInfo( "/manageFiles.do" );
658     addRequestParameter( "action", "unzip" );
659     addRequestParameter( "path", "Public/testdir" );
660     addRequestParameter( "file1", "test.zip" );
661     actionPerform();
662
663     verifyForward( "confirm" );
664     verifyActionMessages( new String JavaDoc[] { "res.confirmunzip" } );
665   }
666
667   public void testManageUnzipConfirm() throws Exception JavaDoc {
668     setUpFiles();
669
670     setRequestPathInfo( "/manageFiles.do" );
671     addRequestParameter( "action", "unzip" );
672     addRequestParameter( "path", "Public/testdir" );
673     addRequestParameter( "file1", "test.zip" );
674     addRequestParameter( "confirm", "Confirm" );
675     actionPerform();
676
677     verifyForwardPath( "/listFiles.do?path=Public/testdir" );
678
679     file = new File( testDir_, "test.txt" );
680     assertTrue( file.exists() );
681     assertTrue( file.isFile() );
682
683     file = new File( testDir_, "subdir" );
684     assertTrue( file.exists() );
685     assertTrue( file.isDirectory() );
686
687     //
688
// make sure the file tree got updated
689
//
690
fileTree = ResUtils.getFileTree( new DefaultResPolicy(), request );
691     assertNotNull( fileTree );
692
693     node = fileTree.find( "Public/testdir/subdir" );
694     assertNotNull( node );
695   }
696
697   public void testDoUploadFileForm() {
698     try {
699
700       //
701
// upload a file for the first time
702
//
703
setUpFiles();
704
705       addRequestParameter( "path", "Public/testdir" );
706       setRequestPathInfo( "/uploadFileForm.do" );
707       actionPerform();
708
709       verifyForward( "form" );
710     }
711     catch ( Exception JavaDoc e ) {
712       e.printStackTrace();
713       fail();
714     }
715   }
716
717   public void beginDoUploadFile( WebRequest webRequest ) throws Exception JavaDoc {
718
719     File file = new File("build.properties" );
720           
721     FilePart part = new FilePart("application/octet-stream", file);
722     part.setName( "file" );
723
724     Part[] parts = new Part[] { part } ;
725     
726     if (Part.getBoundary() != null) {
727       webRequest.setContentType(
728         "multipart/form-data" + "; boundary=" + Part.getBoundary());
729     }
730
731     ByteArrayOutputStream out = new ByteArrayOutputStream();
732     Part.sendParts( out, parts );
733
734     ByteArrayInputStream in = new ByteArrayInputStream( out.toByteArray() );
735     webRequest.setUserData( in );
736   }
737
738   public void testDoUploadFile() throws Exception JavaDoc {
739     //
740
// upload a file for the first time
741
//
742
setUpFiles();
743
744     addRequestParameter( "path", "Public/testdir" );
745     setRequestPathInfo( "/uploadFile.do" );
746     actionPerform();
747
748     verifyForwardPath( "/listFiles.do?path=Public/testdir" );
749
750     file = new File( testDir_, "build.properties" );
751     assertTrue( file.exists() );
752   }
753
754   public void beginDoUploadFileConfirm( WebRequest webRequest ) throws Exception JavaDoc {
755
756     File file = new File("build.properties" );
757           
758     FilePart part = new FilePart("application/octet-stream", file);
759     part.setName( "file" );
760
761     Part[] parts = new Part[] { part } ;
762     
763     if (Part.getBoundary() != null) {
764       webRequest.setContentType(
765         "multipart/form-data" + "; boundary=" + Part.getBoundary());
766     }
767
768     ByteArrayOutputStream out = new ByteArrayOutputStream();
769     Part.sendParts( out, parts );
770
771     ByteArrayInputStream in = new ByteArrayInputStream( out.toByteArray() );
772     webRequest.setUserData( in );
773   }
774
775   public void testDoUploadFileConfirm() throws Exception JavaDoc {
776     setUpFiles();
777
778     //
779
// copy the file into place before the upload
780
//
781
FileUtils.copyFileToDirectory( new File( "build.properties" ), testDir_ );
782
783     addRequestParameter( "path", "Public/testdir" );
784     setRequestPathInfo( "/uploadFile.do" );
785     actionPerform();
786
787     verifyForward( "confirm" );
788     verifyActionMessages( new String JavaDoc[] { "res.fileexists" } );
789   }
790
791   public void testDoUploadFileCancel() {
792     try {
793
794       //
795
// upload a file for the first time
796
//
797
setUpFiles();
798
799       setRequestPathInfo( "/uploadFile.do" );
800       addRequestParameter( "path", "Public/testdir" );
801       addRequestParameter( "cancel", "Cancel" );
802       actionPerform();
803
804       verifyForwardPath( "/listFiles.do?path=Public/testdir" );
805     }
806     catch ( Exception JavaDoc e ) {
807       e.printStackTrace();
808       fail();
809     }
810   }
811
812   public void testDoCreateFileForm() {
813     try {
814
815       //
816
// upload a file for the first time
817
//
818
setUpFiles();
819
820       addRequestParameter( "path", "Public/testdir" );
821       setRequestPathInfo( "/createFileForm.do" );
822       actionPerform();
823
824       verifyForward( "form" );
825     }
826     catch ( Exception JavaDoc e ) {
827       e.printStackTrace();
828       fail();
829     }
830   }
831
832   public void testDoCreateFile() throws Exception JavaDoc {
833     //
834
// create a file
835
//
836
setUpFiles();
837
838     addRequestParameter( "path", "Public/testdir" );
839     addRequestParameter( "name", "newfile.txt" );
840     setRequestPathInfo( "/createFile.do" );
841     actionPerform();
842
843     verifyForwardPath( "/listFiles.do?path=Public/testdir" );
844
845     file = new File( testDir_, "newfile.txt" );
846     assertTrue( file.exists() );
847     assertTrue( file.isFile() );
848   }
849
850   public void testDoCreateFileConfirm() throws Exception JavaDoc {
851     setUpFiles();
852
853     //
854
// create file before we execute the action
855
//
856
FileUtils.copyFile( new File( "build.properties" ), new File( testDir_, "newfile.txt" ) );
857
858     addRequestParameter( "path", "Public/testdir" );
859     addRequestParameter( "name", "newfile.txt" );
860     setRequestPathInfo( "/createFile.do" );
861     actionPerform();
862
863     verifyForward( "confirm" );
864     verifyActionMessages( new String JavaDoc[] { "res.fileexists" } );
865   }
866
867   public void testDoCreateFileCancel() {
868     try {
869
870       //
871
// upload a file for the first time
872
//
873
setUpFiles();
874
875       setRequestPathInfo( "/createFile.do" );
876       addRequestParameter( "path", "Public/testdir" );
877       addRequestParameter( "name", "test.text" );
878       addRequestParameter( "cancel", "Cancel" );
879       actionPerform();
880
881       verifyForwardPath( "/listFiles.do?path=Public/testdir" );
882     }
883     catch ( Exception JavaDoc e ) {
884       e.printStackTrace();
885       fail();
886     }
887   }
888
889   public void testDoCreateDir() throws Exception JavaDoc {
890     setUpFiles();
891
892     addRequestParameter( "path", "Public/testdir" );
893     addRequestParameter( "name", "newdir" );
894     addRequestParameter( "createdir", "yes" );
895     setRequestPathInfo( "/createFile.do" );
896     actionPerform();
897
898     verifyForwardPath( "/listFiles.do?path=Public/testdir" );
899
900     file = new File( testDir_, "newdir" );
901     assertTrue( file.exists() );
902     assertTrue( file.isDirectory() );
903   }
904
905   public void testDoCreateDirCantOverwrite() throws Exception JavaDoc {
906     setUpFiles();
907
908     //
909
// create the dir first
910
//
911
( new File( testDir_, "newdir" ) ).mkdir();
912
913     addRequestParameter( "path", "Public/testdir" );
914     addRequestParameter( "name", "newdir" );
915     addRequestParameter( "createdir", "yes" );
916     setRequestPathInfo( "/createFile.do" );
917     actionPerform();
918
919     verifyInputForward();
920     verifyActionErrors( new String JavaDoc[] { "res.cantoverwrite" } );
921
922     //
923
// make sure the file tree got updated
924
//
925
fileTree = ResUtils.getFileTree( new DefaultResPolicy(), request );
926     assertNotNull( fileTree );
927
928     node = fileTree.find( "Public/testdir/newdir" );
929     assertNotNull( node );
930   }
931 }
932
Popular Tags