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.
 */
/*
 * Signature1Test.java
 *
 * Created on January 15, 2004, 11:53 AM
 */

package org.netbeans.test.refactoring.encapsulate;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.swing.text.StyledDocument;
import junit.textui.TestRunner;
import org.netbeans.modules.refactoring.api.ChangeParameters;
import org.netbeans.modules.refactoring.api.ChangeParameters.ParameterInfo;
import org.netbeans.modules.refactoring.api.EncapsulateFieldRefactoring;
import org.netbeans.modules.refactoring.api.RefactoringSupport;
import org.netbeans.jmi.javamodel.CallableFeature;
import org.netbeans.jmi.javamodel.Field;
import org.netbeans.jmi.javamodel.JavaClass;
import org.netbeans.jmi.javamodel.JavaModelPackage;
import org.netbeans.jmi.javamodel.Method;
import org.netbeans.jmi.javamodel.Parameter;
import org.netbeans.junit.AssertionFailedErrorException;
import org.netbeans.junit.NbTestCase;
import org.netbeans.junit.NbTestSuite;
import org.netbeans.test.refactoring.Utility;
import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
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  Pavel Flaska
 */
public class EncapsulateTest extends NbTestCase {
    
    // filetable is filed by initializer - contain pairs of names for
    // comparing results - second index value 0 represents generated file,
    // second index value 1 represents golden file.
    static String[][] filetable;
    
    static {
        // init filetable
        final String[] filename = { 
            "Encapsulate",
        };
        final String prefix = "org/netbeans/test/encapsulate/";
        filetable = new String[filename.length][2];
        for (int i = 0; i < filename.length; i++) {
            filetable[i][0] = prefix + filename[i] + ".java";
            filetable[i][1] = filename[i] + ".pass";
        }
    };
    
    /** Creates a new instance of Signature1Test */
    public EncapsulateTest() {
        super("Encapsulate");
    }
    
    public static NbTestSuite suite() {
        NbTestSuite suite = new NbTestSuite(EncapsulateTest.class);
        return suite;
    }
    
    JavaClass clazz;
    String cbn;
    JavaModelPackage pkg;
    Field fielda;
    Field fieldb;
    Field fieldx;
    
    protected void setUp() {
        clazz = (JavaClass) Utility.findClass("org.netbeans.test.encapsulate.Encapsulate");
        pkg = (JavaModelPackage) clazz.refOutermostPackage();

        // get a method which signature will be changed
        fielda = clazz.getField("a",false);
        fieldb = clazz.getField("b",false);
        fieldx = clazz.getField("x",false);
    }
    
    public void testEncapsulateField() throws FileStateInvalidException, IOException {
        // initialize refactoring
        RefactoringSupport support = RefactoringSupport.getDefault();
        EncapsulateFieldRefactoring refactoring = new EncapsulateFieldRefactoring(fielda);
        refactoring.setParameters("getA", "setA", 0, 0);
        Collection result = new ArrayList();
        refactoring.prepare(result);
        support.doRefactoring(result, "encapsulate field a", null);
        JavaMetamodel.getUndoManager().saveAll();

        refactoring = new EncapsulateFieldRefactoring(fieldb);
        refactoring.setParameters("getB", "setB", 0, 0);
        result = new ArrayList();
        refactoring.prepare(result);
        support.doRefactoring(result, "encapsulate field b", null);
        JavaMetamodel.getUndoManager().saveAll();

        refactoring = new EncapsulateFieldRefactoring(fieldx);
        refactoring.setParameters("getX", "setX", 0, 0);
        result = new ArrayList();
        refactoring.prepare(result);
        support.doRefactoring(result, "encapsulate field x", null);
        JavaMetamodel.getUndoManager().saveAll();

        assertFiles();
    }
    
    public void assertFiles() throws FileStateInvalidException, IOException {
        // assert all affected files
        for (int i = 0; i < filetable.length; i++) {
            assertFile(Utility.getFile(getDataDir(), filetable[i][0]), getGoldenFile(filetable[i][1]), getWorkDir());
        }
    }
    
    /**
     * 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.