[jboss-svn-commits] JBoss Common SVN: r2866 - in common-core/trunk/src: test/java/org/jboss/test/util/test/protocol and 1 other directory.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Wed Jun 11 06:16:01 EDT 2008
Author: dimitris at jboss.org
Date: 2008-06-11 06:16:01 -0400 (Wed, 11 Jun 2008)
New Revision: 2866
Modified:
common-core/trunk/src/main/java/org/jboss/net/protocol/file/FileURLConnection.java
common-core/trunk/src/test/java/org/jboss/test/util/test/protocol/FileURLConnectionTestCase.java
Log:
JBCOMMON-56, put back org.jboss.protocol.file.FileURLConnection and add missing functionality - add more tests
Modified: common-core/trunk/src/main/java/org/jboss/net/protocol/file/FileURLConnection.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/net/protocol/file/FileURLConnection.java 2008-06-11 09:49:04 UTC (rev 2865)
+++ common-core/trunk/src/main/java/org/jboss/net/protocol/file/FileURLConnection.java 2008-06-11 10:16:01 UTC (rev 2866)
@@ -58,7 +58,9 @@
{
String flag = System.getProperty("org.jboss.net.protocol.file.decodeFilePaths");
if (flag != null)
+ {
decodeFilePaths = Boolean.valueOf(flag).booleanValue();
+ }
}
/** The underlying file */
@@ -67,14 +69,16 @@
public FileURLConnection(final URL url) throws MalformedURLException, IOException
{
super(url);
+
String path = url.getPath();
if (decodeFilePaths)
+ {
path = URLDecoder.decode(path, "UTF-8");
-
+ }
// Convert the url '/' to the os file separator
file = new File(path.replace('/', File.separatorChar).replace('|', ':'));
- doOutput = false;
+ super.doOutput = false;
}
/**
@@ -105,11 +109,11 @@
public InputStream getInputStream() throws IOException
{
- if (!connected)
- connect();
+ connect();
if (file.isDirectory())
{
+ // return a sorted list of the directory contents
String[] files = file.list();
Arrays.sort(files);
StringBuilder sb = new StringBuilder();
@@ -125,10 +129,10 @@
}
}
+ // We should probably disallow this?
public OutputStream getOutputStream() throws IOException
{
- if (!connected)
- connect();
+ connect();
SecurityManager sm = System.getSecurityManager();
if (sm != null)
@@ -141,8 +145,12 @@
}
/**
- * Provides support for returning the value for the
- * <tt>last-modified</tt> header.
+ * Provides support for the following headers:
+ *
+ * <tt>last-modified</tt>
+ * <tt>content-length</tt>
+ * <tt>content-type</tt>
+ * <tt>date</tt>
*/
public String getHeaderField(final String name)
{
@@ -165,20 +173,27 @@
}
else if (name.equalsIgnoreCase("content-type"))
{
- headerField = getFileNameMap().getContentTypeFor(file.getName());
- if (headerField == null)
+ if (file.isDirectory())
{
- try
+ headerField = "text/plain";
+ }
+ else
+ {
+ headerField = getFileNameMap().getContentTypeFor(file.getName());
+ if (headerField == null)
{
- InputStream is = getInputStream();
- BufferedInputStream bis = new BufferedInputStream(is);
- headerField = URLConnection.guessContentTypeFromStream(bis);
- bis.close();
+ try
+ {
+ InputStream is = getInputStream();
+ BufferedInputStream bis = new BufferedInputStream(is);
+ headerField = URLConnection.guessContentTypeFromStream(bis);
+ bis.close();
+ }
+ catch(IOException e)
+ {
+ // ignore
+ }
}
- catch(IOException e)
- {
- // ignore
- }
}
}
else if (name.equalsIgnoreCase("date"))
Modified: common-core/trunk/src/test/java/org/jboss/test/util/test/protocol/FileURLConnectionTestCase.java
===================================================================
--- common-core/trunk/src/test/java/org/jboss/test/util/test/protocol/FileURLConnectionTestCase.java 2008-06-11 09:49:04 UTC (rev 2865)
+++ common-core/trunk/src/test/java/org/jboss/test/util/test/protocol/FileURLConnectionTestCase.java 2008-06-11 10:16:01 UTC (rev 2866)
@@ -24,6 +24,7 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
+import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
@@ -45,97 +46,140 @@
public void testLastModified() throws Exception
{
File tmp = File.createTempFile("testLastModified", "");
+ tmp.deleteOnExit();
long lastModified = tmp.lastModified();
System.out.println("Created file: " + tmp.getAbsolutePath() + ", lastModified:" + lastModified);
- // This will return JDK's provided FileURLConnection
URL tmpURL = tmp.toURL();
- URLConnection tmpConn = tmpURL.openConnection();
- System.out.println("Got URLConnection of type: " + tmpConn.getClass().getName());
- assertEquals(lastModified, tmpConn.getLastModified());
-
- long lastModifiedHdr = tmpConn.getHeaderFieldDate("last-modified", 0);
- System.out.println("last-modified header: "+lastModifiedHdr);
- // the last-modified header is expected to strip the milliseconds to
- // comply with the (dd MMM yyyy HH:mm:ss) format, so the following assertions
- // is invalid on windows that provide millisecond accuracy to File.lastModified()
- // see, http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4504473
- // assertEquals(lastModified, lastModifiedHdr);
-
- // Test that JBoss FileURLConnection matches JDK's FileURLConnection results
- tmpConn = new FileURLConnection(tmpURL);
- tmpConn.connect();
- System.out.println("Got URLConnection of type: " + tmpConn.getClass().getName());
- assertEquals(lastModified, tmpConn.getLastModified());
-
- lastModifiedHdr = tmpConn.getHeaderFieldDate("last-modified", 0);
- System.out.println("last-modified header: "+lastModifiedHdr);
-
- // cleanup
- tmp.delete();
+ // Test JDK's provided FileURLConnection
+ checkLastModified(tmpURL.openConnection(), lastModified);
+ // Test JBoss's FileURLConnection
+ checkLastModified(new FileURLConnection(tmpURL), lastModified);
}
- public void testLength() throws Exception
+ public void testContentLength() throws Exception
{
- File tmp = File.createTempFile("testLength", "");
+ File tmp = File.createTempFile("testContentLength", "");
+ tmp.deleteOnExit();
FileOutputStream fos = new FileOutputStream(tmp);
fos.write("testLength".getBytes());
fos.close();
-
+ long expectedLength = tmp.length();
+
URL tmpURL = tmp.toURL();
- URLConnection tmpConn = tmpURL.openConnection();
- int length = tmpConn.getContentLength();
- System.out.println(tmp.getAbsolutePath()+", length:"+length);
- assertEquals(tmp.length(), length);
- int lengthHdr = tmpConn.getHeaderFieldInt("Content-Length", 0);
- assertEquals(length, lengthHdr);
-
- // cleanup
- tmp.delete();
+ checkContentLength(tmpURL.openConnection(), expectedLength);
+ checkContentLength(new FileURLConnection(tmpURL), expectedLength);
}
- public void testDirectoryList() throws Exception
+ public void testContentType() throws Exception
{
- // create a directory structure
- // testDirectoryList####/
- // +-test.txt
- // \-test.dir/
- File rootDir = File.createTempFile("testDirectoryList", "");
- rootDir.delete();
- rootDir.mkdir();
- System.out.println(rootDir);
- File tmpFile = new File(rootDir, "test.txt");
- tmpFile.createNewFile();
- System.out.println(tmpFile);
- FileOutputStream fos = new FileOutputStream(tmpFile);
- fos.write("this is a test file".getBytes());
+ File tmp = File.createTempFile("testContentType", ".txt");
+ tmp.deleteOnExit();
+
+ FileOutputStream fos = new FileOutputStream(tmp);
+ fos.write("A text file".getBytes());
fos.close();
- File tmpDir = new File(rootDir, "test.dir");
- tmpDir.mkdir();
- System.out.println(tmpDir);
+ String expectedContentType = "text/plain";
- URL rootURL = rootDir.toURL();
- URLConnection rootConn = rootURL.openConnection();
- System.out.println("Got URLConnection of type: " + rootConn.getClass().getName());
- BufferedReader in = new BufferedReader(new InputStreamReader(rootConn.getInputStream()));
- // verify we can read the sorted file list
- assertEquals("directory entry #1", "test.dir", in.readLine());
- assertEquals("directory entry #2", "test.txt", in.readLine());
- assertEquals("directory entry #3", null, in.readLine());
- in.close();
+ URL tmpURL = tmp.toURL();
+ checkContentType(tmpURL.openConnection(), expectedContentType);
+
+ checkContentType(new FileURLConnection(tmpURL), expectedContentType);
- // Test that JBoss FileURLConnection matches JDK's FileURLConnection results
- rootConn = new FileURLConnection(rootURL);
- rootConn.connect();
- System.out.println("Got URLConnection of type: " + rootConn.getClass().getName());
- in = new BufferedReader(new InputStreamReader(rootConn.getInputStream()));
- // verify we can read the sorted file list
- assertEquals("directory entry #1", "test.dir", in.readLine());
- assertEquals("directory entry #2", "test.txt", in.readLine());
- assertEquals("directory entry #3", null, in.readLine());
- in.close();
+ File dir = tmp.getParentFile();
+ URL dirURL = dir.toURL();
+ checkContentType(dirURL.openConnection(), expectedContentType);
- // cleanup
- Files.delete(rootDir);
+ checkContentType(new FileURLConnection(dirURL), expectedContentType);
}
+
+ public void testDirectoryListing() throws Exception
+ {
+ // create a test directory structure
+ // TMPDIR/testDirectoryList39558
+ // TMPDIR/testDirectoryList39558/test.txt
+ // TMPDIR/testDirectoryList39558/test.dir
+ File rootDir = File.createTempFile("testDirectoryList", "");
+ try
+ {
+ rootDir.delete();
+ rootDir.mkdir();
+ System.out.println(rootDir);
+ File tmpFile = new File(rootDir, "test.txt");
+ tmpFile.createNewFile();
+ System.out.println(tmpFile);
+ FileOutputStream fos = new FileOutputStream(tmpFile);
+ fos.write("this is a test file".getBytes());
+ fos.close();
+ File tmpDir = new File(rootDir, "test.dir");
+ tmpDir.mkdir();
+ System.out.println(tmpDir);
+
+ String[] expectedList = { "test.dir", "test.txt", null };
+ URL rootURL = rootDir.toURL();
+
+ // Check JDK FileURLConnection impl
+ checkDirectoryListing(rootURL.openConnection(), expectedList);
+
+ // Test JBoss FileURLConnection impl
+ checkDirectoryListing(new FileURLConnection(rootURL), expectedList);
+ }
+ finally
+ {
+ // cleanup
+ Files.delete(rootDir);
+ }
+ }
+
+ private void checkLastModified(URLConnection conn, long expectedLastModified)
+ {
+ System.out.println("Got URLConnection of type: " + conn.getClass().getName());
+ assertEquals(expectedLastModified, conn.getLastModified());
+
+ long lastModifiedHdr = conn.getHeaderFieldDate("last-modified", 0);
+ System.out.println(conn.getURL() + ", last-modified header: "+lastModifiedHdr);
+ // the last-modified header is expected to strip the milliseconds to
+ // comply with the (dd MMM yyyy HH:mm:ss) format, so the following assertions
+ // is invalid on windows that provide millisecond accuracy to File.lastModified()
+ // see, http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4504473
+ // assertEquals(expectedLastModified, lastModifiedHdr);
+ }
+
+ private void checkContentLength(URLConnection conn, long expectedLength)
+ {
+ System.out.println("Got URLConnection of type: " + conn.getClass().getName());
+ int length = conn.getContentLength();
+ System.out.println(conn.getURL() + ", content-length:" + length);
+ assertEquals(expectedLength, length);
+ int lengthHdr = conn.getHeaderFieldInt("content-length", 0);
+ assertEquals(expectedLength, lengthHdr);
+ }
+
+ private void checkContentType(URLConnection conn, String expectedType)
+ {
+ System.out.println("Got URLConnection of type: " + conn.getClass().getName());
+ String type = conn.getContentType();
+ System.out.println(conn.getURL() + ", content-type: " + type);
+ assertEquals(type, expectedType);
+ }
+
+ private void checkDirectoryListing(URLConnection conn, String[] expectedFiles) throws IOException
+ {
+ System.out.println("Got URLConnection of type: " + conn.getClass().getName());
+ BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
+ try
+ {
+ // verify the sorted directory list
+ for (int i = 0; i < expectedFiles.length; i++)
+ {
+ String msg = "directory entry #" + i;
+ System.out.println(msg + " : " + expectedFiles[i]);
+ assertEquals(msg, expectedFiles[i], in.readLine());
+ }
+ }
+ finally
+ {
+ in.close();
+ }
+ }
}
More information about the jboss-svn-commits
mailing list