KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > repository > vfs > VfsRepositoryTest


1 package fr.jayasoft.ivy.repository.vfs;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.util.Iterator JavaDoc;
6
7 import junit.framework.TestCase;
8 import fr.jayasoft.ivy.util.FileUtil;
9
10 /**
11  * Testing
12  * Testing was the single biggest hurdle I faced. I have tried to provide a
13  * complete test suite that covers all protocols and which can be easily extended.
14  * It does differ - somewhat - in structure from the resolver/repository test
15  * suites.
16
17  * Setting up smb, ftp, sftp will undoubtedly be your biggest headache (it was
18  * mine). Here are a few notes about the setup:
19  * - the VFS test suite uses the build/test/repositories area
20  * - when setting samba, sftp, etc. the corresponding user needs both read and
21  * write privileges.
22  * - the tests assume that the user and password is the same for all services.
23  * - a limited amount of configuration is available by setting the following
24  * properties in the ivy.properties file:
25  * * vfs.host
26  * * vfs.username
27  * * vfs.password
28  * * vfs.samba_share
29  *
30  * Running the test requires that commons-io and ant.jar are on the classpath.
31  *
32  * Also, I would recommend that at some time the tests be converted from straight
33  * junit to something which betters supports functional testing.
34  * Although somewhat crude I am using jsystem (http://jsystemtest.sourceforge.net/)
35  * in other projects and am finding it a much better solution than straight junit.
36  *
37  * Stephen Nesbitt
38  */

39 public class VfsRepositoryTest extends TestCase {
40     private VfsRepository repo = null;
41     private VfsTestHelper helper = null;
42     private File JavaDoc scratchDir = null;
43
44     public VfsRepositoryTest(String JavaDoc arg0) throws Exception JavaDoc {
45         super(arg0);
46     }
47
48     protected void setUp() throws Exception JavaDoc {
49         super.setUp();
50         helper = new VfsTestHelper();
51         repo = new VfsRepository();
52         scratchDir = new File JavaDoc(FileUtil.concat(VfsTestHelper.TEST_REPO_DIR,
53                                                    VfsTestHelper.SCRATCH_DIR));
54         scratchDir.mkdir();
55     }
56
57     protected void tearDown() throws Exception JavaDoc {
58         super.tearDown();
59         repo = null;
60         if (scratchDir.exists()) {
61             FileUtil.forceDelete(scratchDir);
62         }
63     }
64     
65     /**
66      * Basic validation of happy path put - valid VFS URI and no conflict with existing file
67      *
68      * @throws Exception
69      */

70     public void testPutValid() throws Exception JavaDoc {
71         String JavaDoc testResource = VfsTestHelper.TEST_IVY_XML;
72         String JavaDoc srcFile = FileUtil.concat(VfsTestHelper.TEST_REPO_DIR, testResource);
73         String JavaDoc destResource = VfsTestHelper.SCRATCH_DIR + "/" + testResource;
74         String JavaDoc destFile = FileUtil.concat(VfsTestHelper.TEST_REPO_DIR, destResource);
75         
76         Iterator JavaDoc 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 JavaDoc(srcFile), vfsURI.toString(), false);
85                 if (! new File JavaDoc(srcFile).exists()) {
86                     fail("Put didn't happen. Src VfsURI: " + vfsURI.toString() +
87                             ".\nExpected file: " + destFile);
88                 }
89             } catch (IOException JavaDoc e) {
90                 fail("Caught unexpected IOException on Vfs URI: " + vfsURI.toString() + "\n" + e.getLocalizedMessage());
91             }
92         }
93     }
94
95     /**
96      * Validate that we can overwrite an existing file
97      *
98      * @throws Exception
99      */

100     public void testPutOverwriteTrue() throws Exception JavaDoc {
101         String JavaDoc testResource = VfsTestHelper.TEST_IVY_XML;
102         String JavaDoc srcFile = FileUtil.concat(VfsTestHelper.TEST_REPO_DIR, testResource);
103         String JavaDoc destResource = VfsTestHelper.SCRATCH_DIR + "/" + testResource;
104         File JavaDoc destFile = new File JavaDoc(
105                 FileUtil.concat(VfsTestHelper.TEST_REPO_DIR, destResource));
106         
107         Iterator JavaDoc vfsURIs = helper.createVFSUriSet(destResource).iterator();
108         while (vfsURIs.hasNext()) {
109             VfsURI vfsURI = (VfsURI) vfsURIs.next();
110             
111             // remove existing scratch dir and populate it with an empty file
112
// that we can overwrite. We do this so that we can test file sizes.
113
// seeded file has length 0, while put file will have a length > 0
114
if (scratchDir.exists()) {
115                 FileUtil.forceDelete(scratchDir);
116             }
117             destFile.getParentFile().mkdirs();
118             destFile.createNewFile();
119
120             try {
121                 repo.put(new File JavaDoc(srcFile), vfsURI.toString(), true);
122                 if (! new File JavaDoc(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 JavaDoc e) {
130                 fail("Caught unexpected IOException on Vfs URI: " + vfsURI.toString() + "\n" + e.getLocalizedMessage());
131             }
132         }
133     }
134
135     /**
136      * Validate that we put will respect a request not to overwrite an existing file
137      *
138      * @throws Exception
139      */

140     public void testPutOverwriteFalse() throws Exception JavaDoc {
141         String JavaDoc testResource = VfsTestHelper.TEST_IVY_XML;
142         String JavaDoc srcFile = FileUtil.concat(VfsTestHelper.TEST_REPO_DIR, testResource);
143         String JavaDoc destResource = VfsTestHelper.SCRATCH_DIR + "/" + testResource;
144         File JavaDoc destFile = new File JavaDoc(
145                 FileUtil.concat(VfsTestHelper.TEST_REPO_DIR, destResource));
146         destFile.getParentFile().mkdirs();
147         destFile.createNewFile();
148         
149         Iterator JavaDoc vfsURIs = helper.createVFSUriSet(destResource).iterator();
150         while (vfsURIs.hasNext()) {
151             VfsURI vfsURI = (VfsURI) vfsURIs.next();
152             
153             try {
154                 repo.put(new File JavaDoc(srcFile), vfsURI.toString(), false);
155                 fail("Did not throw expected IOException from attempted overwrite of existing file");
156             } catch (IOException JavaDoc e) {
157             }
158         }
159     }
160
161     /**
162      * Test the retrieval of an artifact from the repository creating a new artifact
163      *
164      * @throws Exception
165      */

166     public void testGetNoExisting() throws Exception JavaDoc {
167         String JavaDoc testResource = VfsTestHelper.TEST_IVY_XML;
168         String JavaDoc testFile = FileUtil.concat(scratchDir.getAbsolutePath(),
169                                                  testResource);
170         
171         Iterator JavaDoc 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 JavaDoc(testFile));
180                 if (! new File JavaDoc(testFile).exists()) {
181                     fail("Expected file: " + testFile + "not found. Failed vfsURI: " + vfsURI.toString());
182                 }
183             } catch (IOException JavaDoc e) {
184                 fail("Caught unexpected IOException on Vfs URI: " + vfsURI.toString() + "\n" + e.getLocalizedMessage());
185             }
186         }
187     }
188     
189     /**
190      * Test the retrieval of an artifact from the repository overwriting an existing artifact
191      *
192      * @throws Exception
193      */

194     public void testGetOverwriteExisting() throws Exception JavaDoc {
195         String JavaDoc testResource = VfsTestHelper.TEST_IVY_XML;
196         File JavaDoc testFile = new File JavaDoc( FileUtil.concat(scratchDir.getAbsolutePath(),
197                                                            testResource));
198         
199         Iterator JavaDoc vfsURIs = helper.createVFSUriSet(testResource).iterator();
200         while (vfsURIs.hasNext()) {
201             VfsURI vfsURI = (VfsURI) vfsURIs.next();
202             
203             // setup - remove existing scratch area and populate with a file to override
204
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 JavaDoc e) {
220                 fail("Caught unexpected IOException on Vfs URI: " + vfsURI.toString() + "\n" + e.getLocalizedMessage());
221             }
222         }
223     }
224     
225     /**
226      * Validate that we get a non null Resource instance when
227      * passed a well-formed VfsURI pointing to an existing file
228      *
229      */

230     public void testGetResourceValidExist() throws Exception JavaDoc {
231         String JavaDoc testResource = VfsTestHelper.TEST_IVY_XML;
232         
233         Iterator JavaDoc 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 JavaDoc e) {
240                 fail("Unexpected IOError on fetch of valid resource");
241                 e.printStackTrace();
242             }
243         }
244     }
245
246
247     
248     /**
249      * Validate that we get a non null Resource instance when
250      * passed a well-formed VfsURI pointing to a non-existent
251      * file.
252      *
253      */

254     public void testGetResourceValidNoExist() throws Exception JavaDoc {
255         String JavaDoc testResource = VfsTestHelper.SCRATCH_DIR + "/nosuchfile.jar";
256         
257         Iterator JavaDoc vfsURIs = helper.createVFSUriSet(testResource).iterator();
258         while (vfsURIs.hasNext()) {
259             VfsURI vfsURI = (VfsURI) vfsURIs.next();
260             
261             // make sure the declared resource does not exist
262
if (scratchDir.exists()) {
263                 FileUtil.forceDelete(scratchDir);
264             }
265             try {
266                 assertNotNull(repo.getResource(vfsURI.toString()));
267             } catch (IOException JavaDoc e) {
268                 // this should not happen
269
fail("Unexpected IOException");
270             }
271         }
272     }
273
274     
275 }
276
Popular Tags