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

import java.io.PrintStream;
import java.lang.reflect.Modifier;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import junit.textui.TestRunner;
import org.netbeans.jmi.javamodel.*;
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;

/**
 * @author  Pavel Flaska
 */
public class ThrowsClauseTest extends NbTestCase {
    
    JavaClass clazz;
    JavaModelPackage pkg;
    JavaMetamodel model;
    
    private String resultSource = 
        "doTest: Found in resource 'java/lang/Object.java'." +
        "Method 'wait' found." +
        "throws:" +
        "'java.lang.InterruptedException'";
    private String resultClass = 
        "doTest: Found in resource 'java/lang/Object.class'." +
        "Method 'wait' found." +
        "throws:" +
        "'java.lang.InterruptedException'";
    
    /** Creates a new instance of StringMethodsSigTest */
    public ThrowsClauseTest() {
        super("ThrowsClauseTest");
    }

    protected void setUp() {
        clazz = (JavaClass) Utility.findClass("examples.colorpicker.ColorPicker");
        pkg = (JavaModelPackage) clazz.refImmediatePackage();
        model = JavaMetamodel.getManager();
    }
    
    public static NbTestSuite suite() {
        NbTestSuite suite = new NbTestSuite(ThrowsClauseTest.class);
        return suite;
    }

    /**
     * Tries to find String source file in src.zip, resolves it and check if
     * correct equals and hashCode methods are resolved.
     */
    public void testThrowsFromSources() {
        boolean fail = true;
        Utility.beginTrans(true);
        try {
            model.setClassPath(model.getFileObject(clazz.getResource()), true);
            doTest(resultSource);
            fail = false;
        }
        finally {
            Utility.endTrans(fail);
        }
    }
    
    /**
     * Tries to find String class in rt.jar, resolves it and check if
     * correct equals and hashCode methods are resolved.
     */
    public void testThrowsFromClasses() {
        boolean fail = true;
        Utility.beginTrans(true);
        try {
            model.setClassPath(model.getFileObject(clazz.getResource()));
            doTest(resultClass);
            fail = false;
        }
        finally {
            Utility.endTrans(fail);
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        TestRunner.run(suite());
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // PRIVATE METHODS
    ////////////////////////////////////////////////////////////////////////////
    private void doTest(String result) {
        StringBuffer buf = new StringBuffer(487);
        JavaClass strType = (JavaClass) model.getDefaultExtent().getType().resolve("java.lang.Object");
        Resource res = strType.getResource();
        buf.append("doTest: Found in resource '" + res.getName() + "'.");
        Method method = strType.getMethod("wait", Collections.EMPTY_LIST, false);
        assertNotNull(method);
        buf.append("Method '" + method.getName() + "' found.");
        buf.append("throws:");
        for (Iterator it = method.getExceptions().iterator(); it.hasNext(); ) {
            buf.append("'" + ((JavaClass) it.next()).getName() + "'");
        }
        getLog().println(buf);
        assertEquals("Error.", buf.toString(), result);
    }
    
}
... 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.