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-2004 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.javacore.classpath;

import java.beans.PropertyChangeSupport;
import java.beans.PropertyVetoException;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import junit.framework.*;
import org.netbeans.junit.NbTestCase;
import org.netbeans.spi.java.classpath.ClassPathFactory;
import org.netbeans.spi.java.classpath.ClassPathImplementation;
import org.netbeans.spi.java.classpath.PathResourceImplementation;
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
import org.netbeans.api.java.classpath.ClassPath;
import org.netbeans.api.java.classpath.GlobalPathRegistry;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileSystem;
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.LocalFileSystem;
import org.openide.filesystems.Repository;


/**
 *
 * @author  tom
 */
public class MergedClassPathImplementationTest extends NbTestCase {
    
    private static final String ROOT = "cproot_";
    private static final int ROOT_COUNT = 6;
    private FileSystem fs;
    private FileObject[] roots;
    
    /** Creates a new instance of MergedClassPathImplementationTest */
    public MergedClassPathImplementationTest (String name) {
        super (name);
    }
    
    protected void setUp() throws Exception {
        super.setUp();
        this.clearWorkDir();
        File f = this.getWorkDir();
        fs = mountFs (f);
        FileObject wd = getWorkDirFileObject ();
        this.roots = new FileObject[ROOT_COUNT];
        for (int i=0; i< ROOT_COUNT; i++) {
            this.roots[i] = wd.createFolder(ROOT+i);
        }        
    }
    
    protected void tearDown() throws Exception {
        umountFs (fs);
        super.tearDown();
    }    
    
    
    public void testMergedClassPath () throws IOException {
        ClassPath cp_1 = ClassPathSupport.createClassPath (new FileObject[] {roots[0], roots[1]});
        assertNotNull ("ClassPathSupport.createClassPath() returned null",cp_1);
        ClassPath cp_2 = ClassPathSupport.createClassPath (new FileObject[] {roots[2], roots[3]});
        assertNotNull ("ClassPathSupport.createClassPath() returned null",cp_2);
        DynamicClassPath dympl = new DynamicClassPath();
        ClassPath cp_3 = ClassPathFactory.createClassPath(dympl);
        assertNotNull ("ClassPathSupport.createClassPath() returned null",cp_3);
        GlobalPathRegistry regs = GlobalPathRegistry.getDefault();
        regs.register(ClassPath.COMPILE, new ClassPath[] {cp_1, cp_3});
        ClassPath mcp = ClassPathFactory.createClassPath(MergedClassPathImplementation.getDefault());
        assertNotNull ("ClassPathSupport.createClassPath() returned null for MergedClassPathImplementation" ,mcp);
        List entries = mcp.entries();
        assertEquivalent (entries,new FileObject[] {roots[0],roots[1]});
        regs.register(ClassPath.COMPILE, new ClassPath[] {cp_2});
        entries = mcp.entries();
        assertEquivalent (entries,new FileObject[] {roots[0],roots[1],roots[2],roots[3]});
        regs.unregister(ClassPath.COMPILE, new ClassPath[] {cp_1});
        entries = mcp.entries();
        assertEquivalent (entries,new FileObject[] {roots[2],roots[3]});
        dympl.setRoots(new FileObject[]{roots[4],roots[5]});
        entries = mcp.entries();
        assertEquivalent (entries,new FileObject[] {roots[2],roots[3],roots[4],roots[5]});
        dympl.setRoots(new FileObject[]{});
        entries = mcp.entries();
        assertEquivalent (entries,new FileObject[] {roots[2],roots[3]});
        dympl.setRoots(new FileObject[]{roots[2],roots[3]});
        entries = mcp.entries();
        assertEquivalent (entries,new FileObject[] {roots[2],roots[3],roots[2],roots[3]});
        dympl.setRoots(new FileObject[]{});
        entries = mcp.entries();
        assertEquivalent (entries,new FileObject[] {roots[2],roots[3]});
    }
    
    private static FileSystem mountFs (File f) throws IOException, PropertyVetoException {
        File root = f;
        String parentName = null;
        while ((parentName=root.getParent())!=null) {
            root = new File (parentName);
        }
        LocalFileSystem fs = new LocalFileSystem ();
        fs.setRootDirectory(root);
        Repository.getDefault().addFileSystem(fs);
        return fs;
    }
    
    private static void umountFs (FileSystem fs) {
        assertNotNull("Can not umount null filesystem",fs);
        Repository.getDefault().removeFileSystem(fs);
    }
    
    private FileObject getWorkDirFileObject () throws IOException {
        FileObject[] fos = FileUtil.fromFile(this.getWorkDir());
        if (fos.length>0) {
            return fos[0];
        }
        else {
            throw new IllegalStateException ("Can not resolve FileObject for home dir");
        }
    }
    
    private static void assertEquivalent (List entries, FileObject[] roots) throws IOException {
        assertNotNull ("Entries are null",entries);
        assertNotNull ("Roots are null",roots);
        assertEquals("Wrong number of roots",entries.size(),roots.length);
        Collection eurls = new ArrayList ();
        for (Iterator it = entries.iterator(); it.hasNext(); ) {
            ClassPath.Entry entry = (ClassPath.Entry) it.next();
            URL url = entry.getURL();
            assertNotNull ("ClassPath.Entry.getURL() returned null",url);
            eurls.add (url);
        }
        for (int i=0; i
... 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.