1 17 18 package org.apache.geronimo.system.url.file; 19 20 import java.io.BufferedOutputStream ; 21 import java.io.File ; 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.OutputStream ; 26 import java.net.URL ; 27 import java.net.URLConnection ; 28 29 import org.apache.geronimo.system.url.GeronimoURLFactory; 30 31 import junit.framework.TestCase; 32 33 38 public class FileProtocolTest extends TestCase { 39 static { 40 GeronimoURLFactory.install(); 45 } 46 47 private File file; 48 private URL fileURL; 49 50 protected void setUp() throws Exception { 51 try { 52 file = File.createTempFile("FileProtocolTest", ".tmp"); 53 fileURL = file.toURI().toURL(); 54 } catch (Exception e) { 55 if (file != null) { 56 file.delete(); 57 } 58 throw e; 59 } 60 } 61 62 protected void tearDown() throws Exception { 63 if (file != null) { 64 file.delete(); 65 } 66 } 67 68 public void testCreateURL() throws Exception { 69 new URL ("file:/some/file"); 70 } 71 72 public void testURLConnectionType() throws Exception { 73 File tempFile = null; 74 try { 75 tempFile = File.createTempFile("foo", "bar"); 76 URL url = new URL (tempFile.toURL().toExternalForm()); 77 URLConnection 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 { 87 URL url = file.toURL(); 88 URLConnection c = url.openConnection(); 89 assertEquals(FileURLConnection.class, c.getClass()); 90 } 91 92 public void testGetLastModified() throws Exception { 93 URLConnection 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 { 100 URLConnection 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 file, final int count) throws IOException { 107 OutputStream output = new FileOutputStream (file); 108 try { 109 writeSomeBytes(output, count); 110 } finally { 111 output.close(); 112 } 113 } 114 115 private void writeSomeBytes(final OutputStream output, final int count) throws IOException { 116 output.write(new byte[count]); 117 output.flush(); 118 } 119 120 public void testGetContentLength() throws Exception { 121 int length = 0; 122 URLConnection 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 { 147 File file = null; 148 try { 149 file = File.createTempFile("FileProtocolTest", ".xml"); 150 URLConnection 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 { 160 URLConnection c = fileURL.openConnection(); 161 InputStream 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 e) { 176 } 178 } 179 180 public void testSyncFDUpdatesFileLength() throws Exception { 181 File foo = null; 182 OutputStream out = null; 183 try { 184 foo = File.createTempFile("TestFileLength", ".tmp"); 185 FileOutputStream fos = new FileOutputStream (foo); 186 out = new BufferedOutputStream (fos); 187 188 out.write(new byte[10]); 189 out.flush(); 190 fos.getFD().sync(); assertEquals(10, foo.length()); 193 } finally { 194 if (out != null) { 195 try { 196 out.close(); 197 } catch (IOException ignored) { 198 } 199 } 200 foo.delete(); 201 } 202 } 203 204 public void testGetOutputStream() throws Exception { 205 URLConnection c = fileURL.openConnection(); 206 OutputStream output = c.getOutputStream(); 207 208 int length = 8; 209 writeSomeBytes(output, length); 210 211 214 assertEquals(length, c.getContentLength()); 216 217 writeSomeBytes(output, length); 218 219 222 assertEquals(length * 2, c.getContentLength()); 224 225 try { 226 output.close(); 227 writeSomeBytes(output, 1); 228 fail("Expected IOException"); 229 } catch (IOException e) { 230 } 232 } 233 } 234 | Popular Tags |