2 * Sone - TrustTest.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.wot;
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;
26 import org.hamcrest.Description;
27 import org.hamcrest.Matcher;
28 import org.hamcrest.TypeSafeMatcher;
29 import org.junit.Test;
32 * Unit test for {@link Trust}.
34 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36 public class TrustTest {
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));
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());
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()));
63 public void nullDoesNotMatchTrust() {
64 Trust trust = new Trust(5, 17, 2);
65 assertThat(trust, not(is((Object) null)));
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"));
76 private static Matcher<String> matches(final String regex) {
77 return new TypeSafeMatcher<String>() {
79 protected boolean matchesSafely(String item) {
80 return compile(regex).matcher(item).find();
84 public void describeTo(Description description) {
85 description.appendText("matches: ").appendValue(regex);