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/uiImplJLine/src/test/java/name/angoca/zemucan/ui/impl/jline/JlineInputReaderTest.java $:
28   */
29  package name.angoca.zemucan.ui.impl.jline;
30  
31  import java.io.ByteArrayOutputStream;
32  import java.io.IOException;
33  import java.io.OutputStreamWriter;
34  import java.io.PrintWriter;
35  import java.util.Iterator;
36  
37  import jline.AssertionHelper;
38  import jline.Buffer;
39  import jline.Completor;
40  import jline.ConsoleOperations;
41  import jline.ConsoleReader;
42  import jline.UnixTerminal;
43  import name.angoca.zemucan.AbstractZemucanException;
44  import name.angoca.zemucan.core.lexical.impl.ImplementationLexicalAnalyzer;
45  import name.angoca.zemucan.grammarReader.api.GrammarReaderController;
46  import name.angoca.zemucan.tools.Constants;
47  import name.angoca.zemucan.tools.configurator.Configurator;
48  import name.angoca.zemucan.tools.test.RandomTestRunner;
49  
50  import org.junit.After;
51  import org.junit.Before;
52  import org.junit.BeforeClass;
53  import org.junit.Test;
54  import org.junit.runner.RunWith;
55  
56  /**
57   * Jline console tests.
58   * <p>
59   * This class is based on the jline tests.
60   * <p>
61   * <b>Control Version</b>
62   * <p>
63   * <ul>
64   * <li>1.0.0 Class creation.</li>
65   * <li>1.1.0 Same tests as lexical analyzer with real grammar.</li>
66   * <li>1.1.1 Destroy in after instance.</li>
67   * </ul>
68   *
69   * @author Andres Gomez Casanova <a
70   *         href="mailto:a n g o c a at y a h o o dot c o m" >(AngocA)</a>
71   * @version 1.1.1 2010-09-25
72   */
73  @RunWith(RandomTestRunner.class)
74  public final class JlineInputReaderTest {
75      /**
76       * Establishes the configuration for the tests. This configuration is before
77       * all the tests.
78       *
79       * @throws AbstractZemucanException
80       *             Never.
81       */
82      @BeforeClass
83      public static void setUpBeforeClass() throws AbstractZemucanException {
84          System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
85                  "sa_conf-test-realGrammar.xml"); //$NON-NLS-1$
86          Configurator.getInstance().setProperty(
87                  Constants.GRAMMAR_READER_NAME_PROPERTY,
88                  Configurator.GRAMMAR_READER_NAME_VALUE);
89  
90          // Creates the Lexical analyzer instance.
91          ImplementationLexicalAnalyzer.getInstance();
92      }
93  
94      /**
95       * Configuration before each test.
96       *
97       * @throws IOException
98       *             If there is a problem when initializing the console.
99       */
100     @Before
101     public void setUp() throws IOException {
102         AssertionHelper.setConsole(new ConsoleReader(null, new PrintWriter(
103                 new OutputStreamWriter(new ByteArrayOutputStream())), null,
104                 new UnixTerminal()));
105 
106         // Clear any current completors
107         for (@SuppressWarnings("unchecked")
108         final Iterator<Completor> iterator = AssertionHelper.getConsole()
109                 .getCompletors().iterator(); iterator.hasNext();) {
110             final Completor type = iterator.next();
111             AssertionHelper.getConsole().removeCompletor(type);
112         }
113 
114         // Configures jLine to use the specific completor and
115         // completionHandler.
116         AssertionHelper.getConsole().addCompletor(new ZemucanCompletor());
117         AssertionHelper.getConsole().setCompletionHandler(
118                 new ZemucanCompletionHandler());
119         // FIXME v1.1 test this with variable value.
120         AssertionHelper.getConsole().setAutoprintThreshhold(50);
121     }
122 
123     /**
124      * Destroys the singletons.
125      */
126     @After
127     public void tearDown() {
128         // Destroy any existent instance.
129         ImplementationLexicalAnalyzer.destroyInstance();
130 
131         // Recreates the configurator instance.
132         Configurator.destroyInstance();
133 
134         GrammarReaderController.destroyInstance();
135     }
136 
137     /**
138      * Tests an empty string.
139      *
140      * @throws IOException
141      *             Never.
142      */
143     @Test
144     public void test1() throws IOException {
145         final Buffer b = new Buffer("");
146         b.op(ConsoleOperations.COMPLETE);
147 
148         AssertionHelper.assertBuffer("empty", "", b);
149     }
150 
151     /**
152      * Tests the command 'db2 create'
153      *
154      * @throws IOException
155      *             Never
156      */
157     @Test
158     public void test10() throws IOException {
159         final String command = "db2 create";
160         final Buffer b = new Buffer(command);
161         b.op(ConsoleOperations.COMPLETE);
162 
163         AssertionHelper.assertBuffer(command, "db2 create table", b);
164     }
165 
166     /**
167      * Tests the command ' db2 create'
168      *
169      * @throws IOException
170      *             Never
171      */
172     @Test
173     public void test10a() throws IOException {
174         final String command = "  db2   create";
175         final Buffer b = new Buffer(command);
176         b.op(ConsoleOperations.COMPLETE);
177 
178         AssertionHelper.assertBuffer(command, "  db2   create table", b);
179     }
180 
181     /**
182      * Tests the command 'db2 create '
183      *
184      * @throws IOException
185      *             Never
186      */
187     // @Test TODO v1.1 Varias opciones que comienzan con la misma
188     // palabra y todas son reservadas, entonces autocompletar.
189     public void test11() throws IOException {
190         final String command = "db2 create ";
191         final Buffer b = new Buffer(command);
192         b.op(ConsoleOperations.COMPLETE);
193 
194         AssertionHelper.assertBuffer(command, "db2 create table", b);
195     }
196 
197     /**
198      * Tests the command 'db2 create t'
199      *
200      * @throws IOException
201      *             Never
202      */
203     @Test
204     public void test12() throws IOException {
205         final String command = "db2 create t";
206         final Buffer b = new Buffer(command);
207         b.op(ConsoleOperations.COMPLETE);
208 
209         AssertionHelper.assertBuffer(command, "db2 create table", b);
210     }
211 
212     /**
213      * Tests the command 'db2 create tables'
214      *
215      * @throws IOException
216      *             Never
217      */
218     @Test
219     public void test13() throws IOException {
220         final String command = "db2 create tables";
221         final Buffer b = new Buffer(command);
222         b.op(ConsoleOperations.COMPLETE);
223 
224         AssertionHelper.assertBuffer(command, "db2 create tablespace", b);
225     }
226 
227     /**
228      * Tests the command 'db2 create tablespace'
229      *
230      * @throws IOException
231      *             Never
232      */
233     @Test
234     public void test14() throws IOException {
235         final String command = "db2 create tablespace";
236         final Buffer b = new Buffer(command);
237         b.op(ConsoleOperations.COMPLETE);
238 
239         AssertionHelper.assertBuffer(command, "db2 create tablespace", b);
240     }
241 
242     /**
243      * Tests the command 'db2 create ta'
244      *
245      * @throws IOException
246      *             Never
247      */
248     @Test
249     public void test15() throws IOException {
250         final String command = "db2 create ta";
251         final Buffer b = new Buffer(command);
252         b.op(ConsoleOperations.COMPLETE);
253 
254         AssertionHelper.assertBuffer(command, "db2 create table", b);
255     }
256 
257     /**
258      * Tests the command 'db2 create table'
259      *
260      * @throws IOException
261      *             Never
262      */
263     @Test
264     public void test16() throws IOException {
265         final String command = "db2 create table";
266         final Buffer b = new Buffer(command);
267         b.op(ConsoleOperations.COMPLETE);
268 
269         AssertionHelper.assertBuffer(command, "db2 create table", b);
270     }
271 
272     /**
273      * Tests the command 'db2 create table '
274      *
275      * @throws IOException
276      *             Never
277      */
278     @Test
279     public void test17() throws IOException {
280         final String command = "db2 create table ";
281         final Buffer b = new Buffer(command);
282         b.op(ConsoleOperations.COMPLETE);
283 
284         AssertionHelper.assertBuffer(command, "db2 create table ", b);
285     }
286 
287     /**
288      * Tests the command 'db2 create table t1'
289      *
290      * @throws IOException
291      *             Never
292      */
293     @Test
294     public void test18() throws IOException {
295         final String command = "db2 create table t1";
296         final Buffer b = new Buffer(command);
297         b.op(ConsoleOperations.COMPLETE);
298 
299         AssertionHelper.assertBuffer(command, "db2 create table t1 (", b);
300     }
301 
302     /**
303      * Tests the command 'db2 create table t1 '
304      *
305      * @throws IOException
306      *             Never
307      */
308     @Test
309     public void test19() throws IOException {
310         final String command = "db2 create table t1 ";
311         final Buffer b = new Buffer(command);
312         b.op(ConsoleOperations.COMPLETE);
313 
314         AssertionHelper.assertBuffer(command, "db2 create table t1 (", b);
315     }
316 
317     /**
318      * Tests the command 'd'
319      *
320      * @throws IOException
321      *             Never
322      */
323     @Test
324     public void test2() throws IOException {
325         final String command = "d";
326         final Buffer b = new Buffer(command);
327         b.op(ConsoleOperations.COMPLETE);
328 
329         AssertionHelper.assertBuffer(command, "db2", b);
330     }
331 
332     /**
333      * Tests the command 'db2 create table t1 ('
334      *
335      * @throws IOException
336      *             Never
337      */
338     @Test
339     public void test20() throws IOException {
340         final String command = "db2 create table t1 (";
341         final Buffer b = new Buffer(command);
342         b.op(ConsoleOperations.COMPLETE);
343 
344         AssertionHelper.assertBuffer(command, "db2 create table t1 ( ", b);
345     }
346 
347     /**
348      * Tests the command 'db2 create table t1('
349      *
350      * @throws IOException
351      *             Never
352      */
353     @Test
354     public void test21() throws IOException {
355         final String command = "db2 create table t1(";
356         final Buffer b = new Buffer(command);
357         b.op(ConsoleOperations.COMPLETE);
358 
359         AssertionHelper.assertBuffer(command, "db2 create table t1( ", b);
360     }
361 
362     /**
363      * Tests the command 'db2 create table table t1(c1'
364      *
365      * @throws IOException
366      *             Never
367      */
368     @Test
369     public void test22() throws IOException {
370         final String command = "db2 create table t1(c1";
371         final Buffer b = new Buffer(command);
372         b.op(ConsoleOperations.COMPLETE);
373 
374         AssertionHelper.assertBuffer(command, "db2 create table t1(c1 ", b);
375     }
376 
377     /**
378      * Tests the command 'db2 create table table t1( c1'
379      *
380      * @throws IOException
381      *             Never
382      */
383     @Test
384     public void test23() throws IOException {
385         final String command = "db2 create table t1( c1";
386         final Buffer b = new Buffer(command);
387         b.op(ConsoleOperations.COMPLETE);
388 
389         AssertionHelper.assertBuffer(command, "db2 create table t1( c1 ", b);
390     }
391 
392     /**
393      * Tests the command 'db2 create table table t1 ( c1 int )'
394      *
395      * @throws IOException
396      *             Never
397      */
398     @Test
399     public void test24() throws IOException {
400         final String command = "db2 create table t1 ( c1 int )";
401         final Buffer b = new Buffer(command);
402         b.op(ConsoleOperations.COMPLETE);
403 
404         AssertionHelper.assertBuffer(command, "db2 create table t1 ( c1 int )",
405                 b);
406     }
407 
408     /**
409      * Tests the command ' db2'
410      *
411      * @throws IOException
412      *             Never
413      */
414     @Test
415     public void test25() throws IOException {
416         final String command = " db2";
417         final Buffer b = new Buffer(command);
418         b.op(ConsoleOperations.COMPLETE);
419 
420         AssertionHelper.assertBuffer(command, " db2", b);
421     }
422 
423     /**
424      * Tests the command ' db2'
425      *
426      * @throws IOException
427      *             Never
428      */
429     @Test
430     public void test25a() throws IOException {
431         final String command = "  db2";
432         final Buffer b = new Buffer(command);
433         b.op(ConsoleOperations.COMPLETE);
434 
435         AssertionHelper.assertBuffer(command, "  db2", b);
436     }
437 
438     /**
439      * Tests the command 'DB'
440      *
441      * @throws IOException
442      *             Never
443      */
444     @Test
445     public void test26() throws IOException {
446         final String command = "DB";
447         final Buffer b = new Buffer(command);
448         b.op(ConsoleOperations.COMPLETE);
449 
450         AssertionHelper.assertBuffer(command, "DB2", b);
451     }
452 
453     /**
454      * Tests the command ' DB'
455      *
456      * @throws IOException
457      *             Never
458      */
459     @Test
460     public void test26a() throws IOException {
461         final String command = "  DB";
462         final Buffer b = new Buffer(command);
463         b.op(ConsoleOperations.COMPLETE);
464 
465         AssertionHelper.assertBuffer(command, "  DB2", b);
466     }
467 
468     /**
469      * Tests the command 'DB CREA'
470      *
471      * @throws IOException
472      *             Never
473      */
474     @Test
475     public void test27() throws IOException {
476         final String command = "DB2 CREA";
477         final Buffer b = new Buffer(command);
478         b.op(ConsoleOperations.COMPLETE);
479 
480         AssertionHelper.assertBuffer(command, "DB2 CREAte", b);
481     }
482 
483     /**
484      * Tests the command ' DB CREA'
485      *
486      * @throws IOException
487      *             Never
488      */
489     @Test
490     public void test27a() throws IOException {
491         final String command = "  DB2  CREA";
492         final Buffer b = new Buffer(command);
493         b.op(ConsoleOperations.COMPLETE);
494 
495         AssertionHelper.assertBuffer(command, "  DB2  CREAte", b);
496     }
497 
498     /**
499      * Tests the command 'db'
500      *
501      * @throws IOException
502      *             Never
503      */
504     @Test
505     public void test3() throws IOException {
506         final String command = "db";
507         final Buffer b = new Buffer(command);
508         b.op(ConsoleOperations.COMPLETE);
509 
510         AssertionHelper.assertBuffer(command, "db2", b);
511     }
512 
513     /**
514      * Tests the command 'db2'
515      *
516      * @throws IOException
517      *             Never
518      */
519     @Test
520     public void test4() throws IOException {
521         final String command = "db2";
522         final Buffer b = new Buffer(command);
523         b.op(ConsoleOperations.COMPLETE);
524 
525         AssertionHelper.assertBuffer(command, "db2", b);
526     }
527 
528     /**
529      * Tests the command 'db2 '
530      *
531      * @throws IOException
532      *             Never
533      */
534     @Test
535     public void test5() throws IOException {
536         final String command = "db2 ";
537         final Buffer b = new Buffer(command);
538         b.op(ConsoleOperations.COMPLETE);
539 
540         AssertionHelper.assertBuffer(command, "db2 ", b);
541     }
542 
543     /**
544      * Tests the command 'db2 '
545      *
546      * @throws IOException
547      *             Never
548      */
549     @Test
550     public void test5a() throws IOException {
551         final String command = "db2  ";
552         final Buffer b = new Buffer(command);
553         b.op(ConsoleOperations.COMPLETE);
554 
555         AssertionHelper.assertBuffer(command, "db2  ", b);
556     }
557 
558     /**
559      * Tests the command 'db2 a'
560      *
561      * @throws IOException
562      *             Never
563      */
564     @Test
565     public void test6() throws IOException {
566         final String command = "db2 a";
567         final Buffer b = new Buffer(command);
568         b.op(ConsoleOperations.COMPLETE);
569 
570         AssertionHelper.assertBuffer(command, "db2 attach", b);
571     }
572 
573     /**
574      * Tests the command 'db2 c'
575      *
576      * @throws IOException
577      *             Never
578      */
579     @Test
580     public void test6a() throws IOException {
581         final String command = "db2 c";
582         final Buffer b = new Buffer(command);
583         b.op(ConsoleOperations.COMPLETE);
584 
585         AssertionHelper.assertBuffer(command, "db2 c", b);
586     }
587 
588     /**
589      * Tests the command 'db2 cr'
590      *
591      * @throws IOException
592      *             Never
593      */
594     @Test
595     public void test7() throws IOException {
596         final String command = "db2 cr";
597         final Buffer b = new Buffer(command);
598         b.op(ConsoleOperations.COMPLETE);
599 
600         AssertionHelper.assertBuffer(command, "db2 create", b);
601     }
602 
603     /**
604      * Tests the command 'db2 crea'
605      *
606      * @throws IOException
607      *             Never
608      */
609     @Test
610     public void test8() throws IOException {
611         final String command = "db2 crea";
612         final Buffer b = new Buffer(command);
613         b.op(ConsoleOperations.COMPLETE);
614 
615         AssertionHelper.assertBuffer(command, "db2 create", b);
616     }
617 
618     /**
619      * Tests the command ' db2 crea'
620      *
621      * @throws IOException
622      *             Never
623      */
624     @Test
625     public void test8a() throws IOException {
626         final String command = "  db2   crea";
627         final Buffer b = new Buffer(command);
628         b.op(ConsoleOperations.COMPLETE);
629 
630         AssertionHelper.assertBuffer(command, "  db2   create", b);
631     }
632 
633     /**
634      * Tests the command 'db2 crea '
635      *
636      * @throws IOException
637      *             Never
638      */
639     @Test
640     public void test9() throws IOException {
641         final String command = "db2 crea ";
642         final Buffer b = new Buffer(command);
643         b.op(ConsoleOperations.COMPLETE);
644 
645         AssertionHelper.assertBuffer(command, "db2 crea ", b);
646     }
647 }