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 org.netbeans.modules.autoupdate;

import java.lang.ref.*;
import java.util.*;

//import junit.framework.*;
import org.netbeans.junit.*;

import java.util.List;
import java.awt.Image;
import java.awt.datatransfer.Transferable;
import java.beans.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.jar.*;
import java.util.jar.Manifest;
import java.util.regex.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

//import org.openide.ErrorManager;



public abstract class AbstractTestHid extends NbTestCase {
    protected File userDir, platformDir, clusterDir, nextDir;
    
    public AbstractTestHid (String name) {
        super (name);
    }
    
    protected void setUp () throws Exception {
        System.setProperty("org.openide.util.Lookup", "-"); 
        
        super.setUp ();
        
        // clear directories first
        this.clearWorkDir();
        
        userDir = new File (getWorkDir(), "user");
        assertTrue (userDir.mkdirs ());
        platformDir = new File (getWorkDir(), "platform");
        assertTrue (platformDir.mkdirs ());
        clusterDir = new File (getWorkDir (), "clstr");
        assertTrue (clusterDir.mkdirs ());
        nextDir = new File (getWorkDir (), "nxtclstr");
        assertTrue (nextDir.mkdirs ());
        
        System.setProperty("netbeans.dirs", "");
        System.setProperty("netbeans.home", platformDir.toString ());
        assertEquals (platformDir.toString (), System.getProperty ("netbeans.home"));
        System.setProperty("netbeans.user", userDir.toString ());
        assertEquals (userDir.toString (), System.getProperty ("netbeans.user"));
    }
    
    protected final void initClusters () {
        System.setProperty(
            "netbeans.dirs", 
            clusterDir.toString() + File.pathSeparatorChar + nextDir
        );
    }
    
    //
    // Utilities
    //
    
    private final File createNewNBMFile () throws IOException {
        int i = 0;
        for (;;) {
            File f = new File (this.getWorkDir(), this.getName() + i++ + ".nbm");
            if (!f.exists ()) return f;
        }
    }
    
    protected final File generateNBM(String[] content, String info) throws IOException {
        File f = createNewNBMFile ();
        
        ZipOutputStream os = new ZipOutputStream (new FileOutputStream (f));
        
        for (int i = 0; i < content.length; i++) {
            os.putNextEntry(new ZipEntry (content[i]));
            os.closeEntry();
        }
        os.putNextEntry (new ZipEntry ("Info/info.xml"));
        os.write (info.getBytes());
        os.closeEntry ();
        os.close();
        
        return f;
    }
    
    protected final File generateNBM(java.util.jar.Manifest[] content, String info) throws IOException {
        File f = createNewNBMFile ();
        
        ZipOutputStream os = new ZipOutputStream (new FileOutputStream (f));
        
        for (int i = 0; i < content.length; i++) {
            ZipEntry entry = new ZipEntry (content[i].getMainAttributes ().getValue ("name"));
            os.putNextEntry(entry);
            java.io.ByteArrayOutputStream array = new java.io.ByteArrayOutputStream ();
            java.util.jar.JarOutputStream jar = new java.util.jar.JarOutputStream (array, content[i]);
            jar.close ();
            os.write (array.toByteArray ());
            os.closeEntry();
        }
        os.putNextEntry (new ZipEntry ("Info/info.xml"));
        os.write (info.getBytes());
        os.closeEntry ();
        os.close();
        
        return f;
    }
    
    protected final static void installNBM (File nbm) {
        installNBMs (new File[] { nbm });
    }
    
    protected final static void installNBMs (File[] nbms) {
        class L implements PropertyChangeListener {
            private PropertyChangeEvent ev;
            public synchronized void propertyChange (PropertyChangeEvent ev) {
                if ("FINISHED".equals (ev.getPropertyName())) {
                    this.ev = ev;
                    notify ();
                }
            }
            
            public PropertyChangeEvent waitForEvent () {
                while (ev == null) {
                    try {
                        wait ();
                    } catch (InterruptedException ex) {
                        fail ("No exceptions expected");
                    }
                }
                return ev;
            }
        }
        
        L listener = new L ();
        synchronized (listener) {
            org.netbeans.updater.UpdaterFrame.runFromIDE(nbms, listener);
            listener.waitForEvent ();
        }
    }
    
    /** Scans files in user dir
     * @return Map
     */
    protected final static Map findFiles (File dir) {
        HashMap m = new HashMap ();
        files (m, dir, null);
        return m;
    }
    
    private static void files (HashMap m, File dir, String prefix) {
        File[] files = dir.listFiles();
        for (int i = 0; i < files.length; i++) {
            String name = prefix == null ? files[i].getName () : prefix + "/" + files[i].getName();
            if (files[i].isDirectory()) {
                files (m, files[i], name);
            } else {
                m.put (name, files[i]);
            }
        }
    }
    
    /** Simulates download of the NBM file.
     */
    protected final static void download (ModuleUpdate nbm) {
        boolean res = Downloader.downloadFromLocal (nbm);
        assertTrue ("Download ok for " + nbm.getDistributionFile (), res);
    }
}
... 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.