001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.ant;
019:
020: import java.io.File;
021:
022: import junit.framework.TestCase;
023:
024: import org.apache.tools.ant.Project;
025: import org.apache.tools.ant.types.FileSet;
026: import org.apache.tools.ant.types.Path;
027:
028: public class IvyBuildListTest extends TestCase {
029:
030: /*
031: * Those tests use the ivy files A , B , C , D , E in test/buildlist
032: * The dependencies are :
033: * A -> C
034: * B has no dependency
035: * C -> B
036: * D -> A , B
037: * E has no dependency
038: */
039:
040: //CheckStyle:MagicNumber| OFF
041: //The test very often use MagicNumber. Using a constant is less expessif.
042:
043: public void testSimple() {
044: Project p = new Project();
045:
046: IvyBuildList buildlist = new IvyBuildList();
047: buildlist.setProject(p);
048:
049: FileSet fs = new FileSet();
050: fs.setDir(new File("test/buildlist"));
051: fs.setIncludes("**/build.xml");
052: buildlist.addFileset(fs);
053:
054: buildlist.setReference("ordered.build.files");
055:
056: buildlist.execute();
057:
058: Object o = p.getReference("ordered.build.files");
059: assertNotNull(o);
060: assertTrue(o instanceof Path);
061:
062: Path path = (Path) o;
063: String[] files = path.list();
064: assertNotNull(files);
065:
066: assertEquals(5, files.length);
067:
068: assertEquals(new File("test/buildlist/B/build.xml")
069: .getAbsolutePath(), new File(files[0])
070: .getAbsolutePath());
071: assertEquals(new File("test/buildlist/C/build.xml")
072: .getAbsolutePath(), new File(files[1])
073: .getAbsolutePath());
074: assertEquals(new File("test/buildlist/A/build.xml")
075: .getAbsolutePath(), new File(files[2])
076: .getAbsolutePath());
077: assertEquals(new File("test/buildlist/D/build.xml")
078: .getAbsolutePath(), new File(files[3])
079: .getAbsolutePath());
080: assertEquals(new File("test/buildlist/E/build.xml")
081: .getAbsolutePath(), new File(files[4])
082: .getAbsolutePath());
083: }
084:
085: public void testReverse() {
086: Project p = new Project();
087:
088: IvyBuildList buildlist = new IvyBuildList();
089: buildlist.setProject(p);
090: buildlist.setReverse(true);
091:
092: FileSet fs = new FileSet();
093: fs.setDir(new File("test/buildlist"));
094: fs.setIncludes("**/build.xml");
095: buildlist.addFileset(fs);
096:
097: buildlist.setReference("reverse.ordered.build.files");
098:
099: buildlist.execute();
100:
101: Object o = p.getReference("reverse.ordered.build.files");
102: assertNotNull(o);
103: assertTrue(o instanceof Path);
104:
105: Path path = (Path) o;
106: String[] files = path.list();
107: assertNotNull(files);
108:
109: assertEquals(5, files.length);
110:
111: assertEquals(new File("test/buildlist/E/build.xml")
112: .getAbsolutePath(), new File(files[0])
113: .getAbsolutePath());
114: assertEquals(new File("test/buildlist/D/build.xml")
115: .getAbsolutePath(), new File(files[1])
116: .getAbsolutePath());
117: assertEquals(new File("test/buildlist/A/build.xml")
118: .getAbsolutePath(), new File(files[2])
119: .getAbsolutePath());
120: assertEquals(new File("test/buildlist/C/build.xml")
121: .getAbsolutePath(), new File(files[3])
122: .getAbsolutePath());
123: assertEquals(new File("test/buildlist/B/build.xml")
124: .getAbsolutePath(), new File(files[4])
125: .getAbsolutePath());
126:
127: }
128:
129: public void testWithRoot() {
130: Project p = new Project();
131:
132: IvyBuildList buildlist = new IvyBuildList();
133: buildlist.setProject(p);
134: buildlist.setRoot("C");
135:
136: FileSet fs = new FileSet();
137: fs.setDir(new File("test/buildlist"));
138: fs.setIncludes("**/build.xml");
139: buildlist.addFileset(fs);
140:
141: buildlist.setReference("ordered.build.files");
142:
143: buildlist.execute();
144:
145: Object o = p.getReference("ordered.build.files");
146: assertNotNull(o);
147: assertTrue(o instanceof Path);
148:
149: Path path = (Path) o;
150: String[] files = path.list();
151: assertNotNull(files);
152: assertEquals(2, files.length); // A and D should be filtered out
153:
154: assertEquals(new File("test/buildlist/B/build.xml")
155: .getAbsolutePath(), new File(files[0])
156: .getAbsolutePath());
157: assertEquals(new File("test/buildlist/C/build.xml")
158: .getAbsolutePath(), new File(files[1])
159: .getAbsolutePath());
160: }
161:
162: public void testWithTwoRoots() {
163: Project p = new Project();
164:
165: IvyBuildList buildlist = new IvyBuildList();
166: buildlist.setProject(p);
167: buildlist.setRoot("C,E");
168:
169: FileSet fs = new FileSet();
170: fs.setDir(new File("test/buildlist"));
171: fs.setIncludes("**/build.xml");
172: buildlist.addFileset(fs);
173:
174: buildlist.setReference("ordered.build.files");
175:
176: buildlist.execute();
177:
178: Object o = p.getReference("ordered.build.files");
179: assertNotNull(o);
180: assertTrue(o instanceof Path);
181:
182: Path path = (Path) o;
183: String[] files = path.list();
184: assertNotNull(files);
185:
186: assertEquals(3, files.length); // A and D should be filtered out
187:
188: assertEquals(new File("test/buildlist/B/build.xml")
189: .getAbsolutePath(), new File(files[0])
190: .getAbsolutePath());
191: assertEquals(new File("test/buildlist/C/build.xml")
192: .getAbsolutePath(), new File(files[1])
193: .getAbsolutePath());
194: assertEquals(new File("test/buildlist/E/build.xml")
195: .getAbsolutePath(), new File(files[2])
196: .getAbsolutePath());
197: }
198:
199: public void testWithRootExclude() {
200: Project p = new Project();
201:
202: IvyBuildList buildlist = new IvyBuildList();
203: buildlist.setProject(p);
204: buildlist.setRoot("C");
205: buildlist.setExcludeRoot(true);
206:
207: FileSet fs = new FileSet();
208: fs.setDir(new File("test/buildlist"));
209: fs.setIncludes("**/build.xml");
210: buildlist.addFileset(fs);
211:
212: buildlist.setReference("ordered.build.files");
213:
214: buildlist.execute();
215:
216: Object o = p.getReference("ordered.build.files");
217: assertNotNull(o);
218: assertTrue(o instanceof Path);
219:
220: Path path = (Path) o;
221: String[] files = path.list();
222: assertNotNull(files);
223: assertEquals(1, files.length); // A, D and C should be filtered out
224:
225: assertEquals(new File("test/buildlist/B/build.xml")
226: .getAbsolutePath(), new File(files[0])
227: .getAbsolutePath());
228: }
229:
230: public void testWithRootAndOnlyDirectDep() {
231: Project p = new Project();
232:
233: IvyBuildList buildlist = new IvyBuildList();
234: buildlist.setProject(p);
235: buildlist.setRoot("A");
236: buildlist.setOnlydirectdep(true);
237:
238: FileSet fs = new FileSet();
239: fs.setDir(new File("test/buildlist"));
240: fs.setIncludes("**/build.xml");
241: buildlist.addFileset(fs);
242:
243: buildlist.setReference("ordered.build.files");
244:
245: buildlist.execute();
246:
247: Object o = p.getReference("ordered.build.files");
248: assertNotNull(o);
249: assertTrue(o instanceof Path);
250:
251: Path path = (Path) o;
252: String[] files = path.list();
253: assertNotNull(files);
254: assertEquals(2, files.length); // We should have only A and C
255:
256: assertEquals(new File("test/buildlist/C/build.xml")
257: .getAbsolutePath(), new File(files[0])
258: .getAbsolutePath());
259: assertEquals(new File("test/buildlist/A/build.xml")
260: .getAbsolutePath(), new File(files[1])
261: .getAbsolutePath());
262: }
263:
264: public void testWithLeaf() {
265: Project p = new Project();
266:
267: IvyBuildList buildlist = new IvyBuildList();
268: buildlist.setProject(p);
269: buildlist.setLeaf("C");
270:
271: FileSet fs = new FileSet();
272: fs.setDir(new File("test/buildlist"));
273: fs.setIncludes("**/build.xml");
274: buildlist.addFileset(fs);
275:
276: buildlist.setReference("ordered.build.files");
277:
278: buildlist.execute();
279:
280: Object o = p.getReference("ordered.build.files");
281: assertNotNull(o);
282: assertTrue(o instanceof Path);
283:
284: Path path = (Path) o;
285: String[] files = path.list();
286: assertNotNull(files);
287:
288: assertEquals(3, files.length); // B should be filtered out
289:
290: assertEquals(new File("test/buildlist/C/build.xml")
291: .getAbsolutePath(), new File(files[0])
292: .getAbsolutePath());
293: assertEquals(new File("test/buildlist/A/build.xml")
294: .getAbsolutePath(), new File(files[1])
295: .getAbsolutePath());
296: assertEquals(new File("test/buildlist/D/build.xml")
297: .getAbsolutePath(), new File(files[2])
298: .getAbsolutePath());
299: }
300:
301: public void testWithTwoLeafs() {
302: Project p = new Project();
303:
304: IvyBuildList buildlist = new IvyBuildList();
305: buildlist.setProject(p);
306: buildlist.setLeaf("C,E");
307:
308: FileSet fs = new FileSet();
309: fs.setDir(new File("test/buildlist"));
310: fs.setIncludes("**/build.xml");
311: buildlist.addFileset(fs);
312:
313: buildlist.setReference("ordered.build.files");
314:
315: buildlist.execute();
316:
317: Object o = p.getReference("ordered.build.files");
318: assertNotNull(o);
319: assertTrue(o instanceof Path);
320:
321: Path path = (Path) o;
322: String[] files = path.list();
323: assertNotNull(files);
324:
325: assertEquals(4, files.length); // B should be filtered out
326:
327: assertEquals(new File("test/buildlist/C/build.xml")
328: .getAbsolutePath(), new File(files[0])
329: .getAbsolutePath());
330: assertEquals(new File("test/buildlist/A/build.xml")
331: .getAbsolutePath(), new File(files[1])
332: .getAbsolutePath());
333: assertEquals(new File("test/buildlist/D/build.xml")
334: .getAbsolutePath(), new File(files[2])
335: .getAbsolutePath());
336: assertEquals(new File("test/buildlist/E/build.xml")
337: .getAbsolutePath(), new File(files[3])
338: .getAbsolutePath());
339:
340: }
341:
342: public void testWithLeafExclude() {
343: Project p = new Project();
344:
345: IvyBuildList buildlist = new IvyBuildList();
346: buildlist.setProject(p);
347: buildlist.setLeaf("C");
348: buildlist.setExcludeLeaf(true);
349:
350: FileSet fs = new FileSet();
351: fs.setDir(new File("test/buildlist"));
352: fs.setIncludes("**/build.xml");
353: buildlist.addFileset(fs);
354:
355: buildlist.setReference("ordered.build.files");
356:
357: buildlist.execute();
358:
359: Object o = p.getReference("ordered.build.files");
360: assertNotNull(o);
361: assertTrue(o instanceof Path);
362:
363: Path path = (Path) o;
364: String[] files = path.list();
365: assertNotNull(files);
366: assertEquals(2, files.length); // B and C should be filtered out
367:
368: assertEquals(new File("test/buildlist/A/build.xml")
369: .getAbsolutePath(), new File(files[0])
370: .getAbsolutePath());
371: assertEquals(new File("test/buildlist/D/build.xml")
372: .getAbsolutePath(), new File(files[1])
373: .getAbsolutePath());
374: }
375:
376: public void testWithLeafAndOnlyDirectDep() {
377: Project p = new Project();
378:
379: IvyBuildList buildlist = new IvyBuildList();
380: buildlist.setProject(p);
381: buildlist.setLeaf("C");
382: buildlist.setOnlydirectdep(true);
383:
384: FileSet fs = new FileSet();
385: fs.setDir(new File("test/buildlist"));
386: fs.setIncludes("**/build.xml");
387: buildlist.addFileset(fs);
388:
389: buildlist.setReference("ordered.build.files");
390:
391: buildlist.execute();
392:
393: Object o = p.getReference("ordered.build.files");
394: assertNotNull(o);
395: assertTrue(o instanceof Path);
396:
397: Path path = (Path) o;
398: String[] files = path.list();
399: assertNotNull(files);
400: assertEquals(2, files.length); // We must have only A and C
401:
402: assertEquals(new File("test/buildlist/C/build.xml")
403: .getAbsolutePath(), new File(files[0])
404: .getAbsolutePath());
405: assertEquals(new File("test/buildlist/A/build.xml")
406: .getAbsolutePath(), new File(files[1])
407: .getAbsolutePath());
408: }
409:
410: public void testRestartFrom() {
411: Project p = new Project();
412:
413: IvyBuildList buildlist = new IvyBuildList();
414: buildlist.setProject(p);
415: buildlist.setRestartFrom("C");
416:
417: FileSet fs = new FileSet();
418: fs.setDir(new File("test/buildlist"));
419: fs.setIncludes("**/build.xml");
420: buildlist.addFileset(fs);
421:
422: buildlist.setReference("ordered.build.files");
423:
424: buildlist.execute();
425:
426: Object o = p.getReference("ordered.build.files");
427: assertNotNull(o);
428: assertTrue(o instanceof Path);
429:
430: Path path = (Path) o;
431: String[] files = path.list();
432: assertNotNull(files);
433:
434: assertEquals(4, files.length);
435:
436: assertEquals(new File("test/buildlist/C/build.xml")
437: .getAbsolutePath(), new File(files[0])
438: .getAbsolutePath());
439: assertEquals(new File("test/buildlist/A/build.xml")
440: .getAbsolutePath(), new File(files[1])
441: .getAbsolutePath());
442: assertEquals(new File("test/buildlist/D/build.xml")
443: .getAbsolutePath(), new File(files[2])
444: .getAbsolutePath());
445: assertEquals(new File("test/buildlist/E/build.xml")
446: .getAbsolutePath(), new File(files[3])
447: .getAbsolutePath());
448: }
449:
450: }
451: //CheckStyle:MagicNumber| ON
|