From: Roman Kennke <roman.kennke@aicas.com>
To: mauve-patches <mauve-patches@sources.redhat.com>
Subject: FYI: New TransferHandler test
Date: Tue, 17 Oct 2006 15:24:00 -0000 [thread overview]
Message-ID: <1161098626.4769.12.camel@localhost> (raw)
[-- Attachment #1: Type: text/plain, Size: 194 bytes --]
This tests the method TransferHandler.createTransferable().
2006-10-17 Roman Kennke <kennke@aicas.com>
* gnu/testlet/javax/swing/TransferHandler/createTransferable.java:
New test.
/Roman
[-- Attachment #2: patch.diff --]
[-- Type: text/x-patch, Size: 7575 bytes --]
Index: gnu/testlet/javax/swing/TransferHandler/createTransferable.java
===================================================================
RCS file: gnu/testlet/javax/swing/TransferHandler/createTransferable.java
diff -N gnu/testlet/javax/swing/TransferHandler/createTransferable.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ gnu/testlet/javax/swing/TransferHandler/createTransferable.java 17 Oct 2006 15:22:14 -0000
@@ -0,0 +1,246 @@
+/* createTransferable.java -- Tests TransferHandler.createTransferable()
+ Copyright (C) 2006 Roman Kennke (kennke@aicas.com)
+This file is part of Mauve.
+
+Mauve is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+Mauve is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Mauve; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+*/
+
+// Tags: JDK1.4
+
+package gnu.testlet.javax.swing.TransferHandler;
+
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.IOException;
+
+import javax.swing.JComponent;
+import javax.swing.TransferHandler;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests the TransferHandler.createTransferable() method.
+ */
+public class createTransferable extends TestCase
+{
+
+ /**
+ * Overridden to make createTransferable() public for testing.
+ */
+ private class TestTransferHandler extends TransferHandler
+ {
+ TestTransferHandler(String prop)
+ {
+ super(prop);
+ }
+ public Transferable createTransferable(JComponent comp)
+ {
+ return super.createTransferable(comp);
+ }
+ }
+
+ public class TestComponent
+ extends JComponent
+ {
+ private String value;
+ public void setTestProperty(String val)
+ {
+ value = val;
+ }
+ public String getTestProperty()
+ {
+ return "HelloWorld";
+ }
+ }
+
+ /**
+ * The transfer handler that we test.
+ */
+ private TestTransferHandler transferHandler;
+
+ /**
+ * The component that is passed in the createTransferable() method.
+ */
+ private JComponent component;
+
+ public void setUp()
+ {
+ component = new TestComponent();
+ transferHandler = new TestTransferHandler("testProperty");
+ }
+
+ public void tearDown()
+ {
+ component = null;
+ transferHandler = null;
+ }
+
+ /**
+ * Check what is returned when we initialize the TransferHandler with a
+ * null property.
+ */
+ public void testNullProperty()
+ {
+ transferHandler = new TestTransferHandler(null);
+ Transferable transferable = transferHandler.createTransferable(component);
+ assertNull(transferable);
+ }
+
+ public void testMissingGetter()
+ {
+ component = new JComponent()
+ {
+ String value;
+ public void setTestProperty(String val)
+ {
+ value = val;
+ }
+ };
+ Transferable transferable = transferHandler.createTransferable(component);
+ assertNull(transferable);
+ }
+
+ public void testMissingSetter()
+ {
+ component = new JComponent()
+ {
+ String value;
+ public String getTestProperty()
+ {
+ return value;
+ }
+ };
+ Transferable transferable = transferHandler.createTransferable(component);
+ assertNotNull(transferable);
+ }
+
+ public void testAllOk()
+ {
+ Transferable transferable = transferHandler.createTransferable(component);
+ assertNotNull(transferable);
+ }
+
+ /**
+ * Tests which transfer flavors are supported by the created Transferable.
+ */
+ public void testTransferableTransferFlavors()
+ {
+ Transferable transferable = transferHandler.createTransferable(component);
+ DataFlavor[] flavors = transferable.getTransferDataFlavors();
+ assertEquals(1, flavors.length);
+ assertEquals("application/x-java-jvm-local-objectref; class=java.lang.String", flavors[0].getMimeType());
+ }
+
+ public void testTransferableDataFlavorSupported()
+ {
+ Transferable transferable = transferHandler.createTransferable(component);
+ try
+ {
+ // Primary type doesn't match.
+ DataFlavor flavor = new DataFlavor("xyz/x-java-jvm-local-objectref; class=java.lang.String");
+ assertFalse(transferable.isDataFlavorSupported(flavor));
+ // Subtype type doesn't match.
+ flavor = new DataFlavor("application/x-java-remote-object; class=java.lang.String");
+ assertFalse(transferable.isDataFlavorSupported(flavor));
+ // Representation class doesn't match.
+ flavor = new DataFlavor("application/x-java-jvm-local-objectref; class=java.lang.Integer");
+ assertFalse(transferable.isDataFlavorSupported(flavor));
+
+ // Good match.
+ flavor = new DataFlavor("application/x-java-jvm-local-objectref; class=java.lang.String");
+ assertTrue(transferable.isDataFlavorSupported(flavor));
+ }
+ catch (ClassNotFoundException ex)
+ {
+ fail(ex.getMessage());
+ }
+ }
+
+ public void testTransferableTransferData()
+ {
+ Transferable transferable = transferHandler.createTransferable(component);
+ // Try invalid data flavor.
+ try
+ {
+ DataFlavor flavor = new DataFlavor("xyz/x-java-jvm-local-objectref; class=java.lang.String");
+ Object data = transferable.getTransferData(flavor);
+ fail("UnsupportedOperationException must be thrown");
+ }
+ catch (UnsupportedFlavorException ex)
+ {
+ // Ok.
+ }
+ catch (IOException ex)
+ {
+ fail("UnsupportedFlavorException must be thrown, no IOException");
+ }
+ catch (ClassNotFoundException ex)
+ {
+ fail(ex.getMessage());
+ }
+
+ // Try OK data flavor.
+ try
+ {
+ DataFlavor flavor = new DataFlavor("application/x-java-jvm-local-objectref; class=java.lang.String");
+ Object data = transferable.getTransferData(flavor);
+ assertEquals(data, "HelloWorld");
+ }
+ catch (UnsupportedFlavorException ex)
+ {
+ fail("UnsupportedFlavorException must not be thrown");
+ }
+ catch (IOException ex)
+ {
+ System.err.println(ex.getMessage());
+ fail("IOException must not be thrown");
+ }
+ catch (ClassNotFoundException ex)
+ {
+ fail(ex.getMessage());
+ }
+ // Try non-accessible component.
+ component = new JComponent()
+ {
+ public String getTestProperty()
+ {
+ return "Hello World";
+ }
+ };
+ transferable = transferHandler.createTransferable(component);
+
+ try
+ {
+ DataFlavor flavor = new DataFlavor("application/x-java-jvm-local-objectref; class=java.lang.String");
+ Object data = transferable.getTransferData(flavor);
+ fail("IOException must be thrown");
+ }
+ catch (UnsupportedFlavorException ex)
+ {
+ fail("UnsupportedFlavorException must not be thrown");
+ }
+ catch (IOException ex)
+ {
+ // Ok.
+ }
+ catch (ClassNotFoundException ex)
+ {
+ fail(ex.getMessage());
+ }
+ }
+}
next reply other threads:[~2006-10-17 15:24 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-10-17 15:24 Roman Kennke [this message]
2006-10-28 10:31 Roman Kennke
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1161098626.4769.12.camel@localhost \
--to=roman.kennke@aicas.com \
--cc=mauve-patches@sources.redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).