2 * Sone - LinkPartTest.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.text;
20 import static org.hamcrest.MatcherAssert.assertThat;
21 import static org.hamcrest.Matchers.is;
22 import static org.hamcrest.Matchers.not;
24 import org.hamcrest.BaseMatcher;
25 import org.hamcrest.Description;
26 import org.hamcrest.Matcher;
27 import org.junit.Test;
30 * Unit test for {@link LinkPart}.
32 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34 public class LinkPartTest {
37 public void linkPartIsNotAPlainTextPart() {
38 LinkPart linkPart = new LinkPart("http://li.nk/link.html", "link.html");
39 assertThat(linkPart.isPlainText(), is(false));
43 public void linkPartIsNotAFreenetLink() {
44 LinkPart linkPart = new LinkPart("http://li.nk/link.html", "link.html");
45 assertThat(linkPart.isFreenetLink(), is(false));
49 public void linkPartWithoutTitleGetsTextAsTitle() {
50 LinkPart linkPart = new LinkPart("http://li.nk/link.html", "link.html");
51 assertThat(linkPart.getLink(), is("http://li.nk/link.html"));
52 assertThat(linkPart.getText(), is("link.html"));
53 assertThat(linkPart.getTitle(), is("link.html"));
57 public void linkPartWithDifferentTitleAndTextIsCreatedCorrectly() {
58 LinkPart linkPart = new LinkPart("http://li.nk/link.html", "link.html", "the link");
59 assertThat(linkPart.getLink(), is("http://li.nk/link.html"));
60 assertThat(linkPart.getText(), is("link.html"));
61 assertThat(linkPart.getTitle(), is("the link"));
65 public void twoLinkPartsWithSameParametersAreEqual() {
66 LinkPart linkPart1 = new LinkPart("http://li.nk/link.html", "link.html", "the link");
67 LinkPart linkPart2 = new LinkPart("http://li.nk/link.html", "link.html", "the link");
68 assertThat(linkPart1, is(linkPart2));
69 assertThat(linkPart2, is(linkPart1));
70 assertThat(linkPart1, hasHashCode(linkPart2.hashCode()));
74 public void twoLinkPartsWithDifferentLinksAreNotEqual() {
75 LinkPart linkPart1 = new LinkPart("http://li.nk/link.html", "link.html", "the link");
76 LinkPart linkPart2 = new LinkPart("http://li.nk/link2.html", "link.html", "the link");
77 assertThat(linkPart1, not(is(linkPart2)));
78 assertThat(linkPart2, not(is(linkPart1)));
82 public void twoLinkPartsWithDifferentTextsAreNotEqual() {
83 LinkPart linkPart1 = new LinkPart("http://li.nk/link.html", "link.html", "the link");
84 LinkPart linkPart2 = new LinkPart("http://li.nk/link.html", "link2.html", "the link");
85 assertThat(linkPart1, not(is(linkPart2)));
86 assertThat(linkPart2, not(is(linkPart1)));
90 public void twoLinkPartsWithDifferentTitlesAreNotEqual() {
91 LinkPart linkPart1 = new LinkPart("http://li.nk/link.html", "link.html", "the link");
92 LinkPart linkPart2 = new LinkPart("http://li.nk/link.html", "link.html", "the link2");
93 assertThat(linkPart1, not(is(linkPart2)));
94 assertThat(linkPart2, not(is(linkPart1)));
98 public void linkPartDoesNotEqualAnObject() {
99 LinkPart linkPart1 = new LinkPart("http://li.nk/link.html", "link.html", "the link");
100 assertThat(linkPart1, not(is(new Object())));
101 assertThat(new Object(), not(is((Object) linkPart1)));
104 public class HasHashCode<T> extends BaseMatcher<T> {
106 private final int hashCode;
108 public HasHashCode(int hashCode) {
109 this.hashCode = hashCode;
113 public boolean matches(Object item) {
114 return (item != null) && (item.hashCode() == hashCode);
118 public void describeTo(Description description) {
119 description.appendText("has hash ").appendValue(hashCode);
124 public <T> Matcher<T> hasHashCode(int hashCode) {
125 return new HasHashCode<T>(hashCode);