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