KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > resolver > URLResolverTest


1 /*
2  * This file is subject to the license found in LICENCE.TXT in the root directory of the project.
3  *
4  * #SNAPSHOT#
5  */

6 package fr.jayasoft.ivy.resolver;
7
8 import java.io.File JavaDoc;
9 import java.util.Date JavaDoc;
10 import java.util.GregorianCalendar JavaDoc;
11
12 import junit.framework.TestCase;
13
14 import org.apache.tools.ant.Project;
15 import org.apache.tools.ant.taskdefs.Delete;
16
17 import fr.jayasoft.ivy.Artifact;
18 import fr.jayasoft.ivy.DefaultArtifact;
19 import fr.jayasoft.ivy.DefaultDependencyArtifactDescriptor;
20 import fr.jayasoft.ivy.DefaultDependencyDescriptor;
21 import fr.jayasoft.ivy.Ivy;
22 import fr.jayasoft.ivy.ModuleRevisionId;
23 import fr.jayasoft.ivy.ResolveData;
24 import fr.jayasoft.ivy.ResolvedModuleRevision;
25 import fr.jayasoft.ivy.matcher.ExactPatternMatcher;
26 import fr.jayasoft.ivy.report.ArtifactDownloadReport;
27 import fr.jayasoft.ivy.report.DownloadReport;
28 import fr.jayasoft.ivy.report.DownloadStatus;
29
30 /**
31  * Tests URLResolver. Http tests are based upon ibiblio site.
32  *
33  */

34 public class URLResolverTest extends TestCase {
35     // remote.test
36
private File JavaDoc _cache;
37     private ResolveData _data;
38     private Ivy _ivy = new Ivy();
39     
40     protected void setUp() throws Exception JavaDoc {
41         _cache = new File JavaDoc("build/cache");
42         _data = new ResolveData(_ivy, _cache, null, null, true);
43         _cache.mkdirs();
44     }
45     
46     protected void tearDown() throws Exception JavaDoc {
47         Delete del = new Delete();
48         del.setProject(new Project());
49         del.setDir(_cache);
50         del.execute();
51     }
52     
53     public void testFile() throws Exception JavaDoc {
54         URLResolver resolver = new URLResolver();
55         resolver.setIvy(_ivy);
56         String JavaDoc rootpath = new File JavaDoc("test/repositories/1").getAbsolutePath();
57         resolver.addIvyPattern("file:"+rootpath + "/[organisation]/[module]/ivys/ivy-[revision].xml");
58         resolver.addArtifactPattern("file:"+rootpath + "/[organisation]/[module]/[type]s/[artifact]-[revision].[type]");
59         resolver.setName("test");
60         assertEquals("test", resolver.getName());
61         
62         ModuleRevisionId mrid = ModuleRevisionId.newInstance("org1", "mod1.1", "1.0");
63         ResolvedModuleRevision rmr = resolver.getDependency(new DefaultDependencyDescriptor(mrid, false), _data);
64         assertNotNull(rmr);
65         
66         assertEquals(mrid, rmr.getId());
67         Date JavaDoc pubdate = new GregorianCalendar JavaDoc(2004, 10, 1, 11, 0, 0).getTime();
68         assertEquals(pubdate, rmr.getPublicationDate());
69         
70         
71         // test to ask to download
72
DefaultArtifact artifact = new DefaultArtifact(mrid, pubdate, "mod1.1", "jar", "jar");
73         DownloadReport report = resolver.download(new Artifact[] {artifact}, _data.getIvy(), _cache);
74         assertNotNull(report);
75         
76         assertEquals(1, report.getArtifactsReports().length);
77         
78         ArtifactDownloadReport ar = report.getArtifactReport(artifact);
79         assertNotNull(ar);
80         
81         assertEquals(artifact, ar.getArtifact());
82         assertEquals(DownloadStatus.SUCCESSFUL, ar.getDownloadStatus());
83
84         // test to ask to download again, should use cache
85
report = resolver.download(new Artifact[] {artifact}, _data.getIvy(), _cache);
86         assertNotNull(report);
87         
88         assertEquals(1, report.getArtifactsReports().length);
89         
90         ar = report.getArtifactReport(artifact);
91         assertNotNull(ar);
92         
93         assertEquals(artifact, ar.getArtifact());
94         assertEquals(DownloadStatus.NO, ar.getDownloadStatus());
95     }
96
97     public void testLatestFile() throws Exception JavaDoc {
98         URLResolver resolver = new URLResolver();
99         resolver.setIvy(_ivy);
100         String JavaDoc rootpath = new File JavaDoc("test/repositories/1").getAbsolutePath().replaceAll("\\\\", "/");
101         resolver.addIvyPattern("file:"+rootpath + "/[organisation]/[module]/ivys/ivy-[revision].xml");
102         resolver.addArtifactPattern("file:"+rootpath + "/[organisation]/[module]/[type]s/[artifact]-[revision].[type]");
103         resolver.setName("test");
104         assertEquals("test", resolver.getName());
105         
106         ModuleRevisionId mrid = ModuleRevisionId.newInstance("org1", "mod1.1", "2.0");
107         ResolvedModuleRevision rmr = resolver.getDependency(new DefaultDependencyDescriptor(ModuleRevisionId.newInstance("org1", "mod1.1", "latest.integration"), false), _data);
108         assertNotNull(rmr);
109         
110         assertEquals(mrid, rmr.getId());
111         Date JavaDoc pubdate = new GregorianCalendar JavaDoc(2005, 1, 15, 11, 0, 0).getTime();
112         assertEquals(pubdate, rmr.getPublicationDate());
113     }
114
115     public void testIBiblio() throws Exception JavaDoc {
116         String JavaDoc ibiblioRoot = IBiblioHelper.getIBiblioMirror();
117         if (ibiblioRoot == null) {
118             return;
119         }
120         
121         URLResolver resolver = new URLResolver();
122         resolver.setIvy(_ivy);
123         resolver.addArtifactPattern(ibiblioRoot+"/[module]/[type]s/[artifact]-[revision].[type]");
124         resolver.setName("test");
125         assertEquals("test", resolver.getName());
126         
127         ModuleRevisionId mrid = ModuleRevisionId.newInstance("apache", "commons-fileupload", "1.0");
128         ResolvedModuleRevision rmr = resolver.getDependency(new DefaultDependencyDescriptor(mrid, false), _data);
129         assertNotNull(rmr);
130         assertEquals(mrid, rmr.getId());
131
132         DefaultArtifact artifact = new DefaultArtifact(mrid, rmr.getPublicationDate(), "commons-fileupload", "jar", "jar");
133         DownloadReport report = resolver.download(new Artifact[] {artifact}, _data.getIvy(), _cache);
134         assertNotNull(report);
135         
136         assertEquals(1, report.getArtifactsReports().length);
137         
138         ArtifactDownloadReport ar = report.getArtifactReport(artifact);
139         assertNotNull(ar);
140         
141         assertEquals(artifact, ar.getArtifact());
142         assertEquals(DownloadStatus.SUCCESSFUL, ar.getDownloadStatus());
143
144         // test to ask to download again, should use cache
145
report = resolver.download(new Artifact[] {artifact}, _data.getIvy(), _cache);
146         assertNotNull(report);
147         
148         assertEquals(1, report.getArtifactsReports().length);
149         
150         ar = report.getArtifactReport(artifact);
151         assertNotNull(ar);
152         
153         assertEquals(artifact, ar.getArtifact());
154         assertEquals(DownloadStatus.NO, ar.getDownloadStatus());
155     }
156
157     public void testIBiblioArtifacts() throws Exception JavaDoc {
158         String JavaDoc ibiblioRoot = IBiblioHelper.getIBiblioMirror();
159         if (ibiblioRoot == null) {
160             return;
161         }
162         
163         URLResolver resolver = new URLResolver();
164         resolver.setIvy(_ivy);
165         resolver.addArtifactPattern(ibiblioRoot+"/[module]/[type]s/[artifact]-[revision].[type]");
166         resolver.setName("test");
167         assertEquals("test", resolver.getName());
168         
169         ModuleRevisionId mrid = ModuleRevisionId.newInstance("apache", "nanning", "0.9");
170         DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(mrid, false);
171         dd.addDependencyArtifactIncludes("default", new DefaultDependencyArtifactDescriptor(dd, "nanning-profiler", "jar", "jar", true, ExactPatternMatcher.getInstance()));
172         dd.addDependencyArtifactIncludes("default", new DefaultDependencyArtifactDescriptor(dd, "nanning-trace", "jar", "jar", true, ExactPatternMatcher.getInstance()));
173         ResolvedModuleRevision rmr = resolver.getDependency(dd, _data);
174         assertNotNull(rmr);
175         assertEquals(mrid, rmr.getId());
176
177         DefaultArtifact profiler = new DefaultArtifact(mrid, rmr.getPublicationDate(), "nanning-profiler", "jar", "jar");
178         DefaultArtifact trace = new DefaultArtifact(mrid, rmr.getPublicationDate(), "nanning-trace", "jar", "jar");
179         DownloadReport report = resolver.download(new Artifact[] {profiler, trace}, _data.getIvy(), _cache);
180         assertNotNull(report);
181         
182         assertEquals(2, report.getArtifactsReports().length);
183         
184         ArtifactDownloadReport ar = report.getArtifactReport(profiler);
185         assertNotNull(ar);
186         
187         assertEquals(profiler, ar.getArtifact());
188         assertEquals(DownloadStatus.SUCCESSFUL, ar.getDownloadStatus());
189
190         ar = report.getArtifactReport(trace);
191         assertNotNull(ar);
192         
193         assertEquals(trace, 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[] {profiler, trace}, _data.getIvy(), _cache);
198         assertNotNull(report);
199         
200         assertEquals(2, report.getArtifactsReports().length);
201         
202         ar = report.getArtifactReport(profiler);
203         assertNotNull(ar);
204         
205         assertEquals(profiler, ar.getArtifact());
206         assertEquals(DownloadStatus.NO, ar.getDownloadStatus());
207
208         ar = report.getArtifactReport(trace);
209         assertNotNull(ar);
210         
211         assertEquals(trace, ar.getArtifact());
212         assertEquals(DownloadStatus.NO, ar.getDownloadStatus());
213     }
214
215     public void testLatestIBiblio() throws Exception JavaDoc {
216         String JavaDoc ibiblioRoot = IBiblioHelper.getIBiblioMirror();
217         if (ibiblioRoot == null) {
218             return;
219         }
220         
221         URLResolver resolver = new URLResolver();
222         resolver.setIvy(_ivy);
223         resolver.addArtifactPattern(ibiblioRoot+"/[module]/[type]s/[artifact]-[revision].[type]");
224         resolver.setName("test");
225         assertEquals("test", resolver.getName());
226         
227         ModuleRevisionId mrid = ModuleRevisionId.newInstance("objectweb", "asm", "1.4+");
228         ResolvedModuleRevision rmr = resolver.getDependency(new DefaultDependencyDescriptor(mrid, false), _data);
229         assertNotNull(rmr);
230         assertEquals("1.4.3", rmr.getId().getRevision());
231     }
232
233     public void testUnknown() throws Exception JavaDoc {
234         String JavaDoc ibiblioRoot = IBiblioHelper.getIBiblioMirror();
235         if (ibiblioRoot == null) {
236             return;
237         }
238         
239         URLResolver resolver = new URLResolver();
240         resolver.setIvy(_ivy);
241         resolver.addIvyPattern(ibiblioRoot+"/[module]/ivys/ivy-[revision].xml");
242         resolver.addArtifactPattern(ibiblioRoot+"/maven/[module]/[type]s/[artifact]-[revision].[type]");
243         resolver.setName("test");
244
245         assertNull(resolver.getDependency(new DefaultDependencyDescriptor(ModuleRevisionId.newInstance("unknown", "unknown", "1.0"), false), _data));
246     }
247
248     public void testDownloadWithUseOriginIsTrue() throws Exception JavaDoc {
249         URLResolver resolver = new URLResolver();
250         resolver.setIvy(_ivy);
251         String JavaDoc rootpath = new File JavaDoc("test/repositories/1").getAbsolutePath();
252         resolver.addIvyPattern("file:"+rootpath + "/[organisation]/[module]/ivys/ivy-[revision].xml");
253         resolver.addArtifactPattern("file:"+rootpath + "/[organisation]/[module]/[type]s/[artifact]-[revision].[type]");
254         resolver.setName("test");
255         assertEquals("test", resolver.getName());
256         
257         ModuleRevisionId mrid = ModuleRevisionId.newInstance("org1", "mod1.1", "1.0");
258         ResolvedModuleRevision rmr = resolver.getDependency(new DefaultDependencyDescriptor(mrid, false), _data);
259         assertNotNull(rmr);
260         
261         assertEquals(mrid, rmr.getId());
262         Date JavaDoc pubdate = new GregorianCalendar JavaDoc(2004, 10, 1, 11, 0, 0).getTime();
263         assertEquals(pubdate, rmr.getPublicationDate());
264         
265         
266         // test to ask to download
267
DefaultArtifact artifact = new DefaultArtifact(mrid, pubdate, "mod1.1", "jar", "jar");
268         DownloadReport report = resolver.download(new Artifact[] {artifact}, _data.getIvy(), _cache, true);
269         assertNotNull(report);
270         
271         assertEquals(1, report.getArtifactsReports().length);
272         
273         ArtifactDownloadReport ar = report.getArtifactReport(artifact);
274         assertNotNull(ar);
275         
276         assertEquals(artifact, ar.getArtifact());
277         assertEquals(DownloadStatus.SUCCESSFUL, ar.getDownloadStatus());
278
279         // test to ask to download again, should use cache
280
// report = resolver.download(new Artifact[] {artifact}, _data.getIvy(), _cache);
281
// assertNotNull(report);
282
//
283
// assertEquals(1, report.getArtifactsReports().length);
284
//
285
// ar = report.getArtifactReport(artifact);
286
// assertNotNull(ar);
287
//
288
// assertEquals(artifact, ar.getArtifact());
289
// assertEquals(DownloadStatus.NO, ar.getDownloadStatus());
290
//
291
}
292 }
293
Popular Tags