source: trunk/test/unit/dbnp/studycapturing/TemplateFieldFileTests.groovy @ 550

Last change on this file since 550 was 550, checked in by keesvb, 14 years ago

added entity property to TemplateField?; added validator to Template to ensure fields are of the correct entity, updated BootStrap? accordingly, updated tests accordingly

File size: 6.7 KB
Line 
1package dbnp.studycapturing
2
3import grails.test.*
4
5class TemplateFieldFileTests extends GrailsUnitTestCase {
6    def testEvent;
7    def fileService;
8
9
10    protected void setUp() {
11        super.setUp()
12
13        fileService = new FileService();
14
15        // Override uploadDir method because the applicationContext is not
16        // available in testcases
17        fileService.metaClass.getUploadDir = {
18            return new File( System.properties['base.dir'] + File.separator + 'web-app' + File.separator + 'fileuploads' );
19        }
20       
21        // Create the template itself
22        def testTemplate = new Template(
23                name: 'Template for testing relative file fields',
24                entity: dbnp.studycapturing.Event,
25                fields: [
26                    new TemplateField(
27                        name: 'testRelTime',
28                        type: TemplateFieldType.RELTIME,
29                            entity: Event
30                    ),
31                    new TemplateField(
32                        name: 'testFile',
33                        type: TemplateFieldType.FILE
34                    )
35                ]
36            );
37
38        this.testEvent = new Event(
39                template: testTemplate,
40                startTime: 3600,
41                endTime: 7200
42        )
43
44        // Sometimes the fileService is not created yet
45        if( !testEvent.fileService ) {
46            testEvent.fileService = fileService;
47        }
48
49        // Create two files
50        println( fileService.getUploadDir() );
51
52        new File( fileService.getUploadDir(), 'TemplateFieldTest.txt' ).createNewFile();
53        new File( fileService.getUploadDir(), 'TemplateFieldTest2.txt' ).createNewFile();
54        new File( fileService.getUploadDir(), 'TemplateFieldTest3.txt' ).createNewFile();
55
56        // Create a new directory
57        new File( fileService.getUploadDir(), 'subdir' ).mkdir();
58        new File( fileService.getUploadDir(), 'subdir/TemplateFieldSub.txt' ).createNewFile();
59
60    }
61
62    protected void tearDown() {
63        super.tearDown()
64
65        new File( fileService.getUploadDir(), 'TemplateFieldTest.txt' ).delete();
66        new File( fileService.getUploadDir(), 'TemplateFieldTest2.txt' ).delete();
67        new File( fileService.getUploadDir(), 'TemplateFieldTest3.txt' ).delete();
68
69        new File( fileService.getUploadDir(), 'subdir/TemplateFieldSub.txt' ).delete();
70        new File( fileService.getUploadDir(), 'subdir' ).delete();
71    }
72
73    void testFileFieldCreation() {
74        def FileField = new TemplateField(
75                name: 'testFile',
76                type: TemplateFieldType.FILE
77        );
78    }
79
80
81        // If NULL is given, the field value is emptied and the old file is removed
82        // If an empty string is given, the field value is kept as was
83        // If a file is given, it is moved to the right directory. Old files are deleted. If
84        //   the file does not exist, the field is kept
85        // If a string is given, it is supposed to be a file in the upload directory. If
86        //   it is different from the old one, the old one is deleted. If the file does not
87        //   exist, the field is kept.
88
89    void testFileSetValue() {
90        // Check whether the field exists
91        assert this.testEvent.fieldExists( 'testFile' );
92
93        // See that it is not a domain field
94        assert !this.testEvent.isDomainField( 'testFile' );
95
96        // See that it is a FILE field
97        // assert !this.testEvent.getField( 'testFile' ).type == TemplateFieldType.FILE;
98        println( this.testEvent.getStore( TemplateFieldType.FILE ) );
99       
100        // Set the name of an existing file
101        assert fileService.fileExists( 'TemplateFieldTest.txt' );
102        this.testEvent.setFieldValue( 'testFile', 'TemplateFieldTest.txt' );
103        assert this.testEvent.getFieldValue( 'testFile' ) == 'TemplateFieldTest.txt';
104        assert fileService.fileExists( 'TemplateFieldTest.txt' );
105
106        // Set the name of a non existing file
107        assert !fileService.fileExists( 'TemplateFieldTestNotExisting.txt' );
108        this.testEvent.setFieldValue( 'testFile', 'TemplateFieldTestNotExisting.txt' );
109        assert this.testEvent.getFieldValue( 'testFile' ) == 'TemplateFieldTest.txt';
110        assert fileService.fileExists( 'TemplateFieldTest.txt' );
111        assert !fileService.fileExists( 'TemplateFieldTestNotExisting.txt' );
112
113        // Set the name of a new existing file, and the old one is deleted
114        assert fileService.fileExists( 'TemplateFieldTest.txt' );
115        assert fileService.fileExists( 'TemplateFieldTest2.txt' );
116        this.testEvent.setFieldValue( 'testFile', 'TemplateFieldTest2.txt' );
117        assert this.testEvent.getFieldValue( 'testFile' ) == 'TemplateFieldTest2.txt';
118        assert !fileService.fileExists( 'TemplateFieldTest.txt' );
119        assert fileService.fileExists( 'TemplateFieldTest2.txt' );
120
121        // Set a nonexisting File object
122        assert fileService.fileExists( 'TemplateFieldTest2.txt' );
123        assert !fileService.fileExists( 'NonExistent.txt' );
124        this.testEvent.setFieldValue( 'testFile', new File( fileService.getUploadDir(), 'NonExistent.txt' ) );
125        assert this.testEvent.getFieldValue( 'testFile' ) == 'TemplateFieldTest2.txt';
126        assert fileService.fileExists( 'TemplateFieldTest2.txt' );
127
128        // Set a existing File object
129        assert fileService.fileExists( 'TemplateFieldTest2.txt' );
130        assert fileService.fileExists( 'subdir/TemplateFieldSub.txt' );
131        assert !fileService.fileExists( 'TemplateFieldSub.txt' );
132
133        def f = new File( fileService.getUploadDir(), 'subdir/TemplateFieldSub.txt' )
134        this.testEvent.setFieldValue( 'testFile', f );
135       
136        assert this.testEvent.getFieldValue( 'testFile' ) == 'TemplateFieldSub.txt';
137        assert fileService.fileExists( this.testEvent.getFieldValue( 'testFile' ) );
138        assert !fileService.fileExists( 'TemplateFieldTest2.txt' );
139
140        //fileService.delete( this.testEvent.getFieldValue( 'testFile' ) );
141
142        // Set the name of an empty string
143        assert fileService.fileExists( 'TemplateFieldTest3.txt' );
144        this.testEvent.setFieldValue( 'testFile', 'TemplateFieldTest3.txt' );
145        this.testEvent.setFieldValue( 'testFile', '' );
146        assert this.testEvent.getFieldValue( 'testFile' ) == 'TemplateFieldTest3.txt';
147        assert fileService.fileExists( 'TemplateFieldTest3.txt' );
148
149        // THe old file must be deleted
150        assert !fileService.fileExists( 'TemplateFieldSub.txt' );
151
152        // Set the name to NULL (empty the field
153        this.testEvent.setFieldValue( 'testFile', null );
154        assert this.testEvent.getFieldValue( 'testFile' ) == null;
155        assert !fileService.fileExists( 'TemplateFieldTest3.txt' );
156
157    }
158
159}
Note: See TracBrowser for help on using the repository browser.