1 61 62 63 package org.apache.commons.fileupload; 64 65 66 import java.io.File ; 67 import java.io.IOException ; 68 import java.io.OutputStream ; 69 import java.util.Arrays ; 70 71 import junit.framework.TestCase; 72 import org.apache.commons.fileupload.DefaultFileItem; 73 import org.apache.commons.fileupload.DefaultFileItemFactory; 74 75 76 81 public class DefaultFileItemTest extends TestCase 82 { 83 84 87 private static final String textContentType = "text/plain"; 88 89 92 private static final String fileContentType = "application/octet-stream"; 93 94 97 private static final int threshold = 16; 98 99 104 public DefaultFileItemTest(String name) 105 { 106 super(name); 107 } 108 109 112 public void testTextFieldConstruction() 113 { 114 FileItemFactory factory = createFactory(null); 115 String textFieldName = "textField"; 116 117 FileItem item = factory.createItem( 118 textFieldName, 119 textContentType, 120 true, 121 null 122 ); 123 assertNotNull(item); 124 assertEquals(item.getFieldName(), textFieldName); 125 assertEquals(item.getContentType(), textContentType); 126 assertTrue(item.isFormField()); 127 assertNull(item.getName()); 128 } 129 130 133 public void testFileFieldConstruction() 134 { 135 FileItemFactory factory = createFactory(null); 136 String fileFieldName = "fileField"; 137 String fileName = "originalFileName"; 138 139 FileItem item = factory.createItem( 140 fileFieldName, 141 fileContentType, 142 false, 143 fileName 144 ); 145 assertNotNull(item); 146 assertEquals(item.getFieldName(), fileFieldName); 147 assertEquals(item.getContentType(), fileContentType); 148 assertFalse(item.isFormField()); 149 assertEquals(item.getName(), fileName); 150 } 151 152 156 public void testBelowThreshold() 157 { 158 FileItemFactory factory = createFactory(null); 159 String textFieldName = "textField"; 160 String textFieldValue = "0123456789"; 161 byte[] testFieldValueBytes = textFieldValue.getBytes(); 162 163 FileItem item = factory.createItem( 164 textFieldName, 165 textContentType, 166 true, 167 null 168 ); 169 assertNotNull(item); 170 171 try 172 { 173 OutputStream os = item.getOutputStream(); 174 os.write(testFieldValueBytes); 175 os.close(); 176 } 177 catch(IOException e) 178 { 179 fail("Unexpected IOException"); 180 } 181 assertTrue(item.isInMemory()); 182 assertEquals(item.getSize(), testFieldValueBytes.length); 183 assertTrue(Arrays.equals(item.get(), testFieldValueBytes)); 184 assertEquals(item.getString(), textFieldValue); 185 } 186 187 191 public void testAboveThresholdDefaultRepository() 192 { 193 doTestAboveThreshold(null); 194 } 195 196 200 public void testAboveThresholdSpecifiedRepository() 201 { 202 String tempPath = System.getProperty("java.io.tmpdir"); 203 String tempDirName = "testAboveThresholdSpecifiedRepository"; 204 File tempDir = new File (tempPath, tempDirName); 205 tempDir.mkdir(); 206 doTestAboveThreshold(tempDir); 207 assertTrue(tempDir.delete()); 208 } 209 210 218 public void doTestAboveThreshold(File repository) 219 { 220 FileItemFactory factory = createFactory(repository); 221 String textFieldName = "textField"; 222 String textFieldValue = "01234567890123456789"; 223 byte[] testFieldValueBytes = textFieldValue.getBytes(); 224 225 FileItem item = factory.createItem( 226 textFieldName, 227 textContentType, 228 true, 229 null 230 ); 231 assertNotNull(item); 232 233 try 234 { 235 OutputStream os = item.getOutputStream(); 236 os.write(testFieldValueBytes); 237 os.close(); 238 } 239 catch(IOException e) 240 { 241 fail("Unexpected IOException"); 242 } 243 assertFalse(item.isInMemory()); 244 assertEquals(item.getSize(), testFieldValueBytes.length); 245 assertTrue(Arrays.equals(item.get(), testFieldValueBytes)); 246 assertEquals(item.getString(), textFieldValue); 247 248 assertTrue(item instanceof DefaultFileItem); 249 DefaultFileItem dfi = (DefaultFileItem) item; 250 File storeLocation = dfi.getStoreLocation(); 251 assertNotNull(storeLocation); 252 assertTrue(storeLocation.exists()); 253 assertEquals(storeLocation.length(), testFieldValueBytes.length); 254 255 if (repository != null) 256 { 257 assertEquals(storeLocation.getParentFile(), repository); 258 } 259 260 item.delete(); 261 } 262 263 264 272 protected FileItemFactory createFactory(File repository) 273 { 274 return new DefaultFileItemFactory(threshold, repository); 275 } 276 } 277 | Popular Tags |