scolebourne 2004/07/24 02:58:41
Modified: io/src/test/org/apache/commons/io FileUtilsTestCase.java
io/src/java/org/apache/commons/io FileUtils.java
Log:
Add FileUtils.toFiles(urls)
Revision Changes Path
1.24 +78 -11
jakarta-commons/io/src/test/org/apache/commons/io/FileUtilsTestCase.java
Index: FileUtilsTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/io/src/test/org/apache/commons/io/FileUtilsTestCase.java,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -r1.23 -r1.24
--- FileUtilsTestCase.java 24 Jul 2004 08:41:00 -0000 1.23
+++ FileUtilsTestCase.java 24 Jul 2004 09:58:40 -0000 1.24
@@ -108,18 +108,85 @@
FileUtils.waitFor(new File(""), 2);
}
- // toURL
+ // toFiles
- public void testToURLs() throws Exception {
- File[] files = new File[] { new File("file1"), new File("file2")};
+ public void testToFiles1() throws Exception {
+ URL[] urls = new URL[] {
+ new URL("file", null, "file1.txt"),
+ new URL("file", null, "file2.txt"),
+ };
+ File[] files = FileUtils.toFiles(urls);
+
+ assertEquals(urls.length, files.length);
+ assertEquals("File: " + files[0], true,
files[0].toString().indexOf("file1.txt") >= 0);
+ assertEquals("File: " + files[1], true,
files[1].toString().indexOf("file2.txt") >= 0);
+ }
- URL[] urls = FileUtils.toURLs(files);
+ public void testToFiles2() throws Exception {
+ URL[] urls = new URL[] {
+ new URL("file", null, "file1.txt"),
+ null,
+ };
+ File[] files = FileUtils.toFiles(urls);
+
+ assertEquals(urls.length, files.length);
+ assertEquals("File: " + files[0], true,
files[0].toString().indexOf("file1.txt") >= 0);
+ assertEquals("File: " + files[1], null, files[1]);
+ }
- // Path separator causes equality tests to fail
- //assertEquals(urls[0].getFile(), File.separator +
files[0].getAbsolutePath());
- //assertEquals(urls[1].getFile(), File.separator +
files[1].getAbsolutePath());
+ public void testToFiles3() throws Exception {
+ URL[] urls = null;
+ File[] files = FileUtils.toFiles(urls);
+
+ assertEquals(0, files.length);
+ }
+ public void testToFiles4() throws Exception {
+ URL[] urls = new URL[] {
+ new URL("file", null, "file1.txt"),
+ new URL("http", "jakarta.apache.org", "file1.txt"),
+ };
+ try {
+ FileUtils.toFiles(urls);
+ fail();
+ } catch (IllegalArgumentException ex) {}
}
+
+ // toURLs
+
+ public void testToURLs1() throws Exception {
+ File[] files = new File[] {
+ new File(getTestDirectory(), "file1.txt"),
+ new File(getTestDirectory(), "file2.txt"),
+ };
+ URL[] urls = FileUtils.toURLs(files);
+
+ assertEquals(files.length, urls.length);
+ assertEquals(true, urls[0].toExternalForm().startsWith("file:"));
+ assertEquals(true, urls[0].toExternalForm().indexOf("file1.txt") >= 0);
+ assertEquals(true, urls[1].toExternalForm().startsWith("file:"));
+ assertEquals(true, urls[1].toExternalForm().indexOf("file2.txt") >= 0);
+ }
+
+// public void testToURLs2() throws Exception {
+// File[] files = new File[] {
+// new File(getTestDirectory(), "file1.txt"),
+// null,
+// };
+// URL[] urls = FileUtils.toURLs(files);
+//
+// assertEquals(files.length, urls.length);
+// assertEquals(true, urls[0].toExternalForm().startsWith("file:"));
+// assertEquals(true, urls[0].toExternalForm().indexOf("file1.txt") > 0);
+// assertEquals(null, urls[1]);
+// }
+//
+// public void testToURLs3() throws Exception {
+// File[] files = null;
+// URL[] urls = FileUtils.toURLs(files);
+//
+// assertEquals(0, urls.length);
+// }
// contentEquals
1.36 +47 -9 jakarta-commons/io/src/java/org/apache/commons/io/FileUtils.java
Index: FileUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/io/src/java/org/apache/commons/io/FileUtils.java,v
retrieving revision 1.35
retrieving revision 1.36
diff -u -r1.35 -r1.36
--- FileUtils.java 15 Jul 2004 09:16:17 -0000 1.35
+++ FileUtils.java 24 Jul 2004 09:58:41 -0000 1.36
@@ -87,6 +87,11 @@
* The number of bytes in a gigabyte.
*/
public static final long ONE_GB = ONE_KB * ONE_MB;
+
+ /**
+ * An empty array of type <code>File</code>.
+ */
+ public static final File[] EMPTY_FILE_ARRAY = new File[0];
/**
* Returns a human-readable version of the file size (original is in
@@ -285,11 +290,13 @@
}
}
+ //-----------------------------------------------------------------------
/**
* Convert from a <code>URL</code> to a <code>File</code>.
- * @param url File URL.
- * @return The equivalent <code>File</code> object, or <code>null</code> if the
URL's protocol
- * is not <code>file</code>
+ *
+ * @param url the file URL to convert
+ * @return the equivalent <code>File</code> object, or <code>null</code>
+ * if the URL's protocol is not <code>file</code>
*/
public static File toFile(URL url) {
if (url.getProtocol().equals("file") == false) {
@@ -302,11 +309,42 @@
}
/**
- * Convert the array of Files into a list of URLs.
+ * Converts each of an array of <code>URL</code> to a <code>File</code>.
+ * <p>
+ * Returns an array of the same size as the input.
+ * If the input is null, an empty array is returned.
+ * If the input contains null, the output array contains null at the same index.
+ *
+ * @param urls the file URLs to convert, null returns empty array
+ * @return a non-null array of Files matching the input, with a null item
+ * if there was a null at that index in the input array
+ * @throws IllegalArgumentException if the URL could not be converted to a File
+ */
+ public static File[] toFiles(URL[] urls) {
+ if (urls == null || urls.length == 0) {
+ return EMPTY_FILE_ARRAY;
+ }
+ File[] files = new File[urls.length];
+ for (int i = 0; i < urls.length; i++) {
+ URL url = urls[i];
+ if (url != null) {
+ if (url.getProtocol().equals("file") == false) {
+ throw new IllegalArgumentException("URL could not be converted
to a File: " + url);
+ }
+ files[i] = toFile(url);
+ }
+ }
+ return files;
+ }
+
+ /**
+ * Converts each of an array of <code>File</code> to a <code>URL</code>.
+ * <p>
+ * Returns an array of the same size as the input.
*
- * @param files the array of files
- * @return the array of URLs
- * @throws IOException if an error occurs
+ * @param files the files to convert
+ * @return an array of URLs matching the input
+ * @throws IOException if a file cannot be converted
*/
public static URL[] toURLs(File[] files) throws IOException {
URL[] urls = new URL[files.length];
@@ -318,7 +356,7 @@
return urls;
}
-
+ //-----------------------------------------------------------------------
/**
* Copy file from source to destination. If <code>destinationDirectory</code>
does not exist, it
* (and any parent directories) will be created. If a file <code>source</code>
in
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]