Add tests for isFreenetLink() and isPlainText().
[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 linkPartIsNotAPlainTextPart() {
38                 LinkPart linkPart = new LinkPart("http://li.nk/link.html", "link.html");
39                 assertThat(linkPart.isPlainText(), is(false));
40         }
41
42         @Test
43         public void linkPartIsNotAFreenetLink() {
44                 LinkPart linkPart = new LinkPart("http://li.nk/link.html", "link.html");
45                 assertThat(linkPart.isFreenetLink(), is(false));
46         }
47
48         @Test
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"));
54         }
55
56         @Test
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"));
62         }
63
64         @Test
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()));
71         }
72
73         @Test
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)));
79         }
80
81         @Test
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)));
87         }
88
89         @Test
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)));
95         }
96
97         @Test
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)));
102         }
103
104         public class HasHashCode<T> extends BaseMatcher<T> {
105
106                 private final int hashCode;
107
108                 public HasHashCode(int hashCode) {
109                         this.hashCode = hashCode;
110                 }
111
112                 @Override
113                 public boolean matches(Object item) {
114                         return (item != null) && (item.hashCode() == hashCode);
115                 }
116
117                 @Override
118                 public void describeTo(Description description) {
119                         description.appendText("has hash ").appendValue(hashCode);
120                 }
121
122         }
123
124         public <T> Matcher<T> hasHashCode(int hashCode) {
125                 return new HasHashCode<T>(hashCode);
126         }
127
128 }