1 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 File privateTestDir = new File( session.getServletContext().getRealPath( "/WEB-INF/resources/1" ) ); 71 if ( privateTestDir.exists() ) 72 FileUtils.deleteDirectory( privateTestDir ); 73 privateTestDir.mkdirs(); 74 75 testDir_ = new File( session.getServletContext().getRealPath( "/1/testdir" ) ); 79 if ( testDir_.exists() ) 80 FileUtils.deleteDirectory( testDir_ ); 81 82 testDir_.mkdir(); 86 87 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 e ) { 123 fail(); 124 } 125 } 126 127 static { 128 TestUtils.initLogger(); 129 } 130 131 public ResFormTest( String name ) { 132 super( name ); 133 } 134 135 public void setUp() { 136 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 AuthUtil.setUser( request, TestData.user1 ); 151 } 152 catch ( Exception e ) { 153 fail( e.getMessage() ); 154 } 155 } 156 157 public void tearDown() 158 throws 159 Exception { 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 e ) { 176 e.printStackTrace(); 177 fail(); 178 } 179 } 180 181 public void testAddSubdirOptions() { 182 try { 183 form = new ResForm(); 184 185 list = new ArrayList(); 189 form.addSubdirOptions( list, "Test", new File( session.getServletContext().getRealPath( "/1/testdir" ) ), "" ); 190 191 assertEquals( 4, list.size() ); 195 196 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 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 list = form.getDestinationOptions( fileManager ); 231 232 assertEquals( 4, list.size() ); 236 237 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 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 list = ( List )form.get( "destinations" ); 278 assertNotNull( list ); 279 assertTrue( list.size() > 0 ); 280 281 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 e ) { 300 e.printStackTrace(); 301 fail(); 302 } 303 } 304 } 305
| Popular Tags
|