source: trunk/test/unit/dbnp/studycapturing/FileServiceTests.groovy @ 1430

Last change on this file since 1430 was 1430, checked in by work@…, 10 years ago
  • set keyword expansion
  • Property svn:keywords set to Rev Author Date
File size: 3.3 KB
Line 
1package dbnp.studycapturing
2
3import grails.test.*
4
5/**
6 * FileServiceTests Test
7 *
8 * Description of my test
9 *
10 * @author  your email (+name?)
11 * @since       2010mmdd
12 * @package     ???
13 *
14 * Revision information:
15 * $Rev: 1430 $
16 * $Author: work@osx.eu $
17 * $Date: 2011-01-21 20:05:36 +0000 (vr, 21 jan 2011) $
18 */
19class FileServiceTests extends GrailsUnitTestCase {
20    def tmpFile;
21    def fileService;
22
23    protected void setUp() {
24        super.setUp()
25
26        fileService = new FileService();
27
28        // Override uploadDir method because the applicationContext is not
29        // available in testcases
30        fileService.metaClass.getUploadDir = {
31            return new File( System.properties['base.dir'] + File.separator + 'web-app' + File.separator + 'fileuploads' );
32        }
33
34        // Create two files
35        println( fileService.getUploadDir() );
36
37        new File( fileService.getUploadDir(), 'FileServiceTest.txt' ).createNewFile();
38        new File( fileService.getUploadDir(), 'FileServiceTest2.txt' ).createNewFile();
39        new File( fileService.getUploadDir(), 'FileServiceTest3' ).createNewFile();
40
41        // Make sure the file 'nonExistent.txt' does not exist
42        def f = new File( fileService.getUploadDir(), 'nonExistent.txt' );
43        if( f.exists() ) {
44            tmpFile = new File( fileService.getUploadDir(), 'nonExistent-' . System.currentTimeMillis() . '.txt' );
45            f.transferTo( tmpFile );
46        }
47
48    }
49
50    protected void tearDown() {
51        super.tearDown()
52        new File( fileService.getUploadDir(), 'FileServiceTest.txt' ).delete();
53        new File( fileService.getUploadDir(), 'FileServiceTest2.txt' ).delete();
54        new File( fileService.getUploadDir(), 'FileServiceTest3' ).delete();
55
56        // If file existed, move back
57        if( tmpFile ) {
58            tmpFile.transferTo( new File( fileService.getUploadDir(), 'nonExistent.txt' ) );
59        }
60
61    }
62
63    void testGetUploadDir() {
64        assert fileService.getUploadDir() instanceof File;
65        assert fileService.getUploadDir().exists();
66        assert fileService.getUploadDir().isDirectory();
67
68        println fileService.getUploadDir();
69    }
70
71    void testFileExists() {
72        assert fileService.fileExists( 'FileServiceTest.txt' );
73        assert fileService.fileExists( 'FileServiceTest3' );
74        assert !fileService.fileExists( 'nonExistent.txt' );
75    }
76
77    void testMoveFileToUploadDir() {
78        // Move file
79        fileService.moveFileToUploadDir( new File( fileService.getUploadDir(), 'FileServiceTest2.txt' ) );
80        assert !fileService.fileExists( "FileServiceTest2.txt" );
81        assert fileService.fileExists( "FileServiceTest20.txt" );
82
83        fileService.moveFileToUploadDir( new File( fileService.getUploadDir(), 'FileServiceTest20.txt' ), "FileServiceTest2.txt" );
84        assert fileService.fileExists( "FileServiceTest2.txt" );
85        assert !fileService.fileExists( "FileServiceTest20.txt" );
86    }
87
88    void testUniqueFilename() {
89        assert fileService.fileExists( "FileServiceTest2.txt" );
90        assert !fileService.fileExists( fileService.getUniqueFilename( "FileServiceTest2.txt" ) )
91
92        assert !fileService.fileExists( 'nonExistent.txt' );
93        assert fileService.getUniqueFilename( 'nonExistent.txt' ) == 'nonExistent.txt'
94    }
95
96}
Note: See TracBrowser for help on using the repository browser.