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 class NbmsTest extends AbstractTestHid {
    public NbmsTest(String name) {
        super (name);
    }
    
    public static void main(java.lang.String[] args) {
        if (args.length == 1) {
            junit.textui.TestRunner.run(new NbmsTest (args[0]));
        } else {
            junit.textui.TestRunner.run(new NbTestSuite(NbmsTest.class));
        }
        System.exit (0);
    }    
    
    
    
    public void testNBMFileIsGenerated () throws Exception {
        File f = generateBasicNbm ("3.8");

        ModuleUpdate mu = ModuleUpdate.getModuleUpdate (f);
        download (mu);
        installNBM (Downloader.getNBM (mu));
        
        
        Map userFiles = findFiles (userDir);
        assertNotNull ("Contains x.jar", userFiles.get ("modules/x.jar"));
        assertNotNull ("Contains y.jar", userFiles.get ("modules/y.jar"));
        assertNotNull ("Contains update tracking", userFiles.get ("update_tracking/org-openide.xml"));
        
        assertEquals ("Empty platform dir", 0, findFiles (platformDir).size ());
    }

    public void testNBMFileIsInstalledWithNonCanonicalPaths () throws Exception {
        File f = generateBasicNbm ("3.8");

        ModuleUpdate mu = ModuleUpdate.getModuleUpdate (f);
        download (mu);
        
        File file = Downloader.getNBM (mu);
        File noncanonical = new File (
            new File (
                new File (file.getParentFile (), ".."), 
                file.getParentFile ().getName ()
            ),
            file.getName ()
        );
        assertFalse ("Files are different", noncanonical.equals (file));
        assertEquals ("But point to the same file", noncanonical.getCanonicalFile (), file.getCanonicalFile ());
        
        installNBM (noncanonical);
        
        
        Map userFiles = findFiles (userDir);
        assertNotNull ("Contains x.jar", userFiles.get ("modules/x.jar"));
        assertNotNull ("Contains y.jar", userFiles.get ("modules/y.jar"));
        assertNotNull ("Contains update tracking", userFiles.get ("update_tracking/org-openide.xml"));
        
        assertEquals ("Empty platform dir", 0, findFiles (platformDir).size ());
    }
    
    public void testIfNoUpdateTrackingThenItCannotBeInstalledGlobally () throws Exception {
        File f = generateBasicNbm ("3.8");

        ModuleUpdate mu = ModuleUpdate.getModuleUpdate (f);
        
        assertNull ("Cannot be installed globally", mu.findInstallDirectory ());
        assertTrue ("We should not be allowed to install", Downloader.bannedWriteToInstall (mu));
    }
    
    public void testIfUpdateTrackingExistsInstallIntoSharedDir () throws Exception {
        File f = generateBasicNbm ("3.8");
        
        File update = new File (platformDir, "update_tracking");
        update.mkdirs ();

        ModuleUpdate mu = ModuleUpdate.getModuleUpdate (f);
        
        assertEquals ("Install globally", platformDir, mu.findInstallDirectory ());
    }
    
    public void testUpdateTrackingExistsNbDirsAsWell () throws Exception {
        initClusters ();
        
        File f = generateBasicNbm ("3.8");
        
        File update = new File (platformDir, "update_tracking");
        update.mkdirs ();
        update = new File (clusterDir, "update_tracking");
        update.mkdirs ();
        update = new File (nextDir, "update_tracking");
        update.mkdirs ();

        ModuleUpdate mu = ModuleUpdate.getModuleUpdate (f);
        
        assertEquals (
            "If the NBM is not installed yet, then it goes to the first " +
            "netbeans.dir cluster", 
            clusterDir, mu.findInstallDirectory ()
        );
        
        download (mu);
        
        mu.setToInstallDir (true);
        assertTrue ("Moved to install cluster", Downloader.tryMove (mu));
        File movedNbm = Downloader.getMovedNBM (mu);
        installNBM (movedNbm);
        
        
        assertEquals ("Empty user dir", 0, findFiles (userDir).size ());
        assertEquals ("Empty platform dir", 0, findFiles (platformDir).size ());
        
        Map platFiles = findFiles (clusterDir);
        assertNotNull ("Contains x.jar", platFiles.get ("modules/x.jar"));
        assertNotNull ("Contains y.jar", platFiles.get ("modules/y.jar"));
        assertNotNull ("Contains update tracking", platFiles.get ("update_tracking/org-openide.xml"));
        
    }

    public void testCheckWeCannotInstallIntoUserDirIfWeContainSomethingInstalledInCore () throws Exception {
        doTestOfCannotInstallToUserDirForPrefix ("core");
    }
    
    public void testCheckWeCannotInstallIntoUserDirIfWeContainSomethingInstalledInLib () throws Exception {
        doTestOfCannotInstallToUserDirForPrefix ("lib");
    }
        
    private void doTestOfCannotInstallToUserDirForPrefix (String prefix) throws Exception {
        File update = new File (platformDir, prefix);
        update.mkdirs ();
        File f = new File (update, "openide.jar");
        f.createNewFile ();
        
                
        f = generateBasicNbm (
            "3.9", // new version
            new String[] {
                "netbeans/" + prefix + "/openide.jar"
            }
        ); 
        ModuleUpdate mu = ModuleUpdate.getModuleUpdate (f);
        download (mu);
        assertEquals (
            "No update_tracking, so we cannot install globally", 
            null, mu.findInstallDirectory ()
        );

        assertTrue ("By default we need to install to install dir", mu.isToInstallDir ());
        mu.setToInstallDir (false);
        assertTrue ("We cannot change this", mu.isToInstallDir ());
        
    }
    
    public void testInstallUpdate () throws Exception {
        
        File update = new File (platformDir, "update_tracking");
        update.mkdirs ();
        
        File f = generateBasicNbm ("3.8");
        ModuleUpdate mu = ModuleUpdate.getModuleUpdate (f);
        download (mu);
        
        assertEquals (
            "We do not have any clusters, we go into platformDir", 
            platformDir, mu.findInstallDirectory ()
        );
        
        mu.setToInstallDir (true);
        
        assertTrue ("Moved to install cluster", Downloader.tryMove (mu));
        File movedNbm = Downloader.getMovedNBM (mu);
        installNBM (movedNbm);
        
        Map platFiles = findFiles (platformDir);
        assertNotNull ("Contains x.jar", platFiles.get ("modules/x.jar"));
        assertNotNull ("Contains y.jar", platFiles.get ("modules/y.jar"));
        assertNotNull ("Contains update tracking", platFiles.get ("update_tracking/org-openide.xml"));
        
        assertEquals ("Empty platform dir", 0, findFiles (userDir).size ());
        
        
        update = new File (clusterDir, "update_tracking");
        update.mkdirs ();
        update = new File (nextDir, "update_tracking");
        update.mkdirs ();
        initClusters ();
        
        f = generateBasicNbm (
            "3.9", // new version
            new String[] {
                "netbeans/modules/kuk.jar"
            }
        ); 
        mu = ModuleUpdate.getModuleUpdate (f);
        download (mu);
        mu.setToInstallDir (true);
        
        
        File id = mu.findInstallDirectory ();
        assertEquals (
            "If the NBM is installed it will offer override", platformDir, id
        );
        
        assertTrue ("Moved to platform cluster", Downloader.tryMove (mu));
        movedNbm = Downloader.getMovedNBM (mu);
        installNBM (movedNbm);
        
        platFiles = findFiles (platformDir);
        assertNull ("No x.jar anymore", platFiles.get ("modules/x.jar"));
        assertNull ("No y.jar anymore", platFiles.get ("modules/y.jar"));
        assertNotNull ("Contains update tracking", platFiles.get ("update_tracking/org-openide.xml"));
        assertNotNull ("kuk.jar is there", platFiles.get ("modules/kuk.jar"));
        
        assertEquals ("Empty userdir still", 0, findFiles (userDir).size ());
        
        org.netbeans.updater.UpdateTracking ut;
        ut = org.netbeans.updater.UpdateTracking.getTracking (platformDir, false);
        String cb = mu.getInfoCodenamebase ();
        assertTrue ("Installed", ut.isModuleInstalled (cb));
            
    }
    
    public void testInstallLaterXMLCreatedInSharedDirIssue47077 () throws Exception {
        initClusters ();
        
        File f = generateBasicNbm ("3.8");
        
        File update = new File (platformDir, "update_tracking");
        update.mkdirs ();
        update = new File (clusterDir, "update_tracking");
        update.mkdirs ();
        update = new File (nextDir, "update_tracking");
        update.mkdirs ();

        ModuleUpdate mu = ModuleUpdate.getModuleUpdate (f);
        download (mu);
        mu.setToInstallDir (true);
        assertTrue ("Moved to platform cluster", Downloader.tryMove (mu));
        File movedNbm = Downloader.getMovedNBM (mu);
        
        PreparedModules.getPrepared().addModule( mu );
        PreparedModules.getPrepared ().write ();
        
        assertEquals ("Empty user dir", 0, findFiles (userDir).size ());
        assertEquals ("Empty platform dir", 0, findFiles (platformDir).size ());
        
        Map platFiles = findFiles (clusterDir);
        
        assertNotNull ("Contains install_later.xml", platFiles.get ("update/download/install_later.xml"));
        
        if (platFiles.size () != 2) {
            fail ("Unexcepted files (else than nbm and install_later.xml): " + platFiles);
        }
    }

    public void testInstallLaterXMLCreatedIssue47077 () throws Exception {
        initClusters ();
        
        File f = generateBasicNbm ("3.8");
        
        File update = new File (platformDir, "update_tracking");
        update.mkdirs ();
        update = new File (clusterDir, "update_tracking");
        update.mkdirs ();
        update = new File (nextDir, "update_tracking");
        update.mkdirs ();

        ModuleUpdate mu = ModuleUpdate.getModuleUpdate (f);
        
        assertEquals (
            "If the NBM is not installed yet, then it goes to the first " +
            "netbeans.dir cluster", 
            clusterDir, mu.findInstallDirectory ()
        );
        
        download (mu);
        
        PreparedModules.getPrepared().addModule( mu );
        PreparedModules.getPrepared ().write ();
        
        assertEquals ("Empty cluster dir", 0, findFiles (clusterDir).size ());
        assertEquals ("Empty platform dir", 0, findFiles (platformDir).size ());
        
        Map platFiles = findFiles (userDir);
        
        assertNotNull ("Contains install_later.xml", platFiles.get ("update/download/install_later.xml"));
        
        if (platFiles.size () != 2) {
            fail ("Unexcepted files (else than nbm and install_later.xml): " + platFiles);
        }
    }

    //
    // Utilities
    //
    
    /** Generates sample basic NBM.
     * @param revision a number like 4.15
     */
    private File generateBasicNbm (String revision) throws Exception {
        return generateBasicNbm (revision, new String[] { 
            "netbeans/modules/x.jar", 
            "netbeans/modules/y.jar",            
        });
    }
    
    /** Generates sample basic NBM.
     * @param revision a number like 4.15
     * @param list of files that should be in the NBM
     */
    private File generateBasicNbm (String revision, String[] fileList) throws Exception {
        String manifest =
            "\n" +
            "\n" +
            "\n" +
            "\n" +
            "\n" +
            "\n";

        File f = generateNBM (fileList, manifest);
        
        return f;
    }
    
}
... 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.