alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.openide.actions;

import junit.textui.TestRunner;

import org.openide.filesystems.*;
import org.openide.util.Lookup;
import java.io.IOException;
import java.util.*;
import org.netbeans.junit.*;
import java.beans.PropertyChangeListener;
import javax.swing.Action;

/**
 * Tests SaveAction.
 * @author Jaroslav Tulach
 */
public class SaveActionTest extends NbTestCase {
    
    public SaveActionTest (String name) {
        super(name);
    }
    
    public static void main(String[] args) {
        TestRunner.run(new NbTestSuite(SaveActionTest.class));
    }
    
    protected void setUp() throws Exception {
        System.setProperty ("org.openide.util.Lookup", "org.openide.actions.SaveActionTest$Lkp");
        assertNotNull ("MyDisplayer is used", Lookup.getDefault ().lookup (MyStatusDisplayer.class));
    }
    /*
    protected void tearDown() throws Exception {
    }
     */
    
    protected boolean runInEQ () {
        return true;
    }
    
    /** @see "issue #36616" */
    public void testSaveActionTakesNameOfDataNodeIfAvailable() throws Exception {
        try {
            FileSystem lfs = TestUtilHid.createLocalFileSystem(getName(), new String[] {
                "folder/file.simple",
            });
            FileObject fo = lfs.findResource("folder/file.simple");
            assertNotNull(fo);
            final org.openide.loaders.DataObject obj = org.openide.loaders.DataObject.find (fo);

            SaveAction sa = (SaveAction)SaveAction.get (SaveAction.class);
            
            class MyNode extends org.openide.nodes.FilterNode 
            implements org.openide.cookies.SaveCookie {
                public int cnt;
                
                public MyNode () {
                    super (obj.getNodeDelegate ());
                    disableDelegation (
                        org.openide.nodes.FilterNode.DELEGATE_GET_NAME |
                        org.openide.nodes.FilterNode.DELEGATE_GET_DISPLAY_NAME |
                        org.openide.nodes.FilterNode.DELEGATE_GET_SHORT_DESCRIPTION |
                        org.openide.nodes.FilterNode.DELEGATE_SET_NAME |
                        org.openide.nodes.FilterNode.DELEGATE_SET_DISPLAY_NAME |
                        org.openide.nodes.FilterNode.DELEGATE_SET_SHORT_DESCRIPTION
                    );
                    
                    setName ("my name");
                }
                
                public org.openide.nodes.Node.Cookie getCookie (Class c) {
                    if (c.isInstance (this)) {
                        return this;
                    }
                    return super.getCookie (c);
                }
                
                public void save () {
                    cnt++;
                }
            }
            
            MyNode myNode = new MyNode ();
            Action clone = sa.createContextAwareInstance (org.openide.util.lookup.Lookups.singleton (myNode));
            
            clone.actionPerformed (new java.awt.event.ActionEvent (this, 0, "waitFinished"));
            
            assertEquals ("Save called", 1, myNode.cnt);
            assertEquals ("One msgs", 1, MyStatusDisplayer.cnt);
            if (MyStatusDisplayer.text.indexOf ("file.simple") < 0) {
                fail ("Wrong message: " + MyStatusDisplayer.text);
            }
        } finally {
            TestUtilHid.destroyLocalFileSystem(getName());
        }
    }
    
    public static class Lkp extends org.openide.util.lookup.AbstractLookup {
        public Lkp () {
            this (new org.openide.util.lookup.InstanceContent ());
        }
        
        private Lkp (org.openide.util.lookup.InstanceContent ic) {
            super (ic);
            ic.add (new MyStatusDisplayer ());
            ic.add (new org.openide.loaders.DataLoaderPool () {
                public Enumeration loaders () {
                    return org.openide.util.enum.EmptyEnumeration.EMPTY;
                }
            });
        }
    }

    public static class MyStatusDisplayer extends org.openide.awt.StatusDisplayer {
        public static int cnt;
        public static String text;
        
        public void addChangeListener (javax.swing.event.ChangeListener l) {
        }
        
        public String getStatusText () {
            return text;
        }
        
        public void removeChangeListener (javax.swing.event.ChangeListener l) {
        }
        
        public void setStatusText (String msg) {
            cnt++;
            text = msg;
        }
        
    }
    
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.