KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.*;
25 import java.io.*;
26 import junit.framework.*;
27 import org.apache.log4j.*;
28 import com.methodhead.persistable.*;
29 import com.methodhead.test.*;
30 import com.methodhead.auth.*;
31 import com.methodhead.*;
32 import com.methodhead.sitecontext.*;
33 import com.methodhead.util.*;
34 import servletunit.struts.*;
35 import org.apache.cactus.*;
36 import org.apache.struts.action.*;
37 import org.apache.struts.util.*;
38 import com.methodhead.tree.*;
39 import org.apache.commons.io.*;
40
41 public class ResFormTest extends CactusStrutsTestCase {
42
43   private ResForm form = null;
44   File base = null;
45   File file = null;
46   LabelValueBean labelValue = null;
47   List list = null;
48   FileManager fileManager = null;
49   FoldingTreeNode node = null;
50   ActionMapping mapping = null;
51
52   private File testDir_ = null;
53   private File subDir1_ = null;
54   private File subDir2_ = null;
55   private File subDir3_ = null;
56   private File testFile1_ = null;
57   private File testFile2_ = null;
58   private File testFile3_ = null;
59   private File testFile4_ = null;
60   private File testFile5_ = null;
61
62   private void setUpFiles() {
63     try {
64
65       TestData.createWebappFiles( ServletUtils.getRealFile( request, "" ) );
66
67       //
68
// create a resources dir (we won't use it in these tests though)
69
//
70
File privateTestDir = new File( session.getServletContext().getRealPath( "/WEB-INF/resources/1" ) );
71       if ( privateTestDir.exists() )
72         FileUtils.deleteDirectory( privateTestDir );
73       privateTestDir.mkdirs();
74
75       //
76
// delete testdir if it exists
77
//
78
testDir_ = new File( session.getServletContext().getRealPath( "/1/testdir" ) );
79       if ( testDir_.exists() )
80         FileUtils.deleteDirectory( testDir_ );
81
82       //
83
// create testdir
84
//
85
testDir_.mkdir();
86
87       //
88
// testdir
89
// subdir1
90
// subdir3
91
// testfile4.txt
92
// testfile3.txt
93
// subdir2
94
// testfile5.txt
95
// testfile1.txt
96
// testfile2.txt
97
//
98
subDir1_ = new File( testDir_, "subdir1" );
99       subDir1_.mkdir();
100
101       subDir3_ = new File( testDir_, "subdir1/subdir3" );
102       subDir3_.mkdir();
103
104       testFile4_ = new File( testDir_, "subdir1/subdir3/testfile4.txt" );
105       testFile4_.createNewFile();
106
107       testFile3_ = new File( testDir_, "subdir1/testfile3.txt" );
108       testFile3_.createNewFile();
109
110       subDir2_ = new File( testDir_, "subdir2" );
111       subDir2_.mkdir();
112
113       testFile5_ = new File( testDir_, "subdir2/testfile5.txt" );
114       testFile5_.createNewFile();
115
116       testFile1_ = new File( testDir_, "testfile1.txt" );
117       testFile1_.createNewFile();
118
119       testFile2_ = new File( testDir_, "testfile2.txt" );
120       testFile2_.createNewFile();
121     }
122     catch ( Exception JavaDoc e ) {
123       fail();
124     }
125   }
126
127   static {
128     TestUtils.initLogger();
129   }
130
131   public ResFormTest( String JavaDoc name ) {
132     super( name );
133   }
134
135   public void setUp() {
136     //setLogLevel( Level.DEBUG );
137
try {
138       super.setUp();
139
140       ConnectionSingleton.runBatchUpdate( new FileReader( "webapp/WEB-INF/db/transfer-reset.sql" ) );
141
142       TestData.createUsers();
143       SiteContext.setContext( request, TestData.siteContext1 );
144
145       setUpFiles();
146
147       //
148
// log in an admin
149
//
150
AuthUtil.setUser( request, TestData.user1 );
151     }
152     catch ( Exception JavaDoc e ) {
153       fail( e.getMessage() );
154     }
155   }
156
157   public void tearDown()
158   throws
159     Exception JavaDoc {
160     super.tearDown();
161   }
162
163   public void testGetRelativePath() {
164     try {
165       form = new ResForm();
166
167       base = new File( "/foo" );
168       file = new File( "/foo/bar" );
169       assertEquals( "bar", form.getRelativePath( base, file ) );
170
171       base = new File( "" );
172       file = new File( "bar" );
173       assertEquals( "bar", form.getRelativePath( base, file ) );
174     }
175     catch ( Exception JavaDoc e ) {
176       e.printStackTrace();
177       fail();
178     }
179   }
180
181   public void testAddSubdirOptions() {
182     try {
183       form = new ResForm();
184
185       //
186
// get options for testdir
187
//
188
list = new ArrayList();
189       form.addSubdirOptions( list, "Test", new File( session.getServletContext().getRealPath( "/1/testdir" ) ), "" );
190
191       //
192
// correct number of options
193
//
194
assertEquals( 4, list.size() );
195
196       //
197
// correct options in the correct order
198
//
199
labelValue = ( LabelValueBean )list.get( 0 );
200       assertEquals( "Test", labelValue.getLabel() );
201       assertEquals( "Test", labelValue.getValue() );
202
203       labelValue = ( LabelValueBean )list.get( 1 );
204       assertEquals( "Test/subdir1", labelValue.getLabel() );
205       assertEquals( "Test/subdir1", labelValue.getValue() );
206
207       labelValue = ( LabelValueBean )list.get( 2 );
208       assertEquals( "Test/subdir1/subdir3", labelValue.getLabel() );
209       assertEquals( "Test/subdir1/subdir3", labelValue.getValue() );
210
211       labelValue = ( LabelValueBean )list.get( 3 );
212       assertEquals( "Test/subdir2", labelValue.getLabel() );
213       assertEquals( "Test/subdir2", labelValue.getValue() );
214     }
215     catch ( Exception JavaDoc e ) {
216       e.printStackTrace();
217       fail();
218     }
219   }
220
221   public void testGetDestinationOptions() {
222     try {
223       form = new ResForm();
224       fileManager = new FileManager();
225       fileManager.addDirectory( "Test", new File( session.getServletContext().getRealPath( "/1/testdir" ) ) );
226
227       //
228
// get options for testdir
229
//
230
list = form.getDestinationOptions( fileManager );
231
232       //
233
// correct number of options
234
//
235
assertEquals( 4, list.size() );
236
237       //
238
// correct options in the correct order
239
//
240
labelValue = ( LabelValueBean )list.get( 0 );
241       assertEquals( "Test", labelValue.getLabel() );
242       assertEquals( "Test", labelValue.getValue() );
243
244       labelValue = ( LabelValueBean )list.get( 1 );
245       assertEquals( "Test/subdir1", labelValue.getLabel() );
246       assertEquals( "Test/subdir1", labelValue.getValue() );
247
248       labelValue = ( LabelValueBean )list.get( 2 );
249       assertEquals( "Test/subdir1/subdir3", labelValue.getLabel() );
250       assertEquals( "Test/subdir1/subdir3", labelValue.getValue() );
251
252       labelValue = ( LabelValueBean )list.get( 3 );
253       assertEquals( "Test/subdir2", labelValue.getLabel() );
254       assertEquals( "Test/subdir2", labelValue.getValue() );
255     }
256     catch ( Exception JavaDoc e ) {
257       e.printStackTrace();
258       fail();
259     }
260   }
261
262   public void beginReset( WebRequest request ) {
263     request.addParameter( "path", "Public/testdir" );
264     request.addParameter( "file1", "testfile1.txt" );
265     request.addParameter( "file2", "subdir1" );
266   }
267
268   public void testReset() {
269     try {
270       setRequestPathInfo( "/manageFilesForm.do" );
271       actionPerform();
272       form = ( ResForm )getActionForm();
273
274       //
275
// make sure we got some options
276
//
277
list = ( List )form.get( "destinations" );
278       assertNotNull( list );
279       assertTrue( list.size() > 0 );
280
281       //
282
// make sure we got our files
283
//
284
list = ( List )form.get( "files" );
285       assertNotNull( list );
286       assertEquals( 2, list.size() );
287
288       node = ( FoldingTreeNode )list.get( 0 );
289       assertEquals( "subdir1", node.getLabel() );
290       file = ( File )node.getUserObject();
291       assertEquals( "subdir1", file.getName() );
292       assertTrue( file.isDirectory() );
293
294       node = ( FoldingTreeNode )list.get( 1 );
295       assertEquals( "testfile1.txt", node.getLabel() );
296       file = ( File )node.getUserObject();
297       assertEquals( "testfile1.txt", file.getName() );
298     }
299     catch ( Exception JavaDoc e ) {
300       e.printStackTrace();
301       fail();
302     }
303   }
304 }
305
Popular Tags