001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.responders.files;
004:
005: import fitnesse.http.*;
006: import fitnesse.util.FileUtil;
007: import fitnesse.FitNesseContext;
008: import junit.framework.TestCase;
009:
010: import java.io.File;
011:
012: public class UploadResponderTest extends TestCase {
013: private FitNesseContext context;
014:
015: private UploadResponder responder;
016:
017: private MockRequest request;
018:
019: private File testFile;
020:
021: public void setUp() throws Exception {
022: context = new FitNesseContext();
023: context.rootPagePath = "testdir";
024: FileUtil.makeDir("testdir");
025: FileUtil.makeDir("testdir/files");
026: testFile = FileUtil.createFile("testDir/tempFile.txt",
027: "test content");
028:
029: responder = new UploadResponder();
030: request = new MockRequest();
031: }
032:
033: public void tearDown() throws Exception {
034: FileUtil.deleteFileSystemDirectory("testdir");
035: }
036:
037: public void testMakeResponse() throws Exception {
038: request.addInput("file", new UploadedFile("sourceFilename.txt",
039: "plain/text", testFile));
040: request.setResource("files/");
041:
042: Response response = responder.makeResponse(context, request);
043:
044: File file = new File("testdir/files/sourceFilename.txt");
045: assertTrue(file.exists());
046: assertEquals("test content", FileUtil.getFileContent(file));
047:
048: assertEquals(303, response.getStatus());
049: assertEquals("/files/", response.getHeader("Location"));
050: }
051:
052: public void testMakeResponseSpaceInFileName() throws Exception {
053: request.addInput("file", new UploadedFile(
054: "source filename.txt", "plain/text", testFile));
055: request.setResource("files/");
056:
057: Response response = responder.makeResponse(context, request);
058:
059: File file = new File("testdir/files/source filename.txt");
060: assertTrue(file.exists());
061: assertEquals("test content", FileUtil.getFileContent(file));
062:
063: assertEquals(303, response.getStatus());
064: assertEquals("/files/", response.getHeader("Location"));
065: }
066:
067: public void testMakeRelativeFilename() throws Exception {
068: String name1 = "name1.txt";
069: String name2 = "name2";
070: String name3 = "C:\\folder\\name3.txt";
071: String name4 = "/home/user/name4.txt";
072:
073: assertEquals("name1.txt", UploadResponder
074: .makeRelativeFilename(name1));
075: assertEquals("name2", UploadResponder
076: .makeRelativeFilename(name2));
077: assertEquals("name3.txt", UploadResponder
078: .makeRelativeFilename(name3));
079: assertEquals("name4.txt", UploadResponder
080: .makeRelativeFilename(name4));
081: }
082:
083: public void testMakeNewFilename() throws Exception {
084: assertEquals("file_copy1.txt", UploadResponder.makeNewFilename(
085: "file.txt", 1));
086: assertEquals("file_copy2.txt", UploadResponder.makeNewFilename(
087: "file.txt", 2));
088: assertEquals("a.b.c.d_copy2.txt", UploadResponder
089: .makeNewFilename("a.b.c.d.txt", 2));
090: assertEquals("somefile_copy1", UploadResponder.makeNewFilename(
091: "somefile", 1));
092: }
093:
094: public void testWriteFile() throws Exception {
095: File file = new File("testdir/testFile");
096: File inputFile = FileUtil.createFile("testdir/testInput",
097: "heres the content");
098: UploadedFile uploaded = new UploadedFile("testdir/testOutput",
099: "text", inputFile);
100:
101: long inputFileLength = inputFile.length();
102: String inputFileContent = FileUtil.getFileContent(inputFile);
103:
104: responder.writeFile(file, uploaded);
105:
106: assertEquals(inputFileLength, file.length());
107: assertEquals(inputFileContent, FileUtil.getFileContent(file));
108: }
109: }
|