65c34611b058747f733c4ea2049d1dbfe9a02e84
[Sone.git] / src / test / java / net / pterodactylus / sone / text / LinkPartTest.java
1 /*
2  * Sone - LinkPartTest.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.text;
19
20 import static org.hamcrest.MatcherAssert.assertThat;
21 import static org.hamcrest.Matchers.is;
22 import static org.hamcrest.Matchers.not;
23
24 import org.hamcrest.BaseMatcher;
25 import org.hamcrest.Description;
26 import org.hamcrest.Matcher;
27 import org.junit.Test;
28
29 /**
30  * Unit test for {@link LinkPart}.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public class LinkPartTest {
35
36         @Test
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"));
42         }
43
44         @Test
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"));
50         }
51
52         @Test
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()));
59         }
60
61         @Test
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)));
67         }
68
69         @Test
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)));
75         }
76
77         @Test
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)));
83         }
84
85         @Test
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)));
90         }
91
92         public class HasHashCode<T> extends BaseMatcher<T> {
93
94                 private final int hashCode;
95
96                 public HasHashCode(int hashCode) {
97                         this.hashCode = hashCode;
98                 }
99
100                 @Override
101                 public boolean matches(Object item) {
102                         return (item != null) && (item.hashCode() == hashCode);
103                 }
104
105                 @Override
106                 public void describeTo(Description description) {
107                         description.appendText("has hash ").appendValue(hashCode);
108                 }
109
110         }
111
112         public <T> Matcher<T> hasHashCode(int hashCode) {
113                 return new HasHashCode<T>(hashCode);
114         }
115
116 }