changeset 939:4eb3bdceece6

RT-21287: restricted one DataFormat per mime type. Fixes also RT-20960.
author Pavel Safrata
date Wed, 02 May 2012 11:03:46 +0200
parents 3fa9344823cf
children ea84eaa07624
files javafx-ui-common/src/javafx/scene/input/DataFormat.java javafx-ui-common/test/unit/javafx/scene/input/DataFormatTest.java
diffstat 2 files changed, 23 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/javafx-ui-common/src/javafx/scene/input/DataFormat.java	Tue May 01 10:04:19 2012 -0700
+++ b/javafx-ui-common/src/javafx/scene/input/DataFormat.java	Wed May 02 11:03:46 2012 +0200
@@ -103,8 +103,18 @@
      * With the above code, if I were to look on the clipboard, I'd find the String "Hello"
      * listed both for "text/foo" and "text/bar" on the clipboard.
      * @param ids The set of ids used to represent this DataFormat on the clipboard.
+     * @throws IllegalArgumentException if one of the given mime types is already
+     *         assigned to another DataFormat.
      */
     public DataFormat(String... ids) {
+        DATA_FORMAT_LIST.cleanup();
+        for (String id : ids) {
+            if (lookupMimeType(id) != null) {
+                throw new IllegalArgumentException("DataFormat '" + id +
+                        "' already exists.");
+            }
+        }
+
         // If ids is null, then we will use an empty set.
         Set<String> set = ids == null ?
                 Collections.<String>emptySet() :
--- a/javafx-ui-common/test/unit/javafx/scene/input/DataFormatTest.java	Tue May 01 10:04:19 2012 -0700
+++ b/javafx-ui-common/test/unit/javafx/scene/input/DataFormatTest.java	Wed May 02 11:03:46 2012 +0200
@@ -54,7 +54,7 @@
     private DataFormat format;
     private String mime1;
     private String mime2;
-    
+
     public DataFormatTest(DataFormat format, String mime1, String mime2) {
         this.format = format;
         this.mime1 = mime1;
@@ -82,15 +82,20 @@
     public void testToString() {
         assertNotNull(customFormat.toString());
         assertFalse("".equals(customFormat.toString()));
-    }    
-    
+    }
+
+    @Test(expected=IllegalArgumentException.class)
+    public void shouldNotBePossibleToReuseMimeTypes() {
+        DataFormat customEqual = new DataFormat(format.getIdentifiers().toArray(
+                new String[format.getIdentifiers().size()]));
+    }
+
+
     @Test
     public void testEqualsAndHashCode() {
-        DataFormat customEqual = new DataFormat(format.getIdentifiers().toArray(
-                new String[format.getIdentifiers().size()]));
-        
-        assertEquals(customEqual, format);
-        assertEquals(customEqual.hashCode(), format.hashCode());
+        //cannot have two different equal data formats
+        assertEquals(format, format);
+        assertEquals(format.hashCode(), format.hashCode());
         assertFalse(uniqueFormat.equals(format));
         assertFalse(uniqueFormat.hashCode() == format.hashCode());
     }