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 verifyThatAskingForAnExistingIntDoesNotCauseAnError() throws FcpException {
54 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Int", "15").get();
55 int value = getInt(fieldSet, "Int");
56 assertThat(value, is(15));
59 @Test(expected = FcpException.class)
60 public void verifyThatAskingForANonExistingIntDoesCauseAnError() throws FcpException {
61 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("OtherInt", "15").get();
62 getInt(fieldSet, "Int");
65 @Test(expected = FcpException.class)
66 public void verifyThatAskingForAnInvalidIntDoesCauseAnError() throws FcpException {
67 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Int", "foo").get();
68 getInt(fieldSet, "Int");
72 public void verifyThasAksingForAValidIntWithDefaultValueReturnsTheInt() {
73 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Int", 15).get();
74 int value = getInt(fieldSet, "Int", 30);
75 assertThat(value, is(15));
79 public void verifyThasAksingForANonExistingIntWithDefaultValueReturnsTheDefaultValue() {
80 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Int", 15).get();
81 int value = getInt(fieldSet, "OtherInt", 30);
82 assertThat(value, is(30));
86 public void verifyThasAksingForAnInvalidIntWithDefaultValueReturnsTheDefaultValue() {
87 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Int", "foo").get();
88 int value = getInt(fieldSet, "Int", 30);
89 assertThat(value, is(30));
93 public void verifyThatAskingForAValidBooleanDoesNotCauseAnError() throws FcpException {
94 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Boolean", "true").get();
95 boolean value = getBoolean(fieldSet, "Boolean");
96 assertThat(value, is(true));
99 @Test(expected = FcpException.class)
100 public void verifyThatAskingForAnInvalidBooleanCausesAnError() throws FcpException {
101 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Boolean", "foo").get();
102 getBoolean(fieldSet, "Boolean");
105 @Test(expected = FcpException.class)
106 public void verifyThatAskingForANonExistingBooleanCausesAnError() throws FcpException {
107 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Boolean", "true").get();
108 getBoolean(fieldSet, "OtherBoolean");
112 public void verifyThatAskingForAValidBooleanWithDefaultValueReturnsTheBoolean() {
113 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Boolean", "true").get();
114 boolean value = getBoolean(fieldSet, "Boolean", false);
115 assertThat(value, is(true));
119 public void verifyThatAskingForAnInvalidBooleanWithDefaultValueReturnsTheDefaultValue() {
120 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Boolean", "foo").get();
121 boolean value = getBoolean(fieldSet, "Boolean", true);
122 assertThat(value, is(true));
126 public void verifyThatAskingForANonExistingBooleanWithDefaultValueReturnsTheDefaultValue() {
127 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Boolean", "foo").get();
128 boolean value = getBoolean(fieldSet, "OtherBoolean", true);
129 assertThat(value, is(true));