1 package fr.jayasoft.ivy.repository.vfs; 2 3 import java.io.File ; 4 import java.io.IOException ; 5 import java.util.Iterator ; 6 7 import junit.framework.TestCase; 8 import fr.jayasoft.ivy.util.FileUtil; 9 10 39 public class VfsRepositoryTest extends TestCase { 40 private VfsRepository repo = null; 41 private VfsTestHelper helper = null; 42 private File scratchDir = null; 43 44 public VfsRepositoryTest(String arg0) throws Exception { 45 super(arg0); 46 } 47 48 protected void setUp() throws Exception { 49 super.setUp(); 50 helper = new VfsTestHelper(); 51 repo = new VfsRepository(); 52 scratchDir = new File (FileUtil.concat(VfsTestHelper.TEST_REPO_DIR, 53 VfsTestHelper.SCRATCH_DIR)); 54 scratchDir.mkdir(); 55 } 56 57 protected void tearDown() throws Exception { 58 super.tearDown(); 59 repo = null; 60 if (scratchDir.exists()) { 61 FileUtil.forceDelete(scratchDir); 62 } 63 } 64 65 70 public void testPutValid() throws Exception { 71 String testResource = VfsTestHelper.TEST_IVY_XML; 72 String srcFile = FileUtil.concat(VfsTestHelper.TEST_REPO_DIR, testResource); 73 String destResource = VfsTestHelper.SCRATCH_DIR + "/" + testResource; 74 String destFile = FileUtil.concat(VfsTestHelper.TEST_REPO_DIR, destResource); 75 76 Iterator vfsURIs = helper.createVFSUriSet(destResource).iterator(); 77 while (vfsURIs.hasNext()) { 78 VfsURI vfsURI = (VfsURI) vfsURIs.next(); 79 if (scratchDir.exists()) { 80 FileUtil.forceDelete(scratchDir); 81 } 82 83 try { 84 repo.put(new File (srcFile), vfsURI.toString(), false); 85 if (! new File (srcFile).exists()) { 86 fail("Put didn't happen. Src VfsURI: " + vfsURI.toString() + 87 ".\nExpected file: " + destFile); 88 } 89 } catch (IOException e) { 90 fail("Caught unexpected IOException on Vfs URI: " + vfsURI.toString() + "\n" + e.getLocalizedMessage()); 91 } 92 } 93 } 94 95 100 public void testPutOverwriteTrue() throws Exception { 101 String testResource = VfsTestHelper.TEST_IVY_XML; 102 String srcFile = FileUtil.concat(VfsTestHelper.TEST_REPO_DIR, testResource); 103 String destResource = VfsTestHelper.SCRATCH_DIR + "/" + testResource; 104 File destFile = new File ( 105 FileUtil.concat(VfsTestHelper.TEST_REPO_DIR, destResource)); 106 107 Iterator vfsURIs = helper.createVFSUriSet(destResource).iterator(); 108 while (vfsURIs.hasNext()) { 109 VfsURI vfsURI = (VfsURI) vfsURIs.next(); 110 111 if (scratchDir.exists()) { 115 FileUtil.forceDelete(scratchDir); 116 } 117 destFile.getParentFile().mkdirs(); 118 destFile.createNewFile(); 119 120 try { 121 repo.put(new File (srcFile), vfsURI.toString(), true); 122 if (! new File (srcFile).exists()) { 123 fail("Put didn't happen. Src VfsURI: " + vfsURI.toString() + 124 ".\nExpected file: " + destFile); 125 } 126 if (destFile.length() == 0) { 127 fail("Zero file size indicates file not overwritten"); 128 } 129 } catch (IOException e) { 130 fail("Caught unexpected IOException on Vfs URI: " + vfsURI.toString() + "\n" + e.getLocalizedMessage()); 131 } 132 } 133 } 134 135 140 public void testPutOverwriteFalse() throws Exception { 141 String testResource = VfsTestHelper.TEST_IVY_XML; 142 String srcFile = FileUtil.concat(VfsTestHelper.TEST_REPO_DIR, testResource); 143 String destResource = VfsTestHelper.SCRATCH_DIR + "/" + testResource; 144 File destFile = new File ( 145 FileUtil.concat(VfsTestHelper.TEST_REPO_DIR, destResource)); 146 destFile.getParentFile().mkdirs(); 147 destFile.createNewFile(); 148 149 Iterator vfsURIs = helper.createVFSUriSet(destResource).iterator(); 150 while (vfsURIs.hasNext()) { 151 VfsURI vfsURI = (VfsURI) vfsURIs.next(); 152 153 try { 154 repo.put(new File (srcFile), vfsURI.toString(), false); 155 fail("Did not throw expected IOException from attempted overwrite of existing file"); 156 } catch (IOException e) { 157 } 158 } 159 } 160 161 166 public void testGetNoExisting() throws Exception { 167 String testResource = VfsTestHelper.TEST_IVY_XML; 168 String testFile = FileUtil.concat(scratchDir.getAbsolutePath(), 169 testResource); 170 171 Iterator vfsURIs = helper.createVFSUriSet(testResource).iterator(); 172 while (vfsURIs.hasNext()) { 173 VfsURI vfsURI = (VfsURI) vfsURIs.next(); 174 if (scratchDir.exists()) { 175 FileUtil.forceDelete(scratchDir); 176 } 177 178 try { 179 repo.get(vfsURI.toString(), new File (testFile)); 180 if (! new File (testFile).exists()) { 181 fail("Expected file: " + testFile + "not found. Failed vfsURI: " + vfsURI.toString()); 182 } 183 } catch (IOException e) { 184 fail("Caught unexpected IOException on Vfs URI: " + vfsURI.toString() + "\n" + e.getLocalizedMessage()); 185 } 186 } 187 } 188 189 194 public void testGetOverwriteExisting() throws Exception { 195 String testResource = VfsTestHelper.TEST_IVY_XML; 196 File testFile = new File ( FileUtil.concat(scratchDir.getAbsolutePath(), 197 testResource)); 198 199 Iterator vfsURIs = helper.createVFSUriSet(testResource).iterator(); 200 while (vfsURIs.hasNext()) { 201 VfsURI vfsURI = (VfsURI) vfsURIs.next(); 202 203 if (scratchDir.exists()) { 205 FileUtil.forceDelete(scratchDir); 206 } 207 testFile.getParentFile().mkdirs(); 208 testFile.createNewFile(); 209 210 211 try { 212 repo.get(vfsURI.toString(), testFile); 213 if (! testFile.exists()) { 214 fail("Expected file: " + testFile + "not found. Failed vfsURI: " + vfsURI.toString()); 215 } 216 if (testFile.length() == 0) { 217 fail("Zero file size indicates file not overwritten"); 218 } 219 } catch (IOException e) { 220 fail("Caught unexpected IOException on Vfs URI: " + vfsURI.toString() + "\n" + e.getLocalizedMessage()); 221 } 222 } 223 } 224 225 230 public void testGetResourceValidExist() throws Exception { 231 String testResource = VfsTestHelper.TEST_IVY_XML; 232 233 Iterator vfsURIs = helper.createVFSUriSet(testResource).iterator(); 234 235 while (vfsURIs.hasNext()) { 236 VfsURI vfsURI = (VfsURI) vfsURIs.next(); 237 try { 238 assertNotNull(repo.getResource(vfsURI.toString())); 239 } catch (IOException e) { 240 fail("Unexpected IOError on fetch of valid resource"); 241 e.printStackTrace(); 242 } 243 } 244 } 245 246 247 248 254 public void testGetResourceValidNoExist() throws Exception { 255 String testResource = VfsTestHelper.SCRATCH_DIR + "/nosuchfile.jar"; 256 257 Iterator vfsURIs = helper.createVFSUriSet(testResource).iterator(); 258 while (vfsURIs.hasNext()) { 259 VfsURI vfsURI = (VfsURI) vfsURIs.next(); 260 261 if (scratchDir.exists()) { 263 FileUtil.forceDelete(scratchDir); 264 } 265 try { 266 assertNotNull(repo.getResource(vfsURI.toString())); 267 } catch (IOException e) { 268 fail("Unexpected IOException"); 270 } 271 } 272 } 273 274 275 } 276 | Popular Tags |