1 | package dbnp.studycapturing |
---|
2 | |
---|
3 | import grails.test.* |
---|
4 | |
---|
5 | class TemplateFieldTests extends GrailsUnitTestCase { |
---|
6 | def testEvent; |
---|
7 | protected void setUp() { |
---|
8 | super.setUp() |
---|
9 | |
---|
10 | // Create the template itself |
---|
11 | def testTemplate = new Template( |
---|
12 | name: 'Template for testing relative date fields', |
---|
13 | entity: dbnp.studycapturing.Event, |
---|
14 | fields: [ |
---|
15 | new TemplateField( |
---|
16 | name: 'testStartDate', |
---|
17 | type: TemplateFieldType.DATE |
---|
18 | ), |
---|
19 | new TemplateField( |
---|
20 | name: 'testRelTime', |
---|
21 | type: TemplateFieldType.RELTIME |
---|
22 | ), |
---|
23 | new TemplateField( |
---|
24 | name: 'testFile', |
---|
25 | type: TemplateFieldType.FILE |
---|
26 | ) |
---|
27 | ] |
---|
28 | ); |
---|
29 | |
---|
30 | this.testEvent = new Event( |
---|
31 | template: testTemplate, |
---|
32 | startTime: 3600, |
---|
33 | endTime: 7200 |
---|
34 | ) |
---|
35 | } |
---|
36 | |
---|
37 | protected void tearDown() { |
---|
38 | super.tearDown() |
---|
39 | } |
---|
40 | |
---|
41 | void testRelTimeFieldCreation() { |
---|
42 | def RelTimeField = new TemplateField( |
---|
43 | name: 'RelTime', |
---|
44 | type: TemplateFieldType.RELTIME |
---|
45 | ); |
---|
46 | } |
---|
47 | |
---|
48 | void testRelTimeSetValue() { |
---|
49 | // Check whether the field exists |
---|
50 | assert this.testEvent.fieldExists( 'testRelTime' ); |
---|
51 | |
---|
52 | // See that it is not a domain field |
---|
53 | assert !this.testEvent.isDomainField( 'testRelTime' ); |
---|
54 | println( this.testEvent.getStore( TemplateFieldType.RELTIME ) ); |
---|
55 | |
---|
56 | this.testEvent.setFieldValue( 'testRelTime', 10 ); |
---|
57 | assert this.testEvent.getFieldValue( 'testRelTime' ) == 10; |
---|
58 | |
---|
59 | this.testEvent.setFieldValue( 'testRelTime', 0 ); |
---|
60 | assert this.testEvent.getFieldValue( 'testRelTime' ) == 0; |
---|
61 | |
---|
62 | this.testEvent.setFieldValue( 'testRelTime', -130 ); |
---|
63 | assert this.testEvent.getFieldValue( 'testRelTime' ) == -130; |
---|
64 | |
---|
65 | // RelTime must be able to handle 100 years |
---|
66 | long hundredYears = 100L * 365 * 24 * 3600; |
---|
67 | this.testEvent.setFieldValue( 'testRelTime', hundredYears ); |
---|
68 | assert this.testEvent.getFieldValue( 'testRelTime' ) == hundredYears; |
---|
69 | } |
---|
70 | |
---|
71 | // Tests the parsing of a string for relative dates |
---|
72 | // @see TemplateEntity.setFieldValue |
---|
73 | // |
---|
74 | // The relative date may be set as a string, using the following format |
---|
75 | // |
---|
76 | // #w #d #h #m #s |
---|
77 | // |
---|
78 | // Where w = weeks, d = days, h = hours, m = minutes, s = seconds |
---|
79 | // |
---|
80 | // The spaces between the values are optional. Every timespan |
---|
81 | // (w, d, h, m, s) must appear at most once. You can also omit |
---|
82 | // timespans if needed or use a different order. |
---|
83 | // Other characters are disregarded, allthough results may not |
---|
84 | // always be as expected. |
---|
85 | // |
---|
86 | // If an incorrect format is used, which can't be parsed |
---|
87 | // an IllegalFormatException is thrown. |
---|
88 | // |
---|
89 | // An empty span is treated as zero seconds. |
---|
90 | // |
---|
91 | // Examples: |
---|
92 | // --------- |
---|
93 | // 5d 3h 20m // 5 days, 3 hours and 20 minutes |
---|
94 | // 6h 2d // 2 days, 6 hours |
---|
95 | // 10m 200s // 13 minutes, 20 seconds (200s == 3m + 20s) |
---|
96 | // 5w4h15m // 5 weeks, 4 hours, 15 minutes |
---|
97 | // |
---|
98 | // 16x14w10d // Incorrect. 16x is disregarded, so the |
---|
99 | // // result is 15 weeks, 3 days |
---|
100 | // 13days // Incorrect: days should be d, but this is |
---|
101 | // // parsed as 13d, 0 seconds |
---|
102 | // |
---|
103 | void testRelTimeParser() { |
---|
104 | def s = 1L; |
---|
105 | def m = 60L * s; |
---|
106 | def h = 60L * m; |
---|
107 | def d = 24L * h; |
---|
108 | def w = 7L * d; |
---|
109 | |
---|
110 | this.testEvent.setFieldValue( 'testRelTime', '' ); |
---|
111 | assert this.testEvent.getFieldValue( 'testRelTime' ) == 0; |
---|
112 | |
---|
113 | this.testEvent.setFieldValue( 'testRelTime', ' ' ); |
---|
114 | assert this.testEvent.getFieldValue( 'testRelTime' ) == 0; |
---|
115 | |
---|
116 | this.testEvent.setFieldValue( 'testRelTime', '5d 3h 20m' ); |
---|
117 | assert this.testEvent.getFieldValue( 'testRelTime' ) == 5 * d + 3 * h + 20 * m; |
---|
118 | |
---|
119 | this.testEvent.setFieldValue( 'testRelTime', '6h 2d' ); |
---|
120 | assert this.testEvent.getFieldValue( 'testRelTime' ) == 2 * d + 6 * h; |
---|
121 | |
---|
122 | this.testEvent.setFieldValue( 'testRelTime', '10m 200s' ); |
---|
123 | assert this.testEvent.getFieldValue( 'testRelTime' ) == 10 * m + 200 * s; |
---|
124 | |
---|
125 | this.testEvent.setFieldValue( 'testRelTime', '5w4h15m' ); |
---|
126 | assert this.testEvent.getFieldValue( 'testRelTime' ) == 5 * w + 4 * h + 15 * m; |
---|
127 | |
---|
128 | // Should parse correctly, allthough it is not completely correct |
---|
129 | this.testEvent.setFieldValue( 'testRelTime', '16x14w10d' ); |
---|
130 | assert this.testEvent.getFieldValue( 'testRelTime' ) == 14 * w + 10 * d; |
---|
131 | |
---|
132 | this.testEvent.setFieldValue( 'testRelTime', '13days' ); |
---|
133 | assert this.testEvent.getFieldValue( 'testRelTime' ) == 13 * d; |
---|
134 | |
---|
135 | // Test whether an IllegalFormatException is thrown |
---|
136 | try { |
---|
137 | this.testEvent.setFieldValue( 'testRelTime', 'nonexistent relative date' ); |
---|
138 | fail(); |
---|
139 | } catch(IllegalArgumentException ex) { |
---|
140 | } catch( Exception ex ) { |
---|
141 | fail(); |
---|
142 | } |
---|
143 | |
---|
144 | } |
---|
145 | } |
---|