KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > ant > IvyPublishTest


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.ant;
7
8 import java.io.BufferedReader JavaDoc;
9 import java.io.File JavaDoc;
10 import java.io.FileReader JavaDoc;
11 import java.io.InputStreamReader JavaDoc;
12
13 import junit.framework.TestCase;
14
15 import org.apache.tools.ant.BuildException;
16 import org.apache.tools.ant.Project;
17 import org.apache.tools.ant.taskdefs.Delete;
18 import org.apache.tools.ant.taskdefs.Echo;
19
20 import fr.jayasoft.ivy.Ivy;
21 import fr.jayasoft.ivy.ModuleDescriptor;
22 import fr.jayasoft.ivy.util.FileUtil;
23 import fr.jayasoft.ivy.xml.XmlModuleDescriptorParser;
24
25 public class IvyPublishTest extends TestCase {
26     private File JavaDoc _cache;
27     private IvyPublish _publish;
28     private Project _project;
29     
30     protected void setUp() throws Exception JavaDoc {
31         cleanTestDir();
32         cleanRep();
33         createCache();
34         _project = new Project();
35         _project.setProperty("ivy.conf.file", "test/repositories/ivyconf.xml");
36         _project.setProperty("build", "build/test/publish");
37
38         _publish = new IvyPublish();
39         _publish.setProject(_project);
40         _publish.setCache(_cache);
41     }
42
43     private void createCache() {
44         _cache = new File JavaDoc("build/cache");
45         _cache.mkdirs();
46     }
47     
48     protected void tearDown() throws Exception JavaDoc {
49         cleanCache();
50         cleanTestDir();
51         cleanRep();
52     }
53
54     private void cleanCache() {
55         Delete del = new Delete();
56         del.setProject(new Project());
57         del.setDir(_cache);
58         del.execute();
59     }
60
61     private void cleanTestDir() {
62         Delete del = new Delete();
63         del.setProject(new Project());
64         del.setDir(new File JavaDoc("build/test/publish"));
65         del.execute();
66     }
67
68     private void cleanRep() {
69         Delete del = new Delete();
70         del.setProject(new Project());
71         del.setDir(new File JavaDoc("test/repositories/1/jayasoft"));
72         del.execute();
73     }
74
75     public void testSimple() throws Exception JavaDoc {
76         _project.setProperty("ivy.dep.file", "test/java/fr/jayasoft/ivy/ant/ivy-simple.xml");
77         IvyResolve res = new IvyResolve();
78         res.setProject(_project);
79         res.execute();
80         
81         _publish.setPubrevision("1.2");
82         _publish.setResolver("1");
83         File JavaDoc art = new File JavaDoc("build/test/publish/resolve-simple-1.2.jar");
84         FileUtil.copy(new File JavaDoc("test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"), art, null);
85         _publish.execute();
86         
87         // should have do the ivy delivering
88
assertTrue(new File JavaDoc("build/test/publish/ivy-1.2.xml").exists());
89         
90         // should have published the files with "1" resolver
91
assertTrue(new File JavaDoc("test/repositories/1/jayasoft/resolve-simple/ivys/ivy-1.2.xml").exists());
92         assertTrue(new File JavaDoc("test/repositories/1/jayasoft/resolve-simple/jars/resolve-simple-1.2.jar").exists());
93         
94         // should have updated published ivy version
95
ModuleDescriptor md = XmlModuleDescriptorParser.getInstance().parseDescriptor(new Ivy(), new File JavaDoc("test/repositories/1/jayasoft/resolve-simple/ivys/ivy-1.2.xml").toURL(), false);
96         assertEquals("1.2", md.getModuleRevisionId().getRevision());
97     }
98
99     public void testMultiPatterns() throws Exception JavaDoc {
100         _project.setProperty("ivy.dep.file", "test/java/fr/jayasoft/ivy/ant/ivy-publish-multi.xml");
101         IvyResolve res = new IvyResolve();
102         res.setProject(_project);
103         res.execute();
104         
105         _publish.setPubrevision("1.2");
106         _publish.setResolver("1");
107         File JavaDoc art = new File JavaDoc("build/test/publish/1/multi1-1.2.jar");
108         FileUtil.copy(new File JavaDoc("test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"), art, null);
109         art = new File JavaDoc("build/test/publish/2/multi2-1.2.jar");
110         FileUtil.copy(new File JavaDoc("test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"), art, null);
111         _publish.addArtifactspattern("build/test/publish/1/[artifact]-[revision].[ext]");
112         _publish.addArtifactspattern("build/test/publish/2/[artifact]-[revision].[ext]");
113         _publish.execute();
114         
115         // should have do the ivy delivering
116
assertTrue(new File JavaDoc("build/test/publish/1/ivy-1.2.xml").exists());
117         
118         // should have published the files with "1" resolver
119
assertTrue(new File JavaDoc("test/repositories/1/jayasoft/multi/ivys/ivy-1.2.xml").exists());
120         assertTrue(new File JavaDoc("test/repositories/1/jayasoft/multi/jars/multi1-1.2.jar").exists());
121         assertTrue(new File JavaDoc("test/repositories/1/jayasoft/multi/jars/multi2-1.2.jar").exists());
122     }
123
124     public void testCustom() throws Exception JavaDoc {
125         _project.setProperty("ivy.dep.file", "test/java/fr/jayasoft/ivy/ant/ivy-custom.xml");
126         IvyResolve res = new IvyResolve();
127         res.setValidate(false);
128         res.setProject(_project);
129         res.execute();
130         
131         _publish.setPubrevision("1.2");
132         _publish.setPubdate("20060906141243");
133         _publish.setResolver("1");
134         _publish.setValidate(false);
135         File JavaDoc art = new File JavaDoc("build/test/publish/resolve-custom-1.2.jar");
136         FileUtil.copy(new File JavaDoc("test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"), art, null);
137         _publish.execute();
138         
139         // should have do the ivy delivering
140
assertTrue(new File JavaDoc("build/test/publish/ivy-1.2.xml").exists());
141         
142         File JavaDoc dest = new File JavaDoc("test/repositories/1/jayasoft/resolve-custom/ivys/ivy-1.2.xml");
143         // should have published the files with "1" resolver
144
assertTrue(dest.exists());
145         assertTrue(new File JavaDoc("test/repositories/1/jayasoft/resolve-custom/jars/resolve-custom-1.2.jar").exists());
146         
147         // should have updated published ivy version
148
ModuleDescriptor md = XmlModuleDescriptorParser.getInstance().parseDescriptor(new Ivy(), dest.toURL(), false);
149         assertEquals("1.2", md.getModuleRevisionId().getRevision());
150         
151         // should have kept custom attributes
152
assertEquals("cval1", md.getModuleRevisionId().getAttribute("custom-info"));
153         assertEquals("cval2", md.getConfiguration("default").getAttribute("custom-conf"));
154         assertEquals("cval3", md.getDependencies()[0].getAttribute("custom-dep"));
155         
156         // should respect the ivy file, with descriptions, ...
157
String JavaDoc expected = FileUtil.readEntirely(new BufferedReader JavaDoc(new InputStreamReader JavaDoc(IvyPublishTest.class.getResourceAsStream("published-ivy-custom.xml"))));
158         String JavaDoc updated = FileUtil.readEntirely(new BufferedReader JavaDoc(new FileReader JavaDoc(dest)));
159         assertEquals(expected, updated);
160         
161     }
162
163     public void testNoDeliver() throws Exception JavaDoc {
164         _project.setProperty("ivy.dep.file", "test/java/fr/jayasoft/ivy/ant/ivy-latest.xml");
165         IvyResolve res = new IvyResolve();
166         res.setProject(_project);
167         res.execute();
168         
169         
170         _publish.setPubrevision("1.3");
171         _publish.setResolver("1");
172         _publish.setSrcivypattern("build/test/publish/ivy-1.3.xml");
173
174         FileUtil.copy(new File JavaDoc("test/java/fr/jayasoft/ivy/ant/ivy-publish.xml"), new File JavaDoc("build/test/publish/ivy-1.3.xml"), null);
175
176         File JavaDoc art = new File JavaDoc("build/test/publish/resolve-latest-1.3.jar");
177         FileUtil.copy(new File JavaDoc("test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"), art, null);
178         _publish.execute();
179         
180         // should have published the files with "1" resolver
181
assertTrue(new File JavaDoc("test/repositories/1/jayasoft/resolve-latest/ivys/ivy-1.3.xml").exists());
182         assertTrue(new File JavaDoc("test/repositories/1/jayasoft/resolve-latest/jars/resolve-latest-1.3.jar").exists());
183         
184         // the published ivy version should be ok (ok in ivy-publish file)
185
ModuleDescriptor md = XmlModuleDescriptorParser.getInstance().parseDescriptor(new Ivy(), new File JavaDoc("test/repositories/1/jayasoft/resolve-latest/ivys/ivy-1.3.xml").toURL(), false);
186         assertEquals("1.3", md.getModuleRevisionId().getRevision());
187         
188         // should not have done delivery (replace dynamic revisions with static ones)
189
assertEquals("latest.integration", md.getDependencies()[0].getDependencyRevisionId().getRevision());
190     }
191
192     public void testForceDeliver() throws Exception JavaDoc {
193         _project.setProperty("ivy.dep.file", "test/java/fr/jayasoft/ivy/ant/ivy-latest.xml");
194         IvyResolve res = new IvyResolve();
195         res.setProject(_project);
196         res.execute();
197         
198         _publish.setPubrevision("1.3");
199         _publish.setResolver("1");
200         _publish.setSrcivypattern("build/test/publish/ivy-1.3.xml");
201         _publish.setForcedeliver(true);
202
203         FileUtil.copy(new File JavaDoc("test/java/fr/jayasoft/ivy/ant/ivy-latest.xml"), new File JavaDoc("build/test/publish/ivy-1.3.xml"), null);
204
205         File JavaDoc art = new File JavaDoc("build/test/publish/resolve-latest-1.3.jar");
206         FileUtil.copy(new File JavaDoc("test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"), art, null);
207         _publish.execute();
208         
209         // should have published the files with "1" resolver
210
assertTrue(new File JavaDoc("test/repositories/1/jayasoft/resolve-latest/ivys/ivy-1.3.xml").exists());
211         assertTrue(new File JavaDoc("test/repositories/1/jayasoft/resolve-latest/jars/resolve-latest-1.3.jar").exists());
212         
213         // should have updated published ivy version
214
ModuleDescriptor md = XmlModuleDescriptorParser.getInstance().parseDescriptor(new Ivy(), new File JavaDoc("test/repositories/1/jayasoft/resolve-latest/ivys/ivy-1.3.xml").toURL(), false);
215         assertEquals("1.3", md.getModuleRevisionId().getRevision());
216     }
217
218     public void testBadNoDeliver() throws Exception JavaDoc {
219         _project.setProperty("ivy.dep.file", "test/java/fr/jayasoft/ivy/ant/ivy-latest.xml");
220         IvyResolve res = new IvyResolve();
221         res.setProject(_project);
222         res.execute();
223         
224         
225         _publish.setPubrevision("1.3");
226         _publish.setResolver("1");
227         _publish.setSrcivypattern("build/test/publish/ivy-1.3.xml");
228
229         FileUtil.copy(new File JavaDoc("test/java/fr/jayasoft/ivy/ant/ivy-latest.xml"), new File JavaDoc("build/test/publish/ivy-1.3.xml"), null);
230
231         File JavaDoc art = new File JavaDoc("build/test/publish/resolve-latest-1.3.jar");
232         FileUtil.copy(new File JavaDoc("test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"), art, null);
233         try {
234             _publish.execute();
235             fail("shouldn't publish ivy file with bad revision");
236         } catch (BuildException ex) {
237             
238         }
239     }
240
241     public void testReadonly() throws Exception JavaDoc {
242         _project.setProperty("ivy.dep.file", "test/java/fr/jayasoft/ivy/ant/ivy-simple.xml");
243         IvyResolve res = new IvyResolve();
244         res.setProject(_project);
245         res.execute();
246         
247         _publish.setPubrevision("1.2");
248         _publish.setResolver("1");
249         File JavaDoc art = new File JavaDoc("build/test/publish/resolve-simple-1.2.jar");
250         FileUtil.copy(new File JavaDoc("test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"), art, null);
251         
252         Echo echo = new Echo();
253         echo.setProject(_project);
254         echo.setMessage("new version");
255         echo.setFile(art);
256         echo.execute();
257
258         File JavaDoc dest = new File JavaDoc("test/repositories/1/jayasoft/resolve-simple/jars/resolve-simple-1.2.jar");
259         FileUtil.copy(new File JavaDoc("test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"),
260                 dest, null);
261
262         echo = new Echo();
263         echo.setProject(_project);
264         echo.setMessage("old version");
265         echo.setFile(dest);
266         echo.execute();
267
268         dest.setReadOnly();
269         
270         try {
271             _publish.execute();
272             fail("by default, publish should fail when a readonly artifact already exist");
273         } catch (Exception JavaDoc ex) {
274             assertTrue(dest.exists());
275             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(dest));
276             assertEquals("old version", reader.readLine());
277             reader.close();
278         }
279     }
280
281     public void testOverwrite() throws Exception JavaDoc {
282         _project.setProperty("ivy.dep.file", "test/java/fr/jayasoft/ivy/ant/ivy-simple.xml");
283         IvyResolve res = new IvyResolve();
284         res.setProject(_project);
285         res.execute();
286         
287         _publish.setPubrevision("1.2");
288         _publish.setResolver("1");
289         File JavaDoc art = new File JavaDoc("build/test/publish/resolve-simple-1.2.jar");
290         FileUtil.copy(new File JavaDoc("test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"), art, null);
291         
292         Echo echo = new Echo();
293         echo.setProject(_project);
294         echo.setMessage("new version");
295         echo.setFile(art);
296         echo.execute();
297
298         File JavaDoc dest = new File JavaDoc("test/repositories/1/jayasoft/resolve-simple/jars/resolve-simple-1.2.jar");
299         FileUtil.copy(new File JavaDoc("test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"),
300                 dest, null);
301
302         echo = new Echo();
303         echo.setProject(_project);
304         echo.setMessage("old version");
305         echo.setFile(dest);
306         echo.execute();
307
308
309         _publish.setOverwrite(true);
310         _publish.execute();
311         assertTrue(dest.exists());
312         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(dest));
313         assertEquals("new version", reader.readLine());
314         reader.close();
315     }
316
317     public void testOverwriteReadOnly() throws Exception JavaDoc {
318         _project.setProperty("ivy.dep.file", "test/java/fr/jayasoft/ivy/ant/ivy-simple.xml");
319         IvyResolve res = new IvyResolve();
320         res.setProject(_project);
321         res.execute();
322         
323         _publish.setPubrevision("1.2");
324         _publish.setResolver("1");
325         File JavaDoc art = new File JavaDoc("build/test/publish/resolve-simple-1.2.jar");
326         FileUtil.copy(new File JavaDoc("test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"), art, null);
327         
328         Echo echo = new Echo();
329         echo.setProject(_project);
330         echo.setMessage("new version");
331         echo.setFile(art);
332         echo.execute();
333
334         File JavaDoc dest = new File JavaDoc("test/repositories/1/jayasoft/resolve-simple/jars/resolve-simple-1.2.jar");
335         FileUtil.copy(new File JavaDoc("test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"),
336                 dest, null);
337
338         echo = new Echo();
339         echo.setProject(_project);
340         echo.setMessage("old version");
341         echo.setFile(dest);
342         echo.execute();
343
344         dest.setReadOnly();
345         
346         _publish.setOverwrite(true);
347         _publish.execute();
348         assertTrue(dest.exists());
349         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(dest));
350         assertEquals("new version", reader.readLine());
351         reader.close();
352     }
353
354 }
355
Popular Tags