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.test.refactoring.rename;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.text.StyledDocument;
import junit.textui.TestRunner;
import org.netbeans.modules.refactoring.api.RefactoringSupport;
import org.netbeans.modules.refactoring.api.RenameRefactoring;
import org.netbeans.jmi.javamodel.*;
import org.netbeans.junit.AssertionFailedErrorException;
import org.netbeans.junit.NbTestCase;
import org.netbeans.junit.NbTestSuite;
import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
import org.netbeans.test.refactoring.Utility;
import org.openide.cookies.EditorCookie;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileStateInvalidException;
import org.openide.filesystems.Repository;
import org.openide.loaders.DataObject;

 
/**
 *
 * @author  Daniel Prusa
 */
public class RenameTest extends NbTestCase {
        
    private static final String CLASS_NAME = "org.netbeans.test.rename.MainClass";
    private static final String CLASS_NAME_2 = "org.netbeans.test.rename.over_methods.Interface";
    
    private static String[] resultFiles = {
        "RenamedClass",
        "SecondClass"        
    };
    
    private static String[] resultFiles_2 = {
        "BaseClass",
        "Extension_1",
        "Extension_2",
        "Interface",
        "Interface_2",
        "SubClass_1",
        "SubClass_2"
    };
    
    private String JAVA_EXT = "java";
    private String PASS_EXT = "pass";
    private String PATH_PREFIX = "org/netbeans/test/rename/";
    
    private static TypeClass typeProxy;
    private static JavaClass jc;

    
    /** Creates a new instance of Signature1Test */
    public RenameTest(String name) {
        super(name);
    }
    
    public static NbTestSuite suite() {
        NbTestSuite suite = new NbTestSuite();
        suite.addTest(new RenameTest("testRename"));
        suite.addTest(new RenameTest("testOverridingMethods"));

        jc = (JavaClass) Utility.findClass(CLASS_NAME);
        typeProxy = ((JavaModelPackage) jc.refOutermostPackage()).getType();

        return suite;
    }
    
    public void testRename() throws FileStateInvalidException, IOException {
        RefactoringSupport support = RefactoringSupport.getDefault();
        
        Field field = jc.getField("number", false);
        RenameRefactoring refactoring = new RenameRefactoring(field);
        refactoring.setParameters("renamedField");
        Collection result = new ArrayList();
        refactoring.prepare(result);
        support.doRefactoring(result, "rename field", null);
        JavaMetamodel.getUndoManager().saveAll();

        List argTypes = new ArrayList(2);
        argTypes.add(typeProxy.resolve("char"));
        argTypes.add(typeProxy.resolve("int"));
        jc = (JavaClass) typeProxy.resolve(CLASS_NAME);
        Method method = jc.getMethod("getValue", argTypes, false);
        refactoring = new RenameRefactoring(method);
        refactoring.setParameters("renamedMethod");
        result = new ArrayList();
        refactoring.prepare(result);
        support.doRefactoring(result, "rename method", null);
        JavaMetamodel.getUndoManager().saveAll();

        jc = (JavaClass) typeProxy.resolve(CLASS_NAME);
        refactoring = new RenameRefactoring(jc);
        refactoring.setParameters("RenamedClass");
        result = new ArrayList();
        refactoring.prepare(result);
        support.doRefactoring(result, "rename class", null);
        JavaMetamodel.getUndoManager().saveAll();

        // check modified files
        try {
            for (int x = 0; x < resultFiles.length; x++) {
                String fileName = PATH_PREFIX + resultFiles[x] + '.' + JAVA_EXT;
                String passName = resultFiles[x] + '.' + PASS_EXT;
                assertFile(Utility.getFile(getDataDir(), fileName), getGoldenFile(passName), getWorkDir());
            }
        } catch (FileStateInvalidException e) {
            fail(e.getMessage());
        } catch (IOException e) {
            fail(e.getMessage());
        }
    }
    
    public void testOverridingMethods() throws FileStateInvalidException, IOException {
        jc = (JavaClass) typeProxy.resolve(CLASS_NAME_2);
        RefactoringSupport support = RefactoringSupport.getDefault();
        
        List argTypes = new ArrayList(2);
        Type intType = typeProxy.resolve("int");
        argTypes.add(intType);
        argTypes.add(intType);
        Method method = jc.getMethod("generateNumber", argTypes, false);
        RenameRefactoring refactoring = new RenameRefactoring(method);
        refactoring.setParameters("calculateValue");
        Collection result = new ArrayList();
        refactoring.prepare(result);
        support.doRefactoring(result, "rename method", null);
        JavaMetamodel.getUndoManager().saveAll();

        // check modified files
        try {
            for (int x = 0; x < resultFiles_2.length; x++) {
                String fileName = PATH_PREFIX + "over_methods/" + resultFiles_2[x] + '.' + JAVA_EXT;
                String passName = resultFiles_2[x] + '.' + PASS_EXT;
                assertFile(Utility.getFile(getDataDir(), fileName), getGoldenFile(passName), getWorkDir());
            }
        } catch (FileStateInvalidException e) {
            fail(e.getMessage());
        } catch (IOException e) {
            fail(e.getMessage());
        }
    }
    
    public static String getAsString(String file) {
        String result;
        try {
            FileObject testFile = Repository.getDefault().findResource(file);
            DataObject dob = DataObject.find(testFile);
            
            EditorCookie ec = (EditorCookie) dob.getCookie(EditorCookie.class);
            StyledDocument doc = ec.openDocument();
            result = doc.getText(0, doc.getLength());
        } 
        catch (Exception e) {
            throw new AssertionFailedErrorException(e);
        }
        return result;
    }
    
    /**
     * Used for running test from inside the IDE by internal execution.
     *
     * @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.