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 22:39:08 -0500 (dom, 06 mar 2011) $:
26 * Revision: $LastChangedRevision: 1916 $:
27 * URL: $HeadURL: https://zemucan.svn.sourceforge.net/svnroot/zemucan/branches/zemucan_v1/source-code/interfaze/src/test/java/name/angoca/zemucan/interfaze/InterfaceCoreTest.java $:
28 */
29 package name.angoca.zemucan.interfaze;
30
31 import name.angoca.zemucan.AbstractZemucanException;
32 import name.angoca.zemucan.ParameterNullException;
33 import name.angoca.zemucan.core.lexical.impl.ImplementationLexicalAnalyzer;
34 import name.angoca.zemucan.grammarReader.api.GrammarReaderController;
35 import name.angoca.zemucan.interfaze.model.ReturnOptions;
36 import name.angoca.zemucan.tools.Constants;
37 import name.angoca.zemucan.tools.configurator.Configurator;
38 import name.angoca.zemucan.tools.test.RandomTestRunner;
39
40 import org.junit.AfterClass;
41 import org.junit.Assert;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45
46 /**
47 * Tests for the interface core. Tests the objects returned by this layer.
48 * <p>
49 * <b>Control Version</b>
50 * <p>
51 * <ul>
52 * <li>0.0.1 Class creation.</li>
53 * <li>0.1.0</li>
54 * <li>0.2.0</li>
55 * <li>0.3.0</li>
56 * <li>0.3.1 Recommendations from PMD.</li>
57 * <li>0.3.2 Organized.</li>
58 * <li>0.4.0 Fewer tests (Two classes).</li>
59 * <li>0.5.0 jUnit 4 annotations.</li>
60 * <li>0.5.1 Messages in asserts.</li>
61 * <li>0.6.0 Invalid graph exception.</li>
62 * <li>0.6.1 Configurator.</li>
63 * <li>0.6.2 Destroy instance.</li>
64 * <li>0.7.0. Execute tests</li>
65 * <li>0.7.1. finals</li>
66 * <li>0.8.0. Setup and tearDown</li>
67 * <li>1.0.0 Moved to version 1.</li>
68 * <li>1.1.0 Exception hierarchy changed.</li>
69 * <li>1.2.0 Exit test and take out intergration tests.</li>
70 * <li>1.3.0 More tests and throws.</li>
71 * <li>1.4.0 Refactored tests.</li>
72 * <li>1.4.1 Grammar controller.</li>
73 * <li>1.4.2 Completion fixed.</li>
74 * <li>1.4.3 After class method.</li>
75 * </ul>
76 *
77 * @author Andres Gomez Casanova <a
78 * href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
79 * @version 1.4.3 2010-09-25
80 */
81 @RunWith(RandomTestRunner.class)
82 public final class InterfaceCoreTest {
83 /**
84 * Load the graph.
85 *
86 * @throws AbstractZemucanException
87 * Never.
88 */
89 @BeforeClass
90 public static void setUpBeforeClass() throws AbstractZemucanException {
91 System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
92 "sa_conf-test.xml"); //$NON-NLS-1$
93 Configurator.getInstance().setProperty(
94 Constants.GRAMMAR_READER_NAME_PROPERTY,
95 Configurator.GRAMMAR_READER_NAME_VALUE);
96 Configurator.getInstance()
97 .setProperty(Constants.GRAMMAR_FILE_DESCRIPTORS_PROPERTY,
98 "grammar-test.xml"); //$NON-NLS-1$
99
100 // Creates the lexical analyzer instance.
101 ImplementationLexicalAnalyzer.getInstance();
102 }
103
104 /**
105 * Clears the configuration file name property at the end of the test.
106 */
107 @AfterClass
108 public static void tearDownAfterClass() {
109 // Destroys any existent instance.
110 ImplementationLexicalAnalyzer.destroyInstance();
111
112 // Recreates the configurator instance.
113 Configurator.destroyInstance();
114 GrammarReaderController.destroyInstance();
115 System.clearProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY);
116 }
117
118 /**
119 * Default constructor.
120 */
121 public InterfaceCoreTest() {
122 // Nothing.
123 }
124
125 /**
126 * Analyze exit token.
127 *
128 * @throws AbstractZemucanException
129 * Never.
130 */
131 @Test
132 public void testAnalyzeExit() throws AbstractZemucanException {
133 final String phrase = "exit"; //$NON-NLS-1$
134
135 final String phraseOut = "exit"; //$NON-NLS-1$
136
137 final String[] phrases = {};
138
139 final String[] options = {};
140
141 final ReturnOptions actual = InterfaceCore.analyzePhrase(phrase);
142
143 final ReturnOptions expected = new ReturnOptions(phraseOut, phrases,
144 options);
145
146 Assert.assertEquals("Phrases and options for 'exit'", //$NON-NLS-1$
147 expected, actual);
148 }
149
150 /**
151 * Analyze a null token.
152 *
153 * @throws AbstractZemucanException
154 * Never.
155 */
156 @Test
157 public void testAnalyzeNull() throws AbstractZemucanException {
158 final String phrase = null;
159
160 Exception e = null;
161 try {
162 InterfaceCore.analyzePhrase(phrase);
163 } catch (final Exception exception) {
164 e = exception;
165 }
166 Assert.assertTrue("ParameterNullException",
167 e instanceof ParameterNullException);
168 }
169
170 /**
171 * Test the completion of "create tab".
172 *
173 * @throws AbstractZemucanException
174 * Never.
175 */
176 @Test
177 public void testAnalyzePhraseComplete1() throws AbstractZemucanException {
178 final String phraseActual = "create tab"; //$NON-NLS-1$
179 final String phraseExpected = "create table"; //$NON-NLS-1$
180
181 final String[] phrases = { "tablespace" }; //$NON-NLS-1$
182 final String[] options = {};
183
184 final ReturnOptions actual = InterfaceCore.analyzePhrase(phraseActual);
185 final ReturnOptions expected = new ReturnOptions(phraseExpected,
186 phrases, options);
187
188 Assert.assertEquals("Phrases for 'create tab'", expected, //$NON-NLS-1$
189 actual);
190 }
191
192 /**
193 * Test the completion of "crea".
194 *
195 * @throws AbstractZemucanException
196 * Never.
197 */
198 @Test
199 public void testAnalyzePhraseComplete2() throws AbstractZemucanException {
200 final String phraseActual = "crea"; //$NON-NLS-1$
201 final String phraseExpected = "create"; //$NON-NLS-1$
202
203 final String[] options = {};
204
205 final ReturnOptions actual = InterfaceCore.analyzePhrase(phraseActual);
206 final ReturnOptions expected = new ReturnOptions(phraseExpected,
207 new String[] {}, options);
208
209 Assert.assertEquals("Phrase for 'crea'", expected, actual); //$NON-NLS-1$
210 }
211
212 /**
213 * Test the options of "create table".
214 *
215 * @throws AbstractZemucanException
216 * Never.
217 */
218 @Test
219 public void testAnalyzePhraseOptions1() throws AbstractZemucanException {
220 final String phraseIn = "create table"; //$NON-NLS-1$
221
222 final String phraseOut = "create table"; //$NON-NLS-1$
223
224 final String[] phrases = { "tablespace" }; //$NON-NLS-1$
225
226 final String[] options = { "<tableName>" }; //$NON-NLS-1$
227
228 final ReturnOptions actual = InterfaceCore.analyzePhrase(phraseIn);
229 final ReturnOptions expected = new ReturnOptions(phraseOut, phrases,
230 options);
231
232 Assert.assertEquals("Phrases and options for 'create table'", //$NON-NLS-1$
233 expected, actual);
234 }
235 }