1   /*
2    * db2sa: DB2 Syntax Assistant
3    * Copyright (C) Andres Gomez Casanova
4    *
5    * This file is part of db2sa.
6    *
7    * db2sa 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   * db2sa 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: 2009-07-26 04:10:26 +0200 (Sun, 26 Jul 2009) $:
26   * Revision: $LastChangedRevision: 459 $:
27   * URL:      $HeadURL: https://db2sa.svn.sourceforge.net/svnroot/db2sa/branches/db2sa_beta/source-code/src/test/java/name/angoca/db2sa/core/syntax/graph/GraphTokenTest.java $:
28   */
29  package name.angoca.db2sa.core.syntax.graph;
30  
31  import name.angoca.db2sa.core.syntax.graph.exception.InvalidGraphTokenException;
32  
33  import org.junit.Assert;
34  import org.junit.Test;
35  
36  /**
37   * These are the test for a token in the graph.<br/>
38   * TODO messages for the asserts <b>Control Version</b><br />
39   * <ul>
40   * <li>0.0.1 Class creation.</li>
41   * <li>0.0.2 Recommendations from PMD.</li>
42   * <li>0.0.3 Organized.</li>
43   * <li>0.1.0 jUnit 4 annotations.</li>
44   * <li>0.1.1 condition as a variable.</li>
45   * <li>1.0.0 Moved to version 1.</li>
46   * </ul>
47   * 
48   * @author Andres Gomez Casanova <a
49   *         href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
50   * @version 1.0.0 2009-07-19
51   */
52  public final class GraphTokenTest {
53  
54      /**
55       * The 'create' word.
56       */
57      private final String CREATE = "create"; //$NON-NLS-1$
58      /**
59       * The 'table' word.
60       */
61      private final String TABLE = "table"; //$NON-NLS-1$
62      /**
63       * The 'tablepsace' word.
64       */
65      private final String TABLESPACE = "tablespace"; //$NON-NLS-1$
66      /**
67       * The '<tablename>' word.
68       */
69      private final String TABLENAME = "<tableName>"; //$NON-NLS-1$
70      /**
71       * The '{<tableName>}' word.
72       */
73      private final String TABLENAME_DESC = "{<tableName>}"; //$NON-NLS-1$
74  
75      /**
76       * Default constructor.
77       */
78      public GraphTokenTest() {
79          // Nothing.
80      }
81  
82      /**
83       * Test the creation of a token with an invalid name.
84       */
85      @Test(expected = InvalidGraphTokenException.class)
86      public final void testInvalidName() {
87          final String token = ""; //$NON-NLS-1$
88  
89          new GraphToken(token, true);
90  
91          Assert.fail("Exception before this"); //$NON-NLS-1$
92      }
93  
94      /**
95       * Test two identical tokens with different children.
96       */
97      @Test
98      public final void testEqualsDifferentChildren() {
99          final String token11 = this.CREATE;
100         final String token12 = this.TABLE;
101         final String token21 = this.CREATE;
102         final String token22 = this.TABLESPACE;
103 
104         final GraphToken actual = new GraphToken(token11, true);
105         final GraphToken child1 = new GraphToken(token12, true);
106         actual.addWay(child1);
107         final GraphToken expected = new GraphToken(token21, true);
108         final GraphToken child2 = new GraphToken(token22, true);
109         expected.addWay(child2);
110         final boolean condition1 = actual.equals(expected);
111         final boolean condition2 = expected.equals(actual);
112 
113         Assert.assertFalse("1 - Two identical tokens with different children "//$NON-NLS-1$
114                 + "(a.equals(e))", condition1);//$NON-NLS-1$
115         Assert.assertFalse("2 - Two identical tokens with different children "//$NON-NLS-1$
116                 + "(e.equals(a))", condition2);//$NON-NLS-1$
117     }
118 
119     /**
120      * Test two different tokens (reserved and non reserved).
121      */
122     @Test
123     public final void testEqualsDifferentMixed() {
124         final String token1 = this.CREATE;
125         final String token2 = this.TABLENAME;
126 
127         final GraphToken actual = new GraphToken(token1, true);
128         final GraphToken expected = new GraphToken(token2, false);
129         final boolean condition1 = actual.equals(expected);
130         final boolean condition2 = expected.equals(actual);
131 
132         Assert.assertFalse("1 - Two different tokens - mixed", //$NON-NLS-1$
133                 condition1);
134         Assert.assertFalse("2 - Two different tokens - mixed", //$NON-NLS-1$
135                 condition2);
136     }
137 
138     /**
139      * Test two different tokens (non reserved).
140      */
141     @Test
142     public final void testEqualsDifferentNotReserved() {
143         final String token1 = this.TABLENAME;
144         final String token2 = "<colName>"; //$NON-NLS-1$
145 
146         final GraphToken actual = new GraphToken(token1, false);
147         final GraphToken expected = new GraphToken(token2, false);
148         final boolean condition = actual.equals(expected);
149 
150         Assert.assertFalse("two different tokens - non reserved", //$NON-NLS-1$
151                 condition);
152     }
153 
154     /**
155      * Test two different tokens (reserved).
156      */
157     @Test
158     public final void testEqualsDifferentReserved() {
159         final String token1 = this.CREATE;
160         final String token2 = this.TABLE;
161 
162         final GraphToken actual = new GraphToken(token1, true);
163         final GraphToken expected = new GraphToken(token2, true);
164         final boolean condition = actual.equals(expected);
165 
166         Assert.assertFalse("two different token - reserved", condition); //$NON-NLS-1$
167     }
168 
169     /**
170      * Test two identical tokens with one of them with more children.
171      */
172     @Test
173     public final void testEqualsMoreChildren() {
174         final String token11 = this.CREATE;
175         final String token12 = this.TABLE;
176         final String token21 = this.CREATE;
177         final String token22 = this.TABLE;
178         final String token23 = this.TABLESPACE;
179 
180         final GraphToken actual = new GraphToken(token11, true);
181         final GraphToken child1 = new GraphToken(token12, true);
182         actual.addWay(child1);
183         final GraphToken expected = new GraphToken(token21, true);
184         final GraphToken child2 = new GraphToken(token22, true);
185         expected.addWay(child2);
186         final GraphToken child3 = new GraphToken(token23, true);
187         expected.addWay(child3);
188         final boolean condition1 = actual.equals(expected);
189         final boolean condition2 = expected.equals(actual);
190 
191         Assert.assertFalse("1 - two identical token, one with children", //$NON-NLS-1$
192                 condition1);
193         Assert.assertFalse("2- two identical token, one with children", //$NON-NLS-1$
194                 condition2);
195     }
196 
197     /**
198      * Test two identical tokens that represents a not reserved word.
199      */
200     @Test
201     public final void testEqualsNotReserved() {
202         final String token1 = this.TABLENAME;
203         final String token2 = this.TABLENAME;
204 
205         final GraphToken actual = new GraphToken(token1, false);
206         final GraphToken expected = new GraphToken(token2, false);
207         final boolean condition = actual.equals(expected);
208 
209         Assert.assertTrue("two identical token reserved", condition); //$NON-NLS-1$
210     }
211 
212     /**
213      * Test equals with other null.
214      */
215     @Test
216     public final void testEqualsNull() {
217         final String token1 = this.CREATE;
218 
219         final GraphToken actual = new GraphToken(token1, true);
220 
221         final GraphToken nullObject = null;
222 
223         final boolean condition = actual.equals(nullObject);
224 
225         Assert.assertFalse("equals with null value", condition); //$NON-NLS-1$
226     }
227 
228     /**
229      * Test equals with other object.
230      */
231     @Test
232     public final void testEqualsOtherObject() {
233         final String token1 = this.CREATE;
234 
235         final GraphToken actual = new GraphToken(token1, true);
236         final StartingToken expected = new StartingToken();
237         final boolean condition = actual.equals(expected);
238 
239         Assert.assertFalse("equals with two identical objects", //$NON-NLS-1$
240                 condition);
241     }
242 
243     /**
244      * Test two identical tokens that represents a reserved word.
245      */
246     @Test
247     public final void testEqualsReserved() {
248         final String token1 = this.CREATE;
249         final String token2 = this.CREATE;
250 
251         final GraphToken actual = new GraphToken(token1, true);
252         final GraphToken expected = new GraphToken(token2, true);
253         final boolean condition = actual.equals(expected);
254 
255         Assert.assertTrue("two identical tokens - reserved word", //$NON-NLS-1$
256                 condition);
257     }
258 
259     /**
260      * Test two identical tokens with same children.
261      */
262     @Test
263     public final void testEqualsSameChildren() {
264         final String token11 = this.CREATE;
265         final String token12 = this.TABLE;
266         final String token21 = this.CREATE;
267         final String token22 = this.TABLE;
268 
269         final GraphToken actual = new GraphToken(token11, true);
270         final GraphToken child1 = new GraphToken(token12, true);
271         actual.addWay(child1);
272         final GraphToken expected = new GraphToken(token21, true);
273         final GraphToken child2 = new GraphToken(token22, true);
274         expected.addWay(child2);
275         final boolean condition = actual.equals(expected);
276 
277         Assert.assertTrue(condition);
278     }
279 
280     /**
281      * Test a the hashcode of two different tokens.
282      */
283     @Test
284     public final void testHashCodeDifferent() {
285         final String token1 = this.CREATE;
286         final String token2 = this.TABLENAME;
287 
288         final GraphToken actual = new GraphToken(token1, true);
289         final GraphToken expected = new GraphToken(token2, false);
290         final boolean condition = actual.hashCode() == expected.hashCode();
291 
292         Assert.assertFalse(condition);
293     }
294 
295     /**
296      * Test a the hashcode of two different objects.
297      */
298     @Test
299     public final void testHashCodeDifferentWithChildren() {
300         final String token1 = this.CREATE;
301         final String token2 = this.CREATE;
302         final String token21 = this.TABLE;
303 
304         final GraphToken actual = new GraphToken(token1, false);
305         final GraphToken expected = new GraphToken(token2, false);
306         final GraphToken child = new GraphToken(token21, false);
307         expected.addWay(child);
308         final boolean condition = actual.hashCode() == expected.hashCode();
309 
310         Assert.assertFalse(condition);
311     }
312 
313     /**
314      * Test a the hashcode of two identical non reserved tokens.
315      */
316     @Test
317     public final void testHashCodeIdenticalNotReserved() {
318         final String token1 = this.TABLENAME;
319         final String token2 = this.TABLENAME;
320 
321         final GraphToken actual = new GraphToken(token1, false);
322         final GraphToken expected = new GraphToken(token2, false);
323         final boolean condition = actual.hashCode() == expected.hashCode();
324 
325         Assert.assertTrue(condition);
326     }
327 
328     /**
329      * Test a the hashcode of two identical reserved tokens.
330      */
331     @Test
332     public final void testHashCodeIdenticalReserved() {
333         final String token1 = this.CREATE;
334         final String token2 = this.CREATE;
335 
336         final GraphToken actual = new GraphToken(token1, true);
337         final GraphToken expected = new GraphToken(token2, true);
338         final boolean condition = actual.hashCode() == expected.hashCode();
339 
340         Assert.assertTrue(condition);
341     }
342 
343     /**
344      * Represents a reserved word in capitals with a reserved word not in
345      * capitals.
346      */
347     @Test
348     public final void testRepresentCapitalReservedFalse() {
349         final String token1 = this.CREATE;
350         final String token2 = "TABLE"; //$NON-NLS-1$
351 
352         final GraphToken token = new GraphToken(token1, true);
353         final boolean condition = token.represent(token2);
354 
355         Assert.assertFalse(condition);
356     }
357 
358     /**
359      * Represents a reserved word in capitals.
360      */
361     @Test
362     public final void testRepresentCapitalReservedTrue() {
363         final String token1 = this.CREATE;
364         final String token2 = "CREATE"; //$NON-NLS-1$
365 
366         final GraphToken token = new GraphToken(token1, true);
367         final boolean condition = token.represent(token2);
368 
369         Assert.assertTrue(condition);
370     }
371 
372     /**
373      * Represents a word with a not reserved word.
374      */
375     @Test
376     public final void testRepresentNotReservedTrue() {
377         final String token1 = this.TABLENAME;
378         final String token2 = "Employees"; //$NON-NLS-1$
379 
380         final GraphToken token = new GraphToken(token1, false);
381         final boolean condition = token.represent(token2);
382 
383         Assert.assertTrue(condition);
384     }
385 
386     /**
387      * Represents two different reserved words.
388      */
389     @Test
390     public final void testRepresentReservedFalse() {
391         final String token1 = this.CREATE;
392         final String token2 = this.TABLE;
393 
394         final GraphToken token = new GraphToken(token1, true);
395         final boolean condition = token.represent(token2);
396 
397         Assert.assertFalse(condition);
398     }
399 
400     /**
401      * Represents a reserved word.
402      */
403     @Test
404     public final void testRepresentReservedTrue() {
405         final String token1 = this.CREATE;
406         final String token2 = this.CREATE;
407 
408         final GraphToken token = new GraphToken(token1, true);
409         final boolean condition = token.represent(token2);
410 
411         Assert.assertTrue(condition);
412     }
413 
414     /**
415      * Test a token that is not a reserved word.
416      */
417     @Test
418     public final void testToStringNotReserved() {
419         final String tokenIn = this.TABLENAME;
420         final String tokenOut = this.TABLENAME_DESC;
421 
422         final GraphToken actualToken = new GraphToken(tokenIn, false);
423         final String actual = actualToken.toString();
424         final String expected = tokenOut;
425 
426         Assert.assertEquals(expected, actual);
427     }
428 
429     /**
430      * Test a token that is a reserved word.
431      */
432     @Test
433     public final void testToStringReserved() {
434         final String tokenIn = this.CREATE;
435         final String tokenOut = "{create|R}"; //$NON-NLS-1$
436 
437         final GraphToken actualToken = new GraphToken(tokenIn, true);
438         final String actual = actualToken.toString();
439         final String expected = tokenOut;
440 
441         Assert.assertEquals(expected, actual);
442     }
443 
444     /**
445      * Test a token that with children.
446      */
447     @Test
448     public final void testToStringWithChildren() {
449         final String tokenIn = this.TABLENAME;
450         final String tokenIn1 = this.TABLENAME;
451         final String tokenOut = this.TABLENAME_DESC;
452 
453         final GraphToken actualToken = new GraphToken(tokenIn, false);
454         final GraphToken actualToken1 = new GraphToken(tokenIn1, false);
455         actualToken.addWay(actualToken1);
456         final String actual = actualToken.toString();
457         final String expected = tokenOut;
458 
459         Assert.assertEquals(expected, actual);
460     }
461 
462 }