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/GraphAnswerTest.java $:
28   */
29  package name.angoca.db2sa.core.syntax;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  import name.angoca.db2sa.core.lexical.Token;
35  import name.angoca.db2sa.core.lexical.exceptions.InvalidTokenException;
36  
37  import org.junit.Assert;
38  import org.junit.Test;
39  
40  /**
41   * These are the tests for the GraphAnswer. It tests only the object.<br/>
42   * TODO Revisar que NO se revisen null pointers, ya que por eso se definieron
43   * los params de entrada. <b>Control Version</b><br />
44   * <ul>
45   * <li>0.0.1 Class creation.</li>
46   * <li>0.1.0 More tests.</li>
47   * <li>0.1.1 final.</li>
48   * <li>1.0.0 Moved to version 1.</li>
49   * </ul>
50   * 
51   * @author Andres Gomez Casanova <a
52   *         href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
53   * @version 1.0.0 2009-07-19
54   */
55  public final class GraphAnswerTest {
56  
57      /**
58       * The 'create' word.
59       */
60      private final String CREATE = "create"; //$NON-NLS-1$
61      /**
62       * The 'table' word.
63       */
64      private final String TABLE = "table"; //$NON-NLS-1$
65      /**
66       * The 'tablespace' word.
67       */
68      private final String TABLESPACE = "tablespace"; //$NON-NLS-1$
69      /**
70       * The 'create table' words.
71       */
72      private final String CREATE_TABLE = "create table"; //$NON-NLS-1$
73      /**
74       * The 'create tablespace' word.
75       */
76      private final String CREATE_TABLESPACE = "create talespace"; //$NON-NLS-1$
77      /**
78       * The '<tableName>' word.
79       */
80      private final String TABLENAME = "<tableName>"; //$NON-NLS-1$
81      /**
82       * The '<tablespaceName>' word.
83       */
84      private final String TABLESPACENAME = "<tablespaceName>"; //$NON-NLS-1$
85  
86      /**
87       * Default constructor.
88       */
89      public GraphAnswerTest() {
90          // Nothing.
91      }
92  
93      /**
94       * Tests if the hashcode method does not return 0, 1 or -1.
95       * 
96       * @throws InvalidTokenException
97       *             Never.
98       */
99      @Test
100     public final void testHashCode() throws InvalidTokenException {
101         final String phrase1 = this.CREATE;
102         final String option1 = this.TABLE;
103         final String option2 = this.TABLESPACE;
104 
105         final List<Token> phrases = new ArrayList<Token>();
106         phrases.add(new Token(phrase1));
107         final List<Token> options = new ArrayList<Token>();
108         options.add(new Token(option1));
109         options.add(new Token(option2));
110         final GraphAnswer object = new GraphAnswer(phrases, options);
111 
112         Assert.assertTrue("HashCode different to zero", //$NON-NLS-1$
113                 object.hashCode() != 0);
114         Assert.assertTrue("HashCode different to one", //$NON-NLS-1$
115                 object.hashCode() != 1);
116         Assert.assertTrue("HashCode different to minus one", //$NON-NLS-1$
117                 object.hashCode() != -1);
118     }
119 
120     /**
121      * This tests the hashCode method with two identical objects.
122      * 
123      * @throws InvalidTokenException
124      *             never.
125      */
126     @Test
127     public final void testHashCodeIdentical() throws InvalidTokenException {
128         final String phrase1 = this.CREATE;
129         final String option1 = this.TABLE;
130         final String option2 = this.TABLESPACE;
131 
132         final List<Token> phrases1 = new ArrayList<Token>();
133         phrases1.add(new Token(phrase1));
134         final List<Token> options1 = new ArrayList<Token>();
135         options1.add(new Token(option1));
136         options1.add(new Token(option2));
137         final GraphAnswer expected = new GraphAnswer(phrases1, options1);
138 
139         final List<Token> phrases2 = new ArrayList<Token>();
140         phrases2.add(new Token(phrase1));
141         final List<Token> options2 = new ArrayList<Token>();
142         options2.add(new Token(option1));
143         options2.add(new Token(option2));
144         final GraphAnswer actual = new GraphAnswer(phrases2, options2);
145         Assert.assertEquals("Two identical objects with the same hash code", //$NON-NLS-1$
146                 expected.hashCode(), actual.hashCode());
147     }
148 
149     /**
150      * This tests the hashCode method with two different objects.
151      * 
152      * @throws InvalidTokenException
153      *             never.
154      */
155     @Test
156     public final void testHashCodeDifferent() throws InvalidTokenException {
157         final String phrase1 = this.CREATE_TABLE;
158         final String option11 = this.TABLE;
159         final String option12 = this.TABLESPACE;
160 
161         final String phrase2 = this.CREATE_TABLESPACE;
162         final String option21 = this.TABLENAME;
163         final String option22 = this.TABLESPACENAME;
164 
165         final List<Token> phrases1 = new ArrayList<Token>();
166         phrases1.add(new Token(phrase1));
167         final List<Token> options1 = new ArrayList<Token>();
168         options1.add(new Token(option11));
169         options1.add(new Token(option12));
170         final GraphAnswer expected = new GraphAnswer(phrases1, options1);
171 
172         final List<Token> phrases2 = new ArrayList<Token>();
173         phrases2.add(new Token(phrase2));
174         final List<Token> options2 = new ArrayList<Token>();
175         options2.add(new Token(option21));
176         options2.add(new Token(option22));
177         final GraphAnswer actual = new GraphAnswer(phrases2, options2);
178         // TODO Hacer la comparasión en una variable que se llame condition. En
179         // varios lados cambiar esto.
180         Assert.assertTrue("Two different objects with different hash code", //$NON-NLS-1$
181                 expected.hashCode() != actual.hashCode());
182     }
183 
184     /**
185      * Tests the method equals with a null.
186      * 
187      * @throws InvalidTokenException
188      *             Never.
189      */
190     @Test
191     public final void testEqualsNull() throws InvalidTokenException {
192         final String phrase1 = this.CREATE;
193         final String option1 = this.TABLE;
194         final String option2 = this.TABLESPACE;
195 
196         final List<Token> phrases = new ArrayList<Token>();
197         phrases.add(new Token(phrase1));
198         final List<Token> options = new ArrayList<Token>();
199         options.add(new Token(option1));
200         options.add(new Token(option2));
201         final GraphAnswer object = new GraphAnswer(phrases, options);
202 
203         final GraphAnswer nullObject = null;
204 
205         final boolean condition = object.equals(nullObject);
206 
207         Assert.assertFalse("Equals different to null", condition); //$NON-NLS-1$
208     }
209 
210     /**
211      * Tests the equals method with two identical objects.
212      * 
213      * @throws InvalidTokenException
214      *             Never.
215      */
216     @Test
217     public final void testEqualsObjectTrue() throws InvalidTokenException {
218         final String phrase1 = this.CREATE;
219         final String option1 = this.TABLE;
220         final String option2 = this.TABLESPACE;
221 
222         final List<Token> phrases1 = new ArrayList<Token>();
223         phrases1.add(new Token(phrase1));
224         final List<Token> options1 = new ArrayList<Token>();
225         options1.add(new Token(option1));
226         options1.add(new Token(option2));
227         final GraphAnswer expected = new GraphAnswer(phrases1, options1);
228 
229         final List<Token> phrases2 = new ArrayList<Token>();
230         phrases2.add(new Token(phrase1));
231         final List<Token> options2 = new ArrayList<Token>();
232         options2.add(new Token(option1));
233         options2.add(new Token(option2));
234         final GraphAnswer actual = new GraphAnswer(phrases2, options2);
235         Assert.assertTrue("Two identical objects with are equals", //$NON-NLS-1$
236                 expected.equals(actual));
237     }
238 
239     /**
240      * Tests the equals method with two different objects.
241      * 
242      * @throws InvalidTokenException
243      *             Never.
244      */
245     @Test
246     public final void testEqualsObjectFalse() throws InvalidTokenException {
247         final String phrase1 = this.CREATE_TABLE;
248         final String option11 = this.TABLE;
249         final String option12 = this.TABLESPACE;
250 
251         final String phrase2 = this.CREATE_TABLESPACE;
252         final String option21 = this.TABLENAME;
253         final String option22 = this.TABLESPACENAME;
254 
255         final List<Token> phrases1 = new ArrayList<Token>();
256         phrases1.add(new Token(phrase1));
257         final List<Token> options1 = new ArrayList<Token>();
258         options1.add(new Token(option11));
259         options1.add(new Token(option12));
260         final GraphAnswer expected = new GraphAnswer(phrases1, options1);
261 
262         final List<Token> phrases2 = new ArrayList<Token>();
263         phrases2.add(new Token(phrase2));
264         final List<Token> options2 = new ArrayList<Token>();
265         options2.add(new Token(option21));
266         options2.add(new Token(option22));
267         final GraphAnswer actual = new GraphAnswer(phrases2, options2);
268         Assert.assertFalse("Two different objects are not equals", //$NON-NLS-1$
269                 expected.equals(actual));
270     }
271 
272     /**
273      * Test the toString method.
274      * 
275      * @throws InvalidTokenException
276      *             Never.
277      */
278     @Test
279     public final void testToString() throws InvalidTokenException {
280         final String phrase1 = this.CREATE;
281         final String option1 = this.TABLE;
282         final String option2 = this.TABLESPACE;
283 
284         final List<Token> phrases1 = new ArrayList<Token>();
285         phrases1.add(new Token(phrase1));
286         final List<Token> options1 = new ArrayList<Token>();
287         options1.add(new Token(option1));
288         options1.add(new Token(option2));
289         final GraphAnswer answer = new GraphAnswer(phrases1, options1);
290         final String actual = answer.toString();
291 
292         final String expected = "[T[create]],{T[table]T[tablespace]}"; //$NON-NLS-1$
293 
294         Assert.assertEquals("Method toString", expected, actual); //$NON-NLS-1$
295     }
296 }