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.graph.model;
30
31 import java.util.List;
32
33 import name.angoca.zemucan.AbstractZemucanException;
34 import name.angoca.zemucan.ParameterNullException;
35 import name.angoca.zemucan.core.graph.api.ChildEndingNodeException;
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.After;
41 import org.junit.AfterClass;
42 import org.junit.Assert;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 @RunWith(RandomTestRunner.class)
66 public final class GraphTest {
67
68
69
70 private static final String A = "a";
71
72
73
74 private static final String ADD_RELATION = "add relation";
75
76
77
78 private static final String CREATE = "create";
79
80
81
82 private static final String CREATE1 = "create1";
83
84
85
86 private static final String CREATE2 = "create2";
87
88
89
90 private static final String DUMMY_GRAPH = "dummy graph";
91 private static final String GRAPH = "graph";
92 private static final String GRAPH1 = "graph1";
93 private static final String GRAPH2 = "graph2";
94 private static final String ID = "id";
95 private static final String ID1 = "id1";
96 private static final String ID2 = "id2";
97 private static final String NAME = "name";
98 private static final String NO_CONGRUENT_2 = "no congruent 2";
99 private static final String NOT_EQUALS_TO_FIRST_PHASE_GRAPH = "not equals to first phase graph";
100 private static final String NOT_EQUALS_TO_SIMILAR_GRAPH = "not equals to similar graph";
101 private static final String NOT_EQUALS_TO_SIMPLE_GRAPH = "not equals to simple graph";
102 private static final String NULL_RELATION = "null relation";
103
104
105
106 private static final String OPEN_BRAKET = "{";
107 private static final String OTHER = "other";
108
109
110
111 private static final String PIPE = "|";
112 private static final String QTY_OF_WAYS = "Qty of ways";
113 private static final String SAME_HASHCODE = "Same hashcode";
114 private static final String STARTING_NODE = "starting node";
115 private static final String TABLE = "table";
116 private static final String TEST = "test";
117 private static final String TO_STRING = "toString";
118 private static final String VALIDATED_GRAPH = "validated graph";
119 private static final String VALIDATED_TWICE = "validated twice";
120
121
122
123
124
125
126
127 @BeforeClass
128 public static void setUpBeforeClass() throws AbstractZemucanException {
129
130 System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
131 "sa_conf-test.xml");
132 }
133
134
135
136
137 @AfterClass
138 public static void tearDownAfterClass() {
139 System.clearProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY);
140 }
141
142
143
144
145 @After
146 public void tearDown() {
147 Configurator.destroyInstance();
148 }
149
150
151
152
153
154
155
156 @Test
157 public void testAddNodeInSecondPhase() throws AbstractZemucanException {
158 final Graph graph = new Graph("add node", false);
159 graph.addNode(GraphTest.ID1, GraphTest.ID1, true);
160 graph.firstPhaseFinished();
161
162 Exception e = null;
163 try {
164 graph.addNode(GraphTest.ID2, GraphTest.ID2, true);
165 } catch (final Exception exception) {
166 e = exception;
167 }
168 Assert.assertTrue("InvalidGraphStateException",
169 e instanceof InvalidGraphStateException);
170 }
171
172
173
174
175
176
177
178 @Test
179 public void testAddNodeValidateGraph() throws AbstractZemucanException {
180 final Graph graph = new Graph("add node validated graph", false);
181 graph.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
182 graph.addNode(GraphTest.CREATE, GraphTest.CREATE, true);
183 graph.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
184 graph.firstPhaseFinished();
185 graph.addRelation(Constants.STARTING_NODE, GraphTest.CREATE);
186 graph.addRelation(GraphTest.CREATE, Constants.ENDING_NODE);
187 graph.secondPhaseFinished();
188
189 Exception e = null;
190 try {
191 graph.addNode(GraphTest.TABLE, GraphTest.TABLE, true);
192 } catch (final Exception exception) {
193 e = exception;
194 }
195 Assert.assertTrue("InvalidGraphStateException",
196 e instanceof InvalidGraphStateException);
197 }
198
199
200
201
202
203
204
205 @Test
206 public void testAddNullRelation1() throws AbstractZemucanException {
207 final Graph graph = new Graph(GraphTest.NULL_RELATION, false);
208 graph.addNode(GraphTest.ID1, GraphTest.ID1, true);
209 graph.firstPhaseFinished();
210
211 Exception e = null;
212 try {
213 graph.addRelation(GraphTest.ID1, null);
214 } catch (final Exception exception) {
215 e = exception;
216 }
217 Assert.assertTrue("ParameterNullException",
218 e instanceof ParameterNullException);
219 }
220
221
222
223
224
225
226
227 @Test
228 public void testAddNullRelation2() throws AbstractZemucanException {
229 final Graph graph = new Graph(GraphTest.NULL_RELATION, false);
230 graph.addNode(GraphTest.ID1, GraphTest.ID1, true);
231 graph.firstPhaseFinished();
232
233 Exception e = null;
234 try {
235 graph.addRelation(null, GraphTest.ID1);
236 } catch (final Exception exception) {
237 e = exception;
238 }
239 Assert.assertTrue("ParameterNullException",
240 e instanceof ParameterNullException);
241 }
242
243
244
245
246
247
248
249 @Test
250 public void testAddRelation() throws AbstractZemucanException {
251 final Graph graph = new Graph(GraphTest.ADD_RELATION, false);
252 graph.addNode(GraphTest.ID1, GraphTest.ID1, true);
253 graph.addNode(GraphTest.ID2, GraphTest.ID2, true);
254 graph.firstPhaseFinished();
255 graph.addRelation(GraphTest.ID1, GraphTest.ID2);
256
257 final GraphState actual = graph.getGraphState();
258
259 final GraphState expected = GraphState.SecondPhase;
260
261 Assert.assertEquals("Add a relation", expected, actual);
262 }
263
264
265
266
267
268
269
270 @Test
271 public void testAddRelationInexistantChild()
272 throws AbstractZemucanException {
273 final Graph graph = new Graph("inexistant child", false);
274 graph.addNode(GraphTest.ID1, GraphTest.ID1, true);
275 graph.firstPhaseFinished();
276
277 Exception e = null;
278 try {
279 graph.addRelation(GraphTest.ID1, GraphTest.ID2);
280 } catch (final Exception exception) {
281 e = exception;
282 }
283 Assert.assertTrue("NotExistingChildNodeException",
284 e instanceof NotExistingChildNodeException);
285 }
286
287
288
289
290
291
292
293 @Test
294 public void testAddRelationInexistantParent()
295 throws AbstractZemucanException {
296 final Graph graph = new Graph("inexistant parent", false);
297 graph.addNode(GraphTest.ID2, GraphTest.ID2, true);
298 graph.firstPhaseFinished();
299
300 Exception e = null;
301 try {
302 graph.addRelation(GraphTest.ID1, GraphTest.ID2);
303 } catch (final Exception exception) {
304 e = exception;
305 }
306 Assert.assertTrue("NotExistingNodeException",
307 e instanceof NotExistingNodeException);
308 }
309
310
311
312
313
314
315
316 @Test
317 public void testAddRelationInSecondPhase()
318 throws AbstractZemucanException {
319 final Graph graph = new Graph(GraphTest.ADD_RELATION, false);
320 graph.addNode(GraphTest.ID1, GraphTest.ID1, true);
321 graph.addNode(GraphTest.ID2, GraphTest.ID2, true);
322
323 Exception e = null;
324 try {
325 graph.addRelation(GraphTest.ID1, GraphTest.ID2);
326 } catch (final Exception exception) {
327 e = exception;
328 }
329 Assert.assertTrue("InvalidGraphStateException",
330 e instanceof InvalidGraphStateException);
331 }
332
333
334
335
336
337
338
339 @Test
340 public void testAddRelationValidateGraph()
341 throws AbstractZemucanException {
342 final Graph graph = new Graph("add relation validated", false);
343 graph.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
344 graph.addNode(GraphTest.CREATE, GraphTest.CREATE, true);
345 graph.addNode(GraphTest.TABLE, GraphTest.TABLE, true);
346 graph.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
347 graph.firstPhaseFinished();
348 graph.addRelation(Constants.STARTING_NODE, GraphTest.CREATE);
349 graph.addRelation(Constants.STARTING_NODE, GraphTest.TABLE);
350 graph.addRelation(GraphTest.CREATE, Constants.ENDING_NODE);
351 graph.addRelation(GraphTest.TABLE, Constants.ENDING_NODE);
352 graph.secondPhaseFinished();
353 graph.addRelation(GraphTest.CREATE, GraphTest.TABLE);
354
355 final GraphState actual = graph.getGraphState();
356
357 final GraphState expected = GraphState.SecondPhase;
358
359 Assert.assertEquals("Add a relation in validated graph", expected,
360 actual);
361 }
362
363
364
365
366
367
368
369 @Test
370 public void testCongruent() throws AbstractZemucanException {
371 final Graph graph1 = this.testCreateSimpleGraph();
372 final Graph graph2 = this.testCreateSimpleGraph();
373
374 final boolean result = graph1.hashCode() == graph2.hashCode();
375
376 Assert.assertTrue(GraphTest.SAME_HASHCODE, result);
377 }
378
379
380
381
382
383
384
385 @Test
386 public void testCongruent2() throws AbstractZemucanException {
387 final Graph graph1 = new Graph(GraphTest.NO_CONGRUENT_2, true);
388 final Graph graph2 = new Graph(GraphTest.NO_CONGRUENT_2, true);
389
390 final boolean result = graph1.hashCode() == graph2.hashCode();
391
392 Assert.assertTrue(GraphTest.SAME_HASHCODE, result);
393 }
394
395
396
397
398
399
400
401 @Test
402 public void testConstructed() throws ParameterNullException {
403 final Graph graph = new Graph("just built", false);
404
405 final GraphState actual = graph.getGraphState();
406
407 final GraphState expected = GraphState.FirstPhase;
408
409 Assert.assertEquals("Just built", expected, actual);
410 }
411
412
413
414
415
416
417
418
419 private Graph testCreateSimpleGraph() throws AbstractZemucanException {
420 final Graph graph = new Graph(GraphTest.DUMMY_GRAPH, true);
421 graph.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
422 graph.addNode(GraphTest.CREATE, GraphTest.CREATE, true);
423 graph.addNode(GraphTest.TABLE, GraphTest.TABLE, true);
424 graph.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
425 graph.firstPhaseFinished();
426 graph.addRelation(Constants.STARTING_NODE, GraphTest.CREATE);
427 graph.addRelation(GraphTest.CREATE, GraphTest.TABLE);
428 graph.addRelation(GraphTest.TABLE, Constants.ENDING_NODE);
429 graph.secondPhaseFinished();
430 return graph;
431 }
432
433
434
435
436
437
438
439
440 @Test
441 public void testDirectlySecondPhase() throws AbstractZemucanException {
442 final Graph graph = new Graph("no first phase", false);
443 graph.addNode(GraphTest.ID, GraphTest.ID, true);
444
445 Exception e = null;
446 try {
447 graph.secondPhaseFinished();
448 } catch (final Exception exception) {
449 e = exception;
450 }
451 Assert.assertTrue("InvalidGraphStateException",
452 e instanceof InvalidGraphStateException);
453 }
454
455
456
457
458
459
460
461 @Test
462 public void testDuplicatedNode() throws AbstractZemucanException {
463 final Graph graph = new Graph("duplicated node", false);
464 graph.addNode(GraphTest.ID, GraphTest.ID1, true);
465
466 Exception e = null;
467 try {
468 graph.addNode(GraphTest.ID, GraphTest.ID2, true);
469 } catch (final Exception exception) {
470 e = exception;
471 }
472 Assert.assertTrue("DuplicatedNodeException",
473 e instanceof DuplicatedNodeException);
474 }
475
476
477
478
479
480
481
482 @Test
483 public void testEmptyGrammar() throws AbstractZemucanException {
484 final Graph graph = new Graph("empty grammar", false);
485 graph.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
486 graph.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
487
488 graph.firstPhaseFinished();
489 graph.addRelation(Constants.STARTING_NODE, Constants.ENDING_NODE);
490
491 Exception e = null;
492 try {
493 graph.secondPhaseFinished();
494 } catch (final Exception exception) {
495 e = exception;
496 }
497 Assert.assertTrue("EmptyGrammarException",
498 e instanceof EmptyGrammarException);
499 }
500
501
502
503
504
505
506
507 @Test
508 public void testGetEndingNodeFirstPhase()
509 throws AbstractZemucanException {
510 final Graph graph = new Graph(GraphTest.STARTING_NODE, false);
511 graph.addNode(GraphTest.ID, GraphTest.ID, true);
512
513 Exception e = null;
514 try {
515 graph.getEndingNode();
516 } catch (final Exception exception) {
517 e = exception;
518 }
519 Assert.assertTrue("InvalidGraphStateException",
520 e instanceof InvalidGraphStateException);
521 }
522
523
524
525
526
527
528
529 @Test
530 public void testgetEndingNodeSecondPhase()
531 throws AbstractZemucanException {
532 final Graph graph = new Graph("ending node", false);
533 graph.addNode(GraphTest.ID1, GraphTest.ID1, true);
534 graph.addNode(GraphTest.ID2, GraphTest.ID2, true);
535 graph.firstPhaseFinished();
536 graph.addRelation(GraphTest.ID1, GraphTest.ID2);
537
538 Exception e = null;
539 try {
540 graph.getEndingNode();
541 } catch (final Exception exception) {
542 e = exception;
543 }
544 Assert.assertTrue("InvalidGraphStateException",
545 e instanceof InvalidGraphStateException);
546 }
547
548
549
550
551
552
553
554 @Test
555 public void testGetStartingNode() throws AbstractZemucanException {
556 final Graph graph = new Graph(GraphTest.VALIDATED_GRAPH, false);
557 graph.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
558 graph.addNode(GraphTest.CREATE, GraphTest.CREATE, true);
559 graph.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
560 graph.firstPhaseFinished();
561 graph.addRelation(Constants.STARTING_NODE, GraphTest.CREATE);
562 graph.addRelation(GraphTest.CREATE, Constants.ENDING_NODE);
563 graph.secondPhaseFinished();
564
565 final EndingNode actual1 = graph.getEndingNode();
566
567 final EndingNode expected1 = new EndingNode(graph);
568
569 Assert.assertEquals("getEndingNode", expected1, actual1);
570
571 final String actual2 = graph.getStartingNode().getId();
572
573 final String expected2 = new StartingNode(graph).getId();
574
575 Assert.assertEquals("getStartingNode", expected2, actual2);
576 }
577
578
579
580
581
582
583
584 @Test
585 public void testGetStartingNodeFirstPhase()
586 throws AbstractZemucanException {
587 final Graph graph = new Graph("starting node first phase", false);
588 graph.addNode(GraphTest.ID, GraphTest.ID, true);
589
590 Exception e = null;
591 try {
592 graph.getStartingNode();
593 } catch (final Exception exception) {
594 e = exception;
595 }
596 Assert.assertTrue("InvalidGraphStateException",
597 e instanceof InvalidGraphStateException);
598 }
599
600
601
602
603
604
605
606 @Test
607 public void testgetStartingNodeSecondPhase()
608 throws AbstractZemucanException {
609 final Graph graph = new Graph(GraphTest.STARTING_NODE, false);
610 graph.addNode(GraphTest.ID1, GraphTest.ID1, true);
611 graph.addNode(GraphTest.ID2, GraphTest.ID2, true);
612 graph.firstPhaseFinished();
613 graph.addRelation(GraphTest.ID1, GraphTest.ID2);
614
615 Exception e = null;
616 try {
617 graph.getStartingNode();
618 } catch (final Exception exception) {
619 e = exception;
620 }
621 Assert.assertTrue("InvalidGraphStateException",
622 e instanceof InvalidGraphStateException);
623 }
624
625
626
627
628
629
630
631 @Test
632 public void testHelpToString() throws AbstractZemucanException {
633 final HelpNode node = new HelpNode(new Graph("test", false));
634
635 final String actual = node.toString();
636
637 final String expected = "Type\n"
638 + "'quit' or 'exit' + ENTER to finish the application.\n"
639 + "'about' + TAB to see a short descriptive note.\n"
640 + "'license' + TAB to see the licenses.::{HELP_CONTENT_TOKEN_ID\n0\n0\nfalse|false}";
641
642 Assert.assertEquals("toString de help token", expected, actual);
643 }
644
645
646
647
648
649
650
651 @Test
652 public void testInFirstPhase() throws AbstractZemucanException {
653 final Graph graph = new Graph("In first phase", false);
654 graph.addNode(GraphTest.ID, GraphTest.ID, true);
655
656 final GraphState actual = graph.getGraphState();
657
658 final GraphState expected = GraphState.FirstPhase;
659
660 Assert.assertEquals("One node in first phase", expected, actual);
661 }
662
663
664
665
666
667
668
669 @Test
670 public void testInFirstPhaseDoubleCall()
671 throws AbstractZemucanException {
672 final Graph graph = new Graph("double call", false);
673 graph.addNode(GraphTest.ID, GraphTest.ID, true);
674
675 graph.firstPhaseFinished();
676
677 Exception e = null;
678 try {
679 graph.firstPhaseFinished();
680 } catch (final Exception exception) {
681 e = exception;
682 }
683 Assert.assertTrue("InvalidGraphStateException",
684 e instanceof InvalidGraphStateException);
685 }
686
687
688
689
690
691
692
693 @Test
694 public void testInSecondPhase() throws AbstractZemucanException {
695 final Graph graph = new Graph("In second phase", false);
696 graph.addNode(GraphTest.ID, GraphTest.ID, true);
697 graph.firstPhaseFinished();
698
699 final GraphState actual = graph.getGraphState();
700
701 final GraphState expected = GraphState.SecondPhase;
702
703 Assert.assertEquals("One node in second phase", expected, actual);
704 }
705
706
707
708
709
710
711
712 @Test
713 public void testInvalidEndingNode1() throws AbstractZemucanException {
714 final Graph graph = new Graph(GraphTest.NAME, true);
715
716 Exception e = null;
717 try {
718 graph.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, false);
719 } catch (final Exception exception) {
720 e = exception;
721 }
722 Assert.assertTrue("InvalidEndingNodeException",
723 e instanceof InvalidEndingNodeException);
724 }
725
726
727
728
729
730
731
732 @Test
733 public void testInvalidEndingNode21() throws AbstractZemucanException {
734 final Graph graph = new Graph(GraphTest.NAME, true);
735
736 Exception e = null;
737 try {
738 graph.addNode(Constants.ENDING_NODE, Constants.STARTING_NODE, true);
739 } catch (final Exception exception) {
740 e = exception;
741 }
742 Assert.assertTrue("InvalidEndingNodeException",
743 e instanceof InvalidEndingNodeException);
744 }
745
746
747
748
749
750
751
752 @Test
753 public void testInvalidStartingNode1() throws AbstractZemucanException {
754 final Graph graph = new Graph(GraphTest.NAME, true);
755
756 Exception e = null;
757 try {
758 graph.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE,
759 false);
760 } catch (final Exception exception) {
761 e = exception;
762 }
763 Assert.assertTrue("InvalidStartingNodeException",
764 e instanceof InvalidStartingNodeException);
765 }
766
767
768
769
770
771
772
773 @Test
774 public void testInvalidStartingNode2() throws AbstractZemucanException {
775 final Graph graph = new Graph(GraphTest.NAME, true);
776
777 Exception e = null;
778 try {
779 graph.addNode(Constants.STARTING_NODE, Constants.ENDING_NODE, true);
780 } catch (final Exception exception) {
781 e = exception;
782 }
783 Assert.assertTrue("InvalidStartingNodeException",
784 e instanceof InvalidStartingNodeException);
785 }
786
787
788
789
790
791
792
793 @Test
794 public void testMerge() throws AbstractZemucanException {
795 final String graphName1 = GraphTest.GRAPH1;
796 final String graphName2 = GraphTest.GRAPH2;
797 final String create = GraphTest.CREATE;
798 final Graph graph1 = new Graph(graphName1, false);
799 final Graph graph2 = new Graph(graphName2, false);
800
801 graph1.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
802 graph1.addNode(create, create, true);
803 graph1.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
804 graph1.firstPhaseFinished();
805 graph1.addRelation(Constants.STARTING_NODE, create);
806 graph1.addRelation(create, Constants.ENDING_NODE);
807 graph1.secondPhaseFinished();
808
809 graph2.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
810 graph2.addNode(create, create, true);
811 graph2.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
812 graph2.firstPhaseFinished();
813 graph2.addRelation(Constants.STARTING_NODE, create);
814 graph2.addRelation(create, Constants.ENDING_NODE);
815 graph2.secondPhaseFinished();
816
817 graph1.merge(graph2);
818
819 final String actual = graph2.toString();
820
821 String expectedGraphName = "merged:{" + graphName2 + ',' + graphName1
822 + "}:";
823 String expected = expectedGraphName
824 + '['
825 + GraphState.Validated
826 + "-WithoutExtraNodes{{"
827 + Constants.STARTING_NODE + '<' + expectedGraphName
828 + graphName2 + ':' + create + '-' + expectedGraphName
829 + graphName1 + ':'
830 + create
831 + ">}-{"
832 + Constants.ENDING_NODE + '[' + expectedGraphName + graphName2
833 + ':' + create + '-' + expectedGraphName + graphName1 + ':'
834 + create + "]}}(4:\n";
835
836 expected += expectedGraphName + graphName1 + ':' + create + '-'
837 + create + '\n';
838 expected += expectedGraphName + graphName2 + ':'
839 + Constants.ENDING_NODE + '-' + expectedGraphName + graphName2
840 + ':' + Constants.ENDING_NODE + '\n';
841 expected += expectedGraphName + graphName2 + ':'
842 + Constants.STARTING_NODE + '-' + expectedGraphName
843 + graphName2 + ':' + Constants.STARTING_NODE + '\n';
844 expected += expectedGraphName + graphName2 + ':' + create + '-'
845 + create + '\n';
846 expected += ")]";
847
848 Assert.assertEquals("Simplify merge 1", expected, actual);
849 }
850
851
852
853
854
855
856
857 @Test
858 public void testMergeAndSimplify() throws AbstractZemucanException {
859 final String graphName1 = GraphTest.GRAPH1;
860 final String graphName2 = GraphTest.GRAPH2;
861 final String create = GraphTest.CREATE;
862 final Graph graph1 = new Graph(graphName1, false);
863 final Graph graph2 = new Graph(graphName2, false);
864
865 graph1.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
866 graph1.addNode(create, create, true);
867 graph1.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
868 graph1.firstPhaseFinished();
869 graph1.addRelation(Constants.STARTING_NODE, create);
870 graph1.addRelation(create, Constants.ENDING_NODE);
871 graph1.secondPhaseFinished();
872
873 graph2.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
874 graph2.addNode(create, create, true);
875 graph2.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
876 graph2.firstPhaseFinished();
877 graph2.addRelation(Constants.STARTING_NODE, create);
878 graph2.addRelation(create, Constants.ENDING_NODE);
879 graph2.secondPhaseFinished();
880
881 graph1.merge(graph2);
882
883 graph2.simplifyGraph();
884
885 final String actual = graph2.toString();
886
887 String expectedGraphName = "merged:{" + graphName2 + ',' + graphName1
888 + "}:";
889 String expected = expectedGraphName + '[' + GraphState.Validated
890 + "-WithoutExtraNodes{{" + Constants.STARTING_NODE + '<'
891 + expectedGraphName + graphName2 + ':' + create + ">}-{"
892 + Constants.ENDING_NODE + '[' + expectedGraphName + graphName1
893 + ':' + create + "]}}(3:\n";
894 expected += expectedGraphName + graphName2 + ':'
895 + Constants.ENDING_NODE + '-' + expectedGraphName + graphName2
896 + ':' + Constants.ENDING_NODE + '\n';
897 expected += expectedGraphName + graphName2 + ':'
898 + Constants.STARTING_NODE + '-' + expectedGraphName
899 + graphName2 + ':' + Constants.STARTING_NODE + '\n';
900 expected += expectedGraphName + graphName2 + ':' + create + '-'
901 + create + '\n';
902 expected += ")]";
903
904 Assert.assertEquals("Merge and Simplify", expected, actual);
905 }
906
907
908
909
910
911
912
913 @Test(expected = AssertionError.class)
914 public void testMergeIdentical() throws AbstractZemucanException {
915 final Graph graph1 = this.testCreateSimpleGraph();
916 final Graph graph2 = this.testCreateSimpleGraph();
917 graph1.merge(graph2);
918 }
919
920
921
922
923
924
925
926 @Test(expected = AssertionError.class)
927 public void testMergeNotValidated1() throws AbstractZemucanException {
928 final Graph graph1 = new Graph(GraphTest.GRAPH1, false);
929 final Graph graph2 = new Graph(GraphTest.GRAPH2, false);
930
931 graph2.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
932 graph2.addNode(GraphTest.CREATE, GraphTest.CREATE, true);
933 graph2.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
934 graph2.firstPhaseFinished();
935 graph2.addRelation(Constants.STARTING_NODE, GraphTest.CREATE);
936 graph2.addRelation(GraphTest.CREATE, Constants.ENDING_NODE);
937 graph2.secondPhaseFinished();
938
939 graph1.merge(graph2);
940 }
941
942
943
944
945
946
947
948 @Test(expected = AssertionError.class)
949 public void testMergeNotValidated2() throws AbstractZemucanException {
950 final Graph graph1 = new Graph(GraphTest.GRAPH1, false);
951 final Graph graph2 = new Graph(GraphTest.GRAPH2, false);
952
953 graph1.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
954 graph1.addNode(GraphTest.CREATE, GraphTest.CREATE, true);
955 graph1.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
956 graph1.firstPhaseFinished();
957 graph1.addRelation(Constants.STARTING_NODE, GraphTest.CREATE);
958 graph1.addRelation(GraphTest.CREATE, Constants.ENDING_NODE);
959 graph1.secondPhaseFinished();
960
961 graph1.merge(graph2);
962 }
963
964
965
966
967
968
969
970 @Test(expected = AssertionError.class)
971 public void testMergeSame() throws AbstractZemucanException {
972 final Graph graph = this.testCreateSimpleGraph();
973 graph.merge(graph);
974 }
975
976
977
978
979
980
981
982 @Test
983 public void testNoAboutTokenConfigured()
984 throws AbstractZemucanException {
985
986 System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
987 "sa_conf-test-NoAboutToken.xml");
988 Configurator.destroyInstance();
989
990 String graphName = "no about token";
991 final Graph graph = new Graph(graphName, true);
992 graph.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
993 graph.addNode(GraphTest.CREATE, GraphTest.CREATE, true);
994 graph.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
995 graph.firstPhaseFinished();
996 graph.addRelation(Constants.STARTING_NODE, GraphTest.CREATE);
997 graph.addRelation(GraphTest.CREATE, Constants.ENDING_NODE);
998 graph.secondPhaseFinished();
999
1000 final StartingNode startingNode = graph.getStartingNode();
1001
1002 final List<AbstractGraphNode> nodes = startingNode.getChildren();
1003 boolean found = false;
1004 for (final AbstractGraphNode node : nodes) {
1005 if (node.getId().equals(graphName + ':' + Constants.ABOUT_TOKEN_ID)) {
1006 found = true;
1007 }
1008 }
1009
1010 System.clearProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY);
1011
1012 Assert.assertTrue("Default about token", found);
1013 }
1014
1015
1016
1017
1018
1019
1020
1021
1022 @Test
1023 public void testNodeNameDuplicated() throws AbstractZemucanException {
1024 final Graph graph = new Graph("Duplicated node name", true);
1025 graph.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
1026 graph.addNode(GraphTest.CREATE, GraphTest.CREATE, true);
1027 graph.addNode(GraphTest.TABLE, GraphTest.TABLE, true);
1028 graph.addNode("tablespace", GraphTest.TABLE, true);
1029 graph.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
1030 graph.firstPhaseFinished();
1031 graph.addRelation(Constants.STARTING_NODE, GraphTest.CREATE);
1032 graph.addRelation(GraphTest.CREATE, GraphTest.TABLE);
1033 graph.addRelation(GraphTest.CREATE, "tablespace");
1034 graph.addRelation(GraphTest.TABLE, Constants.ENDING_NODE);
1035 graph.addRelation("tablespace", Constants.ENDING_NODE);
1036
1037 Exception e = null;
1038 try {
1039 graph.secondPhaseFinished();
1040 } catch (final Exception exception) {
1041 e = exception;
1042 }
1043 Assert.assertTrue("DuplicatedNodeNameException",
1044 e instanceof DuplicatedNodeNameException);
1045 }
1046
1047
1048
1049
1050
1051
1052
1053 @Test
1054 public void testNoEndingNode() throws AbstractZemucanException {
1055 final Graph graph = new Graph("no ending node", false);
1056 graph.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
1057 graph.addNode(GraphTest.CREATE, GraphTest.CREATE, true);
1058 graph.firstPhaseFinished();
1059 graph.addRelation(Constants.STARTING_NODE, GraphTest.CREATE);
1060
1061 Exception e = null;
1062 try {
1063 graph.secondPhaseFinished();
1064 } catch (final Exception exception) {
1065 e = exception;
1066 }
1067 Assert.assertTrue("EndingNodeNotDefinedException",
1068 e instanceof EndingNodeNotDefinedException);
1069 }
1070
1071
1072
1073
1074
1075
1076
1077 @Test
1078 public void testNoHelpTokenConfigured() throws AbstractZemucanException {
1079
1080 System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
1081 "sa_conf-test-NoHelpToken.xml");
1082 Configurator.destroyInstance();
1083 String graphName = "no help token";
1084
1085 final Graph graph = new Graph(graphName, true);
1086 graph.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
1087 graph.addNode(GraphTest.CREATE, GraphTest.CREATE, true);
1088 graph.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
1089 graph.firstPhaseFinished();
1090 graph.addRelation(Constants.STARTING_NODE, GraphTest.CREATE);
1091 graph.addRelation(GraphTest.CREATE, Constants.ENDING_NODE);
1092 graph.secondPhaseFinished();
1093
1094 final StartingNode startingNode = graph.getStartingNode();
1095
1096 final List<AbstractGraphNode> nodes = startingNode.getChildren();
1097 boolean found = false;
1098 for (final AbstractGraphNode node : nodes) {
1099 if (node.getId().equals(graphName + ':' + Constants.HELP_TOKEN_ID)) {
1100 found = true;
1101 }
1102 }
1103
1104 System.clearProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY);
1105
1106 Assert.assertTrue("Default help token", found);
1107 }
1108
1109
1110
1111
1112
1113
1114
1115 @Test
1116 public void testNoLicenseTokenConfigured()
1117 throws AbstractZemucanException {
1118
1119 System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
1120 "sa_conf-test-NoLicenseToken.xml");
1121 Configurator.destroyInstance();
1122 String graphName = "no license token";
1123
1124 final Graph graph = new Graph(graphName, true);
1125 graph.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
1126 graph.addNode(GraphTest.CREATE, GraphTest.CREATE, true);
1127 graph.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
1128 graph.firstPhaseFinished();
1129 graph.addRelation(Constants.STARTING_NODE, GraphTest.CREATE);
1130 graph.addRelation(GraphTest.CREATE, Constants.ENDING_NODE);
1131 graph.secondPhaseFinished();
1132
1133 final StartingNode startingNode = graph.getStartingNode();
1134
1135 final List<AbstractGraphNode> nodes = startingNode.getChildren();
1136 boolean found = false;
1137 for (final AbstractGraphNode node : nodes) {
1138 if (node.getId().equals(
1139 graphName + ':' + Constants.LICENSE_TOKEN_ID)) {
1140 found = true;
1141 }
1142 }
1143
1144 System.clearProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY);
1145
1146 Assert.assertTrue("Default license token", found);
1147 }
1148
1149
1150
1151
1152
1153
1154
1155 @Test
1156 public void testNoStartingNode() throws AbstractZemucanException {
1157 final Graph graph = new Graph("no starting node", false);
1158 graph.addNode(GraphTest.CREATE, GraphTest.CREATE, true);
1159 graph.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
1160 graph.firstPhaseFinished();
1161 graph.addRelation(GraphTest.CREATE, Constants.ENDING_NODE);
1162
1163 Exception e = null;
1164 try {
1165 graph.secondPhaseFinished();
1166 } catch (final Exception exception) {
1167 e = exception;
1168 }
1169 Assert.assertTrue("StartingNodeNotDefinedException",
1170 e instanceof StartingNodeNotDefinedException);
1171 }
1172
1173
1174
1175
1176
1177
1178
1179 @Test
1180 public void testNotComingFromStartingNode()
1181 throws AbstractZemucanException {
1182 final Graph graph = new Graph("not coming starting node", true);
1183 graph.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
1184 graph.addNode(GraphTest.CREATE, GraphTest.CREATE, true);
1185 graph.addNode(GraphTest.TABLE, GraphTest.TABLE, true);
1186 graph.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
1187
1188 graph.addNode(GraphTest.OTHER, GraphTest.OTHER, true);
1189
1190 graph.firstPhaseFinished();
1191 graph.addRelation(Constants.STARTING_NODE, GraphTest.CREATE);
1192 graph.addRelation(GraphTest.CREATE, GraphTest.TABLE);
1193 graph.addRelation(GraphTest.TABLE, Constants.ENDING_NODE);
1194
1195 graph.addRelation(GraphTest.OTHER, GraphTest.CREATE);
1196
1197 Exception e = null;
1198 try {
1199 graph.secondPhaseFinished();
1200 } catch (final Exception exception) {
1201 e = exception;
1202 }
1203 Assert.assertTrue("NodeNotComesFromStartingNodeException",
1204 e instanceof NodeNotComesFromStartingNodeException);
1205 }
1206
1207
1208
1209
1210
1211
1212
1213 @Test
1214 public void testNotEqualsFirstPhaseGraph()
1215 throws AbstractZemucanException {
1216 final Graph graph1 = this.testCreateSimpleGraph();
1217 final Graph graph2 = new Graph(GraphTest.TEST, false);
1218 graph2.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
1219 graph2.addNode(GraphTest.CREATE, GraphTest.CREATE, true);
1220 graph2.addNode(GraphTest.TABLE, GraphTest.TABLE, true);
1221 graph2.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
1222 graph2.firstPhaseFinished();
1223
1224 boolean result = graph1.equals(graph2);
1225 Assert.assertFalse(GraphTest.NOT_EQUALS_TO_FIRST_PHASE_GRAPH, result);
1226
1227 result = graph2.equals(graph1);
1228 Assert.assertFalse(GraphTest.NOT_EQUALS_TO_FIRST_PHASE_GRAPH, result);
1229 }
1230
1231
1232
1233
1234
1235
1236
1237 @Test
1238 public void testNotEqualsNull() throws AbstractZemucanException {
1239 final Graph graph1 = this.testCreateSimpleGraph();
1240
1241 final boolean result = graph1.equals(null);
1242
1243 Assert.assertFalse("not equals to null", result);
1244 }
1245
1246
1247
1248
1249
1250
1251
1252 @Test
1253 public void testNotEqualsOtherObject() throws AbstractZemucanException {
1254 final Graph graph1 = this.testCreateSimpleGraph();
1255 final StartingNode node = new StartingNode(graph1);
1256
1257 final boolean result = graph1.equals(node);
1258
1259 Assert.assertFalse("not equals to StartingNode", result);
1260 }
1261
1262
1263
1264
1265
1266
1267
1268 @Test
1269 public void testNotEqualsSimilarGraph() throws AbstractZemucanException {
1270 final Graph graph1 = this.testCreateSimpleGraph();
1271 final Graph graph2 = new Graph(GraphTest.DUMMY_GRAPH, true);
1272 graph2.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
1273 graph2.addNode(GraphTest.CREATE, GraphTest.CREATE, true);
1274 graph2.addNode(GraphTest.OTHER, GraphTest.OTHER, true);
1275 graph2.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
1276 graph2.firstPhaseFinished();
1277 graph2.addRelation(Constants.STARTING_NODE, GraphTest.CREATE);
1278 graph2.addRelation(GraphTest.CREATE, GraphTest.OTHER);
1279 graph2.addRelation(GraphTest.OTHER, Constants.ENDING_NODE);
1280 graph2.secondPhaseFinished();
1281
1282 boolean result = graph1.equals(graph2);
1283 Assert.assertFalse(GraphTest.NOT_EQUALS_TO_SIMILAR_GRAPH, result);
1284
1285 result = graph2.equals(graph1);
1286 Assert.assertFalse(GraphTest.NOT_EQUALS_TO_SIMILAR_GRAPH, result);
1287 }
1288
1289
1290
1291
1292
1293
1294
1295 @Test
1296 public void testNotEqualsSimpleGraph() throws AbstractZemucanException {
1297 final Graph graph1 = this.testCreateSimpleGraph();
1298 final Graph graph2 = new Graph(GraphTest.TEST, false);
1299
1300 boolean result = graph1.equals(graph2);
1301 Assert.assertFalse(GraphTest.NOT_EQUALS_TO_SIMPLE_GRAPH, result);
1302
1303 result = graph2.equals(graph1);
1304 Assert.assertFalse(GraphTest.NOT_EQUALS_TO_SIMPLE_GRAPH, result);
1305 }
1306
1307
1308
1309
1310
1311
1312
1313 @Test
1314 public void testNotGoingToEndingNode() throws AbstractZemucanException {
1315 final Graph graph = new Graph("not going ending node", true);
1316 graph.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
1317 graph.addNode(GraphTest.CREATE, GraphTest.CREATE, true);
1318 graph.addNode(GraphTest.TABLE, GraphTest.TABLE, true);
1319 graph.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
1320
1321 graph.addNode(GraphTest.OTHER, GraphTest.OTHER, true);
1322
1323 graph.firstPhaseFinished();
1324 graph.addRelation(Constants.STARTING_NODE, GraphTest.CREATE);
1325 graph.addRelation(GraphTest.CREATE, GraphTest.TABLE);
1326 graph.addRelation(GraphTest.TABLE, Constants.ENDING_NODE);
1327
1328 graph.addRelation(GraphTest.TABLE, GraphTest.OTHER);
1329
1330 Exception e = null;
1331 try {
1332 graph.secondPhaseFinished();
1333 } catch (final Exception exception) {
1334 e = exception;
1335 }
1336 Assert.assertTrue("NodeNotGoesToEndingNodeException",
1337 e instanceof NodeNotGoesToEndingNodeException);
1338 }
1339
1340
1341
1342
1343
1344
1345
1346 @Test
1347 public void testNullValue() throws ParameterNullException {
1348 Exception e = null;
1349 try {
1350 new Graph(null, false);
1351 } catch (final Exception exception) {
1352 e = exception;
1353 }
1354 Assert.assertTrue("ParameterNullException",
1355 e instanceof ParameterNullException);
1356 }
1357
1358
1359
1360
1361
1362
1363
1364 @Test
1365 public void testReferencingEndingNode() throws AbstractZemucanException {
1366 final Graph graph = new Graph("referencing ending node", true);
1367 graph.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
1368 graph.addNode(GraphTest.CREATE, GraphTest.CREATE, true);
1369 graph.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
1370 graph.firstPhaseFinished();
1371 graph.addRelation(Constants.STARTING_NODE, GraphTest.CREATE);
1372 graph.addRelation(GraphTest.CREATE, Constants.ENDING_NODE);
1373
1374 Exception e = null;
1375 try {
1376 graph.addRelation(Constants.ENDING_NODE, GraphTest.CREATE);
1377 } catch (final Exception exception) {
1378 e = exception;
1379 }
1380 Assert.assertTrue("ChildEndingNodeException",
1381 e instanceof ChildEndingNodeException);
1382 }
1383
1384
1385
1386
1387
1388
1389
1390 @Test
1391 public void testReferencingStartingNode()
1392 throws AbstractZemucanException {
1393 final Graph graph = new Graph("referencing starting node", true);
1394 graph.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
1395 graph.addNode(GraphTest.CREATE, GraphTest.CREATE, true);
1396 graph.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
1397 graph.firstPhaseFinished();
1398 graph.addRelation(Constants.STARTING_NODE, GraphTest.CREATE);
1399 graph.addRelation(GraphTest.CREATE, Constants.ENDING_NODE);
1400
1401 Exception e = null;
1402 try {
1403 graph.addRelation(GraphTest.CREATE, Constants.STARTING_NODE);
1404 } catch (final Exception exception) {
1405 e = exception;
1406 }
1407 Assert.assertTrue("ReferencingStartingNodeException",
1408 e instanceof ReferencingStartingNodeException);
1409 }
1410
1411
1412
1413
1414
1415
1416
1417 @Test
1418 public void testReflexive() throws AbstractZemucanException {
1419 final Graph graph1 = this.testCreateSimpleGraph();
1420 final Graph graph2 = this.testCreateSimpleGraph();
1421
1422 final boolean val1 = graph1.equals(graph2);
1423 final boolean val2 = graph2.equals(graph1);
1424
1425 final boolean result = val1 == val2;
1426
1427 Assert.assertTrue("reflexivity", result);
1428 }
1429
1430
1431
1432
1433
1434
1435
1436 @Test
1437 public void testSimplify1() throws AbstractZemucanException {
1438 final String graphName = GraphTest.GRAPH2;
1439 final Graph graph1 = new Graph(GraphTest.GRAPH1, false);
1440 final Graph graph2 = new Graph(GraphTest.GRAPH2, false);
1441
1442 graph1.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
1443 graph1.addNode(GraphTest.CREATE1, GraphTest.CREATE, true);
1444 graph1.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
1445 graph1.firstPhaseFinished();
1446 graph1.addRelation(Constants.STARTING_NODE, GraphTest.CREATE1);
1447 graph1.addRelation(GraphTest.CREATE1, Constants.ENDING_NODE);
1448 graph1.secondPhaseFinished();
1449
1450 graph2.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
1451 graph2.addNode(GraphTest.CREATE2, GraphTest.CREATE, true);
1452 graph2.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
1453 graph2.firstPhaseFinished();
1454 graph2.addRelation(Constants.STARTING_NODE, GraphTest.CREATE2);
1455 graph2.addRelation(GraphTest.CREATE2, Constants.ENDING_NODE);
1456 graph2.secondPhaseFinished();
1457
1458 graph1.merge(graph2);
1459
1460 graph2.simplifyGraph();
1461
1462 final String actual = graph2.toString();
1463
1464 String expectedGraphName = "merged:{" + GraphTest.GRAPH2 + ','
1465 + GraphTest.GRAPH1 + "}:";
1466 String expected = expectedGraphName + '[' + GraphState.Validated
1467 + "-WithoutExtraNodes{{" + Constants.STARTING_NODE + '<'
1468 + expectedGraphName + graphName + ':' + GraphTest.CREATE2
1469 + ">}-{" + Constants.ENDING_NODE + '[' + expectedGraphName
1470 + graphName + ':' + GraphTest.CREATE2 + "]}}(3:\n";
1471
1472 expected += expectedGraphName + graphName + ':' + Constants.ENDING_NODE
1473 + '-' + expectedGraphName + graphName + ':'
1474 + Constants.ENDING_NODE + '\n';
1475 expected += expectedGraphName + graphName + ':'
1476 + Constants.STARTING_NODE + '-' + expectedGraphName + graphName
1477 + ':' + Constants.STARTING_NODE + '\n';
1478 expected += expectedGraphName + graphName + ':' + GraphTest.CREATE2
1479 + '-' + GraphTest.CREATE + '\n';
1480 expected += ")]";
1481
1482 Assert.assertEquals("Simplify graph 1", expected, actual);
1483 }
1484
1485
1486
1487
1488
1489
1490
1491 @Test
1492 public void testSimplify2() throws AbstractZemucanException {
1493 final String graphName1 = GraphTest.GRAPH1;
1494 final String graphName2 = GraphTest.GRAPH2;
1495 final String create1Id = GraphTest.CREATE1;
1496 final String create2Id = GraphTest.CREATE2;
1497 final String create = GraphTest.CREATE;
1498 final String a = GraphTest.A;
1499 final Graph graph1 = new Graph(graphName1, false);
1500 final Graph graph2 = new Graph(graphName2, false);
1501
1502 graph1.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
1503 graph1.addNode(a, a, true);
1504 graph1.addNode(create1Id, create, true);
1505 graph1.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
1506 graph1.firstPhaseFinished();
1507 graph1.addRelation(Constants.STARTING_NODE, a);
1508 graph1.addRelation(Constants.STARTING_NODE, create1Id);
1509 graph1.addRelation(a, Constants.ENDING_NODE);
1510 graph1.addRelation(create1Id, Constants.ENDING_NODE);
1511 graph1.secondPhaseFinished();
1512
1513 graph2.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
1514 graph2.addNode(a, a, true);
1515 graph2.addNode(create2Id, create, true);
1516 graph2.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
1517 graph2.firstPhaseFinished();
1518 graph2.addRelation(Constants.STARTING_NODE, a);
1519 graph2.addRelation(Constants.STARTING_NODE, create2Id);
1520 graph2.addRelation(a, Constants.ENDING_NODE);
1521 graph2.addRelation(create2Id, Constants.ENDING_NODE);
1522 graph2.secondPhaseFinished();
1523
1524 graph2.merge(graph1);
1525
1526 graph1.simplifyGraph();
1527
1528 final String actual = graph1.toString();
1529
1530 String expectedGraphName = "merged:{" + graphName1 + ',' + graphName2
1531 + "}:";
1532 String expected = expectedGraphName + '[' + GraphState.Validated
1533 + "-WithoutExtraNodes{{" + Constants.STARTING_NODE + '<'
1534 + expectedGraphName + graphName1 + ':' + a + '-'
1535 + expectedGraphName + graphName1 + ':' + create1Id + ">}-{"
1536 + Constants.ENDING_NODE + '[' + expectedGraphName + graphName1
1537 + ':' + create1Id + '-' + expectedGraphName + graphName2 + ':'
1538 + a + "]}}(4:\n";
1539
1540 expected += expectedGraphName + graphName1 + ':'
1541 + Constants.ENDING_NODE + '-' + expectedGraphName + graphName1
1542 + ':' + Constants.ENDING_NODE + '\n';
1543 expected += expectedGraphName + graphName1 + ':'
1544 + Constants.STARTING_NODE + '-' + expectedGraphName
1545 + graphName1 + ':' + Constants.STARTING_NODE + '\n';
1546 expected += expectedGraphName + graphName1 + ':' + a + '-' + a + '\n';
1547 expected += expectedGraphName + graphName1 + ':' + create1Id + '-'
1548 + create + '\n';
1549 expected += ")]";
1550
1551 Assert.assertEquals("Simplify graph 2", expected, actual);
1552 }
1553
1554
1555
1556
1557
1558
1559
1560 @Test
1561 public void testSimplify3() throws AbstractZemucanException {
1562 final String graphName1 = GraphTest.GRAPH1;
1563 final String graphName2 = GraphTest.GRAPH2;
1564 final String create1Id = GraphTest.CREATE1;
1565 final String create2Id = GraphTest.CREATE2;
1566 final String create = GraphTest.CREATE;
1567 final String z = "z";
1568 final Graph graph1 = new Graph(graphName1, false);
1569 final Graph graph2 = new Graph(graphName2, false);
1570
1571 graph1.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
1572 graph1.addNode(create1Id, GraphTest.CREATE, true);
1573 graph1.addNode(z, z, true);
1574 graph1.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
1575 graph1.firstPhaseFinished();
1576 graph1.addRelation(Constants.STARTING_NODE, create1Id);
1577 graph1.addRelation(Constants.STARTING_NODE, z);
1578 graph1.addRelation(create1Id, Constants.ENDING_NODE);
1579 graph1.addRelation(z, Constants.ENDING_NODE);
1580 graph1.secondPhaseFinished();
1581
1582 graph2.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
1583 graph2.addNode(create2Id, GraphTest.CREATE, true);
1584 graph2.addNode(z, z, true);
1585 graph2.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
1586 graph2.firstPhaseFinished();
1587 graph2.addRelation(Constants.STARTING_NODE, create2Id);
1588 graph2.addRelation(Constants.STARTING_NODE, z);
1589 graph2.addRelation(create2Id, Constants.ENDING_NODE);
1590 graph2.addRelation(z, Constants.ENDING_NODE);
1591 graph2.secondPhaseFinished();
1592
1593 graph2.merge(graph1);
1594 graph1.simplifyGraph();
1595
1596 final String actual = graph1.toString();
1597
1598 String expectedGraphName = "merged:{" + graphName1 + ',' + graphName2
1599 + "}:";
1600 String expected = expectedGraphName + '[' + GraphState.Validated
1601 + "-WithoutExtraNodes{{" + Constants.STARTING_NODE + '<'
1602 + expectedGraphName + graphName1 + ':' + create1Id + '-'
1603 + expectedGraphName + graphName1 + ':' + z + ">}-{"
1604 + Constants.ENDING_NODE + '[' + expectedGraphName + graphName1
1605 + ':' + create1Id + '-' + expectedGraphName + graphName2 + ':'
1606 + z + "]}}(4:\n";
1607
1608 expected += expectedGraphName + graphName1 + ':'
1609 + Constants.ENDING_NODE + '-' + expectedGraphName + graphName1
1610 + ':' + Constants.ENDING_NODE + '\n';
1611 expected += expectedGraphName + graphName1 + ':'
1612 + Constants.STARTING_NODE + '-' + expectedGraphName
1613 + graphName1 + ':' + Constants.STARTING_NODE + '\n';
1614 expected += expectedGraphName + graphName1 + ':' + create1Id + '-'
1615 + create + '\n';
1616 expected += expectedGraphName + graphName1 + ':' + z + '-' + z + '\n';
1617 expected += ")]";
1618
1619 Assert.assertEquals("Simplify graph 3", expected, actual);
1620 }
1621
1622
1623
1624
1625
1626
1627
1628 @Test
1629 public void testSimplify4() throws AbstractZemucanException {
1630 final String graphName1 = GraphTest.GRAPH1;
1631 final String graphName2 = GraphTest.GRAPH;
1632 final String a = GraphTest.A;
1633 final String create1Id = "b-create1";
1634 final String c = "c";
1635 final String create2Id = "d-create2";
1636 final String e = "e";
1637 final String create = GraphTest.CREATE;
1638
1639 final Graph graph1 = this.testSimplify4Graph(graphName1, a, create1Id,
1640 e, create);
1641 final Graph graph2 = this.testSimplify4Graph(graphName2, c, create2Id,
1642 e, create);
1643
1644 graph2.merge(graph1);
1645
1646 graph1.simplifyGraph();
1647
1648 final String actual = graph1.toString();
1649
1650 final String expected = this.testSimplify4Text(graphName1, graphName2,
1651 a, create1Id, c, e, create);
1652
1653 Assert.assertEquals("Simplify graph 4", expected, actual);
1654 }
1655
1656 private Graph testSimplify4Graph(final String graphName, final String a,
1657 final String create1Id, final String e, final String create)
1658 throws AbstractZemucanException {
1659 final Graph graph = new Graph(graphName, false);
1660
1661 graph.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
1662 graph.addNode(a, a, true);
1663 graph.addNode(create1Id, create, true);
1664 graph.addNode(e, e, true);
1665 graph.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
1666 graph.firstPhaseFinished();
1667 graph.addRelation(Constants.STARTING_NODE, a);
1668 graph.addRelation(Constants.STARTING_NODE, create1Id);
1669 graph.addRelation(Constants.STARTING_NODE, e);
1670 graph.addRelation(a, Constants.ENDING_NODE);
1671 graph.addRelation(create1Id, Constants.ENDING_NODE);
1672 graph.addRelation(e, Constants.ENDING_NODE);
1673 graph.secondPhaseFinished();
1674 return graph;
1675 }
1676
1677 private String testSimplify4Text(final String graphName1,
1678 final String graphName2, final String a, final String create1Id,
1679 final String c, final String e, final String create) {
1680 String expectedGraphName = "merged:{" + graphName1 + ',' + graphName2
1681 + "}:";
1682 String expected = expectedGraphName + '[' + GraphState.Validated
1683 + "-WithoutExtraNodes{{" + Constants.STARTING_NODE + '<'
1684 + expectedGraphName + graphName1 + ':' + a + '-'
1685 + expectedGraphName + graphName1 + ':' + create1Id + '-'
1686 + expectedGraphName + graphName1 + ':' + e + '-'
1687 + expectedGraphName + graphName2 + ':' + c + ">}-{"
1688 + Constants.ENDING_NODE + '[' + expectedGraphName + graphName1
1689 + ':' + a + '-' + expectedGraphName + graphName1 + ':'
1690 + create1Id + '-' + expectedGraphName + graphName2 + ':' + c
1691 + '-' + expectedGraphName + graphName2 + ':' + e + "]}}(6:\n";
1692 expected += expectedGraphName + graphName1 + ':'
1693 + Constants.ENDING_NODE + '-' + expectedGraphName + graphName1
1694 + ':' + Constants.ENDING_NODE + '\n';
1695 expected += expectedGraphName + graphName1 + ':'
1696 + Constants.STARTING_NODE + '-' + expectedGraphName
1697 + graphName1 + ':' + Constants.STARTING_NODE + '\n';
1698 expected += expectedGraphName + graphName1 + ':' + a + '-' + a + '\n';
1699 expected += expectedGraphName + graphName1 + ':' + create1Id + '-'
1700 + create + '\n';
1701 expected += expectedGraphName + graphName1 + ':' + e + '-' + e + '\n';
1702 expected += expectedGraphName + graphName2 + ':' + c + '-' + c + '\n';
1703 expected += ")]";
1704 return expected;
1705 }
1706
1707
1708
1709
1710
1711
1712
1713 @Test
1714 public void testSymmetric() throws AbstractZemucanException {
1715 final Graph graph1 = this.testCreateSimpleGraph();
1716
1717 final boolean result = graph1.equals(graph1);
1718
1719 Assert.assertTrue("symmetry", result);
1720 }
1721
1722
1723
1724
1725
1726
1727
1728 @Test
1729 public void testToString2() throws AbstractZemucanException {
1730 final String name = GraphTest.NAME;
1731 final Graph graph = new Graph(name, false);
1732
1733 final String actual = graph.toString();
1734
1735 final String expected = name + ":[" + GraphState.FirstPhase
1736 + "-WithoutExtraNodes{null-null}(0:\n)]";
1737
1738 Assert.assertEquals(GraphTest.TO_STRING, expected, actual);
1739 }
1740
1741
1742
1743
1744
1745
1746
1747 @Test
1748 public void testToStringExtraNode() throws AbstractZemucanException {
1749 final String name = GraphTest.NAME;
1750 final Graph graph = new Graph(name, true);
1751 graph.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
1752 graph.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
1753 final String id = "CREA";
1754 final String nameNode = GraphTest.CREATE;
1755 graph.addNode(id, nameNode, true);
1756 graph.firstPhaseFinished();
1757 graph.addRelation(Constants.STARTING_NODE, id);
1758 graph.addRelation(id, Constants.ENDING_NODE);
1759 graph.secondPhaseFinished();
1760
1761 final String actual = graph.toString();
1762
1763 final String aboutContentMessage = new AboutNode(graph).getId();
1764 final String licenseContentMessage = new LicenseNode(graph).getId();
1765 final String helpContentMessage = new HelpNode(graph).getId();
1766 String expectedGraphName = name + ':';
1767 String expected = expectedGraphName
1768 + '['
1769 + GraphState.Validated
1770 + "-WithExtraNodes{{" + Constants.STARTING_NODE + "<"
1771 + expectedGraphName + Constants.ABOUT_TOKEN_ID + '-'
1772 + expectedGraphName + Constants.HELP_TOKEN_ID + '-'
1773 + expectedGraphName + Constants.LICENSE_TOKEN_ID + '-'
1774 + expectedGraphName + id + ">}-{" + Constants.ENDING_NODE + '['
1775 + aboutContentMessage + '-' + helpContentMessage + '-'
1776 + licenseContentMessage + '-' + name + ':' + id + "]}}(9:\n";
1777 expected += expectedGraphName + Constants.ABOUT_CONTENT_TOKEN_ID + '-'
1778 + AboutNode.readAboutInformation() + '\n';
1779 expected += expectedGraphName + Constants.ABOUT_TOKEN_ID + '-'
1780 + Constants.ABOUT_TOKEN_VALUE + '\n';
1781 expected += expectedGraphName + id + '-' + nameNode + '\n';
1782 expected += expectedGraphName + Constants.ENDING_NODE + '-'
1783 + expectedGraphName + Constants.ENDING_NODE + '\n';
1784 expected += expectedGraphName + Constants.HELP_CONTENT_TOKEN_ID + '-'
1785 + new HelpNode(graph).getName() + '\n';
1786 expected += expectedGraphName + Constants.HELP_TOKEN_ID + '-'
1787 + Constants.HELP_TOKEN_VALUE + '\n';
1788 expected += expectedGraphName + Constants.LICENSE_CONTENT_TOKEN_ID
1789 + '-' + LicenseNode.readLicense() + '\n';
1790 expected += expectedGraphName + Constants.LICENSE_TOKEN_ID + '-'
1791 + Constants.LICENSE_TOKEN_VALUE + '\n';
1792 expected += expectedGraphName + Constants.STARTING_NODE + '-'
1793 + expectedGraphName + Constants.STARTING_NODE + '\n';
1794 expected += ")]";
1795
1796 Assert.assertEquals(GraphTest.TO_STRING, expected, actual);
1797 }
1798
1799
1800
1801
1802
1803
1804
1805 @Test
1806 public void testToStringWithoutExtraNode()
1807 throws AbstractZemucanException {
1808 final String name = GraphTest.NAME;
1809 final Graph graph = new Graph(name, false);
1810 graph.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
1811 graph.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
1812 final String id = "CREA";
1813 final String nameNode = GraphTest.CREATE;
1814 graph.addNode(id, nameNode, true);
1815 graph.firstPhaseFinished();
1816 graph.addRelation(Constants.STARTING_NODE, id);
1817 graph.addRelation(id, Constants.ENDING_NODE);
1818 graph.secondPhaseFinished();
1819
1820 final String actual = graph.toString();
1821
1822 String licenseContentMessage = new AboutNode(graph).toString();
1823 licenseContentMessage = licenseContentMessage.substring(
1824 licenseContentMessage.indexOf(GraphTest.OPEN_BRAKET) + 1,
1825 licenseContentMessage.indexOf(GraphTest.PIPE));
1826 String helpContentMessage = new HelpNode(graph).toString();
1827 helpContentMessage = helpContentMessage.substring(
1828 helpContentMessage.indexOf(GraphTest.OPEN_BRAKET) + 1,
1829 helpContentMessage.indexOf(GraphTest.PIPE));
1830 String expected = name
1831 + ":[" + GraphState.Validated
1832 + "-WithoutExtraNodes{{" + Constants.STARTING_NODE + "<" + name
1833 + ':' + id + ">}-{" + Constants.ENDING_NODE + '[' + name + ':'
1834 + id + "]}}(3:\n";
1835 expected += name + ':' + id + '-' + nameNode + '\n';
1836 expected += name + ':' + Constants.ENDING_NODE + '-' + name + ':'
1837 + Constants.ENDING_NODE + '\n';
1838 expected += name + ':' + Constants.STARTING_NODE + '-' + name + ':'
1839 + Constants.STARTING_NODE + '\n';
1840 expected += ")]";
1841
1842 Assert.assertEquals(GraphTest.TO_STRING, expected, actual);
1843 }
1844
1845
1846
1847
1848
1849
1850
1851 @Test
1852 public void testTransitivity() throws AbstractZemucanException {
1853 final Graph graph1 = this.testCreateSimpleGraph();
1854 final Graph graph2 = this.testCreateSimpleGraph();
1855 final Graph graph3 = this.testCreateSimpleGraph();
1856
1857 final boolean val1 = graph1.equals(graph2);
1858 final boolean val2 = graph2.equals(graph3);
1859 final boolean val3 = graph1.equals(graph3);
1860
1861 final boolean result = (val1 == val2) && (val2 == val3);
1862
1863 Assert.assertTrue("transitivity", result);
1864 }
1865
1866
1867
1868
1869
1870
1871
1872 @Test
1873 public void testValidateGraph() throws AbstractZemucanException {
1874 final Graph graph = new Graph(GraphTest.VALIDATED_GRAPH, false);
1875 graph.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
1876 graph.addNode(GraphTest.CREATE, GraphTest.CREATE, true);
1877 graph.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
1878 graph.firstPhaseFinished();
1879 graph.addRelation(Constants.STARTING_NODE, GraphTest.CREATE);
1880 graph.addRelation(GraphTest.CREATE, Constants.ENDING_NODE);
1881 graph.secondPhaseFinished();
1882
1883 final GraphState actual = graph.getGraphState();
1884
1885 final GraphState expected = GraphState.Validated;
1886
1887 Assert.assertEquals("Graph validated", expected, actual);
1888 }
1889
1890
1891
1892
1893
1894
1895
1896
1897 @Test
1898 public void testValidateGraphExtraNodesStructure()
1899 throws AbstractZemucanException {
1900 final Graph graph = new Graph("validated extra nodes", true);
1901 graph.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
1902 graph.addNode(GraphTest.CREATE, GraphTest.CREATE, true);
1903 graph.addNode(GraphTest.TABLE, GraphTest.TABLE, true);
1904 graph.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
1905 graph.firstPhaseFinished();
1906 graph.addRelation(Constants.STARTING_NODE, GraphTest.CREATE);
1907 graph.addRelation(Constants.STARTING_NODE, GraphTest.TABLE);
1908 graph.addRelation(GraphTest.CREATE, Constants.ENDING_NODE);
1909 graph.addRelation(GraphTest.TABLE, Constants.ENDING_NODE);
1910 graph.secondPhaseFinished();
1911
1912 final StartingNode node = graph.getStartingNode();
1913
1914 final List<AbstractGraphNode> nodes = node.getChildren();
1915
1916
1917 Assert.assertEquals(GraphTest.QTY_OF_WAYS, 5, nodes.size());
1918 }
1919
1920
1921
1922
1923
1924
1925
1926 @Test
1927 public void testValidateGraphStructure()
1928 throws AbstractZemucanException {
1929 final Graph graph = new Graph(GraphTest.VALIDATED_GRAPH, false);
1930 graph.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
1931 graph.addNode(GraphTest.CREATE, GraphTest.CREATE, true);
1932 graph.addNode(GraphTest.TABLE, GraphTest.TABLE, true);
1933 graph.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
1934 graph.firstPhaseFinished();
1935 graph.addRelation(Constants.STARTING_NODE, GraphTest.CREATE);
1936 graph.addRelation(Constants.STARTING_NODE, GraphTest.TABLE);
1937 graph.addRelation(GraphTest.CREATE, Constants.ENDING_NODE);
1938 graph.addRelation(GraphTest.TABLE, Constants.ENDING_NODE);
1939 graph.secondPhaseFinished();
1940
1941 final StartingNode node = graph.getStartingNode();
1942
1943 final List<AbstractGraphNode> nodes = node.getChildren();
1944
1945 Assert.assertEquals(GraphTest.QTY_OF_WAYS, 2, nodes.size());
1946 }
1947
1948
1949
1950
1951
1952
1953
1954 @Test
1955 public void testValidateGraphTwice() throws AbstractZemucanException {
1956 final Graph graph = new Graph(GraphTest.VALIDATED_TWICE, false);
1957 graph.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
1958 graph.addNode(GraphTest.CREATE, GraphTest.CREATE, true);
1959 graph.addNode(GraphTest.TABLE, GraphTest.TABLE, true);
1960 graph.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
1961 graph.firstPhaseFinished();
1962 graph.addRelation(Constants.STARTING_NODE, GraphTest.CREATE);
1963 graph.addRelation(Constants.STARTING_NODE, GraphTest.TABLE);
1964 graph.addRelation(GraphTest.CREATE, Constants.ENDING_NODE);
1965 graph.addRelation(GraphTest.TABLE, Constants.ENDING_NODE);
1966 graph.secondPhaseFinished();
1967 graph.addRelation(GraphTest.CREATE, GraphTest.TABLE);
1968 graph.secondPhaseFinished();
1969
1970 final GraphState actual = graph.getGraphState();
1971
1972 final GraphState expected = GraphState.Validated;
1973
1974 Assert.assertEquals("Graph validateed", expected,
1975 actual);
1976 }
1977
1978
1979
1980
1981
1982
1983
1984
1985 @Test
1986 public void testValidateGraphTwiceExtraNodesStructure()
1987 throws AbstractZemucanException {
1988 final Graph graph = new Graph("validated twice extra nodes", true);
1989 graph.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
1990 graph.addNode(GraphTest.CREATE, GraphTest.CREATE, true);
1991 graph.addNode(GraphTest.TABLE, GraphTest.TABLE, true);
1992 graph.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
1993 graph.firstPhaseFinished();
1994 graph.addRelation(Constants.STARTING_NODE, GraphTest.CREATE);
1995 graph.addRelation(Constants.STARTING_NODE, GraphTest.TABLE);
1996 graph.addRelation(GraphTest.CREATE, Constants.ENDING_NODE);
1997 graph.addRelation(GraphTest.TABLE, Constants.ENDING_NODE);
1998 graph.secondPhaseFinished();
1999 graph.addRelation(GraphTest.CREATE, GraphTest.TABLE);
2000 graph.secondPhaseFinished();
2001
2002 final StartingNode node = graph.getStartingNode();
2003
2004 final List<AbstractGraphNode> nodes = node.getChildren();
2005
2006
2007 Assert.assertEquals(GraphTest.QTY_OF_WAYS, 5, nodes.size());
2008 }
2009
2010
2011
2012
2013
2014
2015
2016 @Test
2017 public void testValidateGraphTwiceStructure()
2018 throws AbstractZemucanException {
2019 final Graph graph = new Graph(GraphTest.VALIDATED_TWICE, false);
2020 graph.addNode(Constants.STARTING_NODE, Constants.STARTING_NODE, true);
2021 graph.addNode(GraphTest.CREATE, GraphTest.CREATE, true);
2022 graph.addNode(GraphTest.TABLE, GraphTest.TABLE, true);
2023 graph.addNode(Constants.ENDING_NODE, Constants.ENDING_NODE, true);
2024 graph.firstPhaseFinished();
2025 graph.addRelation(Constants.STARTING_NODE, GraphTest.CREATE);
2026 graph.addRelation(Constants.STARTING_NODE, GraphTest.TABLE);
2027 graph.addRelation(GraphTest.CREATE, Constants.ENDING_NODE);
2028 graph.addRelation(GraphTest.TABLE, Constants.ENDING_NODE);
2029 graph.secondPhaseFinished();
2030 graph.addRelation(GraphTest.CREATE, GraphTest.TABLE);
2031 graph.secondPhaseFinished();
2032
2033 final StartingNode node = graph.getStartingNode();
2034
2035 final List<AbstractGraphNode> nodes = node.getChildren();
2036
2037 Assert.assertEquals(GraphTest.QTY_OF_WAYS, 2, nodes.size());
2038 }
2039 }