KickJava   Java API By Example, From Geeks To Geeks.

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


1 package fr.jayasoft.ivy.repository.vfs;
2
3 import java.net.URI JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Arrays JavaDoc;
6 import java.util.Collections JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9
10 import junit.framework.TestCase;
11
12
13 public class VfsResourceTest extends TestCase {
14     private VfsTestHelper helper = null;
15
16     public VfsResourceTest() {
17         super();
18     }
19
20     public VfsResourceTest(String JavaDoc arg0) {
21         super(arg0);
22     }
23     
24     public void setUp() throws Exception JavaDoc {
25         helper = new VfsTestHelper();
26     }
27     
28     public void tearDown() {
29         helper = null;
30     }
31     
32     /**
33      * Validate VFSResource creation for a valid VFS URI pointing to an physically existing file
34      */

35     public void testCreateResourceThatExists() throws Exception JavaDoc{
36         Iterator JavaDoc vfsURIs = helper.createVFSUriSet(VfsTestHelper.TEST_IVY_XML).iterator();
37         while (vfsURIs.hasNext()) {
38             VfsURI vfsURI = (VfsURI) vfsURIs.next();
39             String JavaDoc resId = vfsURI.toString();
40             VfsResource res = new VfsResource(resId, helper.fsManager);
41             if (res == null) {
42                 fail("Unexpected null value on VFS URI: " + resId);
43             }
44
45             if (! res.exists()) {
46                 fail("Resource does not exist and it should: " + resId);
47             }
48             
49             // VFS apparently does some weird normalization so that resource id used to create
50
// the VFS resource is not necessarily identical to the id returned from the getName
51
// method <sigh>. We try to work around this by transforming things into java URIs.
52
if (! new URI JavaDoc(resId).equals(new URI JavaDoc(res.getName()).normalize())) {
53                 fail("Failed on getName. Expected: " + resId + ". Actual: " + res.getName());
54             }
55             
56             if (res.getLastModified() == 0 ) {
57                 fail("Expected non-null file modification date for URI: " + resId);
58             }
59             
60             if (res.getContentLength() == 0 ) {
61                 fail("Expected non-zero file length for URI: " + resId);
62             }
63             
64             if (! res.physicallyExists()) {
65                 fail("Physical existence check returned false for existing resource: " + resId);
66             }
67         }
68     }
69     
70     /**
71      * Validating that resource can be created for files which don't physically exists -
72      * e.g. resources that are going to created.
73      *
74      */

75     public void testCreateResourceThatDoesntExist() throws Exception JavaDoc {
76         Iterator JavaDoc vfsURIs = helper.createVFSUriSet("zzyyxx.zzyyxx").iterator();
77         while (vfsURIs.hasNext()) {
78             VfsURI vfsURI = (VfsURI) vfsURIs.next();
79             String JavaDoc resId = vfsURI.toString();
80             VfsResource res = new VfsResource(resId, helper.fsManager);
81             if (res == null) {
82                 fail("Unexpected null value on VFS URI: " + resId);
83             }
84
85             if (res.exists()) {
86                 fail("Resource does not exist and it shouldn't: " + resId);
87             }
88             
89             // VFS apparently does some weird normalization so that resource id used to create
90
// the VFS resource is not necessarily identical to the id returned from the getName
91
// method <sigh>. We try to work around this by transforming things into java URIs.
92
if (! new URI JavaDoc(resId).equals(new URI JavaDoc(res.getName()).normalize())) {
93                 fail("Failed on getName. Expected: " + resId + ". Actual: " + res.getName());
94             }
95             
96             if (res.getLastModified() != 0 ) {
97                 fail("Expected null file modification date for URI: " + resId + ": " + res.getLastModified());
98             }
99             
100             if (res.getContentLength() != 0 ) {
101                 fail("Expected non-zero file length for URI: " + resId);
102             }
103             
104             if (res.physicallyExists()) {
105                 fail("Physical existence check returned true for non-existent resource: " + resId);
106             }
107         }
108     }
109     
110     /**
111      * Validate VFSResource creation when given a poorly formed VFS identifier
112      *
113      */

114     public void testBadURI() throws Exception JavaDoc {
115         String JavaDoc vfsURI = "smb1:/goobeldygook";
116         VfsResource res = new VfsResource(vfsURI, helper.fsManager);
117         
118         
119         if (res == null) {
120             fail("Unexpected null value on VFS URI: " + vfsURI);
121         }
122         
123         if (res.exists()) {
124             fail("Resource is marked as existing and it should not: " + vfsURI);
125         }
126         
127         if (! res.getName().equals("smb1:/goobeldygook")) {
128             fail("Failed on getName. Expected: " + vfsURI + ". Actual: " + res.getName());
129         }
130         
131         if (res.getLastModified() != 0 ) {
132             fail("Expected null file modification date for URI: " + vfsURI + ": " + res.getLastModified());
133         }
134         
135         if (res.getContentLength() != 0 ) {
136             fail("Expected zero file length for URI: " + vfsURI);
137         }
138
139         if (res.physicallyExists()) {
140             fail("Physical existence check returned false for existing resource: " + vfsURI);
141         }
142     }
143     
144     
145     
146     /**
147      * Validate getChildren when given a VFS URI for a directory
148      *
149      */

150     public void testListFolderChildren() throws Exception JavaDoc {
151         final String JavaDoc testFolder = "2/mod10.1";
152         final List JavaDoc expectedFiles = Arrays.asList(new String JavaDoc[] {"ivy-1.0.xml",
153                                                                  "ivy-1.1.xml",
154                                                                  "ivy-1.2.xml",
155                                                                  "ivy-1.3.xml"});
156         
157         Iterator JavaDoc baseVfsURIs = helper.createVFSUriSet(testFolder).iterator();
158         while(baseVfsURIs.hasNext()) {
159             VfsURI baseVfsURI = (VfsURI) baseVfsURIs.next();
160             
161             List JavaDoc expected = new ArrayList JavaDoc();
162             for (int i = 0; i < expectedFiles.size(); i++) {
163                 String JavaDoc resId = baseVfsURI.toString() + "/" + expectedFiles.get(i);
164                 expected.add(resId);
165             }
166             
167             List JavaDoc actual = new ArrayList JavaDoc();
168             VfsResource res = new VfsResource(baseVfsURI.toString(), helper.fsManager);
169             Iterator JavaDoc children = res.getChildren().iterator();
170             while (children.hasNext()) {
171                 String JavaDoc resId = (String JavaDoc) children.next();
172                 // remove entries ending in .svn
173
if (! resId.endsWith(".svn")) {
174                     actual.add(resId);
175                 }
176             }
177             
178             Collections.sort(actual);
179             Collections.sort(expected);
180             if (! actual.equals(expected)) {
181                 fail("\nExpected: " + expected.toString() + "\n.Actual: " + actual.toString());
182             }
183         }
184     }
185     
186     /**
187      * Validate that we don't get any results when we query a VFSResource file object
188      * for its children
189      *
190      */

191     public void testListFileChildren() throws Exception JavaDoc {
192         Iterator JavaDoc testSet = helper.createVFSUriSet(VfsTestHelper.TEST_IVY_XML).iterator();
193         while (testSet.hasNext()) {
194             VfsURI vfsURI = (VfsURI) testSet.next();
195             VfsResource res = new VfsResource(vfsURI.toString(), helper.fsManager);
196             List JavaDoc results = res.getChildren();
197             if (results.size() > 0) {
198                 fail("getChildren query on file provided results when it shouldn't have");
199             }
200         }
201     }
202     
203     /**
204      * Validate that we don't get any results if we ask an IMAGINARY VFSResource - a
205      * nonexistent file - for a list of its children
206      *
207      */

208     public void testListImaginary() throws Exception JavaDoc {
209         Iterator JavaDoc testSet = helper.createVFSUriSet("idontexistzzxx").iterator();
210         while (testSet.hasNext()) {
211             VfsURI vfsURI = (VfsURI) testSet.next();
212             VfsResource res = new VfsResource(vfsURI.toString(), helper.fsManager);
213             List JavaDoc results = res.getChildren();
214             if (results.size() > 0) {
215                 fail("getChildren query on file provided results when it shouldn't have");
216             }
217         }
218     }
219 }
220
221
Popular Tags