aboutsummaryrefslogtreecommitdiff
path: root/juick-common/src/test/java/com/juick/MessageTest.java
blob: 6197f86120936390e23483b618bde747b4293f57 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Copyright (C) 2008-2017, Juick
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.juick;

import com.juick.util.MessageUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

/**
 * Created by aalexeev on 12/7/16.
 */
public class MessageTest {

    @Test
    public void equalsShouldBeReturnCorrectResult() {
        Message message1 = new Message();

        Message message2 = new Message();
        message2.setMid(RandomUtils.nextInt());

        Message message3 = message1;

        assertThat(message1, equalTo(message3));
        assertThat(message3, equalTo(message1));

        assertThat(message1, not(equalTo(message2)));
        assertThat(message2, not(equalTo(message1)));

        int id = RandomUtils.nextInt();
        message1.setMid(id);
        message2.setMid(id);

        id = RandomUtils.nextInt();
        message1.setRid(id);
        message2.setRid(id);

        assertThat(message1, equalTo(message2));
        assertThat(message2, equalTo(message1));

        message1.setMid(RandomUtils.nextInt());
        message1.setRid(RandomUtils.nextInt());

        message2.setMid(RandomUtils.nextInt());
        message2.setRid(RandomUtils.nextInt());

        assertThat(message1, not(equalTo(message2)));
        assertThat(message2, not(equalTo(message1)));
    }

    @Test
    public void compareShouldBeReturnCorrectResult() {
        Message message1 = new Message();

        message1.setMid(RandomUtils.nextInt());
        message1.setRid(RandomUtils.nextInt());

        Message message2 = message1;

        assertThat(message1.compareTo(message2), equalTo(0));
        assertThat(message2.compareTo(message1), equalTo(0));

        Message message3 = new Message();

        message3.setMid(message1.getMid());
        message3.setRid(message1.getRid());

        assertThat(message1.compareTo(message3), equalTo(0));
        assertThat(message3.compareTo(message1), equalTo(0));
    }

    @Test
    public void messageWithGreaterMidShouldBeLessThenMesageWithLessMid() {
        Message message1 = new Message();

        message1.setMid(RandomUtils.nextInt());
        message1.setRid(RandomUtils.nextInt());

        Message message2 = new Message();

        message2.setMid(message1.getMid() + RandomUtils.nextInt(1, 10000));
        message2.setRid(RandomUtils.nextInt());

        assertThat(message1.compareTo(message2), equalTo(1));
        assertThat(message2.compareTo(message1), equalTo(-1));
    }

    @Test
    public void messageWithGreaterRidAndEqualsMidShouldBeGreaterThenMessageWithLessRid() {
        Message message1 = new Message();

        message1.setMid(RandomUtils.nextInt());
        message1.setRid(RandomUtils.nextInt());

        Message message2 = new Message();

        message2.setMid(message1.getMid());
        message2.setRid(message1.getRid() +  + RandomUtils.nextInt(1, 10000));

        assertThat(message1.compareTo(message2), equalTo(-1));
        assertThat(message2.compareTo(message1), equalTo(1));
    }

    @Test
    public void tagsStringShouldBeEmptyIfNoTags() {
        Message message = new Message();

        assertThat(MessageUtils.getTagsString(message), isEmptyString());

        message.FriendsOnly = true;

        assertThat(MessageUtils.getTagsString(message), isEmptyString());

        message.Hidden = true;
        message.ReadOnly = true;

        assertThat(MessageUtils.getTagsString(message), isEmptyString());

        message.setTags(MessageUtils.parseTags(" "));

        assertThat(MessageUtils.getTagsString(message), isEmptyString());
    }

    @Test
    public void tagsStringShouldHasNoTagDuplicates() {
        Message message = new Message();

        message.setTags(MessageUtils.parseTags("test"));

        assertThat(StringUtils.countMatches(MessageUtils.getTagsString(message), "*test"), equalTo(1));

        message.setTags(MessageUtils.parseTags("test test"));

        assertThat(StringUtils.countMatches(MessageUtils.getTagsString(message), "*test"), equalTo(1));

        message.setTags(MessageUtils.parseTags("test test ab test"));

        assertThat(StringUtils.countMatches(MessageUtils.getTagsString(message), "*test"), equalTo(1));
        assertThat(StringUtils.countMatches(MessageUtils.getTagsString(message), "*ab"), equalTo(1));
    }
    @Test
    public void markdownContentShouldNotHaveUnescapedReplyNumbersBecauseOfTelegram() {
        Message msg = new Message();
        msg.setMid(1);
        msg.setText("See /303 again");
        assertThat(MessageUtils.formatMarkdownText(msg), is("See [/303](https://juick.com/m/1#303) again"));
    }
    @Test
    public void shouldNotThrowIfUrlContainsIllegalCharacters() {
        String msg = "[te](http://juick.com/)[st](http://juick.com/)";
        assertThat(MessageUtils.stripNonSafeUrls(msg), is(msg));
    }
    @Test
    public void mentionsCount() {
        Message msg = new Message();
        msg.setText("@ugnich go home");
        List<String> mentions = MessageUtils.getMentions(msg);
        assertThat(mentions.size(), is(1));
        assertThat(mentions.get(0), is("@ugnich"));
        msg.setText("And dick is @ugnich@jabber.zp.ua");
        assertThat(MessageUtils.getGlobalMentions(msg).size(), is(1));
    }
}