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.jmi.javamodel.usages;

import java.io.PrintStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import junit.textui.TestRunner;
import org.netbeans.jmi.javamodel.JavaClass;
import org.netbeans.jmi.javamodel.codegen.Utility;
import org.netbeans.junit.NbTestCase;
import org.netbeans.junit.NbTestSuite;


/**
 * @author Martin Matula
 */
public class FindSubTypesTest extends NbTestCase {
    
    public static final String[] SUBTYPES_ABSTRACT_COLLECTION = {
        "java.util.HashMap.Values", 
        "java.util.AbstractSet", 
        "java.util.IdentityHashMap.Values", 
        "java.util.Hashtable.ValueCollection",
        "java.util.WeakHashMap.Values",
        "java.util.AbstractList"
    };
    
    public static final String[] SUBTYPES_ABSTRACT_COLLECTION_TRANSITIVE = {
        "java.util.Vector",
        "java.util.HashMap.KeySet",
        "java.util.WeakHashMap.Values",
        "java.util.Collections.SingletonList",
        "java.util.HashMap.EntrySet",
        "java.util.ArrayList",
        "java.util.TreeSet",
        "java.util.WeakHashMap.EntrySet",
        "java.util.WeakHashMap.KeySet",
        "java.util.Hashtable.EntrySet",
        "java.util.AbstractList",
        "java.util.IdentityHashMap.Values",
        "java.util.Hashtable.ValueCollection",
        "java.util.Collections.EmptySet",
        "java.util.IdentityHashMap.EntrySet",
        "java.util.SubList",
        "java.util.TreeMap.SubMap.EntrySetView",
        "java.util.RandomAccessSubList",
        "java.util.IdentityHashMap.KeySet",
        "java.util.Collections.SingletonSet",
        "java.util.AbstractSequentialList",
        "java.util.Hashtable.KeySet",
        "java.util.AbstractSet",
        "java.util.Arrays.ArrayList",
        "java.util.HashSet",
        "java.util.Collections.EmptyList",
        "java.util.Collections.CopiesList",
        "java.util.HashMap.Values",
        "java.util.Stack",
        "java.util.LinkedList",
        "org.netbeans.test.codegen.Generics",
        "java.util.LinkedHashSet"
    };
    
    public static final String[] SUBTYPES_LIST = {
        "java.util.ArrayList",
        "java.util.Vector",
        "java.util.LinkedList",
        "java.util.Collections.SynchronizedList",
        "java.util.Collections.UnmodifiableList",
        "java.util.AbstractList",
        "org.netbeans.test.codegen.Generics",
        "org.netbeans.test.codegen.InterfacePositionTest"
    };
    
    public static final String[] SUBTYPES_LIST_TRANSITIVE = {
        "java.util.Vector",
        "java.util.Collections.UnmodifiableList",
        "java.util.Collections.SingletonList",
        "java.util.ArrayList",
        "java.util.Collections.SynchronizedRandomAccessList",
        "java.util.AbstractList",
        "java.util.SubList",
        "java.util.RandomAccessSubList",
        "java.util.AbstractSequentialList",
        "java.util.Collections.SynchronizedList",
        "java.util.Arrays.ArrayList",
        "java.util.Collections.EmptyList",
        "java.util.Collections.CopiesList",
        "java.util.Stack",
        "java.util.LinkedList",
        "java.util.Collections.UnmodifiableRandomAccessList",
        "org.netbeans.test.codegen.Generics",
        "org.netbeans.test.codegen.InterfacePositionTest"
    };
    
    public static final String[] SUBCLASSES_ABSTRACT_COLLECTION = {
        "java.util.HashMap.Values",
        "java.util.AbstractSet",
        "java.util.IdentityHashMap.Values",
        "java.util.Hashtable.ValueCollection",
        "java.util.WeakHashMap.Values",
        "java.util.AbstractList"
    };
    
    public static final String[] SUBCLASSES_LIST = {};
    public static final String[] IMPLEMENTORS_ABSTRACT_COLLECTION = {};
    public static final String[] IMPLEMENTORS_LIST = {
        "java.util.ArrayList",
        "java.util.Vector",
        "java.util.LinkedList",
        "java.util.Collections.SynchronizedList",
        "java.util.Collections.UnmodifiableList",
        "java.util.AbstractList",
        "org.netbeans.test.codegen.Generics",
        "org.netbeans.test.codegen.InterfacePositionTest"
    };
    
    private JavaClass abstractCol;
    private JavaClass list;
    
    /** Need to be defined because of JUnit */
    public FindSubTypesTest(String name) {
        super(name);
    }
    
    public static NbTestSuite suite() {
        return new NbTestSuite(FindSubTypesTest.class);
    }
    
    /** Use for execution inside IDE */
    public static void main(java.lang.String[] args) {
        TestRunner.run(suite());
    }
    
    public void setUp() {
        abstractCol = Utility.findClass("java.util.AbstractCollection");
        list = Utility.findClass("java.util.List");
    }
    
    public void testFindAbstractCollectionSubTypes() {
        compare(abstractCol.findSubTypes(false), SUBTYPES_ABSTRACT_COLLECTION);
    }

    public void testFindAbstractCollectionSubTypesTrans() {
        compare(abstractCol.findSubTypes(true), SUBTYPES_ABSTRACT_COLLECTION_TRANSITIVE);
    }

    public void testFindListSubTypes() {
        compare(list.findSubTypes(false), SUBTYPES_LIST);
    }

    public void testFindListSubTypesTrans() {
        compare(list.findSubTypes(true), SUBTYPES_LIST_TRANSITIVE);
    }
    
    public void testFindAbstractCollectionSubClasses() {
        compare(abstractCol.getSubClasses(), SUBCLASSES_ABSTRACT_COLLECTION);
    }

    public void testFindListSubClasses() {
        compare(list.getSubClasses(), SUBCLASSES_LIST);
    }
    
    public void testFindAbstractCollectionImplementors() {
        compare(abstractCol.getImplementors(), IMPLEMENTORS_ABSTRACT_COLLECTION);
    }

    public void testFindListImplementors() {
        compare(list.getImplementors(), IMPLEMENTORS_LIST);
    }
    
    private void compare(Collection col, String[] classNames) {
        PrintStream log = getLog();
        HashSet names = new HashSet(Arrays.asList(classNames));
        
        boolean passed = true;
        for (Iterator it = col.iterator(); it.hasNext();) {
            String name = ((JavaClass) it.next()).getName();
            if (!names.remove(name) && (name.startsWith("java.") || name.startsWith("org.netbeans."))) {
                log.println("Unexpected type found: " + name);
                passed = false;
            }
        }
        
        if (!names.isEmpty()) {
            log.println();
            log.println("Missing types from java.** and org.netbeans.** packages:");
            passed = false;
            for (Iterator it = names.iterator(); it.hasNext();) {
                log.println(it.next());
            }
        }
        if (!passed) {
            fail("Test failed, see log file for details");
        }
    }
}
... 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.