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 09:19:05 -0500 (dom, 06 mar 2011) $:
26   * Revision: $LastChangedRevision: 1910 $:
27   * URL:      $HeadURL: https://zemucan.svn.sourceforge.net/svnroot/zemucan/branches/zemucan_v1/source-code/analyzers/src/test/java/name/angoca/zemucan/core/lexical/model/TokenTest.java $:
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   * This class tests the Token object and its principal methods.
40   * <p>
41   * <b>Control Version</b>
42   * <p>
43   * <ul>
44   * <li>0.0.1 Class creation.</li>
45   * <li>0.1.0 Throw exception.</li>
46   * <li>0.1.1 final.</li>
47   * <li>1.0.0 Moved to version 1.</li>
48   * <li>1.1.0 Symmetric and congruent test.</li>
49   * <li>1.2.0 Simple methods (coverage.)</li>
50   * <li>1.3.0 Name of the tests and null test</li>
51   * <li>1.3.1 Token is reserved.</li>
52   * </ul>
53   *
54   * @author Andres Gomez Casanova <a
55   *         href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
56   * @version 1.3.1 2010-05-14
57   */
58  @RunWith(RandomTestRunner.class)
59  public final class TokenTest {
60      /**
61       * The 'create' word.
62       */
63      private static final String CREATE = "create"; //$NON-NLS-1$
64  
65      /**
66       * Default constructor.
67       */
68      public TokenTest() {
69          // Nothing.
70      }
71  
72      /**
73       * Tests if two consecutive calls returns the same value.
74       *
75       * @throws InvalidTokenException
76       *             Never.
77       */
78      @Test
79      public void testCongruent() throws InvalidTokenException {
80          final String text = "test"; //$NON-NLS-1$
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", //$NON-NLS-1$
89                  condition);
90      }
91  
92      /**
93       * A token cannot be a empty string.
94       *
95       * @throws InvalidTokenException
96       *             Always, this method test this thrown.
97       */
98      @Test
99      public void testCreation() throws InvalidTokenException {
100         final String token = ""; //$NON-NLS-1$
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      * Tests the equals method with two different objects.
111      *
112      * @throws InvalidTokenException
113      *             Never.
114      */
115     @Test
116     public void testEqualsDifferentObject() throws InvalidTokenException {
117         final String token1 = TokenTest.CREATE;
118         final String token2 = "table"; //$NON-NLS-1$
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 //$NON-NLS-1$
124                 .equals(expected));
125     }
126 
127     /**
128      * Tests the equals method with two identical objects.
129      *
130      * @throws InvalidTokenException
131      *             Never.
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 //$NON-NLS-1$
141                 .equals(expected));
142     }
143 
144     /**
145      * Tests the equal method with a null value.
146      *
147      * @throws InvalidTokenException
148      *             Never.
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); //$NON-NLS-1$
161     }
162 
163     /**
164      * Tests that the hashcode is different to 1, 0 and -1.
165      *
166      * @throws InvalidTokenException
167      *             Never.
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", //$NON-NLS-1$
178                 condition);
179         condition = object.hashCode() != 1;
180         Assert.assertTrue("hashcode different to 1", //$NON-NLS-1$
181                 condition);
182         condition = object.hashCode() != -1;
183         Assert.assertTrue("hashcode different to -1", //$NON-NLS-1$
184                 condition);
185     }
186 
187     /**
188      * Tests that the hashcode of two different objects are different.
189      *
190      * @throws InvalidTokenException
191      *             Never.
192      */
193     @Test
194     public void testHashCodeDifferentObject() throws InvalidTokenException {
195         final String token1 = TokenTest.CREATE;
196         final String token2 = "table"; //$NON-NLS-1$
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", //$NON-NLS-1$
204                 condition);
205     }
206 
207     /**
208      * Test that the hashcode of two objects with the same parameters have the
209      * same hashcode.
210      *
211      * @throws InvalidTokenException
212      *             Never.
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", //$NON-NLS-1$
222                 actual.hashCode(), expected.hashCode());
223     }
224 
225     /**
226      * Tests the simple methods.
227      *
228      * @throws InvalidTokenException
229      *             Never.
230      */
231     @Test
232     public void testSimpleMethods() throws InvalidTokenException {
233         final String text = "test"; //$NON-NLS-1$
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); //$NON-NLS-1$
242 
243         actual = token.toString();
244 
245         expected = "T[" + text + ']'; //$NON-NLS-1$
246     }
247 
248     /**
249      * Tests the symmetry of equals.
250      *
251      * @throws InvalidTokenException
252      *             Never
253      */
254     @Test
255     public void testSymmetric() throws InvalidTokenException {
256         final String text = "test"; //$NON-NLS-1$
257 
258         final Token actual = new Token(text, true);
259 
260         final boolean value = actual.equals(actual);
261 
262         Assert.assertTrue("Symmetry", value); //$NON-NLS-1$
263     }
264 
265     /**
266      * Tests that the constructor cannot receive a null parameter.
267      *
268      * @throws InvalidTokenException
269      *             Never.
270      */
271     @Test(expected = AssertionError.class)
272     public void testTokenNullParam() throws InvalidTokenException {
273         new Token(null, true);
274     }
275 }