2 * Sone - PluginStoreConfigurationBackendTest.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.freenet;
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;
30 import net.pterodactylus.util.config.ConfigurationException;
32 import freenet.client.async.DatabaseDisabledException;
33 import freenet.pluginmanager.PluginRespirator;
34 import freenet.pluginmanager.PluginStore;
36 import org.junit.Test;
39 * Unit test for {@link PluginStoreConfigurationBackend}.
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43 public class PluginStoreConfigurationBackendTest {
45 private final PluginRespirator pluginRespirator = mock(PluginRespirator.class);
46 private final PluginStore pluginStore = new PluginStore();
47 private final PluginStoreConfigurationBackend pluginStoreConfigurationBackend;
49 public PluginStoreConfigurationBackendTest() throws DatabaseDisabledException {
50 when(pluginRespirator.getStore()).thenReturn(pluginStore);
51 pluginStoreConfigurationBackend = new PluginStoreConfigurationBackend(pluginRespirator);
54 @Test(expected = DatabaseDisabledException.class)
55 public void notGettingAPluginStoreResultsInAnException() throws DatabaseDisabledException {
56 PluginRespirator pluginRespirator = mock(PluginRespirator.class);
57 new PluginStoreConfigurationBackend(pluginRespirator);
61 public void gettingAnExistingStringValueRetunsTheValue() throws ConfigurationException {
62 pluginStore.strings.put("Key", "Value");
63 String value = pluginStoreConfigurationBackend.getValue("Key");
64 assertThat(value, is("Value"));
67 @Test(expected = ConfigurationException.class)
68 public void gettingANonExistingStringValueThrowsAnException() throws ConfigurationException {
69 pluginStoreConfigurationBackend.getValue("Key");
73 public void storingAStringValue() throws ConfigurationException {
74 pluginStoreConfigurationBackend.putValue("Key", "Value");
75 assertThat(pluginStore.strings, hasEntry("Key", "Value"));
79 public void gettingAnExistingBooleanReturnsTheCorrectValue() throws ConfigurationException {
80 pluginStore.booleans.put("Key", true);
81 boolean value = pluginStoreConfigurationBackend.getBooleanValue("Key");
82 assertThat(value, is(true));
85 @Test(expected = ConfigurationException.class)
86 public void gettingANonExistingBooleanThrowsAnException() throws ConfigurationException {
87 pluginStoreConfigurationBackend.getBooleanValue("Key");
91 public void storingABooleanValue() throws ConfigurationException {
92 pluginStoreConfigurationBackend.setBooleanValue("Key", true);
93 assertThat(pluginStore.booleans, hasEntry("Key", true));
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));
103 @Test(expected = ConfigurationException.class)
104 public void gettingANonExistingStringAsDoubleCausesAnError() throws ConfigurationException {
105 pluginStoreConfigurationBackend.getDoubleValue("Key");
108 @Test(expected = ConfigurationException.class)
109 public void gettingAnExistingButNotCorrectlyFormattedStringAsDoubleValueCausesAnError() throws ConfigurationException {
110 pluginStore.strings.put("Key", "foo");
111 pluginStoreConfigurationBackend.getDoubleValue("Key");
115 public void gettingANullStringAsDoubleValueReturnsNull() throws ConfigurationException {
116 pluginStore.strings.put("Key", null);
117 Double value = pluginStoreConfigurationBackend.getDoubleValue("Key");
118 assertThat(value, nullValue());
122 public void storingADoubleValue() throws ConfigurationException {
123 pluginStoreConfigurationBackend.setDoubleValue("Key", 1.234);
124 assertThat(pluginStore.strings, hasEntry("Key", "1.234"));
128 public void gettingAnIntegerValueReturnsTheCorrectValue() throws ConfigurationException {
129 pluginStore.integers.put("Key", 17);
130 int value = pluginStoreConfigurationBackend.getIntegerValue("Key");
131 assertThat(value, is(17));
134 @Test(expected = ConfigurationException.class)
135 public void gettingANonExistingIntegerValueCausesAnError() throws ConfigurationException {
136 pluginStoreConfigurationBackend.getIntegerValue("Key");
140 public void storingAnIntegerValue() throws ConfigurationException {
141 pluginStoreConfigurationBackend.setIntegerValue("Key", 17);
142 assertThat(pluginStore.integers, hasEntry("Key", 17));
146 public void gettingALongValueReturnsTheCorrectValue() throws ConfigurationException {
147 pluginStore.longs.put("Key", 17L);
148 long value = pluginStoreConfigurationBackend.getLongValue("Key");
149 assertThat(value, is(17L));
152 @Test(expected = ConfigurationException.class)
153 public void gettingANonExistingLongValueCausesAnError() throws ConfigurationException {
154 pluginStoreConfigurationBackend.getLongValue("Key");
158 public void storingALongValue() throws ConfigurationException {
159 pluginStoreConfigurationBackend.setLongValue("Key", 17L);
160 assertThat(pluginStore.longs, hasEntry("Key", 17L));
164 public void savingStoresThePluginStoreInTheRespirator() throws ConfigurationException, DatabaseDisabledException {
165 pluginStoreConfigurationBackend.save();
166 verify(pluginRespirator).putStore(eq(pluginStore));
169 @Test(expected = ConfigurationException.class)
170 public void savingWithADisabledDatabaseCausesAnError() throws DatabaseDisabledException, ConfigurationException {
171 doThrow(DatabaseDisabledException.class).when(pluginRespirator).putStore(eq(pluginStore));
172 pluginStoreConfigurationBackend.save();