Add unit test for PluginStoreConfigurationBackend.
[Sone.git] / src / test / java / net / pterodactylus / sone / freenet / PluginStoreConfigurationBackendTest.java
1 /*
2  * Sone - PluginStoreConfigurationBackendTest.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.freenet;
19
20 import static org.hamcrest.MatcherAssert.assertThat;
21 import static org.hamcrest.Matchers.hasEntry;
22 import static org.hamcrest.Matchers.is;
23 import static org.hamcrest.Matchers.nullValue;
24 import static org.mockito.Matchers.eq;
25 import static org.mockito.Mockito.doThrow;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29
30 import net.pterodactylus.util.config.ConfigurationException;
31
32 import freenet.client.async.DatabaseDisabledException;
33 import freenet.pluginmanager.PluginRespirator;
34 import freenet.pluginmanager.PluginStore;
35
36 import org.junit.Test;
37
38 /**
39  * Unit test for {@link PluginStoreConfigurationBackend}.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class PluginStoreConfigurationBackendTest {
44
45         private final PluginRespirator pluginRespirator = mock(PluginRespirator.class);
46         private final PluginStore pluginStore = new PluginStore();
47         private final PluginStoreConfigurationBackend pluginStoreConfigurationBackend;
48
49         public PluginStoreConfigurationBackendTest() throws DatabaseDisabledException {
50                 when(pluginRespirator.getStore()).thenReturn(pluginStore);
51                 pluginStoreConfigurationBackend = new PluginStoreConfigurationBackend(pluginRespirator);
52         }
53
54         @Test(expected = DatabaseDisabledException.class)
55         public void notGettingAPluginStoreResultsInAnException() throws DatabaseDisabledException {
56                 PluginRespirator pluginRespirator = mock(PluginRespirator.class);
57                 new PluginStoreConfigurationBackend(pluginRespirator);
58         }
59
60         @Test
61         public void gettingAnExistingStringValueRetunsTheValue() throws ConfigurationException {
62                 pluginStore.strings.put("Key", "Value");
63                 String value = pluginStoreConfigurationBackend.getValue("Key");
64                 assertThat(value, is("Value"));
65         }
66
67         @Test(expected = ConfigurationException.class)
68         public void gettingANonExistingStringValueThrowsAnException() throws ConfigurationException {
69                 pluginStoreConfigurationBackend.getValue("Key");
70         }
71
72         @Test
73         public void storingAStringValue() throws ConfigurationException {
74                 pluginStoreConfigurationBackend.putValue("Key", "Value");
75                 assertThat(pluginStore.strings, hasEntry("Key", "Value"));
76         }
77
78         @Test
79         public void gettingAnExistingBooleanReturnsTheCorrectValue() throws ConfigurationException {
80                 pluginStore.booleans.put("Key", true);
81                 boolean value = pluginStoreConfigurationBackend.getBooleanValue("Key");
82                 assertThat(value, is(true));
83         }
84
85         @Test(expected = ConfigurationException.class)
86         public void gettingANonExistingBooleanThrowsAnException() throws ConfigurationException {
87                 pluginStoreConfigurationBackend.getBooleanValue("Key");
88         }
89
90         @Test
91         public void storingABooleanValue() throws ConfigurationException {
92                 pluginStoreConfigurationBackend.setBooleanValue("Key", true);
93                 assertThat(pluginStore.booleans, hasEntry("Key", true));
94         }
95
96         @Test
97         public void gettingAnExistingAndCorrectlyFormattedStringAsDoubleReturnsTheCorrectValue() throws ConfigurationException {
98                 pluginStore.strings.put("Key", "1.234");
99                 double value = pluginStoreConfigurationBackend.getDoubleValue("Key");
100                 assertThat(value, is(1.234));
101         }
102
103         @Test(expected = ConfigurationException.class)
104         public void gettingANonExistingStringAsDoubleCausesAnError() throws ConfigurationException {
105                 pluginStoreConfigurationBackend.getDoubleValue("Key");
106         }
107
108         @Test(expected = ConfigurationException.class)
109         public void gettingAnExistingButNotCorrectlyFormattedStringAsDoubleValueCausesAnError() throws ConfigurationException {
110                 pluginStore.strings.put("Key", "foo");
111                 pluginStoreConfigurationBackend.getDoubleValue("Key");
112         }
113
114         @Test
115         public void gettingANullStringAsDoubleValueReturnsNull() throws ConfigurationException {
116                 pluginStore.strings.put("Key", null);
117                 Double value = pluginStoreConfigurationBackend.getDoubleValue("Key");
118                 assertThat(value, nullValue());
119         }
120
121         @Test
122         public void storingADoubleValue() throws ConfigurationException {
123                 pluginStoreConfigurationBackend.setDoubleValue("Key", 1.234);
124                 assertThat(pluginStore.strings, hasEntry("Key", "1.234"));
125         }
126
127         @Test
128         public void gettingAnIntegerValueReturnsTheCorrectValue() throws ConfigurationException {
129                 pluginStore.integers.put("Key", 17);
130                 int value = pluginStoreConfigurationBackend.getIntegerValue("Key");
131                 assertThat(value, is(17));
132         }
133
134         @Test(expected = ConfigurationException.class)
135         public void gettingANonExistingIntegerValueCausesAnError() throws ConfigurationException {
136                 pluginStoreConfigurationBackend.getIntegerValue("Key");
137         }
138
139         @Test
140         public void storingAnIntegerValue() throws ConfigurationException {
141                 pluginStoreConfigurationBackend.setIntegerValue("Key", 17);
142                 assertThat(pluginStore.integers, hasEntry("Key", 17));
143         }
144
145         @Test
146         public void gettingALongValueReturnsTheCorrectValue() throws ConfigurationException {
147                 pluginStore.longs.put("Key", 17L);
148                 long value = pluginStoreConfigurationBackend.getLongValue("Key");
149                 assertThat(value, is(17L));
150         }
151
152         @Test(expected = ConfigurationException.class)
153         public void gettingANonExistingLongValueCausesAnError() throws ConfigurationException {
154                 pluginStoreConfigurationBackend.getLongValue("Key");
155         }
156
157         @Test
158         public void storingALongValue() throws ConfigurationException {
159                 pluginStoreConfigurationBackend.setLongValue("Key", 17L);
160                 assertThat(pluginStore.longs, hasEntry("Key", 17L));
161         }
162
163         @Test
164         public void savingStoresThePluginStoreInTheRespirator() throws ConfigurationException, DatabaseDisabledException {
165                 pluginStoreConfigurationBackend.save();
166                 verify(pluginRespirator).putStore(eq(pluginStore));
167         }
168
169         @Test(expected = ConfigurationException.class)
170         public void savingWithADisabledDatabaseCausesAnError() throws DatabaseDisabledException, ConfigurationException {
171                 doThrow(DatabaseDisabledException.class).when(pluginRespirator).putStore(eq(pluginStore));
172                 pluginStoreConfigurationBackend.save();
173         }
174
175 }