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/uiApi/src/test/java/name/angoca/zemucan/interfaze/model/ReturnOptionsTest.java $:
28   */
29  package name.angoca.zemucan.interfaze.model;
30  
31  import name.angoca.zemucan.ParameterNullException;
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 is the test for the ReturnOption class, testing the main method,
40   * including those that retrieve array copies.
41   * <p>
42   * <b>Control Version</b>
43   * <p>
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   * <li>1.1.0 Symmetric and congruent tests.</li>
50   * <li>1.2.0 More tests and thorws.</li>
51   * </ul>
52   *
53   * @author Andres Gomez Casanova <a
54   *         href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
55   * @version 1.2.0 2009-09-27
56   */
57  @RunWith(RandomTestRunner.class)
58  public final class ReturnOptionsTest {
59      /**
60       * The 'create' word.
61       */
62      private static final String CREATE = "create"; //$NON-NLS-1$
63      /**
64       * The 'table' word.
65       */
66      private static final String TABLE = "table"; //$NON-NLS-1$
67      /**
68       * The '&lt;tableName&gt;' word.
69       */
70      private static final String TABLE_NAME = "<tableName>"; //$NON-NLS-1$
71      /**
72       * The 'tablespace' word.
73       */
74      private static final String TABLESPACE = "tablespace"; //$NON-NLS-1$
75  
76      /**
77       * Default constructor.
78       */
79      public ReturnOptionsTest() {
80          // Nothing.
81      }
82  
83      /**
84       * Tests if two consecutive calls returns the same value.
85       *
86       * @throws ParameterNullException
87       *             Never.
88       */
89      @Test
90      public void testCongruent() throws ParameterNullException {
91          final String phrase = "test"; //$NON-NLS-1$
92          final String[] phrases = new String[] { "test1" }; //$NON-NLS-1$
93          final String[] options = new String[] { "test2" }; //$NON-NLS-1$
94  
95          final ReturnOptions token = new ReturnOptions(phrase, phrases, options);
96          final int val1 = token.hashCode();
97          final int val2 = token.hashCode();
98  
99          final boolean condition = val1 == val2;
100 
101         Assert.assertTrue("Two consecutive calls return the same value", //$NON-NLS-1$
102                 condition);
103     }
104 
105     /**
106      * Tests the equals method with two different objects.
107      *
108      * @throws ParameterNullException
109      *             Never.
110      */
111     @Test
112     public void testEqualsObjectDifferent1() throws ParameterNullException {
113         final String phrase1 = "create table"; //$NON-NLS-1$
114         final String phrase11 = ReturnOptionsTest.TABLESPACE;
115         final String option11 = ReturnOptionsTest.TABLE_NAME;
116 
117         final String phrase2 = ReturnOptionsTest.CREATE;
118         final String option21 = ReturnOptionsTest.TABLE;
119         final String option22 = ReturnOptionsTest.TABLESPACE;
120 
121         final ReturnOptions object1 = new ReturnOptions(phrase1,
122                 new String[] { phrase11 }, new String[] { option11 });
123 
124         final ReturnOptions object2 = new ReturnOptions(phrase2,
125                 new String[] {}, new String[] { option21, option22 });
126 
127         Assert.assertFalse("equals between two different objects", //$NON-NLS-1$
128                 object1.equals(object2));
129     }
130 
131     /**
132      * Tests the equals method with two different objects.
133      *
134      * @throws ParameterNullException
135      *             Never.
136      */
137     @Test
138     public void testEqualsObjectDifferent2() throws ParameterNullException {
139         final String phrase1 = ReturnOptionsTest.CREATE;
140         final String option1 = ReturnOptionsTest.TABLESPACE;
141 
142         final String phrase2 = ReturnOptionsTest.CREATE;
143         final String option2 = ReturnOptionsTest.TABLE;
144 
145         final ReturnOptions object1 = new ReturnOptions(phrase1,
146                 new String[] {}, new String[] { option1 });
147 
148         final ReturnOptions object2 = new ReturnOptions(phrase2,
149                 new String[] {}, new String[] { option2 });
150 
151         Assert.assertFalse("equals between two different objects", //$NON-NLS-1$
152                 object1.equals(object2));
153     }
154 
155     /**
156      * Tests the equals method with two different objects.
157      *
158      * @throws ParameterNullException
159      *             Never.
160      */
161     @Test
162     public void testEqualsObjectDifferent3() throws ParameterNullException {
163         final String phrase1 = ReturnOptionsTest.CREATE;
164         final String phrase11 = ReturnOptionsTest.TABLESPACE;
165 
166         final String phrase2 = ReturnOptionsTest.CREATE;
167         final String phrase21 = ReturnOptionsTest.TABLE;
168 
169         final ReturnOptions object1 = new ReturnOptions(phrase1,
170                 new String[] { phrase11 }, new String[] {});
171 
172         final ReturnOptions object2 = new ReturnOptions(phrase2,
173                 new String[] { phrase21 }, new String[] {});
174 
175         Assert.assertFalse("equals between two different objects", //$NON-NLS-1$
176                 object1.equals(object2));
177     }
178 
179     /**
180      * Tests the equals method with two identical objects.
181      *
182      * @throws ParameterNullException
183      *             Never.
184      */
185     @Test
186     public void testEqualsObjectIdentical() throws ParameterNullException {
187         final String phrase = "create table"; //$NON-NLS-1$
188         final String phrase1 = ReturnOptionsTest.TABLESPACE;
189         final String option1 = ReturnOptionsTest.TABLE_NAME;
190 
191         final ReturnOptions object1 = new ReturnOptions(phrase,
192                 new String[] { phrase1 }, new String[] { option1 });
193 
194         final ReturnOptions object2 = new ReturnOptions(phrase,
195                 new String[] { phrase1 }, new String[] { option1 });
196 
197         Assert.assertTrue("Equals between two identical objects", object1 //$NON-NLS-1$
198                 .equals(object2));
199     }
200 
201     /**
202      * Tests the equals method with a null value.
203      *
204      * @throws ParameterNullException
205      *             Never.
206      */
207     @Test
208     public void testEqualsObjectNull() throws ParameterNullException {
209         final String phrase = "create table"; //$NON-NLS-1$
210         final String phrase1 = ReturnOptionsTest.TABLESPACE;
211         final String option1 = ReturnOptionsTest.TABLE_NAME;
212 
213         final ReturnOptions object = new ReturnOptions(phrase,
214                 new String[] { phrase1 }, new String[] { option1 });
215 
216         final ReturnOptions nullObject = null;
217 
218         final boolean condition = object.equals(nullObject);
219 
220         Assert.assertFalse("Equals with null value", condition); //$NON-NLS-1$
221     }
222 
223     /**
224      * Tests the array copy of the options.
225      *
226      * @throws ParameterNullException
227      *             Never.
228      */
229     @Test
230     public void testGetOptions() throws ParameterNullException {
231         final String phrase = ReturnOptionsTest.CREATE;
232         final String option1 = ReturnOptionsTest.TABLE;
233         final String option2 = ReturnOptionsTest.TABLESPACE;
234 
235         final ReturnOptions actual = new ReturnOptions(phrase, new String[] {},
236                 new String[] { option1, option2 });
237 
238         final String[] expected = { option1, option2 };
239 
240         Assert.assertEquals("copy of options - length", expected.length, //$NON-NLS-1$
241                 actual.getOptions().length);
242         Assert.assertEquals("copy of options - 0", expected[0], actual //$NON-NLS-1$
243                 .getOptions()[0]);
244         Assert.assertEquals("copy of options - 1", expected[1], actual //$NON-NLS-1$
245                 .getOptions()[1]);
246     }
247 
248     /**
249      * Tests the array copy of the phrases.
250      *
251      * @throws ParameterNullException
252      *             Never.
253      */
254     @Test
255     public void testGetPhrases() throws ParameterNullException {
256         final String phrase = "create tab"; //$NON-NLS-1$
257         final String phrase1 = ReturnOptionsTest.TABLE;
258         final String phrase2 = ReturnOptionsTest.TABLESPACE;
259 
260         final ReturnOptions actual = new ReturnOptions(phrase, new String[] {
261                 phrase1, phrase2 }, new String[] {});
262 
263         final String[] expected = { phrase1, phrase2 };
264 
265         Assert.assertEquals("copy of phrases - length", expected.length, //$NON-NLS-1$
266                 actual.getPhrases().length);
267         Assert.assertEquals("copy of phrases - 0", expected[0], actual //$NON-NLS-1$
268                 .getPhrases()[0]);
269         Assert.assertEquals("copy of phrases - 1", expected[1], actual //$NON-NLS-1$
270                 .getPhrases()[1]);
271     }
272 
273     /**
274      * Tests that the hashcode is different to 0, -1 and 1.
275      *
276      * @throws ParameterNullException
277      *             Never.
278      */
279     @Test
280     public void testHashCode() throws ParameterNullException {
281         final String phrase = "create table"; //$NON-NLS-1$
282         final String phrase1 = ReturnOptionsTest.TABLESPACE;
283         final String option1 = ReturnOptionsTest.TABLE_NAME;
284 
285         final ReturnOptions object = new ReturnOptions(phrase,
286                 new String[] { phrase1 }, new String[] { option1 });
287 
288         Assert.assertTrue("hashcode different to 0", //$NON-NLS-1$
289                 object.hashCode() != 0);
290         Assert.assertTrue("hashcode different to 1", //$NON-NLS-1$
291                 object.hashCode() != 1);
292         Assert.assertTrue("hashcode different to -1", //$NON-NLS-1$
293                 object.hashCode() != -1);
294     }
295 
296     /**
297      * Tests if the hashcode of two different objects are different.
298      *
299      * @throws ParameterNullException
300      *             Never.
301      */
302     @Test
303     public void testHashCodeDifferent() throws ParameterNullException {
304         final String phrase1 = "create table"; //$NON-NLS-1$
305         final String phrase11 = ReturnOptionsTest.TABLESPACE;
306         final String option11 = ReturnOptionsTest.TABLE_NAME;
307 
308         final String phrase2 = ReturnOptionsTest.CREATE;
309         final String option21 = ReturnOptionsTest.TABLE;
310         final String option22 = ReturnOptionsTest.TABLESPACE;
311 
312         final ReturnOptions object1 = new ReturnOptions(phrase1,
313                 new String[] { phrase11 }, new String[] { option11 });
314 
315         final ReturnOptions object2 = new ReturnOptions(phrase2,
316                 new String[] {}, new String[] { option21, option22 });
317 
318         final boolean condition = object1.hashCode() != object2.hashCode();
319 
320         Assert.assertTrue("hashcode of two different objects", //$NON-NLS-1$
321                 condition);
322     }
323 
324     /**
325      * Tests if the hashcode of two identical objects are the same.
326      *
327      * @throws ParameterNullException
328      *             Never.
329      */
330     @Test
331     public void testHashCodeIdentical() throws ParameterNullException {
332         final String phrase = "create table"; //$NON-NLS-1$
333         final String phrase1 = ReturnOptionsTest.TABLESPACE;
334         final String option1 = ReturnOptionsTest.TABLE_NAME;
335 
336         final ReturnOptions object1 = new ReturnOptions(phrase,
337                 new String[] { phrase1 }, new String[] { option1 });
338 
339         final ReturnOptions object2 = new ReturnOptions(phrase,
340                 new String[] { phrase1 }, new String[] { option1 });
341 
342         final boolean condition = object1.hashCode() == object2.hashCode();
343 
344         Assert.assertTrue("hashcode of two identical objects", //$NON-NLS-1$
345                 condition);
346     }
347 
348     /**
349      * Tests the null exception when the options is null.
350      *
351      * @throws ParameterNullException
352      *             Never.
353      */
354     @Test
355     public void testReturnOptionsNullOptions() throws ParameterNullException {
356         final String phrase = "create table"; //$NON-NLS-1$
357         final String phrases = ReturnOptionsTest.TABLESPACE;
358 
359         Exception e = null;
360         try {
361             new ReturnOptions(phrase, new String[] { phrases }, null);
362         } catch (final Exception exception) {
363             e = exception;
364         }
365         Assert.assertTrue("ParameterNullException",
366                 e instanceof ParameterNullException);
367     }
368 
369     /**
370      * Tests the null exception when the phrase is null.
371      *
372      * @throws ParameterNullException
373      *             Never.
374      */
375     @Test
376     public void testReturnOptionsNullPhrase() throws ParameterNullException {
377         final String phrases = ReturnOptionsTest.TABLESPACE;
378         final String options = ReturnOptionsTest.TABLE_NAME;
379 
380         Exception e = null;
381         try {
382             new ReturnOptions(null, new String[] { phrases },
383                     new String[] { options });
384         } catch (final Exception exception) {
385             e = exception;
386         }
387         Assert.assertTrue("ParameterNullException",
388                 e instanceof ParameterNullException);
389     }
390 
391     /**
392      * Tests the null exception when the phrases is null.
393      *
394      * @throws ParameterNullException
395      *             Never.
396      */
397     @Test
398     public void testReturnOptionsNullPhrases() throws ParameterNullException {
399         final String phrase = "create table"; //$NON-NLS-1$
400         final String options = ReturnOptionsTest.TABLE_NAME;
401 
402         Exception e = null;
403         try {
404             new ReturnOptions(phrase, null, new String[] { options });
405         } catch (final Exception exception) {
406             e = exception;
407         }
408         Assert.assertTrue("ParameterNullException",
409                 e instanceof ParameterNullException);
410     }
411 
412     /**
413      * Tests the null exception when one element in the options is null.
414      *
415      * @throws ParameterNullException
416      *             Never.
417      */
418     @Test
419     public void testReturnOptionsOneNullInOptions()
420             throws ParameterNullException {
421         final String phrase = "create table"; //$NON-NLS-1$
422         final String phrases = ReturnOptionsTest.TABLESPACE;
423 
424         Exception e = null;
425         try {
426             new ReturnOptions(phrase, new String[] { phrases },
427                     new String[] { null });
428         } catch (final Exception exception) {
429             e = exception;
430         }
431         Assert.assertTrue("ParameterNullException",
432                 e instanceof ParameterNullException);
433     }
434 
435     /**
436      * Tests the null exception when one element in the phrases is null.
437      *
438      * @throws ParameterNullException
439      *             Never.
440      */
441     @Test
442     public void testReturnOptionsOneNullInPhrases()
443             throws ParameterNullException {
444         final String phrase = "create table"; //$NON-NLS-1$
445         final String options = ReturnOptionsTest.TABLE_NAME;
446 
447         Exception e = null;
448         try {
449             new ReturnOptions(phrase, new String[] { null },
450                     new String[] { options });
451         } catch (final Exception exception) {
452             e = exception;
453         }
454         Assert.assertTrue("ParameterNullException",
455                 e instanceof ParameterNullException);
456     }
457 
458     /**
459      * Tests the symmetry of equals.
460      *
461      * @throws ParameterNullException
462      *             Never.
463      */
464     @Test
465     public void testSymmetric() throws ParameterNullException {
466         final String phrase = "test"; //$NON-NLS-1$
467         final String[] phrases = new String[] { "test1" }; //$NON-NLS-1$
468         final String[] options = new String[] { "test2" }; //$NON-NLS-1$
469         final ReturnOptions actual = new ReturnOptions(phrase, phrases, options);
470 
471         final boolean value = actual.equals(actual);
472 
473         Assert.assertTrue("Symmetry", value); //$NON-NLS-1$
474     }
475 
476     /**
477      * Test the toString method.
478      *
479      * @throws ParameterNullException
480      *             Never.
481      */
482     @Test
483     public void testToString1() throws ParameterNullException {
484         final String phrase = ReturnOptionsTest.CREATE;
485         final String option1 = ReturnOptionsTest.TABLE;
486         final String option2 = ReturnOptionsTest.TABLESPACE;
487 
488         final ReturnOptions returnOptions = new ReturnOptions(phrase,
489                 new String[] {}, new String[] { option1, option2 });
490 
491         final String actual = returnOptions.toString();
492 
493         final String expected = "[create]{[table][tablespace]}"; //$NON-NLS-1$
494 
495         Assert.assertEquals("Method toString", expected, actual); //$NON-NLS-1$
496     }
497 
498     /**
499      * Test the toString method.
500      *
501      * @throws ParameterNullException
502      *             Never.
503      */
504     @Test
505     public void testToString2() throws ParameterNullException {
506         final String phrase = "create table"; //$NON-NLS-1$
507         final String phrase1 = ReturnOptionsTest.TABLESPACE;
508         final String option1 = ReturnOptionsTest.TABLE_NAME;
509 
510         final ReturnOptions returnOptions = new ReturnOptions(phrase,
511                 new String[] { phrase1 }, new String[] { option1 });
512 
513         final String actual = returnOptions.toString();
514 
515         final String expected = "[create table]<(tablespace)>{[<tableName>]}"; //$NON-NLS-1$
516 
517         Assert.assertEquals("Method toString", expected, actual); //$NON-NLS-1$
518     }
519 }