2 * Sone - FieldTest.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.data;
20 import static org.hamcrest.MatcherAssert.assertThat;
21 import static org.hamcrest.Matchers.contains;
22 import static org.hamcrest.Matchers.is;
24 import java.util.List;
26 import net.pterodactylus.sone.data.Profile.Field;
28 import com.google.common.base.Optional;
29 import org.junit.Test;
32 * Unit test for {@link Profile}.
34 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36 public class ProfileTest {
38 final Profile profile = new Profile((Sone) null);
41 public void testAddingAField() {
42 profile.addField("TestField");
43 Optional<Field> testField = profile.getFieldByName("TestField");
44 assertThat(testField.isPresent(), is(true));
45 assertThat(testField.get().getName(), is("TestField"));
49 public void testGettingAFieldByName() {
50 profile.addField("TestField");
51 Optional<Field> testField = profile.getFieldByName("TestField");
52 assertThat(testField.isPresent(), is(true));
56 public void testGettingANonExistingFieldByName() {
57 profile.addField("TestField");
58 Optional<Field> testField = profile.getFieldByName("TestField2");
59 assertThat(testField.isPresent(), is(false));
63 public void testGettingAFieldById() {
64 profile.addField("TestField");
65 Optional<Field> testField = profile.getFieldByName("TestField");
66 testField = profile.getFieldById(testField.get().getId());
67 assertThat(testField.isPresent(), is(true));
71 public void testGettingANonExistingFieldById() {
72 Optional<Field> testField = profile.getFieldById("does not exist");
73 assertThat(testField.isPresent(), is(false));
77 public void testRenamingAField() {
78 profile.addField("TestField");
79 Optional<Field> testField = profile.getFieldByName("TestField");
80 profile.renameField(testField.get(), "RenamedField");
81 Optional<Field> renamedField = profile.getFieldByName("RenamedField");
82 assertThat(testField.get().getId(), is(renamedField.get().getId()));
86 public void testRenamingANonExistingField() {
87 Field testField = profile.addField("TestField");
88 profile.removeField(testField);
89 profile.renameField(testField, "TestField2");
90 Optional<Field> testField2 = profile.getFieldByName("TestField2");
91 assertThat(testField2.isPresent(), is(false));
95 public void testChangingTheValueOfAField() {
96 profile.addField("TestField");
97 Optional<Field> testField = profile.getFieldByName("TestField");
98 profile.setField(testField.get(), "Test");
99 testField = profile.getFieldByName("TestField");
100 assertThat(testField.get().getValue(), is("Test"));
104 public void testChangingTheValueOfANonExistingField() {
105 Field testField = profile.addField("TestField");
106 profile.removeField(testField);
107 profile.setField(testField, "Test");
108 Optional<Field> testField2 = profile.getFieldByName("TestField");
109 assertThat(testField2.isPresent(), is(false));
113 public void testDeletingAField() {
114 profile.addField("TestField");
115 Optional<Field> testField = profile.getFieldByName("TestField");
116 profile.removeField(testField.get());
117 testField = profile.getFieldByName("TestField");
118 assertThat(testField.isPresent(), is(false));
122 public void testGettingFieldList() {
123 Field firstField = profile.addField("First");
124 Field secondField = profile.addField("Second");
125 List<Field> fields = profile.getFields();
126 assertThat(fields, contains(firstField, secondField));
130 public void testMovingAFieldUp() {
131 Field firstField = profile.addField("First");
132 Field secondField = profile.addField("Second");
133 profile.moveFieldUp(secondField);
134 List<Field> fields = profile.getFields();
135 assertThat(fields, contains(secondField, firstField));
139 public void testMovingTheFirstFieldUp() {
140 Field firstField = profile.addField("First");
141 Field secondField = profile.addField("Second");
142 profile.moveFieldUp(firstField);
143 List<Field> fields = profile.getFields();
144 assertThat(fields, contains(firstField, secondField));
148 public void testMovingAFieldDown() {
149 Field firstField = profile.addField("First");
150 Field secondField = profile.addField("Second");
151 profile.moveFieldDown(firstField);
152 List<Field> fields = profile.getFields();
153 assertThat(fields, contains(secondField, firstField));
157 public void testMovingTheLastFieldDown() {
158 Field firstField = profile.addField("First");
159 Field secondField = profile.addField("Second");
160 profile.moveFieldDown(secondField);
161 List<Field> fields = profile.getFields();
162 assertThat(fields, contains(firstField, secondField));
166 public void testModifyingAProfile() {
167 profile.modify().setFirstName("First").setMiddleName("M.").setLastName("Last").setBirthYear(2013).setBirthMonth(10).setBirthDay(24).update();
168 assertThat(profile.getFirstName(), is("First"));
169 assertThat(profile.getMiddleName(), is("M."));
170 assertThat(profile.getLastName(), is("Last"));
171 assertThat(profile.getBirthYear(), is(2013));
172 assertThat(profile.getBirthMonth(), is(10));
173 assertThat(profile.getBirthDay(), is(24));