View Javadoc

1   /*
2    * Zemucan: A Syntax Assistant for DB2
3    * Copyright (C) 2009, 2010 Andres Gomez Casanova
4    *
5    * This file is part of Zemucan.
6    *
7    * Zemucan is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as published by
9    * the Free Software Foundation; either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * Zemucan is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public License
18   * along with this library; if not, see <http://www.gnu.org/licenses/>.
19   *
20   * Contact:
21   * a n g o c a  at  y a h o o  dot  c o m
22   * Cra. 45 No 61 - 31, Bogota, Colombia.
23   *
24   * Author:   $LastChangedBy: angoca $:
25   * Date:     $LastChangedDate: 2011-03-06 10:15:32 -0500 (dom, 06 mar 2011) $:
26   * Revision: $LastChangedRevision: 1913 $:
27   * URL:      $HeadURL: https://zemucan.svn.sourceforge.net/svnroot/zemucan/branches/zemucan_v1/source-code/analyzers/src/test/java/name/angoca/zemucan/core/syntactic/model/GraphAnswerTest.java $:
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   * These are the tests for the GraphAnswer. It tests only the object.
45   * <p>
46   * <b>Control Version</b>
47   * <p>
48   * <ul>
49   * <li>0.0.1 Class creation.</li>
50   * <li>0.1.0 More tests.</li>
51   * <li>0.1.1 final.</li>
52   * <li>1.0.0 Moved to version 1.</li>
53   * <li>1.1.0 Symmetric and congruent test.</li>
54   * <li>1.2.0 Simple methods (coverage.)</li>
55   * <li>1.3.0 Name tests, and null tests</li>
56   * <li>1.3.1 Token is reserved.</li>
57   * </ul>
58   *
59   * @author Andres Gomez Casanova <a
60   *         href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
61   * @version 1.3.1 2010-05-14
62   */
63  @RunWith(RandomTestRunner.class)
64  public final class GraphAnswerTest {
65  
66      /**
67       * The 'create' word.
68       */
69      private static final String CREATE = "create"; //$NON-NLS-1$
70      /**
71       * The 'create table' words.
72       */
73      private static final String CREATE_TABLE = "create table"; //$NON-NLS-1$
74      /**
75       * The 'create tablespace' word.
76       */
77      private static final String CREATE_TABLESPACE = "create talespace"; //$NON-NLS-1$
78      /**
79       * The 'table' word.
80       */
81      private static final String TABLE = "table"; //$NON-NLS-1$
82      /**
83       * The '&lt;tableName&gt;' word.
84       */
85      private static final String TABLENAME = "<tableName>"; //$NON-NLS-1$
86      /**
87       * The 'tablespace' word.
88       */
89      private static final String TABLESPACE = "tablespace"; //$NON-NLS-1$
90      /**
91       * The '&lt;tablespaceName&gt;' word.
92       */
93      private static final String TABLESPACENAME = "<tablespaceName>"; //$NON-NLS-1$
94      /**
95       * Word 'test1'
96       */
97      private static final String TEST1 = "test1"; //$NON-NLS-1$
98      /**
99       * Word 'test2'
100      */
101     private static final String TEST2 = "test2"; //$NON-NLS-1$
102 
103     /**
104      * Default constructor.
105      */
106     public GraphAnswerTest() {
107         // Nothing.
108     }
109 
110     /**
111      * Tests if two consecutive calls returns the same value.
112      *
113      * @throws InvalidTokenException
114      *             Never.
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); //$NON-NLS-1$
131     }
132 
133     /**
134      * Tests the equals with a similar but not equal object.
135      *
136      * @throws InvalidTokenException
137      *             Never.
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"; //$NON-NLS-1$
144         final String tokenString4 = "test4"; //$NON-NLS-1$
145         final String tokenString5 = "test5"; //$NON-NLS-1$
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); //$NON-NLS-1$
162     }
163 
164     /**
165      * Tests the method equals with a null.
166      *
167      * @throws InvalidTokenException
168      *             Never.
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); //$NON-NLS-1$
188     }
189 
190     /**
191      * Tests the equals method with two different objects.
192      *
193      * @throws InvalidTokenException
194      *             Never.
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", //$NON-NLS-1$
223                 condition);
224     }
225 
226     /**
227      * Tests the equals method with two identical objects.
228      *
229      * @throws InvalidTokenException
230      *             Never.
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", //$NON-NLS-1$
255                 condition);
256     }
257 
258     /**
259      * Tests the constructor with a null parameter.
260      */
261     @Test(expected = AssertionError.class)
262     public void testGraphAnswerNull1() {
263         new GraphAnswer(null, new ArrayList<Token>());
264     }
265 
266     /**
267      * Tests the constructor with a null parameter.
268      */
269     @Test(expected = AssertionError.class)
270     public void testGraphAnswerNull2() {
271         new GraphAnswer(new ArrayList<Token>(), null);
272     }
273 
274     /**
275      * Tests if the hashcode method does not return 0, 1 or -1.
276      *
277      * @throws InvalidTokenException
278      *             Never.
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", //$NON-NLS-1$
294                 object.hashCode() != 0);
295         Assert.assertTrue("HashCode different to one", //$NON-NLS-1$
296                 object.hashCode() != 1);
297         Assert.assertTrue("HashCode different to minus one", //$NON-NLS-1$
298                 object.hashCode() != -1);
299     }
300 
301     /**
302      * This tests the hashCode method with two different objects.
303      *
304      * @throws InvalidTokenException
305      *             never.
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", //$NON-NLS-1$
334                 condition);
335     }
336 
337     /**
338      * This tests the hashCode method with two identical objects.
339      *
340      * @throws InvalidTokenException
341      *             never.
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", //$NON-NLS-1$
363                 expected.hashCode(), actual.hashCode());
364     }
365 
366     /**
367      * Test the simple methods.
368      *
369      * @throws AbstractZemucanException
370      *             Never.
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); //$NON-NLS-1$
388 
389         actual = graphAnswer.getPhrases();
390 
391         expected = phrases;
392 
393         Assert.assertEquals("phrases", expected, actual); //$NON-NLS-1$
394     }
395 
396     /**
397      * Tests the symmetry of equals.
398      *
399      * @throws InvalidTokenException
400      *             Never.
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); //$NON-NLS-1$
416     }
417 
418     /**
419      * Test the toString method.
420      *
421      * @throws InvalidTokenException
422      *             Never.
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]}"; //$NON-NLS-1$
439 
440         Assert.assertEquals("Method toString", expected, actual); //$NON-NLS-1$
441     }
442 
443     /**
444      * Test the toString method.
445      *
446      * @throws InvalidTokenException
447      *             Never.
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>]}"; //$NON-NLS-1$
462 
463         Assert.assertEquals("Method toString", expected, actual); //$NON-NLS-1$
464     }
465 }