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

package DataLoaderTests.DataObjectTest;

import java.awt.datatransfer.Transferable;

import junit.framework.*;
import java.io.*;
import java.util.Collection;
import java.util.Iterator;
import org.netbeans.junit.*;
import org.openide.cookies.ConnectionCookie;
import org.openide.loaders.*; 
import org.openide.filesystems.*;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.Lookup;
import org.openide.util.lookup.AbstractLookup;
import org.openide.util.lookup.InstanceContent;

/**
 *
 * @author  pzajac
 */
public class SomeTests extends NbTestCase {
    // src/DataLoader/DataObjectTest/data dataFolder
    private DataObject resources;    
    private FileObject resourcesFo;
    
    /** Creates a new instance of SomeTests */
    public SomeTests(String name) {
        super(name);
    }
    
        
    /** Testing connection Listener 
     */
    private static class ConListener implements  ConnectionCookie.Listener {
        private  ConnectionCookie.Event lastEvent;
        
        public void notify(org.openide.cookies.ConnectionCookie.Event ev) throws IllegalArgumentException, ClassCastException {
            System.out.println("event fired");
            lastEvent = ev;
        }
        
        public ConnectionCookie.Event getLastEvent () {
            return lastEvent;
        }
        /** erase last event */
        public void clear() {
            lastEvent = null;
        }
        
    }
    private  static class TestNode extends AbstractNode { 
        TestNode (Lookup lookup) {
            super(Children.LEAF,lookup);
        }
        
        public static TestNode createInstance() {
            InstanceContent ic = new InstanceContent();
            AbstractLookup lookup = new AbstractLookup(ic);
            ic.add (new ConListener());
            return new TestNode(lookup);
        }

    }
    /** Testing ConnectionCookie.Type implementation
     */
    
    private  static class ConnectionTypeA implements ConnectionCookie.Type {
        
        public Class getEventClass() {
            return ConnectionCookie.Event.class;
        }
        
        public boolean isPersistent() {
            return false;
        }
        
        public boolean overlaps(org.openide.cookies.ConnectionCookie.Type type) {
            System.out.println("overlaps: " + type.getClass());
            boolean retType = type instanceof ConnectionTypeA;
            System.out.println("overlaps: " + retType);
            return retType;
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    }

    
    
    
    protected void setUp() {
            //initializing ide
            

            String xtestData = System.getProperty("xtest.data");
            File dataDir = new File (xtestData,"DataObjectTest");
            assertTrue(dataDir.exists());
            resourcesFo = FileUtil.toFileObject(dataDir);
            assertNotNull(resourcesFo);
            resources = DataFolder.findFolder(resourcesFo);
            
    }

    public void testDataConnection() throws Exception {
        //ConnectionCookie.Type 
        // get any MultiFileObject
        System.out.println(resourcesFo);
         log(resourcesFo.getPath());
         MultiDataObject dobj = (MultiDataObject) DataObject.find(resourcesFo.getFileObject("SwingFormObject.java" )); 
         assertNotNull(dobj); 
         MultiDataObject.Entry entry = dobj.getPrimaryEntry(); 
         
         ConnectionTypeA connectionType = new ConnectionTypeA();
         
         ConnectionSupport conSupport = new ConnectionSupport(entry, new ConnectionCookie.Type[]{connectionType});
         TestNode node = TestNode.createInstance();
         conSupport.register(connectionType, node);
         ConnectionCookie.Event event = new ConnectionCookie.Event(node, connectionType);
         
         Collection registeredTypes = conSupport.getRegisteredTypes() ;
         System.out.println("registered types:" + registeredTypes.size());
         assertTrue("number reagistered types must be 1", registeredTypes.size() == 1);
         
         conSupport.fireEvent(event);
         ConListener listener = (ConListener)node.getCookie(ConnectionCookie.Listener.class);
         assertNotNull(listener);
         assertNotNull(listener.getLastEvent());
         assertTrue ("fired and listened evend is not fired",  listener.getLastEvent() == event); 
         
         // test unregister
         conSupport.unregister(connectionType, node);
         listener.clear();
         conSupport.fireEvent(event);
    }
    
    public static Test suite() {
        NbTestSuite suite = new NbTestSuite(SomeTests.class);
        return suite;
    }
    
    public void setFilter(Filter filter) {
        super.setFilter(filter);
    }
}
... 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.