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 test.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 javafx.scene.input.DataFormat;
  33 import org.junit.Test;
  34 import org.junit.runner.RunWith;
  35 import static org.junit.Assert.*;
  36 
  37 @RunWith(Parameterized.class)
  38 public class DataFormatTest {
  39     
  40     static DataFormat customFormat = new DataFormat("Custom1", "Custom2");
  41     static DataFormat uniqueFormat = new DataFormat("Unique");
  42 
  43     @Parameters
  44     public static Collection getParams() {
  45         return Arrays.asList(new Object[][] {
  46             { DataFormat.PLAIN_TEXT, "text/plain", null },
  47             { DataFormat.HTML, "text/html", null },
  48             { DataFormat.RTF, "text/rtf", null },
  49             { DataFormat.URL, "text/uri-list", null },
  50             { DataFormat.IMAGE, "application/x-java-rawimage", null },
  51             { DataFormat.FILES, "application/x-java-file-list",  "java.file-list" },
  52             { customFormat, "Custom1", "Custom2" }
  53         });
  54     }
  55     
  56     private DataFormat format;
  57     private String mime1;
  58     private String mime2;
  59 
  60     public DataFormatTest(DataFormat format, String mime1, String mime2) {
  61         this.format = format;
  62         this.mime1 = mime1;
  63         this.mime2 = mime2;
  64     }
  65     
  66     @Test
  67     public void testMimeTypes() {
  68         assertEquals(mime2 != null ? 2 : 1, format.getIdentifiers().size());
  69         assertTrue(format.getIdentifiers().contains(mime1));
  70         if (mime2 != null) {
  71             assertTrue(format.getIdentifiers().contains(mime2));
  72         }
  73     }
  74 
  75     @Test
  76     public void dataFormatsShouldBeFound() {
  77         assertSame(format, DataFormat.lookupMimeType(mime1));
  78         if (mime2 != null) {
  79             assertSame(format, DataFormat.lookupMimeType(mime2));            
  80         }
  81     }
  82 
  83     @Test
  84     public void testToString() {
  85         assertNotNull(customFormat.toString());
  86         assertFalse("".equals(customFormat.toString()));
  87     }
  88 
  89     @Test(expected=IllegalArgumentException.class)
  90     public void shouldNotBePossibleToReuseMimeTypes() {
  91         DataFormat customEqual = new DataFormat(format.getIdentifiers().toArray(
  92                 new String[format.getIdentifiers().size()]));
  93     }
  94 
  95 
  96     @Test
  97     public void testEqualsAndHashCode() {
  98         //cannot have two different equal data formats
  99         assertEquals(format, format);
 100         assertEquals(format.hashCode(), format.hashCode());
 101         assertFalse(uniqueFormat.equals(format));
 102         assertFalse(uniqueFormat.hashCode() == format.hashCode());
 103     }
 104 }