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.classindex;

import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import junit.framework.AssertionFailedError;
import junit.textui.TestRunner;
import org.netbeans.jmi.javamodel.JavaClass;
import org.netbeans.jmi.javamodel.JavaModelPackage;
import org.netbeans.jmi.javamodel.codegen.Utility;
import org.netbeans.junit.NbTestCase;
import org.netbeans.junit.NbTestSuite;
import org.netbeans.modules.javacore.ClassIndex;
import org.netbeans.modules.javacore.internalapi.JavaMetamodel;

/**
 * Tests class index. Basic tests for looking for classes in index.
 * Doesn't cover all functionality, only tests method getClassesBySimpleName()
 * and getClassesBySNPrefix() from the class index.
 *
 * @author  Pavel Flaska
 */
public class ClassIndexTest extends NbTestCase {
    private static final String NAME = "ClassIndexTest";
    private static final String PREFIX = "ClassIndex";
    
    JavaModelPackage model;
    ClassIndex index;
    
    /** Creates a new instance of ClassIndexTest */
    public ClassIndexTest() {
        super("ClassIndexTest");
    }
    
    public static NbTestSuite suite() {
        NbTestSuite suite = new NbTestSuite(ClassIndexTest.class);
        return suite;
    }

    protected void setUp() {
        JavaClass clazz = Utility.findClass("org.netbeans.test.classindex.index1." + NAME);
        model = (JavaModelPackage) clazz.refImmediatePackage();
        index = ClassIndex.getIndex(model);
    }

    public void testCaseSensitive() {
        Collection c = index.getClassesBySimpleName(NAME);
        printFound(c);
        if (c.size() != 1) {
            throw new AssertionFailedError("Found #" + c.size() + " classes instead of #1.");
        }
    }
    
    public void testCaseInsensitive() {
        Collection c = index.getClassesBySimpleName(NAME, false);
        printFound(c);
        if (c.size() != 3) {
            throw new AssertionFailedError("Found #" + c.size() + " classes instead of #3.");
        }
    }
    
    public void testPrefixCaseSensitive() {
        Collection c = index.getClassesBySNPrefix(PREFIX);
        printFound(c);
        if (c.size() != 2) {
            throw new AssertionFailedError("Found #" + c.size() + " classes instead of #2.");
        }
    }
    
    public void testPrefixCaseInsensitive() {
        Collection c = index.getClassesBySNPrefix(PREFIX, false);
        printFound(c);
        if (c.size() != 6) {
            throw new AssertionFailedError("Found #" + c.size() + " classes instead of #6.");
        }
    }
    
    private void printFound(Collection c) {
        for (Iterator it = c.iterator(); it.hasNext(); ) {
            getLog().println(it.next());
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        TestRunner.run(suite());
    }

}
... 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.