93eebb8a35e9f80286f6898717c0cde667608ad7
[Sone.git] / src / test / java / net / pterodactylus / sone / freenet / wot / TrustTest.java
1 /*
2  * Sone - TrustTest.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.wot;
19
20 import static java.util.regex.Pattern.compile;
21 import static org.hamcrest.MatcherAssert.assertThat;
22 import static org.hamcrest.Matchers.is;
23 import static org.hamcrest.Matchers.not;
24 import static org.hamcrest.Matchers.nullValue;
25
26 import org.hamcrest.Description;
27 import org.hamcrest.Matcher;
28 import org.hamcrest.TypeSafeMatcher;
29 import org.junit.Test;
30
31 /**
32  * Unit test for {@link Trust}.
33  *
34  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35  */
36 public class TrustTest {
37
38         @Test
39         public void trustCanBeCreated() {
40                 Trust trust = new Trust(5, 17, 2);
41                 assertThat(trust.getExplicit(), is(5));
42                 assertThat(trust.getImplicit(), is(17));
43                 assertThat(trust.getDistance(), is(2));
44         }
45
46         @Test
47         public void nullTrustCanBeCreated() {
48                 Trust trust = new Trust(null, null, null);
49                 assertThat(trust.getExplicit(), nullValue());
50                 assertThat(trust.getImplicit(), nullValue());
51                 assertThat(trust.getDistance(), nullValue());
52         }
53
54         @Test
55         public void equalTrustsHaveTheSameHashCode() {
56                 Trust trust1 = new Trust(5, 17, 2);
57                 Trust trust2 = new Trust(5, 17, 2);
58                 assertThat(trust1, is(trust2));
59                 assertThat(trust1.hashCode(), is(trust2.hashCode()));
60         }
61
62         @Test
63         public void nullDoesNotMatchTrust() {
64                 Trust trust = new Trust(5, 17, 2);
65                 assertThat(trust, not(is((Object) null)));
66         }
67
68         @Test
69         public void toStringContainsTheThreeValues() {
70                 String trustString = new Trust(5, 17, 2).toString();
71                 assertThat(trustString, matches("\\b5\\b"));
72                 assertThat(trustString, matches("\\b17\\b"));
73                 assertThat(trustString, matches("\\b2\\b"));
74         }
75
76         private static Matcher<String> matches(final String regex) {
77                 return new TypeSafeMatcher<String>() {
78                         @Override
79                         protected boolean matchesSafely(String item) {
80                                 return compile(regex).matcher(item).find();
81                         }
82
83                         @Override
84                         public void describeTo(Description description) {
85                                 description.appendText("matches: ").appendValue(regex);
86                         }
87                 };
88         }
89
90 }