1 /*
   2  * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.scene.input;
  27 
  28 import java.util.Collection;
  29 import org.junit.runners.Parameterized.Parameters;
  30 import org.junit.runners.Parameterized;
  31 import java.util.Arrays;
  32 import org.junit.Test;
  33 import org.junit.runner.RunWith;
  34 import static org.junit.Assert.*;
  35 
  36 @RunWith(Parameterized.class)
  37 public class DataFormatTest {
  38     
  39     static DataFormat customFormat = new DataFormat("Custom1", "Custom2");
  40     static DataFormat uniqueFormat = new DataFormat("Unique");
  41 
  42     @Parameters
  43     public static Collection getParams() {
  44         return Arrays.asList(new Object[][] {
  45             { DataFormat.PLAIN_TEXT, "text/plain", null },
  46             { DataFormat.HTML, "text/html", null },
  47             { DataFormat.RTF, "text/rtf", null },
  48             { DataFormat.URL, "text/uri-list", null },
  49             { DataFormat.IMAGE, "application/x-java-rawimage", null },
  50             { DataFormat.FILES, "application/x-java-file-list",  "java.file-list" },
  51             { customFormat, "Custom1", "Custom2" }
  52         });
  53     }
  54     
  55     private DataFormat format;
  56     private String mime1;
  57     private String mime2;
  58 
  59     public DataFormatTest(DataFormat format, String mime1, String mime2) {
  60         this.format = format;
  61         this.mime1 = mime1;
  62         this.mime2 = mime2;
  63     }
  64     
  65     @Test
  66     public void testMimeTypes() {
  67         assertEquals(mime2 != null ? 2 : 1, format.getIdentifiers().size());
  68         assertTrue(format.getIdentifiers().contains(mime1));
  69         if (mime2 != null) {
  70             assertTrue(format.getIdentifiers().contains(mime2));
  71         }
  72     }
  73 
  74     @Test
  75     public void dataFormatsShouldBeFound() {
  76         assertSame(format, DataFormat.lookupMimeType(mime1));
  77         if (mime2 != null) {
  78             assertSame(format, DataFormat.lookupMimeType(mime2));            
  79         }
  80     }
  81 
  82     @Test
  83     public void testToString() {
  84         assertNotNull(customFormat.toString());
  85         assertFalse("".equals(customFormat.toString()));
  86     }
  87 
  88     @Test(expected=IllegalArgumentException.class)
  89     public void shouldNotBePossibleToReuseMimeTypes() {
  90         DataFormat customEqual = new DataFormat(format.getIdentifiers().toArray(
  91                 new String[format.getIdentifiers().size()]));
  92     }
  93 
  94 
  95     @Test
  96     public void testEqualsAndHashCode() {
  97         //cannot have two different equal data formats
  98         assertEquals(format, format);
  99         assertEquals(format.hashCode(), format.hashCode());
 100         assertFalse(uniqueFormat.equals(format));
 101         assertFalse(uniqueFormat.hashCode() == format.hashCode());
 102     }
 103 }