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 linkPartWithoutTitleGetsTextAsTitle() {
38 LinkPart linkPart = new LinkPart("http://li.nk/link.html", "link.html");
39 assertThat(linkPart.getLink(), is("http://li.nk/link.html"));
40 assertThat(linkPart.getText(), is("link.html"));
41 assertThat(linkPart.getTitle(), is("link.html"));
45 public void linkPartWithDifferentTitleAndTextIsCreatedCorrectly() {
46 LinkPart linkPart = new LinkPart("http://li.nk/link.html", "link.html", "the link");
47 assertThat(linkPart.getLink(), is("http://li.nk/link.html"));
48 assertThat(linkPart.getText(), is("link.html"));
49 assertThat(linkPart.getTitle(), is("the link"));
53 public void twoLinkPartsWithSameParametersAreEqual() {
54 LinkPart linkPart1 = new LinkPart("http://li.nk/link.html", "link.html", "the link");
55 LinkPart linkPart2 = new LinkPart("http://li.nk/link.html", "link.html", "the link");
56 assertThat(linkPart1, is(linkPart2));
57 assertThat(linkPart2, is(linkPart1));
58 assertThat(linkPart1, hasHashCode(linkPart2.hashCode()));
62 public void twoLinkPartsWithDifferentLinksAreNotEqual() {
63 LinkPart linkPart1 = new LinkPart("http://li.nk/link.html", "link.html", "the link");
64 LinkPart linkPart2 = new LinkPart("http://li.nk/link2.html", "link.html", "the link");
65 assertThat(linkPart1, not(is(linkPart2)));
66 assertThat(linkPart2, not(is(linkPart1)));
70 public void twoLinkPartsWithDifferentTextsAreNotEqual() {
71 LinkPart linkPart1 = new LinkPart("http://li.nk/link.html", "link.html", "the link");
72 LinkPart linkPart2 = new LinkPart("http://li.nk/link.html", "link2.html", "the link");
73 assertThat(linkPart1, not(is(linkPart2)));
74 assertThat(linkPart2, not(is(linkPart1)));
78 public void twoLinkPartsWithDifferentTitlesAreNotEqual() {
79 LinkPart linkPart1 = new LinkPart("http://li.nk/link.html", "link.html", "the link");
80 LinkPart linkPart2 = new LinkPart("http://li.nk/link.html", "link.html", "the link2");
81 assertThat(linkPart1, not(is(linkPart2)));
82 assertThat(linkPart2, not(is(linkPart1)));
86 public void linkPartDoesNotEqualAnObject() {
87 LinkPart linkPart1 = new LinkPart("http://li.nk/link.html", "link.html", "the link");
88 assertThat(linkPart1, not(is(new Object())));
89 assertThat(new Object(), not(is((Object) linkPart1)));
92 public class HasHashCode<T> extends BaseMatcher<T> {
94 private final int hashCode;
96 public HasHashCode(int hashCode) {
97 this.hashCode = hashCode;
101 public boolean matches(Object item) {
102 return (item != null) && (item.hashCode() == hashCode);
106 public void describeTo(Description description) {
107 description.appendText("has hash ").appendValue(hashCode);
112 public <T> Matcher<T> hasHashCode(int hashCode) {
113 return new HasHashCode<T>(hashCode);