1 package org.apache.maven.werkz;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import java.util.Arrays;
22
23 import junit.framework.TestCase;
24
25 public class ProjectTest
26 extends TestCase
27 {
28 private Session session;
29
30 public ProjectTest( 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 testAddGoal()
46 {
47 Goal g = new Goal( "goal-1" );
48
49 WerkzProject project = new WerkzProject();
50
51 project.addGoal( g );
52
53 assertSame( g, project.getGoal( "goal-1" ) );
54 }
55
56 public void testGetGoal_Bogus()
57 {
58 WerkzProject project = new WerkzProject();
59
60 assertNull( project.getGoal( "goal-1" ) );
61 }
62
63 public void testAttainGoal()
64 {
65 GoalTracker tracker = new GoalTracker();
66
67 WerkzProject project = new WerkzProject();
68
69 MockGoal g = new MockGoal( "goal-1", tracker );
70
71 project.addGoal( g );
72
73 tracker.addExpectedGoal( g );
74
75 try
76 {
77 project.attainGoal( "goal-1", this.session );
78
79 tracker.verify();
80 }
81 catch ( NoSuchGoalException e )
82 {
83 fail( e.getLocalizedMessage() );
84 }
85 catch ( UnattainableGoalException e )
86 {
87 fail( e.getLocalizedMessage() );
88 }
89 catch ( NoActionDefinitionException e )
90 {
91 fail( e.getLocalizedMessage() );
92 }
93 }
94
95 public void testAttainGoal_Bogus()
96 {
97 WerkzProject project = new WerkzProject();
98
99 try
100 {
101 project.attainGoal( "goal-1", this.session );
102
103 fail( "Should have thrown NoSuchGoalException" );
104 }
105 catch ( NoSuchGoalException e )
106 {
107
108 }
109 catch ( UnattainableGoalException e )
110 {
111 fail( e.getLocalizedMessage() );
112 }
113 catch ( NoActionDefinitionException e )
114 {
115 fail( e.getLocalizedMessage() );
116 }
117 }
118
119 public void testGetExecutionChain()
120 throws Exception
121 {
122 WerkzProject project = new WerkzProject();
123
124 Goal a = new Goal( "a" );
125 Goal b = new Goal( "b" );
126 Goal c = new Goal( "c" );
127 Goal d = new Goal( "d" );
128 Goal e = new Goal( "e" );
129 Goal f = new Goal( "f" );
130
131 a.addPrecursor( b );
132 a.addPrecursor( c );
133
134 b.addPrecursor( d );
135
136 c.addPrecursor( e );
137
138 d.addPrecursor( f );
139 e.addPrecursor( f );
140
141 project.addGoal( a );
142 project.addGoal( b );
143 project.addGoal( c );
144 project.addGoal( d );
145 project.addGoal( e );
146 project.addGoal( f );
147
148 Goal[] chain = project.getExecutionChain( "a" );
149
150 assertEquals( 6, chain.length );
151
152 assertSame( a, chain[5] );
153
154
155
156
157
158 assertBefore( b, a, chain );
159 assertBefore( c, a, chain );
160 assertBefore( d, a, chain );
161 assertBefore( e, a, chain );
162 assertBefore( f, a, chain );
163
164
165
166
167
168 assertBefore( d, b, chain );
169 assertBefore( f, b, chain );
170
171
172
173
174
175 assertBefore( e, c, chain );
176 assertBefore( f, c, chain );
177
178
179
180
181
182 assertBefore( f, d, chain );
183
184
185
186
187
188 assertBefore( f, e, chain );
189
190
191
192 assertContains( a, chain );
193 assertContains( b, chain );
194 assertContains( c, chain );
195 assertContains( d, chain );
196 assertContains( e, chain );
197 }
198
199 protected void assertBefore( Goal first, Goal after, Goal[] chain )
200 {
201 int firstIndex = -1;
202 int afterIndex = -1;
203
204 for ( int i = 0; i < chain.length; ++i )
205 {
206 if ( chain[i].equals( first ) )
207 {
208 firstIndex = i;
209 }
210 else if ( chain[i].equals( after ) )
211 {
212 afterIndex = i;
213 }
214 }
215
216 if ( firstIndex < 0 )
217 {
218 fail( "first element " + first + " not in " + Arrays.asList( chain ) );
219 }
220
221 if ( firstIndex < 0 )
222 {
223 fail( "after element " + after + " not in " + Arrays.asList( chain ) );
224 }
225
226 assertTrue( firstIndex < afterIndex );
227 }
228
229 protected void assertContains( Object obj, Object[] array )
230 {
231 for ( int i = 0; i < array.length; ++i )
232 {
233 if ( array[i].equals( obj ) )
234 {
235 return;
236 }
237 }
238
239 fail( obj + " not in " + Arrays.asList( array ) );
240 }
241 }