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.plugins.resolver;
019:
020: import java.io.File;
021: import java.util.List;
022:
023: import org.apache.ivy.core.IvyContext;
024: import org.apache.ivy.core.event.EventManager;
025: import org.apache.ivy.core.module.descriptor.Artifact;
026: import org.apache.ivy.core.module.descriptor.DefaultArtifact;
027: import org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor;
028: import org.apache.ivy.core.module.descriptor.DefaultIncludeRule;
029: import org.apache.ivy.core.module.id.ArtifactId;
030: import org.apache.ivy.core.module.id.ModuleRevisionId;
031: import org.apache.ivy.core.report.ArtifactDownloadReport;
032: import org.apache.ivy.core.report.DownloadReport;
033: import org.apache.ivy.core.report.DownloadStatus;
034: import org.apache.ivy.core.resolve.ResolveData;
035: import org.apache.ivy.core.resolve.ResolveEngine;
036: import org.apache.ivy.core.resolve.ResolveOptions;
037: import org.apache.ivy.core.resolve.ResolvedModuleRevision;
038: import org.apache.ivy.core.settings.IvySettings;
039: import org.apache.ivy.core.sort.SortEngine;
040: import org.apache.ivy.plugins.matcher.ExactPatternMatcher;
041: import org.apache.ivy.util.MockMessageLogger;
042: import org.apache.tools.ant.Project;
043: import org.apache.tools.ant.taskdefs.Delete;
044:
045: /**
046: *
047: */
048: public class IBiblioResolverTest extends AbstractDependencyResolverTest {
049: // remote.test
050:
051: private IvySettings _settings;
052:
053: private ResolveEngine _engine;
054:
055: private ResolveData _data;
056:
057: private File _cache;
058:
059: protected void setUp() throws Exception {
060: _settings = new IvySettings();
061: _engine = new ResolveEngine(_settings, new EventManager(),
062: new SortEngine(_settings));
063: _cache = new File("build/cache");
064: _data = new ResolveData(_engine, new ResolveOptions());
065: _cache.mkdirs();
066: _settings.setDefaultCache(_cache);
067: }
068:
069: protected void tearDown() throws Exception {
070: Delete del = new Delete();
071: del.setProject(new Project());
072: del.setDir(_cache);
073: del.execute();
074: }
075:
076: public void testDefaults() {
077: IBiblioResolver resolver = new IBiblioResolver();
078: _settings.setVariable("ivy.ibiblio.default.artifact.root",
079: "http://www.ibiblio.org/mymaven/");
080: _settings.setVariable("ivy.ibiblio.default.artifact.pattern",
081: "[module]/jars/[artifact]-[revision].jar");
082: resolver.setSettings(_settings);
083: List l = resolver.getArtifactPatterns();
084: assertNotNull(l);
085: assertEquals(1, l.size());
086: assertEquals(
087: "http://www.ibiblio.org/mymaven/[module]/jars/[artifact]-[revision].jar",
088: l.get(0));
089: }
090:
091: public void testInitFromConf() throws Exception {
092: _settings.setVariable("ivy.ibiblio.default.artifact.root",
093: "http://www.ibiblio.org/maven/");
094: _settings.setVariable("ivy.ibiblio.default.artifact.pattern",
095: "[module]/jars/[artifact]-[revision].jar");
096: _settings.setVariable("my.ibiblio.root",
097: "http://www.ibiblio.org/mymaven/");
098: _settings.setVariable("my.ibiblio.pattern",
099: "[module]/[artifact]-[revision].jar");
100: _settings.load(IBiblioResolverTest.class
101: .getResource("ibiblioresolverconf.xml"));
102: IBiblioResolver resolver = (IBiblioResolver) _settings
103: .getResolver("ibiblioA");
104: assertNotNull(resolver);
105: List l = resolver.getArtifactPatterns();
106: assertNotNull(l);
107: assertEquals(1, l.size());
108: assertEquals(
109: "http://www.ibiblio.org/mymaven/[module]/[artifact]-[revision].jar",
110: l.get(0));
111:
112: resolver = (IBiblioResolver) _settings.getResolver("ibiblioB");
113: assertNotNull(resolver);
114: l = resolver.getArtifactPatterns();
115: assertNotNull(l);
116: assertEquals(1, l.size());
117: assertEquals(
118: "http://www.ibiblio.org/mymaven/[organisation]/jars/[artifact]-[revision].jar",
119: l.get(0));
120:
121: resolver = (IBiblioResolver) _settings.getResolver("ibiblioC");
122: assertTrue(resolver.isM2compatible());
123: assertNotNull(resolver);
124: l = resolver.getArtifactPatterns();
125: assertNotNull(l);
126: assertEquals(1, l.size());
127: assertEquals(
128: "http://repo1.maven.org/maven2/[organisation]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]",
129: l.get(0));
130:
131: resolver = (IBiblioResolver) _settings.getResolver("ibiblioD");
132: assertFalse(resolver.isM2compatible());
133: assertNotNull(resolver);
134: l = resolver.getArtifactPatterns();
135: assertNotNull(l);
136: assertEquals(1, l.size());
137: assertEquals(
138: "http://www.ibiblio.org/maven/[module]/jars/[artifact]-[revision].jar",
139: l.get(0));
140:
141: resolver = (IBiblioResolver) _settings.getResolver("ibiblioE");
142: assertTrue(resolver.isM2compatible());
143: assertNotNull(resolver);
144: l = resolver.getArtifactPatterns();
145: assertNotNull(l);
146: assertEquals(1, l.size());
147: assertEquals(
148: "http://www.ibiblio.org/mymaven/[organisation]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]",
149: l.get(0));
150:
151: resolver = (IBiblioResolver) _settings.getResolver("ibiblioF");
152: assertTrue(resolver.isM2compatible());
153: assertNotNull(resolver);
154: l = resolver.getArtifactPatterns();
155: assertNotNull(l);
156: assertEquals(1, l.size());
157: assertEquals(
158: "http://www.ibiblio.org/mymaven/[organisation]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]",
159: l.get(0));
160: }
161:
162: public void testIBiblio() throws Exception {
163: String ibiblioRoot = IBiblioHelper.getIBiblioMirror();
164: if (ibiblioRoot == null) {
165: return;
166: }
167:
168: IBiblioResolver resolver = new IBiblioResolver();
169: resolver.setRoot(ibiblioRoot);
170: resolver.setName("test");
171: resolver.setSettings(_settings);
172: assertEquals("test", resolver.getName());
173:
174: ModuleRevisionId mrid = ModuleRevisionId.newInstance("apache",
175: "commons-fileupload", "1.0");
176: ResolvedModuleRevision rmr = resolver.getDependency(
177: new DefaultDependencyDescriptor(mrid, false), _data);
178: assertNotNull(rmr);
179: assertEquals(mrid, rmr.getId());
180:
181: DefaultArtifact artifact = new DefaultArtifact(mrid, rmr
182: .getPublicationDate(), "commons-fileupload", "jar",
183: "jar");
184: DownloadReport report = resolver.download(
185: new Artifact[] { artifact }, downloadOptions());
186: assertNotNull(report);
187:
188: assertEquals(1, report.getArtifactsReports().length);
189:
190: ArtifactDownloadReport ar = report.getArtifactReport(artifact);
191: assertNotNull(ar);
192:
193: assertEquals(artifact, ar.getArtifact());
194: assertEquals(DownloadStatus.SUCCESSFUL, ar.getDownloadStatus());
195:
196: // test to ask to download again, should use cache
197: report = resolver.download(new Artifact[] { artifact },
198: downloadOptions());
199: assertNotNull(report);
200:
201: assertEquals(1, report.getArtifactsReports().length);
202:
203: ar = report.getArtifactReport(artifact);
204: assertNotNull(ar);
205:
206: assertEquals(artifact, ar.getArtifact());
207: assertEquals(DownloadStatus.NO, ar.getDownloadStatus());
208: }
209:
210: public void testErrorReport() throws Exception {
211: IBiblioResolver resolver = new IBiblioResolver();
212: resolver.setRoot("http://unknown.host.comx/");
213: resolver.setName("test");
214: resolver.setM2compatible(true);
215: resolver.setSettings(_settings);
216: assertEquals("test", resolver.getName());
217:
218: MockMessageLogger mockMessageImpl = new MockMessageLogger();
219: IvyContext.getContext().getIvy().getLoggerEngine()
220: .setDefaultLogger(mockMessageImpl);
221:
222: ModuleRevisionId mrid = ModuleRevisionId.newInstance(
223: "org.apache", "commons-fileupload", "1.0");
224: ResolvedModuleRevision rmr = resolver.getDependency(
225: new DefaultDependencyDescriptor(mrid, false), _data);
226: assertNull(rmr);
227:
228: mockMessageImpl
229: .assertLogContains("tried http://unknown.host.comx/org/apache/commons-fileupload/1.0/commons-fileupload-1.0.pom");
230: mockMessageImpl
231: .assertLogContains("tried http://unknown.host.comx/org/apache/commons-fileupload/1.0/commons-fileupload-1.0.jar");
232: }
233:
234: public void testIBiblioArtifacts() throws Exception {
235: String ibiblioRoot = IBiblioHelper.getIBiblioMirror();
236: if (ibiblioRoot == null) {
237: return;
238: }
239:
240: IBiblioResolver resolver = new IBiblioResolver();
241: resolver.setRoot(ibiblioRoot);
242: resolver.setName("test");
243: resolver.setSettings(_settings);
244: assertEquals("test", resolver.getName());
245:
246: ModuleRevisionId mrid = ModuleRevisionId.newInstance("apache",
247: "nanning", "0.9");
248: DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(
249: mrid, false);
250: dd.addIncludeRule("default", new DefaultIncludeRule(
251: new ArtifactId(mrid.getModuleId(), "nanning-profiler",
252: "jar", "jar"), ExactPatternMatcher.INSTANCE,
253: null));
254: dd.addIncludeRule("default", new DefaultIncludeRule(
255: new ArtifactId(mrid.getModuleId(), "nanning-trace",
256: "jar", "jar"), ExactPatternMatcher.INSTANCE,
257: null));
258: ResolvedModuleRevision rmr = resolver.getDependency(dd, _data);
259: assertNotNull(rmr);
260: assertEquals(mrid, rmr.getId());
261:
262: DefaultArtifact profiler = new DefaultArtifact(mrid, rmr
263: .getPublicationDate(), "nanning-profiler", "jar", "jar");
264: DefaultArtifact trace = new DefaultArtifact(mrid, rmr
265: .getPublicationDate(), "nanning-trace", "jar", "jar");
266: DownloadReport report = resolver.download(new Artifact[] {
267: profiler, trace }, downloadOptions());
268: assertNotNull(report);
269:
270: assertEquals(2, report.getArtifactsReports().length);
271:
272: ArtifactDownloadReport ar = report.getArtifactReport(profiler);
273: assertNotNull(ar);
274:
275: assertEquals(profiler, ar.getArtifact());
276: assertEquals(DownloadStatus.SUCCESSFUL, ar.getDownloadStatus());
277:
278: ar = report.getArtifactReport(trace);
279: assertNotNull(ar);
280:
281: assertEquals(trace, ar.getArtifact());
282: assertEquals(DownloadStatus.SUCCESSFUL, ar.getDownloadStatus());
283:
284: // test to ask to download again, should use cache
285: report = resolver.download(new Artifact[] { profiler, trace },
286: downloadOptions());
287: assertNotNull(report);
288:
289: assertEquals(2, report.getArtifactsReports().length);
290:
291: ar = report.getArtifactReport(profiler);
292: assertNotNull(ar);
293:
294: assertEquals(profiler, ar.getArtifact());
295: assertEquals(DownloadStatus.NO, ar.getDownloadStatus());
296:
297: ar = report.getArtifactReport(trace);
298: assertNotNull(ar);
299:
300: assertEquals(trace, ar.getArtifact());
301: assertEquals(DownloadStatus.NO, ar.getDownloadStatus());
302: }
303:
304: public void testUnknown() throws Exception {
305: String ibiblioRoot = IBiblioHelper.getIBiblioMirror();
306: if (ibiblioRoot == null) {
307: return;
308: }
309:
310: IBiblioResolver resolver = new IBiblioResolver();
311: resolver.setRoot(ibiblioRoot);
312: resolver.setName("test");
313: resolver.setSettings(_settings);
314:
315: assertNull(resolver.getDependency(
316: new DefaultDependencyDescriptor(ModuleRevisionId
317: .newInstance("unknown", "unknown", "1.0"),
318: false), _data));
319: }
320:
321: }
|