Add unit test for SimpleFieldSetBuilder.
[Sone.git] / src / test / java / net / pterodactylus / sone / freenet / SimpleFieldSetBuilderTest.java
1 /*
2  * Sone - SimpleFieldSetBuilderTest.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 net.pterodactylus.sone.Matchers.contains;
21 import static net.pterodactylus.sone.Matchers.containsInAnyOrder;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.is;
24
25 import freenet.node.FSParseException;
26 import freenet.support.SimpleFieldSet;
27
28 import org.junit.Test;
29
30 /**
31  * Unit test for {@link SimpleFieldSetBuilder}.
32  *
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public class SimpleFieldSetBuilderTest {
36
37         private final SimpleFieldSetBuilder simpleFieldSetBuilder = new SimpleFieldSetBuilder();
38
39         @Test
40         public void creatingASimpleFieldSetWithAStringValue() {
41                 SimpleFieldSet fieldSet = simpleFieldSetBuilder.put("Key", "Value").get();
42                 assertThat(fieldSet.keyIterator(), contains("Key"));
43                 assertThat(fieldSet.get("Key"), is("Value"));
44         }
45
46         @Test
47         public void creatingASimpleFieldSetWithAnIntValue() throws FSParseException {
48                 SimpleFieldSet fieldSet = simpleFieldSetBuilder.put("Key", 17).get();
49                 assertThat(fieldSet.keyIterator(), contains("Key"));
50                 assertThat(fieldSet.getInt("Key"), is(17));
51         }
52
53         @Test
54         public void creatingASimpleFieldSetWithALongValue() throws FSParseException {
55                 SimpleFieldSet fieldSet = simpleFieldSetBuilder.put("Key", 17L).get();
56                 assertThat(fieldSet.keyIterator(), contains("Key"));
57                 assertThat(fieldSet.getLong("Key"), is(17L));
58         }
59
60         @Test
61         public void creatingASimpleFieldSetWithAnotherFieldSet() throws FSParseException {
62                 SimpleFieldSet originalFieldSet = new SimpleFieldSet(true);
63                 originalFieldSet.putSingle("StringKey", "Value");
64                 originalFieldSet.put("IntKey", 17);
65                 originalFieldSet.put("LongKey", 17L);
66                 SimpleFieldSet fieldSet = simpleFieldSetBuilder.put(originalFieldSet).get();
67                 assertThat(fieldSet.keyIterator(), containsInAnyOrder("IntKey", "StringKey", "LongKey"));
68                 assertThat(fieldSet.get("StringKey"), is("Value"));
69                 assertThat(fieldSet.getInt("IntKey"), is(17));
70                 assertThat(fieldSet.getLong("LongKey"), is(17L));
71         }
72
73 }