1 6 package fr.jayasoft.ivy.ant; 7 8 import java.io.BufferedReader ; 9 import java.io.File ; 10 import java.io.FileReader ; 11 import java.io.InputStreamReader ; 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 _cache; 27 private IvyPublish _publish; 28 private Project _project; 29 30 protected void setUp() throws Exception { 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 ("build/cache"); 45 _cache.mkdirs(); 46 } 47 48 protected void tearDown() throws Exception { 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 ("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 ("test/repositories/1/jayasoft")); 72 del.execute(); 73 } 74 75 public void testSimple() throws Exception { 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 art = new File ("build/test/publish/resolve-simple-1.2.jar"); 84 FileUtil.copy(new File ("test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"), art, null); 85 _publish.execute(); 86 87 assertTrue(new File ("build/test/publish/ivy-1.2.xml").exists()); 89 90 assertTrue(new File ("test/repositories/1/jayasoft/resolve-simple/ivys/ivy-1.2.xml").exists()); 92 assertTrue(new File ("test/repositories/1/jayasoft/resolve-simple/jars/resolve-simple-1.2.jar").exists()); 93 94 ModuleDescriptor md = XmlModuleDescriptorParser.getInstance().parseDescriptor(new Ivy(), new File ("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 { 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 art = new File ("build/test/publish/1/multi1-1.2.jar"); 108 FileUtil.copy(new File ("test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"), art, null); 109 art = new File ("build/test/publish/2/multi2-1.2.jar"); 110 FileUtil.copy(new File ("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 assertTrue(new File ("build/test/publish/1/ivy-1.2.xml").exists()); 117 118 assertTrue(new File ("test/repositories/1/jayasoft/multi/ivys/ivy-1.2.xml").exists()); 120 assertTrue(new File ("test/repositories/1/jayasoft/multi/jars/multi1-1.2.jar").exists()); 121 assertTrue(new File ("test/repositories/1/jayasoft/multi/jars/multi2-1.2.jar").exists()); 122 } 123 124 public void testCustom() throws Exception { 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 art = new File ("build/test/publish/resolve-custom-1.2.jar"); 136 FileUtil.copy(new File ("test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"), art, null); 137 _publish.execute(); 138 139 assertTrue(new File ("build/test/publish/ivy-1.2.xml").exists()); 141 142 File dest = new File ("test/repositories/1/jayasoft/resolve-custom/ivys/ivy-1.2.xml"); 143 assertTrue(dest.exists()); 145 assertTrue(new File ("test/repositories/1/jayasoft/resolve-custom/jars/resolve-custom-1.2.jar").exists()); 146 147 ModuleDescriptor md = XmlModuleDescriptorParser.getInstance().parseDescriptor(new Ivy(), dest.toURL(), false); 149 assertEquals("1.2", md.getModuleRevisionId().getRevision()); 150 151 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 String expected = FileUtil.readEntirely(new BufferedReader (new InputStreamReader (IvyPublishTest.class.getResourceAsStream("published-ivy-custom.xml")))); 158 String updated = FileUtil.readEntirely(new BufferedReader (new FileReader (dest))); 159 assertEquals(expected, updated); 160 161 } 162 163 public void testNoDeliver() throws Exception { 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 ("test/java/fr/jayasoft/ivy/ant/ivy-publish.xml"), new File ("build/test/publish/ivy-1.3.xml"), null); 175 176 File art = new File ("build/test/publish/resolve-latest-1.3.jar"); 177 FileUtil.copy(new File ("test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"), art, null); 178 _publish.execute(); 179 180 assertTrue(new File ("test/repositories/1/jayasoft/resolve-latest/ivys/ivy-1.3.xml").exists()); 182 assertTrue(new File ("test/repositories/1/jayasoft/resolve-latest/jars/resolve-latest-1.3.jar").exists()); 183 184 ModuleDescriptor md = XmlModuleDescriptorParser.getInstance().parseDescriptor(new Ivy(), new File ("test/repositories/1/jayasoft/resolve-latest/ivys/ivy-1.3.xml").toURL(), false); 186 assertEquals("1.3", md.getModuleRevisionId().getRevision()); 187 188 assertEquals("latest.integration", md.getDependencies()[0].getDependencyRevisionId().getRevision()); 190 } 191 192 public void testForceDeliver() throws Exception { 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 ("test/java/fr/jayasoft/ivy/ant/ivy-latest.xml"), new File ("build/test/publish/ivy-1.3.xml"), null); 204 205 File art = new File ("build/test/publish/resolve-latest-1.3.jar"); 206 FileUtil.copy(new File ("test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"), art, null); 207 _publish.execute(); 208 209 assertTrue(new File ("test/repositories/1/jayasoft/resolve-latest/ivys/ivy-1.3.xml").exists()); 211 assertTrue(new File ("test/repositories/1/jayasoft/resolve-latest/jars/resolve-latest-1.3.jar").exists()); 212 213 ModuleDescriptor md = XmlModuleDescriptorParser.getInstance().parseDescriptor(new Ivy(), new File ("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 { 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 ("test/java/fr/jayasoft/ivy/ant/ivy-latest.xml"), new File ("build/test/publish/ivy-1.3.xml"), null); 230 231 File art = new File ("build/test/publish/resolve-latest-1.3.jar"); 232 FileUtil.copy(new File ("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 { 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 art = new File ("build/test/publish/resolve-simple-1.2.jar"); 250 FileUtil.copy(new File ("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 dest = new File ("test/repositories/1/jayasoft/resolve-simple/jars/resolve-simple-1.2.jar"); 259 FileUtil.copy(new File ("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 ex) { 274 assertTrue(dest.exists()); 275 BufferedReader reader = new BufferedReader (new FileReader (dest)); 276 assertEquals("old version", reader.readLine()); 277 reader.close(); 278 } 279 } 280 281 public void testOverwrite() throws Exception { 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 art = new File ("build/test/publish/resolve-simple-1.2.jar"); 290 FileUtil.copy(new File ("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 dest = new File ("test/repositories/1/jayasoft/resolve-simple/jars/resolve-simple-1.2.jar"); 299 FileUtil.copy(new File ("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 reader = new BufferedReader (new FileReader (dest)); 313 assertEquals("new version", reader.readLine()); 314 reader.close(); 315 } 316 317 public void testOverwriteReadOnly() throws Exception { 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 art = new File ("build/test/publish/resolve-simple-1.2.jar"); 326 FileUtil.copy(new File ("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 dest = new File ("test/repositories/1/jayasoft/resolve-simple/jars/resolve-simple-1.2.jar"); 335 FileUtil.copy(new File ("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 reader = new BufferedReader (new FileReader (dest)); 350 assertEquals("new version", reader.readLine()); 351 reader.close(); 352 } 353 354 } 355 | Popular Tags |