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-2002 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.openide.loaders;

import java.io.PrintStream;
import javax.swing.Action;
import junit.textui.TestRunner;
import org.netbeans.junit.*;
import org.openide.DialogDescriptor;
import org.openide.cookies.EditorCookie;
import org.openide.cookies.OpenCookie;
import org.openide.filesystems.*;
import org.openide.util.actions.SystemAction;

/** DefaultDataObject is supposed to have open operation that shows the text
 * editor or invokes a dialog with questions.
 *
 * @author  Jaroslav Tulach
 */
public final class DefautDataObjectHasOpenTest extends NbTestCase {
    
    private FileSystem lfs;
    private DataObject obj;
    
    /** Creates a new instance of DefaultSettingsContextTest */
    public DefautDataObjectHasOpenTest(String name) {
        super(name);
    }
    
    public static void main(String[] args) {
        TestRunner.run(new NbTestSuite(DefautDataObjectHasOpenTest.class));
        System.exit (0);
    }
    
    protected void setUp() throws java.lang.Exception {
        System.setProperty("org.openide.util.Lookup", "org.openide.loaders.DefautDataObjectHasOpenTest$Lkp");
        super.setUp();
        
        String fsstruct [] = new String [] {
            "AA/a.test"
        };
        
        TestUtilHid.destroyLocalFileSystem(getName());
        lfs = TestUtilHid.createLocalFileSystem(getName(), fsstruct);
        
        FileObject fo = lfs.findResource("AA/a.test");
        assertNotNull("file not found", fo);
        obj = DataObject.find(fo);
        
        assertEquals ("The right class", obj.getClass (), DefaultDataObject.class);

        DD.options = null;
    }
    
    protected void tearDown() throws java.lang.Exception {
        super.tearDown();
    }
    
    public void testHasOpenCookie () {
        assertNotNull (obj.getCookie (OpenCookie.class));
    }
    
    public void testHasEditorCookieForResonableContentOfFiles () throws Exception {
        EditorCookie c = tryToOpen (
            "Ahoj Jardo," +
            "how are you" +
            "\t\n\rBye.\u00a9\u00e1\u00e9\u00ed\u00f3\u00fa"
        );
        assertNotNull (c);
        
        doRegularCheck (c);
    }
    
    private void doRegularCheck (EditorCookie c) throws Exception {
        assertEquals (
            "Next questions results in the same cookie", 
            c, 
            obj.getCookie(EditorCookie.class)
        );
        assertEquals (
            "Print cookie is provided",
            c,
            obj.getCookie(org.openide.cookies.PrintCookie.class)
        );
        assertEquals (
            "CloseCookie as well",
            c,
            obj.getCookie(org.openide.cookies.CloseCookie.class)
        );
        
        OpenCookie open = (OpenCookie)obj.getCookie (OpenCookie.class);
        open.open ();
        
        javax.swing.text.Document d = c.getDocument();
        assertNotNull (d);
        
        d.insertString(0, "Kuk", null);
        
        assertNotNull (
            "Now there is a save cookie", 
            obj.getCookie (org.openide.cookies.SaveCookie.class)
        );
    }
    
    public void testNoEditorCookieForBinaryFiles () throws Exception {
        EditorCookie c = tryToOpen ("\u0003\u0001\u0000");
        assertNull (c);
    }
    
    public void testOpenAsksAQuestionForBinaryFiles () throws Exception {
        EditorCookie c = tryToOpen ("\u0003\u0001\u0000");
        assertNull (c);
        
        DD.toReturn = DialogDescriptor.CANCEL_OPTION;
        OpenCookie open = (OpenCookie)obj.getCookie (OpenCookie.class);
        open.open ();
        
        assertNotNull ("There was a query", DD.options);
        assertEquals ("Yes no options", 2, DD.options.length);
        assertEquals (DialogDescriptor.OK_OPTION, DD.options[0]);
        assertEquals (DialogDescriptor.CANCEL_OPTION, DD.options[1]);
    }
    
    public void testOpenActionIsAlwaysFirst () throws Exception {
        org.openide.nodes.Node n = obj.getNodeDelegate();
        
        assertEquals (
            "Open action is the default one", 
            org.openide.actions.OpenAction.get (org.openide.actions.OpenAction.class),
            n.getPreferredAction()
        );
        
        Action[] actions = n.getActions (false);
        assertTrue ("There are some actions", actions.length > 1);
        
        assertEquals (
            "First one is open",
            org.openide.actions.OpenAction.get (org.openide.actions.OpenAction.class),
            actions[0]
        );
        
        assertNull (
            "Then there is a separator",
            actions[1]
        );
    }

    public void testBinaryFilesQuestionContainsOpenActions () throws Exception {
        EditorCookie c = tryToOpen ("\u0003\u0001\u0000");
        assertNull (c);
        
        DD.toReturn = DialogDescriptor.CLOSED_OPTION;
        OpenCookie open = (OpenCookie)obj.getCookie (OpenCookie.class);
        open.open ();

        assertNotNull ("There was a query", DD.options);
        assertEquals ("Two options", 2, DD.options.length);
        assertEquals (DialogDescriptor.OK_OPTION, DD.options[0]);
        assertEquals (DialogDescriptor.CANCEL_OPTION, DD.options[1]);
        DD.options = null;

        DD.toReturn = DialogDescriptor.OK_OPTION;
        open.open ();

        assertNotNull ("There was a query", DD.options);
        assertEquals ("Still 2 options", 2, DD.options.length);
        assertEquals (DialogDescriptor.OK_OPTION, DD.options[0]);
        assertEquals (DialogDescriptor.CANCEL_OPTION, DD.options[1]);

        c = (EditorCookie)obj.getCookie (EditorCookie.class);
        assertNotNull ("Editor cookie created", c);
        
        doRegularCheck (c);
    }
    
    private EditorCookie tryToOpen (String content) throws Exception {
        FileObject fo = obj.getPrimaryFile();
        FileLock lock = fo.lock();
        PrintStream os = new PrintStream (fo.getOutputStream(lock));
        os.print (content);
        os.close ();
        lock.releaseLock();
        
        return (EditorCookie)obj.getCookie (EditorCookie.class);
    }
    
    public void testThatTheNameOfDataObjectsNodeContainsTheExtensionIssue18780 () throws Exception {
        String name = obj.getPrimaryFile ().getNameExt ();
        assertEquals ("Name contains extension", name, obj.getNodeDelegate ().getName ());
    }
    
    //
    // Our fake lookup
    //
    public static final 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 DD ());
        }
    }

    /** Our own dialog displayer.
     */
    private static final class DD extends org.openide.DialogDisplayer {
        public static Object[] options;
        public static Object toReturn;
        
        public java.awt.Dialog createDialog(org.openide.DialogDescriptor descriptor) {
            throw new IllegalStateException ("Not implemented");
        }
        
        public Object notify(org.openide.NotifyDescriptor descriptor) {
            assertNull (options);
            assertNotNull (toReturn);
            options = descriptor.getOptions();
            Object r = toReturn;
            toReturn = null;
            return r;
        }
        
    } // end of DD


}
... 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.