2 * Sone - AbstractCommandTest.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.fcp;
20 import static net.pterodactylus.sone.freenet.fcp.AbstractCommand.getBoolean;
21 import static net.pterodactylus.sone.freenet.fcp.AbstractCommand.getInt;
22 import static net.pterodactylus.sone.freenet.fcp.AbstractCommand.getString;
23 import static org.hamcrest.MatcherAssert.assertThat;
24 import static org.hamcrest.Matchers.is;
26 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
28 import freenet.support.SimpleFieldSet;
30 import org.junit.Test;
33 * Unit test for {@link AbstractCommand}.
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 public class AbstractCommandTest {
39 @Test(expected = FcpException.class)
40 public void verifyThatAskingForANonExistingKeyCausesAnError() throws FcpException {
41 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().get();
42 getString(fieldSet, "NonExistingKey");
46 public void verifyThatAskingForAnExistingKeyDoesNotCauseAnError() throws FcpException {
47 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Key", "Value").get();
48 String value = getString(fieldSet, "Key");
49 assertThat(value, is("Value"));
53 public void verifyThatAskingForAnExistingKeyWithDefaultReturnsTheKey() {
54 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Key", "Value").get();
55 String value = getString(fieldSet, "Key", "DefaultValue");
56 assertThat(value, is("Value"));
60 public void verifyThatAskingForANonExistingKeyWithDefaultReturnsTheDefaultKey() {
61 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().get();
62 String value = getString(fieldSet, "Key", "DefaultValue");
63 assertThat(value, is("DefaultValue"));
67 public void verifyThatAskingForAnExistingIntDoesNotCauseAnError() throws FcpException {
68 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Int", "15").get();
69 int value = getInt(fieldSet, "Int");
70 assertThat(value, is(15));
73 @Test(expected = FcpException.class)
74 public void verifyThatAskingForANonExistingIntDoesCauseAnError() throws FcpException {
75 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("OtherInt", "15").get();
76 getInt(fieldSet, "Int");
79 @Test(expected = FcpException.class)
80 public void verifyThatAskingForAnInvalidIntDoesCauseAnError() throws FcpException {
81 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Int", "foo").get();
82 getInt(fieldSet, "Int");
86 public void verifyThasAksingForAValidIntWithDefaultValueReturnsTheInt() {
87 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Int", 15).get();
88 int value = getInt(fieldSet, "Int", 30);
89 assertThat(value, is(15));
93 public void verifyThasAksingForANonExistingIntWithDefaultValueReturnsTheDefaultValue() {
94 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Int", 15).get();
95 int value = getInt(fieldSet, "OtherInt", 30);
96 assertThat(value, is(30));
100 public void verifyThasAksingForAnInvalidIntWithDefaultValueReturnsTheDefaultValue() {
101 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Int", "foo").get();
102 int value = getInt(fieldSet, "Int", 30);
103 assertThat(value, is(30));
107 public void verifyThatAskingForAValidBooleanDoesNotCauseAnError() throws FcpException {
108 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Boolean", "true").get();
109 boolean value = getBoolean(fieldSet, "Boolean");
110 assertThat(value, is(true));
113 @Test(expected = FcpException.class)
114 public void verifyThatAskingForAnInvalidBooleanCausesAnError() throws FcpException {
115 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Boolean", "foo").get();
116 getBoolean(fieldSet, "Boolean");
119 @Test(expected = FcpException.class)
120 public void verifyThatAskingForANonExistingBooleanCausesAnError() throws FcpException {
121 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Boolean", "true").get();
122 getBoolean(fieldSet, "OtherBoolean");
126 public void verifyThatAskingForAValidBooleanWithDefaultValueReturnsTheBoolean() {
127 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Boolean", "true").get();
128 boolean value = getBoolean(fieldSet, "Boolean", false);
129 assertThat(value, is(true));
133 public void verifyThatAskingForAnInvalidBooleanWithDefaultValueReturnsTheDefaultValue() {
134 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Boolean", "foo").get();
135 boolean value = getBoolean(fieldSet, "Boolean", true);
136 assertThat(value, is(true));
140 public void verifyThatAskingForANonExistingBooleanWithDefaultValueReturnsTheDefaultValue() {
141 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Boolean", "foo").get();
142 boolean value = getBoolean(fieldSet, "OtherBoolean", true);
143 assertThat(value, is(true));