1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package name.angoca.zemucan.core.lexical.impl;
30
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import name.angoca.zemucan.AbstractZemucanException;
35 import name.angoca.zemucan.core.lexical.model.Token;
36 import name.angoca.zemucan.grammarReader.api.GrammarReaderController;
37 import name.angoca.zemucan.interfaze.model.ReturnOptions;
38 import name.angoca.zemucan.tools.Constants;
39 import name.angoca.zemucan.tools.configurator.Configurator;
40 import name.angoca.zemucan.tools.test.RandomTestRunner;
41
42 import org.junit.AfterClass;
43 import org.junit.Assert;
44 import org.junit.BeforeClass;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 @RunWith(RandomTestRunner.class)
87 public final class ImplementationLexicalAnalyzerTest {
88
89
90
91 private static final String CLOSE_PARENTHESIS = ")";
92
93
94
95
96 private static final String COL_NAME = "<colName>";
97
98
99
100
101 private static final String CREATE = "create";
102
103
104
105
106 private static final String CREATE_TAB = "create tab";
107
108
109
110 private static final String SPACE = " ";
111
112
113
114
115
116
117
118 @BeforeClass
119 public static void setUpBeforeClass() throws AbstractZemucanException {
120 System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
121 "sa_conf-test.xml");
122 Configurator.getInstance().setProperty(
123 Constants.GRAMMAR_READER_NAME_PROPERTY,
124 Configurator.GRAMMAR_READER_NAME_VALUE);
125 Configurator.getInstance()
126 .setProperty(Constants.GRAMMAR_FILE_DESCRIPTORS_PROPERTY,
127 "grammar-test.xml");
128
129
130 ImplementationLexicalAnalyzer.getInstance();
131 }
132
133
134
135
136 @AfterClass
137 public static void tearDownAfterClass() {
138
139 ImplementationLexicalAnalyzer.destroyInstance();
140 GrammarReaderController.destroyInstance();
141
142 Configurator.destroyInstance();
143 System.clearProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY);
144 }
145
146
147
148
149 public ImplementationLexicalAnalyzerTest() {
150
151 }
152
153
154
155
156
157
158
159
160 @Test
161 public void testCloseFirstParenthesis() throws AbstractZemucanException {
162 final String cmdIn = "create table t1 (c1 int, c2 char(10";
163
164 final String cmdOut = "create table t1 (c1 int, c2 char(10 )";
165
166 final ReturnOptions actual = ImplementationLexicalAnalyzer
167 .getInstance().analyzePhrase(cmdIn);
168
169 final ReturnOptions expected = new ReturnOptions(cmdOut,
170 new String[] {}, new String[] {});
171
172 Assert.assertEquals("object for 'create table t1 (c1 int, c2 char(10'",
173 expected, actual);
174 }
175
176
177
178
179
180
181
182 @Test
183 public void testComma() throws AbstractZemucanException {
184 final String cmdIn = "create table t2(c1 int";
185
186 final String cmdOut = "create table t2(c1 int ";
187
188 final String option1 = ",";
189 final String option2 = ImplementationLexicalAnalyzerTest.CLOSE_PARENTHESIS;
190
191 final ReturnOptions actual = ImplementationLexicalAnalyzer
192 .getInstance().analyzePhrase(cmdIn);
193
194 final ReturnOptions expected = new ReturnOptions(cmdOut,
195 new String[] {}, new String[] { option1, option2 });
196
197 Assert.assertEquals("object for 'create table t2(c1 int'",
198 expected, actual);
199 }
200
201
202
203
204
205
206
207 @Test
208 public void testConvertionCrea() throws AbstractZemucanException {
209 final String phraseIn = "crea";
210
211 final String phraseOut = ImplementationLexicalAnalyzerTest.CREATE;
212
213 final String token1 = "crea";
214 final List<Token> tokens = new ArrayList<Token>();
215 tokens.add(new Token(token1, true));
216
217 final String actual = ImplementationLexicalAnalyzer.getInstance()
218 .replaceLastToken(phraseIn, tokens,
219 ImplementationLexicalAnalyzerTest.CREATE);
220
221 final String expected = phraseOut;
222
223 Assert.assertEquals("conversion for 'crea'", expected, actual);
224 }
225
226
227
228
229
230
231
232 @Test
233 public void testConvertionCreateTab() throws AbstractZemucanException {
234 final String phraseIn = ImplementationLexicalAnalyzerTest.CREATE_TAB;
235
236 final String phraseOut = ImplementationLexicalAnalyzerTest.CREATE_TAB;
237
238 final String token1 = ImplementationLexicalAnalyzerTest.CREATE;
239 final String token2 = "tab";
240 final List<Token> tokens = new ArrayList<Token>();
241 tokens.add(new Token(token1, true));
242 tokens.add(new Token(token2, true));
243
244 final String actual = ImplementationLexicalAnalyzer.getInstance()
245 .replaceLastToken(phraseIn, tokens, "tab");
246
247 final String expected = phraseOut;
248
249 Assert.assertEquals("conversion for 'create tab'", expected,
250 actual);
251 }
252
253
254
255
256
257
258
259 @Test
260 public void testCreaPhrase1() throws AbstractZemucanException {
261 final String cmdIn = "crea";
262
263 final String cmdOut = ImplementationLexicalAnalyzerTest.CREATE;
264
265 final ReturnOptions ret = ImplementationLexicalAnalyzer.getInstance()
266 .analyzePhrase(cmdIn);
267 final String actual = ret.getPhrase();
268
269 final String expected = cmdOut;
270
271 Assert.assertEquals("complete for 'crea'", expected, actual);
272 }
273
274
275
276
277
278
279
280 @Test
281 public void testCreaPhrase2() throws AbstractZemucanException {
282 final String cmdIn = "crea";
283
284 final ReturnOptions ret = ImplementationLexicalAnalyzer.getInstance()
285 .analyzePhrase(cmdIn);
286 final int actual = ret.getOptions().length;
287
288 final int expected = new String[] {}.length;
289
290 Assert.assertEquals("options for 'crea'", expected, actual);
291 }
292
293
294
295
296
297
298
299 @Test
300 public void testCreaPhrase3() throws AbstractZemucanException {
301 final String cmdIn = "crea";
302
303 final ReturnOptions ret = ImplementationLexicalAnalyzer.getInstance()
304 .analyzePhrase(cmdIn);
305 final int actual = ret.getPhrases().length;
306
307 final int expected = new String[] {}.length;
308
309 Assert.assertEquals("phrases for 'crea'", expected, actual);
310 }
311
312
313
314
315
316
317
318 @Test
319 public void testCreaPhrase4() throws AbstractZemucanException {
320 final String cmdIn = "crea";
321
322 final String cmdOut = ImplementationLexicalAnalyzerTest.CREATE;
323
324 final ReturnOptions actual = ImplementationLexicalAnalyzer
325 .getInstance().analyzePhrase(cmdIn);
326
327 final ReturnOptions expected = new ReturnOptions(cmdOut,
328 new String[] {}, new String[] {});
329
330 Assert.assertEquals("object for 'crea'", expected, actual);
331 }
332
333
334
335
336
337
338
339 @Test
340 public void testCreateCommand() throws AbstractZemucanException {
341 final String cmdIn = ImplementationLexicalAnalyzerTest.CREATE;
342
343 final String cmdOut = ImplementationLexicalAnalyzerTest.CREATE
344 + " table";
345
346 final String opt1 = "table";
347 final String opt2 = "tablespace";
348
349 final ReturnOptions actual = ImplementationLexicalAnalyzer
350 .getInstance().analyzePhrase(cmdIn);
351
352 final ReturnOptions expected = new ReturnOptions(cmdOut,
353 new String[] {}, new String[] { opt1, opt2 });
354
355 Assert.assertEquals("object for 'create'", expected, actual);
356 }
357
358
359
360
361
362
363
364 @Test
365 public void testCreateTab() throws AbstractZemucanException {
366 final String cmdIn = ImplementationLexicalAnalyzerTest.CREATE_TAB;
367
368 final String cmdOut = "create table";
369
370 final String opt1 = "tablespace";
371
372 final ReturnOptions actual = ImplementationLexicalAnalyzer
373 .getInstance().analyzePhrase(cmdIn);
374
375 final ReturnOptions expected = new ReturnOptions(cmdOut,
376 new String[] { opt1 }, new String[] {});
377
378 Assert.assertEquals("object for 'create tab'", expected, actual);
379 }
380
381
382
383
384
385
386
387 @Test
388 public void testCreateTableCommand1() throws AbstractZemucanException {
389 final String cmdIn = "create table";
390
391 final String opt1 = "<tableName>";
392
393 final ReturnOptions ret = ImplementationLexicalAnalyzer.getInstance()
394 .analyzePhrase(cmdIn);
395 final String[] actual = ret.getOptions();
396
397 final String[] expected = new String[] { opt1 };
398
399 Assert.assertEquals("options for 'create table'",
400 expected.length, actual.length);
401 }
402
403
404
405
406
407
408
409 @Test
410 public void testCreateTableCommand2() throws AbstractZemucanException {
411 final String cmdIn = "create table";
412
413 final String phrase1 = "tablespace";
414
415 final ReturnOptions ret = ImplementationLexicalAnalyzer.getInstance()
416 .analyzePhrase(cmdIn);
417 final String[] actual = ret.getPhrases();
418
419 final String[] expected = new String[] { phrase1 };
420
421 Assert.assertEquals("phrases for 'create table'",
422 expected.length, actual.length);
423 }
424
425
426
427
428
429
430
431 @Test
432 public void testCreateTableCommand3() throws AbstractZemucanException {
433 final String cmdIn = "create table";
434
435 final String cmdOut = "create table";
436
437 final ReturnOptions ret = ImplementationLexicalAnalyzer.getInstance()
438 .analyzePhrase(cmdIn);
439 final String actual = ret.getPhrase();
440
441 final String expected = cmdOut;
442
443 Assert.assertEquals("complete for 'create table'", expected,
444 actual);
445 }
446
447
448
449
450
451
452
453 @Test
454 public void testCreateTableCommand4() throws AbstractZemucanException {
455 final String cmdIn = "create table";
456
457 final String cmdOut = "create table";
458
459 final String phrase1 = "tablespace";
460
461 final String opt1 = "<tableName>";
462
463 final ReturnOptions actual = ImplementationLexicalAnalyzer
464 .getInstance().analyzePhrase(cmdIn);
465
466 final ReturnOptions expected = new ReturnOptions(cmdOut,
467 new String[] { phrase1 }, new String[] { opt1 });
468
469 Assert.assertEquals("object for 'create table'", expected,
470 actual);
471 }
472
473
474
475
476
477
478
479 @Test
480 public void testCreateTableCommand4WithSpace()
481 throws AbstractZemucanException {
482 final String cmdIn = "create table ";
483
484 final String cmdOut = "create table ";
485
486 final String opt1 = "<tableName>";
487
488 final ReturnOptions actual = ImplementationLexicalAnalyzer
489 .getInstance().analyzePhrase(cmdIn);
490
491 final ReturnOptions expected = new ReturnOptions(cmdOut,
492 new String[] {}, new String[] { opt1 });
493
494 Assert.assertEquals("object for 'create table '", expected,
495 actual);
496 }
497
498
499
500
501
502
503
504 @Test
505 public void testCreateTableTableNameCommand()
506 throws AbstractZemucanException {
507 final String cmdIn = "create table t1";
508
509 final String cmdOut = "create table t1 (";
510
511 final ReturnOptions actual = ImplementationLexicalAnalyzer
512 .getInstance().analyzePhrase(cmdIn);
513
514 final ReturnOptions expected = new ReturnOptions(cmdOut,
515 new String[] {}, new String[] {});
516
517 Assert.assertEquals("object for 'create table t1'", expected,
518 actual);
519 }
520
521
522
523
524
525
526
527
528 @Test
529 public void testCTtnParenthesis() throws AbstractZemucanException {
530 final String cmdIn = "create table t2(";
531 final String cmdOut = "create table t2( ";
532 final String opt1 = ImplementationLexicalAnalyzerTest.COL_NAME;
533
534 final ReturnOptions actual = ImplementationLexicalAnalyzer
535 .getInstance().analyzePhrase(cmdIn);
536
537 final ReturnOptions expected = new ReturnOptions(cmdOut,
538 new String[] {}, new String[] { opt1 });
539
540 Assert.assertEquals("object for 'create table t2('", expected,
541 actual);
542 }
543
544
545
546
547
548
549
550
551 @Test
552 public void testCTtnSpaceParenthesis() throws AbstractZemucanException {
553 final String cmdIn = "create table t2 (";
554
555 final String cmdOut = "create table t2 ( ";
556
557 final String opt1 = ImplementationLexicalAnalyzerTest.COL_NAME;
558
559 final ReturnOptions actual = ImplementationLexicalAnalyzer
560 .getInstance().analyzePhrase(cmdIn);
561
562 final ReturnOptions expected = new ReturnOptions(cmdOut,
563 new String[] {}, new String[] { opt1 });
564
565 Assert.assertEquals("object for 'create table t2 ('", expected,
566 actual);
567 }
568
569
570
571
572
573
574
575
576 @Test
577 public void testCTtnSpaceParenthesisWithSpace()
578 throws AbstractZemucanException {
579 final String cmdIn = "create table t2 ( ";
580
581 final String cmdOut = "create table t2 ( ";
582
583 final String opt1 = ImplementationLexicalAnalyzerTest.COL_NAME;
584
585 final ReturnOptions actual = ImplementationLexicalAnalyzer
586 .getInstance().analyzePhrase(cmdIn);
587
588 final ReturnOptions expected = new ReturnOptions(cmdOut,
589 new String[] {}, new String[] { opt1 });
590
591 Assert.assertEquals("object for 'create table t2 ('", expected,
592 actual);
593 }
594
595
596
597
598
599
600
601 @Test
602 public void testDataType() throws AbstractZemucanException {
603 final String cmdIn = "create table t2(c1";
604
605 final String cmdOut = "create table t2(c1 ";
606
607 final String option1 = "int";
608 final String option2 = "char";
609
610 final ReturnOptions actual = ImplementationLexicalAnalyzer
611 .getInstance().analyzePhrase(cmdIn);
612
613 final ReturnOptions expected = new ReturnOptions(cmdOut,
614 new String[] {}, new String[] { option1, option2 });
615
616 Assert.assertEquals("object for 'create table t2(c1'", expected,
617 actual);
618 }
619
620
621
622
623
624
625
626 @Test
627 public void testDoubleSpace() throws AbstractZemucanException {
628 final String cmdIn = ImplementationLexicalAnalyzerTest.SPACE
629 + ImplementationLexicalAnalyzerTest.SPACE;
630
631 final String cmdOut = "";
632
633 final ReturnOptions ret = ImplementationLexicalAnalyzer.getInstance()
634 .analyzePhrase(cmdIn);
635 final String actual = ret.getPhrase();
636
637 final String expected = cmdOut;
638
639 Assert.assertEquals("Empty phrase for an space", expected,
640 actual);
641 }
642
643
644
645
646
647
648
649 @Test
650 public void testEmptyString1() throws AbstractZemucanException {
651 final String cmdIn = "";
652
653 final String cmdOut = "";
654
655 final ReturnOptions ret = ImplementationLexicalAnalyzer.getInstance()
656 .analyzePhrase(cmdIn);
657 final String actual = ret.getPhrase();
658
659 final String expected = cmdOut;
660
661 Assert.assertEquals("Return Empty phrase", expected, actual);
662 }
663
664
665
666
667
668
669
670
671
672
673
674 @Test
675 public void testEmptyString2() throws AbstractZemucanException {
676 final String cmdIn = "";
677
678 final ReturnOptions actual = ImplementationLexicalAnalyzer
679 .getInstance().analyzePhrase(cmdIn);
680
681 final boolean condition = 0 < actual.getOptions().length;
682
683 Assert.assertTrue("Zero options for an empty phrase",
684 condition);
685 }
686
687
688
689
690
691
692
693 @Test
694 public void testEmptyString3() throws AbstractZemucanException {
695 final String cmdIn = "";
696
697 final ReturnOptions actual = ImplementationLexicalAnalyzer
698 .getInstance().analyzePhrase(cmdIn);
699
700 Assert.assertEquals("Zero phrases for an empty phrase", 0,
701 actual.getPhrases().length);
702 }
703
704
705
706
707
708
709
710 @Test
711 public void testInvalidCommand1() throws AbstractZemucanException {
712 final String cmdIn = "create table t1 (c1 int char)";
713
714 final String cmdOut = "create table t1 (c1 int char)";
715
716 final ReturnOptions actual = ImplementationLexicalAnalyzer
717 .getInstance().analyzePhrase(cmdIn);
718
719 final ReturnOptions expected = new ReturnOptions(cmdOut,
720 new String[] {}, new String[] {});
721
722 Assert.assertEquals("object for 'create table t1 (c1 int char)'",
723 expected, actual);
724 }
725
726
727
728
729
730
731
732 @Test
733 public void testInvalidCommand2() throws AbstractZemucanException {
734 final String cmdIn = "Andres Gomez Casanova";
735
736 final String cmdOut = "Andres Gomez Casanova";
737
738 final ReturnOptions actual = ImplementationLexicalAnalyzer
739 .getInstance().analyzePhrase(cmdIn);
740
741 final ReturnOptions expected = new ReturnOptions(cmdOut,
742 new String[] {}, new String[] {});
743
744 Assert.assertEquals("object for 'Andres Gomez Casanova'",
745 expected, actual);
746 }
747
748
749
750
751
752
753
754 @Test(expected = AssertionError.class)
755 public void testNullAnswerReplacetoken()
756 throws AbstractZemucanException {
757 ImplementationLexicalAnalyzer.getInstance().replaceLastToken("",
758 new ArrayList<Token>(), null);
759 }
760
761
762
763
764
765
766
767 @Test(expected = AssertionError.class)
768 public void testNullPhrase() throws AbstractZemucanException {
769 ImplementationLexicalAnalyzer.getInstance().analyzePhrase(null);
770 }
771
772
773
774
775
776
777
778 @Test(expected = AssertionError.class)
779 public void testNullPhraseReplacetoken()
780 throws AbstractZemucanException {
781 ImplementationLexicalAnalyzer.getInstance().replaceLastToken(null,
782 new ArrayList<Token>(), "");
783 }
784
785
786
787
788
789
790
791 @Test(expected = AssertionError.class)
792 public void testNullTokenInTokensReplacetoken()
793 throws AbstractZemucanException {
794 final List<Token> tokens = new ArrayList<Token>();
795 tokens.add(null);
796 ImplementationLexicalAnalyzer.getInstance().replaceLastToken("",
797 tokens, "");
798 }
799
800
801
802
803
804
805
806 @Test(expected = AssertionError.class)
807 public void testNullTokensReplacetoken()
808 throws AbstractZemucanException {
809 ImplementationLexicalAnalyzer.getInstance().replaceLastToken("",
810 null, "");
811 }
812
813
814
815
816
817
818
819 @Test
820 public void testPrecision() throws AbstractZemucanException {
821 final String cmdIn = "create table t1 (c1 int, c2 char(";
822
823 final String cmdOut = "create table t1 (c1 int, c2 char( ";
824
825 final String option1 = "<charTypeValue>";
826
827 final ReturnOptions actual = ImplementationLexicalAnalyzer
828 .getInstance().analyzePhrase(cmdIn);
829
830 final ReturnOptions expected = new ReturnOptions(cmdOut,
831 new String[] {}, new String[] { option1 });
832
833 Assert.assertEquals("object for 'create table t1 (c1 int, c2 char('",
834 expected, actual);
835 }
836
837
838
839
840
841
842
843 @Test
844 public void testSecondColumn() throws AbstractZemucanException {
845 final String cmdIn = "create table t2(c1 int,";
846
847 final String cmdOut = "create table t2(c1 int, ";
848
849 final String option = ImplementationLexicalAnalyzerTest.COL_NAME;
850
851 final ReturnOptions actual = ImplementationLexicalAnalyzer
852 .getInstance().analyzePhrase(cmdIn);
853
854 final ReturnOptions expected = new ReturnOptions(cmdOut,
855 new String[] {}, new String[] { option });
856
857 Assert.assertEquals("object for create table t2(c1 int,'",
858 expected, actual);
859 }
860
861
862
863
864
865
866
867
868 @Test
869 public void testSeveralOptionsAfterColumn()
870 throws AbstractZemucanException {
871 final String cmdIn = "create table t1 (c1 int, c2 char(10)";
872
873 final String cmdOut = "create table t1 (c1 int, c2 char(10) ";
874
875 final String option1 = ",";
876 final String option2 = ImplementationLexicalAnalyzerTest.CLOSE_PARENTHESIS;
877
878 final ReturnOptions actual = ImplementationLexicalAnalyzer
879 .getInstance().analyzePhrase(cmdIn);
880
881 final ReturnOptions expected = new ReturnOptions(cmdOut,
882 new String[] {}, new String[] { option1, option2 });
883
884 Assert.assertEquals(
885 "object for 'create table t1 (c1 int, c2 char(10)'",
886 expected, actual);
887 }
888
889
890
891
892
893
894
895 @Test
896 public void testSpace1() throws AbstractZemucanException {
897 final String cmdIn = ImplementationLexicalAnalyzerTest.SPACE;
898
899 final String cmdOut = "";
900
901 final ReturnOptions ret = ImplementationLexicalAnalyzer.getInstance()
902 .analyzePhrase(cmdIn);
903 final String actual = ret.getPhrase();
904
905 final String expected = cmdOut;
906
907 Assert.assertEquals("Empty phrase for an space", expected,
908 actual);
909 }
910
911
912
913
914
915
916
917 @Test
918 public void testSpace2() throws AbstractZemucanException {
919 final String cmdIn = ImplementationLexicalAnalyzerTest.SPACE;
920
921 final ReturnOptions actual = ImplementationLexicalAnalyzer
922 .getInstance().analyzePhrase(cmdIn);
923
924 final boolean condition = 0 < actual.getOptions().length;
925
926 Assert.assertTrue("Several options for a space",
927 condition);
928 }
929
930
931
932
933
934
935
936 @Test
937 public void testSpace3() throws AbstractZemucanException {
938 final String cmdIn = ImplementationLexicalAnalyzerTest.SPACE;
939
940 final ReturnOptions actual = ImplementationLexicalAnalyzer
941 .getInstance().analyzePhrase(cmdIn);
942
943 Assert.assertEquals("Zero phrases for a space", 0,
944 actual.getPhrases().length);
945 }
946
947
948
949
950
951
952
953 @Test
954 public void testValidCommand1() throws AbstractZemucanException {
955 final String cmdIn = "create table t1 (c1 int)";
956
957 final String cmdOut = "create table t1 (c1 int)";
958
959 final ReturnOptions actual = ImplementationLexicalAnalyzer
960 .getInstance().analyzePhrase(cmdIn);
961
962 final ReturnOptions expected = new ReturnOptions(cmdOut,
963 new String[] {}, new String[] {});
964
965 Assert.assertEquals("object for 'create table t1 (c1 int)'",
966 expected, actual);
967 }
968
969
970
971
972
973
974
975
976 @Test
977 public void testValidCommand2() throws AbstractZemucanException {
978 final String cmdIn = "create table t1 (c1 int, c2 char(10))";
979
980 final String cmdOut = "create table t1 (c1 int, c2 char(10))";
981
982 final ReturnOptions actual = ImplementationLexicalAnalyzer
983 .getInstance().analyzePhrase(cmdIn);
984
985 final ReturnOptions expected = new ReturnOptions(cmdOut,
986 new String[] {}, new String[] {});
987
988 Assert.assertEquals(
989 "object for 'create table t1 (c1 int, c2 char(10))'",
990 expected, actual);
991 }
992 }