KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > url > file > FileProtocolTest


1 /**
2  *
3  * Copyright 2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.system.url.file;
19
20 import java.io.BufferedOutputStream JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.net.URLConnection JavaDoc;
28
29 import org.apache.geronimo.system.url.GeronimoURLFactory;
30
31 import junit.framework.TestCase;
32
33 /**
34  * Unit test for the 'file' protocol.
35  *
36  * @version $Rev: 53762 $ $Date: 2004-10-04 15:54:53 -0700 (Mon, 04 Oct 2004) $
37  */

38 public class FileProtocolTest extends TestCase {
39     static {
40         //
41
// Have to install factory to make sure that our file handler is used
42
// and not Sun's
43
//
44
GeronimoURLFactory.install();
45     }
46
47     private File JavaDoc file;
48     private URL JavaDoc fileURL;
49
50     protected void setUp() throws Exception JavaDoc {
51         try {
52             file = File.createTempFile("FileProtocolTest", ".tmp");
53             fileURL = file.toURI().toURL();
54         } catch (Exception JavaDoc e) {
55             if (file != null) {
56                 file.delete();
57             }
58             throw e;
59         }
60     }
61
62     protected void tearDown() throws Exception JavaDoc {
63         if (file != null) {
64             file.delete();
65         }
66     }
67
68     public void testCreateURL() throws Exception JavaDoc {
69         new URL JavaDoc("file:/some/file");
70     }
71
72     public void testURLConnectionType() throws Exception JavaDoc {
73         File JavaDoc tempFile = null;
74         try {
75             tempFile = File.createTempFile("foo", "bar");
76             URL JavaDoc url = new URL JavaDoc(tempFile.toURL().toExternalForm());
77             URLConnection JavaDoc c = url.openConnection();
78             assertEquals(FileURLConnection.class, c.getClass());
79         } finally {
80             if (tempFile != null) {
81                 tempFile.delete();
82             }
83         }
84     }
85
86     public void testFileToURL() throws Exception JavaDoc {
87         URL JavaDoc url = file.toURL();
88         URLConnection JavaDoc c = url.openConnection();
89         assertEquals(FileURLConnection.class, c.getClass());
90     }
91
92     public void testGetLastModified() throws Exception JavaDoc {
93         URLConnection JavaDoc c = fileURL.openConnection();
94         assertEquals(file.lastModified(), c.getLastModified());
95         file.setLastModified(System.currentTimeMillis());
96         assertEquals(file.lastModified(), c.getLastModified());
97     }
98
99     public void testGetDate() throws Exception JavaDoc {
100         URLConnection JavaDoc c = fileURL.openConnection();
101         assertEquals(file.lastModified(), c.getDate());
102         file.setLastModified(System.currentTimeMillis());
103         assertEquals(file.lastModified(), c.getDate());
104     }
105
106     private void writeSomeBytes(final File JavaDoc file, final int count) throws IOException JavaDoc {
107         OutputStream JavaDoc output = new FileOutputStream JavaDoc(file);
108         try {
109             writeSomeBytes(output, count);
110         } finally {
111             output.close();
112         }
113     }
114
115     private void writeSomeBytes(final OutputStream JavaDoc output, final int count) throws IOException JavaDoc {
116         output.write(new byte[count]);
117         output.flush();
118     }
119
120     public void testGetContentLength() throws Exception JavaDoc {
121         int length = 0;
122         URLConnection JavaDoc c = fileURL.openConnection();
123         assertEquals(file.length(), c.getContentLength());
124
125         length += 8;
126         writeSomeBytes(file, length);
127         assertEquals(length, file.length());
128         assertEquals(file.length(), c.getContentLength());
129
130         length += 1;
131         writeSomeBytes(file, length);
132         assertEquals(length, file.length());
133         assertEquals(file.length(), c.getContentLength());
134
135         length += 10;
136         writeSomeBytes(file, length);
137         assertEquals(length, file.length());
138         assertEquals(file.length(), c.getContentLength());
139
140         length *= 2;
141         writeSomeBytes(file, length);
142         assertEquals(length, file.length());
143         assertEquals(file.length(), c.getContentLength());
144     }
145
146     public void testGetContentType() throws Exception JavaDoc {
147         File JavaDoc file = null;
148         try {
149             file = File.createTempFile("FileProtocolTest", ".xml");
150             URLConnection JavaDoc c = file.toURI().toURL().openConnection();
151             assertEquals("application/xml", c.getContentType());
152         } finally {
153             if (file != null) {
154                 file.delete();
155             }
156         }
157     }
158
159     public void testGetInputStream() throws Exception JavaDoc {
160         URLConnection JavaDoc c = fileURL.openConnection();
161         InputStream JavaDoc input = c.getInputStream();
162
163         int length = 8;
164         writeSomeBytes(file, length);
165         assertEquals(length, input.available());
166
167         length *= 8;
168         writeSomeBytes(file, length);
169         assertEquals(length, input.available());
170
171         try {
172             input.close();
173             input.read();
174             fail("Expected IOException");
175         } catch (IOException JavaDoc e) {
176             // OK
177
}
178     }
179
180     public void testSyncFDUpdatesFileLength() throws Exception JavaDoc {
181         File JavaDoc foo = null;
182         OutputStream JavaDoc out = null;
183         try {
184             foo = File.createTempFile("TestFileLength", ".tmp");
185             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(foo);
186             out = new BufferedOutputStream JavaDoc(fos);
187
188             out.write(new byte[10]);
189             out.flush();
190             // out.close();
191
fos.getFD().sync(); // this is required on Windows for foo.length to be updated
192
assertEquals(10, foo.length());
193         } finally {
194             if (out != null) {
195                 try {
196                     out.close();
197                 } catch (IOException JavaDoc ignored) {
198                 }
199             }
200             foo.delete();
201         }
202     }
203
204     public void testGetOutputStream() throws Exception JavaDoc {
205         URLConnection JavaDoc c = fileURL.openConnection();
206         OutputStream JavaDoc output = c.getOutputStream();
207
208         int length = 8;
209         writeSomeBytes(output, length);
210
211         // Do not check file length, may fail on windows
212
// assertEquals(length, file.length());
213

214         // This should work, as the connection should sync the fd
215
assertEquals(length, c.getContentLength());
216
217         writeSomeBytes(output, length);
218
219         // Do not check file length, may fail on windows
220
// assertEquals(length * 2, file.length());
221

222         // This should work, as the connection should sync the fd
223
assertEquals(length * 2, c.getContentLength());
224
225         try {
226             output.close();
227             writeSomeBytes(output, 1);
228             fail("Expected IOException");
229         } catch (IOException JavaDoc e) {
230             // OK
231
}
232     }
233 }
234
Popular Tags