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 23:10:20 -0500 (dom, 06 mar 2011) $:
26   * Revision: $LastChangedRevision: 1917 $:
27   * URL:      $HeadURL: https://zemucan.svn.sourceforge.net/svnroot/zemucan/branches/zemucan_v1/source-code/grammarReaderApi/src/test/java/name/angoca/zemucan/grammarReader/api/GrammarReaderControllerTest.java $:
28   */
29  package name.angoca.zemucan.grammarReader.api;
30  
31  import name.angoca.zemucan.AbstractZemucanException;
32  import name.angoca.zemucan.core.graph.model.StartingNode;
33  import name.angoca.zemucan.tools.Constants;
34  import name.angoca.zemucan.tools.configurator.Configurator;
35  import name.angoca.zemucan.tools.test.RandomTestRunner;
36  
37  import org.junit.After;
38  import org.junit.Assert;
39  import org.junit.Test;
40  import org.junit.runner.RunWith;
41  
42  /**
43   * Set of test of the grammar reader controller.
44   * <p>
45   * <b>Control Version</b>
46   * <p>
47   * <ul>
48   * <li>1.0.0 Class creation.</li>
49   * <li>1.1.0 Grammar file tests.</li>
50   * <li>1.1.1 Specific exception.</li>
51   * <li>1.2.0 More tests of file names.</li>
52   * <li>1.2.1 GraphNode Hierarchy.</li>
53   * <li>1.2.2 After method instead of before.</li>
54   * </ul>
55   *
56   * @author Andres Gomez Casanova <a
57   *         href="mailto:a n g o c a at y a h o o dot c o m" >(AngocA)</a>
58   * @version 1.2.2 2010-09-25
59   */
60  @RunWith(RandomTestRunner.class)
61  public class GrammarReaderControllerTest {
62      /**
63       * Implementation in memory class name.
64       */
65      private static final String IN_MEMORY_GRAMMAR_READER = "name.angoca.zemucan.grammarReader.api.ImplementationInMemoryGrammarReader"; //$NON-NLS-1$
66  
67      /**
68       * Destroys the configurator and grammar reader instances.
69       */
70      @After
71      public void tearDown() {
72          // Destroys the configurator instance.
73          Configurator.destroyInstance();
74  
75          // Destroys the GrammarReaderController instance.
76          GrammarReaderController.destroyInstance();
77      }
78  
79      /**
80       * Test an abstract bad constructor for the grammar reader.
81       *
82       * @throws AbstractZemucanException
83       *             Never.
84       */
85      @Test
86      public final void testAbstractBadConstructorGrammar()
87              throws AbstractZemucanException {
88          Configurator
89                  .getInstance()
90                  .setProperty(Constants.GRAMMAR_READER_NAME_PROPERTY,
91                          "name.angoca.zemucan.grammarReader.api.AbstractBadConstructorGrammarReader"); //$NON-NLS-1$
92  
93          try {
94              GrammarReaderController.getInstance();
95          } catch (final Exception exception) {
96              Assert.assertTrue("GeneralGrammarReaderProblemException",
97                      exception instanceof GeneralGrammarReaderProblemException);
98          }
99      }
100 
101     /**
102      * Test a bad constructor for the grammar reader.
103      *
104      * @throws AbstractZemucanException
105      *             Never.
106      */
107     @Test
108     public final void testBadConstructorGrammar() throws AbstractZemucanException {
109         Configurator
110                 .getInstance()
111                 .setProperty(Constants.GRAMMAR_READER_NAME_PROPERTY,
112                         "name.angoca.zemucan.grammarReader.api.ImplementationBadConstructorGrammarReader"); //$NON-NLS-1$
113 
114         try {
115             GrammarReaderController.getInstance();
116         } catch (final Exception exception) {
117             Assert.assertTrue("GeneralGrammarReaderProblemException",
118                     exception instanceof GeneralGrammarReaderProblemException);
119         }
120     }
121 
122     /**
123      * Tests that the grammar reader name cannot be empty.
124      *
125      * @throws AbstractZemucanException
126      *             Never
127      */
128     @Test
129     public final void testEmptyValue() throws AbstractZemucanException {
130         System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
131                 "sa_conf-test-NoGrammarReaderDefined.xml"); //$NON-NLS-1$
132 
133         try {
134             GrammarReaderController.getInstance();
135         } catch (final Exception exception) {
136             Assert.assertTrue("GrammarReaderNotDefinedException",
137                     exception instanceof GrammarReaderNotDefinedException);
138         }
139     }
140 
141     /**
142      * Tests to read the files with extra commas.
143      *
144      * @throws AbstractZemucanException
145      *             Never.
146      */
147     @Test
148     public final void testExtraCommas1() throws AbstractZemucanException {
149         Configurator.getInstance().setProperty(
150                 Constants.GRAMMAR_READER_NAME_PROPERTY,
151                 GrammarReaderControllerTest.IN_MEMORY_GRAMMAR_READER);
152         Configurator.getInstance().setProperty(
153                 Constants.GRAMMAR_FILE_DESCRIPTORS_PROPERTY,
154                 "sa_conf-test-grammar1.xml;sa_conf-test-grammar2.xml;"); //$NON-NLS-1$
155 
156         final StartingNode node = GrammarReaderController.getInstance()
157                 .getStartingNode();
158 
159         Assert.assertTrue("Extra commas", node != null);
160     }
161 
162     /**
163      * Tests to read the files with extra commas.
164      *
165      * @throws AbstractZemucanException
166      *             Never.
167      */
168     @Test
169     public final void testExtraCommas2() throws AbstractZemucanException {
170         Configurator.getInstance().setProperty(
171                 Constants.GRAMMAR_READER_NAME_PROPERTY,
172                 GrammarReaderControllerTest.IN_MEMORY_GRAMMAR_READER);
173         Configurator.getInstance().setProperty(
174                 Constants.GRAMMAR_FILE_DESCRIPTORS_PROPERTY,
175                 "sa_conf-test-grammar1.xml;;sa_conf-test-grammar2.xml"); //$NON-NLS-1$
176 
177         final StartingNode node = GrammarReaderController.getInstance()
178                 .getStartingNode();
179 
180         Assert.assertTrue("Extra commas", node != null);
181     }
182 
183     /**
184      * Tests to read the files with extra commas.
185      *
186      * @throws AbstractZemucanException
187      *             Never.
188      */
189     @Test
190     public final void testExtraCommas3() throws AbstractZemucanException {
191         Configurator.getInstance().setProperty(
192                 Constants.GRAMMAR_READER_NAME_PROPERTY,
193                 GrammarReaderControllerTest.IN_MEMORY_GRAMMAR_READER);
194         Configurator.getInstance().setProperty(
195                 Constants.GRAMMAR_FILE_DESCRIPTORS_PROPERTY,
196                 ";sa_conf-test-grammar1.xml;sa_conf-test-grammar2.xml"); //$NON-NLS-1$
197 
198         final StartingNode node = GrammarReaderController.getInstance()
199                 .getStartingNode();
200 
201         Assert.assertTrue("Extra commas", node != null);
202     }
203 
204     /**
205      * Tests to read the files with extra spaces.
206      *
207      * @throws AbstractZemucanException
208      *             Never.
209      */
210     @Test
211     public final void testExtraSpaces1() throws AbstractZemucanException {
212         Configurator.getInstance().setProperty(
213                 Constants.GRAMMAR_READER_NAME_PROPERTY,
214                 GrammarReaderControllerTest.IN_MEMORY_GRAMMAR_READER);
215         Configurator.getInstance().setProperty(
216                 Constants.GRAMMAR_FILE_DESCRIPTORS_PROPERTY,
217                 "sa_conf-test-grammar1.xml "); //$NON-NLS-1$
218 
219         try {
220             GrammarReaderController.getInstance().getStartingNode();
221         } catch (final Exception exception) {
222             Assert.assertTrue("GeneralGrammarReaderProblemException",
223                     exception instanceof GeneralGrammarReaderProblemException);
224         }
225     }
226 
227     /**
228      * Tests to read the files with extra spaces.
229      *
230      * @throws AbstractZemucanException
231      *             Never.
232      */
233     @Test
234     public final void testExtraSpaces2() throws AbstractZemucanException {
235         Configurator.getInstance().setProperty(
236                 Constants.GRAMMAR_READER_NAME_PROPERTY,
237                 GrammarReaderControllerTest.IN_MEMORY_GRAMMAR_READER);
238         Configurator.getInstance().setProperty(
239                 Constants.GRAMMAR_FILE_DESCRIPTORS_PROPERTY,
240                 "sa_conf-test-grammar1.xml ;sa_conf-test-grammar2.xml"); //$NON-NLS-1$
241 
242         try {
243             GrammarReaderController.getInstance().getStartingNode();
244         } catch (final Exception exception) {
245             Assert.assertTrue("GeneralGrammarReaderProblemException",
246                     exception instanceof GeneralGrammarReaderProblemException);
247         }
248     }
249 
250     /**
251      * Tests to read the files with extra spaces.
252      *
253      * @throws AbstractZemucanException
254      *             Never.
255      */
256     @Test
257     public final void testExtraSpaces3() throws AbstractZemucanException {
258         Configurator.getInstance().setProperty(
259                 Constants.GRAMMAR_READER_NAME_PROPERTY,
260                 GrammarReaderControllerTest.IN_MEMORY_GRAMMAR_READER);
261         Configurator.getInstance().setProperty(
262                 Constants.GRAMMAR_FILE_DESCRIPTORS_PROPERTY,
263                 "sa_conf-test-grammar1.xml; sa_conf-test-grammar2.xml"); //$NON-NLS-1$
264 
265         try {
266             GrammarReaderController.getInstance().getStartingNode();
267         } catch (final Exception exception) {
268             Assert.assertTrue("GeneralGrammarReaderProblemException",
269                     exception instanceof GeneralGrammarReaderProblemException);
270         }
271     }
272 
273     /**
274      * Tests to read the files with extra spaces.
275      *
276      * @throws AbstractZemucanException
277      *             Never.
278      */
279     @Test
280     public final void testExtraSpaces4() throws AbstractZemucanException {
281         Configurator.getInstance().setProperty(
282                 Constants.GRAMMAR_READER_NAME_PROPERTY,
283                 GrammarReaderControllerTest.IN_MEMORY_GRAMMAR_READER);
284         Configurator.getInstance().setProperty(
285                 Constants.GRAMMAR_FILE_DESCRIPTORS_PROPERTY,
286                 "sa_conf-test-grammar1.xml ; sa_conf-test-grammar2.xml"); //$NON-NLS-1$
287 
288         try {
289             GrammarReaderController.getInstance().getStartingNode();
290         } catch (final Exception exception) {
291             Assert.assertTrue("GeneralGrammarReaderProblemException",
292                     exception instanceof GeneralGrammarReaderProblemException);
293         }
294     }
295 
296     /**
297      * Test an invalid value for the grammar reader.
298      *
299      * @throws AbstractZemucanException
300      *             Never.
301      */
302     @Test
303     public final void testInexistantGrammar() throws AbstractZemucanException {
304         Configurator.getInstance().setProperty(
305                 Constants.GRAMMAR_READER_NAME_PROPERTY, "Inexistant"); //$NON-NLS-1$
306 
307         try {
308             GrammarReaderController.getInstance();
309         } catch (final Exception exception) {
310             Assert.assertTrue("GeneralGrammarReaderProblemException",
311                     exception instanceof GeneralGrammarReaderProblemException);
312         }
313     }
314 
315     /**
316      * Tests that the grammar reader name cannot be empty.
317      *
318      * @throws AbstractZemucanException
319      *             Never
320      */
321     @Test
322     public final void testInvalidValue() throws AbstractZemucanException {
323         System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
324                 "sa_conf-test-InvalidGrammarReaderDefined.xml"); //$NON-NLS-1$
325 
326         try {
327             GrammarReaderController.getInstance();
328         } catch (final Exception exception) {
329             Assert.assertTrue("GeneralGrammarReaderProblemException",
330                     exception instanceof GeneralGrammarReaderProblemException);
331         }
332     }
333 
334     /**
335      * Tests that the grammar reader name cannot be null.
336      *
337      * @throws AbstractZemucanException
338      *             Never
339      */
340     @Test
341     public final void testNullValue() throws AbstractZemucanException {
342         System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
343                 "sa_conf-test-NoGrammarReader.xml"); //$NON-NLS-1$
344 
345         try {
346             GrammarReaderController.getInstance();
347         } catch (final Exception exception) {
348             Assert.assertTrue("GrammarReaderNotDefinedException",
349                     exception instanceof GrammarReaderNotDefinedException);
350         }
351     }
352 
353     /**
354      * Tests the merge of the same file twice.
355      *
356      * @throws AbstractZemucanException
357      *             Never.
358      */
359     @Test(expected = AssertionError.class)
360     public final void testSameGrammarFileTwice() throws AbstractZemucanException {
361         Configurator.getInstance().setProperty(
362                 Constants.GRAMMAR_READER_NAME_PROPERTY,
363                 GrammarReaderControllerTest.IN_MEMORY_GRAMMAR_READER);
364         Configurator.getInstance().setProperty(
365                 Constants.GRAMMAR_FILE_DESCRIPTORS_PROPERTY,
366                 "sa_conf-test-grammar1.xml;sa_conf-test-grammar1.xml"); //$NON-NLS-1$
367 
368         GrammarReaderController.getInstance();
369     }
370 
371     /**
372      * Tests the merge of two different grammar files.
373      *
374      * @throws AbstractZemucanException
375      *             Never.
376      */
377     @Test
378     public final void testTwoDifferentFileGrammars()
379             throws AbstractZemucanException {
380         Configurator.getInstance().setProperty(
381                 Constants.GRAMMAR_READER_NAME_PROPERTY,
382                 GrammarReaderControllerTest.IN_MEMORY_GRAMMAR_READER);
383         Configurator.getInstance().setProperty(
384                 Constants.GRAMMAR_FILE_DESCRIPTORS_PROPERTY,
385                 "sa_conf-test-grammar1.xml;sa_conf-test-grammar2.xml"); //$NON-NLS-1$
386 
387         final StartingNode node = GrammarReaderController.getInstance()
388                 .getStartingNode();
389 
390         Assert.assertTrue("Two different file grammars", node != null);
391     }
392 
393     /**
394      * Tests the merge of two different files containing the same grammar.
395      *
396      * @throws AbstractZemucanException
397      *             Never.
398      */
399     @Test
400     public final void testTwoDifferentFileIdenticalGrammars()
401             throws AbstractZemucanException {
402         Configurator.getInstance().setProperty(
403                 Constants.GRAMMAR_READER_NAME_PROPERTY,
404                 GrammarReaderControllerTest.IN_MEMORY_GRAMMAR_READER);
405         Configurator
406                 .getInstance()
407                 .setProperty(Constants.GRAMMAR_FILE_DESCRIPTORS_PROPERTY,
408                         "sa_conf-test-grammarIdentical1.xml;sa_conf-test-grammarIdentical2.xml"); //$NON-NLS-1$
409 
410         final StartingNode node = GrammarReaderController.getInstance()
411                 .getStartingNode();
412 
413         Assert.assertTrue("Two different file grammars with identical grammar",
414                 node != null);
415     }
416 
417     /**
418      * Try to load an unrecognized grammar.
419      *
420      * @throws AbstractZemucanException
421      *             Never.
422      */
423     @Test
424     public final void testUnrecognizedGrammar() throws AbstractZemucanException {
425         Configurator.getInstance().setProperty(
426                 Constants.GRAMMAR_READER_NAME_PROPERTY,
427                 GrammarReaderControllerTest.IN_MEMORY_GRAMMAR_READER);
428         Configurator.getInstance()
429                 .setProperty(Constants.GRAMMAR_FILE_DESCRIPTORS_PROPERTY,
430                         "unrecognized.xml"); //$NON-NLS-1$
431 
432         try {
433             GrammarReaderController.getInstance();
434         } catch (final Exception exception) {
435             Assert.assertTrue("GeneralGrammarReaderProblemException",
436                     exception instanceof GeneralGrammarReaderProblemException);
437         }
438     }
439 }