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.nodes;

import java.beans.*;
import java.util.*;

import junit.textui.TestRunner;
import org.netbeans.junit.NbTestCase;
import org.netbeans.junit.NbTestSuite;

import org.openide.nodes.*;

/** Test Children.Array.
 * @author Jesse Glick
 */
public class ChildrenArrayTest extends NbTestCase {
    
    public ChildrenArrayTest(String name) {
        super(name);
    }
    
    public static void main(String[] args) {
        TestRunner.run(new NbTestSuite(ChildrenArrayTest.class));
    }
    
    /** Tests that node membership events are fired even before getNodes is called.
     * @see "#24851"
     */
    public void testNodeEventsFiredAtOnce() {
        // Stage 1: call getNodes first, after addNodeListener.
        Children kids = new Children.Array();
        Node root = new AbstractNode(kids);
        WaitNL l = new WaitNL();
        root.addNodeListener(l);
        kids.getNodes();
        Node a = node("a");
        kids.add(new Node[] {a});
        assertTrue(l.didAdd(a));
        Node b = node("b");
        kids.add(new Node[] {b});
        assertTrue(l.didAdd(b));
        Node c = node("c");
        kids.add(new Node[] {c});
        assertTrue(l.didAdd(c));
        // Stage 2: call before addNodeListener.
        kids = new Children.Array();
        root = new AbstractNode(kids);
        kids.getNodes();
        l = new WaitNL();
        root.addNodeListener(l);
        a = node("a");
        kids.add(new Node[] {a});
        assertTrue(l.didAdd(a));
        b = node("b");
        kids.add(new Node[] {b});
        assertTrue(l.didAdd(b));
        c = node("c");
        kids.add(new Node[] {c});
        assertTrue(l.didAdd(c));
        // Stage 3: don't call getNodes explicitly. Events should not be fired      
        kids = new Children.Array();
        root = new AbstractNode(kids);
        l = new WaitNL();
        root.addNodeListener(l);
        a = node("a");
        kids.add(new Node[] {a});
        assertTrue("First node added w/o explicit getNodes() is notified", !l.didAdd(a));
        b = node("b");
        kids.add(new Node[] {b});
        assertFalse(l.didAdd(b));
        c = node("c");
        kids.add(new Node[] {c});
        assertFalse(l.didAdd(c));
    }
    
    public void testParentAssigned() {
        Children ch = new Children.Array();
        Node n1 = new AbstractNode( ch );
        Node n2 = new AbstractNode( Children.LEAF );
        
        ch.add( new Node[] { n2 } );
        
        assertEquals( "Parent is assigned", n1, n2.getParentNode() );
        
    }
    
    private static Node node(String name) {
        Node n = new AbstractNode(Children.LEAF);
        n.setName(name);
        return n;
    }
    
    
    
    /** Node listener that will tell you if it gets a change.
     */
    private static final class WaitNL implements NodeListener {
        
        private final Set added = new HashSet(); // Set
        private final Set removed = new HashSet(); // Set
        
        public WaitNL() {}
        
        public synchronized boolean didAdd(Node n) {
            if (added.contains(n)) return true;
            try {
                wait(1500);
            } catch (InterruptedException ie) {
                ie.printStackTrace();
            }
            return added.contains(n);
        }
        
        public synchronized Set getAdded() {
            return new HashSet(added);
        }
        public synchronized Set getRemoved() {
            return new HashSet(removed);
        }
        
        public synchronized void childrenAdded(NodeMemberEvent ev) {
            added.addAll(Arrays.asList(ev.getDelta()));
            notifyAll();
        }
        
        public synchronized void childrenRemoved(NodeMemberEvent ev) {
            removed.addAll(Arrays.asList(ev.getDelta()));
            notifyAll();
        }
        
        public void propertyChange(PropertyChangeEvent evt) {}
        public void childrenReordered(NodeReorderEvent ev) {}
        public void nodeDestroyed(NodeEvent ev) {}
        
    }
    
}
... 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.