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: import java.io.IOException;
022:
023: import junit.framework.TestCase;
024:
025: import org.apache.ivy.TestHelper;
026: import org.apache.ivy.core.IvyPatternHelper;
027: import org.apache.ivy.util.CacheCleaner;
028: import org.apache.tools.ant.BuildException;
029: import org.apache.tools.ant.Project;
030:
031: public class IvyRetrieveTest extends TestCase {
032: private static final String IVY_RETRIEVE_PATTERN = "build/test/lib/[organisation]/[module]/ivy-[revision].xml";
033:
034: private static final String RETRIEVE_PATTERN = "build/test/lib/[conf]/[artifact]-[revision].[type]";
035:
036: private File cache;
037:
038: private IvyRetrieve retrieve;
039:
040: private Project project;
041:
042: protected void setUp() throws Exception {
043: createCache();
044: CacheCleaner.deleteDir(new File("build/test/lib"));
045: project = new Project();
046: project.setProperty("ivy.settings.file",
047: "test/repositories/ivysettings.xml");
048:
049: retrieve = new IvyRetrieve();
050: retrieve.setProject(project);
051: System.setProperty("ivy.cache.dir", cache.getAbsolutePath());
052: retrieve.setPattern(RETRIEVE_PATTERN);
053: }
054:
055: private void createCache() {
056: cache = new File("build/cache");
057: cache.mkdirs();
058: }
059:
060: protected void tearDown() throws Exception {
061: CacheCleaner.deleteDir(cache);
062: CacheCleaner.deleteDir(new File("build/test/lib"));
063: }
064:
065: public void testSimple() throws Exception {
066: project.setProperty("ivy.dep.file",
067: "test/java/org/apache/ivy/ant/ivy-simple.xml");
068: retrieve.execute();
069: assertTrue(new File(IvyPatternHelper.substitute(
070: RETRIEVE_PATTERN, "org1", "mod1.2", "2.0", "mod1.2",
071: "jar", "jar")).exists());
072: }
073:
074: public void testRetrievePrivateWithWildcard() throws Exception {
075: project.setProperty("ivy.dep.file",
076: "test/java/org/apache/ivy/ant/ivy-381.xml");
077: retrieve.setConf("*");
078: retrieve.execute();
079: assertTrue(new File(IvyPatternHelper.substitute(
080: RETRIEVE_PATTERN, "org1", "mod1.2", "1.1", "mod1.2",
081: "jar", "jar", "public")).exists());
082: assertTrue(new File(IvyPatternHelper.substitute(
083: RETRIEVE_PATTERN, "org3", "mod3.2", "1.4", "mod3.2",
084: "jar", "jar", "private")).exists());
085: }
086:
087: public void testInline() throws Exception {
088: // we first resolve another ivy file
089: IvyResolve resolve = new IvyResolve();
090: resolve.setProject(project);
091: resolve.setFile(new File(
092: "test/java/org/apache/ivy/ant/ivy-latest.xml"));
093: resolve.execute();
094:
095: assertTrue(getArchiveFileInCache("org1", "mod1.2", "2.2",
096: "mod1.2", "jar", "jar").exists());
097:
098: // then we resolve a dependency directly
099: retrieve.setOrganisation("org1");
100: retrieve.setModule("mod1.2");
101: retrieve.setRevision("2.0");
102: retrieve.setInline(true);
103: retrieve.execute();
104: assertTrue(new File(IvyPatternHelper.substitute(
105: RETRIEVE_PATTERN, "org1", "mod1.2", "2.0", "mod1.2",
106: "jar", "jar")).exists());
107: }
108:
109: public void testWithConf() throws Exception {
110: project.setProperty("ivy.dep.file",
111: "test/repositories/1/org6/mod6.2/ivys/ivy-0.4.xml");
112: retrieve.execute();
113:
114: assertTrue(new File(IvyPatternHelper.substitute(
115: RETRIEVE_PATTERN, "org6", "mod6.1", "0.4", "mod6.1",
116: "jar", "jar", "default")).exists());
117: assertTrue(new File(IvyPatternHelper.substitute(
118: RETRIEVE_PATTERN, "org6", "mod6.1", "0.4", "mod6.1",
119: "jar", "jar", "extension")).exists());
120: assertTrue(new File(IvyPatternHelper.substitute(
121: RETRIEVE_PATTERN, "org1", "mod1.2", "2.1", "mod1.2",
122: "jar", "jar", "extension")).exists());
123: }
124:
125: public void testSync() throws Exception {
126: project.setProperty("ivy.dep.file",
127: "test/repositories/1/org6/mod6.2/ivys/ivy-0.4.xml");
128: retrieve.setSync(true);
129:
130: File[] old = new File[] {
131: new File(IvyPatternHelper.substitute(RETRIEVE_PATTERN,
132: "org6", "mod6.1", "0.4", "mod6.1", "jar",
133: "jar", "unknown")), // unknown configuration
134: new File(IvyPatternHelper.substitute(RETRIEVE_PATTERN,
135: "org6", "mod6.1", "0.4", "mod6.1", "unknown",
136: "unknown", "default")), // unknown type
137: new File(IvyPatternHelper.substitute(RETRIEVE_PATTERN,
138: "org6", "mod6.1", "0.4", "unknown", "jar",
139: "jar", "default")), // unknown artifact
140: new File(IvyPatternHelper.substitute(RETRIEVE_PATTERN,
141: "org6", "mod6.1", "unknown", "mod6.1", "jar",
142: "jar", "default")), // unknown revision
143: };
144: for (int i = 0; i < old.length; i++) {
145: touch(old[i]);
146: }
147: retrieve.execute();
148:
149: assertTrue(new File(IvyPatternHelper.substitute(
150: RETRIEVE_PATTERN, "org6", "mod6.1", "0.4", "mod6.1",
151: "jar", "jar", "default")).exists());
152: assertTrue(new File(IvyPatternHelper.substitute(
153: RETRIEVE_PATTERN, "org6", "mod6.1", "0.4", "mod6.1",
154: "jar", "jar", "extension")).exists());
155: assertTrue(new File(IvyPatternHelper.substitute(
156: RETRIEVE_PATTERN, "org1", "mod1.2", "2.1", "mod1.2",
157: "jar", "jar", "extension")).exists());
158: for (int i = 0; i < old.length; i++) {
159: assertFalse(old[i] + " should have been deleted by sync",
160: old[i].exists());
161: }
162: assertFalse(new File("build/test/lib/unknown").exists()); // even conf directory should
163: // have been deleted
164: }
165:
166: public void testWithAPreviousResolve() throws Exception {
167: // first we do a resolve in another project
168: Project project = new Project();
169: project.setProperty("ivy.settings.file",
170: "test/repositories/ivysettings.xml");
171: project.setProperty("ivy.dep.file",
172: "test/java/org/apache/ivy/ant/ivy-simple.xml");
173: IvyResolve resolve = new IvyResolve();
174: resolve.setProject(project);
175: resolve.execute();
176:
177: // then we do a retrieve with the correct module information
178: retrieve.setOrganisation("apache");
179: retrieve.setModule("resolve-simple");
180: retrieve.setConf("default");
181: retrieve.execute();
182:
183: assertTrue(new File(IvyPatternHelper.substitute(
184: RETRIEVE_PATTERN, "org1", "mod1.2", "2.0", "mod1.2",
185: "jar", "jar")).exists());
186: }
187:
188: public void testWithAPreviousResolveAndResolveId() throws Exception {
189: // first we do a resolve in another project
190: Project project = new Project();
191: project.setProperty("ivy.settings.file",
192: "test/repositories/ivysettings.xml");
193: project.setProperty("ivy.dep.file",
194: "test/java/org/apache/ivy/ant/ivy-simple.xml");
195: IvyResolve resolve = new IvyResolve();
196: resolve.setProject(project);
197: resolve.setResolveId("testWithAPreviousResolveAndResolveId");
198: resolve.execute();
199:
200: // then we do a retrieve with the correct module information
201: retrieve.setOrganisation("apache");
202: retrieve.setModule("resolve-simple");
203: retrieve.setConf("default");
204: retrieve.setResolveId("testWithAPreviousResolveAndResolveId");
205: retrieve.execute();
206:
207: assertTrue(new File(IvyPatternHelper.substitute(
208: RETRIEVE_PATTERN, "org1", "mod1.2", "2.0", "mod1.2",
209: "jar", "jar")).exists());
210: }
211:
212: public void testUseOrigin() throws Exception {
213: // test case for IVY-304
214: // first we do a resolve with useOrigin=true in another project
215: Project project = new Project();
216: project.setProperty("ivy.settings.file",
217: "test/repositories/ivysettings.xml");
218: project.setProperty("ivy.dep.file",
219: "test/java/org/apache/ivy/ant/ivy-simple.xml");
220: IvyResolve resolve = new IvyResolve();
221: resolve.setProject(project);
222: resolve.setUseOrigin(true);
223: resolve.execute();
224:
225: // then we do a retrieve with the correct module information and useOrigin=false
226: retrieve.setOrganisation("apache");
227: retrieve.setModule("resolve-simple");
228: retrieve.setConf("default");
229: retrieve.setUseOrigin(false);
230: retrieve.execute();
231:
232: assertTrue(new File(IvyPatternHelper.substitute(
233: RETRIEVE_PATTERN, "org1", "mod1.2", "2.0", "mod1.2",
234: "jar", "jar")).exists());
235: }
236:
237: public void testRetrieveWithOriginalNamePattern() throws Exception {
238: retrieve.setFile(new File(
239: "test/java/org/apache/ivy/ant/ivy-631.xml"));
240: retrieve.setConf("default");
241: retrieve
242: .setPattern("build/test/lib/[conf]/[originalname].[ext]");
243: retrieve.setSync(true);
244: retrieve.execute();
245:
246: assertTrue(new File("build/test/lib/default/mod1.2-2.2.jar")
247: .exists());
248: }
249:
250: public void testFailureWithoutAPreviousResolve() throws Exception {
251: // we do a retrieve with the module information whereas no resolve has been previously done
252: try {
253: retrieve.setOrganisation("apache");
254: retrieve.setModule("resolve-simple");
255: retrieve.setConf("default");
256: retrieve.execute();
257: fail("retrieve without previous resolve should have thrown an exception");
258: } catch (Exception ex) {
259: // OK
260: }
261: }
262:
263: public void testFailure() throws Exception {
264: try {
265: project.setProperty("ivy.dep.file",
266: "test/java/org/apache/ivy/ant/ivy-failure.xml");
267: retrieve.execute();
268: fail("failure didn't raised an exception with default haltonfailure setting");
269: } catch (BuildException ex) {
270: // ok => should raised an exception
271: }
272: }
273:
274: public void testHaltOnFailure() throws Exception {
275: try {
276: project.setProperty("ivy.dep.file",
277: "test/java/org/apache/ivy/ant/ivy-failure.xml");
278: retrieve.setHaltonfailure(false);
279: retrieve.execute();
280:
281: } catch (BuildException ex) {
282: fail("failure raised an exception with haltonfailure set to false");
283: }
284: }
285:
286: public void testCustomIvyPattern() throws Exception {
287: // mod2.5 depends on virtual mod2.3 which depends on mod2.1 which depends on mod1.1 which
288: // depends on mod1.2
289: project.setProperty("ivy.dep.file",
290: "test/repositories/1/org2/mod2.5/ivys/ivy-0.6.1.xml");
291:
292: String ivyPattern = IVY_RETRIEVE_PATTERN;
293:
294: retrieve.setIvypattern(ivyPattern);
295: retrieve.execute();
296:
297: assertTrue(new File(IvyPatternHelper.substitute(ivyPattern,
298: "org2", "mod2.3", "0.4.1", "ivy", "ivy", "xml"))
299: .exists());
300: assertTrue(new File(IvyPatternHelper.substitute(ivyPattern,
301: "org2", "mod2.1", "0.3", "ivy", "ivy", "xml")).exists());
302: assertTrue(new File(IvyPatternHelper.substitute(ivyPattern,
303: "org1", "mod1.1", "1.0", "ivy", "ivy", "xml")).exists());
304: assertFalse(new File(IvyPatternHelper.substitute(ivyPattern,
305: "org1", "mod1.2", "2.0", "ivy", "ivy", "xml")).exists());
306: }
307:
308: public void testCustomIvyPatternWithConf() throws Exception {
309: project.setProperty("ivy.dep.file",
310: "test/repositories/1/org6/mod6.2/ivys/ivy-0.4.xml");
311:
312: String ivyPattern = "build/test/lib/[conf]/[organisation]/[module]/ivy-[revision].xml";
313:
314: retrieve.setIvypattern(ivyPattern);
315: retrieve.execute();
316:
317: assertTrue(new File(IvyPatternHelper
318: .substitute(ivyPattern, "org6", "mod6.1", "0.4", "ivy",
319: "ivy", "xml", "default")).exists());
320: assertTrue(new File(IvyPatternHelper.substitute(ivyPattern,
321: "org6", "mod6.1", "0.4", "ivy", "ivy", "xml",
322: "extension")).exists());
323: assertFalse(new File(IvyPatternHelper.substitute(ivyPattern,
324: "org1", "mod1.2", "2.1", "ivy", "ivy", "xml",
325: "extension")).exists());
326: }
327:
328: public void testSyncWithIvyPattern() throws Exception {
329: project.setProperty("ivy.dep.file",
330: "test/repositories/1/org6/mod6.2/ivys/ivy-0.4.xml");
331:
332: String ivyPattern = "build/test/lib/[conf]/[organisation]/[module]/ivy-[revision].xml";
333:
334: retrieve.setIvypattern(ivyPattern);
335:
336: retrieve.setSync(true);
337:
338: File[] old = new File[] {
339: new File(IvyPatternHelper.substitute(RETRIEVE_PATTERN,
340: "org6", "mod6.1", "0.4", "mod6.1", "jar",
341: "jar", "unknown")), // unknown configuration
342: new File(IvyPatternHelper.substitute(RETRIEVE_PATTERN,
343: "org6", "mod6.1", "0.4", "mod6.1", "unknown",
344: "unknown", "default")), // unknown type
345: new File(IvyPatternHelper.substitute(ivyPattern,
346: "org6", "mod6.1", "0.4", "ivy", "ivy", "xml",
347: "unk")), // unknown conf for ivy
348: new File(IvyPatternHelper.substitute(ivyPattern,
349: "unknown", "mod6.1", "0.4", "ivy", "ivy",
350: "xml", "default")), // unknown organisation for ivy
351: };
352: for (int i = 0; i < old.length; i++) {
353: touch(old[i]);
354: }
355:
356: retrieve.execute();
357:
358: assertTrue(new File(IvyPatternHelper
359: .substitute(ivyPattern, "org6", "mod6.1", "0.4", "ivy",
360: "ivy", "xml", "default")).exists());
361: assertTrue(new File(IvyPatternHelper.substitute(ivyPattern,
362: "org6", "mod6.1", "0.4", "ivy", "ivy", "xml",
363: "extension")).exists());
364: assertFalse(new File(IvyPatternHelper.substitute(ivyPattern,
365: "org1", "mod1.2", "2.1", "ivy", "ivy", "xml",
366: "extension")).exists());
367: for (int i = 0; i < old.length; i++) {
368: assertFalse(old[i] + " should have been deleted by sync",
369: old[i].exists());
370: }
371: assertFalse(new File("build/test/lib/unknown").exists());
372: assertFalse(new File("build/test/lib/unk").exists());
373: assertFalse(new File("build/test/lib/default/unknown").exists());
374: }
375:
376: public void testDoubleRetrieveWithDifferentConfigurations() {
377: // IVY-315
378: project.setProperty("ivy.dep.file",
379: "test/java/org/apache/ivy/ant/ivy-doubleretrieve.xml");
380:
381: retrieve.setConf("compile");
382: retrieve.execute();
383:
384: retrieve = new IvyRetrieve();
385: retrieve.setProject(project);
386: retrieve.setPattern(RETRIEVE_PATTERN);
387: retrieve.setConf("compile,unittest");
388: retrieve.execute();
389: }
390:
391: // creates an empty file, creating parent directories if necessary
392: private void touch(File file) throws IOException {
393: if (file.getParentFile() != null) {
394: file.getParentFile().mkdirs();
395: }
396: file.createNewFile();
397: }
398:
399: private File getArchiveFileInCache(String organisation,
400: String module, String revision, String artifact,
401: String type, String ext) {
402: return TestHelper.getArchiveFileInCache(retrieve
403: .getIvyInstance(), organisation, module, revision,
404: artifact, type, ext);
405: }
406:
407: }
|