harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From odea...@apache.org
Subject svn commit: r812111 - in /harmony/enhanced/classlib/branches/java6/modules/swing: ./ src/main/java/common/javax/swing/filechooser/ src/test/api/java/org/ src/test/api/java/org/apache/ src/test/api/java/org/apache/harmony/ src/test/api/java/org/apache/h...
Date Mon, 07 Sep 2009 12:37:00 GMT
Author: odeakin
Date: Mon Sep  7 12:37:00 2009
New Revision: 812111

URL: http://svn.apache.org/viewvc?rev=812111&view=rev
Log:
Add missing FileNameExtensionFilter class and unit test - gets SwingSet2 demo a little further.

Added:
    harmony/enhanced/classlib/branches/java6/modules/swing/src/main/java/common/javax/swing/filechooser/FileNameExtensionFilter.java
  (with props)
    harmony/enhanced/classlib/branches/java6/modules/swing/src/test/api/java/org/
    harmony/enhanced/classlib/branches/java6/modules/swing/src/test/api/java/org/apache/
    harmony/enhanced/classlib/branches/java6/modules/swing/src/test/api/java/org/apache/harmony/
    harmony/enhanced/classlib/branches/java6/modules/swing/src/test/api/java/org/apache/harmony/swing/
    harmony/enhanced/classlib/branches/java6/modules/swing/src/test/api/java/org/apache/harmony/swing/tests/
    harmony/enhanced/classlib/branches/java6/modules/swing/src/test/api/java/org/apache/harmony/swing/tests/javax/
    harmony/enhanced/classlib/branches/java6/modules/swing/src/test/api/java/org/apache/harmony/swing/tests/javax/swing/
    harmony/enhanced/classlib/branches/java6/modules/swing/src/test/api/java/org/apache/harmony/swing/tests/javax/swing/filechooser/
    harmony/enhanced/classlib/branches/java6/modules/swing/src/test/api/java/org/apache/harmony/swing/tests/javax/swing/filechooser/FileNameExtensionFilterTest.java
  (with props)
Modified:
    harmony/enhanced/classlib/branches/java6/modules/swing/build.xml

Modified: harmony/enhanced/classlib/branches/java6/modules/swing/build.xml
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/swing/build.xml?rev=812111&r1=812110&r2=812111&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/swing/build.xml (original)
+++ harmony/enhanced/classlib/branches/java6/modules/swing/build.xml Mon Sep  7 12:37:00 2009
@@ -154,6 +154,7 @@
         <compile-tests description="swing tests">
             <javac-elements>
                 <src>
+                    <pathelement location="src/test/api/java" />
                     <pathelement location="src/test/api/java.injected" />
                 </src>
             </javac-elements>

Added: harmony/enhanced/classlib/branches/java6/modules/swing/src/main/java/common/javax/swing/filechooser/FileNameExtensionFilter.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/swing/src/main/java/common/javax/swing/filechooser/FileNameExtensionFilter.java?rev=812111&view=auto
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/swing/src/main/java/common/javax/swing/filechooser/FileNameExtensionFilter.java
(added)
+++ harmony/enhanced/classlib/branches/java6/modules/swing/src/main/java/common/javax/swing/filechooser/FileNameExtensionFilter.java
Mon Sep  7 12:37:00 2009
@@ -0,0 +1,73 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package javax.swing.filechooser;
+
+import java.io.File;
+
+public final class FileNameExtensionFilter extends FileFilter {
+
+    private String description;
+    private String[] extensions;
+
+    public FileNameExtensionFilter(String description, String... extensions) {
+        if (extensions == null || extensions.length == 0) {
+            throw new IllegalArgumentException("Extensions array cannot be null or zero in
size");
+        }
+
+        for (int i=0; i<extensions.length; i++) {
+            if (extensions[i] == null || extensions[i].length() == 0) {
+                throw new IllegalArgumentException("Extensions cannot contain a null or zero
length string");
+            }
+        }
+
+        this.description = description;
+        this.extensions = extensions;
+    }
+
+    public boolean accept(File f) {
+        if (f.isDirectory()) return true;
+
+        for (int i=0; i<extensions.length; i++) {
+            if (f.getName().endsWith("." + extensions[i])) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public String[] getExtensions() {
+        return extensions;
+    }
+
+    public String toString() {
+        String resultString = super.toString() + "[description="+description+" extensions=[";
+        for (int i=0; i<extensions.length; i++) {
+            resultString += extensions[i];
+            if (i != extensions.length - 1) {
+                resultString += ", ";
+            }
+        }
+        resultString += "]]";
+        return resultString;
+    }
+}

Propchange: harmony/enhanced/classlib/branches/java6/modules/swing/src/main/java/common/javax/swing/filechooser/FileNameExtensionFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/branches/java6/modules/swing/src/test/api/java/org/apache/harmony/swing/tests/javax/swing/filechooser/FileNameExtensionFilterTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/swing/src/test/api/java/org/apache/harmony/swing/tests/javax/swing/filechooser/FileNameExtensionFilterTest.java?rev=812111&view=auto
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/swing/src/test/api/java/org/apache/harmony/swing/tests/javax/swing/filechooser/FileNameExtensionFilterTest.java
(added)
+++ harmony/enhanced/classlib/branches/java6/modules/swing/src/test/api/java/org/apache/harmony/swing/tests/javax/swing/filechooser/FileNameExtensionFilterTest.java
Mon Sep  7 12:37:00 2009
@@ -0,0 +1,138 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.harmony.swing.tests.javax.swing.filechooser;
+
+import junit.framework.TestCase;
+import javax.swing.filechooser.FileNameExtensionFilter;
+import java.io.File;
+import java.util.Random;
+
+public class FileNameExtensionFilterTest extends TestCase {
+
+    private final String[] extensions = {"zip", "jar"};
+    private final String description = "A description";
+
+    public void test_constructor() {
+        // Should be able to create fnfe with a null descriptor without exception
+        FileNameExtensionFilter fnfe = new FileNameExtensionFilter(null, extensions);
+        assertNull(fnfe.getDescription()); // Just check getDescription() returns null
+
+        // Should throw IllegalArgumentException for a null extensions array
+        String[] extensionsArray = null;
+        try {
+            fnfe = new FileNameExtensionFilter(description, extensionsArray);
+            fail("Should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // Expected
+        }
+
+        // Should throw IllegalArgumentException for 0 length extensions array
+        try {
+            fnfe = new FileNameExtensionFilter(description);
+            fail("Should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // Expected
+        }
+
+        // Should throw IllegalArgumentException for a null extension
+        try {
+            fnfe = new FileNameExtensionFilter(description, "zip", null);
+            fail("Should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // Expected
+        }
+
+        // Should throw IllegalArgumentException for a 0 length extension
+        try {
+            fnfe = new FileNameExtensionFilter(description, "zip", "");
+            fail("Should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // Expected
+        }
+    }
+
+    public void test_getDescription() {
+        FileNameExtensionFilter fnfe = new FileNameExtensionFilter(description, extensions);
+
+        assertEquals(description, fnfe.getDescription());
+    }
+
+    public void test_getExtensions() {
+        FileNameExtensionFilter fnfe = new FileNameExtensionFilter(description, extensions);
+
+        String[] returnedExt = fnfe.getExtensions();
+        for (int i=0; i<extensions.length; i++) {
+            assertEquals(extensions[i], returnedExt[i]);
+        }        
+    }
+
+    public void test_accept() throws Exception {
+        FileNameExtensionFilter fnfe = new FileNameExtensionFilter(description, extensions);
+
+        File file = new File("my_test.jpg");
+        assertFalse(fnfe.accept(file));
+
+        file = new File("my_test.zipp");
+        assertFalse(fnfe.accept(file));
+
+        file = new File("my_testzip");
+        assertFalse(fnfe.accept(file));
+
+        file = new File("my_test.zzip");
+        assertFalse(fnfe.accept(file));
+
+        file = new File("my_test.zip");
+        assertTrue(fnfe.accept(file));
+
+        file = new File("my_test.jar");
+        assertTrue(fnfe.accept(file));
+
+        /* accept() should always return true for directories, whatever the extension */
+        file = createTempDir("FileNameExtensionFilterTest", ".zip");
+        assertTrue(fnfe.accept(file));
+
+        file = createTempDir("FileNameExtensionFilterTest", ".dir");
+        assertTrue(fnfe.accept(file));
+    }
+
+    public void test_toString() {
+        FileNameExtensionFilter fnfe = new FileNameExtensionFilter(description, extensions);
+
+        assertTrue(fnfe.toString().endsWith("[description=A description extensions=[zip,
jar]]"));
+    }
+
+    /* Creates an empty temporary dir using the logic from java.io.File.createTempFile()
*/
+    private File createTempDir(String prefix, String suffix) {
+        String tmpDir = System.getProperty("java.io.tmpdir");
+        File tempDir;
+        int counter = 0;
+        do {
+            if (counter == 0) {
+                int newInt = new Random().nextInt();
+                counter = ((newInt / 65535) & 0xFFFF) + 0x2710;
+            } else{
+                counter++;
+            }
+                        
+            tempDir = new File(tmpDir, prefix + counter + suffix);
+        } while (tempDir.exists());
+        tempDir.mkdir();
+        tempDir.deleteOnExit();
+        return tempDir;
+    }
+}

Propchange: harmony/enhanced/classlib/branches/java6/modules/swing/src/test/api/java/org/apache/harmony/swing/tests/javax/swing/filechooser/FileNameExtensionFilterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message