1 19 20 package org.openide.actions; 21 22 import java.awt.event.ActionEvent ; 23 import java.io.File ; 24 import javax.swing.Action ; 25 import javax.swing.event.ChangeListener ; 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 42 public class SaveActionTest extends NbTestCase { 43 44 public SaveActionTest(String name) { 45 super(name); 46 } 47 48 protected void setUp() throws Exception { 49 MockServices.setServices(new Class [] {MyStatusDisplayer.class}); 50 assertNotNull("MyDisplayer is used", Lookup.getDefault().lookup(MyStatusDisplayer.class)); 51 } 52 53 protected boolean runInEQ() { 54 return true; 55 } 56 57 58 public void testSaveActionTakesNameOfDataNodeIfAvailable() throws Exception { 59 try { 60 LocalFileSystem lfs = new LocalFileSystem(); 61 File workDir = getWorkDir(); 62 File simpleFile = new File (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 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 clone = sa.createContextAwareInstance(Lookups.singleton(myNode)); 107 108 clone.actionPerformed(new ActionEvent (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 text; 123 124 public void addChangeListener(ChangeListener l) {} 125 126 public String getStatusText() { 127 return text; 128 } 129 130 public void removeChangeListener(ChangeListener l) {} 131 132 public void setStatusText(String msg) { 133 cnt++; 134 text = msg; 135 } 136 137 } 138 139 } 140 | Popular Tags |