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.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 @RunWith(RandomTestRunner.class)
61 public class GrammarReaderControllerTest {
62
63
64
65 private static final String IN_MEMORY_GRAMMAR_READER = "name.angoca.zemucan.grammarReader.api.ImplementationInMemoryGrammarReader";
66
67
68
69
70 @After
71 public void tearDown() {
72
73 Configurator.destroyInstance();
74
75
76 GrammarReaderController.destroyInstance();
77 }
78
79
80
81
82
83
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");
92
93 try {
94 GrammarReaderController.getInstance();
95 } catch (final Exception exception) {
96 Assert.assertTrue("GeneralGrammarReaderProblemException",
97 exception instanceof GeneralGrammarReaderProblemException);
98 }
99 }
100
101
102
103
104
105
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");
113
114 try {
115 GrammarReaderController.getInstance();
116 } catch (final Exception exception) {
117 Assert.assertTrue("GeneralGrammarReaderProblemException",
118 exception instanceof GeneralGrammarReaderProblemException);
119 }
120 }
121
122
123
124
125
126
127
128 @Test
129 public final void testEmptyValue() throws AbstractZemucanException {
130 System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
131 "sa_conf-test-NoGrammarReaderDefined.xml");
132
133 try {
134 GrammarReaderController.getInstance();
135 } catch (final Exception exception) {
136 Assert.assertTrue("GrammarReaderNotDefinedException",
137 exception instanceof GrammarReaderNotDefinedException);
138 }
139 }
140
141
142
143
144
145
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;");
155
156 final StartingNode node = GrammarReaderController.getInstance()
157 .getStartingNode();
158
159 Assert.assertTrue("Extra commas", node != null);
160 }
161
162
163
164
165
166
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");
176
177 final StartingNode node = GrammarReaderController.getInstance()
178 .getStartingNode();
179
180 Assert.assertTrue("Extra commas", node != null);
181 }
182
183
184
185
186
187
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");
197
198 final StartingNode node = GrammarReaderController.getInstance()
199 .getStartingNode();
200
201 Assert.assertTrue("Extra commas", node != null);
202 }
203
204
205
206
207
208
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 ");
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
229
230
231
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");
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
252
253
254
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");
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
275
276
277
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");
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
298
299
300
301
302 @Test
303 public final void testInexistantGrammar() throws AbstractZemucanException {
304 Configurator.getInstance().setProperty(
305 Constants.GRAMMAR_READER_NAME_PROPERTY, "Inexistant");
306
307 try {
308 GrammarReaderController.getInstance();
309 } catch (final Exception exception) {
310 Assert.assertTrue("GeneralGrammarReaderProblemException",
311 exception instanceof GeneralGrammarReaderProblemException);
312 }
313 }
314
315
316
317
318
319
320
321 @Test
322 public final void testInvalidValue() throws AbstractZemucanException {
323 System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
324 "sa_conf-test-InvalidGrammarReaderDefined.xml");
325
326 try {
327 GrammarReaderController.getInstance();
328 } catch (final Exception exception) {
329 Assert.assertTrue("GeneralGrammarReaderProblemException",
330 exception instanceof GeneralGrammarReaderProblemException);
331 }
332 }
333
334
335
336
337
338
339
340 @Test
341 public final void testNullValue() throws AbstractZemucanException {
342 System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
343 "sa_conf-test-NoGrammarReader.xml");
344
345 try {
346 GrammarReaderController.getInstance();
347 } catch (final Exception exception) {
348 Assert.assertTrue("GrammarReaderNotDefinedException",
349 exception instanceof GrammarReaderNotDefinedException);
350 }
351 }
352
353
354
355
356
357
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");
367
368 GrammarReaderController.getInstance();
369 }
370
371
372
373
374
375
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");
386
387 final StartingNode node = GrammarReaderController.getInstance()
388 .getStartingNode();
389
390 Assert.assertTrue("Two different file grammars", node != null);
391 }
392
393
394
395
396
397
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");
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
419
420
421
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");
431
432 try {
433 GrammarReaderController.getInstance();
434 } catch (final Exception exception) {
435 Assert.assertTrue("GeneralGrammarReaderProblemException",
436 exception instanceof GeneralGrammarReaderProblemException);
437 }
438 }
439 }