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.core.retrieve;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.util.ArrayList;
023: import java.util.List;
024:
025: import junit.framework.TestCase;
026:
027: import org.apache.ivy.Ivy;
028: import org.apache.ivy.core.IvyPatternHelper;
029: import org.apache.ivy.core.event.IvyEvent;
030: import org.apache.ivy.core.event.IvyListener;
031: import org.apache.ivy.core.event.retrieve.EndRetrieveEvent;
032: import org.apache.ivy.core.event.retrieve.StartRetrieveEvent;
033: import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
034: import org.apache.ivy.core.report.ResolveReport;
035: import org.apache.ivy.core.resolve.ResolveOptions;
036: import org.apache.tools.ant.Project;
037: import org.apache.tools.ant.taskdefs.Delete;
038:
039: public class RetrieveTest extends TestCase {
040: private Ivy ivy;
041:
042: private File cache;
043:
044: protected void setUp() throws Exception {
045: ivy = Ivy.newInstance();
046: ivy.configure(new File("test/repositories/ivysettings.xml"));
047: createCache();
048: }
049:
050: private void createCache() {
051: cache = new File("build/cache");
052: cache.mkdirs();
053: }
054:
055: protected void tearDown() throws Exception {
056: cleanCache();
057: Delete del = new Delete();
058: del.setProject(new Project());
059: del.setDir(new File("build/test/retrieve"));
060: del.execute();
061: }
062:
063: private void cleanCache() {
064: Delete del = new Delete();
065: del.setProject(new Project());
066: del.setDir(cache);
067: del.execute();
068: }
069:
070: public void testRetrieveSimple() throws Exception {
071: // mod1.1 depends on mod1.2
072: ResolveReport report = ivy.resolve(new File(
073: "test/repositories/1/org1/mod1.1/ivys/ivy-1.0.xml")
074: .toURL(), getResolveOptions(new String[] { "*" }));
075: assertNotNull(report);
076: ModuleDescriptor md = report.getModuleDescriptor();
077: assertNotNull(md);
078:
079: String pattern = "build/test/retrieve/[module]/[conf]/[artifact]-[revision].[ext]";
080: ivy.retrieve(md.getModuleRevisionId(), pattern,
081: getRetrieveOptions());
082: assertTrue(new File(IvyPatternHelper.substitute(pattern,
083: "org1", "mod1.2", "2.0", "mod1.2", "jar", "jar",
084: "default")).exists());
085:
086: pattern = "build/test/retrieve/[module]/[conf]/[type]s/[artifact]-[revision].[ext]";
087: ivy.retrieve(md.getModuleRevisionId(), pattern,
088: getRetrieveOptions());
089: assertTrue(new File(IvyPatternHelper.substitute(pattern,
090: "org1", "mod1.2", "2.0", "mod1.2", "jar", "jar",
091: "default")).exists());
092: }
093:
094: public void testEvent() throws Exception {
095: ResolveReport report = ivy.resolve(new File(
096: "test/repositories/1/org1/mod1.1/ivys/ivy-1.0.xml")
097: .toURL(), getResolveOptions(new String[] { "*" }));
098:
099: final List events = new ArrayList();
100: ivy.getEventManager().addIvyListener(new IvyListener() {
101: public void progress(IvyEvent event) {
102: events.add(event);
103: }
104: });
105: ModuleDescriptor md = report.getModuleDescriptor();
106: String pattern = "build/test/retrieve/[module]/[conf]/[artifact]-[revision].[ext]";
107: ivy.retrieve(md.getModuleRevisionId(), pattern,
108: getRetrieveOptions());
109: assertEquals(2, events.size());
110: assertTrue(events.get(0) instanceof StartRetrieveEvent);
111: assertTrue(events.get(1) instanceof EndRetrieveEvent);
112: EndRetrieveEvent ev = (EndRetrieveEvent) events.get(1);
113: assertEquals(1, ev.getNbCopied());
114: assertEquals(0, ev.getNbUpToDate());
115:
116: events.clear();
117: ivy.retrieve(md.getModuleRevisionId(), pattern,
118: getRetrieveOptions());
119: assertEquals(2, events.size());
120: assertTrue(events.get(0) instanceof StartRetrieveEvent);
121: assertTrue(events.get(1) instanceof EndRetrieveEvent);
122: ev = (EndRetrieveEvent) events.get(1);
123: assertEquals(0, ev.getNbCopied());
124: assertEquals(1, ev.getNbUpToDate());
125: }
126:
127: public void testRetrieveOverwrite() throws Exception {
128: // mod1.1 depends on mod1.2
129: ResolveReport report = ivy.resolve(new File(
130: "test/repositories/1/org1/mod1.1/ivys/ivy-1.0.xml")
131: .toURL(), getResolveOptions(new String[] { "*" }));
132: assertNotNull(report);
133: ModuleDescriptor md = report.getModuleDescriptor();
134: assertNotNull(md);
135:
136: String pattern = "build/test/retrieve/[module]/[conf]/[artifact]-[revision].[ext]";
137:
138: // we create a fake old file to see if it is overwritten
139: File file = new File(IvyPatternHelper.substitute(pattern,
140: "org1", "mod1.2", "2.0", "mod1.2", "jar", "jar",
141: "default"));
142: file.getParentFile().mkdirs();
143: file.createNewFile();
144: file.setLastModified(10000);
145: ivy.retrieve(md.getModuleRevisionId(), pattern,
146: getRetrieveOptions());
147: assertEquals(new File(
148: "test/repositories/1/org1/mod1.2/jars/mod1.2-2.0.jar")
149: .lastModified(), file.lastModified());
150: }
151:
152: public void testRetrieveWithSymlinks() throws Exception {
153: // mod1.1 depends on mod1.2
154: ResolveReport report = ivy.resolve(new File(
155: "test/repositories/1/org1/mod1.1/ivys/ivy-1.0.xml")
156: .toURL(), getResolveOptions(new String[] { "*" }));
157: assertNotNull(report);
158: ModuleDescriptor md = report.getModuleDescriptor();
159: assertNotNull(md);
160:
161: String pattern = "build/test/retrieve/[module]/[conf]/[artifact]-[revision].[ext]";
162: ivy.retrieve(md.getModuleRevisionId(), pattern,
163: getRetrieveOptions().setMakeSymlinks(true));
164: assertLink(IvyPatternHelper.substitute(pattern, "org1",
165: "mod1.2", "2.0", "mod1.2", "jar", "jar", "default"));
166:
167: pattern = "build/test/retrieve/[module]/[conf]/[type]s/[artifact]-[revision].[ext]";
168: ivy.retrieve(md.getModuleRevisionId(), pattern,
169: getRetrieveOptions().setMakeSymlinks(true));
170: assertLink(IvyPatternHelper.substitute(pattern, "org1",
171: "mod1.2", "2.0", "mod1.2", "jar", "jar", "default"));
172: }
173:
174: private void assertLink(String filename) throws IOException {
175: // if the OS is known to support symlink, check that the file is a symlink,
176: // otherwise just check the file exist.
177:
178: File file = new File(filename);
179: assertTrue("The file " + filename + " doesn't exist", file
180: .exists());
181:
182: String os = System.getProperty("os.name");
183: if (os.equals("Linux") || os.equals("Solaris")
184: || os.equals("FreeBSD")) {
185: // these OS should support symnlink, so check that the file is actually a symlink.
186: // this is done be checking that the canonical path is different from the absolute
187: // path.
188: File absFile = file.getAbsoluteFile();
189: File canFile = file.getCanonicalFile();
190: assertFalse("The file " + filename + " isn't a symlink",
191: absFile.equals(canFile));
192: }
193: }
194:
195: public void testRetrieveWithVariable() throws Exception {
196: // mod1.1 depends on mod1.2
197: ivy.setVariable("retrieve.dir", "retrieve");
198: ResolveReport report = ivy.resolve(new File(
199: "test/repositories/1/org1/mod1.1/ivys/ivy-1.0.xml")
200: .toURL(), getResolveOptions(new String[] { "*" }));
201: assertNotNull(report);
202: ModuleDescriptor md = report.getModuleDescriptor();
203: assertNotNull(md);
204:
205: String pattern = "build/test/${retrieve.dir}/[module]/[conf]/[artifact]-[revision].[ext]";
206: ivy.retrieve(md.getModuleRevisionId(), pattern,
207: getRetrieveOptions());
208: pattern = IvyPatternHelper.substituteVariable(pattern,
209: "retrieve.dir", "retrieve");
210: assertTrue(new File(IvyPatternHelper.substitute(pattern,
211: "org1", "mod1.2", "2.0", "mod1.2", "jar", "jar",
212: "default")).exists());
213:
214: pattern = "build/test/${retrieve.dir}/[module]/[conf]/[type]s/[artifact]-[revision].[ext]";
215: ivy.retrieve(md.getModuleRevisionId(), pattern,
216: getRetrieveOptions());
217: pattern = IvyPatternHelper.substituteVariable(pattern,
218: "retrieve.dir", "retrieve");
219: assertTrue(new File(IvyPatternHelper.substitute(pattern,
220: "org1", "mod1.2", "2.0", "mod1.2", "jar", "jar",
221: "default")).exists());
222: }
223:
224: private RetrieveOptions getRetrieveOptions() {
225: return new RetrieveOptions();
226: }
227:
228: private ResolveOptions getResolveOptions(String[] confs) {
229: return new ResolveOptions().setConfs(confs);
230: }
231:
232: }
|