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

package org.netbeans.core;

import junit.framework.AssertionFailedError;
import junit.textui.TestRunner;
import org.netbeans.junit.*;
import org.openide.filesystems.FileSystem;
import org.openide.filesystems.Repository;
import org.openide.nodes.Node;
import org.openide.loaders.DataObject;
import java.util.Enumeration;
import org.openide.loaders.DataFolder;
import java.awt.event.AWTEventListener;
import java.awt.Toolkit;
import java.awt.AWTEvent;
import java.awt.event.WindowEvent;
import java.awt.Window;
import java.awt.Frame;
import java.awt.Dialog;
import javax.swing.SwingUtilities;
import org.netbeans.core.NbTopManager;

/** Test Plain top manager. Must be run externally.
 * @author Jesse Glick
 */
public class PlainTest extends NbTestCase {
    
    public PlainTest(String name) {
        super(name);
    }
    
    public static void main(String[] args) {
        TestRunner.run(new NbTestSuite(PlainTest.class));
        // In case it fails to shut itself down afterwards:
        System.exit(0);
    }
    
    public void testStartPlain() throws Exception {
        assertTrue("Default TopManager is Plain", NbTopManager.get() instanceof Plain);
        // That's all folks!
    }
    
    /** Using Plain is not supposed to invoke the AWT Event Queue.
     * Well, it can (sometimes this happens when some GUI component
     * is created but never shown), but at least no window ought to
     * be opened.
     */
    public void testPlainShowsNoGUI() throws Exception {
        EnsureNoWindowsOpened l = new EnsureNoWindowsOpened();
        l.test("Initially");
        NbTopManager tm = NbTopManager.get();
        l.test("After getting TopManager");
        FileSystem sfs = Repository.getDefault().getDefaultFileSystem();
        DataFolder top = DataFolder.findFolder(sfs.getRoot());
        Enumeration e = top.children(true);
        while (e.hasMoreElements()) {
            DataObject o = (DataObject)e.nextElement();
            String prim = o.getPrimaryFile().getPath();
            if (prim.startsWith("Windows/Components/")) {
                // These load the window system while calculating display name,
                // and can actual pop up the component as a window.
                continue;
            }
            if (prim.endsWith(".ser")) {
                // extbrowser's *.ser throw ugly exceptions. Don't make test fail,
                // but clutter sysout of test and look confusing. Skip them.
                continue;
            }
            l.test("After encountering " + prim);
            Node n = o.getNodeDelegate();
            l.test("After creating the node for " + prim);
            String name = n.getDisplayName();
            l.test("After calculating the display name " + name + " for the node for " + prim);
        }
        l.test("After scanning SFS");
    }
    
    private final class EnsureNoWindowsOpened implements AWTEventListener {
        private String opened = null;
        public EnsureNoWindowsOpened() {
            Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.WINDOW_EVENT_MASK);
        }
        public void eventDispatched(AWTEvent event) {
            if (event.getID() == WindowEvent.WINDOW_OPENED && opened == null) {
                Window w = ((WindowEvent)event).getWindow();
                if (w instanceof Frame) {
                    opened = ((Frame)w).getTitle();
                } else if (w instanceof Dialog) {
                    opened = ((Dialog)w).getTitle();
                } else {
                    opened = w.toString();
                }
            }
        }
        public void test(String msg) throws Exception {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    // Great, do nothing.
                    // Maybe some events have been delivered now!
                }
            });
            if (opened != null) {
                assertTrue(msg + ": " + opened, false);
            }
        }
    }
    
}
... 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.