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.ant.freeform;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.netbeans.api.java.classpath.ClassPath;
import org.netbeans.api.java.classpath.GlobalPathRegistry;
import org.netbeans.spi.project.support.ant.EditableProperties;
import org.openide.filesystems.FileLock;
import org.openide.filesystems.FileObject;

// XXX testClasspathsOfBuildProducts
// - should have BOOT and EXECUTE
// XXX testBootClasspathChanges
// - check that after e.g. changing plain source level, new JDK picked
// XXX testSourcepathChanges
// - check that after adding/removing a source root to a given compilation unit, SOURCE changes to match
// (but can this really work generally? what if a single compilation unit is split in half??)
// XXX testIgnoredRoot
// - check that after removing a compilation unit, ClassPath.gCP returns null again, and the ClassPath object is invalidated
// XXX testRegisterDeregisterAfterChanges
// - check that after changing set of compilation units, new/removed classpaths are added to or removed from list in GPR
// XXX testNonexistentEntries
// - check that correct URL (ending in '/') picked for dirs (not JARs) in CP that do not yet exist
//   and that for nonexistent JARs ('.' in simple name) the correct jar: protocol URL is used

/**
 * Test functionality of classpath definitions in FreeformProject.
 * This class just tests the basic functionality found in the "simple" project.
 * @author Jesse Glick
 */
public class ClasspathsTest extends TestBase {

    public ClasspathsTest(String name) {
        super(name);
    }
    
    public void testSourcePath() throws Exception {
        ClassPath cp = ClassPath.getClassPath(myAppJava, ClassPath.SOURCE);
        assertNotNull("have some SOURCE classpath for src/", cp);
        FileObject[] roots = cp.getRoots();
        assertEquals("have one entry in " + cp, 1, roots.length);
        assertEquals("that is src/", simple.getProjectDirectory().getFileObject("src"), roots[0]);
        cp = ClassPath.getClassPath(specialTaskJava, ClassPath.SOURCE);
        assertNotNull("have some SOURCE classpath for antsrc/", cp);
        roots = cp.getRoots();
        assertEquals("have one entry", 1, roots.length);
        assertEquals("that is antsrc/", simple.getProjectDirectory().getFileObject("antsrc"), roots[0]);
        cp = ClassPath.getClassPath(buildProperties, ClassPath.SOURCE);
        assertNull("have no SOURCE classpath for build.properties", cp);
    }
    
    public void testCompileClasspath() throws Exception {
        ClassPath cp = ClassPath.getClassPath(myAppJava, ClassPath.COMPILE);
        assertNotNull("have some COMPILE classpath for src/", cp);
        assertEquals("have two entries in " + cp, 2, cp.entries().size());
        assertEquals("have two roots in " + cp, 2, cp.getRoots().length);
        assertNotNull("found WeakSet in " + cp, cp.findResource("org/openide/util/WeakSet.class"));
        assertNotNull("found NullInputStream", cp.findResource("org/openide/util/io/NullInputStream.class"));
        cp = ClassPath.getClassPath(specialTaskJava, ClassPath.COMPILE);
        assertNotNull("have some COMPILE classpath for antsrc/", cp);
        assertEquals("have one entry", 1, cp.getRoots().length);
        assertNotNull("found Task", cp.findResource("org/apache/tools/ant/Task.class"));
        cp = ClassPath.getClassPath(buildProperties, ClassPath.COMPILE);
        assertNull("have no COMPILE classpath for build.properties", cp);
    }
    
    public void testExecuteClasspath() throws Exception {
        // For now, just the same as COMPILE.
        // XXX should include built-to paths
        ClassPath cp = ClassPath.getClassPath(myAppJava, ClassPath.EXECUTE);
        assertNotNull("have some EXECUTE classpath for src/", cp);
        assertEquals("have two entries in " + cp, 2, cp.getRoots().length);
        assertNotNull("found WeakSet in " + cp, cp.findResource("org/openide/util/WeakSet.class"));
        assertNotNull("found NullInputStream", cp.findResource("org/openide/util/io/NullInputStream.class"));
        cp = ClassPath.getClassPath(specialTaskJava, ClassPath.EXECUTE);
        assertNotNull("have some EXECUTE classpath for antsrc/", cp);
        assertEquals("have one entry", 1, cp.getRoots().length);
        assertNotNull("found Task", cp.findResource("org/apache/tools/ant/Task.class"));
        cp = ClassPath.getClassPath(buildProperties, ClassPath.EXECUTE);
        assertNull("have no EXECUTE classpath for build.properties", cp);
    }
    
    public void testBootClasspath() throws Exception {
        ClassPath cp = ClassPath.getClassPath(myAppJava, ClassPath.BOOT);
        assertNotNull("have some BOOT classpath for src/", cp);
        assertEquals("and it is JDK 1.4", "1.4", specOfBootClasspath(cp));
        ClassPath cp2 = ClassPath.getClassPath(specialTaskJava, ClassPath.BOOT);
        assertNotNull("have some BOOT classpath for antsrc/", cp2);
        assertEquals("and it is JDK 1.4", "1.4", specOfBootClasspath(cp2));
        /* Not actually required:
        assertEquals("same BOOT classpath for all files (since use same spec level)", cp, cp2);
         */
        cp = ClassPath.getClassPath(buildProperties, ClassPath.BOOT);
        assertNull("have no BOOT classpath for build.properties", cp);
    }
    
    private static String specOfBootClasspath(ClassPath cp) {
        List/**/ entries = cp.entries();
        if (entries.size() != 1) {
            return null;
        }
        ClassPath.Entry entry = (ClassPath.Entry)entries.get(0);
        String u = entry.getURL().toExternalForm();
        // Cf. DummyJavaPlatformProvider.
        Pattern p = Pattern.compile("jar:file:/c:/java/([0-9.]+)/jre/lib/rt\\.jar!/");
        Matcher m = p.matcher(u);
        if (m.matches()) {
            return m.group(1);
        } else {
            return null;
        }
    }
    
    public void testGlobalPathRegistryUsage() throws Exception {
        Classpaths cp = (Classpaths)simple.getLookup().lookup(Classpaths.class);
        assertNotNull("have a Classpaths", cp);
        GlobalPathRegistry gpr = GlobalPathRegistry.getDefault();
        assertEquals("no BOOT classpaths yet", Collections.EMPTY_SET, gpr.getPaths(ClassPath.BOOT));
        assertEquals("no COMPILE classpaths yet", Collections.EMPTY_SET, gpr.getPaths(ClassPath.COMPILE));
        assertEquals("no EXECUTE classpaths yet", Collections.EMPTY_SET, gpr.getPaths(ClassPath.EXECUTE));
        assertEquals("no SOURCE classpaths yet", Collections.EMPTY_SET, gpr.getPaths(ClassPath.SOURCE));
        cp.opened();
        Set/**/ boot = gpr.getPaths(ClassPath.BOOT);
        Set/**/ compile = gpr.getPaths(ClassPath.COMPILE);
        Set/**/ execute = gpr.getPaths(ClassPath.EXECUTE);
        Set/**/ source = gpr.getPaths(ClassPath.SOURCE);
        Set/**/ expected = new HashSet();
        expected.add(ClassPath.getClassPath(myAppJava, ClassPath.BOOT));
        expected.add(ClassPath.getClassPath(specialTaskJava, ClassPath.BOOT));
        assertEquals("correct set of BOOT classpaths", expected, boot);
        expected = new HashSet();
        expected.add(ClassPath.getClassPath(myAppJava, ClassPath.COMPILE));
        expected.add(ClassPath.getClassPath(specialTaskJava, ClassPath.COMPILE));
        assertEquals("correct set of COMPILE classpaths", expected, compile);
        expected = new HashSet();
        expected.add(ClassPath.getClassPath(myAppJava, ClassPath.EXECUTE));
        expected.add(ClassPath.getClassPath(specialTaskJava, ClassPath.EXECUTE));
        assertEquals("correct set of EXECUTE classpaths", expected, execute);
        expected = new HashSet();
        expected.add(ClassPath.getClassPath(myAppJava, ClassPath.SOURCE));
        expected.add(ClassPath.getClassPath(specialTaskJava, ClassPath.SOURCE));
        assertEquals("correct set of SOURCE classpaths", expected, source);
        cp.closed();
        assertEquals("again no BOOT classpaths", Collections.EMPTY_SET, gpr.getPaths(ClassPath.BOOT));
        assertEquals("again no COMPILE classpaths", Collections.EMPTY_SET, gpr.getPaths(ClassPath.COMPILE));
        assertEquals("again no EXECUTE classpaths", Collections.EMPTY_SET, gpr.getPaths(ClassPath.EXECUTE));
        assertEquals("again no SOURCE classpaths", Collections.EMPTY_SET, gpr.getPaths(ClassPath.SOURCE));
    }
    
    public void testCompileClasspathChanges() throws Exception {
        clearWorkDir();
        FreeformProject simple2 = copyProject(simple);
        FileObject myAppJava2 = simple2.getProjectDirectory().getFileObject("src/org/foo/myapp/MyApp.java");
        assertNotNull("found MyApp.java", myAppJava2);
        ClassPath cp = ClassPath.getClassPath(myAppJava2, ClassPath.COMPILE);
        assertNotNull("have some COMPILE classpath for src/", cp);
        assertEquals("have two entries in " + cp, 2, cp.entries().size());
        assertEquals("have two roots in " + cp, 2, cp.getRoots().length);
        assertNotNull("found WeakSet in " + cp, cp.findResource("org/openide/util/WeakSet.class"));
        assertNotNull("found NullInputStream", cp.findResource("org/openide/util/io/NullInputStream.class"));
        TestPCL l = new TestPCL();
        cp.addPropertyChangeListener(l);
        EditableProperties props = new EditableProperties();
        FileObject buildProperties = simple2.getProjectDirectory().getFileObject("build.properties");
        assertNotNull("have build.properties", buildProperties);
        InputStream is = buildProperties.getInputStream();
        try {
            props.load(is);
        } finally {
            is.close();
        }
        assertEquals("right original src.cp", "${lib.dir}/lib1.jar:${lib.dir}/lib2.jar", props.getProperty("src.cp"));
        props.setProperty("src.cp", "${lib.dir}/lib1.jar");
        FileLock lock = buildProperties.lock();
        try {
            OutputStream os = buildProperties.getOutputStream(lock);
            try {
                props.store(os);
            } finally {
                os.close();
            }
        } finally {
            lock.releaseLock();
        }
        assertEquals("ROOTS fired", new HashSet(Arrays.asList(new String[] {ClassPath.PROP_ENTRIES, ClassPath.PROP_ROOTS})), l.changed);
        assertEquals("have one entry in " + cp, 1, cp.entries().size());
        assertEquals("have one root in " + cp, 1, cp.getRoots().length);
        assertNotNull("found WeakSet in " + cp, cp.findResource("org/openide/util/WeakSet.class"));
        assertNull("did not find NullInputStream", cp.findResource("org/openide/util/io/NullInputStream.class"));
    }
    
}
... 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.