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 package name.angoca.zemucan.core.lexical.model;
30
31 import name.angoca.zemucan.core.lexical.impl.InvalidTokenException;
32 import name.angoca.zemucan.tools.test.RandomTestRunner;
33
34 import org.junit.Assert;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 @RunWith(RandomTestRunner.class)
59 public final class TokenTest {
60
61
62
63 private static final String CREATE = "create";
64
65
66
67
68 public TokenTest() {
69
70 }
71
72
73
74
75
76
77
78 @Test
79 public void testCongruent() throws InvalidTokenException {
80 final String text = "test";
81
82 final Token actual = new Token(text, true);
83 final int val1 = actual.hashCode();
84 final int val2 = actual.hashCode();
85
86 final boolean condition = val1 == val2;
87
88 Assert.assertTrue("Two consecutive calls return the same value",
89 condition);
90 }
91
92
93
94
95
96
97
98 @Test
99 public void testCreation() throws InvalidTokenException {
100 final String token = "";
101 try {
102 new Token(token, true);
103 } catch (final Exception exception) {
104 Assert.assertTrue("InvalidTokenException",
105 exception instanceof InvalidTokenException);
106 }
107 }
108
109
110
111
112
113
114
115 @Test
116 public void testEqualsDifferentObject() throws InvalidTokenException {
117 final String token1 = TokenTest.CREATE;
118 final String token2 = "table";
119
120 final Token expected = new Token(token1, true);
121 final Token actual = new Token(token2, true);
122
123 Assert.assertFalse("Equals between two different objects", actual
124 .equals(expected));
125 }
126
127
128
129
130
131
132
133 @Test
134 public void testEqualsIdenticalObject() throws InvalidTokenException {
135 final String token = TokenTest.CREATE;
136
137 final Token expected = new Token(token, true);
138 final Token actual = new Token(token, true);
139
140 Assert.assertTrue("Equals between two identical objects.", actual
141 .equals(expected));
142 }
143
144
145
146
147
148
149
150 @Test
151 public void testEqualsNull() throws InvalidTokenException {
152 final String token = TokenTest.CREATE;
153
154 final Token expected = new Token(token, true);
155
156 final Token nullObject = null;
157
158 final boolean condition = expected.equals(nullObject);
159
160 Assert.assertFalse("Equals with a null value", condition);
161 }
162
163
164
165
166
167
168
169 @Test
170 public void testHashCode() throws InvalidTokenException {
171 final String token = TokenTest.CREATE;
172
173 final Token object = new Token(token, true);
174
175 boolean condition = object.hashCode() != 0;
176
177 Assert.assertTrue("hashcode different to 0",
178 condition);
179 condition = object.hashCode() != 1;
180 Assert.assertTrue("hashcode different to 1",
181 condition);
182 condition = object.hashCode() != -1;
183 Assert.assertTrue("hashcode different to -1",
184 condition);
185 }
186
187
188
189
190
191
192
193 @Test
194 public void testHashCodeDifferentObject() throws InvalidTokenException {
195 final String token1 = TokenTest.CREATE;
196 final String token2 = "table";
197
198 final Token expected = new Token(token1, true);
199 final Token actual = new Token(token2, true);
200
201 final boolean condition = actual.hashCode() != expected.hashCode();
202
203 Assert.assertTrue("Two different objects with different hashcodes",
204 condition);
205 }
206
207
208
209
210
211
212
213
214 @Test
215 public void testHashCodeIdenticalObject() throws InvalidTokenException {
216 final String token = TokenTest.CREATE;
217
218 final Token expected = new Token(token, true);
219 final Token actual = new Token(token, true);
220
221 Assert.assertEquals("Two identical objects with the same hashcode",
222 actual.hashCode(), expected.hashCode());
223 }
224
225
226
227
228
229
230
231 @Test
232 public void testSimpleMethods() throws InvalidTokenException {
233 final String text = "test";
234
235 final Token token = new Token(text, true);
236
237 String actual = token.getToken();
238
239 String expected = text;
240
241 Assert.assertEquals("getToken", expected, actual);
242
243 actual = token.toString();
244
245 expected = "T[" + text + ']';
246 }
247
248
249
250
251
252
253
254 @Test
255 public void testSymmetric() throws InvalidTokenException {
256 final String text = "test";
257
258 final Token actual = new Token(text, true);
259
260 final boolean value = actual.equals(actual);
261
262 Assert.assertTrue("Symmetry", value);
263 }
264
265
266
267
268
269
270
271 @Test(expected = AssertionError.class)
272 public void testTokenNullParam() throws InvalidTokenException {
273 new Token(null, true);
274 }
275 }