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.syntactic.model;
30
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import name.angoca.zemucan.AbstractZemucanException;
35 import name.angoca.zemucan.core.lexical.impl.InvalidTokenException;
36 import name.angoca.zemucan.core.lexical.model.Token;
37 import name.angoca.zemucan.tools.test.RandomTestRunner;
38
39 import org.junit.Assert;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 @RunWith(RandomTestRunner.class)
64 public final class GraphAnswerTest {
65
66
67
68
69 private static final String CREATE = "create";
70
71
72
73 private static final String CREATE_TABLE = "create table";
74
75
76
77 private static final String CREATE_TABLESPACE = "create talespace";
78
79
80
81 private static final String TABLE = "table";
82
83
84
85 private static final String TABLENAME = "<tableName>";
86
87
88
89 private static final String TABLESPACE = "tablespace";
90
91
92
93 private static final String TABLESPACENAME = "<tablespaceName>";
94
95
96
97 private static final String TEST1 = "test1";
98
99
100
101 private static final String TEST2 = "test2";
102
103
104
105
106 public GraphAnswerTest() {
107
108 }
109
110
111
112
113
114
115
116 @Test
117 public void testCongruent() throws InvalidTokenException {
118 final String tokenString1 = GraphAnswerTest.TEST1;
119 final String tokenString2 = GraphAnswerTest.TEST2;
120
121 final List<Token> phrases = new ArrayList<Token>();
122 phrases.add(new Token(tokenString1, true));
123 final List<Token> options = new ArrayList<Token>();
124 options.add(new Token(tokenString2, true));
125 final GraphAnswer actual = new GraphAnswer(phrases, options);
126 final int val1 = actual.hashCode();
127 final int val2 = actual.hashCode();
128 final boolean val = val1 == val2;
129
130 Assert.assertTrue("Two consecutive calls return the same value", val);
131 }
132
133
134
135
136
137
138
139 @Test
140 public void testEqualsDifferentObject() throws InvalidTokenException {
141 final String tokenString1 = GraphAnswerTest.TEST1;
142 final String tokenString2 = GraphAnswerTest.TEST2;
143 final String tokenString3 = "test3";
144 final String tokenString4 = "test4";
145 final String tokenString5 = "test5";
146
147 final List<Token> phrases1 = new ArrayList<Token>();
148 phrases1.add(new Token(tokenString2, true));
149 phrases1.add(new Token(tokenString1, true));
150 final List<Token> phrases2 = new ArrayList<Token>();
151 phrases2.add(new Token(tokenString3, true));
152 phrases2.add(new Token(tokenString1, true));
153 final List<Token> options = new ArrayList<Token>();
154 options.add(new Token(tokenString4, true));
155 options.add(new Token(tokenString5, true));
156 final GraphAnswer graph1 = new GraphAnswer(phrases1, options);
157 final GraphAnswer graph2 = new GraphAnswer(phrases2, options);
158
159 final boolean value = graph1.equals(graph2);
160
161 Assert.assertFalse("Different object", value);
162 }
163
164
165
166
167
168
169
170 @Test
171 public void testEqualsNull() throws InvalidTokenException {
172 final String phrase1 = GraphAnswerTest.CREATE;
173 final String option1 = GraphAnswerTest.TABLE;
174 final String option2 = GraphAnswerTest.TABLESPACE;
175
176 final List<Token> phrases = new ArrayList<Token>();
177 phrases.add(new Token(phrase1, true));
178 final List<Token> options = new ArrayList<Token>();
179 options.add(new Token(option1, true));
180 options.add(new Token(option2, true));
181 final GraphAnswer object = new GraphAnswer(phrases, options);
182
183 final GraphAnswer nullObject = null;
184
185 final boolean condition = object.equals(nullObject);
186
187 Assert.assertFalse("Equals different to null", condition);
188 }
189
190
191
192
193
194
195
196 @Test
197 public void testEqualsObjectFalse() throws InvalidTokenException {
198 final String phrase1 = GraphAnswerTest.CREATE_TABLE;
199 final String option11 = GraphAnswerTest.TABLE;
200 final String option12 = GraphAnswerTest.TABLESPACE;
201
202 final String phrase2 = GraphAnswerTest.CREATE_TABLESPACE;
203 final String option21 = GraphAnswerTest.TABLENAME;
204 final String option22 = GraphAnswerTest.TABLESPACENAME;
205
206 final List<Token> phrases1 = new ArrayList<Token>();
207 phrases1.add(new Token(phrase1, true));
208 final List<Token> options1 = new ArrayList<Token>();
209 options1.add(new Token(option11, true));
210 options1.add(new Token(option12, true));
211 final GraphAnswer expected = new GraphAnswer(phrases1, options1);
212
213 final List<Token> phrases2 = new ArrayList<Token>();
214 phrases2.add(new Token(phrase2, true));
215 final List<Token> options2 = new ArrayList<Token>();
216 options2.add(new Token(option21, true));
217 options2.add(new Token(option22, true));
218 final GraphAnswer actual = new GraphAnswer(phrases2, options2);
219
220 final boolean condition = expected.equals(actual);
221
222 Assert.assertFalse("Two different objects are not equals",
223 condition);
224 }
225
226
227
228
229
230
231
232 @Test
233 public void testEqualsObjectTrue() throws InvalidTokenException {
234 final String phrase1 = GraphAnswerTest.CREATE;
235 final String option1 = GraphAnswerTest.TABLE;
236 final String option2 = GraphAnswerTest.TABLESPACE;
237
238 final List<Token> phrases1 = new ArrayList<Token>();
239 phrases1.add(new Token(phrase1, true));
240 final List<Token> options1 = new ArrayList<Token>();
241 options1.add(new Token(option1, true));
242 options1.add(new Token(option2, true));
243 final GraphAnswer expected = new GraphAnswer(phrases1, options1);
244
245 final List<Token> phrases2 = new ArrayList<Token>();
246 phrases2.add(new Token(phrase1, true));
247 final List<Token> options2 = new ArrayList<Token>();
248 options2.add(new Token(option1, true));
249 options2.add(new Token(option2, true));
250 final GraphAnswer actual = new GraphAnswer(phrases2, options2);
251
252 final boolean condition = expected.equals(actual);
253
254 Assert.assertTrue("Two identical objects with are equals",
255 condition);
256 }
257
258
259
260
261 @Test(expected = AssertionError.class)
262 public void testGraphAnswerNull1() {
263 new GraphAnswer(null, new ArrayList<Token>());
264 }
265
266
267
268
269 @Test(expected = AssertionError.class)
270 public void testGraphAnswerNull2() {
271 new GraphAnswer(new ArrayList<Token>(), null);
272 }
273
274
275
276
277
278
279
280 @Test
281 public void testHashCode() throws InvalidTokenException {
282 final String phrase1 = GraphAnswerTest.CREATE;
283 final String option1 = GraphAnswerTest.TABLE;
284 final String option2 = GraphAnswerTest.TABLESPACE;
285
286 final List<Token> phrases = new ArrayList<Token>();
287 phrases.add(new Token(phrase1, true));
288 final List<Token> options = new ArrayList<Token>();
289 options.add(new Token(option1, true));
290 options.add(new Token(option2, true));
291 final GraphAnswer object = new GraphAnswer(phrases, options);
292
293 Assert.assertTrue("HashCode different to zero",
294 object.hashCode() != 0);
295 Assert.assertTrue("HashCode different to one",
296 object.hashCode() != 1);
297 Assert.assertTrue("HashCode different to minus one",
298 object.hashCode() != -1);
299 }
300
301
302
303
304
305
306
307 @Test
308 public void testHashCodeDifferent() throws InvalidTokenException {
309 final String phrase1 = GraphAnswerTest.CREATE_TABLE;
310 final String option11 = GraphAnswerTest.TABLE;
311 final String option12 = GraphAnswerTest.TABLESPACE;
312
313 final String phrase2 = GraphAnswerTest.CREATE_TABLESPACE;
314 final String option21 = GraphAnswerTest.TABLENAME;
315 final String option22 = GraphAnswerTest.TABLESPACENAME;
316
317 final List<Token> phrases1 = new ArrayList<Token>();
318 phrases1.add(new Token(phrase1, true));
319 final List<Token> options1 = new ArrayList<Token>();
320 options1.add(new Token(option11, true));
321 options1.add(new Token(option12, true));
322 final GraphAnswer expected = new GraphAnswer(phrases1, options1);
323
324 final List<Token> phrases2 = new ArrayList<Token>();
325 phrases2.add(new Token(phrase2, true));
326 final List<Token> options2 = new ArrayList<Token>();
327 options2.add(new Token(option21, true));
328 options2.add(new Token(option22, true));
329 final GraphAnswer actual = new GraphAnswer(phrases2, options2);
330
331 final boolean condition = expected.hashCode() != actual.hashCode();
332
333 Assert.assertTrue("Two different objects with different hash code",
334 condition);
335 }
336
337
338
339
340
341
342
343 @Test
344 public void testHashCodeIdentical() throws InvalidTokenException {
345 final String phrase1 = GraphAnswerTest.CREATE;
346 final String option1 = GraphAnswerTest.TABLE;
347 final String option2 = GraphAnswerTest.TABLESPACE;
348
349 final List<Token> phrases1 = new ArrayList<Token>();
350 phrases1.add(new Token(phrase1, true));
351 final List<Token> options1 = new ArrayList<Token>();
352 options1.add(new Token(option1, true));
353 options1.add(new Token(option2, true));
354 final GraphAnswer expected = new GraphAnswer(phrases1, options1);
355
356 final List<Token> phrases2 = new ArrayList<Token>();
357 phrases2.add(new Token(phrase1, true));
358 final List<Token> options2 = new ArrayList<Token>();
359 options2.add(new Token(option1, true));
360 options2.add(new Token(option2, true));
361 final GraphAnswer actual = new GraphAnswer(phrases2, options2);
362 Assert.assertEquals("Two identical objects with the same hash code",
363 expected.hashCode(), actual.hashCode());
364 }
365
366
367
368
369
370
371
372 @Test
373 public void testSimpleMethods() throws AbstractZemucanException {
374 final String tokenString1 = GraphAnswerTest.TEST1;
375 final String tokenString2 = GraphAnswerTest.TEST2;
376
377 final List<Token> phrases = new ArrayList<Token>();
378 phrases.add(new Token(tokenString1, true));
379 final List<Token> options = new ArrayList<Token>();
380 options.add(new Token(tokenString2, true));
381 final GraphAnswer graphAnswer = new GraphAnswer(phrases, options);
382
383 List<Token> actual = graphAnswer.getOptions();
384
385 List<Token> expected = options;
386
387 Assert.assertEquals("options", expected, actual);
388
389 actual = graphAnswer.getPhrases();
390
391 expected = phrases;
392
393 Assert.assertEquals("phrases", expected, actual);
394 }
395
396
397
398
399
400
401
402 @Test
403 public void testSymmetric() throws InvalidTokenException {
404 final String tokenString1 = GraphAnswerTest.TEST1;
405 final String tokenString2 = GraphAnswerTest.TEST2;
406
407 final List<Token> phrases = new ArrayList<Token>();
408 phrases.add(new Token(tokenString1, true));
409 final List<Token> options = new ArrayList<Token>();
410 options.add(new Token(tokenString2, true));
411 final GraphAnswer actual = new GraphAnswer(phrases, options);
412
413 final boolean value = actual.equals(actual);
414
415 Assert.assertTrue("Symmetry", value);
416 }
417
418
419
420
421
422
423
424 @Test
425 public void testToString() throws InvalidTokenException {
426 final String phrase1 = GraphAnswerTest.CREATE;
427 final String option1 = GraphAnswerTest.TABLE;
428 final String option2 = GraphAnswerTest.TABLESPACE;
429
430 final List<Token> phrases1 = new ArrayList<Token>();
431 phrases1.add(new Token(phrase1, true));
432 final List<Token> options1 = new ArrayList<Token>();
433 options1.add(new Token(option1, true));
434 options1.add(new Token(option2, true));
435 final GraphAnswer answer = new GraphAnswer(phrases1, options1);
436 final String actual = answer.toString();
437
438 final String expected = "[T[create|R]],{T[table|R]T[tablespace|R]}";
439
440 Assert.assertEquals("Method toString", expected, actual);
441 }
442
443
444
445
446
447
448
449 @Test
450 public void testToString2() throws InvalidTokenException {
451 final String phrase1 = GraphAnswerTest.TABLE;
452 final String option1 = GraphAnswerTest.TABLENAME;
453
454 final List<Token> phrases1 = new ArrayList<Token>();
455 phrases1.add(new Token(phrase1, true));
456 final List<Token> options1 = new ArrayList<Token>();
457 options1.add(new Token(option1, false));
458 final GraphAnswer answer = new GraphAnswer(phrases1, options1);
459 final String actual = answer.toString();
460
461 final String expected = "[T[table|R]],{T[<tableName>]}";
462
463 Assert.assertEquals("Method toString", expected, actual);
464 }
465 }