KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > datatransfer > ExClipboardTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.util.datatransfer;
21
22 import java.awt.datatransfer.StringSelection JavaDoc;
23 import java.awt.datatransfer.Transferable JavaDoc;
24 import junit.framework.TestCase;
25
26 /**
27  *
28  * @author Jaroslav Tulach
29  */

30 public class ExClipboardTest extends TestCase {
31     private ExClipboard clipboard;
32
33     private ExClipboard.Convertor[] convertors = new ExClipboard.Convertor[0];
34
35     public ExClipboardTest (String JavaDoc testName) {
36         super (testName);
37     }
38
39     protected void setUp () throws Exception JavaDoc {
40         clipboard = new ExClipboard ("test clipboard") {
41             protected ExClipboard.Convertor[] getConvertors () {
42                 return convertors;
43             }
44         };
45     }
46
47     public void testAddRemoveClipboardListener () {
48         
49         class L implements ClipboardListener {
50             public int cnt;
51             public ClipboardEvent ev;
52             public void clipboardChanged (ClipboardEvent ev) {
53                 cnt++;
54                 this.ev = ev;
55             }
56         }
57         L listener = new L ();
58         
59         clipboard.addClipboardListener (listener);
60         clipboard.fireClipboardChange ();
61         assertEquals ("One event", 1, listener.cnt);
62         assertNotNull ("An event", listener.ev);
63         assertEquals ("source is right", clipboard, listener.ev.getSource ());
64         
65         clipboard.removeClipboardListener (listener);
66         clipboard.fireClipboardChange ();
67         
68         assertEquals ("no new change", 1, listener.cnt);
69     }
70
71     public void testConvert () {
72         class WillNotGetNull implements ExClipboard.Convertor {
73             public Transferable JavaDoc convert (Transferable JavaDoc t) {
74                 assertNotNull ("Never get null", t);
75                 return null;
76             }
77         }
78         
79         convertors = new ExClipboard.Convertor[] {
80             new WillNotGetNull (),
81             new WillNotGetNull (),
82             new WillNotGetNull (),
83         };
84         
85         Transferable JavaDoc ret = clipboard.convert (new StringSelection JavaDoc ("Ahoj"));
86         assertNull ("Correctly returned null", ret);
87         assertNull ("Handle also null parameter", clipboard.convert (null));
88     }
89
90 }
91
Popular Tags