1   package org.apache.maven.werkz;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import java.util.List;
22  
23  import junit.framework.TestCase;
24  
25  public class GoalTest
26      extends TestCase
27  {
28      private Session session;
29  
30      public GoalTest( String name )
31      {
32          super( name );
33      }
34  
35      public void setUp()
36      {
37          this.session = new Session();
38      }
39  
40      public void tearDown()
41      {
42          this.session = null;
43      }
44  
45      public void testHashCode()
46      {
47  
48          Goal a1 = new Goal( "a" );
49          Goal a2 = new Goal( "a" );
50  
51          Goal b1 = new Goal( "b" );
52          Goal b2 = new Goal( "b" );
53  
54          assertEquals( a1.hashCode(), a2.hashCode() );
55  
56          assertEquals( b1.hashCode(), b2.hashCode() );
57  
58          assertTrue( a1.hashCode() != b1.hashCode() );
59      }
60  
61      public void testEquals()
62      {
63          Goal a1 = new Goal( "a" );
64          Goal a2 = new Goal( "a" );
65  
66          Goal b1 = new Goal( "b" );
67          Goal b2 = new Goal( "b" );
68  
69          assertEquals( a1, a2 );
70  
71          assertEquals( b1, b2 );
72  
73          assertTrue( !a1.equals( b1 ) );
74      }
75  
76      public void testHasBeenMet()
77      {
78          Goal g = new Goal( "a" );
79  
80          assertTrue( !g.isSatisfied( this.session ) );
81  
82          this.session.addSatisfiedGoal( g );
83  
84          assertTrue( g.isSatisfied( this.session ) );
85  
86          this.session.removeSatisfiedGoal( g );
87  
88          assertTrue( !g.isSatisfied( this.session ) );
89      }
90  
91      public void testAddPrerequisite_NoCycle()
92      {
93          Goal g1 = new Goal( "goal-1" );
94          Goal g2 = new Goal( "goal-2" );
95          Goal g3 = new Goal( "goal-3" );
96  
97          try
98          {
99              g1.addPrecursor( g2 );
100             g1.addPrecursor( g3 );
101 
102             assertEquals( 2, g1.getPrecursors().size() );
103 
104             assertTrue( g1.getPrecursors().contains( g2 ) );
105             assertTrue( g1.getPrecursors().contains( g3 ) );
106 
107             assertEquals( 0, g2.getPrecursors().size() );
108             assertEquals( 0, g3.getPrecursors().size() );
109 
110         }
111         catch ( CyclicGoalChainException e )
112         {
113             fail( e.getLocalizedMessage() );
114         }
115     }
116 
117     public void testAddPostcursor_NoCycle()
118     {
119         Goal g1 = new Goal( "goal-1" );
120         Goal g2 = new Goal( "goal-2" );
121         Goal g3 = new Goal( "goal-3" );
122         Goal g4 = new Goal( "goal-4" );
123         Goal g5 = new Goal( "goal-5" );
124         Goal g6 = new Goal( "goal-6" );
125 
126         try
127         {
128             g1.addPostcursor( g2 );
129             g3.addPrecursor( g2 );
130             g3.addPostcursor( g4 );
131             g4.addPostcursor( g5 );
132             g4.addPostcursor( g6 );
133 
134             assertEquals( 0, g1.getPrecursors().size() );
135             assertEquals( 1, g1.getPostcursors().size() );
136             assertEquals( 1, g2.getPrecursors().size() );
137             assertEquals( 1, g2.getPostcursors().size() );
138             assertEquals( 1, g3.getPrecursors().size() );
139             assertEquals( 1, g3.getPostcursors().size() );
140             assertEquals( 1, g4.getPrecursors().size() );
141             assertEquals( 2, g4.getPostcursors().size() );
142             assertEquals( 1, g5.getPrecursors().size() );
143             assertEquals( 0, g5.getPostcursors().size() );
144             assertEquals( 1, g6.getPrecursors().size() );
145             assertEquals( 0, g6.getPostcursors().size() );
146 
147             assertTrue( g1.getPostcursors().contains( g2 ) );
148             assertTrue( g4.getPostcursors().contains( g5 ) );
149             assertTrue( g4.getPostcursors().contains( g6 ) );
150             assertTrue( g5.getPrecursors().contains( g4 ) );
151             assertTrue( g6.getPrecursors().contains( g4 ) );
152         }
153         catch ( CyclicGoalChainException e )
154         {
155             fail( e.getLocalizedMessage() );
156         }
157     }
158 
159     public void testAddPrecursor_LocalCycle()
160     {
161         Goal g1 = new Goal( "goal-1" );
162         Goal g2 = new Goal( "goal-2" );
163 
164         try
165         {
166             g1.addPrecursor( g2 );
167 
168             try
169             {
170                 g2.addPrecursor( g1 );
171 
172                 fail( "Should have thrown CyclicGoalChainException" );
173             }
174             catch ( CyclicGoalChainException e )
175             {
176                 // expected and correct
177             }
178         }
179         catch ( CyclicGoalChainException e )
180         {
181             fail( e.getLocalizedMessage() );
182         }
183     }
184 
185     public void testPostcursor_LocalCycle()
186     {
187         Goal g1 = new Goal( "goal-1" );
188         Goal g2 = new Goal( "goal-2" );
189 
190         try
191         {
192             g1.addPostcursor( g2 );
193 
194             try
195             {
196                 g2.addPostcursor( g1 );
197 
198                 fail( "Should have thrown CyclicGoalChainException" );
199             }
200             catch ( CyclicGoalChainException e )
201             {
202                 // expected and correct
203             }
204         }
205         catch ( CyclicGoalChainException e )
206         {
207             fail( e.getLocalizedMessage() );
208         }
209     }
210 
211     public void testAddPrecursor_DistantFullCycle()
212     {
213         Goal g1 = new Goal( "goal-1" );
214         Goal g2 = new Goal( "goal-2" );
215         Goal g3 = new Goal( "goal-3" );
216         Goal g4 = new Goal( "goal-4" );
217         Goal g5 = new Goal( "goal-5" );
218         Goal g6 = new Goal( "goal-6" );
219         Goal g7 = new Goal( "goal-7" );
220         Goal g8 = new Goal( "goal-8" );
221         Goal g9 = new Goal( "goal-9" );
222 
223         try
224         {
225             g1.addPrecursor( g2 );
226             g2.addPrecursor( g3 );
227             g3.addPrecursor( g4 );
228             g4.addPrecursor( g5 );
229             g5.addPrecursor( g6 );
230             g6.addPrecursor( g7 );
231             g7.addPrecursor( g8 );
232             g8.addPrecursor( g9 );
233 
234             try
235             {
236                 g9.addPrecursor( g1 );
237 
238                 fail( "Should have thrown CyclicGoalChainException" );
239             }
240             catch ( CyclicGoalChainException e )
241             {
242                 // expected and correct
243             }
244         }
245         catch ( CyclicGoalChainException e )
246         {
247             fail( e.getLocalizedMessage() );
248         }
249     }
250 
251     public void testAddPostcursor_DistantFullCycle()
252     {
253         Goal g1 = new Goal( "goal-1" );
254         Goal g2 = new Goal( "goal-2" );
255         Goal g3 = new Goal( "goal-3" );
256         Goal g4 = new Goal( "goal-4" );
257         Goal g5 = new Goal( "goal-5" );
258         Goal g6 = new Goal( "goal-6" );
259         Goal g7 = new Goal( "goal-7" );
260         Goal g8 = new Goal( "goal-8" );
261         Goal g9 = new Goal( "goal-9" );
262 
263         try
264         {
265             g1.addPostcursor( g2 );
266             g2.addPostcursor( g3 );
267             g3.addPostcursor( g4 );
268             g4.addPostcursor( g5 );
269             g5.addPostcursor( g6 );
270             g6.addPostcursor( g7 );
271             g7.addPostcursor( g8 );
272             g8.addPostcursor( g9 );
273 
274             try
275             {
276                 g9.addPostcursor( g1 );
277 
278                 fail( "Should have thrown CyclicGoalChainException" );
279             }
280             catch ( CyclicGoalChainException e )
281             {
282                 // expected and correct
283             }
284         }
285         catch ( CyclicGoalChainException e )
286         {
287             fail( e.getLocalizedMessage() );
288         }
289     }
290 
291     public void testAddPrecursor_DistantPartialCycle()
292     {
293         Goal g1 = new Goal( "goal-1" );
294         Goal g2 = new Goal( "goal-2" );
295         Goal g3 = new Goal( "goal-3" );
296         Goal g4 = new Goal( "goal-4" );
297         Goal g5 = new Goal( "goal-5" );
298         Goal g6 = new Goal( "goal-6" );
299         Goal g7 = new Goal( "goal-7" );
300         Goal g8 = new Goal( "goal-8" );
301         Goal g9 = new Goal( "goal-9" );
302 
303         try
304         {
305             g1.addPrecursor( g2 );
306             g2.addPrecursor( g3 );
307             g3.addPrecursor( g4 );
308             g4.addPrecursor( g5 );
309             g5.addPrecursor( g6 );
310             g6.addPrecursor( g7 );
311             g7.addPrecursor( g8 );
312             g8.addPrecursor( g9 );
313 
314             try
315             {
316                 g9.addPrecursor( g5 );
317 
318                 fail( "Should have thrown CyclicGoalChainException" );
319             }
320             catch ( CyclicGoalChainException e )
321             {
322                 // expected and correct
323             }
324         }
325         catch ( CyclicGoalChainException e )
326         {
327             fail( e.getLocalizedMessage() );
328         }
329     }
330 
331     public void testAddPostcursor_DistantPartialCycle()
332     {
333         Goal g1 = new Goal( "goal-1" );
334         Goal g2 = new Goal( "goal-2" );
335         Goal g3 = new Goal( "goal-3" );
336         Goal g4 = new Goal( "goal-4" );
337         Goal g5 = new Goal( "goal-5" );
338         Goal g6 = new Goal( "goal-6" );
339         Goal g7 = new Goal( "goal-7" );
340         Goal g8 = new Goal( "goal-8" );
341         Goal g9 = new Goal( "goal-9" );
342 
343         try
344         {
345             g1.addPostcursor( g2 );
346             g2.addPostcursor( g3 );
347             g3.addPostcursor( g4 );
348             g4.addPostcursor( g5 );
349             g5.addPostcursor( g6 );
350             g6.addPostcursor( g7 );
351             g7.addPostcursor( g8 );
352             g8.addPostcursor( g9 );
353 
354             try
355             {
356                 g9.addPostcursor( g5 );
357 
358                 fail( "Should have thrown CyclicGoalChainException" );
359             }
360             catch ( CyclicGoalChainException e )
361             {
362                 // expected and correct
363             }
364         }
365         catch ( CyclicGoalChainException e )
366         {
367             fail( e.getLocalizedMessage() );
368         }
369     }
370 
371     public void testAddPrecursor_DistantPartialCycle_PieceWise()
372     {
373         Goal g1 = new Goal( "goal-1" );
374         Goal g2 = new Goal( "goal-2" );
375         Goal g3 = new Goal( "goal-3" );
376         Goal g4 = new Goal( "goal-4" );
377         Goal g5 = new Goal( "goal-5" );
378         Goal g6 = new Goal( "goal-6" );
379         Goal g7 = new Goal( "goal-7" );
380         Goal g8 = new Goal( "goal-8" );
381         Goal g9 = new Goal( "goal-9" );
382 
383         try
384         {
385             g1.addPrecursor( g2 );
386             g2.addPrecursor( g3 );
387             g3.addPrecursor( g4 );
388             //g4.addPrecursor( g5 );
389             g5.addPrecursor( g6 );
390             g6.addPrecursor( g7 );
391             g7.addPrecursor( g8 );
392             g8.addPrecursor( g9 );
393             g9.addPrecursor( g4 );
394 
395             try
396             {
397                 g4.addPrecursor( g5 );
398 
399                 fail( "Should have thrown CyclicGoalChainException" );
400             }
401             catch ( CyclicGoalChainException e )
402             {
403                 // expected and correct
404             }
405         }
406         catch ( CyclicGoalChainException e )
407         {
408             fail( e.getLocalizedMessage() );
409         }
410     }
411 
412     public void testAddPostcursor_DistantPartialCycle_PieceWise()
413     {
414         Goal g1 = new Goal( "goal-1" );
415         Goal g2 = new Goal( "goal-2" );
416         Goal g3 = new Goal( "goal-3" );
417         Goal g4 = new Goal( "goal-4" );
418         Goal g5 = new Goal( "goal-5" );
419         Goal g6 = new Goal( "goal-6" );
420         Goal g7 = new Goal( "goal-7" );
421         Goal g8 = new Goal( "goal-8" );
422         Goal g9 = new Goal( "goal-9" );
423 
424         try
425         {
426             g1.addPostcursor( g2 );
427             g2.addPostcursor( g3 );
428             g3.addPostcursor( g4 );
429             //g4.addPostcursor( g5 );
430             g5.addPostcursor( g6 );
431             g6.addPostcursor( g7 );
432             g7.addPostcursor( g8 );
433             g8.addPostcursor( g9 );
434             g9.addPostcursor( g4 );
435 
436             try
437             {
438                 g4.addPostcursor( g5 );
439 
440                 fail( "Should have thrown CyclicGoalChainException" );
441             }
442             catch ( CyclicGoalChainException e )
443             {
444                 // expected and correct
445             }
446         }
447         catch ( CyclicGoalChainException e )
448         {
449             fail( e.getLocalizedMessage() );
450         }
451     }
452 
453     public void testAttain_Linear()
454     {
455         GoalTracker tracker = new GoalTracker();
456 
457         Goal g1 = new MockGoal( "goal-1", tracker );
458 
459         Goal g2 = new MockGoal( "goal-2", tracker );
460 
461         Goal g3 = new MockGoal( "goal-3", tracker );
462 
463         try
464         {
465             g1.addPrecursor( g2 );
466             g2.addPrecursor( g3 );
467 
468             tracker.addExpectedGoal( g3 );
469             tracker.addExpectedGoal( g2 );
470             tracker.addExpectedGoal( g1 );
471 
472             g1.attain( this.session );
473         }
474         catch ( CyclicGoalChainException e )
475         {
476             fail( e.getLocalizedMessage() );
477         }
478         catch ( UnattainableGoalException e )
479         {
480             fail( e.getLocalizedMessage() );
481         }
482         catch ( NoActionDefinitionException e )
483         {
484             fail( e.getLocalizedMessage() );
485         }
486 
487         tracker.verify();
488     }
489 
490     public void testPostAttain_Linear()
491     {
492         GoalTracker tracker = new GoalTracker();
493 
494         Goal g1 = new MockGoal( "goal-1", tracker );
495 
496         Goal g2 = new MockGoal( "goal-2", tracker );
497 
498         Goal g3 = new MockGoal( "goal-3", tracker );
499 
500         try
501         {
502             g1.addPostcursor( g2 );
503             g2.addPostcursor( g3 );
504 
505             tracker.addExpectedGoal( g1 );
506             tracker.addExpectedGoal( g2 );
507             tracker.addExpectedGoal( g3 );
508 
509             g3.attain( this.session );
510         }
511         catch ( CyclicGoalChainException e )
512         {
513             fail( e.getLocalizedMessage() );
514         }
515         catch ( UnattainableGoalException e )
516         {
517             fail( e.getLocalizedMessage() );
518         }
519         catch ( NoActionDefinitionException e )
520         {
521             fail( e.getLocalizedMessage() );
522         }
523 
524         tracker.verify();
525     }
526 
527     public void testPostPercolate_Linear()
528     {
529         GoalTracker tracker = new GoalTracker();
530 
531         Goal g1 = new MockGoal( "goal-1", tracker );
532 
533         Goal g2 = new MockGoal( "goal-2", tracker );
534 
535         Goal g3 = new MockGoal( "goal-3", tracker );
536 
537         try
538         {
539             g1.addPostcursor( g2 );
540             g2.addPostcursor( g3 );
541 
542             tracker.addExpectedGoal( g1 );
543             tracker.addExpectedGoal( g2 );
544             tracker.addExpectedGoal( g3 );
545 
546             g1.percolate( this.session );
547         }
548         catch ( CyclicGoalChainException e )
549         {
550             fail( e.getLocalizedMessage() );
551         }
552         catch ( UnattainableGoalException e )
553         {
554             fail( e.getLocalizedMessage() );
555         }
556         catch ( NoActionDefinitionException e )
557         {
558             fail( e.getLocalizedMessage() );
559         }
560 
561         tracker.verify();
562     }
563 
564     public void testAttain_Tree()
565     {
566         GoalTracker tracker = new GoalTracker();
567 
568         MockGoal g1 = new MockGoal( "goal-1", tracker );
569 
570         MockGoal g2 = new MockGoal( "goal-2", tracker );
571 
572         MockGoal g3 = new MockGoal( "goal-3", tracker );
573 
574         MockGoal g4 = new MockGoal( "goal-4", tracker );
575 
576         MockGoal g5 = new MockGoal( "goal-5", tracker );
577 
578         try
579         {
580             g1.addPrecursor( g2 );
581             g2.addPrecursor( g3 );
582             g2.addPrecursor( g4 );
583             g4.addPrecursor( g5 );
584 
585             tracker.addExpectedGoal( g3 );
586             tracker.addExpectedGoal( g5 );
587             tracker.addExpectedGoal( g4 );
588             tracker.addExpectedGoal( g2 );
589             tracker.addExpectedGoal( g1 );
590 
591             g1.attain( this.session );
592         }
593         catch ( CyclicGoalChainException e )
594         {
595             fail( e.getLocalizedMessage() );
596         }
597         catch ( UnattainableGoalException e )
598         {
599             fail( e.getLocalizedMessage() );
600         }
601         catch ( NoActionDefinitionException e )
602         {
603             fail( e.getLocalizedMessage() );
604         }
605 
606         tracker.verify();
607     }
608 
609     public void testPostPercolate_Tree()
610     {
611         GoalTracker tracker = new GoalTracker();
612 
613         MockGoal g1 = new MockGoal( "goal-1", tracker );
614 
615         MockGoal g2 = new MockGoal( "goal-2", tracker );
616 
617         MockGoal g3 = new MockGoal( "goal-3", tracker );
618 
619         MockGoal g4 = new MockGoal( "goal-4", tracker );
620 
621         MockGoal g5 = new MockGoal( "goal-5", tracker );
622 
623         try
624         {
625             g1.addPostcursor( g2 );
626             g2.addPostcursor( g3 );
627             g2.addPostcursor( g4 );
628             g4.addPostcursor( g5 );
629 
630             tracker.addExpectedGoal( g1 );
631             tracker.addExpectedGoal( g2 );
632             tracker.addExpectedGoal( g3 );
633             tracker.addExpectedGoal( g4 );
634             tracker.addExpectedGoal( g5 );
635 
636             g1.percolate( this.session );
637         }
638         catch ( CyclicGoalChainException e )
639         {
640             fail( e.getLocalizedMessage() );
641         }
642         catch ( UnattainableGoalException e )
643         {
644             fail( e.getLocalizedMessage() );
645         }
646         catch ( NoActionDefinitionException e )
647         {
648             fail( e.getLocalizedMessage() );
649         }
650 
651         tracker.verify();
652     }
653 
654     public void testAttain_Tree_PartiallyNotRequired()
655     {
656         GoalTracker tracker = new GoalTracker();
657 
658         MockGoal g1 = new MockGoal( "goal-1", tracker );
659 
660         MockGoal g2 = new MockGoal( "goal-2", tracker );
661 
662         MockGoal g3 = new MockGoal( "goal-3", tracker );
663 
664         MockGoal g4 = new MockGoal( "goal-4", tracker );
665 
666         MockGoal g5 = new MockGoal( "goal-5", tracker );
667 
668         try
669         {
670             g1.addPrecursor( g2 );
671             g2.addPrecursor( g3 );
672             g2.addPrecursor( g4 );
673             g4.addPrecursor( g5 );
674 
675             g4.requiresAction( false );
676             g1.requiresAction( false );
677 
678             tracker.addExpectedGoal( g3 );
679             tracker.addExpectedGoal( g5 );
680             // tracker.addExpectedGoal( g4 );
681             tracker.addExpectedGoal( g2 );
682             //tracker.addExpectedGoal( g1 );
683 
684             g1.attain( this.session );
685         }
686         catch ( CyclicGoalChainException e )
687         {
688             fail( e.getLocalizedMessage() );
689         }
690         catch ( UnattainableGoalException e )
691         {
692             fail( e.getLocalizedMessage() );
693         }
694         catch ( NoActionDefinitionException e )
695         {
696             fail( e.getLocalizedMessage() );
697         }
698 
699         tracker.verify();
700     }
701 
702     public void testPercolate_Tree_PartiallyNotRequired()
703     {
704         GoalTracker tracker = new GoalTracker();
705 
706         MockGoal g1 = new MockGoal( "goal-1", tracker );
707 
708         MockGoal g2 = new MockGoal( "goal-2", tracker );
709 
710         MockGoal g3 = new MockGoal( "goal-3", tracker );
711 
712         MockGoal g4 = new MockGoal( "goal-4", tracker );
713 
714         MockGoal g5 = new MockGoal( "goal-5", tracker );
715 
716         try
717         {
718             g1.addPostcursor( g2 );
719             g2.addPostcursor( g3 );
720             g2.addPostcursor( g4 );
721             g4.addPostcursor( g5 );
722 
723             g4.requiresAction( false );
724             g1.requiresAction( false );
725 
726             //tracker.addExpectedGoal( g1 );
727             tracker.addExpectedGoal( g2 );
728             tracker.addExpectedGoal( g3 );
729             //tracker.addExpectedGoal( g4 );
730             tracker.addExpectedGoal( g5 );
731 
732             g1.percolate( this.session );
733         }
734         catch ( CyclicGoalChainException e )
735         {
736             fail( e.getLocalizedMessage() );
737         }
738         catch ( UnattainableGoalException e )
739         {
740             fail( e.getLocalizedMessage() );
741         }
742         catch ( NoActionDefinitionException e )
743         {
744             fail( e.getLocalizedMessage() );
745         }
746 
747         tracker.verify();
748     }
749 
750     public void testAttain_Tree_Overlap()
751     {
752         GoalTracker tracker = new GoalTracker();
753 
754         MockGoal g1_1 = new MockGoal( "goal-1-1", tracker );
755 
756         MockGoal g1_2 = new MockGoal( "goal-1-2", tracker );
757 
758         MockGoal g1_3 = new MockGoal( "goal-1-3", tracker );
759 
760         MockGoal g2_1 = new MockGoal( "goal-2-1", tracker );
761 
762         MockGoal g2_2 = new MockGoal( "goal-2-2", tracker );
763 
764         MockGoal g2_3 = new MockGoal( "goal-2-3", tracker );
765 
766         MockGoal gc_1 = new MockGoal( "goal-c-1", tracker );
767 
768         MockGoal gc_2 = new MockGoal( "goal-c-2", tracker );
769 
770         try
771         {
772             g1_1.addPrecursor( g1_2 );
773             g1_2.addPrecursor( g1_3 );
774 
775             g2_1.addPrecursor( g2_2 );
776             g2_2.addPrecursor( g2_3 );
777 
778             g1_3.addPrecursor( gc_1 );
779             g2_3.addPrecursor( gc_1 );
780 
781             gc_1.addPrecursor( gc_2 );
782 
783             // ----------------------------------------
784             //   test 1
785             // ----------------------------------------
786 
787             tracker.clear();
788 
789             tracker.addExpectedGoal( gc_2 );
790             tracker.addExpectedGoal( gc_1 );
791             tracker.addExpectedGoal( g1_3 );
792             tracker.addExpectedGoal( g1_2 );
793             tracker.addExpectedGoal( g1_1 );
794 
795             g1_1.attain( this.session );
796 
797             tracker.verify();
798 
799             // ----------------------------------------
800             //   test 2
801             // ----------------------------------------
802 
803             tracker.clear();
804 
805             tracker.addExpectedGoal( g2_3 );
806             tracker.addExpectedGoal( g2_2 );
807             tracker.addExpectedGoal( g2_1 );
808 
809             g2_1.attain( this.session );
810         }
811         catch ( CyclicGoalChainException e )
812         {
813             fail( e.getLocalizedMessage() );
814         }
815         catch ( UnattainableGoalException e )
816         {
817             fail( e.getLocalizedMessage() );
818         }
819         catch ( NoActionDefinitionException e )
820         {
821             fail( e.getLocalizedMessage() );
822         }
823     }
824 
825     public void testPercolate_Tree_Overlap()
826     {
827         GoalTracker tracker = new GoalTracker();
828 
829         MockGoal g1_1 = new MockGoal( "goal-1-1", tracker );
830 
831         MockGoal g1_2 = new MockGoal( "goal-1-2", tracker );
832 
833         MockGoal g1_3 = new MockGoal( "goal-1-3", tracker );
834 
835         MockGoal g2_1 = new MockGoal( "goal-2-1", tracker );
836 
837         MockGoal g2_2 = new MockGoal( "goal-2-2", tracker );
838 
839         MockGoal g2_3 = new MockGoal( "goal-2-3", tracker );
840 
841         MockGoal gc_1 = new MockGoal( "goal-c-1", tracker );
842 
843         MockGoal gc_2 = new MockGoal( "goal-c-2", tracker );
844 
845         try
846         {
847             g1_1.addPostcursor( g1_2 );
848             g1_2.addPostcursor( g1_3 );
849 
850             g2_1.addPostcursor( g2_2 );
851             g2_2.addPostcursor( g2_3 );
852 
853             g1_3.addPostcursor( gc_1 );
854             g2_3.addPostcursor( gc_1 );
855 
856             gc_1.addPostcursor( gc_2 );
857 
858             // ----------------------------------------
859             //   test 1
860             // ----------------------------------------
861 
862             tracker.clear();
863 
864             tracker.addExpectedGoal( g1_1 );
865             tracker.addExpectedGoal( g1_2 );
866             tracker.addExpectedGoal( g1_3 );
867             tracker.addExpectedGoal( gc_1 );
868             tracker.addExpectedGoal( gc_2 );
869 
870             g1_1.percolate( this.session );
871 
872             tracker.verify();
873 
874             // ----------------------------------------
875             //   test 2
876             // ----------------------------------------
877 
878             tracker.clear();
879 
880             tracker.addExpectedGoal( g2_1 );
881             tracker.addExpectedGoal( g2_2 );
882             tracker.addExpectedGoal( g2_3 );
883 
884             g2_1.percolate( this.session );
885         }
886         catch ( CyclicGoalChainException e )
887         {
888             fail( e.getLocalizedMessage() );
889         }
890         catch ( UnattainableGoalException e )
891         {
892             fail( e.getLocalizedMessage() );
893         }
894         catch ( NoActionDefinitionException e )
895         {
896             fail( e.getLocalizedMessage() );
897         }
898     }
899 
900     public void testRequiresAction_False()
901     {
902         GoalTracker tracker = new GoalTracker();
903 
904         MockGoal g = new MockGoal( "goal-1", tracker );
905 
906         g.requiresAction( false );
907 
908         try
909         {
910             g.attain( this.session );
911 
912             tracker.verify();
913         }
914         catch ( UnattainableGoalException e )
915         {
916             fail( e.getLocalizedMessage() );
917         }
918         catch ( NoActionDefinitionException e )
919         {
920             fail( e.getLocalizedMessage() );
921         }
922     }
923 
924     public void testRequiresAction_True()
925     {
926         GoalTracker tracker = new GoalTracker();
927 
928         MockGoal g = new MockGoal( "goal-1", tracker );
929 
930         tracker.addExpectedGoal( g );
931 
932         try
933         {
934             g.attain( this.session );
935 
936             tracker.verify();
937         }
938         catch ( UnattainableGoalException e )
939         {
940             fail( e.getLocalizedMessage() );
941         }
942         catch ( NoActionDefinitionException e )
943         {
944             fail( e.getLocalizedMessage() );
945         }
946     }
947 
948     public void testPreGoalCallback_AddRemove()
949     {
950         MockCallback callback = new MockCallback();
951 
952         Goal g = new Goal( "goal-1" );
953 
954         g.addPreGoalCallback( callback );
955         g.addPreGoalCallback( callback );
956 
957         List callbacks = g.getPreGoalCallbacks();
958 
959         assertEquals( 2, callbacks.size() );
960 
961         assertSame( callback, callbacks.get( 0 ) );
962 
963         assertSame( callback, callbacks.get( 1 ) );
964 
965         g.removePreGoalCallback( callback );
966 
967         callbacks = g.getPreGoalCallbacks();
968 
969         assertEquals( 0, callbacks.size() );
970     }
971 
972     public void testPostGoalCallback_AddRemove()
973     {
974         MockCallback callback = new MockCallback();
975 
976         Goal g = new Goal( "goal-1" );
977 
978         g.addPostGoalCallback( callback );
979         g.addPostGoalCallback( callback );
980 
981         List callbacks = g.getPostGoalCallbacks();
982 
983         assertEquals( 2, callbacks.size() );
984 
985         assertSame( callback, callbacks.get( 0 ) );
986 
987         assertSame( callback, callbacks.get( 1 ) );
988 
989         g.removePostGoalCallback( callback );
990 
991         callbacks = g.getPostGoalCallbacks();
992 
993         assertEquals( 0, callbacks.size() );
994     }
995 
996     public void testPreActionCallback_AddRemove()
997     {
998         MockCallback callback = new MockCallback();
999 
1000         Goal g = new Goal( "goal-1" );
1001 
1002         g.addPreActionCallback( callback );
1003         g.addPreActionCallback( callback );
1004 
1005         List callbacks = g.getPreActionCallbacks();
1006 
1007         assertEquals( 2, callbacks.size() );
1008 
1009         assertSame( callback, callbacks.get( 0 ) );
1010 
1011         assertSame( callback, callbacks.get( 1 ) );
1012 
1013         g.removePreActionCallback( callback );
1014 
1015         callbacks = g.getPreActionCallbacks();
1016 
1017         assertEquals( 0, callbacks.size() );
1018     }
1019 
1020     public void testPostActionCallback_AddRemove()
1021     {
1022         MockCallback callback = new MockCallback();
1023 
1024         Goal g = new Goal( "goal-1" );
1025 
1026         g.addPostActionCallback( callback );
1027         g.addPostActionCallback( callback );
1028 
1029         List callbacks = g.getPostActionCallbacks();
1030 
1031         assertEquals( 2, callbacks.size() );
1032 
1033         assertSame( callback, callbacks.get( 0 ) );
1034 
1035         assertSame( callback, callbacks.get( 1 ) );
1036 
1037         g.removePostActionCallback( callback );
1038 
1039         callbacks = g.getPostActionCallbacks();
1040 
1041         assertEquals( 0, callbacks.size() );
1042     }
1043 }