source: trunk/test/unit/AppConfigServiceTests.groovy @ 234

Last change on this file since 234 was 234, checked in by gav, 14 years ago

New AppConfig class for storing application configuration settings in the database.
Service class, unit tests, views and controller to suite.

File size: 3.2 KB
Line 
1import grails.test.*
2
3/**
4* Unit tests for AppConfigService class.
5*/
6class AppConfigServiceTests extends GrailsUnitTestCase {
7
8    def appConfigService // not automatically injected, assignment is bellow.
9
10    // Some test data.
11    def configName01 = "baseDataCreated"
12    def configName02 = "dateFormat"
13    def configName03 = "blankValue"
14    def blankString = ''
15    def configDefaultValue = "true"
16    def configValue01 = "false"
17    def configValue02= "EEE, dd-MMM-yyyy"
18
19    protected void setUp() {
20        super.setUp()
21
22        // Mock the domain class.
23        mockDomain(AppConfig)
24        mockForConstraintsTests(AppConfig)
25
26        // Mock logging with debug enabled/disabled, then get an instance of the service.
27        mockLogging(AppConfigService, true)
28        appConfigService = new AppConfigService()
29    }
30
31    protected void tearDown() {
32        super.tearDown()
33    }
34
35    void testSet() {
36
37        // Set returns false for blank name.
38        assert AppConfig.list().size == 0
39        assertFalse appConfigService.set(blankString)
40        assert AppConfig.list().size == 0
41
42        // Set returns true and saves an object.
43        assertTrue appConfigService.set(configName01)
44        assert AppConfig.list().size == 1
45
46        // Set returns true and does not save a new object for existing appConfig.
47        assertTrue appConfigService.set(configName01, configValue01)
48        assert AppConfig.list().size == 1
49
50        // Set returns true and saves different objects.
51        assertTrue appConfigService.set(configName02, configValue02)
52        assert AppConfig.list().size == 2
53
54        // Set returns false when trying to set a blank value.
55        assertFalse appConfigService.set(configName03, blankString)
56        assert AppConfig.list().size == 2
57    }
58
59    void testExists() {
60
61        // Exists returns false for non existant appConfig.
62        assertFalse appConfigService.exists(configName01)
63
64        // Exists returns true for existing appConfig.
65        appConfigService.set(configName01)
66        assertTrue appConfigService.exists(configName01)
67    }
68
69    void testGetValue() {
70
71        // Get returns false for non existant appConfig.
72        assertFalse appConfigService.getValue(configName01)
73
74        // Set default and check that it returns.
75        appConfigService.set(configName01)
76        assert appConfigService.getValue(configName01) == configDefaultValue
77
78        // Set a value and check that it returns.
79        appConfigService.set(configName01, configValue01)
80        assert appConfigService.getValue(configName01) == configValue01
81    }
82
83    void testDelete() {
84
85        // Deleting returns false for non existant appConfig.
86        assert AppConfig.list().size == 0
87        assertFalse appConfigService.delete(configName01)
88
89        // First set.
90        appConfigService.set(configName01)
91        assert AppConfig.list().size == 1
92
93        // Second set.
94        appConfigService.set(configName02, configValue02)
95        assert AppConfig.list().size == 2
96
97        // Delete first set.
98        assertTrue appConfigService.delete(configName01)
99        assert AppConfig.list().size == 1
100
101        // Delete second set.
102        assertTrue appConfigService.delete(configName02)
103        assert AppConfig.list().size == 0
104    }
105
106}
Note: See TracBrowser for help on using the repository browser.