Add unit test for abstract command.
[Sone.git] / src / test / java / net / pterodactylus / sone / freenet / fcp / AbstractCommandTest.java
1 /*
2  * Sone - AbstractCommandTest.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.fcp;
19
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;
25
26 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
27
28 import freenet.support.SimpleFieldSet;
29
30 import org.junit.Test;
31
32 /**
33  * Unit test for {@link AbstractCommand}.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class AbstractCommandTest {
38
39         @Test(expected = FcpException.class)
40         public void verifyThatAskingForANonExistingKeyCausesAnError() throws FcpException {
41                 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().get();
42                 getString(fieldSet, "NonExistingKey");
43         }
44
45         @Test
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"));
50         }
51
52         @Test
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));
57         }
58
59         @Test(expected = FcpException.class)
60         public void verifyThatAskingForANonExistingIntDoesCauseAnError() throws FcpException {
61                 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("OtherInt", "15").get();
62                 getInt(fieldSet, "Int");
63         }
64
65         @Test(expected = FcpException.class)
66         public void verifyThatAskingForAnInvalidIntDoesCauseAnError() throws FcpException {
67                 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Int", "foo").get();
68                 getInt(fieldSet, "Int");
69         }
70
71         @Test
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));
76         }
77
78         @Test
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));
83         }
84
85         @Test
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));
90         }
91
92         @Test
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));
97         }
98
99         @Test(expected = FcpException.class)
100         public void verifyThatAskingForAnInvalidBooleanCausesAnError() throws FcpException {
101                 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Boolean", "foo").get();
102                 getBoolean(fieldSet, "Boolean");
103         }
104
105         @Test(expected = FcpException.class)
106         public void verifyThatAskingForANonExistingBooleanCausesAnError() throws FcpException {
107                 SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Boolean", "true").get();
108                 getBoolean(fieldSet, "OtherBoolean");
109         }
110
111         @Test
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));
116         }
117
118         @Test
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));
123         }
124
125         @Test
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));
130         }
131
132 }