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.db2sa.core.syntax.graph;
30
31 import java.io.StringBufferInputStream;
32
33 import name.angoca.db2sa.Configurator;
34 import name.angoca.db2sa.Constants;
35 import name.angoca.db2sa.core.syntax.graph.exception.DuplicateNodeException;
36 import name.angoca.db2sa.core.syntax.graph.exception.EndingNodeNotDefinedException;
37 import name.angoca.db2sa.core.syntax.graph.exception.GrammarFileException;
38 import name.angoca.db2sa.core.syntax.graph.exception.InvalidGraphException;
39 import name.angoca.db2sa.core.syntax.graph.exception.StartingNodeNotDefinedException;
40
41 import org.junit.AfterClass;
42 import org.junit.Assert;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.xml.sax.InputSource;
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 @SuppressWarnings("deprecation")
63 public final class GraphConstructorTest {
64
65
66
67 public GraphConstructorTest() {
68
69 }
70
71
72
73
74
75 @Before
76 public final void setUp() {
77 System.clearProperty(Constants.DB2SA_CONF_XML);
78 }
79
80
81
82
83
84 @AfterClass
85 public static void tearDownAfterClass() {
86 System.clearProperty(Constants.DB2SA_CONF_XML);
87 }
88
89
90
91
92
93
94
95 @Test
96 public final void testBuild() throws InvalidGraphException {
97 String xml = "";
98 xml += "<grammar>";
99 xml += " <tokens>";
100
101 xml += " <token>";
102 xml += " <id>STARTING_TOKEN</id>";
103 xml += " <name>STARTING_TOKEN</name>";
104 xml += " <reserved />";
105 xml += " <children>";
106 xml += " <idNode>create</idNode>";
107 xml += " </children>";
108 xml += " </token>";
109
110 xml += " <token>";
111 xml += " <id>create</id>";
112 xml += " <name>create</name>";
113 xml += " <reserved/>";
114 xml += " <children>";
115 xml += " <idNode>ENDING_TOKEN</idNode>";
116 xml += " </children>";
117 xml += " </token>";
118
119 xml += " <token>";
120 xml += " <id>ENDING_TOKEN</id>";
121 xml += " <name>ENDING_TOKEN</name>";
122 xml += " <reserved/>";
123 xml += " </token>";
124 xml += " </tokens>";
125 xml += "</grammar>";
126
127 final InputSource content = new InputSource(
128 new StringBufferInputStream(xml));
129 final StartingToken actual = GraphConstructor.build(content);
130
131 final StartingToken expected = new StartingToken();
132
133
134 final GraphToken create = new GraphToken("create", true);
135 final GraphToken help = new GraphToken("?", true);
136 final GraphToken about = new GraphToken("about", true);
137
138 final EndingToken end = new EndingToken();
139 expected.addWay(create);
140 expected.addWay(help);
141 expected.addWay(about);
142
143 create.addWay(end);
144 help.addWay(end);
145 about.addWay(end);
146
147 Assert.assertEquals("Simple graph", expected, actual);
148 }
149
150
151
152
153
154
155
156 @Test(expected = EndingNodeNotDefinedException.class)
157 public final void testBuildNoEndingNode() throws InvalidGraphException {
158 String xml = "";
159 xml += "<grammar>";
160 xml += " <tokens>";
161
162 xml += " <token>";
163 xml += " <id>STARTING_TOKEN</id>";
164 xml += " <name>STARTING_TOKEN</name>";
165 xml += " <reserved />";
166 xml += " </token>";
167 xml += " </tokens>";
168 xml += "</grammar>";
169
170 final InputSource content = new InputSource(
171 new StringBufferInputStream(xml));
172 GraphConstructor.build(content);
173 }
174
175
176
177
178
179
180
181 @Test(expected = DuplicateNodeException.class)
182 public final void testBuildDuplicatedNode() throws InvalidGraphException {
183 String xml = "";
184 xml += "<grammar>";
185 xml += " <tokens>";
186
187 xml += " <token>";
188 xml += " <id>STARTING_TOKEN</id>";
189 xml += " <name>STARTING_TOKEN</name>";
190 xml += " <reserved />";
191 xml += " <children>";
192 xml += " <idNode>ENDING_TOKEN</idNode>";
193 xml += " </children>";
194 xml += " </token>";
195
196 xml += " <token>";
197 xml += " <id>ENDING_TOKEN</id>";
198 xml += " <name>ENDING_TOKEN</name>";
199 xml += " <reserved/>";
200 xml += " </token>";
201
202 xml += " <token>";
203 xml += " <id>ENDING_TOKEN</id>";
204 xml += " <name>ENDING_TOKEN</name>";
205 xml += " <reserved/>";
206 xml += " </token>";
207 xml += " </tokens>";
208 xml += "</grammar>";
209
210 final InputSource content = new InputSource(
211 new StringBufferInputStream(xml));
212 GraphConstructor.build(content);
213 }
214
215
216
217
218
219
220
221 @Test(expected = RuntimeException.class)
222 public final void testBuildNotAllChildren() throws InvalidGraphException {
223 String xml = "";
224 xml += "<grammar>";
225 xml += " <tokens>";
226
227 xml += " <token>";
228 xml += " <id>STARTING_TOKEN</id>";
229 xml += " <name>STARTING_TOKEN</name>";
230 xml += " <reserved />";
231 xml += " <children>";
232 xml += " <idNode>ENDING_TOKEN</idNode>";
233 xml += " </children>";
234 xml += " </token>";
235 xml += " </tokens>";
236 xml += "</grammar>";
237
238 final InputSource content = new InputSource(
239 new StringBufferInputStream(xml));
240 GraphConstructor.build(content);
241 }
242
243
244
245
246
247
248
249 @Test(expected = GrammarFileException.class)
250 public final void testBuildEmptyFile() throws InvalidGraphException {
251 final String xml = "";
252
253 final InputSource content = new InputSource(
254 new StringBufferInputStream(xml));
255 GraphConstructor.build(content);
256 }
257
258
259
260
261
262
263
264 @Test(expected = GrammarFileException.class)
265 public final void testBuildUnknownStructure() throws InvalidGraphException {
266 final String xml = "This can be a test file";
267
268 final InputSource content = new InputSource(
269 new StringBufferInputStream(xml));
270 GraphConstructor.build(content);
271 }
272
273
274
275
276
277
278
279 @Test(expected = GrammarFileException.class)
280 public final void testBuildInvalidStructure() throws InvalidGraphException {
281 String xml = "";
282 xml += "<grammar>";
283 xml += " <tokens>";
284 xml += " <token>";
285 xml += " <id>STARTING_TOKEN</id>";
286
287 final InputSource content = new InputSource(
288 new StringBufferInputStream(xml));
289 GraphConstructor.build(content);
290 }
291
292
293
294
295
296
297
298 @Test(expected = StartingNodeNotDefinedException.class)
299 public final void testBuildNoNodes() throws InvalidGraphException {
300 String xml = "";
301 xml += "<grammar>";
302 xml += " <tokens>";
303 xml += " </tokens>";
304 xml += "</grammar>";
305
306 final InputSource content = new InputSource(
307 new StringBufferInputStream(xml));
308 GraphConstructor.build(content);
309 }
310
311
312
313
314
315
316
317 @Test(expected = StartingNodeNotDefinedException.class)
318 public final void testBuildStartingNode() throws InvalidGraphException {
319 String xml = "";
320 xml += "<grammar>";
321 xml += " <tokens>";
322
323 xml += " <token>";
324 xml += " <id>create</id>";
325 xml += " <name>create</name>";
326 xml += " <reserved/>";
327 xml += " <children>";
328 xml += " <idNode>ENDING_TOKEN</idNode>";
329 xml += " </children>";
330 xml += " </token>";
331
332 xml += " <token>";
333 xml += " <id>ENDING_TOKEN</id>";
334 xml += " <name>ENDING_TOKEN</name>";
335 xml += " <reserved/>";
336 xml += " </token>";
337 xml += " </tokens>";
338 xml += "</grammar>";
339
340 final InputSource content = new InputSource(
341 new StringBufferInputStream(xml));
342 GraphConstructor.build(content);
343 }
344
345
346
347
348
349
350
351 @Test(expected = RuntimeException.class)
352 public final void testBuildNoName() throws InvalidGraphException {
353 String xml = "";
354 xml += "<grammar>";
355 xml += " <tokens>";
356
357 xml += " <token>";
358 xml += " <id>STARTING_TOKEN</id>";
359 xml += " <name>STARTING_TOKEN</name>";
360 xml += " <reserved />";
361 xml += " <children>";
362 xml += " <idNode>create</idNode>";
363 xml += " </children>";
364 xml += " </token>";
365
366 xml += " <token>";
367 xml += " <id>create</id>";
368 xml += " <reserved/>";
369 xml += " <children>";
370 xml += " <idNode>ENDING_TOKEN</idNode>";
371 xml += " </children>";
372 xml += " </token>";
373
374 xml += " <token>";
375 xml += " <id>ENDING_TOKEN</id>";
376 xml += " <name>ENDING_TOKEN</name>";
377 xml += " <reserved/>";
378 xml += " </token>";
379 xml += " </tokens>";
380 xml += "</grammar>";
381
382 final InputSource content = new InputSource(
383 new StringBufferInputStream(xml));
384 GraphConstructor.build(content);
385 }
386
387
388
389
390
391
392
393 @Test(expected = RuntimeException.class)
394 public final void testBuildNoId() throws InvalidGraphException {
395 String xml = "";
396 xml += "<grammar>";
397 xml += " <tokens>";
398
399 xml += " <token>";
400 xml += " <id>STARTING_TOKEN</id>";
401 xml += " <name>STARTING_TOKEN</name>";
402 xml += " <reserved />";
403 xml += " <children>";
404 xml += " <idNode>create</idNode>";
405 xml += " </children>";
406 xml += " </token>";
407
408 xml += " <token>";
409 xml += " <name>create</name>";
410 xml += " <reserved/>";
411 xml += " <children>";
412 xml += " <idNode>ENDING_TOKEN</idNode>";
413 xml += " </children>";
414 xml += " </token>";
415
416 xml += " <token>";
417 xml += " <id>ENDING_TOKEN</id>";
418 xml += " <name>ENDING_TOKEN</name>";
419 xml += " <reserved/>";
420 xml += " </token>";
421 xml += " </tokens>";
422 xml += "</grammar>";
423
424 final InputSource content = new InputSource(
425 new StringBufferInputStream(xml));
426 GraphConstructor.build(content);
427 }
428
429
430
431
432
433
434
435
436 @Test
437 public final void testBuildNotConnectedGraph() throws InvalidGraphException {
438 String xml = "";
439 xml += "<grammar>";
440 xml += " <tokens>";
441
442 xml += " <token>";
443 xml += " <id>STARTING_TOKEN</id>";
444 xml += " <name>STARTING_TOKEN</name>";
445 xml += " <reserved />";
446 xml += " </token>";
447
448 xml += " <token>";
449 xml += " <id>ENDING_TOKEN</id>";
450 xml += " <name>ENDING_TOKEN</name>";
451 xml += " <reserved/>";
452 xml += " </token>";
453 xml += " </tokens>";
454 xml += "</grammar>";
455
456 final InputSource content = new InputSource(
457 new StringBufferInputStream(xml));
458 GraphConstructor.build(content);
459
460
461 }
462
463
464
465
466
467
468
469
470 @Test(expected = RuntimeException.class)
471 public final void testBuildNoAboutToken() throws InvalidGraphException {
472
473 System.setProperty(Constants.DB2SA_CONF_XML,
474 "db2sa_conf-test-NoAboutToken.xml");
475 Configurator.destroyInstance();
476
477 String xml = "";
478 xml += "<grammar>";
479 xml += " <tokens>";
480
481 xml += " <token>";
482 xml += " <id>STARTING_TOKEN</id>";
483 xml += " <name>STARTING_TOKEN</name>";
484 xml += " <reserved />";
485 xml += " <children>";
486 xml += " <idNode>create</idNode>";
487 xml += " </children>";
488 xml += " </token>";
489
490 xml += " <token>";
491 xml += " <id>create</id>";
492 xml += " <name>create</name>";
493 xml += " <reserved/>";
494 xml += " <children>";
495 xml += " <idNode>ENDING_TOKEN</idNode>";
496 xml += " </children>";
497 xml += " </token>";
498
499 xml += " <token>";
500 xml += " <id>ENDING_TOKEN</id>";
501 xml += " <name>ENDING_TOKEN</name>";
502 xml += " <reserved/>";
503 xml += " </token>";
504 xml += " </tokens>";
505 xml += "</grammar>";
506
507 final InputSource content = new InputSource(
508 new StringBufferInputStream(xml));
509 final StartingToken actual = GraphConstructor.build(content);
510 actual.toString();
511
512 }
513
514
515
516
517
518
519
520
521 @Test(expected = RuntimeException.class)
522 public final void testBuildNoHelpToken() throws InvalidGraphException {
523
524 System.setProperty(Constants.DB2SA_CONF_XML,
525 "db2sa_conf-test-NoHelpToken.xml");
526 Configurator.destroyInstance();
527
528 String xml = "";
529 xml += "<grammar>";
530 xml += " <tokens>";
531
532 xml += " <token>";
533 xml += " <id>STARTING_TOKEN</id>";
534 xml += " <name>STARTING_TOKEN</name>";
535 xml += " <reserved />";
536 xml += " <children>";
537 xml += " <idNode>create</idNode>";
538 xml += " </children>";
539 xml += " </token>";
540
541 xml += " <token>";
542 xml += " <id>create</id>";
543 xml += " <name>create</name>";
544 xml += " <reserved/>";
545 xml += " <children>";
546 xml += " <idNode>ENDING_TOKEN</idNode>";
547 xml += " </children>";
548 xml += " </token>";
549
550 xml += " <token>";
551 xml += " <id>ENDING_TOKEN</id>";
552 xml += " <name>ENDING_TOKEN</name>";
553 xml += " <reserved/>";
554 xml += " </token>";
555 xml += " </tokens>";
556 xml += "</grammar>";
557
558 final InputSource content = new InputSource(
559 new StringBufferInputStream(xml));
560 final StartingToken actual = GraphConstructor.build(content);
561 actual.toString();
562
563 }
564 }