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

package org.netbeans.core.modules;

//import junit.framework.TestCase;
import org.netbeans.junit.NbTestCase;
import java.io.*;
import java.util.*;
import java.beans.*;
import java.net.URL;
import java.util.jar.Manifest;
import org.openide.filesystems.*;
import org.openide.filesystems.FileSystem; // override java.io.FileSystem
import org.netbeans.core.NbTopManager;

/** Some infrastructure for module system tests.
 * @author Jesse Glick
 */
abstract class SetupHid extends NbTestCase {
    
    public SetupHid(String name) {
        super(name);
    }
    
    /** directory full of JAR files to test */
    protected File jars;
    
    protected void setUp() throws Exception {
        java.util.Locale.setDefault (java.util.Locale.US);
        jars = new File(ModuleManagerTest.class.getResource("jars").getFile());
    }
    
    protected static void deleteRec(File f) throws IOException {
        if (f.isDirectory()) {
            File[] kids = f.listFiles();
            if (kids == null) throw new IOException("Could not list: " + f);
            for (int i = 0; i < kids.length; i++) {
                deleteRec(kids[i]);
            }
        }
        if (! f.delete()) throw new IOException("Could not delete: " + f);
    }
    
    protected static void copyStreams(InputStream is, OutputStream os) throws IOException {
        byte[] buf = new byte[4096];
        try {
            int i;
            while ((i = is.read(buf)) != -1) {
                os.write(buf, 0, i);
            }
        } finally {
            is.close();
        }
    }
    
    protected static void copy(File a, File b) throws IOException {
        OutputStream os = new FileOutputStream(b);
        try {
            copyStreams(new FileInputStream(a), os);
        } finally {
            os.close();
        }
    }
    
    protected static void copy(File a, FileObject b) throws IOException {
        FileLock lock = b.lock();
        try {
            OutputStream os = b.getOutputStream(lock);
            try {
                copyStreams(new FileInputStream(a), os);
            } finally {
                os.close();
            }
        } finally {
            lock.releaseLock();
        }
    }
    
    protected static String slurp(String path) throws IOException {
        NbTopManager.get(); // #26451
        FileObject fo = Repository.getDefault().getDefaultFileSystem().findResource(path);
        if (fo == null) return null;
        InputStream is = fo.getInputStream();
        StringBuffer text = new StringBuffer((int)fo.getSize());
        byte[] buf = new byte[1024];
        int read;
        while ((read = is.read(buf)) != -1) {
            text.append(new String(buf, 0, read, "US-ASCII"));
        }
        return text.toString();
    }
    
    protected void addOpenide(ModuleManager mgr) throws Exception {
        ClassLoader l = SetupHid.class.getClassLoader();
        Enumeration e = l.getResources("META-INF/MANIFEST.MF");
        Manifest mani = null;
        while (e.hasMoreElements()) {
            URL u = (URL)e.nextElement();
            Manifest m = new Manifest(u.openStream());
            if ("org.openide/1".equals(m.getMainAttributes().getValue("OpenIDE-Module"))) {
                mani = m;
                break;
            }
        }
        if (mani == null) throw new IllegalStateException("openide.jar not in classpath?");
        mgr.enable(mgr.createFixed(mani, null, l));
    }
    
    protected static class FakeModuleInstaller extends ModuleInstaller {
        // For examining results of what happened:
        public final List actions = new ArrayList(); // List
        public final List args = new ArrayList(); // List
        public void clear() {
            actions.clear();
            args.clear();
        }
        // For adding invalid modules:
        public final Set delinquents = new HashSet(); // Set
        // For adding modules that don't want to close:
        public final Set wontclose = new HashSet(); // Set
        public void prepare(Module m) throws InvalidException {
            if (delinquents.contains(m)) throw new InvalidException(m, "not supposed to be installed");
            actions.add("prepare");
            args.add(m);
        }
        public void dispose(Module m) {
            actions.add("dispose");
            args.add(m);
        }
        public void load(List modules) {
            actions.add("load");
            args.add(new ArrayList(modules));
        }
        public void unload(List modules) {
            actions.add("unload");
            args.add(new ArrayList(modules));
        }
        public boolean closing(List modules) {
            actions.add("closing");
            args.add(new ArrayList(modules));
            Iterator it = modules.iterator();
            while (it.hasNext()) {
                if (wontclose.contains(it.next())) return false;
            }
            return true;
        }
        public void close(List modules) {
            actions.add("close");
            args.add(new ArrayList(modules));
        }
    }
    
    protected static final class FakeEvents extends Events {
        protected void logged(String message, Object[] args) {
            // do nothing
            // XXX is it better to test events or the installer??
        }
    }
    
    protected static final class LoggedPCListener implements PropertyChangeListener {
        private final Set changes = new HashSet(100); // Set
        public synchronized void propertyChange(PropertyChangeEvent evt) {
            ChangeFirer.Change c = new ChangeFirer.Change(evt.getSource(), evt.getPropertyName(), null, null/*evt.getOldValue(), evt.getNewValue()*/);
            //System.err.println("Got change: " + c);
            changes.add(c);
            notify();
        }
        public synchronized void waitForChanges() throws InterruptedException {
            wait(5000);
        }
        public synchronized boolean hasChange(Object source, String prop) {
            return changes.contains(new ChangeFirer.Change(source, prop, null, null));
        }
        public synchronized boolean waitForChange(Object source, String prop) throws InterruptedException {
            while (! hasChange(source, prop)) {
                long start = System.currentTimeMillis();
                waitForChanges();
                if (System.currentTimeMillis() - start > 4000) {
                    //System.err.println("changes=" + changes);
                    return false;
                }
            }
            return true;
        }
    }
    
    protected static class LoggedFileListener implements FileChangeListener {
        /** names of files that have changed: */
        private final Set files = new HashSet(100); // Set
        private synchronized void change(FileEvent ev) {
            files.add(ev.getFile().getPath());
            notify();
        }
        public synchronized void waitForChanges() throws InterruptedException {
            wait(5000);
        }
        public synchronized boolean hasChange(String fname) {
            return files.contains(fname);
        }
        public synchronized boolean waitForChange(String fname) throws InterruptedException {
            while (! hasChange(fname)) {
                long start = System.currentTimeMillis();
                waitForChanges();
                if (System.currentTimeMillis() - start > 4000) {
                    //System.err.println("changes=" + changes);
                    return false;
                }
            }
            return true;
        }
        public void fileDeleted(FileEvent fe) {
            change(fe);
        }
        public void fileFolderCreated(FileEvent fe) {
            change(fe);
        }
        public void fileDataCreated(FileEvent fe) {
            change(fe);
        }
        public void fileAttributeChanged(FileAttributeEvent fe) {
            // ignore?
        }
        public void fileRenamed(FileRenameEvent fe) {
            change(fe);
        }
        public void fileChanged(FileEvent fe) {
            change(fe);
        }
    }
    
}

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