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