KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > actions > SaveActionTest


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.actions;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.io.File JavaDoc;
24 import javax.swing.Action JavaDoc;
25 import javax.swing.event.ChangeListener JavaDoc;
26 import org.netbeans.junit.MockServices;
27 import org.netbeans.junit.NbTestCase;
28 import org.openide.awt.StatusDisplayer;
29 import org.openide.cookies.SaveCookie;
30 import org.openide.filesystems.FileObject;
31 import org.openide.filesystems.LocalFileSystem;
32 import org.openide.loaders.DataObject;
33 import org.openide.nodes.FilterNode;
34 import org.openide.nodes.Node;
35 import org.openide.util.Lookup;
36 import org.openide.util.lookup.Lookups;
37
38 /**
39  * Tests SaveAction.
40  * @author Jaroslav Tulach
41  */

42 public class SaveActionTest extends NbTestCase {
43     
44     public SaveActionTest(String JavaDoc name) {
45         super(name);
46     }
47     
48     protected void setUp() throws Exception JavaDoc {
49         MockServices.setServices(new Class JavaDoc[] {MyStatusDisplayer.class});
50         assertNotNull("MyDisplayer is used", Lookup.getDefault().lookup(MyStatusDisplayer.class));
51     }
52     
53     protected boolean runInEQ() {
54         return true;
55     }
56     
57     /** @see "issue #36616" */
58     public void testSaveActionTakesNameOfDataNodeIfAvailable() throws Exception JavaDoc {
59         try {
60             LocalFileSystem lfs = new LocalFileSystem();
61             File JavaDoc workDir = getWorkDir();
62             File JavaDoc simpleFile = new File JavaDoc(workDir, "folder/file.simple");
63             if (!simpleFile.exists()) {
64                 simpleFile.getParentFile().mkdirs();
65                 simpleFile.createNewFile();
66                 assertTrue(simpleFile.exists());
67             }
68             lfs.setRootDirectory(workDir);
69             FileObject fo = lfs.findResource("folder/file.simple");
70             assertNotNull(fo);
71             final DataObject obj = DataObject.find(fo);
72
73             SaveAction sa = (SaveAction) SaveAction.get(SaveAction.class);
74             
75             class MyNode extends FilterNode
76             implements SaveCookie {
77                 public int cnt;
78                 
79                 public MyNode() {
80                     super(obj.getNodeDelegate());
81                     disableDelegation(
82                         FilterNode.DELEGATE_GET_NAME |
83                         FilterNode.DELEGATE_GET_DISPLAY_NAME |
84                         FilterNode.DELEGATE_GET_SHORT_DESCRIPTION |
85                         FilterNode.DELEGATE_SET_NAME |
86                         FilterNode.DELEGATE_SET_DISPLAY_NAME |
87                         FilterNode.DELEGATE_SET_SHORT_DESCRIPTION
88                     );
89                     
90                     setName("my name");
91                 }
92                 
93                 public Node.Cookie getCookie(Class JavaDoc c) {
94                     if (c.isInstance(this)) {
95                         return this;
96                     }
97                     return super.getCookie(c);
98                 }
99                 
100                 public void save() {
101                     cnt++;
102                 }
103             }
104             
105             MyNode myNode = new MyNode();
106             Action JavaDoc clone = sa.createContextAwareInstance(Lookups.singleton(myNode));
107             
108             clone.actionPerformed(new ActionEvent JavaDoc(this, 0, "waitFinished"));
109             
110             assertEquals("Save called", 1, myNode.cnt);
111             assertEquals("One msgs", 1, MyStatusDisplayer.cnt);
112             if (MyStatusDisplayer.text.indexOf("file.simple") < 0) {
113                 fail("Wrong message: " + MyStatusDisplayer.text);
114             }
115         } finally {
116             clearWorkDir();
117         }
118     }
119     
120     public static class MyStatusDisplayer extends StatusDisplayer {
121         public static int cnt;
122         public static String JavaDoc text;
123         
124         public void addChangeListener(ChangeListener JavaDoc l) {}
125         
126         public String JavaDoc getStatusText() {
127             return text;
128         }
129         
130         public void removeChangeListener(ChangeListener JavaDoc l) {}
131         
132         public void setStatusText(String JavaDoc msg) {
133             cnt++;
134             text = msg;
135         }
136         
137     }
138     
139 }
140
Popular Tags